From: Stephane Gamard Date: Tue, 3 Jun 2014 14:07:23 +0000 (+0200) Subject: SONAR-5329 - Added test case X-Git-Tag: 4.4-RC1~633 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1e5d2a80cf02d3689ebe9ebfb2c881bd645ee3f2;p=sonarqube.git SONAR-5329 - Added test case --- diff --git a/sonar-core/src/main/java/org/sonar/core/log/db/LogDto.java b/sonar-core/src/main/java/org/sonar/core/log/db/LogDto.java index c362c53675f..b9464b3c57c 100644 --- a/sonar-core/src/main/java/org/sonar/core/log/db/LogDto.java +++ b/sonar-core/src/main/java/org/sonar/core/log/db/LogDto.java @@ -93,7 +93,8 @@ public final class LogDto extends Dto { public 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); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java index 75d11ba7078..5180caf80bd 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java @@ -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 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); + } } } diff --git a/sonar-server/src/test/java/org/sonar/server/log/db/LogDaoTest.java b/sonar-server/src/test/java/org/sonar/server/log/db/LogDaoTest.java index 8c265ac492d..6eeb79ad1e3 100644 --- a/sonar-server/src/test/java/org/sonar/server/log/db/LogDaoTest.java +++ b/sonar-server/src/test/java/org/sonar/server/log/db/LogDaoTest.java @@ -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 index 00000000000..b509071d3a4 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeLogTest.java @@ -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 index b668b2bea43..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeTest.java +++ /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