1 19 20 package org.netbeans.core.startup; 21 22 import java.net.URL ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import java.util.SortedSet ; 27 import java.util.TreeSet ; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.modules.Dependency; 30 33 public class AutomaticDependenciesTest extends NbTestCase { 34 35 public AutomaticDependenciesTest(String name) { 36 super(name); 37 } 38 39 private AutomaticDependencies ad; 40 protected void setUp() throws Exception { 41 ad = AutomaticDependencies.parse(new URL [] { 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 { 48 Set <Dependency> deps = new HashSet <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 <Dependency> _deps = new HashSet <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 { 61 Set <Dependency> deps = new HashSet <Dependency>(); 62 deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "orig > 1.0")); 63 Set <Dependency> _deps = new HashSet <Dependency>(deps); 64 ad.refineDependencies("a", _deps); 65 assertEquals("orig > 1.0", normal(_deps)); 66 _deps = new HashSet <Dependency>(deps); 67 ad.refineDependencies("a.x", _deps); 68 assertEquals("Nue, nue > 1.0, orig > 1.0", normal(_deps)); 69 _deps = new HashSet <Dependency>(deps); 70 ad.refineDependencies("b", _deps); 71 assertEquals("orig > 1.0", normal(_deps)); 72 _deps = new HashSet <Dependency>(deps); 73 ad.refineDependencies("b.x", _deps); 74 assertEquals("orig > 1.0", normal(_deps)); 75 } 76 77 public void testVersionSensitivity() throws Exception { 78 Set <Dependency> deps = new HashSet <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 <Dependency> _deps = new HashSet <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 { 87 Set <Dependency> deps = new HashSet <Dependency>(); 88 deps.addAll(Dependency.create(Dependency.TYPE_PACKAGE, "javax.death[AbstractCoffin] > 1.0")); 89 Set <Dependency> _deps = new HashSet <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 { 96 Set <Dependency> deps = new HashSet <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 <Dependency> _deps = new HashSet <Dependency>(deps); 100 ad.refineDependencies("random", _deps); 101 assertEquals("javax.death/1 > 1.0", normal(_deps)); 102 deps = new HashSet <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 <Dependency>(deps); 106 ad.refineDependencies("random", _deps); 107 assertEquals("javax.death/1 > 1.0", normal(_deps)); 108 deps = new HashSet <Dependency>(); 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 <Dependency>(deps); 112 ad.refineDependencies("random", _deps); 113 assertEquals("javax.death/1 > 1.0", normal(_deps)); 114 deps = new HashSet <Dependency>(); 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 <Dependency>(deps); 118 ad.refineDependencies("random", _deps); 119 assertEquals("javax.death/1 > 1.3", normal(_deps)); 120 } 121 122 public void testExclusionOfImplDeps() throws Exception { 123 Set <Dependency> deps = new HashSet <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 <Dependency> _deps = new HashSet <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 { 136 Set <Dependency> deps = new HashSet <Dependency>(); 139 deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/0")); 140 Set <Dependency> _deps = new HashSet <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 deps = new HashSet <Dependency>(); 147 deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/0 > 0.5")); 148 _deps = new HashSet <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 deps = new HashSet <Dependency>(); 155 deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/0 = whatever")); 156 _deps = new HashSet <Dependency>(deps); 157 rep = ad.refineDependenciesAndReport("random", _deps); 158 assertEquals("stabilized/0 = whatever", normal(_deps)); 159 assertFalse(rep.isModified()); 160 assertEquals("", normalMessages(rep)); 161 deps = new HashSet <Dependency>(); 163 deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/1")); 164 _deps = new HashSet <Dependency>(deps); 165 rep = ad.refineDependenciesAndReport("random", _deps); 166 assertEquals("stabilized/1", normal(_deps)); 167 assertFalse(rep.isModified()); 168 assertEquals("", normalMessages(rep)); 169 deps = new HashSet <Dependency>(); 171 deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/1 > 1.1")); 172 _deps = new HashSet <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 deps = new HashSet <Dependency>(); 179 deps.addAll(Dependency.create(Dependency.TYPE_MODULE, "stabilized/1 = whatever")); 180 _deps = new HashSet <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 normal(Set <Dependency> deps) { 188 SortedSet <String > s = new TreeSet <String >(); Iterator <Dependency> it = deps.iterator(); 190 while (it.hasNext()) { 191 Dependency d = it.next(); 192 s.add(dep2String(d)); 193 } 194 StringBuffer b = new StringBuffer (); 195 Iterator <String > 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 normalMessages(AutomaticDependencies.Report rep) { 206 SortedSet <String > s = new TreeSet <String >(); Iterator it = rep.getMessages().iterator(); 208 while (it.hasNext()) { 209 s.add((String )it.next()); 210 } 211 StringBuffer b = new StringBuffer (); 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 dep2String(Dependency d) { 223 StringBuffer b = new StringBuffer (); 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 (); 238 } 239 return b.toString(); 240 } 241 242 } 243 | Popular Tags |