View Javadoc
1   package org.codehaus.mojo.gwt.test;
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 java.io.File;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.util.DirectoryScanner;
28  
29  /**
30   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
31   * @version $Id$
32   */
33  public class TestTemplate
34  {
35      /**
36       * Callback to do something with a test
37       */
38      public interface CallBack
39      {
40          /**
41           * @param sourceDir where the test was found
42           * @param test the test name
43           * @throws MojoExecutionException some error occured
44           */
45          void doWithTest( File sourceDir, String test )
46              throws MojoExecutionException;
47      }
48  
49      /**
50       * @param project the maven project
51       * @param includes inclusion patterns
52       * @param excludes exclusion patterns
53       * @param callBack what to do with thoses tests
54       * @throws MojoExecutionException some error occured
55       */
56      public TestTemplate( MavenProject project, String includes, String excludes, CallBack callBack )
57          throws MojoExecutionException
58      {
59          for ( String root : ( List < String > ) project.getTestCompileSourceRoots() )
60          {
61              File sourceDir = new File( root );
62              if ( !sourceDir.exists() )
63              {
64                  continue;
65              }
66              DirectoryScanner scanner = new DirectoryScanner();
67              scanner.setBasedir( sourceDir );
68              if ( includes != null && !"".equals( includes ) )
69              {
70                  scanner.setIncludes( includes.split( "," ) );
71              }
72              if ( excludes != null && !"".equals( excludes ) )
73              {
74                  scanner.setExcludes( excludes.split( "," ) );
75              }
76              scanner.scan();
77              String[] files = scanner.getIncludedFiles();
78              for ( String file : files )
79              {
80                  callBack.doWithTest( sourceDir, file );
81              }
82          }
83      }
84  }