/testutil/src/main/java/org/switchyard/test/ShrinkwrapUtil.java

https://github.com/objectiser/switchyard-release · Java · 193 lines · 77 code · 24 blank · 92 comment · 6 complexity · 0c970e9d288f51bf88f5f8253a37d054 MD5 · raw file

  1. /*
  2. * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package org.switchyard.test;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.zip.ZipException;
  18. import java.util.zip.ZipFile;
  19. import org.jboss.shrinkwrap.api.Archive;
  20. import org.jboss.shrinkwrap.api.ShrinkWrap;
  21. import org.jboss.shrinkwrap.api.importer.ZipImporter;
  22. import org.jboss.shrinkwrap.api.spec.JavaArchive;
  23. import org.jboss.shrinkwrap.api.spec.WebArchive;
  24. import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
  25. import org.junit.Assert;
  26. /**
  27. * Shrinkwrap utilities.
  28. *
  29. * @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
  30. */
  31. public final class ShrinkwrapUtil {
  32. /**
  33. * Current SwitchYard version test Environment variable.
  34. */
  35. public static final String SWITCHYARD_VERSION = "SWITCHYARD_VERSION";
  36. private static final String LOCAL_MAVEN_REPO =
  37. System.getProperty("maven.repo.local") != null
  38. ? System.getProperty("maven.repo.local")
  39. : (System.getProperty("user.home") + File.separatorChar + ".m2" + File.separatorChar + "repository");
  40. private ShrinkwrapUtil() {
  41. }
  42. /**
  43. * Get a SwitchYard maven Java Artifact Archive.
  44. * <p/>
  45. * Gets the SwitchYard version from the mandatory {@link #SWITCHYARD_VERSION} env property.
  46. *
  47. * @param groupId Maven groupId
  48. * @param artifactId Maven artifactId.
  49. * @return The Maven artifact archive.
  50. */
  51. public static JavaArchive getSwitchYardJavaArchive(String groupId, String artifactId) {
  52. return getJavaArchive(groupId, artifactId, getSwitchYardVersion());
  53. }
  54. /**
  55. * Get a maven Java Artifact Archive.
  56. *
  57. * @param groupId Maven groupId
  58. * @param artifactId Maven artifactId.
  59. * @param version Artifact version.
  60. * @return The Maven artifact archive.
  61. */
  62. public static JavaArchive getJavaArchive(String groupId, String artifactId, String version) {
  63. return getArchive(groupId, artifactId, version, JavaArchive.class, "jar");
  64. }
  65. /**
  66. * Get a SwitchYard maven Web Artifact Archive.
  67. * <p/>
  68. * Gets the SwitchYard version from the mandatory {@link #SWITCHYARD_VERSION} env property.
  69. *
  70. * @param groupId Maven groupId
  71. * @param artifactId Maven artifactId.
  72. * @return The Maven artifact archive.
  73. */
  74. public static WebArchive getSwitchYardWebArchive(String groupId, String artifactId) {
  75. return getWebArchive(groupId, artifactId, getSwitchYardVersion());
  76. }
  77. /**
  78. * Get a maven Web Artifact Archive.
  79. *
  80. * @param groupId Maven groupId
  81. * @param artifactId Maven artifactId.
  82. * @param version Artifact version.
  83. * @return The Maven artifact archive.
  84. */
  85. public static WebArchive getWebArchive(String groupId, String artifactId, String version) {
  86. return getArchive(groupId, artifactId, version, WebArchive.class, "war");
  87. }
  88. /**
  89. * Get a SwitchYard maven Enterprise Artifact Archive.
  90. * <p/>
  91. * Gets the SwitchYard version from the mandatory {@link #SWITCHYARD_VERSION} env property.
  92. *
  93. * @param groupId Maven groupId
  94. * @param artifactId Maven artifactId.
  95. * @return The Maven artifact archive.
  96. */
  97. public static EnterpriseArchive getSwitchYardEarArchive(String groupId, String artifactId) {
  98. return getEarArchive(groupId, artifactId, getSwitchYardVersion());
  99. }
  100. /**
  101. * Get a maven Enterprise Artifact Archive.
  102. *
  103. * @param groupId Maven groupId
  104. * @param artifactId Maven artifactId.
  105. * @param version Artifact version.
  106. * @return The Maven artifact archive.
  107. */
  108. public static EnterpriseArchive getEarArchive(String groupId, String artifactId, String version) {
  109. return getArchive(groupId, artifactId, version, EnterpriseArchive.class, "ear");
  110. }
  111. /**
  112. * Get a maven Artifact Archive.
  113. *
  114. * @param groupId Maven groupId
  115. * @param artifactId Maven artifactId.
  116. * @param version Artifact version.
  117. * @param archiveType The artifact type.
  118. * @param fileExtension The artifact file extension.
  119. * @return The Maven artifact archive.
  120. *
  121. * @param <A> Archive type.
  122. */
  123. public static <A extends Archive> A getArchive(String groupId, String artifactId, String version, Class<A> archiveType, String fileExtension) {
  124. Assert.assertNotNull("'groupId' argument is null.", groupId);
  125. Assert.assertNotNull("'artifactId' argument is null.", artifactId);
  126. Assert.assertNotNull("'version' argument is null.", version);
  127. File artifactFile = new File(LOCAL_MAVEN_REPO,
  128. groupId.replace(".", "/")
  129. + "/" + artifactId
  130. + "/" + version
  131. + "/" + artifactId + "-" + version + "." + fileExtension);
  132. if (!artifactFile.isFile()) {
  133. String artifact = groupId + ":" + artifactId + ":" + version;
  134. Assert.fail("Failed to resolve artifact '" + artifact + "'. The artifact must be declared as a dependency in your POM, thereby making it available in your local repository.");
  135. }
  136. A archive = ShrinkWrap.create(ZipImporter.class, artifactFile.getName()).importFrom(convert(artifactFile)).as(archiveType);
  137. return archive;
  138. }
  139. /**
  140. * Get the SwitchYard version being tested.
  141. * <p/>
  142. * Gets the SwitchYard version from the mandatory {@link #SWITCHYARD_VERSION} env property.
  143. *
  144. * @return The SwitchYard version being tested.
  145. */
  146. public static String getSwitchYardVersion() {
  147. String version = System.getenv(SWITCHYARD_VERSION);
  148. if (version == null) {
  149. Assert.fail("Test Environment variable '" + SWITCHYARD_VERSION + "' is not configured. "
  150. + "\n\t\t- If running the test in your IDE, set this Environment variable (in the test Run/Debug Configuration) to the current version of SwitchYard (maven artifact version)."
  151. + "\n\t\t- If running the tests through Maven, make sure that the surefire plugin sets this Environment variable to the current version of SwitchYard (maven artifact version).");
  152. }
  153. version = version.trim();
  154. if (version.length() == 0) {
  155. Assert.fail("Test Environment variable '" + SWITCHYARD_VERSION + "' not configured. If running the test in your IDE, set this Environment variable to the current version od SwitchYard.");
  156. }
  157. return version;
  158. }
  159. // converts a file to a ZIP file
  160. private static ZipFile convert(File file) {
  161. try {
  162. return new ZipFile(file);
  163. } catch (ZipException e) {
  164. throw new RuntimeException("Unable to treat dependency artifact \"" + file.getAbsolutePath() + "\" as a ZIP file", e);
  165. } catch (IOException e) {
  166. throw new RuntimeException("Unable to access artifact file at \"" + file.getAbsolutePath() + "\".", e);
  167. }
  168. }
  169. }