diff options
Diffstat (limited to 'sonar-java-api/src')
4 files changed, 229 insertions, 0 deletions
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 index 00000000000..4a2b9d7086b --- /dev/null +++ b/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java @@ -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 index 00000000000..a1925501b5d --- /dev/null +++ b/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java @@ -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 index 00000000000..576d7d28c41 --- /dev/null +++ b/sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java @@ -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 index 00000000000..f98853e5306 --- /dev/null +++ b/sonar-java-api/src/test/java/org/sonar/java/api/JavaUtilsTest.java @@ -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")); + } +} |