-
Notifications
You must be signed in to change notification settings - Fork 103
/
XWikiTestSetup.java
131 lines (122 loc) · 5.04 KB
/
XWikiTestSetup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* 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.it.framework;
import junit.extensions.TestSetup;
import junit.framework.Test;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
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 String DEBUG = System.getProperty("debug");
private static final int TIMEOUT_SECONDS = 15;
public XWikiTestSetup(Test test)
{
super(test);
}
protected void setUp() throws Exception
{
startXWiki();
waitForXWikiToLoad();
}
private void startXWiki() throws Exception
{
// Start XWiki
if (DEBUG != null) {
System.out.println("Startig XWiki with command [" + START_COMMAND
+ "] in directory [" + EXECUTION_DIRECTORY + "]");
}
Process process =
Runtime.getRuntime().exec(START_COMMAND, null, new File(EXECUTION_DIRECTORY));
if (process.waitFor() != 0) {
tearDown();
throw new RuntimeException("Failed to start XWiki with command [" + START_COMMAND
+ "] in directory [" + EXECUTION_DIRECTORY + "]");
}
// Read the output of the process we used to start XWiki
BufferedInputStream in = new BufferedInputStream(process.getInputStream());
final BufferedReader br = new BufferedReader(new InputStreamReader(in));
String buffer;
while ((buffer = br.readLine()) != null) {
System.out.println("[XWiki Execution] " + buffer);
}
}
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();
if (DEBUG != null) {
System.out.println("Result of pinging [" + url + "] = ["
+ connection.getResponseCode() + "], Message = ["
+ connection.getResponseMessage() + "]");
}
connected = (connection.getResponseCode() == 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
Runtime.getRuntime().exec(STOP_COMMAND, null, new File(EXECUTION_DIRECTORY));
}
}