Skip to content

Test : Remove hardcoded new line "\n" from tests #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
public abstract class XmlTestBase
extends TestCase
{

protected static final String DEFAULT_NEW_LINE;

static {
String newLine = System.getProperty("line.separator");
DEFAULT_NEW_LINE = newLine == null ? "\n" : newLine;
}

@JsonPropertyOrder({ "first", "last", "id" })
protected static class NameBean {
@JacksonXmlProperty(isAttribute=true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void testSerializeAsText() throws IOException
assertEquals("<Simple a=\"13\">something</Simple>", xml);
// [dataformat-xml#56]: should work with indentation as well
xml = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(new Simple());
assertEquals("<Simple a=\"13\">something</Simple>\n", xml);
assertEquals("<Simple a=\"13\">something</Simple>" + DEFAULT_NEW_LINE, xml);
}

public void testDeserializeAsText() throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ private void _testBinaryVariants(Base64Variant b64v, String expEncoded,
r = r.with(b64v);
}
final String EXP = indent ?
"<BinaryValue>\n <value>"+expEncoded+"</value>\n</BinaryValue>" :
"<BinaryValue>" + DEFAULT_NEW_LINE +
" <value>"+expEncoded+"</value>" + DEFAULT_NEW_LINE +
"</BinaryValue>" :
"<BinaryValue><value>"+expEncoded+"</value></BinaryValue>";
final String xml = w.writeValueAsString(new BinaryValue(BINARY_DATA)).trim();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ public void setUp() throws Exception {
_xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
}

private static final String SYSTEM_DEFAULT_NEW_LINE = System.getProperty("line.separator");

Copy link
Member Author

@JooHyukKim JooHyukKim Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was dynamically set SYSTEM_DEFAULT_NEW_LINE in ONLY XmlPrettyPrinterTest.java.
I moved it up to XmlTestBase.

/*
/**********************************************************
/* Unit tests
Expand Down Expand Up @@ -159,16 +157,21 @@ public void testSimpleMap() throws Exception
public void testWithAttr() throws Exception
{
String xml = _xmlMapper.writeValueAsString(new AttrBean());
assertEquals("<AttrBean count=\"3\"/>\n", xml);
assertEquals("<AttrBean count=\"3\"/>" + DEFAULT_NEW_LINE, xml);
String xml2 = _xmlMapper.writeValueAsString(new AttrBean2());
assertEquals("<AttrBean2 count=\"3\">\n <value>14</value>\n</AttrBean2>\n", xml2);
assertEquals(
"<AttrBean2 count=\"3\">" + DEFAULT_NEW_LINE +
" <value>14</value>" + DEFAULT_NEW_LINE +
"</AttrBean2>" + DEFAULT_NEW_LINE,
xml2);
}

public void testEmptyElem() throws Exception
{
PojoFor123 simple = new PojoFor123("foobar");
String xml = _xmlMapper.writeValueAsString(simple);
assertEquals("<PojoFor123 name=\"foobar\"/>\n", xml);
assertEquals("<PojoFor123 name=\"foobar\"/>" + DEFAULT_NEW_LINE,
xml);
}

public void testMultiLevel172() throws Exception
Expand All @@ -181,15 +184,15 @@ public void testMultiLevel172() throws Exception
// unify possible apostrophes to quotes
xml = a2q(xml);

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYSTEM_DEFAULT_NEW_LINE
+"<Company>" + SYSTEM_DEFAULT_NEW_LINE
+" <e>" + SYSTEM_DEFAULT_NEW_LINE
+" <employee>" + SYSTEM_DEFAULT_NEW_LINE
+" <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
+" <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
+" </employee>" + SYSTEM_DEFAULT_NEW_LINE
+" </e>" + SYSTEM_DEFAULT_NEW_LINE
+"</Company>" + SYSTEM_DEFAULT_NEW_LINE,
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + DEFAULT_NEW_LINE
+"<Company>" + DEFAULT_NEW_LINE
+" <e>" + DEFAULT_NEW_LINE
+" <employee>" + DEFAULT_NEW_LINE
+" <id>abc</id>" + DEFAULT_NEW_LINE
+" <type>FULL_TIME</type>" + DEFAULT_NEW_LINE
+" </employee>" + DEFAULT_NEW_LINE
+" </e>" + DEFAULT_NEW_LINE
+"</Company>" + DEFAULT_NEW_LINE,
xml);
}

Expand Down Expand Up @@ -232,15 +235,15 @@ public void testNewLine_systemDefault() throws Exception {
xml = a2q(xml);

// with indentation, should get newLines in prolog/epilog too
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYSTEM_DEFAULT_NEW_LINE
+ "<Company>" + SYSTEM_DEFAULT_NEW_LINE
+ " <e>" + SYSTEM_DEFAULT_NEW_LINE
+ " <employee>" + SYSTEM_DEFAULT_NEW_LINE
+ " <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
+ " <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
+ " </employee>" + SYSTEM_DEFAULT_NEW_LINE
+ " </e>" + SYSTEM_DEFAULT_NEW_LINE
+ "</Company>" + SYSTEM_DEFAULT_NEW_LINE,
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + DEFAULT_NEW_LINE
+ "<Company>" + DEFAULT_NEW_LINE
+ " <e>" + DEFAULT_NEW_LINE
+ " <employee>" + DEFAULT_NEW_LINE
+ " <id>abc</id>" + DEFAULT_NEW_LINE
+ " <type>FULL_TIME</type>" + DEFAULT_NEW_LINE
+ " </employee>" + DEFAULT_NEW_LINE
+ " </e>" + DEFAULT_NEW_LINE
+ "</Company>" + DEFAULT_NEW_LINE,
xml);
}

Expand All @@ -255,15 +258,15 @@ public void testNewLine_UseSystemDefaultLineSeperatorOnNullCustomNewLine() throw
// unify possible apostrophes to quotes
xml = a2q(xml);

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYSTEM_DEFAULT_NEW_LINE
+ "<Company>" + SYSTEM_DEFAULT_NEW_LINE
+ " <e>" + SYSTEM_DEFAULT_NEW_LINE
+ " <employee>" + SYSTEM_DEFAULT_NEW_LINE
+ " <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
+ " <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
+ " </employee>" + SYSTEM_DEFAULT_NEW_LINE
+ " </e>" + SYSTEM_DEFAULT_NEW_LINE
+ "</Company>" + SYSTEM_DEFAULT_NEW_LINE,
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + DEFAULT_NEW_LINE
+ "<Company>" + DEFAULT_NEW_LINE
+ " <e>" + DEFAULT_NEW_LINE
+ " <employee>" + DEFAULT_NEW_LINE
+ " <id>abc</id>" + DEFAULT_NEW_LINE
+ " <type>FULL_TIME</type>" + DEFAULT_NEW_LINE
+ " </employee>" + DEFAULT_NEW_LINE
+ " </e>" + DEFAULT_NEW_LINE
+ "</Company>" + DEFAULT_NEW_LINE,
xml);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ public void testNilPropertyWithIndent() throws IOException
final String xml = MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(new WrapperBean<>(null))
.trim();
assertEquals(
"<WrapperBean>\n"
+" <value xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>\n"
+"</WrapperBean>", xml);
assertEquals("<WrapperBean>" + DEFAULT_NEW_LINE
+ " <value xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>" + DEFAULT_NEW_LINE
+ "</WrapperBean>", xml);
}
}