1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.dataportal.junit;
|
5
|
|
6
|
import java.io.IOException;
|
7
|
import java.util.HashMap;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.junit.After;
|
13
|
import org.junit.AfterClass;
|
14
|
import org.junit.Assert;
|
15
|
import org.junit.BeforeClass;
|
16
|
import org.junit.runner.RunWith;
|
17
|
import org.openqa.selenium.WebDriver;
|
18
|
|
19
|
import eu.etaxonomy.dataportal.DataPortalContext;
|
20
|
import eu.etaxonomy.dataportal.DataPortalSite;
|
21
|
import eu.etaxonomy.dataportal.DrupalVars;
|
22
|
import eu.etaxonomy.dataportal.selenium.WebDriverFactory;
|
23
|
import eu.etaxonomy.drush.DrushExecuter;
|
24
|
|
25
|
/**
|
26
|
* @author a.kohlbecker
|
27
|
*
|
28
|
*/
|
29
|
@RunWith(DataPortalContextSuite.class)
|
30
|
public abstract class CdmDataPortalTestBase extends Assert{
|
31
|
|
32
|
public static final Logger logger = Logger.getLogger(CdmDataPortalTestBase.class);
|
33
|
|
34
|
protected static WebDriver driver;
|
35
|
|
36
|
private DataPortalContext context;
|
37
|
|
38
|
private Map<String,String> drupalVarsBeforeTest = new HashMap<>();
|
39
|
|
40
|
public DataPortalContext getContext() {
|
41
|
return context;
|
42
|
}
|
43
|
|
44
|
public void setContext(DataPortalContext context) {
|
45
|
this.context = context;
|
46
|
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Return the {@link DataPortalSite#getSiteUri()} of the currently active
|
51
|
* context as String
|
52
|
*
|
53
|
* @return string representation of the DataPortal site URI
|
54
|
*/
|
55
|
public String getSiteUrl() {
|
56
|
return context.getSiteUri().toString();
|
57
|
}
|
58
|
|
59
|
@BeforeClass
|
60
|
public static void setUpDriver() {
|
61
|
logger.debug("@BeforeClass: setUpDriver()");
|
62
|
driver = WebDriverFactory.newWebDriver();
|
63
|
}
|
64
|
|
65
|
@AfterClass
|
66
|
public static void closeDriver() {
|
67
|
logger.debug("@AfterClass: closeDriver()");
|
68
|
if (driver != null) {
|
69
|
driver.quit();
|
70
|
}
|
71
|
}
|
72
|
|
73
|
@After
|
74
|
public void resetToOriginalState() throws IOException, InterruptedException {
|
75
|
restoreOriginalVars();
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Safely set a Drupal variable to a new value. Any changes to the Drupal
|
80
|
* variables are reset after the test through {@link #resetToOriginalState()}.
|
81
|
*
|
82
|
* @param varKey The key of the Drupal variable to set. In {@link DrupalVars}
|
83
|
* predefined variable key constants can be found.
|
84
|
*
|
85
|
* @param varValue The value to set
|
86
|
*
|
87
|
* @throws IOException
|
88
|
* @throws InterruptedException
|
89
|
*/
|
90
|
protected void setDrupalVar(String varKey, String varValue) throws IOException, InterruptedException {
|
91
|
DrushExecuter dex = getContext().drushExecuter();
|
92
|
List<String> result = dex.execute(DrushExecuter.variableGet, varKey);
|
93
|
assertEquals(1, result.size());
|
94
|
if(!drupalVarsBeforeTest.containsKey(varKey)) {
|
95
|
// stored original values must not be replaced
|
96
|
drupalVarsBeforeTest.put(varKey, result.get(0));
|
97
|
}
|
98
|
result = dex.execute(DrushExecuter.variableSet, varKey, varValue);
|
99
|
assertEquals("success", result.get(1));
|
100
|
}
|
101
|
|
102
|
protected void restoreOriginalVars() throws IOException, InterruptedException {
|
103
|
DrushExecuter dex = getContext().drushExecuter();
|
104
|
boolean fail = false;
|
105
|
for(String varKey : drupalVarsBeforeTest.keySet()) {
|
106
|
try {
|
107
|
List<String> result = dex.execute(DrushExecuter.variableSet, varKey, drupalVarsBeforeTest.get(varKey));
|
108
|
assertEquals("success", result.get(1));
|
109
|
} catch (Exception e) {
|
110
|
logger.error("FATAL ERROR: Restoring the original drupal variable " + varKey + " = " + drupalVarsBeforeTest.get(varKey) + " failed.", e);
|
111
|
fail = true;
|
112
|
}
|
113
|
}
|
114
|
drupalVarsBeforeTest.clear();
|
115
|
if(fail) {
|
116
|
throw new IOException("Restoring a original drupal variable has previously failed. You may want to fix the site settings manually!");
|
117
|
}
|
118
|
}
|
119
|
|
120
|
|
121
|
}
|