diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 21:12:37 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 21:12:37 +0000 |
commit | 7dc48cc48d76d70d6acbf16232fb21ee78ec8549 (patch) | |
tree | 646e99416f0f81bc54cd8f54e88b196a57ffee70 /sonar-plugin-api | |
parent | 790ba5c2d84ff5a07e3d42eb1caf85673535e8b8 (diff) | |
download | sonarqube-7dc48cc48d76d70d6acbf16232fb21ee78ec8549.tar.gz sonarqube-7dc48cc48d76d70d6acbf16232fb21ee78ec8549.zip |
SONAR-1749 MavenPlugin api, offer way to build nested xml-elements
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenPlugin.java | 45 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/batch/maven/MavenPluginTest.java | 46 |
2 files changed, 76 insertions, 15 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenPlugin.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenPlugin.java index ce035ccb45f..f6a7670fd70 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenPlugin.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenPlugin.java @@ -21,7 +21,6 @@ package org.sonar.api.batch.maven; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.maven.model.Dependency; import org.apache.maven.model.Plugin; import org.apache.maven.model.ReportPlugin; import org.apache.maven.project.MavenProject; @@ -103,7 +102,8 @@ public class MavenPlugin { /** * Gets a list of parameters of the plugin from a param key * - * @param key the param key + * @param key param key with option-index snippet: e.g. item[0], item[1]. If no index snippet is passed, then + * 0 is default (index <=> index[0]) * @return an array of parameters if any, an empty array otherwise */ public String[] getParameters(String key) { @@ -162,7 +162,8 @@ public class MavenPlugin { /** * Adds a parameter to the maven plugin * - * @param key the param key + * @param key the param key with option-index snippet: e.g. item[0], item[1]. If no index snippet is passed, then + * 0 is default (index <=> index[0]) * @param value the param value * @return this */ @@ -179,18 +180,35 @@ public class MavenPlugin { } private static Xpp3Dom getOrCreateChild(Xpp3Dom node, String key) { - Xpp3Dom child = node.getChild(key); - if (child == null) { - child = new Xpp3Dom(key); + int childIndex = getIndex(key); + + if (node.getChildren(removeIndexSnippet(key)).length <= childIndex) { + Xpp3Dom child = new Xpp3Dom(removeIndexSnippet(key)); node.addChild(child); + return child; + } + return node.getChildren(removeIndexSnippet(key))[childIndex]; + + } + + private static int getIndex(String key) { + //parsing index-syntax (e.g. item[1]) + if (key.matches(".*?\\[\\d+\\]")) { + return Integer.parseInt(StringUtils.substringBetween(key, "[", "]")); } - return child; + // for down-compatibility of api we fallback to default 0 + return 0; + } + + private static String removeIndexSnippet(String key) { + return StringUtils.substringBefore(key, "["); } /** * Remove a parameter from the maven plugin based on its key * - * @param key the param key + * @param key param key with option-index snippet: e.g. item[0], item[1]. If no index snippet is passed, then + * 0 is default (index <=> index[0]) */ public void removeParameter(String key) { Xpp3Dom node = findNodeWith(key); @@ -204,7 +222,12 @@ public class MavenPlugin { String[] keyParts = key.split("/"); Xpp3Dom node = configuration; for (String keyPart : keyParts) { - node = node.getChild(keyPart); + + if(node.getChildren(removeIndexSnippet(keyPart)).length <= getIndex(keyPart)) { + return null; + } + + node = node.getChildren(removeIndexSnippet(keyPart))[getIndex(keyPart)]; if (node == null) { return null; } @@ -224,7 +247,7 @@ public class MavenPlugin { } /** - * @return whether the maven plugin has got configuration + * @return whether the maven plugin has got configuration */ public boolean hasConfiguration() { return configuration.getChildCount()>0; @@ -270,7 +293,7 @@ public class MavenPlugin { * Returns a plugin from a pom based on its group id and artifact id * <p/> * <p>It searches in the build section, then the reporting section and finally the pluginManagement section</p> - * + * * @param pom the project pom * @param groupId the plugin group id * @param artifactId the plugin artifact id diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/maven/MavenPluginTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/maven/MavenPluginTest.java index 66ea58e4946..92a12cd7704 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/maven/MavenPluginTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/maven/MavenPluginTest.java @@ -21,14 +21,16 @@ package org.sonar.api.batch.maven; import org.apache.maven.model.Plugin; import org.apache.maven.project.MavenProject; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; import org.sonar.api.test.MavenTestUtils; import java.util.Collection; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; + public class MavenPluginTest { private MavenPlugin fakePlugin; @@ -53,14 +55,12 @@ public class MavenPluginTest { @Test public void shouldWriteAndReadSimpleConfiguration() { fakePlugin.setParameter("abc", "test"); - assertThat(fakePlugin.getParameter("abc"), is("test")); } @Test public void shouldWriteAndReadComplexConfiguration() { fakePlugin.setParameter("abc/def/ghi", "test"); - assertThat(fakePlugin.getParameter("abc/def/ghi"), is("test")); } @@ -151,6 +151,44 @@ public class MavenPluginTest { assertThat(fakePlugin.toString(), fakePlugin.getParameters("excludes/exclude"), is(new String[]{"abc", "def"})); } + @Test + public void defaultParameterIndexIsZero() { + fakePlugin.addParameter("items/item/entry", "value1"); + fakePlugin.addParameter("items/item/entry", "value2"); + + assertThat(fakePlugin.toString(), fakePlugin.getParameters("items/item/entry"), is(new String[]{"value1", "value2"})); + assertThat(fakePlugin.toString(), fakePlugin.getParameters("items/item[0]/entry"), is(new String[]{"value1", "value2"})); + } + + + @Test + public void addIndexedParameters() { + fakePlugin.addParameter("items/item[0]/entry", "value1"); + fakePlugin.addParameter("items/item[1]/entry", "value2"); + + assertThat(fakePlugin.getParameter("items/item[0]/entry"), is("value1")); + assertThat(fakePlugin.getParameters("items/item[0]/entry"), is(new String[]{"value1"})); + + assertThat(fakePlugin.getParameter("items/item[1]/entry"), is("value2")); + assertThat(fakePlugin.getParameters("items/item[1]/entry"), is(new String[]{"value2"})); + + //ensure that indexes aren't serialized to real configuration + assertThat(fakePlugin.getPlugin().getConfiguration().toString(), not(containsString("item[0]"))); + assertThat(fakePlugin.getPlugin().getConfiguration().toString(), not(containsString("item[1]"))); + } + + @Test + public void removeIndexedParameter(){ + fakePlugin.addParameter("items/item[0]/entry", "value1"); + fakePlugin.addParameter("items/item[1]/entry", "value2"); + + fakePlugin.removeParameter("items/item[1]"); + fakePlugin.removeParameter("items/notExists"); + + assertThat(fakePlugin.getParameter("items/item[0]/entry"), notNullValue()); + assertThat(fakePlugin.getParameter("items/item[1]/entry"), nullValue()); + assertThat(fakePlugin.getParameter("items/notExists"), nullValue()); + } @Test public void registerNewPlugin() { |