Skip to content

Commit

Permalink
Refactored unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Jan 22, 2025
1 parent 8d84dac commit b42e0b8
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 254 deletions.
28 changes: 4 additions & 24 deletions pmml-model/src/test/java/org/dmg/pmml/VersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,21 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class VersionTest {

@Test
public void forNamespaceURI(){

try {
Version.forNamespaceURI("http://www.dmg.org/PMML-2_0");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> Version.forNamespaceURI("http://www.dmg.org/PMML-2_0"));

Version.forNamespaceURI("http://www.dmg.org/PMML-3_0");
Version.forNamespaceURI("http://www.dmg.org/PMML-3_2");
Version.forNamespaceURI("http://www.dmg.org/PMML-4_0");
Version.forNamespaceURI("http://www.dmg.org/PMML-4_3");
Version.forNamespaceURI("http://www.dmg.org/PMML-4_4");

try {
Version.forNamespaceURI("http://www.dmg.org/PMML-4_5");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}

try {
Version.forNamespaceURI("http://www.dmg.org/PMML-5_0");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> Version.forNamespaceURI("http://www.dmg.org/PMML-4_5"));
assertThrows(IllegalArgumentException.class, () -> Version.forNamespaceURI("http://www.dmg.org/PMML-5_0"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,16 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class FieldNameAdapterTest {

@Test
public void unmarshal(){
FieldNameAdapter adapter = new FieldNameAdapter();

try {
adapter.unmarshal(null);

fail();
} catch(NullPointerException npe){
// Ignored
} // End try

try {
adapter.unmarshal("");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(NullPointerException.class, () -> adapter.unmarshal(null));
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal(""));

assertNotNull(adapter.unmarshal("x"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class NonNegativeIntegerAdapterTest {

@Test
public void unmarshal(){
IntegerAdapter adapter = new NonNegativeIntegerAdapter();

try {
adapter.unmarshal("-1");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("-1"));

assertEquals((Integer)0, adapter.unmarshal("0"));
assertEquals((Integer)1, adapter.unmarshal("1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,16 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class NumberAdapterTest {

@Test
public void unmarshal(){
NumberAdapter adapter = new NumberAdapter();

try {
adapter.unmarshal("NaN");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}

try {
adapter.unmarshal("INF");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("NaN"));
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("INF"));

assertEquals(1, adapter.unmarshal("1"));
assertEquals(1d, adapter.unmarshal("1.0"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,16 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class PositiveIntegerAdapterTest {

@Test
public void unmarshal(){
IntegerAdapter adapter = new PositiveIntegerAdapter();

try {
adapter.unmarshal("-1");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}

try {
adapter.unmarshal("0");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("-1"));
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("0"));

assertEquals((Integer)1, adapter.unmarshal("1"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,20 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ProbabilityNumberAdapterTest {

@Test
public void unmarshal(){
NumberAdapter adapter = new ProbabilityNumberAdapter();

try {
adapter.unmarshal("-0.1");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("-0.1"));

assertEquals(-0d, adapter.unmarshal("-0.0"));
assertEquals(0d, adapter.unmarshal("0.0"));
assertEquals(1d, adapter.unmarshal("1.0"));

try {
adapter.unmarshal("1.1");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("1.1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class RealNumberAdapterTest {

Expand All @@ -18,13 +18,7 @@ public void unmarshal(){
assertEquals(Double.NEGATIVE_INFINITY, adapter.unmarshal("-INF"));
assertEquals(Double.POSITIVE_INFINITY, adapter.unmarshal("INF"));

try {
adapter.unmarshal("Infinity");

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> adapter.unmarshal("Infinity"));

assertEquals(1, adapter.unmarshal("1"));
assertEquals(1d, adapter.unmarshal("1.0"));
Expand Down
18 changes: 3 additions & 15 deletions pmml-model/src/test/java/org/dmg/pmml/tree/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class NodeTest {

Expand Down Expand Up @@ -61,22 +61,10 @@ public void jaxbClone() throws Exception {
assertEquals("2b", jaxbNode1.requireDefaultChild());

assertSame(True.INSTANCE, node1.requirePredicate(True.class));

try {
node1.requirePredicate(False.class);

fail();
} catch(UnsupportedElementException uee){
// Ignored
}
assertThrows(UnsupportedElementException.class, () -> node1.requirePredicate(False.class));

assertNotSame(True.INSTANCE, jaxbNode1.requirePredicate(True.class));

try {
jaxbNode1.requirePredicate(False.class);
} catch(UnsupportedElementException uee){
// Ignored
}
assertThrows(UnsupportedElementException.class, () -> jaxbNode1.requirePredicate(False.class));

List<Node> jaxbNodes = jaxbNode1.getNodes();

Expand Down
18 changes: 3 additions & 15 deletions pmml-model/src/test/java/org/jpmml/model/JavaSerializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class JavaSerializerTest extends SerializerTest {

Expand All @@ -31,13 +31,7 @@ public void nonLocatableClone() throws Exception {

assertTrue(pmml.hasLocator());

try {
clone(serializer, pmml);

fail();
} catch(NotSerializableException nse){
// Ignored
}
assertThrows(NotSerializableException.class, () -> clone(serializer, pmml));

pmml.accept(new LocatorNullifier());

Expand All @@ -56,13 +50,7 @@ public void locatableClone() throws Exception {

assertTrue(pmml.hasLocator());

try {
clone(serializer, pmml);

fail();
} catch(NotSerializableException nse){
// Ignored
}
assertThrows(NotSerializableException.class, () -> clone(serializer, pmml));

pmml.accept(new LocatorTransformer());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class PMMLOutputStreamTest {

@Test
public void marshal() throws Exception {
PMML pmml = new PMML();

try {
marshal(pmml, Version.XPMML);

fail();
} catch(IllegalArgumentException iae){
// Ignored
}
assertThrows(IllegalArgumentException.class, () -> marshal(pmml, Version.XPMML));

String string = marshal(pmml, Version.PMML_4_4);

Expand Down
Loading

0 comments on commit b42e0b8

Please sign in to comment.