]> source.dussan.org Git - sonarqube.git/commitdiff
Add the method ResourceUtils#isPersistable(Resource)
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 26 Jan 2011 17:25:23 +0000 (18:25 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 26 Jan 2011 17:25:23 +0000 (18:25 +0100)
sonar-batch/src/main/java/org/sonar/batch/index/DefaultPersistenceManager.java
sonar-batch/src/test/java/org/sonar/batch/index/DefaultPersistenceManagerTest.java [deleted file]
sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceUtils.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceUtilsTest.java

index ea019c5ee4c1698d90faaa2e8a4efce787f18f1d..f8df73fd11977934c4f0760d8db11244e406a4bb 100644 (file)
  */
 package org.sonar.batch.index;
 
-import org.apache.commons.lang.StringUtils;
 import org.sonar.api.batch.Event;
 import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.design.Dependency;
 import org.sonar.api.measures.Measure;
-import org.sonar.api.resources.*;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectLink;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.ResourceUtils;
 
 import java.util.List;
 
@@ -66,7 +68,7 @@ public final class DefaultPersistenceManager implements PersistenceManager {
   }
 
   public Snapshot saveResource(Project project, Resource resource, Resource parent) {
-    if (isPersistable(resource)) {
+    if (ResourceUtils.isPersistable(resource)) {
       return resourcePersister.saveResource(project, resource, parent);
     }
     return null;
@@ -77,13 +79,13 @@ public final class DefaultPersistenceManager implements PersistenceManager {
   }
 
   public void saveMeasure(Resource resource, Measure measure) {
-    if (isPersistable(resource)) {
+    if (ResourceUtils.isPersistable(resource)) {
       measurePersister.saveMeasure(resource, measure);
     }
   }
 
   public void saveDependency(Project project, Dependency dependency, Dependency parentDependency) {
-    if (isPersistable(dependency.getFrom()) && isPersistable(dependency.getTo())) {
+    if (ResourceUtils.isPersistable(dependency.getFrom()) && ResourceUtils.isPersistable(dependency.getTo())) {
       dependencyPersister.saveDependency(project, dependency, parentDependency);
     }
   }
@@ -105,17 +107,8 @@ public final class DefaultPersistenceManager implements PersistenceManager {
   }
 
   public void saveEvent(Resource resource, Event event) {
-    if (isPersistable(resource)) {
+    if (ResourceUtils.isPersistable(resource)) {
       eventPersister.saveEvent(resource, event);
     }
   }
-
-  static boolean isPersistable(Resource resource) {
-    if (resource != null) {
-      return resource instanceof File || resource instanceof Directory || resource instanceof Library || resource instanceof Project ||
-          // for deprecated resources
-          StringUtils.equals(Scopes.PROJECT, resource.getScope()) || StringUtils.equals(Scopes.DIRECTORY, resource.getScope()) || StringUtils.equals(Scopes.FILE, resource.getScope());
-    }
-    return false;
-  }
 }
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultPersistenceManagerTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultPersistenceManagerTest.java
deleted file mode 100644 (file)
index 48458f6..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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.batch.index;
-
-import org.junit.Test;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Library;
-import org.sonar.api.resources.Project;
-import org.sonar.java.api.JavaClass;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-public class DefaultPersistenceManagerTest {
-  
-  @Test
-  public void shouldPersistResoucesWithScopeHigherThanFile() {
-    assertThat(DefaultPersistenceManager.isPersistable(new File("Foo.java")), is(true));
-    assertThat(DefaultPersistenceManager.isPersistable(new Directory("bar/Foo.java")), is(true));
-    assertThat(DefaultPersistenceManager.isPersistable(new Project("foo")), is(true));
-    assertThat(DefaultPersistenceManager.isPersistable(new Library("foo", "1.2")), is(true));
-  }
-
-  @Test
-  public void shouldNotPersistResoucesWithScopeLowerThanFile() {
-    assertThat(DefaultPersistenceManager.isPersistable(JavaClass.createRef("com.foo.Bar")), is(false));
-  }
-}
index 71d92857f1a1b7ab95faf02cc6c1922dbb4d8a7b..eb6728f6a6e15b4d106878a2ea378703fa4caa87 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.api.resources;
 
+import org.apache.commons.lang.StringUtils;
+
 /**
  * @since 1.10
  */
@@ -126,4 +128,14 @@ public final class ResourceUtils {
   public static boolean isLibrary(Resource resource) {
     return Resource.QUALIFIER_LIB.equals(resource.getQualifier());
   }
+
+  /**
+   * @param resource not nullable
+   * @return true if this type of resource is persisted in database
+   * @since 2.6
+   */
+  public static boolean isPersistable(Resource resource) {
+    return StringUtils.equals(Scopes.PROJECT, resource.getScope()) || StringUtils.equals(Scopes.DIRECTORY, resource.getScope()) ||
+        StringUtils.equals(Scopes.FILE, resource.getScope());
+  }
 }
index 747b42ed90ee29b6ee7da56278faf2098597ce51..319b29759548211a7c1eed503ef3199ff12b58c5 100644 (file)
  */
 package org.sonar.api.resources;
 
+import org.junit.Test;
+
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
-import org.junit.Test;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class ResourceUtilsTest {
 
@@ -63,4 +66,23 @@ public class ResourceUtilsTest {
     assertThat(ResourceUtils.isRootProject(pack), is(false));
     assertThat(ResourceUtils.isUnitTestClass(pack), is(false));
   }
+
+  @Test
+  public void shouldBePersistable() {
+    assertThat(ResourceUtils.isPersistable(new File("Foo.java")), is(true));
+    assertThat(ResourceUtils.isPersistable(new Directory("bar/Foo.java")), is(true));
+    assertThat(ResourceUtils.isPersistable(new Project("foo")), is(true));
+    assertThat(ResourceUtils.isPersistable(new Library("foo", "1.2")), is(true));
+  }
+
+  @Test
+  public void shouldNotBePersistable() {
+    Resource javaClass = mock(Resource.class);
+    when(javaClass.getScope()).thenReturn(Scopes.TYPE);
+    Resource javaMethod = mock(Resource.class);
+    when(javaMethod.getScope()).thenReturn(Scopes.BLOCK_UNIT);
+
+    assertThat(ResourceUtils.isPersistable(javaClass), is(false));
+    assertThat(ResourceUtils.isPersistable(javaMethod), is(false));
+  }
 }