]> source.dussan.org Git - sonarqube.git/commitdiff
create the module sonar-java-api dedicated to Java support
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 19 Jan 2011 21:36:38 +0000 (22:36 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 19 Jan 2011 21:49:50 +0000 (22:49 +0100)
plugins/sonar-squid-java-plugin/pom.xml
pom.xml
sonar-java-api/pom.xml [new file with mode: 0644]
sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java [new file with mode: 0644]
sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java [new file with mode: 0644]
sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java [new file with mode: 0644]
sonar-java-api/src/test/java/org/sonar/java/api/JavaUtilsTest.java [new file with mode: 0644]

index c5054b67cb19e9b357b2bbfa1d35f211d89ba719..70cb1d91ee276096125a0d9e313a7404b170eebc 100644 (file)
   <description>Squid analyzer for Java.</description>
 
   <dependencies>
+    <dependency>
+      <groupId>org.codehaus.sonar</groupId>
+      <artifactId>sonar-java-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
     <dependency>
       <groupId>org.codehaus.sonar</groupId>
       <artifactId>sonar-plugin-api</artifactId>
diff --git a/pom.xml b/pom.xml
index d7dbbdf2d680c3bd56c2dd5fe3dc1c93de33bce8..f582f85f35df95af288ddee0cc348b971fc8c9d9 100644 (file)
--- a/pom.xml
+++ b/pom.xml
     <!-- public artifacts to be deployed to central repository -->
     <module>archetypes/sonar-basic-plugin</module>
     <module>archetypes/sonar-gwt-plugin</module>
-    <module>sonar-core</module>
-    <module>sonar-deprecated</module>
     <module>sonar-batch</module>
-    <module>sonar-maven-plugin</module>
-    <module>sonar-maven3-plugin</module>
     <module>sonar-channel</module>
     <module>sonar-check-api</module>
     <module>sonar-colorizer</module>
+    <module>sonar-core</module>
+    <module>sonar-deprecated</module>
     <module>sonar-duplications</module>
     <module>sonar-graph</module>
     <module>sonar-gwt-api</module>
+    <module>sonar-java-api</module>
+    <module>sonar-maven-plugin</module>
+    <module>sonar-maven3-plugin</module>
     <module>sonar-plugin-api</module>
-    <module>sonar-testing-harness</module>
     <module>sonar-squid</module>
+    <module>sonar-testing-harness</module>
     <module>sonar-ws-client</module>
     <module>plugins/sonar-core-gwt</module>
     <module>plugins/sonar-core-plugin</module>
           </archive>
         </configuration>
       </plugin>
-      <!--      <plugin>
-        <groupId>com.atlassian.maven.plugins</groupId>
-        <artifactId>maven-clover2-plugin</artifactId>
-        <version>2.5.1</version>
-        <configuration>
-          <outputDirectory>${project.reporting.outputDirectory}/${project.version}/clover</outputDirectory>
-          <generateHistorical>false</generateHistorical>
-          <generateXml>false</generateXml>
-          <license><![CDATA[opmOXvoSTwRurJtdPUPnnqXUoPxgewBKvhvFsQghghMpHN
-mi2KCHwAl26X>gPZJcsdXAvR2KEZT5o>pDZqs>VNq7CbJD
-OomqxtmMpmQQorqpnOqWVrrrQONpmONrNRUVuWTtSWSoop
-qqnmqmUUnooqqonommmmmUUnooqqonommmmmUUJlkXoUUn
-mmmm
-]]></license>
-        </configuration>
-      </plugin>-->
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-javadoc-plugin</artifactId>
@@ -342,8 +327,6 @@ mmmm
           <reportOutputDirectory>${project.reporting.outputDirectory}/${project.version}/apidocs</reportOutputDirectory>
         </configuration>
       </plugin>
-
-
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jxr-plugin</artifactId>
@@ -353,7 +336,6 @@ mmmm
           <javadocDir>${project.reporting.outputDirectory}/${project.version}/apidocs</javadocDir>
         </configuration>
       </plugin>
-
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-source-plugin</artifactId>
@@ -413,6 +395,11 @@ mmmm
         <artifactId>sonar-gwt-api</artifactId>
         <version>${project.version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.codehaus.sonar</groupId>
+        <artifactId>sonar-java-api</artifactId>
+        <version>${project.version}</version>
+      </dependency>
       <dependency>
         <groupId>org.codehaus.sonar</groupId>
         <artifactId>sonar-plugin-api</artifactId>
diff --git a/sonar-java-api/pom.xml b/sonar-java-api/pom.xml
new file mode 100644 (file)
index 0000000..26577b6
--- /dev/null
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.codehaus.sonar</groupId>
+    <artifactId>sonar</artifactId>
+    <version>2.6-SNAPSHOT</version>
+  </parent>
+  <artifactId>sonar-java-api</artifactId>
+  <name>Sonar :: Java API</name>
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.sonar</groupId>
+      <artifactId>sonar-plugin-api</artifactId>
+    </dependency>
+
+    <!-- unit tests -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-all</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java b/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java
new file mode 100644 (file)
index 0000000..4a2b9d7
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.java.api;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.resources.*;
+
+/**
+ * @since 2.6
+ */
+public final class JavaClass extends Resource {
+
+  private String name;
+
+  private JavaClass(String name) {
+    this.name = name;
+    setKey(name);
+  }
+
+  public String getPackageName() {
+    return StringUtils.substringBeforeLast(name, JavaUtils.PACKAGE_SEPARATOR);
+  }
+
+  public String getClassName() {
+    return StringUtils.substringAfterLast(name, JavaUtils.PACKAGE_SEPARATOR);
+  }
+
+  @Override
+  public String getName() {
+    return getClassName();
+  }
+
+  @Override
+  public String getLongName() {
+    return name;
+  }
+
+  @Override
+  public String getDescription() {
+    return null;
+  }
+
+  @Override
+  public Language getLanguage() {
+    return Java.INSTANCE;
+  }
+
+  @Override
+  public String getScope() {
+    return ResourceScopes.TYPE;
+  }
+
+  @Override
+  public String getQualifier() {
+    return ResourceQualifiers.CLASS;
+  }
+
+  @Override
+  public Resource getParent() {
+    return null;
+  }
+
+  @Override
+  public boolean matchFilePattern(String antPattern) {
+    return false;
+  }
+
+  public static JavaClass create(String name) {
+    return new JavaClass(name);
+  }
+
+  public static JavaClass create(String packageName, String className) {
+    if (StringUtils.isBlank(packageName)) {
+      return new JavaClass(className);
+    }
+    String name = new StringBuilder().append(packageName).append(JavaUtils.PACKAGE_SEPARATOR).append(className).toString();
+    return new JavaClass(name);
+  }
+}
diff --git a/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java b/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java
new file mode 100644 (file)
index 0000000..a192550
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.java.api;
+
+import org.apache.commons.lang.StringUtils;
+
+public final class JavaUtils {
+
+  public static final String PACKAGE_SEPARATOR = ".";
+
+  private JavaUtils() {
+    // only static methods
+  }
+
+  public static String abbreviatePackage(String packageName) {
+    String[] parts = StringUtils.split(packageName, PACKAGE_SEPARATOR);
+    StringBuilder sb = new StringBuilder();
+    if (parts.length>=1) {
+      sb.append(parts[0]);
+    }
+    for (int index=1 ; index<parts.length ; index++) {
+      sb.append(PACKAGE_SEPARATOR).append(parts[index].charAt(0));
+    }
+    return sb.toString();
+  }
+}
\ No newline at end of file
diff --git a/sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java b/sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java
new file mode 100644 (file)
index 0000000..576d7d2
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.java.api;
+
+import org.junit.Test;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Language;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class JavaClassTest {
+
+  @Test
+  public void shouldCreateJavaClassFromName() {
+    JavaClass javaClass = JavaClass.create("org.foo.Bar");
+    assertThat(javaClass.getClassName(), is("Bar"));
+    assertThat(javaClass.getKey(), is("org.foo.Bar"));
+    assertThat(javaClass.getLanguage(), is((Language)Java.INSTANCE));
+    assertThat(javaClass.getName(), is("Bar"));
+    assertThat(javaClass.getLongName(), is("org.foo.Bar"));
+  }
+
+  @Test
+  public void shouldCreateJavaClassFromPackageAndClassname() {
+    JavaClass javaClass = JavaClass.create("org.foo", "Bar");
+    assertThat(javaClass.getClassName(), is("Bar"));
+    assertThat(javaClass.getKey(), is("org.foo.Bar"));
+    assertThat(javaClass.getLanguage(), is((Language)Java.INSTANCE));
+    assertThat(javaClass.getName(), is("Bar"));
+    assertThat(javaClass.getLongName(), is("org.foo.Bar"));
+  }
+}
diff --git a/sonar-java-api/src/test/java/org/sonar/java/api/JavaUtilsTest.java b/sonar-java-api/src/test/java/org/sonar/java/api/JavaUtilsTest.java
new file mode 100644 (file)
index 0000000..f98853e
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.java.api;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class JavaUtilsTest {
+
+  @Test
+  public void shouldAbbreviatePackage() {
+    assertThat(JavaUtils.abbreviatePackage(""), is(""));
+    assertThat(JavaUtils.abbreviatePackage("com"), is("com"));
+    assertThat(JavaUtils.abbreviatePackage("com.foo"), is("com.f"));
+    assertThat(JavaUtils.abbreviatePackage("com.foo.bar.buz"), is("com.f.b.b"));
+    assertThat(JavaUtils.abbreviatePackage("..."), is(""));
+    assertThat(JavaUtils.abbreviatePackage("com.foo."), is("com.f"));
+    assertThat(JavaUtils.abbreviatePackage("com.foo..bar"), is("com.f.b"));
+  }
+}