Author: nbelaevski
Date: 2009-07-12 18:49:21 -0400 (Sun, 12 Jul 2009)
New Revision: 14901
Added:
framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractThreadedTest.java
Modified:
framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractFacesTest.java
framework/trunk/test-base/src/test/java/org/richfaces/test/FacesServerTest.java
Log:
Framework unit tests updated
Modified:
framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractFacesTest.java
===================================================================
---
framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractFacesTest.java 2009-07-12
22:33:31 UTC (rev 14900)
+++
framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractFacesTest.java 2009-07-12
22:49:21 UTC (rev 14901)
@@ -17,6 +17,7 @@
import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.ApplicationFactory;
+import javax.faces.application.ProjectStage;
import javax.faces.application.StateManager;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
@@ -189,7 +190,8 @@
/**
* This template method called from {@link #setUp()} to append appropriate init
parameters to the test server.
- * The default implementation sets state saving method to the "server" and
default jsf page suffix to the ".xhtml"
+ * The default implementation sets state saving method to the "server",
default jsf page suffix to the ".xhtml"
+ * and project stage to UnitTest
*/
protected void setupJsfInitParameters() {
facesServer.addInitParameter(
@@ -197,6 +199,9 @@
StateManager.STATE_SAVING_METHOD_SERVER);
facesServer.addInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME,
".xhtml");
+
+ facesServer.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME,
+ ProjectStage.UnitTest.name());
}
/**
Added:
framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractThreadedTest.java
===================================================================
--- framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractThreadedTest.java
(rev 0)
+++
framework/trunk/test-base/src/main/java/org/richfaces/test/AbstractThreadedTest.java 2009-07-12
22:49:21 UTC (rev 14901)
@@ -0,0 +1,128 @@
+/**
+ *
+ */
+package org.richfaces.test;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+import junit.framework.TestResult;
+
+/**
+ * @author asmirnov
+ * @author Nick Belaevski
+ */
+public abstract class AbstractThreadedTest extends TestCase {
+
+ /**
+ * The threads that are executing.
+ */
+ private Thread threads[] = null;
+ /**
+ * The tests TestResult.*/
+ private TestResult testResult = null;
+
+ public void interruptThreads() {
+ if(threads != null) {
+ for(int i = 0;i < threads.length;i++) {
+ threads[i].interrupt();
+ }
+ }
+ }
+
+ /**
+ * Override run so we can squirrel away the test result.
+ *
+ */
+ @Override
+ public void run(final TestResult result) {
+ testResult = result;
+ super.run(result);
+ testResult = null;
+ }
+
+ /**
+ * Create instances of classes and run threads with it.
+ * @param clazz - class of test thread implementation.
+ * @param numThreads - number of threads to run.
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
+ protected void runTestCaseThreads(Class<?> clazz, int numThreads) {
+ TestCaseRunnable[] runnables = new TestCaseRunnable[numThreads];
+ for (int i = 0; i < runnables.length; i++) {
+ try {
+ runnables[i]= (TestCaseRunnable) clazz.newInstance();
+ } catch (Exception e) {
+ testResult.addError(this, e);
+ return;
+ }
+ }
+ runTestCaseRunnables(runnables);
+ }
+ /**
+ * Run the test case threads.
+ * @param runnables - array with instances of {@link TestCaseRunnable} with concrete
tests
+ */
+ protected void runTestCaseRunnables (final TestCaseRunnable[] runnables) {
+ if(runnables == null) {
+ throw new IllegalArgumentException("runnables is null");
+ }
+ threads = new Thread[runnables.length];
+ for(int i = 0;i < threads.length;i++) {
+ threads[i] = new Thread(runnables[i]);
+ }
+ for(int i = 0;i < threads.length;i++) {
+ threads[i].start();
+ }
+ try {
+ for(int i = 0;i < threads.length;i++) {
+ threads[i].join();
+ }
+ }
+ catch(InterruptedException ignore) {
+ System.out.println("Thread join interrupted.");
+ }
+ threads = null;
+ }
+
+ /**
+ * Handle an exception. Since multiple threads won't have their
+ * exceptions caught the threads must manually catch them and call
+ * <code>handleException ()</code>.
+ * @param t Exception to handle.*/
+ private void handleException(final Throwable t) {
+ synchronized(testResult) {
+ if(t instanceof AssertionFailedError) {
+ testResult.addFailure(this, (AssertionFailedError)t);
+ }
+ else {
+ testResult.addError(this, t);
+ }
+ }
+ }
+
+ /**
+ * A test case thread. Override runTestCase () and define
+ * behaviour of test in there.*/
+ public abstract class TestCaseRunnable implements Runnable {
+ /**
+ * Override this to define the test*/
+
+ public abstract void runTestCase()
+ throws Throwable;
+ /**
+ * Run the test in an environment where
+ * we can handle the exceptions generated by the test method.*/
+
+ public void run() {
+ try {
+ runTestCase();
+ }
+ catch(Throwable t) /* Any other exception we handle and then we interrupt the
other threads.*/ {
+ handleException(t);
+ interruptThreads();
+ }
+ }
+ }
+
+}
Modified: framework/trunk/test-base/src/test/java/org/richfaces/test/FacesServerTest.java
===================================================================
---
framework/trunk/test-base/src/test/java/org/richfaces/test/FacesServerTest.java 2009-07-12
22:33:31 UTC (rev 14900)
+++
framework/trunk/test-base/src/test/java/org/richfaces/test/FacesServerTest.java 2009-07-12
22:49:21 UTC (rev 14901)
@@ -3,37 +3,21 @@
*/
package org.richfaces.test;
-import static org.junit.Assert.*;
-
import java.io.IOException;
-import java.io.InputStream;
import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.logging.Level;
-import java.util.logging.LogManager;
-import java.util.logging.Logger;
-import javax.faces.application.StateManager;
-import javax.faces.application.ViewHandler;
-import javax.faces.webapp.FacesServlet;
import javax.servlet.http.HttpSession;
import org.junit.After;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
import org.w3c.dom.Element;
-import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
-import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.SubmittableElement;
-import com.sun.faces.config.ConfigureListener;
-import com.sun.faces.util.FacesLogger;
/**
* @author asmirnov
Show replies by date