[Yanel-commits] rev 20446 - in public/yanel/trunk/src: build resources/xml/src resources/xml/src/test resources/xml/src/test/htmlunit resources/xml/src/test/htmlunit/org resources/xml/src/test/htmlunit/org/wyona resources/xml/src/test/htmlunit/org/wyona/yanel resources/xml/src/test/htmlunit/org/wyona/yanel/impl resources/xml/src/test/htmlunit/org/wyona/yanel/impl/resources resources/xml/src/test/junit resources/xml/src/test/junit/org resources/xml/src/test/junit/org/wyona resources/xml/src/test/junit/org/wyona/yanel resources/xml/src/test/junit/org/wyona/yanel/impl resources/xml/src/test/junit/org/wyona/yanel/impl/resources test test/htmlunit test/htmlunit/org test/htmlunit/org/wyona test/htmlunit/org/wyona/yanel test/htmlunit/org/wyona/yanel/htmlunit test/htmlunit/org/wyona/yanel/htmlunit/yanelwebsite test/junit test/junit/org test/junit/org/wyona test/junit/org/wyona/yanel test/junit/org/wyona/yanel/impl test/junit/org/wyona/yanel/impl/map test/junit/org/wyona/yanel/junit

josias at wyona.com josias at wyona.com
Wed Nov 29 19:14:12 CET 2006


Author: josias
Date: 2006-11-29 19:14:06 +0100 (Wed, 29 Nov 2006)
New Revision: 20446

Added:
   public/yanel/trunk/src/resources/xml/src/test/
   public/yanel/trunk/src/resources/xml/src/test/htmlunit/
   public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/
   public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/
   public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/
   public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/impl/
   public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/impl/resources/
   public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/impl/resources/XMLResourceWebTest.java
   public/yanel/trunk/src/resources/xml/src/test/junit/
   public/yanel/trunk/src/resources/xml/src/test/junit/org/
   public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/
   public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/
   public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/impl/
   public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/impl/resources/
   public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/impl/resources/XMLResourceTest.java
   public/yanel/trunk/src/test/README.txt
   public/yanel/trunk/src/test/build.xml
   public/yanel/trunk/src/test/htmlunit/
   public/yanel/trunk/src/test/htmlunit/htmlunit-properties.xml
   public/yanel/trunk/src/test/htmlunit/org/
   public/yanel/trunk/src/test/htmlunit/org/wyona/
   public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/
   public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/
   public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/AbstractHtmlUnitTest.java
   public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/yanelwebsite/
   public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/yanelwebsite/SimpleWebTest.java
   public/yanel/trunk/src/test/junit/
   public/yanel/trunk/src/test/junit/junit-properties.xml
   public/yanel/trunk/src/test/junit/org/
   public/yanel/trunk/src/test/junit/org/wyona/
   public/yanel/trunk/src/test/junit/org/wyona/yanel/
   public/yanel/trunk/src/test/junit/org/wyona/yanel/impl/
   public/yanel/trunk/src/test/junit/org/wyona/yanel/impl/map/
   public/yanel/trunk/src/test/junit/org/wyona/yanel/impl/map/MapImplTest.java
   public/yanel/trunk/src/test/junit/org/wyona/yanel/junit/
   public/yanel/trunk/src/test/junit/org/wyona/yanel/junit/AbstractYanelTest.java
   public/yanel/trunk/src/test/test-resources.xsl
Modified:
   public/yanel/trunk/src/build/build.xml
Log:
added initial version of a testing framework. see bug #4943

Modified: public/yanel/trunk/src/build/build.xml
===================================================================
--- public/yanel/trunk/src/build/build.xml	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/build/build.xml	2006-11-29 18:14:06 UTC (rev 20446)
@@ -766,5 +766,9 @@
       <param name="servlet.context.prefix" expression="${servlet.context.prefix}"/>
     </xslt>
   </target>
+  
+  <target name="test" description="Run tests">
+    <ant dir="../test" target="test" inheritall="false"/>
+  </target>
 
 </project>

