1 19 20 package org.netbeans.modules.projectimport.j2seimport; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.util.jar.JarEntry ; 26 import java.util.jar.JarOutputStream ; 27 import junit.framework.*; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.filesystems.FileUtil; 30 31 32 36 public class AbstractProjectDefinitionTest extends NbTestCase { 37 protected AbstractProject testProject; 38 39 static { 40 System.setProperty("projectimport.logging.level", "FINEST"); 41 } 42 43 public AbstractProjectDefinitionTest(String testName) { 44 super(testName); 45 } 46 47 protected final void setUp() throws Exception { 48 clearWorkDir(); 49 try { 50 testProject = new AbstractProject(getName(), FileUtil.toFileObject(getWorkDir())); 51 } catch(IOException iex) { 52 assert false : iex.getLocalizedMessage(); 53 throw new IllegalStateException (iex.getLocalizedMessage()); 54 } 55 56 specificSetUp(testProject); 57 } 58 59 60 protected void specificSetUp(AbstractProject projectDefinition) {} 61 62 private AbstractProject generalSetUp(String testName) { 63 AbstractProject retVal = null; 64 try { 65 retVal = new AbstractProject(testName, FileUtil.toFileObject(getWorkDir())); 66 } catch(IOException iex) { 67 assert false : iex.getLocalizedMessage(); 68 throw new IllegalStateException (iex.getLocalizedMessage()); 69 } 70 71 return retVal; 72 } 73 74 protected void tearDown() throws Exception { 75 clearWorkDir(); 76 } 77 78 79 public static Test suite() { 80 TestSuite suite = new TestSuite(AbstractProjectDefinitionTest.class); 81 82 return suite; 83 } 84 85 88 public void testGetName() { 89 assertEquals(testProject.getName(), this.getName()); 90 } 91 92 95 public void testGetProjectDir() throws Exception { 96 assertEquals(testProject.getProjectDir(), FileUtil.toFileObject(getWorkDir())); 97 } 98 99 102 public void testIsAlreadyImported() { 103 assertFalse(testProject.isAlreadyImported()); 104 } 105 106 109 public void testSetAsImported() { 110 testProject.setAsImported(); 111 assertTrue(testProject.isAlreadyImported()); 112 } 113 114 117 public void testGetLibraryEntries() { 118 assertNotNull(testProject.getLibraries()); 119 assertEquals(testProject.getLibraries().size(),0); 120 } 121 122 125 public void testAddLibraryEntry() throws Exception { 126 File archiv = createArchivFile(getWorkDir(),"lib.jar"); 127 128 assertEquals(testProject.getLibraries().size(),0); 129 assertEquals(testProject.getWarnings().size(),0); 130 131 AbstractProject.Library library = new AbstractProject.Library(archiv); 132 assertTrue(library.isValid()); 133 testProject.addLibrary(library); 134 assertEquals(testProject.getLibraries().size(),1); 135 assertEquals(testProject.getWarnings().size(),0); 136 assertTrue(testProject.getLibraries().contains(library)); 137 138 139 140 library = new AbstractProject.Library(getWorkDir()); 141 assertFalse(library.isValid()); 142 testProject.addLibrary(library); 143 assertEquals(testProject.getLibraries().size(),2); 144 assertEquals(testProject.getWarnings().size(),1); 145 assertTrue(testProject.getLibraries().contains(library)); 146 } 147 148 151 public void testGetUserLibraries() { 152 assertNotNull(testProject.getUserLibraries()); 153 assertEquals(testProject.getUserLibraries().size(),0); 154 } 155 156 159 public void testAddLUserLibrary() throws Exception { 160 AbstractProject.UserLibrary uLibrary = new AbstractProject.UserLibrary(this.getName()); 161 assertFalse("UserLibrary is valid if isn't empty",uLibrary.isValid()); 162 163 164 165 AbstractProject.Library library1 = 166 new AbstractProject.Library(createArchivFile(getWorkDir(),"lib1.jar")); 168 169 uLibrary.addLibrary(library1); 170 assertTrue(uLibrary.isValid()); 171 assertEquals(uLibrary.getLibraries().size(),1); 172 assertTrue(uLibrary.getLibraries().contains(library1)); 173 174 175 testProject.addUserLibrary(uLibrary); 176 assertEquals(testProject.getUserLibraries().size(),1); 177 assertEquals(testProject.getWarnings().size(),0); 178 assertTrue(testProject.getUserLibraries().contains(uLibrary)); 179 assertFalse("the same can be added just once",testProject.addUserLibrary(uLibrary)); 180 assertEquals("the same library isn't added again",testProject.getUserLibraries().size(),1); 181 assertEquals(testProject.getWarnings().size(),1); 182 183 184 AbstractProject.UserLibrary uLibrary2 = new AbstractProject.UserLibrary(this.getName()+"2"); 185 testProject.addUserLibrary(uLibrary2); 186 187 assertEquals("UserLibrary must be added in non empy state else warning is fired",testProject.getWarnings().size(),2); 189 } 190 191 192 public static File createArchivFile(File workDir,String name) throws Exception { 193 File archiv = new File (workDir, name); 194 assertTrue(archiv.createNewFile()); 195 JarOutputStream jos = new JarOutputStream (new FileOutputStream (archiv)); 196 jos.setComment("Just for testing"); JarEntry je = new JarEntry ("oneEntry"); jos.putNextEntry(je); 199 jos.close(); 200 201 return archiv; 202 } 203 204 } 205 | Popular Tags |