From ba2eea0ca8d1214039bce00a23b2404254c24a5c Mon Sep 17 00:00:00 2001 From: jensp Date: Fri, 1 May 2015 15:48:55 +0000 Subject: [PATCH] CCM NG: - Fixed several issues reported by PMD - Test for the UriConverter git-svn-id: https://svn.libreccm.org/ccm/jpa@3369 8810af33-2d31-482b-a856-94f89814c4df --- ccm-core/pom.xml | 48 +----- .../categorization/Categorization.java | 125 +++++++++++++-- .../org/libreccm/categorization/Category.java | 4 + .../org/libreccm/categorization/Domain.java | 8 +- .../categorization/DomainOwnership.java | 4 + .../main/java/org/libreccm/core/Resource.java | 6 +- .../org/libreccm/l10n/LocalizedString.java | 2 +- ccm-core/src/site/site.xml | 16 ++ .../libreccm/jpautils/UriConverterTest.java | 146 ++++++++++++++++++ .../libreccm/tests/categories/UnitTest.java | 31 ++++ src/site/site.xml | 17 ++ 11 files changed, 345 insertions(+), 62 deletions(-) create mode 100644 ccm-core/src/site/site.xml create mode 100644 ccm-core/src/test/java/org/libreccm/jpautils/UriConverterTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/tests/categories/UnitTest.java create mode 100644 src/site/site.xml diff --git a/ccm-core/pom.xml b/ccm-core/pom.xml index 544b97b36..8e6bc7926 100644 --- a/ccm-core/pom.xml +++ b/ccm-core/pom.xml @@ -30,52 +30,6 @@ - - - jp-digital-snapshots - http://archiva.jp-digital.de/repository/jp-digital-snapshots/ - - false - - - true - - - - jp-digital-releases - http://archiva.jp-digital.de/repository/jp-digital-releases/ - - true - - - false - - - - - - - jp-digital-snapshots - http://archiva.jp-digital.de/repository/jp-digital-snapshots/ - - false - - - true - - - - jp-digital-releases - http://archiva.jp-digital.de/repository/jp-digital-releases/ - - true - - - false - - - - javax @@ -127,7 +81,7 @@ maven-surefire-plugin 2.18.1 - de.jpdigital.research.accessibilitystudy.tests.UnitTest + org.libreccm.tests.categories.UnitTest diff --git a/ccm-core/src/main/java/org/libreccm/categorization/Categorization.java b/ccm-core/src/main/java/org/libreccm/categorization/Categorization.java index 395a7973d..fd7f82bf4 100644 --- a/ccm-core/src/main/java/org/libreccm/categorization/Categorization.java +++ b/ccm-core/src/main/java/org/libreccm/categorization/Categorization.java @@ -19,6 +19,7 @@ package org.libreccm.categorization; import java.io.Serializable; + import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -26,8 +27,11 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; + import org.libreccm.core.CcmObject; +import java.util.Objects; + /** * * @author Jens Pelzetter @@ -35,26 +39,129 @@ import org.libreccm.core.CcmObject; @Entity @Table(name = "categorizations") public class Categorization implements Serializable { - + private static final long serialVersionUID = 201504301320L; - + @Id @Column(name = "categorization_id") @GeneratedValue(strategy = GenerationType.AUTO) private long categorizationId; - + @ManyToOne private Category category; - + @ManyToOne private CcmObject categorizedObject; - + @Column(name = "category_order") private long categoryOrder; - + @Column(name = "object_order") private long objectOrder; - - - + + public long getCategorizationId() { + return categorizationId; + } + + public void setCategorizationId(final long categorizationId) { + this.categorizationId = categorizationId; + } + + public Category getCategory() { + return category; + } + + protected void setCategory(final Category category) { + this.category = category; + } + + public CcmObject getCategorizedObject() { + return categorizedObject; + } + + protected void setCategorizedObject(final CcmObject categorizedObject) { + this.categorizedObject = categorizedObject; + } + + public long getCategoryOrder() { + return categoryOrder; + } + + public void setCategoryOrder(final long categoryOrder) { + this.categoryOrder = categoryOrder; + } + + public long getObjectOrder() { + return objectOrder; + } + + public void setObjectOrder(final long objectOrder) { + this.objectOrder = objectOrder; + } + + @Override + public int hashCode() { + int hash = 7; + hash + = 89 * hash + (int) (categorizationId ^ (categorizationId >>> 32)); + hash = 89 * hash + Objects.hashCode(category); + hash = 89 * hash + Objects.hashCode(categorizedObject); + hash = 89 * hash + (int) (categoryOrder ^ (categoryOrder >>> 32)); + hash = 89 * hash + (int) (objectOrder ^ (objectOrder >>> 32)); + return hash; + } + + @Override + public boolean equals(final Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Categorization other = (Categorization) obj; + if (categorizationId != other.getCategorizationId()) { + return false; + } + if (!other.canEqual(this)) { + return false; + } + + if (!Objects.equals(category, other.getCategory())) { + return false; + } + if (!Objects.equals(categorizedObject, other.getCategorizedObject())) { + return false; + } + if (categoryOrder != other.getCategoryOrder()) { + return false; + } + return objectOrder == other.getObjectOrder(); + } + + public boolean canEqual(final Object obj) { + return obj instanceof Categorization; + } + + @Override + public String toString() { + return toString(""); + } + + public String toString(final String data) { + return String.format("%s{ " + + "categorizationId = %d, " + + "category = %s, " + + "categorizedObject = %s, " + + "categoryOrder = %d, " + + "objectOrder = %d" + + "%s }", + super.toString(), + categorizationId, + Objects.toString(category), + Objects.toString(categorizedObject), + categoryOrder, + objectOrder); + } + } diff --git a/ccm-core/src/main/java/org/libreccm/categorization/Category.java b/ccm-core/src/main/java/org/libreccm/categorization/Category.java index f431ee88b..a9b7f39ad 100644 --- a/ccm-core/src/main/java/org/libreccm/categorization/Category.java +++ b/ccm-core/src/main/java/org/libreccm/categorization/Category.java @@ -216,6 +216,10 @@ public class Category extends CcmObject implements Serializable { } @Override + @SuppressWarnings({"PMD.NPathComplexity", + "PMD.CyclomaticComplexity", + "PMD.StdCyclomaticComplexity", + "PMD.ModifiedCyclomaticComplexity"}) public boolean equals(final Object obj) { if (obj == null) { return false; diff --git a/ccm-core/src/main/java/org/libreccm/categorization/Domain.java b/ccm-core/src/main/java/org/libreccm/categorization/Domain.java index 748e02e53..573ddd1d2 100644 --- a/ccm-core/src/main/java/org/libreccm/categorization/Domain.java +++ b/ccm-core/src/main/java/org/libreccm/categorization/Domain.java @@ -122,7 +122,7 @@ public class Domain extends CcmObject implements Serializable { return version; } - public void setVersion(String version) { + public void setVersion(final String version) { this.version = version; } @@ -172,7 +172,11 @@ public class Domain extends CcmObject implements Serializable { } @Override - public boolean equals(Object obj) { + @SuppressWarnings({"PMD.NPathComplexity", + "PMD.CyclomaticComplexity", + "PMD.StdCyclomaticComplexity", + "PMD.ModifiedCyclomaticComplexity"}) + public boolean equals(final Object obj) { if (obj == null) { return false; } diff --git a/ccm-core/src/main/java/org/libreccm/categorization/DomainOwnership.java b/ccm-core/src/main/java/org/libreccm/categorization/DomainOwnership.java index 5503ceac7..15133eea3 100644 --- a/ccm-core/src/main/java/org/libreccm/categorization/DomainOwnership.java +++ b/ccm-core/src/main/java/org/libreccm/categorization/DomainOwnership.java @@ -120,6 +120,10 @@ public class DomainOwnership implements Serializable { } @Override + @SuppressWarnings({"PMD.NPathComplexity", + "PMD.CyclomaticComplexity", + "PMD.StdCyclomaticComplexity", + "PMD.ModifiedCyclomaticComplexity"}) public boolean equals(final Object obj) { if (obj == null) { return false; 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 82d6eaf4d..cb8cd4cf3 100644 --- a/ccm-core/src/main/java/org/libreccm/core/Resource.java +++ b/ccm-core/src/main/java/org/libreccm/core/Resource.java @@ -81,7 +81,7 @@ public class Resource extends CcmObject implements Serializable { return title; } - public void setTitle(LocalizedString title) { + public void setTitle(final LocalizedString title) { this.title = title; } @@ -89,7 +89,7 @@ public class Resource extends CcmObject implements Serializable { return description; } - public void setDescription(LocalizedString description) { + public void setDescription(final LocalizedString description) { this.description = description; } @@ -121,7 +121,7 @@ public class Resource extends CcmObject implements Serializable { return parent; } - protected void setParent(Resource parent) { + protected void setParent(final Resource parent) { this.parent = parent; } diff --git a/ccm-core/src/main/java/org/libreccm/l10n/LocalizedString.java b/ccm-core/src/main/java/org/libreccm/l10n/LocalizedString.java index 354419ae8..a120861ee 100644 --- a/ccm-core/src/main/java/org/libreccm/l10n/LocalizedString.java +++ b/ccm-core/src/main/java/org/libreccm/l10n/LocalizedString.java @@ -56,7 +56,7 @@ public class LocalizedString implements Serializable { */ @ElementCollection @MapKeyColumn(name = "locale") - @Column(name = "value") + @Column(name = "localized_value") @Lob @XmlElementWrapper(name = "values") @XmlElement(name = "value") diff --git a/ccm-core/src/site/site.xml b/ccm-core/src/site/site.xml new file mode 100644 index 000000000..3d12fa6cc --- /dev/null +++ b/ccm-core/src/site/site.xml @@ -0,0 +1,16 @@ + + + + + + + + org.apache.maven.skins + maven-fluido-skin + 1.3.1 + + + diff --git a/ccm-core/src/test/java/org/libreccm/jpautils/UriConverterTest.java b/ccm-core/src/test/java/org/libreccm/jpautils/UriConverterTest.java new file mode 100644 index 000000000..0809d86b6 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/jpautils/UriConverterTest.java @@ -0,0 +1,146 @@ +/* + * 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.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; + +import java.net.URI; +import java.net.URISyntaxException; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +/** + * This test suite checks the functionality of the {@link UriConverter} class + * + * @author Jens Pelzetter + */ +@Category(UnitTest.class) +public class UriConverterTest { + + private static final String WWW_EXAMPLE_ORG = "http://www.example.org"; + private static final String WWW_EXAMPLE_COM = "http://www.example.com"; + private static final String EXAMPLE_ORG_WITH_PATH2 + = "http://example.org/some/path"; + private static final String WWW_EXAMPLE_ORG_WITH_PATH + = "http://www.example.org/some/path"; + private static final String WITH_USER_AND_PORT_AND_PATH + = "http://foo:bar@example.org/api/?query=foo"; + private static final String FILE_PATH = "file:///home/foo/some/file"; + private static final String HTTP = "http"; + private static final String FILE = "file"; + + private transient UriConverter uriConverter; + + public UriConverterTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + uriConverter = new UriConverter(); + } + + @After + public void tearDown() { + uriConverter = null; + } + + /** + * Verifies that URI passed to + * {@link UriConverter#convertToDatabaseColumn(java.net.URI)} is converted + * to the expected string value. + * + * @throws URISyntaxException If one the test URIs could not be created + * (should never happen). + */ + @Test + public void verifyToDatabaseColumn() throws URISyntaxException { + final URI wwwExampleOrg = new URI(WWW_EXAMPLE_ORG); + final URI wwwExampleCom = new URI(WWW_EXAMPLE_COM); + final URI wwwExampleOrgWithPath = new URI(WWW_EXAMPLE_ORG_WITH_PATH); + final URI exampleOrgWithPath = new URI(EXAMPLE_ORG_WITH_PATH2); + final URI filePath = new URI(FILE_PATH); + final URI withUserAndPortAndPath = new URI( + WITH_USER_AND_PORT_AND_PATH); + + assertThat(uriConverter.convertToDatabaseColumn(wwwExampleOrg), + is(equalTo(WWW_EXAMPLE_ORG))); + assertThat(uriConverter.convertToDatabaseColumn(wwwExampleCom), + is(equalTo(WWW_EXAMPLE_COM))); + assertThat(uriConverter.convertToDatabaseColumn(wwwExampleOrgWithPath), + is(equalTo(WWW_EXAMPLE_ORG_WITH_PATH))); + assertThat(uriConverter.convertToDatabaseColumn(exampleOrgWithPath), + is(equalTo(EXAMPLE_ORG_WITH_PATH2))); + assertThat(uriConverter.convertToDatabaseColumn(filePath), + is(equalTo(FILE_PATH))); + assertThat(uriConverter.convertToDatabaseColumn(withUserAndPortAndPath), + is(equalTo(WITH_USER_AND_PORT_AND_PATH))); + } + + /** + * Verifies that + * {@link UriConverter#convertToEntityAttribute(java.lang.String)} + * returns the expected URI from the string passed to the method. + */ + @Test + public void verifyToEntityAttribute() { + final URI wwwExampleOrg = uriConverter.convertToEntityAttribute( + WWW_EXAMPLE_ORG); + assertThat(wwwExampleOrg, is(instanceOf(URI.class))); + assertThat(wwwExampleOrg.getScheme(), is(equalTo(HTTP))); + assertThat(wwwExampleOrg.getHost(), is(equalTo("www.example.org"))); + + final URI filePath = uriConverter.convertToEntityAttribute(FILE_PATH); + assertThat(filePath, is(instanceOf(URI.class))); + assertThat(filePath.getScheme(), is(equalTo(FILE))); + + final URI withUserAndPortAndPath = uriConverter + .convertToEntityAttribute(WITH_USER_AND_PORT_AND_PATH); + assertThat(withUserAndPortAndPath, is(instanceOf(URI.class))); + assertThat(withUserAndPortAndPath.getScheme(), is(equalTo(HTTP))); + assertThat(withUserAndPortAndPath.getHost(), is(equalTo("example.org"))); + assertThat(withUserAndPortAndPath.getUserInfo(), is(equalTo("foo:bar"))); + assertThat(withUserAndPortAndPath.getPath(), is(equalTo("/api/"))); + assertThat(withUserAndPortAndPath.getQuery(), is(equalTo("query=foo"))); + } + + /** + * Checks if + * {@link UriConverter#convertToEntityAttribute(java.lang.String)} + * throws an {@link IllegalArgumentException} for an invalid URI. + */ + @Test(expected = IllegalArgumentException.class) + public void invalidUriInDb() { + uriConverter.convertToEntityAttribute("file:///foo/b([[ar"); + } +} diff --git a/ccm-core/src/test/java/org/libreccm/tests/categories/UnitTest.java b/ccm-core/src/test/java/org/libreccm/tests/categories/UnitTest.java new file mode 100644 index 000000000..e452e15b2 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/tests/categories/UnitTest.java @@ -0,0 +1,31 @@ +/* + * 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.tests.categories; + +/** + * A marker interface for use with JUnit's {@link Category} annotation. + * + * Tests which are grouped into this category are used to test an + * individual class. All external dependencies should be mocked. + * + * @author Jens Pelzetter + */ +public interface UnitTest { + +} diff --git a/src/site/site.xml b/src/site/site.xml new file mode 100644 index 000000000..716e5103b --- /dev/null +++ b/src/site/site.xml @@ -0,0 +1,17 @@ + + + + + + + + + org.apache.maven.skins + maven-fluido-skin + 1.3.1 + + +