Added: public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/impl/resources/XMLResourceWebTest.java
===================================================================
--- public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/impl/resources/XMLResourceWebTest.java	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/impl/resources/XMLResourceWebTest.java	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.impl.resources;
+
+import org.wyona.yanel.htmlunit.AbstractHtmlUnitTest;
+
+/**
+ * Test loading a page which is an xml resource and verifies the title and 
+ * page content.
+ */
+public class XMLResourceWebTest extends AbstractHtmlUnitTest {
+
+    protected void setUp() throws Exception {
+        this.testName = "XML Resource Web Test";
+        super.setUp();
+    }
+
+    /**
+     * Loads a page and verifies its title.
+     */
+    public void testXMLResource() throws Exception {
+        loadHtmlPage("yanel-website/en/about.html");
+        assertTitleEquals("About - Yanel");
+        assertPageContainsText("Content Management");
+    }
+
+}


Property changes on: public/yanel/trunk/src/resources/xml/src/test/htmlunit/org/wyona/yanel/impl/resources/XMLResourceWebTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/impl/resources/XMLResourceTest.java
===================================================================
--- public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/impl/resources/XMLResourceTest.java	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/impl/resources/XMLResourceTest.java	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.impl.resources;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+
+import org.wyona.yanel.core.Path;
+import org.wyona.yanel.junit.AbstractYanelTest;
+
+/**
+ * Test for the XML Resource.
+ */
+public class XMLResourceTest extends AbstractYanelTest {
+
+    protected XMLResource resource;
+
+    protected Path path;
+
+    public void setUp() throws Exception {
+        super.setUp();
+        this.testName = "Test for the XML Resource";
+        this.resource = new XMLResource();
+        this.path = new Path("/yanel-website/test-xml-resource.html");
+        this.resource.setYanel(this.yanel);
+
+    }
+
+    public void testWriteRead() throws Exception {
+
+        // Write resource
+        OutputStream os = this.resource.getOutputStream(path);
+        assertNotNull("getOutputStream() should not return null for path: " + path, os);
+        Writer writer = new OutputStreamWriter(os, "UTF-8");
+        String testContent = "Hello World! " + System.currentTimeMillis();
+
+        writer.write(testContent);
+        writer.close();
+
+        // Read resource
+        InputStream is = this.resource.getInputStream(path);
+        assertNotNull("getInputStream() should not return null for path: " + path, is);
+        Reader reader = new InputStreamReader(is, "UTF-8");
+        BufferedReader br = new BufferedReader(reader);
+        String line = br.readLine();
+        br.close();
+        reader.close();
+
+        assertEquals(line, testContent);
+
+    }
+
+}


Property changes on: public/yanel/trunk/src/resources/xml/src/test/junit/org/wyona/yanel/impl/resources/XMLResourceTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/README.txt
===================================================================
--- public/yanel/trunk/src/test/README.txt	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/README.txt	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,54 @@
+Testing Framework
+=================
+
+General information
+-------------------
+
+There are two kind of tests:
+junit:    used to test specific functionality of certain java components/classes
+          see http://junit.org/
+ 
+htmlunit: used to test the web pages (simulating user interaction)
+          see http://htmlunit.sourceforge.net/
+
+
+How to run the tests
+--------------------
+
+It's necessary to build yanel before running the tests.
+If you want to run the htmlunit tests, you have to start tomcat and configure 
+the host and url in $YANEL_HOME/src/test/htmlunit/default-properties.xml.
+
+Then execute in $YANEL_HOME:
+./build.sh test
+
+To run single tests standalone:
+cd $YANEL_HOME/src/test
+ant junit -Dtest.class.name=org.wyona.yanel.impl.map.MapImplTest
+ant htmlunit -Dtest.class.name=org.wyona.yanel.htmlunit.yanelwebsite.SimpleWebTest
+
+
+How to write tests
+------------------
+
+Where to place core tests:
+$YANEL_HOME/src/test/junit/
+$YANEL_HOME/src/test/htmlunit/
+
+Where to place resource tests:
+$YANEL_HOME/src/resources/MYRESOURCE/src/test/junit/
+$YANEL_HOME/src/resources/MYRESOURCE/src/test/htmlunit/
+
+Choose the name of the class according to the following convention:
+
+*Test.java
+
+e.g. MyResourceTest.java
+
+The tests which adhere to these conventions will be executed automatically by 
+the testing framework.
+
+Have a look at the existing tests to see how it works, e.g. in the xml resource.
+You can find more example htmlunit tests at:
+http://svn.wyona.com/repos/public/lenya/htmlunit/
+


