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