/project-model-maven-tests/src/test/java/org/jboss/forge/maven/mavenplugins/ConfigurationElementBuilderTest.java

https://github.com/includeeasy/core · Java · 214 lines · 104 code · 31 blank · 79 comment · 0 complexity · 21f421e7db14f723b9fd7e6d98826acc MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.forge.maven.mavenplugins;
  23. import org.jboss.forge.maven.plugins.ConfigurationElementBuilder;
  24. import org.jboss.forge.maven.plugins.MavenPlugin;
  25. import org.jboss.forge.maven.plugins.MavenPluginBuilder;
  26. import org.jboss.forge.project.dependencies.DependencyBuilder;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. /**
  30. * @author <a href="mailto:paul.bakker.nl@gmail.com">Paul Bakker</a>
  31. */
  32. public class ConfigurationElementBuilderTest
  33. {
  34. private static final String XML = "<additionalClasspathElements><additionalClasspathElement>test</additionalClasspathElement></additionalClasspathElements>";
  35. private static final String XML_WITH_SUB_PLUGIN = "<reportPlugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.3.2</version></plugin></reportPlugins>";
  36. private static final String XML_WITH_SUB_PLUGIN_AND_CONFIGURATION = "<reportPlugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.3.2</version><configuration><xmlOutput>true</xmlOutput></configuration></plugin></reportPlugins>";
  37. private static final String COMPILER_PLUGIN = "<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin>";
  38. private static final String SITE_PLUGIN = "<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-site-plugin</artifactId><version>3.0</version><configuration><reportPlugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.3.2</version><configuration><xmlOutput>true</xmlOutput></configuration></plugin></reportPlugins></configuration></plugin>";
  39. private static final String EAR_PLUGIN = "<plugin><artifactId>maven-ear-plugin</artifactId><version>2.5</version><configuration><modules><webModule><groupId>mygroupid</groupId><artifactId>myartifact</artifactId><contextRoot>/myapp</contextRoot></webModule></modules></configuration></plugin>";
  40. @Test
  41. public void testCreateConfigElement() {
  42. ConfigurationElementBuilder builder = ConfigurationElementBuilder.create()
  43. .setName("additionalClasspathElements")
  44. .addChild("additionalClasspathElement").setText("test").getParentElement();
  45. assertEquals(XML, builder.toString());
  46. }
  47. @Test
  48. public void testCreateWithSubPlugin() {
  49. MavenPluginBuilder findbugsPlugin = MavenPluginBuilder.create()
  50. .setDependency(
  51. DependencyBuilder.create()
  52. .setGroupId("org.codehaus.mojo")
  53. .setArtifactId("findbugs-maven-plugin")
  54. .setVersion("2.3.2")
  55. );
  56. ConfigurationElementBuilder builder = ConfigurationElementBuilder.create()
  57. .setName("reportPlugins")
  58. .addChild(findbugsPlugin);
  59. assertEquals(XML_WITH_SUB_PLUGIN, builder.toString());
  60. }
  61. @Test
  62. public void testCreateWithSubPluginWithConfiguration() {
  63. MavenPluginBuilder findbugsPlugin = MavenPluginBuilder.create()
  64. .setDependency(
  65. DependencyBuilder.create()
  66. .setGroupId("org.codehaus.mojo")
  67. .setArtifactId("findbugs-maven-plugin")
  68. .setVersion("2.3.2")
  69. )
  70. .createConfiguration()
  71. .createConfigurationElement("xmlOutput").setText("true").getParentPluginConfig()
  72. .getOrigin();
  73. ConfigurationElementBuilder builder = ConfigurationElementBuilder.create()
  74. .setName("reportPlugins")
  75. .addChild(findbugsPlugin);
  76. assertEquals(XML_WITH_SUB_PLUGIN_AND_CONFIGURATION, builder.toString());
  77. }
  78. @Test
  79. public void testCreateCompilerPlugin() {
  80. /*
  81. OUTPUT: ----------------
  82. <plugin>
  83. <groupId>org.apache.maven.plugins</groupId>
  84. <artifactId>maven-compiler-plugin</artifactId>
  85. <configuration>
  86. <source>1.6</source>
  87. <target>1.6</target>
  88. </configuration>
  89. </plugin>
  90. ---------------------
  91. */
  92. MavenPluginBuilder compilerPlugin = MavenPluginBuilder.create()
  93. .setDependency(
  94. DependencyBuilder.create()
  95. .setGroupId("org.apache.maven.plugins")
  96. .setArtifactId("maven-compiler-plugin")
  97. )
  98. .createConfiguration().createConfigurationElement("source").setText("1.6").getParentPluginConfig()
  99. .createConfigurationElement("target").setText("1.6").getParentPluginConfig().getOrigin();
  100. assertEquals(COMPILER_PLUGIN, compilerPlugin.toString());
  101. }
  102. @Test
  103. public void testCreateSitePlugin() {
  104. /*
  105. OUTPUT: ----------------
  106. <plugin>
  107. <groupId>org.apache.maven.plugins</groupId>
  108. <artifactId>maven-site-plugin</artifactId>
  109. <version>3.0</version>
  110. <configuration>
  111. <reportPlugins>
  112. <plugin>
  113. <groupId>org.codehaus.mojo</groupId>
  114. <artifactId>findbugs-maven-plugin</artifactId>
  115. <version>2.3.2</version>
  116. <configuration>
  117. <xmlOutput>true</xmlOutput>
  118. </configuration>
  119. </plugin>
  120. </reportPlugins>
  121. </configuration>
  122. </plugin>
  123. ---------------------
  124. */
  125. MavenPluginBuilder findbugsPlugin = MavenPluginBuilder.create()
  126. .setDependency(
  127. DependencyBuilder.create()
  128. .setGroupId("org.codehaus.mojo")
  129. .setArtifactId("findbugs-maven-plugin")
  130. .setVersion("2.3.2")
  131. )
  132. .createConfiguration()
  133. .createConfigurationElement("xmlOutput")
  134. .setText("true").getParentPluginConfig().getOrigin();
  135. MavenPlugin sitePlugin = MavenPluginBuilder.create()
  136. .setDependency(
  137. DependencyBuilder.create()
  138. .setGroupId("org.apache.maven.plugins")
  139. .setArtifactId("maven-site-plugin")
  140. .setVersion("3.0")
  141. )
  142. .createConfiguration().createConfigurationElement("reportPlugins").addChild(findbugsPlugin).getParentPluginConfig().getOrigin();
  143. assertEquals(SITE_PLUGIN, sitePlugin.toString());
  144. }
  145. @Test
  146. public void testCreateEarPlugin() {
  147. /* OUTPUT --------------
  148. <plugin>
  149. <artifactId>maven-ear-plugin</artifactId>
  150. <version>2.5</version>
  151. <configuration>
  152. <modules>
  153. <webModule>
  154. <groupId>mygroupid</groupId>
  155. <artifactId>myartifact</artifactId>
  156. <contextRoot>/myapp</contextRoot>
  157. </webModule>
  158. </modules>
  159. </configuration>
  160. </plugin>
  161. -----------------------
  162. */
  163. MavenPluginBuilder earPlugin = MavenPluginBuilder.create()
  164. .setDependency(
  165. DependencyBuilder.create()
  166. .setArtifactId("maven-ear-plugin")
  167. .setVersion("2.5")
  168. )
  169. .createConfiguration()
  170. .createConfigurationElement("modules")
  171. .createConfigurationElement("webModule")
  172. .createConfigurationElement("groupId").setText("mygroupid").getParentElement()
  173. .createConfigurationElement("artifactId").setText("myartifact").getParentElement()
  174. .createConfigurationElement("contextRoot").setText("/myapp").getParentElement().getParentElement().getParentPluginConfig().getOrigin();
  175. assertEquals(EAR_PLUGIN, earPlugin.toString());
  176. }
  177. }