Author: nbelaevski
Date: 2007-04-02 15:52:02 -0400 (Mon, 02 Apr 2007)
New Revision: 48
Modified:
trunk/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java
Log:
Unpacking of /WEB-XML to temp folder implemented to configure env. when test is packed
into JAR
Modified: trunk/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java
===================================================================
--- trunk/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java 2007-03-31
00:27:58 UTC (rev 47)
+++ trunk/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java 2007-04-02
19:52:02 UTC (rev 48)
@@ -22,10 +22,15 @@
package org.ajax4jsf.tests;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
+import java.util.Random;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
import javax.faces.FacesException;
import javax.faces.FactoryFinder;
@@ -67,6 +72,8 @@
*/
public class AbstractAjax4JsfTestCase extends AbstractJsfTestCase {
+ private File tmpRoot = null;
+
/**
* @param name
*/
@@ -102,7 +109,50 @@
servletContext.setAttribute("a4jSkin", new SkinBean());
// Setup servlet context and testing web.xml
request.setPathElements("/testContext","/faces","/vievId.jsf","");
- servletContext.setDocumentRoot(new
File(getClass().getResource("/WEB-INF/web.xml").getFile()).getParentFile().getParentFile());
+
+ File webRoot = new
File(getClass().getResource("/WEB-INF/web.xml").getFile()).getParentFile().getParentFile();
+ if (webRoot.exists()) {
+ servletContext.setDocumentRoot(webRoot);
+ } else {
+ // Prepare WEB-ROOT in temp folder
+ tmpRoot = File.createTempFile("TmpTestRoot" + new Random().nextInt(),
null);
+ tmpRoot.delete();
+ tmpRoot.mkdir();
+ servletContext.setDocumentRoot(tmpRoot);
+
+ URL jarUrl =
AbstractAjax4JsfTestCase.class.getProtectionDomain().getCodeSource().getLocation();
+ ZipInputStream zis = new ZipInputStream(jarUrl.openStream());
+ try {
+ ZipEntry entry;
+ byte[] buffer = new byte[8192];
+ while ((entry = zis.getNextEntry()) != null) {
+ String name = entry.getName();
+ if (name.startsWith("WEB-INF/")) {
+ File out = new File(tmpRoot, name);
+ if (entry.isDirectory()) {
+ out.mkdirs();
+ } else {
+ out.getParentFile().mkdirs();
+ OutputStream os = new FileOutputStream(out);
+ try {
+ int count;
+ while ((count = zis.read(buffer)) > 0) {
+ os.write(buffer, 0, count);
+ }
+ } finally {
+ os.close();
+ }
+ zis.closeEntry();
+ }
+ }
+ }
+ } catch (IOException e) {
+ deleteRecursively(tmpRoot);
+ throw e;
+ }
+ servletContext.setDocumentRoot(tmpRoot);
+ }
+
try {
InternetResourceBuilder.getInstance().init(servletContext, "A4J");
} catch (ServletException e) {
@@ -125,7 +175,27 @@
webClient.setThrowExceptionOnScriptError(false);
}
+ private void deleteRecursively(File file) {
+ if (file != null) {
+ String[] list = file.list();
+ if (list != null) {
+ for (int i = 0; i < list.length; i++) {
+ String name = list[i];
+ File f = new File(file, name);
+ if (f.isDirectory()) {
+ deleteRecursively(f);
+ } else {
+ f.delete();
+ }
+ }
+ }
+
+ file.delete();
+ }
+ }
+
+
/* (non-Javadoc)
* @see org.apache.shale.test.base.AbstractJsfTestCase#tearDown()
*/
@@ -133,13 +203,15 @@
// This method MUST BE OVERRIDEN in any subclasses - since Junit see for it in class
for call
super.tearDown();
vcpRenderKit = null;
-// Thread.currentThread().setContextClassLoader(threadContextClassLoader);
-// threadContextClassLoader = null;
- webClient = null;
- webConnection = null;
- writer = null;
+// Thread.currentThread().setContextClassLoader(threadContextClassLoader);
+// threadContextClassLoader = null;
+ webClient = null;
+ webConnection = null;
+ writer = null;
SkinFactory.reset();
- InternetResourceBuilder.setInstance(null);
+ InternetResourceBuilder.setInstance(null);
+
+ deleteRecursively(tmpRoot);
}
// Protected configurations URL's
@@ -159,7 +231,7 @@
protected URL[] getImplementationUrls() {
return new URL[0];
}
-
+
/**
* Create component with given render kit and unique Id from ViewRoot.
* @param type - component type.
@@ -209,14 +281,14 @@
comp.setId(facesContext.getViewRoot().createUniqueId());
return comp;
}
-
- /**
- * Render all children for given component.
- * @param component
- * @throws IOException
- */
- protected void renderChildren(FacesContext context,
+
+ /**
+ * Render all children for given component.
+ * @param component
+ * @throws IOException
+ */
+ protected void renderChildren(FacesContext context,
UIComponent component) throws IOException {
if (component.getChildCount() > 0) {
for (Iterator it = component.getChildren().iterator(); it.hasNext();) {
@@ -233,7 +305,7 @@
* @throws IOException
*/
protected void renderChild(FacesContext context, UIComponent child)
- throws IOException {
+ throws IOException {
if (!child.isRendered()) {
return;
}
@@ -246,7 +318,7 @@
}
child.encodeEnd(context);
}
-
+
/**
* Render test view and parse to htmlunit page structure.
* @return
@@ -280,7 +352,7 @@
facesContext.setResponseWriter(writer);
writer.startDocument();
}
-
+
/**
* Parse collected content of mock response to Page instance, used for check rendered
html.
* @return
@@ -298,9 +370,9 @@
}
return webClient.getPage(page);
}
-
+
protected AjaxContext ajaxContext = null;
-
+
/**
* Initialised instance of VCP render kit.
*/
@@ -311,21 +383,21 @@
*/
protected MockResponseWriter writer;
- /**
- * <p>The htmlunit web client for this test case. </p>
- *
- */
- protected WebClient webClient = null;
-
- /**
- * Mock web connection for accept stored content of JSF encoding. For testing
JavaScript code, all URL's for scripts must be
- * rregistered by {@link MockWebConnection#setResponse(java.net.URL, byte[], int,
java.lang.String, java.lang.String, java.util.List)} method
- * By default, for unregistered pages return 404 - not found.
- */
- protected MockWebConnection webConnection = null;
+ /**
+ * <p>The htmlunit web client for this test case. </p>
+ *
+ */
+ protected WebClient webClient = null;
+ /**
+ * Mock web connection for accept stored content of JSF encoding. For testing JavaScript
code, all URL's for scripts must be
+ * rregistered by {@link MockWebConnection#setResponse(java.net.URL, byte[], int,
java.lang.String, java.lang.String, java.util.List)} method
+ * By default, for unregistered pages return 404 - not found.
+ */
+ protected MockWebConnection webConnection = null;
- // Thread context class loader saved and restored after each test
- private ClassLoader threadContextClassLoader = null;
-
+
+ // Thread context class loader saved and restored after each test
+ private ClassLoader threadContextClassLoader = null;
+
}