View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.html;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.util.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.annotation.*;
25  import org.apache.juneau.testutils.pojos.*;
26  import org.junit.jupiter.api.*;
27  
28  /**
29   * Most of the heavy testing for HtmlSchemaSerializer is done in JsonSchemaGeneratorTest.
30   */
31  class HtmlSchemaSerializer_Test extends TestBase {
32  
33  	//====================================================================================================
34  	// Simple objects
35  	//====================================================================================================
36  	@Test void simpleObjects() throws Exception {
37  		var s = HtmlSchemaSerializer.DEFAULT_SIMPLE;
38  
39  		assertEquals("<table><tr><td>type</td><td>integer</td></tr><tr><td>format</td><td>int16</td></tr></table>", s.serialize((short)1));
40  		assertEquals("<table><tr><td>type</td><td>integer</td></tr><tr><td>format</td><td>int32</td></tr></table>", s.serialize(1));
41  		assertEquals("<table><tr><td>type</td><td>integer</td></tr><tr><td>format</td><td>int64</td></tr></table>", s.serialize(1L));
42  		assertEquals("<table><tr><td>type</td><td>number</td></tr><tr><td>format</td><td>float</td></tr></table>", s.serialize(1f));
43  		assertEquals("<table><tr><td>type</td><td>number</td></tr><tr><td>format</td><td>double</td></tr></table>", s.serialize(1d));
44  		assertEquals("<table><tr><td>type</td><td>boolean</td></tr></table>", s.serialize(true));
45  		assertEquals("<table><tr><td>type</td><td>string</td></tr></table>", s.serialize("foo"));
46  		assertEquals("<table><tr><td>type</td><td>string</td></tr></table>", s.serialize(new StringBuilder("foo")));
47  		assertEquals("<table><tr><td>type</td><td>string</td></tr></table>", s.serialize('c'));
48  		assertEquals("<table><tr><td>type</td><td>string</td></tr><tr><td>enum</td><td><ul><li>one</li><li>two</li><li>three</li></ul></td></tr></table>", s.serialize(TestEnumToString.ONE));
49  		assertEquals("<table><tr><td>type</td><td>object</td></tr><tr><td>properties</td><td><table><tr><td>f1</td><td><table><tr><td>type</td><td>string</td></tr></table></td></tr></table></td></tr></table>", s.serialize(new SimpleBean()));
50  	}
51  
52  	public static class SimpleBean {
53  		public String f1;
54  	}
55  
56  	//====================================================================================================
57  	// Documentation examples
58  	//====================================================================================================
59  
60  	@Bean(properties="name,birthDate,addresses")
61  	public static class Person {
62  		public String name;
63  		public Calendar birthDate;
64  		public List<Address> addresses;
65  	}
66  
67  	@Bean(properties="street,city,state,zip,isCurrent")
68  	public static class Address {
69  		public String street, city;
70  		public StateEnum state;
71  		public int zip;
72  		public boolean isCurrent;
73  	}
74  
75  	public enum StateEnum {
76  		AL,PA,NC
77  	}
78  
79  	@Test void documentationExample() throws Exception {
80  		var s = HtmlSchemaSerializer.DEFAULT_SIMPLE;
81  		assertEquals("<table><tr><td>type</td><td>object</td></tr><tr><td>properties</td><td><table><tr><td>name</td><td><table><tr><td>type</td><td>string</td></tr></table></td></tr><tr><td>birthDate</td><td><table><tr><td>type</td><td>string</td></tr></table></td></tr><tr><td>addresses</td><td><table><tr><td>type</td><td>array</td></tr><tr><td>items</td><td><table><tr><td>type</td><td>object</td></tr><tr><td>properties</td><td><table><tr><td>street</td><td><table><tr><td>type</td><td>string</td></tr></table></td></tr><tr><td>city</td><td><table><tr><td>type</td><td>string</td></tr></table></td></tr><tr><td>state</td><td><table><tr><td>type</td><td>string</td></tr><tr><td>enum</td><td><ul><li>AL</li><li>PA</li><li>NC</li></ul></td></tr></table></td></tr><tr><td>zip</td><td><table><tr><td>type</td><td>integer</td></tr><tr><td>format</td><td>int32</td></tr></table></td></tr><tr><td>isCurrent</td><td><table><tr><td>type</td><td>boolean</td></tr></table></td></tr></table></td></tr></table></td></tr></table></td></tr></table></td></tr></table>", s.serialize(Person.class));
82  	}
83  }