Browse Source

SONAR-1749 MavenPlugin api, offer way to build nested xml-elements

tags/2.6
simonbrandhof 13 years ago
parent
commit
7dc48cc48d

+ 34
- 11
sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenPlugin.java View File

@@ -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

+ 42
- 4
sonar-plugin-api/src/test/java/org/sonar/api/batch/maven/MavenPluginTest.java View File

@@ -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() {

Loading…
Cancel
Save