]> source.dussan.org Git - archiva.git/blob
03d94f346475bfbda4cee72e3eceac34fb1d03d4
[archiva.git] /
1 package org.apache.archiva.maven.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  * 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.maven.repository.AbstractRepositoryLayerTestCase;
24 import org.apache.archiva.maven.repository.MavenManagedRepository;
25 import org.apache.archiva.maven.repository.metadata.storage.mock.MockConfiguration;
26 import org.apache.archiva.policies.CachedFailuresPolicy;
27 import org.apache.archiva.policies.ChecksumPolicy;
28 import org.apache.archiva.policies.ReleasesPolicy;
29 import org.apache.archiva.policies.SnapshotsPolicy;
30 import org.apache.archiva.repository.ManagedRepositoryContent;
31 import org.apache.archiva.repository.RemoteRepositoryContent;
32 import org.apache.archiva.repository.RepositoryContentProvider;
33 import org.apache.archiva.repository.content.ItemSelector;
34 import org.apache.archiva.repository.content.LayoutException;
35 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
36 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
37 import org.apache.archiva.repository.metadata.base.MetadataTools;
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         ItemSelector reference = ArchivaItemSelector.builder()
166         .withNamespace( "org.apache.archiva.metadata.tests" )
167         .withProjectId( "missing_artifact" ).build();
168
169         prepProjectTestRepo( 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.updateProjectMetadata( 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         ItemSelector selector = ArchivaItemSelector.builder( )
256             .withNamespace( "com.foo" )
257             .withArtifactId( "foo-tool" )
258             .withProjectId( "foo-tool" )
259             .withVersion( "1.0" ).build( );
260
261         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( selector ) );
262     }
263
264     @Test
265     public void testToPathFromProjectReference()
266     {
267         ItemSelector selector = ArchivaItemSelector.builder( )
268             .withNamespace( "com.foo" )
269             .withProjectId( "foo-tool" )
270             .withArtifactId( "foo-tool" ).build( );
271
272         assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( selector ) );
273     }
274
275     @Test
276     public void testToProjectReferenceFooTools()
277         throws RepositoryMetadataException
278     {
279         assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
280     }
281
282     @Test
283     public void testToProjectReferenceAReallyLongPath()
284         throws RepositoryMetadataException
285     {
286         String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
287         String artifactId = "a";
288         String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/maven-metadata.xml";
289
290         assertProjectReference( groupId, artifactId, path );
291     }
292
293     @Test
294     public void testToProjectReferenceCommonsLang()
295         throws RepositoryMetadataException
296     {
297         String groupId = "commons-lang";
298         String artifactId = "commons-lang";
299         String path = "commons-lang/commons-lang/maven-metadata.xml";
300
301         assertProjectReference( groupId, artifactId, path );
302     }
303
304     private void assertProjectReference( String groupId, String artifactId, String path )
305         throws RepositoryMetadataException
306     {
307         ItemSelector reference = tools.toProjectSelector( path );
308
309         assertNotNull( "Reference should not be null.", reference );
310         assertEquals( "ProjectReference.groupId", groupId, reference.getNamespace() );
311         assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
312     }
313
314     @Test
315     public void testToVersionedReferenceFooTool()
316         throws RepositoryMetadataException
317     {
318         String groupId = "com.foo";
319         String artifactId = "foo-tool";
320         String version = "1.0";
321         String path = "com/foo/foo-tool/1.0/maven-metadata.xml";
322
323         assertVersionedReference( groupId, artifactId, version, path );
324     }
325
326     @Test
327     public void testToVersionedReferenceAReallyLongPath()
328         throws RepositoryMetadataException
329     {
330         String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
331         String artifactId = "a";
332         String version = "1.1-alpha-1";
333         String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/1.1-alpha-1/maven-metadata.xml";
334
335         assertVersionedReference( groupId, artifactId, version, path );
336     }
337
338     @Test
339     public void testToVersionedReferenceCommonsLang()
340         throws RepositoryMetadataException
341     {
342         String groupId = "commons-lang";
343         String artifactId = "commons-lang";
344         String version = "2.1";
345         String path = "commons-lang/commons-lang/2.1/maven-metadata.xml";
346
347         assertVersionedReference( groupId, artifactId, version, path );
348     }
349
350     @Test
351     public void testToVersionedReferenceSnapshot()
352         throws RepositoryMetadataException
353     {
354         String groupId = "com.foo";
355         String artifactId = "foo-connector";
356         String version = "2.1-SNAPSHOT";
357         String path = "com/foo/foo-connector/2.1-SNAPSHOT/maven-metadata.xml";
358
359         assertVersionedReference( groupId, artifactId, version, path );
360     }
361
362     private void assertVersionedReference( String groupId, String artifactId, String version, String path )
363         throws RepositoryMetadataException
364     {
365         ItemSelector reference = tools.toVersionedSelector( path );
366         assertNotNull( "Reference should not be null.", reference );
367
368         assertEquals( "VersionedReference.groupId", groupId, reference.getNamespace() );
369         assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
370         assertEquals( "VersionedReference.version", version, reference.getVersion() );
371     }
372
373     private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
374         throws Exception
375     {
376         Path repoRootDir = getRepositoryPath( "metadata-repository" );
377
378         ItemSelector reference = ArchivaItemSelector.builder( )
379             .withNamespace( "org.apache.archiva.metadata.tests" )
380             .withArtifactId( artifactId )
381             .withProjectId( artifactId )
382             .withVersion( version ).build( );
383
384         MavenManagedRepository repo =
385             createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRootDir );
386
387         RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
388
389         ManagedRepositoryContent repoContent =
390             provider.createManagedContent( repo );
391
392         Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
393
394         // Sort the list (for asserts)
395         List<String> testedVersions = new ArrayList<>();
396         testedVersions.addAll( testedVersionSet );
397         Collections.sort( testedVersions, new VersionComparator() );
398
399         // Test the expected array of versions, to the actual tested versions
400         assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
401
402         for ( int i = 0; i < expectedVersions.length; i++ )
403         {
404             String actualVersion = testedVersions.get( i );
405             assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
406         }
407     }
408
409     private void assertProjectMetadata( String expectedMetadata, ManagedRepositoryContent repository,
410                                  ItemSelector reference )
411         throws LayoutException, IOException, SAXException, ParserConfigurationException
412     {
413         Path metadataFile = repository.getRepository().getRoot().getFilePath().resolve(tools.toPath( reference ) );
414         String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString( metadataFile, Charset.defaultCharset() );
415
416         Diff detailedDiff = DiffBuilder.compare( expectedMetadata ).withTest( actualMetadata ).checkForSimilar().build();
417         if ( detailedDiff.hasDifferences() )
418         {
419             for ( Difference diff : detailedDiff.getDifferences() ) {
420                 System.out.println( diff );
421             }
422             // If it isn't similar, dump the difference.
423             assertEquals( expectedMetadata, actualMetadata );
424         }
425     }
426
427     private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
428                                  ItemSelector reference )
429         throws LayoutException, IOException, SAXException, ParserConfigurationException
430     {
431         Path metadataFile = repository.getRepository().getRoot().getFilePath().resolve( tools.toPath( reference ) );
432         String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString( metadataFile, Charset.defaultCharset() );
433
434         Diff detailedDiff = DiffBuilder.compare( expectedMetadata ).withTest( actualMetadata ).checkForSimilar().build();
435         if ( detailedDiff.hasDifferences() )
436         {
437             for ( Difference diff : detailedDiff.getDifferences() ) {
438                 System.out.println( diff );
439             }
440             // If it isn't similar, dump the difference.
441             assertEquals( expectedMetadata, actualMetadata );
442         }
443     }
444
445     private void assertMetadataPath( String expected, String actual )
446     {
447         assertEquals( "Repository Specific Metadata Path", expected, actual );
448     }
449
450     private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
451                                                String releaseVersion )
452         throws Exception
453     {
454         ManagedRepositoryContent testRepo = createTestRepoContent();
455         ItemSelector reference = ArchivaItemSelector.builder( )
456             .withNamespace( "org.apache.archiva.metadata.tests" )
457             .withProjectId( artifactId ).build();
458
459         prepProjectTestRepo( testRepo, reference );
460
461         tools.updateProjectMetadata( testRepo, reference );
462
463         StringBuilder buf = new StringBuilder();
464         buf.append( "<metadata>\n" );
465         buf.append( "  <groupId>" ).append( reference.getNamespace() ).append( "</groupId>\n" );
466         buf.append( "  <artifactId>" ).append( reference.getProjectId() ).append( "</artifactId>\n" );
467         // buf.append( "  <version>1.0</version>\n" );
468
469         if ( expectedVersions != null )
470         {
471             buf.append( "  <versioning>\n" );
472             if ( latestVersion != null )
473             {
474                 buf.append( "    <latest>" ).append( latestVersion ).append( "</latest>\n" );
475             }
476             if ( releaseVersion != null )
477             {
478                 buf.append( "    <release>" ).append( releaseVersion ).append( "</release>\n" );
479             }
480
481             buf.append( "    <versions>\n" );
482             for ( int i = 0; i < expectedVersions.length; i++ )
483             {
484                 buf.append( "      <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
485             }
486             buf.append( "    </versions>\n" );
487             buf.append( "  </versioning>\n" );
488         }
489         buf.append( "</metadata>" );
490
491         assertProjectMetadata( buf.toString(), testRepo, reference );
492     }
493
494     private void assertProjectMetadata( ManagedRepositoryContent testRepo, ItemSelector reference,
495                                         String artifactId, String[] expectedVersions, String latestVersion,
496                                         String releaseVersion )
497         throws Exception
498     {
499         StringBuilder buf = new StringBuilder();
500         buf.append( "<metadata>\n" );
501         buf.append( "  <groupId>" ).append( reference.getNamespace() ).append( "</groupId>\n" );
502         buf.append( "  <artifactId>" ).append( reference.getProjectId() ).append( "</artifactId>\n" );
503
504         if ( expectedVersions != null )
505         {
506             buf.append( "  <versioning>\n" );
507             if ( latestVersion != null )
508             {
509                 buf.append( "    <latest>" ).append( latestVersion ).append( "</latest>\n" );
510             }
511             if ( releaseVersion != null )
512             {
513                 buf.append( "    <release>" ).append( releaseVersion ).append( "</release>\n" );
514             }
515
516             buf.append( "    <versions>\n" );
517             for ( int i = 0; i < expectedVersions.length; i++ )
518             {
519                 buf.append( "      <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
520             }
521             buf.append( "    </versions>\n" );
522             buf.append( "  </versioning>\n" );
523         }
524         buf.append( "</metadata>" );
525
526         assertProjectMetadata( buf.toString(), testRepo, reference );
527     }
528
529
530     private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
531         throws Exception
532     {
533         ManagedRepositoryContent testRepo = createTestRepoContent();
534
535         ItemSelector selector = ArchivaItemSelector.builder()
536             .withNamespace(  "org.apache.archiva.metadata.tests" )
537             .withProjectId(  artifactId )
538             .withArtifactId( artifactId )
539             .withVersion(  version ).build();
540
541         prepTestRepo( testRepo, selector );
542
543         tools.updateVersionMetadata( testRepo, selector );
544
545         StringBuilder buf = new StringBuilder();
546         buf.append( "<metadata>\n" );
547         buf.append( "  <groupId>" ).append( selector.getNamespace() ).append( "</groupId>\n" );
548         buf.append( "  <artifactId>" ).append( selector.getArtifactId() ).append( "</artifactId>\n" );
549         buf.append( "  <version>" ).append( selector.getVersion() ).append( "</version>\n" );
550         buf.append( "</metadata>" );
551
552         assertMetadata( buf.toString(), testRepo, selector );
553     }
554
555     private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
556                                                        String expectedTime, String expectedBuildNumber )
557         throws Exception
558     {
559         ManagedRepositoryContent testRepo = createTestRepoContent();
560         ItemSelector reference = ArchivaItemSelector.builder()
561         .withNamespace( "org.apache.archiva.metadata.tests" )
562         .withArtifactId( artifactId )
563             .withProjectId( artifactId )
564             .withVersion(  version ).build();
565
566         prepTestRepo( testRepo, reference );
567
568         tools.updateVersionMetadata( testRepo, reference );
569
570         StringBuilder buf = new StringBuilder();
571         buf.append( "<metadata>\n" );
572         buf.append( "  <groupId>" ).append( reference.getNamespace() ).append( "</groupId>\n" );
573         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
574         buf.append( "  <version>" ).append( reference.getVersion() ).append( "</version>\n" );
575         buf.append( "  <versioning>\n" );
576         buf.append( "    <snapshot>\n" );
577         buf.append( "      <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
578         buf.append( "      <timestamp>" );
579         buf.append( expectedDate ).append( "." ).append( expectedTime );
580         buf.append( "</timestamp>\n" );
581         buf.append( "    </snapshot>\n" );
582         buf.append( "    <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
583         buf.append( "  </versioning>\n" );
584         buf.append( "</metadata>" );
585
586         assertMetadata( buf.toString(), testRepo, reference );
587     }
588
589     private void removeProxyConnector( String sourceRepoId, String targetRepoId )
590     {
591         ProxyConnectorConfiguration toRemove = null;
592         for ( ProxyConnectorConfiguration pcc : config.getConfiguration().getProxyConnectors() )
593         {
594             if ( pcc.getTargetRepoId().equals( targetRepoId ) && pcc.getSourceRepoId().equals( sourceRepoId ) )
595             {
596                 toRemove = pcc;
597             }
598         }
599         if ( toRemove != null )
600         {
601             config.getConfiguration().removeProxyConnector( toRemove );
602             String prefix = "proxyConnectors.proxyConnector(" + "1" + ")";  // XXX 
603             config.triggerChange( prefix + ".sourceRepoId", toRemove.getSourceRepoId() );
604             config.triggerChange( prefix + ".targetRepoId", toRemove.getTargetRepoId() );
605             config.triggerChange( prefix + ".proxyId", toRemove.getProxyId() );
606             config.triggerChange( prefix + ".policies.releases", toRemove.getPolicy( "releases", "" ) );
607             config.triggerChange( prefix + ".policies.checksum", toRemove.getPolicy( "checksum", "" ) );
608             config.triggerChange( prefix + ".policies.snapshots", toRemove.getPolicy( "snapshots", "" ) );
609             config.triggerChange( prefix + ".policies.cache-failures", toRemove.getPolicy( "cache-failures", "" ) );
610         }
611     }
612
613     private void createProxyConnector( String sourceRepoId, String targetRepoId )
614     {
615         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
616         connectorConfig.setSourceRepoId( sourceRepoId );
617         connectorConfig.setTargetRepoId( targetRepoId );
618         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE.getId() );
619         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS.getId() );
620         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ALWAYS.getId() );
621         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO.getId() );
622
623         int count = config.getConfiguration().getProxyConnectors().size();
624         config.getConfiguration().addProxyConnector( connectorConfig );
625
626         // Proper Triggering ...
627         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
628         config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
629         config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
630         config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
631         config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
632         config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
633         config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
634         config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
635     }
636
637     private ManagedRepositoryContent createTestRepoContent()
638         throws Exception
639     {
640         Path repoRoot = Paths.get( "target/metadata-tests/" + name.getMethodName() );
641         if ( Files.exists(repoRoot) )
642         {
643             org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRoot );
644         }
645
646         Files.createDirectories(repoRoot);
647
648         MavenManagedRepository repoConfig =
649             createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRoot );
650
651         RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
652
653         ManagedRepositoryContent repoContent =
654             provider.createManagedContent( repoConfig );
655         return repoContent;
656     }
657
658     private void prepProjectTestRepo( ManagedRepositoryContent repo, ItemSelector reference)
659         throws IOException
660     {
661         String groupDir = StringUtils.replaceChars( reference.getNamespace(), '.', '/' );
662         String path = groupDir + "/" + reference.getArtifactId();
663
664         Path srcRepoDir = getRepositoryPath( "metadata-repository" );
665         Path srcDir = srcRepoDir.resolve( path );
666         Path destDir = repo.getRepository().getRoot().getFilePath().resolve( path );
667
668         assertTrue( "Source Dir exists: " + srcDir, Files.exists(srcDir) );
669         Files.createDirectories(destDir);
670
671         FileUtils.copyDirectory( srcDir.toFile(), destDir.toFile() );
672     }
673
674     private void prepTestRepo( ManagedRepositoryContent repo, ItemSelector reference )
675         throws IOException
676     {
677         String groupDir = StringUtils.replaceChars( reference.getNamespace(), '.', '/' );
678         String path = groupDir + "/" + reference.getArtifactId();
679
680         Path srcRepoDir = getRepositoryPath( "metadata-repository" );
681         Path srcDir = srcRepoDir.resolve( path );
682         Path destDir = repo.getRepository().getRoot().getFilePath().resolve( path );
683
684         assertTrue( "Source Dir exists: " + srcDir, Files.exists(srcDir) );
685         Files.createDirectories(destDir);
686
687         FileUtils.copyDirectory( srcDir.toFile(), destDir.toFile() );
688     }
689
690
691 }