View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archetype.test;
20  
21  import java.io.File;
22  import java.util.Properties;
23  
24  import org.apache.maven.archetype.ArchetypeGenerationRequest;
25  import org.apache.maven.archetype.ArchetypeGenerationResult;
26  import org.apache.maven.archetype.ArchetypeManager;
27  import org.apache.maven.archetype.catalog.Archetype;
28  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
29  import org.codehaus.plexus.ContainerConfiguration;
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.eclipse.aether.DefaultRepositorySystemSession;
33  import org.eclipse.aether.RepositorySystem;
34  import org.eclipse.aether.repository.LocalRepository;
35  import org.eclipse.aether.repository.LocalRepositoryManager;
36  
37  /**
38   *
39   * @author rafale
40   */
41  public class InternalCatalogArchetypesVerificationTest extends PlexusTestCase {
42      private static final String CENTRAL = "https://repo.maven.apache.org/maven2";
43  
44      @Override
45      protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
46          configuration.setClassPathScanning("index");
47      }
48  
49      public void testInternalCatalog() throws Exception {
50  
51          File outputDirectory = new File(getBasedir(), "target/internal-archetypes-projects");
52          outputDirectory.mkdirs();
53          FileUtils.cleanDirectory(outputDirectory);
54  
55          ArchetypeManager archetypeManager = (ArchetypeManager) lookup(ArchetypeManager.class.getName());
56          ArchetypeCatalog catalog = archetypeManager.getInternalCatalog();
57  
58          // quickstart has a parameters with defaults ... so it should not be needed
59          // can be connected with ARCHETYPE-574
60          Properties props = new Properties();
61          props.put("javaCompilerVersion", "11");
62          props.put("junitVersion", "5.11.0");
63  
64          int count = 1;
65          for (Archetype archetype : catalog.getArchetypes()) {
66              // this should be also default ...
67              archetype.setRepository(CENTRAL);
68  
69              DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
70              RepositorySystem repositorySystem = lookup(RepositorySystem.class);
71              LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager(
72                      repositorySession, new LocalRepository("target/test-classes/repositories/local"));
73              repositorySession.setLocalRepositoryManager(localRepositoryManager);
74  
75              ArchetypeGenerationRequest request = new ArchetypeGenerationRequest(archetype)
76                      .setGroupId("org.apache.maven.archetype.test")
77                      .setArtifactId("archetype" + count)
78                      .setVersion("1.0-SNAPSHOT")
79                      .setPackage("com.acme")
80                      .setProperties(props)
81                      .setOutputDirectory(outputDirectory.getPath())
82                      .setRepositorySession(repositorySession);
83  
84              ArchetypeGenerationResult generationResult = archetypeManager.generateProjectFromArchetype(request);
85  
86              assertNull(
87                      "Archetype wasn't generated successfully: " + generationResult.getCause(),
88                      generationResult.getCause());
89  
90              count++;
91          }
92      }
93  }