Author: andygumbrecht
Date: Thu Aug 28 16:24:51 2014
New Revision: 1621158

URL: http://svn.apache.org/r1621158
Log:
TOMEE-1338 - DeployerEjb tests
Fix DeployerEjb property override.
Fix potential NP if OpenEjb.destroy is called be user.

Modified:
    
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
    
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
    
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/assembler/DeployerEjbTest.java

Modified: 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java?rev=1621158&r1=1621157&r2=1621158&view=diff
==============================================================================
--- 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
 (original)
+++ 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
 Thu Aug 28 16:24:51 2014
@@ -173,7 +173,7 @@ public class DeployerEjb implements Depl
             AUTO_DEPLOY.set(autoDeploy);
             try {
                 final AppInfo appInfo = 
SystemInstance.get().getComponent(WebAppDeployer.class)
-                        .deploy(host, contextRoot(properties, 
file.getAbsolutePath()), file);
+                    .deploy(host, contextRoot(properties, 
file.getAbsolutePath()), file);
                 saveIfNeeded(properties, file, appInfo);
                 return appInfo;
             } finally {
@@ -273,7 +273,8 @@ public class DeployerEjb implements Depl
     }
 
     private void saveIfNeeded(final Properties properties, final File file, 
final AppInfo appInfo) {
-        if (SAVE_DEPLOYMENTS || 
"true".equalsIgnoreCase(properties.getProperty(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS,
 "false"))) {
+        if ((SAVE_DEPLOYMENTS && null == 
properties.getProperty(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS))
+            || 
"true".equalsIgnoreCase(properties.getProperty(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS,
 "false"))) {
             appInfo.properties.setProperty("save-deployment", "true");
             saveDeployment(file, true);
         }
@@ -402,15 +403,21 @@ public class DeployerEjb implements Depl
     }
 
     private void reload(final AppInfo info) {
+
         if (info.webAppAlone) {
-            
SystemInstance.get().getComponent(WebAppDeployer.class).reload(info.path);
-        } else {
-            try {
-                assembler.destroyApplication(info);
-                assembler.createApplication(info);
-            } catch (final Exception e) {
-                throw new OpenEJBRuntimeException(e);
+            final WebAppDeployer component = 
SystemInstance.get().getComponent(WebAppDeployer.class);
+
+            if (null != component) {
+                component.reload(info.path);
+                return;
             }
         }
+
+        try {
+            assembler.destroyApplication(info);
+            assembler.createApplication(info);
+        } catch (final Exception e) {
+            throw new OpenEJBRuntimeException(e);
+        }
     }
 }

Modified: 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java?rev=1621158&r1=1621157&r2=1621158&view=diff
==============================================================================
--- 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
 (original)
+++ 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
 Thu Aug 28 16:24:51 2014
@@ -132,7 +132,7 @@ public final class ApplicationComposers 
     private ThreadContext previous;
     private MockHttpSession session;
     private MockServletContext servletContext;
-    private Collection<String> globalJndiEntries = new ArrayList<String>();
+    private final Collection<String> globalJndiEntries = new 
ArrayList<String>();
 
     public ApplicationComposers(final Class<?> klass, final Object... 
additionalModules) {
         testClass = klass;
@@ -814,10 +814,16 @@ public final class ApplicationComposers 
                 // no-op
             }
 
-            final Context context = 
SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
-            for (final String entry : globalJndiEntries) {
-                context.unbind(entry);
+            final ContainerSystem component = 
SystemInstance.get().getComponent(ContainerSystem.class);
+
+            if (null != component) {
+                final Context context = component.getJNDIContext();
+
+                for (final String entry : globalJndiEntries) {
+                    context.unbind(entry);
+                }
             }
+
             globalJndiEntries.clear();
 
             try {

Modified: 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/assembler/DeployerEjbTest.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/assembler/DeployerEjbTest.java?rev=1621158&r1=1621157&r2=1621158&view=diff
==============================================================================
--- 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/assembler/DeployerEjbTest.java
 (original)
+++ 
tomee/tomee/branches/tomee-1.7.x/container/openejb-core/src/test/java/org/apache/openejb/assembler/DeployerEjbTest.java
 Thu Aug 28 16:24:51 2014
@@ -1,5 +1,8 @@
 package org.apache.openejb.assembler;
 
+import org.apache.openejb.OpenEJB;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.assembler.classic.AppInfo;
 import org.apache.openejb.jee.WebApp;
 import org.apache.openejb.junit.ApplicationComposer;
 import org.apache.openejb.loader.FileUtils;
@@ -20,6 +23,10 @@ import org.junit.runner.RunWith;
 import javax.naming.Context;
 import javax.naming.NamingException;
 import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Properties;
 import java.util.concurrent.atomic.AtomicReference;
 
 @RunWith(ApplicationComposer.class)
@@ -76,12 +83,17 @@ public class DeployerEjbTest {
 
     @Before
     public void before() throws Exception {
+        final File deployments = new 
File(SystemInstance.get().getBase().getDirectory("conf", false), 
"deployments.xml");
+        if (deployments.exists()) {
+            Files.delete(deployments);
+        }
         System.setProperty(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS, 
Boolean.TRUE.toString());
     }
 
     @After
     public void after() throws Exception {
         System.setProperty(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS, 
Boolean.FALSE.toString());
+        OpenEJB.destroy();
     }
 
     private Deployer getDeployer() throws NamingException {
@@ -97,15 +109,46 @@ public class DeployerEjbTest {
 
     @Test
     public void testGetDeployedApps() throws Exception {
+        getAppInfos();
+    }
+
+    private Collection<AppInfo> getAppInfos() throws Exception {
+        final Deployer deployer = getDeployer();
+        Collection<AppInfo> deployedApps = deployer.getDeployedApps();
+
+        if (null == deployedApps) {
+            deployedApps = new ArrayList<AppInfo>();
+        }
+
+        if (deployedApps.size() < 1) {
+            getAppInfo();
+            deployedApps.addAll(deployer.getDeployedApps());
+        }
 
+        Assert.assertTrue("Found no deployed apps", deployedApps.size() > 0);
+        return deployedApps;
     }
 
     @Test
-    public void testDeploy() throws Exception {
+    public void testDeployWarSave() throws Exception {
+
+        final Collection<AppInfo> deployedApps = 
getDeployer().getDeployedApps();
+        Assert.assertTrue("Found more than one app", deployedApps.size() < 2);
 
         final File deployments = new 
File(SystemInstance.get().getBase().getDirectory("conf", false), 
"deployments.xml");
         Assert.assertFalse("Found existing: " + deployments.getAbsolutePath(), 
deployments.exists());
 
+        getAppInfo();
+
+        Assert.assertTrue("Failed to find: " + deployments.getAbsolutePath(), 
deployments.exists());
+    }
+
+    private AppInfo getAppInfo() throws IOException, NamingException, 
OpenEJBException {
+        return getAppInfo(null);
+    }
+
+    private AppInfo getAppInfo(final Properties p) throws IOException, 
NamingException, OpenEJBException {
+
         final Deployer deployer = getDeployer();
 
         final File war = warArchive.get();
@@ -113,28 +156,92 @@ public class DeployerEjbTest {
             Assert.fail("War file does not exist: " + war.getAbsolutePath());
         }
 
-        deployer.deploy(war.getAbsolutePath());
-        Assert.assertTrue("Failed to find: " + deployments.getAbsolutePath(), 
deployments.exists());
+        return (null != p ? deployer.deploy(war.getAbsolutePath(), p) : 
deployer.deploy(war.getAbsolutePath()));
     }
 
     @Test
-    public void testDeploy1() throws Exception {
+    public void testDeployWarNoSave() throws Exception {
+        final Collection<AppInfo> deployedApps = 
getDeployer().getDeployedApps();
+        Assert.assertTrue("Found more than one app", deployedApps.size() < 2);
+
+        final File deployments = new 
File(SystemInstance.get().getBase().getDirectory("conf", false), 
"deployments.xml");
+        if (deployments.exists()) {
+            Files.delete(deployments);
+        }
+
+        Assert.assertFalse("Found existing: " + deployments.getAbsolutePath(), 
deployments.exists());
 
+        final Properties p = new Properties();
+        p.setProperty(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS, 
Boolean.FALSE.toString());
+        getAppInfo(p);
+
+        Assert.assertFalse("Found existing: " + deployments.getAbsolutePath(), 
deployments.exists());
     }
 
     @Test
-    public void testDeploy2() throws Exception {
+    public void testDeployProperties() throws Exception {
+        final Properties p = new Properties();
+        final String path = warArchive.get().getAbsolutePath();
+
+        p.setProperty(Deployer.FILENAME, path);
+        p.setProperty(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS, 
Boolean.FALSE.toString());
 
+        final Deployer deployer = getDeployer();
+        final AppInfo appInfo = deployer.deploy(p);
+        Assert.assertTrue("Paths do not match: " + path + " - " + 
appInfo.path, path.equals(appInfo.path));
     }
 
     @Test
     public void testUndeploy() throws Exception {
+        final AppInfo appInfo = getDeployedApp();
+
+        Assert.assertNotNull("Failed to deploy app", appInfo);
+
+        final Deployer deployer = getDeployer();
+        deployer.undeploy(appInfo.path);
 
+        final Collection<AppInfo> appInfos = getAppInfos();
+        Assert.assertTrue("Failed to undeploy app", appInfos.size() < 2);
+    }
+
+    private AppInfo getDeployedApp() throws Exception {
+        final Collection<AppInfo> appInfos = getAppInfos();
+
+        AppInfo appInfo = null;
+        final File file = warArchive.get();
+
+        if (appInfos.size() < 2) {
+            appInfo = getAppInfo();
+        } else {
+
+            final String name = file.getName().toLowerCase();
+
+            for (final AppInfo info : appInfos) {
+                if (name.contains(info.appId.toLowerCase())) {
+                    appInfo = info;
+                }
+            }
+        }
+        return appInfo;
     }
 
     @Test
     public void testReload() throws Exception {
 
+        final AppInfo appInfo = getDeployedApp();
+
+        final Deployer deployer = getDeployer();
+        deployer.reload(appInfo.path);
+
+        final Collection<AppInfo> deployedApps = deployer.getDeployedApps();
+        boolean found = false;
+        for (final AppInfo app : deployedApps) {
+            if (app.path.equals(appInfo.path)) {
+                found = true;
+            }
+        }
+
+        Assert.assertTrue("Failed to find app after redeploy", found);
     }
 
     public static class TestClass {


Reply via email to