Author: bdelacretaz
Date: Fri Jan 13 23:54:42 2012
New Revision: 1231404
URL: http://svn.apache.org/viewvc?rev=1231404&view=rev
Log:
SLING-2368 - ServerSetup utility, work in progress
Added:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetup.java
(with props)
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetupSingleton.java
(with props)
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/SetupPhase.java
(with props)
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/StartRunnableJarPhase.java
(with props)
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/ServerSetupSingletonTest.java
(with props)
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestServerSetup.java
(with props)
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestSetupPhase.java
(with props)
Modified:
sling/trunk/testing/tools/pom.xml
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/jarexec/JarExecutor.java
Modified: sling/trunk/testing/tools/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/pom.xml?rev=1231404&r1=1231403&r2=1231404&view=diff
==============================================================================
--- sling/trunk/testing/tools/pom.xml (original)
+++ sling/trunk/testing/tools/pom.xml Fri Jan 13 23:54:42 2012
@@ -99,6 +99,12 @@
<version>1.5.11</version>
</dependency>
<dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <version>1.5.11</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
Modified:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/jarexec/JarExecutor.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/jarexec/JarExecutor.java?rev=1231404&r1=1231403&r2=1231404&view=diff
==============================================================================
---
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/jarexec/JarExecutor.java
(original)
+++
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/jarexec/JarExecutor.java
Fri Jan 13 23:54:42 2012
@@ -27,6 +27,7 @@ import org.apache.commons.exec.DefaultEx
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteResultHandler;
import org.apache.commons.exec.Executor;
+import org.apache.commons.exec.ProcessDestroyer;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;
import org.slf4j.Logger;
@@ -161,7 +162,12 @@ public class JarExecutor {
log.info("Executing " + cl);
e.setStreamHandler(new PumpStreamHandler());
- e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
+ e.setProcessDestroyer(getProcessDestroyer());
e.execute(cl, h);
}
+
+ /** Can be overridden to return a custom ProcessDestroyer */
+ protected ProcessDestroyer getProcessDestroyer() {
+ return new ShutdownHookProcessDestroyer();
+ }
}
Added:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetup.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetup.java?rev=1231404&view=auto
==============================================================================
---
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetup.java
(added)
+++
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetup.java
Fri Jan 13 23:54:42 2012
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package org.apache.sling.testing.tools.serversetup;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import junit.framework.AssertionFailedError;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/** This is an evolution of the SlingTestBase/JarExecutor
+ * combination that we had at revision 1201491, used
+ * to control the server side of integration tests.
+ *
+ * This class allows a number of startup and shutdown phases
+ * to be defined, and executes some or all of them in a specified
+ * order, according to a property which lists their names.
+ *
+ * Flexibility in those startup/shutdown phases allows for
+ * creating test scenarios like automated testing of
+ * system upgrades, where you would for example:
+ *
+ * <pre>
+ * 1. Start the old runnable jar
+ * 2. Wait for it to be ready
+ * 3. Install some bundles and wait for them to be ready
+ * 4. Create some content in that version
+ * 5. Stop that jar
+ * 6. Start the new runnable jar
+ * 7. Wait for it to be ready
+ * 8. Run tests against that new jar to verify the upgrade
+ * </pre>
+ *
+ * Running the whole thing might take a long time, so when
+ * debugging the upgrade or the tests you might want to
+ * restart from a state saved at step 5, and only run steps
+ * 6 to 8, for example.
+ *
+ * Those steps are SetupPhase objects identified by
+ * their name, and specifying a partial list of names allows you
+ * to run only some of them in a given test run, speeding up
+ * development and troubleshooting as much as possible.
+ *
+ * See the companion samples/integration-tests module for an
+ * example that uses this class to setup the Sling server
+ * that it tests.
+ */
+public class ServerSetup {
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ /** Context that our SetupPhase objects can use to exchange data */
+ private final Map<String, Object> context = new HashMap<String, Object>();
+
+ private final List<String> phasesToRun = new ArrayList<String>();
+
+ /** Our configuration */
+ private Properties config;
+
+ /** Prefix used for our property names */
+ public static final String PROP_NAME_PREFIX = "server.setup";
+
+ /** Config property name: comma-separated list of phases to run */
+ public static final String PHASES_TO_RUN_PROP = PROP_NAME_PREFIX +
".phases.to.run";
+
+ /** Standard suffix for shutdown tasks IDs */
+ public static final String SHUTDOWN_ID_SUFFIX = ".shutdown";
+
+ /** Our SetupPhases, keyed by their id which must be unique */
+ private final Map<String, SetupPhase> phases = new HashMap<String,
SetupPhase>();
+
+ /** List of phases that already ran */
+ private final Set<String> donePhases = new HashSet<String>();
+
+ /** List of phases that failed */
+ private final Set<String> failedPhases = new HashSet<String>();
+
+ /** Shutdown hook thread */
+ private Thread shutdownHook;
+
+ @SuppressWarnings("serial")
+ public static class SetupException extends Exception {
+ public SetupException(String reason) {
+ super(reason);
+ }
+
+ public SetupException(String reason, Throwable cause) {
+ super(reason, cause);
+ }
+ };
+
+ /** Runs all startup phases that have not run yet,
+ * and throws an Exception or call Junit's fail()
+ * method if one of them fails or failed in a
+ * previous call of this method.
+ *
+ * This can be called several times, will only run
+ * setup phases that have not run yet.
+ */
+ public synchronized void setupTestServer() throws Exception {
+ // If any phases failed, we're doomed
+ if(!failedPhases.isEmpty()) {
+ throw new SetupException("Some SetupPhases previously failed: " +
failedPhases);
+ }
+
+ // Run all startup phases that didn't run yet
+ runRemainingPhases(true);
+
+ // And setup our shutdown hook
+ if(shutdownHook == null) {
+ shutdownHook = new Thread(getClass().getSimpleName() + "Shutdown")
{
+ public void run() {
+ try {
+ shutdown();
+ } catch(Exception e) {
+ log.warn("Exception in shutdown hook", e);
+ }
+
+ }
+ };
+ Runtime.getRuntime().addShutdownHook(shutdownHook);
+ log.info("Shutdown hook added to run shutdown phases");
+ }
+ }
+
+ /** Run phases that haven't run yet */
+ private void runRemainingPhases(boolean startup) throws Exception {
+ for(String id : phasesToRun) {
+ final SetupPhase p = phases.get(id);
+ if(donePhases.contains(id)) {
+ continue;
+ }
+ if(p != null && p.isStartupPhase() == startup) {
+ log.info("Executing {}", p);
+ try {
+ p.run(this);
+ } catch(Exception e) {
+ failedPhases.add(id);
+ throw e;
+ } catch(AssertionFailedError ae) {
+ // Some of our tools throw this, might not to avoid it in
the future
+ failedPhases.add(id);
+ throw new Exception("AssertionFailedError in
runRemainingPhases", ae);
+ } finally {
+ donePhases.add(id);
+ }
+ } else {
+ log.info("{} ignored, not in the list of phases to run ({})",
p, phasesToRun);
+ }
+ }
+ }
+
+ /** Called by a shutdown hook to run
+ * all shutdown phases, but can also
+ * be called explicitly, each shutdown
+ * phase only runs once anyway.
+ */
+ public void shutdown() throws Exception {
+ runRemainingPhases(false);
+ }
+
+ public Map<String, Object> getContext() {
+ return context;
+ }
+
+ /** Set configuration and reset our lists of phases
+ * that already ran or failed.
+ */
+ public void setConfig(Properties props) {
+ config = props;
+
+ final String str = props.getProperty(PHASES_TO_RUN_PROP);
+ phasesToRun.clear();
+ final String [] phases = str == null ? new String [] {} :
str.split(",");
+ for(int i=0 ; i < phases.length; i++) {
+ phases[i] = phases[i].trim();
+ }
+ phasesToRun.addAll(Arrays.asList(phases));
+
+ donePhases.clear();
+ failedPhases.clear();
+ }
+
+ public Properties getConfig() {
+ return config;
+ }
+
+ /** Return the IDs of phases that should run */
+ public List<String> getPhasesToRun() {
+ return Collections.unmodifiableList(phasesToRun);
+ }
+
+ /** Add a SetupPhase to our list. Its ID must be
+ * unique in that list.
+ */
+ public void addSetupPhase(SetupPhase p) {
+ if(phases.containsKey(p.getId())) {
+ throw new IllegalArgumentException("A SetupPhase with ID=" +
p.getId() + " is already in our list:" + phases.keySet());
+ }
+ phases.put(p.getId(), p);
+ }
+}
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetup.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetup.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetupSingleton.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetupSingleton.java?rev=1231404&view=auto
==============================================================================
---
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetupSingleton.java
(added)
+++
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetupSingleton.java
Fri Jan 13 23:54:42 2012
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package org.apache.sling.testing.tools.serversetup;
+
+import java.util.Properties;
+
+/** In general we just need a singleton ServerSetup, that
+ * uses System properties for its configuration - this class
+ * supplies that.
+ */
+public class ServerSetupSingleton {
+
+ /** Property name of the ServerSetup class that we instantiate */
+ public static final String CLASS_NAME_PROP = ServerSetup.PROP_NAME_PREFIX
+ ".class.name";
+
+ private static ServerSetup instance;
+
+ /** Create an instance based on the {@CLASS_NAME_PROP)
+ * property if needed and return it.
+ *
+ * @param config Ignored unless an instance is created
+ */
+ public static ServerSetup instance(Properties config) throws
InstantiationException, IllegalAccessException, ClassNotFoundException {
+ if(instance == null) {
+ synchronized (ServerSetupSingleton.class) {
+ if(instance == null) {
+ final String className =
config.getProperty(CLASS_NAME_PROP);
+ if(className == null) {
+ throw new IllegalArgumentException("Missing config
property: " + CLASS_NAME_PROP);
+ }
+ instance = (ServerSetup)
+ ServerSetupSingleton.class.getClassLoader()
+ .loadClass(className)
+ .newInstance();
+ instance.setConfig(config);
+ }
+ }
+ }
+ return instance;
+ }
+
+ /** Same as no-parameter instance() method, but uses System properties
+ * to create its instance.
+ */
+ public static ServerSetup instance() throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
+ return instance(System.getProperties());
+ }
+}
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetupSingleton.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/ServerSetupSingleton.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/SetupPhase.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/SetupPhase.java?rev=1231404&view=auto
==============================================================================
---
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/SetupPhase.java
(added)
+++
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/SetupPhase.java
Fri Jan 13 23:54:42 2012
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package org.apache.sling.testing.tools.serversetup;
+
+/** A single phase of the test server setup */
+public interface SetupPhase {
+ /** Run this phase in the context of supplied ServerSetup */
+ public void run(ServerSetup owner) throws Exception;
+
+ /** Is this a startup or shutdown phase? */
+ public boolean isStartupPhase();
+
+ /** Describe this phase */
+ public String getDescription();
+
+ /** Get the phase ID string, a list of those
+ * is used by {@link ServerSetup} to decide
+ * which phases to run
+ */
+ public String getId();
+}
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/SetupPhase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/SetupPhase.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/StartRunnableJarPhase.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/StartRunnableJarPhase.java?rev=1231404&view=auto
==============================================================================
---
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/StartRunnableJarPhase.java
(added)
+++
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/StartRunnableJarPhase.java
Fri Jan 13 23:54:42 2012
@@ -0,0 +1,77 @@
+package org.apache.sling.testing.tools.serversetup;
+
+import java.util.Properties;
+
+import org.apache.commons.exec.ProcessDestroyer;
+import org.apache.commons.exec.ShutdownHookProcessDestroyer;
+import org.apache.sling.testing.tools.jarexec.JarExecutor;
+import org.apache.sling.testing.tools.jarexec.JarExecutor.ExecutorException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** SetupPhase that uses a JarExecutor to start
+ * a runnable jar, and stop it at system shutdown
+ * if our SetupServer wants that.
+ */
+public class StartRunnableJarPhase implements SetupPhase {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private final String id;
+ private final String description;
+ private final JarExecutor executor;
+
+ public StartRunnableJarPhase(final ServerSetup owner, String id, String
description, Properties config) throws ExecutorException {
+ this.id = id;
+ this.description = description;
+
+ /** Our JarExecutor uses a ProcessDestroyer which does noting
+ * if our ServerSetup owner would not run
+ * a shutdown task with our name + SHUTDOWN_ID_SUFFIX
+ */
+ final String shutdownId = id + ServerSetup.SHUTDOWN_ID_SUFFIX;
+ final ProcessDestroyer destroyer = new ShutdownHookProcessDestroyer() {
+ @Override
+ public void run() {
+ if(owner.getPhasesToRun().contains(shutdownId)) {
+ log.info(
+ "{}: {} allows {} phase to run, shutting down
runnable jar",
+ new Object[] { this, owner, shutdownId } );
+ super.run();
+ } else {
+ log.info(
+ "{}: {} does not {} phase to run, doing nothing",
+ new Object[] { this, owner, shutdownId } );
+ }
+ }
+
+ };
+
+ executor = new JarExecutor(config) {
+ @Override
+ protected ProcessDestroyer getProcessDestroyer() {
+ return destroyer;
+ }
+
+ };
+ }
+
+ /** @inheritDoc */
+ public void run(ServerSetup owner) throws Exception {
+ executor.start();
+ }
+
+ /** @inheritDoc */
+ public boolean isStartupPhase() {
+ return true;
+ }
+
+ /** @inheritDoc */
+ public String getDescription() {
+ return description;
+ }
+
+ /** @inheritDoc */
+ public String getId() {
+ return id;
+ }
+}
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/StartRunnableJarPhase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/serversetup/StartRunnableJarPhase.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/ServerSetupSingletonTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/ServerSetupSingletonTest.java?rev=1231404&view=auto
==============================================================================
---
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/ServerSetupSingletonTest.java
(added)
+++
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/ServerSetupSingletonTest.java
Fri Jan 13 23:54:42 2012
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.testing.tools.test;
+
+import static org.junit.Assert.fail;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Properties;
+
+import org.apache.sling.testing.tools.serversetup.ServerSetup;
+import org.apache.sling.testing.tools.serversetup.ServerSetupSingleton;
+import org.junit.Before;
+import org.junit.Test;
+
+/** Test the ServerSetupSingleton */
+public class ServerSetupSingletonTest {
+ private ServerSetup serverSetup;
+ private Properties props;
+
+ @Before
+ public void setup() throws Exception {
+ props = new Properties();
+ props.setProperty(ServerSetupSingleton.CLASS_NAME_PROP,
TestServerSetup.class.getName());
+ props.setProperty(ServerSetup.PHASES_TO_RUN_PROP, "one, \t\n two,
three, four, five \t");
+ serverSetup = ServerSetupSingleton.instance(props);
+ serverSetup.setConfig(props);
+ TestSetupPhase.clearExecutionLog();
+ TestSetupPhase.failingPhases = "";
+ }
+
+ @Test
+ public void testStartup() throws Exception {
+ serverSetup.setupTestServer();
+ assertEquals("Expecting all startup phases to have run",
+ "one,two,three", TestSetupPhase.executionLog.toString());
+
+ serverSetup.setupTestServer();
+ assertEquals("Expecting second setup call to have no effect",
+ "one,two,three", TestSetupPhase.executionLog.toString());
+ }
+
+ @Test
+ public void testShutdown() throws Exception {
+ serverSetup.shutdown();
+ assertEquals("Expecting all shutdown phases to have run",
+ "four,five", TestSetupPhase.executionLog.toString());
+
+ serverSetup.shutdown();
+ assertEquals("Expecting second shutdown call to be ignored",
+ "four,five", TestSetupPhase.executionLog.toString());
+ }
+
+ @Test
+ public void testStartupAndShutdown() throws Exception {
+ serverSetup.setupTestServer();
+ assertEquals("Expecting all startup phases to have run",
+ "one,two,three", TestSetupPhase.executionLog.toString());
+
+ serverSetup.shutdown();
+ assertEquals("Expecting all phases to have run",
+ "one,two,three,four,five",
TestSetupPhase.executionLog.toString());
+ }
+
+ @Test
+ public void testStartupSomeOnly() throws Exception {
+ props.setProperty(ServerSetup.PHASES_TO_RUN_PROP, "one, three, five");
+ serverSetup.setConfig(props);
+
+ serverSetup.setupTestServer();
+ assertEquals("Expecting only two startup phases to have run",
+ "one,three", TestSetupPhase.executionLog.toString());
+
+ serverSetup.setupTestServer();
+ assertEquals("Expecting second setup call to have no effect",
+ "one,three", TestSetupPhase.executionLog.toString());
+ }
+
+ @Test
+ public void testShutdownSomeOnly() throws Exception {
+ props.setProperty(ServerSetup.PHASES_TO_RUN_PROP, "four");
+ serverSetup.setConfig(props);
+
+ serverSetup.shutdown();
+ assertEquals("Expecting only one shutdown phase to have run",
+ "four", TestSetupPhase.executionLog.toString());
+
+ serverSetup.shutdown();
+ assertEquals("Expecting second setup call to have no effect",
+ "four", TestSetupPhase.executionLog.toString());
+ }
+
+ @Test
+ public void testFailingStartup() {
+ TestSetupPhase.failingPhases = "two, five";
+
+ // setupTestServer will fail every time it's called
+ // after a failure, as that means the server is unusable
+ for(int i=0; i < 3; i++) {
+ try {
+ serverSetup.setupTestServer();
+ fail("startup should have failed");
+ } catch(Exception ignored) {
+ }
+
+ assertEquals("Expecting only one startup phase to have run",
+ "one", TestSetupPhase.executionLog.toString());
+ }
+ }
+
+ @Test
+ public void testFailingShutdown() throws Exception {
+ TestSetupPhase.failingPhases = "two, five";
+
+ try {
+ serverSetup.shutdown();
+ fail("shutdown should have failed");
+ } catch(Exception ignored) {
+ }
+
+ assertEquals("Expecting only one startup phase to have run",
+ "four", TestSetupPhase.executionLog.toString());
+
+ // Calling shutdown again does not throw an Exception again,
+ // it's not really useful at shutdown.
+ serverSetup.shutdown();
+
+ assertEquals("Still expecting only one startup phase to have run",
+ "four", TestSetupPhase.executionLog.toString());
+ }
+}
\ No newline at end of file
Propchange:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/ServerSetupSingletonTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/ServerSetupSingletonTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestServerSetup.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestServerSetup.java?rev=1231404&view=auto
==============================================================================
---
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestServerSetup.java
(added)
+++
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestServerSetup.java
Fri Jan 13 23:54:42 2012
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.testing.tools.test;
+
+import org.apache.sling.testing.tools.serversetup.ServerSetup;
+
+public class TestServerSetup extends ServerSetup {
+ public TestServerSetup() {
+ addSetupPhase(new TestSetupPhase("one", true));
+ addSetupPhase(new TestSetupPhase("three", true));
+ addSetupPhase(new TestSetupPhase("two", true));
+ addSetupPhase(new TestSetupPhase("five", false));
+ addSetupPhase(new TestSetupPhase("four", false));
+ }
+}
Propchange:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestServerSetup.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestServerSetup.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestSetupPhase.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestSetupPhase.java?rev=1231404&view=auto
==============================================================================
---
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestSetupPhase.java
(added)
+++
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestSetupPhase.java
Fri Jan 13 23:54:42 2012
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.testing.tools.test;
+
+import org.apache.sling.testing.tools.serversetup.ServerSetup;
+import org.apache.sling.testing.tools.serversetup.SetupPhase;
+
+class TestSetupPhase implements SetupPhase {
+ static StringBuilder executionLog;
+ static String failingPhases = "";
+ private final boolean isStartup;
+ private final String id;
+
+ TestSetupPhase(String id, boolean isStartup) {
+ this.id = id;
+ this.isStartup = isStartup;
+ }
+
+ static void clearExecutionLog() {
+ executionLog = new StringBuilder();
+ }
+
+ public void run(ServerSetup owner) throws Exception {
+ if(failingPhases.contains(id)) {
+ throw new Exception("Failing as failingPhase contains my id");
+ }
+ if(executionLog.length() > 0) {
+ executionLog.append(",");
+ }
+ executionLog.append(getId());
+ }
+
+ public boolean isStartupPhase() {
+ return isStartup;
+ }
+
+ public String getDescription() {
+ return "";
+ }
+
+ public String getId() {
+ return id;
+ }
+}
Propchange:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestSetupPhase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/test/TestSetupPhase.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL