From 0cd20cfaeea808a80c72f5cf62ea32f95e1cff12 Mon Sep 17 00:00:00 2001 From: jensp Date: Sat, 30 May 2015 14:41:42 +0000 Subject: [PATCH] CCM NG: Tests and fixed bugs found by tests git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3442 8810af33-2d31-482b-a856-94f89814c4df --- .../org/libreccm/core/GroupMembership.java | 2 +- .../main/java/org/libreccm/core/Party.java | 23 ++- .../java/org/libreccm/core/Permission.java | 47 +++--- .../java/org/libreccm/core/PersonName.java | 2 +- .../java/org/libreccm/core/Privilege.java | 4 +- .../main/java/org/libreccm/core/Resource.java | 28 ++-- .../src/main/java/org/libreccm/core/Role.java | 20 +-- .../src/main/java/org/libreccm/core/User.java | 2 +- .../java/org/libreccm/core/UserGroup.java | 29 ++-- .../categorization/EqualsAndHashCodeTest.java | 50 ++++++ .../libreccm/categorization/ToStringTest.java | 50 ++++++ .../core/CategorizationEntitiesTest.java | 39 ----- .../libreccm/core/CcmCoreEntitiesTest.java | 45 ----- .../org/libreccm/core/EntitiesTestCore.java | 14 +- .../libreccm/core/EqualsAndHashCodeTest.java | 57 +++++++ .../java/org/libreccm/core/EqualsTest.java | 156 ------------------ .../org/libreccm/core/ResourceEntityTest.java | 82 +++++++++ .../java/org/libreccm/core/ToStringTest.java | 59 +++++++ .../org/libreccm/jpautils/EqualsVerifier.java | 73 ++++++++ .../libreccm/jpautils/ToStringVerifier.java | 103 ++++++++++++ 20 files changed, 565 insertions(+), 320 deletions(-) create mode 100644 ccm-core/src/test/java/org/libreccm/categorization/EqualsAndHashCodeTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/categorization/ToStringTest.java delete mode 100644 ccm-core/src/test/java/org/libreccm/core/CategorizationEntitiesTest.java delete mode 100644 ccm-core/src/test/java/org/libreccm/core/CcmCoreEntitiesTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/core/EqualsAndHashCodeTest.java delete mode 100644 ccm-core/src/test/java/org/libreccm/core/EqualsTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/core/ResourceEntityTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/core/ToStringTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/jpautils/EqualsVerifier.java create mode 100644 ccm-core/src/test/java/org/libreccm/jpautils/ToStringVerifier.java diff --git a/ccm-core/src/main/java/org/libreccm/core/GroupMembership.java b/ccm-core/src/main/java/org/libreccm/core/GroupMembership.java index 221946c0c..82304d9da 100644 --- a/ccm-core/src/main/java/org/libreccm/core/GroupMembership.java +++ b/ccm-core/src/main/java/org/libreccm/core/GroupMembership.java @@ -88,7 +88,7 @@ public class GroupMembership implements Serializable { if (obj == null) { return false; } - if (getClass() != obj.getClass()) { + if (!(obj instanceof GroupMembership)) { return false; } final GroupMembership other = (GroupMembership) obj; diff --git a/ccm-core/src/main/java/org/libreccm/core/Party.java b/ccm-core/src/main/java/org/libreccm/core/Party.java index b98455394..036fb2739 100644 --- a/ccm-core/src/main/java/org/libreccm/core/Party.java +++ b/ccm-core/src/main/java/org/libreccm/core/Party.java @@ -56,12 +56,16 @@ public class Party extends CcmObject implements Serializable { public Party() { super(); - + grantedPermissions = new ArrayList<>(); } - + public List getEmailAddresses() { - return Collections.unmodifiableList(emailAddresses); + if (emailAddresses == null) { + return null; + } else { + return Collections.unmodifiableList(emailAddresses); + } } protected void setEmailAddresses(final List eMailAddresses) { @@ -80,14 +84,15 @@ public class Party extends CcmObject implements Serializable { return Collections.unmodifiableList(grantedPermissions); } - protected void setGrantedPermissions(final List grantedPermissions) { + protected void setGrantedPermissions( + final List grantedPermissions) { this.grantedPermissions = grantedPermissions; } - + protected void addGrantedPermission(final Permission permission) { grantedPermissions.add(permission); } - + protected void removeGrantedPermission(final Permission permission) { grantedPermissions.remove(permission); } @@ -95,7 +100,7 @@ public class Party extends CcmObject implements Serializable { @Override public int hashCode() { int hash = super.hashCode(); - hash = 41 * hash + Objects.hashCode(this.emailAddresses); + hash = 41 * hash + Objects.hashCode(emailAddresses); return hash; } @@ -108,7 +113,7 @@ public class Party extends CcmObject implements Serializable { if (obj == null) { return false; } - if (getClass() != obj.getClass()) { + if (!(obj instanceof Party)) { return false; } final Party other = (Party) obj; @@ -116,7 +121,7 @@ public class Party extends CcmObject implements Serializable { return false; } - return Objects.equals(this.emailAddresses, other.getEmailAddresses()); + return Objects.equals(emailAddresses, other.getEmailAddresses()); } @Override diff --git a/ccm-core/src/main/java/org/libreccm/core/Permission.java b/ccm-core/src/main/java/org/libreccm/core/Permission.java index eb4aba8e3..0322ba7b9 100644 --- a/ccm-core/src/main/java/org/libreccm/core/Permission.java +++ b/ccm-core/src/main/java/org/libreccm/core/Permission.java @@ -48,27 +48,27 @@ public class Permission implements Serializable { @Column(name = "permission_id") @GeneratedValue(strategy = GenerationType.AUTO) private long permissionId; - + @ManyToOne @JoinColumn(name = "grantee_id") private Party grantee; - + @OneToOne @JoinColumn(name = "granted_privilege_id") private Privilege grantedPrivilege; - + @ManyToOne @JoinColumn(name = "object_id") private CcmObject object; - + @ManyToOne @JoinColumn(name = "creation_user_id") private User creationUser; - + @Column(name = "creation_date") @Temporal(TemporalType.TIMESTAMP) private Date creationDate; - + @Column(name = "creation_ip") private String creationIp; @@ -113,7 +113,11 @@ public class Permission implements Serializable { } public Date getCreationDate() { - return new Date(creationDate.getTime()); + if (creationDate == null) { + return null; + } else { + return new Date(creationDate.getTime()); + } } public void setCreationDate(final Date creationDate) { @@ -132,7 +136,7 @@ public class Permission implements Serializable { public int hashCode() { int hash = 3; hash - = 31 * hash + (int) (permissionId ^ (permissionId >>> 32)); + = 31 * hash + (int) (permissionId ^ (permissionId >>> 32)); hash = 31 * hash + Objects.hashCode(grantee); hash = 31 * hash + Objects.hashCode(grantedPrivilege); hash = 31 * hash + Objects.hashCode(object); @@ -147,15 +151,15 @@ public class Permission implements Serializable { if (obj == null) { return false; } - - if (getClass() != obj.getClass()) { + + if (!(obj instanceof Permission)) { return false; } final Permission other = (Permission) obj; if (!other.canEqual(this)) { return false; } - + if (permissionId != other.getPermissionId()) { return false; } @@ -176,22 +180,22 @@ public class Permission implements Serializable { } return Objects.equals(creationIp, other.getCreationIp()); } - + public boolean canEqual(final Object obj) { return obj instanceof Permission; } - + @Override public String toString() { return String.format("%s{ " - + "permissionId = %d, " - + "grantee = %s, " - + "grantedPrivilege = %s, " - + "object = %s, " - + "creationUser = %s," - + "creationDate = %tF %Jens Pelzetter */ @Entity -@Table(name = "privileges") +@Table(name = "ccm_privileges") public class Privilege implements Serializable { private static final long serialVersionUID = -3986038536996049440L; @@ -75,7 +75,7 @@ public class Privilege implements Serializable { if (obj == null) { return false; } - if (getClass() != obj.getClass()) { + if (!(obj instanceof Privilege)) { return false; } final Privilege other = (Privilege) obj; diff --git a/ccm-core/src/main/java/org/libreccm/core/Resource.java b/ccm-core/src/main/java/org/libreccm/core/Resource.java index e0900f27d..5fe3a4c93 100644 --- a/ccm-core/src/main/java/org/libreccm/core/Resource.java +++ b/ccm-core/src/main/java/org/libreccm/core/Resource.java @@ -39,18 +39,18 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; /** - * The {@code Resource} class is a base class for several other classes, for + * The {@code Resource} class is a base class for several other classes, for * example the {@link Application} class. - * + * * Resources can be nested, a resource can have multiple child resources. - * - * This class is an adopted variant of the class + * + * This class is an adopted variant of the class * {@code com.arsdigita.kernel.Resource} from the old structure. This class is * maybe removed in future releases. Therefore it is strictly recommend not to - * use this class directly. - * + * use this class directly. + * * @author Jens Pelzetter - * + * */ @Entity @Table(name = "resources") @@ -69,7 +69,7 @@ public class Resource extends CcmObject implements Serializable { @JoinColumn(name = "object_id")})) private LocalizedString title; - /** + /** * A localisable description for the {@code Resource}. */ @Embedded @@ -102,7 +102,6 @@ public class Resource extends CcmObject implements Serializable { // @Column(name = "resource_type") // private String resourceType; - public LocalizedString getTitle() { return title; } @@ -120,7 +119,11 @@ public class Resource extends CcmObject implements Serializable { } public Date getCreated() { - return new Date(created.getTime()); + if (created == null) { + return null; + } else { + return new Date(created.getTime()); + } } public void setCreated(final Date created) { @@ -158,7 +161,6 @@ public class Resource extends CcmObject implements Serializable { // public void setResourceType(final String resourceType) { // this.resourceType = resourceType; // } - @Override public int hashCode() { int hash = 5; @@ -174,7 +176,7 @@ public class Resource extends CcmObject implements Serializable { if (obj == null) { return false; } - if (getClass() != obj.getClass()) { + if (!(obj instanceof Resource)) { return false; } final Resource other = (Resource) obj; @@ -205,7 +207,7 @@ public class Resource extends CcmObject implements Serializable { return super.toString( String.format(", title = %s, " + "created = %tF % roles; - + @OneToMany(mappedBy = "group") private List members; @@ -66,7 +66,11 @@ public class UserGroup extends Party implements Serializable { } public List getRoles() { - return Collections.unmodifiableList(roles); + if (roles == null) { + return null; + } else { + return Collections.unmodifiableList(roles); + } } protected void setRoles(final List roles) { @@ -80,26 +84,30 @@ public class UserGroup extends Party implements Serializable { protected void removeRole(final Role role) { roles.remove(role); } - + public List getMembers() { - return Collections.unmodifiableList(members); + if (members == null) { + return null; + } else { + return Collections.unmodifiableList(members); + } } - + protected void setMembers(final List members) { this.members = members; } - + protected void addMember(final GroupMembership member) { members.add(member); } - + protected void removeMember(final GroupMembership member) { members.remove(member); } @Override public int hashCode() { - int hash = 5; + int hash = super.hashCode(); hash = 83 * hash + Objects.hashCode(this.name); hash = 83 * hash + Objects.hashCode(this.roles); return hash; @@ -114,7 +122,7 @@ public class UserGroup extends Party implements Serializable { if (obj == null) { return false; } - if (getClass() != obj.getClass()) { + if (!(obj instanceof UserGroup)) { return false; } final UserGroup other = (UserGroup) obj; @@ -128,7 +136,8 @@ public class UserGroup extends Party implements Serializable { return Objects.equals(this.roles, other.getRoles()); } - public boolean canEquals(final Object obj) { + @Override + public boolean canEqual(final Object obj) { return obj instanceof UserGroup; } diff --git a/ccm-core/src/test/java/org/libreccm/categorization/EqualsAndHashCodeTest.java b/ccm-core/src/test/java/org/libreccm/categorization/EqualsAndHashCodeTest.java new file mode 100644 index 000000000..302a1a69e --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/categorization/EqualsAndHashCodeTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.categorization; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.libreccm.jpautils.EqualsVerifier; +import org.libreccm.tests.categories.UnitTest; + +import java.util.Arrays; +import java.util.Collection; + +/** + * + * @author Jens Pelzetter + */ +@RunWith(Parameterized.class) +@org.junit.experimental.categories.Category(UnitTest.class) +public class EqualsAndHashCodeTest extends EqualsVerifier { + + @Parameterized.Parameters(name = "{0}") + public static Collection> data() { + return Arrays.asList(new Class[]{ + Categorization.class, + org.libreccm.categorization.Category.class, + Domain.class, + DomainOwnership.class}); + } + + public EqualsAndHashCodeTest(final Class entitiesClass) { + super(entitiesClass); + } + +} diff --git a/ccm-core/src/test/java/org/libreccm/categorization/ToStringTest.java b/ccm-core/src/test/java/org/libreccm/categorization/ToStringTest.java new file mode 100644 index 000000000..796ab78e2 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/categorization/ToStringTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.categorization; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.libreccm.jpautils.ToStringVerifier; +import org.libreccm.tests.categories.UnitTest; + +import java.util.Arrays; +import java.util.Collection; + +/** + * + * @author Jens Pelzetter + */ +@RunWith(Parameterized.class) +@org.junit.experimental.categories.Category(UnitTest.class) +public class ToStringTest extends ToStringVerifier { + + @Parameterized.Parameters(name = "{0}") + public static Collection> data() { + return Arrays.asList(new Class[]{ + Categorization.class, + org.libreccm.categorization.Category.class, + Domain.class, + DomainOwnership.class}); + } + + public ToStringTest(final Class entitiesClass) { + super(entitiesClass); + } + +} diff --git a/ccm-core/src/test/java/org/libreccm/core/CategorizationEntitiesTest.java b/ccm-core/src/test/java/org/libreccm/core/CategorizationEntitiesTest.java deleted file mode 100644 index 203758a71..000000000 --- a/ccm-core/src/test/java/org/libreccm/core/CategorizationEntitiesTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.libreccm.core; - -import java.util.Arrays; -import java.util.Collection; -import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.libreccm.categorization.Categorization; -import org.libreccm.categorization.Domain; -import org.libreccm.categorization.DomainOwnership; -import org.libreccm.tests.categories.UnitTest; - -/** - * - * @author Jens Pelzetter - */ -@RunWith(Parameterized.class) -@Category(UnitTest.class) -public class CategorizationEntitiesTest extends EntitiesTestCore { - - @Parameterized.Parameters - public static Collection> data() { - return Arrays.asList(new Class[]{ - Categorization.class, - org.libreccm.categorization.Category.class, - Domain.class, - DomainOwnership.class}); - } - - public CategorizationEntitiesTest(final Class entitiesClass) { - super(entitiesClass); - } - -} diff --git a/ccm-core/src/test/java/org/libreccm/core/CcmCoreEntitiesTest.java b/ccm-core/src/test/java/org/libreccm/core/CcmCoreEntitiesTest.java deleted file mode 100644 index 0c49cff25..000000000 --- a/ccm-core/src/test/java/org/libreccm/core/CcmCoreEntitiesTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.libreccm.core; - -import java.util.Arrays; -import java.util.Collection; -import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.libreccm.tests.categories.UnitTest; - -/** - * The tests in this class are used to verify the implementations of the - * {@code equals}, {@code hashCode} and {@code toString} methods of the entities - * in the {@code org.libreccm.core} package. - * - * @author Jens Pelzetter - */ -@RunWith(Parameterized.class) -@Category(UnitTest.class) -public class CcmCoreEntitiesTest extends EntitiesTestCore { - - @Parameterized.Parameters(name = "{0}") - public static Collection> data() { - return Arrays.asList(new Class[]{ - CcmObject.class, - EmailAddress.class, - GroupMembership.class, - Party.class, - Permission.class, - PersonName.class, - Privilege.class, - Resource.class, - Role.class, - User.class, - UserGroup.class}); - } - - public CcmCoreEntitiesTest(final Class entityClass) { - super(entityClass); - } -} diff --git a/ccm-core/src/test/java/org/libreccm/core/EntitiesTestCore.java b/ccm-core/src/test/java/org/libreccm/core/EntitiesTestCore.java index 5c56785e3..b8ed303b3 100644 --- a/ccm-core/src/test/java/org/libreccm/core/EntitiesTestCore.java +++ b/ccm-core/src/test/java/org/libreccm/core/EntitiesTestCore.java @@ -10,15 +10,11 @@ import nl.jqno.equalsverifier.Warning; import org.junit.Assert; import org.junit.Test; -import java.beans.BeanInfo; import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.lang.reflect.Modifier; /** @@ -38,6 +34,7 @@ public class EntitiesTestCore { EqualsVerifier .forClass(entityClass) .suppress(Warning.STRICT_INHERITANCE) + .suppress(Warning.NONFINAL_FIELDS) .withRedefinedSuperclass() .verify(); } @@ -49,18 +46,11 @@ public class EntitiesTestCore { IllegalArgumentException, InvocationTargetException { final Object obj = entityClass.newInstance(); -// final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass); -// -// final PropertyDescriptor[] properties = beanInfo -// .getPropertyDescriptors(); -// for (PropertyDescriptor property : properties) { -// final Method setter = property.getWriteMethod(); -// setter.invoke(obj, new Object[]{null}); final Field[] fields = entityClass.getDeclaredFields(); for (Field field : fields) { if (!Modifier.isStatic(field.getModifiers()) - && !field.getType().isPrimitive()) { + && !field.getType().isPrimitive()) { field.setAccessible(true); field.set(obj, null); } diff --git a/ccm-core/src/test/java/org/libreccm/core/EqualsAndHashCodeTest.java b/ccm-core/src/test/java/org/libreccm/core/EqualsAndHashCodeTest.java new file mode 100644 index 000000000..aff93d2f1 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/EqualsAndHashCodeTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core; + +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.libreccm.jpautils.EqualsVerifier; +import org.libreccm.tests.categories.UnitTest; + +import java.util.Arrays; +import java.util.Collection; + +/** + * + * @author Jens Pelzetter + */ +@RunWith(Parameterized.class) +@Category(UnitTest.class) +public class EqualsAndHashCodeTest extends EqualsVerifier { + + @Parameterized.Parameters(name = "{0}") + public static Collection> data() { + return Arrays.asList(new Class[]{ + CcmObject.class, + EmailAddress.class, + GroupMembership.class, + Party.class, + Permission.class, + PersonName.class, + Privilege.class, + Role.class, + User.class, + UserGroup.class}); + } + + public EqualsAndHashCodeTest(final Class entityClass) { + super(entityClass); + } + +} diff --git a/ccm-core/src/test/java/org/libreccm/core/EqualsTest.java b/ccm-core/src/test/java/org/libreccm/core/EqualsTest.java deleted file mode 100644 index 8121d1ab1..000000000 --- a/ccm-core/src/test/java/org/libreccm/core/EqualsTest.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (C) 2015 LibreCCM Foundation. - * - * This library 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 2.1 of the License, or (at your option) any later version. - * - * This library 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 library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301 USA - */ -package org.libreccm.core; - -import nl.jqno.equalsverifier.EqualsVerifier; -import nl.jqno.equalsverifier.Warning; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.libreccm.tests.categories.UnitTest; - -/** - * - * @author Jens Pelzetter - */ -@Category(UnitTest.class) -public class EqualsTest { - - public EqualsTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - @Test - public void verifyCcmObject() { - EqualsVerifier - .forClass(CcmObject.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyEmailAddress() { - EqualsVerifier - .forClass(EmailAddress.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyGroupMembership() { - EqualsVerifier - .forClass(GroupMembership.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyParty() { - EqualsVerifier - .forClass(Party.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyPermission() { - EqualsVerifier - .forClass(Permission.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyPersonName() { - EqualsVerifier - .forClass(PersonName.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyPrivilege() { - EqualsVerifier - .forClass(Privilege.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyResource() { - EqualsVerifier - .forClass(Resource.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyRole() { - EqualsVerifier - .forClass(Role.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyUser() { - EqualsVerifier - .forClass(User.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - - @Test - public void verifyUserGroup() { - EqualsVerifier - .forClass(UserGroup.class) - .suppress(Warning.STRICT_INHERITANCE) - .withRedefinedSuperclass() - .verify(); - } - -} diff --git a/ccm-core/src/test/java/org/libreccm/core/ResourceEntityTest.java b/ccm-core/src/test/java/org/libreccm/core/ResourceEntityTest.java new file mode 100644 index 000000000..6f7ce06e1 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/ResourceEntityTest.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core; + +import nl.jqno.equalsverifier.EqualsVerifier; +import nl.jqno.equalsverifier.Warning; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.libreccm.l10n.LocalizedString; +import org.libreccm.tests.categories.UnitTest; + +import java.util.Locale; + + + +/** + * + * @author Jens Pelzetter + */ +@Category(UnitTest.class) +public class ResourceEntityTest { + + public ResourceEntityTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Test + public void verifyEqualsAndHashCode() { + final Resource resource1 = new Resource(); + final LocalizedString title1 = new LocalizedString(); + title1.addValue(Locale.ENGLISH, "Resource 1"); + resource1.setTitle(title1); + + final Resource resource2 = new Resource(); + final LocalizedString title2 = new LocalizedString(); + title2.addValue(Locale.ENGLISH, "Resource 2"); + resource2.setTitle(title2); + + EqualsVerifier + .forClass(Resource.class) + .suppress(Warning.STRICT_INHERITANCE) + .suppress(Warning.NONFINAL_FIELDS) + .withPrefabValues(Resource.class, resource1, resource2) + .withRedefinedSuperclass() + .verify(); + } +} diff --git a/ccm-core/src/test/java/org/libreccm/core/ToStringTest.java b/ccm-core/src/test/java/org/libreccm/core/ToStringTest.java new file mode 100644 index 000000000..827cb6b20 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/ToStringTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core; + +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.libreccm.jpautils.ToStringVerifier; +import org.libreccm.tests.categories.UnitTest; + +import java.util.Arrays; +import java.util.Collection; + +/** + * + * @author Jens Pelzetter + */ +@RunWith(Parameterized.class) +@Category(UnitTest.class) +public class ToStringTest extends ToStringVerifier { + + @Parameterized.Parameters(name = "{0}") + public static Collection> data() { + return Arrays.asList(new Class[]{ + CcmObject.class, + EmailAddress.class, + GroupMembership.class, + Party.class, + Permission.class, + PersonName.class, + Privilege.class, + Resource.class, + Role.class, + User.class, + UserGroup.class}); + } + + public ToStringTest(final Class entityClass) { + super(entityClass); + } + + +} diff --git a/ccm-core/src/test/java/org/libreccm/jpautils/EqualsVerifier.java b/ccm-core/src/test/java/org/libreccm/jpautils/EqualsVerifier.java new file mode 100644 index 000000000..0ee7f6d98 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/jpautils/EqualsVerifier.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.jpautils; + +import nl.jqno.equalsverifier.Warning; +import org.junit.Test; +import org.junit.runners.Parameterized; + +/** + * A base class for verifying the implementations of the {@code equals()} + * and {@code hashCode} methods of an object using the {@link Parameterized} + * test runner from JUnit. + * + * To use this class create a new JUnit test class which extends this class + * and which uses the {@link Parameterized} test runner. The class must have a + * static method which provides the classes to be tested. Example for testing + * the classes {@code Foo} and {@code Bar} (imports have been omitted): + * + *
+ * @RunWith(Parameterized.class)
+ * @Category(UnitTest.class)
+ * public class FooBarTest extends EqualsVerifier {
+ * 
+ *     @Parameterized.Parameters(name = "{0}")
+ *     public static Collection> data() {
+ *         return Arrays.asList(new Class[] {
+ *             Foo.class,
+ *             Bar.class
+ *         }); 
+ *     }
+ * 
+ *     public FooBarTest(final Class entityClass) {
+ *         super(entityClass);
+ *     }
+ * }
+ * 
+ * + * @author Jens Pelzetter + */ +public class EqualsVerifier { + + private final Class entityClass; + + public EqualsVerifier(final Class entityClass) { + this.entityClass = entityClass; + } + + @Test + public void verifyEqualsAndHashCode() { + nl.jqno.equalsverifier.EqualsVerifier + .forClass(entityClass) + .suppress(Warning.STRICT_INHERITANCE) + .suppress(Warning.NONFINAL_FIELDS) + .withRedefinedSuperclass() + .verify(); + } +} diff --git a/ccm-core/src/test/java/org/libreccm/jpautils/ToStringVerifier.java b/ccm-core/src/test/java/org/libreccm/jpautils/ToStringVerifier.java new file mode 100644 index 000000000..938926253 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/jpautils/ToStringVerifier.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.jpautils; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runners.Parameterized; + +import java.beans.IntrospectionException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + +/** + * A base class for verifying the implementation of the {@code toString()} + * method of an object using the {@link Parameterized} + * test runner from JUnit. + * + * To use this class create a new JUnit test class which extends this class + * and which uses the {@link Parameterized} test runner. The class must have a + * static method which provides the classes to be tested. Example for testing + * the classes {@code Foo} and {@code Bar} (imports have been omitted): + * + *
+ * @RunWith(Parameterized.class)
+ * @Category(UnitTest.class)
+ * public class FooBarTest extends ToStringVerifier {
+ * 
+ *     @Parameterized.Parameters(name = "{0}")
+ *     public static Collection> data() {
+ *         return Arrays.asList(new Class[] {
+ *             Foo.class,
+ *             Bar.class
+ *         }); 
+ *     }
+ * 
+ *     public FooBarTest(final Class entityClass) {
+ *         super(entityClass);
+ *     }
+ * }
+ * 
+ * + * @author Jens Pelzetter + */ +public class ToStringVerifier { + + private final Class entityClass; + + public ToStringVerifier(final Class entityClass) { + this.entityClass = entityClass; + } + + @Test + public void verifyToString() throws IntrospectionException, + InstantiationException, + IllegalAccessException, + IllegalArgumentException, + InvocationTargetException { + final Object obj = entityClass.newInstance(); + + final Field[] fields = entityClass.getDeclaredFields(); + for (Field field : fields) { + if (!Modifier.isStatic(field.getModifiers()) + && !field.getType().isPrimitive()) { + field.setAccessible(true); + field.set(obj, null); + } + } + + try { + obj.toString(); + } catch (NullPointerException ex) { + final StringWriter strWriter = new StringWriter(); + final PrintWriter writer = new PrintWriter(strWriter); + ex.printStackTrace(writer); + Assert.fail(String.format( + "toString() implemention of class \"%s\" " + + "is not null safe:\n %s", + entityClass.getName(), + strWriter.toString())); + + } + } + +}