1 19 package org.netbeans.test.j2ee; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectManager; 30 import org.netbeans.jellytools.JellyTestCase; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.junit.NbTestSuite; 33 import org.netbeans.modules.j2ee.earproject.EarProject; 34 import org.netbeans.modules.j2ee.earproject.EarProjectType; 35 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties; 36 import org.netbeans.spi.project.AuxiliaryConfiguration; 37 import org.netbeans.spi.project.support.ant.AntProjectHelper; 38 import org.netbeans.spi.project.support.ant.ReferenceHelper; 39 import org.openide.filesystems.FileUtil; 40 import org.netbeans.test.j2ee.lib.J2eeProjectSupport; 41 import org.netbeans.test.j2ee.lib.Reporter; 42 import org.netbeans.test.j2ee.lib.RequiredFiles; 43 44 45 52 public class J2eeProjectsTest extends JellyTestCase { 53 54 private Reporter reporter; 55 56 59 public J2eeProjectsTest(String name) { 60 super(name); 61 } 62 63 64 public void setUp() throws Exception { 65 super.setUp(); 66 reporter = Reporter.getReporter((NbTestCase)this); 67 } 68 69 public void tearDown() throws Exception { 70 super.tearDown(); 71 reporter.close(); 72 } 73 74 78 public void testCreateEjbProject() { 79 File wd = null; 80 try { 81 wd = getWorkDir(); 82 } catch (IOException ioe) { 83 ioe.printStackTrace(reporter.getLogStream()); 84 } 85 assertNotNull(wd); 86 RequiredFiles rf = null; 87 try { 88 rf = new RequiredFiles(new File (getDataDir(), "structures/ejbProject.str")); 89 } catch (IOException ioe) { 90 ioe.printStackTrace(reporter.getLogStream()); 91 } 92 assertNotNull(rf); 93 Project p = (Project) J2eeProjectSupport.createProject(wd, "EJBModule", J2eeProjectSupport.EJB_PROJECT, null); 94 assertNotNull(p); 95 checkProjectStructure(p, rf); 96 } 97 98 102 public void testCreateWebProject() { 103 File wd = null; 104 try { 105 wd = getWorkDir(); 106 } catch (IOException ioe) { 107 ioe.printStackTrace(reporter.getLogStream()); 108 } 109 assertNotNull(wd); 110 RequiredFiles rf = null; 111 try { 112 rf = new RequiredFiles(new File (getDataDir(), "structures/webProject.str")); 113 } catch (IOException ioe) { 114 ioe.printStackTrace(reporter.getLogStream()); 115 } 116 assertNotNull(rf); 117 Project p = (Project) J2eeProjectSupport.createProject(wd, "WebModule", J2eeProjectSupport.WEB_PROJECT, null); 118 assertNotNull(p); 119 checkProjectStructure(p, rf); 120 } 121 122 126 public void testCreateEmptyJ2eeProject() { 127 File wd = null; 128 try { 129 wd = getWorkDir(); 130 } catch (IOException ioe) { 131 ioe.printStackTrace(reporter.getLogStream()); 132 } 133 assertNotNull(wd); 134 RequiredFiles rf = null; 135 try { 136 rf = new RequiredFiles(new File (getDataDir(), "structures/emptyJ2eeProject.str")); 137 } catch (IOException ioe) { 138 ioe.printStackTrace(reporter.getLogStream()); 139 } 140 assertNotNull(rf); 141 Project p = (Project) J2eeProjectSupport.createProject(wd, "J2eePrj", J2eeProjectSupport.J2EE_PROJECT, null); 142 assertNotNull(p); 143 checkProjectStructure(p, rf); 144 } 145 146 149 public void testAddModulesToJ2eeProject() { 150 EarProject earPrj = null; 151 Project warPrj = null; 152 Project ejbPrj = null; 153 ProjectManager pm = ProjectManager.getDefault(); 154 try { 155 earPrj = (EarProject) pm.findProject(FileUtil.toFileObject(new File (getWorkDir(), "../testCreateEmptyJ2eeProject/J2eePrj").getCanonicalFile())); 156 warPrj = pm.findProject(FileUtil.toFileObject(new File (getWorkDir(), "../testCreateWebProject/WebModule").getCanonicalFile())); 157 ejbPrj = pm.findProject(FileUtil.toFileObject(new File (getWorkDir(), "../testCreateEjbProject/EJBModule").getCanonicalFile())); 158 AntProjectHelper h = earPrj.getUpdateHelper().getAntProjectHelper(); 159 AuxiliaryConfiguration aux = h.createAuxiliaryConfiguration(); 160 ReferenceHelper refHelper = new ReferenceHelper(h, aux, h.getStandardPropertyEvaluator()); 161 EarProjectProperties epp = new EarProjectProperties(earPrj, refHelper, new EarProjectType()); 162 epp.addJ2eeSubprojects(new Project[] {warPrj, ejbPrj}); 163 } catch (IOException ioe) { 164 ioe.printStackTrace(reporter.getLogStream()); 165 fail("IOEx while adding modules to EAR project."); 166 } 167 } 168 169 protected void checkProjectStructure(Project p, RequiredFiles r) { 170 Set l = J2eeProjectSupport.getFileSet(p); 171 Set rf = r.getRequiredFiles(); 172 reporter.ref("Project: " + p.toString()); 173 reporter.ref("Expected: " + rf); 174 reporter.ref("Real: " + l); 175 assertTrue("Files: " + getDifference(l, rf) + " are missing in project: " + p.toString() , l.containsAll(rf)); 176 rf = r.getRequiredFiles(); 177 reporter.ref("Project: " + p.toString()); 178 reporter.ref("Expected: " + rf); 179 reporter.ref("Real: " + l); 180 Set s = getDifference(rf, l); 181 assertTrue("Files: " + s + " are new in project: " + p.toString() , s.isEmpty()); 182 } 183 184 public static TestSuite suite() { 185 TestSuite suite = new NbTestSuite(); 186 suite.addTest(new J2eeProjectsTest("testCreateEjbProject")); 187 suite.addTest(new J2eeProjectsTest("testCreateWebProject")); 188 suite.addTest(new J2eeProjectsTest("testCreateEmptyJ2eeProject")); 189 suite.addTest(new J2eeProjectsTest("testAddModulesToJ2eeProject")); 190 return suite; 191 } 192 193 private Set getDifference(Set s1, Set s2) { 194 Set result = new HashSet (); 195 s2.removeAll(s1); 196 for (Iterator i = s2.iterator(); i.hasNext();) { 197 String s = (String ) i.next(); 198 if (s.indexOf(".LCK") < 0) { 199 result.add(s); 200 } else { 201 reporter.log("Additional file: " + s); 202 } 203 } 204 return result; 205 } 206 } 207 | Popular Tags |