/testing/itest/contribution-import-export/import-wsdl/src/test/java/org/apache/tuscany/sca/test/contribution/HelloWorldServerTestCase.java

https://github.com/vorburger/tuscany-sca-2.x · Java · 137 lines · 83 code · 22 blank · 32 comment · 8 complexity · 36a68a8f2d06ec9e6215ed89d78b4a17 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.tuscany.sca.test.contribution;
  20. import helloworld.HelloWorldService;
  21. import java.io.IOException;
  22. import java.net.MalformedURLException;
  23. import java.net.Socket;
  24. import java.net.URL;
  25. import junit.framework.TestCase;
  26. import org.apache.tuscany.sca.assembly.Composite;
  27. import org.apache.tuscany.sca.contribution.Contribution;
  28. import org.apache.tuscany.sca.contribution.service.ContributionService;
  29. import org.apache.tuscany.sca.host.embedded.impl.EmbeddedSCADomain;
  30. /**
  31. * Tests that the helloworld server is available
  32. */
  33. public class HelloWorldServerTestCase extends TestCase {
  34. private ClassLoader cl;
  35. private EmbeddedSCADomain domain;
  36. private Contribution wsdlContribution;
  37. private Contribution consumerContribution;
  38. @Override
  39. protected void setUp() throws Exception {
  40. // Create a test embedded SCA domain
  41. cl = getClass().getClassLoader();
  42. domain = new EmbeddedSCADomain(cl, "http://localhost");
  43. //Start the domain
  44. domain.start();
  45. // Contribute the SCA contribution
  46. ContributionService contributionService = domain.getContributionService();
  47. // File wsdlContribLocation = new File("../export-wsdl/target/classes");
  48. // URL wsdlContribURL = wsdlContribLocation.toURL();
  49. URL wsdlContribURL = getContributionURL(getClass().getClassLoader(), "helloworld.wsdl");
  50. wsdlContribution = contributionService.contribute("http://import-export/export-wsdl", wsdlContribURL, false);
  51. for (Composite deployable : wsdlContribution.getDeployables()) {
  52. domain.getDomainComposite().getIncludes().add(deployable);
  53. domain.buildComposite(deployable);
  54. }
  55. // File helloWorldContribLocation = new File("./target/classes/");
  56. // URL helloWorldContribURL = helloWorldContribLocation.toURL();
  57. URL helloWorldContribURL = getContributionURL(HelloWorldService.class);
  58. consumerContribution =
  59. contributionService.contribute("http://import-export/helloworld", helloWorldContribURL, false);
  60. for (Composite deployable : consumerContribution.getDeployables()) {
  61. domain.getDomainComposite().getIncludes().add(deployable);
  62. domain.buildComposite(deployable);
  63. }
  64. // Start Components from my composite
  65. for (Composite deployable : consumerContribution.getDeployables()) {
  66. domain.getCompositeActivator().activate(deployable);
  67. domain.getCompositeActivator().start(deployable);
  68. }
  69. }
  70. public void testPing() throws IOException {
  71. new Socket("127.0.0.1", 8085);
  72. }
  73. private URL getContributionURL(Class<?> cls) throws MalformedURLException {
  74. String flag = "/" + cls.getName().replace('.', '/') + ".class";
  75. URL url = cls.getResource(flag);
  76. String root = url.toExternalForm();
  77. root = root.substring(0, root.length() - flag.length() + 1);
  78. if (root.startsWith("jar:") && root.endsWith("!/")) {
  79. root = root.substring(4, root.length() - 2);
  80. }
  81. url = new URL(root);
  82. return url;
  83. }
  84. private URL getContributionURL(ClassLoader cl, String flag) throws MalformedURLException {
  85. URL url = cl.getResource(flag);
  86. String root = url.toExternalForm();
  87. root = root.substring(0, root.length() - flag.length());
  88. if (root.startsWith("jar:") && root.endsWith("!/")) {
  89. root = root.substring(4, root.length() - 2);
  90. }
  91. url = new URL(root);
  92. // System.out.println(url);
  93. return url;
  94. }
  95. public void testServiceCall() throws IOException {
  96. HelloWorldService helloWorldService =
  97. domain.getService(HelloWorldService.class, "HelloWorldServiceComponent/HelloWorldService");
  98. assertNotNull(helloWorldService);
  99. assertEquals("Hello Smith", helloWorldService.getGreetings("Smith"));
  100. }
  101. @Override
  102. public void tearDown() throws Exception {
  103. ContributionService contributionService = domain.getContributionService();
  104. // Remove the contribution from the in-memory repository
  105. contributionService.remove("http://import-export/helloworld");
  106. contributionService.remove("http://import-export/export-wsdl");
  107. // Stop Components from my composite
  108. for (Composite deployable : consumerContribution.getDeployables()) {
  109. domain.getCompositeActivator().stop(deployable);
  110. domain.getCompositeActivator().deactivate(deployable);
  111. }
  112. domain.stop();
  113. domain.close();
  114. }
  115. }