1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.FileOutputStream ; 23 import java.io.FileWriter ; 24 import java.util.jar.JarEntry ; 25 import java.util.jar.JarOutputStream ; 26 import java.util.jar.Manifest ; 27 import junit.framework.*; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 import java.util.StringTokenizer ; 33 import java.util.jar.JarFile ; 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.types.Parameter; 36 import org.apache.tools.ant.types.selectors.SelectorUtils; 37 38 import org.netbeans.junit.NbTestCase; 39 40 44 public class ModuleSelectorTest extends NbTestCase { 45 private ModuleSelector selector; 46 47 public ModuleSelectorTest(String testName) { 48 super(testName); 49 } 50 51 protected void setUp() throws Exception { 52 selector = new ModuleSelector(); 53 } 54 55 public void testIsSelectedForNotAModule() throws IOException { 56 File noModule = generateJar(new String [0], ModuleDependenciesTest.createManifest ()); 57 assertFalse("Not acceptable", selector.isSelected(getWorkDir(), noModule.toString(), noModule)); 58 } 59 60 public void testIncludesAllModulesByDefault() throws Exception { 61 Manifest m = ModuleDependenciesTest.createManifest (); 62 m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module"); 63 File aModule = generateJar(new String [0], m); 64 assertTrue("Accepted", selector.isSelected(getWorkDir(), aModule.toString(), aModule)); 65 } 66 67 public void testCanExcludeAModule() throws Exception { 68 Parameter p = new Parameter(); 69 p.setName("excludeModules"); 70 p.setValue("org.my.module"); 71 selector.setParameters(new Parameter[] { p }); 72 73 Manifest m = ModuleDependenciesTest.createManifest (); 74 m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module"); 75 File aModule = generateJar(new String [0], m); 76 assertFalse("Refused", selector.isSelected(getWorkDir(), aModule.toString(), aModule)); 77 } 78 79 public void testCanShowOnlyExcludedModules() throws Exception { 80 Parameter p = new Parameter(); 81 p.setName("excluded"); 82 p.setValue("true"); 83 Parameter p2 = new Parameter(); 84 p2.setName("excludeModules"); 85 p2.setValue("org.my.module"); 86 selector.setParameters(new Parameter[] { p, p2 }); 87 88 Manifest m = ModuleDependenciesTest.createManifest (); 89 m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module"); 90 File aModule = generateJar(new String [0], m); 91 assertTrue("Now we are accepting only excluded modules", selector.isSelected(getWorkDir(), aModule.toString(), aModule)); 92 } 93 94 public void testIsSelectedForNotAModuleIsStillFalseEvenWeAcceptOnlyExcludedModules() throws IOException { 95 Parameter p = new Parameter(); 96 p.setName("excluded"); 97 p.setValue("true"); 98 Parameter p2 = new Parameter(); 99 p2.setName("excludeModules"); 100 p2.setValue("org.my.module"); 101 selector.setParameters(new Parameter[] { p, p2 }); 102 103 104 File noModule = generateJar(new String [0], ModuleDependenciesTest.createManifest ()); 105 assertFalse("Not acceptable", selector.isSelected(getWorkDir(), noModule.toString(), noModule)); 106 } 107 108 public void testCanExcludeACluster() throws Exception { 109 Parameter p = new Parameter(); 110 p.setName("includeClusters"); 111 p.setValue("nonexistent"); 112 selector.setParameters(new Parameter[] { p }); 113 114 Manifest m = ModuleDependenciesTest.createManifest (); 115 m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module"); 116 File aModule = generateJar(new String [0], m); 117 new File (getWorkDir(), "update_tracking").mkdir(); 118 assertFalse("Refused", selector.isSelected(getWorkDir().getParentFile(), aModule.toString(), aModule)); 119 } 120 121 public void testWhatItDoesOnADirectory() throws Exception { 122 assertFalse("Refused", selector.isSelected(getWorkDir().getParentFile(), getWorkDir().getName(), getWorkDir())); 123 } 124 125 public void testNoManifest() throws Exception { 126 File aModule = generateJar(new String [] { "some/fake/entry.txt" }, null); 127 assertFalse("Refused", selector.isSelected(getWorkDir().getParentFile(), aModule.toString(), aModule)); 128 } 129 130 public void testParsingOfUpdateTrackingFiles() throws Exception { 131 doParsingOfUpdateTrackingFiles(1); 132 } 133 134 public void testParsingOfUpdateTrackingFilesOnMoreDirs() throws Exception { 135 doParsingOfUpdateTrackingFiles(2); 136 } 137 138 139 private void doParsingOfUpdateTrackingFiles(int parents) throws Exception { 140 File updateTracking = new File (getWorkDir(), "update-tracking"); 141 updateTracking.mkdirs(); 142 assertTrue("Created", updateTracking.isDirectory()); 143 144 Manifest m = ModuleDependenciesTest.createManifest (); 145 m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module"); 146 File aModule = generateJar(new String [0], m); 147 148 File trackingFile = new File (updateTracking, "org-my-module.xml"); 149 FileWriter w = new FileWriter (trackingFile); 150 w.write( 151 "<?xml version='1.0' encoding='UTF-8'?>\n" + 152 "<module codename='org.apache.tools.ant.module/3'>\n" + 153 "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" + 154 "<file name='ant/bin/ant' crc='1536373800'/>\n" + 155 "<file name='ant/bin/ant.bat' crc='3245456472'/>\n" + 156 "<file name='ant/bin/ant.cmd' crc='3819623376'/>\n" + 157 "<file name='ant/bin/antRun' crc='2103827286'/>\n" + 158 "<file name='ant/bin/antRun.bat' crc='2739687679'/>\n" + 159 "<file name='ant/bin/antRun.pl' crc='3955456526'/>\n" + 160 " </module_version>\n" + 161 "</module>\n" 162 ); 163 w.close(); 164 165 StringBuffer sb = new StringBuffer (); 166 sb.append(trackingFile.getPath()); 167 168 while (--parents > 0) { 169 File x = new File (getWorkDir(), parents + ".xml"); 170 FileWriter xw = new FileWriter (x); 171 xw.write( 172 "<?xml version='1.0' encoding='UTF-8'?>\n" + 173 "<module codename='" + x + "/3'>\n" + 174 "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" + 175 " </module_version>\n" + 176 "</module>\n" 177 ); 178 xw.close(); 179 180 sb.insert(0, File.pathSeparator); 181 sb.insert(0, x.getPath()); 182 } 183 184 Parameter p = new Parameter(); 185 p.setName("updateTrackingFiles"); 186 p.setValue(sb.toString()); 187 selector.setParameters(new Parameter[] { p }); 188 189 assertTrue("module accepted", selector.isSelected(getWorkDir(), aModule.toString(), aModule)); 190 assertTrue("its file as well", selector.isSelected(getWorkDir(), "ant/bin/ant.bat", new File (aModule.getParent(), "ant/bin/ant.bat"))); 191 assertTrue("also the tracking file is accepted", selector.isSelected(getWorkDir(), "update-tracking/" + trackingFile.getName(), trackingFile)); 192 } 193 194 private final File createNewJarFile () throws IOException { 195 int i = 0; 196 for (;;) { 197 File f = new File (this.getWorkDir(), i++ + ".jar"); 198 if (!f.exists ()) return f; 199 } 200 } 201 202 protected final File generateJar (String [] content, Manifest manifest) throws IOException { 203 File f = createNewJarFile (); 204 205 JarOutputStream os; 206 if (manifest != null) { 207 os = new JarOutputStream (new FileOutputStream (f), manifest); 208 } else { 209 os = new JarOutputStream (new FileOutputStream (f)); 210 } 211 212 for (int i = 0; i < content.length; i++) { 213 os.putNextEntry(new JarEntry (content[i])); 214 os.closeEntry(); 215 } 216 os.closeEntry (); 217 os.close(); 218 219 return f; 220 } 221 222 } 223 | Popular Tags |