View Javadoc
1   package org.apache.maven.shared.io.location;
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.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.shared.io.logging.DefaultMessageHolder;
27  import org.apache.maven.shared.io.logging.MessageHolder;
28  
29  import junit.framework.TestCase;
30  
31  import static org.easymock.EasyMock.*;
32  
33  public class LocatorTest
34      extends TestCase
35  {
36  
37      public void testShouldConstructWithNoParams()
38      {
39          new Locator();
40      }
41  
42      public void testShouldConstructWithStrategyStackAndMessageHolder()
43      {
44          new Locator( Collections.<LocatorStrategy>emptyList(), new DefaultMessageHolder() );
45      }
46  
47      public void testShouldAllowModificationOfStrategiesAfterConstructionWithUnmodifiableStack()
48      {
49          Locator locator = new Locator( Collections.unmodifiableList( Collections.<LocatorStrategy>emptyList() ),
50                                         new DefaultMessageHolder() );
51  
52          locator.addStrategy( new FileLocatorStrategy() );
53  
54          assertEquals( 1, locator.getStrategies().size() );
55      }
56  
57      public void testShouldRetrieveNonNullMessageHolderWhenConstructedWithoutParams()
58      {
59          assertNotNull( new Locator().getMessageHolder() );
60      }
61  
62      public void testSetStrategiesShouldClearAnyPreExistingStrategiesOut()
63      {
64          LocatorStrategy originalStrategy = createMock( LocatorStrategy.class );
65          LocatorStrategy replacementStrategy = createMock( LocatorStrategy.class );
66  
67          replay( originalStrategy, replacementStrategy );
68  
69          Locator locator = new Locator();
70          locator.addStrategy( originalStrategy );
71  
72          locator.setStrategies( Collections.singletonList( replacementStrategy ) );
73  
74          List<LocatorStrategy> strategies = locator.getStrategies();
75  
76          assertFalse( strategies.contains( originalStrategy ) );
77          assertTrue( strategies.contains( replacementStrategy ) );
78  
79          verify( originalStrategy, replacementStrategy );
80      }
81  
82      public void testShouldRemovePreviouslyAddedStrategy()
83      {
84          LocatorStrategy originalStrategy = createMock( LocatorStrategy.class );
85  
86          replay( originalStrategy );
87  
88          Locator locator = new Locator();
89          locator.addStrategy( originalStrategy );
90  
91          List<LocatorStrategy> strategies = locator.getStrategies();
92  
93          assertTrue( strategies.contains( originalStrategy ) );
94  
95          locator.removeStrategy( originalStrategy );
96  
97          strategies = locator.getStrategies();
98  
99          assertFalse( strategies.contains( originalStrategy ) );
100 
101         verify( originalStrategy );
102     }
103 
104     public void testResolutionFallsThroughStrategyStackAndReturnsNullIfNotResolved()
105     {
106         List<LocatorStrategy> strategies = new ArrayList<LocatorStrategy>();
107 
108         strategies.add( new LoggingLocatorStrategy() );
109         strategies.add( new LoggingLocatorStrategy() );
110         strategies.add( new LoggingLocatorStrategy() );
111 
112         MessageHolder mh = new DefaultMessageHolder();
113 
114         Locator locator = new Locator( strategies, mh );
115 
116         Location location = locator.resolve( "some-specification" );
117 
118         assertNull( location );
119 
120         assertEquals( 3, mh.size() );
121     }
122 
123     public static final class LoggingLocatorStrategy implements LocatorStrategy
124     {
125 
126         static int instanceCounter = 0;
127 
128         int counter = instanceCounter++;
129 
130         public Location resolve( String locationSpecification, MessageHolder messageHolder )
131         {
132             messageHolder.addMessage( "resolve hit on strategy-" + (counter) );
133             return null;
134         }
135 
136     }
137 
138 }