Skip to content
Snippets Groups Projects
Commit 358c575f authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Bedre test-dekningsgrad (#1)

parent 764152f1
No related branches found
No related tags found
No related merge requests found
package simpleex.core;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
......@@ -16,6 +19,13 @@ public class LatLongTest {
testLatLong(LatLong.valueOf("63.0, 10.0"), 63.0, 10.0);
testLatLong(LatLong.valueOf("63.0, 10.0", ","), 63.0, 10.0);
testLatLong(LatLong.valueOf("63.0; 10.0", ";"), 63.0, 10.0);
try {
testLatLong(LatLong.valueOf("63.0; 10.0", ","), 63.0, 10.0);
Assert.fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException e) {
} catch (final Exception e) {
Assert.fail("Should throw IllegalArgumentException");
}
}
private void testLatLong(final LatLong latLong, final double lat, final double lon) {
......@@ -29,4 +39,33 @@ public class LatLongTest {
Assert.assertFalse(new LatLong(10.0, 63.0).equals(new LatLong(63.0, 10.0)));
Assert.assertFalse(new LatLong(10.0, 63.0).equals(null));
}
@Test
public void testHashCode() {
final Map<LatLong, String> map = new HashMap<>();
map.put(new LatLong(63.0, 10.0), "first");
map.put(new LatLong(63.0, 10.0), "second");
Assert.assertEquals(1, map.size());
Assert.assertEquals("second", map.get(new LatLong(63.0, 10.0)));
}
@Test
public void testDistance() {
final LatLong trd = new LatLong(63.4217137055, 10.4221522734);
final LatLong str = new LatLong(63.0339713594, 10.2946225585);
checkDistance(trd.distance(trd), 0.0, 10.0);
checkDistance(str.distance(str), 0.0, 10.0);
checkDistance(trd.distance(str), 43000.0, 45000.0);
checkDistance(str.distance(trd), 43000.0, 45000.0);
checkDistance(LatLong.distance(trd, trd), 0.0, 10.0);
checkDistance(LatLong.distance(str, str), 0.0, 10.0);
checkDistance(LatLong.distance(trd, str), 43000.0, 45000.0);
checkDistance(LatLong.distance(str, trd), 43000.0, 45000.0);
}
private void checkDistance(final double d, final double lower, final double upper) {
Assert.assertTrue(d + " isn't between " + lower + " and " + upper, d <= upper && d >= lower);
}
}
package simpleex.core;
import java.util.Arrays;
import java.util.Iterator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -17,4 +20,45 @@ public class LatLongsTest {
public void testEmptyConstructor() {
Assert.assertEquals(0, latLongs.getLatLongCount());
}
private static void checkLatLongs(final LatLongs latLongs1, final LatLong... latLongs2) {
Assert.assertEquals(latLongs2.length, latLongs1.getLatLongCount());
final Iterator<LatLong> it = latLongs1.iterator();
for (int i = 0; i < latLongs2.length; i++) {
Assert.assertTrue(it.hasNext());
Assert.assertEquals(latLongs2[i], latLongs1.getLatLong(i));
Assert.assertEquals(latLongs2[i], it.next());
}
Assert.assertFalse(it.hasNext());
}
@Test
public void testConstructor() {
checkLatLongs(new LatLongs(63.0, 10.3, 63.1, 10.2), new LatLong(63.0, 10.3), new LatLong(63.1, 10.2));
checkLatLongs(new LatLongs(new LatLong(63.0, 10.3), new LatLong(63.1, 10.2)), new LatLong(63.0, 10.3), new LatLong(63.1, 10.2));
checkLatLongs(new LatLongs(Arrays.asList(new LatLong(63.1, 10.2), new LatLong(63.1, 10.1))), new LatLong(63.1, 10.2), new LatLong(63.1, 10.1));
}
@Test
public void testAddLatLong() {
latLongs.addLatLong(new LatLong(63.0, 10.3));
Assert.assertEquals(1, latLongs.getLatLongCount());
latLongs.addLatLong(new LatLong(63.1, 10.2));
Assert.assertEquals(2, latLongs.getLatLongCount());
}
@Test
public void testAddLatLongs() {
final LatLongs lls1 = new LatLongs();
lls1.addLatLongs(63.0, 10.3, 63.1, 10.2);
checkLatLongs(lls1, new LatLong(63.0, 10.3), new LatLong(63.1, 10.2));
final LatLongs lls2 = new LatLongs();
lls2.addLatLongs(new LatLong(63.0, 10.3), new LatLong(63.1, 10.2));
checkLatLongs(lls2, new LatLong(63.0, 10.3), new LatLong(63.1, 10.2));
final LatLongs lls3 = new LatLongs();
lls3.addLatLongs(Arrays.asList(new LatLong(63.1, 10.2), new LatLong(63.1, 10.1)));
checkLatLongs(lls3, new LatLong(63.1, 10.2), new LatLong(63.1, 10.1));
}
}
......@@ -27,7 +27,12 @@ public class LatLongJsonTest extends AbstractJsonTest {
}
@Test
public void testLatLongDeserialization() throws Exception {
public void testLatLongObjectDeserialization() throws Exception {
Assert.assertEquals(new LatLong(63.1, 12.3), readValue("{\"latitude\":63.1,\"longitude\":12.3}", LatLong.class));
}
@Test
public void testLatLongArrayDeserialization() throws Exception {
Assert.assertEquals(new LatLong(63.1, 12.3), readValue("[ 63.1, 12.3 ]", LatLong.class));
}
}
package simpleex.json;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import simpleex.core.LatLong;
import simpleex.core.LatLongs;
public class LatLongsJsonTest extends AbstractJsonTest {
@Before
@Override
public void setUp() {
super.setUp();
}
@Override
protected ObjectMapper createObjectMapper() {
final SimpleModule module = createSimpleModule(LatLong.class,new LatLongSerializer(), new LatLongDeserializer());
module.addSerializer(LatLongs.class, new LatLongsSerializer());
module.addDeserializer(LatLongs.class, new LatLongsDeserializer());
return new ObjectMapper().registerModule(module);
}
@Test
public void testLatLongsSerialization() throws Exception {
assertWriteValue("[{\"latitude\":63.1,\"longitude\":12.3},{\"latitude\":63.0,\"longitude\":12.4}]", new LatLongs(new LatLong(63.1, 12.3), new LatLong(63.0, 12.4)));
}
@Test
public void testLatLongsDeserialization() throws Exception {
final LatLongs latLongs = readValue("[{\"latitude\":63.1,\"longitude\":12.3}, [63.0,12.4]]", LatLongs.class);
Assert.assertEquals(2, latLongs.getLatLongCount());
Assert.assertEquals(new LatLong(63.1, 12.3), latLongs.getLatLong(0));
Assert.assertEquals(new LatLong(63.0, 12.4), latLongs.getLatLong(1));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment