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.TestUtils.*;
20  import static org.apache.juneau.xml.annotation.XmlFormat.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.net.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.annotation.*;
27  import org.apache.juneau.xml.annotation.*;
28  import org.junit.jupiter.api.*;
29  
30  class CommonXml_Test extends TestBase {
31  
32  	//====================================================================================================
33  	// Test 18a - @Bean.uri annotation
34  	//====================================================================================================
35  	@Test void a01_beanUriAnnotation() throws Exception {
36  		var p = XmlParser.DEFAULT;
37  		var s = XmlSerializer.DEFAULT_SQ;
38  
39  		var t = new A("http://foo", 123, "bar");
40  		var xml = s.serialize(t);
41  		assertEquals("<object url='http://foo' id='123'><name>bar</name></object>", xml);
42  
43  		t = p.parse(xml, A.class);
44  		assertEquals("http://foo", t.url.toString());
45  		assertEquals(123, t.id);
46  		assertEquals("bar", t.name);
47  
48  		validateXml(t, s);
49  	}
50  
51  	@Bean(p="url,id,name")
52  	public static class A {
53  		@Xml(format=XmlFormat.ATTR) public URL url;
54  		@Xml(format=ATTR) public int id;
55  		public String name;
56  		public A() {}
57  		public A(String url, int id, String name) {
58  			this.url = url(url);
59  			this.id = id;
60  			this.name = name;
61  		}
62  	}
63  
64  	//====================================================================================================
65  	// Bean.uri annotation, only uri property
66  	//====================================================================================================
67  	@Test void a02_beanUriAnnotationOnlyUriProperty() throws Exception {
68  		var s = XmlSerializer.create().sq().build();
69  
70  		var t = new B("http://foo");
71  		var xml = s.serialize(t);
72  		assertEquals("<object url='http://foo'><url2>http://foo/2</url2></object>", xml);
73  	}
74  
75  	public static class B {
76  		@Xml(format=XmlFormat.ATTR) public URL url;
77  		public URL url2;
78  		public B() {}
79  		public B(String url) {
80  			this.url = url(url);
81  			this.url2 = url(url+"/2");
82  		}
83  	}
84  }