View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.config;
21  
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import java.lang.invoke.MethodHandles;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  public class TobagoConfigSorterUnitTest {
32  
33    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
34  
35    @Test
36    public void testCompare() {
37  
38      // config + names
39  
40      final TobagoConfigFragment a = new TobagoConfigFragment();
41      a.setName("a");
42  
43      final TobagoConfigFragment b = new TobagoConfigFragment();
44      b.setName("b");
45  
46      final TobagoConfigFragment c = new TobagoConfigFragment();
47      c.setName("c");
48  
49      final TobagoConfigFragment d = new TobagoConfigFragment();
50      d.setName("d");
51  
52      final TobagoConfigFragment e = new TobagoConfigFragment();
53      e.setName("e");
54  
55      final TobagoConfigFragment f = new TobagoConfigFragment();
56      f.setName("f");
57  
58      final TobagoConfigFragment m = new TobagoConfigFragment();
59      m.setName("m");
60  
61      final TobagoConfigFragment n = new TobagoConfigFragment();
62      n.setName("n");
63  
64      // unnamed
65      final TobagoConfigFragment u1 = new TobagoConfigFragment();
66      final TobagoConfigFragment u2 = new TobagoConfigFragment();
67      final TobagoConfigFragment u3 = new TobagoConfigFragment();
68  
69      // before
70      a.getBefore().add("b");
71      b.getBefore().add("c");
72  
73      u1.getBefore().add("d");
74      u2.getBefore().add("d");
75  
76      u2.getBefore().add("y"); // not relevant
77  
78      // after
79      e.getAfter().add("d");
80      f.getAfter().add("e");
81  
82      u1.getAfter().add("c");
83      u2.getAfter().add("c");
84  
85      u2.getAfter().add("z"); // not relevant
86  
87      n.getAfter().add("m");
88  
89      final List<TobagoConfigFragment> list = new ArrayList<>();
90      list.add(a);
91      list.add(b);
92      list.add(c);
93      list.add(d);
94      list.add(e);
95      list.add(f);
96      list.add(u1);
97      list.add(u2);
98      list.add(u3);
99      list.add(m);
100     list.add(n);
101 
102     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
103     final List<TobagoConfigFragment> result = sorter.topologicalSort();
104 
105     Assertions.assertEquals(a, result.get(0));
106     Assertions.assertEquals(b, result.get(1));
107     Assertions.assertEquals(c, result.get(2));
108     Assertions.assertEquals(u1, result.get(3));
109     Assertions.assertEquals(u2, result.get(4));
110     Assertions.assertEquals(d, result.get(5));
111     Assertions.assertEquals(e, result.get(6));
112     Assertions.assertEquals(f, result.get(7));
113     Assertions.assertEquals(u3, result.get(8));
114     Assertions.assertEquals(m, result.get(9));
115     Assertions.assertEquals(n, result.get(10));
116   }
117 
118   @Test
119   public void test0() {
120 
121     // config + names
122 
123     final List<TobagoConfigFragment> list = new ArrayList<>();
124 
125     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
126 
127     final List<TobagoConfigFragment> result = sorter.topologicalSort();
128 
129     Assertions.assertTrue(result.isEmpty());
130   }
131 
132   @Test
133   public void testCycle1Before() {
134 
135     // config + names
136 
137     final TobagoConfigFragment a = new TobagoConfigFragment();
138     a.setName("a");
139 
140     a.getBefore().add("a");
141 
142     final List<TobagoConfigFragment> list = new ArrayList<>();
143     list.add(a);
144 
145     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
146 
147     try {
148       sorter.topologicalSort();
149 
150       Assertions.fail("Cycle was not detected!");
151     } catch (final RuntimeException e) {
152       LOG.info("Success: Cycle found: {}", e.getMessage());
153     }
154   }
155 
156   @Test
157   public void testCycle1After() {
158 
159     // config + names
160 
161     final TobagoConfigFragment a = new TobagoConfigFragment();
162     a.setName("a");
163 
164     a.getAfter().add("a");
165 
166     final List<TobagoConfigFragment> list = new ArrayList<>();
167     list.add(a);
168 
169     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
170 
171     try {
172       sorter.topologicalSort();
173 
174       Assertions.fail("Cycle was not detected!");
175     } catch (final RuntimeException e) {
176       LOG.info("Success: Cycle found: {}", e.getMessage());
177     }
178   }
179 
180   @Test
181   public void testCycle2() {
182 
183     // config + names
184 
185     final TobagoConfigFragment a = new TobagoConfigFragment();
186     a.setName("a");
187 
188     final TobagoConfigFragment b = new TobagoConfigFragment();
189     b.setName("b");
190 
191     // before
192     a.getBefore().add("b");
193     b.getBefore().add("a");
194 
195     final List<TobagoConfigFragment> list = new ArrayList<>();
196     list.add(a);
197     list.add(b);
198 
199     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
200 
201     try {
202       sorter.topologicalSort();
203 
204       Assertions.fail("Cycle was not detected!");
205     } catch (final RuntimeException e) {
206       LOG.info("Success: Cycle found: {}", e.getMessage());
207     }
208   }
209 
210   @Test
211   public void testCycle2BeforeAfter() {
212 
213     // config + names
214 
215     final TobagoConfigFragment a = new TobagoConfigFragment();
216     a.setName("a");
217 
218     final TobagoConfigFragment b = new TobagoConfigFragment();
219     b.setName("b");
220 
221     // before
222     a.getBefore().add("b");
223     // after
224     a.getAfter().add("b");
225 
226     final List<TobagoConfigFragment> list = new ArrayList<>();
227     list.add(a);
228     list.add(b);
229 
230     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
231     try {
232       sorter.topologicalSort();
233 
234       Assertions.fail("Cycle was not detected!");
235     } catch (final RuntimeException e) {
236       LOG.info("Success: Cycle found: {}", e.getMessage());
237     }
238   }
239 
240   @Test
241   public void testCycle3() {
242 
243     // config + names
244 
245     final TobagoConfigFragment a = new TobagoConfigFragment();
246     a.setName("a");
247 
248     final TobagoConfigFragment b = new TobagoConfigFragment();
249     b.setName("b");
250 
251     final TobagoConfigFragment c = new TobagoConfigFragment();
252     c.setName("c");
253 
254     // before
255     a.getBefore().add("b");
256     b.getBefore().add("c");
257     a.getAfter().add("c");
258 
259     final List<TobagoConfigFragment> list = new ArrayList<>();
260     list.add(a);
261     list.add(b);
262     list.add(c);
263 
264     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
265 
266     try {
267       sorter.topologicalSort();
268 
269       Assertions.fail("Cycle was not detected!");
270     } catch (final RuntimeException e) {
271       LOG.info("Success: Cycle found: {}", e.getMessage());
272     }
273   }
274 
275   @Test
276   public void testReal() {
277 
278     // config + names
279 
280     final TobagoConfigFragment blank = new TobagoConfigFragment();
281     blank.setName("tobago-example-blank");
282 
283     final TobagoConfigFragment charlotteville = new TobagoConfigFragment();
284     charlotteville.setName("tobago-theme-charlotteville");
285 
286     final TobagoConfigFragment roxborough = new TobagoConfigFragment();
287     roxborough.setName("tobago-theme-roxborough");
288 
289     final TobagoConfigFragment scarborough = new TobagoConfigFragment();
290     scarborough.setName("tobago-theme-scarborough");
291 
292     final TobagoConfigFragment speyside = new TobagoConfigFragment();
293     speyside.setName("tobago-theme-speyside");
294 
295     final TobagoConfigFragment standard = new TobagoConfigFragment();
296     standard.setName("tobago-theme-standard");
297 
298     final TobagoConfigFragment core = new TobagoConfigFragment();
299     core.setName("tobago-core");
300 
301     // after
302     blank.getAfter().add(speyside.getName());
303     blank.getAfter().add(scarborough.getName());
304     blank.getAfter().add(roxborough.getName());
305     blank.getAfter().add(standard.getName());
306     blank.getAfter().add(charlotteville.getName());
307     charlotteville.getAfter().add(standard.getName());
308     roxborough.getAfter().add(standard.getName());
309     scarborough.getAfter().add(standard.getName());
310     speyside.getAfter().add(standard.getName());
311     standard.getAfter().add(core.getName());
312 
313 
314     final List<TobagoConfigFragment> list = new ArrayList<>();
315     list.add(blank);
316     list.add(charlotteville);
317     list.add(roxborough);
318     list.add(scarborough);
319     list.add(speyside);
320     list.add(standard);
321     list.add(core);
322 
323     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
324 
325     final List<TobagoConfigFragment> result = sorter.topologicalSort();
326 
327     Assertions.assertEquals(core, result.get(0));
328     Assertions.assertEquals(standard, result.get(1));
329     Assertions.assertEquals(blank, result.get(6));
330     final int blankPos = result.indexOf(blank);
331     final int speysidePos = result.indexOf(speyside);
332     Assertions.assertTrue(blankPos > speysidePos);
333   }
334 }