Author: vmassol
Date: 2007-09-02 13:14:03 +0200 (Sun, 02 Sep 2007)
New Revision: 4696

Added:
   xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/
   xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/pom.xml
   xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/
   xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/
   xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/
   xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/
   
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/
   
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/
   
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/
   
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/AntBuildListener.java
   
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java
Log:
XTOOLS-8: Add new shared-test module for test framework classes shared between 
functional modules

Added: xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/pom.xml
===================================================================
--- xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/pom.xml    
2007-09-02 11:13:18 UTC (rev 4695)
+++ xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/pom.xml    
2007-09-02 11:14:03 UTC (rev 4696)
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ *
+ * Copyright 2006-2007, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>com.xpn.xwiki.platform</groupId>
+    <artifactId>xwiki-build-tools</artifactId>
+    <version>1.4-SNAPSHOT</version>
+  </parent>
+  <artifactId>xwiki-shared-test</artifactId>
+  <name>XWiki Platform - Tools - Build - Shared Test</name>
+  <packaging>jar</packaging>
+  <description>Test framework classes shared between functional test 
modules</description>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.2</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.ant</groupId>
+      <artifactId>ant</artifactId>
+      <version>1.7.0</version>
+    </dependency>
+  </dependencies>
+</project>

Added: 
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/AntBuildListener.java
===================================================================
--- 
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/AntBuildListener.java
     2007-09-02 11:13:18 UTC (rev 4695)
+++ 
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/AntBuildListener.java
     2007-09-02 11:14:03 UTC (rev 4696)
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2007, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ */
+package com.xpn.xwiki.test;
+
+import org.apache.tools.ant.BuildEvent;
+import org.apache.tools.ant.BuildListener;
+import org.apache.tools.ant.Project;
+
+public class AntBuildListener implements BuildListener
+{
+    private boolean isDebugModeOn;
+
+    public AntBuildListener(boolean isDebugModeOn)
+    {
+        this.isDebugModeOn = isDebugModeOn;
+    }
+
+    public void buildStarted(BuildEvent event) {
+        // Voluntarily do nothing
+    }
+
+    public void buildFinished(BuildEvent event) {
+        // Voluntarily do nothing
+    }
+
+    public void targetStarted(BuildEvent event) {
+        // Voluntarily do nothing
+    }
+
+    public void targetFinished(BuildEvent event) {
+        // Voluntarily do nothing
+    }
+
+    public void taskStarted(BuildEvent event) {
+        // Voluntarily do nothing
+    }
+
+    public void taskFinished(BuildEvent event) {
+        // Voluntarily do nothing
+    }
+
+    public void messageLogged(BuildEvent event)
+    {
+        if ((event.getPriority() != Project.MSG_DEBUG) || isDebugModeOn) {
+            System.out.println(event.getMessage());
+        }
+    }
+}

Added: 
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java
===================================================================
--- 
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java
       2007-09-02 11:13:18 UTC (rev 4695)
+++ 
xwiki-platform/xwiki-tools/trunk/build-tools/shared-test/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java
       2007-09-02 11:14:03 UTC (rev 4696)
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2007, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package com.xpn.xwiki.test;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.types.Commandline;
+import org.apache.tools.ant.taskdefs.ExecTask;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * JUnit TestSetup extension that starts/stops XWiki using a script passed 
using System Properties.
+ * These properties are meant to be passed by the underlying build system. 
This class is meant
+ * to wrap a JUnit TestSuite. For example:
+ * <pre><code>
+ * public static Test suite()
+ * {
+ *     // Create some TestSuite object here
+ *     return new XWikiTestSetup(suite);
+ * }
+ * </code></pre>
+ *
+ * <p>Note: We could start XWiki using Java directly but we're using a script 
so that we can test
+ * the exact same script used by XWiki users who download the standalone 
distribution.</p>
+ *
+ * @version $Id: $
+ */
+public class XWikiTestSetup extends TestSetup
+{
+    private static final String EXECUTION_DIRECTORY = 
System.getProperty("xwikiExecutionDirectory");
+    private static final String START_COMMAND = 
System.getProperty("xwikiExecutionStartCommand");
+    private static final String STOP_COMMAND = 
System.getProperty("xwikiExecutionStopCommand");
+    private static final String PORT = System.getProperty("xwikiPort", "8080");
+    private static final boolean DEBUG = System.getProperty("debug", 
"false").equalsIgnoreCase("true");
+    private static final int TIMEOUT_SECONDS = 60;
+
+    private Project project;
+
+    public XWikiTestSetup(Test test)
+    {
+        super(test);
+
+        this.project = new Project();
+           this.project.init();
+        this.project.addBuildListener(new AntBuildListener(DEBUG));
+    }
+
+    protected void setUp() throws Exception
+    {
+        startXWikiInSeparateThread();
+        waitForXWikiToLoad();
+    }
+
+    private void startXWikiInSeparateThread()
+    {
+        Thread startThread = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    startXWiki();
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
+        startThread.start();
+    }
+
+    private void startXWiki() throws Exception
+    {
+        ExecTask execTask = (ExecTask) this.project.createTask("exec");
+        execTask.setDir(new File(EXECUTION_DIRECTORY));
+        Commandline commandLine = new Commandline(START_COMMAND);
+        execTask.setCommand(commandLine);
+        execTask.execute();
+    }
+
+    private Task createStopTask()
+    {
+        ExecTask execTask = (ExecTask) this.project.createTask("exec");
+        execTask.setDir(new File(EXECUTION_DIRECTORY));
+        Commandline commandLine = new Commandline(STOP_COMMAND);
+        execTask.setCommand(commandLine);
+        return execTask;
+    }
+
+    private void waitForXWikiToLoad() throws Exception
+    {
+        // Wait till the main page becomes available which means the server is 
started fine
+        System.out.println("Checking that XWiki is up and running...");
+        URL url = new URL("http://localhost:"; + PORT + 
"/xwiki/bin/view/Main/");
+        HttpURLConnection connection = (HttpURLConnection) 
url.openConnection();
+        boolean connected = false;
+        boolean timedOut = false;
+        long startTime = System.currentTimeMillis();
+        while (!connected && !timedOut) {
+            try {
+                connection.connect();
+                int responseCode = connection.getResponseCode();
+                if (DEBUG) {
+                    System.out.println("Result of pinging [" + url + "] = ["
+                        + responseCode + "], Message = ["
+                        + connection.getResponseMessage() + "]");
+                }
+                connected = (responseCode == 200);
+            } catch (IOException e) {
+                // Do nothing as it simply means the server is not ready yet...
+            }
+            Thread.sleep(100L);
+            timedOut = (System.currentTimeMillis() - startTime > 
TIMEOUT_SECONDS * 1000L);
+        }
+        if (timedOut) {
+            String message = "Failed to start XWiki in [" + TIMEOUT_SECONDS + 
"] seconds";
+            System.out.println(message);
+            tearDown();
+            throw new RuntimeException(message);
+        }
+    }
+
+    protected void tearDown() throws Exception
+    {
+        // Stop XWiki
+        createStopTask().execute();
+    }
+}

_______________________________________________
notifications mailing list
notifications@xwiki.org
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to