Search in sources :

Example 51 with MavenSession

use of org.apache.maven.execution.MavenSession in project maven-plugins by apache.

the class JavadocReportTest method testHelpfile.

/**
 * Method to test the <code>&lt;helpfile/&gt;</code> parameter.
 *
 * @throws Exception if any
 */
public void testHelpfile() throws Exception {
    File testPom = new File(unit, "helpfile-test/pom.xml");
    JavadocReport mojo = lookupMojo(testPom);
    assertNotNull(mojo);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "session", session);
    File apidocs = new File(getBasedir(), "target/test/unit/helpfile-test/target/site/apidocs");
    File helpfile = new File(apidocs, "help-doc.html");
    File options = new File(apidocs, "options");
    // helpfile by default
    mojo.execute();
    String content = readFile(helpfile);
    assertTrue(content.contains("<!-- Generated by javadoc"));
    String optionsContent = readFile(options);
    assertFalse(optionsContent.contains("-helpfile"));
    // helpfile defined in a javadoc plugin dependency
    setVariableValueToObject(mojo, "helpfile", "com/mycompany/app/javadoc/helpfile/help-doc.html");
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    content = readFile(helpfile);
    assertTrue(content.contains("<!--  Help file from artefact -->"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-helpfile"));
    File help = new File(apidocs, "help-doc.html");
    assertTrue(optionsContent.contains("'" + help.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
    // helpfile defined as a project resource
    setVariableValueToObject(mojo, "helpfile", "com/mycompany/app/javadoc/helpfile2/help-doc.html");
    mojo.execute();
    content = readFile(helpfile);
    assertTrue(content.contains("<!--  Help file from file -->"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-helpfile"));
    help = new File(unit, "helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html");
    assertTrue(optionsContent.contains("'" + help.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
    // helpfile defined as file
    help = new File(unit, "helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html");
    setVariableValueToObject(mojo, "helpfile", help.getAbsolutePath());
    mojo.execute();
    content = readFile(helpfile);
    assertTrue(content.contains("<!--  Help file from file -->"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-helpfile"));
    assertTrue(optionsContent.contains("'" + help.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) LegacySupport(org.apache.maven.plugin.LegacySupport) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 52 with MavenSession

use of org.apache.maven.execution.MavenSession in project maven-plugins by apache.

the class JavadocReportTest method testDoclets.

/**
 * Method to test the doclet artifact configuration
 *
 * @throws Exception if any
 */
public void testDoclets() throws Exception {
    // ----------------------------------------------------------------------
    // doclet-test: check if the file generated by UmlGraph exists and if
    // doclet path contains the UmlGraph artifact
    // ----------------------------------------------------------------------
    File testPom = new File(unit, "doclet-test/doclet-test-plugin-config.xml");
    JavadocReport mojo = lookupMojo(testPom);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    File generatedFile = new File(getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    File optionsFile = new File(mojo.getOutputDirectory(), "options");
    assertTrue(optionsFile.exists());
    String options = readFile(optionsFile);
    assertTrue(options.contains("/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar"));
    // ----------------------------------------------------------------------
    // doclet-path: check if the file generated by UmlGraph exists and if
    // doclet path contains the twice UmlGraph artifacts
    // ----------------------------------------------------------------------
    testPom = new File(unit, "doclet-path-test/doclet-path-test-plugin-config.xml");
    mojo = lookupMojo(testPom);
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    generatedFile = new File(getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    optionsFile = new File(mojo.getOutputDirectory(), "options");
    assertTrue(optionsFile.exists());
    options = readFile(optionsFile);
    assertTrue(options.contains("/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar"));
    assertTrue(options.contains("/target/local-repo/umlgraph/UMLGraph-bis/2.1/UMLGraph-bis-2.1.jar"));
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) LegacySupport(org.apache.maven.plugin.LegacySupport) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 53 with MavenSession

use of org.apache.maven.execution.MavenSession in project maven-plugins by apache.

the class JavadocReportTest method testProxy.

/**
 * Method to test proxy support in the javadoc
 *
 * @throws Exception if any
 */
public void testProxy() throws Exception {
    Settings settings = new Settings();
    Proxy proxy = new Proxy();
    // dummy proxy
    proxy.setActive(true);
    proxy.setHost("127.0.0.1");
    proxy.setPort(80);
    proxy.setProtocol("http");
    proxy.setUsername("toto");
    proxy.setPassword("toto");
    proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
    settings.addProxy(proxy);
    File testPom = new File(getBasedir(), "src/test/resources/unit/proxy-test/proxy-test-plugin-config.xml");
    JavadocReport mojo = lookupMojo(testPom);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "settings", settings);
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    File commandLine = new File(getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/javadoc." + (SystemUtils.IS_OS_WINDOWS ? "bat" : "sh"));
    assertTrue(FileUtils.fileExists(commandLine.getAbsolutePath()));
    String readed = readFile(commandLine);
    assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
    assertTrue(readed.contains("-J-Dhttp.proxyHost=127.0.0.1"));
    assertTrue(readed.contains("-J-Dhttp.proxyPort=80"));
    assertTrue(readed.contains("-J-Dhttp.proxyUser=\\\"toto\\\""));
    assertTrue(readed.contains("-J-Dhttp.proxyPassword=\\\"toto\\\""));
    assertTrue(readed.contains("-J-Dhttp.nonProxyHosts=\\\"www.google.com|*.somewhere.com\\\""));
    File options = new File(getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/options");
    assertTrue(FileUtils.fileExists(options.getAbsolutePath()));
    String optionsContent = readFile(options);
    // NO -link expected
    assertFalse(optionsContent.contains("-link"));
    // real proxy
    ProxyServer proxyServer = null;
    AuthAsyncProxyServlet proxyServlet;
    try {
        proxyServlet = new AuthAsyncProxyServlet();
        proxyServer = new ProxyServer(proxyServlet);
        proxyServer.start();
        settings = new Settings();
        proxy = new Proxy();
        proxy.setActive(true);
        proxy.setHost(proxyServer.getHostName());
        proxy.setPort(proxyServer.getPort());
        proxy.setProtocol("http");
        settings.addProxy(proxy);
        mojo = lookupMojo(testPom);
        setVariableValueToObject(mojo, "settings", settings);
        setVariableValueToObject(mojo, "session", session);
        mojo.execute();
        readed = readFile(commandLine);
        assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
        assertTrue(readed.contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName()));
        assertTrue(readed.contains("-J-Dhttp.proxyPort=" + proxyServer.getPort()));
        optionsContent = readFile(options);
    // -link expected
    // TODO: This got disabled for now!
    // This test fails since the last commit but I actually think it only ever worked by accident.
    // It did rely on a commons-logging-1.0.4.pom which got resolved by a test which did run previously.
    // But after updating to commons-logging.1.1.1 there is no pre-resolved artifact available in
    // target/local-repo anymore, thus the javadoc link info cannot get built and the test fails
    // I'll for now just disable this line of code, because the test as far as I can see _never_
    // did go upstream. The remoteRepository list used is always empty!.
    // 
    // assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
    } finally {
        if (proxyServer != null) {
            proxyServer.stop();
        }
    }
    // auth proxy
    Map<String, String> authentications = new HashMap<>();
    authentications.put("foo", "bar");
    try {
        proxyServlet = new AuthAsyncProxyServlet(authentications);
        proxyServer = new ProxyServer(proxyServlet);
        proxyServer.start();
        settings = new Settings();
        proxy = new Proxy();
        proxy.setActive(true);
        proxy.setHost(proxyServer.getHostName());
        proxy.setPort(proxyServer.getPort());
        proxy.setProtocol("http");
        proxy.setUsername("foo");
        proxy.setPassword("bar");
        settings.addProxy(proxy);
        mojo = lookupMojo(testPom);
        setVariableValueToObject(mojo, "settings", settings);
        setVariableValueToObject(mojo, "session", session);
        mojo.execute();
        readed = readFile(commandLine);
        assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
        assertTrue(readed.contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName()));
        assertTrue(readed.contains("-J-Dhttp.proxyPort=" + proxyServer.getPort()));
        assertTrue(readed.contains("-J-Dhttp.proxyUser=\\\"foo\\\""));
        assertTrue(readed.contains("-J-Dhttp.proxyPassword=\\\"bar\\\""));
        optionsContent = readFile(options);
    // -link expected
    // see comment above (line 829)
    // assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
    } finally {
        if (proxyServer != null) {
            proxyServer.stop();
        }
    }
}
Also used : LegacySupport(org.apache.maven.plugin.LegacySupport) HashMap(java.util.HashMap) AuthAsyncProxyServlet(org.apache.maven.plugins.javadoc.ProxyServer.AuthAsyncProxyServlet) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) Proxy(org.apache.maven.settings.Proxy) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) File(java.io.File) Settings(org.apache.maven.settings.Settings)

Example 54 with MavenSession

use of org.apache.maven.execution.MavenSession in project antlr4 by antlr.

the class Antlr4MojoTest method processWhenDependencyRemoved.

@Test
public void processWhenDependencyRemoved() throws Exception {
    Path baseDir = resources.getBasedir("dependencyRemoved").toPath();
    Path antlrDir = baseDir.resolve("src/main/antlr4");
    Path baseGrammar = antlrDir.resolve("imports/HelloBase.g4");
    MavenProject project = maven.readMavenProject(baseDir.toFile());
    MavenSession session = maven.newMavenSession(project);
    MojoExecution exec = maven.newMojoExecution("antlr4");
    maven.executeMojo(session, project, exec);
    try (Change temp = Change.of(baseGrammar)) {
        // if the base grammar no longer exists, processing must be performed
        Files.delete(baseGrammar);
        thrown.expect(MojoExecutionException.class);
        thrown.expectMessage("ANTLR 4 caught 1 build errors.");
        maven.executeMojo(session, project, exec);
    }
}
Also used : Path(java.nio.file.Path) MavenSession(org.apache.maven.execution.MavenSession) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) Test(org.junit.Test)

Example 55 with MavenSession

use of org.apache.maven.execution.MavenSession in project antlr4 by antlr.

the class Antlr4MojoTest method importsCustomLayout.

@Test
public void importsCustomLayout() throws Exception {
    Path baseDir = resources.getBasedir("importsCustom").toPath();
    Path antlrDir = baseDir.resolve("src/main/antlr4");
    Path generatedSources = baseDir.resolve("src/main/java");
    Path genTestLexer = generatedSources.resolve("foo/TestLexer.java");
    Path genTestParser = generatedSources.resolve("foo/TestParser.java");
    Path genHello = generatedSources.resolve("foo/HelloParser.java");
    Path baseGrammar = antlrDir.resolve("imports/TestBaseLexer.g4");
    Path lexerGrammar = antlrDir.resolve("TestLexer.g4");
    Path parserGrammar = antlrDir.resolve("TestParser.g4");
    Xpp3Dom outputDirectory = TestMavenRuntime.newParameter("outputDirectory", "src/main/java/foo");
    Xpp3Dom arguments = new Xpp3Dom("arguments");
    arguments.addChild(TestMavenRuntime.newParameter("argument", "-package"));
    arguments.addChild(TestMavenRuntime.newParameter("argument", "foo"));
    MavenProject project = maven.readMavenProject(baseDir.toFile());
    MavenSession session = maven.newMavenSession(project);
    MojoExecution exec = maven.newMojoExecution("antlr4", outputDirectory, arguments);
    // //////////////////////////////////////////////////////////////////////
    // 1st - all grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    assertFalse(Files.exists(genHello));
    assertFalse(Files.exists(genTestParser));
    assertFalse(Files.exists(genTestLexer));
    maven.executeMojo(session, project, exec);
    assertTrue(Files.exists(genHello));
    assertTrue(Files.exists(genTestParser));
    assertTrue(Files.exists(genTestLexer));
    // //////////////////////////////////////////////////////////////////////
    // 2nd - nothing has been modified, no grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assertTrue(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertTrue(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    }
    // modify the grammar to make checksum comparison detect a change
    try (Change change = Change.of(baseGrammar, "DOT: '.' ;")) {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    }
    // modify the grammar to make checksum comparison detect a change
    try (Change change = Change.of(lexerGrammar, "fragment DOT : '.';")) {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    }
    // modify the grammar to make checksum comparison detect a change
    try (Change change = Change.of(parserGrammar, " t : WS* ;")) {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assertTrue(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    }
}
Also used : Path(java.nio.file.Path) MavenSession(org.apache.maven.execution.MavenSession) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) Test(org.junit.Test)

Aggregations

MavenSession (org.apache.maven.execution.MavenSession)118 File (java.io.File)65 MavenProject (org.apache.maven.project.MavenProject)47 SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)30 DefaultRepositorySystemSession (org.sonatype.aether.util.DefaultRepositorySystemSession)23 Test (org.junit.Test)21 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)20 LegacySupport (org.apache.maven.plugin.LegacySupport)20 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)18 Properties (java.util.Properties)17 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)16 MojoExecution (org.apache.maven.plugin.MojoExecution)13 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)13 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)12 Artifact (org.apache.maven.artifact.Artifact)10 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)9 ManipulationSession (org.commonjava.maven.ext.core.ManipulationSession)8 MavenExecutionResult (org.apache.maven.execution.MavenExecutionResult)7 Settings (org.apache.maven.settings.Settings)7 ArrayList (java.util.ArrayList)6