public void testSuperType() throws Exception { Map<String, String> clazMap = new HashMap<String, String> (); clazMap.put("subtype1", SubType1.class.getName()); clazMap.put("subtype2", SubType2.class.getName()); Visitor visitor = new ClassToNamespaceVisitor(false); Strategy strategy = new VisitorStrategy(visitor); Serializer serializer = new Persister(strategy); MyMap map = new MyMap(); SubType1 subtype1 = new SubType1(); SubType2 subtype2 = new SubType2(); StringWriter writer = new StringWriter(); subtype1.text = "subtype1"; subtype2.superType = subtype1; map.getInternalMap().put("one", subtype1); map.getInternalMap().put("two", subtype2); serializer.write(map, writer); serializer.write(map, System.out); serializer.read(MyMap.class, writer.toString()); }
public void testPrimitiveErasure() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureExample<Double> example = new ErasureExample<Double>(); example.addItem("a", 2.0); example.addItem("b", 1.2); example.addItem("c", 5.4); example.addDoubleGeneric(7.8, 8.7); example.addDoubleGeneric(1.2, 2.1); example.addDoubleGeneric(3.1, 1.3); persister.write(example, System.out); validate(example, persister); }
public void testEnumErasure() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureExample<ErasureEnum> example = new ErasureExample<ErasureEnum>(); example.addItem("a", ErasureEnum.A); example.addItem("b", ErasureEnum.B); example.addItem("c", ErasureEnum.C); example.addDoubleGeneric(ErasureEnum.A, ErasureEnum.B); example.addDoubleGeneric(ErasureEnum.B, ErasureEnum.C); example.addDoubleGeneric(ErasureEnum.C, ErasureEnum.D); persister.write(example, System.out); validate(example, persister); }
public void testErasureWithMapAttributeIllegalExample() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureWithMapAttributeIllegalExample<ErasureEnum> example = new ErasureWithMapAttributeIllegalExample<ErasureEnum>(); boolean failure = false; example.addItem(ErasureEnum.A, "a"); example.addItem(ErasureEnum.B, "b"); example.addItem(ErasureEnum.C, "c"); try { persister.write(example, System.out); }catch(Exception e) { e.printStackTrace(); failure = true; } assertTrue("Attribute should not be possible with an erased key", failure); }
public void testVisitor() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Visitor visitor = new RenameVisitor(); VisitorStrategy strategy = new VisitorStrategy(visitor); Persister renamePersister = new Persister(strategy); Persister normalPersister = new Persister(format); RenameExample example = new RenameExample("example name", "example value"); StringWriter renameWriter = new StringWriter(); StringWriter normalWriter = new StringWriter(); renamePersister.write(example, renameWriter); normalPersister.write(example, normalWriter); String renameResult = renameWriter.toString(); String normalResult = normalWriter.toString(); System.out.println(renameResult); System.out.println(normalResult); assertElementExists(renameResult, "/RENAMEEXAMPLE"); assertElementExists(renameResult, "/RENAMEEXAMPLE/NAME"); assertElementExists(renameResult, "/RENAMEEXAMPLE/VALUE"); assertElementHasValue(renameResult, "/RENAMEEXAMPLE/NAME", "example name"); assertElementHasValue(renameResult, "/RENAMEEXAMPLE/VALUE", "example value"); assertElementExists(normalResult, "/RenameExample"); assertElementExists(normalResult, "/RenameExample/Name"); assertElementExists(normalResult, "/RenameExample/Value"); assertElementHasValue(normalResult, "/RenameExample/Name", "example name"); assertElementHasValue(normalResult, "/RenameExample/Value", "example value"); }
public void testErasure() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureExample<ErasureItem> example = new ErasureExample<ErasureItem>(); StringWriter writer = new StringWriter(); example.addItem("a", new ErasureItem("A", "1")); example.addItem("b", new ErasureItem("B", "2")); example.addItem("c", new ErasureItem("C", "3")); example.addDoubleGeneric(new ErasureItem("1", "1"), new ErasureItem("A", "1")); example.addDoubleGeneric(new ErasureItem("2", "2"), new ErasureItem("B", "2")); example.addDoubleGeneric(new ErasureItem("3", "3"), new ErasureItem("C", "3")); persister.write(example, writer); String text = writer.toString(); System.out.println(text); ErasureExample<ErasureItem> exampleCopy = persister.read(ErasureExample.class, text); assertEquals(exampleCopy.getItem("a").name, "A"); assertEquals(exampleCopy.getItem("b").name, "B"); assertEquals(exampleCopy.getItem("c").name, "C"); validate(example, persister); }