1 package org.apache.archiva.configuration;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.commons.lang3.StringUtils;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.test.context.ContextConfiguration;
30 import javax.inject.Inject;
31 import java.nio.charset.Charset;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.List;
37 import static org.junit.Assert.*;
40 * Test the configuration store.
42 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
43 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
44 public class ArchivaConfigurationMRM789Test
47 private static String FILE_ENCODING = "UTF-8";
50 protected ApplicationContext applicationContext;
55 public static Path getTestFile( String path )
57 return Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
60 @SuppressWarnings( "unchecked" )
61 protected <T> T lookup( Class<T> clazz, String hint )
63 return (T) applicationContext.getBean( "archivaConfiguration#" + hint, ArchivaConfiguration.class );
68 public void testGetConfigurationFromDefaultsWithDefaultRepoLocationAlreadyExisting()
71 Path repo = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-classes/existing_snapshots" );
72 Files.createDirectories(repo);
74 repo = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-classes/existing_internal" );
75 Files.createDirectories(repo);
77 String existingTestDefaultArchivaConfigFile = FileUtils.readFileToString(
78 getTestFile( "target/test-classes/org/apache/archiva/configuration/test-default-archiva.xml" ).toFile(), FILE_ENCODING );
79 existingTestDefaultArchivaConfigFile =
80 StringUtils.replace( existingTestDefaultArchivaConfigFile, "${appserver.base}", org.apache.archiva.common.utils.FileUtils.getBasedir() );
82 Path generatedTestDefaultArchivaConfigFile = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
83 "target/test-classes/org/apache/archiva/configuration/default-archiva.xml" );
85 FileUtils.writeStringToFile( generatedTestDefaultArchivaConfigFile.toFile(), existingTestDefaultArchivaConfigFile,
86 Charset.forName(FILE_ENCODING) );
88 ArchivaConfiguration archivaConfiguration =
89 lookup( ArchivaConfiguration.class, "test-defaults-default-repo-location-exists" );
90 Configuration configuration = archivaConfiguration.getConfiguration();
91 assertConfiguration( configuration, 2, 2, 2 );
93 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
94 assertTrue( "check managed repositories", repository.getLocation().endsWith( "data/repositories/internal" ) );
96 Files.deleteIfExists(generatedTestDefaultArchivaConfigFile);
97 assertFalse( Files.exists(generatedTestDefaultArchivaConfigFile) );
102 * Ensures that the provided configuration matches the details present in the archiva-default.xml file.
104 private void assertConfiguration( Configuration configuration, int managedExpected, int remoteExpected,
105 int proxyConnectorExpected )
109 assertEquals( "check managed repositories: " + configuration.getManagedRepositories(), managedExpected,
110 configuration.getManagedRepositories().size() );
111 assertEquals( "check remote repositories: " + configuration.getRemoteRepositories(), remoteExpected,
112 configuration.getRemoteRepositories().size() );
113 assertEquals( "check proxy connectors:" + configuration.getProxyConnectors(), proxyConnectorExpected,
114 configuration.getProxyConnectors().size() );
116 RepositoryScanningConfiguration repoScanning = configuration.getRepositoryScanning();
117 assertNotNull( "check repository scanning", repoScanning );
118 assertEquals( "check file types", 4, repoScanning.getFileTypes().size() );
119 assertEquals( "check known consumers", 9, repoScanning.getKnownContentConsumers().size() );
120 assertEquals( "check invalid consumers", 1, repoScanning.getInvalidContentConsumers().size() );
122 List<String> patterns = filetypes.getFileTypePatterns( "artifacts" );
123 assertNotNull( "check 'artifacts' file type", patterns );
124 assertEquals( "check 'artifacts' patterns", 13, patterns.size() );
126 WebappConfiguration webapp = configuration.getWebapp();
127 assertNotNull( "check webapp", webapp );
129 UserInterfaceOptions ui = webapp.getUi();
130 assertNotNull( "check webapp ui", ui );
131 assertTrue( "check showFindArtifacts", ui.isShowFindArtifacts() );
132 assertTrue( "check appletFindEnabled", ui.isAppletFindEnabled() );