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.Assert;
23  import org.junit.Test;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  public class TobagoConfigSorterUnitTest {
29  
30    @Test
31    public void testCompare() {
32  
33      // config + names
34  
35      final TobagoConfigFragment a = new TobagoConfigFragment();
36      a.setName("a");
37  
38      final TobagoConfigFragment b = new TobagoConfigFragment();
39      b.setName("b");
40  
41      final TobagoConfigFragment c = new TobagoConfigFragment();
42      c.setName("c");
43  
44      final TobagoConfigFragment d = new TobagoConfigFragment();
45      d.setName("d");
46  
47      final TobagoConfigFragment e = new TobagoConfigFragment();
48      e.setName("e");
49  
50      final TobagoConfigFragment f = new TobagoConfigFragment();
51      f.setName("f");
52  
53      final TobagoConfigFragment m = new TobagoConfigFragment();
54      m.setName("m");
55  
56      final TobagoConfigFragment n = new TobagoConfigFragment();
57      n.setName("n");
58  
59      // unnamed
60      final TobagoConfigFragment u1 = new TobagoConfigFragment();
61      final TobagoConfigFragment u2 = new TobagoConfigFragment();
62      final TobagoConfigFragment u3 = new TobagoConfigFragment();
63  
64      // before
65      a.getBefore().add("b");
66      b.getBefore().add("c");
67  
68      u1.getBefore().add("d");
69      u2.getBefore().add("d");
70  
71      u2.getBefore().add("y"); // not relevant
72  
73      // after
74      e.getAfter().add("d");
75      f.getAfter().add("e");
76  
77      u1.getAfter().add("c");
78      u2.getAfter().add("c");
79  
80      u2.getAfter().add("z"); // not relevant
81  
82      n.getAfter().add("m");
83  
84      final List<TobagoConfigFragment> list = new ArrayList<TobagoConfigFragment>();
85      list.add(a);
86      list.add(b);
87      list.add(c);
88      list.add(d);
89      list.add(e);
90      list.add(f);
91      list.add(u1);
92      list.add(u2);
93      list.add(u3);
94      list.add(m);
95      list.add(n);
96  
97      final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
98      sorter.createRelevantPairs();
99  
100     Assert.assertEquals(9, sorter.getPairs().size()); // all but these with "z" and "y"
101 
102     sorter.makeTransitive();
103 
104     Assert.assertEquals(28, sorter.getPairs().size());
105 
106     sorter.ensureIrreflexive();
107 
108     sorter.ensureAntiSymmetric();
109 
110     sorter.sort0();
111 
112     Assert.assertEquals(a, list.get(0));
113     Assert.assertEquals(b, list.get(1));
114     Assert.assertEquals(c, list.get(2));
115     Assert.assertEquals(u1, list.get(3));
116     Assert.assertEquals(u2, list.get(4));
117     Assert.assertEquals(d, list.get(5));
118     Assert.assertEquals(e, list.get(6));
119     Assert.assertEquals(f, list.get(7));
120     Assert.assertEquals(u3, list.get(8));
121     Assert.assertEquals(m, list.get(9));
122     Assert.assertEquals(n, list.get(10));
123   }
124 
125   @Test
126   public void testCycle() {
127 
128     // config + names
129 
130     final TobagoConfigFragment a = new TobagoConfigFragment();
131     a.setName("a");
132 
133     final TobagoConfigFragment b = new TobagoConfigFragment();
134     b.setName("b");
135 
136     // before
137     a.getBefore().add("b");
138     b.getBefore().add("a");
139 
140     final List<TobagoConfigFragment> list = new ArrayList<TobagoConfigFragment>();
141     list.add(a);
142     list.add(b);
143 
144     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
145     sorter.createRelevantPairs();
146 
147     Assert.assertEquals(2, sorter.getPairs().size()); // all but these with "z" and "y"
148 
149     sorter.makeTransitive();
150 
151     try {
152       sorter.ensureIrreflexive();
153       sorter.ensureAntiSymmetric();
154 
155       Assert.fail("Cycle was not found");
156     } catch (final RuntimeException e) {
157       // must find the cycle
158     }
159   }
160 
161   @Test
162   public void testCycle2() {
163 
164     // config + names
165 
166     final TobagoConfigFragment a = new TobagoConfigFragment();
167     a.setName("a");
168 
169     final TobagoConfigFragment b = new TobagoConfigFragment();
170     b.setName("b");
171 
172     // before
173     a.getBefore().add("b");
174     // after
175     a.getAfter().add("b");
176 
177     final List<TobagoConfigFragment> list = new ArrayList<TobagoConfigFragment>();
178     list.add(a);
179     list.add(b);
180 
181     final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
182     sorter.createRelevantPairs();
183 
184     Assert.assertEquals(2, sorter.getPairs().size()); // all but these with "z" and "y"
185 
186     sorter.makeTransitive();
187 
188     try {
189       sorter.ensureIrreflexive();
190       sorter.ensureAntiSymmetric();
191 
192       Assert.fail("Cycle was not found");
193     } catch (final RuntimeException e) {
194       // must find the cycle
195     }
196   }
197 }