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