1 17 package org.apache.servicemix.jbi.installation; 18 19 import java.io.File ; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.servicemix.jbi.framework.ManagementSupport; 24 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 25 import org.apache.servicemix.jbi.jaxp.StringSource; 26 import org.apache.servicemix.jbi.util.DOMUtil; 27 import org.apache.xml.utils.PrefixResolver; 28 import org.apache.xpath.CachedXPathAPI; 29 import org.apache.xpath.objects.XObject; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 33 public class DeploymentMessageTest extends AbstractManagementTest { 34 35 protected void initContainer() { 36 container.setCreateMBeanServer(false); 37 container.setMonitorInstallationDirectory(false); 38 container.setMonitorDeploymentDirectory(false); 39 } 40 41 public void testDeployNullJarUrl() throws Exception { 42 startContainer(true); 43 try { 44 getDeploymentService().deploy(null); 45 fail("Deploy with null jar url should have failed"); 46 } catch (Exception e) { 47 assertTrue(e instanceof Exception ); 48 String str = e.getMessage(); 49 System.err.println(str); 50 Node node = new SourceTransformer().toDOMNode(new StringSource(str)); 51 assertNotNull(node); 52 assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 53 assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type")); 54 assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 55 } 56 } 57 58 public void testDeployNonExistentJar() throws Exception { 59 startContainer(true); 60 try { 61 getDeploymentService().deploy("hello"); 62 fail("Deploy with non existent jar url should have failed"); 63 } catch (Exception e) { 64 assertTrue(e instanceof Exception ); 65 String str = e.getMessage(); 66 System.err.println(str); 67 Node node = new SourceTransformer().toDOMNode(new StringSource(str)); 68 assertNotNull(node); 69 assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 70 assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type")); 71 assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 72 } 73 } 74 75 public void testDeployNoDescriptor() throws Exception { 76 startContainer(true); 77 String jarUrl = createDummyArchive().getAbsolutePath(); 78 try { 79 getDeploymentService().deploy(jarUrl); 80 fail("Deploy with non existent descriptor should have failed"); 81 } catch (Exception e) { 82 assertTrue(e instanceof Exception ); 83 String str = e.getMessage(); 84 System.err.println(str); 85 Node node = new SourceTransformer().toDOMNode(new StringSource(str)); 86 assertNotNull(node); 87 assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 88 assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type")); 89 assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 90 } 91 } 92 93 public void testDeployWithNonSADescriptor() throws Exception { 94 startContainer(true); 95 String jarUrl = createInstallerArchive("component1").getAbsolutePath(); 96 try { 97 getDeploymentService().deploy(jarUrl); 98 fail("Deploy with non existent descriptor should have failed"); 99 } catch (Exception e) { 100 assertTrue(e instanceof Exception ); 101 String str = e.getMessage(); 102 System.err.println(str); 103 Node node = new SourceTransformer().toDOMNode(new StringSource(str)); 104 assertNotNull(node); 105 assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 106 assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type")); 107 assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 108 } 109 } 110 111 public void testDeployWithSuccess() throws Exception { 112 DummyComponent component = new DummyComponent(); 113 String deployResult = DOMUtil.asXML(ManagementSupport.createComponentSuccess("deploy", "component1")); 114 component.setResult(deployResult); 115 System.err.println(deployResult); 116 startContainer(true); 117 getContainer().activateComponent(component, "component1"); 118 getContainer().getEnvironmentContext().createComponentRootDir("component1"); 119 File installSaUrl = createServiceAssemblyArchive("sa", "su", "component1"); 120 String result = getDeploymentService().deploy(installSaUrl.getAbsolutePath()); 121 System.err.println(result); 122 Node node = new SourceTransformer().toDOMNode(new StringSource(result)); 123 assertNotNull(node); 124 assertEquals("SUCCESS", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 126 assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 127 assertEquals("SUCCESS", textValueOfXPath(node, "//jbi:component-task-result//jbi:task-result")); 129 assertEquals("deploy", textValueOfXPath(node, "//jbi:component-task-result//jbi:task-id")); 130 } 131 132 public void testDeployWithOneSuccessAndOneError() throws Exception { 133 DummyComponent component1 = new DummyComponent(); 134 String deployResult1 = DOMUtil.asXML(ManagementSupport.createComponentFailure("deploy", "component1", "xxx", null)); 135 component1.setResult(deployResult1); 136 component1.setException(true); 137 System.err.println(deployResult1); 138 DummyComponent component2 = new DummyComponent(); 139 String deployResult2 = DOMUtil.asXML(ManagementSupport.createComponentSuccess("deploy", "component2")); 140 component2.setResult(deployResult2); 141 System.err.println(deployResult2); 142 startContainer(true); 143 getContainer().activateComponent(component1, "component1"); 144 getContainer().getEnvironmentContext().createComponentRootDir("component1"); 145 getContainer().activateComponent(component2, "component2"); 146 getContainer().getEnvironmentContext().createComponentRootDir("component2"); 147 File installSaUrl = createServiceAssemblyArchive("sa", new String [] { "su1", "su2" }, new String [] { "component1", "component2"}); 148 String result = null;; 149 try { 150 result = getDeploymentService().deploy(installSaUrl.getAbsolutePath()); 151 fail("Deployment with an error is not supported"); 152 } catch (Exception e) { 153 result = e.getMessage(); 154 } 155 System.err.println(result); 156 Node node = new SourceTransformer().toDOMNode(new StringSource(result)); 157 assertNotNull(node); 158 assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 160 assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type")); 161 assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 162 assertEquals("FAILED", textValueOfXPath(node, "//jbi:component-task-result[1]//jbi:task-result")); 164 assertEquals("deploy", textValueOfXPath(node, "//jbi:component-task-result[1]//jbi:task-id")); 165 assertEquals("SUCCESS", textValueOfXPath(node, "//jbi:component-task-result[2]//jbi:task-result")); 167 assertEquals("deploy", textValueOfXPath(node, "//jbi:component-task-result[2]//jbi:task-id")); 168 } 169 170 public void testUndeployNullJarUrl() throws Exception { 171 startContainer(true); 172 try { 173 getDeploymentService().undeploy(null); 174 fail("Deploy with null jar url should have failed"); 175 } catch (Exception e) { 176 assertTrue(e instanceof Exception ); 177 String str = e.getMessage(); 178 System.err.println(str); 179 Node node = new SourceTransformer().toDOMNode(new StringSource(str)); 180 assertNotNull(node); 181 assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 182 assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type")); 183 assertEquals("undeploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 184 } 185 } 186 187 public void testUndeployNonDeployedSA() throws Exception { 188 startContainer(true); 189 try { 190 getDeploymentService().undeploy("my-sa"); 191 fail("Deploy with non deployed sa should have failed"); 192 } catch (Exception e) { 193 assertTrue(e instanceof Exception ); 194 String str = e.getMessage(); 195 System.err.println(str); 196 Node node = new SourceTransformer().toDOMNode(new StringSource(str)); 197 assertNotNull(node); 198 assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result")); 199 assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type")); 200 assertEquals("undeploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id")); 201 } 202 } 203 204 protected String textValueOfXPath(Node node, String xpath) throws TransformerException { 205 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 206 XObject list = cachedXPathAPI.eval(node, xpath, new PrefixResolver() { 207 public String getNamespaceForPrefix(String prefix) { 208 if (prefix.equals("jbi")) { 209 return "http://java.sun.com/xml/ns/jbi/management-message"; 210 } 211 return null; 212 } 213 public String getNamespaceForPrefix(String arg0, Node arg1) { 214 return null; 215 } 216 public String getBaseIdentifier() { 217 return null; 218 } 219 public boolean handlesNullPrefixes() { 220 return false; 221 } 222 }); 223 Node root = list.nodeset().nextNode(); 224 if (root instanceof Element ) { 225 Element element = (Element ) root; 226 if (element == null) { 227 return ""; 228 } 229 String text = DOMUtil.getElementText(element); 230 return text; 231 } 232 else if (root != null) { 233 return root.getNodeValue(); 234 } else { 235 return null; 236 } 237 } 238 239 } 240 | Popular Tags |