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