001 package org.apache.maven.tools.plugin.generator; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import org.apache.maven.model.Build; 023 import org.apache.maven.plugin.descriptor.MojoDescriptor; 024 import org.apache.maven.plugin.descriptor.Parameter; 025 import org.apache.maven.plugin.descriptor.PluginDescriptor; 026 import org.apache.maven.project.MavenProject; 027 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest; 028 import org.codehaus.plexus.PlexusTestCase; 029 import org.codehaus.plexus.component.repository.ComponentDependency; 030 import org.codehaus.plexus.util.FileUtils; 031 032 import java.io.File; 033 import java.util.ArrayList; 034 import java.util.Collections; 035 import java.util.List; 036 037 /** 038 * @author <a href="mailto:jason@maven.org">Jason van Zyl </a> 039 * @version $Id: AbstractGeneratorTestCase.java,v 1.1 2005/02/20 16:25:21 040 * jdcasey Exp $ 041 */ 042 public abstract class AbstractGeneratorTestCase 043 extends PlexusTestCase 044 { 045 protected Generator generator; 046 047 protected String basedir; 048 049 protected void setUp() 050 throws Exception 051 { 052 super.setUp(); 053 basedir = System.getProperty( "basedir" ); 054 } 055 056 public void testGenerator() 057 throws Exception 058 { 059 setupGenerator(); 060 061 MojoDescriptor mojoDescriptor = new MojoDescriptor(); 062 mojoDescriptor.setGoal( "testGoal" ); 063 mojoDescriptor.setImplementation( "org.apache.maven.tools.plugin.generator.TestMojo" ); 064 mojoDescriptor.setDependencyResolutionRequired( "compile" ); 065 066 List<Parameter> params = new ArrayList<Parameter>(); 067 068 Parameter param = new Parameter(); 069 param.setExpression( "${project.build.directory}" ); 070 param.setDefaultValue( "</markup-must-be-escaped>" ); 071 param.setName( "dir" ); 072 param.setRequired( true ); 073 param.setType( "java.lang.String" ); 074 param.setDescription( "Test parameter description" ); 075 076 params.add( param ); 077 078 mojoDescriptor.setParameters( params ); 079 080 PluginDescriptor pluginDescriptor = new PluginDescriptor(); 081 mojoDescriptor.setPluginDescriptor( pluginDescriptor ); 082 083 pluginDescriptor.addMojo( mojoDescriptor ); 084 085 pluginDescriptor.setArtifactId( "maven-unitTesting-plugin" ); 086 pluginDescriptor.setGoalPrefix( "test" ); 087 088 ComponentDependency dependency = new ComponentDependency(); 089 dependency.setGroupId( "testGroup" ); 090 dependency.setArtifactId( "testArtifact" ); 091 dependency.setVersion( "0.0.0" ); 092 093 pluginDescriptor.setDependencies( Collections.singletonList( dependency ) ); 094 095 File destinationDirectory = new File( System.getProperty( "java.io.tmpdir" ), "testGenerator-outDir" ); 096 FileUtils.deleteDirectory( destinationDirectory ); 097 destinationDirectory.mkdir(); 098 099 MavenProject mavenProject = new MavenProject(); 100 mavenProject.setGroupId( "foo" ); 101 mavenProject.setArtifactId( "bar" ); 102 mavenProject.setBuild( new Build() 103 { 104 @Override 105 public String getDirectory() 106 { 107 return basedir + "/target"; 108 } 109 110 @Override 111 public String getOutputDirectory() 112 { 113 return basedir + "/target"; 114 } 115 } ); 116 117 generator.execute( destinationDirectory, new DefaultPluginToolsRequest( mavenProject, pluginDescriptor ) ); 118 119 validate( destinationDirectory ); 120 121 FileUtils.deleteDirectory( destinationDirectory ); 122 } 123 124 // ---------------------------------------------------------------------- 125 // 126 // ---------------------------------------------------------------------- 127 128 protected void setupGenerator() 129 throws Exception 130 { 131 String generatorClassName = getClass().getName(); 132 133 generatorClassName = generatorClassName.substring( 0, generatorClassName.length() - 4 ); 134 135 try 136 { 137 Class<?> generatorClass = Thread.currentThread().getContextClassLoader().loadClass( generatorClassName ); 138 139 generator = (Generator) generatorClass.newInstance(); 140 } 141 catch ( Exception e ) 142 { 143 throw new Exception( "Cannot find " + generatorClassName + 144 "! Make sure your test case is named in the form ${generatorClassName}Test " + 145 "or override the setupPlugin() method to instantiate the mojo yourself." ); 146 } 147 } 148 149 // ---------------------------------------------------------------------- 150 // 151 // ---------------------------------------------------------------------- 152 153 protected void validate( File destinationDirectory ) 154 throws Exception 155 { 156 // empty 157 } 158 }