001    package org.apache.maven.repository;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Arrays;
023    import java.util.List;
024    
025    import org.apache.maven.artifact.repository.ArtifactRepository;
026    import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
027    import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
028    import org.apache.maven.settings.Mirror;
029    import org.codehaus.plexus.PlexusTestCase;
030    
031    public class MirrorProcessorTest
032        extends PlexusTestCase
033    {
034        private DefaultMirrorSelector mirrorSelector;
035        private ArtifactRepositoryFactory repositorySystem;
036    
037        protected void setUp()
038            throws Exception
039        {
040            mirrorSelector = (DefaultMirrorSelector) lookup( MirrorSelector.class );
041            repositorySystem = lookup( ArtifactRepositoryFactory.class );
042        }
043    
044        @Override
045        protected void tearDown()
046            throws Exception
047        {
048            mirrorSelector = null;
049            repositorySystem = null;
050    
051            super.tearDown();
052        }
053    
054        public void testExternalURL()
055        {
056            assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost" ) ) );
057            assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) );
058            assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) );
059            assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) );
060            assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://" ) ) );
061            // these are local
062            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) );
063            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) );
064            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) );
065            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) );
066            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost" ) ) );
067            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) );
068            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) );
069            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) );
070    
071            // not a proper url so returns false;
072            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) );
073            assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "" ) ) );
074        }
075    
076        public void testMirrorLookup()
077        {
078            Mirror mirrorA = newMirror( "a", "a", "http://a" );
079            Mirror mirrorB = newMirror( "b", "b", "http://b" );
080    
081            List<Mirror> mirrors = Arrays.asList( mirrorA, mirrorB );
082    
083            assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
084    
085            assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) );
086    
087            assertNull( mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) );
088        }
089    
090        public void testMirrorWildcardLookup()
091        {
092            Mirror mirrorA = newMirror( "a", "a", "http://a" );
093            Mirror mirrorB = newMirror( "b", "b", "http://b" );
094            Mirror mirrorC = newMirror( "c", "*", "http://wildcard" );
095    
096            List<Mirror> mirrors = Arrays.asList( mirrorA, mirrorB, mirrorC );
097    
098            assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
099    
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    }