http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java b/juneau-core/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java new file mode 100755 index 0000000..55ad7aa --- /dev/null +++ b/juneau-core/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java @@ -0,0 +1,106 @@ +/*************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + ***************************************************************************************************************************/ +package org.apache.juneau.dto.cognos; + +import static org.apache.juneau.TestUtils.*; +import static org.apache.juneau.serializer.SerializerContext.*; +import static org.apache.juneau.xml.XmlSerializerContext.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.xml.*; +import org.junit.*; + +public class CognosXmlTest { + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + String expected = "" + + "<dataset xmlns='http://developer.cognos.com/schemas/xmldata/1/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n" + + " <metadata>\n" + + " <item name='asOfDate' type='xs:string' length='12'/>\n" + + " <item name='rateOfReturn' type='xs:double'/>\n" + + " <item name='famAcctIndex' type='xs:string' length='3'/>\n" + + " <item name='rowID' type='xs:string' length='1'/>\n" + + " <item name='brM' type='xs:string' length='1'/>\n" + + " <item name='productLineCode' type='xs:int'/>\n" + + " </metadata>\n" + + " <data>\n" + + " <row>\n" + + " <value>Apr 26, 2002</value>\n" + + " <value>0.21006642</value>\n" + + " <value>JA1</value>\n" + + " <value>F</value>\n" + + " <value>B</value>\n" + + " <value>1</value>\n" + + " </row>\n" + + " <row>\n" + + " <value>Apr 27, 2002</value>\n" + + " <value>0.1111111</value>\n" + + " <value>BBB</value>\n" + + " <value>G</value>\n" + + " <value>B</value>\n" + + " <value>2</value>\n" + + " </row>\n" + + " </data>\n" + + "</dataset>\n"; + + List<Object> rows = new LinkedList<Object>(); + rows.add(new ObjectMap("{asOfDate:'Apr 26, 2002',rateOfReturn:0.210066429,famAcctIndex:'JA1',rowID:'F',brM:'B',productLineCode:1}")); + rows.add(new Item("Apr 27, 2002", 0.1111111, "BBB", "G", "B", 2)); + + Column[] c = { + new Column("asOfDate", "xs:string", 12), + new Column("rateOfReturn", "xs:double"), + new Column("famAcctIndex", "xs:string", 3), + new Column("rowID", "xs:string", 1), + new Column("brM", "xs:string", 1), + new Column("productLineCode", "xs:int") + }; + + XmlSerializer s = new XmlSerializer().setProperty(SERIALIZER_useIndentation, true).setProperty(SERIALIZER_quoteChar, '\'').setProperty(XML_defaultNamespaceUri, "cognos"); + + DataSet ds = new DataSet(c, rows, BeanContext.DEFAULT); + + String out = s.serialize(ds); + + assertEquals(expected, out); + + // Make sure we can parse it back into a POJO. + DataSet ds2 = XmlParser.DEFAULT.parse(out, DataSet.class); + assertEqualObjects(ds, ds2); + } + + public static class Item { + public String asOfDate; + public double rateOfReturn; + public String famAcctIndex; + public String rowID; + public String brM; + public int productLineCode; + + public Item(String asOfDate, double rateOfReturn, String famAcctIndex, String rowID, String brM, int productLineCode) { + this.asOfDate = asOfDate; + this.rateOfReturn = rateOfReturn; + this.famAcctIndex = famAcctIndex; + this.rowID = rowID; + this.brM = brM; + this.productLineCode = productLineCode; + } + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java b/juneau-core/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java new file mode 100755 index 0000000..c889af3 --- /dev/null +++ b/juneau-core/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java @@ -0,0 +1,103 @@ +/*************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + ***************************************************************************************************************************/ +package org.apache.juneau.dto.jsonschema; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import java.net.*; + +import org.apache.juneau.json.*; +import org.junit.*; + + +public class JsonSchemaTest { + + @Test + public void testSchema1() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX_READABLE; + JsonParser p = JsonParser.DEFAULT; + String r; + Schema t, t2; + + t = getTest1(); + r = s.serialize(t); + String expected = readFile(getClass().getResource("/dto/jsonschema/test1.json").getPath()); + assertEquals(expected, r); + t2 = p.parse(r, Schema.class); + assertEqualObjects(t, t2); + } + + @Test + public void testSchema2() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX_READABLE; + JsonParser p = JsonParser.DEFAULT; + String r; + Schema t, t2; + + t = getTest2(); + r = s.serialize(t); + String expected = readFile(getClass().getResource("/dto/jsonschema/test2.json").getPath()); + assertEquals(expected, r); + t2 = p.parse(r, Schema.class); + assertEqualObjects(t, t2); + } + + /** Bean with simple values for each property */ + public static Schema getTest1() { + return new Schema() + .setId("http://id") + .setSchemaVersionUri("http://schemaVersionUri") + .setTitle("title") + .setDescription("description") + .setType(JsonType.NUMBER) + .addDefinition("definition", new SchemaRef("http://definition")) + .addProperties(new SchemaProperty("property", JsonType.NUMBER)) + .addPatternProperties(new SchemaProperty("/pattern/", JsonType.NUMBER)) + .addDependency("dependency", new SchemaRef("http://dependency")) + .addItems(new Schema().setType(JsonType.NUMBER)) + .setMultipleOf(1) + .setMaximum(2) + .setExclusiveMaximum(true) + .setMinimum(3) + .setExclusiveMinimum(true) + .setMaxLength(4) + .setMinLength(5) + .setPattern("/pattern/") + .addAdditionalItems(new SchemaProperty("additionalItem", JsonType.NUMBER)) + .setMaxItems(6) + .setMinItems(7) + .setUniqueItems(true) + .setMaxProperties(8) + .setMinProperties(9) + .addRequired("required") + .setAdditionalProperties(new SchemaRef("http://additionalProperty")) + .addEnum("enum") + .addAllOf(new SchemaRef("http://allOf")) + .addAnyOf(new SchemaRef("http://anyOf")) + .addOneOf(new SchemaRef("http://oneOf")) + .setNot(new SchemaRef("http://not")); + } + + /** Bean with other possible property value types not covered in test1 */ + public static Schema getTest2() { + return new Schema() + .setId(URI.create("http://id")) + .setSchemaVersionUri(URI.create("http://schemaVersionUri")) + .setType(new JsonTypeArray(JsonType.STRING, JsonType.NUMBER)) + .addDefinition("definition", new Schema().setId("http://definition")) + .setItems(new SchemaArray(new SchemaRef("http://items"))) + .setAdditionalItems(Boolean.TRUE) + .setAdditionalProperties(Boolean.TRUE); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/test/java/org/apache/juneau/html/CommonParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/test/java/org/apache/juneau/html/CommonParserTest.java b/juneau-core/src/test/java/org/apache/juneau/html/CommonParserTest.java new file mode 100755 index 0000000..c3a970f --- /dev/null +++ b/juneau-core/src/test/java/org/apache/juneau/html/CommonParserTest.java @@ -0,0 +1,162 @@ +/*************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + ***************************************************************************************************************************/ +package org.apache.juneau.html; + +import static org.apache.juneau.BeanContext.*; +import static org.apache.juneau.serializer.SerializerContext.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.parser.*; +import org.junit.*; + +@SuppressWarnings({"rawtypes","serial"}) +public class CommonParserTest { + + //==================================================================================================== + // testFromSerializer + //==================================================================================================== + @Test + public void testFromSerializer() throws Exception { + ReaderParser p = HtmlParser.DEFAULT.clone().setClassLoader(getClass().getClassLoader()); + Map m = null; + String in; + + in = "<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>a</string></td><td><number>1</number></td></tr></table>"; + m = (Map)p.parse(in, Object.class); + assertEquals(1, m.get("a")); + + in = "<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>a</string></td><td><number>1</number></td></tr><tr><td><string>b</string></td><td><string>foo bar</string></td></tr></table>"; + m = (Map)p.parse(in, Object.class); + assertEquals(1, m.get("a")); + assertEquals("foo bar", m.get("b")); + + in = "<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>a</string></td><td><number>1</number></td></tr><tr><td><string>b</string></td><td><string>foo bar</string></td></tr><tr><td><string>c</string></td><td><boolean>false</boolean></td></tr></table>"; + m = (Map)p.parse(in, Object.class); + assertEquals(1, m.get("a")); + assertEquals(false, m.get("c")); + + in = " <table type='object'> <tr> <th> <string> key </string> </th> <th> <string> value </string> </th> </tr> <tr> <td> <string> a </string> </td> <td> <number> 1 </number> </td> </tr> <tr> <td> <string> b </string> </td> <td> <string> foo </string> </td> </tr> <tr> <td> <string> c </string> </td> <td> <boolean> false </boolean> </td> </tr> </table> "; + m = (Map)p.parse(in, Object.class); + assertEquals(1, m.get("a")); + assertEquals("foo", m.get("b")); + assertEquals(false, m.get("c")); + + in = "<table type='array'><tr><th>attribute</th></tr><tr><td><string>value</string></td></tr><tr><td><string>value</string></td></tr></table>"; + ObjectList jl = (ObjectList)p.parse(in, Object.class); + assertEquals("value", jl.getObjectMap(0).getString("attribute")); + assertEquals("value", jl.getObjectMap(1).getString("attribute")); + + A1 t1 = new A1(); + A2 t2 = new A2(); + t2.add(new A3("name0","value0")); + t2.add(new A3("name1","value1")); + t1.list = t2; + in = new HtmlSerializer().setProperty(SERIALIZER_addClassAttrs, true).serialize(t1); + t1 = (A1)p.parse(in, Object.class); + assertEquals("value1", t1.list.get(1).value); + + in = HtmlSerializer.DEFAULT.serialize(t1); + t1 = p.parse(in, A1.class); + assertEquals("value1", t1.list.get(1).value); + } + + public static class A1 { + public A2 list; + } + + public static class A2 extends LinkedList<A3> { + } + + public static class A3 { + public String name, value; + public A3(){} + public A3(String name, String value) { + this.name = name; + this.value = value; + } + } + + //==================================================================================================== + // Correct handling of unknown properties. + //==================================================================================================== + @Test + public void testCorrectHandlingOfUnknownProperties() throws Exception { + ReaderParser p = new HtmlParser().setProperty(BEAN_ignoreUnknownBeanProperties, true); + B t; + + String in = "<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>a</string></td><td><number>1</number></td></tr><tr><td><string>unknown</string></td><td><number>1</number></td></tr><tr><td><string>b</string></td><td><number>2</number></td></tr></table>"; + t = p.parse(in, B.class); + assertEquals(t.a, 1); + assertEquals(t.b, 2); + + try { + p = new HtmlParser(); + p.parse(in, B.class); + fail("Exception expected"); + } catch (ParseException e) {} + } + + public static class B { + public int a, b; + } + + //==================================================================================================== + // Writing to Collection properties with no setters. + //==================================================================================================== + @Test + public void testCollectionPropertiesWithNoSetters() throws Exception { + ReaderParser p = HtmlParser.DEFAULT; + + String in = "<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>ints</string></td><td><ul><li><number>1</number></li><li><number>2</number></li><li><number>3</number></li></ul></td></tr><tr><td><string>beans</string></td><td><table type='array'><tr><th>a</th><th>b</th></tr><tr><td><number>1</number></td><td><number>2</number></td></tr></table></td></tr></table>"; + C t = p.parse(in, C.class); + assertEquals(t.getInts().size(), 3); + assertEquals(t.getBeans().get(0).b, 2); + } + + public static class C { + private Collection<Integer> ints = new LinkedList<Integer>(); + private List<B> beans = new LinkedList<B>(); + public Collection<Integer> getInts() { + return ints; + } + public List<B> getBeans() { + return beans; + } + } + + //==================================================================================================== + // Parser listeners. + //==================================================================================================== + @Test + public void testParserListeners() throws Exception { + final List<String> events = new LinkedList<String>(); + HtmlParser p = new HtmlParser().setProperty(BEAN_ignoreUnknownBeanProperties, true); + p.addListener( + new ParserListener() { + @Override /* ParserListener */ + public <T> void onUnknownProperty(String propertyName, Class<T> beanClass, T bean, int line, int col) { + events.add(propertyName + "," + line + "," + col); + } + } + ); + + String in = "<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>a</string></td><td><number>1</number></td></tr><tr><td><string>unknown</string></td><td><string>/foo</string></td></tr><tr><td><string>b</string></td><td><number>2</number></td></tr></table>"; + p.parse(in, B.class); + assertEquals(1, events.size()); + assertEquals("unknown,-1,-1", events.get(0)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/test/java/org/apache/juneau/html/CommonTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/test/java/org/apache/juneau/html/CommonTest.java b/juneau-core/src/test/java/org/apache/juneau/html/CommonTest.java new file mode 100755 index 0000000..462ceea --- /dev/null +++ b/juneau-core/src/test/java/org/apache/juneau/html/CommonTest.java @@ -0,0 +1,564 @@ +/*************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + ***************************************************************************************************************************/ +package org.apache.juneau.html; + +import static org.apache.juneau.BeanContext.*; +import static org.apache.juneau.TestUtils.*; +import static org.apache.juneau.html.HtmlSerializerContext.*; +import static org.apache.juneau.serializer.SerializerContext.*; +import static org.junit.Assert.*; + +import java.net.*; +import java.net.URI; +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.testbeans.*; +import org.apache.juneau.xml.*; +import org.junit.*; + +@SuppressWarnings("serial") +public class CommonTest { + + //==================================================================================================== + // Trim nulls from beans + //==================================================================================================== + @Test + public void testTrimNullsFromBeans() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + HtmlParser p = HtmlParser.DEFAULT; + A t1 = A.create(), t2; + + s.setProperty(SERIALIZER_trimNullProperties, false); + String r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>s1</string></td><td><null/></td></tr><tr><td><string>s2</string></td><td><string>s2</string></td></tr></table>", r); + t2 = p.parse(r, A.class); + assertEqualObjects(t1, t2); + + s.setProperty(SERIALIZER_trimNullProperties, true); + r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>s2</string></td><td><string>s2</string></td></tr></table>", r); + t2 = p.parse(r, A.class); + assertEqualObjects(t1, t2); + } + + public static class A { + public String s1, s2; + + public static A create() { + A t = new A(); + t.s2 = "s2"; + return t; + } + } + + //==================================================================================================== + // Trim empty maps + //==================================================================================================== + @Test + public void testTrimEmptyMaps() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + HtmlParser p = HtmlParser.DEFAULT; + B t1 = B.create(), t2; + String r; + + s.setProperty(SERIALIZER_trimEmptyMaps, false); + r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr></table></td></tr><tr><td><string>f2</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f2a</string></td><td><null/></td></tr><tr><td><string>f2b</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>s2</string></td><td><string>s2</string></td></tr></table></td></tr></table></td></tr></table>", r); + t2 = p.parse(r, B.class); + assertEqualObjects(t1, t2); + + s.setProperty(SERIALIZER_trimEmptyMaps, true); + r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f2</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f2a</string></td><td><null/></td></tr><tr><td><string>f2b</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>s2</string></td><td><string>s2</string></td></tr></table></td></tr></table></td></tr></table>", r); + t2 = p.parse(r, B.class); + assertNull(t2.f1); + } + + public static class B { + public TreeMap<String,A> f1, f2; + + public static B create() { + B t = new B(); + t.f1 = new TreeMap<String,A>(); + t.f2 = new TreeMap<String,A>(){{put("f2a",null);put("f2b",A.create());}}; + return t; + } + } + + //==================================================================================================== + // Trim empty lists + //==================================================================================================== + @Test + public void testTrimEmptyLists() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + HtmlParser p = HtmlParser.DEFAULT; + C t1 = C.create(), t2; + String r; + + s.setProperty(SERIALIZER_trimEmptyLists, false); + r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><ul></ul></td></tr><tr><td><string>f2</string></td><td><table type='array'><tr><th>s1</th><th>s2</th></tr><tr><null/></tr><tr><td><null/></td><td><string>s2</string></td></tr></table></td></tr></table>", r); + t2 = p.parse(r, C.class); + assertEqualObjects(t1, t2); + + s.setProperty(SERIALIZER_trimEmptyLists, true); + r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f2</string></td><td><table type='array'><tr><th>s1</th><th>s2</th></tr><tr><null/></tr><tr><td><null/></td><td><string>s2</string></td></tr></table></td></tr></table>", r); + t2 = p.parse(r, C.class); + assertNull(t2.f1); + } + + public static class C { + public List<A> f1, f2; + + public static C create() { + C t = new C(); + t.f1 = new LinkedList<A>(); + t.f2 = new LinkedList<A>(){{add(null);add(A.create());}}; + return t; + } + } + + //==================================================================================================== + // Trim empty arrays + //==================================================================================================== + @Test + public void testTrimEmptyArrays() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + HtmlParser p = HtmlParser.DEFAULT; + D t1 = D.create(), t2; + String r; + + s.setProperty(SERIALIZER_trimEmptyLists, false); + r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><ul></ul></td></tr><tr><td><string>f2</string></td><td><table type='array'><tr><th>s1</th><th>s2</th></tr><tr><null/></tr><tr><td><null/></td><td><string>s2</string></td></tr></table></td></tr></table>", r); + t2 = p.parse(r, D.class); + assertEqualObjects(t1, t2); + + s.setProperty(SERIALIZER_trimEmptyLists, true); + r = s.serialize(t1); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f2</string></td><td><table type='array'><tr><th>s1</th><th>s2</th></tr><tr><null/></tr><tr><td><null/></td><td><string>s2</string></td></tr></table></td></tr></table>", r); + t2 = p.parse(r, D.class); + assertNull(t2.f1); + } + + public static class D { + public A[] f1, f2; + + public static D create() { + D t = new D(); + t.f1 = new A[]{}; + t.f2 = new A[]{null, A.create()}; + return t; + } + } + + //==================================================================================================== + // @BeanProperty.properties annotation. + //==================================================================================================== + @Test + public void testBeanPropertyProperties() throws Exception { + HtmlSerializer s = HtmlSerializer.DEFAULT_SQ; + E1 t = new E1(); + String r; + + r = s.serialize(t); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>x1</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><number>1</number></td></tr></table></td></tr><tr><td><string>x2</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><number>1</number></td></tr></table></td></tr><tr><td><string>x3</string></td><td><table type='array'><tr><th>f1</th></tr><tr><td><number>1</number></td></tr></table></td></tr><tr><td><string>x4</string></td><td><table type='array'><tr><th>f1</th></tr><tr><td><number>1</number></td></tr></table></td></tr><tr><td><string>x5</string></td><td><table type='array'><tr><th>f1</th></tr><tr><td><number>1</number></td></tr></table></td></tr><tr><td><string>x6</string></td><td><table type='array'><tr><th>f1</th></tr><tr><td>< number>1</number></td></tr></table></td></tr></table>", r); + r = s.getSchemaSerializer().serialize(new E1()); + assertTrue(r.indexOf("f2") == -1); + } + + public static class E1 { + @BeanProperty(properties={"f1"}) public E2 x1 = new E2(); + @BeanProperty(properties={"f1"}) public Map<String,Integer> x2 = new LinkedHashMap<String,Integer>() {{ + put("f1",1); put("f2",2); + }}; + @BeanProperty(properties={"f1"}) public E2[] x3 = {new E2()}; + @BeanProperty(properties={"f1"}) public List<E2> x4 = new LinkedList<E2>() {{ + add(new E2()); + }}; + @BeanProperty(properties={"f1"}) public ObjectMap[] x5 = {new ObjectMap().append("f1",1).append("f2",2)}; + @BeanProperty(properties={"f1"}) public List<ObjectMap> x6 = new LinkedList<ObjectMap>() {{ + add(new ObjectMap().append("f1",1).append("f2",2)); + }}; + } + + public static class E2 { + public int f1 = 1; + public int f2 = 2; + } + + //==================================================================================================== + // @BeanProperty.properties annotation on list of beans. + //==================================================================================================== + @Test + public void testBeanPropertyPropertiesOnListOfBeans() throws Exception { + HtmlSerializer s = HtmlSerializer.DEFAULT_SQ; + List<F> l = new LinkedList<F>(); + F t = new F(); + t.x1.add(new F()); + l.add(t); + String html = s.serialize(l); + assertEquals("<table type='array'><tr><th>x1</th><th>x2</th></tr><tr><td><table type='array'><tr><th>x2</th></tr><tr><td><number>2</number></td></tr></table></td><td><number>2</number></td></tr></table>", html); + } + + public static class F { + @BeanProperty(properties={"x2"}) public List<F> x1 = new LinkedList<F>(); + public int x2 = 2; + } + + //==================================================================================================== + // Test that URLs and URIs are serialized and parsed correctly. + //==================================================================================================== + @Test + public void testURIAttr() throws Exception { + HtmlSerializer s = HtmlSerializer.DEFAULT_SQ; + HtmlParser p = HtmlParser.DEFAULT; + + G t = new G(); + t.uri = new URI("http://uri"); + t.f1 = new URI("http://f1"); + t.f2 = new URL("http://f2"); + + String html = s.serialize(t); + t = p.parse(html, G.class); + assertEquals("http://uri", t.uri.toString()); + assertEquals("http://f1", t.f1.toString()); + assertEquals("http://f2", t.f2.toString()); + } + + public static class G { + @BeanProperty(beanUri=true) public URI uri; + public URI f1; + public URL f2; + } + + //==================================================================================================== + // Test URIs with URI_CONTEXT and URI_AUTHORITY + //==================================================================================================== + @Test + public void testUris() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq().setProperty(HTML_uriAnchorText, PROPERTY_NAME).setProperty(SERIALIZER_useIndentation, false); + TestURI t = new TestURI(); + String r; + String expected; + + s.setProperty(SERIALIZER_relativeUriBase, null); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='f0/x0'>f0</a>" + +"\n[f1]=<a href='f1/x1'>f1</a>" + +"\n[f2]=<a href='/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='f4/x4'>f4</a>" + +"\n[f5]=<a href='/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='f8/x8'>f8</a>" + +"\n[f9]=<a href='f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_relativeUriBase, ""); // Same as null. + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='f0/x0'>f0</a>" + +"\n[f1]=<a href='f1/x1'>f1</a>" + +"\n[f2]=<a href='/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='f4/x4'>f4</a>" + +"\n[f5]=<a href='/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='f8/x8'>f8</a>" + +"\n[f9]=<a href='f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_relativeUriBase, "/cr"); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>f0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>f1</a>" + +"\n[f2]=<a href='/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>f4</a>" + +"\n[f5]=<a href='/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>f8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_relativeUriBase, "/cr/"); // Same as above + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>f0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>f1</a>" + +"\n[f2]=<a href='/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>f4</a>" + +"\n[f5]=<a href='/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>f8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_relativeUriBase, "/"); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/f0/x0'>f0</a>" + +"\n[f1]=<a href='/f1/x1'>f1</a>" + +"\n[f2]=<a href='/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='/f4/x4'>f4</a>" + +"\n[f5]=<a href='/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='/f8/x8'>f8</a>" + +"\n[f9]=<a href='/f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_relativeUriBase, null); + + s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo"); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='f0/x0'>f0</a>" + +"\n[f1]=<a href='f1/x1'>f1</a>" + +"\n[f2]=<a href='http://foo/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='f4/x4'>f4</a>" + +"\n[f5]=<a href='http://foo/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='f8/x8'>f8</a>" + +"\n[f9]=<a href='f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo/"); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='f0/x0'>f0</a>" + +"\n[f1]=<a href='f1/x1'>f1</a>" + +"\n[f2]=<a href='http://foo/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='f4/x4'>f4</a>" + +"\n[f5]=<a href='http://foo/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='f8/x8'>f8</a>" + +"\n[f9]=<a href='f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_absolutePathUriBase, ""); // Same as null. + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='f0/x0'>f0</a>" + +"\n[f1]=<a href='f1/x1'>f1</a>" + +"\n[f2]=<a href='/f2/x2'>f2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>f3</a>" + +"\n[f4]=<a href='f4/x4'>f4</a>" + +"\n[f5]=<a href='/f5/x5'>f5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>f6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>f7</a>" + +"\n[f8]=<a href='f8/x8'>f8</a>" + +"\n[f9]=<a href='f9/x9'>f9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>fa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>fd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>fe</a>" + ; + assertEquals(expected, r); + } + + private String strip(String html) { + return html + .replace("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr>", "") + .replace("</table>", "") + .replace("<tr><td><string>", "\n[") + .replace("</string></td><td>", "]=") + .replace("</td></tr>", ""); + } + + //==================================================================================================== + // Validate that you cannot update properties on locked serializer. + //==================================================================================================== + @Test + public void testLockedSerializer() throws Exception { + HtmlSerializer s = new HtmlSerializer().lock(); + try { + s.setProperty(XmlSerializerContext.XML_enableNamespaces, true); + fail("Locked exception not thrown"); + } catch (LockedException e) {} + try { + s.setProperty(SerializerContext.SERIALIZER_addClassAttrs, true); + fail("Locked exception not thrown"); + } catch (LockedException e) {} + try { + s.setProperty(BeanContext.BEAN_beanMapPutReturnsOldValue, true); + fail("Locked exception not thrown"); + } catch (LockedException e) {} + } + + //==================================================================================================== + // Recursion + //==================================================================================================== + @Test + public void testRecursion() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + + R1 r1 = new R1(); + R2 r2 = new R2(); + R3 r3 = new R3(); + r1.r2 = r2; + r2.r3 = r3; + r3.r1 = r1; + + // No recursion detection + try { + s.serialize(r1); + fail("Exception expected!"); + } catch (Exception e) { + String msg = e.getLocalizedMessage(); + assertTrue(msg.contains("It's recommended you use the SerializerContext.SERIALIZER_detectRecursions setting to help locate the loop.")); + } + + // Recursion detection, no ignore + s.setProperty(SERIALIZER_detectRecursions, true); + try { + s.serialize(r1); + fail("Exception expected!"); + } catch (Exception e) { + String msg = e.getLocalizedMessage(); + assertTrue(msg.contains("[0]<noname>:org.apache.juneau.html.CommonTest$R1")); + assertTrue(msg.contains("->[1]r2:org.apache.juneau.html.CommonTest$R2")); + assertTrue(msg.contains("->[2]r3:org.apache.juneau.html.CommonTest$R3")); + assertTrue(msg.contains("->[3]r1:org.apache.juneau.html.CommonTest$R1")); + } + + s.setProperty(SERIALIZER_ignoreRecursions, true); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>name</string></td><td><string>foo</string></td></tr><tr><td><string>r2</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>name</string></td><td><string>bar</string></td></tr><tr><td><string>r3</string></td><td><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>name</string></td><td><string>baz</string></td></tr></table></td></tr></table></td></tr></table>", + s.serialize(r1)); + + // Make sure this doesn't blow up. + s.getSchemaSerializer().serialize(r1); + } + + public static class R1 { + public String name = "foo"; + public R2 r2; + } + public static class R2 { + public String name = "bar"; + public R3 r3; + } + public static class R3 { + public String name = "baz"; + public R1 r1; + } + + //==================================================================================================== + // Basic bean + //==================================================================================================== + @Test + public void testBasicBean() throws Exception { + WriterSerializer s = new HtmlSerializer.Sq().setProperty(SERIALIZER_trimNullProperties, false).setProperty(BEAN_sortProperties, true); + + J a = new J(); + a.setF1("J"); + a.setF2(100); + a.setF3(true); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><string>J</string></td></tr><tr><td><string>f2</string></td><td><number>100</number></td></tr><tr><td><string>f3</string></td><td><boolean>true</boolean></td></tr></table>", s.serialize(a)); + } + + public static class J { + private String f1 = null; + private int f2 = -1; + private boolean f3 = false; + + public String getF1() { + return this.f1; + } + + public void setF1(String f1) { + this.f1 = f1; + } + + public int getF2() { + return this.f2; + } + + public void setF2(int f2) { + this.f2 = f2; + } + + public boolean isF3() { + return this.f3; + } + + public void setF3(boolean f3) { + this.f3 = f3; + } + + @Override /* Object */ + public String toString() { + return ("J(f1: " + this.getF1() + ", f2: " + this.getF2() + ")"); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/test/java/org/apache/juneau/html/HtmlTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/test/java/org/apache/juneau/html/HtmlTest.java b/juneau-core/src/test/java/org/apache/juneau/html/HtmlTest.java new file mode 100755 index 0000000..4cf5dfe --- /dev/null +++ b/juneau-core/src/test/java/org/apache/juneau/html/HtmlTest.java @@ -0,0 +1,389 @@ +/*************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + ***************************************************************************************************************************/ +package org.apache.juneau.html; + +import static org.apache.juneau.html.HtmlSerializerContext.*; +import static org.apache.juneau.serializer.SerializerContext.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.annotation.Transform; +import org.apache.juneau.html.annotation.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.testbeans.*; +import org.apache.juneau.transform.*; +import org.junit.*; + +public class HtmlTest { + + //==================================================================================================== + // Verifies that lists of maps/beans are converted to tables correctly. + //==================================================================================================== + @Test + public void testTables1() throws Exception { + HtmlSerializer s = HtmlSerializer.DEFAULT_SQ; + Object[] t; + String html; + + t = new Object[] {new A1(), new A1()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + t = new Object[] {new A1(), new A2()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + t = new Object[] {new A1(), new ObjectMap("{f1:'f1'}")}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + t = new Object[] {new ObjectMap("{f1:'f1'}"), new A1()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + // This should be serialized as a list since the objects have different properties. + t = new Object[] {new A1(), new ObjectMap("{f2:'f2'}")}; + html = s.serialize(t); + assertEquals("<ul><li><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><string>f1</string></td></tr></table></li><li><table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f2</string></td><td><string>f2</string></td></tr></table></li></ul>", html); + + // Tables with some beans with @Bean#properties annotations. + t = new Object[] {new A1(), new A3()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + t = new Object[] {new A3(), new A1()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + // Tables with some beans with @Bean#transforms annotations. + t = new Object[] {new A4(), new A1()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + t = new Object[] {new A1(), new A4()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + t = new Object[] {new A5(), new A1()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + + t = new Object[] {new A1(), new A5()}; + html = s.serialize(t); + assertEquals("<table type='array'><tr><th>f1</th></tr><tr><td><string>f1</string></td></tr><tr><td><string>f1</string></td></tr></table>", html); + } + + public static class A1 { + public String f1 = "f1"; + } + + public static class A2 { + public String f1 = "f1"; + } + + @Bean(properties="f1") + public static class A3 { + public String f1 = "f1"; + public String f2 = "f2"; + } + + @Transform(A4Transform.class) + public static class A4 { + public String f2 = "f2"; + } + + public static class A4Transform extends PojoTransform<A4,A1> { + @Override /* PojoTransform */ + public A1 transform(A4 o) throws SerializeException { + return new A1(); + } + } + + @Transform(A5Transform.class) + public static class A5 { + public String f2 = "f2"; + } + + public static class A5Transform extends PojoTransform<A5,ObjectMap> { + @Override /* PojoTransform */ + public ObjectMap transform(A5 o) { + return new ObjectMap().append("f1", "f1"); + } + } + + //==================================================================================================== + // Test URI_ANCHOR_SET options + //==================================================================================================== + @Test + public void testAnchorTextOptions() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + TestURI t = new TestURI(); + String r; + String expected = null; + + s.setProperty(HTML_uriAnchorText, TO_STRING); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='f0/x0'>f0/x0</a>" + +"\n[f1]=<a href='f1/x1'>f1/x1</a>" + +"\n[f2]=<a href='/f2/x2'>/f2/x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>http://www.ibm.com/f3/x3</a>" + +"\n[f4]=<a href='f4/x4'>f4/x4</a>" + +"\n[f5]=<a href='/f5/x5'>/f5/x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>http://www.ibm.com/f6/x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>http://www.ibm.com/f7/x7</a>" + +"\n[f8]=<a href='f8/x8'>f8/x8</a>" + +"\n[f9]=<a href='f9/x9'>f9/x9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>http://www.ibm.com/fa/xa#MY_LABEL</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</a>"; + assertEquals(expected, r); + + s.setProperty(SERIALIZER_absolutePathUriBase, "http://myhost"); + s.setProperty(SERIALIZER_relativeUriBase, "/cr"); + s.setProperty(HTML_uriAnchorText, TO_STRING); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>f0/x0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>f1/x1</a>" + +"\n[f2]=<a href='http://myhost/f2/x2'>/f2/x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>http://www.ibm.com/f3/x3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>f4/x4</a>" + +"\n[f5]=<a href='http://myhost/f5/x5'>/f5/x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>http://www.ibm.com/f6/x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>http://www.ibm.com/f7/x7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>f8/x8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>f9/x9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>http://www.ibm.com/fa/xa#MY_LABEL</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</a>"; + assertEquals(expected, r); + + s.setProperty(HTML_uriAnchorText, URI); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>/cr/f0/x0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>/cr/f1/x1</a>" + +"\n[f2]=<a href='http://myhost/f2/x2'>http://myhost/f2/x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>http://www.ibm.com/f3/x3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>/cr/f4/x4</a>" + +"\n[f5]=<a href='http://myhost/f5/x5'>http://myhost/f5/x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>http://www.ibm.com/f6/x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>http://www.ibm.com/f7/x7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>/cr/f8/x8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>/cr/f9/x9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>http://www.ibm.com/fa/xa#MY_LABEL</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</a>"; + assertEquals(expected, r); + + s.setProperty(HTML_uriAnchorText, LAST_TOKEN); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>x0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>x1</a>" + +"\n[f2]=<a href='http://myhost/f2/x2'>x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>x3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>x4</a>" + +"\n[f5]=<a href='http://myhost/f5/x5'>x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>x7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>x8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>x9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>xa</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>xd</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>xe</a>"; + assertEquals(expected, r); + + s.setProperty(HTML_uriAnchorText, URI_ANCHOR); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>f0/x0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>f1/x1</a>" + +"\n[f2]=<a href='http://myhost/f2/x2'>/f2/x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>http://www.ibm.com/f3/x3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>f4/x4</a>" + +"\n[f5]=<a href='http://myhost/f5/x5'>/f5/x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>http://www.ibm.com/f6/x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>http://www.ibm.com/f7/x7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>f8/x8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>f9/x9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>MY_LABEL</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</a>"; + assertEquals(expected, r); + + s.setProperty(HTML_labelParameter, "label2"); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>f0/x0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>f1/x1</a>" + +"\n[f2]=<a href='http://myhost/f2/x2'>/f2/x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>http://www.ibm.com/f3/x3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>f4/x4</a>" + +"\n[f5]=<a href='http://myhost/f5/x5'>/f5/x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>http://www.ibm.com/f6/x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>http://www.ibm.com/f7/x7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>f8/x8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>f9/x9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>MY_LABEL</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>MY_LABEL</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>MY_LABEL</a>"; + assertEquals(expected, r); + + s.setProperty(HTML_detectLinksInStrings, false); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>f0/x0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>f1/x1</a>" + +"\n[f2]=<a href='http://myhost/f2/x2'>/f2/x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>http://www.ibm.com/f3/x3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>f4/x4</a>" + +"\n[f5]=<a href='http://myhost/f5/x5'>/f5/x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>http://www.ibm.com/f6/x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>http://www.ibm.com/f7/x7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>f8/x8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>f9/x9</a>" + +"\n[fa]=<string>http://www.ibm.com/fa/xa#MY_LABEL</string>" + +"\n[fb]=<string>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</string>" + +"\n[fc]=<string>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</string>" + +"\n[fd]=<string>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</string>" + +"\n[fe]=<string>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</string>"; + assertEquals(expected, r); + + s.setProperty(HTML_detectLinksInStrings, true); + s.setProperty(HTML_lookForLabelParameters, false); + r = strip(s.serialize(t)); + expected = "" + +"\n[f0]=<a href='/cr/f0/x0'>f0/x0</a>" + +"\n[f1]=<a href='/cr/f1/x1'>f1/x1</a>" + +"\n[f2]=<a href='http://myhost/f2/x2'>/f2/x2</a>" + +"\n[f3]=<a href='http://www.ibm.com/f3/x3'>http://www.ibm.com/f3/x3</a>" + +"\n[f4]=<a href='/cr/f4/x4'>f4/x4</a>" + +"\n[f5]=<a href='http://myhost/f5/x5'>/f5/x5</a>" + +"\n[f6]=<a href='http://www.ibm.com/f6/x6'>http://www.ibm.com/f6/x6</a>" + +"\n[f7]=<a href='http://www.ibm.com/f7/x7'>http://www.ibm.com/f7/x7</a>" + +"\n[f8]=<a href='/cr/f8/x8'>f8/x8</a>" + +"\n[f9]=<a href='/cr/f9/x9'>f9/x9</a>" + +"\n[fa]=<a href='http://www.ibm.com/fa/xa#MY_LABEL'>MY_LABEL</a>" + +"\n[fb]=<a href='http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar'>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</a>" + +"\n[fc]=<a href='http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL'>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</a>" + +"\n[fd]=<a href='http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar'>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</a>" + +"\n[fe]=<a href='http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL'>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</a>"; + assertEquals(expected, r); + } + + private String strip(String html) { + return html + .replace("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr>", "") + .replace("</table>", "") + .replace("<tr><td><string>", "\n[") + .replace("</string></td><td>", "]=") + .replace("</td></tr>", ""); + } + + //==================================================================================================== + // Test @Html.asPlainText annotation on classes and fields + //==================================================================================================== + @Test + public void testHtmlAnnotationAsPlainText() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + Object o = null; + String r; + + o = new B1(); + r = s.serialize(o); + assertEquals("<test>", r); + + o = new B2(); + r = s.serialize(o); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><f1></td></tr></table>", r); + } + + @Html(asPlainText=true) + public static class B1 { + public String f1 = "<f1>"; + @Override /* Object */ + public String toString() { + return "<test>"; + } + } + + public static class B2 { + @Html(asPlainText=true) + public String f1 = "<f1>"; + } + + //==================================================================================================== + // Test @Html.asXml annotation on classes and fields + //==================================================================================================== + @Test + public void testHtmlAnnotationAsXml() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + Object o = null; + String r; + + o = new C1(); + r = s.serialize(o); + assertEquals("<object><f1><f1></f1></object>", r); + + o = new C2(); + r = s.serialize(o); + assertEquals("<table type='object'><tr><th><string>key</string></th><th><string>value</string></th></tr><tr><td><string>f1</string></td><td><string><f1></string></td></tr></table>", r); + } + + @Html(asXml=true) + public static class C1 { + public String f1 = "<f1>"; + } + + public static class C2 { + @Html(asXml=true) + public String f1 = "<f1>"; + } + + //==================================================================================================== + // Test @Html.noTableHeaders + //==================================================================================================== + @Test + public void testNoTableHeaders() throws Exception { + HtmlSerializer s = new HtmlSerializer.Sq(); + Object o = null; + String r; + + Map m = new MyMap(); + m.put("foo", "bar"); + o = new ObjectList().append(m); + r = s.serialize(o); + assertEquals("<ul><li><table type='object'><tr><td><string>foo</string></td><td><string>bar</string></td></tr></table></li></ul>", r); + } + + @Html(noTables=true, noTableHeaders=true) + public static class MyMap extends LinkedHashMap<String,String> {} + +} \ No newline at end of file
