KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > tooling > ArchetypeTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.tooling;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.maven.cli.ConsoleDownloadMonitor;
28 import org.apache.maven.embedder.MavenEmbedder;
29 import org.apache.maven.embedder.MavenEmbedderConsoleLogger;
30 import org.apache.maven.embedder.MavenEmbedderLogger;
31 import org.apache.maven.embedder.PlexusLoggerAdapter;
32 import org.apache.maven.monitor.event.DefaultEventMonitor;
33 import org.apache.maven.monitor.event.EventMonitor;
34 import org.apache.maven.project.MavenProject;
35 import org.codehaus.plexus.util.FileUtils;
36
37 public class ArchetypeTest extends TestCase {
38
39     private File JavaDoc baseDir = new File JavaDoc(System.getProperty("basedir", ".")).getAbsoluteFile();
40
41     private MavenEmbedder maven;
42     private Properties JavaDoc sysProps = System.getProperties();
43     private String JavaDoc version;
44     
45     protected void setUp() throws Exception JavaDoc {
46         maven = new MavenEmbedder();
47         maven.setOffline(true);
48         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
49         maven.setClassLoader(classLoader);
50         MavenEmbedderLogger logger = new MavenEmbedderConsoleLogger();
51         //logger.setThreshold(MavenEmbedderLogger.LEVEL_DEBUG);
52
maven.setLogger(logger);
53         maven.start();
54
55         /*
56         Field f = maven.getClass().getDeclaredField("wagonManager");
57         f.setAccessible(true);
58         WagonManager wagon = (WagonManager) f.get(maven);
59         wagon.setOnline(false);
60         */

61         
62         MavenProject project = maven.readProject(new File JavaDoc(baseDir, "pom.xml"));
63         version = project.getVersion();
64     }
65     
66     protected void tearDown() throws Exception JavaDoc {
67         maven.stop();
68         maven = null;
69         System.gc();
70     }
71     
72     public void testBindingComponent() throws Exception JavaDoc {
73         testServiceMixArchetype("binding-component");
74     }
75     
76     public void testEIPServiceUnit() throws Exception JavaDoc {
77         testServiceMixArchetype("eip-service-unit");
78     }
79     
80     public void testEmbeddedSimple() throws Exception JavaDoc {
81         testServiceMixArchetype("embedded-simple");
82     }
83     
84     public void testHttpConsumerServiceUnit() throws Exception JavaDoc {
85         testServiceMixArchetype("http-consumer-service-unit");
86     }
87     
88     public void testHttpProviderServiceUnit() throws Exception JavaDoc {
89         testServiceMixArchetype("http-provider-service-unit");
90     }
91     
92     public void testJmsConsumerServiceUnit() throws Exception JavaDoc {
93         testServiceMixArchetype("jms-consumer-service-unit");
94     }
95     
96     public void testJmsProviderServiceUnit() throws Exception JavaDoc {
97         testServiceMixArchetype("jms-provider-service-unit");
98     }
99     
100     public void testJsr181WsdlFirstServiceUnit() throws Exception JavaDoc {
101         testServiceMixArchetype("jsr181-wsdl-first-service-unit");
102     }
103     
104     public void testLwContainerServiceUnit() throws Exception JavaDoc {
105         testServiceMixArchetype("lwcontainer-service-unit");
106     }
107     
108     public void testServiceAssembly() throws Exception JavaDoc {
109         testServiceMixArchetype("service-assembly");
110     }
111     
112     public void testServiceEngine() throws Exception JavaDoc {
113         testServiceMixArchetype("service-engine");
114     }
115     
116     public void testServiceUnit() throws Exception JavaDoc {
117         testServiceMixArchetype("service-unit");
118     }
119     
120     public void testSharedLibrary() throws Exception JavaDoc {
121         testServiceMixArchetype("shared-library");
122     }
123     
124     protected void testServiceMixArchetype(String JavaDoc artifactId) throws Exception JavaDoc {
125         testArchetype("org.apache.servicemix.tooling", "servicemix-" + artifactId, version);
126     }
127     
128     protected void testArchetype(String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version) throws Exception JavaDoc {
129         File JavaDoc targetDir = new File JavaDoc(baseDir, "target/archetypes/" + artifactId);
130         FileUtils.deleteDirectory(targetDir);
131         targetDir.mkdirs();
132         EventMonitor eventMonitor = new DefaultEventMonitor(new PlexusLoggerAdapter(
133                         new MavenEmbedderConsoleLogger()));
134         Properties JavaDoc props = new Properties JavaDoc();
135         props.setProperty("archetypeGroupId", groupId);
136         props.setProperty("archetypeArtifactId", artifactId);
137         props.setProperty("archetypeVersion", version);
138         props.setProperty("groupId", "sample");
139         props.setProperty("artifactId", "sample");
140         props.setProperty("basedir", targetDir.getAbsolutePath());
141         
142         MavenProject parent = maven.readProject(getDefaultArchetypePom());
143         System.setProperties((Properties JavaDoc) sysProps.clone());
144         System.setProperty("user.dir", targetDir.getAbsolutePath());
145         maven.execute(parent,
146                       Collections.singletonList("archetype:create"),
147                       eventMonitor,
148                       new ConsoleDownloadMonitor(),
149                       props,
150                       targetDir);
151
152         System.setProperties((Properties JavaDoc) sysProps.clone());
153         targetDir = new File JavaDoc(targetDir, "sample");
154         System.setProperty("user.dir", targetDir.getAbsolutePath());
155         System.setProperty("basedir", targetDir.getAbsolutePath());
156         MavenProject prj = maven.readProject(new File JavaDoc(targetDir, "pom.xml"));
157         maven.execute(prj,
158                       Collections.singletonList("package"),
159                       eventMonitor,
160                       new ConsoleDownloadMonitor(),
161                       new Properties JavaDoc(),
162                       targetDir);
163     }
164
165     private File JavaDoc getDefaultArchetypePom() throws IOException JavaDoc {
166         URL JavaDoc archetypePom = getClass().getClassLoader().getResource("archetype-pom.xml");
167         File JavaDoc pomFile = File.createTempFile("archetypePom", "xml");
168         FileUtils.copyURLToFile(archetypePom, pomFile);
169         return pomFile;
170     }
171
172 }
173
Popular Tags