1 19 20 package org.netbeans.modules.junit; 21 22 import org.openide.*; 23 import org.openide.src.*; 24 import org.openide.filesystems.*; 25 import org.openide.loaders.*; 26 import org.openide.cookies.*; 27 import org.netbeans.modules.java.*; 28 29 import junit.framework.*; 30 import org.netbeans.junit.*; 31 32 import java.io.*; 33 import java.lang.reflect.*; 34 import java.util.*; 35 36 public class TestCreatorTest extends NbTestCase implements DataLoader.RecognizedFiles { 37 38 public TestCreatorTest(java.lang.String testName) { 39 super(testName); 40 } 41 42 public static void main(java.lang.String [] args) { 43 junit.textui.TestRunner.run(suite()); 44 } 45 46 public static Test suite() { 47 return new TestSuite(TestCreatorTest.class); 48 } 49 50 51 public void testCreateTestClass() throws Exception { System.out.println("testCreateTestClass"); 53 54 JUnitSettings js = JUnitSettings.getDefault(); 55 56 setGenerateFlags(js, true); 57 TestCreator.initialize(); 58 59 JavaDataLoader jdl = new JavaDataLoader(); 60 LocalFileSystem fsData = new LocalFileSystem(); 61 fsData.setRootDirectory(new File(appendSlash(m_pathData) + "CreateTestClass")); 62 63 FileObject foSrc = fsData.findResource("src"); 64 FileObject foTrg = fsData.findResource("trg"); 65 FileObject foPass = fsData.findResource("pass"); 66 67 DataObject doTempl = jdl.findDataObject (fsData.findResource(CLASS_TEMPLATE), this); 68 DataFolder doTrg = (DataFolder) DataFolder.find(foTrg); 69 70 FileObject foList[] = foSrc.getChildren(); 71 for (int i = 0; i< foList.length; i++) { 72 FileObject foEntry = foList[i]; 73 if (foEntry.getExt().equals("java")) { 74 DataObject doSrcEntry = null; 75 DataObject doTrgEntry = null; 76 String name = foEntry.getName() + "Test"; 77 File fTmp = new File(appendSlash(fsData.getRootDirectory().getPath()) + appendSlash(foTrg.getNameExt()) + name + ".java"); 78 79 fTmp.delete(); 80 doSrcEntry = jdl.findDataObject (foEntry, this); 81 doTrgEntry = doTempl.createFromTemplate(doTrg, name); 82 83 ClassElement ceSrc = getClassElementFromDO(doSrcEntry); 84 ClassElement ceTrg = getClassElementFromDO(doTrgEntry); 85 TestCreator.createTestClass(ceSrc, ceTrg); 86 SaveCookie sc = (SaveCookie) doTrgEntry.getCookie(SaveCookie.class); 87 sc.save(); 88 89 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), name + ".java"), new File(System.getProperty("xresults"))); 90 } 91 } 92 93 DataObject doSrcEntry = DataObject.find(fsData.findResource("src/TestClass001.java")); 94 DataObject doTrgEntry; 95 ClassElement ceSrc = getClassElementFromDO(doSrcEntry); 96 ClassElement ceTrg; 97 98 setGenerateFlags(js, false); 99 100 new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestPublic.java").delete(); 101 js.setMembersPublic(true); assertTrue(true == js.isMembersPublic()); 102 TestCreator.initialize(); 103 doTrgEntry = doTempl.createFromTemplate(doTrg, "TestPublic"); 104 ceTrg = getClassElementFromDO(doTrgEntry); 105 TestCreator.createTestClass(ceSrc, ceTrg); 106 ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save(); 107 js.setMembersPublic(false); assertTrue(false == js.isMembersPublic()); 108 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestPublic.java"), new File(System.getProperty("xresults"))); 109 110 new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestProtected.java").delete(); 111 js.setMembersProtected(true); assertTrue(true == js.isMembersProtected()); 112 TestCreator.initialize(); 113 doTrgEntry = doTempl.createFromTemplate(doTrg, "TestProtected"); 114 ceTrg = getClassElementFromDO(doTrgEntry); 115 TestCreator.createTestClass(ceSrc, ceTrg); 116 ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save(); 117 js.setMembersProtected(false); assertTrue(false == js.isMembersProtected()); 118 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestProtected.java"), new File(System.getProperty("xresults"))); 119 120 new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestPackage.java").delete(); 121 js.setMembersPackage(true); assertTrue(true == js.isMembersPackage()); 122 TestCreator.initialize(); 123 doTrgEntry = doTempl.createFromTemplate(doTrg, "TestPackage"); 124 ceTrg = getClassElementFromDO(doTrgEntry); 125 TestCreator.createTestClass(ceSrc, ceTrg); 126 ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save(); 127 js.setMembersPackage(false); assertTrue(false == js.isMembersPackage()); 128 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestPackage.java"), new File(System.getProperty("xresults"))); 129 130 new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestBodyComments.java").delete(); 131 js.setMembersPublic(true); assertTrue(true == js.isMembersPublic()); 132 TestCreator.initialize(); 133 js.setBodyComments(true); assertTrue(true == js.isBodyComments()); 134 doTrgEntry = doTempl.createFromTemplate(doTrg, "TestBodyComments"); 135 ceTrg = getClassElementFromDO(doTrgEntry); 136 TestCreator.createTestClass(ceSrc, ceTrg); 137 ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save(); 138 js.setBodyComments(false); assertTrue(false == js.isBodyComments()); 139 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestBodyComments.java"), new File(System.getProperty("xresults"))); 140 141 new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestBodyContent.java").delete(); 142 js.setBodyContent(true); assertTrue(true == js.isBodyContent()); 143 doTrgEntry = doTempl.createFromTemplate(doTrg, "TestBodyContent"); 144 ceTrg = getClassElementFromDO(doTrgEntry); 145 TestCreator.createTestClass(ceSrc, ceTrg); 146 ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save(); 147 js.setBodyContent(false); assertTrue(false == js.isBodyContent()); 148 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestBodyContent.java"), new File(System.getProperty("xresults"))); 149 150 new File(appendSlash(fsData.getRootDirectory().getPath()) + "trg/TestJavaDoc.java").delete(); 151 js.setJavaDoc(true); assertTrue(true == js.isJavaDoc()); 152 doTrgEntry = doTempl.createFromTemplate(doTrg, "TestJavaDoc"); 153 ceTrg = getClassElementFromDO(doTrgEntry); 154 TestCreator.createTestClass(ceSrc, ceTrg); 155 ((SaveCookie) doTrgEntry.getCookie(SaveCookie.class)).save(); 156 js.setJavaDoc(false); assertTrue(false == js.isJavaDoc()); 157 assertFile(getFile(doTrgEntry.getPrimaryFile()), new File(getFile(foPass), "TestJavaDoc.java"), new File(System.getProperty("xresults"))); 158 } 159 160 161 public void testCreateTestSuit() throws Exception { 162 System.out.println("testCreateTestSuit"); 163 164 JavaDataLoader jdl = new JavaDataLoader(); 165 LocalFileSystem fsData = new LocalFileSystem(); 166 fsData.setRootDirectory(new File(appendSlash(m_pathData) + "CreateTestSuite")); 167 168 FileObject foPass = fsData.findResource(TESTSUITE + ".java.pass"); 169 DataObject doTempl = jdl.findDataObject (fsData.findResource(CLASS_TEMPLATE), this); 170 171 File fTmp = new File(appendSlash(fsData.getRootDirectory().getPath()) + TESTSUITE + ".java"); 172 173 fTmp.delete(); 174 DataObject doTrgEntry = doTempl.createFromTemplate((DataFolder)DataFolder.find(fsData.getRoot()), TESTSUITE); 175 ClassElement ceTrg = getClassElementFromDO(doTrgEntry); 176 177 LinkedList lst = new LinkedList(); 178 lst.add("a.b.c.TestClass1"); 179 lst.add("a.b.c.TestClass2"); 180 lst.add("TestClass3"); 181 182 TestCreator.createTestSuit(lst, "some.test.pckg", ceTrg); 183 SaveCookie sc = (SaveCookie) doTrgEntry.getCookie(SaveCookie.class); 184 sc.save(); 185 186 assertFile(getFile(doTrgEntry.getPrimaryFile()), getFile(foPass), new File(System.getProperty("xresults"))); 187 } 188 189 190 public void testInitialize() { 191 System.out.println("testInitialize"); 192 193 } 195 196 197 public void testIsClassTestable() throws Exception { 198 System.out.println("testIsClassTestable"); 199 200 FileObject foClass; 201 DataObject doClass; 202 ClassElement clazz; 203 JUnitSettings js = JUnitSettings.getDefault(); 204 JavaDataLoader jdl = new JavaDataLoader(); 205 LocalFileSystem fsData = new LocalFileSystem(); 206 fsData.setRootDirectory(new File(appendSlash(m_pathData) + "IsClassTestable")); 207 208 foClass = fsData.findResource("SimpleClass.java"); 209 doClass = jdl.findDataObject (foClass, this); 210 clazz = getClassElementFromDO(doClass); 211 assertTrue(true == TestCreator.isClassTestable(clazz)); 212 213 foClass = fsData.findResource("NonPublicClass.java"); 214 doClass = jdl.findDataObject (foClass, this); 215 clazz = getClassElementFromDO(doClass); 216 assertTrue(false == TestCreator.isClassTestable(clazz)); 217 218 foClass = fsData.findResource("AbstractClass.java"); 219 doClass = jdl.findDataObject (foClass, this); 220 clazz = getClassElementFromDO(doClass); 221 assertTrue(true == TestCreator.isClassTestable(clazz)); 222 223 foClass = fsData.findResource("SimpleInterface.java"); 224 doClass = jdl.findDataObject (foClass, this); 225 clazz = getClassElementFromDO(doClass); 226 assertTrue(false == TestCreator.isClassTestable(clazz)); 227 228 js.setGenerateExceptionClasses(false); 229 assertTrue(false == js.isGenerateExceptionClasses()); 230 foClass = fsData.findResource("ExceptionClass.java"); 231 doClass = jdl.findDataObject (foClass, this); 232 clazz = getClassElementFromDO(doClass); 233 assertTrue(false == TestCreator.isClassTestable(clazz)); 234 235 js.setGenerateExceptionClasses(true); 236 assertTrue(true == js.isGenerateExceptionClasses()); 237 assertTrue(true == TestCreator.isClassTestable(clazz)); 238 } 239 240 protected static final String PROP_CLASSPATH = "java.class.path"; 242 protected String m_pathData = null; 243 244 protected void setUp() { 245 if (null == m_pathData) 246 m_pathData = System.getProperty("xdata"); 247 } 248 249 protected void tearDown() { 250 } 251 252 protected class FileExtFilter implements FileFilter { 253 protected String extension = null; 254 public FileExtFilter(String extension) { 255 this.extension = extension; 256 } 257 public boolean accept(File pathname) { 258 String ext = null; 259 int i; 260 261 if (-1 != (i = pathname.getPath().lastIndexOf('.'))) 262 ext = pathname.getPath().substring(i + 1); 263 264 if (null == ext) 265 return (null == extension); 266 267 return extension.equals(ext); 268 } 269 } 270 271 private static final String CLASS_TEMPLATE = "ClassTemplate.java"; 273 private static final String TESTSUITE = "TestSuite"; 274 275 private String appendSlash(String path) { 276 if (null == path) 277 return new String (); 278 279 if (!path.endsWith("\\") && !path.endsWith("/")) 280 return path + "\\"; 281 282 return path; 283 } 284 285 private ClassElement getClassElementFromDO(DataObject dO) { 286 SourceCookie sc; 287 SourceElement se; 288 289 sc = (SourceCookie) dO.getCookie(SourceCookie.class); 290 se = sc.getSource(); 291 return se.getClass(Identifier.create(dO.getPrimaryFile().getName())); 292 } 293 294 private File getFile(FileObject fo) throws FileStateInvalidException { 295 StringBuffer name = new StringBuffer (); 296 LocalFileSystem lfs = (LocalFileSystem)fo.getFileSystem(); 297 298 name.append(lfs.getRootDirectory().getPath()); 299 if (0 != name.length()) 300 name.append("/"); 301 name.append(fo.getPackageNameExt('/', '.')); 302 303 return new File(name.toString()); 304 } 305 306 private void setGenerateFlags(JUnitSettings js, boolean flag) { 307 js.setMembersPublic(flag); assertTrue(flag == js.isMembersPublic()); 308 js.setMembersProtected(flag); assertTrue(flag == js.isMembersProtected()); 309 js.setMembersPackage(flag); assertTrue(flag == js.isMembersPackage()); 310 js.setBodyComments(flag); assertTrue(flag == js.isBodyComments()); 311 js.setBodyContent(flag); assertTrue(flag == js.isBodyContent()); 312 js.setJavaDoc(flag); assertTrue(flag == js.isJavaDoc()); 313 } 314 315 public void markRecognized(FileObject fo) { 316 } 317 318 } 319 | Popular Tags |