com.conwet.silbops.model.AbstractMappingTest.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.model.AbstractMappingTest.java

Source

package com.conwet.silbops.model;

/*
 * #%L
 * SilboPS API
 * %%
 * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Before;
import org.junit.Test;

import com.conwet.silbops.model.basic.Attribute;
import com.conwet.silbops.model.basic.Type;
import com.conwet.silbops.model.basic.Value;

public class AbstractMappingTest<T extends AbstractMapping<T>> {

    private T instance;
    private JSONObject json;

    private Attribute attrib1;
    private Attribute attrib2;

    /**
     * Subclasses should override this method
     * @return a new instance of the Type under test
     */
    @SuppressWarnings("unchecked")
    protected T newInstance() {

        return (T) new MockMap();
    }

    @Before
    public void setUp() throws ParseException {

        String jsonString = "{\"symbol:str\":\"GOOG\", \"value:long\":18}";
        json = (JSONObject) new JSONParser().parse(jsonString);

        attrib1 = new Attribute("symbol", Type.STRING);
        attrib2 = new Attribute("value", Type.LONG);

        instance = newInstance();
        instance.attribute(attrib1, "GOOG").attribute(attrib2, 18L);
    }

    @Test
    public void shouldReturnThis() {

        assertThat(instance.getThis()).isEqualTo(instance);
    }

    @Test
    public void shouldReturnType() {

        assertThat(instance.getThisType()).isEqualTo(instance.getClass());
    }

    @Test
    public void shouldAddAttributeByName() {

        Attribute attr2 = new Attribute("money", Type.DOUBLE);

        instance.attribute(attr2.getName(), attr2.getType(), Value.valueOf(22.0D));

        assertThat(instance.getAttributes()).contains(attr2);
    }

    @Test
    public void shouldReturnAllAttributes() {

        assertThat(instance.getAttributes()).containsOnly(attrib1, attrib2);
    }

    @Test
    public void shouldReturnMappedValue() {

        assertThat(instance.getValue(attrib1).getString()).isEqualTo("GOOG");
        assertThat(instance.getValue(attrib2.getName(), attrib2.getType()).getLong()).isEqualTo(18L);
    }

    @Test
    public void shouldBeEqualToItself() {

        assertThat(instance).isEqualTo(instance);
    }

    @Test
    public void shouldBeEqualByContent() {

        T other = newInstance();
        other.attribute(attrib1, "GOOG").attribute(attrib2, 18L);

        assertThat(instance).isEqualTo(other);
        assertThat(instance.hashCode()).isEqualTo(other.hashCode());
    }

    @Test
    public void shouldntBeEqualToNull() {

        assertThat(instance.equals(null)).isFalse();
    }

    @Test
    public void shouldntBeEqualToOtherType() {

        Object other = mock(AbstractMapping.class);

        assertThat(instance.equals(other)).isFalse();
    }

    @Test
    public void shouldDifferByContent() {

        T other = newInstance();
        other.attribute("other", Type.LONG, 22L);

        assertThat(instance).isNotEqualTo(other);
    }

    @Test
    public void shouldToJSONString() {

        assertThat(instance.toJSONString()).isEqualTo(json.toJSONString());
    }

    @Test
    @SuppressWarnings({ "unchecked" })
    public void shouldJSONise() {

        assertThat(instance.toJSON()).isEqualTo(json);
        assertThat(AbstractMapping.fromJSON(instance.getThisType(), json)).isEqualTo(instance);
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldntAttributeAndValueWithDifferentType() {

        instance.attribute("other", Type.DOUBLE, 10L);
    }

    @SuppressWarnings("unchecked")
    @Test(expected = IllegalArgumentException.class)
    public void shouldntReThrowExceptionClass() {

        // abstract classes can't be instantiated
        AbstractMapping.fromJSON(AbstractMapping.class, null);
    }

    @Test
    public void shouldExternalize() throws IOException, ClassNotFoundException {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput output = new ObjectOutputStream(baos);

        output.writeObject(instance);
        output.close();

        ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
        T decoded = instance.getThisType().cast(input.readObject());

        assertThat(decoded).isEqualTo(instance);
    }

    @Test
    public void shouldOverwriteValue() throws Exception {

        T overWritten = newInstance();
        overWritten.attribute("symbol", Type.STRING, "GOOG").attribute("value", Type.LONG, 25L).attribute("value",
                Type.LONG, 18L);

        assertThat(overWritten).isEqualTo(instance);
    }

    // MockMap class to extend AbstractMapping and test it
    public static class MockMap extends AbstractMapping<MockMap> {

        @Override
        protected MockMap getThis() {

            return this;
        }

        @Override
        protected Class<MockMap> getThisType() {

            return MockMap.class;
        }
    }
}