Author: julien_viet
Date: 2009-12-15 12:28:17 -0500 (Tue, 15 Dec 2009)
New Revision: 1073
Removed:
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/RootContainerBootstrap.java
Modified:
kernel/branches/config-branch/exo.kernel.container/src/main/java/org/exoplatform/container/RootContainer.java
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/TestPropertyManagerConfigurator.java
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/configuration/AbstractProfileTest.java
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestContainer.java
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/support/ContainerBuilder.java
Log:
simplify and use the new container builder
Modified:
kernel/branches/config-branch/exo.kernel.container/src/main/java/org/exoplatform/container/RootContainer.java
===================================================================
---
kernel/branches/config-branch/exo.kernel.container/src/main/java/org/exoplatform/container/RootContainer.java 2009-12-15
17:10:28 UTC (rev 1072)
+++
kernel/branches/config-branch/exo.kernel.container/src/main/java/org/exoplatform/container/RootContainer.java 2009-12-15
17:28:17 UTC (rev 1073)
@@ -449,20 +449,27 @@
else
{
booting = true;
- log.info("Building root container");
- long time = -System.currentTimeMillis();
- result = buildRootContainer();
- if (result != null)
+ try
{
- time += System.currentTimeMillis();
- log.info("Root container is built (build time " + time +
"ms)");
- ExoContainerContext.setTopContainer(result);
- singleton_ = result;
- log.info("Root container booted");
+ log.info("Building root container");
+ long time = -System.currentTimeMillis();
+ result = buildRootContainer();
+ if (result != null)
+ {
+ time += System.currentTimeMillis();
+ log.info("Root container is built (build time " + time
+ "ms)");
+ ExoContainerContext.setTopContainer(result);
+ singleton_ = result;
+ log.info("Root container booted");
+ }
+ else
+ {
+ log.error("Failed to boot root container");
+ }
}
- else
+ finally
{
- log.error("Failed to boot root container");
+ booting = false;
}
}
}
Deleted:
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/RootContainerBootstrap.java
===================================================================
---
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/RootContainerBootstrap.java 2009-12-15
17:10:28 UTC (rev 1072)
+++
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/RootContainerBootstrap.java 2009-12-15
17:28:17 UTC (rev 1073)
@@ -1,71 +0,0 @@
-/*
- * Copyright (C) 2003-2007 eXo Platform SAS.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Affero General Public License
- * as published by the Free Software Foundation; either version 3
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not,
see<http://www.gnu.org/licenses/>.
- */
-package org.exoplatform.container;
-
-import junit.framework.AssertionFailedError;
-import org.exoplatform.container.configuration.ConfigurationManager;
-import org.exoplatform.container.configuration.ConfigurationManagerImpl;
-
-import java.lang.reflect.Field;
-import java.net.URL;
-import java.util.Arrays;
-import java.util.HashSet;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class RootContainerBootstrap
-{
-
- public static RootContainer bootstrap(URL configurationURL, String... profiles)
- {
- // Must clear the top container first otherwise it's not going to work well
- // it's a big ugly but I don't want to change anything in the
ExoContainerContext class for now
- // and this is for unit testing
- try
- {
- Field topContainerField =
ExoContainerContext.class.getDeclaredField("topContainer");
- topContainerField.setAccessible(true);
- topContainerField.set(null, null);
- }
- catch (Exception e)
- {
- AssertionFailedError err = new AssertionFailedError();
- err.initCause(e);
- throw err;
- }
-
- // Create a root container manually
- try
- {
- RootContainer container = new RootContainer();
- ConfigurationManager manager = new ConfigurationManagerImpl(new
HashSet<String>(Arrays.asList(profiles)));
- manager.addConfiguration(configurationURL);
- container.registerComponentInstance(ConfigurationManager.class, manager);
- container.initContainer();
- container.start();
- return container;
- }
- catch (Exception e)
- {
- AssertionError err = new AssertionError("Could not start root
container");
- err.initCause(e);
- throw err;
- }
- }
-}
Modified:
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/TestPropertyManagerConfigurator.java
===================================================================
---
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/TestPropertyManagerConfigurator.java 2009-12-15
17:10:28 UTC (rev 1072)
+++
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/TestPropertyManagerConfigurator.java 2009-12-15
17:28:17 UTC (rev 1073)
@@ -20,6 +20,7 @@
import org.exoplatform.commons.utils.PropertyManager;
import org.exoplatform.commons.utils.Tools;
import org.exoplatform.container.configuration.ConfigurationManagerImpl;
+import org.exoplatform.container.support.ContainerBuilder;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.PropertiesParam;
import org.exoplatform.container.xml.ValueParam;
@@ -78,7 +79,7 @@
System.setProperty("property_2", "property_value_2");
URL url =
TestPropertyManagerConfigurator.class.getResource("property-configurator-configuration.xml");
assertNotNull(url);
- RootContainerBootstrap.bootstrap(url);
+ ContainerBuilder.bootstrap(url);
Map<String, String> additions = reset();
assertEquals("property_value_1", additions.get("property_1"));
assertEquals("property_value_2", additions.get("property_2"));
@@ -90,7 +91,7 @@
reset();
URL url =
TestPropertyManagerConfigurator.class.getResource("property-configurator-with-profile-configuration.xml");
assertNotNull(url);
- RootContainerBootstrap.bootstrap(url, "foo");
+ ContainerBuilder.bootstrap(url, "foo");
Map<String, String> additions = reset();
assertEquals("property_value_1_foo",
additions.get("property_1"));
}
@@ -103,7 +104,7 @@
System.setProperty("properties.url", propertiesURL.toString());
System.setProperty("property_2", "property_value_2");
URL url =
TestPropertyManagerConfigurator.class.getResource("property-configurator-with-path-configuration.xml");
- RootContainerBootstrap.bootstrap(url);
+ ContainerBuilder.bootstrap(url);
Map<String, String> additions = reset();
assertEquals("property_value_1", additions.get("property_1"));
assertEquals("property_value_2", additions.get("property_2"));
Modified:
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/configuration/AbstractProfileTest.java
===================================================================
---
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/configuration/AbstractProfileTest.java 2009-12-15
17:10:28 UTC (rev 1072)
+++
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/configuration/AbstractProfileTest.java 2009-12-15
17:28:17 UTC (rev 1073)
@@ -19,7 +19,7 @@
import junit.framework.TestCase;
import org.exoplatform.commons.utils.Tools;
import org.exoplatform.container.RootContainer;
-import org.exoplatform.container.RootContainerBootstrap;
+import org.exoplatform.container.support.ContainerBuilder;
import org.exoplatform.container.xml.Configuration;
import java.io.File;
@@ -44,6 +44,6 @@
{
String basedir = System.getProperty("basedir");
File f = new File(basedir +
"/src/test/resources/org/exoplatform/container/configuration/" + configName);
- return RootContainerBootstrap.bootstrap(f.toURI().toURL(), profiles);
+ return ContainerBuilder.bootstrap(f.toURI().toURL(), profiles);
}
}
Modified:
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestContainer.java
===================================================================
---
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestContainer.java 2009-12-15
17:10:28 UTC (rev 1072)
+++
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestContainer.java 2009-12-15
17:28:17 UTC (rev 1073)
@@ -20,15 +20,10 @@
import junit.framework.TestCase;
-import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.RootContainer;
-import org.exoplatform.container.RootContainerBootstrap;
+import org.exoplatform.container.support.ContainerBuilder;
-import java.io.IOException;
import java.net.URL;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Enumeration;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
@@ -36,49 +31,10 @@
*/
public class AbstractTestContainer extends TestCase
{
-
public RootContainer createRootContainer(String relativeConfigurationFile)
{
URL url = getClass().getResource(relativeConfigurationFile);
assertNotNull(url);
- return RootContainerBootstrap.bootstrap(url);
+ return new ContainerBuilder().withRoot(url).build();
}
-
- public PortalContainer createPortalContainer(RootContainer container, String name,
String relativeConfigurationFile)
- {
- final URL url = getClass().getResource(relativeConfigurationFile);
- ClassLoader cl = new ClassLoader(Thread.currentThread().getContextClassLoader())
- {
- @Override
- public Enumeration<URL> getResources(String name) throws IOException
- {
- if ("conf/portal/configuration.xml".equals(name))
- {
- return Collections.enumeration(Arrays.asList(url));
- }
- else if ("conf/portal/test-configuration.xml".equals(name))
- {
- return Collections.enumeration(Collections.<URL>emptyList());
- }
- else
- {
- return super.getResources(name);
- }
- }
- };
-
- //
- Thread.currentThread().setContextClassLoader(cl);
-
- //
- try
- {
- return container.getPortalContainer(name);
- }
- finally
- {
- Thread.currentThread().setContextClassLoader(null);
- }
- }
-
}
Modified:
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/support/ContainerBuilder.java
===================================================================
---
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/support/ContainerBuilder.java 2009-12-15
17:10:28 UTC (rev 1072)
+++
kernel/branches/config-branch/exo.kernel.container/src/test/java/org/exoplatform/container/support/ContainerBuilder.java 2009-12-15
17:28:17 UTC (rev 1073)
@@ -17,6 +17,7 @@
package org.exoplatform.container.support;
import junit.framework.AssertionFailedError;
+import org.exoplatform.commons.utils.PropertyManager;
import org.exoplatform.commons.utils.Tools;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.container.RootContainer;
@@ -27,11 +28,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
+import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
- * An helper for building a root container and a portal container.
+ * An helper for building a root container and a portal container. I have done several
attempt to make easily
+ * and safe root/portal container boot for unit test. This one is my best attempt so
far.
*
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
@@ -39,12 +42,16 @@
public class ContainerBuilder
{
+ /** . */
private ClassLoader loader;
+ /** . */
private List<URL> configURLs;
+ /** . */
private List<URL> portalConfigURLs;
+ /** . */
private Set<String> profiles;
public ContainerBuilder()
@@ -120,6 +127,12 @@
private RootContainer _build() throws Exception
{
+ //
+ if (configURLs.size() == 0)
+ {
+ throw new IllegalStateException("Must provide at least one URL for building
the root container");
+ }
+
// Must clear the top container first otherwise it's not going to work well
// it's a big ugly but I don't want to change anything in the
ExoContainerContext class for now
// and this is for unit testing
@@ -127,6 +140,30 @@
topContainerField.setAccessible(true);
topContainerField.set(null, null);
+ // Same remark than above
+ Field singletonField =
RootContainer.class.getDeclaredField("singleton_");
+ singletonField.setAccessible(true);
+ singletonField.set(null, null);
+
+ // Setup profiles
+ if (profiles == null)
+ {
+ PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, "");
+ }
+ else
+ {
+ StringBuilder builder = new StringBuilder();
+ for (Iterator<String> i = profiles.iterator();i.hasNext();)
+ {
+ builder.append(i.next());
+ if (i.hasNext())
+ {
+ builder.append(',');
+ }
+ }
+ PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES,
builder.toString());
+ }
+
//
ClassLoader rootCL = new ClassLoader(loader)
{
@@ -160,8 +197,15 @@
try
{
Thread.currentThread().setContextClassLoader(rootCL);
+
+ //
root = RootContainer.getInstance();
- root.getPortalContainer("portal");
+
+ //
+ if (portalConfigURLs.size() > 0)
+ {
+ root.getPortalContainer("portal");
+ }
}
finally
{
@@ -171,4 +215,12 @@
//
return root;
}
+
+ public static RootContainer bootstrap(URL configurationURL, String... profiles)
+ {
+ ContainerBuilder builder = new ContainerBuilder();
+ builder.withRoot(configurationURL);
+ builder.profiledBy(profiles);
+ return builder.build();
+ }
}