KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > UtilitiesTopologicalSortTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.netbeans.junit.*;
26 import junit.textui.TestRunner;
27
28 /** Test Utilities.topologicalSort.
29  * @author Jesse Glick
30  */

31 public class UtilitiesTopologicalSortTest extends NbTestCase {
32
33     public UtilitiesTopologicalSortTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public static void main(String JavaDoc[] args) {
38         TestRunner.run(new NbTestSuite(UtilitiesTopologicalSortTest.class));
39     }
40
41     /**
42      * @see "#27286"
43      */

44     public void testTopologicalSort() throws Exception JavaDoc {
45         Collection c = Arrays.asList(new String JavaDoc[] {"a", "b", "c", "d", "e", "f"});
46         Map m = new HashMap();
47         m.put("f", Collections.singletonList("a"));
48         List l = Utilities.topologicalSort(c, m);
49         assertNotNull(l);
50         assertTrue(l.indexOf("f") < l.indexOf("a"));
51         m.put("e", Collections.singletonList("b"));
52         l = Utilities.topologicalSort(c, m);
53         assertNotNull(l);
54         assertTrue(l.indexOf("f") < l.indexOf("a"));
55         assertTrue(l.indexOf("e") < l.indexOf("b"));
56         m.put("b", Collections.singletonList("a"));
57         l = Utilities.topologicalSort(c, m);
58         assertNotNull(l);
59         assertTrue(l.indexOf("f") < l.indexOf("a"));
60         assertTrue(l.indexOf("e") < l.indexOf("b"));
61         assertTrue(l.indexOf("b") < l.indexOf("a"));
62         // Test that it is modifiable:
63
l.add("foo");
64         Collections.reverse(l);
65         // Test cycles:
66
m.put("a", Collections.singletonList("e"));
67         try {
68             l = Utilities.topologicalSort(c, m);
69             fail ("Should throw an exception");
70         } catch (TopologicalSortException ex) {
71         }
72     }
73     
74     public void testTopologicalSortError() throws Exception JavaDoc {
75         Collection c = Arrays.asList(new String JavaDoc[] {"first", "a", "b", "c", "d", "e", "f", "last"});
76         Map m = new HashMap();
77         // to make sure that not whole visited graph will be in the cycle
78
m.put("first", Collections.singletonList ("f"));
79         m.put("last", Collections.singletonList ("f"));
80         
81         
82         m.put("f", Collections.singletonList("a"));
83         Utilities.topologicalSort (c, m); // does not throw an error
84
m.put("e", Collections.singletonList("b"));
85         Utilities.topologicalSort (c, m); // does not throw an error
86
m.put("b", Collections.singletonList("a"));
87         Utilities.topologicalSort (c, m); // does not throw an error
88
m.put("a", Collections.singletonList("e"));
89         
90         try {
91             Utilities.topologicalSort (c, m);
92             fail ("Should throw an error");
93         } catch (TopologicalSortException ex) {
94             Set[] sets = ex.topologicalSets();
95             
96             assertEquals ("There is one cycle of size 3, all other objects are sortable", 6, sets.length);
97             
98             Set cycle = null;
99             for (int i = 0; i < sets.length; i++) {
100                 if (sets[i].size () > 1) {
101                     assertNull ("There is just one unsortable component", cycle);
102                     cycle = sets[i];
103                 } else {
104                     assertEquals ("Size is 1", 1, sets[i].size ());
105                 }
106             }
107             
108             assertTrue ("a is there", cycle.contains ("a"));
109             assertTrue ("b is there", cycle.contains ("b"));
110             assertTrue ("e is there", cycle.contains ("e"));
111             assertEquals ("Three vertexes are in the cycle", 3, cycle.size ());
112             
113             assertBefore ("first", "f", sets);
114             assertBefore ("last", "f", sets);
115             assertBefore ("f", "a", sets);
116             assertBefore ("f", "b", sets);
117             assertBefore ("f", "e", sets);
118             
119             List partial = ex.partialSort ();
120             assertEquals ("Has the same size as the original", c.size (), partial.size ());
121             assertBefore ("first", "f", partial);
122             assertBefore ("last", "f", partial);
123             assertBefore ("f", "a", partial);
124             assertBefore ("f", "b", partial);
125             assertBefore ("f", "e", partial);
126         }
127     }
128     
129     public void testMoreCycles () throws Exception JavaDoc {
130         Collection c = Arrays.asList(new String JavaDoc[] {"first", "a", "b", "c", "d", "e", "f", "last"});
131         Map m = new HashMap();
132         // to make sure that not whole visited graph will be in the cycle
133

134         m.put ("a", Collections.singletonList ("a")); // self cycle
135

136         // cycle 2
137
m.put ("b", Collections.singletonList ("c"));
138         m.put ("c", Collections.singletonList ("b"));
139         
140         // cycle 3
141
m.put ("d", Collections.singletonList ("e"));
142         m.put ("e", Collections.singletonList ("f"));
143         m.put ("f", Collections.singletonList ("d"));
144         
145         Collection sizes = new ArrayList ();
146         
147         try {
148             Utilities.topologicalSort (c, m);
149             fail ("Should throw an error");
150         } catch (TopologicalSortException ex) {
151             Set[] sets = ex.topologicalSets();
152             for (int i = 0; i < sets.length; i++) {
153                 sizes.add (new Integer JavaDoc (sets[i].size ()));
154             }
155             
156             assertEquals ("There were three cycles plus first+last", 5, sizes.size ());
157             assertTrue ("One of size 1", sizes.contains (new Integer JavaDoc (1)));
158             assertTrue ("One of size 2", sizes.contains (new Integer JavaDoc (2)));
159             assertTrue ("One of size 3", sizes.contains (new Integer JavaDoc (3)));
160             
161             sets = ex.unsortableSets();
162             assertEquals ("Three cycles", 3, sets.length);
163         }
164     }
165     
166     public void testDetectSelfCycle () throws Exception JavaDoc {
167         Collection c = Arrays.asList(new String JavaDoc[] {"a", "b" });
168         Map m = new HashMap();
169         
170         m.put ("a", Arrays.asList (new String JavaDoc[] { "a" }));
171         m.put ("b", Arrays.asList (new String JavaDoc[] { "a" }));
172         
173         try {
174             Utilities.topologicalSort (c, m);
175             fail ("Definitively there is a cycle");
176         } catch (TopologicalSortException ex) {
177             Set[] sets = ex.unsortableSets ();
178             
179             assertEquals ("One cycle", 1, sets.length);
180             assertEquals ("Contains one item", 1, sets[0].size ());
181             assertTrue ("Contains a", sets[0].contains ("a"));
182         }
183     }
184
185     public void testFindLongestCycle () throws Exception JavaDoc {
186         Collection c = Arrays.asList(new String JavaDoc[] {"a", "b", "c", "d", "e", "f" });
187         Map m = new HashMap();
188         // to make sure that not whole visited graph will be in the cycle
189

190         m.put ("a", Arrays.asList (new String JavaDoc[] { "b", "c" }));
191         m.put ("b", Arrays.asList (new String JavaDoc[] { "a", "d" }));
192         m.put ("c", Arrays.asList (new String JavaDoc[] { "a", "d" }));
193         m.put ("d", Arrays.asList (new String JavaDoc[] { "b", "c", "e" }));
194         m.put ("f", Arrays.asList (new String JavaDoc[] { "a" }));
195         
196         try {
197             Utilities.topologicalSort (c, m);
198             fail ("Definitively there is a cycle");
199         } catch (TopologicalSortException ex) {
200             Set[] sets = ex.topologicalSets();
201             
202             assertEquals ("There is one cycle and e+f", 3, sets.length);
203             
204             assertBefore ("f", "a", sets);
205             assertBefore ("f", "b", sets);
206             assertBefore ("f", "c", sets);
207             assertBefore ("f", "d", sets);
208             assertBefore ("f", "e", sets);
209             
210             assertBefore ("a", "e", sets);
211             assertBefore ("b", "e", sets);
212             assertBefore ("c", "e", sets);
213             assertBefore ("d", "e", sets);
214             
215             assertEquals ("set of f", 1, sets[0].size ());
216             assertEquals ("set of a,b,c,d", 4, sets[1].size ());
217             assertEquals ("set of e", 1, sets[2].size ());
218         }
219     }
220     
221     public void testStability() throws Exception JavaDoc {
222         Collection c = Arrays.asList(new String JavaDoc[] {"a", "b", "c", "d", "e", "f"});
223         assertEquals(c, Utilities.topologicalSort(c, Collections.EMPTY_MAP));
224         Map m = new HashMap();
225         m.put("e", Collections.singletonList("d"));
226         List l = Utilities.topologicalSort(c, m);
227         assertNotNull(l);
228         assertEquals(Arrays.asList(new String JavaDoc[] {"a", "b", "c"}), l.subList(0, 3));
229         m = new HashMap();
230         m.put("c", Collections.singletonList("a"));
231         l = Utilities.topologicalSort(c, m);
232         assertNotNull(l);
233         assertEquals(Arrays.asList(new String JavaDoc[] {"d", "e", "f"}), l.subList(3, 6));
234         m = new HashMap();
235         m.put("a", Collections.singletonList("f"));
236         assertEquals(c, Utilities.topologicalSort(c, m));
237         m.put("a", Collections.singletonList("b"));
238         assertEquals(c, Utilities.topologicalSort(c, m));
239         m.put("b", Collections.singletonList("f"));
240         assertEquals(c, Utilities.topologicalSort(c, m));
241         m.put("c", Collections.singletonList("e"));
242         assertEquals(c, Utilities.topologicalSort(c, m));
243         m.put("a", Collections.singletonList("e"));
244         assertEquals(c, Utilities.topologicalSort(c, m));
245         /* Does not work - oh well:
246         c = Arrays.asList(new String[] {"a", "b", "c", "d", "e", "x", "y"});
247         m = new HashMap();
248         m.put("c", Arrays.asList(new String[] {"x", "y"}));
249         m.put("x", Collections.singletonList("d"));
250         m.put("y", Collections.singletonList("d"));
251         System.err.println("-----");
252         l = Utilities.topologicalSort(c, m);
253         assertNotNull(l);
254         System.err.println(l);
255         assertEquals(Arrays.asList(new String[] {"a", "b", "c"}), l.subList(0, 3));
256         assertEquals(Arrays.asList(new String[] {"d", "e"}), l.subList(5, 7));
257         */

258     }
259     public void testTopologicalSortCompile() throws Exception JavaDoc {
260         Collection<String JavaDoc> c = new ArrayList<String JavaDoc>();
261         Map<Object JavaDoc, Collection<String JavaDoc>> edges = new HashMap<Object JavaDoc,Collection<String JavaDoc>>();
262         
263         List<String JavaDoc> result = Utilities.topologicalSort(c, edges);
264     }
265     
266     public void testTopologicalSortCompile2() throws Exception JavaDoc {
267         Collection<String JavaDoc> c = new ArrayList<String JavaDoc>();
268         Map<Object JavaDoc, List<String JavaDoc>> edges = new HashMap<Object JavaDoc,List<String JavaDoc>>();
269         
270         List<String JavaDoc> result = Utilities.topologicalSort(c, edges);
271     }
272     public void testTopologicalSortCompile3() throws Exception JavaDoc {
273         Collection<Number JavaDoc> c = new ArrayList<Number JavaDoc>();
274         Map<Object JavaDoc, List<Integer JavaDoc>> edges = new HashMap<Object JavaDoc,List<Integer JavaDoc>>();
275         
276         List<Number JavaDoc> result = Utilities.topologicalSort(c, edges);
277     }
278     public void testErrorReporting () throws Exception JavaDoc {
279         Collection c = Arrays.asList(new String JavaDoc[] {"a", "b", "c", "d", "e", "f"});
280         Map m = new HashMap ();
281         m.put ("a", Arrays.asList (new String JavaDoc[] { "a" }));
282         m.put ("b", Arrays.asList (new String JavaDoc[] { "c" }));
283         m.put ("c", Arrays.asList (new String JavaDoc[] { "b" }));
284         
285         try {
286             Utilities.topologicalSort(c, m);
287             fail ("Unsortable");
288         } catch (TopologicalSortException ex) {
289             StringWriter w = new StringWriter ();
290             ex.printStackTrace (new PrintWriter (w, true));
291             
292             ByteArrayOutputStream s = new ByteArrayOutputStream ();
293             ex.printStackTrace (new java.io.PrintStream JavaDoc (s, true));
294             
295             byte[] warr = w.toString().getBytes("utf-8");
296             byte[] sarr = s.toByteArray();
297             
298             assertTrue ("Both messages should be the same", Arrays.equals(warr, sarr));
299             
300             String JavaDoc msg = w.toString();
301             
302             int cnt = 0;
303             int indx = -1;
304             for (;;) {
305                 indx = msg.indexOf("Conflict #", indx + 1);
306                 if (indx == -1) break;
307                 cnt++;
308             }
309             
310             assertEquals ("There is the same number of lines with 'Conflict #' as unsortable sets", cnt, ex.unsortableSets().length);
311         }
312         
313         
314         
315     }
316
317     private static void assertBefore (String JavaDoc first, String JavaDoc second, Collection c) {
318         assertBefore (first, second, c, false);
319     }
320     
321     private static void assertBefore (String JavaDoc first, String JavaDoc second, Set[] sets) {
322         assertBefore (first, second, Arrays.asList (sets), true);
323     }
324     
325     private static void assertBefore (String JavaDoc first, String JavaDoc second, Collection c, boolean useSets) {
326         Iterator it = c.iterator();
327         boolean wasFirst = false;
328         boolean wasSecond = false;
329         while (it.hasNext ()) {
330             Object JavaDoc obj = it.next ();
331             
332             if (useSets ? ((Set)obj).contains (second) : obj == second) {
333                 assertTrue ("[" + second + "] found before [" + first + "] in " + c, wasFirst);
334                 wasSecond = true;
335             }
336             
337             if (useSets ? ((Set)obj).contains (first) : obj == first) {
338                 wasFirst = true;
339             }
340         }
341         
342         assertTrue ("[" + first + "] not found", wasFirst);
343         assertTrue ("[" + second + "] not found", wasSecond);
344     }
345 }
346
Popular Tags