Skip to content

Commit

Permalink
[#568] add example of toString extended strategy and tests for toStri…
Browse files Browse the repository at this point in the history
…ng OK for each version
  • Loading branch information
laurentschoelens authored and mattrpav committed Nov 20, 2024
1 parent 5adc7ef commit bc49bb6
Show file tree
Hide file tree
Showing 43 changed files with 631 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<version>2.0.15-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>jaxb2-basics-test-po-older-runtime</artifactId>
<artifactId>jaxb2-basics-test-po-0.11.0-runtime</artifactId>
<packaging>jar</packaging>
<name>JAXB Tools :: JAXB Basics :: Test [po-older-runtime]</name>
<name>JAXB Tools :: JAXB Basics :: Test [po-0.11.0-runtime]</name>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
Expand All @@ -35,10 +35,12 @@
<artifactId>jaxb-maven-plugin</artifactId>
<configuration>
<extension>true</extension>
<generatePackage>generatedolder</generatePackage>
<generatePackage>generated.v0_11_0</generatePackage>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-XtoString</arg>
<arg>-XtoString-toStringStrategyClass=internal.v0_11_0.EnumToStringStrategy</arg>
</args>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal.v0_11_0;

import org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;

public class EnumToStringStrategy extends JAXBToStringStrategy {

public boolean isUseIdentityHashCode() {
return false;
}

public StringBuilder appendStart(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return stringBuilder;
} else {
return super.appendStart(parentLocator, parent, stringBuilder);
}
}

public StringBuilder appendEnd(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return appendEnum((Enum) parent, stringBuilder);
} else {
return super.appendEnd(parentLocator, parent, stringBuilder);
}
}

