]> source.dussan.org Git - archiva.git/blob
a8dddadc523af3c78c0eacf2abbbcc933f1ee9f0
[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.redback.components.registry.RegistryException;
23 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
24 import org.apache.commons.io.FileUtils;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.test.annotation.DirtiesContext;
31 import org.springframework.test.annotation.DirtiesContext.ClassMode;
32 import org.springframework.test.context.ContextConfiguration;
33
34 import javax.inject.Inject;
35 import java.nio.charset.Charset;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.List;
40
41 import static org.easymock.EasyMock.*;
42 import static org.junit.Assert.*;
43
44 /**
45  * Test the configuration store.
46  */
47 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
48 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
49 @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
50 public class ArchivaConfigurationTest
51 {
52
53     private Logger log = LoggerFactory.getLogger( getClass() );
54
55     @Inject
56     protected ApplicationContext applicationContext;
57
58     @Inject
59     FileTypes filetypes;
60
61     public static Path getTestFile( String path )
62     {
63         return Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
64     }
65
66     @SuppressWarnings( "unchecked" )
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         Path file = getTestFile( "target/test/test-file.xml" );
201         Files.deleteIfExists(file);
202         assertFalse( Files.exists(file) );
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", Files.exists(file) );
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         Path baseFile = getTestFile( "target/test/test-file.xml" );
255         Files.deleteIfExists( baseFile );
256         assertFalse( Files.exists(baseFile) );
257
258         Path userFile = getTestFile( "target/test/test-file-user.xml" );
259         Files.deleteIfExists( userFile );
260         assertFalse( Files.exists(userFile) );
261
262         Files.createDirectories(userFile.getParent());
263         FileUtils.writeStringToFile( userFile.toFile(), "<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", Files.exists(userFile) );
275         assertFalse( "Check file not created", Files.exists(baseFile) );
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         Path baseFile = getTestFile( "target/test/test-file.xml" );
287         Files.delete(baseFile);
288         assertFalse( Files.exists(baseFile) );
289
290         Path userFile = getTestFile( "target/test/test-file-user.xml" );
291         Files.deleteIfExists(userFile);
292         assertFalse( Files.exists(userFile) );
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", Files.exists(userFile) );
316         assertFalse( "Check file not created", Files.exists(baseFile) );
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         Path baseFile = getTestFile( "target/test/test-file.xml" );
343         Files.deleteIfExists(baseFile);
344         assertFalse( Files.exists(baseFile) );
345
346         Path userFile = getTestFile( "target/test/test-file-user.xml" );
347         Files.deleteIfExists(userFile);
348         assertFalse( Files.exists(userFile) );
349
350         Files.createDirectories( baseFile.getParent());
351         FileUtils.writeStringToFile( baseFile.toFile(), "<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", Files.exists(baseFile) );
366         assertFalse( "Check file not created", Files.exists(userFile) );
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         Path baseFile = getTestFile( "target/test/test-file.xml" );
378         Files.deleteIfExists(baseFile);
379         assertFalse( Files.exists(baseFile) );
380
381         Path userFile = getTestFile( "target/test/test-file-user.xml" );
382         Files.deleteIfExists(userFile);
383         assertFalse( Files.exists(userFile) );
384
385         Files.createDirectories( baseFile.getParent() );
386         FileUtils.writeStringToFile( baseFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
387
388         Files.createDirectories( userFile.getParent());
389         FileUtils.writeStringToFile( userFile.toFile(), "<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", Files.exists(baseFile) );
403         assertEquals( "Check base file is unchanged", "<configuration/>",
404                       FileUtils.readFileToString( baseFile.toFile(), Charset.forName( "UTF-8" ) ) );
405         assertTrue( "Check file exists", Files.exists(userFile) );
406         assertFalse( "Check base file is changed",
407                      "<configuration/>".equals( FileUtils.readFileToString( userFile.toFile(), 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         Path baseFile = getTestFile( "target/test/test-file.xml" );
419         Files.deleteIfExists(baseFile);
420         assertFalse( Files.exists(baseFile) );
421
422         Path userFile = getTestFile( "target/test/test-file-user.xml" );
423         Files.deleteIfExists(userFile);
424         assertFalse( Files.exists(userFile) );
425
426         Files.createDirectories( userFile.getParent() );
427         FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ).toFile(), userFile.toFile() );
428
429         Files.createDirectories(baseFile.getParent());
430         FileUtils.writeStringToFile( baseFile.toFile(), "<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", Files.exists(baseFile) );
444         assertEquals( "Check base file is unchanged", "<configuration/>",
445                       FileUtils.readFileToString( baseFile.toFile(), Charset.forName( "UTF-8" ) ) );
446         assertTrue( "Check file exists", Files.exists(userFile) );
447         assertFalse( "Check base file is changed",
448                      "<configuration/>".equals( FileUtils.readFileToString( userFile.toFile(), 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         Path baseFile = getTestFile( "target/test/test-file.xml" );
460         Files.deleteIfExists(baseFile);
461         assertFalse( Files.exists(baseFile) );
462
463         Path userFile = getTestFile( "target/test/test-file-user.xml" );
464         Files.deleteIfExists(userFile);
465         assertFalse( Files.exists(userFile) );
466
467         Files.createDirectories(baseFile.getParent());
468         FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ).toFile(), baseFile.toFile() );
469
470         Files.createDirectories(userFile.getParent());
471         FileUtils.writeStringToFile( userFile.toFile(), "<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         Path testConfDir = getTestFile( "target/test-appserver-base/conf/" );
519         Files.createDirectories( testConfDir );
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
530     @Test
531     public void testConfigurationUpgradeFrom13()
532         throws Exception
533     {
534         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-1.3" );
535
536         // we just use the defaults when upgrading from 1.3 at this point.
537         Configuration configuration = archivaConfiguration.getConfiguration();
538         assertConfiguration( configuration, 2, 2, 2 );
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}/data/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         log.info( "knowContentConsumers {}", configuration.getRepositoryScanning().getKnownContentConsumers() );
551
552         assertFalse(
553             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "update-db-artifact" ) );
554         assertFalse( configuration.getRepositoryScanning().getKnownContentConsumers().contains(
555             "update-db-repository-metadata" ) );
556
557         assertTrue(
558             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) );
559
560         assertTrue(
561             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) );
562     }
563
564
565     @Test
566     public void testCronExpressionsWithComma()
567         throws Exception
568     {
569         Path baseFile = getTestFile( "target/test/test-file.xml" );
570         Files.deleteIfExists(baseFile);
571         assertFalse( Files.exists(baseFile) );
572
573         Path userFile = getTestFile( "target/test/test-file-user.xml" );
574         Files.deleteIfExists(userFile);
575         assertFalse( Files.exists(userFile) );
576
577         Files.createDirectories(baseFile.getParent());
578         FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ).toFile(), baseFile.toFile() );
579
580         Files.createDirectories(userFile.getParent());
581         FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.defaultCharset() );
582
583         final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
584
585         archivaConfiguration.reload();
586
587         Configuration configuration = archivaConfiguration.getConfiguration();
588
589         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
590
591         assertEquals( "check cron expression", "0 0,30 * * * ?", repository.getRefreshCronExpression().trim() );
592
593         // add a test listener to confirm it doesn't see the escaped format. We don't need to test the number of calls,
594         // etc. as it's done in other tests
595         archivaConfiguration.addListener( new ConfigurationListener()
596         {
597             @Override
598             public void configurationEvent( ConfigurationEvent event )
599             {
600                 assertEquals( ConfigurationEvent.SAVED, event.getType() );
601
602             }
603         } );
604
605         archivaConfiguration.save( configuration );
606
607         configuration = archivaConfiguration.getConfiguration();
608
609         // test for the escape character '\' showing up on repositories.jsp
610         repository.setRefreshCronExpression( "0 0,20 0 * * ?" );
611
612         archivaConfiguration.save( configuration );
613
614         repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( "snapshots" );
615
616         assertEquals( "check cron expression", "0 0,20 0 * * ?", repository.getRefreshCronExpression() );
617     }
618
619     @Test
620     public void testRemoveLastElements()
621         throws Exception
622     {
623         Path baseFile = getTestFile( "target/test/test-file.xml" );
624         Files.deleteIfExists(baseFile);
625         assertFalse( Files.exists(baseFile) );
626
627         Path userFile = getTestFile( "target/test/test-file-user.xml" );
628         Files.deleteIfExists(userFile);
629         assertFalse( Files.exists(userFile) );
630
631         Files.createDirectories( baseFile.getParent() );
632         FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ).toFile(), baseFile.toFile() );
633
634         Files.createDirectories( userFile.getParent());
635         FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
636
637         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-remove-central" );
638
639         archivaConfiguration.reload();
640
641         Configuration configuration = archivaConfiguration.getConfiguration();
642
643         RepositoryGroupConfiguration repositoryGroup = configuration.getRepositoryGroups().get( 0 );
644         assertNotNull( repositoryGroup );
645         configuration.removeRepositoryGroup( repositoryGroup );
646         assertTrue( configuration.getRepositoryGroups().isEmpty() );
647
648         RemoteRepositoryConfiguration repository = configuration.getRemoteRepositoriesAsMap().get( "central" );
649         assertNotNull( repository );
650         configuration.removeRemoteRepository( repository );
651         assertTrue( configuration.getRemoteRepositories().isEmpty() );
652
653         ManagedRepositoryConfiguration managedRepository =
654             configuration.getManagedRepositoriesAsMap().get( "snapshots" );
655         assertNotNull( managedRepository );
656         configuration.removeManagedRepository( managedRepository );
657         assertTrue( configuration.getManagedRepositories().isEmpty() );
658
659         ProxyConnectorConfiguration proxyConnector = configuration.getProxyConnectors().get( 0 );
660         assertNotNull( proxyConnector );
661         configuration.removeProxyConnector( proxyConnector );
662         assertTrue( configuration.getProxyConnectors().isEmpty() );
663
664         NetworkProxyConfiguration networkProxy = configuration.getNetworkProxiesAsMap().get( "proxy" );
665         assertNotNull( networkProxy );
666         configuration.removeNetworkProxy( networkProxy );
667         assertTrue( configuration.getNetworkProxies().isEmpty() );
668
669         LegacyArtifactPath path = configuration.getLegacyArtifactPaths().get( 0 );
670         assertNotNull( path );
671         configuration.removeLegacyArtifactPath( path );
672         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
673
674         RepositoryScanningConfiguration scanning = configuration.getRepositoryScanning();
675         String consumer = scanning.getKnownContentConsumers().get( 0 );
676         assertNotNull( consumer );
677         scanning.removeKnownContentConsumer( consumer );
678         // default values
679         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
680         consumer = scanning.getInvalidContentConsumers().get( 0 );
681         assertNotNull( consumer );
682         scanning.removeInvalidContentConsumer( consumer );
683         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
684
685         archivaConfiguration.save( configuration );
686
687         archivaConfiguration = lookup( ArchivaConfiguration.class, "test-read-saved" );
688         configuration = archivaConfiguration.getConfiguration();
689         assertNull( configuration.getRemoteRepositoriesAsMap().get( "central" ) );
690         assertTrue( configuration.getRepositoryGroups().isEmpty() );
691         assertNull( configuration.getManagedRepositoriesAsMap().get( "snapshots" ) );
692         assertTrue( configuration.getProxyConnectors().isEmpty() );
693         assertNull( configuration.getNetworkProxiesAsMap().get( "proxy" ) );
694         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
695         scanning = configuration.getRepositoryScanning();
696         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
697         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
698     }
699
700     /**
701      * [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
702      */
703     @Test
704     public void testGetConfigurationFixEmptyRemoteRepoUsernamePassword()
705         throws Exception
706     {
707         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
708
709         Configuration configuration = archivaConfiguration.getConfiguration();
710         assertConfiguration( configuration, 2, 2, 2 );
711         assertEquals( "check remote repositories", 2, configuration.getRemoteRepositories().size() );
712
713         RemoteRepositoryConfiguration repository =
714             configuration.getRemoteRepositoriesAsMap().get( "maven2-repository.dev.java.net" );
715
716         assertEquals( "remote repository.url", "https://maven2-repository.dev.java.net/nonav/repository",
717                       repository.getUrl() );
718         assertEquals( "remote repository.name", "Java.net Repository for Maven 2", repository.getName() );
719         assertEquals( "remote repository.id", "maven2-repository.dev.java.net", repository.getId() );
720         assertEquals( "remote repository.layout", "default", repository.getLayout() );
721         assertNull( "remote repository.username == null", repository.getUsername() );
722         assertNull( "remote repository.password == null", repository.getPassword() );
723     }
724 }