Project

General

Profile

Download (3.71 KB) Statistics
| Branch: | Tag: | Revision:
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
import eu.etaxonomy.drush.DrushExecutionFailure;
25

    
26
/**
27
 * @author a.kohlbecker
28
 *
29
 */
30
@RunWith(DataPortalContextSuite.class)
31
public abstract class CdmDataPortalTestBase extends Assert{
32

    
33
	public static final Logger logger = Logger.getLogger(CdmDataPortalTestBase.class);
34

    
35
	protected static WebDriver driver;
36

    
37
	private DataPortalContext context;
38

    
39
    private Map<String,Object> drupalVarsBeforeTest = new HashMap<>();
40

    
41
	public DataPortalContext getContext() {
42
		return context;
43
	}
44

    
45
	public void setContext(DataPortalContext context) {
46
		this.context = context;
47

    
48
	}
49

    
50
	/**
51
     * Return the {@link DataPortalSite#getSiteUri()} of the currently active
52
     * context as String
53
     *
54
     * @return string representation of the DataPortal site URI
55
     */
56
    public String getSiteUrl() {
57
    	return context.getSiteUri().toString();
58
    }
59

    
60
    @BeforeClass
61
	public static void setUpDriver() {
62
		logger.debug("@BeforeClass: setUpDriver()");
63
		driver = WebDriverFactory.newWebDriver();
64
	}
65

    
66
	@AfterClass
67
	public static void closeDriver() {
68
		logger.debug("@AfterClass: closeDriver()");
69
		if (driver != null) {
70
			driver.quit();
71
		}
72
	}
73

    
74
	@After
75
    public void resetToOriginalState() throws IOException, InterruptedException, DrushExecutionFailure {
76
        restoreOriginalVars();
77
    }
78

    
79
    /**
80
     * Safely set a Drupal variable to a new value. Any changes to the Drupal
81
     * variables are reset after the test through {@link #resetToOriginalState()}.
82
     *
83
     * @param varKey The key of the Drupal variable to set. In {@link DrupalVars}
84
     * predefined variable key constants can be found.
85
     *
86
     * @param varValue The value to set
87
     *
88
     * @throws IOException
89
     * @throws InterruptedException
90
     * @throws DrushExecutionFailure
91
     */
92
    protected void setDrupalVar(String varKey, String varValue) throws IOException, InterruptedException, DrushExecutionFailure {
93
        DrushExecuter dex = getContext().drushExecuter();
94
        List<Object> result = dex.execute(DrushExecuter.variableGet, varKey);
95
        assertEquals(1, result.size());
96
        if(!drupalVarsBeforeTest.containsKey(varKey)) {
97
            // stored original values must not be replaced
98
            drupalVarsBeforeTest.put(varKey, result.get(0));
99
        }
100
        result = dex.execute(DrushExecuter.variableSet, varKey, varValue);
101
    }
102

    
103
    protected void restoreOriginalVars() throws IOException, InterruptedException, DrushExecutionFailure {
104
        DrushExecuter dex = getContext().drushExecuter();
105
        boolean fail = false;
106
        for(String varKey : drupalVarsBeforeTest.keySet()) {
107
            try {
108
                List<Object> result = dex.execute(DrushExecuter.variableSet, varKey, drupalVarsBeforeTest.get(varKey).toString());
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
}
(2-2/3)