PageRenderTime 106ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/antlr-maven-plugin-2.2/src/test/java/org/codehaus/mojo/antlr/AntlrPluginTest.java

#
Java | 206 lines | 138 code | 25 blank | 43 comment | 4 complexity | caa7c4aa4eac77a9312def881dab41ab MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package org.codehaus.mojo.antlr;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import org.apache.maven.artifact.Artifact;
  24. import org.apache.maven.artifact.repository.ArtifactRepository;
  25. import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
  26. import org.apache.maven.artifact.repository.DefaultArtifactRepositoryFactory;
  27. import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
  28. import org.apache.maven.artifact.resolver.ArtifactResolver;
  29. import org.apache.maven.plugin.Mojo;
  30. import org.apache.maven.plugin.testing.AbstractMojoTestCase;
  31. import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
  32. import org.apache.maven.project.MavenProject;
  33. import org.codehaus.mojo.antlr.stubs.DependencyArtifactStubFactory;
  34. import org.codehaus.mojo.antlr.stubs.DependencyProjectStub;
  35. import org.codehaus.plexus.util.FileUtils;
  36. /**
  37. * <code>Unit tests</code> of Antlr plugin
  38. *
  39. * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  40. * @version $Id: AntlrPluginTest.java 6588 2008-03-28 12:22:57Z bentmann $
  41. */
  42. public class AntlrPluginTest
  43. extends AbstractMojoTestCase
  44. {
  45. protected File testDir;
  46. protected DependencyArtifactStubFactory artifactStubFactory;
  47. /**
  48. * @see junit.framework.TestCase#setUp()
  49. */
  50. protected void setUp()
  51. throws Exception
  52. {
  53. // required for mojo lookups to work
  54. super.setUp();
  55. testDir = new File( getBasedir() );
  56. testDir = new File( testDir, "target" );
  57. testDir = new File( testDir, "unit-tests" );
  58. testDir = new File( testDir, "antlr-tests" );
  59. removeDirectory( testDir );
  60. assertFalse( testDir.exists() );
  61. artifactStubFactory = new DependencyArtifactStubFactory( this.testDir, false );
  62. }
  63. /**
  64. * @see junit.framework.TestCase#tearDown()
  65. */
  66. protected void tearDown()
  67. throws Exception
  68. {
  69. if ( testDir != null )
  70. {
  71. try
  72. {
  73. removeDirectory( testDir );
  74. }
  75. catch ( IOException e )
  76. {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. fail( "Trying to remove directory:" + testDir + "\r\n" + e.toString() );
  80. }
  81. assertFalse( testDir.exists() );
  82. testDir = null;
  83. }
  84. artifactStubFactory = null;
  85. }
  86. /**
  87. * Method to test Antlr generation
  88. *
  89. * @throws Exception
  90. */
  91. public void testJavaGrammar()
  92. throws Exception
  93. {
  94. File testPom = new File( getBasedir(),
  95. "src/test/resources/unit/java-grammar-test/java-grammar-test-plugin-config.xml" );
  96. AntlrPlugin mojo = (AntlrPlugin) lookupMojo( testPom, true );
  97. mojo.execute();
  98. File outputDir = new File( getBasedir(),
  99. "target/test/unit/java-grammar-test/target/generated-sources/antlr/" );
  100. assertTrue( new File( outputDir, "JavaLexer.java" ).exists() );
  101. assertTrue( new File( outputDir, "JavaRecognizer.java" ).exists() );
  102. assertTrue( new File( outputDir, "JavaTokenTypes.java" ).exists() );
  103. assertTrue( new File( outputDir, "JavaTokenTypes.txt" ).exists() );
  104. }
  105. /**
  106. * Method to test Antlr generation
  107. *
  108. * @throws Exception
  109. */
  110. public void testJavaGrammarInheritance()
  111. throws Exception
  112. {
  113. File testPom = new File( getBasedir(),
  114. "src/test/resources/unit/java-grammar-inheritance-test/java-grammar-inheritance-test-plugin-config.xml" );
  115. AntlrPlugin mojo = (AntlrPlugin) lookupMojo( testPom, true );
  116. mojo.execute();
  117. File outputDir = new File( getBasedir(),
  118. "target/test/unit/java-grammar-inheritance-test/target/generated-sources/antlr/" );
  119. assertTrue( outputDir.exists() );
  120. assertTrue( new File( outputDir, "GnuCEmitter.java" ).exists() );
  121. assertTrue( new File( outputDir, "GnuCEmitterTokenTypes.java" ).exists() );
  122. assertTrue( new File( outputDir, "GnuCLexer.java" ).exists() );
  123. assertTrue( new File( outputDir, "GnuCLexerTokenTypes.java" ).exists() );
  124. assertTrue( new File( outputDir, "GnuCParser.java" ).exists() );
  125. assertTrue( new File( outputDir, "GNUCTokenTypes.java" ).exists() );
  126. assertTrue( new File( outputDir, "GnuCTreeParser.java" ).exists() );
  127. assertTrue( new File( outputDir, "GnuCTreeParserTokenTypes.java" ).exists() );
  128. assertTrue( new File( outputDir, "StdCLexer.java" ).exists() );
  129. assertTrue( new File( outputDir, "StdCParser.java" ).exists() );
  130. assertTrue( new File( outputDir, "STDCTokenTypes.java" ).exists() );
  131. }
  132. public void testMissingAntlrDependency() throws Exception {
  133. try {
  134. File testPom = new File( getBasedir(),
  135. "src/test/resources/unit/no-antlr-dep/no-antlr-dep-test-plugin-config.xml" );
  136. AntlrPlugin mojo = (AntlrPlugin) lookupMojo( testPom, false );
  137. mojo.execute();
  138. fail( "was expecting failure due to missing antlr dep" );
  139. }
  140. catch ( AntlrPlugin.NoAntlrDependencyDefinedException expected ) {
  141. // expected behavior
  142. }
  143. }
  144. protected Mojo lookupMojo(File pom, boolean addAntlrDep) throws Exception {
  145. AntlrPlugin mojo = (AntlrPlugin) super.lookupMojo( "generate", pom );
  146. assertNotNull( mojo );
  147. assertNotNull( mojo.project );
  148. MavenProject project = mojo.project;
  149. if ( addAntlrDep ) {
  150. Artifact antlrArtifact = artifactStubFactory.createArtifact( "antlr", "antlr", "2.7.7" );
  151. resolveArtifact( antlrArtifact );
  152. ArrayList artifacts = new ArrayList();
  153. artifacts.add( antlrArtifact );
  154. ( ( DependencyProjectStub ) project ).setCompileArtifacts( artifacts );
  155. }
  156. return mojo;
  157. }
  158. protected void resolveArtifact(Artifact artifact) throws Exception {
  159. if ( artifact.isResolved() ) {
  160. return;
  161. }
  162. File localRepoDir = new File( new File( new File( getBasedir(), "target" ), "test" ), "stub-repo" );
  163. ArtifactRepository localRepository = new StubArtifactRepository( localRepoDir.getAbsolutePath() );
  164. DefaultArtifactRepositoryFactory artifactRepositoryFactory = new DefaultArtifactRepositoryFactory();
  165. ArtifactRepository centralRepo = artifactRepositoryFactory.createArtifactRepository(
  166. "central-stub",
  167. "http://repo1.maven.org/maven2/",
  168. new DefaultRepositoryLayout(),
  169. new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE ),
  170. new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE )
  171. );
  172. ArrayList remoteRepositories = new ArrayList();
  173. remoteRepositories.add( centralRepo );
  174. ArtifactResolver artifactResolver = ( ArtifactResolver ) super.lookup( "org.apache.maven.artifact.resolver.ArtifactResolver" );
  175. artifactResolver.resolve( artifact, remoteRepositories, localRepository );
  176. artifact.setResolved( true );
  177. }
  178. public static void removeDirectory( File dir ) throws IOException {
  179. FileUtils.deleteDirectory( dir );
  180. }
  181. }