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.objecttools;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.api.*;
24  
25  /**
26   * Tests the PojoPaginator class.
27   */
28  class ObjectSorter_Test extends TestBase {
29  
30  	ObjectSorter os = new ObjectSorter();
31  	BeanSession bs = BeanContext.DEFAULT_SESSION;
32  
33  	//-----------------------------------------------------------------------------------------------------------------
34  	// Null input
35  	//-----------------------------------------------------------------------------------------------------------------
36  
37  	@Test void a01_nullInput() {
38  		assertNull(os.run(bs, null, null));
39  	}
40  
41  	@Test void a02_emptySort() {
42  		var in = set(A.create("c"),A.create("a"),A.create("b"));
43  		assertBeans(os.run(bs, in, sa("")), "f", "c", "a", "b");
44  		assertBeans(os.run(in, ""), "f", "c", "a", "b");
45  	}
46  
47  	@Test void a03_invalidDataType() {
48  		var in = map("a","b");
49  		assertBean(os.run(bs, in, sa("x")), "a", "b");
50  		assertNull(os.run(in, "x"));
51  	}
52  
53  	//-----------------------------------------------------------------------------------------------------------------
54  	// Arrays
55  	//-----------------------------------------------------------------------------------------------------------------
56  
57  	public static class A {
58  		public String f;
59  
60  		public static A create(String f) {
61  			var a = new A();
62  			a.f = f;
63  			return a;
64  		}
65  	}
66  
67  	@Test void b01_beanArray() {
68  		var in = a(A.create("c"),A.create("a"),A.create("b"),A.create("e"),A.create("d"));
69  		assertBeans(os.run(bs, in, sa("f")), "f", "a", "b", "c", "d", "e");
70  		assertBeans(os.run(in, "f"), "f", "a", "b", "c", "d", "e");
71  	}
72  
73  	@Test void b02_beanArray_reverse() {
74  		var in = a(A.create("c"),A.create("a"),A.create("b"),A.create("e"),A.create("d"));
75  		assertBeans(os.run(bs, in, sa("f-")), "f", "e", "d", "c", "b", "a");
76  		assertBeans(os.run(in, "f-"), "f", "e", "d", "c", "b", "a");
77  	}
78  
79  	@Test void b03_beanArrayContainingNulls() {
80  		var in = a(A.create("c"),A.create("a"),null,null,A.create("b"));
81  		assertBeans(os.run(bs, in, sa("f")), "f", "<null>", "<null>", "a", "b", "c");
82  		assertBeans(os.run(in, "f"), "f", "<null>", "<null>", "a", "b", "c");
83  	}
84  
85  	@Test void b04_beanArrayContainingDups() {
86  		var in = a(A.create("c"),A.create("a"),null,A.create("a"),A.create("b"));
87  		assertBeans(os.run(bs, in, sa("f")), "f", "<null>", "a", "a", "b", "c");
88  		assertBeans(os.run(in, "f"), "f", "<null>", "a", "a", "b", "c");
89  	}
90  
91  	//-----------------------------------------------------------------------------------------------------------------
92  	// Lists
93  	//-----------------------------------------------------------------------------------------------------------------
94  
95  	@Test void c01_beanList() {
96  		var in = list(A.create("c"),A.create("a"),A.create("b"),A.create("e"),A.create("d"));
97  		assertBeans(os.run(bs, in, sa("f")), "f", "a", "b", "c", "d", "e");
98  		assertBeans(os.run(in, "f"), "f", "a", "b", "c", "d", "e");
99  	}
100 
101 	@Test void c02_beanList_reverse() {
102 		var in = list(A.create("c"),A.create("a"),A.create("b"),A.create("e"),A.create("d"));
103 		assertBeans(os.run(bs, in, sa("f-")), "f", "e", "d", "c", "b", "a");
104 		assertBeans(os.run(in, "f-"), "f", "e", "d", "c", "b", "a");
105 	}
106 
107 	@Test void c03_beanListContainingNull() {
108 		var in = list(A.create("c"),A.create("a"),null,null,A.create("b"));
109 		assertBeans(os.run(bs, in, sa("f")), "f", "<null>", "<null>", "a", "b", "c");
110 		assertBeans(os.run(in, "f"), "f", "<null>", "<null>", "a", "b", "c");
111 	}
112 
113 	@Test void c04_beanListContainingDups() {
114 		var in = list(A.create("c"),A.create("a"),null,A.create("a"),A.create("b"));
115 		assertBeans(os.run(bs, in, sa("f")), "f", "<null>", "a", "a", "b", "c");
116 		assertBeans(os.run(in, "f"), "f", "<null>", "a", "a", "b", "c");
117 	}
118 
119 	//-----------------------------------------------------------------------------------------------------------------
120 	// Sets
121 	//-----------------------------------------------------------------------------------------------------------------
122 
123 	@Test void d01_beanSet() {
124 		var in = set(A.create("c"),A.create("a"),A.create("b"),A.create("e"),A.create("d"));
125 		assertBeans(os.run(bs, in, sa("f")), "f", "a", "b", "c", "d", "e");
126 		assertBeans(os.run(in, "f"), "f", "a", "b", "c", "d", "e");
127 	}
128 
129 	@Test void d02_beanSet_reverse() {
130 		var in = set(A.create("c"),A.create("a"),A.create("b"),A.create("e"),A.create("d"));
131 		assertBeans(os.run(bs, in, sa("f-")), "f", "e", "d", "c", "b", "a");
132 		assertBeans(os.run(in, "f-"), "f", "e", "d", "c", "b", "a");
133 	}
134 
135 	@Test void d03_beanSetContainingNull() {
136 		var in = set(A.create("c"),A.create("a"),null,null,A.create("b"));
137 		assertBeans(os.run(bs, in, sa("f")), "f", "<null>", "a", "b", "c");
138 		assertBeans(os.run(in, "f"), "f", "<null>", "a", "b", "c");
139 	}
140 
141 	@Test void d04_beanSetContainingDups() {
142 		var in = set(A.create("c"),A.create("a"),null,A.create("a"),A.create("b"));
143 		assertBeans(os.run(bs, in, sa("f")), "f", "<null>", "a", "a", "b", "c");
144 		assertBeans(os.run(in, "f"), "f", "<null>", "a", "a", "b", "c");
145 	}
146 
147 	//-----------------------------------------------------------------------------------------------------------------
148 	// Lists of Maps
149 	//-----------------------------------------------------------------------------------------------------------------
150 
151 	@Test void e01_listOfMaps() {
152 		var in = list(map("f","c"),map("f","a"),map("f","b"),map("f","e"),map("f","d"));
153 		var sa = sa("f");
154 		assertBeans(os.run(bs, in, sa), "f", "a", "b", "c", "d", "e");
155 		assertBeans(os.run(in, "f"), "f", "a", "b", "c", "d", "e");
156 	}
157 
158 	@Test void e02_listOfMaps_reverse() {
159 		var in = list(map("f","c"),map("f","a"),map("f","b"),map("f","e"),map("f","d"));
160 		assertBeans(os.run(bs, in, sa("f-")), "f", "e", "d", "c", "b", "a");
161 		assertBeans(os.run(in, "f-"), "f", "e", "d", "c", "b", "a");
162 	}
163 
164 	//-----------------------------------------------------------------------------------------------------------------
165 	// Lists of Other
166 	//-----------------------------------------------------------------------------------------------------------------
167 
168 	@Test void f01_listOfOther() {
169 		var in = list(list("c"),list("a"),list("b"));
170 		assertList(os.run(bs, in, sa("f")), "[c]", "[a]", "[b]");
171 		assertList(os.run(in, "f"), "[c]", "[a]", "[b]");
172 	}
173 
174 	@Test void f02_listOfOther_reverse() {
175 		var in = list(list("c"),list("a"),list("b"));
176 		assertList(os.run(bs, in, sa("f-")), "[c]", "[a]", "[b]");
177 		assertList(os.run(in, "f-"), "[c]", "[a]", "[b]");
178 	}
179 
180 	//-----------------------------------------------------------------------------------------------------------------
181 	// Other
182 	//-----------------------------------------------------------------------------------------------------------------
183 
184 	@Test void g01_nonExistentField() {
185 		var in = a(A.create("c"),A.create("a"),A.create("b"),A.create("e"),A.create("d"));
186 		assertBeans(os.run(bs, in, sa("fx")), "f", "c", "a", "b", "e", "d");
187 		assertBeans(os.run(in, "fx"), "f", "c", "a", "b", "e", "d");
188 	}
189 
190 	public static class B {
191 		public Object f;
192 
193 		public static B create(Object f) {
194 			var b = new B();
195 			b.f = f;
196 			return b;
197 		}
198 	}
199 
200 	// Should gracefully handle different sorting data types.
201 	@Test void g02_mixtureOfTypes() {
202 		var in = a(B.create(1),B.create(true),B.create("a"));
203 		assertBeans(os.run(bs, in, sa("f")), "f", "1", "true", "a");
204 		assertBeans(os.run(in, "f"), "f", "1", "true", "a");
205 	}
206 
207 	//-----------------------------------------------------------------------------------------------------------------
208 	// Sort by multiple columns.
209 	//-----------------------------------------------------------------------------------------------------------------
210 
211 	public static class C {
212 		public int f1;
213 		public float f2;
214 
215 		public static C create(int f1, float f2) {
216 			var c = new C();
217 			c.f1 = f1;
218 			c.f2 = f2;
219 			return c;
220 		}
221 	}
222 
223 	@Test void h01_sortMultipleColumns() {
224 		var in = a(C.create(1,1),C.create(3,2),C.create(3,1),C.create(2,1),C.create(2,2));
225 		assertBeans(os.run(bs, in, sa("f1,f2")), "f1,f2", "1,1.0", "2,1.0", "2,2.0", "3,1.0", "3,2.0");
226 		assertBeans(os.run(in, "f1,f2"), "f1,f2", "1,1.0", "2,1.0", "2,2.0", "3,1.0", "3,2.0");
227 	}
228 
229 	@Test void h02_sortMultipleColumns_descending() {
230 		var in = a(C.create(1,1),C.create(3,2),C.create(3,1),C.create(2,1),C.create(2,2));
231 		assertBeans(os.run(bs, in, sa("f1-,f2-")), "f1,f2", "3,2.0", "3,1.0", "2,2.0", "2,1.0", "1,1.0");
232 		assertBeans(os.run(in, "f1-,f2-"), "f1,f2", "3,2.0", "3,1.0", "2,2.0", "2,1.0", "1,1.0");
233 	}
234 
235 	@Test void h03_sortMultipleColumns_ascendingAndDescending() {
236 		var in = a(C.create(1,1),C.create(3,2),C.create(3,1),C.create(2,1),C.create(2,2));
237 		assertBeans(os.run(bs, in, sa("f1-,f2+")), "f1,f2", "3,1.0", "3,2.0", "2,1.0", "2,2.0", "1,1.0");
238 		assertBeans(os.run(in, "f1-,f2+"), "f1,f2", "3,1.0", "3,2.0", "2,1.0", "2,2.0", "1,1.0");
239 	}
240 
241 	private static SortArgs sa(String value) {
242 		return SortArgs.create(value);
243 	}
244 }