Property changes on: public/yanel/trunk/src/test/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/build.xml
===================================================================
--- public/yanel/trunk/src/test/build.xml	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/build.xml	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,147 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project name="test" default="test" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
+
+  <property file="../build/local.build.properties"/>
+  <property file="../build/build.properties"/>
+
+  <property name="junit.src.dir" value="junit"/>
+  <property name="htmlunit.src.dir" value="htmlunit"/>
+  <property name="test.results.dir" value="test-results"/>
+  <property name="conf.dir" value="../../conf"/>
+  <property name="build.dir" value="../../build"/>
+  <property name="yanel.build.dir" value="${build.dir}/classes"/>
+  <property name="junit.build.dir" value="${build.dir}/test/junit"/>
+  <property name="htmlunit.build.dir" value="${build.dir}/test/htmlunit"/>
+  <property name="webapp.classes.dir" value="${build.dir}/webapps/yanel/WEB-INF/classes"/>
+  <property name="webapp.lib.dir" value="${build.dir}/webapps/yanel/WEB-INF/lib"/>
+
+  <property name="yanel.source.version" value="${yanel.version}-r${yanel.revision}"/>
+  
+  <target name="init" depends="dependencies">
+    <path id="classpath">
+      <pathelement path="${maven2.cp}"/>
+      <pathelement location="${junit.build.dir}"/>
+      <pathelement location="${htmlunit.build.dir}"/>
+      <pathelement location="${webapp.classes.dir}"/>
+      <fileset dir="${webapp.lib.dir}">
+        <include name="**.jar"/>
+      </fileset>
+    </path>
+    <!-- <property name="cp" refid="classpath"/>
+    <echo>classpath: ${cp}</echo> -->
+  </target>
+  
+  <target name="dependencies" description="Dependencies">
+    <!-- INFO: http://maven.apache.org/ant-tasks.html -->
+    <artifact:remoteRepository id="wyona.remote.repository" url="http://www.wyona.org/maven2/"/>
+
+    <artifact:dependencies pathId="maven2.classpath" filesetId="maven2.fileset">
+      <remoteRepository refid="wyona.remote.repository"/>
+      <dependency groupId="apache-jakarta-commons" artifactId="apache-jakarta-commons-codec" version="1.3"/>
+      <dependency groupId="apache-jakarta-commons" artifactId="apache-jakarta-commons-collections" version="3.2"/>
+      <dependency groupId="apache-jakarta-commons" artifactId="apache-jakarta-commons-configuration" version="1.1"/>
+      <dependency groupId="apache-jakarta-commons" artifactId="apache-jakarta-commons-httpclient" version="3.0.1"/>
+      <dependency groupId="apache-jakarta-commons" artifactId="apache-jakarta-commons-io" version="1.2"/>
+      <dependency groupId="apache-jakarta-commons" artifactId="apache-jakarta-commons-lang" version="2.1"/>
+      <dependency groupId="apache-jakarta-commons" artifactId="apache-jakarta-commons-logging" version="1.0.4"/>
+      <dependency groupId="htmlunit" artifactId="htmlunit" version="1.10"/>
+      <dependency groupId="jaxen" artifactId="jaxen" version="1.1-beta-10"/>
+      <dependency groupId="rhino" artifactId="rhino" version="1.6R2"/>
+      <dependency groupId="nekohtml" artifactId="nekohtml" version="0.9.5"/>
+    </artifact:dependencies>
+
+    <property name="maven2.cp" refid="maven2.classpath"/>
+  </target>
+ 
+  <target name="build" depends="init" description="Build tests">
+    <mkdir dir="${build.dir}"/>
+    <mkdir dir="${junit.build.dir}"/>
+    <mkdir dir="${htmlunit.build.dir}"/>
+    <javac srcdir="${junit.src.dir}" destdir="${junit.build.dir}">
+      <classpath refid="classpath" />
+    </javac>
+    <javac srcdir="${htmlunit.src.dir}" destdir="${htmlunit.build.dir}">
+      <classpath refid="classpath" />
+    </javac>
+    <antcall target="build-resources"/>
+  </target>
+
+  <target name="build-resources" depends="init" description="Build tests of resources">
+    <condition property="local.yanel.config.exists">
+      <available file="${conf.dir}/conf/local.yanel.xml" type="file"/>
+    </condition>
+    <antcall target="generate-resources-test-build-from-default"/>
+    <antcall target="generate-resources-test-build-from-local"/>
+    <antcall target="copy-properties"/>
+    <ant antfile="${build.dir}/test-resources.xml" target="build-test-resources" inheritRefs="true"/>
+  </target>
+  
+  <target name="generate-resources-test-build-from-default" depends="init" unless="local.yanel.config.exists">
+    <xslt in="${conf.dir}/yanel.xml" out="${build.dir}/test-resources.xml" style="test-resources.xsl" force="false"/>
+  </target>
+
+  <target name="generate-resources-test-build-from-local" depends="init" if="local.yanel.config.exists">
+    <xslt in="${conf.dir}/conf/local.yanel.xml" out="${build.dir}/test-resources.xml" style="test-resources.xsl" force="true"/>
+  </target>
+  
+  <target name="copy-properties">
+    <copy todir="${junit.build.dir}">
+      <fileset dir="${junit.src.dir}">
+        <include name="*.xml"/>
+      </fileset>
+    </copy>
+    <copy todir="${htmlunit.build.dir}">
+      <fileset dir="${htmlunit.src.dir}">
+        <include name="*.xml"/>
+      </fileset>
+    </copy>
+  </target>
+
+  <target name="clean" description="Clean the build">
+    <echo>Clean ${junit.build.dir}</echo>
+    <delete dir="${junit.build.dir}"/>
+    <echo>Clean ${htmlunit.build.dir}</echo>
+    <delete dir="${htmlunit.build.dir}"/>
+    <echo>Remove ${build.dir}/test-resources.xml</echo>
+    <delete file="${build.dir}/test-resources.xml"/>
+  </target>
+
+  <target name="test" depends="build" description="Run all tests">
+    <mkdir dir="${test.results.dir}" />
+    <echo>Running the tests</echo>
+    <echo>NOTE: Running the tests might take a while. Thanks for your patience ...</echo>
+    <antcall target="junit"/>
+    <antcall target="htmlunit"/>
+  </target>
+  
+  <target name="junit" depends="build" description="Run junit tests">
+    <echo>Running the junit tests</echo>
+    <junit fork="yes" printsummary="yes" haltonfailure="no" failureproperty="junit.tests.failed">
+      <batchtest todir="${test.results.dir}" unless="test.class.name">
+        <fileset dir="${junit.build.dir}" includes="**/*Test.class" excludes="**/Abstract*.class"/>
+      </batchtest>
+      <test todir="${test.results.dir}" name="${test.class.name}" if="test.class.name"/>
+      <formatter type="plain" usefile="false"/>
+      <formatter type="xml"/>
+      <classpath refid="classpath" />
+      <jvmarg value="-Djunit.base.dir=${basedir}/junit"/>
+    </junit>
+    <fail if="junit.tests.failed"  message="One or more junit tests failed."/>
+  </target>
+
+  <target name="htmlunit" depends="build" description="Run htmlunit tests">
+    <echo>Running the htmlunit tests</echo>
+    <junit fork="yes" printsummary="yes" haltonfailure="no" failureproperty="htmlunit.tests.failed">
+      <batchtest todir="${test.results.dir}" unless="test.class.name">
+        <fileset dir="${htmlunit.build.dir}" includes="**/*Test.class" excludes="**/Abstract*.class"/>
+      </batchtest>
+      <formatter type="plain" usefile="false"/>
+      <formatter type="xml"/>
+      <classpath refid="classpath" />
+      <jvmarg value="-Dhtmlunit.base.dir=${basedir}/htmlunit"/>
+    </junit>
+    <fail if="htmlunit.tests.failed"  message="One or more htmlunit tests failed."/>
+  </target>
+
+</project>


