View Javadoc
1   package org.apache.maven.shared.artifact.filter.collection;
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  import java.lang.reflect.Constructor;
22  import java.lang.reflect.InvocationTargetException;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  /**
34   * Abstract test case for subclasses of AbstractArtifactFeatureFilter
35   */
36  public abstract class AbstractArtifactFeatureFilterTest
37  {
38      protected Set<Artifact> artifacts = new HashSet<>();
39  
40      protected Class<?> filterClass;
41  
42      private Object createObjectViaReflection( Class<?> clazz, Object[] conArgs )
43          throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException,
44          IllegalAccessException, InvocationTargetException
45      {
46          Class<?>[] argslist = new Class<?>[2];
47          argslist[0] = String.class;
48          argslist[1] = String.class;
49          Constructor<?> ct = clazz.getConstructor( argslist );
50          return ct.newInstance( conArgs );
51      }
52  
53      @Test
54      public abstract void testParsing()
55          throws Exception;
56  
57      public void parsing()
58          throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException,
59          IllegalAccessException, InvocationTargetException
60      {
61          Object[] conArgs = new Object[] { "one,two", "three,four," };
62  
63          AbstractArtifactFeatureFilter filter =
64              (AbstractArtifactFeatureFilter) createObjectViaReflection( filterClass, conArgs );
65          List<String> includes = filter.getIncludes();
66          List<String> excludes = filter.getExcludes();
67  
68          assertEquals( 2, includes.size() );
69          assertEquals( 2, excludes.size() );
70          assertEquals( "one", includes.get( 0 ) );
71          assertEquals( "two", includes.get( 1 ) );
72          assertEquals( "three", excludes.get( 0 ) );
73          assertEquals( "four", excludes.get( 1 ) );
74      }
75  
76      @Test
77      public abstract void testFiltering()
78          throws Exception;
79  
80      public Set<Artifact> filtering()
81          throws SecurityException, IllegalArgumentException, NoSuchMethodException, InstantiationException,
82          IllegalAccessException, InvocationTargetException
83      {
84          Object[] conArgs = new Object[] { "one,two", "one,three," };
85          AbstractArtifactFeatureFilter filter =
86              (AbstractArtifactFeatureFilter) createObjectViaReflection( filterClass, conArgs );
87          Set<Artifact> result = filter.filter( artifacts );
88          assertEquals( 1, result.size() );
89          return result;
90      }
91  
92      @Test
93      public abstract void testFiltering2()
94          throws Exception;
95  
96      public Set<Artifact> filtering2()
97          throws SecurityException, IllegalArgumentException, NoSuchMethodException, InstantiationException,
98          IllegalAccessException, InvocationTargetException
99      {
100         Object[] conArgs = new Object[] { null, "one,three," };
101         AbstractArtifactFeatureFilter filter =
102             (AbstractArtifactFeatureFilter) createObjectViaReflection( filterClass, conArgs );
103         Set<Artifact> result = filter.filter( artifacts );
104         assertEquals( 2, result.size() );
105         return result;
106 
107     }
108 
109     @Test
110     public abstract void testFiltering3()
111         throws Exception;
112 
113     public void filtering3()
114         throws SecurityException, IllegalArgumentException, NoSuchMethodException, InstantiationException,
115         IllegalAccessException, InvocationTargetException
116     {
117         Object[] conArgs = new Object[] { null, null };
118         AbstractArtifactFeatureFilter filter =
119             (AbstractArtifactFeatureFilter) createObjectViaReflection( filterClass, conArgs );
120         Set<Artifact> result = filter.filter( artifacts );
121         assertEquals( 4, result.size() );
122     }
123 }