1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.PrintStream ; 25 import junit.framework.AssertionFailedError; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.types.Path; 29 import org.netbeans.junit.*; 30 31 35 public class SortSuiteModulesTest extends NbTestCase { 36 private Project project; 37 String a = "a"; 38 String b = "b"; 39 String c = "c"; 40 String d = "d"; 41 String e = "e"; 42 String f = "f"; 43 String g = "g"; 44 String SORTED_MODULES = "sorted_modules"; 45 String NULL[] = new String [0]; 46 47 public SortSuiteModulesTest(java.lang.String testName) { 48 super(testName); 49 } 50 51 protected void setUp() throws Exception { 52 project = new Project(); 53 project.setBaseDir(getWorkDir()); 54 } 55 56 57 public void testOnlyModuleDependencies() throws IOException { 58 59 61 createModule(g,NULL); 65 createModule(d,NULL); 66 createModule(c,NULL); 67 createModule(e,NULL); 68 createModule(a,new String []{b,c}); 69 createModule(b,new String []{e,d}); 70 createModule(f,new String []{g}); 71 72 Path path = createPath(new String []{a,b,c,d,e,f,g}); 73 SortSuiteModules ssm = new SortSuiteModules(); 74 ssm.setProject(project); 75 ssm.setUnsortedModules(path); 76 ssm.setSortedModulesProperty(SORTED_MODULES); 77 ssm.execute(); 78 79 String property = project.getProperty(SORTED_MODULES); 80 assertNotNull("null sorted modules path",property); 81 String paths[] = getSorted(property); 82 83 assertEdge(paths,a,b); 84 assertEdge(paths,a,c); 85 assertEdge(paths,b,d); 86 assertEdge(paths,b,e); 87 assertEdge(paths,f,g); 88 } 89 public void testModuleDependenciesCycle() throws IOException { 90 createModule(a,new String []{b}); 91 createModule(b,new String []{a}); 92 Path path = createPath(new String []{a,b}); 93 SortSuiteModules ssm = new SortSuiteModules(); 94 ssm.setProject(project); 95 ssm.setUnsortedModules(path); 96 ssm.setSortedModulesProperty(SORTED_MODULES); 97 try { 98 ssm.execute(); 99 fail("Exception must be thrown"); 100 } catch(BuildException be) { 101 } 103 } 104 public void testModuleAndTestDependenciesDisabledTestSort() throws IOException { 105 generateTestModules1(false); 106 107 String property = project.getProperty(SORTED_MODULES); 108 assertNotNull("null sorted modules path",property); 109 String paths[] = getSorted(property); 110 111 assertEdge(paths,a,b); 112 assertEdge(paths,a,c); 113 assertEdge(paths,b,d); 114 assertEdge(paths,b,e); 115 assertEdge(paths,f,g); 116 try { 117 assertEdge(paths,b,g); 118 fail("sort test deps disabled"); 119 } catch (AssertionFailedError be) {}; 120 } 121 public void testModuleAndTestDependenciesEnabledTestSort() throws IOException { 122 generateTestModules1(true); 123 124 String property = project.getProperty(SORTED_MODULES); 125 assertNotNull("null sorted modules path",property); 126 String paths[] = getSorted(property); 127 128 assertEdge(paths,a,b); 129 assertEdge(paths,a,c); 130 assertEdge(paths,b,d); 131 assertEdge(paths,b,e); 132 assertEdge(paths,b,g); 133 assertEdge(paths,f,g); 134 } 135 136 private void generateTestModules1(boolean sortTests) throws IOException , BuildException { 137 138 140 createModule(g,NULL); 144 createModule(d,NULL); 145 createModule(c,NULL); 146 createModule(e,NULL); 147 createModule(a,new String []{b,c}); 148 createModule(b,new String []{e,d},new String []{g},NULL); 149 createModule(f,new String []{g}); 150 151 Path path = createPath(new String []{a,b,c,d,e,f,g}); 152 SortSuiteModules ssm = new SortSuiteModules(); 153 ssm.setProject(project); 154 ssm.setUnsortedModules(path); 155 ssm.setSortedModulesProperty(SORTED_MODULES); 156 if (sortTests) { 157 ssm.setSortTests(true); 158 } 159 ssm.execute(); 160 } 161 162 private void createModule(String module, String [] mdeps) throws IOException { 163 createModule(module,mdeps,new String [0],new String [0]); 164 } 165 166 172 private void createModule(String module, String [] mdeps, String [] udeps, String [] qadeps) throws IOException { 173 File dir = new File (getWorkDir(),module + File.separator + "nbproject"); 174 assertTrue("cannot create module dir",dir.mkdirs()); 175 File xml = new File (dir,"project.xml"); 176 PrintStream ps = new PrintStream (xml); 177 ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 178 ps.println("<project xmlns=\"http://www.netbeans.org/ns/project/1\">"); 179 ps.println(" <type>org.netbeans.modules.apisupport.project</type>"); 180 ps.println(" <configuration>"); 181 ps.println(" <data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">"); 182 ps.println(" <code-name-base>" + module + "</code-name-base>"); 183 ps.println(" <module-dependencies>"); 184 for (int it = 0 ; it < mdeps.length ; it++) { 185 ps.println(" <dependency>"); 186 ps.println(" <code-name-base>" + mdeps[it] + "</code-name-base>"); 187 ps.println(" <build-prerequisite/>"); 188 ps.println(" </dependency>"); 189 } 190 ps.println(" </module-dependencies>"); 191 ps.println(" <test-dependencies>"); 192 ps.println(" <test-type>"); 193 ps.println(" <name>unit</name>"); 194 for (int it = 0 ; it < udeps.length ; it++ ) { 195 ps.println(" <test-dependency>"); 196 ps.println(" <code-name-base>" + udeps[it] + "</code-name-base>"); 197 ps.println(" <test/>"); 198 ps.println(" </test-dependency>"); 199 } 200 ps.println(" </test-type>"); 201 ps.println(" <test-type>"); 202 ps.println(" <name>qa-functional</name>"); 203 for (int it = 0 ; it < qadeps.length ; it++ ) { 204 ps.println(" <test-dependency>"); 205 ps.println(" <code-name-base>" + qadeps[it] + "</code-name-base>"); 206 ps.println(" <test/>"); 207 ps.println(" </test-dependency>"); 208 } 209 ps.println(" </test-type>"); 210 ps.println(" </test-dependencies>"); 211 ps.println(" <public-packages/>"); 212 ps.println(" </data>"); 213 ps.println(" </configuration>"); 214 ps.println("</project>"); 215 } 216 217 private Path createPath(String [] paths) { 218 Path path = new Path(project); 219 StringBuffer sb = new StringBuffer (); 220 for (int it = 0; it < paths.length; it++) { 221 if (sb.length() > 0) { 222 sb.append(":"); 223 } 224 sb.append(paths[it]); 225 } 226 path.setPath(sb.toString()); 227 return path; 228 } 229 230 private String [] getSorted(String property) { 231 Path path = new Path(project); 232 path.setPath(property); 233 String paths[] = path.list(); 234 235 String rets [] = new String [paths.length]; 236 for (int i = 0; i < paths.length; i++) { 237 rets[i] = new File (paths[i]).getName(); 238 239 } 240 return rets; 241 } 242 243 private void assertEdge(String [] names, String a, String b) { 244 assertTrue( a + " ->" + b, getIndex(names,a) > getIndex(names,b)); 245 } 246 247 private int getIndex(String [] names, String a) { 248 for (int i = 0; i < names.length; i++) { 249 log(names[i]); 250 if (names[i].equals(a)) { 251 return i; 252 } 253 } 254 fail("index " + a); 255 return -1; 256 } 257 258 public void testTestDependenciesCycleEnabledTestSort() throws IOException { 259 createModule(a,new String []{b},new String []{b},NULL); 260 createModule(b,NULL,new String []{a},NULL); 261 Path path = createPath(new String []{a,b}); 262 SortSuiteModules ssm = new SortSuiteModules(); 263 ssm.setProject(project); 264 ssm.setUnsortedModules(path); 265 ssm.setSortedModulesProperty(SORTED_MODULES); 266 ssm.setSortTests(true); 267 try { 268 ssm.execute(); 269 fail("Exception must be thrown"); 270 } catch(BuildException be) { 271 } 273 } 274 public void testTestDependenciesCycleDisabledTestSort() throws IOException { 275 createModule(a,new String []{b},new String []{b},NULL); 276 createModule(b,NULL,new String []{a},NULL); 277 Path path = createPath(new String []{a,b}); 278 SortSuiteModules ssm = new SortSuiteModules(); 279 ssm.setProject(project); 280 ssm.setUnsortedModules(path); 281 ssm.setSortedModulesProperty(SORTED_MODULES); 282 ssm.execute(); 284 } 285 } 286 | Popular Tags |