001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.test.server.appserver.deployment;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import com.tc.test.TCTestCase;
011: import com.tc.test.TestConfigObject;
012: import com.tc.test.server.appserver.AppServerFactory;
013: import com.tc.test.server.util.TcConfigBuilder;
014:
015: import java.util.ArrayList;
016: import java.util.Date;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
021:
022: public abstract class AbstractDeploymentTest extends TCTestCase {
023:
024: protected Log logger = LogFactory.getLog(getClass());
025:
026: private ServerManager serverManager;
027: private WatchDog watchDog;
028:
029: Map disabledVariants = new HashMap();
030: List disabledJavaVersion = new ArrayList();
031:
032: private static final int TIMEOUT_DEFAULT = 30 * 60;
033:
034: public AbstractDeploymentTest() {
035: int id = AppServerFactory.getCurrentAppServerId();
036: boolean glassFishOrJetty = (id == AppServerFactory.GLASSFISH || id == AppServerFactory.JETTY);
037: if (isSessionTest() && glassFishOrJetty) {
038: disableAllUntil(new Date(Long.MAX_VALUE));
039: }
040: }
041:
042: protected void beforeTimeout() throws Throwable {
043: getServerManager().timeout();
044: }
045:
046: protected boolean shouldKillAppServersEachRun() {
047: return true;
048: }
049:
050: protected boolean isSessionTest() {
051: return true;
052: }
053:
054: public void runBare() throws Throwable {
055:
056: if (shouldDisable()) {
057: return;
058: }
059:
060: watchDog = new WatchDog(getTimeout());
061: try {
062: watchDog.startWatching();
063: super .runBare();
064: } finally {
065: watchDog.stopWatching();
066: }
067: }
068:
069: protected ServerManager getServerManager() {
070: if (serverManager == null) {
071: try {
072: serverManager = ServerManagerUtil.startAndBind(
073: getClass(), isWithPersistentStore());
074: } catch (Exception e) {
075: throw new RuntimeException(
076: "Unable to create server manager; "
077: + e.toString(), e);
078: }
079: }
080: return serverManager;
081: }
082:
083: protected int getTimeout() {
084: String timeout = TestConfigObject.getInstance()
085: .springTestsTimeout();
086: if (timeout == null) {
087: return TIMEOUT_DEFAULT;
088: } else {
089: return Integer.parseInt(timeout);
090: }
091: }
092:
093: protected void tearDown() throws Exception {
094: if (shouldKillAppServersEachRun()) {
095: ServerManagerUtil.stopAllWebServers(serverManager);
096: }
097: super .tearDown();
098: }
099:
100: /**
101: * tcConfig: resource path to tc-config.xml
102: */
103: protected WebApplicationServer makeWebApplicationServer(
104: String tcConfig) throws Exception {
105: return getServerManager().makeWebApplicationServer(tcConfig);
106: }
107:
108: protected WebApplicationServer makeWebApplicationServer(
109: TcConfigBuilder configBuilder) throws Exception {
110: return getServerManager().makeWebApplicationServer(
111: configBuilder);
112: }
113:
114: protected void restartDSO() throws Exception {
115: getServerManager().restartDSO(isWithPersistentStore());
116: }
117:
118: protected DeploymentBuilder makeDeploymentBuilder(String warFileName) {
119: return getServerManager().makeDeploymentBuilder(warFileName);
120: }
121:
122: // XXX: This causes the bad war file name which breaks WLS tests
123: // protected DeploymentBuilder makeDeploymentBuilder() throws IOException {
124: // return serverManager.makeDeploymentBuilder();
125: // }
126:
127: protected void waitForSuccess(int timeoutInSeconds,
128: TestCallback callback) throws Throwable {
129: long startingTime = System.currentTimeMillis();
130: long timeout = timeoutInSeconds * 1000;
131: while (true) {
132: try {
133: logger.debug("checking");
134: callback.check();
135: logger.debug("check passed");
136: return;
137: } catch (Throwable e) {
138: logger.debug("check failed");
139: if ((System.currentTimeMillis() - startingTime) >= timeout) {
140: logger.debug("check timed out", e);
141: throw e;
142: }
143: }
144: logger.debug("check sleeping");
145: Thread.sleep(100L);
146: }
147: }
148:
149: protected void stopAllWebServers() {
150: ServerManagerUtil.stopAllWebServers(getServerManager());
151: }
152:
153: public boolean isWithPersistentStore() {
154: return false;
155: }
156:
157: protected final boolean cleanTempDir() {
158: return false;
159: }
160:
161: protected void disableVariant(String variantName,
162: String variantValue) {
163: List variantList = (List) disabledVariants.get(variantName);
164: if (variantList == null) {
165: variantList = new ArrayList();
166: disabledVariants.put(variantName, variantList);
167: }
168: variantList.add(variantValue);
169: }
170:
171: protected void disableForJavaVersion(String version) {
172: this .disabledJavaVersion.add(version);
173: }
174:
175: void disableAllTests() {
176: this .disableAllUntil(new Date(Long.MAX_VALUE));
177: }
178:
179: public boolean shouldDisable() {
180: return isAllDisabled() || shouldDisableForJavaVersion()
181: || shouldDisableForVariants();
182: }
183:
184: private boolean shouldDisableForVariants() {
185: for (Iterator iter = disabledVariants.entrySet().iterator(); iter
186: .hasNext();) {
187: Map.Entry entry = (Map.Entry) iter.next();
188: String variantName = (String) entry.getKey();
189: List variants = (List) entry.getValue();
190: String selected = getServerManager().getTestConfig()
191: .selectedVariantFor(variantName);
192: if (variants.contains(selected)) {
193: logger.warn("Test " + getName() + " is disabled for "
194: + variantName + " = " + selected);
195: return true;
196: }
197: }
198: return false;
199: }
200:
201: private boolean shouldDisableForJavaVersion() {
202: String currentVersion = System.getProperties().getProperty(
203: "java.version");
204: for (Iterator iter = disabledJavaVersion.iterator(); iter
205: .hasNext();) {
206: String version = (String) iter.next();
207: if (currentVersion.matches(version)) {
208: logger.warn("Test " + getName() + " is disabled for "
209: + version);
210: return true;
211: }
212: }
213: return false;
214: }
215:
216: }
|