1 package org.apache.maven.archetype;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import org.apache.commons.io.IOUtils;
21 import org.apache.maven.archetype.catalog.Archetype;
22 import org.apache.maven.archetype.catalog.ArchetypeCatalog;
23 import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer;
24 import org.codehaus.plexus.PlexusTestCase;
25 import org.mortbay.jetty.Server;
26 import org.mortbay.jetty.webapp.WebAppContext;
27
28 import java.io.File;
29 import java.io.FileWriter;
30 import java.io.Writer;
31
32
33
34
35 public class ArchetypeCatalogsTest
36 extends PlexusTestCase
37 {
38 public void testRemoteCatalog()
39 throws Exception
40 {
41 ArchetypeManager archetype = (ArchetypeManager) lookup( ArchetypeManager.class.getName() );
42
43 ArchetypeCatalog result = archetype.getRemoteCatalog( "http://localhost:" + port + "/repo/" );
44
45 assertEquals( 1, result.getArchetypes().size() );
46 assertEquals( "groupId", result.getArchetypes().get( 0 ).getGroupId() );
47 assertEquals( "artifactId", result.getArchetypes().get( 0 ).getArtifactId() );
48 assertEquals( "1", result.getArchetypes().get( 0 ).getVersion() );
49 assertEquals( "http://localhost:" + port + "/repo/", result.getArchetypes().get( 0 ).getRepository() );
50 }
51
52 public void testLocalCatalog()
53 throws Exception
54 {
55 ArchetypeManager archetype = (ArchetypeManager) lookup( ArchetypeManager.class.getName() );
56
57 ArchetypeCatalog result =
58 archetype.getLocalCatalog( getTestFile( "target/test-classes/repositories/test-catalog" ).
59 getAbsolutePath() );
60
61 assertEquals( 1, result.getArchetypes().size() );
62 assertEquals( "groupId", result.getArchetypes().get( 0 ).getGroupId() );
63 assertEquals( "artifactId", result.getArchetypes().get( 0 ).getArtifactId() );
64 assertEquals( "1", result.getArchetypes().get( 0 ).getVersion() );
65 assertEquals( "http://localhost:" + port + "/repo/", result.getArchetypes().get( 0 ).getRepository() );
66 }
67
68
69 private Server server;
70
71 int port;
72
73 protected void setUp()
74 throws Exception
75 {
76 super.setUp();
77
78 System.setProperty( "org.apache.maven.archetype.repository.directory",
79 getTestPath( "target/test-classes/repositories/test-catalog" ) );
80
81
82 server = new Server( 0 );
83
84 WebAppContext webapp = new WebAppContext();
85 webapp.setContextPath( "/repo" );
86 webapp.setWar( "target/wars/archetype-repository.war" );
87 server.setHandler( webapp );
88
89 server.start();
90
91 port = server.getConnectors()[0].getLocalPort();
92
93 File catalogDirectory = getTestFile( "target/test-classes/repositories/test-catalog" );
94 catalogDirectory.mkdirs();
95
96 getTestFile( "target/test-classes/repositories/test-catalog/dummy" ).createNewFile();
97
98 ArchetypeCatalog catalog = new ArchetypeCatalog();
99 Archetype generatedArchetype = new Archetype();
100 generatedArchetype.setGroupId( "groupId" );
101 generatedArchetype.setArtifactId( "artifactId" );
102 generatedArchetype.setVersion( "1" );
103 generatedArchetype.setRepository( "http://localhost:" + port + "/repo/" );
104 catalog.addArchetype( generatedArchetype );
105
106 File catalogFile = new File( catalogDirectory, "archetype-catalog.xml" );
107 ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
108 Writer writer = new FileWriter( catalogFile );
109 catalogWriter.write( writer, catalog );
110 IOUtils.closeQuietly( writer );
111 }
112
113 protected void tearDown()
114 throws Exception
115 {
116 super.tearDown();
117
118 server.stop();
119 }
120 }