1 19 20 package org.netbeans.modules.j2ee.earproject.ui.wizards; 21 22 import java.awt.Dialog ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.net.URL ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 import org.netbeans.api.java.classpath.ClassPath; 32 import org.netbeans.api.java.platform.JavaPlatform; 33 import org.netbeans.api.java.platform.Specification; 34 import org.netbeans.api.project.ProjectManager; 35 import org.netbeans.junit.NbTestCase; 36 import org.netbeans.modules.j2ee.dd.api.application.Application; 37 import org.netbeans.modules.j2ee.dd.api.application.DDProvider; 38 import org.netbeans.modules.j2ee.dd.api.application.Module; 39 import org.netbeans.modules.j2ee.dd.api.application.Web; 40 import org.netbeans.modules.j2ee.earproject.EarProject; 41 import org.netbeans.modules.j2ee.earproject.EarProjectTest; 42 import org.netbeans.modules.j2ee.earproject.ModuleType; 43 import org.netbeans.modules.j2ee.earproject.test.TestUtil; 44 import org.netbeans.modules.java.platform.JavaPlatformProvider; 45 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 46 import org.netbeans.spi.project.support.ant.AntProjectHelper; 47 import org.netbeans.spi.project.support.ant.EditableProperties; 48 import org.openide.DialogDescriptor; 49 import org.openide.DialogDisplayer; 50 import org.openide.NotifyDescriptor; 51 import org.openide.filesystems.FileObject; 52 import org.openide.filesystems.FileUtil; 53 import org.openide.modules.SpecificationVersion; 54 55 58 public class ImportBlueprintEarWizardIteratorTest extends NbTestCase { 59 60 private static final String CUSTOM_CONTEXT_ROOT = "/my-context-root"; 61 62 63 private String name = "Test EnterpriseApplication"; 64 private String j2eeLevel = "1.5"; 65 private String warName = "testEA-war"; 66 private String jarName = "testEA-ejb"; 67 private String carName = "testEA-app-client"; 68 private String mainClass = "testEA.app.client.Main"; 69 private String platformName = null; 70 private String sourceLevel = "1.5"; 71 72 private String serverInstanceID; 73 private File prjDirF; 74 75 public ImportBlueprintEarWizardIteratorTest(String testName) { 76 super(testName); 77 } 78 79 protected void setUp() throws Exception { 80 clearWorkDir(); 81 serverInstanceID = TestUtil.registerSunAppServer( 82 this, new Object [] { new SilentDialogDisplayer(), new SimplePlatformProvider() }); 83 assertTrue("wrong dialog displayer", DialogDisplayer.getDefault() instanceof SilentDialogDisplayer); 84 prjDirF = new File (getWorkDir(), "testEA"); 86 } 87 88 public void testTestableInstantiateBasics() throws Exception { 89 generateJ2EEApplication(false); 90 File importedDir = new File (getWorkDir(), "testEA-imported"); 91 ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel, 92 j2eeLevel, importedDir, prjDirF, serverInstanceID, name, 93 Collections.<FileObject, ModuleType>emptyMap(), null); 94 95 FileObject fo = FileUtil.toFileObject(importedDir); 96 EarProject project = (EarProject) ProjectManager.getDefault().findProject(fo); 97 EditableProperties props = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 98 assertEquals("j2ee.platform was set to 1.5", "1.5", props.getProperty("j2ee.platform")); } 100 101 public void testTestableInstantiateWitoutDD() throws Exception { 102 FileObject prjDirFO = generateJ2EEApplication(true); 103 104 prjDirFO.getFileObject("src/conf/application.xml").delete(); 106 107 Map <FileObject, ModuleType> userModules = new HashMap <FileObject, ModuleType>(); 108 userModules.put(prjDirFO.getFileObject(warName), ModuleType.WEB); 109 userModules.put(prjDirFO.getFileObject(jarName), ModuleType.EJB); 110 userModules.put(prjDirFO.getFileObject(carName), ModuleType.CLIENT); 111 File importedDir = new File (getWorkDir(), "testEA-imported"); 112 ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel, 113 j2eeLevel, importedDir, prjDirF, serverInstanceID, name, userModules, null); 114 115 FileObject importedDirFO = FileUtil.toFileObject(importedDir); 116 FileObject ddFO = prjDirFO.getFileObject("src/conf/application.xml"); 117 assertNotNull("deployment descriptor was created", ddFO); 118 EarProjectTest.validate(ddFO); 119 EarProject project = (EarProject) ProjectManager.getDefault().findProject(importedDirFO); 120 EarProjectTest.openProject(project); 121 Application app = DDProvider.getDefault().getDDRoot(ddFO); 122 assertSame("three modules", 3, app.getModule().length); 123 } 124 125 public void testTestableInstantiateWithWebAndEJBAndAC() throws Exception { 126 this.j2eeLevel = "1.4"; 127 FileObject prjDirFO = generateJ2EEApplication(true); 128 129 File importedDir = new File (getWorkDir(), "testEA-imported"); 130 ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel, 131 j2eeLevel, importedDir, prjDirF, serverInstanceID, name, 132 Collections.<FileObject, ModuleType>emptyMap(), null); 133 134 assertNotNull("have a backup copy of application.xml", prjDirFO.getFileObject("src/conf/original_application.xml")); 135 assertNotNull("have a backup copy of manifest", prjDirFO.getFileObject("src/conf/original_MANIFEST.MF")); 136 FileObject importedDirFO = FileUtil.toFileObject(importedDir); 137 EarProject project = (EarProject) ProjectManager.getDefault().findProject(importedDirFO); 138 EarProjectTest.openProject(project); 139 140 FileObject ddFO = project.getAppModule().getDeploymentDescriptor(); 141 Application app = DDProvider.getDefault().getDDRoot(ddFO); 142 EarProjectTest.validate(ddFO); 143 assertSame("three modules", 3, app.getModule().length); 144 NewEarProjectWizardIteratorTest.doTestThatEJBWasAddedToWebAndAC( importedDirFO.getFileObject("testEA-war"), 146 importedDirFO.getFileObject("testEA-app-client")); 147 } 148 149 public void off_testWebContextRootIsSet() throws Exception { 151 this.j2eeLevel = "1.4"; 152 generateJ2EEApplicationWithWeb(); 153 154 File importedDir = new File (getWorkDir(), "testEA-imported"); 155 ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel, 156 j2eeLevel, importedDir, prjDirF, serverInstanceID, name, 157 Collections.<FileObject, ModuleType>emptyMap(), null); 158 159 String importedContextRoot = null; 160 FileObject ddFO = FileUtil.toFileObject(prjDirF).getFileObject("src/conf/application.xml"); 161 assertNotNull(ddFO); 162 EarProjectTest.validate(ddFO); 163 Application app = DDProvider.getDefault().getDDRoot(ddFO); 164 assertNotNull(app); 165 for (Module module : app.getModule()) { 166 Web web = module.getWeb(); 167 if (web != null) { 168 importedContextRoot = web.getContextRoot(); 169 break; 170 } 171 } 172 173 assertNotNull("context-root set", importedContextRoot); 174 assertEquals("context-root successfully imported", CUSTOM_CONTEXT_ROOT, importedContextRoot); 175 } 176 177 private FileObject generateJ2EEApplication() throws Exception { 178 NewEarProjectWizardIteratorTest.generateEARProject( 180 prjDirF, name, j2eeLevel, serverInstanceID, 181 warName, jarName, carName, mainClass, platformName, sourceLevel); 182 183 FileObject ddFO = FileUtil.toFileObject(prjDirF).getFileObject("src/conf/application.xml"); 187 Application app = DDProvider.getDefault().getDDRoot(ddFO); 188 for (Module module : app.getModule()) { 189 Web web = module.getWeb(); 190 if (web != null) { 191 web.setContextRoot("/my-context-root"); 192 app.write(ddFO); 193 break; 194 } 195 } 196 197 FileObject prjDirFO = FileUtil.toFileObject(prjDirF); 199 prjDirFO.getFileObject("nbproject").delete(); 200 if (warName != null) { 201 prjDirFO.getFileObject("testEA-war/nbproject").delete(); 202 } 203 if (jarName != null) { 204 prjDirFO.getFileObject("testEA-ejb/nbproject").delete(); 205 } 206 if (carName != null) { 207 prjDirFO.getFileObject("testEA-app-client/nbproject").delete(); 208 } 209 return prjDirFO; 210 } 211 212 private FileObject generateJ2EEApplication(boolean withSubModules) throws Exception { 213 if (!withSubModules) { 214 this.warName = null; 215 this.jarName = null; 216 this.carName = null; 217 this.mainClass = null; 218 } 219 return generateJ2EEApplication(); 220 } 221 222 private FileObject generateJ2EEApplicationWithWeb() throws Exception { 223 this.jarName = null; 224 this.carName = null; 225 this.mainClass = null; 226 return generateJ2EEApplication(); 227 } 228 229 private static final class SilentDialogDisplayer extends DialogDisplayer { 232 233 public Object notify(NotifyDescriptor descriptor) { 234 return null; 235 } 236 237 public Dialog createDialog(DialogDescriptor descriptor) { 238 return null; 239 } 240 241 } 242 243 private static class SimplePlatformProvider implements JavaPlatformProvider { 244 245 public void removePropertyChangeListener(PropertyChangeListener listener) { 246 } 247 248 public void addPropertyChangeListener(PropertyChangeListener listener) { 249 } 250 251 public JavaPlatform[] getInstalledPlatforms() { 252 return new JavaPlatform[] { 253 getDefaultPlatform() 254 }; 255 } 256 257 public JavaPlatform getDefaultPlatform() { 258 return new TestDefaultPlatform(); 259 } 260 261 } 262 263 private static class TestDefaultPlatform extends JavaPlatform { 264 265 public FileObject findTool(String toolName) { 266 return null; 267 } 268 269 public String getDisplayName() { 270 return "Default Platform"; 271 } 272 273 public ClassPath getBootstrapLibraries() { 274 return ClassPathSupport.createClassPath(new URL [0]); 275 } 276 277 public Collection getInstallFolders() { 278 return null; 279 } 280 281 public ClassPath getStandardLibraries() { 282 return null; 283 } 284 285 public String getVendor() { 286 return null; 287 } 288 289 public Specification getSpecification() { 290 return new Specification("j2se", new SpecificationVersion("1.5")); 291 } 292 293 public ClassPath getSourceFolders() { 294 return null; 295 } 296 297 public List getJavadocFolders() { 298 return null; 299 } 300 301 public Map getProperties() { 302 return Collections.singletonMap("platform.ant.name","default_platform"); 303 } 304 305 } 306 307 } 308 | Popular Tags |