]> source.dussan.org Git - sonarqube.git/commitdiff
Improve code coverage
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 4 Sep 2013 15:14:05 +0000 (17:14 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 4 Sep 2013 15:14:05 +0000 (17:14 +0200)
sonar-core/src/main/java/org/sonar/core/dryrun/DryRunCache.java
sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java
sonar-core/src/test/java/org/sonar/core/dryrun/DryRunCacheTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
sonar-core/src/test/java/org/sonar/core/properties/PropertyDtoTest.java [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml

index 104234b1f28703937b9d8be468b0011d60adaf89..2916fcb3e49273509a0c5e7d20548bdfad3db5d5 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.core.dryrun;
 import org.apache.commons.io.FileUtils;
 import org.sonar.api.ServerExtension;
 import org.sonar.api.platform.ServerFileSystem;
+import org.sonar.api.utils.SonarException;
 import org.sonar.core.properties.PropertiesDao;
 import org.sonar.core.properties.PropertyDto;
 import org.sonar.core.resource.ResourceDao;
@@ -65,8 +66,11 @@ public class DryRunCache implements ServerExtension {
       return Long.valueOf(dto.getValue());
     }
     // For modules look for root project last modification timestamp
-    Long rootId = resourceDao.getRootProjectByComponentId(projectId).getId();
-    PropertyDto dto = propertiesDao.selectProjectProperty(rootId, SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY);
+    ResourceDto rootProject = resourceDao.getRootProjectByComponentId(projectId);
+    if (rootProject == null) {
+      throw new SonarException("Unable to find root project for project with [id=" + projectId + "]");
+    }
+    PropertyDto dto = propertiesDao.selectProjectProperty(rootProject.getId(), SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY);
     if (dto == null) {
       return 0;
     }
index 35e9e0e898f70e00610df698d683b36366fca60e..af70702632222cd5611049f5314135924d14561e 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.core.properties;
 
+import com.google.common.base.Objects;
+
 public final class PropertyDto {
   private Long id;
   private String key;
@@ -70,4 +72,35 @@ public final class PropertyDto {
     this.userId = userId;
     return this;
   }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (obj == null) {
+      return false;
+    }
+    if (getClass() != obj.getClass()) {
+      return false;
+    }
+    final PropertyDto other = (PropertyDto) obj;
+
+    return Objects.equal(this.key, other.key)
+      && Objects.equal(this.value, other.value)
+      && Objects.equal(this.userId, other.userId)
+      && Objects.equal(this.resourceId, other.resourceId);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hashCode(this.key, this.value, this.resourceId, this.userId);
+  }
+
+  @Override
+  public String toString() {
+    return Objects.toStringHelper(this)
+      .addValue(this.key)
+      .addValue(this.value)
+      .addValue(this.resourceId)
+      .addValue(this.userId)
+      .toString();
+  }
 }
diff --git a/sonar-core/src/test/java/org/sonar/core/dryrun/DryRunCacheTest.java b/sonar-core/src/test/java/org/sonar/core/dryrun/DryRunCacheTest.java
new file mode 100644 (file)
index 0000000..6b9b808
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.core.dryrun;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.platform.ServerFileSystem;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.core.resource.ResourceDao;
+import org.sonar.core.resource.ResourceDto;
+
+import java.io.File;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class DryRunCacheTest {
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  private DryRunCache dryRunCache;
+  private ServerFileSystem serverFileSystem;
+  private PropertiesDao propertiesDao;
+  private ResourceDao resourceDao;
+
+  @Before
+  public void prepare() {
+    serverFileSystem = mock(ServerFileSystem.class);
+    propertiesDao = mock(PropertiesDao.class);
+    resourceDao = mock(ResourceDao.class);
+    dryRunCache = new DryRunCache(serverFileSystem, propertiesDao, resourceDao);
+  }
+
+  @Test
+  public void test_get_cache_location() throws Exception {
+    File tempFolder = temp.newFolder();
+    when(serverFileSystem.getTempDir()).thenReturn(tempFolder);
+
+    assertThat(dryRunCache.getCacheLocation(null)).isEqualTo(new File(new File(tempFolder, "dryRun"), "default"));
+    assertThat(dryRunCache.getCacheLocation(123L)).isEqualTo(new File(new File(tempFolder, "dryRun"), "123"));
+  }
+
+  @Test
+  public void test_get_modification_timestamp() {
+    when(propertiesDao.selectGlobalProperty(DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY)).thenReturn(null);
+    assertThat(dryRunCache.getModificationTimestamp(null)).isEqualTo(0L);
+
+    when(propertiesDao.selectGlobalProperty(DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY)).thenReturn(new PropertyDto().setValue("123456"));
+    assertThat(dryRunCache.getModificationTimestamp(null)).isEqualTo(123456L);
+
+    when(resourceDao.getRootProjectByComponentId(123L)).thenReturn(new ResourceDto().setId(456L));
+
+    when(propertiesDao.selectProjectProperty(456L, DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY)).thenReturn(null);
+    assertThat(dryRunCache.getModificationTimestamp(123L)).isEqualTo(0L);
+
+    when(propertiesDao.selectProjectProperty(456L, DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY)).thenReturn(new PropertyDto().setValue("123456"));
+    assertThat(dryRunCache.getModificationTimestamp(123L)).isEqualTo(123456L);
+  }
+
+  @Test
+  public void test_clean_all() throws Exception {
+    File tempFolder = temp.newFolder();
+    when(serverFileSystem.getTempDir()).thenReturn(tempFolder);
+    File cacheLocation = dryRunCache.getCacheLocation(null);
+    FileUtils.forceMkdir(cacheLocation);
+
+    dryRunCache.cleanAll();
+    verify(propertiesDao).deleteAllProperties(DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY);
+
+    assertThat(cacheLocation).doesNotExist();
+  }
+
+  @Test
+  public void test_clean() throws Exception {
+    File tempFolder = temp.newFolder();
+    when(serverFileSystem.getTempDir()).thenReturn(tempFolder);
+    File cacheLocation = dryRunCache.getCacheLocation(null);
+    FileUtils.forceMkdir(cacheLocation);
+
+    dryRunCache.clean(null);
+
+    assertThat(cacheLocation).doesNotExist();
+  }
+
+  @Test
+  public void test_report_global_modification() {
+    dryRunCache.reportGlobalModification();
+
+    verify(propertiesDao).setProperty(
+      new PropertyDto()
+        .setKey(DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY)
+        .setValue(anyString()));
+  }
+
+  @Test
+  public void test_report_resource_modification() {
+    when(resourceDao.getRootProjectByComponentKey("foo")).thenReturn(new ResourceDto().setId(456L));
+
+    dryRunCache.reportResourceModification("foo");
+
+    verify(propertiesDao).setProperty(
+      new PropertyDto()
+        .setKey(DryRunCache.SONAR_DRY_RUN_CACHE_LAST_UPDATE_KEY)
+        .setValue(anyString())
+        .setResourceId(456L));
+  }
+}
index 1e9de300df16441bce01489dd376d11f35330a14..a8d1b3ca651260ff73073e5a7a3c4a1614b55d34 100644 (file)
@@ -133,6 +133,15 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
     assertThat(first.getValue(), is("one"));
   }
 
