]> source.dussan.org Git - archiva.git/blob
a46a1a5b62b2de991b498ca75dba854b65fe71da
[archiva.git] /
1 package org.apache.archiva.configuration;
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 junit.framework.TestCase;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.codehaus.plexus.registry.RegistryException;
27 import org.codehaus.redback.components.springutils.ComponentContainer;
28 import org.custommonkey.xmlunit.XMLAssert;
29 import org.easymock.MockControl;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.test.context.ContextConfiguration;
35 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36
37 import javax.inject.Inject;
38 import java.io.File;
39 import java.util.List;
40 import java.util.Map;
41
42 /**
43  * Test the configuration store.
44  */
45 @RunWith( SpringJUnit4ClassRunner.class )
46 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
47 public class ArchivaConfigurationTest
48     extends TestCase
49 {
50
51     private Logger log = LoggerFactory.getLogger( getClass( ) );
52
53     @Inject
54     private ComponentContainer componentContainer;
55
56     public static File getTestFile( String path )
57     {
58         return new File( FileUtil.getBasedir( ), path );
59     }
60
61     protected <T> T lookup( Class<T> clazz, String hint )
62     {
63         return componentContainer.getComponent( clazz, hint );
64     }
65
66     protected <T> T lookup( Class<T> clazz )
67     {
68         return componentContainer.getComponent( clazz );
69     }
70
71     @Test
72     public void testGetConfigurationFromRegistryWithASingleNamedConfigurationResource( )
73         throws Exception
74     {
75         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
76
77         Configuration configuration = archivaConfiguration.getConfiguration( );
78         assertConfiguration( configuration, 2, 2, 2 );
79         assertEquals( "check network proxies", 1, configuration.getNetworkProxies( ).size( ) );
80
81         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories( ).get( 0 );
82
83         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
84                       repository.getLocation( ) );
85         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
86         assertEquals( "check managed repositories", "internal", repository.getId( ) );
87         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
88         assertTrue( "check managed repositories", repository.isScanned( ) );
89     }
90
91     @Test
92     public void testGetConfigurationFromDefaults( )
93         throws Exception
94     {
95         ArchivaConfiguration archivaConfiguration =
96             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-defaults" );
97
98         Configuration configuration = archivaConfiguration.getConfiguration( );
99         assertConfiguration( configuration, 2, 1, 1 );
100         assertEquals( "check network proxies", 0, configuration.getNetworkProxies( ).size( ) );
101
102         ManagedRepositoryConfiguration repository =
103             (ManagedRepositoryConfiguration) configuration.getManagedRepositories( ).get( 0 );
104
105         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
106                       repository.getLocation( ) );
107         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
108         assertEquals( "check managed repositories", "internal", repository.getId( ) );
109         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
110         assertTrue( "check managed repositories", repository.isScanned( ) );
111     }
112
113     // test for [MRM-789]
114     @Test
115     public void testGetConfigurationFromDefaultsWithDefaultRepoLocationAlreadyExisting( )
116         throws Exception
117     {
118         File repo = new File( FileUtil.getBasedir( ), "/target/test-classes/existing_snapshots" );
119         repo.mkdirs( );
120
121         repo = new File( FileUtil.getBasedir( ), "/target/test-classes/existing_internal" );
122         repo.mkdirs( );
123
124         String existingTestDefaultArchivaConfigFile = FileUtils.readFileToString(
125             getTestFile( "target/test-classes/org/apache/archiva/configuration/test-default-archiva.xml" ) );
126         existingTestDefaultArchivaConfigFile =
127             StringUtils.replace( existingTestDefaultArchivaConfigFile, "${appserver.base}", FileUtil.getBasedir( ) );
128
129         File generatedTestDefaultArchivaConfigFile = new File( FileUtil.getBasedir( ),
130                                                                "target/test-classes/org/apache/archiva/configuration/default-archiva.xml" );
131
132         FileUtils.writeStringToFile( generatedTestDefaultArchivaConfigFile, existingTestDefaultArchivaConfigFile,
133                                      null );
134
135         ArchivaConfiguration archivaConfiguration =
136             lookup( ArchivaConfiguration.class, "test-defaults-default-repo-location-exists" );
137
138         Configuration configuration = archivaConfiguration.getConfiguration( );
139         assertConfiguration( configuration, 2, 1, 1 );
140
141         ManagedRepositoryConfiguration repository =
142             (ManagedRepositoryConfiguration) configuration.getManagedRepositories( ).get( 0 );
143         assertTrue( "check managed repositories", repository.getLocation( ).endsWith( "data/repositories/internal" ) );
144
145         generatedTestDefaultArchivaConfigFile.delete( );
146         assertFalse( generatedTestDefaultArchivaConfigFile.exists( ) );
147     }
148
149     private void assertConfiguration( Configuration configuration )
150         throws Exception
151     {
152         assertConfiguration( configuration, 2, 2, 1 );
153     }
154
155     /**
156      * Ensures that the provided configuration matches the details present in the archiva-default.xml file.
157      */
158     private void assertConfiguration( Configuration configuration, int managedExpected, int remoteExpected,
159                                       int proxyConnectorExpected )
160         throws Exception
161     {
162         FileTypes filetypes = lookup( FileTypes.class );
163
164         assertEquals( "check managed repositories: " + configuration.getManagedRepositories( ), managedExpected,
165                       configuration.getManagedRepositories( ).size( ) );
166         assertEquals( "check remote repositories: " + configuration.getRemoteRepositories( ), remoteExpected,
167                       configuration.getRemoteRepositories( ).size( ) );
168         assertEquals( "check proxy connectors:" + configuration.getProxyConnectors( ), proxyConnectorExpected,
169                       configuration.getProxyConnectors( ).size( ) );
170
171         RepositoryScanningConfiguration repoScanning = configuration.getRepositoryScanning( );
172         assertNotNull( "check repository scanning", repoScanning );
173         assertEquals( "check file types", 4, repoScanning.getFileTypes( ).size( ) );
174         assertEquals( "check known consumers", 9, repoScanning.getKnownContentConsumers( ).size( ) );
175         assertEquals( "check invalid consumers", 1, repoScanning.getInvalidContentConsumers( ).size( ) );
176
177         List<String> patterns = filetypes.getFileTypePatterns( "artifacts" );
178         assertNotNull( "check 'artifacts' file type", patterns );
179         assertEquals( "check 'artifacts' patterns", 13, patterns.size( ) );
180
181         WebappConfiguration webapp = configuration.getWebapp( );
182         assertNotNull( "check webapp", webapp );
183
184         UserInterfaceOptions ui = webapp.getUi( );
185         assertNotNull( "check webapp ui", ui );
186         assertTrue( "check showFindArtifacts", ui.isShowFindArtifacts( ) );
187         assertTrue( "check appletFindEnabled", ui.isAppletFindEnabled( ) );
188     }
189
190     @Test
191     public void testGetConfigurationFromRegistryWithTwoConfigurationResources( )
192         throws Exception
193     {
194         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration-both" );
195
196         Configuration configuration = archivaConfiguration.getConfiguration( );
197
198         // from base
199         assertEquals( "check repositories", 2, configuration.getManagedRepositories( ).size( ) );
200         assertEquals( "check repositories", 2, configuration.getRemoteRepositories( ).size( ) );
201         // from user
202         assertEquals( "check proxy connectors", 2, configuration.getProxyConnectors( ).size( ) );
203
204         WebappConfiguration webapp = configuration.getWebapp( );
205         assertNotNull( "check webapp", webapp );
206
207         UserInterfaceOptions ui = webapp.getUi( );
208         assertNotNull( "check webapp ui", ui );
209         // from base
210         assertFalse( "check showFindArtifacts", ui.isShowFindArtifacts( ) );
211         // from user
212         assertFalse( "check appletFindEnabled", ui.isAppletFindEnabled( ) );
213     }
214
215     @Test
216     public void testGetConfigurationSystemOverride( )
217         throws Exception
218     {
219
220         System.setProperty( "org.apache.archiva.webapp.ui.appletFindEnabled", "false" );
221
222         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
223
224         archivaConfiguration.reload( );
225
226         try
227         {
228             Configuration configuration = archivaConfiguration.getConfiguration( );
229
230             assertFalse( "check boolean", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
231         }
232         finally
233         {
234             System.getProperties( ).remove( "org.apache.archiva.webapp.ui.appletFindEnabled" );
235         }
236     }
237
238     @Test
239     public void testStoreConfiguration( )
240         throws Exception
241     {
242         File file = getTestFile( "target/test/test-file.xml" );
243         file.delete( );
244         assertFalse( file.exists( ) );
245
246         // TODO: remove with commons-configuration 1.4
247         //file.getParentFile().mkdirs();
248         //FileUtils.writeStringToFile( file, "<configuration/>", null );
249
250         DefaultArchivaConfiguration archivaConfiguration =
251             (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save" );
252
253         archivaConfiguration.reload( );
254
255         Configuration configuration = new Configuration( );
256         configuration.setVersion( "1" );
257         configuration.setWebapp( new WebappConfiguration( ) );
258         configuration.getWebapp( ).setUi( new UserInterfaceOptions( ) );
259         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
260
261         // add a change listener
262         MockControl control = createConfigurationListenerMockControl( );
263         ConfigurationListener listener = (ConfigurationListener) control.getMock( );
264         archivaConfiguration.addListener( listener );
265
266         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
267         control.setVoidCallable( );
268
269         control.replay( );
270
271         archivaConfiguration.save( configuration );
272
273         control.verify( );
274
275         assertTrue( "Check file exists", file.exists( ) );
276
277         // check it
278         configuration = archivaConfiguration.getConfiguration( );
279         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
280
281         // read it back
282         archivaConfiguration = (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-read-saved" );
283
284         archivaConfiguration.reload( );
285         configuration = archivaConfiguration.getConfiguration( );
286         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
287     }
288
289     private static MockControl createConfigurationListenerMockControl( )
290     {
291         return MockControl.createControl( ConfigurationListener.class );
292     }
293
294     @Test
295     public void testStoreConfigurationUser( )
296         throws Exception
297     {
298         File baseFile = getTestFile( "target/test/test-file.xml" );
299         baseFile.delete( );
300         assertFalse( baseFile.exists( ) );
301
302         File userFile = getTestFile( "target/test/test-file-user.xml" );
303         userFile.delete( );
304         assertFalse( userFile.exists( ) );
305
306         userFile.getParentFile( ).mkdirs( );
307         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
308
309         ArchivaConfiguration archivaConfiguration =
310             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
311
312         Configuration configuration = new Configuration( );
313         configuration.setWebapp( new WebappConfiguration( ) );
314         configuration.getWebapp( ).setUi( new UserInterfaceOptions( ) );
315         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
316
317         archivaConfiguration.save( configuration );
318
319         assertTrue( "Check file exists", userFile.exists( ) );
320         assertFalse( "Check file not created", baseFile.exists( ) );
321
322         // check it
323         configuration = archivaConfiguration.getConfiguration( );
324         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
325     }
326
327     @Test
328     public void testStoreConfigurationLoadedFromDefaults( )
329         throws Exception
330     {
331         File baseFile = getTestFile( "target/test/test-file.xml" );
332         baseFile.delete( );
333         assertFalse( baseFile.exists( ) );
334
335         File userFile = getTestFile( "target/test/test-file-user.xml" );
336         userFile.delete( );
337         assertFalse( userFile.exists( ) );
338
339         ArchivaConfiguration archivaConfiguration =
340             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-defaults" );
341
342         archivaConfiguration.reload( );
343
344         Configuration configuration = new Configuration( );
345         configuration.setWebapp( new WebappConfiguration( ) );
346         configuration.getWebapp( ).setUi( new UserInterfaceOptions( ) );
347         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
348
349         // add a change listener
350         MockControl control = createConfigurationListenerMockControl( );
351         ConfigurationListener listener = (ConfigurationListener) control.getMock( );
352         archivaConfiguration.addListener( listener );
353
354         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
355
356         control.setVoidCallable( 1 );
357
358         control.replay( );
359
360         archivaConfiguration.save( configuration );
361
362         control.verify( );
363
364         assertTrue( "Check file exists", userFile.exists( ) );
365         assertFalse( "Check file not created", baseFile.exists( ) );
366
367         // check it
368         configuration = archivaConfiguration.getConfiguration( );
369         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
370     }
371
372     @Test
373     public void testDefaultUserConfigFilename( )
374         throws Exception
375     {
376         DefaultArchivaConfiguration archivaConfiguration =
377             (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "default" );
378
379         archivaConfiguration.reload( );
380
381         assertEquals( System.getProperty( "user.home" ) + "/.m2/archiva.xml",
382                       archivaConfiguration.getUserConfigFilename( ) );
383         assertEquals( System.getProperty( "appserver.base", "${appserver.base}" ) + "/conf/archiva.xml",
384                       archivaConfiguration.getAltConfigFilename( ) );
385     }
386
387     @Test
388     public void testStoreConfigurationFallback( )
389         throws Exception
390     {
391         File baseFile = getTestFile( "target/test/test-file.xml" );
392         baseFile.delete( );
393         assertFalse( baseFile.exists( ) );
394
395         File userFile = getTestFile( "target/test/test-file-user.xml" );
396         userFile.delete( );
397         assertFalse( userFile.exists( ) );
398
399         baseFile.getParentFile( ).mkdirs( );
400         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
401
402         ArchivaConfiguration archivaConfiguration =
403             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-fallback" );
404
405         archivaConfiguration.reload( );
406
407         Configuration configuration = new Configuration( );
408         configuration.setWebapp( new WebappConfiguration( ) );
409         configuration.getWebapp( ).setUi( new UserInterfaceOptions( ) );
410         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
411
412         archivaConfiguration.save( configuration );
413
414         assertTrue( "Check file exists", baseFile.exists( ) );
415         assertFalse( "Check file not created", userFile.exists( ) );
416
417         // check it
418         configuration = archivaConfiguration.getConfiguration( );
419         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
420     }
421
422     @Test
423     public void testStoreConfigurationFailsWhenReadFromBothLocationsNoLists( )
424         throws Exception
425     {
426         File baseFile = getTestFile( "target/test/test-file.xml" );
427         baseFile.delete( );
428         assertFalse( baseFile.exists( ) );
429
430         File userFile = getTestFile( "target/test/test-file-user.xml" );
431         userFile.delete( );
432         assertFalse( userFile.exists( ) );
433
434         baseFile.getParentFile( ).mkdirs( );
435         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
436
437         userFile.getParentFile( ).mkdirs( );
438         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
439
440         ArchivaConfiguration archivaConfiguration =
441             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
442
443         archivaConfiguration.reload( );
444
445         Configuration configuration = archivaConfiguration.getConfiguration( );
446         assertTrue( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
447
448         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
449
450         archivaConfiguration.save( configuration );
451
452         assertTrue( "Check file exists", baseFile.exists( ) );
453         assertEquals( "Check base file is unchanged", "<configuration/>",
454                       FileUtils.readFileToString( baseFile, null ) );
455         assertTrue( "Check file exists", userFile.exists( ) );
456         assertFalse( "Check base file is changed",
457                      "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
458
459         // check it
460         configuration = archivaConfiguration.getConfiguration( );
461         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
462     }
463
464     @Test
465     public void testStoreConfigurationFailsWhenReadFromBothLocationsUserHasLists( )
466         throws Exception
467     {
468         File baseFile = getTestFile( "target/test/test-file.xml" );
469         baseFile.delete( );
470         assertFalse( baseFile.exists( ) );
471
472         File userFile = getTestFile( "target/test/test-file-user.xml" );
473         userFile.delete( );
474         assertFalse( userFile.exists( ) );
475
476         userFile.getParentFile( ).mkdirs( );
477         FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ), userFile );
478
479         baseFile.getParentFile( ).mkdirs( );
480         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
481
482         ArchivaConfiguration archivaConfiguration =
483             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
484
485         archivaConfiguration.reload( );
486
487         Configuration configuration = archivaConfiguration.getConfiguration( );
488         assertTrue( "check value", configuration.getWebapp( ).getUi( ).isShowFindArtifacts( ) );
489
490         configuration.getWebapp( ).getUi( ).setShowFindArtifacts( false );
491
492         archivaConfiguration.save( configuration );
493
494         assertTrue( "Check file exists", baseFile.exists( ) );
495         assertEquals( "Check base file is unchanged", "<configuration/>",
496                       FileUtils.readFileToString( baseFile, null ) );
497         assertTrue( "Check file exists", userFile.exists( ) );
498         assertFalse( "Check base file is changed",
499                      "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
500
501         // check it
502         configuration = archivaConfiguration.getConfiguration( );
503         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isShowFindArtifacts( ) );
504     }
505
506     @Test
507     public void testStoreConfigurationFailsWhenReadFromBothLocationsAppserverHasLists( )
508         throws Exception
509     {
510         File baseFile = getTestFile( "target/test/test-file.xml" );
511         baseFile.delete( );
512         assertFalse( baseFile.exists( ) );
513
514         File userFile = getTestFile( "target/test/test-file-user.xml" );
515         userFile.delete( );
516         assertFalse( userFile.exists( ) );
517
518         baseFile.getParentFile( ).mkdirs( );
519         FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ), baseFile );
520
521         userFile.getParentFile( ).mkdirs( );
522         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
523
524         ArchivaConfiguration archivaConfiguration =
525             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
526
527         archivaConfiguration.reload( );
528
529         Configuration configuration = archivaConfiguration.getConfiguration( );
530         assertTrue( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
531
532         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
533
534         try
535         {
536             archivaConfiguration.save( configuration );
537             fail( "Configuration saving should not succeed if it was loaded from two locations" );
538         }
539         catch ( IndeterminateConfigurationException e )
540         {
541             // check it was reverted
542             configuration = archivaConfiguration.getConfiguration( );
543             assertTrue( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
544         }
545     }
546
547     @Test
548     public void testLoadConfigurationFromInvalidBothLocationsOnDisk( )
549         throws Exception
550     {
551         ArchivaConfiguration archivaConfiguration =
552             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-both" );
553         Configuration config = archivaConfiguration.getConfiguration( );
554
555         try
556         {
557             archivaConfiguration.save( config );
558             fail( "Should have thrown a RegistryException because the configuration can't be saved." );
559         }
560         catch ( RegistryException e )
561         {
562             /* expected exception */
563         }
564     }
565
566     @Test
567     public void testLoadConfigurationFromInvalidUserLocationOnDisk( )
568         throws Exception
569     {
570         File testConfDir = getTestFile( "target/test-appserver-base/conf/" );
571         testConfDir.mkdirs( );
572
573         ArchivaConfiguration archivaConfiguration =
574             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-user" );
575         Configuration config = archivaConfiguration.getConfiguration( );
576         archivaConfiguration.save( config );
577         // No Exception == test passes.
578         // Expected Path is: Should not have thrown an exception.
579     }
580
581     @Test
582     public void testConfigurationUpgradeFrom09( )
583         throws Exception
584     {
585         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-09" );
586
587         // we just use the defaults when upgrading from 0.9 at this point.
588         Configuration configuration = archivaConfiguration.getConfiguration( );
589         // test-upgrade-09 contains a managed with id: local so it's 3 managed
590         assertConfiguration( configuration, 3, 1, 1 );
591         assertEquals( "check network proxies", 0, configuration.getNetworkProxies( ).size( ) );
592
593         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories( ).get( 0 );
594
595         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
596                       repository.getLocation( ) );
597         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
598         assertEquals( "check managed repositories", "internal", repository.getId( ) );
599         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
600         assertTrue( "check managed repositories", repository.isScanned( ) );
601     }
602
603     @Test
604     public void testConfigurationUpgradeFrom13( )
605         throws Exception
606     {
607         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-1.3" );
608
609         // we just use the defaults when upgrading from 1.3 at this point.
610         Configuration configuration = archivaConfiguration.getConfiguration( );
611         assertConfiguration( configuration, 2, 2, 2 );
612         assertEquals( "check network proxies", 0, configuration.getNetworkProxies( ).size( ) );
613
614         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories( ).get( 0 );
615
616         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
617                       repository.getLocation( ) );
618         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
619         assertEquals( "check managed repositories", "internal", repository.getId( ) );
620         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
621         assertTrue( "check managed repositories", repository.isScanned( ) );
622
623         log.info( "knowContentConsumers " + configuration.getRepositoryScanning( ).getKnownContentConsumers( ) );
624
625         assertFalse(
626             configuration.getRepositoryScanning( ).getKnownContentConsumers( ).contains( "update-db-artifact" ) );
627         assertFalse( configuration.getRepositoryScanning( ).getKnownContentConsumers( ).contains(
628             "update-db-repository-metadata" ) );
629
630         assertTrue(
631             configuration.getRepositoryScanning( ).getKnownContentConsumers( ).contains( "create-archiva-metadata" ) );
632     }
633
634     @Test
635     public void testAutoDetectV1( )
636         throws Exception
637     {
638         // Setup the autodetect-v1.xml file in the target directory (so we can save/load it)
639         File userFile = getTestFile( "target/test-autodetect-v1/archiva-user.xml" );
640         userFile.delete( );
641         assertFalse( userFile.exists( ) );
642
643         userFile.getParentFile( ).mkdirs( );
644         FileUtils.copyFile( getTestFile( "src/test/conf/autodetect-v1.xml" ), userFile );
645
646         // Load the original (unconverted) archiva.xml
647         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
648
649         archivaConfiguration.reload( );
650
651         Configuration configuration = archivaConfiguration.getConfiguration( );
652         assertConfiguration( configuration, 2, 2, 2 );
653         assertEquals( "check network proxies", 1, configuration.getNetworkProxies( ).size( ) );
654
655         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories( ).get( 0 );
656
657         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
658                       repository.getLocation( ) );
659         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
660         assertEquals( "check managed repositories", "internal", repository.getId( ) );
661         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
662         assertTrue( "check managed repositories", repository.isScanned( ) );
663
664         // Test that only 1 set of repositories exist.
665         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories( ).size( ) );
666         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories( ).size( ) );
667         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories( ).size( ) );
668
669         // Save the file.
670         archivaConfiguration.save( configuration );
671
672         // Release existing
673         //release( archivaConfiguration );
674
675         // Reload.
676         archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
677         configuration = archivaConfiguration.getConfiguration( );
678
679         // Test that only 1 set of repositories exist.
680         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories( ).size( ) );
681         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositoriesAsMap( ).size( ) );
682         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories( ).size( ) );
683         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositoriesAsMap( ).size( ) );
684         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories( ).size( ) );
685
686         String actualXML = FileUtils.readFileToString( userFile, null );
687         XMLAssert.assertXpathNotExists( "//configuration/repositories/repository", actualXML );
688         XMLAssert.assertXpathNotExists( "//configuration/repositories", actualXML );
689     }
690
691     @Test
692     public void testArchivaV1( )
693         throws Exception
694     {
695         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-archiva-v1" );
696
697         Configuration configuration = archivaConfiguration.getConfiguration( );
698         assertConfiguration( configuration, 2, 2, 2 );
699         assertEquals( "check network proxies", 1, configuration.getNetworkProxies( ).size( ) );
700
701         assertEquals( "check managed repositories", 2, configuration.getManagedRepositories( ).size( ) );
702         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories( ).size( ) );
703
704         Map<String, ManagedRepositoryConfiguration> map = configuration.getManagedRepositoriesAsMap( );
705
706         ManagedRepositoryConfiguration repository = map.get( "internal" );
707         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
708                       repository.getLocation( ) );
709         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
710         assertEquals( "check managed repositories", "internal", repository.getId( ) );
711         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
712         assertTrue( "check managed repositories", repository.isScanned( ) );
713         assertFalse( "check managed repositories", repository.isSnapshots( ) );
714
715         repository = map.get( "snapshots" );
716         assertEquals( "check managed repositories", "${appserver.base}/repositories/snapshots",
717                       repository.getLocation( ) );
718         assertEquals( "check managed repositories", "Archiva Managed Snapshot Repository", repository.getName( ) );
719         assertEquals( "check managed repositories", "snapshots", repository.getId( ) );
720         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
721         assertFalse( "check managed repositories", repository.isScanned( ) );
722         assertTrue( "check managed repositories", repository.isSnapshots( ) );
723     }
724
725     @Test
726     public void testCronExpressionsWithComma( )
727         throws Exception
728     {
729         File baseFile = getTestFile( "target/test/test-file.xml" );
730         baseFile.delete( );
731         assertFalse( baseFile.exists( ) );
732
733         File userFile = getTestFile( "target/test/test-file-user.xml" );
734         userFile.delete( );
735         assertFalse( userFile.exists( ) );
736
737         baseFile.getParentFile( ).mkdirs( );
738         FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ), baseFile );
739
740         userFile.getParentFile( ).mkdirs( );
741         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
742
743         final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
744
745         archivaConfiguration.reload( );
746
747         Configuration configuration = archivaConfiguration.getConfiguration( );
748
749         ManagedRepositoryConfiguration repository =
750             (ManagedRepositoryConfiguration) configuration.getManagedRepositories( ).get( 0 );
751
752         assertEquals( "check cron expression", "0 0,30 * * * ?", repository.getRefreshCronExpression( ).trim( ) );
753
754         // add a test listener to confirm it doesn't see the escaped format. We don't need to test the number of calls,
755         // etc. as it's done in other tests
756         archivaConfiguration.addListener( new ConfigurationListener( )
757         {
758             public void configurationEvent( ConfigurationEvent event )
759             {
760                 assertEquals( ConfigurationEvent.SAVED, event.getType( ) );
761
762             }
763         } );
764
765         archivaConfiguration.save( configuration );
766
767         configuration = archivaConfiguration.getConfiguration( );
768
769         // test for the escape character '\' showing up on repositories.jsp
770         repository.setRefreshCronExpression( "0 0,20 0 * * ?" );
771
772         archivaConfiguration.save( configuration );
773
774         repository = archivaConfiguration.getConfiguration( ).findManagedRepositoryById( "snapshots" );
775
776         assertEquals( "check cron expression", "0 0,20 0 * * ?", repository.getRefreshCronExpression( ) );
777     }
778
779     @Test
780     public void testRemoveLastElements( )
781         throws Exception
782     {
783         File baseFile = getTestFile( "target/test/test-file.xml" );
784         baseFile.delete( );
785         assertFalse( baseFile.exists( ) );
786
787         File userFile = getTestFile( "target/test/test-file-user.xml" );
788         userFile.delete( );
789         assertFalse( userFile.exists( ) );
790
791         baseFile.getParentFile( ).mkdirs( );
792         FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ), baseFile );
793
794         userFile.getParentFile( ).mkdirs( );
795         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
796
797         ArchivaConfiguration archivaConfiguration =
798             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-remove-central" );
799
800         archivaConfiguration.reload( );
801
802         Configuration configuration = archivaConfiguration.getConfiguration( );
803
804         RepositoryGroupConfiguration repositoryGroup =
805             (RepositoryGroupConfiguration) configuration.getRepositoryGroups( ).get( 0 );
806         assertNotNull( repositoryGroup );
807         configuration.removeRepositoryGroup( repositoryGroup );
808         assertTrue( configuration.getRepositoryGroups( ).isEmpty( ) );
809
810         RemoteRepositoryConfiguration repository = configuration.getRemoteRepositoriesAsMap( ).get( "central" );
811         assertNotNull( repository );
812         configuration.removeRemoteRepository( repository );
813         assertTrue( configuration.getRemoteRepositories( ).isEmpty( ) );
814
815         ManagedRepositoryConfiguration managedRepository =
816             configuration.getManagedRepositoriesAsMap( ).get( "snapshots" );
817         assertNotNull( managedRepository );
818         configuration.removeManagedRepository( managedRepository );
819         assertTrue( configuration.getManagedRepositories( ).isEmpty( ) );
820
821         ProxyConnectorConfiguration proxyConnector =
822             (ProxyConnectorConfiguration) configuration.getProxyConnectors( ).get( 0 );
823         assertNotNull( proxyConnector );
824         configuration.removeProxyConnector( proxyConnector );
825         assertTrue( configuration.getProxyConnectors( ).isEmpty( ) );
826
827         NetworkProxyConfiguration networkProxy = configuration.getNetworkProxiesAsMap( ).get( "proxy" );
828         assertNotNull( networkProxy );
829         configuration.removeNetworkProxy( networkProxy );
830         assertTrue( configuration.getNetworkProxies( ).isEmpty( ) );
831
832         LegacyArtifactPath path = (LegacyArtifactPath) configuration.getLegacyArtifactPaths( ).get( 0 );
833         assertNotNull( path );
834         configuration.removeLegacyArtifactPath( path );
835         assertTrue( configuration.getLegacyArtifactPaths( ).isEmpty( ) );
836
837         RepositoryScanningConfiguration scanning = configuration.getRepositoryScanning( );
838         String consumer = (String) scanning.getKnownContentConsumers( ).get( 0 );
839         assertNotNull( consumer );
840         scanning.removeKnownContentConsumer( consumer );
841         assertTrue( scanning.getKnownContentConsumers( ).isEmpty( ) );
842         consumer = (String) scanning.getInvalidContentConsumers( ).get( 0 );
843         assertNotNull( consumer );
844         scanning.removeInvalidContentConsumer( consumer );
845         assertTrue( scanning.getInvalidContentConsumers( ).isEmpty( ) );
846
847         archivaConfiguration.save( configuration );
848
849         archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-read-saved" );
850         configuration = archivaConfiguration.getConfiguration( );
851         assertNull( configuration.getRemoteRepositoriesAsMap( ).get( "central" ) );
852         assertTrue( configuration.getRepositoryGroups( ).isEmpty( ) );
853         assertNull( configuration.getManagedRepositoriesAsMap( ).get( "snapshots" ) );
854         assertTrue( configuration.getProxyConnectors( ).isEmpty( ) );
855         assertNull( configuration.getNetworkProxiesAsMap( ).get( "proxy" ) );
856         assertTrue( configuration.getLegacyArtifactPaths( ).isEmpty( ) );
857         scanning = configuration.getRepositoryScanning( );
858         assertTrue( scanning.getKnownContentConsumers( ).isEmpty( ) );
859         assertTrue( scanning.getInvalidContentConsumers( ).isEmpty( ) );
860     }
861
862     /**
863      * [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
864      */
865     @Test
866     public void testGetConfigurationFixEmptyRemoteRepoUsernamePassword( )
867         throws Exception
868     {
869         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
870
871         archivaConfiguration.reload( );
872         Configuration configuration = archivaConfiguration.getConfiguration( );
873         assertConfiguration( configuration, 2, 2, 2 );
874         assertEquals( "check remote repositories", 2, configuration.getRemoteRepositories( ).size( ) );
875
876         RemoteRepositoryConfiguration repository =
877             configuration.getRemoteRepositoriesAsMap( ).get( "maven2-repository.dev.java.net" );
878
879         assertEquals( "remote repository.url", "https://maven2-repository.dev.java.net/nonav/repository",
880                       repository.getUrl( ) );
881         assertEquals( "remote repository.name", "Java.net Repository for Maven 2", repository.getName( ) );
882         assertEquals( "remote repository.id", "maven2-repository.dev.java.net", repository.getId( ) );
883         assertEquals( "remote repository.layout", "default", repository.getLayout( ) );
884         assertNull( "remote repository.username == null", repository.getUsername( ) );
885         assertNull( "remote repository.password == null", repository.getPassword( ) );
886     }
887 }