3 |
3 |
*/
|
4 |
4 |
package eu.etaxonomy.dataportal.junit;
|
5 |
5 |
|
|
6 |
import java.io.IOException;
|
|
7 |
import java.util.HashMap;
|
|
8 |
import java.util.List;
|
|
9 |
import java.util.Map;
|
|
10 |
|
6 |
11 |
import org.apache.log4j.Logger;
|
|
12 |
import org.junit.After;
|
7 |
13 |
import org.junit.AfterClass;
|
8 |
14 |
import org.junit.Assert;
|
9 |
15 |
import org.junit.BeforeClass;
|
... | ... | |
12 |
18 |
|
13 |
19 |
import eu.etaxonomy.dataportal.DataPortalContext;
|
14 |
20 |
import eu.etaxonomy.dataportal.DataPortalSite;
|
|
21 |
import eu.etaxonomy.dataportal.DrupalVars;
|
15 |
22 |
import eu.etaxonomy.dataportal.selenium.WebDriverFactory;
|
|
23 |
import eu.etaxonomy.drush.DrushExecuter;
|
16 |
24 |
|
17 |
25 |
/**
|
18 |
26 |
* @author a.kohlbecker
|
... | ... | |
27 |
35 |
|
28 |
36 |
private DataPortalContext context;
|
29 |
37 |
|
|
38 |
private Map<String,String> drupalVarsBeforeTest = new HashMap<>();
|
|
39 |
|
30 |
40 |
public DataPortalContext getContext() {
|
31 |
41 |
return context;
|
32 |
42 |
}
|
... | ... | |
36 |
46 |
|
37 |
47 |
}
|
38 |
48 |
|
39 |
|
@BeforeClass
|
|
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
|
40 |
60 |
public static void setUpDriver() {
|
41 |
61 |
logger.debug("@BeforeClass: setUpDriver()");
|
42 |
62 |
driver = WebDriverFactory.newWebDriver();
|
... | ... | |
50 |
70 |
}
|
51 |
71 |
}
|
52 |
72 |
|
53 |
|
/**
|
54 |
|
* Return the {@link DataPortalSite#getSiteUri()} of the currently active
|
55 |
|
* context as String
|
56 |
|
*
|
57 |
|
* @return string representation of the DataPortal base URI
|
58 |
|
*/
|
59 |
|
public String getBaseUrl() {
|
60 |
|
return context.getSiteUri().toString();
|
61 |
|
}
|
|
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 |
}
|
62 |
119 |
|
63 |
120 |
|
64 |
121 |
}
|
ref #9181 consistent drupal variable modification and restoring in the test base class