1 package com.simpligility.maven.plugins.android;
2
3 import static org.easymock.EasyMock.*;
4
5 import java.io.File;
6
7 import org.apache.commons.io.FileUtils;
8 import org.apache.commons.io.FilenameUtils;
9 import org.apache.maven.execution.MavenSession;
10 import org.apache.maven.plugin.DebugConfigurationListener;
11 import org.apache.maven.plugin.MojoExecution;
12 import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
13 import org.apache.maven.plugin.descriptor.MojoDescriptor;
14 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
15 import org.apache.maven.project.MavenProject;
16 import org.apache.maven.project.path.DefaultPathTranslator;
17 import org.apache.maven.project.path.PathTranslator;
18 import org.codehaus.plexus.component.configurator.ComponentConfigurator;
19 import org.codehaus.plexus.component.configurator.ConfigurationListener;
20 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
21 import org.codehaus.plexus.configuration.PlexusConfiguration;
22 import org.codehaus.plexus.logging.Logger;
23 import org.codehaus.plexus.logging.console.ConsoleLogger;
24 import org.junit.Assert;
25 import org.powermock.reflect.Whitebox;
26
27 import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
28 import com.simpligility.maven.plugins.android.standalonemojos.ManifestUpdateMojo;
29 import com.simpligility.maven.plugins.android.standalonemojos.MojoProjectStub;
30
31 public abstract class AbstractAndroidMojoTestCase<T extends AbstractAndroidMojo> extends AbstractMojoTestCase {
32
33 protected MavenSession session;
34 protected MojoExecution execution;
35
36
37
38
39
40
41
42
43
44
45 public abstract String getPluginGoalName();
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 protected T createMojo(String resourceProject) throws Exception {
65
66 String testResourcePath = "src/test/resources/" + resourceProject;
67 testResourcePath = FilenameUtils.separatorsToSystem(testResourcePath);
68 File exampleDir = new File(getBasedir(), testResourcePath);
69 Assert.assertTrue("Path should exist: " + exampleDir, exampleDir.exists());
70
71
72 String testingPath = "target/tests/" + this.getClass().getSimpleName() + "." + getName();
73 testingPath = FilenameUtils.separatorsToSystem(testingPath);
74 File testingDir = new File(getBasedir(), testingPath);
75
76 if (testingDir.exists()) {
77 FileUtils.cleanDirectory(testingDir);
78 } else {
79 Assert.assertTrue("Could not create directory: " + testingDir, testingDir.mkdirs());
80 }
81
82
83
84
85 FileUtils.copyDirectory(exampleDir, testingDir);
86
87
88 final MavenProject project = new MojoProjectStub(testingDir);
89
90
91 PlexusConfiguration config = extractPluginConfiguration("android-maven-plugin", project.getFile());
92 @SuppressWarnings("unchecked")
93 final T mojo = (T) lookupMojo(getPluginGoalName(), project.getFile());
94
95
96 setVariableValueToObject(mojo, "project", project);
97
98
99
100 MojoDescriptor mojoDesc = new MojoDescriptor();
101
102 mojoDesc.setGoal(getPluginGoalName());
103 MojoExecution mojoExec = new MojoExecution(mojoDesc);
104
105
106
107 PathTranslator pathTranslator = new DefaultPathTranslator();
108
109 Logger logger = new ConsoleLogger(Logger.LEVEL_DEBUG, mojo.getClass().getName());
110
111 MavenSession context = createNiceMock( MavenSession.class );
112
113 expect( context.getExecutionProperties() ).andReturn( project.getProperties() );
114 expect( context.getCurrentProject() ).andReturn( project );
115 replay( context );
116
117
118 ExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(
119 context, mojoExec, pathTranslator, logger, project, project.getProperties() );
120
121 ComponentConfigurator configurator = (ComponentConfigurator) lookup(ComponentConfigurator.ROLE,"basic");
122
123 ConfigurationListener listener = new DebugConfigurationListener( logger );
124 configurator.configureComponent( mojo, config, evaluator, getContainer().getContainerRealm(), listener );
125
126 this.session = context;
127 this.execution = mojoExec;
128
129 Whitebox.setInternalState( mojo, "session", this.session );
130 Whitebox.setInternalState( mojo, "execution", this.execution );
131
132 return mojo;
133 }
134
135
136
137
138
139
140
141
142
143
144 public File getProjectDir(AbstractAndroidMojo mojo) throws IllegalAccessException {
145 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
146 return project.getFile().getParentFile();
147 }
148 }