CCM NG: Tested #equals, #hashCode and #toString of new KernelConfig class

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3787 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-01-12 11:06:04 +00:00
parent cdc61894c6
commit c024ac5593
4 changed files with 174 additions and 28 deletions

View File

@ -22,9 +22,12 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.libreccm.configuration.Configuration;
import org.libreccm.configuration.Setting;
import java.util.StringJoiner;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
@ -56,11 +59,13 @@ public final class KernelConfig {
@Setting(descKey = "kernel.config.supported_languages")
private Set<String> supportedLanguages = new HashSet<>(
Arrays.asList(new String[]{"en"}));
Arrays.asList(new String[]{"en"}));
@Setting(descKey = "kernel.config.default_language")
private String defaultLanguage = "en";
public KernelConfig() {
super();
}
public boolean isDebugEnabled() {
@ -84,7 +89,7 @@ public final class KernelConfig {
}
public void setDataPermissionCheckEnabled(
final boolean dataPermissionCheckEnabled) {
final boolean dataPermissionCheckEnabled) {
this.dataPermissionCheckEnabled = dataPermissionCheckEnabled;
}
@ -94,12 +99,12 @@ public final class KernelConfig {
public void setPrimaryUserIdentifier(final String primaryUserIdentifier) {
if ("screen_name".equals(primaryUserIdentifier)
|| "email".equals(primaryUserIdentifier)) {
|| "email".equals(primaryUserIdentifier)) {
this.primaryUserIdentifier = primaryUserIdentifier;
} else {
throw new IllegalArgumentException(
"Primary user identifier can only be \"screen_name\" or "
+ "\"email\"");
"Primary user identifier can only be \"screen_name\" or "
+ "\"email\"");
}
}
@ -128,13 +133,46 @@ public final class KernelConfig {
}
public Set<String> getSupportedLanguages() {
return supportedLanguages;
if (supportedLanguages == null) {
return null;
} else {
return new HashSet<>(supportedLanguages);
}
}
public void setSupportedLanguages(final Set<String> supportedLanguages) {
this.supportedLanguages = supportedLanguages;
}
public void addSupportedLanguage(final String language) {
if (language == null) {
throw new IllegalArgumentException("Language can't be null.");
}
supportedLanguages.add(language);
}
public void removeSupportedLanguage(final String language) {
supportedLanguages.remove(language);
}
public String getDefaultLanguage() {
return defaultLanguage;
}
public void setDefaultLanguage(final String defaultLanguage) {
if (defaultLanguage == null) {
throw new IllegalArgumentException("Default language can't be null");
}
if (!supportedLanguages.contains(defaultLanguage)) {
throw new IllegalArgumentException(
"Default language must be one of the supported languages");
}
this.defaultLanguage = defaultLanguage;
}
@Override
public int hashCode() {
int hash = 7;
@ -146,6 +184,7 @@ public final class KernelConfig {
hash = 61 * hash + (rememberLoginEnabled ? 1 : 0);
hash = 61 * hash + (secureLoginEnabled ? 1 : 0);
hash = 61 * hash + Objects.hashCode(supportedLanguages);
hash = 61 * hash + Objects.hashCode(defaultLanguage);
return hash;
}
@ -161,46 +200,53 @@ public final class KernelConfig {
return false;
}
final KernelConfig other = (KernelConfig) obj;
if (debugEnabled != other.debugEnabled) {
if (debugEnabled != other.isDebugEnabled()) {
return false;
}
if (webdevSupportEnabled != other.webdevSupportEnabled) {
if (webdevSupportEnabled != other.isWebdevSupportEnabled()) {
return false;
}
if (dataPermissionCheckEnabled != other.dataPermissionCheckEnabled) {
if (dataPermissionCheckEnabled != other.isDataPermissionCheckEnabled()) {
return false;
}
if (ssoEnabled != other.ssoEnabled) {
if (ssoEnabled != other.isSsoEnabled()) {
return false;
}
if (rememberLoginEnabled != other.rememberLoginEnabled) {
if (rememberLoginEnabled != other.isRememberLoginEnabled()) {
return false;
}
if (secureLoginEnabled != other.secureLoginEnabled) {
if (secureLoginEnabled != other.isSecureLoginEnabled()) {
return false;
}
if (!Objects.equals(primaryUserIdentifier,
other.primaryUserIdentifier)) {
other.getPrimaryUserIdentifier())) {
return false;
}
return Objects.equals(supportedLanguages, other.supportedLanguages);
if (!Objects.equals(supportedLanguages, other.getSupportedLanguages())) {
return false;
}
return Objects.equals(defaultLanguage, other.getDefaultLanguage());
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
supportedLanguages.forEach(s -> builder.append(s));
final StringJoiner joiner = new StringJoiner(",");
if (supportedLanguages != null) {
supportedLanguages.forEach(s -> joiner.add(s));
}
return String.format("%s{ "
+ "debugEnabled = %b, "
+ "webdevSupportEnabled = %b, "
+ "dataPermissionCheckEnabled = %b, "
+ "primaryUserIdentifier = \"%s\", "
+ "ssoEnabled = %b, "
+ "rememberLoginEnabeled = %b, "
+ "secureLoginEnabled = %b, "
+ "supportedLanguages = \"%s\""
+ " }",
+ "debugEnabled = %b, "
+ "webdevSupportEnabled = %b, "
+ "dataPermissionCheckEnabled = %b, "
+ "primaryUserIdentifier = \"%s\", "
+ "ssoEnabled = %b, "
+ "rememberLoginEnabeled = %b, "
+ "secureLoginEnabled = %b, "
+ "supportedLanguages = \"%s\", "
+ "defaultLanguage = \"%s\""
+ " }",
super.toString(),
debugEnabled,
webdevSupportEnabled,
@ -209,6 +255,8 @@ public final class KernelConfig {
ssoEnabled,
rememberLoginEnabled,
secureLoginEnabled,
builder.toString());
joiner.toString(),
defaultLanguage);
}
}

View File

@ -22,6 +22,7 @@ import static org.libreccm.core.CoreConstants.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;
@ -55,7 +56,7 @@ public class EnumSetting
if (value == null) {
return null;
} else {
return value;
return new HashSet<>(value);
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2016 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 com.arsdigita.kernel;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.libreccm.tests.categories.UnitTest;
import org.libreccm.testutils.EqualsVerifier;
import java.util.Arrays;
import java.util.Collection;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RunWith(Parameterized.class)
@Category(UnitTest.class)
public class EqualsAndHashCodeTest extends EqualsVerifier {
@Parameterized.Parameters(name = "{0}")
public static Collection<Class<?>> data() {
return Arrays.asList(new Class<?>[] {
KernelConfig.class
});
}
public EqualsAndHashCodeTest(final Class<?> entityClass) {
super(entityClass);
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (C) 2016 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 com.arsdigita.kernel;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.libreccm.tests.categories.UnitTest;
import org.libreccm.testutils.ToStringVerifier;
import java.util.Arrays;
import java.util.Collection;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RunWith(Parameterized.class)
@Category(UnitTest.class)
public class ToStringTest extends ToStringVerifier {
@Parameterized.Parameters(name = "{0}")
public static Collection<Class<?>> data() {
return Arrays.asList(new Class<?>[]{
KernelConfig.class
});
}
public ToStringTest(final Class<?> entityClass) {
super(entityClass);
}
}