View Javadoc
1   package org.apache.maven.shared.artifact.resolver;
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 static junit.framework.Assert.assertEquals;
23  import static org.easymock.EasyMock.createMock;
24  import static org.easymock.EasyMock.expect;
25  import static org.easymock.EasyMock.replay;
26  import static org.easymock.EasyMock.verify;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.junit.Test;
30  
31  public class CumlativeScopeArtifactFilterTest
32  {
33  
34      @Test
35      public void testNothingEnabledWhenNoScopesAdded()
36      {
37          assertScopeInclusion( false, false, false, false );
38      }
39  
40      @Test
41      public void testProvidedOnlyEnabledWhenProvidedScopeOnlyIsAdded()
42      {
43          assertScopeInclusion( false, false, false, true, Artifact.SCOPE_PROVIDED );
44      }
45  
46      @Test
47      public void testCompileAndProvidedEnabledWhenCompileScopeOnlyIsAdded()
48      {
49          assertScopeInclusion( true, false, false, true, Artifact.SCOPE_COMPILE );
50      }
51  
52      @Test
53      public void testCompileAndRuntimeEnabledWhenRuntimeScopeOnlyIsAdded()
54      {
55          assertScopeInclusion( true, false, true, false, Artifact.SCOPE_RUNTIME );
56      }
57  
58      @Test
59      public void testCompileRuntimeProvidedAndTestEnabledWhenTestScopeOnlyIsAdded()
60      {
61          assertScopeInclusion( true, true, true, true, Artifact.SCOPE_TEST );
62      }
63  
64      @Test
65      public void testCompileRuntimeProvidedAndTestEnabledWhenTestAndCompileScopesAreAdded()
66      {
67          assertScopeInclusion( true, true, true, true, Artifact.SCOPE_TEST, Artifact.SCOPE_COMPILE );
68      }
69  
70      @Test
71      public void testCompileRuntimeAndProvidedEnabledWhenRuntimeAndCompileScopeAreAdded()
72      {
73          assertScopeInclusion( true, false, true, true, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_COMPILE );
74      }
75  
76      private void assertScopeInclusion( boolean compileIncluded, boolean testIncluded, boolean runtimeIncluded,
77                                         boolean providedIncluded, String...scopes )
78      {
79          CumulativeScopeArtifactFilter filter = new CumulativeScopeArtifactFilter();
80          if ( scopes != null && scopes.length > 0 )
81          {
82              for ( String scope : scopes )
83              {
84                  filter.addScope( scope );
85              }
86          }
87  
88          Artifact compile = createMock( Artifact.class );
89          expect( compile.getScope() ).andReturn( Artifact.SCOPE_COMPILE ).anyTimes();
90  
91          Artifact runtime = createMock( Artifact.class );
92          expect( runtime.getScope() ).andReturn( Artifact.SCOPE_RUNTIME ).anyTimes();
93  
94          Artifact test = createMock( Artifact.class );
95          expect( test.getScope() ).andReturn( Artifact.SCOPE_TEST ).anyTimes();
96  
97          Artifact provided = createMock( Artifact.class );
98          expect( provided.getScope() ).andReturn( Artifact.SCOPE_PROVIDED ).anyTimes();
99  
100         replay( compile, runtime, test, provided );
101 
102         assertEquals( "Compile scope should " + ( compileIncluded ? "" : "not " ) + "be included.", compileIncluded,
103                       filter.include( compile ) );
104 
105         assertEquals( "Runtime scope should " + ( runtimeIncluded ? "" : "not " ) + "be included.", runtimeIncluded,
106                       filter.include( runtime ) );
107 
108         assertEquals( "Test scope should " + ( testIncluded ? "" : "not " ) + "be included.", testIncluded, filter.include( test ) );
109 
110         assertEquals( "Provided scope should " + ( providedIncluded ? "" : "not " ) + "be included.", providedIncluded,
111                       filter.include( provided ) );
112 
113         verify( compile, runtime, test, provided );
114     }
115 }