]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 - Added test case
authorStephane Gamard <stephane.gamard@searchbox.com>
Tue, 3 Jun 2014 14:07:23 +0000 (16:07 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 4 Jun 2014 13:55:02 +0000 (15:55 +0200)
sonar-core/src/main/java/org/sonar/core/log/db/LogDto.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java
sonar-server/src/test/java/org/sonar/server/log/db/LogDaoTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeLogTest.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeTest.java [deleted file]

index c362c53675f285690d2c977632e656fb84d3ac73..b9464b3c57c7e48ec7f43fae1b4a4684a87c7180 100644 (file)
@@ -93,7 +93,8 @@ public final class LogDto extends Dto<LogKey> {
 
   public <K extends Activity> K getActivity() {
     try {
-      return (K) ((Activity) Class.forName(this.payload.clazz).newInstance()).deSerialize(this.data);
+      Activity activity = ((Activity) Class.forName(this.payload.clazz, true, ClassLoader.getSystemClassLoader()).newInstance());
+      return (K) activity.deSerialize(this.data);
     } catch (Exception e) {
       throw new IllegalStateException("Could not read Activity from DB. '" + this.payload
         + "' is most likely missing a public no-args ctor", e);
index 75d11ba7078d4aecf7598ddb7b1d2a55eaf8ba56..5180caf80bd10464d1c7044196e3baeb33fe0f98 100644 (file)
@@ -22,12 +22,18 @@ package org.sonar.server.qualityprofile;
 import com.google.common.collect.Maps;
 import org.sonar.core.log.Activity;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
 import java.util.Map;
 
-public class ActiveRuleChange extends Activity {
+public class ActiveRuleChange extends Activity implements Serializable {
 
   static enum Type {
     ACTIVATED, DEACTIVATED, UPDATED
@@ -40,6 +46,11 @@ public class ActiveRuleChange extends Activity {
   private ActiveRule.Inheritance previousInheritance = null, inheritance = null;
   private Map<String, String> parameters = Maps.newHashMap();
 
+  public ActiveRuleChange(){
+    type = null;
+    key = null;
+  }
+
   ActiveRuleChange(Type type, ActiveRuleKey key) {
     this.type = type;
     this.key = key;
@@ -75,12 +86,14 @@ public class ActiveRuleChange extends Activity {
     return severity;
   }
 
-  public void setSeverity(@Nullable String severity) {
+  public ActiveRuleChange setSeverity(@Nullable String severity) {
     this.severity = severity;
+    return this;
   }
 
-  public void setInheritance(@Nullable ActiveRule.Inheritance inheritance) {
+  public ActiveRuleChange setInheritance(@Nullable ActiveRule.Inheritance inheritance) {
     this.inheritance = inheritance;
+    return this;
   }
 
   @CheckForNull
@@ -99,13 +112,30 @@ public class ActiveRuleChange extends Activity {
 
   @Override
   public String serialize() {
-    //TODO implement String serialization
-    return null;
+    //TODO do not use JDK's serialization
+    try {
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      ObjectOutputStream oos = new ObjectOutputStream(baos);
+      oos.writeObject(this);
+      oos.close();
+      return new String(Base64Coder.encode(baos.toByteArray()));
+    } catch (Exception e) {
+      throw new IllegalStateException("Could not serialize.",e);
+    }
   }
 
   @Override
-  public Activity deSerialize(String data) {
-    //TODO implement String deSerialization
-    return null;
+  public ActiveRuleChange deSerialize(String data) {
+    //TODO do not use JDK's deserialization
+    try {
+    byte [] bytes = Base64Coder.decode(data);
+    ObjectInputStream ois = new ObjectInputStream(
+      new ByteArrayInputStream(bytes));
+      ActiveRuleChange o  = (ActiveRuleChange) ois.readObject();
+    ois.close();
+    return o;
+    } catch (Exception e) {
+      throw new IllegalStateException("Could not serialize.",e);
+    }
   }
 }
index 8c265ac492dcf432e75838b4f636f06db8bda46e..6eeb79ad1e3cd6c594cba0ed44213023fb7e2497 100644 (file)
@@ -55,8 +55,6 @@ public class LogDaoTest extends AbstractDaoTestCase{
 
     TestActivity activity = new TestActivity("hello world");
 
-    System.out.println("activity.getClass().getName() = " + activity.getClass().getName());
-    
     LogDto log = new LogDto("SYSTEM_USER", activity);
 
     dao.insert(session, log);
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeLogTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeLogTest.java
new file mode 100644 (file)
index 0000000..b509071
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.server.qualityprofile;
+
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.System2;
+import org.sonar.core.log.db.LogDto;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
+import org.sonar.server.log.db.LogDao;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class ActiveRuleChangeLogTest extends AbstractDaoTestCase{
+
+
+  private LogDao dao;
+  private DbSession session;
+  private System2 system2;
+
+  @Before
+  public void before() throws Exception {
+    this.session = getMyBatis().openSession(false);
+    this.system2 = mock(System2.class);
+    this.dao = new LogDao(system2);
+  }
+
+  @After
+  public void after(){
+    session.close();
+  }
+
+  @Test
+  public void insert_log(){
+
+    ActiveRuleKey ruleKey = ActiveRuleKey.of(
+      QualityProfileKey.of("name", "java"),
+      RuleKey.of("repository","S001"));
+    ActiveRuleChange ruleChange = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, ruleKey)
+      .setInheritance(ActiveRule.Inheritance.INHERITED);
+
+    LogDto log = new LogDto("SYSTEM_USER", ruleChange);
+
+    dao.insert(session, log);
+
+    LogDto newDto = dao.getByKey(session, log.getKey());
+    assertThat(newDto.getAuthor()).isEqualTo(log.getAuthor());
+
+    ActiveRuleChange loggedRuleChange = newDto.getActivity();
+    assertThat(loggedRuleChange.getKey()).isEqualTo(ruleKey);
+    assertThat(ruleChange.getInheritance()).isEqualTo(ActiveRule.Inheritance.INHERITED);
+
+  }
+}
\ No newline at end of file
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeTest.java
deleted file mode 100644 (file)
index b668b2b..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.server.qualityprofile;
-
-public class ActiveRuleChangeTest {
-
-//  @Test
-//  public void serialize() {
-//    ActiveRuleChange change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED,
-//      ActiveRuleKey.of(QualityProfileKey.of("name", "lang"),
-//        RuleKey.of("repository","rule")));
-//
-//    String result = Activity.serialize(change);
-//
-//    System.out.println("result = " + result);
-//  }
-
-}
\ No newline at end of file