AnalysisConfigurationGatherer { void gatherConfig(Revapi revapi, AnalysisContext.Builder ctxBld) throws MojoExecutionException { if (analysisConfigurationFiles != null && analysisConfigurationFiles.length > 0) { for (Object pathOrConfigFile : analysisConfigurationFiles) { ConfigurationFile configFile; if (pathOrConfigFile instanceof String) { configFile = new ConfigurationFile(); configFile.setPath((String) pathOrConfigFile); } else { configFile = (ConfigurationFile) pathOrConfigFile; } String path = configFile.getPath(); String resource = configFile.getResource(); if (path == null && resource == null) { throw new MojoExecutionException( "Either 'path' or 'resource' has to be specified in a configurationFile definition."); } else if (path != null && resource != null) { throw new MojoExecutionException( "Either 'path' or 'resource' has to be specified in a configurationFile definition but" + " not both."); } String readErrorMessage = "Error while processing the configuration file on " + (path == null ? "classpath " + resource : "path " + path); Supplier<Iterator<InputStream>> configFileContents; if (path != null) { File f = new File(path); if (!f.isAbsolute()) { f = new File(relativePathBaseDir, path); } if (!f.isFile() || !f.canRead()) { String message = "Could not locate analysis configuration file '" + f.getAbsolutePath() + "'."; if (failOnMissingConfigurationFiles) { throw new MojoExecutionException(message); } else { log.debug(message); continue; } } final File ff = f; configFileContents = () -> { try { return Collections.<InputStream>singletonList(new FileInputStream(ff)).iterator(); } catch (FileNotFoundException e) { throw new IllegalArgumentException("Failed to read the configuration file '" + ff.getAbsolutePath() + "'.", e); } }; } else { configFileContents = () -> { try { return Collections.list(getClass().getClassLoader().getResources(resource)) .stream() .map(url -> { try { return url.openStream(); } catch (IOException e) { throw new IllegalArgumentException( "Failed to read the classpath resource '" + url + "'."); } }).iterator(); } catch (IOException e) { throw new IllegalArgumentException( "Failed to locate classpath resources on path '" + resource + "'."); } }; } Iterator<InputStream> it = configFileContents.get(); List<Integer> nonJsonIndexes = new ArrayList<>(4); int idx = 0; while (it.hasNext()) { ModelNode config; try (InputStream in = it.next()) { config = readJson(in); } catch (IllegalArgumentException | IOException e) { throw new MojoExecutionException(readErrorMessage, e.getCause()); } if (config == null) { nonJsonIndexes.add(idx); continue; } expandVariables(config); mergeJsonConfigFile(ctxBld, configFile, config); idx++; } if (!nonJsonIndexes.isEmpty()) { idx = 0; it = configFileContents.get(); while (it.hasNext()) { try (Reader rdr = new InputStreamReader(it.next())) { if (nonJsonIndexes.contains(idx)) { mergeXmlConfigFile(revapi, ctxBld, configFile, rdr); } } catch (IllegalArgumentException | IOException | XmlPullParserException e) { throw new MojoExecutionException(readErrorMessage, e.getCause()); } idx++; } } } } if (analysisConfiguration != null) { String text = analysisConfiguration.getValue(); if (text == null || text.isEmpty()) { convertNewStyleConfigFromXml(ctxBld, revapi); } else { ctxBld.mergeConfiguration(expandVariables(ModelNode.fromJSONString(JSONUtil.stripComments(text)))); } } } AnalysisConfigurationGatherer(PlexusConfiguration analysisConfiguration,
            Object[] analysisConfigurationFiles, boolean failOnMissingConfigurationFiles, boolean expandProperties,
            PropertyValueInterpolator interpolator, File relativePathBaseDir, Log log);   }