+  @Test
+  public void selectProjectProperty() {
+    setupData("selectProjectProperties");
+    PropertyDto property = dao.selectProjectProperty(11L, "commonslang.one");
+
+    assertThat(property.getKey(), is("commonslang.one"));
+    assertThat(property.getValue(), is("two"));
+  }
+
   @Test
   public void setProperty_update() {
     setupData("update");
diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertyDtoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertyDtoTest.java
new file mode 100644 (file)
index 0000000..6379f1f
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.core.properties;
+
+import org.junit.Test;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class PropertyDtoTest extends AbstractDaoTestCase {
+
+  private PropertiesDao dao;
+
+  @Test
+  public void testEquals() {
+    assertThat(new PropertyDto().setKey("123").setResourceId(123L)).isEqualTo(new PropertyDto().setKey("123").setResourceId(123L));
+    assertThat(new PropertyDto().setKey("1234").setResourceId(123L)).isNotEqualTo(new PropertyDto().setKey("123").setResourceId(123L));
+    assertThat(new PropertyDto().setKey("1234").setResourceId(123L)).isNotEqualTo(null);
+    assertThat(new PropertyDto().setKey("1234").setResourceId(123L)).isNotEqualTo(new Object());
+  }
+
+  @Test
+  public void testHashCode() {
+    assertThat(new PropertyDto().setKey("123").setResourceId(123L).hashCode()).isNotNull();
+    assertThat(new PropertyDto().setKey("123").setResourceId(123L).hashCode())
+      .isEqualTo(new PropertyDto().setKey("123").setResourceId(123L).hashCode());
+  }
+
+  @Test
+  public void testToString() {
+    assertThat(new PropertyDto().setKey("foo:bar").setValue("value").setResourceId(123L).setUserId(456L).toString()).isEqualTo("PropertyDto{foo:bar, value, 123, 456}");
+  }
+}
index a6ea934fdc366727f90a71db2a51dd6fd2c0ac74..27c48034999297ee4e656e96a9b0a7290aaa9978 100644 (file)
@@ -7,13 +7,16 @@
   <!-- struts -->
   <properties id="3" prop_key="struts.one" text_value="one" resource_id="10" user_id="[null]"/>
 
-  <!-- struts -->
-  <properties id="4" prop_key="commonslang.one" text_value="one" resource_id="11" user_id="[null]"/>
+  <!-- commons -->
+  <properties id="4" prop_key="commonslang.one" text_value="two" resource_id="11" user_id="[null]"/>
 
   <!-- user -->
   <properties id="5" prop_key="user.one" text_value="one" resource_id="[null]" user_id="100"/>
   <properties id="6" prop_key="user.two" text_value="two" resource_id="10" user_id="100"/>
 
+  <properties id="7" prop_key="commonslang.one" text_value="one" resource_id="12" user_id="[null]"/>
+
   <projects id="10" kee="org.struts:struts"/>
   <projects id="11" kee="org.apache:commons-lang"/>
+  <projects id="12" kee="other"/>
 </dataset>