Skip to content

Commit

Permalink
PLANNER-450 Improve test coverage for XStreamBendableScoreConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Sep 17, 2015
1 parent b13fa3b commit a4f6ed2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
Expand Up @@ -18,22 +18,22 @@


import java.io.Serializable; import java.io.Serializable;


import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.annotations.XStreamConverter;
import org.junit.Test; import org.junit.Test;
import org.optaplanner.core.api.score.buildin.bendable.BendableScore; import org.optaplanner.core.api.score.buildin.bendable.BendableScore;
import org.optaplanner.core.impl.testdata.util.PlannerTestUtils; import org.optaplanner.core.impl.testdata.util.PlannerTestUtils;
import org.optaplanner.persistence.xstream.impl.score.XStreamBendableScoreConverter;


import static org.junit.Assert.*; import static org.junit.Assert.*;


public class XStreamBendableScoreConverterTest { public class XStreamBendableScoreConverterTest {


@Test @Test
public void serializeAndDeserializeWithNullField() { public void serializeAndDeserializeWithNullField() {
XStreamBendableScoreConverterTestObject input = new XStreamBendableScoreConverterTestObject(null); TestXStreamObject input = new TestXStreamObject(null);
PlannerTestUtils.serializeAndDeserializeWithAll(input, PlannerTestUtils.serializeAndDeserializeWithAll(input,
new PlannerTestUtils.OutputAsserter<XStreamBendableScoreConverterTestObject>() { new PlannerTestUtils.OutputAsserter<TestXStreamObject>() {
public void assertOutput(XStreamBendableScoreConverterTestObject output) { public void assertOutput(TestXStreamObject output) {
assertEquals(null, output.getScore()); assertEquals(null, output.getScore());
} }
} }
Expand All @@ -42,11 +42,13 @@ public void assertOutput(XStreamBendableScoreConverterTestObject output) {


@Test @Test
public void serializeAndDeserialize() { public void serializeAndDeserialize() {
XStreamBendableScoreConverterTestObject input = new XStreamBendableScoreConverterTestObject( TestXStreamObject input = new TestXStreamObject(
BendableScore.valueOf(new int[]{-5}, new int[]{-300, -4000})); BendableScore.valueOf(new int[]{-5}, new int[]{-300, -4000}));
XStreamScoreConverterTest.assertXStreamXml(
"<TestXStreamObject>\\s*<score>-5/-300/-4000</score>\\s*</TestXStreamObject>", input);
PlannerTestUtils.serializeAndDeserializeWithAll(input, PlannerTestUtils.serializeAndDeserializeWithAll(input,
new PlannerTestUtils.OutputAsserter<XStreamBendableScoreConverterTestObject>() { new PlannerTestUtils.OutputAsserter<TestXStreamObject>() {
public void assertOutput(XStreamBendableScoreConverterTestObject output) { public void assertOutput(TestXStreamObject output) {
BendableScore score = output.getScore(); BendableScore score = output.getScore();
assertEquals(1, score.getHardLevelsSize()); assertEquals(1, score.getHardLevelsSize());
assertEquals(-5, score.getHardScore(0)); assertEquals(-5, score.getHardScore(0));
Expand All @@ -58,12 +60,13 @@ public void assertOutput(XStreamBendableScoreConverterTestObject output) {
); );
} }


public static class XStreamBendableScoreConverterTestObject implements Serializable { @XStreamAlias("TestXStreamObject")
public static class TestXStreamObject implements Serializable {


@XStreamConverter(value = XStreamBendableScoreConverter.class, ints = {1, 2}) @XStreamConverter(value = XStreamBendableScoreConverter.class, ints = {1, 2})
private BendableScore score; private BendableScore score;


public XStreamBendableScoreConverterTestObject(BendableScore score) { public TestXStreamObject(BendableScore score) {
this.score = score; this.score = score;
} }


Expand Down
Expand Up @@ -18,23 +18,24 @@


import java.io.Serializable; import java.io.Serializable;


import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.annotations.XStreamConverter;
import org.junit.Test; import org.junit.Test;
import org.optaplanner.core.api.score.buildin.simple.SimpleScore; import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
import org.optaplanner.core.impl.score.buildin.simple.SimpleScoreDefinition; import org.optaplanner.core.impl.score.buildin.hardsoft.HardSoftScoreDefinition;
import org.optaplanner.core.impl.testdata.util.PlannerTestUtils; import org.optaplanner.core.impl.testdata.util.PlannerTestUtils;
import org.optaplanner.persistence.xstream.impl.score.XStreamScoreConverter;


import static org.junit.Assert.*; import static org.junit.Assert.*;


public class XStreamScoreConverterTest { public class XStreamScoreConverterTest {


@Test @Test
public void serializeAndDeserializeWithNullField() { public void serializeAndDeserializeWithNullField() {
XStreamScoreConverterTestObject input = new XStreamScoreConverterTestObject(null); TestXStreamObject input = new TestXStreamObject(null);
PlannerTestUtils.serializeAndDeserializeWithAll(input, PlannerTestUtils.serializeAndDeserializeWithAll(input,
new PlannerTestUtils.OutputAsserter<XStreamScoreConverterTestObject>() { new PlannerTestUtils.OutputAsserter<TestXStreamObject>() {
public void assertOutput(XStreamScoreConverterTestObject output) { public void assertOutput(TestXStreamObject output) {
assertEquals(null, output.getScore()); assertEquals(null, output.getScore());
} }
} }
Expand All @@ -43,30 +44,43 @@ public void assertOutput(XStreamScoreConverterTestObject output) {


@Test @Test
public void serializeAndDeserialize() { public void serializeAndDeserialize() {
XStreamScoreConverterTestObject input = new XStreamScoreConverterTestObject(SimpleScore.valueOf(123)); TestXStreamObject input = new TestXStreamObject(HardSoftScore.valueOf(1200, 34));
assertXStreamXml("<TestXStreamObject>\\s*<score>1200hard/34soft</score>\\s*</TestXStreamObject>", input);
PlannerTestUtils.serializeAndDeserializeWithAll(input, PlannerTestUtils.serializeAndDeserializeWithAll(input,
new PlannerTestUtils.OutputAsserter<XStreamScoreConverterTestObject>() { new PlannerTestUtils.OutputAsserter<TestXStreamObject>() {
public void assertOutput(XStreamScoreConverterTestObject output) { public void assertOutput(TestXStreamObject output) {
assertEquals(123, output.getScore().getScore()); assertEquals(HardSoftScore.valueOf(1200, 34), output.getScore());
} }
} }
); );
} }


public static class XStreamScoreConverterTestObject implements Serializable { public static void assertXStreamXml(String regex, Object input) {
XStream xStream = new XStream();
xStream.setMode(XStream.NO_REFERENCES);
xStream.processAnnotations(input.getClass());
String xml = xStream.toXML(input);
if (!xml.matches(regex)) {
fail("Regular expression match failed.\nExpected regular expression: " + regex + "\nActual: " + xml);
}
assertTrue(xml.matches(regex));
}

@XStreamAlias("TestXStreamObject")
public static class TestXStreamObject implements Serializable {


@XStreamConverter(value = XStreamScoreConverter.class, types = {SimpleScoreDefinition.class}) @XStreamConverter(value = XStreamScoreConverter.class, types = {HardSoftScoreDefinition.class})
private SimpleScore score; private HardSoftScore score;


public XStreamScoreConverterTestObject(SimpleScore score) { public TestXStreamObject(HardSoftScore score) {
this.score = score; this.score = score;
} }


public SimpleScore getScore() { public HardSoftScore getScore() {
return score; return score;
} }


public void setScore(SimpleScore score) { public void setScore(HardSoftScore score) {
this.score = score; this.score = score;
} }
} }
Expand Down
Expand Up @@ -72,6 +72,9 @@ public static <T> T serializeAndDeserializeWithJavaSerialization(T input) {
public static <T> T serializeAndDeserializeWithXStream(T input) { public static <T> T serializeAndDeserializeWithXStream(T input) {
XStream xStream = new XStream(); XStream xStream = new XStream();
xStream.setMode(XStream.ID_REFERENCES); xStream.setMode(XStream.ID_REFERENCES);
if (input != null) {
xStream.processAnnotations(input.getClass());
}
String xmlString = xStream.toXML(input); String xmlString = xStream.toXML(input);
return (T) xStream.fromXML(xmlString); return (T) xStream.fromXML(xmlString);
} }
Expand Down

0 comments on commit a4f6ed2

Please sign in to comment.