KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > AutomaticDependenciesTest


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.netbeans.core.startup;
21
22 import java.net.URL JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.SortedSet JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.modules.Dependency;
30 /** Test AutomaticDependencies (end-to-end).
31  * @author Jesse Glick
32  */

33 public class AutomaticDependenciesTest extends NbTestCase {
34
35     public AutomaticDependenciesTest(String JavaDoc name) {
36         super(name);
37     }
38
39     private AutomaticDependencies ad;
40     protected void setUp() throws Exception JavaDoc {
41         ad = AutomaticDependencies.parse(new URL JavaDoc[] {
42             AutomaticDependenciesTest.class.getResource("data/auto-deps-1.xml"),
43             AutomaticDependenciesTest.class.getResource("data/auto-deps-2.xml"),
44         });
45     }
46     
47     public void testBasicOperation() throws Exception JavaDoc {
48         Set JavaDoc<Dependency> deps = new HashSet JavaDoc<Dependency>();
49         deps.addAll(Dependency.create(Dependency.TYPE_JAVA, "Java > 1.3"));
50         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "unrelated"));
51         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig > 1.0"));
52         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig2/1 > 1.5.1"));
53         Set JavaDoc<Dependency> _deps = new HashSet JavaDoc<Dependency>(deps);
54         AutomaticDependencies.Report rep = ad.refineDependenciesAndReport("random", _deps);
55         assertEquals("Java > 1.3, Nue, nue > 1.0, nue2 > 1.0, orig > 1.0, orig2/1 > 1.5.1, unrelated", normal(_deps));
56         assertTrue(rep.isModified());
57         assertEquals("message-1, message-2", normalMessages(rep));
58     }
59     
60     public void testExcludes() throws Exception JavaDoc {
61         Set JavaDoc<Dependency> deps = new HashSet JavaDoc<Dependency>();
62         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig > 1.0"));
63         Set JavaDoc<Dependency> _deps = new HashSet JavaDoc<Dependency>(deps);
64         ad.refineDependencies("a", _deps);
65         assertEquals("orig > 1.0", normal(_deps));
66         _deps = new HashSet JavaDoc<Dependency>(deps);
67         ad.refineDependencies("a.x", _deps);
68         assertEquals("Nue, nue > 1.0, orig > 1.0", normal(_deps));
69         _deps = new HashSet JavaDoc<Dependency>(deps);
70         ad.refineDependencies("b", _deps);
71         assertEquals("orig > 1.0", normal(_deps));
72         _deps = new HashSet JavaDoc<Dependency>(deps);
73         ad.refineDependencies("b.x", _deps);
74         assertEquals("orig > 1.0", normal(_deps));
75     }
76     
77     public void testVersionSensitivity() throws Exception JavaDoc {
78         Set JavaDoc<Dependency> deps = new HashSet JavaDoc<Dependency>();
79         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig > 1.1"));
80         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig2/2 > 0.1"));
81         Set JavaDoc<Dependency> _deps = new HashSet JavaDoc<Dependency>(deps);
82         ad.refineDependencies("random", _deps);
83         assertEquals("orig > 1.1, orig2/2 > 0.1", normal(_deps));
84     }
85     
86     public void testCancellations() throws Exception JavaDoc {
87         Set JavaDoc<Dependency> deps = new HashSet JavaDoc<Dependency>();
88         deps.addAll(Dependency.create(Dependency.TYPE_PACKAGE, "javax.death[AbstractCoffin] > 1.0"));
89         Set JavaDoc<Dependency> _deps = new HashSet JavaDoc<Dependency>(deps);
90         AutomaticDependencies.Report rep = ad.refineDependenciesAndReport("random", _deps);
91         assertEquals("javax.death/1 > 1.0", normal(_deps));
92         assertEquals("message-1", normalMessages(rep));
93     }
94     
95     public void testMerges() throws Exception JavaDoc {
96         Set JavaDoc<Dependency> deps = new HashSet JavaDoc<Dependency>();
97         deps.addAll(Dependency.create(Dependency.TYPE_PACKAGE, "javax.death[AbstractCoffin] > 1.0"));
98         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "javax.death/1 > 1.0"));
99         Set JavaDoc<Dependency> _deps = new HashSet JavaDoc<Dependency>(deps);
100         ad.refineDependencies("random", _deps);
101         assertEquals("javax.death/1 > 1.0", normal(_deps));
102         deps = new HashSet JavaDoc<Dependency>();
103         deps.addAll(Dependency.create(Dependency.TYPE_PACKAGE, "javax.death[AbstractCoffin] > 1.0"));
104         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "javax.death/1 > 0.3"));
105         _deps = new HashSet JavaDoc<Dependency>(deps);
106         ad.refineDependencies("random", _deps);
107         assertEquals("javax.death/1 > 1.0", normal(_deps));
108         deps = new HashSet JavaDoc<Dependency>(); // Set<Dependency>
109
deps.addAll(Dependency.create(Dependency.TYPE_PACKAGE, "[javax.death.AbstractCoffin]"));
110         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "javax.death/1 > 0.3"));
111         _deps = new HashSet JavaDoc<Dependency>(deps);
112         ad.refineDependencies("random", _deps);
113         assertEquals("javax.death/1 > 1.0", normal(_deps));
114         deps = new HashSet JavaDoc<Dependency>(); // Set<Dependency>
115
deps.addAll(Dependency.create(Dependency.TYPE_PACKAGE, "javax.death[AbstractCoffin] > 1.0"));
116         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "javax.death/1 > 1.3"));
117         _deps = new HashSet JavaDoc<Dependency>(deps);
118         ad.refineDependencies("random", _deps);
119         assertEquals("javax.death/1 > 1.3", normal(_deps));
120     }
121     
122     public void testExclusionOfImplDeps() throws Exception JavaDoc {
123         // Cf. #46961.
124
Set JavaDoc<Dependency> deps = new HashSet JavaDoc<Dependency>();
125         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig = some-orig-version"));
126         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig2/1 = some-orig2-version"));
127         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig2/0 = some-ancient-orig2-version"));
128         Set JavaDoc<Dependency> _deps = new HashSet JavaDoc<Dependency>(deps);
129         AutomaticDependencies.Report rep = ad.refineDependenciesAndReport("random", _deps);
130         assertEquals("orig = some-orig-version, orig2/0 = some-ancient-orig2-version, orig2/1 = some-orig2-version", normal(_deps));
131         assertFalse(rep.isModified());
132         assertEquals("", normalMessages(rep));
133     }
134
135     public void testCanUpgradeMajorVersions() throws Exception JavaDoc {
136         // Need to be able to say that anything using module/0 (w/ or w/o spec) should use module/1 instead.
137
// Plain dep on old module is upgraded:
138
Set JavaDoc<Dependency> deps = new HashSet JavaDoc<Dependency>();
139         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/0"));
140         Set JavaDoc<Dependency> _deps = new HashSet JavaDoc<Dependency>(deps);
141         AutomaticDependencies.Report rep = ad.refineDependenciesAndReport("random", _deps);
142         assertEquals("stabilized/1 > 1.0", normal(_deps));
143         assertTrue(rep.isModified());
144         assertEquals("message-1a", normalMessages(rep));
145         // Spec dep on old module is upgraded:
146
deps = new HashSet JavaDoc<Dependency>();
147         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/0 > 0.5"));
148         _deps = new HashSet JavaDoc<Dependency>(deps);
149         rep = ad.refineDependenciesAndReport("random", _deps);
150         assertEquals("stabilized/1 > 1.0", normal(_deps));
151         assertTrue(rep.isModified());
152         assertEquals("message-1a", normalMessages(rep));
153         // Impl dep on old module is left alone (will be useless anyway on new version):
154
deps = new HashSet JavaDoc<Dependency>();
155         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/0 = whatever"));
156         _deps = new HashSet JavaDoc<Dependency>(deps);
157         rep = ad.refineDependenciesAndReport("random", _deps);
158         assertEquals("stabilized/0 = whatever", normal(_deps));
159         assertFalse(rep.isModified());
160         assertEquals("", normalMessages(rep));
161         // Plain dep on new module is left alone:
162
deps = new HashSet JavaDoc<Dependency>();
163         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/1"));
164         _deps = new HashSet JavaDoc<Dependency>(deps);
165         rep = ad.refineDependenciesAndReport("random", _deps);
166         assertEquals("stabilized/1", normal(_deps));
167         assertFalse(rep.isModified());
168         assertEquals("", normalMessages(rep));
169         // Spec dep on new module is left alone:
170
deps = new HashSet JavaDoc<Dependency>();
171         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/1 > 1.1"));
172         _deps = new HashSet JavaDoc<Dependency>(deps);
173         rep = ad.refineDependenciesAndReport("random", _deps);
174         assertEquals("stabilized/1 > 1.1", normal(_deps));
175         assertFalse(rep.isModified());
176         assertEquals("", normalMessages(rep));
177         // Impl dep on new module is left alone:
178
deps = new HashSet JavaDoc<Dependency>();
179         deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/1 = whatever"));
180         _deps = new HashSet JavaDoc<Dependency>(deps);
181         rep = ad.refineDependenciesAndReport("random", _deps);
182         assertEquals("stabilized/1 = whatever", normal(_deps));
183         assertFalse(rep.isModified());
184         assertEquals("", normalMessages(rep));
185     }
186     
187     private static String JavaDoc normal(Set JavaDoc<Dependency> deps) {
188         SortedSet JavaDoc<String JavaDoc> s = new TreeSet JavaDoc<String JavaDoc>(); // Set<String>
189
Iterator JavaDoc<Dependency> it = deps.iterator();
190         while (it.hasNext()) {
191             Dependency d = it.next();
192             s.add(dep2String(d));
193         }
194         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
195         Iterator JavaDoc<String JavaDoc> it2 = s.iterator();
196         while (it2.hasNext()) {
197             b.append(it2.next());
198             if (it2.hasNext()) {
199                 b.append(", ");
200             }
201         }
202         return b.toString();
203     }
204     
205     private static String JavaDoc normalMessages(AutomaticDependencies.Report rep) {
206         SortedSet JavaDoc<String JavaDoc> s = new TreeSet JavaDoc<String JavaDoc>(); // Set<String>
207
Iterator JavaDoc it = rep.getMessages().iterator();
208         while (it.hasNext()) {
209             s.add((String JavaDoc)it.next());
210         }
211         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
212         it = s.iterator();
213         while (it.hasNext()) {
214             b.append(it.next());
215             if (it.hasNext()) {
216                 b.append(", ");
217             }
218         }
219         return b.toString();
220     }
221     
222     private static String JavaDoc dep2String(Dependency d) {
223         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
224         b.append(d.getName());
225         switch (d.getComparison()) {
226         case Dependency.COMPARE_ANY:
227             break;
228         case Dependency.COMPARE_IMPL:
229             b.append(" = ");
230             b.append(d.getVersion());
231             break;
232         case Dependency.COMPARE_SPEC:
233             b.append(" > ");
234             b.append(d.getVersion());
235             break;
236         default:
237             throw new IllegalStateException JavaDoc();
238         }
239         return b.toString();
240     }
241     
242 }
243
Popular Tags