Property changes on: public/yanel/trunk/src/test/build.xml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/htmlunit/htmlunit-properties.xml
===================================================================
--- public/yanel/trunk/src/test/htmlunit/htmlunit-properties.xml	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/htmlunit/htmlunit-properties.xml	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+<config>
+  <htmlunit>
+    <!--
+      Logging:
+       1. trace (the least serious)
+       2. debug
+       3. info
+       4. warn
+       5. error
+       6. fatal (the most serious)
+    -->
+    <debugLevel>4</debugLevel>
+    <baseUrl>http://localhost:8080/yanel/</baseUrl>
+    <language>en-us</language>
+  </htmlunit>
+</config>
\ No newline at end of file


Property changes on: public/yanel/trunk/src/test/htmlunit/htmlunit-properties.xml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/AbstractHtmlUnitTest.java
===================================================================
--- public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/AbstractHtmlUnitTest.java	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/AbstractHtmlUnitTest.java	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,301 @@
+/*
+ * Copyright 2005 The Apache Software Foundation Licensed 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.wyona.yanel.htmlunit;
+
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration.ConfigurationFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.impl.SimpleLog;
+
+import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
+import com.gargoylesoftware.htmlunit.Page;
+import com.gargoylesoftware.htmlunit.ThreadedRefreshHandler;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebResponse;
+import com.gargoylesoftware.htmlunit.html.ClickableElement;
+import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import com.gargoylesoftware.htmlunit.html.HtmlForm;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
+
+/**
+ * Base class to run HtmlUnit tests with JUnit.
+ * 
+ * Example:
+ * 
+ * <pre>
+ * public void testHomePage() throws Exception {
+ * 
+ *     loadHtmlPage(&quot;index.html&quot;); // base url set in properties.xml
+ *     assertTitleEquals(&quot;Welcome To The Index Page&quot;);
+ * 
+ *     clickLinkWithText(&quot;Contact&quot;);
+ *     assertTitleContains(&quot;Contact&quot;);
+ * }
+ * 
+ * </pre>
+ * 
+ * <p>
+ * You also have full access to the htmlunit features (eg, the current page is
+ * stored in the variable currentPage).
+ * </p>
+ * 
+ * <pre>
+ * public void testHomePage() throws Exception {
+ *     final WebClient webClient = new WebClient();
+ *     final URL url = new URL(&quot;http://localhost:8080/index.html&quot;);
+ *     final HtmlPage page = (HtmlPage) webClient.getPage(url);
+ *     assertEquals(&quot;Welcome To The Index Page&quot;, page.getTitleText());
+ * }
+ * </pre>
+ */
+public abstract class AbstractHtmlUnitTest extends TestCase {
+
+    /** configuration */
+    protected Configuration config;
+
+    /**
+     * Logger for informative output by test cases. The output level can be
+     * specified in properties.xml
+     */
+    protected Log logger;
+
+    /**
+     * Base URL of the running Cocoon server which is to be tested. Set by
+     * property htmlunit.test.baseurl.
+     */
+    protected String baseURL;
+
+    /**
+     * Low-level access to WebClient object.
+     */
+    protected WebClient webClient;
+
+    /**
+     * Low-level access to WebResponse object.
+     */
+    protected WebResponse response;
+
+    /**
+     * Holds the current HtmlPage which was loaded e.g. with loadHtmlPage().
+     */
+    protected HtmlPage currentPage;
+
+    /**
+     * The name of the testcase, used for debug output.
+     */
+    protected String testName;
+
+    protected File baseDir;
+
+    protected List collectedAlerts = new ArrayList();
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see junit.framework.TestCase#setUp()
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // read the config file
+        URL configURL = this.getClass().getClassLoader().getResource("htmlunit-properties.xml");
+        config = new XMLConfiguration(configURL);
+
+        SimpleLog log = new SimpleLog("htmlunit log");
+        log.setLevel(Integer.parseInt(this.config.getString("htmlunit.debugLevel")));
+        this.logger = log;
+        this.baseURL = this.config.getString("htmlunit.baseUrl");
+        // this.webClient = new WebClient(BrowserVersion.MOZILLA_1_0);
+        // this.webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6_0);
+        this.webClient = new WebClient();
+        this.webClient.addRequestHeader("Accept-Language", this.config
+                .getString("htmlunit.language"));
+        this.webClient.setRedirectEnabled(true);
+        this.webClient.setAlertHandler(new CollectingAlertHandler(this.collectedAlerts));
+
+        // disable javascript because it lead to problems with complex scripts.
+        this.webClient.setJavaScriptEnabled(false);
+        this.webClient.setRefreshHandler(new ThreadedRefreshHandler());
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see junit.framework.TestCase#runTest()
+     */
+    protected void runTest() throws Throwable {
+        this.logger.info("-------- Begin testcase [" + this.testName + "] ---------");
+        this.logger.info("On Host [" + this.config.getString("htmlunit.baseUrl") + "]");
+        try {
+            super.runTest();
+        } catch (Throwable t) {
+            this.logger.error("Exception in testcase [" + this.testName + "]", t);
+            if (this.currentPage != null) {
+                WebResponse response = this.currentPage.getWebResponse();
+                this.logger.error("Response URL: " + response.getUrl());
+                this.logger.error("Response status code: " + response.getStatusCode());
+                this.logger.error("Response status message: " + response.getStatusMessage());
+                this.logger.error("Response content type: " + response.getContentType());
+                this.logger.error("Response content charset: " + response.getContentCharSet());
+                this.logger.error("Response load time in ms: "
+                        + response.getLoadTimeInMilliSeconds());
+                this.logger.info(this.currentPage.asXml());
+            }
+            // print alerts:
+            if (this.collectedAlerts.size() > 0) {
+                this.logger.error("Alerts:");
+                for (int i = 0; i < this.collectedAlerts.size(); i++) {
+                    this.logger.error(this.collectedAlerts.get(i).toString());
+                }
+            }
+            throw t;
+        }
+        this.logger.info("-------- End testcase [" + this.testName + "] ---------");
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see junit.framework.TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception {
+        this.response = null;
+        this.currentPage = null;
+        super.tearDown();
+    }
+
+    /**
+     * Sends an HTTP request and handles the response as HTML document.
+     * 
+     * @param pageURL
+     * @throws Exception
+     */
+    protected void loadHtmlPage(String pageURL) throws Exception {
+        URL url = new URL(baseURL + pageURL);
+        this.logger.info("Attempting to load page " + url);
+        Page page = webClient.getPage(url);
+        this.currentPage = handlePage(page);
+    }
+
+    /**
+     * Click on an element like a button or an anchor (link).
+     * 
+     * @param element
+     * @throws Exception
+     */
+    protected void click(ClickableElement element) throws Exception {
+        Page page = element.click();
+        this.currentPage = handlePage(page);
+    }
+
+    /**
+     * Clicks the given button of the first form in the current page.
+     * 
+     * @param name
+     *            name attribute of the button
+     * @throws Exception
+     */
+    protected void clickButton(String name) throws Exception {
+        HtmlForm form = (HtmlForm) this.currentPage.getForms().get(0);
+        HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName(name);
+        click(button);
+    }
+
+    /**
+     * Performs a few tests on a page, then attempts to convert the page to an
+     * html page and returns it.
+     * 
+     * @param page
+     * @return
+     * @throws Exception
+     */
+    protected HtmlPage handlePage(Page page) throws Exception {
+        this.response = page.getWebResponse();
+
+        assertNotNull("Response contains invalid HTML", page);
+        assertTrue("Response should be an HTML page", page instanceof HtmlPage);
+        // assertTrue("Response status not 200",
+        // this.response.getStatusCode() == 200);
+
+        if (this.logger.isDebugEnabled()) {
+            this.logger.debug(((HtmlPage) page).asXml());
+        }
+        return (HtmlPage) page;
+    }
+
+    /**
+     * Simulates a click. A link with the provided text must be present on the
+     * page
+     */
+    protected void clickLinkWithText(String linkText) throws Exception {
+        HtmlAnchor link = currentPage.getFirstAnchorByText(linkText);
+        click(link);
+    }
+
+    /**
+     * Simulates a click. The path must be present on the page
+     */
+    protected void clickLink(String linkPath) throws Exception {
+
+        final HtmlElement documentElement = currentPage.getDocumentElement();
+        final List links = documentElement.getHtmlElementsByAttribute("a", "href", linkPath);
+        HtmlAnchor link = (HtmlAnchor) links.get(0);
+        click(link);
+    }
+
+    /**
+     * Asserts that the title of the current page contains the given string.
+     * 
+     * @param titleExtract
+     */
+    protected void assertTitleContains(String titleExtract) {
+        String title = currentPage.getTitleText();
+        assertFalse("provided text \"" + titleExtract + "\" not found in title \"" + title + "\"",
+                title.indexOf(titleExtract) == -1);
+    }
+
+    /**
+     * Asserts that the title of the current page is equal to the given string.
+     * 
+     * @param expectedTitle
+     */
+    protected void assertTitleEquals(String expectedTitle) {
+        String title = currentPage.getTitleText();
+        assertEquals("provided title \"" + expectedTitle + "\" doesn't match page title \"" + title
+                + "\"", title, expectedTitle);
+    }
+
+    /**
+     * Asserts that the page contains the given string.
+     * 
+     * @param expectedContent
+     */
+    protected void assertPageContainsText(String expectedContent) {
+        String page = currentPage.asText();
+        assertFalse("provided content \"" + expectedContent + "\" not found on this page", page
+                .indexOf(expectedContent) == -1);
+    }
+
+}


Property changes on: public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/AbstractHtmlUnitTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/yanelwebsite/SimpleWebTest.java
===================================================================
--- public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/yanelwebsite/SimpleWebTest.java	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/yanelwebsite/SimpleWebTest.java	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.htmlunit.yanelwebsite;
+
+import org.wyona.yanel.htmlunit.AbstractHtmlUnitTest;
+
+/**
+ * Simple test which loads a yanel page and verifies the title.
+ */
+public class SimpleWebTest extends AbstractHtmlUnitTest {
+
+    protected void setUp() throws Exception {
+        this.testName = "Simple Yanel Web Test";
+        super.setUp();
+    }
+
+    /**
+     * Loads a yanel page and verifies its title.
+     */
+    public void testYanelWebsite() throws Exception {
+        loadHtmlPage(""); // root page
+        assertTitleEquals("Welcome to Yanel");
+        assertPageContainsText("Welcome to Yanel");
+    }
+
+}


Property changes on: public/yanel/trunk/src/test/htmlunit/org/wyona/yanel/htmlunit/yanelwebsite/SimpleWebTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/junit/junit-properties.xml
===================================================================
--- public/yanel/trunk/src/test/junit/junit-properties.xml	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/junit/junit-properties.xml	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<config>
+  <junit>
+    <!--
+      Logging:
+       1. trace (the least serious)
+       2. debug
+       3. info
+       4. warn
+       5. error
+       6. fatal (the most serious)
+    -->
+    <debugLevel>4</debugLevel>
+  </junit>
+</config>
\ No newline at end of file


Property changes on: public/yanel/trunk/src/test/junit/junit-properties.xml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/junit/org/wyona/yanel/impl/map/MapImplTest.java
===================================================================
--- public/yanel/trunk/src/test/junit/org/wyona/yanel/impl/map/MapImplTest.java	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/junit/org/wyona/yanel/impl/map/MapImplTest.java	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.impl.map;
+
+import org.wyona.yanel.core.Path;
+import org.wyona.yanel.core.map.Map;
+import org.wyona.yanel.junit.AbstractYanelTest;
+
+/**
+ * Simple test for the map implementation.
+ */
+public class MapImplTest extends AbstractYanelTest {
+
+    protected Map map;
+
+    public void setUp() throws Exception {
+        super.setUp();
+        this.testName = "Test for the Map Implementation";
+        this.map = new MapImpl();
+    }
+
+    /**
+     * Tests if the map returns the correct rti for a given path.
+     */
+    public void testRTI() throws Exception {
+        Path path = new Path("/yanel-website/en/about.html");
+        String rti = map.getResourceTypeIdentifier(path);
+        assertEquals("Incorrect RTI for path: " + path.toString(),
+                "<{http://www.wyona.org/yanel/resource/1.0}xml/>", rti);
+    }
+
+}


Property changes on: public/yanel/trunk/src/test/junit/org/wyona/yanel/impl/map/MapImplTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/junit/org/wyona/yanel/junit/AbstractYanelTest.java
===================================================================
--- public/yanel/trunk/src/test/junit/org/wyona/yanel/junit/AbstractYanelTest.java	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/junit/org/wyona/yanel/junit/AbstractYanelTest.java	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.junit;
+
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.wyona.yanel.core.Yanel;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration.ConfigurationFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.impl.SimpleLog;
+
+/**
+ * Base class for yanel junit tests.
+ */
+public abstract class AbstractYanelTest extends TestCase {
+
+    /** configuration */
+    protected Configuration config;
+
+    /**
+     * Logger for informative output by test cases. The output level can be
+     * specified in properties.xml
+     */
+    protected Log logger;
+
+    /**
+     * The name of the testcase, used for debug output.
+     */
+    protected String testName;
+
+    protected File baseDir;
+
+    protected Yanel yanel;
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see junit.framework.TestCase#setUp()
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // read the config file
+        URL configURL = this.getClass().getClassLoader().getResource("junit-properties.xml");
+
+        config = new XMLConfiguration(configURL);
+
+        SimpleLog log = new SimpleLog("junit log");
+        log.setLevel(Integer.parseInt(this.config.getString("junit.debugLevel")));
+        this.logger = log;
+
+        this.yanel = Yanel.getInstance();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see junit.framework.TestCase#runTest()
+     */
+    protected void runTest() throws Throwable {
+        this.logger.info("-------- Begin testcase [" + this.testName + "] ---------");
+        try {
+            super.runTest();
+        } catch (Throwable t) {
+            this.logger.error("Exception in testcase [" + this.testName + "]", t);
+            throw t;
+        } finally {
+            this.logger.info("-------- End testcase [" + this.testName + "] ---------");
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see junit.framework.TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+}


Property changes on: public/yanel/trunk/src/test/junit/org/wyona/yanel/junit/AbstractYanelTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/test/test-resources.xsl
===================================================================
--- public/yanel/trunk/src/test/test-resources.xsl	2006-11-29 18:11:05 UTC (rev 20445)
+++ public/yanel/trunk/src/test/test-resources.xsl	2006-11-29 18:14:06 UTC (rev 20446)
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+
+<xsl:stylesheet version="1.0"
+    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:yanel="http://www.wyona.org/yanel/1.0">
+
+  <xsl:output method="xml" indent="yes"/>
+
+  <xsl:template match="/">
+
+    <xsl:comment> NOTE: This file has been generated automatically from conf/yanel.xml! </xsl:comment>
+    <project name="test-resources">
+
+      <target name="init">
+        <dirname property="build.dir" file="${{ant.file}}"/>
+      </target>
+
+      <target name="build-test-resources" description="Build resources" depends="init">
+
+        <javac destdir="${{junit.build.dir}}">
+          <classpath refid="classpath" />
+          <include name="src/test/junit/**/*.java"/>
+          <xsl:apply-templates select="/yanel:yanel/yanel:resources/yanel:resource"/>
+        </javac>
+        
+        <javac destdir="${{htmlunit.build.dir}}">
+          <classpath refid="classpath" />
+          <include name="src/test/htmlunit/**/*.java"/>
+          <xsl:apply-templates select="/yanel:yanel/yanel:resources/yanel:resource"/>
+        </javac>
+        
+      </target>
+    </project>
+  </xsl:template>
+  
+  <xsl:template match="yanel:resource[@compile='true']">
+    <xsl:variable name="srcpath">
+      <xsl:choose>
+        <xsl:when test="starts-with(@src, '/')">
+          <xsl:value-of select="@src"/>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:value-of select="concat('${build.dir}/', @src)"/>
+        </xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+    <src path="{$srcpath}"/>
+  </xsl:template>
+  
+  <xsl:template match="yanel:resource[@compile='false']">
+  </xsl:template>
+
+</xsl:stylesheet>


Property changes on: public/yanel/trunk/src/test/test-resources.xsl
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the Yanel-commits mailing list