diff --git a/.gitignore b/.gitignore index a749801..6ecd8d6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ target .settings .idea *.iml - +*.ipr +*.iws diff --git a/README b/README deleted file mode 100644 index 0bee332..0000000 --- a/README +++ /dev/null @@ -1,29 +0,0 @@ -1. Introduction. - - FitNesse plugin that provides Maven Classpath support. - -2. How to use. - - - Download the distribution. - - Get yourself an up-to-date copy of fitnesse (> 20110601) - - Add the following line to plugins.properties: SymbolTypes = fitnesse.wikitext.widgets.MavenClasspathSymbolType. - - Refer to the pom file as follows: !pomFile /path/to/pom.xml. - - you can define the file as pom.xml@compile to include a specific scope. - - For v20101101 and earlier: - - Download the 1.2 distribution. - - Add the following line to plugins.properties: WikiWidgets = fitnesse.wikitext.widgets.MavenClasspathWidget. - - Copy the maven-classpath-plugin-[version]-jar-with-dependencies.jar to the plugins directory of the directory where the fitnesse.jar is located. - - Start FitNesse: java -jar fitnesse.jar - - Refer to the pom file as follows: !pomFile /path/to/pom.xml. - - The plugin will use the test classpath from the pom as classpath in FitNesse. - - ** Releases prior to v20091121: - - Run fitnesse as follows: java -cp fitnesse.jar:plugins/maven-classpath-plugin-[version]-jar-with-dependencies.jar fitnesseMain.FitNesseMain. (**) - -3. How to contribute. - - - Send patches. - - Fork the repository and send pull requests. - - diff --git a/README.md b/README.md new file mode 100644 index 0000000..39b7bc7 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Introduction. + +FitNesse plugin that provides Maven Classpath support. + +# How to use. + + - Download the distribution. + - Get yourself an up-to-date copy of fitnesse (>= 20150114) + - Add the following line to plugins.properties: + + SymbolTypes = fitnesse.wikitext.widgets.MavenClasspathSymbolType + + - Refer to the pom file as follows: + + !pomFile /path/to/pom.xml + + - you can define the file as `pom.xml@compile` to include a specific scope. + +# How to contribute. + + - Fork the repository and send pull requests. + + diff --git a/pom.xml b/pom.xml index a046df3..c61ce0a 100644 --- a/pom.xml +++ b/pom.xml @@ -1,77 +1,104 @@ - - 4.0.0 - org.fitnesse.plugins - maven-classpath-plugin - Maven Classpath Plugin - 1.3-SNAPSHOT + + 4.0.0 + + + org.sonatype.oss + oss-parent + 7 + + + org.fitnesse.plugins + maven-classpath-plugin + Maven Classpath Plugin + 1.10-SNAPSHOT + https://github.com/amolenaar/fitnesse-maven-classpath + + + FitNesse plugin for adding libraries from a Maven pom.xml configuration. + - - - org.jvnet.hudson - hudson-maven-embedder - 3.2 - - - org.fitnesse - fitnesse - 20111003 - provided - - - junit - junit - 4.6 - test - - - org.mockito - mockito-all - 1.8.5 - test - - + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + scm:git:git@github.com:amolenaar/fitnesse-maven-classpath.git + scm:git:git@github.com:amolenaar/fitnesse-maven-classpath.git + scm:git:git@github.com:amolenaar/fitnesse-maven-classpath.git + + + + + org.fitnesse + fitnesse + 20150114 + provided + + + junit + junit + 4.6 + test + + + org.mockito + mockito-all + 1.8.5 + test + + + org.apache.maven + maven-embedder + 3.0.4 + jar + compile + + - - - os-xebia - Xebia Open Source Repository - http://os.xebia.com/repository/ - + + + + maven-assembly-plugin + 2.2-beta-4 + + + jar-with-dependencies + + + + + make-jar + package + + single + + + + + + maven-compiler-plugin + + 1.6 + 1.6 + + + + - - fitnesse-local - file://${basedir}/src/fitnesse - - - - - - - maven-assembly-plugin - 2.2-beta-4 - - - jar-with-dependencies - - - - - make-jar - package - - single - - - - - - maven-compiler-plugin - - 1.6 - 1.6 - - - - + + + lvonk + lvonk@gmail.com + Lars Vonk + + + amolenaar + amolenaar@xebia.com + Arjan Molenaar + + diff --git a/src/fitnesse/org/fitnesse/fitnesse/20110104/fitnesse-20110104.jar b/src/fitnesse/org/fitnesse/fitnesse/20110104/fitnesse-20110104.jar deleted file mode 100644 index 5c3f81d..0000000 Binary files a/src/fitnesse/org/fitnesse/fitnesse/20110104/fitnesse-20110104.jar and /dev/null differ diff --git a/src/main/java/fitnesse/wikitext/widgets/DependencyResolvingMavenEmbedder.java b/src/main/java/fitnesse/wikitext/widgets/DependencyResolvingMavenEmbedder.java deleted file mode 100644 index d147b15..0000000 --- a/src/main/java/fitnesse/wikitext/widgets/DependencyResolvingMavenEmbedder.java +++ /dev/null @@ -1,551 +0,0 @@ -package fitnesse.wikitext.widgets; - -import hudson.maven.MavenEmbedderException; -import hudson.maven.MavenEmbedderUtils; -import hudson.maven.MavenRequest; -import org.apache.maven.DefaultMaven; -import org.apache.maven.Maven; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.InvalidRepositoryException; -import org.apache.maven.artifact.manager.WagonManager; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; -import org.apache.maven.cli.MavenCli; -import org.apache.maven.execution.*; -import org.apache.maven.model.Model; -import org.apache.maven.model.Profile; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.model.io.xpp3.MavenXpp3Writer; -import org.apache.maven.plugin.LegacySupport; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.*; -import org.apache.maven.repository.RepositorySystem; -import org.apache.maven.settings.*; -import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; -import org.apache.maven.settings.building.SettingsBuilder; -import org.apache.maven.settings.building.SettingsBuildingException; -import org.apache.maven.settings.building.SettingsBuildingRequest; -import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.util.DirectoryScanner; -import org.codehaus.plexus.util.Os; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -import org.sonatype.aether.RepositorySystemSession; - -import java.io.*; -import java.util.*; - -/** - * Copied from Hudson's Maven Embedded which hides most of this behind private methods, making it impossible to change - * it's behavior. Only change is the addition of buildProject which builds the project using dependency resolution. - */ -public class DependencyResolvingMavenEmbedder { - public static final String userHome = System.getProperty("user.home"); - - private MavenXpp3Reader modelReader; - private MavenXpp3Writer modelWriter; - - private final File mavenHome; - private final PlexusContainer plexusContainer; - private final MavenRequest mavenRequest; - private MavenExecutionRequest mavenExecutionRequest; - private final MavenSession mavenSession; - - public DependencyResolvingMavenEmbedder(File mavenHome, MavenRequest mavenRequest) throws MavenEmbedderException { - this(mavenHome, mavenRequest, MavenEmbedderUtils.buildPlexusContainer(mavenHome, mavenRequest)); - } - - public DependencyResolvingMavenEmbedder(ClassLoader mavenClassLoader, ClassLoader parent, MavenRequest mavenRequest) throws MavenEmbedderException { - this(null, mavenRequest, MavenEmbedderUtils.buildPlexusContainer(mavenClassLoader, parent, mavenRequest)); - } - - private DependencyResolvingMavenEmbedder(File mavenHome, MavenRequest mavenRequest, PlexusContainer plexusContainer) - throws MavenEmbedderException { - this.mavenHome = mavenHome; - this.mavenRequest = mavenRequest; - this.plexusContainer = plexusContainer; - - try { - this.buildMavenExecutionRequest(); - - RepositorySystemSession rss = ((DefaultMaven) lookup(Maven.class)).newRepositorySession(mavenExecutionRequest); - - mavenSession = new MavenSession(plexusContainer, rss, mavenExecutionRequest, new DefaultMavenExecutionResult()); - - lookup(LegacySupport.class).setSession(mavenSession); - } catch (MavenEmbedderException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } catch (ComponentLookupException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } - } - - - public DependencyResolvingMavenEmbedder(ClassLoader mavenClassLoader, MavenRequest mavenRequest) throws MavenEmbedderException { - this(mavenClassLoader, null, mavenRequest); - } - - - private void buildMavenExecutionRequest() - throws MavenEmbedderException, ComponentLookupException { - this.mavenExecutionRequest = new DefaultMavenExecutionRequest(); - - if (this.mavenRequest.getGlobalSettingsFile() != null) { - this.mavenExecutionRequest.setGlobalSettingsFile(this.mavenExecutionRequest.getGlobalSettingsFile()); - } - - if (this.mavenExecutionRequest.getUserSettingsFile() != null) { - this.mavenExecutionRequest.setUserSettingsFile(new File(mavenRequest.getUserSettingsFile())); - } - - try { - lookup(MavenExecutionRequestPopulator.class).populateFromSettings(this.mavenExecutionRequest, - getSettings()); - - lookup(MavenExecutionRequestPopulator.class).populateDefaults(mavenExecutionRequest); - } catch (MavenExecutionRequestPopulationException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } - - ArtifactRepository localRepository = getLocalRepository(); - this.mavenExecutionRequest.setLocalRepository(localRepository); - this.mavenExecutionRequest.setLocalRepositoryPath(localRepository.getBasedir()); - this.mavenExecutionRequest.setOffline(this.mavenExecutionRequest.isOffline()); - - this.mavenExecutionRequest.setUpdateSnapshots(this.mavenRequest.isUpdateSnapshots()); - - // TODO check null and create a console one ? - this.mavenExecutionRequest.setTransferListener(this.mavenRequest.getTransferListener()); - - this.mavenExecutionRequest.setCacheNotFound(this.mavenRequest.isCacheNotFound()); - this.mavenExecutionRequest.setCacheTransferError(true); - - this.mavenExecutionRequest.setUserProperties(this.mavenRequest.getUserProperties()); - this.mavenExecutionRequest.getSystemProperties().putAll(System.getProperties()); - if (this.mavenRequest.getSystemProperties() != null) { - this.mavenExecutionRequest.getSystemProperties().putAll(this.mavenRequest.getSystemProperties()); - } - this.mavenExecutionRequest.getSystemProperties().putAll(getEnvVars()); - - if (this.mavenHome != null) { - this.mavenExecutionRequest.getSystemProperties().put("maven.home", this.mavenHome.getAbsolutePath()); - } - - if (this.mavenRequest.getProfiles() != null && !this.mavenRequest.getProfiles().isEmpty()) { - for (String id : this.mavenRequest.getProfiles()) { - Profile p = new Profile(); - p.setId(id); - p.setSource("cli"); - this.mavenExecutionRequest.addProfile(p); - this.mavenExecutionRequest.addActiveProfile(id); - } - } - - this.mavenExecutionRequest.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_WARN); - - lookup(Logger.class).setThreshold(Logger.LEVEL_WARN); - - this.mavenExecutionRequest.setExecutionListener(this.mavenRequest.getExecutionListener()) - .setInteractiveMode(this.mavenRequest.isInteractive()) - .setGlobalChecksumPolicy(this.mavenRequest.getGlobalChecksumPolicy()) - .setGoals(this.mavenRequest.getGoals()); - - if (this.mavenRequest.getPom() != null) { - this.mavenExecutionRequest.setPom(new File(this.mavenRequest.getPom())); - } - - if (this.mavenRequest.getWorkspaceReader() != null) { - this.mavenExecutionRequest.setWorkspaceReader(this.mavenRequest.getWorkspaceReader()); - } - } - - - private Properties getEnvVars() { - Properties envVars = new Properties(); - boolean caseSensitive = !Os.isFamily(Os.FAMILY_WINDOWS); - for (Map.Entry entry : System.getenv().entrySet()) { - String key = "env." + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH)); - envVars.setProperty(key, entry.getValue()); - } - return envVars; - } - - public Settings getSettings() - throws MavenEmbedderException, ComponentLookupException { - - SettingsBuildingRequest settingsBuildingRequest = new DefaultSettingsBuildingRequest(); - if (this.mavenRequest.getGlobalSettingsFile() != null) { - settingsBuildingRequest.setGlobalSettingsFile(new File(this.mavenRequest.getGlobalSettingsFile())); - } else { - settingsBuildingRequest.setGlobalSettingsFile(MavenCli.DEFAULT_GLOBAL_SETTINGS_FILE); - } - if (this.mavenRequest.getUserSettingsFile() != null) { - settingsBuildingRequest.setUserSettingsFile(new File(this.mavenRequest.getUserSettingsFile())); - } else { - settingsBuildingRequest.setUserSettingsFile(MavenCli.DEFAULT_USER_SETTINGS_FILE); - } - - settingsBuildingRequest.setUserProperties(this.mavenRequest.getUserProperties()); - settingsBuildingRequest.getSystemProperties().putAll(System.getProperties()); - settingsBuildingRequest.getSystemProperties().putAll(this.mavenRequest.getSystemProperties()); - settingsBuildingRequest.getSystemProperties().putAll(getEnvVars()); - - try { - return lookup(SettingsBuilder.class).build(settingsBuildingRequest).getEffectiveSettings(); - } catch (SettingsBuildingException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } - } - - public ArtifactRepository getLocalRepository() throws ComponentLookupException { - try { - String localRepositoryPath = getLocalRepositoryPath(); - if (localRepositoryPath != null) { - return lookup(RepositorySystem.class).createLocalRepository(new File(localRepositoryPath)); - } - return lookup(RepositorySystem.class).createLocalRepository(RepositorySystem.defaultUserLocalRepository); - } catch (InvalidRepositoryException e) { - // never happened - throw new IllegalStateException(e); - } - } - - public String getLocalRepositoryPath() { - String path = null; - - try { - Settings settings = getSettings(); - path = settings.getLocalRepository(); - } catch (MavenEmbedderException e) { - // ignore - } catch (ComponentLookupException e) { - // ignore - } - - if (this.mavenRequest.getLocalRepositoryPath() != null) { - path = this.mavenRequest.getLocalRepositoryPath(); - } - - if (path == null) { - path = RepositorySystem.defaultUserLocalRepository.getAbsolutePath(); - } - return path; - } - - // ---------------------------------------------------------------------- - // Model - // ---------------------------------------------------------------------- - - public Model readModel(File model) - throws XmlPullParserException, FileNotFoundException, IOException { - return modelReader.read(new FileReader(model)); - } - - public void writeModel(Writer writer, Model model) - throws IOException { - modelWriter.write(writer, model); - } - - // ---------------------------------------------------------------------- - // Project - // ---------------------------------------------------------------------- - - public MavenProject readProject(File mavenProject) - throws ProjectBuildingException, MavenEmbedderException { - - List projects = readProjects(mavenProject, false); - return projects == null || projects.isEmpty() ? null : projects.get(0); - - } - - public List readProjects(File mavenProject, boolean recursive) - throws ProjectBuildingException, MavenEmbedderException { - ClassLoader originalCl = Thread.currentThread().getContextClassLoader(); - try { - List results = buildProjects(mavenProject, recursive); - List projects = new ArrayList(results.size()); - for (ProjectBuildingResult result : results) { - projects.add(result.getProject()); - } - return projects; - } finally { - Thread.currentThread().setContextClassLoader(originalCl); - } - - } - - public ProjectBuildingResult buildProject(File mavenProject) throws ProjectBuildingException, MavenEmbedderException { - ClassLoader originalCl = Thread.currentThread().getContextClassLoader(); - try { - Thread.currentThread().setContextClassLoader(this.plexusContainer.getContainerRealm()); - ProjectBuilder projectBuilder = lookup(ProjectBuilder.class); - ProjectBuildingRequest projectBuildingRequest = this.mavenExecutionRequest.getProjectBuildingRequest(); - - projectBuildingRequest.setValidationLevel(this.mavenRequest.getValidationLevel()); - - RepositorySystemSession repositorySystemSession = buildRepositorySystemSession(); - - projectBuildingRequest.setRepositorySession(repositorySystemSession); - - projectBuildingRequest.setProcessPlugins(this.mavenRequest.isProcessPlugins()); - - projectBuildingRequest.setResolveDependencies(this.mavenRequest.isResolveDependencies()); - - return projectBuilder.build(mavenProject, projectBuildingRequest); - } catch (ComponentLookupException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } finally { - Thread.currentThread().setContextClassLoader(originalCl); - } - - } - - public List buildProjects(File mavenProject, boolean recursive) - throws ProjectBuildingException, MavenEmbedderException { - ClassLoader originalCl = Thread.currentThread().getContextClassLoader(); - try { - Thread.currentThread().setContextClassLoader(this.plexusContainer.getContainerRealm()); - ProjectBuilder projectBuilder = lookup(ProjectBuilder.class); - ProjectBuildingRequest projectBuildingRequest = this.mavenExecutionRequest.getProjectBuildingRequest(); - - projectBuildingRequest.setValidationLevel(this.mavenRequest.getValidationLevel()); - - RepositorySystemSession repositorySystemSession = buildRepositorySystemSession(); - - projectBuildingRequest.setRepositorySession(repositorySystemSession); - - projectBuildingRequest.setProcessPlugins(this.mavenRequest.isProcessPlugins()); - - projectBuildingRequest.setResolveDependencies(this.mavenRequest.isResolveDependencies()); - - - List results = projectBuilder.build(Arrays.asList(mavenProject), recursive, projectBuildingRequest); - - return results; - } catch (ComponentLookupException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } finally { - Thread.currentThread().setContextClassLoader(originalCl); - } - - } - - private RepositorySystemSession buildRepositorySystemSession() throws ComponentLookupException { - DefaultMaven defaultMaven = (DefaultMaven) plexusContainer.lookup(Maven.class); - return defaultMaven.newRepositorySession(mavenExecutionRequest); - } - - public List collectProjects(File basedir, String[] includes, String[] excludes) - throws MojoExecutionException, MavenEmbedderException { - List projects = new ArrayList(); - - List poms = getPomFiles(basedir, includes, excludes); - - for (File pom : poms) { - try { - MavenProject p = readProject(pom); - - projects.add(p); - - } catch (ProjectBuildingException e) { - throw new MojoExecutionException("Error loading " + pom, e); - } - } - - return projects; - } - - // ---------------------------------------------------------------------- - // Artifacts - // ---------------------------------------------------------------------- - - public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) - throws MavenEmbedderException { - try { - RepositorySystem repositorySystem = lookup(RepositorySystem.class); - return repositorySystem.createArtifact(groupId, artifactId, version, scope, type); - } catch (ComponentLookupException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } - - } - - public Artifact createArtifactWithClassifier(String groupId, String artifactId, String version, String type, String classifier) - throws MavenEmbedderException { - try { - RepositorySystem repositorySystem = lookup(RepositorySystem.class); - return repositorySystem.createArtifactWithClassifier(groupId, artifactId, version, type, classifier); - } catch (ComponentLookupException e) { - throw new MavenEmbedderException(e.getMessage(), e); - } - } - - public void resolve(Artifact artifact, List remoteRepositories, ArtifactRepository localRepository) - throws ArtifactResolutionException, ArtifactNotFoundException { - // FIXME ? - } - - // ---------------------------------------------------------------------- - // Execution of phases/goals - // ---------------------------------------------------------------------- - - public MavenExecutionResult execute(MavenRequest mavenRequest) - throws ComponentLookupException { - Maven maven = lookup(Maven.class); - ClassLoader original = Thread.currentThread().getContextClassLoader(); - try { - Thread.currentThread().setContextClassLoader(this.plexusContainer.getContainerRealm()); - return maven.execute(mavenExecutionRequest); - } finally { - Thread.currentThread().setContextClassLoader(original); - } - } - // ---------------------------------------------------------------------- - // Local Repository - // ---------------------------------------------------------------------- - - public static final String DEFAULT_LOCAL_REPO_ID = "local"; - - public static final String DEFAULT_LAYOUT_ID = "default"; - - public ArtifactRepository createLocalRepository(File localRepository) - throws ComponentLookupException { - return createLocalRepository(localRepository.getAbsolutePath(), DEFAULT_LOCAL_REPO_ID); - } - - public ArtifactRepository createLocalRepository(Settings settings) - throws ComponentLookupException { - return createLocalRepository(settings.getLocalRepository(), DEFAULT_LOCAL_REPO_ID); - } - - public ArtifactRepository createLocalRepository(String url, String repositoryId) - throws ComponentLookupException { - if (!url.startsWith("file:")) { - url = "file://" + url; - } - - return createRepository(url, repositoryId); - } - - public ArtifactRepository createRepository(String url, String repositoryId) - throws ComponentLookupException { - // snapshots vs releases - // offline = to turning the update policy off - - //TODO: we'll need to allow finer grained creation of repositories but this will do for now - - String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS; - - String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN; - - ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy(true, updatePolicyFlag, checksumPolicyFlag); - - ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy(true, updatePolicyFlag, checksumPolicyFlag); - - RepositorySystem repositorySystem = lookup(RepositorySystem.class); - - ArtifactRepositoryLayout repositoryLayout = lookup(ArtifactRepositoryLayout.class, "default"); - - return repositorySystem.createArtifactRepository(repositoryId, url, repositoryLayout, snapshotsPolicy, releasesPolicy); - - } - - // ---------------------------------------------------------------------- - // Internal utility code - // ---------------------------------------------------------------------- - - - private List getPomFiles(File basedir, String[] includes, String[] excludes) { - DirectoryScanner scanner = new DirectoryScanner(); - - scanner.setBasedir(basedir); - - scanner.setIncludes(includes); - - scanner.setExcludes(excludes); - - scanner.scan(); - - List poms = new ArrayList(); - - for (int i = 0; i < scanner.getIncludedFiles().length; i++) { - poms.add(new File(basedir, scanner.getIncludedFiles()[i])); - } - - return poms; - } - - - /** - * {@link org.apache.maven.artifact.manager.WagonManager} can't configure itself from {@link Settings}, so we need to baby-sit them. - * So much for dependency injection. - */ - private void resolveParameters(WagonManager wagonManager, Settings settings) - throws ComponentLookupException, ComponentLifecycleException, SettingsConfigurationException { - - // TODO todo or not todo ? - - Proxy proxy = settings.getActiveProxy(); - - if (proxy != null) { - if (proxy.getHost() == null) { - throw new SettingsConfigurationException("Proxy in settings.xml has no host"); - } - - //wagonManager.addProxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), - // proxy.getPassword(), proxy.getNonProxyHosts()); - } - - for (Server server : (List) settings.getServers()) { - //wagonManager.addAuthenticationInfo(server.getId(), server.getUsername(), server.getPassword(), - // server.getPrivateKey(), server.getPassphrase()); - - //wagonManager.addPermissionInfo(server.getId(), server.getFilePermissions(), - // server.getDirectoryPermissions()); - - if (server.getConfiguration() != null) { - //wagonManager.addConfiguration(server.getId(), (Xpp3Dom) server.getConfiguration()); - } - } - - for (Mirror mirror : (List) settings.getMirrors()) { - //wagonManager.addMirror(mirror.getId(), mirror.getMirrorOf(), mirror.getUrl()); - } - } - - public T lookup(Class clazz) throws ComponentLookupException { - return plexusContainer.lookup(clazz); - } - - public T lookup(Class clazz, String hint) throws ComponentLookupException { - return plexusContainer.lookup(clazz, hint); - } - - public Object lookup(String role, String hint) throws ComponentLookupException { - return plexusContainer.lookup(role, hint); - } - - public Object lookup(String role) throws ComponentLookupException { - return plexusContainer.lookup(role); - } - - private Map propertiesToMap(Properties properties) { - if (properties == null || properties.isEmpty()) { - return new HashMap(0); - } - Map result = new HashMap(properties.size()); - for (Map.Entry entry : properties.entrySet()) { - result.put((String) entry.getKey(), (String) entry.getValue()); - } - return result; - } - - public MavenRequest getMavenRequest() { - return mavenRequest; - } -} diff --git a/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractionException.java b/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractionException.java index 06b207e..6be4696 100644 --- a/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractionException.java +++ b/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractionException.java @@ -1,6 +1,7 @@ package fitnesse.wikitext.widgets; -public class MavenClasspathExtractionException extends RuntimeException { +@SuppressWarnings("serial") +public class MavenClasspathExtractionException extends Exception { public MavenClasspathExtractionException(String message, Throwable cause) { super(message, cause); diff --git a/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractor.java b/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractor.java index f465a4d..e032f37 100644 --- a/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractor.java +++ b/src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractor.java @@ -1,54 +1,78 @@ package fitnesse.wikitext.widgets; -import hudson.maven.MavenEmbedderException; -import hudson.maven.MavenRequest; +import org.apache.maven.DefaultMaven; +import org.apache.maven.Maven; import org.apache.maven.artifact.DependencyResolutionRequiredException; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.project.ProjectBuildingResult; +import org.apache.maven.cli.MavenCli; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequestPopulationException; +import org.apache.maven.execution.MavenExecutionRequestPopulator; +import org.apache.maven.project.*; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.building.*; +import org.codehaus.plexus.*; +import org.codehaus.plexus.classworlds.ClassWorld; +import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.logging.console.ConsoleLoggerManager; +import org.codehaus.plexus.util.Os; +import org.sonatype.aether.RepositorySystemSession; import java.io.File; import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; /** - * Utiity class to extract classpath elements from Maven projects. Heavily based on code copied from Jenkin's Maven + * Utility class to extract classpath elements from Maven projects. Heavily based on code copied from Jenkin's Maven * support. */ public class MavenClasspathExtractor { public final static String DEFAULT_SCOPE = "test"; + public final static String MAVEN_USER_SETTINGS = "maven.user.settings"; + + private final Logger logger = new ConsoleLoggerManager().getLoggerForComponent("maven-classpath-plugin"); - private File userSettingsFile; - private File globalSettingsFile; + private PlexusContainer plexusContainer; + + // Ensure M2_HOME variable is handled in a way similar to the mvn executable (script). To the extend possible. + static { + String m2Home = System.getenv().get("M2_HOME"); + if (m2Home != null && System.getProperty("maven.home") == null) { + System.setProperty("maven.home", m2Home); + } + } - public List extractClasspathEntries(File pomFile) { + public MavenClasspathExtractor() throws PlexusContainerException { + plexusContainer = buildPlexusContainer(getClass().getClassLoader(), null); + } + + public List extractClasspathEntries(File pomFile) throws MavenClasspathExtractionException { return extractClasspathEntries(pomFile, DEFAULT_SCOPE); } public List extractClasspathEntries(File pomFile, String scope) throws MavenClasspathExtractionException { try { - MavenRequest mavenRequest = mavenConfiguration(); - mavenRequest.setResolveDependencies(true); - mavenRequest.setBaseDirectory(pomFile.getParent()); - mavenRequest.setPom(pomFile.getAbsolutePath()); - - DependencyResolvingMavenEmbedder dependencyResolvingMavenEmbedder = - new DependencyResolvingMavenEmbedder(getClass().getClassLoader(), mavenRequest); + MavenExecutionRequest mavenExecutionRequest = mavenConfiguration(); + mavenExecutionRequest.setBaseDirectory(pomFile.getParentFile()); + mavenExecutionRequest.setPom(pomFile); - ProjectBuildingResult projectBuildingResult = dependencyResolvingMavenEmbedder.buildProject(pomFile); + ProjectBuildingResult projectBuildingResult = buildProject(pomFile, mavenExecutionRequest); + return getClasspathForScope(projectBuildingResult, scope); - } catch (MavenEmbedderException mee) { - throw new MavenClasspathExtractionException(mee); - } catch (ComponentLookupException cle) { - throw new MavenClasspathExtractionException(cle); + } catch (ComponentLookupException e) { + throw new MavenClasspathExtractionException(e); } catch (DependencyResolutionRequiredException e) { throw new MavenClasspathExtractionException(e); } catch (ProjectBuildingException e) { throw new MavenClasspathExtractionException(e); - } + } } private List getClasspathForScope( @@ -65,40 +89,135 @@ private List getClasspathForScope( } - // protected for test purposes - protected MavenRequest mavenConfiguration() throws MavenEmbedderException, ComponentLookupException { - MavenRequest mavenRequest = new MavenRequest(); + protected MavenExecutionRequest mavenConfiguration() throws MavenClasspathExtractionException { + MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); - if (userSettingsFile != null && userSettingsFile.exists()) { - mavenRequest.setUserSettingsFile(userSettingsFile.getAbsolutePath()); - } - if (globalSettingsFile != null && globalSettingsFile.exists()) { - mavenRequest.setGlobalSettingsFile(globalSettingsFile.getAbsolutePath()); + try { + MavenExecutionRequestPopulator executionRequestPopulator = lookup(MavenExecutionRequestPopulator.class); + MavenExecutionRequestPopulator populator = lookup(MavenExecutionRequestPopulator.class); + + mavenExecutionRequest.setInteractiveMode(false); + + mavenExecutionRequest.setSystemProperties(System.getProperties()); + mavenExecutionRequest.getSystemProperties().putAll(getEnvVars()); + + Settings settings = getSettings(mavenExecutionRequest); + + executionRequestPopulator.populateFromSettings(mavenExecutionRequest, settings); + populator.populateDefaults(mavenExecutionRequest); + + logger.debug( "Local repository " + mavenExecutionRequest.getLocalRepository()); + + } catch (ComponentLookupException e) { + throw new MavenClasspathExtractionException(e); + } catch (SettingsBuildingException e) { + throw new MavenClasspathExtractionException(e); + } catch (MavenExecutionRequestPopulationException e) { + throw new MavenClasspathExtractionException(e); + } + return mavenExecutionRequest; + } + + private Settings getSettings(MavenExecutionRequest mavenExecutionRequest) + throws ComponentLookupException, SettingsBuildingException { + + SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest(); + + // TODO: should be configurable by system properties? + final File globalSettingsFile = MavenCli.DEFAULT_GLOBAL_SETTINGS_FILE; + + //Allows users to set "maven.user.settings" if they use a custom settings.xml + File userSettingsFile; + if(System.getProperty(MAVEN_USER_SETTINGS) != null) { + userSettingsFile = new File(System.getProperty(MAVEN_USER_SETTINGS)); + } else { + userSettingsFile = MavenCli.DEFAULT_USER_SETTINGS_FILE; + } + + settingsRequest.setGlobalSettingsFile(globalSettingsFile); + settingsRequest.setUserSettingsFile(userSettingsFile); + + settingsRequest.setSystemProperties(mavenExecutionRequest + .getSystemProperties()); + settingsRequest.setUserProperties(mavenExecutionRequest + .getUserProperties()); + + logger.debug("Reading global settings from " + settingsRequest.getGlobalSettingsFile()); + logger.debug("Reading user settings from " + settingsRequest.getUserSettingsFile()); + + SettingsBuilder settingsBuilder = lookup(SettingsBuilder.class); + + SettingsBuildingResult settingsResult = settingsBuilder + .build(settingsRequest); + + if (!settingsResult.getProblems().isEmpty()) { + logger.warn(""); + logger.warn("Some problems were encountered while building the effective settings"); + + for (SettingsProblem problem : settingsResult.getProblems()) { + logger.warn(problem.getMessage() + " @ " + + problem.getLocation()); + } + + logger.warn(""); + } + + return settingsResult.getEffectiveSettings(); + } + + private Properties getEnvVars() { + Properties envVars = new Properties(); + boolean caseSensitive = !Os.isFamily(Os.FAMILY_WINDOWS); + for (Map.Entry entry : System.getenv().entrySet()) { + String key = "env." + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH)); + envVars.setProperty(key, entry.getValue()); } + return envVars; + } + + + public ProjectBuildingResult buildProject(File mavenProject, MavenExecutionRequest mavenExecutionRequest) throws ProjectBuildingException, ComponentLookupException { + ProjectBuilder projectBuilder = lookup(ProjectBuilder.class); + ProjectBuildingRequest projectBuildingRequest = mavenExecutionRequest.getProjectBuildingRequest(); + + RepositorySystemSession repositorySystemSession = buildRepositorySystemSession(mavenExecutionRequest); + + projectBuildingRequest.setRepositorySession(repositorySystemSession); + + projectBuildingRequest.setProcessPlugins(false); - DependencyResolvingMavenEmbedder mavenEmbedder = new DependencyResolvingMavenEmbedder(MavenClasspathExtractor.class.getClassLoader(), mavenRequest); - mavenEmbedder.getMavenRequest().setLocalRepositoryPath(getLocalRepository(mavenEmbedder.getSettings().getLocalRepository())); + projectBuildingRequest.setResolveDependencies(true); - return mavenEmbedder.getMavenRequest(); + return projectBuilder.build(mavenProject, projectBuildingRequest); + } + + public T lookup(Class clazz) throws ComponentLookupException { + return plexusContainer.lookup(clazz); } - /* - * can be overridden for test purposes. - */ - protected String getLocalRepository(String localRepository) { - return localRepository; + private RepositorySystemSession buildRepositorySystemSession(MavenExecutionRequest mavenExecutionRequest) throws ComponentLookupException { + DefaultMaven defaultMaven = (DefaultMaven) lookup(Maven.class); + return defaultMaven.newRepositorySession(mavenExecutionRequest); } + public static PlexusContainer buildPlexusContainer(ClassLoader mavenClassLoader, ClassLoader parent) throws PlexusContainerException { + DefaultContainerConfiguration conf = new DefaultContainerConfiguration(); - // protected for test purposes - protected void setMavenUserSettingsFile(File userSettingsFile) { - this.userSettingsFile = userSettingsFile; + ClassWorld classWorld = new ClassWorld(); + + ClassRealm classRealm = new ClassRealm( classWorld, "maven", mavenClassLoader ); + classRealm.setParentRealm( new ClassRealm( classWorld, "maven-parent", + parent == null ? Thread.currentThread().getContextClassLoader() + : parent ) ); + conf.setRealm( classRealm ); + + return buildPlexusContainer(conf); } - // protected for test purposes - protected void setMavenGlobalSettingsFile(File globalSettingsFile) { - this.globalSettingsFile = globalSettingsFile; + private static PlexusContainer buildPlexusContainer(ContainerConfiguration containerConfiguration ) throws PlexusContainerException { + DefaultPlexusContainer plexusContainer = new DefaultPlexusContainer( containerConfiguration ); + return plexusContainer; } } diff --git a/src/main/java/fitnesse/wikitext/widgets/MavenClasspathSymbolType.java b/src/main/java/fitnesse/wikitext/widgets/MavenClasspathSymbolType.java index 2fa6803..b494d4b 100644 --- a/src/main/java/fitnesse/wikitext/widgets/MavenClasspathSymbolType.java +++ b/src/main/java/fitnesse/wikitext/widgets/MavenClasspathSymbolType.java @@ -1,23 +1,43 @@ package fitnesse.wikitext.widgets; -import fitnesse.html.HtmlUtil; -import fitnesse.wikitext.parser.*; -import util.Maybe; +import fitnesse.wikitext.parser.Matcher; +import fitnesse.wikitext.parser.Maybe; +import fitnesse.wikitext.parser.Parser; +import fitnesse.wikitext.parser.Path; +import fitnesse.wikitext.parser.PathsProvider; +import fitnesse.wikitext.parser.Rule; +import fitnesse.wikitext.parser.Symbol; +import fitnesse.wikitext.parser.SymbolProvider; +import fitnesse.wikitext.parser.SymbolType; +import fitnesse.wikitext.parser.Translation; +import fitnesse.wikitext.parser.Translator; +import org.codehaus.plexus.PlexusContainerException; import java.io.File; import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** - * FitNesse SymbolType implementation which enables Maven classpath integration for FitNesse. + * FitNesse SymbolType implementation. Enables Maven classpath integration for FitNesse. */ public class MavenClasspathSymbolType extends SymbolType implements Rule, Translation, PathsProvider { + /** System property to disable this Symbol (if given value true). */ + public static final String DISABLE_KEY = "fitnesse.wikitext.widgets.MavenClasspathSymbolType.Disable"; private MavenClasspathExtractor mavenClasspathExtractor; - public MavenClasspathSymbolType() { + private final Map> classpathCache = new HashMap>(); + + public MavenClasspathSymbolType() throws PlexusContainerException { super("MavenClasspathSymbolType"); - this.mavenClasspathExtractor = new MavenClasspathExtractor(); + + String disablePropertyValue = System.getProperty(DISABLE_KEY); + if (!"true".equalsIgnoreCase(disablePropertyValue)) { + this.mavenClasspathExtractor = new MavenClasspathExtractor(); + } wikiMatcher(new Matcher().startLineOrCell().string("!pomFile")); @@ -27,40 +47,53 @@ public MavenClasspathSymbolType() { @Override public String toTarget(Translator translator, Symbol symbol) { - List classpathElements = getClasspathElements(symbol); - - String classpathForRender = ""; - for (String element : classpathElements) { - classpathForRender += HtmlUtil.metaText("classpath: " + element) + HtmlUtil.BRtag; - + List classpathElements = null; + ParsedSymbol parsedSymbol = getParsedSymbol(translator, symbol); + StringBuilder classpathForRender = new StringBuilder("

