OpearPlanMojo extends AbstractCommonMojo implements MojoWithPlanParams { @Override public void execute() throws MojoFailureException { planFile.getParentFile().mkdirs(); final JsonWriterFactory writerFactory = Json.createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true)); ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(planFile), StandardCharsets.UTF_8); JsonWriter writer = writerFactory.createWriter(osw)) { Thread.currentThread().setContextClassLoader(createContainerClassLoader()); OakpalPlan plan = buildPlan(); writer.writeObject(plan.toJson()); } catch (IOException e) { throw new MojoFailureException("Failed to write plan json to file " + planFile.getAbsolutePath(), e); } finally { Thread.currentThread().setContextClassLoader(oldCl); } }  @Override void execute(); @Override PlanBuilderParams getPlanBuilderParams();  }
@Test(expected = MojoFailureException.class) public void testExecute_failDir() throws Exception { final File testOutDir = new File(testOutBaseDir, "testExecute_failDir"); FileUtils.deleteDirectory(testOutDir); OpearPlanMojo mojo = newMojo(); final MavenProject project = mock(MavenProject.class); mojo.project = project; final Build build = mock(Build.class); when(project.getBuild()).thenReturn(build); final File classes = new File(testOutDir, "classes"); when(build.getOutputDirectory()).thenReturn(classes.getPath()); final MavenSession session = mock(MavenSession.class); mojo.session = session; final MavenExecutionRequest executionRequest = mock(MavenExecutionRequest.class); when(session.getRequest()).thenReturn(executionRequest); final RepositorySystem repositorySystem = mock(RepositorySystem.class); mojo.repositorySystem = repositorySystem; mojo.planFile = new File(testOutDir, "opear-plans/plan.json"); mojo.planFile.mkdirs(); mojo.execute(); }
@Test public void testExecute() throws Exception { final File testOutDir = new File(testOutBaseDir, "testExecute"); FileUtils.deleteDirectory(testOutDir); OpearPlanMojo mojo = newMojo(); final MavenProject project = mock(MavenProject.class); mojo.project = project; final Build build = mock(Build.class); when(project.getBuild()).thenReturn(build); final File classes = new File(testOutDir, "classes"); when(build.getOutputDirectory()).thenReturn(classes.getPath()); final MavenSession session = mock(MavenSession.class); mojo.session = session; final MavenExecutionRequest executionRequest = mock(MavenExecutionRequest.class); when(session.getRequest()).thenReturn(executionRequest); final RepositorySystem repositorySystem = mock(RepositorySystem.class); mojo.repositorySystem = repositorySystem; mojo.planParams = new PlanParams(); mojo.planParams.setChecklists(Arrays.asList("checklist1", "checklist2")); mojo.planFile = new File(testOutDir, "opear-plans/plan.json"); mojo.execute(); try (Reader reader = new InputStreamReader(new FileInputStream(mojo.planFile), StandardCharsets.UTF_8); JsonReader jsonReader = Json.createReader(reader)) { final JsonObject readObject = jsonReader.readObject(); assertEquals("expect json", key("checklists", arr("checklist1", "checklist2")).get(), readObject); } }