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.transforms;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.MediaType;
23  import org.apache.juneau.html.*;
24  import org.apache.juneau.json.*;
25  import org.apache.juneau.serializer.*;
26  import org.apache.juneau.serializer.WriterSerializer.Builder;
27  import org.apache.juneau.swap.*;
28  import org.apache.juneau.xml.*;
29  import org.junit.jupiter.api.*;
30  
31  /**
32   * Tests the example code in the ObjectSwap class.
33   */
34  class ObjectSwapTest extends TestBase {
35  
36  	public static class MyPojo {}
37  
38  	public static class MyJsonSwap extends ObjectSwap<MyPojo,String> {
39  
40  		@Override
41  		public MediaType[] forMediaTypes() {
42  			return MediaType.ofAll("*/json");
43  		}
44  
45  		@Override
46  		public String swap(BeanSession session, MyPojo o) throws Exception {
47  			return "It's JSON!";
48  		}
49  	}
50  
51  	public static class MyXmlSwap extends ObjectSwap<MyPojo,String> {
52  
53  		@Override
54  		public MediaType[] forMediaTypes() {
55  			return MediaType.ofAll("*/xml");
56  		}
57  
58  		@Override
59  		public String swap(BeanSession session, MyPojo o) throws Exception {
60  			return "It's XML!";
61  		}
62  	}
63  
64  	public static class MyOtherSwap extends ObjectSwap<MyPojo,String> {
65  
66  		@Override
67  		public MediaType[] forMediaTypes() {
68  			return MediaType.ofAll("*/*");
69  		}
70  
71  		@Override
72  		public String swap(BeanSession session, MyPojo o) throws Exception {
73  			return "It's something else!";
74  		}
75  	}
76  
77  	@Test void doTest() throws Exception {
78  
79  		var s = SerializerSet.create()
80  			.add(JsonSerializer.class, XmlSerializer.class, HtmlSerializer.class)
81  			.forEach(WriterSerializer.Builder.class, Builder::sq)
82  			.forEach(Serializer.Builder.class, x -> x.swaps(MyJsonSwap.class, MyXmlSwap.class, MyOtherSwap.class))
83  			.build();
84  
85  		var myPojo = new MyPojo();
86  
87  		var json = s.getWriterSerializer("text/json").serialize(myPojo);
88  		assertEquals("'It\\'s JSON!'", json);
89  
90  		var xml = s.getWriterSerializer("text/xml").serialize(myPojo);
91  		assertEquals("<string>It's XML!</string>", xml);
92  
93  		var html = s.getWriterSerializer("text/html").serialize(myPojo);
94  		assertEquals("<string>It's something else!</string>", html);
95  	}
96  }