Maven classpath [file: ") + .append(parsedSymbol.getPomFile()) + .append(", scope: ") + .append(parsedSymbol.getScope()) + .append("]:

") + .append("
    "); + try { + classpathElements = getClasspathElements(parsedSymbol); + for (String element : classpathElements) { + classpathForRender.append("
  • ").append(element).append("
  • "); + } + } catch (MavenClasspathExtractionException e) { + classpathForRender.append("
  • Unable to parse POM file: ") + .append(e.getMessage()).append("
  • "); } - return classpathForRender; - } + classpathForRender.append("
"); + return classpathForRender.toString(); - private List getClasspathElements(Symbol symbol) { - String pomFile = symbol.childAt(0).getContent(); - String scope = MavenClasspathExtractor.DEFAULT_SCOPE; + } - if (pomFile.contains("@")) { - String[] s = pomFile.split("@"); - pomFile = s[0]; - scope = s[1]; - } + @SuppressWarnings("unchecked") + private List getClasspathElements(final ParsedSymbol parsedSymbol) throws MavenClasspathExtractionException { + String symbol = parsedSymbol.symbol; + if(classpathCache.containsKey(symbol)) { + return classpathCache.get(symbol); + } else { + List classpath = Collections.emptyList(); + if (mavenClasspathExtractor != null) { + classpath = mavenClasspathExtractor.extractClasspathEntries(parsedSymbol.getPomFile(), parsedSymbol.getScope()); + } + classpathCache.put(symbol, classpath); + return classpath; + } + } - return mavenClasspathExtractor.extractClasspathEntries(new File(pomFile), scope); - } + private ParsedSymbol getParsedSymbol(Translator translator, Symbol symbol) { + return new ParsedSymbol(translator.translate(symbol.childAt(0))); + } @Override - public Maybe parse(Symbol symbol, Parser parser) { - Symbol next = parser.moveNext(1); - - if (!next.isType(SymbolType.Whitespace)) return Symbol.nothing; - - symbol.add(parser.moveNext(1).getContent()); + public Maybe parse(Symbol current, Parser parser) { + if (!parser.isMoveNext(SymbolType.Whitespace)) return Symbol.nothing; - - return new Maybe(symbol); + return new Maybe(current.add(parser.parseToEnds(0, SymbolProvider.pathRuleProvider, new SymbolType[] {SymbolType.Newline}))); } @Override @@ -68,7 +101,6 @@ public boolean matchesFor(SymbolType symbolType) { return symbolType instanceof Path || super.matchesFor(symbolType); } - /** * Exposed for testing */ @@ -78,7 +110,65 @@ protected void setMavenClasspathExtractor(MavenClasspathExtractor mavenClasspath @Override public Collection providePaths(Translator translator, Symbol symbol) { - return getClasspathElements(symbol); + try { + return getClasspathElements(getParsedSymbol(translator, symbol)); + } catch (MavenClasspathExtractionException e) { + return Collections.EMPTY_LIST; + } + } + + /** + * Turn the pom+scope key into a comparable object, using the pom's last modified timestamp as + * cache key. + */ + static class ParsedSymbol { + private String symbol; + private File pomFile; + private String scope; + private long lastModified; + + public ParsedSymbol(String symbol) { + super(); + this.symbol = symbol; + parseSymbol(); + } + + private void parseSymbol() { + if (symbol.contains("@")) { + String[] s = symbol.split("@"); + pomFile = new File(s[0]); + scope = s[1]; + } else { + pomFile = new File(symbol); + scope = MavenClasspathExtractor.DEFAULT_SCOPE; + } + + lastModified = pomFile.lastModified(); + } + + public File getPomFile() { + return pomFile; + } + + public String getScope() { + return scope; + } + + /* hashCode() and equals() are optimized for used in the cache */ + + @Override + public int hashCode() { + return symbol.hashCode() + (int) lastModified; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof ParsedSymbol) { + ParsedSymbol ps = (ParsedSymbol) obj; + return symbol.equals(ps.symbol) && lastModified == ps.lastModified; + } + return false; + } } } diff --git a/src/test/java/fitnesse/wikitext/widgets/MavenClasspathExtractorTest.java b/src/test/java/fitnesse/wikitext/widgets/MavenClasspathExtractorTest.java index 24f8a8e..70ee850 100644 --- a/src/test/java/fitnesse/wikitext/widgets/MavenClasspathExtractorTest.java +++ b/src/test/java/fitnesse/wikitext/widgets/MavenClasspathExtractorTest.java @@ -1,5 +1,6 @@ package fitnesse.wikitext.widgets; +import org.codehaus.plexus.PlexusContainerException; import org.junit.Before; import org.junit.Test; @@ -15,7 +16,7 @@ public class MavenClasspathExtractorTest { private File pomFile; @Before - public void setUp() { + public void setUp() throws PlexusContainerException { pomFile = new File(MavenClasspathExtractor.class .getClassLoader().getResource("MavenClasspathWidget/pom.xml").getFile()); @@ -23,7 +24,7 @@ public void setUp() { } @Test - public void extractedClasspathIncludesTestScopeDependencies() { + public void extractedClasspathIncludesTestScopeDependencies() throws MavenClasspathExtractionException { List classpathEntries = mavenClasspathExtractor.extractClasspathEntries(pomFile); StringBuffer sb = new StringBuffer(); for (String cpEntry : classpathEntries) { @@ -37,8 +38,9 @@ public void extractedClasspathIncludesTestScopeDependencies() { } @Test(expected = MavenClasspathExtractionException.class) - public void failsOnNonExistingPom() { + public void failsOnNonExistingPom() throws MavenClasspathExtractionException { mavenClasspathExtractor.extractClasspathEntries(new File("test-pom.xml")); } + } diff --git a/src/test/java/fitnesse/wikitext/widgets/MavenClasspathSymbolTypeTest.java b/src/test/java/fitnesse/wikitext/widgets/MavenClasspathSymbolTypeTest.java index af9b032..79da3e2 100644 --- a/src/test/java/fitnesse/wikitext/widgets/MavenClasspathSymbolTypeTest.java +++ b/src/test/java/fitnesse/wikitext/widgets/MavenClasspathSymbolTypeTest.java @@ -1,12 +1,12 @@ package fitnesse.wikitext.widgets; -import fitnesse.wikitext.parser.Parser; -import fitnesse.wikitext.parser.Symbol; -import fitnesse.wikitext.parser.SymbolType; +import fitnesse.wiki.PageData; +import fitnesse.wiki.WikiPage; +import fitnesse.wiki.fs.InMemoryPage; +import fitnesse.wikitext.parser.*; import org.junit.Before; import org.junit.Test; -import util.Maybe; import java.io.File; import java.util.Arrays; @@ -19,12 +19,13 @@ public class MavenClasspathSymbolTypeTest { private MavenClasspathSymbolType mavenClasspathSymbolType; private MavenClasspathExtractor mavenClasspathExtractor; private Symbol symbol; - private Parser parser; + private WikiPage wikiPage; @Before public void setUp() throws Exception { + System.clearProperty(MavenClasspathSymbolType.DISABLE_KEY); symbol = mock(Symbol.class); - parser = mock(Parser.class); + wikiPage = InMemoryPage.makeRoot("RooT"); mavenClasspathExtractor = mock(MavenClasspathExtractor.class); mavenClasspathSymbolType = new MavenClasspathSymbolType(); @@ -32,44 +33,71 @@ public void setUp() throws Exception { } @Test - public void canParseAProperDirective() { - when(parser.moveNext(1)) - .thenReturn(new Symbol(SymbolType.Whitespace)) - .thenReturn(new Symbol(SymbolType.Text, "thePomFile")); + public void translatesToClasspathEntries() throws MavenClasspathExtractionException { + Symbol child = mock(Symbol.class); + Translator translator = mock(Translator.class); + + when(symbol.childAt(0)).thenReturn(child); + when(translator.translate(child)).thenReturn("thePomFile"); - Maybe result = mavenClasspathSymbolType.parse(symbol, parser); - assertNotNull(result); - assertNotSame(Symbol.nothing, result); + when(mavenClasspathExtractor.extractClasspathEntries(any(File.class), isA(String.class))) + .thenReturn(Arrays.asList("test1", "test2")); - verify(symbol).add("thePomFile"); + assertEquals("

