Java tutorial
/** * Copyright (C) 2011 Kurt Zettel kurt@goodformobile.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.goodformobile.build.mobile; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import org.apache.commons.io.FileUtils; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.project.MavenProject; import org.apache.maven.settings.DefaultMavenSettingsBuilder; import org.apache.maven.settings.Profile; import org.apache.maven.settings.Settings; import org.apache.maven.settings.building.DefaultSettingsBuilder; import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; import org.codehaus.plexus.util.StringUtils; import org.junit.Before; public abstract class AbstractRIMMojoTest<T extends AbstractRIMBuildMojo> extends AbstractMojoTestCase { @Override @Before protected void setUp() throws Exception { // This must be here. Without it components cannot be looked up. super.setUp(); File pluginXml = null; try { pluginXml = getRelativeFile("META-INF/maven/plugin.xml"); } catch (Throwable t) { fail("META-INF/maven/plugin.xml must exist in the target directory. This is generated by Maven. Try building once and then re-run this test case."); } assertTrue( "META-INF/maven/plugin.xml must exist in the target directory. This is generated by Maven. Try building once and then re-run this test case.", pluginXml.exists()); } protected File getRelativeFile(String path) throws URISyntaxException { URL url = getClassLoader().getResource(path); File pluginConfig = new File(url.toURI()); return pluginConfig; } protected T setupMojo() throws URISyntaxException, Exception { File pluginConfig = getRelativeFile(getPluginConfigFileName()); assertTrue("Unable to find file: " + pluginConfig.getAbsolutePath(), pluginConfig.exists()); @SuppressWarnings("unchecked") T mojo = (T) lookupMojo(getGoal(), pluginConfig); setVariableValueToObject(mojo, "rapcDirectory", new File(getVariableFromRimProfile("jde.directory"))); assertNotNull(mojo); return mojo; } protected abstract String getGoal(); protected abstract String getPluginConfigFileName(); protected MavenProject getProject(AbstractRIMBuildMojo mojo) throws IllegalAccessException { MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); assertNotNull(project); return project; } protected File setupCleanCopyOfProject(File projectDirectory) throws IOException { File workProjectDirectory = new File(getBasedir(), "target/temp/project"); if (workProjectDirectory.exists()) { FileUtils.cleanDirectory(workProjectDirectory); } FileUtils.copyDirectory(projectDirectory, workProjectDirectory); return workProjectDirectory; } protected Settings getSettings() throws Exception { DefaultSettingsBuilderFactory defaultSettingsBuilderFactory = new DefaultSettingsBuilderFactory(); DefaultSettingsBuilder settingsBuilder = defaultSettingsBuilderFactory.newInstance(); DefaultMavenSettingsBuilder mavenSettingsBuilder = new DefaultMavenSettingsBuilder(); setVariableValueToObject(mavenSettingsBuilder, "settingsBuilder", settingsBuilder); return mavenSettingsBuilder.buildSettings(); } protected String getVariableFromRimProfile(String parameterName) throws Exception { Settings settings = getSettings(); for (Profile profile : settings.getProfiles()) { if (profile.getId().toLowerCase().contains("rim")) { String value = profile.getProperties().getProperty(parameterName); if (!StringUtils.isEmpty(value)) { return value; } } } throw new MojoExecutionException("Unable to find valid parameter for [" + parameterName + "] in any profiles with an id containing the word rim."); } protected void setupProject(File workProjectDirectory, MavenProject project) { project.setArtifactId("maven-test-app"); MavenProjectBasicStub projectStub = (MavenProjectBasicStub) project; projectStub.setBaseDir(workProjectDirectory); File buildDirectory = new File(workProjectDirectory, "target"); project.getBuild().setDirectory(buildDirectory.getAbsolutePath()); project.getBuild().setOutputDirectory( new File(new File(workProjectDirectory, "target"), "classes").getAbsolutePath()); File source = new File(workProjectDirectory, "src"); File main = new File(source, "main"); File java = new File(main, "java"); File resources = new File(main, "resources"); // Setup the source directories. project.getCompileSourceRoots().add(java.getAbsolutePath()); project.getCompileSourceRoots().add(resources.getAbsolutePath()); } protected boolean isWindows() { String os = System.getProperty("os.name").toLowerCase(); // windows return (os.indexOf("win") >= 0); } }