Skip to content

Commit

Permalink
Merge pull request #20 from phraktle/perf-opt
Browse files Browse the repository at this point in the history
Unit test cleanup
  • Loading branch information
oschwald committed Jan 1, 2016
2 parents 8bf2c92 + a6ba96b commit a57bd1c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 128 deletions.
1 change: 1 addition & 0 deletions src/test/java/com/maxmind/db/DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ private static <T> void testTypeDecoding(Decoder.Type type, Map<T, byte[]> tests
*/
private static FileChannel getFileChannel(byte[] data) throws IOException {
File file = File.createTempFile(UUID.randomUUID().toString(), "tmp");
file.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
fc.write(ByteBuffer.wrap(data));
Expand Down
22 changes: 5 additions & 17 deletions src/test/java/com/maxmind/db/MultiThreadedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
Expand All @@ -27,12 +23,8 @@ public void multipleMmapOpens() throws InterruptedException,
ExecutionException {
Callable<JsonNode> task = new Callable<JsonNode>() {
@Override
public JsonNode call() throws IOException,
URISyntaxException {
URI file = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb")
.toURI();
final Reader reader = new Reader(new File(file));
public JsonNode call() throws IOException {
Reader reader = new Reader(ReaderTest.getFile("MaxMind-DB-test-decoder.mmdb"));
try {
return reader.get(InetAddress.getByName("::1.1.1.0"));
} finally {
Expand All @@ -46,9 +38,7 @@ public JsonNode call() throws IOException,
@Test
public void streamThreadTest() throws IOException, InterruptedException,
ExecutionException {
final Reader reader = new Reader(ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb")
.openStream());
Reader reader = new Reader(ReaderTest.getStream("MaxMind-DB-test-decoder.mmdb"));
try {
MultiThreadedTest.threadTest(reader);
} finally {
Expand All @@ -58,10 +48,8 @@ public void streamThreadTest() throws IOException, InterruptedException,

@Test
public void mmapThreadTest() throws IOException, InterruptedException,
ExecutionException, URISyntaxException {
URI file = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();
final Reader reader = new Reader(new File(file));
ExecutionException {
Reader reader = new Reader(ReaderTest.getFile("MaxMind-DB-test-decoder.mmdb"));
try {
MultiThreadedTest.threadTest(reader);
} finally {
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/com/maxmind/db/PointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import org.junit.Test;

Expand All @@ -15,10 +14,8 @@
public class PointerTest {
@SuppressWarnings("static-method")
@Test
public void testWithPointers() throws
IOException, URISyntaxException {
File file = new File(PointerTest.class.getResource(
"/maxmind-db/test-data/maps-with-pointers.raw").toURI());
public void testWithPointers() throws IOException {
File file = ReaderTest.getFile("maps-with-pointers.raw");
BufferHolder ptf = new BufferHolder(file, FileMode.MEMORY);
Decoder decoder = new Decoder(ptf.get(), 0);

Expand Down
152 changes: 46 additions & 106 deletions src/test/java/com/maxmind/db/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import java.io.InputStream;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -48,13 +46,11 @@ public void teardownReader() throws IOException {
}

@Test
public void test() throws IOException, URISyntaxException {
public void test() throws IOException {
for (long recordSize : new long[]{24, 28, 32}) {
for (int ipVersion : new int[]{4, 6}) {
URI file = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-ipv" + ipVersion
+ "-" + recordSize + ".mmdb").toURI();
Reader reader = new Reader(new File(file));
File file = getFile("MaxMind-DB-test-ipv" + ipVersion + "-" + recordSize + ".mmdb");
Reader reader = new Reader(file);
try {
this.testMetadata(reader, ipVersion, recordSize);
if (ipVersion == 4) {
Expand All @@ -70,28 +66,18 @@ public void test() throws IOException, URISyntaxException {
}

@Test
public void testNoIpV4SearchTreeFile() throws IOException,
URISyntaxException {
URI file = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb")
.toURI();

this.testReader = new Reader(new File(file));
public void testNoIpV4SearchTreeFile() throws IOException {
this.testReader = new Reader(getFile("MaxMind-DB-no-ipv4-search-tree.mmdb"));
this.testNoIpV4SearchTree(this.testReader);
}

@Test
public void testNoIpV4SearchTreeURL() throws IOException,
URISyntaxException {
InputStream stream = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb")
.openStream();
this.testReader = new Reader(stream);
public void testNoIpV4SearchTreeStream() throws IOException {
this.testReader = new Reader(getStream("MaxMind-DB-no-ipv4-search-tree.mmdb"));
this.testNoIpV4SearchTree(this.testReader);
}

private void testNoIpV4SearchTree(Reader reader) throws IOException,
URISyntaxException {
private void testNoIpV4SearchTree(Reader reader) throws IOException {

assertEquals("::0/64", reader.get(InetAddress.getByName("1.1.1.1"))
.textValue());
Expand All @@ -100,26 +86,18 @@ private void testNoIpV4SearchTree(Reader reader) throws IOException,
}

@Test
public void testDecodingTypesFile() throws URISyntaxException, IOException {
URI file = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();

this.testReader = new Reader(new File(file));
public void testDecodingTypesFile() throws IOException {
this.testReader = new Reader(getFile("MaxMind-DB-test-decoder.mmdb"));
this.testDecodingTypes(this.testReader);
}

@Test
public void testDecodingTypesURL() throws URISyntaxException, IOException {
InputStream stream = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb")
.openStream();

this.testReader = new Reader(stream);
public void testDecodingTypesStream() throws IOException {
this.testReader = new Reader(getStream("MaxMind-DB-test-decoder.mmdb"));
this.testDecodingTypes(this.testReader);
}

private void testDecodingTypes(Reader reader) throws URISyntaxException,
IOException {
private void testDecodingTypes(Reader reader) throws IOException {
JsonNode record = reader.get(InetAddress.getByName("::1.1.1.0"));

assertEquals(true, record.get("boolean").booleanValue());
Expand Down Expand Up @@ -163,26 +141,18 @@ private void testDecodingTypes(Reader reader) throws URISyntaxException,
}

@Test
public void testZerosFile() throws URISyntaxException, IOException {
URI file = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();

this.testReader = new Reader(new File(file));
public void testZerosFile() throws IOException {
this.testReader = new Reader(getFile("MaxMind-DB-test-decoder.mmdb"));
this.testZeros(this.testReader);
}

@Test
public void testZerosURL() throws URISyntaxException, IOException {
InputStream stream = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb")
.openStream();

this.testReader = new Reader(stream);
public void testZerosStream() throws IOException {
this.testReader = new Reader(getFile("MaxMind-DB-test-decoder.mmdb"));
this.testZeros(this.testReader);
}

private void testZeros(Reader reader) throws URISyntaxException,
IOException {
private void testZeros(Reader reader) throws IOException {
JsonNode record = reader.get(InetAddress.getByName("::"));

assertEquals(false, record.get("boolean").booleanValue());
Expand Down Expand Up @@ -210,29 +180,18 @@ private void testZeros(Reader reader) throws URISyntaxException,
public ExpectedException thrown = ExpectedException.none();

@Test
public void testBrokenDatabaseFile() throws URISyntaxException, IOException {
URI file = ReaderTest.class
.getResource(
"/maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb")
.toURI();

this.testReader = new Reader(new File(file));
public void testBrokenDatabaseFile() throws IOException {
this.testReader = new Reader(getFile("GeoIP2-City-Test-Broken-Double-Format.mmdb"));
this.testBrokenDatabase(this.testReader);
}

@Test
public void testBrokenDatabaseURL() throws URISyntaxException, IOException {
InputStream stream = ReaderTest.class
.getResource(
"/maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb")
.openStream();

this.testReader = new Reader(stream);
public void testBrokenDatabaseStream() throws IOException {
this.testReader = new Reader(getStream("GeoIP2-City-Test-Broken-Double-Format.mmdb"));
this.testBrokenDatabase(this.testReader);
}

private void testBrokenDatabase(Reader reader) throws URISyntaxException,
IOException {
private void testBrokenDatabase(Reader reader) throws IOException {

this.thrown.expect(InvalidDatabaseException.class);
this.thrown
Expand All @@ -242,31 +201,19 @@ private void testBrokenDatabase(Reader reader) throws URISyntaxException,
}

@Test
public void testBrokenSearchTreePointerFile() throws URISyntaxException,
IOException {
URI file = ReaderTest.class
.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
.toURI();

this.testReader = new Reader(new File(file));
public void testBrokenSearchTreePointerFile() throws IOException {
this.testReader = new Reader(getFile("MaxMind-DB-test-broken-pointers-24.mmdb"));
this.testBrokenSearchTreePointer(this.testReader);
}

@Test
public void testBrokenSearchTreePointerURL() throws URISyntaxException,
IOException {
InputStream stream = ReaderTest.class
.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
.openStream();

this.testReader = new Reader(stream);
public void testBrokenSearchTreePointerStream() throws IOException {
this.testReader = new Reader(getStream("MaxMind-DB-test-broken-pointers-24.mmdb"));
this.testBrokenSearchTreePointer(this.testReader);
}

private void testBrokenSearchTreePointer(Reader reader)
throws URISyntaxException, IOException {
throws IOException {

this.thrown.expect(InvalidDatabaseException.class);
this.thrown
Expand All @@ -276,31 +223,18 @@ private void testBrokenSearchTreePointer(Reader reader)
}

@Test
public void testBrokenDataPointerFile() throws IOException,
URISyntaxException {
URI file = ReaderTest.class
.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
.toURI();

this.testReader = new Reader(new File(file));
public void testBrokenDataPointerFile() throws IOException {
this.testReader = new Reader(getFile("MaxMind-DB-test-broken-pointers-24.mmdb"));
this.testBrokenDataPointer(this.testReader);
}

@Test
public void testBrokenDataPointerURL() throws IOException,
URISyntaxException {
InputStream stream = ReaderTest.class
.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
.openStream();

this.testReader = new Reader(stream);
public void testBrokenDataPointerStream() throws IOException {
this.testReader = new Reader(getStream("MaxMind-DB-test-broken-pointers-24.mmdb"));
this.testBrokenDataPointer(this.testReader);
}

private void testBrokenDataPointer(Reader reader) throws IOException,
URISyntaxException {
private void testBrokenDataPointer(Reader reader) throws IOException {

this.thrown.expect(InvalidDatabaseException.class);
this.thrown
Expand All @@ -310,11 +244,8 @@ private void testBrokenDataPointer(Reader reader) throws IOException,
}

@Test
public void testClosedReaderThrowsException() throws IOException,
URISyntaxException {
URI file = ReaderTest.class.getResource(
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();
Reader reader = new Reader(new File(file));
public void testClosedReaderThrowsException() throws IOException {
Reader reader = new Reader(getFile("MaxMind-DB-test-decoder.mmdb"));

this.thrown.expect(ClosedDatabaseException.class);
this.thrown.expectMessage("The MaxMind DB has been closed.");
Expand Down Expand Up @@ -349,7 +280,7 @@ private void testMetadata(Reader reader, int ipVersion, long recordSize) {
assertTrue(metadata.getBuildDate().compareTo(cal.getTime()) > 0);
}

private void testIpV4(Reader reader, URI file) throws IOException {
private void testIpV4(Reader reader, File file) throws IOException {

for (int i = 0; i <= 5; i++) {
String address = "1.1.1." + (int) Math.pow(2, i);
Expand Down Expand Up @@ -382,7 +313,7 @@ private void testIpV4(Reader reader, URI file) throws IOException {
}

// XXX - logic could be combined with above
private void testIpV6(Reader reader, URI file) throws IOException {
private void testIpV6(Reader reader, File file) throws IOException {
String[] subnets = new String[]{"::1:ffff:ffff", "::2:0:0",
"::2:0:40", "::2:0:50", "::2:0:58"};

Expand Down Expand Up @@ -416,4 +347,13 @@ private void testIpV6(Reader reader, URI file) throws IOException {
assertNull(reader.get(InetAddress.getByName(ip)));
}
}

static File getFile(String name) {
return new File(ReaderTest.class.getResource("/maxmind-db/test-data/" + name).getFile());
}

static InputStream getStream(String name) {
return ReaderTest.class.getResourceAsStream("/maxmind-db/test-data/" + name);
}

}

0 comments on commit a57bd1c

Please sign in to comment.