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