]> source.dussan.org Git - archiva.git/blob
fa66d29bf068f563318635ed2bbffec89988415a
[archiva.git] /
1 package org.apache.archiva.repository.maven.metadata;
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  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.common.utils.VersionComparator;
22 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
23 import org.apache.archiva.repository.ManagedRepositoryContent;
24 import org.apache.archiva.repository.maven.metadata.storage.mock.MockConfiguration;
25 import org.apache.archiva.model.ProjectReference;
26 import org.apache.archiva.model.VersionedReference;
27 import org.apache.archiva.policies.CachedFailuresPolicy;
28 import org.apache.archiva.policies.ChecksumPolicy;
29 import org.apache.archiva.policies.ReleasesPolicy;
30 import org.apache.archiva.policies.SnapshotsPolicy;
31 import org.apache.archiva.repository.maven.AbstractRepositoryLayerTestCase;
32 import org.apache.archiva.repository.LayoutException;
33 import org.apache.archiva.repository.RemoteRepositoryContent;
34 import org.apache.archiva.repository.RepositoryContentProvider;
35 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
36 import org.apache.archiva.repository.metadata.base.MetadataTools;
37 import org.apache.archiva.repository.maven.MavenManagedRepository;
38 import org.apache.commons.io.FileUtils;
39 import org.apache.commons.lang3.StringUtils;
40 import org.junit.Test;
41 import org.springframework.test.context.ContextConfiguration;
42 import org.xml.sax.SAXException;
43 import org.xmlunit.builder.DiffBuilder;
44 import org.xmlunit.diff.Diff;
45 import org.xmlunit.diff.Difference;
46
47 import javax.inject.Inject;
48 import javax.inject.Named;
49 import javax.xml.parsers.ParserConfigurationException;
50 import java.io.IOException;
51 import java.net.URISyntaxException;
52 import java.nio.charset.Charset;
53 import java.nio.file.Files;
54 import java.nio.file.Path;
55 import java.nio.file.Paths;
56 import java.util.ArrayList;
57 import java.util.Collections;
58 import java.util.List;
59 import java.util.Set;
60
61 import static org.junit.Assert.*;
62
63 /**
64  * MetadataToolsTest
65  */
66 @SuppressWarnings( "deprecation" )
67 @ContextConfiguration (
68     { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-metadata-tools-test.xml" } )
69 public class MetadataToolsTest
70     extends AbstractRepositoryLayerTestCase
71 {
72     @Inject
73     @Named ( "metadataTools#test" )
74     private MetadataTools tools;
75
76     @Inject
77     @Named ( "archivaConfiguration#mock" )
78     protected MockConfiguration config;
79
80     private Path getRepositoryPath(String repoName) {
81         try
82         {
83             return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
84         }
85         catch ( URISyntaxException e )
86         {
87             throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
88         }
89     }
90
91     @Test
92     public void testGatherSnapshotVersionsA()
93         throws Exception
94     {
95         removeProxyConnector( "test-repo", "apache-snapshots" );
96         removeProxyConnector( "test-repo", "internal-snapshots" );
97         removeProxyConnector( "test-repo", "snapshots.codehaus.org" );
98
99         assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
100                                 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
101                                     "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
102                                     "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
103                                     "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
104     }
105
106     @Test
107     public void testGatherSnapshotVersionsAWithProxies()
108         throws Exception
109     {
110         // These proxied repositories do not need to exist for the purposes of this unit test,
111         // just the repository ids are important.
112         createProxyConnector( "test-repo", "apache-snapshots" );
113         createProxyConnector( "test-repo", "internal-snapshots" );
114         createProxyConnector( "test-repo", "snapshots.codehaus.org" );
115
116         assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
117                                 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
118                                     "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
119                                     "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
120                                     "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070315.033030-10"
121                                     /* Arrives in via snapshots.codehaus.org proxy */,
122                                     "1.0-alpha-11-20070316.175232-11" } );
123     }
124
125     @Test
126     public void testGetRepositorySpecificName()
127         throws Exception
128     {
129         RemoteRepositoryContent repoJavaNet =
130             createRemoteRepositoryContent( "maven2-repository.dev.java.net", "Java.net Repository for Maven 2",
131                                            "http://download.java.net/maven/2/", "default" );
132         RemoteRepositoryContent repoCentral =
133             createRemoteRepositoryContent( "central", "Central Global Repository", "http://repo1.maven.org/maven2/",
134                                            "default" );
135
136         String convertedName =
137             tools.getRepositorySpecificName( repoJavaNet, "commons-lang/commons-lang/maven-metadata.xml" );
138         assertMetadataPath( "commons-lang/commons-lang/maven-metadata-maven2-repository.dev.java.net.xml",
139                             convertedName );
140
141         convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
142         assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
143     }
144
145     // TODO: replace with group tests
146 //    public void testUpdateProjectBadArtifact()
147 //        throws Exception
148 //    {
149 //        try
150 //        {
151 //            assertUpdatedProjectMetadata( "bad_artifact", null );
152 //            fail( "Should have thrown an IOException on a bad artifact." );
153 //        }
154 //        catch ( IOException e )
155 //        {
156 //            // Expected path
157 //        }
158 //    }
159
160     @Test
161     public void testUpdateProjectNonExistingVersion()
162         throws Exception
163     {
164         ManagedRepositoryContent testRepo = createTestRepoContent();
165         ProjectReference reference = new ProjectReference();
166         reference.setGroupId( "org.apache.archiva.metadata.tests" );
167         reference.setArtifactId( "missing_artifact" );
168
169         prepTestRepo( testRepo, reference );
170
171         // check metadata prior to update -- should contain the non-existing artifact version
172         assertProjectMetadata( testRepo, reference, "missing_artifact",
173                                new String[]{ "1.0-SNAPSHOT", "1.1-SNAPSHOT", "1.2-SNAPSHOT" }, "1.2-SNAPSHOT", null );
174
175         tools.updateMetadata( testRepo, reference );
176
177         // metadata should not contain the non-existing artifact version -- 1.1-SNAPSHOT
178         assertProjectMetadata( testRepo, reference, "missing_artifact", new String[]{ "1.0-SNAPSHOT", "1.2-SNAPSHOT" },
179                                "1.2-SNAPSHOT", null );
180     }
181
182     @Test
183     public void testUpdateProjectMissingMultipleVersions()
184         throws Exception
185     {
186         assertUpdatedProjectMetadata( "missing_metadata_b",
187                                       new String[]{ "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" },
188                                       "2.0-20070821-dev", "2.0-20070821-dev" );
189     }
190
191     @Test
192     public void testUpdateProjectMissingMultipleVersionsWithProxies()
193         throws Exception
194     {
195         // Attach the (bogus) proxies to the managed repo.
196         // These proxied repositories do not need to exist for the purposes of this unit test,
197         // just the repository ids are important.
198         createProxyConnector( "test-repo", "central" );
199         createProxyConnector( "test-repo", "java.net" );
200
201         assertUpdatedProjectMetadata( "proxied_multi",
202                                       new String[]{ "1.0-spec" /* in java.net */, "1.0" /* in managed, and central */,
203                                           "1.0.1" /* in central */, "1.1" /* in managed */, "2.0-proposal-beta"
204                                           /* in java.net */, "2.0-spec" /* in java.net */, "2.0"
205                                           /* in central, and java.net */, "2.0.1" /* in java.net */, "2.1"
206                                           /* in managed */, "3.0" /* in central */, "3.1" /* in central */ }, "3.1",
207                                       "3.1" );
208     }
209
210     @Test
211     public void testUpdateProjectSimpleYetIncomplete()
212         throws Exception
213     {
214         assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[]{ "1.0" }, "1.0", "1.0" );
215     }
216
217     @Test
218     public void testUpdateProjectSimpleYetMissing()
219         throws Exception
220     {
221         assertUpdatedProjectMetadata( "missing_metadata_a", new String[]{ "1.0" }, "1.0", "1.0" );
222     }
223
224     @Test
225     public void testUpdateVersionSimple10()
226         throws Exception
227     {
228         assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
229     }
230
231     @Test
232     public void testUpdateVersionSimple20()
233         throws Exception
234     {
235         assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
236     }
237
238     @Test
239     public void testUpdateVersionSimple20NotSnapshot()
240         throws Exception
241     {
242         assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
243     }
244
245     @Test
246     public void testUpdateVersionSnapshotA()
247         throws Exception
248     {
249         assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
250     }
251
252     @Test
253     public void testToPathFromVersionReference()
254     {
255         VersionedReference reference = new VersionedReference();
256         reference.setGroupId( "com.foo" );
257         reference.setArtifactId( "foo-tool" );
258         reference.setVersion( "1.0" );
259
260         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
261     }
262
263     @Test
264     public void testToPathFromProjectReference()
265     {
266         ProjectReference reference = new ProjectReference();
267         reference.setGroupId( "com.foo" );
268         reference.setArtifactId( "foo-tool" );
269
270         assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
271     }
272
273     @Test
274     public void testToProjectReferenceFooTools()
275         throws RepositoryMetadataException
276     {
277         assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
278     }
279
280     @Test
281     public void testToProjectReferenceAReallyLongPath()
282         throws RepositoryMetadataException
283     {
284         String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
285         String artifactId = "a";
286         String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/maven-metadata.xml";
287
288         assertProjectReference( groupId, artifactId, path );
289     }
290
291     @Test
292     public void testToProjectReferenceCommonsLang()
293         throws RepositoryMetadataException
294     {
295         String groupId = "commons-lang";
296         String artifactId = "commons-lang";
297         String path = "commons-lang/commons-lang/maven-metadata.xml";
298
299         assertProjectReference( groupId, artifactId, path );
300     }
301
302     private void assertProjectReference( String groupId, String artifactId, String path )
303         throws RepositoryMetadataException
304     {
305         ProjectReference reference = tools.toProjectReference( path );
306
307         assertNotNull( "Reference should not be null.", reference );
308         assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
309         assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
310     }
311
312     @Test
313     public void testToVersionedReferenceFooTool()
314         throws RepositoryMetadataException
315     {
316         String groupId = "com.foo";
317         String artifactId = "foo-tool";
318         String version = "1.0";
319         String path = "com/foo/foo-tool/1.0/maven-metadata.xml";
320
321         assertVersionedReference( groupId, artifactId, version, path );
322     }
323
324     @Test
325     public void testToVersionedReferenceAReallyLongPath()
326         throws RepositoryMetadataException
327     {
328         String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
329         String artifactId = "a";
330         String version = "1.1-alpha-1";
331         String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/1.1-alpha-1/maven-metadata.xml";
332
333         assertVersionedReference( groupId, artifactId, version, path );
334     }
335
336     @Test
337     public void testToVersionedReferenceCommonsLang()
338         throws RepositoryMetadataException
339     {
340         String groupId = "commons-lang";
341         String artifactId = "commons-lang";
342         String version = "2.1";
343         String path = "commons-lang/commons-lang/2.1/maven-metadata.xml";
344
345         assertVersionedReference( groupId, artifactId, version, path );
346     }
347
348     @Test
349     public void testToVersionedReferenceSnapshot()
350         throws RepositoryMetadataException
351     {
352         String groupId = "com.foo";
353         String artifactId = "foo-connector";
354         String version = "2.1-SNAPSHOT";
355         String path = "com/foo/foo-connector/2.1-SNAPSHOT/maven-metadata.xml";
356
357         assertVersionedReference( groupId, artifactId, version, path );
358     }
359
360     private void assertVersionedReference( String groupId, String artifactId, String version, String path )
361         throws RepositoryMetadataException
362     {
363         VersionedReference reference = tools.toVersionedReference( path );
364         assertNotNull( "Reference should not be null.", reference );
365
366         assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
367         assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
368         assertEquals( "VersionedReference.version", version, reference.getVersion() );
369     }
370
371     private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
372         throws Exception
373     {
374         Path repoRootDir = getRepositoryPath( "metadata-repository" );
375
376         VersionedReference reference = new VersionedReference();
377         reference.setGroupId( "org.apache.archiva.metadata.tests" );
378         reference.setArtifactId( artifactId );
379         reference.setVersion( version );
380
381         MavenManagedRepository repo =
382             createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRootDir );
383
384         RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
385
386         ManagedRepositoryContent repoContent =
387             provider.createManagedContent( repo );
388
389         Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
390
391         // Sort the list (for asserts)
392         List<String> testedVersions = new ArrayList<>();
393         testedVersions.addAll( testedVersionSet );
394         Collections.sort( testedVersions, new VersionComparator() );
395
396         // Test the expected array of versions, to the actual tested versions
397         assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
398
399         for ( int i = 0; i < expectedVersions.length; i++ )
400         {
401             String actualVersion = testedVersions.get( i );
402             assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
403         }
404     }
405
406     private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
407                                  ProjectReference reference )
408         throws LayoutException, IOException, SAXException, ParserConfigurationException
409     {
410         Path metadataFile = repository.getRepository().getAsset( "" ).getFilePath().resolve(tools.toPath( reference ) );
411         String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString( metadataFile, Charset.defaultCharset() );
412
413         Diff detailedDiff = DiffBuilder.compare( expectedMetadata ).withTest( actualMetadata ).checkForSimilar().build();
414         if ( detailedDiff.hasDifferences() )
415         {
416             for ( Difference diff : detailedDiff.getDifferences() ) {
417                 System.out.println( diff );
418             }
419             // If it isn't similar, dump the difference.
420             assertEquals( expectedMetadata, actualMetadata );
421         }
422     }
423
424     private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
425                                  VersionedReference reference )
426         throws LayoutException, IOException, SAXException, ParserConfigurationException
427     {
428         Path metadataFile = repository.getRepository().getAsset("").getFilePath().resolve( tools.toPath( reference ) );
429         String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString( metadataFile, Charset.defaultCharset() );
430
431         Diff detailedDiff = DiffBuilder.compare( expectedMetadata ).withTest( actualMetadata ).checkForSimilar().build();
432         if ( detailedDiff.hasDifferences() )
433         {
434             for ( Difference diff : detailedDiff.getDifferences() ) {
435                 System.out.println( diff );
436             }
437             // If it isn't similar, dump the difference.
438             assertEquals( expectedMetadata, actualMetadata );
439         }
440     }
441
442     private void assertMetadataPath( String expected, String actual )
443     {
444         assertEquals( "Repository Specific Metadata Path", expected, actual );
445     }
446
447     private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
448                                                String releaseVersion )
449         throws Exception
450     {
451         ManagedRepositoryContent testRepo = createTestRepoContent();
452         ProjectReference reference = new ProjectReference();
453         reference.setGroupId( "org.apache.archiva.metadata.tests" );
454         reference.setArtifactId( artifactId );
455
456         prepTestRepo( testRepo, reference );
457
458         tools.updateMetadata( testRepo, reference );
459
460         StringBuilder buf = new StringBuilder();
461         buf.append( "<metadata>\n" );
462         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
463         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
464         // buf.append( "  <version>1.0</version>\n" );
465
466         if ( expectedVersions != null )
467         {
468             buf.append( "  <versioning>\n" );
469             if ( latestVersion != null )
470             {
471                 buf.append( "    <latest>" ).append( latestVersion ).append( "</latest>\n" );
472             }
473             if ( releaseVersion != null )
474             {
475                 buf.append( "    <release>" ).append( releaseVersion ).append( "</release>\n" );
476             }
477
478             buf.append( "    <versions>\n" );
479             for ( int i = 0; i < expectedVersions.length; i++ )
480             {
481                 buf.append( "      <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
482             }
483             buf.append( "    </versions>\n" );
484             buf.append( "  </versioning>\n" );
485         }
486         buf.append( "</metadata>" );
487
488         assertMetadata( buf.toString(), testRepo, reference );
489     }
490
491     private void assertProjectMetadata( ManagedRepositoryContent testRepo, ProjectReference reference,
492                                         String artifactId, String[] expectedVersions, String latestVersion,
493                                         String releaseVersion )
494         throws Exception
495     {
496         StringBuilder buf = new StringBuilder();
497         buf.append( "<metadata>\n" );
498         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
499         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
500
501         if ( expectedVersions != null )
502         {
503             buf.append( "  <versioning>\n" );
504             if ( latestVersion != null )
505             {
506                 buf.append( "    <latest>" ).append( latestVersion ).append( "</latest>\n" );
507             }
508             if ( releaseVersion != null )
509             {
510                 buf.append( "    <release>" ).append( releaseVersion ).append( "</release>\n" );
511             }
512
513             buf.append( "    <versions>\n" );
514             for ( int i = 0; i < expectedVersions.length; i++ )
515             {
516                 buf.append( "      <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
517             }
518             buf.append( "    </versions>\n" );
519             buf.append( "  </versioning>\n" );
520         }
521         buf.append( "</metadata>" );
522
523         assertMetadata( buf.toString(), testRepo, reference );
524     }
525
526     private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
527         throws Exception
528     {
529         ManagedRepositoryContent testRepo = createTestRepoContent();
530         VersionedReference reference = new VersionedReference();
531         reference.setGroupId( "org.apache.archiva.metadata.tests" );
532         reference.setArtifactId( artifactId );
533         reference.setVersion( version );
534
535         prepTestRepo( testRepo, reference );
536
537         tools.updateMetadata( testRepo, reference );
538
539         StringBuilder buf = new StringBuilder();
540         buf.append( "<metadata>\n" );
541         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
542         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
543         buf.append( "  <version>" ).append( reference.getVersion() ).append( "</version>\n" );
544         buf.append( "</metadata>" );
545
546         assertMetadata( buf.toString(), testRepo, reference );
547     }
548
549     private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
550                                                        String expectedTime, String expectedBuildNumber )
551         throws Exception
552     {
553         ManagedRepositoryContent testRepo = createTestRepoContent();
554         VersionedReference reference = new VersionedReference();
555         reference.setGroupId( "org.apache.archiva.metadata.tests" );
556         reference.setArtifactId( artifactId );
557         reference.setVersion( version );
558
559         prepTestRepo( testRepo, reference );
560
561         tools.updateMetadata( testRepo, reference );
562
563         StringBuilder buf = new StringBuilder();
564         buf.append( "<metadata>\n" );
565         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
566         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
567         buf.append( "  <version>" ).append( reference.getVersion() ).append( "</version>\n" );
568         buf.append( "  <versioning>\n" );
569         buf.append( "    <snapshot>\n" );
570         buf.append( "      <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
571         buf.append( "      <timestamp>" );
572         buf.append( expectedDate ).append( "." ).append( expectedTime );
573         buf.append( "</timestamp>\n" );
574         buf.append( "    </snapshot>\n" );
575         buf.append( "    <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
576         buf.append( "  </versioning>\n" );
577         buf.append( "</metadata>" );
578
579         assertMetadata( buf.toString(), testRepo, reference );
580     }
581
582     private void removeProxyConnector( String sourceRepoId, String targetRepoId )
583     {
584         ProxyConnectorConfiguration toRemove = null;
585         for ( ProxyConnectorConfiguration pcc : config.getConfiguration().getProxyConnectors() )
586         {
587             if ( pcc.getTargetRepoId().equals( targetRepoId ) && pcc.getSourceRepoId().equals( sourceRepoId ) )
588             {
589                 toRemove = pcc;
590             }
591         }
592         if ( toRemove != null )
593         {
594             config.getConfiguration().removeProxyConnector( toRemove );
595             String prefix = "proxyConnectors.proxyConnector(" + "1" + ")";  // XXX 
596             config.triggerChange( prefix + ".sourceRepoId", toRemove.getSourceRepoId() );
597             config.triggerChange( prefix + ".targetRepoId", toRemove.getTargetRepoId() );
598             config.triggerChange( prefix + ".proxyId", toRemove.getProxyId() );
599             config.triggerChange( prefix + ".policies.releases", toRemove.getPolicy( "releases", "" ) );
600             config.triggerChange( prefix + ".policies.checksum", toRemove.getPolicy( "checksum", "" ) );
601             config.triggerChange( prefix + ".policies.snapshots", toRemove.getPolicy( "snapshots", "" ) );
602             config.triggerChange( prefix + ".policies.cache-failures", toRemove.getPolicy( "cache-failures", "" ) );
603         }
604     }
605
606     private void createProxyConnector( String sourceRepoId, String targetRepoId )
607     {
608         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
609         connectorConfig.setSourceRepoId( sourceRepoId );
610         connectorConfig.setTargetRepoId( targetRepoId );
611         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE.getId() );
612         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS.getId() );
613         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ALWAYS.getId() );
614         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO.getId() );
615
616         int count = config.getConfiguration().getProxyConnectors().size();
617         config.getConfiguration().addProxyConnector( connectorConfig );
618
619         // Proper Triggering ...
620         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
621         config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
622         config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
623         config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
624         config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
625         config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
626         config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
627         config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
628     }
629
630     private ManagedRepositoryContent createTestRepoContent()
631         throws Exception
632     {
633         Path repoRoot = Paths.get( "target/metadata-tests/" + name.getMethodName() );
634         if ( Files.exists(repoRoot) )
635         {
636             org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRoot );
637         }
638
639         Files.createDirectories(repoRoot);
640
641         MavenManagedRepository repoConfig =
642             createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRoot );
643
644         RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
645
646         ManagedRepositoryContent repoContent =
647             provider.createManagedContent( repoConfig );
648         return repoContent;
649     }
650
651     private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
652         throws IOException
653     {
654         String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
655         String path = groupDir + "/" + reference.getArtifactId();
656
657         Path srcRepoDir = getRepositoryPath( "metadata-repository" );
658         Path srcDir = srcRepoDir.resolve( path );
659         Path destDir = repo.getRepository().getAsset( "" ).getFilePath().resolve( path );
660
661         assertTrue( "Source Dir exists: " + srcDir, Files.exists(srcDir) );
662         Files.createDirectories(destDir);
663
664         FileUtils.copyDirectory( srcDir.toFile(), destDir.toFile() );
665     }
666
667     private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
668         throws IOException
669     {
670         ProjectReference projectRef = new ProjectReference();
671         projectRef.setGroupId( reference.getGroupId() );
672         projectRef.setArtifactId( reference.getArtifactId() );
673
674         prepTestRepo( repo, projectRef );
675     }
676
677
678 }