Search in sources :

Example 11 with MojoExecution

use of org.apache.maven.plugin.MojoExecution in project meecrowave by apache.

the class MeecrowaveRunMojoTest method run.

@Test
public void run() throws Exception {
    final File moduleBase = jarLocation(MeecrowaveRunMojoTest.class).getParentFile().getParentFile();
    final File basedir = new File(moduleBase, "src/test/resources/" + getClass().getSimpleName());
    final File pom = new File(basedir, "pom.xml");
    final MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request.setBaseDirectory(basedir);
    final ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
    final DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(repositorySession, new LocalRepository(new File(moduleBase, "target/fake"), "")));
    configuration.setRepositorySession(repositorySession);
    final MavenProject project = mojo.lookup(ProjectBuilder.class).build(pom, configuration).getProject();
    final MavenSession session = mojo.newMavenSession(project);
    final int port;
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        port = serverSocket.getLocalPort();
    }
    final MojoExecution execution = mojo.newMojoExecution("run");
    execution.getConfiguration().addChild(new Xpp3Dom("httpPort") {

        {
            setValue(Integer.toString(port));
        }
    });
    final InputStream in = System.in;
    final CountDownLatch latch = new CountDownLatch(1);
    System.setIn(new InputStream() {

        // just to not return nothing
        private int val = 2;

        @Override
        public int read() throws IOException {
            try {
                latch.await();
            } catch (final InterruptedException e) {
                Thread.interrupted();
                fail(e.getMessage());
            }
            return val--;
        }
    });
    final Thread runner = new Thread() {

        @Override
        public void run() {
            try {
                mojo.executeMojo(session, project, execution);
            } catch (final Exception e) {
                fail(e.getMessage());
            }
        }
    };
    try {
        runner.start();
        for (int i = 0; i < 120; i++) {
            try {
                assertEquals("simple", IOUtils.toString(new URL("http://localhost:" + port + "/api/test")));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("first_name"));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("last_name"));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("firstname"));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("null"));
                latch.countDown();
                break;
            } catch (final Exception | AssertionError e) {
                Thread.sleep(500);
            }
        }
    } finally {
        runner.join(TimeUnit.MINUTES.toMillis(1));
        System.setIn(in);
        if (runner.isAlive()) {
            runner.interrupt();
            fail("Runner didn't terminate properly");
        }
    }
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) InputStream(java.io.InputStream) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) LocalRepository(org.eclipse.aether.repository.LocalRepository) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) URL(java.net.URL) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) SimpleLocalRepositoryManagerFactory(org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory) File(java.io.File) Test(org.junit.Test)

Aggregations

MojoExecution (org.apache.maven.plugin.MojoExecution)11 MavenProject (org.apache.maven.project.MavenProject)7 MavenSession (org.apache.maven.execution.MavenSession)5 Test (org.junit.Test)5 Path (java.nio.file.Path)4 MojoDescriptor (org.apache.maven.plugin.descriptor.MojoDescriptor)3 IOException (java.io.IOException)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ServerSocket (java.net.ServerSocket)1 URL (java.net.URL)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)1 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)1 MavenExecutionPlan (org.apache.maven.lifecycle.MavenExecutionPlan)1 LifecycleExecutionPlanCalculator (org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator)1