]> source.dussan.org Git - archiva.git/blob
03bb489b27e2724f8704c84b212193830aa0a300
[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         ConfigurationListener listener = createMock( ConfigurationListener.class );
221         archivaConfiguration.addListener( listener );
222
223         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
224
225         replay( listener );
226
227         archivaConfiguration.save( configuration );
228
229         verify( listener );
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 ConfigurationListener createConfigurationListenerMockControl()
246     {
247         return createMock( ConfigurationListener.class );// 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         ConfigurationListener listener = createConfigurationListenerMockControl();
305         archivaConfiguration.addListener( listener );
306
307         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
308
309         replay( listener );
310
311         archivaConfiguration.save( configuration );
312
313         verify( listener );
314
315         assertTrue( "Check file exists", userFile.exists() );
316         assertFalse( "Check file not created", baseFile.exists() );
317
318         // check it
319         configuration = archivaConfiguration.getConfiguration();
320         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
321     }
322
323     @Test
324     public void testDefaultUserConfigFilename()
325         throws Exception
326     {
327         DefaultArchivaConfiguration archivaConfiguration =
328             (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "default" );
329
330         archivaConfiguration.reload();
331
332         assertEquals( System.getProperty( "user.home" ) + "/.m2/archiva.xml",
333                       archivaConfiguration.getUserConfigFilename() );
334         assertEquals( System.getProperty( "appserver.base", "${appserver.base}" ) + "/conf/archiva.xml",
335                       archivaConfiguration.getAltConfigFilename() );
336     }
337
338     @Test
339     public void testStoreConfigurationFallback()
340         throws Exception
341     {
342         File baseFile = getTestFile( "target/test/test-file.xml" );
343         baseFile.delete();
344         assertFalse( baseFile.exists() );
345
346         File userFile = getTestFile( "target/test/test-file-user.xml" );
347         userFile.delete();
348         assertFalse( userFile.exists() );
349
350         baseFile.getParentFile().mkdirs();
351         FileUtils.writeStringToFile( baseFile, "<configuration/>", Charset.forName( "UTF-8" ) );
352
353         ArchivaConfiguration archivaConfiguration =
354             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-fallback" );
355
356         archivaConfiguration.reload();
357
358         Configuration configuration = new Configuration();
359         configuration.setWebapp( new WebappConfiguration() );
360         configuration.getWebapp().setUi( new UserInterfaceOptions() );
361         configuration.getWebapp().getUi().setAppletFindEnabled( false );
362
363         archivaConfiguration.save( configuration );
364
365         assertTrue( "Check file exists", baseFile.exists() );
366         assertFalse( "Check file not created", userFile.exists() );
367
368         // check it
369         configuration = archivaConfiguration.getConfiguration();
370         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
371     }
372
373     @Test
374     public void testStoreConfigurationFailsWhenReadFromBothLocationsNoLists()
375         throws Exception
376     {
377         File baseFile = getTestFile( "target/test/test-file.xml" );
378         baseFile.delete();
379         assertFalse( baseFile.exists() );
380
381         File userFile = getTestFile( "target/test/test-file-user.xml" );
382         userFile.delete();
383         assertFalse( userFile.exists() );
384
385         baseFile.getParentFile().mkdirs();
386         FileUtils.writeStringToFile( baseFile, "<configuration/>", Charset.forName( "UTF-8" ) );
387
388         userFile.getParentFile().mkdirs();
389         FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.forName( "UTF-8" ) );
390
391         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
392
393         archivaConfiguration.reload();
394
395         Configuration configuration = archivaConfiguration.getConfiguration();
396         assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
397
398         configuration.getWebapp().getUi().setAppletFindEnabled( false );
399
400         archivaConfiguration.save( configuration );
401
402         assertTrue( "Check file exists", baseFile.exists() );
403         assertEquals( "Check base file is unchanged", "<configuration/>",
404                       FileUtils.readFileToString( baseFile, Charset.forName( "UTF-8" ) ) );
405         assertTrue( "Check file exists", userFile.exists() );
406         assertFalse( "Check base file is changed",
407                      "<configuration/>".equals( FileUtils.readFileToString( userFile, Charset.forName( "UTF-8" ) ) ) );
408
409         // check it
410         configuration = archivaConfiguration.getConfiguration();
411         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
412     }
413
414     @Test
415     public void testStoreConfigurationFailsWhenReadFromBothLocationsUserHasLists()
416         throws Exception
417     {
418         File baseFile = getTestFile( "target/test/test-file.xml" );
419         baseFile.delete();
420         assertFalse( baseFile.exists() );
421
422         File userFile = getTestFile( "target/test/test-file-user.xml" );
423         userFile.delete();
424         assertFalse( userFile.exists() );
425
426         userFile.getParentFile().mkdirs();
427         FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ), userFile );
428
429         baseFile.getParentFile().mkdirs();
430         FileUtils.writeStringToFile( baseFile, "<configuration/>", Charset.forName( "UTF-8" ) );
431
432         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
433
434         archivaConfiguration.reload();
435
436         Configuration configuration = archivaConfiguration.getConfiguration();
437         assertTrue( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
438
439         configuration.getWebapp().getUi().setShowFindArtifacts( false );
440
441         archivaConfiguration.save( configuration );
442
443         assertTrue( "Check file exists", baseFile.exists() );
444         assertEquals( "Check base file is unchanged", "<configuration/>",
445                       FileUtils.readFileToString( baseFile, Charset.forName( "UTF-8" ) ) );
446         assertTrue( "Check file exists", userFile.exists() );
447         assertFalse( "Check base file is changed",
448                      "<configuration/>".equals( FileUtils.readFileToString( userFile, Charset.forName( "UTF-8" ) ) ) );
449
450         // check it
451         configuration = archivaConfiguration.getConfiguration();
452         assertFalse( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
453     }
454
455     @Test
456     public void testStoreConfigurationFailsWhenReadFromBothLocationsAppserverHasLists()
457         throws Exception
458     {
459         File baseFile = getTestFile( "target/test/test-file.xml" );
460         baseFile.delete();
461         assertFalse( baseFile.exists() );
462
463         File userFile = getTestFile( "target/test/test-file-user.xml" );
464         userFile.delete();
465         assertFalse( userFile.exists() );
466
467         baseFile.getParentFile().mkdirs();
468         FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ), baseFile );
469
470         userFile.getParentFile().mkdirs();
471         FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.forName( "UTF-8" ) );
472
473         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
474
475         archivaConfiguration.reload();
476
477         Configuration configuration = archivaConfiguration.getConfiguration();
478         assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
479
480         configuration.getWebapp().getUi().setAppletFindEnabled( false );
481
482         try
483         {
484             archivaConfiguration.save( configuration );
485             fail( "Configuration saving should not succeed if it was loaded from two locations" );
486         }
487         catch ( IndeterminateConfigurationException e )
488         {
489             // check it was reverted
490             configuration = archivaConfiguration.getConfiguration();
491             assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
492         }
493     }
494
495     @Test
496     public void testLoadConfigurationFromInvalidBothLocationsOnDisk()
497         throws Exception
498     {
499         ArchivaConfiguration archivaConfiguration =
500             lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-both" );
501         Configuration config = archivaConfiguration.getConfiguration();
502
503         try
504         {
505             archivaConfiguration.save( config );
506             fail( "Should have thrown a RegistryException because the configuration can't be saved." );
507         }
508         catch ( RegistryException e )
509         {
510             /* expected exception */
511         }
512     }
513
514     @Test
515     public void testLoadConfigurationFromInvalidUserLocationOnDisk()
516         throws Exception
517     {
518         File testConfDir = getTestFile( "target/test-appserver-base/conf/" );
519         testConfDir.mkdirs();
520
521         ArchivaConfiguration archivaConfiguration =
522             lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-user" );
523         Configuration config = archivaConfiguration.getConfiguration();
524         archivaConfiguration.save( config );
525         // No Exception == test passes.
526         // Expected Path is: Should not have thrown an exception.
527     }
528
529     @Test
530     public void testConfigurationUpgradeFrom09()
531         throws Exception
532     {
533         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-09" );
534
535         // we just use the defaults when upgrading from 0.9 at this point.
536         Configuration configuration = archivaConfiguration.getConfiguration();
537         // test-upgrade-09 contains a managed with id: local so it's 3 managed
538         assertConfiguration( configuration, 3, 1, 1 );
539         assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
540
541         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
542
543         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
544                       repository.getLocation() );
545         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
546         assertEquals( "check managed repositories", "internal", repository.getId() );
547         assertEquals( "check managed repositories", "default", repository.getLayout() );
548         assertTrue( "check managed repositories", repository.isScanned() );
549     }
550
551     @Test
552     public void testConfigurationUpgradeFrom13()
553         throws Exception
554     {
555         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-1.3" );
556
557         // we just use the defaults when upgrading from 1.3 at this point.
558         Configuration configuration = archivaConfiguration.getConfiguration();
559         assertConfiguration( configuration, 2, 2, 2 );
560         assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
561
562         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
563
564         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
565                       repository.getLocation() );
566         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
567         assertEquals( "check managed repositories", "internal", repository.getId() );
568         assertEquals( "check managed repositories", "default", repository.getLayout() );
569         assertTrue( "check managed repositories", repository.isScanned() );
570
571         log.info( "knowContentConsumers {}", configuration.getRepositoryScanning().getKnownContentConsumers() );
572
573         assertFalse(
574             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "update-db-artifact" ) );
575         assertFalse( configuration.getRepositoryScanning().getKnownContentConsumers().contains(
576             "update-db-repository-metadata" ) );
577
578         assertTrue(
579             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) );
580
581         assertTrue(
582             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) );
583     }
584
585     @Test
586     public void testAutoDetectV1()
587         throws Exception
588     {
589         // Setup the autodetect-v1.xml file in the target directory (so we can save/load it)
590         File userFile = getTestFile( "target/test-autodetect-v1/archiva-user.xml" );
591         userFile.delete();
592         assertFalse( userFile.exists() );
593
594         userFile.getParentFile().mkdirs();
595         FileUtils.copyFile( getTestFile( "src/test/conf/autodetect-v1.xml" ), userFile );
596
597         // Load the original (unconverted) archiva.xml
598         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
599
600         archivaConfiguration.reload();
601
602         Configuration configuration = archivaConfiguration.getConfiguration();
603         assertConfiguration( configuration, 2, 2, 2 );
604         assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
605
606         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
607
608         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
609                       repository.getLocation() );
610         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
611         assertEquals( "check managed repositories", "internal", repository.getId() );
612         assertEquals( "check managed repositories", "default", repository.getLayout() );
613         assertTrue( "check managed repositories", repository.isScanned() );
614
615         // Test that only 1 set of repositories exist.
616         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories().size() );
617         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories().size() );
618         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
619
620         // Save the file.
621         archivaConfiguration.save( configuration );
622
623         // Reload.
624         archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
625         configuration = archivaConfiguration.getConfiguration();
626
627         // Test that only 1 set of repositories exist.
628         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories().size() );
629         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositoriesAsMap().size() );
630         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories().size() );
631         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositoriesAsMap().size() );
632         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
633
634         String actualXML = FileUtils.readFileToString( userFile, Charset.forName( "UTF-8" ) );
635         XMLAssert.assertXpathNotExists( "//configuration/repositories/repository", actualXML );
636         XMLAssert.assertXpathNotExists( "//configuration/repositories", actualXML );
637     }
638
639     @Test
640     public void testArchivaV1()
641         throws Exception
642     {
643         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-archiva-v1" );
644
645         Configuration configuration = archivaConfiguration.getConfiguration();
646         assertConfiguration( configuration, 2, 2, 2 );
647         assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
648
649         assertEquals( "check managed repositories", 2, configuration.getManagedRepositories().size() );
650         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
651
652         Map<String, ManagedRepositoryConfiguration> map = configuration.getManagedRepositoriesAsMap();
653
654         ManagedRepositoryConfiguration repository = map.get( "internal" );
655         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
656                       repository.getLocation() );
657         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
658         assertEquals( "check managed repositories", "internal", repository.getId() );
659         assertEquals( "check managed repositories", "default", repository.getLayout() );
660         assertTrue( "check managed repositories", repository.isScanned() );
661         assertFalse( "check managed repositories", repository.isSnapshots() );
662
663         repository = map.get( "snapshots" );
664         assertEquals( "check managed repositories", "${appserver.base}/repositories/snapshots",
665                       repository.getLocation() );
666         assertEquals( "check managed repositories", "Archiva Managed Snapshot Repository", repository.getName() );
667         assertEquals( "check managed repositories", "snapshots", repository.getId() );
668         assertEquals( "check managed repositories", "default", repository.getLayout() );
669         assertFalse( "check managed repositories", repository.isScanned() );
670         assertTrue( "check managed repositories", repository.isSnapshots() );
671     }
672
673     @Test
674     public void testCronExpressionsWithComma()
675         throws Exception
676     {
677         File baseFile = getTestFile( "target/test/test-file.xml" );
678         baseFile.delete();
679         assertFalse( baseFile.exists() );
680
681         File userFile = getTestFile( "target/test/test-file-user.xml" );
682         userFile.delete();
683         assertFalse( userFile.exists() );
684
685         baseFile.getParentFile().mkdirs();
686         FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ), baseFile );
687
688         userFile.getParentFile().mkdirs();
689         FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.defaultCharset() );
690
691         final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
692
693         archivaConfiguration.reload();
694
695         Configuration configuration = archivaConfiguration.getConfiguration();
696
697         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
698
699         assertEquals( "check cron expression", "0 0,30 * * * ?", repository.getRefreshCronExpression().trim() );
700
701         // add a test listener to confirm it doesn't see the escaped format. We don't need to test the number of calls,
702         // etc. as it's done in other tests
703         archivaConfiguration.addListener( new ConfigurationListener()
704         {
705             @Override
706             public void configurationEvent( ConfigurationEvent event )
707             {
708                 assertEquals( ConfigurationEvent.SAVED, event.getType() );
709
710             }
711         } );
712
713         archivaConfiguration.save( configuration );
714
715         configuration = archivaConfiguration.getConfiguration();
716
717         // test for the escape character '\' showing up on repositories.jsp
718         repository.setRefreshCronExpression( "0 0,20 0 * * ?" );
719
720         archivaConfiguration.save( configuration );
721
722         repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( "snapshots" );
723
724         assertEquals( "check cron expression", "0 0,20 0 * * ?", repository.getRefreshCronExpression() );
725     }
726
727     @Test
728     public void testRemoveLastElements()
729         throws Exception
730     {
731         File baseFile = getTestFile( "target/test/test-file.xml" );
732         baseFile.delete();
733         assertFalse( baseFile.exists() );
734
735         File userFile = getTestFile( "target/test/test-file-user.xml" );
736         userFile.delete();
737         assertFalse( userFile.exists() );
738
739         baseFile.getParentFile().mkdirs();
740         FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ), baseFile );
741
742         userFile.getParentFile().mkdirs();
743         FileUtils.writeStringToFile( userFile, "<configuration/>", Charset.forName( "UTF-8" ) );
744
745         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-remove-central" );
746
747         archivaConfiguration.reload();
748
749         Configuration configuration = archivaConfiguration.getConfiguration();
750
751         RepositoryGroupConfiguration repositoryGroup = configuration.getRepositoryGroups().get( 0 );
752         assertNotNull( repositoryGroup );
753         configuration.removeRepositoryGroup( repositoryGroup );
754         assertTrue( configuration.getRepositoryGroups().isEmpty() );
755
756         RemoteRepositoryConfiguration repository = configuration.getRemoteRepositoriesAsMap().get( "central" );
757         assertNotNull( repository );
758         configuration.removeRemoteRepository( repository );
759         assertTrue( configuration.getRemoteRepositories().isEmpty() );
760
761         ManagedRepositoryConfiguration managedRepository =
762             configuration.getManagedRepositoriesAsMap().get( "snapshots" );
763         assertNotNull( managedRepository );
764         configuration.removeManagedRepository( managedRepository );
765         assertTrue( configuration.getManagedRepositories().isEmpty() );
766
767         ProxyConnectorConfiguration proxyConnector = configuration.getProxyConnectors().get( 0 );
768         assertNotNull( proxyConnector );
769         configuration.removeProxyConnector( proxyConnector );
770         assertTrue( configuration.getProxyConnectors().isEmpty() );
771
772         NetworkProxyConfiguration networkProxy = configuration.getNetworkProxiesAsMap().get( "proxy" );
773         assertNotNull( networkProxy );
774         configuration.removeNetworkProxy( networkProxy );
775         assertTrue( configuration.getNetworkProxies().isEmpty() );
776
777         LegacyArtifactPath path = configuration.getLegacyArtifactPaths().get( 0 );
778         assertNotNull( path );
779         configuration.removeLegacyArtifactPath( path );
780         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
781
782         RepositoryScanningConfiguration scanning = configuration.getRepositoryScanning();
783         String consumer = scanning.getKnownContentConsumers().get( 0 );
784         assertNotNull( consumer );
785         scanning.removeKnownContentConsumer( consumer );
786         // default values
787         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
788         consumer = scanning.getInvalidContentConsumers().get( 0 );
789         assertNotNull( consumer );
790         scanning.removeInvalidContentConsumer( consumer );
791         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
792
793         archivaConfiguration.save( configuration );
794
795         archivaConfiguration = lookup( ArchivaConfiguration.class, "test-read-saved" );
796         configuration = archivaConfiguration.getConfiguration();
797         assertNull( configuration.getRemoteRepositoriesAsMap().get( "central" ) );
798         assertTrue( configuration.getRepositoryGroups().isEmpty() );
799         assertNull( configuration.getManagedRepositoriesAsMap().get( "snapshots" ) );
800         assertTrue( configuration.getProxyConnectors().isEmpty() );
801         assertNull( configuration.getNetworkProxiesAsMap().get( "proxy" ) );
802         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
803         scanning = configuration.getRepositoryScanning();
804         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
805         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
806     }
807
808     /**
809      * [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
810      */
811     @Test
812     public void testGetConfigurationFixEmptyRemoteRepoUsernamePassword()
813         throws Exception
814     {
815         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
816
817         archivaConfiguration.reload();
818         Configuration configuration = archivaConfiguration.getConfiguration();
819         assertConfiguration( configuration, 2, 2, 2 );
820         assertEquals( "check remote repositories", 2, configuration.getRemoteRepositories().size() );
821
822         RemoteRepositoryConfiguration repository =
823             configuration.getRemoteRepositoriesAsMap().get( "maven2-repository.dev.java.net" );
824
825         assertEquals( "remote repository.url", "https://maven2-repository.dev.java.net/nonav/repository",
826                       repository.getUrl() );
827         assertEquals( "remote repository.name", "Java.net Repository for Maven 2", repository.getName() );
828         assertEquals( "remote repository.id", "maven2-repository.dev.java.net", repository.getId() );
829         assertEquals( "remote repository.layout", "default", repository.getLayout() );
830         assertNull( "remote repository.username == null", repository.getUsername() );
831         assertNull( "remote repository.password == null", repository.getPassword() );
832     }
833 }