From: Simon Brandhof Date: Tue, 16 Aug 2011 16:49:05 +0000 (+0200) Subject: SONAR-2655 define toString() on all the hibernate models X-Git-Tag: 2.11^2~147 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=804d3141d8dee14082a9a10b75ad191e386c5576;p=sonarqube.git SONAR-2655 define toString() on all the hibernate models --- diff --git a/sonar-core/src/main/java/org/sonar/api/database/configuration/Property.java b/sonar-core/src/main/java/org/sonar/api/database/configuration/Property.java index f464c73adb6..9e95166c7f2 100644 --- a/sonar-core/src/main/java/org/sonar/api/database/configuration/Property.java +++ b/sonar-core/src/main/java/org/sonar/api/database/configuration/Property.java @@ -19,7 +19,7 @@ */ package org.sonar.api.database.configuration; -import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.BaseIdentifiable; @@ -139,11 +139,6 @@ public class Property extends BaseIdentifiable { @Override public String toString() { - return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) - .append("key", key) - .append("resource", resourceId) - .append("user", userId) - .append("value", value) - .toString(); + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); } } diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/ManualMeasure.java b/sonar-core/src/main/java/org/sonar/jpa/entity/ManualMeasure.java index 94feb1299db..cdb2a575aff 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/ManualMeasure.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/ManualMeasure.java @@ -19,6 +19,9 @@ */ package org.sonar.jpa.entity; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + import javax.persistence.*; import java.util.Date; @@ -91,4 +94,9 @@ public final class ManualMeasure { public String getUserLogin() { return userLogin; } + + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); + } } diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java b/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java index 8428ab9a20e..b1221100509 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java @@ -20,6 +20,8 @@ package org.sonar.jpa.entity; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.notifications.Notification; import org.sonar.api.utils.SonarException; @@ -98,4 +100,8 @@ public class NotificationQueueElement { } } + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); + } } diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java index e0ab95f1abd..ea17a6e5195 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java @@ -19,15 +19,17 @@ */ package org.sonar.jpa.entity; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + +import javax.persistence.*; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import javax.persistence.*; - @Entity -@Table(name = SchemaMigration.TABLE_NAME, uniqueConstraints = { @UniqueConstraint(columnNames = { "version" }) }) +@Table(name = SchemaMigration.TABLE_NAME, uniqueConstraints = {@UniqueConstraint(columnNames = {"version"})}) public class SchemaMigration { public final static int VERSION_UNKNOWN = -1; @@ -102,4 +104,9 @@ public class SchemaMigration { } } } + + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/SnapshotSource.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/SnapshotSource.java index 6a631ab808c..0a4a94519d9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/SnapshotSource.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/SnapshotSource.java @@ -19,6 +19,9 @@ */ package org.sonar.api.database.model; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.BaseIdentifiable; import org.sonar.api.database.DatabaseProperties; @@ -79,4 +82,12 @@ public class SnapshotSource extends BaseIdentifiable { public int hashCode() { return snapshotId.hashCode(); } + + @Override + public String toString() { + return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) + .append("snapshot_id", snapshotId) + .append("data", StringUtils.abbreviate(data, 1000)) + .toString(); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/User.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/User.java index 8c428324501..dc0b318cab8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/User.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/User.java @@ -19,6 +19,8 @@ */ package org.sonar.api.database.model; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.BaseIdentifiable; import javax.persistence.Column; @@ -84,4 +86,9 @@ public class User extends BaseIdentifiable { public int hashCode() { return login.hashCode(); } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/design/DependencyDto.java b/sonar-plugin-api/src/main/java/org/sonar/api/design/DependencyDto.java index 40de2688d24..1b84579927b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/design/DependencyDto.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/design/DependencyDto.java @@ -21,7 +21,8 @@ package org.sonar.api.design; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; -import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import javax.persistence.*; @@ -190,18 +191,6 @@ public class DependencyDto { @Override public String toString() { - return new ToStringBuilder(this) - .append("id", getId()) - .append("fromSnapshotId", fromSnapshotId) - .append("fromResourceId", fromResourceId) - .append("fromScope", fromScope) - .append("toSnapshotId", toSnapshotId) - .append("toResourceId", toResourceId) - .append("toScope", toScope) - .append("weight", weight) - .append("usage", usage) - .append("projectSnapshotId", projectSnapshotId) - .append("parentDependencyId", parentDependencyId) - .toString(); + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java index 4ecd5ab5eac..cf4c1b693ef 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java @@ -20,7 +20,8 @@ package org.sonar.api.measures; import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.BatchExtension; import org.sonar.api.ServerExtension; @@ -122,7 +123,7 @@ public class Metric implements ServerExtension, BatchExtension { /** * Creates an empty metric - * + * * @deprecated in 1.12. Use the {@link Builder} factory. */ @Deprecated @@ -230,7 +231,7 @@ public class Metric implements ServerExtension, BatchExtension { } private Metric(String key, String name, ValueType type, String description, Integer direction, String domain, Boolean qualitative, Double worstValue, Double bestValue, - Boolean optimizedBestValue, Boolean hidden, Formula formula, boolean userManaged) { + Boolean optimizedBestValue, Boolean hidden, Formula formula, boolean userManaged) { this.key = key; this.name = name; this.description = description; @@ -548,19 +549,7 @@ public class Metric implements ServerExtension, BatchExtension { @Override public String toString() { - return new ToStringBuilder(this) - .append("key", key) - .append("name", name) - .append("type", type) - .append("enabled", enabled) - .append("qualitative", qualitative) - .append("direction", direction) - .append("domain", domain) - .append("worstValue", worstValue) - .append("bestValue", bestValue) - .append("optimizedBestValue", optimizedBestValue) - .append("hidden", hidden) - .toString(); + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); } /** @@ -605,7 +594,7 @@ public class Metric implements ServerExtension, BatchExtension { private boolean userManaged = false; /** - * @param key the metric key, should be unique among all metrics + * @param key the metric key, should be unique among all metrics * @param name the metric name * @param type the metric type */ @@ -696,6 +685,7 @@ public class Metric implements ServerExtension, BatchExtension { /** * Values of user-managed metrics can be set online in the "Manual measures" page. + * * @since 2.10 */ public boolean isUserManaged() { @@ -704,7 +694,7 @@ public class Metric implements ServerExtension, BatchExtension { /** * Values of user-managed metrics can be set online in the "Manual measures" page. - * + * * @since 2.10 */ public Builder setUserManaged(boolean b) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java index e7f61d92df6..0455940f88f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java @@ -25,6 +25,8 @@ import org.apache.commons.collections.Transformer; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.model.ResourceModel; import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.Rule; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectLink.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectLink.java index f3cb290e15e..d81eab634fc 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectLink.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectLink.java @@ -20,7 +20,9 @@ package org.sonar.api.resources; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.BaseIdentifiable; import org.sonar.api.database.model.ResourceModel; @@ -119,12 +121,7 @@ public class ProjectLink extends BaseIdentifiable { @Override public String toString() { - return new ToStringBuilder(this) - .append("key", key) - .append("name", name) - .append("href", href) - .append("resource", resource) - .toString(); + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); } public void copyFieldsFrom(ProjectLink link) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleChange.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleChange.java index e9373e73bc8..58c713f8585 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleChange.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleChange.java @@ -19,9 +19,7 @@ */ package org.sonar.api.rules; -import org.apache.commons.lang.builder.EqualsBuilder; -import org.apache.commons.lang.builder.HashCodeBuilder; -import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.*; import org.sonar.api.database.BaseIdentifiable; import org.sonar.api.profiles.RulesProfile; @@ -192,15 +190,7 @@ public class ActiveRuleChange extends BaseIdentifiable { @Override public String toString() { - return new ToStringBuilder(this) - .append("id", getId()) - .append("profile", rulesProfile) - .append("rule", rule) - .append("modifier", userName) - .append("changed at", date) - .append("enabled", enabled) - .append("new severity", newSeverity) - .toString(); + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java index 01cf4bb0952..3314ab50ce0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java @@ -20,9 +20,7 @@ package org.sonar.api.rules; import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.builder.EqualsBuilder; -import org.apache.commons.lang.builder.HashCodeBuilder; -import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.*; import org.sonar.api.database.DatabaseProperties; import org.sonar.check.Cardinality; @@ -398,13 +396,7 @@ public final class Rule { @Override public String toString() { - return new ToStringBuilder(this) - .append("id", getId()) - .append("name", name) - .append("key", key) - .append("configKey", configKey) - .append("plugin", pluginName) - .toString(); + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); } private String removeNewLineCharacters(String text) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/security/GroupRole.java b/sonar-plugin-api/src/main/java/org/sonar/api/security/GroupRole.java index 0dce5bea884..d0cfb857dc8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/security/GroupRole.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/security/GroupRole.java @@ -19,6 +19,8 @@ */ package org.sonar.api.security; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.BaseIdentifiable; import javax.persistence.Column; @@ -81,4 +83,9 @@ public class GroupRole extends BaseIdentifiable { public boolean isAnyone() { return groupId==ANYONE_GROUP_ID; } + + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/security/UserRole.java b/sonar-plugin-api/src/main/java/org/sonar/api/security/UserRole.java index eb1a8a10598..7925e55513f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/security/UserRole.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/security/UserRole.java @@ -19,6 +19,8 @@ */ package org.sonar.api.security; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.BaseIdentifiable; import javax.persistence.Column; @@ -78,4 +80,9 @@ public class UserRole extends BaseIdentifiable { this.resourceId = resourceId; return this; } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/database/model/UserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/database/model/UserTest.java new file mode 100644 index 00000000000..661ae8ac33b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/database/model/UserTest.java @@ -0,0 +1,58 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * 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.api.database.model; + +import org.hamcrest.core.Is; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.internal.matchers.StringContains.containsString; + +public class UserTest { + + @Test + public void testToString() { + User user = new User() + .setEmail("super@m.an") + .setLogin("superman") + .setName("Superman"); + assertThat(user.toString(), containsString("super@m.an")); + assertThat(user.toString(), containsString("superman")); + assertThat(user.toString(), containsString("Superman")); + } + + @Test + public void testEquals() { + User one = new User() + .setLogin("one") + .setName("One"); + + User two = new User() + .setLogin("two") + .setName("Two"); + + assertThat(one.equals(one), Is.is(true)); + assertThat(one.equals(new User().setLogin("one")), Is.is(true)); + assertThat(one.equals(two), Is.is(false)); + + assertEquals(one.hashCode(), new User().setLogin("one").hashCode()); + } +}