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.source;
20  
21  /*
22   *  Copyright 2007 rafale.
23   *
24   *  Licensed under the Apache License, Version 2.0 (the "License");
25   *  you may not use this file except in compliance with the License.
26   *  You may obtain a copy of the License at
27   *
28   *       http://www.apache.org/licenses/LICENSE-2.0
29   *
30   *  Unless required by applicable law or agreed to in writing, software
31   *  distributed under the License is distributed on an "AS IS" BASIS,
32   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33   *  See the License for the specific language governing permissions and
34   *  limitations under the License.
35   *  under the License.
36   */
37  
38  import java.io.File;
39  import java.io.FileWriter;
40  import java.io.Writer;
41  
42  import org.apache.maven.archetype.ArchetypeManager;
43  import org.apache.maven.archetype.catalog.Archetype;
44  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
45  import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer;
46  import org.codehaus.plexus.ContainerConfiguration;
47  import org.codehaus.plexus.PlexusTestCase;
48  import org.eclipse.aether.DefaultRepositorySystemSession;
49  import org.eclipse.aether.RepositorySystem;
50  import org.eclipse.aether.repository.LocalRepository;
51  import org.eclipse.aether.repository.LocalRepositoryManager;
52  
53  public class LocalCatalogArchetypeDataSourceTest extends PlexusTestCase {
54  
55      @Override
56      protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
57          configuration.setClassPathScanning("index");
58      }
59  
60      @Override
61      protected void setUp() throws Exception {
62          super.setUp();
63  
64          File catalogDirectory = getTestFile("target/test-classes/repositories/test-catalog");
65          catalogDirectory.mkdirs();
66  
67          ArchetypeCatalog catalog = new ArchetypeCatalog();
68          Archetype generatedArchetype = new Archetype();
69          generatedArchetype.setGroupId("groupId");
70          generatedArchetype.setArtifactId("artifactId");
71          generatedArchetype.setVersion("1");
72          generatedArchetype.setRepository("http://localhost:0/repo/");
73          catalog.addArchetype(generatedArchetype);
74  
75          File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
76          ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
77          try (Writer writer = new FileWriter(catalogFile)) {
78              catalogWriter.write(writer, catalog);
79          }
80      }
81  
82      public void testLocalCatalog() throws Exception {
83          ArchetypeManager archetype = lookup(ArchetypeManager.class);
84          RepositorySystem repositorySystem = lookup(RepositorySystem.class);
85  
86          DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
87          LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager(
88                  repositorySession, new LocalRepository(getTestFile("target/test-classes/repositories/test-catalog")));
89          repositorySession.setLocalRepositoryManager(localRepositoryManager);
90  
91          ArchetypeCatalog result = archetype.getLocalCatalog(repositorySession);
92  
93          assertEquals(1, result.getArchetypes().size());
94          assertEquals("groupId", result.getArchetypes().get(0).getGroupId());
95          assertEquals("artifactId", result.getArchetypes().get(0).getArtifactId());
96          assertEquals("1", result.getArchetypes().get(0).getVersion());
97          assertEquals("http://localhost:0/repo/", result.getArchetypes().get(0).getRepository());
98      }
99  }