1 15 package hivemind.test.ant; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.LineNumberReader ; 26 27 import org.apache.hivemind.ant.ConstructRegistry; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.Target; 31 import org.apache.tools.ant.types.Path; 32 33 47 public class TestConstructRegistry extends FrameworkTestCase 48 { 49 private static final boolean JDK1_3 = System.getProperty("java.version").startsWith("1.3."); 50 51 protected Project _project = new Project(); 52 53 protected ConstructRegistry create() 54 { 55 Target t = new Target(); 56 57 ConstructRegistry result = new ConstructRegistry(); 58 result.setProject(_project); 59 result.setOwningTarget(t); 60 result.setTaskName("constructRegistry"); 61 62 return result; 63 } 64 65 public void testNoFile() throws Exception 66 { 67 ConstructRegistry cr = create(); 68 69 try 70 { 71 cr.execute(); 72 unreachable(); 73 } 74 catch (BuildException ex) 75 { 76 assertExceptionSubstring(ex, "You must specify an output file"); 77 } 78 } 79 80 public void testNoDescriptors() throws Exception 81 { 82 ConstructRegistry cr = create(); 83 84 File f = File.createTempFile("testNoDescriptors-", ".xml"); 85 86 cr.setOutput(f); 87 88 assertSame(f, cr.getOutput()); 89 90 try 91 { 92 cr.execute(); 93 unreachable(); 94 } 95 catch (BuildException ex) 96 { 97 assertExceptionSubstring(ex, "You must specify a set of modules"); 98 } 99 100 f.delete(); 101 } 102 103 public void _testBasic() throws Exception 104 { 105 if (JDK1_3) 106 return; 107 108 ConstructRegistry cr = create(); 109 110 Path p = cr.createModules(); 111 112 p.createPath().setLocation( 113 new File (getFrameworkPath("/test-data/TestConstructRegistry/master.xml"))); 114 p.createPath().setLocation( 115 new File (getFrameworkPath("/test-data/TestConstructRegistry/Symbols.xml"))); 116 117 File output = File.createTempFile("testBasic-", ".xml"); 118 119 121 output.delete(); 122 123 output.deleteOnExit(); 124 125 cr.setOutput(output); 126 127 cr.execute(); 128 129 compare( 130 output, 131 getFrameworkPath("/test-data/TestConstructRegistry/testBasic.xml.master")); 132 } 133 134 public void _testLocalRefs() throws Exception 135 { 136 if (JDK1_3) 137 return; 138 139 ConstructRegistry cr = create(); 140 141 Path p = cr.createModules(); 142 143 p.createPath().setLocation( 144 new File (getFrameworkPath("/test-data/TestConstructRegistry/LocalRefs.xml"))); 145 146 File output = File.createTempFile("testLocalRefs-", ".xml"); 147 148 150 output.delete(); 151 152 output.deleteOnExit(); 153 154 cr.setOutput(output); 155 156 cr.execute(); 157 158 compare( 159 output, 160 getFrameworkPath("/test-data/TestConstructRegistry/testLocalRefs.xml.master")); 161 } 162 163 public void _testUptoDate() throws Exception 164 { 165 if (JDK1_3) 166 return; 167 168 ConstructRegistry cr = create(); 169 170 Path p = cr.createModules(); 171 172 p.createPath().setLocation( 173 new File (getFrameworkPath("/test-data/TestConstructRegistry/master.xml"))); 174 p.createPath().setLocation( 175 new File (getFrameworkPath("/test-data/TestConstructRegistry/Symbols.xml"))); 176 177 File output = File.createTempFile("testUptoDate-", ".xml"); 178 179 181 output.delete(); 182 183 output.deleteOnExit(); 184 185 cr.setOutput(output); 186 187 cr.execute(); 188 189 compare( 190 output, 191 getFrameworkPath("/test-data/TestConstructRegistry/testUptoDate.xml.master")); 192 193 long stamp = output.lastModified(); 194 195 cr.execute(); 196 197 assertEquals(stamp, output.lastModified()); 198 } 199 200 public void _testJars() throws Exception 201 { 202 if (JDK1_3) 203 return; 204 205 ConstructRegistry cr = create(); 206 207 Path p = cr.createModules(); 208 209 p.createPath().setLocation( 210 new File (getFrameworkPath("/test-data/TestConstructRegistry/master.xml"))); 211 p.createPath().setLocation( 212 new File (getFrameworkPath("/test-data/TestConstructRegistry/empty.jar"))); 213 p.createPath().setLocation( 214 new File (getFrameworkPath("/test-data/TestConstructRegistry/module.jar"))); 215 216 217 File output = File.createTempFile("testJars-", ".xml"); 218 219 output.delete(); 220 221 output.deleteOnExit(); 222 223 cr.setOutput(output); 224 225 cr.execute(); 226 227 compare(output, getFrameworkPath("/test-data/TestConstructRegistry/testJars.xml.master")); 228 } 229 230 protected void compare(File actual, String expectedPath) throws Exception 231 { 232 String expectedContent = readFile(new File (expectedPath)); 233 String actualContent = readFile(actual); 234 235 assertEquals(expectedContent, actualContent); 236 } 237 238 protected String readFile(File f) throws Exception 239 { 240 InputStream in = new FileInputStream (f); 241 BufferedInputStream bin = new BufferedInputStream (in); 242 InputStreamReader reader = new InputStreamReader (bin); 243 BufferedReader br = new BufferedReader (reader); 244 LineNumberReader r = new LineNumberReader (br); 245 246 StringBuffer buffer = new StringBuffer (); 247 248 while (true) 249 { 250 String line = r.readLine(); 251 252 if (line == null) 253 break; 254 255 buffer.append(line); 256 buffer.append("\n"); 257 } 258 259 r.close(); 260 261 return buffer.toString(); 262 } 263 264 } | Popular Tags |