View Javadoc
1   package org.apache.maven.plugin.verifier;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  
26  import java.io.File;
27  import java.io.UnsupportedEncodingException;
28  import java.net.URLDecoder;
29  
30  public class VerifierMojoTest
31      extends TestCase
32  {
33      private File getResourceFile( String name ) throws UnsupportedEncodingException
34      {
35          String file = getClass().getResource( name ).getFile();
36          String decode = URLDecoder.decode( file, "UTF-8" ); // necessary for JDK 1.5+, where spaces are escaped to %20
37          return new File( decode );
38      }
39  
40      public void testPrefixWithBaseDir()
41      {
42          VerifierMojo mojo = new VerifierMojo();
43          mojo.setBaseDir( new File( "c:/some/path" ) );
44  
45          File result = mojo.getAbsoluteFileToCheck( new File( "target/dummy.txt" ) );
46  
47          File expectedResult = new File( "c:/some/path/target/dummy.txt" );
48          assertEquals( expectedResult.getPath(), result.getPath() );
49      }
50  
51      public void testDoNotPrefixWhenAbsolutePath()
52      {
53          VerifierMojo mojo = new VerifierMojo();
54          mojo.setBaseDir( new File( "/some/path" ).getAbsoluteFile() );
55  
56          File absoluteFile = new File( "/project/target/dummy.txt" ).getAbsoluteFile();
57          File result = mojo.getAbsoluteFileToCheck( absoluteFile );
58  
59          assertEquals( absoluteFile.getPath(), result.getPath() );
60      }
61  
62      public void testCheckFileThatDoesNotExist()
63          throws Exception
64      {
65          VerifierMojo mojo = new VerifierMojo();
66          File file = getResourceFile( "/FileDoesNotExist.xml" );
67          mojo.setBaseDir( new File( "c:/some/path" ) );
68          mojo.setVerificationFile( file );
69          mojo.setFailOnError( true );
70          mojo.setVerificationResultPrinter( new VerificationResultPrinter()
71          {
72              public void print( VerificationResult result )
73              {
74                  assertEquals( 1, result.getExistenceFailures().size() );
75                  assertEquals( 0, result.getNonExistenceFailures().size() );
76                  assertEquals( 0, result.getContentFailures().size() );
77              }
78          } );
79  
80          try
81          {
82              mojo.execute();
83              fail( "Should have thrown an exception" );
84          }
85          catch ( MojoExecutionException expected )
86          {
87              assertTrue( true );
88          }
89      }
90  
91      public void testCheckFileThatExists()
92          throws Exception
93      {
94          VerifierMojo mojo = new VerifierMojo();
95          File file = getResourceFile( "/File Exists.xml" );
96          mojo.setBaseDir( file.getParentFile() );
97          mojo.setVerificationFile( file );
98          mojo.setFailOnError( true );
99          mojo.setVerificationResultPrinter( new VerificationResultPrinter()
100         {
101             public void print( VerificationResult result )
102             {
103                 assertEquals( 0, result.getExistenceFailures().size() );
104                 assertEquals( 0, result.getNonExistenceFailures().size() );
105                 assertEquals( 0, result.getContentFailures().size() );
106             }
107         } );
108 
109         mojo.execute();
110     }
111 
112     public void testCheckForInexistentFile()
113         throws Exception
114     {
115         VerifierMojo mojo = new VerifierMojo();
116         File file = getResourceFile( "/InexistentFile.xml" );
117         mojo.setBaseDir( new File( "c:/some/path" ) );
118         mojo.setVerificationFile( file );
119         mojo.setVerificationResultPrinter( new VerificationResultPrinter()
120         {
121             public void print( VerificationResult result )
122             {
123                 assertEquals( 0, result.getExistenceFailures().size() );
124                 assertEquals( 0, result.getNonExistenceFailures().size() );
125                 assertEquals( 0, result.getContentFailures().size() );
126             }
127         } );
128 
129         mojo.execute();
130     }
131 
132     public void testCheckForInexistentFileThatExists()
133         throws Exception
134     {
135         VerifierMojo mojo = new VerifierMojo();
136         File file = getResourceFile( "/InexistentFileThatExists.xml" );
137         mojo.setBaseDir( file.getParentFile() );
138         mojo.setVerificationFile( file );
139         mojo.setFailOnError( true );
140         mojo.setVerificationResultPrinter( new VerificationResultPrinter()
141         {
142             public void print( VerificationResult result )
143             {
144                 assertEquals( 0, result.getExistenceFailures().size() );
145                 assertEquals( 1, result.getNonExistenceFailures().size() );
146                 assertEquals( 0, result.getContentFailures().size() );
147             }
148         } );
149 
150         try
151         {
152             mojo.execute();
153             fail( "Should have thrown an exception" );
154         }
155         catch ( MojoExecutionException expected )
156         {
157             assertTrue( true );
158         }
159     }
160 
161     public void testCheckFileForContent()
162         throws Exception
163     {
164         VerifierMojo mojo = new VerifierMojo();
165         File file = getResourceFile( "/FileExistsValidContent.xml" );
166         mojo.setBaseDir( file.getParentFile() );
167         mojo.setVerificationFile( file );
168         mojo.setVerificationResultPrinter( new VerificationResultPrinter()
169         {
170             public void print( VerificationResult result )
171             {
172                 assertEquals( 0, result.getExistenceFailures().size() );
173                 assertEquals( 0, result.getNonExistenceFailures().size() );
174                 assertEquals( 0, result.getContentFailures().size() );
175             }
176         } );
177 
178         mojo.execute();
179     }
180 
181     public void testCheckFileForInvalidContent()
182         throws Exception
183     {
184         VerifierMojo mojo = new VerifierMojo();
185         File file = getResourceFile( "/FileExistsInvalidContent.xml" );
186         mojo.setBaseDir( file.getParentFile() );
187         mojo.setVerificationFile( file );
188         mojo.setFailOnError( true );
189         mojo.setVerificationResultPrinter( new VerificationResultPrinter()
190         {
191             public void print( VerificationResult result )
192             {
193                 assertEquals( 0, result.getExistenceFailures().size() );
194                 assertEquals( 0, result.getNonExistenceFailures().size() );
195                 assertEquals( 1, result.getContentFailures().size() );
196             }
197         } );
198 
199         try
200         {
201             mojo.execute();
202             fail( "Should have thrown an exception" );
203         }
204         catch ( MojoExecutionException expected )
205         {
206             assertTrue( true );
207         }
208     }
209 
210 }