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.a.rttests;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.commons.utils.CollectionUtils.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.collections.*;
24  import org.apache.juneau.parser.*;
25  import org.apache.juneau.serializer.*;
26  import org.junit.jupiter.params.*;
27  import org.junit.jupiter.params.provider.*;
28  
29  /**
30   * Tests for the {@link Serializer#SERIALIZER_trimStrings} and {@link Parser#PARSER_trimStrings}.
31   */
32  class TrimStrings_RoundTripTest extends RoundTripTest_Base {
33  
34  	//====================================================================================================
35  	// test
36  	//====================================================================================================
37  
38  	@ParameterizedTest
39  	@MethodSource("testers")
40  	void a01_basic(RoundTrip_Tester t) throws Exception {
41  		if (t.isValidationOnly())
42  			return;
43  		var s = t.getSerializer();
44  		var p = t.getParser();
45  
46  		var s2 = s.copy().trimStrings().build();
47  		var p2 = p.copy().trimStrings().build();
48  
49  		var in = (Object)" foo bar ";
50  		var e = (Object)"foo bar";
51  		var a = (Object)p.parse(s2.serialize(in), String.class);
52  		assertEquals(json(a), json(e));
53  		a = p2.parse(s.serialize(in), String.class);
54  		assertEquals(json(a), json(e));
55  
56  		in = JsonMap.ofJson("{' foo ': ' bar '}");
57  		e = JsonMap.ofJson("{foo:'bar'}");
58  		a = p.parse(s2.serialize(in), JsonMap.class);
59  		assertEquals(json(a), json(e));
60  		a = p2.parse(s.serialize(in), JsonMap.class);
61  		assertEquals(json(a), json(e));
62  
63  		in = new JsonList("[' foo ', {' foo ': ' bar '}]");
64  		e = new JsonList("['foo',{foo:'bar'}]");
65  		a = p.parse(s2.serialize(in), JsonList.class);
66  		assertEquals(json(a), json(e));
67  		a = p2.parse(s.serialize(in), JsonList.class);
68  		assertEquals(json(a), json(e));
69  
70  		in = new A().init1();
71  		e = new A().init2();
72  		a = p.parse(s2.serialize(in), A.class);
73  		assertEquals(json(a), json(e));
74  		a = p2.parse(s.serialize(in), A.class);
75  		assertEquals(json(a), json(e));
76  	}
77  
78  	public static class A {
79  		public String f1;
80  		public String[] f2;
81  		public JsonList f3;
82  		public JsonMap f4;
83  
84  		public A init1() throws Exception {
85  			f1 = " f1 ";
86  			f2 = a(" f2a ", " f2b ");
87  			f3 = JsonList.ofJson("[' f3a ',' f3b ']");
88  			f4 = JsonMap.ofJson("{' foo ':' bar '}");
89  			return this;
90  		}
91  
92  		public A init2() throws Exception {
93  			f1 = "f1";
94  			f2 = a("f2a", "f2b");
95  			f3 = JsonList.ofJson("['f3a','f3b']");
96  			f4 = JsonMap.ofJson("{'foo':'bar'}");
97  			return this;
98  		}
99  	}
100 }