Author: rwatler
Date: Mon Nov 24 19:41:06 2014
New Revision: 1641451
URL: http://svn.apache.org/r1641451
Log:
JS2-1307: Refactor Jexl Scriptable Test Case for Reuse: include PAM tests
missed on first pass.
Modified:
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestCase.java
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestServer.java
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractSpringTestCase.java
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManagerServer.java
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/TestPortletApplicationManager.java
Modified:
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestCase.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestCase.java?rev=1641451&r1=1641450&r2=1641451&view=diff
==============================================================================
---
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestCase.java
(original)
+++
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestCase.java
Mon Nov 24 19:41:06 2014
@@ -17,7 +17,6 @@
package org.apache.jetspeed.components.test;
-import junit.framework.TestCase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -40,7 +39,7 @@ import java.util.Map;
* @author <a href="mailto:[email protected]">Randy Watler</a>
* @version $Id:$
*/
-public abstract class AbstractJexlSpringTestCase extends TestCase {
+public abstract class AbstractJexlSpringTestCase extends
AbstractSpringTestCase {
private static final long LOGGING_PUMP_WAIT = 50;
@@ -54,6 +53,11 @@ public abstract class AbstractJexlSpring
private Map<String,String> systemProperties;
private String classPath;
+ /**
+ * Setup test program process context.
+ *
+ * @throws Exception
+ */
@Override
protected void setUp() throws Exception {
// environment setup
@@ -94,21 +98,99 @@ public abstract class AbstractJexlSpring
super.setUp();
}
+ @Override
+ protected String[] getConfigurations() {
+ // disabled Spring component manager by default
+ return null;
+ }
+
+ @Override
+ protected String getBeanDefinitionFilterCategories() {
+ // disabled Spring component manager by default
+ return null;
+ }
+
+ /**
+ * Filter system property values per test program context. Typically used
to modify
+ * system property values that need to be different based on test program
index.
+ *
+ * @param propertyName system property name
+ * @param index test program index
+ * @param propertyValue original system property value
+ * @return original or modified system property value
+ */
protected String testProgramSystemPropertyValueFilter(String propertyName,
int index, String propertyValue) {
// return original property value by default
return propertyValue;
}
+ /**
+ * Set additional system properties to be set for test program process.
+ *
+ * @return map of system properties
+ */
protected Map<String,String> testProgramSystemProperties() {
return new HashMap<String,String>();
}
+ /**
+ * Sleep for specified interval continuing to pump logging messages for
test
+ * program server.
+ *
+ * @param server test program server
+ * @param millis sleep interval
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ protected void sleep(TestProgram server, long millis) throws IOException,
InterruptedException {
+ sleep(new TestProgram[]{server}, millis);
+ }
+
+ /**
+ * Sleep for specified interval continuing to pump logging messages for
test
+ * program servers.
+ *
+ * @param server0 test program server
+ * @param server1 test program server
+ * @param millis sleep interval
+ * @throws IOException
+ * @throws InterruptedException
+ */
protected void sleep(TestProgram server0, TestProgram server1, long
millis) throws IOException, InterruptedException {
+ sleep(new TestProgram[]{server0, server1}, millis);
+ }
+
+ /**
+ * Sleep for specified interval continuing to pump logging messages for
test
+ * program servers.
+ *
+ * @param server0 test program server
+ * @param server1 test program server
+ * @param server2 test program server
+ * @param millis sleep interval
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ protected void sleep(TestProgram server0, TestProgram server1, TestProgram
server2, long millis) throws IOException, InterruptedException {
+ sleep(new TestProgram[]{server0, server1, server2}, millis);
+ }
+
+ /**
+ * Sleep for specified interval continuing to pump logging messages for
test
+ * program servers.
+ *
+ * @param servers test program servers
+ * @param millis sleep interval
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ protected void sleep(TestProgram [] servers, long millis) throws
IOException, InterruptedException {
long slept = 0;
while (slept < millis) {
// poll servers for logging
- server0.poll();
- server1.poll();
+ for (TestProgram server : servers) {
+ server.poll();
+ }
// sleep for interval
long sleep = Math.min(millis-slept, LOGGING_PUMP_WAIT);
Thread.sleep(sleep);
@@ -116,6 +198,9 @@ public abstract class AbstractJexlSpring
}
}
+ /**
+ * Test program process implementation.
+ */
protected class TestProgram {
private String name;
@@ -126,12 +211,24 @@ public abstract class AbstractJexlSpring
private BufferedWriter processInput;
private BufferedReader processOutput;
+ /**
+ * Test program constructor.
+ *
+ * @param name test program name
+ * @param mainClass server main program class
+ * @param index test program index
+ */
public TestProgram(String name, Class<?> mainClass, int index) {
this.name = name;
this.mainClass = mainClass;
this.index = index;
}
+ /**
+ * Start remote test program process with log message pump.
+ *
+ * @throws IOException
+ */
public synchronized void start() throws IOException {
assertNull(process);
@@ -170,8 +267,12 @@ public abstract class AbstractJexlSpring
}
}
- public synchronized void poll() throws IOException
- {
+ /**
+ * Poll remote test program process for output log messages.
+ *
+ * @throws IOException
+ */
+ public synchronized void poll() throws IOException {
assertNotNull(process);
// read messages from process
@@ -180,8 +281,20 @@ public abstract class AbstractJexlSpring
}
}
- public synchronized String execute(String scriptLine) throws
IOException
- {
+ /**
+ * Execute script line in remote test program process. Returns line
with
+ * prompt, the script line executed, and the non-null result string
following a
+ * '->' delimiter. Examples:
+ * > x.getStatus(); -> STARTED
+ * > y.doSomething();
+ * Also includes a poll() invocation to pull log messages from the
test program
+ * output.
+ *
+ * @param scriptLine script line to execute
+ * @return script line and result
+ * @throws IOException
+ */
+ public synchronized String execute(String scriptLine) throws
IOException {
// poll to read messages from process
poll();
@@ -206,6 +319,14 @@ public abstract class AbstractJexlSpring
return resultLine;
}
+ /**
+ * Shutdown remote test program process, forcibly if necessary after
+ * waiting for the specified timeout if it does not stop in its own.
+ *
+ * @param millis shutdown timeout
+ * @throws IOException
+ * @throws InterruptedException
+ */
public synchronized void shutdown(final long millis) throws
IOException, InterruptedException {
assertNotNull(process);
@@ -241,8 +362,12 @@ public abstract class AbstractJexlSpring
destroyThread.join();
}
- private void logProcessLine(String line)
- {
+ /**
+ * Log pumped error and info lines from the remote test program
process.
+ *
+ * @param line remote process output line.
+ */
+ private void logProcessLine(String line) {
if (line.contains("ERROR") || line.contains("Exception") ||
line.matches("\\s+at\\s.*")) {
log.error("{"+name+"} "+line);
} else {
@@ -250,4 +375,54 @@ public abstract class AbstractJexlSpring
}
}
}
+
+ /**
+ * Execute script against specified server asynchronously.
+ */
+ protected static class TestExecuteThread extends Thread {
+
+ private TestProgram server;
+ private String scriptLine;
+ private String result;
+ private Exception exception;
+
+ /**
+ * Construct thread.
+ *
+ * @param server test program server
+ * @param scriptLine script line to execute
+ */
+ public TestExecuteThread(TestProgram server, String scriptLine) {
+ this.server = server;
+ this.scriptLine = scriptLine;
+ }
+
+ /**
+ * Execute script line in thread.
+ */
+ public void run() {
+ try {
+ result = server.execute(scriptLine);
+ } catch (Exception e) {
+ exception = e;
+ }
+ }
+
+ /**
+ * Join and return script line result.
+ *
+ * @return script line result
+ * @throws Exception throws execution exception
+ */
+ public String getResult() throws Exception {
+ try {
+ join();
+ } catch (InterruptedException ie) {
+ }
+ if (exception != null) {
+ throw exception;
+ }
+ return result;
+ }
+ }
}
Modified:
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestServer.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestServer.java?rev=1641451&r1=1641450&r2=1641451&view=diff
==============================================================================
---
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestServer.java
(original)
+++
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractJexlSpringTestServer.java
Mon Nov 24 19:41:06 2014
@@ -48,7 +48,7 @@ public abstract class AbstractJexlSpring
protected boolean exit;
/**
- * Initialize page manager server instance and script context.
+ * Initialize server component manager and script context.
*
* @throws Exception
*/
@@ -101,7 +101,7 @@ public abstract class AbstractJexlSpring
protected abstract Map<String,Object> getContextVars();
/**
- * Terminate page manager server instance.
+ * Terminate server component manager.
*
* @throws Exception
*/
@@ -111,7 +111,7 @@ public abstract class AbstractJexlSpring
}
/**
- * Execute a single line script against page manager server context.
+ * Execute a single line script against server context.
*
* @param scriptLine jexl script
* @return script result line
Modified:
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractSpringTestCase.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractSpringTestCase.java?rev=1641451&r1=1641450&r2=1641451&view=diff
==============================================================================
---
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractSpringTestCase.java
(original)
+++
portals/jetspeed-2/portal/trunk/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/test/AbstractSpringTestCase.java
Mon Nov 24 19:41:06 2014
@@ -16,14 +16,14 @@
*/
package org.apache.jetspeed.components.test;
-import java.io.IOException;
-import java.util.Properties;
-
import org.apache.jetspeed.JetspeedActions;
import org.apache.jetspeed.components.JetspeedBeanDefinitionFilter;
import org.apache.jetspeed.components.SpringComponentManager;
import org.apache.jetspeed.test.JetspeedTestCase;
+import java.io.IOException;
+import java.util.Properties;
+
/**
* <p>
* AbstractSpringTestCase
@@ -60,17 +60,23 @@ public abstract class AbstractSpringTest
protected void setUp() throws Exception
{
super.setUp();
- scm = new SpringComponentManager(getBeanDefinitionFilter(),
getBootConfigurations(), getConfigurations(),
getBaseDir()+"target/test-classes/webapp", getInitProperties(), false);
- scm.start();
- new JetspeedActions(getSupportedPortletModes(),
getSupportedWindowStates());
+ String [] configurations = getConfigurations();
+ if ((configurations != null) && (configurations.length > 0))
+ {
+ scm = new SpringComponentManager(getBeanDefinitionFilter(),
getBootConfigurations(), getConfigurations(), getBaseDir() +
"target/test-classes/webapp", getInitProperties(), false);
+ scm.start();
+ new JetspeedActions(getSupportedPortletModes(),
getSupportedWindowStates());
+ }
}
/**
* close Spring context as part of test teardown
*/
protected void tearDown() throws Exception
- {
- scm.stop();
+ {
+ if (scm != null) {
+ scm.stop();
+ }
super.tearDown();
}
@@ -94,7 +100,8 @@ public abstract class AbstractSpringTest
protected JetspeedBeanDefinitionFilter getBeanDefinitionFilter() throws
IOException
{
- return new
JetspeedBeanDefinitionFilter(getBeanDefinitionFilterCategories());
+ String categories = getBeanDefinitionFilterCategories();
+ return ((categories != null) ? new
JetspeedBeanDefinitionFilter(categories) : new JetspeedBeanDefinitionFilter());
}
/**
Modified:
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManagerServer.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManagerServer.java?rev=1641451&r1=1641450&r2=1641451&view=diff
==============================================================================
---
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManagerServer.java
(original)
+++
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManagerServer.java
Mon Nov 24 19:41:06 2014
@@ -16,24 +16,18 @@
*/
package org.apache.jetspeed.tools.pamanager;
-import org.apache.commons.jexl.JexlContext;
-import org.apache.commons.jexl.JexlHelper;
-import org.apache.commons.jexl.Script;
-import org.apache.commons.jexl.ScriptFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.jetspeed.components.JetspeedBeanDefinitionFilter;
-import org.apache.jetspeed.components.SpringComponentManager;
import org.apache.jetspeed.components.jndi.JetspeedTestJNDIComponent;
import org.apache.jetspeed.components.portletregistry.RegistryException;
+import org.apache.jetspeed.components.test.AbstractJexlSpringTestServer;
import org.apache.jetspeed.util.DirectoryHelper;
-import java.io.BufferedReader;
import java.io.File;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
* PortletApplicationManagerServer
@@ -41,38 +35,41 @@ import java.util.List;
* @author <a href="mailto:[email protected]">Randy Watler</a>
* @version $Id: $
*/
-public class PortletApplicationManagerServer
-{
+public class PortletApplicationManagerServer extends
AbstractJexlSpringTestServer {
+
protected static Log log =
LogFactory.getLog(PortletApplicationManagerServer.class);
- // Constants
-
- public static final String SCRIPT_RESULT_LINE_PREFIX = "> ";
-
- // Members
-
private JetspeedTestJNDIComponent jndiDS;
- private String baseDir;
- private SpringComponentManager scm;
- private JexlContext jexlContext;
- private boolean exit;
private PortletApplicationManagement portletApplicationManager;
- // Life cycle
-
- /**
- * Initialize page manager server instance and script context.
- *
- * @throws Exception
- */
- public void initialize() throws Exception
- {
- // setup jetspeed test datasource and component manager
+ @Override
+ public void initialize() throws Exception {
+ // setup jetspeed test datasource
jndiDS = new JetspeedTestJNDIComponent();
jndiDS.setup();
- final JetspeedBeanDefinitionFilter beanDefinitionFilter = new
JetspeedBeanDefinitionFilter("default,jdbcDS,xmlPageManager,security,dbSecurity");
- final String [] bootConfigurations = new
String[]{"boot/datasource.xml"};
- final List<String> configurationsList = new ArrayList<String>();
+
+ // initialize component manager and server
+ super.initialize();
+
+ // access portal application manager
+ portletApplicationManager = scm.lookupComponent("PAM");
+
+ log.info("PortalApplicationManager server initialized");
+ }
+
+ @Override
+ protected String getBeanDefinitionFilterCategories() {
+ return "default,jdbcDS,xmlPageManager,security,dbSecurity";
+ }
+
+ @Override
+ protected String[] getBootConfigurations() {
+ return new String[]{"boot/datasource.xml"};
+ }
+
+ @Override
+ protected String[] getConfigurations() {
+ List<String> configurationsList = new ArrayList<String>();
configurationsList.add("transaction.xml");
configurationsList.add("cache.xml");
configurationsList.add("jetspeed-base.xml");
@@ -96,77 +93,34 @@ public class PortletApplicationManagerSe
configurationsList.add("JETSPEED-INF/spring/JetspeedPrincipalManagerProviderOverride.xml");
configurationsList.add("search.xml");
configurationsList.add("cluster-node.xml");
- final String[] configurations = configurationsList.toArray(new
String[configurationsList.size()]);
- baseDir = System.getProperty("basedir");
- if ((baseDir == null) || (baseDir.length() == 0))
- {
- baseDir = System.getProperty("user.dir");
- }
- final String appRoot = baseDir+"/target/test-classes/webapp";
- scm = new SpringComponentManager(beanDefinitionFilter,
bootConfigurations, configurations, appRoot, false);
- scm.start();
+ return configurationsList.toArray(new
String[configurationsList.size()]);
+ }
- // access portal application manager
- portletApplicationManager = scm.lookupComponent("PAM");
-
- // create jexl context
- jexlContext = JexlHelper.createContext();
- jexlContext.getVars().put("portletApplicationManagerServer", this);
-
- log.info( "PortalApplicationManager server initialized");
+ @Override
+ protected Map<String,Object> getContextVars() {
+ Map<String,Object> contextVars = new HashMap<String,Object>();
+ contextVars.put("portletApplicationManagerServer", this);
+ return contextVars;
}
-
- /**
- * Terminate page manager server instance.
- *
- * @throws Exception
- */
- public void terminate() throws Exception
- {
- // tear down jetspeed component manager and test datasource
- scm.stop();
+
+ @Override
+ public void terminate() throws Exception {
+ // tear down component manager and server
+ super.terminate();
+
+ // tear down jetspeed test datasource
jndiDS.tearDown();
- log.info( "PortalApplicationManager server terminated");
+ log.info("PortalApplicationManager server terminated");
}
- // Implementation
-
- /**
- * Execute a single line script against page manager server context.
- *
- * @param scriptLine jexl script
- * @return script result line
- */
- public String execute(final String scriptLine)
- {
- // execute script line and return result line
- String resultLine = scriptLine;
- try
- {
- final Script jexlScript = ScriptFactory.createScript(scriptLine);
- final Object result = jexlScript.execute(jexlContext);
- if (result != null)
- {
- resultLine += " -> "+result;
- }
- }
- catch (final Exception e)
- {
- resultLine += " -> "+e;
- }
- return resultLine;
- }
-
/**
* Start test portlet application.
*
* @throws RegistryException
*/
- public void startPortletApplication() throws RegistryException
- {
- if (portletApplicationManager.isStarted())
- {
+ public void startPortletApplication() throws RegistryException {
+ if (portletApplicationManager.isStarted()) {
DirectoryHelper portletApplicationDir = new DirectoryHelper(new
File(baseDir+"/src/test/testdata/"+TestPortletApplicationManager.CONTEXT_NAME));
ClassLoader contextClassLoader =
Thread.currentThread().getContextClassLoader();
portletApplicationManager.startPortletApplication(TestPortletApplicationManager.CONTEXT_NAME,
TestPortletApplicationManager.CONTEXT_PATH, portletApplicationDir,
contextClassLoader);
@@ -178,10 +132,8 @@ public class PortletApplicationManagerSe
*
* @throws RegistryException
*/
- public void stopPortletApplication() throws RegistryException
- {
- if (portletApplicationManager.isStarted())
- {
+ public void stopPortletApplication() throws RegistryException {
+ if (portletApplicationManager.isStarted()) {
portletApplicationManager.stopPortletApplication(TestPortletApplicationManager.CONTEXT_NAME);
}
}
@@ -191,82 +143,21 @@ public class PortletApplicationManagerSe
*
* @throws RegistryException
*/
- public void unregisterPortletApplication() throws RegistryException
- {
- if (portletApplicationManager.isStarted())
- {
+ public void unregisterPortletApplication() throws RegistryException {
+ if (portletApplicationManager.isStarted()) {
portletApplicationManager.unregisterPortletApplication(TestPortletApplicationManager.CONTEXT_NAME);
}
}
/**
- * Sets server exit flag.
- */
- public void exit()
- {
- exit = true;
- }
-
- // Data access
-
- /**
- * @return server exit flag
- */
- public boolean isExit()
- {
- return exit;
- }
-
- // Application entry point
-
- /**
* Server main entry point.
*
* @param args not used
*/
- public static void main(final String [] args)
- {
- try
- {
- // create and initialize server
- final PortletApplicationManagerServer server = new
PortletApplicationManagerServer();
- server.initialize();
-
- // simple server reads script lines from standard
- // input and writes results on standard output
- final BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
- final PrintWriter out = new PrintWriter(System.out, true);
- do
- {
- // read single line scripts to execute
- String scriptLine = in.readLine();
- if (scriptLine != null)
- {
- scriptLine = scriptLine.trim();
- String resultLine = "";
- if (scriptLine.length() > 0)
- {
- // execute script
- resultLine = server.execute(scriptLine);
- }
-
- // write prefixed single line results
- out.println(SCRIPT_RESULT_LINE_PREFIX+resultLine);
- }
- else
- {
- // exit server on input EOF
- server.exit();
- }
- }
- while (!server.isExit());
-
- // terminate server
- server.terminate();
- }
- catch (final Throwable t)
- {
- log.error( "Unexpected exception: "+t, t);
+ public static void main(String [] args) {
+ Throwable error = (new PortletApplicationManagerServer()).run();
+ if (error != null) {
+ log.error( "Unexpected exception: "+error, error);
}
}
}
Modified:
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/TestPortletApplicationManager.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/TestPortletApplicationManager.java?rev=1641451&r1=1641450&r2=1641451&view=diff
==============================================================================
---
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/TestPortletApplicationManager.java
(original)
+++
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/tools/pamanager/TestPortletApplicationManager.java
Mon Nov 24 19:41:06 2014
@@ -21,30 +21,20 @@ import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.jetspeed.AbstractRequestContextTestCase;
+import org.apache.jetspeed.components.jndi.JetspeedTestJNDIComponent;
import org.apache.jetspeed.components.portletregistry.RegistryException;
+import org.apache.jetspeed.components.test.AbstractJexlSpringTestCase;
import org.apache.jetspeed.security.RoleManager;
import org.apache.jetspeed.security.SecurityDomain;
import org.apache.jetspeed.security.impl.SecurityDomainImpl;
import org.apache.jetspeed.security.spi.SecurityDomainAccessManager;
import org.apache.jetspeed.security.spi.SecurityDomainStorageManager;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.net.URL;
-import java.net.URLClassLoader;
import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.Properties;
-public class TestPortletApplicationManager extends
AbstractRequestContextTestCase
+public class TestPortletApplicationManager extends AbstractJexlSpringTestCase
{
private static final Log log =
LogFactory.getLog(TestPortletApplicationManager.class);
@@ -55,152 +45,144 @@ public class TestPortletApplicationManag
public static final String CONTEXT_NAME = "test-pa";
public static final String CONTEXT_PATH = "/"+CONTEXT_NAME;
- private String osExecutableExtension;
- private String fileSeparator;
- private File javaExecutablePath;
- private String classPathSeparator;
- private File projectDirectoryPath;
- private Map<String,String> systemProperties;
- private String classPath;
+ private static final long TEST_PROCESS_SHUTDOWN_WAIT = 5000;
- private String baseDir;
- private PortletApplicationManagement portletApplicationManager;
+ protected JetspeedTestJNDIComponent jndiDS;
+ private PortletApplicationManagement portletApplicationManager;
/**
* Configure test methods.
*
* @return test suite.
*/
- public static Test suite()
- {
+ public static Test suite() {
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestPortletApplicationManager.class);
}
- /* (non-Javadoc)
- * @see
org.apache.jetspeed.AbstractRequestContextTestCase#getConfigurations()
- */
- protected String[] getConfigurations()
- {
- String[] confs = super.getConfigurations();
- List<String> confList = new ArrayList<String>(Arrays.asList(confs));
- confList.add("deployment.xml");
- if (TEST_USE_VERSIONED_PAM)
- {
- confList.add("alternate/versioned-deployment/deployment.xml");
- }
- confList.add("search.xml");
- confList.add("cluster-node.xml");
- return (String[]) confList.toArray(new String[1]);
- }
-
- /* (non-Javadoc)
- * @see
org.apache.jetspeed.components.test.AbstractSpringTestCase#getInitProperties()
- */
- protected Properties getInitProperties()
- {
- // setup dummy autodeployment properties
- baseDir = System.getProperty("basedir", ".");
- if ((baseDir == null) || (baseDir.length() == 0))
- {
- baseDir = System.getProperty("user.dir");
- }
- // set test properties
- return setTestProperties(baseDir, super.getInitProperties());
- }
-
- /* (non-Javadoc)
- * @see
org.apache.jetspeed.components.util.RegistrySupportedTestCase#setUp()
- */
- protected void setUp() throws Exception
- {
- // environment setup
- osExecutableExtension =
(System.getProperty("os.name").startsWith("Windows") ? ".exe" : "");
- fileSeparator = System.getProperty("file.separator");
- javaExecutablePath = new
File(System.getProperty("java.home")+fileSeparator+"bin"+fileSeparator+"java"+osExecutableExtension);
- classPathSeparator = System.getProperty("path.separator");
- projectDirectoryPath = new File(System.getProperty("basedir"));
- systemProperties = new HashMap<String,String>();
- for (final Map.Entry<Object,Object> systemProperty :
System.getProperties().entrySet())
- {
- final String propertyName = systemProperty.getKey().toString();
- final String propertyValue = systemProperty.getValue().toString();
- if (propertyName.startsWith("org.apache.jetspeed.") ||
propertyName.startsWith("java.net.") || propertyName.equals("basedir"))
- {
- systemProperties.put(propertyName, propertyValue);
- }
- }
- // construct launcher classpath from current class loader
- final StringBuilder classPathBuilder = new StringBuilder();
- final ClassLoader loader = this.getClass().getClassLoader();
- assertTrue(loader instanceof URLClassLoader);
- final URLClassLoader urlLoader = (URLClassLoader)loader;
- assertNotNull(urlLoader.getURLs());
- for (final URL pathURL : urlLoader.getURLs())
- {
- // convert path URL to file path
- final String path = new File(pathURL.toURI()).getCanonicalPath();
+ @Override
+ protected void setUp() throws Exception {
+ // setup jetspeed test datasource
+ jndiDS = new JetspeedTestJNDIComponent();
+ jndiDS.setup();
- // build class path
- if (classPathBuilder.length() > 0)
- {
- classPathBuilder.append(classPathSeparator);
- }
- classPathBuilder.append(path);
- }
- classPath = classPathBuilder.toString();
- assertTrue(classPath.length() > 0);
+ // setup scripting and Spring test case
+ super.setUp();
// setup test
- super.setUp();
portletApplicationManager = scm.lookupComponent("PAM");
assertTrue(portletApplicationManager.isStarted());
Class<?> portletApplicationManagerClass =
scm.lookupComponent("org.apache.jetspeed.tools.pamanager.PortletApplicationManager").getClass();
log.info("PortletApplicationManager class: " +
portletApplicationManagerClass.getSimpleName());
+
// unregister portlet application
- try
- {
+ try {
portletApplicationManager.unregisterPortletApplication(CONTEXT_NAME);
+ } catch (RegistryException re) {
}
- catch (RegistryException re)
- {
- }
+
// create standard default security domain and user role as necessary
// for portlet application permissions
SecurityDomainAccessManager domainAccessManager =
scm.lookupComponent("org.apache.jetspeed.security.spi.SecurityDomainAccessManager");
- if (domainAccessManager.getDomainByName(SecurityDomain.DEFAULT_NAME)
== null)
- {
+ if (domainAccessManager.getDomainByName(SecurityDomain.DEFAULT_NAME)
== null) {
SecurityDomainStorageManager domainStorageManager =
scm.lookupComponent("org.apache.jetspeed.security.spi.SecurityDomainStorageManager");
SecurityDomainImpl defaultSecurityDomain = new
SecurityDomainImpl();
defaultSecurityDomain.setName(SecurityDomain.DEFAULT_NAME);
domainStorageManager.addDomain(defaultSecurityDomain);
}
RoleManager roleManager =
scm.lookupComponent("org.apache.jetspeed.security.RoleManager");
- if (!roleManager.roleExists("user"))
- {
+ if (!roleManager.roleExists("user")) {
roleManager.addRole("user");
}
- }
+ }
+
+ @Override
+ protected String[] getConfigurations() {
+ List<String> confList = new ArrayList<String>();
+ confList.add("transaction.xml");
+ confList.add("cache.xml");
+ confList.add("jetspeed-base.xml");
+ confList.add("jetspeed-properties.xml");
+ confList.add("page-manager.xml");
+ confList.add("registry.xml");
+ confList.add("search.xml");
+ confList.add("JETSPEED-INF/spring/RequestDispatcherService.xml");
+ confList.add("rc2.xml");
+ confList.add("static-bean-references.xml");
+ confList.add("security-managers.xml");
+ confList.add("security-providers.xml");
+ confList.add("security-spi.xml");
+ confList.add("security-atn.xml");
+ confList.add("security-spi-atn.xml");
+ confList.add("security-atz.xml");
+
confList.add("JETSPEED-INF/spring/JetspeedPrincipalManagerProviderOverride.xml");
+ confList.add("deployment.xml");
+ if (TEST_USE_VERSIONED_PAM)
+ {
+ confList.add("alternate/versioned-deployment/deployment.xml");
+ }
+ confList.add("search.xml");
+ confList.add("cluster-node.xml");
+ return confList.toArray(new String[1]);
+ }
+
+ @Override
+ protected String[] getBootConfigurations() {
+ return new String[]{"boot/datasource.xml"};
+ }
+
+ @Override
+ protected String getBeanDefinitionFilterCategories() {
+ return "default,jdbcDS,xmlPageManager,security,dbSecurity";
+ }
+
+ @Override
+ protected Properties getInitProperties() {
+ Properties properties = super.getInitProperties();
+ properties.setProperty("autodeployment.catalina.base",
getBaseDir()+"/target");
+ properties.setProperty("autodeployment.catalina.engine", "Catalina");
+ properties.setProperty("autodeployment.delay", "10000");
+ properties.setProperty("autodeployment.password", "test");
+ properties.setProperty("autodeployment.port", "8080");
+ properties.setProperty("autodeployment.server", "localhost");
+ properties.setProperty("autodeployment.staging.dir",
getBaseDir()+"/target");
+ properties.setProperty("autodeployment.target.dir",
getBaseDir()+"/target");
+ properties.setProperty("autodeployment.user", "test");
+ return properties;
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ // unregister portlet application
+ try {
+
portletApplicationManager.unregisterPortletApplication(CONTEXT_NAME);
+ } catch (RegistryException re) {
+ }
+ portletApplicationManager = null;
+
+ // tear down test
+ super.tearDown();
+
+ // tear down jetspeed test datasource
+ jndiDS.tearDown();
+ }
/**
* Test basic PortletApplicationManager operation.
*/
- public void testPortletApplicationManager()
- {
+ public void testPortletApplicationManager() {
// check for distributed database support
String databaseName =
System.getProperty("org.apache.jetspeed.database.default.name");
- if ((databaseName != null) && databaseName.equals("derby"))
- {
+ if ((databaseName != null) && databaseName.equals("derby")) {
System.out.println("Database support not distributed: system
limitation... test skipped");
log.warn("Database support not distributed: system limitation...
test skipped");
return;
}
// start portlet application manager test servers
- final TestProgram server0 = new TestProgram("server-0",
PortletApplicationManagerServer.class);
- final TestProgram server1 = new TestProgram("server-1",
PortletApplicationManagerServer.class);
- try
- {
+ final TestProgram server0 = new TestProgram("server-0",
PortletApplicationManagerServer.class, 0);
+ final TestProgram server1 = new TestProgram("server-1",
PortletApplicationManagerServer.class, 1);
+ try {
// start servers
server0.start();
server1.start();
@@ -211,11 +193,9 @@ public class TestPortletApplicationManag
// test starting and stopping portlet application
String result;
- for (int i = 0; (i < TEST_PORTLET_APPLICATION_RESTARTS); i++)
- {
+ for (int i = 0; (i < TEST_PORTLET_APPLICATION_RESTARTS); i++) {
// start portlet application
- if (TEST_CONCURRENT_PAM_ACCESS)
- {
+ if (TEST_CONCURRENT_PAM_ACCESS) {
// start portlet application asynchronously in background
threads per server
log.info("test concurrent register/start/stop portlet
application, iteration "+i+"...");
TestExecuteThread startPortletApplication0 = new
TestExecuteThread(server0,
"portletApplicationManagerServer.startPortletApplication();");
@@ -226,9 +206,7 @@ public class TestPortletApplicationManag
assertTrue(!result.contains("Exception"));
result = startPortletApplication1.getResult();
assertTrue(!result.contains("Exception"));
- }
- else
- {
+ } else {
// stop portlet application synchronously
log.info("test serial register/start/stop portlet
application, iteration "+i+"...");
result =
server0.execute("portletApplicationManagerServer.startPortletApplication();");
@@ -236,290 +214,44 @@ public class TestPortletApplicationManag
result =
server1.execute("portletApplicationManagerServer.startPortletApplication();");
assertTrue(!result.contains("Exception"));
}
+
// stop portlet application synchronously
result =
server1.execute("portletApplicationManagerServer.stopPortletApplication();");
assertTrue(!result.contains("Exception"));
result =
server0.execute("portletApplicationManagerServer.stopPortletApplication();");
assertTrue(!result.contains("Exception"));
+
// unregister portlet application
log.info("test unregister portlet application, iteration
"+i+"...");
- try
- {
+ try {
portletApplicationManager.unregisterPortletApplication(CONTEXT_NAME);
- }
- catch (RegistryException re)
- {
+ } catch (RegistryException re) {
}
}
- }
- catch (final Exception e)
- {
+ } catch (final Exception e) {
log.error("Server test exception: "+e, e);
fail("Server test exception: "+e);
- }
- finally
- {
+ } finally {
// silently shutdown servers
- try
- {
- server0.shutdown();
- }
- catch (final Exception e)
- {
+ try {
+ server0.shutdown(TEST_PROCESS_SHUTDOWN_WAIT);
+ } catch (final Exception e) {
log.error( "Server shutdown exception: "+e, e);
}
- try
- {
- server1.shutdown();
- }
- catch (final Exception e)
- {
+ try {
+ server1.shutdown(TEST_PROCESS_SHUTDOWN_WAIT);
+ } catch (final Exception e) {
log.error( "Server shutdown exception: "+e, e);
}
}
}
- /* (non-Javadoc)
- * @see
org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase#tearDown()
- */
- protected void tearDown() throws Exception
- {
- // unregister portlet application
- try
- {
-
portletApplicationManager.unregisterPortletApplication(CONTEXT_NAME);
- }
- catch (RegistryException re)
- {
- }
- portletApplicationManager = null;
- // teardown test
- super.tearDown();
- }
-
- /**
- * TestProgram
- *
- * Implementation of test program executables.
- */
- private class TestProgram
- {
- private String name;
- private Class<?> mainClass;
-
- private Process process;
- private BufferedWriter processInput;
- private BufferedReader processOutput;
-
- public TestProgram(final String name, final Class<?> mainClass)
- {
- this.name = name;
- this.mainClass = mainClass;
- }
-
- public synchronized void start() throws IOException
- {
- assertNull(process);
-
- // configure launcher with paths and properties
- final ProcessBuilder launcher = new ProcessBuilder();
- final List<String> commandAndArgs = new ArrayList<String>();
- commandAndArgs.add(javaExecutablePath.getCanonicalPath());
- for (Map.Entry<String,String> systemProperty :
systemProperties.entrySet())
- {
- final String propertyName = systemProperty.getKey();
- final String propertyValue = systemProperty.getValue();
- commandAndArgs.add( "-D"+propertyName+"="+propertyValue);
- }
-
commandAndArgs.add("-Dlog4j.configuration=log4j-stdout.properties");
- commandAndArgs.add("-classpath");
- commandAndArgs.add(classPath);
- commandAndArgs.add(mainClass.getName());
- log.info("Launcher command for "+name+": "+commandAndArgs);
- launcher.command(commandAndArgs);
- launcher.directory(projectDirectoryPath);
- launcher.redirectErrorStream(true);
-
- // launch test programs
- process = launcher.start();
-
- // setup I/O for process
- processInput = new BufferedWriter(new
OutputStreamWriter(process.getOutputStream()));
- processOutput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
-
- // read messages from process
- for (String line; (processOutput.ready() && ((line =
processOutput.readLine()) != null));)
- {
- logProcessLine(line);
- }
- }
-
- public synchronized String execute(final String scriptLine) throws
IOException
- {
- assertNotNull(process);
-
- // read messages from process
- for (String line; (processOutput.ready() && ((line =
processOutput.readLine()) != null));)
- {
- logProcessLine(line);
- }
-
- // write script line to process
- processInput.write(scriptLine);
- processInput.newLine();
- processInput.flush();
-
- // read result or messages from process
- String resultLine = null;
- for (String line; ((line = processOutput.readLine()) != null);)
- {
- if (!
line.startsWith(PortletApplicationManagerServer.SCRIPT_RESULT_LINE_PREFIX))
- {
- logProcessLine(line);
- }
- else
- {
- resultLine = line;
- break;
- }
- }
- if ( resultLine == null)
- {
- throw new IOException("Unexpected EOF from process output");
- }
- return resultLine;
- }
-
- public synchronized void shutdown() throws IOException,
InterruptedException
- {
- assertNotNull( process);
-
- // start thread to destroy process on timeout
- final Thread destroyThread = new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- Thread.sleep(10000);
- if ( process != null)
- {
- log.warn( "Forcibly stopping "+name);
- process.destroy();
- }
- }
- catch ( final Exception e)
- {
- }
- }
- }, "DestroyThread");
- destroyThread.setDaemon( true);
- destroyThread.start();
-
- // close process input to shutdown server and read messages
- processInput.close();
- for (String line; ((line = processOutput.readLine()) != null);)
- {
- logProcessLine(line);
- }
-
- // join on process completion
- process.waitFor();
- processOutput.close();
- process = null;
-
- // join on destroy thread
- destroyThread.interrupt();
- destroyThread.join();
- }
-
- private void logProcessLine(final String line)
- {
- if (!line.contains("INFO") && (line.contains("ERROR") ||
line.contains("Exception") || line.matches("\\s+at\\s.*")))
- {
- log.error("{"+name+"} "+line);
- }
- else
- {
- log.info("{"+name+"} "+line);
- }
- }
- }
-
- /**
- * TestExecuteThread
- *
- * Execute script against specified server asynchronously.
- */
- private class TestExecuteThread extends Thread
- {
- private TestProgram server;
- private String scriptLine;
- private String result;
- private Exception exception;
-
- private TestExecuteThread(TestProgram server, String scriptLine)
- {
- this.server = server;
- this.scriptLine = scriptLine;
- }
-
- public void run()
- {
- try
- {
- result = server.execute(scriptLine);
- }
- catch (Exception e)
- {
- exception = e;
- }
- }
-
- public String getResult() throws Exception
- {
- try
- {
- join();
- }
- catch (InterruptedException ie)
- {
- }
- if (exception != null)
- {
- throw exception;
- }
- return result;
- }
- }
-
- /**
- * Set test configuration properties.
- *
- * @param baseDir project base directory path
- * @param properties properties set to configure
- */
- public static Properties setTestProperties(String baseDir, Properties
properties)
- {
- properties.setProperty("autodeployment.catalina.base",
baseDir+"/target");
- properties.setProperty("autodeployment.catalina.engine", "Catalina");
- properties.setProperty("autodeployment.delay", "10000");
- properties.setProperty("autodeployment.password", "test");
- properties.setProperty("autodeployment.port", "8080");
- properties.setProperty("autodeployment.server", "localhost");
- properties.setProperty("autodeployment.staging.dir",
baseDir+"/target");
- properties.setProperty("autodeployment.target.dir", baseDir+"/target");
- properties.setProperty("autodeployment.user", "test");
- return properties;
- }
-
/**
* Start the tests.
*
* @param args the arguments. Not used
*/
- public static void main(String args[])
- {
+ public static void main(String args[]) {
TestRunner.main(new String[]
{TestPortletApplicationManager.class.getName()});
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]