@Test public void testReadingConfigurationFromJSON() throws Exception { PlexusConfiguration analysisConfiguration = new DefaultPlexusConfiguration("analysisConfiguration"); String json = "[{\"extension\": \"test\", \"configuration\": \"yes\"}]"; analysisConfiguration.setValue(json); AnalysisConfigurationGatherer gatherer = new AnalysisConfigurationGatherer(analysisConfiguration, new Object[0], false, false, new PropertyValueInterpolator(new Properties()), null, null); Revapi revapi = Revapi.builder() .withFilters(TestExtension.class) .build(); AnalysisContext.Builder ctxBld = AnalysisContext.builder(); gatherer.gatherConfig(revapi, ctxBld); AnalysisContext ctx = ctxBld.build(); ModelNode cfg = ctx.getConfiguration(); ModelNode expected = ModelNode.fromJSONString(json); assertEquals(expected, cfg); }
@Test public void testReadingConfigurationFromXML() throws Exception { PlexusConfiguration analysisConfiguration = new DefaultPlexusConfiguration("analysisConfiguration"); PlexusConfiguration testConfig = new DefaultPlexusConfiguration("test"); testConfig.setValue("yes"); analysisConfiguration.addChild(testConfig); AnalysisConfigurationGatherer gatherer = new AnalysisConfigurationGatherer(analysisConfiguration, new Object[0], false, false, new PropertyValueInterpolator(new Properties()), null, null); Revapi revapi = Revapi.builder() .withFilters(TestExtension.class) .build(); AnalysisContext.Builder ctxBld = AnalysisContext.builder(); gatherer.gatherConfig(revapi, ctxBld); AnalysisContext ctx = ctxBld.build(); ModelNode cfg = ctx.getConfiguration(); String json = "[{\"extension\": \"test\", \"configuration\": \"yes\"}]"; ModelNode expected = ModelNode.fromJSONString(json); assertEquals(expected, cfg); }
@Test public void testReadingConfigurationFromFile() throws Exception { InputStream data = getClass().getResourceAsStream("/test-configuration-file.json"); assertNotNull("Could not read the test configuration file.", data); File f = File.createTempFile("revapi-maven-plugin-test", null); Files.copy(data, f.toPath(), StandardCopyOption.REPLACE_EXISTING); try { AnalysisConfigurationGatherer gatherer = new AnalysisConfigurationGatherer(null, new Object[] { f.getAbsolutePath() }, false, false, new PropertyValueInterpolator(new Properties()), null, null); Revapi revapi = Revapi.builder() .withFilters(TestExtension.class) .build(); AnalysisContext.Builder ctxBld = AnalysisContext.builder(); gatherer.gatherConfig(revapi, ctxBld); AnalysisContext ctx = ctxBld.build(); ModelNode cfg = ctx.getConfiguration(); String json = "[{\"extension\": \"test\", \"configuration\": \"yes\"}]"; ModelNode expected = ModelNode.fromJSONString(json); assertEquals(expected, cfg); } finally { f.delete(); } }
@Test public void testReadingConfigurationFromClassPath() throws Exception { ConfigurationFile configurationFile = new ConfigurationFile(); configurationFile.setResource("test-configuration-file.xml"); AnalysisConfigurationGatherer gatherer = new AnalysisConfigurationGatherer(null, new Object[] { configurationFile }, false, false, new PropertyValueInterpolator(new Properties()), null, null); Revapi revapi = Revapi.builder() .withFilters(TestExtension.class) .build(); AnalysisContext.Builder ctxBld = AnalysisContext.builder(); gatherer.gatherConfig(revapi, ctxBld); AnalysisContext ctx = ctxBld.build(); ModelNode cfg = ctx.getConfiguration(); String json = "[{\"extension\": \"test\", \"configuration\": \"yes\"}]"; ModelNode expected = ModelNode.fromJSONString(json); assertEquals(expected, cfg); }
@Test public void testPropertyExpansionInJSON() throws Exception { ConfigurationFile configurationFile = new ConfigurationFile(); configurationFile.setResource("prop-expansion-config.json"); Properties props = new Properties(); props.put("prop", "yes"); AnalysisConfigurationGatherer gatherer = new AnalysisConfigurationGatherer(null, new Object[] { configurationFile }, false, true, new PropertyValueInterpolator(props), null, null); Revapi revapi = Revapi.builder() .withFilters(TestExtension.class) .build(); AnalysisContext.Builder ctxBld = AnalysisContext.builder(); gatherer.gatherConfig(revapi, ctxBld); AnalysisContext ctx = ctxBld.build(); ModelNode cfg = ctx.getConfiguration(); String json = "[{\"extension\": \"test\", \"configuration\": \"yes\"}]"; ModelNode expected = ModelNode.fromJSONString(json); assertEquals(expected, cfg); }
@Test public void testPropertyExpansionInXml() throws Exception { ConfigurationFile configurationFile = new ConfigurationFile(); configurationFile.setResource("prop-expansion-config.xml"); Properties props = new Properties(); props.put("prop", "yes"); AnalysisConfigurationGatherer gatherer = new AnalysisConfigurationGatherer(null, new Object[] { configurationFile }, false, true, new PropertyValueInterpolator(props), null, null); Revapi revapi = Revapi.builder() .withFilters(TestExtension.class) .build(); AnalysisContext.Builder ctxBld = AnalysisContext.builder(); gatherer.gatherConfig(revapi, ctxBld); AnalysisContext ctx = ctxBld.build(); ModelNode cfg = ctx.getConfiguration(); String json = "[{\"extension\": \"test\", \"configuration\": \"yes\"}]"; ModelNode expected = ModelNode.fromJSONString(json); assertEquals(expected, cfg); }