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.marshaller;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.collections.*;
27  import org.junit.jupiter.api.*;
28  
29  class Html_Test extends TestBase {
30  
31  	@Test void a01_to() throws Exception {
32  		var in1 = "foo";
33  		var in2 = JsonMap.of("foo", "bar");
34  		var expected1 = "<string>foo</string>";
35  		var expected2 = "<table><tr><td>foo</td><td>bar</td></tr></table>";
36  
37  		assertString(expected1, Html.of(in1));
38  		assertString(expected1, Html.of(in1,stringWriter()));
39  		assertString(expected2, Html.of(in2));
40  		assertString(expected2, Html.of(in2,stringWriter()));
41  	}
42  
43  	@Test void a02_from() throws Exception {
44  		var in1 = "<string>foo</string>";
45  		var in2 = "<table><tr><td>foo</td><td>bar</td></tr></table>";
46  		var expected1 = "foo";
47  		var expected2 = "{foo:'bar'}";
48  
49  		assertEquals(expected1, Html.to(in1, String.class));
50  		assertEquals(expected1, Html.to(stringReader(in1), String.class));
51  		assertJson(expected2, Html.to(in2, Map.class, String.class, String.class));
52  		assertJson(expected2, Html.to(stringReader(in2), Map.class, String.class, String.class));
53  	}
54  
55  	//-----------------------------------------------------------------------------------------------------------------
56  	// Helper methods
57  	//-----------------------------------------------------------------------------------------------------------------
58  
59  	private Writer stringWriter() {
60  		return new StringWriter();
61  	}
62  
63  	private Reader stringReader(String s) {
64  		return new StringReader(s);
65  	}
66  }