1 19 20 package org.netbeans.modules.j2ee.earproject; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.lang.ref.Reference ; 29 import java.lang.ref.WeakReference ; 30 import java.net.URL ; 31 import javax.xml.parsers.SAXParser ; 32 import javax.xml.parsers.SAXParserFactory ; 33 import junit.framework.Assert; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.api.project.ProjectManager; 36 import org.netbeans.api.project.ui.OpenProjects; 37 import org.netbeans.junit.NbTestCase; 38 import org.netbeans.modules.j2ee.dd.api.application.Application; 39 import org.netbeans.modules.j2ee.dd.api.application.DDProvider; 40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 41 import org.netbeans.modules.j2ee.earproject.EarProject.ProjectOpenedHookImpl; 42 import org.netbeans.modules.j2ee.earproject.test.TestUtil; 43 import org.netbeans.modules.j2ee.earproject.ui.wizards.NewEarProjectWizardIteratorTest; 44 import org.netbeans.spi.project.ui.LogicalViewProvider; 45 import org.netbeans.spi.project.ui.ProjectOpenedHook; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.nodes.Node; 49 import org.xml.sax.SAXException ; 50 import org.xml.sax.SAXParseException ; 51 import org.xml.sax.helpers.DefaultHandler ; 52 53 56 public class EarProjectTest extends NbTestCase { 57 58 private String serverID; 59 60 public EarProjectTest(String testName) { 61 super(testName); 62 } 63 64 protected void setUp() throws Exception { 65 super.setUp(); 66 TestUtil.makeScratchDir(this); 67 serverID = TestUtil.registerSunAppServer(this); 68 } 69 70 public void testEarWithoutDDOpening() throws Exception { File prjDirF = new File (getWorkDir(), "TestEarProject_15"); 72 EarProjectGenerator.createProject(prjDirF, "test-project", 73 J2eeModule.JAVA_EE_5, serverID, "1.5"); 74 File dirCopy = copyFolder(prjDirF); 75 File ddF = new File (dirCopy, "src/conf/application.xml"); 76 assertTrue("has deployment descriptor", ddF.isFile()); 77 ddF.delete(); FileObject fo = FileUtil.toFileObject(dirCopy); 79 Project project = ProjectManager.getDefault().findProject(fo); 80 assertNotNull("project is found", project); 81 EarProjectTest.openProject((EarProject) project); 83 } 84 85 public void testThatMissingDDIsRegeneratedCorrectlyDuringOpening() throws Exception { File earDirF = new File (getWorkDir(), "testEA"); 87 String name = "Test EnterpriseApplication"; 88 String j2eeLevel = J2eeModule.JAVA_EE_5; 89 String ejbName = "testEA-ejb"; 90 String acName = "testEA-ac"; 91 NewEarProjectWizardIteratorTest.generateEARProject(earDirF, name, j2eeLevel, 92 serverID, null, ejbName, acName, null, null, null); 93 File dirCopy = copyFolder(earDirF); 94 File ddF = new File (dirCopy, "src/conf/application.xml"); 95 assertTrue("has deployment descriptor", ddF.isFile()); 96 validate(ddF); 97 ddF.delete(); FileObject fo = FileUtil.toFileObject(dirCopy); 99 Project project = ProjectManager.getDefault().findProject(fo); 100 assertNotNull("project is found", project); 101 EarProjectTest.openProject((EarProject) project); 102 assertTrue("deployment descriptor was regenerated", ddF.isFile()); 103 validate(ddF); 104 Application app = DDProvider.getDefault().getDDRoot(FileUtil.toFileObject(ddF)); 105 assertSame("two modules", 2, app.getModule().length); 106 } 107 108 public void testOpeningWihtoutPrivateMetadataAndSrcDirectory() throws Exception { File earDirF = new File (getWorkDir(), "testEA"); 110 String name = "Test EnterpriseApplication"; 111 String j2eeLevel = J2eeModule.JAVA_EE_5; 112 String ejbName = "testEA-ejb"; 113 String acName = "testEA-ac"; 114 NewEarProjectWizardIteratorTest.generateEARProject(earDirF, name, j2eeLevel, 115 serverID, null, ejbName, acName, null, null, null); 116 File dirCopy = copyFolder(earDirF); 117 TestUtil.deleteRec(new File (new File (dirCopy, "nbproject"), "private")); 118 TestUtil.deleteRec(new File (dirCopy, "src")); 119 TestUtil.deleteRec(new File (new File (new File (dirCopy, "testEA-ac"), "nbproject"), "private")); 120 TestUtil.deleteRec(new File (new File (new File (dirCopy, "testEA-ejb"), "nbproject"), "private")); 121 FileObject fo = FileUtil.toFileObject(dirCopy); 122 Project project = ProjectManager.getDefault().findProject(fo); 123 assertNotNull("project is found", project); 124 EarProjectTest.openProject((EarProject) project); 125 } 126 127 public void testEarProjectIsGCed() throws Exception { File prjDirF = new File (getWorkDir(), "testEA"); 129 String name = "Test EnterpriseApplication"; 130 String j2eeLevel = "1.4"; 131 132 NewEarProjectWizardIteratorTest.generateEARProject(prjDirF, name, j2eeLevel, 134 serverID, null, null, null, null, null, null); 135 Project earProject = ProjectManager.getDefault().findProject(FileUtil.toFileObject(prjDirF)); 136 EarProjectTest.openProject((EarProject) earProject); 137 Node rootNode = ((LogicalViewProvider) earProject.getLookup().lookup(LogicalViewProvider.class)).createLogicalView(); 138 rootNode.getChildren().getNodes(true); Reference <Project> wr = new WeakReference <Project>(earProject); 140 OpenProjects.getDefault().close(new Project[] { earProject }); 141 EarProjectTest.closeProject((EarProject) earProject); 142 rootNode = null; 143 earProject = null; 144 assertGC("project cannot be garbage collected", wr); 145 } 146 147 151 public static void openProject(final EarProject p) { 152 ProjectOpenedHookImpl hook = (ProjectOpenedHookImpl) p.getLookup().lookup(ProjectOpenedHook.class); 153 assertNotNull("has an OpenedHook", hook); 154 hook.projectOpened(); } 156 157 public static void closeProject(final EarProject p) { 158 ProjectOpenedHookImpl hook = (ProjectOpenedHookImpl) p.getLookup().lookup(ProjectOpenedHook.class); 159 assertNotNull("has an OpenedHook", hook); 160 hook.projectClosed(); } 162 163 167 private File copyFolder(File d) throws IOException { 168 assert d.isDirectory(); 169 File workdir = getWorkDir(); 170 String name = d.getName(); 171 while (name.length() < 3) { 172 name = name + "x"; 173 } 174 File todir = workdir.createTempFile(name, null, workdir); 175 todir.delete(); 176 doCopy(d, todir); 177 return todir; 178 } 179 180 private static void doCopy(File from, File to) throws IOException { 181 if (from.isDirectory()) { 182 if (from.getName().equals("CVS")) { 183 return; 184 } 185 to.mkdir(); 186 String [] kids = from.list(); 187 for (int i = 0; i < kids.length; i++) { 188 doCopy(new File (from, kids[i]), new File (to, kids[i])); 189 } 190 } else { 191 assert from.isFile(); 192 InputStream is = new FileInputStream (from); 193 try { 194 OutputStream os = new FileOutputStream (to); 195 try { 196 FileUtil.copy(is, os); 197 } finally { 198 os.close(); 199 } 200 } finally { 201 is.close(); 202 } 203 } 204 } 205 206 public static void validate(final File ddFile) throws Exception { 207 SAXParserFactory f = (SAXParserFactory ) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); 208 if (f == null) { 209 System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath"); 210 return; 211 } 212 f.setNamespaceAware(true); 213 f.setValidating(true); 214 SAXParser p = f.newSAXParser(); 215 URL schemaURL_1_4 = EarProjectTest.class.getResource("/org/netbeans/modules/j2ee/dd/impl/resources/application_1_4.xsd"); 216 URL schemaURL_5 = EarProjectTest.class.getResource("/org/netbeans/modules/j2ee/dd/impl/resources/application_5.xsd"); 217 assertNotNull("have access to schema", schemaURL_1_4); 218 assertNotNull("have access to schema", schemaURL_5); 219 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 220 "http://www.w3.org/2001/XMLSchema"); 221 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", new String [] { 222 schemaURL_1_4.toExternalForm(), 223 schemaURL_5.toExternalForm() 224 }); 225 try { 226 p.parse(ddFile.toURI().toString(), new Handler ()); 227 } catch (SAXParseException e) { 228 fail("Validation of XML document " + ddFile + " against schema failed. Details: " + 229 e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage()); 230 } 231 } 232 233 public static void validate(FileObject ddFO) throws Exception { 234 Assert.assertNotNull(ddFO); 235 File ddF = FileUtil.toFile(ddFO); 236 Assert.assertNotNull(ddF); 237 validate(ddF); 238 } 239 240 private static final class Handler extends DefaultHandler { 241 public void warning(SAXParseException e) throws SAXException { 242 throw e; 243 } 244 public void error(SAXParseException e) throws SAXException { 245 throw e; 246 } 247 public void fatalError(SAXParseException e) throws SAXException { 248 throw e; 249 } 250 } 251 252 } 253 | Popular Tags |