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