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