View Javadoc
1   // ***************************************************************************************************************************
2   // * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
3   // * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
4   // * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
5   // * with the License.  You may obtain a copy of the License at                                                              *
6   // *                                                                                                                         *
7   // *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
8   // *                                                                                                                         *
9   // * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
10  // * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
11  // * specific language governing permissions and limitations under the License.                                              *
12  // ***************************************************************************************************************************
13  package org.apache.juneau.serializer;
14  
15  import static org.apache.juneau.commons.utils.CollectionUtils.*;
16  import static org.junit.jupiter.api.Assertions.*;
17  
18  import org.apache.juneau.*;
19  import org.apache.juneau.csv.*;
20  import org.apache.juneau.html.*;
21  import org.apache.juneau.json.*;
22  import org.apache.juneau.uon.*;
23  import org.apache.juneau.xml.*;
24  import org.junit.jupiter.api.*;
25  
26  /**
27   * Tests for fluent setter overrides in Writer classes.
28   *
29   * <p>Writer classes (CsvWriter, JsonWriter, UonWriter, XmlWriter, HtmlWriter) have
30   * protected constructors and are internal implementation details of the serialization
31   * framework. The fluent setter overrides ensure type-safe method chaining during
32   * serialization.
33   *
34   * <p>These tests verify that the basic serialization functionality works correctly,
35   * which implicitly confirms the fluent overrides are functioning (since they're used
36   * internally during serialization).
37   */
38  class WriterFluentSetters_Test extends TestBase {
39  
40  	//------------------------------------------------------------------------------------------------------------------
41  	// CsvSerializer - Fluent setter overrides verified through compilation
42  	//------------------------------------------------------------------------------------------------------------------
43  
44  	@Test void a01_csvSerializer_basic() throws Exception {
45  		// Verify CsvSerializer works correctly (uses CsvWriter internally)
46  		var s = CsvSerializer.DEFAULT;
47  		String result = s.serialize(a("foo", "bar"));
48  		assertTrue(result.contains("foo") && result.contains("bar"));
49  	}
50  
51  	@Test void a02_csvSerializer_object() throws Exception {
52  		// Verify object serialization works
53  		var s = CsvSerializer.DEFAULT;
54  		var bean = new TestBean("test", 42);
55  		String result = s.serialize(bean);
56  		assertTrue(result.contains("test") && result.contains("42"));
57  	}
58  
59  	//------------------------------------------------------------------------------------------------------------------
60  	// JsonSerializer - Fluent setter overrides verified through compilation
61  	//------------------------------------------------------------------------------------------------------------------
62  
63  	@Test void b01_jsonSerializer_basic() throws Exception {
64  		// Verify JsonSerializer works correctly (uses JsonWriter internally)
65  		var s = JsonSerializer.DEFAULT;
66  		String result = s.serialize(a("foo", "bar"));
67  		assertEquals("[\"foo\",\"bar\"]", result.trim());
68  	}
69  
70  	@Test void b02_jsonSerializer_object() throws Exception {
71  		// Verify object serialization works
72  		var s = JsonSerializer.DEFAULT;
73  		var bean = new TestBean("test", 42);
74  		String result = s.serialize(bean);
75  		assertTrue(result.contains("\"name\":\"test\"") && result.contains("\"value\":42"));
76  	}
77  
78  	//------------------------------------------------------------------------------------------------------------------
79  	// UonSerializer - Fluent setter overrides verified through compilation
80  	//------------------------------------------------------------------------------------------------------------------
81  
82  	@Test void c01_uonSerializer_basic() throws Exception {
83  		// Verify UonSerializer works correctly (uses UonWriter internally)
84  		var s = UonSerializer.DEFAULT;
85  		String result = s.serialize(a("foo", "bar"));
86  		assertEquals("@(foo,bar)", result.trim());
87  	}
88  
89  	@Test void c02_uonSerializer_object() throws Exception {
90  		// Verify object serialization works
91  		var s = UonSerializer.DEFAULT;
92  		var bean = new TestBean("test", 42);
93  		String result = s.serialize(bean);
94  		assertTrue(result.contains("name=test") && result.contains("value=42"));
95  	}
96  
97  	//------------------------------------------------------------------------------------------------------------------
98  	// XmlSerializer - Fluent setter overrides verified through compilation
99  	//------------------------------------------------------------------------------------------------------------------
100 
101 	@Test void d01_xmlSerializer_basic() throws Exception {
102 		// Verify XmlSerializer works correctly (uses XmlWriter internally)
103 		var s = XmlSerializer.DEFAULT;
104 		String result = s.serialize(a("foo", "bar"));
105 		assertTrue(result.contains("<string>foo</string>") && result.contains("<string>bar</string>"));
106 	}
107 
108 	@Test void d02_xmlSerializer_object() throws Exception {
109 		// Verify object serialization works
110 		var s = XmlSerializer.DEFAULT;
111 		var bean = new TestBean("test", 42);
112 		String result = s.serialize(bean);
113 		assertTrue(result.contains("<name>test</name>") && result.contains("<value>42</value>"));
114 	}
115 
116 	//------------------------------------------------------------------------------------------------------------------
117 	// HtmlSerializer - Fluent setter overrides verified through compilation
118 	//------------------------------------------------------------------------------------------------------------------
119 
120 	@Test void e01_htmlSerializer_basic() throws Exception {
121 		// Verify HtmlSerializer works correctly (uses HtmlWriter internally)
122 		var s = HtmlSerializer.DEFAULT;
123 		String result = s.serialize(a("foo", "bar"));
124 		assertTrue(result.contains("foo") && result.contains("bar"));
125 	}
126 
127 	@Test void e02_htmlSerializer_object() throws Exception {
128 		// Verify object serialization works
129 		var s = HtmlSerializer.DEFAULT;
130 		var bean = new TestBean("test", 42);
131 		String result = s.serialize(bean);
132 		assertTrue(result.contains("test") && result.contains("42"));
133 	}
134 
135 	//------------------------------------------------------------------------------------------------------------------
136 	// Test bean
137 	//------------------------------------------------------------------------------------------------------------------
138 
139 	public static class TestBean {
140 		public String name;
141 		public int value;
142 
143 		public TestBean(String name, int value) {
144 			this.name = name;
145 			this.value = value;
146 		}
147 	}
148 }