Skip to content

Commit

Permalink
Merge pull request #33 from emmanuel-keller/master
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
emmanuel-keller committed Oct 23, 2016
2 parents ca1b58d + 34ca395 commit 746c96c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ public class ExternalizorException extends RuntimeException {
super(message);
}

ExternalizorException(final String message, final Throwable e) {
super(message, e);
}

}
7 changes: 3 additions & 4 deletions src/test/java/com/qwazr/externalizor/BenchmarkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BenchmarkTest {

private final int TIME = 10;
private final int TIME = 2;

private static Workbook workbook;
private static Sheet sheet;
Expand Down Expand Up @@ -184,9 +184,8 @@ public <T> void benchmarkCompare(Callable<T> callNewObject, Class<T> clazz) thro
benchmark("Default Java - Compress", Duration.ofSeconds(TIME), callNewObject, BenchmarkTest::write,
BenchmarkTest::read);

final BenchResult compress2 =
benchmark("Externalizor - Compress", Duration.ofSeconds(TIME), callNewObject, ExternalizerTest::write,
bytes -> ExternalizerTest.read(bytes, clazz));
final BenchResult compress2 = benchmark("Externalizor - Compress", Duration.ofSeconds(TIME), callNewObject,
ExternalizerTest::writeCompressed, bytes -> ExternalizerTest.readCompressed(bytes, clazz));

System.out.println(compress1);
System.out.println(compress2);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/qwazr/externalizor/ComplexExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class ComplexExample extends SimpleLang implements Serializable {
final public SimpleCollection nullObject;
final public AbstractCollection abstractCollection;
final public HashMap<String, SimplePrimitive> mapObject;
final public NoEmptyConstructorSerial noEmptyConstructor;
final public NoEmptyConstructorSerial noEmptyConstructorNull;
final private SimpleTime timeObject;
transient String transientValue;

Expand All @@ -42,6 +44,9 @@ public ComplexExample() {
for (int i = 0; i < RandomUtils.nextInt(2, 5); i++)
mapObject.put(RandomStringUtils.randomAscii(5), new SimplePrimitive());

noEmptyConstructor = new NoEmptyConstructorSerial(RandomStringUtils.randomAscii(5));
noEmptyConstructorNull = null;

timeObject = new SimpleTime();

transientValue = RandomStringUtils.randomAscii(12);
Expand All @@ -66,6 +71,11 @@ public boolean equals(Object o) {
if (!Objects.deepEquals(mapObject, s.mapObject))
return false;

if (!Objects.equals(noEmptyConstructor, s.noEmptyConstructor))
return false;
if (!Objects.equals(noEmptyConstructorNull, s.noEmptyConstructorNull))
return false;

if (!Objects.equals(timeObject, s.timeObject))
return false;

Expand Down
51 changes: 45 additions & 6 deletions src/test/java/com/qwazr/externalizor/ExternalizerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.function.Function;

public class ExternalizerTest {

final static <T> byte[] write(final T object) {
final static <T> byte[] writeCompressed(final T object) {
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
Externalizor.serialize(object, bos);
return bos.toByteArray();
Expand All @@ -34,7 +35,7 @@ final static <T> byte[] write(final T object) {
}
}

final static <T> T read(final byte[] bytes, final Class<T> clazz) {
final static <T> T readCompressed(final byte[] bytes, final Class<T> clazz) {
try (final ByteArrayInputStream bis = new ByteArrayInputStream(bytes)) {
return Externalizor.deserialize(bis, clazz);
} catch (IOException | ReflectiveOperationException e) {
Expand Down Expand Up @@ -71,23 +72,61 @@ public void abstractCollectionTest() throws IOException, ClassNotFoundException

@Test
public void noEmptyConstructorTest() throws IOException, ClassNotFoundException {
classTest(new NoEmptyConstructor("Test"));
classTest(new NoEmptyConstructorSerial("Test"));
}

private <T> T classTest(T write) {
private void checkError(Object object, Function<ExternalizorException, Boolean> checker) {
try {
classTest(object);
Assert.fail("The exception is not thrown");
} catch (ExternalizorException e) {
Assert.assertTrue(checker.apply(e));
}
}

@Test
public void errorEmptyConstructorTest() throws IOException, ClassNotFoundException {
checkError(new NoEmptyConstructor("Test"), e -> e.getMessage().contains(NoEmptyConstructor.class.getName()));
}

@Test
public void errorEmptyConstructorAsFieldTest() throws IOException, ClassNotFoundException {
checkError(new NoEmptyConstructor.AsField(), e -> e.getMessage().contains(NoEmptyConstructor.class.getName()));
}

private <T> T classRawTest(T write) {
//Write
final byte[] byteArray = writeRaw(write);
Assert.assertNotNull(byteArray);

//Read
final T read = readRaw(byteArray, (Class<T>) write.getClass());
Assert.assertNotNull(read);

// Check equals
Assert.assertEquals(write, read);
return read;
}

private <T> T classCompressedTest(T write) {
//Write
final byte[] byteArray = write(write);
final byte[] byteArray = writeCompressed(write);
Assert.assertNotNull(byteArray);

//Read
final T read = read(byteArray, (Class<T>) write.getClass());
final T read = readCompressed(byteArray, (Class<T>) write.getClass());
Assert.assertNotNull(read);

// Check equals
Assert.assertEquals(write, read);
return read;
}

private <T> T classTest(T write) {
classRawTest(write);
return classCompressedTest(write);
}

@Test
public void simpleLangTest() {
classTest(new SimpleLang());
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/com/qwazr/externalizor/NoEmptyConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package com.qwazr.externalizor;

import java.io.Serializable;
import java.util.Objects;

public class NoEmptyConstructor implements Serializable {
public class NoEmptyConstructor {

private final String test;

Expand All @@ -34,4 +33,8 @@ public boolean equals(Object o) {
return o != null && Objects.equals(test, ((NoEmptyConstructor) o).test);
}

public static class AsField {

public NoEmptyConstructor empty;
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/qwazr/externalizor/NoEmptyConstructorSerial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2016 Emmanuel Keller / QWAZR
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qwazr.externalizor;

import java.io.Serializable;
import java.util.Objects;

public class NoEmptyConstructorSerial implements Serializable {

private final String test;

/**
* This class don't have an empty constructor
*/
public NoEmptyConstructorSerial(String test) {
this.test = test;
}

@Override
public boolean equals(Object o) {
return o != null && Objects.equals(test, ((NoEmptyConstructorSerial) o).test);
}

}

0 comments on commit 746c96c

Please sign in to comment.