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.xml;
18  
19  import static org.apache.juneau.common.utils.Utils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.function.*;
23  
24  import javax.xml.stream.*;
25  import javax.xml.stream.events.*;
26  import javax.xml.stream.util.*;
27  
28  import org.apache.juneau.*;
29  import org.apache.juneau.internal.*;
30  import org.apache.juneau.reflect.*;
31  import org.apache.juneau.svl.*;
32  import org.apache.juneau.xml.annotation.*;
33  import org.junit.jupiter.api.*;
34  
35  /**
36   * Tests the @XmlConfig annotation.
37   */
38  class XmlConfigAnnotationTest extends TestBase {
39  
40  	private static void check(String expected, Object o) {
41  		assertEquals(expected, TO_STRING.apply(o));
42  	}
43  
44  	private static final Function<Object,String> TO_STRING = t -> {
45  		if (t == null)
46  			return null;
47  		if (isArray(t))
48  			return XmlConfigAnnotationTest.TO_STRING.apply(ArrayUtils.toList(t, Object.class));
49  		if (t instanceof AA)
50  			return "AA";
51  		if (t instanceof AB)
52  			return "AB";
53  		if (t instanceof AC)
54  			return "AC";
55  		return t.toString();
56  	};
57  
58  	static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
59  
60  	//-----------------------------------------------------------------------------------------------------------------
61  	// Basic tests
62  	//-----------------------------------------------------------------------------------------------------------------
63  
64  	public static class AA extends XmlEventAllocator {
65  		@Override
66  		public XMLEventAllocator newInstance() {
67  			return null;
68  		}
69  		@Override
70  		public XMLEvent allocate(XMLStreamReader reader) throws XMLStreamException {
71  			return null;
72  		}
73  		@Override
74  		public void allocate(XMLStreamReader reader, XMLEventConsumer consumer) throws XMLStreamException {/* no-op */}
75  	}
76  	public static class AB extends XmlReporter {
77  		@Override
78  		public void report(String message, String errorType, Object relatedInformation, Location location) throws XMLStreamException {/* no-op */}
79  	}
80  	public static class AC extends XmlResolver {
81  		@Override
82  		public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
83  			return null;
84  		}
85  	}
86  
87  	@XmlConfig(
88  		addBeanTypes="$X{true}",
89  		addNamespaceUrisToRoot="$X{true}",
90  		disableAutoDetectNamespaces="$X{true}",
91  		defaultNamespace="$X{foo}",
92  		enableNamespaces="$X{true}",
93  		eventAllocator=AA.class,
94  		namespaces="$X{foo}",
95  		preserveRootElement="$X{true}",
96  		reporter=AB.class,
97  		resolver=AC.class,
98  		validating="$X{true}"
99  	)
100 	static class A {}
101 	static ClassInfo a = ClassInfo.of(A.class);
102 
103 	@Test void basicSerializer() {
104 		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
105 		var x = XmlSerializer.create().apply(al).build().getSession();
106 		check("true", x.isAddBeanTypes());
107 		check("true", x.isAddNamespaceUrisToRoot());
108 		check("false", x.isAutoDetectNamespaces());
109 		check("foo:null", x.getDefaultNamespace());
110 		check("true", x.isEnableNamespaces());
111 		check("[foo:null]", x.getNamespaces());
112 	}
113 
114 	@Test void basicParser() {
115 		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
116 		var x = XmlParser.create().apply(al).build().getSession();
117 		check("AA", x.getEventAllocator());
118 		check("true", x.isPreserveRootElement());
119 		check("AB", x.getReporter());
120 		check("AC", x.getResolver());
121 		check("true", x.isValidating());
122 	}
123 
124 	//-----------------------------------------------------------------------------------------------------------------
125 	// Annotation with no values.
126 	//-----------------------------------------------------------------------------------------------------------------
127 
128 	@XmlConfig()
129 	static class B {}
130 	static ClassInfo b = ClassInfo.of(B.class);
131 
132 	@Test void noValuesSerializer() {
133 		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
134 		var x = XmlSerializer.create().apply(al).build().getSession();
135 		check("false", x.isAddBeanTypes());
136 		check("false", x.isAddNamespaceUrisToRoot());
137 		check("true", x.isAutoDetectNamespaces());
138 		check("juneau:http://www.apache.org/2013/Juneau", x.getDefaultNamespace());
139 		check("false", x.isEnableNamespaces());
140 		check("[]", x.getNamespaces());
141 	}
142 
143 	@Test void noValuesParser() {
144 		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
145 		var x = XmlParser.create().apply(al).build().getSession();
146 		check(null, x.getEventAllocator());
147 		check("false", x.isPreserveRootElement());
148 		check(null, x.getReporter());
149 		check(null, x.getResolver());
150 		check("false", x.isValidating());
151 	}
152 
153 	//-----------------------------------------------------------------------------------------------------------------
154 	// No annotation.
155 	//-----------------------------------------------------------------------------------------------------------------
156 
157 	static class C {}
158 	static ClassInfo c = ClassInfo.of(C.class);
159 
160 	@Test void noAnnotationSerializer() {
161 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
162 		var x = XmlSerializer.create().apply(al).build().getSession();
163 		check("false", x.isAddBeanTypes());
164 		check("false", x.isAddNamespaceUrisToRoot());
165 		check("true", x.isAutoDetectNamespaces());
166 		check("juneau:http://www.apache.org/2013/Juneau", x.getDefaultNamespace());
167 		check("false", x.isEnableNamespaces());
168 		check("[]", x.getNamespaces());
169 	}
170 
171 	@Test void noAnnotationParser() {
172 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
173 		var x = XmlParser.create().apply(al).build().getSession();
174 		check(null, x.getEventAllocator());
175 		check("false", x.isPreserveRootElement());
176 		check(null, x.getReporter());
177 		check(null, x.getResolver());
178 		check("false", x.isValidating());
179 	}
180 }