protected StringBuilder appendEnum(Enum e, StringBuilder stringBuilder) {
stringBuilder.append(e.name());
return stringBuilder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jvnet.jaxb2_commons.tests.v0_11_0;

import org.junit.Assert;
import org.junit.Test;

public class EqualsPo_V0_11_0_Test {

@Test
public void testEqualsOld() {
generated.v0_11_0.Items.Item item = new generated.v0_11_0.Items.Item();
item.setComment("a");


generated.v0_11_0.Items.Item anotherItem = new generated.v0_11_0.Items.Item();
anotherItem.setComment("a");

Assert.assertEquals("equals doesnt work with 0.11.0 runtime", item, anotherItem);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jvnet.jaxb2_commons.tests.v0_11_0;

import org.junit.Assert;
import org.junit.Test;

public class ToStringPo_V0_11_0_Test {

@Test
public void testToString() {
generated.v0_11_0.Items.Item item = new generated.v0_11_0.Items.Item();
item.setComment("a");

Assert.assertEquals("toString doesnt work with 0.11.0 runtime",
"generated.v0_11_0.Items$Item[productName=<null>(default), quantity=0, usPrice=<null>(default), comment=a, shipDate=<null>(default), partNum=<null>(default)]",
item.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<version>2.0.15-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>jaxb2-basics-test-po-old-runtime</artifactId>
<artifactId>jaxb2-basics-test-po-0.13.1-runtime</artifactId>
<packaging>jar</packaging>
<name>JAXB Tools :: JAXB Basics :: Test [po-old-runtime]</name>
<name>JAXB Tools :: JAXB Basics :: Test [po-0.13.1-runtime]</name>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
Expand All @@ -35,10 +35,12 @@
<artifactId>jaxb-maven-plugin</artifactId>
<configuration>
<extension>true</extension>
<generatePackage>generatedold</generatePackage>
<generatePackage>generated.v0_13_1</generatePackage>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-XtoString</arg>
<arg>-XtoString-toStringStrategyClass=internal.v0_13_1.EnumToStringStrategy</arg>
</args>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal.v0_13_1;

import org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;

public class EnumToStringStrategy extends JAXBToStringStrategy {

public boolean isUseIdentityHashCode() {
return false;
}

public StringBuilder appendStart(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return stringBuilder;
} else {
return super.appendStart(parentLocator, parent, stringBuilder);
}
}

public StringBuilder appendEnd(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return appendEnum((Enum) parent, stringBuilder);
} else {
return super.appendEnd(parentLocator, parent, stringBuilder);
}
}

protected StringBuilder appendEnum(Enum e, StringBuilder stringBuilder) {
stringBuilder.append(e.name());
return stringBuilder;
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.jvnet.jaxb2_commons.tests.old;
package org.jvnet.jaxb2_commons.tests.v0_13_1;

import org.junit.Assert;
import org.junit.Test;

public class EqualsPoOldTest {
public class EqualsPo_V0_13_1_Test {

@Test
public void testEqualsOld() {
generatedold.Items.Item item = new generatedold.Items.Item();
generated.v0_13_1.Items.Item item = new generated.v0_13_1.Items.Item();
item.setComment("a");


generatedold.Items.Item anotherItem = new generatedold.Items.Item();
generated.v0_13_1.Items.Item anotherItem = new generated.v0_13_1.Items.Item();
anotherItem.setComment("a");

Assert.assertEquals("equals doesnt work with 0.13.1 runtime", item, anotherItem);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jvnet.jaxb2_commons.tests.v0_13_1;

import org.junit.Assert;
import org.junit.Test;

public class ToStringPo_V0_13_1_Test {

@Test
public void testToString() {
generated.v0_13_1.Items.Item item = new generated.v0_13_1.Items.Item();
item.setComment("a");

Assert.assertEquals("toString doesnt work with 0.13.1 runtime",
"generated.v0_13_1.Items$Item[productName=<null>(default), quantity=0, usPrice=<null>(default), comment=a, shipDate=<null>(default), partNum=<null>(default)]",
item.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<version>2.0.15-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>jaxb2-basics-test-po-fake-older-runtime</artifactId>
<artifactId>jaxb2-basics-test-po-1.11.1-runtime</artifactId>
<packaging>jar</packaging>
<name>JAXB Tools :: JAXB Basics :: Test [po-fake-older-runtime]</name>
<name>JAXB Tools :: JAXB Basics :: Test [po-1.11.1-runtime]</name>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
Expand All @@ -35,10 +35,12 @@
<artifactId>jaxb-maven-plugin</artifactId>
<configuration>
<extension>true</extension>
<generatePackage>generatedfakeolder</generatePackage>
<generatePackage>generated.v1_11_1</generatePackage>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-XtoString</arg>
<arg>-XtoString-toStringStrategyClass=internal.v1_11_1.EnumToStringStrategy</arg>
</args>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal.v1_11_1;

import org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;

public class EnumToStringStrategy extends JAXBToStringStrategy {

public boolean isUseIdentityHashCode() {
return false;
}

public StringBuilder appendStart(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return stringBuilder;
} else {
return super.appendStart(parentLocator, parent, stringBuilder);
}
}

public StringBuilder appendEnd(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return appendEnum((Enum) parent, stringBuilder);
} else {
return super.appendEnd(parentLocator, parent, stringBuilder);
}
}

protected StringBuilder appendEnum(Enum e, StringBuilder stringBuilder) {
stringBuilder.append(e.name());
return stringBuilder;
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.jvnet.jaxb2_commons.tests.fakeolder;
package org.jvnet.jaxb2_commons.tests.v1_11_1;

import org.junit.Assert;
import org.junit.Test;

public class EqualsPoFakeOlderTest {
public class EqualsPo_V1_11_1_Test {

@Test
public void testEqualsFakeOlder() {
generatedfakeolder.Items.Item item = new generatedfakeolder.Items.Item();
generated.v1_11_1.Items.Item item = new generated.v1_11_1.Items.Item();
item.setComment("a");


generatedfakeolder.Items.Item anotherItem = new generatedfakeolder.Items.Item();
generated.v1_11_1.Items.Item anotherItem = new generated.v1_11_1.Items.Item();
anotherItem.setComment("a");

Assert.assertEquals("equals doesnt work with 1.11.1 runtime", item, anotherItem);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jvnet.jaxb2_commons.tests.v1_11_1;

import org.junit.Assert;
import org.junit.Test;

public class ToStringPo_V1_11_1_Test {

@Test
public void testToString() {
generated.v1_11_1.Items.Item item = new generated.v1_11_1.Items.Item();
item.setComment("a");

Assert.assertEquals("toString doesnt work with 1.11.1 runtime",
"generated.v1_11_1.Items$Item[productName=<null>(default), quantity=0, usPrice=<null>(default), comment=a, shipDate=<null>(default), partNum=<null>(default)]",
item.toString());
}

}
56 changes: 56 additions & 0 deletions basics/tests/po-2.0.14-runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-basics-project</artifactId>
<version>2.0.15-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>jaxb2-basics-test-po-2.0.14-runtime</artifactId>
<packaging>jar</packaging>
<name>JAXB Tools :: JAXB Basics :: Test [po-2.0.14-runtime]</name>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>2.0.14</version>
</dependency>
</dependencies>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<configuration>
<extension>true</extension>
<generatePackage>generated.v2_0_14</generatePackage>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-XtoString</arg>
<arg>-XtoString-toStringStrategyClass=internal.v2_0_14.EnumToStringStrategy</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>2.0.14</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal.v2_0_14;

import org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;

public class EnumToStringStrategy extends JAXBToStringStrategy {

public boolean isUseIdentityHashCode() {
return false;
}

public StringBuilder appendStart(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return stringBuilder;
} else {
return super.appendStart(parentLocator, parent, stringBuilder);
}
}

public StringBuilder appendEnd(ObjectLocator parentLocator, Object parent, StringBuilder stringBuilder) {
if (parent instanceof Enum) {
return appendEnum((Enum) parent, stringBuilder);
} else {
return super.appendEnd(parentLocator, parent, stringBuilder);
}
}

protected StringBuilder appendEnum(Enum e, StringBuilder stringBuilder) {
stringBuilder.append(e.name());
return stringBuilder;
}

}
Loading

0 comments on commit bc49bb6

Please sign in to comment.