From 88216b3e6e6636b53d7d257018b02a13f5f38bb7 Mon Sep 17 00:00:00 2001
From: vmassol <vincent@massol.net>
Date: Sun, 2 Sep 2007 11:14:03 +0000
Subject: [PATCH] XTOOLS-8: Add new shared-test module for test framework
 classes shared between functional modules

git-svn-id: https://svn.xwiki.org/svnroot/xwiki/xwiki-platform/xwiki-tools/trunk/build-tools/shared-test@4696 f329d543-caf0-0310-9063-dda96c69346f
---
 xwiki-platform-tools/pom.xml                  |  37 +++--
 .../com/xpn/xwiki/test/AntBuildListener.java  |  66 ++++++++
 .../com/xpn/xwiki/test/XWikiTestSetup.java    | 146 ++++++++++++++++++
 3 files changed, 230 insertions(+), 19 deletions(-)
 create mode 100644 xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/AntBuildListener.java
 create mode 100644 xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java

diff --git a/xwiki-platform-tools/pom.xml b/xwiki-platform-tools/pom.xml
index a6a4b874206..de2aae906de 100644
--- a/xwiki-platform-tools/pom.xml
+++ b/xwiki-platform-tools/pom.xml
@@ -26,24 +26,23 @@
   <modelVersion>4.0.0</modelVersion>
   <parent>
     <groupId>com.xpn.xwiki.platform</groupId>
-    <artifactId>xwiki</artifactId>
-    <version>5</version>
-    <relativePath>../xwiki-platform-pom</relativePath>
+    <artifactId>xwiki-build-tools</artifactId>
+    <version>1.4-SNAPSHOT</version>
   </parent>
-  <artifactId>xwiki-tools</artifactId>
-  <name>XWiki Platform - Tools - Parent POM</name>
-  <version>3</version>
-  <packaging>pom</packaging>
-  <description>XWiki Tools</description>
-  <scm>
-    <connection>scm:svn:http://svnmirror.xwiki.org/svn/mirror/xwiki/xwiki-platform/xwiki-tools/tags/xwiki-tools-3</connection>
-    <developerConnection>scm:svn:svn+ssh://svn.forge.objectweb.org/svnroot/xwiki/xwiki-platform/xwiki-tools/tags/xwiki-tools-3</developerConnection>
-    <url>http://svn.forge.objectweb.org/cgi-bin/viewcvs.cgi/xwiki/xwiki-platform/xwiki-tools/tags/xwiki-tools-3</url>
-  </scm>
-  <modules>
-    <module>build-tools</module>
-    <module>packager</module>
-    <module>xwiki-configuration-resources</module>
-    <module>xwiki-jetty-resources</module>
-  </modules>
+  <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>
diff --git a/xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/AntBuildListener.java b/xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/AntBuildListener.java
new file mode 100644
index 00000000000..95d4eb6c0cf
--- /dev/null
+++ b/xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/AntBuildListener.java
@@ -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());
+        }
+    }
+}
diff --git a/xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java b/xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java
new file mode 100644
index 00000000000..82c700cc44f
--- /dev/null
+++ b/xwiki-platform-tools/src/main/java/com/xpn/xwiki/test/XWikiTestSetup.java
@@ -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();
+    }
+}
-- 
GitLab