1 package org.apache.archiva.repository.maven2;
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.common.utils.FileUtils;
23 import org.apache.archiva.configuration.ArchivaRuntimeConfiguration;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.apache.archiva.metadata.repository.storage.maven2.conf.MockConfiguration;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.archiva.repository.PasswordCredentials;
29 import org.apache.archiva.repository.ReleaseScheme;
30 import org.apache.archiva.repository.RemoteRepository;
31 import org.apache.archiva.repository.RepositoryType;
32 import org.apache.archiva.repository.UnsupportedFeatureException;
33 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
34 import org.apache.archiva.repository.features.IndexCreationFeature;
35 import org.apache.archiva.repository.features.RemoteIndexFeature;
36 import org.apache.archiva.repository.features.StagingRepositoryFeature;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.time.Duration;
46 import java.time.Period;
47 import java.time.temporal.ChronoUnit;
48 import java.util.HashMap;
51 import static org.junit.Assert.*;
54 * @author Martin Stockhammer <martin_s@apache.org>
56 public class MavenRepositoryProviderTest
59 MavenRepositoryProvider provider;
68 provider = new MavenRepositoryProvider( );
69 MockConfiguration mockConfiguration =new MockConfiguration();
70 mockConfiguration.getConfiguration().setArchivaRuntimeConfiguration( new ArchivaRuntimeConfiguration() );
71 mockConfiguration.getConfiguration().getArchivaRuntimeConfiguration().setRepositoryBaseDirectory( "repositories" );
72 provider.setArchivaConfiguration( mockConfiguration );
76 public void cleanUp() {
77 if (repoLocation!=null && Files.exists( repoLocation )) {
78 FileUtils.deleteQuietly( repoLocation );
83 public void provides( ) throws Exception
85 assertEquals(1, provider.provides().size());
86 assertEquals( RepositoryType.MAVEN, provider.provides().iterator().next());
90 public void createManagedInstance( ) throws Exception
92 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration( );
93 repo.setId("testm001");
94 repo.setName("Managed Test Repo 001");
95 repo.setDescription( "This is a managed test" );
96 repo.setRetentionPeriod( 37 );
97 repoLocation = Files.createTempDirectory( "test-repo-001");
98 repo.setLocation( repoLocation.toAbsolutePath().toString() );
99 repo.setSnapshots( true );
100 repo.setReleases( true );
101 repo.setRefreshCronExpression( "4 0 0 ? * TUE" );
102 repo.setScanned( true );
103 repo.setBlockRedeployments( true );
104 repo.setDeleteReleasedSnapshots( true );
105 repo.setRetentionCount( 33 );
106 repo.setSkipPackedIndexCreation( true );
107 repo.setStageRepoNeeded( true );
108 repo.setIndexDir( "testmanaged/.index" );
109 repo.setLayout( "maven2" );
110 repo.setType( RepositoryType.MAVEN.toString() );
113 ManagedRepository mr = provider.createManagedInstance( repo );
114 assertNotNull(mr.getLocation());
115 String repoUri = repoLocation.toUri().toString();
116 assertTrue(Files.exists(repoLocation));
117 repoUri = repoUri.substring( 0, repoUri.length()-1 );
118 assertEquals(repoUri, mr.getLocation().toString());
119 assertEquals("This is a managed test", mr.getDescription());
120 assertEquals("Managed Test Repo 001", mr.getName());
121 assertEquals(2, mr.getActiveReleaseSchemes().size());
122 assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ));
123 assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.SNAPSHOT));
124 assertEquals("testm001", mr.getId());
125 assertTrue(mr.blocksRedeployments());
126 assertEquals("4 0 0 ? * TUE", mr.getSchedulingDefinition());
127 assertTrue(mr.isScanned());
128 ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class ).get();
129 assertEquals( Period.ofDays( 37), artifactCleanupFeature.getRetentionPeriod());
130 assertTrue(artifactCleanupFeature.isDeleteReleasedSnapshots());
131 assertEquals(33, artifactCleanupFeature.getRetentionCount());
133 IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class ).get();
134 assertNotNull(indexCreationFeature.getIndexPath());
135 assertEquals("testmanaged/.index", indexCreationFeature.getIndexPath().toString());
136 assertFalse(indexCreationFeature.getIndexPath().isAbsolute());
137 assertTrue(indexCreationFeature.isSkipPackedIndexCreation());
139 StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class ).get();
140 assertTrue(stagingRepositoryFeature.isStageRepoNeeded());
141 assertNull(stagingRepositoryFeature.getStagingRepository());
147 public void createRemoteInstance( ) throws Exception
149 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration( );
150 repo.setUsername("testuser001");
151 repo.setPassword( "pwd0000abc" );
152 repo.setCheckPath( "test/check.html" );
153 repo.setTimeout( 50 );
154 repo.setUrl( "https://repo.maven.apache.org/maven2/test" );
155 repo.setDownloadRemoteIndex( true );
156 repo.setDownloadRemoteIndexOnStartup( true );
157 Map header = new HashMap( );
158 header.put("header1","value1");
159 header.put("header2","value2");
160 repo.setExtraHeaders( header );
161 Map params = new HashMap( );
162 params.put("param1","pval1");
163 params.put("param2","pval2");
164 repo.setExtraParameters( params );
165 repo.setRefreshCronExpression( "0 1 07 ? * MON" );
166 repo.setRemoteDownloadTimeout( 333 );
167 repo.setRemoteIndexUrl( "testremote/.index" );
168 repo.setDescription( "This is a test" );
169 repo.setId( "test001" );
170 repo.setName( "Remote Test Repo 001" );
171 repo.setIndexDir( "testindex/.index" );
172 repo.setLayout( "maven2" );
173 repo.setType( RepositoryType.MAVEN.toString() );
174 repo.setIndexDir( "local/.index" );
176 RemoteRepository mr = provider.createRemoteInstance( repo );
177 assertEquals("test001", mr.getId());
178 assertEquals("This is a test", mr.getDescription());
179 assertNotNull(mr.getLocation());
180 assertEquals("https://repo.maven.apache.org/maven2/test", mr.getLocation().toString());
181 assertEquals("Remote Test Repo 001", mr.getName());
182 assertEquals("test001", mr.getId());
183 assertEquals("0 1 07 ? * MON", mr.getSchedulingDefinition());
184 assertEquals(50, mr.getTimeout().get( ChronoUnit.SECONDS ));
185 assertTrue(mr.isScanned());
186 assertNotNull(mr.getLoginCredentials());
187 assertTrue(mr.getLoginCredentials() instanceof PasswordCredentials);
188 PasswordCredentials creds = (PasswordCredentials) mr.getLoginCredentials();
189 assertEquals("testuser001", creds.getUsername());
190 assertEquals("pwd0000abc", new String(creds.getPassword()));
191 assertEquals("value1", mr.getExtraHeaders().get("header1"));
192 assertEquals("pval2", mr.getExtraParameters().get("param2"));
193 assertEquals( "maven2", mr.getLayout());
196 ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class ).get( );
197 throw new Exception("artifactCleanupFeature should not be available");
198 } catch ( UnsupportedFeatureException e ) {
202 IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class ).get( );
203 assertEquals("local/.index", indexCreationFeature.getIndexPath().toString());
206 StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class ).get( );
207 throw new Exception("stagingRepositoryFeature should not be available");
208 } catch (UnsupportedFeatureException e) {
211 RemoteIndexFeature remoteIndexFeature = mr.getFeature( RemoteIndexFeature.class ).get();
212 assertNull(remoteIndexFeature.getProxyId());
216 public void getManagedConfiguration() throws Exception {
217 MavenManagedRepository repo = new MavenManagedRepository( "test01", "My Test repo", Paths.get("target/repositories") );
219 repo.setLocation( new URI("file:///this.is/a/test") );
220 repo.setScanned( true );
221 repo.setDescription( repo.getPrimaryLocale(), "This is a description" );
222 repo.setLayout( "maven2" );
223 repo.setBlocksRedeployment( true );
224 repo.setName( repo.getPrimaryLocale(), "test0002" );
225 repo.setSchedulingDefinition( "0 0 05 ? * WED" );
226 repo.addActiveReleaseScheme( ReleaseScheme.RELEASE );
227 repo.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
228 StagingRepositoryFeature stagingFeat = repo.getFeature( StagingRepositoryFeature.class ).get( );
229 stagingFeat.setStageRepoNeeded( true );
230 IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get();
231 indexCreationFeature.setIndexPath( new URI("test/.indexes") );
232 indexCreationFeature.setSkipPackedIndexCreation( true );
233 ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class ).get();
234 artifactCleanupFeature.setRetentionPeriod( Period.ofDays( 5 ) );
235 artifactCleanupFeature.setRetentionCount( 7 );
236 artifactCleanupFeature.setDeleteReleasedSnapshots( true );
238 ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( repo );
239 assertEquals("/this.is/a/test", cfg.getLocation());
240 assertTrue(cfg.isScanned());
241 assertEquals( "This is a description", cfg.getDescription() );
242 assertEquals("maven2", cfg.getLayout());
243 assertTrue(cfg.isBlockRedeployments());
244 assertEquals("test0002", cfg.getName());
245 assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression());
246 assertTrue(cfg.isStageRepoNeeded());
247 assertEquals("test/.indexes", cfg.getIndexDir());
248 assertTrue(cfg.isSkipPackedIndexCreation());
249 assertEquals(5, cfg.getRetentionPeriod());
250 assertEquals(7, cfg.getRetentionCount());
251 assertTrue(cfg.isDeleteReleasedSnapshots());
252 assertTrue(cfg.isReleases());
253 assertTrue(cfg.isSnapshots());
254 assertTrue(cfg.isScanned());
261 public void getRemoteConfiguration() throws Exception {
262 MavenRemoteRepository repo = new MavenRemoteRepository( "test01", "My Test repo", Paths.get("target/remotes") );
264 repo.setLocation( new URI("https://this.is/a/test") );
265 repo.setScanned( true );
266 repo.setDescription( repo.getPrimaryLocale(), "This is a description" );
267 repo.setLayout( "maven2" );
268 repo.setName( repo.getPrimaryLocale(), "test0003" );
269 repo.setSchedulingDefinition( "0 0 05 ? * WED" );
270 RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class ).get();
271 remoteIndexFeature.setProxyId( "proxyabc" );
272 remoteIndexFeature.setDownloadTimeout( Duration.ofSeconds( 54 ) );
273 remoteIndexFeature.setDownloadRemoteIndex( false );
274 remoteIndexFeature.setIndexUri( new URI("/this/remote/.index") );
275 remoteIndexFeature.setDownloadRemoteIndexOnStartup( true );
276 IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get();
277 indexCreationFeature.setIndexPath( new URI("/this/local/.index") );
279 RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration( repo );
280 assertEquals("https://this.is/a/test", cfg.getUrl());
281 assertEquals( "This is a description", cfg.getDescription() );
282 assertEquals("maven2", cfg.getLayout());
283 assertEquals("test0003", cfg.getName());
284 assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression());
285 assertEquals("/this/remote/.index", cfg.getRemoteIndexUrl());
286 assertEquals("proxyabc", cfg.getRemoteDownloadNetworkProxyId());
287 assertEquals(54, cfg.getRemoteDownloadTimeout());
288 assertFalse(cfg.isDownloadRemoteIndex());
289 assertTrue(cfg.isDownloadRemoteIndexOnStartup());
290 assertEquals("/this/local/.index", cfg.getIndexDir());