]> source.dussan.org Git - archiva.git/blob
90802a64bbc9e66bad47e769b7de3d77bd73e096
[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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.test.annotation.DirtiesContext;
32 import org.springframework.test.annotation.DirtiesContext.ClassMode;
33 import org.springframework.test.context.ContextConfiguration;
34
35 import javax.inject.Inject;
36 import java.nio.charset.Charset;
37 import java.nio.file.FileSystems;
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.easymock.EasyMock.*;
44 import static org.junit.Assert.*;
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 = createMock( ConfigurationListener.class );
223         archivaConfiguration.addListener( listener );
224
225         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
226
227         replay( listener );
228
229         archivaConfiguration.save( configuration );
230
231         verify( listener );
232
233         assertTrue( "Check file exists", Files.exists(file) );
234
235         // check it
236         configuration = archivaConfiguration.getConfiguration();
237         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
238
239         // read it back
240         archivaConfiguration = (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-read-saved" );
241
242         archivaConfiguration.reload();
243         configuration = archivaConfiguration.getConfiguration();
244         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
245     }
246
247     private static ConfigurationListener createConfigurationListenerMockControl()
248     {
249         return createMock( ConfigurationListener.class );// MockControl.createControl( ConfigurationListener.class );
250     }
251
252     @Test
253     public void testStoreConfigurationUser()
254         throws Exception
255     {
256         Path baseFile = getTestFile( "target/test/test-file.xml" );
257         Files.deleteIfExists( baseFile );
258         assertFalse( Files.exists(baseFile) );
259
260         Path userFile = getTestFile( "target/test/test-file-user.xml" );
261         Files.deleteIfExists( userFile );
262         assertFalse( Files.exists(userFile) );
263
264         Files.createDirectories(userFile.getParent());
265         FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
266
267         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
268
269         Configuration configuration = new Configuration();
270         configuration.setWebapp( new WebappConfiguration() );
271         configuration.getWebapp().setUi( new UserInterfaceOptions() );
272         configuration.getWebapp().getUi().setAppletFindEnabled( false );
273
274         archivaConfiguration.save( configuration );
275
276         assertTrue( "Check file exists", Files.exists(userFile) );
277         assertFalse( "Check file not created", Files.exists(baseFile) );
278
279         // check it
280         configuration = archivaConfiguration.getConfiguration();
281         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
282     }
283
284     @Test
285     public void testStoreConfigurationLoadedFromDefaults()
286         throws Exception
287     {
288         Path baseFile = getTestFile( "target/test/test-file.xml" );
289         Files.delete(baseFile);
290         assertFalse( Files.exists(baseFile) );
291
292         Path userFile = getTestFile( "target/test/test-file-user.xml" );
293         Files.deleteIfExists(userFile);
294         assertFalse( Files.exists(userFile) );
295
296         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user-defaults" );
297
298         archivaConfiguration.reload();
299
300         Configuration configuration = new Configuration();
301         configuration.setWebapp( new WebappConfiguration() );
302         configuration.getWebapp().setUi( new UserInterfaceOptions() );
303         configuration.getWebapp().getUi().setAppletFindEnabled( false );
304
305         // add a change listener
306         ConfigurationListener listener = createConfigurationListenerMockControl();
307         archivaConfiguration.addListener( listener );
308
309         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
310
311         replay( listener );
312
313         archivaConfiguration.save( configuration );
314
315         verify( listener );
316
317         assertTrue( "Check file exists", Files.exists(userFile) );
318         assertFalse( "Check file not created", Files.exists(baseFile) );
319
320         // check it
321         configuration = archivaConfiguration.getConfiguration();
322         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
323     }
324
325     @Test
326     public void testDefaultUserConfigFilename()
327         throws Exception
328     {
329         DefaultArchivaConfiguration archivaConfiguration =
330             (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "default" );
331         String expectedFile = System.getProperty( "user.home" ) + "/.m2/archiva.xml";
332         String systemFile = System.getProperty(ArchivaConfiguration.USER_CONFIG_PROPERTY);
333         if (StringUtils.isNotEmpty( systemFile )) {
334             expectedFile = systemFile;
335         } else
336         {
337             String envFile = System.getenv( ArchivaConfiguration.USER_CONFIG_ENVVAR );
338             if ( StringUtils.isNotEmpty( envFile ) )
339                 expectedFile = envFile;
340         }
341
342         archivaConfiguration.reload();
343
344         assertEquals( expectedFile,
345                       archivaConfiguration.getUserConfigFilename() );
346         assertEquals( System.getProperty( "appserver.base", "${appserver.base}" ) + "/conf/archiva.xml",
347                       archivaConfiguration.getAltConfigFilename() );
348     }
349
350     @Test
351     public void testStoreConfigurationFallback()
352         throws Exception
353     {
354         Path baseFile = getTestFile( "target/test/test-file.xml" );
355         Files.deleteIfExists(baseFile);
356         assertFalse( Files.exists(baseFile) );
357
358         Path userFile = getTestFile( "target/test/test-file-user.xml" );
359         Files.deleteIfExists(userFile);
360         assertFalse( Files.exists(userFile) );
361
362         Files.createDirectories( baseFile.getParent());
363         FileUtils.writeStringToFile( baseFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
364
365         ArchivaConfiguration archivaConfiguration =
366             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-fallback" );
367
368         archivaConfiguration.reload();
369
370         Configuration configuration = new Configuration();
371         configuration.setWebapp( new WebappConfiguration() );
372         configuration.getWebapp().setUi( new UserInterfaceOptions() );
373         configuration.getWebapp().getUi().setAppletFindEnabled( false );
374
375         archivaConfiguration.save( configuration );
376
377         assertTrue( "Check file exists", Files.exists(baseFile) );
378         assertFalse( "Check file not created", Files.exists(userFile) );
379
380         // check it
381         configuration = archivaConfiguration.getConfiguration();
382         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
383     }
384
385     @Test
386     public void testStoreConfigurationFailsWhenReadFromBothLocationsNoLists()
387         throws Exception
388     {
389         Path baseFile = getTestFile( "target/test/test-file.xml" );
390         Files.deleteIfExists(baseFile);
391         assertFalse( Files.exists(baseFile) );
392
393         Path userFile = getTestFile( "target/test/test-file-user.xml" );
394         Files.deleteIfExists(userFile);
395         assertFalse( Files.exists(userFile) );
396
397         Files.createDirectories( baseFile.getParent() );
398         FileUtils.writeStringToFile( baseFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
399
400         Files.createDirectories( userFile.getParent());
401         FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
402
403         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
404
405         archivaConfiguration.reload();
406
407         Configuration configuration = archivaConfiguration.getConfiguration();
408         assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
409
410         configuration.getWebapp().getUi().setAppletFindEnabled( false );
411
412         archivaConfiguration.save( configuration );
413
414         assertTrue( "Check file exists", Files.exists(baseFile) );
415         assertEquals( "Check base file is unchanged", "<configuration/>",
416                       FileUtils.readFileToString( baseFile.toFile(), Charset.forName( "UTF-8" ) ) );
417         assertTrue( "Check file exists", Files.exists(userFile) );
418         assertFalse( "Check base file is changed",
419                      "<configuration/>".equals( FileUtils.readFileToString( userFile.toFile(), Charset.forName( "UTF-8" ) ) ) );
420
421         // check it
422         configuration = archivaConfiguration.getConfiguration();
423         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
424     }
425
426     @Test
427     public void testStoreConfigurationFailsWhenReadFromBothLocationsUserHasLists()
428         throws Exception
429     {
430         Path baseFile = getTestFile( "target/test/test-file.xml" );
431         Files.deleteIfExists(baseFile);
432         assertFalse( Files.exists(baseFile) );
433
434         Path userFile = getTestFile( "target/test/test-file-user.xml" );
435         Files.deleteIfExists(userFile);
436         assertFalse( Files.exists(userFile) );
437
438         Files.createDirectories( userFile.getParent() );
439         FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ).toFile(), userFile.toFile() );
440
441         Files.createDirectories(baseFile.getParent());
442         FileUtils.writeStringToFile( baseFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
443
444         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
445
446         archivaConfiguration.reload();
447
448         Configuration configuration = archivaConfiguration.getConfiguration();
449         assertTrue( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
450
451         configuration.getWebapp().getUi().setShowFindArtifacts( false );
452
453         archivaConfiguration.save( configuration );
454
455         assertTrue( "Check file exists", Files.exists(baseFile) );
456         assertEquals( "Check base file is unchanged", "<configuration/>",
457                       FileUtils.readFileToString( baseFile.toFile(), Charset.forName( "UTF-8" ) ) );
458         assertTrue( "Check file exists", Files.exists(userFile) );
459         assertFalse( "Check base file is changed",
460                      "<configuration/>".equals( FileUtils.readFileToString( userFile.toFile(), Charset.forName( "UTF-8" ) ) ) );
461
462         // check it
463         configuration = archivaConfiguration.getConfiguration();
464         assertFalse( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
465     }
466
467     @Test
468     public void testStoreConfigurationFailsWhenReadFromBothLocationsAppserverHasLists()
469         throws Exception
470     {
471         Path baseFile = getTestFile( "target/test/test-file.xml" );
472         Files.deleteIfExists(baseFile);
473         assertFalse( Files.exists(baseFile) );
474
475         Path userFile = getTestFile( "target/test/test-file-user.xml" );
476         Files.deleteIfExists(userFile);
477         assertFalse( Files.exists(userFile) );
478
479         Files.createDirectories(baseFile.getParent());
480         FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ).toFile(), baseFile.toFile() );
481
482         Files.createDirectories(userFile.getParent());
483         FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
484
485         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
486
487         archivaConfiguration.reload();
488
489         Configuration configuration = archivaConfiguration.getConfiguration();
490         assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
491
492         configuration.getWebapp().getUi().setAppletFindEnabled( false );
493
494         try
495         {
496             archivaConfiguration.save( configuration );
497             fail( "Configuration saving should not succeed if it was loaded from two locations" );
498         }
499         catch ( IndeterminateConfigurationException e )
500         {
501             // check it was reverted
502             configuration = archivaConfiguration.getConfiguration();
503             assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
504         }
505     }
506
507     @Test
508     public void testLoadConfigurationFromInvalidBothLocationsOnDisk()
509         throws Exception
510     {
511         String propFile = System.getProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY );
512         System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY, "/../../..//*intentionally:invalid*/.m2/archiva-user.xml" );
513         ArchivaConfiguration archivaConfiguration =
514             lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-both" );
515         Configuration config = archivaConfiguration.getConfiguration();
516
517         try
518         {
519             archivaConfiguration.save( config );
520             fail( "Should have thrown a RegistryException because the configuration can't be saved." );
521         }
522         catch ( RegistryException e )
523         {
524             /* expected exception */
525         }
526         if (propFile!=null)
527         {
528             System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY, propFile );
529         }
530     }
531
532     @Test
533     public void testLoadConfigurationFromInvalidUserLocationOnDisk()
534         throws Exception
535     {
536         Path testConfDir = getTestFile( "target/test-appserver-base/conf/" );
537         Files.createDirectories( testConfDir );
538
539         ArchivaConfiguration archivaConfiguration =
540             lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-user" );
541         Configuration config = archivaConfiguration.getConfiguration();
542         archivaConfiguration.save( config );
543         // No Exception == test passes.
544         // Expected Path is: Should not have thrown an exception.
545     }
546
547
548     @Test
549     public void testConfigurationUpgradeFrom13()
550         throws Exception
551     {
552         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-1.3" );
553
554         // we just use the defaults when upgrading from 1.3 at this point.
555         Configuration configuration = archivaConfiguration.getConfiguration();
556         assertConfiguration( configuration, 2, 2, 2 );
557         assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
558
559         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
560
561         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
562                       repository.getLocation() );
563         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
564         assertEquals( "check managed repositories", "internal", repository.getId() );
565         assertEquals( "check managed repositories", "default", repository.getLayout() );
566         assertTrue( "check managed repositories", repository.isScanned() );
567
568         log.info( "knowContentConsumers {}", configuration.getRepositoryScanning().getKnownContentConsumers() );
569
570         assertFalse(
571             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "update-db-artifact" ) );
572         assertFalse( configuration.getRepositoryScanning().getKnownContentConsumers().contains(
573             "update-db-repository-metadata" ) );
574
575         assertTrue(
576             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) );
577
578         assertTrue(
579             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) );
580     }
581
582
583     @Test
584     public void testCronExpressionsWithComma()
585         throws Exception
586     {
587         Path baseFile = getTestFile( "target/test/test-file.xml" );
588         Files.deleteIfExists(baseFile);
589         assertFalse( Files.exists(baseFile) );
590
591         Path userFile = getTestFile( "target/test/test-file-user.xml" );
592         Files.deleteIfExists(userFile);
593         assertFalse( Files.exists(userFile) );
594
595         Files.createDirectories(baseFile.getParent());
596         FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ).toFile(), baseFile.toFile() );
597
598         Files.createDirectories(userFile.getParent());
599         FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.defaultCharset() );
600
601         final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
602
603         archivaConfiguration.reload();
604
605         Configuration configuration = archivaConfiguration.getConfiguration();
606
607         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
608
609         assertEquals( "check cron expression", "0 0,30 * * * ?", repository.getRefreshCronExpression().trim() );
610
611         // add a test listener to confirm it doesn't see the escaped format. We don't need to test the number of calls,
612         // etc. as it's done in other tests
613         archivaConfiguration.addListener( new ConfigurationListener()
614         {
615             @Override
616             public void configurationEvent( ConfigurationEvent event )
617             {
618                 assertEquals( ConfigurationEvent.SAVED, event.getType() );
619
620             }
621         } );
622
623         archivaConfiguration.save( configuration );
624
625         configuration = archivaConfiguration.getConfiguration();
626
627         // test for the escape character '\' showing up on repositories.jsp
628         repository.setRefreshCronExpression( "0 0,20 0 * * ?" );
629
630         archivaConfiguration.save( configuration );
631
632         repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( "snapshots" );
633
634         assertEquals( "check cron expression", "0 0,20 0 * * ?", repository.getRefreshCronExpression() );
635     }
636
637     @Test
638     public void testRemoveLastElements()
639         throws Exception
640     {
641         Path baseFile = getTestFile( "target/test/test-file.xml" );
642         Files.deleteIfExists(baseFile);
643         assertFalse( Files.exists(baseFile) );
644
645         Path userFile = getTestFile( "target/test/test-file-user.xml" );
646         Files.deleteIfExists(userFile);
647         assertFalse( Files.exists(userFile) );
648
649         Files.createDirectories( baseFile.getParent() );
650         FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ).toFile(), baseFile.toFile() );
651
652         Files.createDirectories( userFile.getParent());
653         FileUtils.writeStringToFile( userFile.toFile(), "<configuration/>", Charset.forName( "UTF-8" ) );
654
655         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-remove-central" );
656
657         archivaConfiguration.reload();
658
659         Configuration configuration = archivaConfiguration.getConfiguration();
660
661         RepositoryGroupConfiguration repositoryGroup = configuration.getRepositoryGroups().get( 0 );
662         assertNotNull( repositoryGroup );
663         configuration.removeRepositoryGroup( repositoryGroup );
664         assertTrue( configuration.getRepositoryGroups().isEmpty() );
665
666         RemoteRepositoryConfiguration repository = configuration.getRemoteRepositoriesAsMap().get( "central" );
667         assertNotNull( repository );
668         configuration.removeRemoteRepository( repository );
669         assertTrue( configuration.getRemoteRepositories().isEmpty() );
670
671         ManagedRepositoryConfiguration managedRepository =
672             configuration.getManagedRepositoriesAsMap().get( "snapshots" );
673         assertNotNull( managedRepository );
674         configuration.removeManagedRepository( managedRepository );
675         assertTrue( configuration.getManagedRepositories().isEmpty() );
676
677         ProxyConnectorConfiguration proxyConnector = configuration.getProxyConnectors().get( 0 );
678         assertNotNull( proxyConnector );
679         configuration.removeProxyConnector( proxyConnector );
680         assertTrue( configuration.getProxyConnectors().isEmpty() );
681
682         NetworkProxyConfiguration networkProxy = configuration.getNetworkProxiesAsMap().get( "proxy" );
683         assertNotNull( networkProxy );
684         configuration.removeNetworkProxy( networkProxy );
685         assertTrue( configuration.getNetworkProxies().isEmpty() );
686
687         LegacyArtifactPath path = configuration.getLegacyArtifactPaths().get( 0 );
688         assertNotNull( path );
689         configuration.removeLegacyArtifactPath( path );
690         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
691
692         RepositoryScanningConfiguration scanning = configuration.getRepositoryScanning();
693         String consumer = scanning.getKnownContentConsumers().get( 0 );
694         assertNotNull( consumer );
695         scanning.removeKnownContentConsumer( consumer );
696         // default values
697         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
698         consumer = scanning.getInvalidContentConsumers().get( 0 );
699         assertNotNull( consumer );
700         scanning.removeInvalidContentConsumer( consumer );
701         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
702
703         archivaConfiguration.save( configuration );
704
705         archivaConfiguration = lookup( ArchivaConfiguration.class, "test-read-saved" );
706         configuration = archivaConfiguration.getConfiguration();
707         assertNull( configuration.getRemoteRepositoriesAsMap().get( "central" ) );
708         assertTrue( configuration.getRepositoryGroups().isEmpty() );
709         assertNull( configuration.getManagedRepositoriesAsMap().get( "snapshots" ) );
710         assertTrue( configuration.getProxyConnectors().isEmpty() );
711         assertNull( configuration.getNetworkProxiesAsMap().get( "proxy" ) );
712         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
713         scanning = configuration.getRepositoryScanning();
714         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
715         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
716     }
717
718     /**
719      * [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
720      */
721     @Test
722     public void testGetConfigurationFixEmptyRemoteRepoUsernamePassword()
723         throws Exception
724     {
725         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
726
727         Configuration configuration = archivaConfiguration.getConfiguration();
728         assertConfiguration( configuration, 2, 2, 2 );
729         assertEquals( "check remote repositories", 2, configuration.getRemoteRepositories().size() );
730
731         RemoteRepositoryConfiguration repository =
732             configuration.getRemoteRepositoriesAsMap().get( "maven2-repository.dev.java.net" );
733
734         assertEquals( "remote repository.url", "https://maven2-repository.dev.java.net/nonav/repository",
735                       repository.getUrl() );
736         assertEquals( "remote repository.name", "Java.net Repository for Maven 2", repository.getName() );
737         assertEquals( "remote repository.id", "maven2-repository.dev.java.net", repository.getId() );
738         assertEquals( "remote repository.layout", "default", repository.getLayout() );
739         assertNull( "remote repository.username == null", repository.getUsername() );
740         assertNull( "remote repository.password == null", repository.getPassword() );
741     }
742 }