Maven classpath [file: thePomFile, scope: test]:

  • test1
  • test2
" + , mavenClasspathSymbolType.toTarget(translator, symbol)); } @Test - public void translatesToClasspathEntries() { + public void translatesToJavaClasspath() throws MavenClasspathExtractionException { Symbol child = mock(Symbol.class); + Translator translator = mock(Translator.class); when(symbol.childAt(0)).thenReturn(child); - when(child.getContent()).thenReturn("thePomFile"); + when(translator.translate(child)).thenReturn("thePomFile"); when(mavenClasspathExtractor.extractClasspathEntries(any(File.class), isA(String.class))) .thenReturn(Arrays.asList("test1", "test2")); - assertEquals("classpath: test1
classpath: test2
" - , mavenClasspathSymbolType.toTarget(null, symbol)); + assertArrayEquals(new Object[]{"test1", "test2"}, mavenClasspathSymbolType.providePaths(translator, symbol).toArray()); + } + + @Test + public void loadPomXml() throws Exception { + configureMavenClasspathSymbolType(); + PageData pageData = wikiPage.getData(); + pageData.setContent("!pomFile pom.xml\n"); + wikiPage.commit(pageData); + String html = wikiPage.getHtml(); + assertTrue(html, html.startsWith("

Maven classpath [file: pom.xml, scope: test]:

  • ")); + } + + @Test + public void loadPomXmlFromVariable() throws Exception { + configureMavenClasspathSymbolType(); + PageData pageData = wikiPage.getData(); + pageData.setContent("!define POM_XML {pom.xml}\n" + + "!pomFile ${POM_XML}\n"); + wikiPage.commit(pageData); + String html = wikiPage.getHtml(); + assertTrue(html, html.contains("

    Maven classpath [file: pom.xml, scope: test]:

    • ")); } @Test - public void translatesToJavaClasspath() { + public void canBeDisabled() throws Exception { + System.setProperty(MavenClasspathSymbolType.DISABLE_KEY, "TRUE"); + mavenClasspathSymbolType = new MavenClasspathSymbolType(); + Symbol child = mock(Symbol.class); + Translator translator = mock(Translator.class); when(symbol.childAt(0)).thenReturn(child); - when(child.getContent()).thenReturn("thePomFile"); + when(translator.translate(child)).thenReturn("thePomFile"); - when(mavenClasspathExtractor.extractClasspathEntries(any(File.class), isA(String.class))) - .thenReturn(Arrays.asList("test1", "test2")); - - assertArrayEquals(new Object[] { "test1", "test2" }, mavenClasspathSymbolType.providePaths(null, symbol).toArray()); + assertEquals("

      Maven classpath [file: thePomFile, scope: test]:

        " + , mavenClasspathSymbolType.toTarget(translator, symbol)); } - + private void configureMavenClasspathSymbolType() throws Exception { + SymbolProvider.wikiParsingProvider.add(new MavenClasspathSymbolType()); + } }