001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.lwcontainer;
018:
019: import java.io.File;
020: import java.net.URI;
021: import java.net.URL;
022:
023: import javax.jbi.messaging.InOut;
024: import javax.jbi.messaging.MessagingException;
025: import javax.xml.namespace.QName;
026:
027: import junit.framework.TestCase;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.servicemix.client.DefaultServiceMixClient;
031: import org.apache.servicemix.client.ServiceMixClient;
032: import org.apache.servicemix.jbi.container.InstallComponent;
033: import org.apache.servicemix.jbi.container.InstallSharedLibrary;
034: import org.apache.servicemix.jbi.container.JBIContainer;
035:
036: /**
037: *
038: * @version $Revision: 598164 $
039: */
040: public class LwContainerComponentTest extends TestCase {
041: private static transient Log log = LogFactory
042: .getLog(LwContainerComponentTest.class);
043:
044: protected JBIContainer container = new JBIContainer();
045:
046: private File tempRootDir;
047:
048: /*
049: * @see TestCase#setUp()
050: */
051: protected void setUp() throws Exception {
052: super .setUp();
053: container.setCreateMBeanServer(false);
054: container.setMonitorInstallationDirectory(false);
055: tempRootDir = File.createTempFile("servicemix", "rootDir");
056: tempRootDir.delete();
057: File tempTemp = new File(tempRootDir.getAbsolutePath()
058: + "/temp");
059: if (!tempTemp.mkdirs()) {
060: fail("Unable to create temporary working root directory ["
061: + tempTemp.getAbsolutePath() + "]");
062: }
063: log.info("Using temporary root directory ["
064: + tempRootDir.getAbsolutePath() + "]");
065:
066: container.setRootDir(tempRootDir.getAbsolutePath());
067: container.setMonitorInstallationDirectory(false);
068: container.setUseMBeanServer(false);
069: container.setCreateMBeanServer(false);
070: container.setFlowName("st");
071: container.init();
072: container.start();
073: }
074:
075: public void testComponentInstallation() throws Exception {
076: LwContainerComponent component = new LwContainerComponent();
077: container.activateComponent(component, "#ServiceMixComponent#");
078: URL url = getClass().getResource("su1-src/servicemix.xml");
079: File path = new File(new URI(url.toString()));
080: path = path.getParentFile();
081: ServiceMixClient client = new DefaultServiceMixClient(container);
082:
083: for (int i = 0; i < 2; i++) {
084: // Deploy and start su
085: component.getServiceUnitManager().deploy("su1",
086: path.getAbsolutePath());
087: component.getServiceUnitManager().init("su1",
088: path.getAbsolutePath());
089: component.getServiceUnitManager().start("su1");
090:
091: // Send message
092: InOut inout = client.createInOutExchange();
093: inout.setService(new QName(
094: "http://servicemix.apache.org/demo/", "chained"));
095: client.send(inout);
096:
097: // Stop and undeploy
098: component.getServiceUnitManager().stop("su1");
099: component.getServiceUnitManager().shutDown("su1");
100: component.getServiceUnitManager().undeploy("su1",
101: path.getAbsolutePath());
102:
103: // Send message
104: inout = client.createInOutExchange();
105: inout.setService(new QName(
106: "http://servicemix.apache.org/demo/", "chained"));
107: try {
108: client.send(inout);
109: } catch (MessagingException e) {
110: // Ok, the lw component is undeployed
111: }
112:
113: }
114: }
115:
116: public void testEndpoints() throws Exception {
117: LwContainerComponent component = new LwContainerComponent();
118: container.activateComponent(component, "#ServiceMixComponent#");
119:
120: InstallSharedLibrary isl = new InstallSharedLibrary();
121: isl.setGroupId("org.apache.servicemix");
122: isl.setArtifactId("servicemix-shared");
123: isl.setVersion(getServiceMixVersion());
124: isl.afterPropertiesSet();
125: isl.deploy(container);
126:
127: InstallComponent ic = new InstallComponent();
128: ic.setGroupId("org.apache.servicemix");
129: ic.setArtifactId("servicemix-quartz");
130: ic.setVersion(getServiceMixVersion());
131: ic.afterPropertiesSet();
132: ic.deploy(container);
133:
134: URL url = getClass().getResource("su2-src/servicemix.xml");
135: File path = new File(new URI(url.toString()));
136: path = path.getParentFile();
137:
138: // Deploy and start su
139: component.getServiceUnitManager().deploy("su2",
140: path.getAbsolutePath());
141: component.getServiceUnitManager().init("su2",
142: path.getAbsolutePath());
143: component.getServiceUnitManager().start("su2");
144:
145: component.getServiceUnitManager().stop("su2");
146: component.getServiceUnitManager().shutDown("su2");
147: }
148:
149: protected String getServiceMixVersion() throws Exception {
150: Package p = Package.getPackage("org.apache.servicemix");
151: return p.getImplementationVersion();
152: }
153:
154: /*
155: * @see TestCase#tearDown()
156: */
157: protected void tearDown() throws Exception {
158: super .tearDown();
159: container.stop();
160: container.shutDown();
161: deleteDir(tempRootDir);
162: }
163:
164: public static boolean deleteDir(File dir) {
165: log.info("Deleting directory : " + dir.getAbsolutePath());
166: if (dir.isDirectory()) {
167: String[] children = dir.list();
168: for (int i = 0; i < children.length; i++) {
169: boolean success = deleteDir(new File(dir, children[i]));
170: if (!success) {
171: return false;
172: }
173: }
174: }
175: // The directory is now empty so delete it
176: return dir.delete();
177: }
178: }
|