1 19 20 package org.netbeans.modules.j2ee.earproject; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import org.netbeans.api.project.ProjectManager; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 29 import org.netbeans.modules.j2ee.earproject.test.TestUtil; 30 import org.netbeans.spi.project.support.ant.AntProjectHelper; 31 import org.netbeans.spi.project.support.ant.EditableProperties; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.Mutex; 35 import org.openide.xml.XMLUtil; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.xml.sax.InputSource ; 39 40 43 public class EarProjectGeneratorTest extends NbTestCase { 44 45 private String serverID; 46 47 private static final String [] CREATED_FILES = { 48 "build.xml", 49 "nbproject/build-impl.xml", 50 "nbproject/genfiles.properties", 51 "nbproject/project.xml", 52 "nbproject/project.properties", 53 "nbproject/private/private.properties", 54 "src/conf/application.xml" 55 }; 56 57 private static final String [] CREATED_FILES_EXT_SOURCES = { 58 "build.xml", 59 "nbproject/build-impl.xml", 60 "nbproject/genfiles.properties", 61 "nbproject/project.xml", 62 "nbproject/project.properties", 63 "nbproject/private/private.properties", 64 }; 65 66 private static final String [] CREATED_PROPERTIES = { 67 "build.classes.excludes", 68 "build.dir", 69 "build.generated.dir", 70 "client.module.uri", 71 "client.urlPart", 72 "debug.classpath", 73 "display.browser", 74 "dist.dir", 75 "dist.jar", 76 "j2ee.appclient.mainclass.args", 77 "j2ee.platform", 78 "j2ee.server.type", 79 "jar.compress", 80 "jar.content.additional", 81 "jar.name", 82 "javac.debug", 83 "javac.deprecation", 84 "javac.source", 85 "javac.target", 86 "meta.inf", 87 "no.dependencies", 88 "platform.active", 89 "resource.dir", 90 "source.root", 91 }; 92 93 private static final String [] CREATED_PROPERTIES_EXT_SOURCES = { 94 "build.classes.excludes", 95 "build.dir", 96 "build.generated.dir", 97 "client.module.uri", 98 "client.urlPart", 99 "debug.classpath", 100 "display.browser", 101 "dist.dir", 102 "dist.jar", 103 "j2ee.appclient.mainclass.args", 104 "j2ee.platform", 105 "j2ee.server.type", 106 "jar.compress", 107 "jar.content.additional", 108 "jar.name", 109 "javac.debug", 110 "javac.deprecation", 111 "javac.source", 112 "javac.target", 113 "meta.inf", 114 "no.dependencies", 115 "platform.active", 116 "source.root", 119 }; 120 121 public EarProjectGeneratorTest(String name) { 122 super(name); 123 } 124 125 protected void setUp() throws Exception { 126 super.setUp(); 127 TestUtil.makeScratchDir(this); 128 serverID = TestUtil.registerSunAppServer(this); 129 } 130 131 public void testCreateProject() throws Exception { 132 File prjDirF = new File (getWorkDir(), "EARProject"); 133 AntProjectHelper aph = EarProjectGenerator.createProject(prjDirF, "test-project", 134 J2eeModule.JAVA_EE_5, serverID, "1.5"); 135 assertNotNull(aph); 136 FileObject prjDirFO = aph.getProjectDirectory(); 137 for (String file : CREATED_FILES) { 138 assertNotNull(file + " file/folder cannot be found", prjDirFO.getFileObject(file)); 139 } 140 EditableProperties props = aph.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 141 @SuppressWarnings ("unchecked") 142 List createdProperties = new ArrayList (props.keySet()); 143 for (String property : CREATED_PROPERTIES) { 144 assertNotNull(property + " property cannot be found in project.properties", props.getProperty(property)); 145 createdProperties.remove(property); 146 } 147 assertEquals("Found unexpected property: " + createdProperties, 148 CREATED_PROPERTIES.length, props.keySet().size()); 149 } 150 151 public void testImportProject() throws Exception { 152 File prjDirF = new File (getWorkDir(), "EARProject"); 153 AntProjectHelper helper = EarProjectGenerator.importProject(prjDirF, prjDirF, 154 "test-project-ext-src", J2eeModule.JAVA_EE_5, serverID, null, 155 "1.5", Collections.<FileObject, ModuleType>emptyMap()); 156 assertNotNull(helper); 157 FileObject prjDirFO = FileUtil.toFileObject(prjDirF); 158 for (String createdFile : CREATED_FILES_EXT_SOURCES) { 159 assertNotNull(createdFile + " file/folder cannot be found", prjDirFO.getFileObject(createdFile)); 160 } 161 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 162 @SuppressWarnings ("unchecked") 163 List createdProperties = new ArrayList (props.keySet()); 164 int extFileRefCount = 0; 165 for (String propName : CREATED_PROPERTIES_EXT_SOURCES) { 166 String propValue = props.getProperty(propName); 167 assertNotNull(propName+" property cannot be found in project.properties", propValue); 168 createdProperties.remove(propName); 169 if ("manifest.file".equals(propName)) { 170 assertEquals("Invalid value of manifest.file property.", "manifest.mf", propValue); 171 } 172 } 173 assertEquals("Found unexpected property: " + createdProperties, 174 CREATED_PROPERTIES_EXT_SOURCES.length, props.keySet().size() - extFileRefCount); 175 } 176 177 public void testProjectNameIsSet() throws Exception { File prjDirF = new File (getWorkDir(), "EARProject"); 179 EarProjectGenerator.createProject(prjDirF, "test-project", 180 J2eeModule.JAVA_EE_5, serverID, "1.5"); 181 final File buildXML = new File (prjDirF, "build.xml"); 183 String projectName = (String ) ProjectManager.mutex().readAccess(new Mutex.ExceptionAction() { 184 public Object run() throws Exception { 185 Document doc = XMLUtil.parse(new InputSource (buildXML.toURI().toString()), 186 false, true, null, null); 187 Element project = doc.getDocumentElement(); 188 return project.getAttribute("name"); 189 } 190 }); 191 assertEquals("project name is set in the build.xml", "test-project", projectName); 192 } 193 194 public void testProjectNameIsEscaped() throws Exception { 195 final File prjDirF = new File (getWorkDir(), "EARProject"); 196 EarProjectGenerator.createProject(prjDirF, "test project", 197 J2eeModule.JAVA_EE_5, serverID, "1.5"); 198 String buildXmlProjectName = (String ) ProjectManager.mutex().readAccess(new Mutex.ExceptionAction() { 200 public Object run() throws Exception { 201 Document doc = XMLUtil.parse(new InputSource (new File (prjDirF, "build.xml").toURI().toString()), 202 false, true, null, null); 203 Element project = doc.getDocumentElement(); 204 return project.getAttribute("name"); 205 } 206 }); 207 assertEquals("project name is escaped in build.xml", "test_project", buildXmlProjectName); 208 String buildImplXmlProjectName = (String ) ProjectManager.mutex().readAccess(new Mutex.ExceptionAction() { 210 public Object run() throws Exception { 211 Document doc = XMLUtil.parse(new InputSource (new File (prjDirF, "nbproject/build-impl.xml").toURI().toString()), 212 false, true, null, null); 213 Element project = doc.getDocumentElement(); 214 return project.getAttribute("name"); 215 } 216 }); 217 assertEquals("project name is escaped in build-impl.xml", "test_project-impl", buildImplXmlProjectName); 218 } 219 220 } 221 | Popular Tags |