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);
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
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;
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
@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);
+ }
}
}
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);
--- /dev/null
+/*
+ * 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
+++ /dev/null
-/*
- * 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