-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAXB toString strategy for more succinct output
Outputs only simple class names instead of fully qualified, and omits fields set to null. Enabled using the -XtoString argument, followed by -XtoString-toStringStrategyClass=no.digipost.org.jvnet.jaxb.lang.SuccinctToStringStrategy for JAXB 3.0+ (Jakarta) or -XtoString-toStringStrategyClass=no.digipost.org.jvnet.jaxb2_commons.lang.SuccinctToStringStrategy for JAXB 2.x (javax) https://github.com/highsource/jaxb-tools/wiki/JAXB2-Basic https://github.com/highsource/jaxb-tools/wiki/JAXB2-ToString-Plugin
- Loading branch information
1 parent
eb5f2e7
commit 6462ae2
Showing
8 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
src/main/java/no/digipost/jakarta | ||
src/main/java/no/digipost/javax | ||
src/main/java/no/digipost/org/jvnet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...avax/src/main/java/no/digipost/org/jvnet/jaxb2_commons/lang/SuccinctToStringStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package no.digipost.org.jvnet.jaxb2_commons.lang; | ||
|
||
import org.jvnet.jaxb2_commons.lang.DefaultToStringStrategy; | ||
import org.jvnet.jaxb2_commons.locator.ObjectLocator; | ||
|
||
public class SuccinctToStringStrategy extends DefaultToStringStrategy { | ||
|
||
@Override | ||
public boolean isUseIdentityHashCode() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isUseDefaultFieldValueMarkers() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void appendClassName(StringBuilder toString, Object object) { | ||
if (object != null) { | ||
toString.append(object.getClass().getSimpleName()); | ||
} | ||
} | ||
|
||
@Override | ||
public StringBuilder appendField(ObjectLocator parentLocator, Object parent, String fieldName, StringBuilder buffer, Object value, boolean valueSet) { | ||
if (valueSet) { | ||
return super.appendField(parentLocator, parent, fieldName, buffer, value, valueSet); | ||
} | ||
return buffer; | ||
} | ||
|
||
private static final SuccinctToStringStrategy INSTANCE = new SuccinctToStringStrategy(); | ||
|
||
public static SuccinctToStringStrategy getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
bind-javax/src/test/java/no/digipost/xml/alieninvasion/SuccinctToStringStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package no.digipost.xml.alieninvasion; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static java.lang.Integer.toHexString; | ||
import static java.lang.reflect.Modifier.isStatic; | ||
import static java.util.stream.Collectors.toList; | ||
import static no.digipost.DiggExceptions.applyUnchecked; | ||
import static no.digipost.xml.alieninvasion.DiplomaticAgreement.FOE; | ||
import static no.digipost.xml.alieninvasion.DiplomaticAgreement.FRIEND; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static uk.co.probablyfine.matchers.Java8Matchers.where; | ||
|
||
class SuccinctToStringStrategyTest { | ||
|
||
@Test | ||
void emptyObject() { | ||
Alien alien = new Alien(); | ||
assertThat("toString outputs simple class name instead of fully qualified", | ||
alien, where(Alien::toString, startsWith(Alien.class.getSimpleName()))); | ||
assertThat("toString outputs no key-value pairs for its properties", | ||
alien, where(Alien::toString, not(containsString("=")))); | ||
} | ||
|
||
@Test | ||
void doesNotIncludehashCode() { | ||
Alien alien = new Alien("Blarg", new Planet("Alpha Centauri b", "Centaurus"), FOE); | ||
assertThat("toString does not include hashCode", | ||
alien, where(Alien::toString, not(containsString(toHexString(alien.hashCode()))))); | ||
} | ||
|
||
@Test | ||
void onlyIncludesNonNullFields() { | ||
Alien homelessAlien = new Alien("Blarg", null, FRIEND); | ||
List<Field> alienFields = Stream.of(Alien.class.getDeclaredFields()) | ||
.filter(field -> !isStatic(field.getModifiers())) | ||
.map(field -> { | ||
field.setAccessible(true); | ||
return field; | ||
}) | ||
.collect(toList()); | ||
|
||
assertAll("toString includes all non-null field values", alienFields.stream() | ||
.filter(field -> applyUnchecked(field::get, homelessAlien) != null) | ||
.map(Field::getName) | ||
.map(fieldName -> () -> assertThat(homelessAlien, where(Alien::toString, containsString(fieldName))))); | ||
|
||
assertAll("toString excludes all null field values", alienFields.stream() | ||
.filter(field -> applyUnchecked(field::get, homelessAlien) == null) | ||
.map(Field::getName) | ||
.map(fieldName -> () -> assertThat(homelessAlien, where(Alien::toString, not(containsString(fieldName)))))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xsd:schema version="1.0" targetNamespace="https://digipost.no/digipost-xml-bind" | ||
xmlns="https://digipost.no/digipost-xml-bind" | ||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | ||
elementFormDefault="qualified"> | ||
|
||
<xsd:complexType name="Alien"> | ||
<xsd:sequence> | ||
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1" /> | ||
<xsd:element name="home" type="Planet" minOccurs="1" maxOccurs="1" /> | ||
<xsd:element name="diplomatic-agreement" type="diplomatic-agreement" minOccurs="1" maxOccurs="1" /> | ||
</xsd:sequence> | ||
</xsd:complexType> | ||
|
||
<xsd:complexType name="Planet"> | ||
<xsd:sequence> | ||
<xsd:element name="name" type="xsd:string" /> | ||
<xsd:element name="constellation-name" type="xsd:string" /> | ||
</xsd:sequence> | ||
</xsd:complexType> | ||
|
||
<xsd:simpleType name="diplomatic-agreement"> | ||
<xsd:restriction base="xsd:string"> | ||
<xsd:enumeration value="friend" /> | ||
<xsd:enumeration value="foe" /> | ||
</xsd:restriction> | ||
</xsd:simpleType> | ||
|
||
</xsd:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters