From cd2781c7d4614adaf898017a5743b28aab79066f Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Wed, 4 May 2011 17:52:38 +0200 Subject: [PATCH] Improve plugin samples --- samples/assembly.xml | 27 +++++ samples/gwt-plugin/pom.xml | 102 ++++++++++++++++++ .../com/mycompany/sonar/gwt/GwtPlugin.java | 19 ++++ .../mycompany/sonar/gwt/page/SamplePage.java | 18 ++++ .../sonar/gwt/page/client/I18nConstants.java | 11 ++ .../gwt/page/client/SamplePagePanel.java | 18 ++++ .../sonar/gwt/viewer/SampleViewer.java | 17 +++ .../gwt/viewer/client/SampleViewerPanel.java | 14 +++ .../sonar/gwt/page/SamplePage.gwt.xml | 13 +++ .../page/client/I18nConstants_fr.properties | 2 + .../mycompany/sonar/gwt/page/public/test.html | 32 ++++++ .../sonar/gwt/viewer/SampleViewer.gwt.xml | 10 ++ samples/pom.xml | 59 ++++++++++ samples/standard-plugin/pom.xml | 57 ++++++++++ .../sonar/standard/SampleFooter.java | 9 ++ .../sonar/standard/SampleMetrics.java | 31 ++++++ .../sonar/standard/SamplePlugin.java | 21 ++++ .../sonar/standard/SampleRubyWidget.java | 38 +++++++ .../sonar/standard/SampleSensor.java | 30 ++++++ .../resources/sample_ruby_widget.html.erb | 40 +++++++ 20 files changed, 568 insertions(+) create mode 100644 samples/assembly.xml create mode 100644 samples/gwt-plugin/pom.xml create mode 100644 samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/GwtPlugin.java create mode 100644 samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/SamplePage.java create mode 100644 samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/I18nConstants.java create mode 100644 samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/SamplePagePanel.java create mode 100644 samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/SampleViewer.java create mode 100644 samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java create mode 100644 samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/SamplePage.gwt.xml create mode 100644 samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/client/I18nConstants_fr.properties create mode 100644 samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/public/test.html create mode 100644 samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/viewer/SampleViewer.gwt.xml create mode 100644 samples/pom.xml create mode 100644 samples/standard-plugin/pom.xml create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleFooter.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleMetrics.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleRubyWidget.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java create mode 100644 samples/standard-plugin/src/main/resources/sample_ruby_widget.html.erb diff --git a/samples/assembly.xml b/samples/assembly.xml new file mode 100644 index 00000000000..014a7dc4036 --- /dev/null +++ b/samples/assembly.xml @@ -0,0 +1,27 @@ + + + distribution + + zip + + true + + + + standard-plugin + standard-plugin + + src/**/* + pom.xml + + + + gwt-plugin + gwt-plugin + + src/**/* + pom.xml + + + + diff --git a/samples/gwt-plugin/pom.xml b/samples/gwt-plugin/pom.xml new file mode 100644 index 00000000000..975327bca1e --- /dev/null +++ b/samples/gwt-plugin/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + + com.mycompany.sonar + sonar-gwt-plugin + sonar-plugin + 2.8-SNAPSHOT + Sonar :: Samples :: GWT plugin + Description of plugin with GWT extensions + + + + ${project.version} + + + + + + org.codehaus.sonar + sonar-plugin-api + ${sonar.buildVersion} + + + org.codehaus.sonar + sonar-gwt-api + ${sonar.buildVersion} + provided + + + com.google.gwt + gwt-user + 2.0.3 + provided + + + com.google.gwt + gwt-incubator + 2.0.1 + provided + + + + + org.codehaus.sonar + sonar-testing-harness + ${sonar.buildVersion} + test + + + junit + junit + 4.8.2 + test + + + + + + + org.codehaus.sonar + sonar-packaging-maven-plugin + 1.0 + true + + com.mycompany.sonar.gwt.GwtPlugin + + + + org.codehaus.mojo + gwt-maven-plugin + 1.2 + + + + + com.mycompany.sonar.gwt.viewer.SampleViewer + com.mycompany.sonar.gwt.page.SamplePage + + ${project.build.directory}/classes + -Xmx512m + + + compile + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + UTF-8 + + + + + diff --git a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/GwtPlugin.java b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/GwtPlugin.java new file mode 100644 index 00000000000..40d78ca7cfa --- /dev/null +++ b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/GwtPlugin.java @@ -0,0 +1,19 @@ +package com.mycompany.sonar.gwt; + +import com.mycompany.sonar.gwt.page.SamplePage; +import com.mycompany.sonar.gwt.viewer.SampleViewer; +import org.sonar.api.SonarPlugin; + +import java.util.Arrays; +import java.util.List; + +public final class GwtPlugin extends SonarPlugin { + + public List getExtensions() { + return Arrays.asList(SampleViewer.class, SamplePage.class); + } + + public String toString() { + return getKey(); + } +} \ No newline at end of file diff --git a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/SamplePage.java b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/SamplePage.java new file mode 100644 index 00000000000..dc2dd743502 --- /dev/null +++ b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/SamplePage.java @@ -0,0 +1,18 @@ +package com.mycompany.sonar.gwt.page; + +import org.sonar.api.web.GwtPage; +import org.sonar.api.web.NavigationSection; +import org.sonar.api.web.UserRole; + +@NavigationSection(NavigationSection.RESOURCE) +@UserRole(UserRole.USER) +public class SamplePage extends GwtPage { + + public String getGwtId() { + return "com.mycompany.sonar.gwt.page.SamplePage"; + } + + public String getTitle() { + return "Sample"; + } +} diff --git a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/I18nConstants.java b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/I18nConstants.java new file mode 100644 index 00000000000..4dde01ebff5 --- /dev/null +++ b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/I18nConstants.java @@ -0,0 +1,11 @@ +package com.mycompany.sonar.gwt.page.client; + +import com.google.gwt.core.client.GWT; + +public interface I18nConstants extends com.google.gwt.i18n.client.Constants { + + static I18nConstants INSTANCE = GWT.create(I18nConstants.class); + + @DefaultStringValue("This is a sample") + String sample(); +} diff --git a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/SamplePagePanel.java b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/SamplePagePanel.java new file mode 100644 index 00000000000..c0d16edbc19 --- /dev/null +++ b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/page/client/SamplePagePanel.java @@ -0,0 +1,18 @@ +package com.mycompany.sonar.gwt.page.client; + +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.VerticalPanel; +import com.google.gwt.user.client.ui.Widget; +import org.sonar.gwt.ui.Page; +import org.sonar.wsclient.services.Resource; + +public class SamplePagePanel extends Page { + + @Override + protected Widget doOnResourceLoad(Resource resource) { + VerticalPanel panel = new VerticalPanel(); + panel.add(new Label(resource.getName(true))); + panel.add(new Label(I18nConstants.INSTANCE.sample())); + return panel; + } +} diff --git a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/SampleViewer.java b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/SampleViewer.java new file mode 100644 index 00000000000..53ca2a45590 --- /dev/null +++ b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/SampleViewer.java @@ -0,0 +1,17 @@ +package com.mycompany.sonar.gwt.viewer; + +import org.sonar.api.web.GwtPage; +import org.sonar.api.web.NavigationSection; +import org.sonar.api.web.UserRole; + +@NavigationSection(NavigationSection.RESOURCE_TAB) +@UserRole(UserRole.USER) +public class SampleViewer extends GwtPage { + public String getTitle() { + return "Sample"; + } + + public String getGwtId() { + return "com.mycompany.sonar.gwt.viewer.SampleViewer"; + } +} \ No newline at end of file diff --git a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java new file mode 100644 index 00000000000..a1188d0c085 --- /dev/null +++ b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java @@ -0,0 +1,14 @@ +package com.mycompany.sonar.gwt.viewer.client; + +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.Widget; +import org.sonar.gwt.ui.Page; +import org.sonar.wsclient.services.Resource; + +public class SampleViewerPanel extends Page { + + @Override + protected Widget doOnResourceLoad(Resource resource) { + return new Label("This is a sample"); + } +} \ No newline at end of file diff --git a/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/SamplePage.gwt.xml b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/SamplePage.gwt.xml new file mode 100644 index 00000000000..8f95c2e2ca1 --- /dev/null +++ b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/SamplePage.gwt.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/client/I18nConstants_fr.properties b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/client/I18nConstants_fr.properties new file mode 100644 index 00000000000..aa92d17b38b --- /dev/null +++ b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/client/I18nConstants_fr.properties @@ -0,0 +1,2 @@ +# This file must use UTF-8 encoding +sample=Ceci est un exemple \ No newline at end of file diff --git a/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/public/test.html b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/public/test.html new file mode 100644 index 00000000000..04371e3acb1 --- /dev/null +++ b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/page/public/test.html @@ -0,0 +1,32 @@ + + + + + + GWT Smaple + + + + + + + + + + + + + + +
+
+ + + + \ No newline at end of file diff --git a/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/viewer/SampleViewer.gwt.xml b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/viewer/SampleViewer.gwt.xml new file mode 100644 index 00000000000..af900e8d883 --- /dev/null +++ b/samples/gwt-plugin/src/main/resources/com/mycompany/sonar/gwt/viewer/SampleViewer.gwt.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/samples/pom.xml b/samples/pom.xml new file mode 100644 index 00000000000..34f0baa72dc --- /dev/null +++ b/samples/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + org.codehaus.sonar + sonar + 2.8-SNAPSHOT + .. + + org.codehaus.sonar.samples + sonar-samples + pom + Sonar :: Samples + + + standard-plugin + gwt-plugin + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + maven-assembly-plugin + + + package + + single + + + false + sonar-samples-${project.version} + + assembly.xml + + + + + 420 + + 493 + 493 + + + + + + + + + \ No newline at end of file diff --git a/samples/standard-plugin/pom.xml b/samples/standard-plugin/pom.xml new file mode 100644 index 00000000000..a075f687271 --- /dev/null +++ b/samples/standard-plugin/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + com.mycompany.sonar + sonar-standard-plugin + sonar-plugin + 2.8-SNAPSHOT + Sonar :: Samples :: Standard plugin + Description of standard plugin + + + + ${project.version} + + + + + org.codehaus.sonar + sonar-plugin-api + ${sonar.buildVersion} + + + + + org.codehaus.sonar + sonar-testing-harness + ${sonar.buildVersion} + test + + + + + + + org.codehaus.sonar + sonar-packaging-maven-plugin + 1.0 + true + + com.mycompany.sonar.standard.SamplePlugin + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + UTF-8 + + + + + diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleFooter.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleFooter.java new file mode 100644 index 00000000000..ab72e402427 --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleFooter.java @@ -0,0 +1,9 @@ +package com.mycompany.sonar.standard; + +import org.sonar.api.web.Footer; + +public final class SampleFooter implements Footer { + public String getHtml() { + return "

Sample footer - This is static HTML

"; + } +} diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleMetrics.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleMetrics.java new file mode 100644 index 00000000000..6d493bcee5e --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleMetrics.java @@ -0,0 +1,31 @@ +package com.mycompany.sonar.standard; + +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Metric; +import org.sonar.api.measures.Metrics; + +import java.util.Arrays; +import java.util.List; + +public final class SampleMetrics implements Metrics { + + public static final Metric MESSAGE = new Metric.Builder("message_key", "Message", Metric.ValueType.STRING) + .setDescription("This is a metric to store a well known message") + .setDirection(Metric.DIRECTION_WORST) + .setQualitative(false) + .setDomain(CoreMetrics.DOMAIN_GENERAL) + .create(); + + public static final Metric RANDOM = new Metric.Builder("random", "Random", Metric.ValueType.FLOAT) + .setDescription("Random value") + .setDirection(Metric.DIRECTION_BETTER) + .setQualitative(false) + .setDomain(CoreMetrics.DOMAIN_GENERAL) + .create(); + + // getMetrics() method is defined in the Metrics interface and is used by + // Sonar to retrieve the list of new Metric + public List getMetrics() { + return Arrays.asList(MESSAGE, RANDOM); + } +} diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java new file mode 100644 index 00000000000..e6ca0cc048a --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java @@ -0,0 +1,21 @@ +package com.mycompany.sonar.standard; + +import org.sonar.api.SonarPlugin; + +import java.util.Arrays; +import java.util.List; + +/** + * This class is the entry point for all extensions + */ +public final class SamplePlugin extends SonarPlugin { + + // This is where you're going to declare all your Sonar extensions + public List getExtensions() { + return Arrays.asList( + SampleMetrics.class, SampleSensor.class, SampleRubyWidget.class, + + // UI + SampleFooter.class); + } +} \ No newline at end of file diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleRubyWidget.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleRubyWidget.java new file mode 100644 index 00000000000..7a97194c534 --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleRubyWidget.java @@ -0,0 +1,38 @@ +package com.mycompany.sonar.standard; + +import org.sonar.api.web.*; + +@UserRole(UserRole.USER) +@Description("Show how to use Ruby Widget API") +@WidgetProperties({ + @WidgetProperty(key="param1", + description="This is a mandatory parameter", + optional=false + ), + @WidgetProperty(key="max", + description="max threshold", + type=WidgetPropertyType.INTEGER, + defaultValue="80" + ), + @WidgetProperty(key="param2", + description="This is an optional parameter" + ), + @WidgetProperty(key="floatprop", + description="test description" + ) +}) +public class SampleRubyWidget extends AbstractRubyTemplate implements RubyRailsWidget { + + public String getId() { + return "sample"; + } + + public String getTitle() { + return "Sample"; + } + + @Override + protected String getTemplatePath() { + return "/sample_ruby_widget.html.erb"; + } +} \ No newline at end of file diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java new file mode 100644 index 00000000000..f37749e286b --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java @@ -0,0 +1,30 @@ +package com.mycompany.sonar.standard; + +import org.apache.commons.lang.math.RandomUtils; +import org.sonar.api.batch.Sensor; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.measures.Measure; +import org.sonar.api.resources.Project; + +public class SampleSensor implements Sensor { + + public boolean shouldExecuteOnProject(Project project) { + // this sensor is executed on any type of project + return true; + } + + public void analyse(Project project, SensorContext sensorContext) { + saveLabelMeasure(sensorContext); + saveNumericMeasure(sensorContext); + } + + private void saveNumericMeasure(SensorContext context) { + // Sonar API includes many libraries like commons-lang and google-collections + context.saveMeasure(SampleMetrics.RANDOM, RandomUtils.nextDouble()); + } + + private void saveLabelMeasure(SensorContext context) { + Measure measure = new Measure(SampleMetrics.MESSAGE, "Hello World!"); + context.saveMeasure(measure); + } +} diff --git a/samples/standard-plugin/src/main/resources/sample_ruby_widget.html.erb b/samples/standard-plugin/src/main/resources/sample_ruby_widget.html.erb new file mode 100644 index 00000000000..c2602c49646 --- /dev/null +++ b/samples/standard-plugin/src/main/resources/sample_ruby_widget.html.erb @@ -0,0 +1,40 @@ +
+

+

+ + <%= format_measure('random') -%> <%= trend_icon(measure('random')) %> + +

+

<%= format_measure('message') -%>

+

+ <%= format_measure('ncloc', :suffix => ' lines', :url => url_for_drilldown('ncloc')) -%> +

+
+
+

Help

+

+ Read configuration : <%= configuration('sonar.jdbc.username') -%> +

+ +

+ Sample of Jfree Eastwood chart :
+ <%= gchart('cht=bhs&chco=FF0000,00FF00,0000FF&chs=200x125&chd=s:FOE,THE,Bar&chxt=x,y&chxl=1:|Dec|Nov|Oct|0:||20K||60K||100K|') -%> +

+

+ Widget properties: + + + + + + + + + + + + + +
max:<%= widget_properties['max'] -%>
param1:<%= widget_properties['param1'] -%>
param2:<%= widget_properties['param2'] -%>
+

+
-- 2.39.5