1   package org.apache.maven.repository;
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.Arrays;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
27  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
28  import org.apache.maven.settings.Mirror;
29  import org.codehaus.plexus.PlexusTestCase;
30  
31  public class MirrorProcessorTest
32      extends PlexusTestCase
33  {
34      private DefaultMirrorSelector mirrorSelector;
35      private ArtifactRepositoryFactory repositorySystem;
36  
37      protected void setUp()
38          throws Exception
39      {
40          mirrorSelector = (DefaultMirrorSelector) lookup( MirrorSelector.class );
41          repositorySystem = lookup( ArtifactRepositoryFactory.class );
42      }
43  
44      @Override
45      protected void tearDown()
46          throws Exception
47      {
48          mirrorSelector = null;
49          repositorySystem = null;
50  
51          super.tearDown();
52      }
53  
54      public void testExternalURL()
55      {
56          assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost" ) ) );
57          assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) );
58          assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) );
59          assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) );
60          assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://" ) ) );
61          // these are local
62          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) );
63          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) );
64          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) );
65          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) );
66          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost" ) ) );
67          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) );
68          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) );
69          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) );
70  
71          // not a proper url so returns false;
72          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) );
73          assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "" ) ) );
74      }
75  
76      public void testMirrorLookup()
77      {
78          Mirror mirrorA = newMirror( "a", "a", "http://a" );
79          Mirror mirrorB = newMirror( "b", "b", "http://b" );
80  
81          List<Mirror> mirrors = Arrays.asList( mirrorA, mirrorB );
82  
83          assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
84  
85          assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) );
86  
87          assertNull( mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) );
88      }
89  
90      public void testMirrorWildcardLookup()
91      {
92          Mirror mirrorA = newMirror( "a", "a", "http://a" );
93          Mirror mirrorB = newMirror( "b", "b", "http://b" );
94          Mirror mirrorC = newMirror( "c", "*", "http://wildcard" );
95  
96          List<Mirror> mirrors = Arrays.asList( mirrorA, mirrorB, mirrorC );
97  
98          assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
99  
100         assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) );
101 
102         assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) );
103     }
104 
105     public void testMirrorStopOnFirstMatch()
106     {
107         // exact matches win first
108         Mirror mirrorA2 = newMirror( "a2", "a,b", "http://a2" );
109         Mirror mirrorA = newMirror( "a", "a", "http://a" );
110         // make sure repeated entries are skipped
111         Mirror mirrorA3 = newMirror( "a", "a", "http://a3" );
112 
113         Mirror mirrorB = newMirror( "b", "b", "http://b" );
114         Mirror mirrorC = newMirror( "c", "d,e", "http://de" );
115         Mirror mirrorC2 = newMirror( "c", "*", "http://wildcard" );
116         Mirror mirrorC3 = newMirror( "c", "e,f", "http://ef" );
117 
118         List<Mirror> mirrors = Arrays.asList( mirrorA2, mirrorA, mirrorA3, mirrorB, mirrorC, mirrorC2, mirrorC3 );
119 
120         assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
121 
122         assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) );
123 
124         assertSame( mirrorC2, mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) );
125 
126         assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "d", "http://d" ), mirrors ) );
127 
128         assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "e", "http://e" ), mirrors ) );
129 
130         assertSame( mirrorC2, mirrorSelector.getMirror( getRepo( "f", "http://f" ), mirrors ) );
131     }
132 
133     public void testPatterns()
134     {
135         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*" ) );
136         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*," ) );
137         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), ",*," ) );
138         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*," ) );
139 
140         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a" ) );
141         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a," ) );
142         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), ",a," ) );
143         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a," ) );
144 
145         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a" ) );
146         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a," ) );
147         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), ",a" ) );
148         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), ",a," ) );
149 
150         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a,b" ) );
151         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a,b" ) );
152 
153         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "a,b" ) );
154 
155         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*" ) );
156         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,b" ) );
157         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,!b" ) );
158 
159         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,!a" ) );
160         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "!a,*" ) );
161 
162         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "*,!a" ) );
163         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "!a,*" ) );
164 
165         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "!a,!c" ) );
166         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "d" ), "!a,!c*" ) );
167     }
168 
169     public void testPatternsWithExternal()
170     {
171         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "*" ) );
172         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) );
173 
174         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) );
175         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) );
176         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) );
177         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "!a,external:*" ) );
178 
179         assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c", "http://localhost" ), "!a,external:*" ) );
180         assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) );
181     }
182 
183     public void testLayoutPattern()
184     {
185         assertTrue( DefaultMirrorSelector.matchesLayout( "default", null ) );
186         assertTrue( DefaultMirrorSelector.matchesLayout( "default", "" ) );
187         assertTrue( DefaultMirrorSelector.matchesLayout( "default", "*" ) );
188 
189         assertTrue( DefaultMirrorSelector.matchesLayout( "default", "default" ) );
190         assertFalse( DefaultMirrorSelector.matchesLayout( "default", "legacy" ) );
191 
192         assertTrue( DefaultMirrorSelector.matchesLayout( "default", "legacy,default" ) );
193         assertTrue( DefaultMirrorSelector.matchesLayout( "default", "default,legacy" ) );
194 
195         assertFalse( DefaultMirrorSelector.matchesLayout( "default", "legacy,!default" ) );
196         assertFalse( DefaultMirrorSelector.matchesLayout( "default", "!default,legacy" ) );
197 
198         assertFalse( DefaultMirrorSelector.matchesLayout( "default", "*,!default" ) );
199         assertFalse( DefaultMirrorSelector.matchesLayout( "default", "!default,*" ) );
200     }
201 
202     public void testMirrorLayoutConsideredForMatching()
203     {
204         ArtifactRepository repo = getRepo( "a" );
205 
206         Mirror mirrorA = newMirror( "a", "a", null, "http://a" );
207         Mirror mirrorB = newMirror( "b", "a", "p2", "http://b" );
208 
209         Mirror mirrorC = newMirror( "c", "*", null, "http://c" );
210         Mirror mirrorD = newMirror( "d", "*", "p2", "http://d" );
211 
212         assertSame( mirrorA, mirrorSelector.getMirror( repo, Arrays.asList( mirrorA ) ) );
213         assertNull( mirrorSelector.getMirror( repo, Arrays.asList( mirrorB ) ) );
214 
215         assertSame( mirrorC, mirrorSelector.getMirror( repo, Arrays.asList( mirrorC ) ) );
216         assertNull( mirrorSelector.getMirror( repo, Arrays.asList( mirrorD ) ) );
217     }
218 
219     /**
220      * Build an ArtifactRepository object.
221      *
222      * @param id
223      * @param url
224      * @return
225      */
226     private ArtifactRepository getRepo( String id, String url )
227     {
228         return repositorySystem.createArtifactRepository( id, url, new DefaultRepositoryLayout(), null, null );
229     }
230 
231     /**
232      * Build an ArtifactRepository object.
233      *
234      * @param id
235      * @return
236      */
237     private ArtifactRepository getRepo( String id )
238     {
239         return getRepo( id, "http://something" );
240     }
241 
242     private Mirror newMirror( String id, String mirrorOf, String url )
243     {
244         return newMirror( id, mirrorOf, null, url );
245     }
246 
247     private Mirror newMirror( String id, String mirrorOf, String layouts, String url )
248     {
249         Mirror mirror = new Mirror();
250 
251         mirror.setId( id );
252         mirror.setMirrorOf( mirrorOf );
253         mirror.setMirrorOfLayouts( layouts );
254         mirror.setUrl( url );
255 
256         return mirror;
257     }
258 
259 }