001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.server.appserver.unit;
006:
007: import org.apache.commons.io.FileUtils;
008: import org.apache.commons.logging.LogFactory;
009: import org.jboss.logging.JBossJDKLogManager;
010: import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
011: import org.jboss.system.ServiceMBeanSupport;
012: import org.jboss.xb.QNameBuilder;
013:
014: import com.meterware.httpunit.WebConversation;
015: import com.meterware.httpunit.WebResponse;
016: import com.tc.test.server.appserver.AppServerFactory;
017: import com.tc.test.server.appserver.deployment.AbstractTwoServerDeploymentTest;
018: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
019: import com.tc.test.server.appserver.deployment.JARBuilder;
020: import com.tc.test.server.appserver.deployment.WebApplicationServer;
021: import com.tc.test.server.util.TcConfigBuilder;
022: import com.tc.util.runtime.Vm;
023: import com.tctest.service.DirectoryMonitor;
024: import com.tctest.webapp.servlets.JBossSarServlet;
025:
026: import java.io.File;
027: import java.util.Date;
028:
029: import junit.framework.Test;
030:
031: public class JBossSarTest extends AbstractTwoServerDeploymentTest {
032: private static final String CONTEXT = "jbossSar";
033: private static final String SERVLET = "JBossSarServlet";
034: protected boolean parentDelegation = false;
035:
036: public JBossSarTest() {
037: if (AppServerFactory.getCurrentAppServerId() != AppServerFactory.JBOSS
038: || Vm.isJDK14()) {
039: disableAllUntil(new Date(Long.MAX_VALUE));
040: }
041: }
042:
043: public static Test suite() {
044: return new JBossSarTestSetup(JBossSarTest.class);
045: }
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: File sarFile = buildSar();
050:
051: System.out.println("Deploying SAR...");
052: File deploy = new File(server0.getWorkingDirectory(), "deploy");
053: FileUtils.copyFileToDirectory(sarFile, deploy);
054: deploy = new File(server1.getWorkingDirectory(), "deploy");
055: FileUtils.copyFileToDirectory(sarFile, deploy);
056:
057: // give jboss server time to pick up the hot deploy sar
058: Thread.sleep(10 * 1000);
059: }
060:
061: public void testSar() throws Exception {
062: doTest();
063: }
064:
065: protected void doTest() throws Exception {
066: System.out.println("Hitting jboss servers...");
067: WebResponse resp = request(server0, "", new WebConversation());
068: System.out.println("server0 response: " + resp.getText());
069: assertTrue(resp.getText().startsWith("OK"));
070: assertContains("YMCA", resp.getText());
071:
072: resp = request(server1, "", new WebConversation());
073: System.out.println("server1 response: " + resp.getText());
074: assertTrue(resp.getText().startsWith("OK"));
075: assertContains("YMCA", resp.getText());
076: }
077:
078: private File buildSar() throws Exception {
079: File sarFile = getTempFile("directory-monitor.sar");
080: JARBuilder sar = new JARBuilder(sarFile.getName(), sarFile
081: .getParentFile());
082:
083: sar.addResource("/com/tctest/service",
084: "DirectoryMonitor.class", "com/tctest/service");
085: sar.addResource("/com/tctest/service",
086: "DirectoryMonitorMBean.class", "com/tctest/service");
087: sar.addResource("/com/tctest/service",
088: "DirectoryMonitor$ScannerThread.class",
089: "com/tctest/service");
090: sar.addDirectoryOrJARContainingClass(LogFactory.class);
091:
092: if (parentDelegation) {
093: sar.addResourceFullpath("/jboss-sar",
094: "jboss-service-true.xml",
095: "META-INF/jboss-service.xml");
096: } else {
097: sar.addResourceFullpath("/jboss-sar",
098: "jboss-service-false.xml",
099: "META-INF/jboss-service.xml");
100: }
101:
102: sar.finish();
103: return sarFile;
104: }
105:
106: private WebResponse request(WebApplicationServer server,
107: String params, WebConversation con) throws Exception {
108: return server.ping(
109: "/" + CONTEXT + "/" + SERVLET + "?" + params, con);
110: }
111:
112: static class JBossSarTestSetup extends TwoServerTestSetup {
113:
114: public JBossSarTestSetup(Class testClass) {
115: super (testClass, CONTEXT);
116: }
117:
118: protected void configureWar(DeploymentBuilder builder) {
119: builder
120: .addDirectoryOrJARContainingClass(DirectoryMonitor.class);
121: builder
122: .addDirectoryOrJARContainingClass(ServiceMBeanSupport.class);
123: builder
124: .addDirectoryOrJARContainingClass(JBossNotificationBroadcasterSupport.class);
125: builder
126: .addDirectoryOrJARContainingClass(JBossJDKLogManager.class);
127: builder
128: .addDirectoryOrJARContainingClass(QNameBuilder.class);
129:
130: builder.addServlet(SERVLET, "/" + SERVLET + "/*",
131: JBossSarServlet.class, null, true);
132: }
133:
134: protected void configureTcConfig(TcConfigBuilder clientConfig) {
135: clientConfig.addWebApplication(CONTEXT);
136: clientConfig.addInstrumentedClass(DirectoryMonitor.class
137: .getName());
138:
139: clientConfig.addRoot(DirectoryMonitor.class.getName()
140: + ".list", "list");
141: clientConfig.addRoot(JBossSarServlet.class.getName()
142: + ".list", "list");
143:
144: clientConfig.addAutoLock("* "
145: + DirectoryMonitor.class.getName() + ".*(..)",
146: "write");
147: clientConfig.addAutoLock("* "
148: + JBossSarServlet.class.getName() + ".*(..)",
149: "write");
150: }
151: }
152:
153: }
|