1 17 package org.apache.servicemix.tooling; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.URL ; 22 import java.util.Collections ; 23 import java.util.Properties ; 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 baseDir = new File (System.getProperty("basedir", ".")).getAbsoluteFile(); 40 41 private MavenEmbedder maven; 42 private Properties sysProps = System.getProperties(); 43 private String version; 44 45 protected void setUp() throws Exception { 46 maven = new MavenEmbedder(); 47 maven.setOffline(true); 48 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 49 maven.setClassLoader(classLoader); 50 MavenEmbedderLogger logger = new MavenEmbedderConsoleLogger(); 51 maven.setLogger(logger); 53 maven.start(); 54 55 61 62 MavenProject project = maven.readProject(new File (baseDir, "pom.xml")); 63 version = project.getVersion(); 64 } 65 66 protected void tearDown() throws Exception { 67 maven.stop(); 68 maven = null; 69 System.gc(); 70 } 71 72 public void testBindingComponent() throws Exception { 73 testServiceMixArchetype("binding-component"); 74 } 75 76 public void testEIPServiceUnit() throws Exception { 77 testServiceMixArchetype("eip-service-unit"); 78 } 79 80 public void testEmbeddedSimple() throws Exception { 81 testServiceMixArchetype("embedded-simple"); 82 } 83 84 public void testHttpConsumerServiceUnit() throws Exception { 85 testServiceMixArchetype("http-consumer-service-unit"); 86 } 87 88 public void testHttpProviderServiceUnit() throws Exception { 89 testServiceMixArchetype("http-provider-service-unit"); 90 } 91 92 public void testJmsConsumerServiceUnit() throws Exception { 93 testServiceMixArchetype("jms-consumer-service-unit"); 94 } 95 96 public void testJmsProviderServiceUnit() throws Exception { 97 testServiceMixArchetype("jms-provider-service-unit"); 98 } 99 100 public void testJsr181WsdlFirstServiceUnit() throws Exception { 101 testServiceMixArchetype("jsr181-wsdl-first-service-unit"); 102 } 103 104 public void testLwContainerServiceUnit() throws Exception { 105 testServiceMixArchetype("lwcontainer-service-unit"); 106 } 107 108 public void testServiceAssembly() throws Exception { 109 testServiceMixArchetype("service-assembly"); 110 } 111 112 public void testServiceEngine() throws Exception { 113 testServiceMixArchetype("service-engine"); 114 } 115 116 public void testServiceUnit() throws Exception { 117 testServiceMixArchetype("service-unit"); 118 } 119 120 public void testSharedLibrary() throws Exception { 121 testServiceMixArchetype("shared-library"); 122 } 123 124 protected void testServiceMixArchetype(String artifactId) throws Exception { 125 testArchetype("org.apache.servicemix.tooling", "servicemix-" + artifactId, version); 126 } 127 128 protected void testArchetype(String groupId, String artifactId, String version) throws Exception { 129 File targetDir = new File (baseDir, "target/archetypes/" + artifactId); 130 FileUtils.deleteDirectory(targetDir); 131 targetDir.mkdirs(); 132 EventMonitor eventMonitor = new DefaultEventMonitor(new PlexusLoggerAdapter( 133 new MavenEmbedderConsoleLogger())); 134 Properties props = new Properties (); 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 ) 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 ) sysProps.clone()); 153 targetDir = new File (targetDir, "sample"); 154 System.setProperty("user.dir", targetDir.getAbsolutePath()); 155 System.setProperty("basedir", targetDir.getAbsolutePath()); 156 MavenProject prj = maven.readProject(new File (targetDir, "pom.xml")); 157 maven.execute(prj, 158 Collections.singletonList("package"), 159 eventMonitor, 160 new ConsoleDownloadMonitor(), 161 new Properties (), 162 targetDir); 163 } 164 165 private File getDefaultArchetypePom() throws IOException { 166 URL archetypePom = getClass().getClassLoader().getResource("archetype-pom.xml"); 167 File pomFile = File.createTempFile("archetypePom", "xml"); 168 FileUtils.copyURLToFile(archetypePom, pomFile); 169 return pomFile; 170 } 171 172 } 173 | Popular Tags |