-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e47307d
commit ae2fa61
Showing
5 changed files
with
459 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package unittests; | ||
|
||
import org.junit.Test; | ||
import xmlparser.XmlParser; | ||
import xmlparser.annotations.XmlMapTagIsKey; | ||
import xmlparser.annotations.XmlMapWithAttributes; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static xmlparser.XmlParser.newXmlParser; | ||
|
||
public class EscapeMapTagIsKeyTest { | ||
|
||
private final XmlParser parserDefault = new XmlParser(); | ||
private final XmlParser parserEncodeUTF8 = newXmlParser().shouldEncodeUTF8().build(); | ||
|
||
private static class PojoMap { | ||
@XmlMapTagIsKey | ||
private final Map<String, String> map; | ||
|
||
private PojoMap(final Map<String, String> map) { | ||
this.map = map; | ||
} | ||
} | ||
|
||
@Test | ||
public void serializeMapWithDangerousChars() { | ||
final PojoMap inputPojo = new PojoMap(Map.of("key", "<>&\"'")); | ||
final String expected = "<pojomap>\n" + | ||
" <map>\n" + | ||
" <key><>&"'</key>\n" + | ||
" </map>\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserDefault.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void deserializeMapWithEscapedChars() { | ||
final String inputXml = "<pojomap>\n" + | ||
" <map>\n" + | ||
" <key><>&"'</key>\n" + | ||
" </map>\n" + | ||
"</pojomap>\n"; | ||
final PojoMap expected = new PojoMap(Map.of("key", "<>&\"'")); | ||
|
||
final PojoMap actual = parserDefault.fromXml(inputXml, PojoMap.class); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected.map.get("key"), actual.map.get("key")); | ||
} | ||
|
||
@Test | ||
public void serializeMapUTF8Characters() { | ||
final PojoMap inputPojo = new PojoMap(Map.of("key", "ƑƟƠƄǠȒ")); | ||
final String expected = "<pojomap>\n" + | ||
" <map>\n" + | ||
" <key>ƑƟƠƄǠȒ</key>\n" + | ||
" </map>\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserEncodeUTF8.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void dontSerializeMapUTF8Characters() { | ||
final PojoMap inputPojo = new PojoMap(Map.of("key", "ƑƟƠƄǠȒ")); | ||
final String expected = "<pojomap>\n" + | ||
" <map>\n" + | ||
" <key>ƑƟƠƄǠȒ</key>\n" + | ||
" </map>\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserDefault.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void deserializeMapUTF8Characters() { | ||
final String inputXml = "<pojomap>\n" + | ||
" <map>\n" + | ||
" <key>ƑƟƠƄǠȒ</key>\n" + | ||
" </map>\n" + | ||
"</pojomap>\n"; | ||
final PojoMap expected = new PojoMap(Map.of("key", "ƑƟƠƄǠȒ")); | ||
|
||
final PojoMap actual = parserEncodeUTF8.fromXml(inputXml, PojoMap.class); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected.map.get("key"), actual.map.get("key")); | ||
} | ||
|
||
} |
161 changes: 161 additions & 0 deletions
161
src/test/java/unittests/EscapeMapWithAttributesTest.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,161 @@ | ||
package unittests; | ||
|
||
import org.junit.Test; | ||
import xmlparser.XmlParser; | ||
import xmlparser.annotations.XmlMapWithAttributes; | ||
import xmlparser.annotations.XmlMapWithChildNodes; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static xmlparser.XmlParser.newXmlParser; | ||
|
||
public class EscapeMapWithAttributesTest { | ||
|
||
private final XmlParser parserDefault = new XmlParser(); | ||
private final XmlParser parserEncodeUTF8 = newXmlParser().shouldEncodeUTF8().build(); | ||
|
||
private static class PojoMap { | ||
@XmlMapWithAttributes(keyName = "key") | ||
private final Map<String, String> map1; | ||
@XmlMapWithAttributes(keyName = "key", valueName = "value") | ||
private final Map<String, String> map2; | ||
|
||
private PojoMap(final Map<String, String> map1, final Map<String, String> map2) { | ||
this.map1 = map1; | ||
this.map2 = map2; | ||
} | ||
} | ||
|
||
@Test | ||
public void serializeMap1WithDangerousChars() { | ||
final PojoMap inputPojo = new PojoMap(Map.of("<>&\"'", "<>&\"'"), null); | ||
final String expected = "<pojomap>\n" + | ||
" <map1 key=\"<>&"'\"><>&"'</map1>\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserDefault.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void serializeMap2WithDangerousChars() { | ||
final PojoMap inputPojo = new PojoMap(null, Map.of("<>&\"'", "<>&\"'")); | ||
final String expected = "<pojomap>\n" + | ||
" <map2 key=\"<>&"'\" value=\"<>&"'\" />\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserDefault.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void deserializeMap1WithEscapedChars() { | ||
final String inputXml = "<pojomap>\n" + | ||
" <map1 key=\"<>&"'\"><>&"'</map1>\n" + | ||
"</pojomap>\n"; | ||
final PojoMap expected = new PojoMap(Map.of("<>&\"'", "<>&\"'"), null); | ||
|
||
final PojoMap actual = parserDefault.fromXml(inputXml, PojoMap.class); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected.map1.get("<>&\"'"), actual.map1.get("<>&\"'")); | ||
} | ||
|
||
@Test | ||
public void deserializeMap2WithEscapedChars() { | ||
final String inputXml = "<pojomap>\n" + | ||
" <map2 key=\"<>&"'\" value=\"<>&"'\" />\n" + | ||
"</pojomap>\n"; | ||
final PojoMap expected = new PojoMap(null, Map.of("<>&\"'", "<>&\"'")); | ||
|
||
final PojoMap actual = parserDefault.fromXml(inputXml, PojoMap.class); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected.map2.get("<>&\"'"), actual.map2.get("<>&\"'")); | ||
} | ||
|
||
@Test | ||
public void serializeMap1UTF8Characters() { | ||
final PojoMap inputPojo = new PojoMap(Map.of("ƑƟƠƄǠȒ", "ƑƟƠƄǠȒ"), null); | ||
final String expected = "<pojomap>\n" + | ||
" <map1 key=\"ƑƟƠƄǠȒ\">ƑƟƠƄǠȒ</map1>\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserEncodeUTF8.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void dontSerializeMap1UTF8Characters() { | ||
final PojoMap inputPojo = new PojoMap(Map.of("ƑƟƠƄǠȒ", "ƑƟƠƄǠȒ"), null); | ||
final String expected = "<pojomap>\n" + | ||
" <map1 key=\"ƑƟƠƄǠȒ\">ƑƟƠƄǠȒ</map1>\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserDefault.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void deserializeMap1UTF8Characters() { | ||
final String inputXml = "<pojomap>\n" + | ||
" <map1 key=\"ƑƟƠƄǠȒ\">ƑƟƠƄǠȒ</map1>\n" + | ||
"</pojomap>\n"; | ||
final PojoMap expected = new PojoMap(Map.of("ƑƟƠƄǠȒ", "ƑƟƠƄǠȒ"), null); | ||
|
||
final PojoMap actual = parserEncodeUTF8.fromXml(inputXml, PojoMap.class); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected.map1.get("ƑƟƠƄǠȒ"), actual.map1.get("ƑƟƠƄǠȒ")); | ||
} | ||
|
||
@Test | ||
public void serializeMap2UTF8Characters() { | ||
final PojoMap inputPojo = new PojoMap(null, Map.of("ƑƟƠƄǠȒ", "ƑƟƠƄǠȒ")); | ||
final String expected = "<pojomap>\n" + | ||
" <map2 key=\"ƑƟƠƄǠȒ\" value=\"ƑƟƠƄǠȒ\" />\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserEncodeUTF8.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void dontSerializeMap2UTF8Characters() { | ||
final PojoMap inputPojo = new PojoMap(null, Map.of("ƑƟƠƄǠȒ", "ƑƟƠƄǠȒ")); | ||
final String expected = "<pojomap>\n" + | ||
" <map2 key=\"ƑƟƠƄǠȒ\" value=\"ƑƟƠƄǠȒ\" />\n" + | ||
"</pojomap>\n"; | ||
|
||
final String actual = parserDefault.toXml(inputPojo); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected, actual); | ||
} | ||
|
||
@Test | ||
public void deserializeMap2UTF8Characters() { | ||
final String inputXml = "<pojomap>\n" + | ||
" <map2 key=\"ƑƟƠƄǠȒ\" value=\"ƑƟƠƄǠȒ\" />\n" + | ||
"</pojomap>\n"; | ||
final PojoMap expected = new PojoMap(null, Map.of("ƑƟƠƄǠȒ", "ƑƟƠƄǠȒ")); | ||
|
||
final PojoMap actual = parserEncodeUTF8.fromXml(inputXml, PojoMap.class); | ||
|
||
assertNotNull("No serialization response", actual); | ||
assertEquals("Invalid serialized output", expected.map2.get("ƑƟƠƄǠȒ"), actual.map2.get("ƑƟƠƄǠȒ")); | ||
} | ||
|
||
} |
Oops, something went wrong.