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