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