From 03d9045ff24f6b36f7ac7e83eacdd60916fdc3b9 Mon Sep 17 00:00:00 2001 From: Nils Christian Ehmke <nie@informatik.uni-kiel.de> Date: Tue, 28 May 2013 14:12:04 +0200 Subject: [PATCH] Modified some tests; Fixed the RoleStringConverter class in order to suit the API description; Added another FB filter and added some comments to the file. --- Kieker.WebGUI/.classpath | 7 +- Kieker.WebGUI/.project | 4 +- .../org.eclipse.core.resources.prefs | 1 + .../config/quality-config/fb-filter.xml | 30 ++- .../web/converter/RoleStringConverter.java | 17 +- .../ExceptionsExtendingAbstractClassTest.java | 10 +- .../Class2ModelInstanceConverterTest.java | 174 ++++++++++++++++++ .../util/CloseableURLClassLoaderTest.java | 102 ++++++++++ .../impl/util/PluginFinderTest.java | 26 ++- .../impl/GraphLayoutServiceImplTest.java | 13 +- .../converter/RoleStringConverterTest.java | 54 +++--- 11 files changed, 375 insertions(+), 63 deletions(-) create mode 100644 Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/Class2ModelInstanceConverterTest.java create mode 100644 Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/CloseableURLClassLoaderTest.java diff --git a/Kieker.WebGUI/.classpath b/Kieker.WebGUI/.classpath index 98730f54..658fc2e3 100644 --- a/Kieker.WebGUI/.classpath +++ b/Kieker.WebGUI/.classpath @@ -11,7 +11,12 @@ <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> - <classpathentry kind="src" path="src/test/java"/> + <classpathentry kind="src" output="target/test-classes" path="src/test/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> <attributes> <attribute name="maven.pomderived" value="true"/> diff --git a/Kieker.WebGUI/.project b/Kieker.WebGUI/.project index 5a6290a8..447a4dc6 100644 --- a/Kieker.WebGUI/.project +++ b/Kieker.WebGUI/.project @@ -21,12 +21,12 @@ </arguments> </buildCommand> <buildCommand> - <name>org.eclipse.m2e.core.maven2Builder</name> + <name>net.sf.eclipsecs.core.CheckstyleBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> - <name>net.sf.eclipsecs.core.CheckstyleBuilder</name> + <name>org.eclipse.m2e.core.maven2Builder</name> <arguments> </arguments> </buildCommand> diff --git a/Kieker.WebGUI/.settings/org.eclipse.core.resources.prefs b/Kieker.WebGUI/.settings/org.eclipse.core.resources.prefs index abdea9ac..839d647e 100644 --- a/Kieker.WebGUI/.settings/org.eclipse.core.resources.prefs +++ b/Kieker.WebGUI/.settings/org.eclipse.core.resources.prefs @@ -1,4 +1,5 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 encoding/<project>=UTF-8 diff --git a/Kieker.WebGUI/config/quality-config/fb-filter.xml b/Kieker.WebGUI/config/quality-config/fb-filter.xml index 1c7a80d4..29a2bcb7 100644 --- a/Kieker.WebGUI/config/quality-config/fb-filter.xml +++ b/Kieker.WebGUI/config/quality-config/fb-filter.xml @@ -1,11 +1,25 @@ <FindBugsFilter> - <Match> - <Class name="kieker.webgui.persistence.impl.util.CloseableURLClassLoader" /> - <Bug code="DP, RI" /> - </Match> + + <!-- This class is full of necessary hacks. --> + <Match> + <Class name="kieker.webgui.persistence.impl.util.CloseableURLClassLoader" /> + + <Bug code="DP, RI" /> + </Match> + + <!-- Ignores the fact that we don't close database connections in the DAO. The connections are automatically closed. --> + <Match> + <Class name="kieker.webgui.persistence.impl.DerbyUserDAOImpl" /> + + <Bug code="ODR, OBL" /> + </Match> + + <!-- Ignores the fact that we ignore the return values of some delete methods. --> + <Match> + <Class name="kieker.webgui.persistence.impl.FSProjectDAOImpl" /> + <Method name="addProject" params="java.lang.String, java.lang.String" returns="void" /> + + <Bug code="RV" /> + </Match> - <Match> - <Class name="kieker.webgui.persistence.impl.DerbyUserDAOImpl" /> - <Bug code="ODR, OBL" /> - </Match> </FindBugsFilter> \ No newline at end of file diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/web/converter/RoleStringConverter.java b/Kieker.WebGUI/src/main/java/kieker/webgui/web/converter/RoleStringConverter.java index 4e993f5a..b42f5a9c 100644 --- a/Kieker.WebGUI/src/main/java/kieker/webgui/web/converter/RoleStringConverter.java +++ b/Kieker.WebGUI/src/main/java/kieker/webgui/web/converter/RoleStringConverter.java @@ -19,6 +19,7 @@ package kieker.webgui.web.converter; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; +import javax.faces.convert.ConverterException; import javax.faces.convert.FacesConverter; import kieker.webgui.domain.Role; @@ -40,15 +41,25 @@ public class RoleStringConverter implements Converter { @Override public Object getAsObject(final FacesContext context, final UIComponent comp, final String str) { - return Role.valueOf(str); + if (str == null) { + return null; + } else { + try { + return Role.valueOf(str); + } catch (final IllegalArgumentException ex) { + throw new ConverterException("The given string is not a valid role", ex); + } + } } @Override public String getAsString(final FacesContext context, final UIComponent comp, final Object obj) { - if (obj instanceof Role) { + if (obj == null) { + return ""; + } else if (obj instanceof Role) { return ((Role) obj).toString(); } else { - return ""; + throw new ConverterException("The class of the given object is invalid"); } } diff --git a/Kieker.WebGUI/src/test/java/kieker/webgui/common/ExceptionsExtendingAbstractClassTest.java b/Kieker.WebGUI/src/test/java/kieker/webgui/common/ExceptionsExtendingAbstractClassTest.java index b309585f..a329c8e6 100644 --- a/Kieker.WebGUI/src/test/java/kieker/webgui/common/ExceptionsExtendingAbstractClassTest.java +++ b/Kieker.WebGUI/src/test/java/kieker/webgui/common/ExceptionsExtendingAbstractClassTest.java @@ -74,12 +74,12 @@ public class ExceptionsExtendingAbstractClassTest { } private static Collection<File> listJavaSourceFiles() { - final IOFileFilter filter = new RegexFileFilter(ExceptionsExtendingAbstractClassTest.PATTERN_EXCEPTION_SOURCE_FILES); - return FileUtils.listFiles(new File(ExceptionsExtendingAbstractClassTest.DIR_NAME_SOURCES), filter, TrueFileFilter.INSTANCE); + final IOFileFilter filter = new RegexFileFilter(PATTERN_EXCEPTION_SOURCE_FILES); + return FileUtils.listFiles(new File(DIR_NAME_SOURCES), filter, TrueFileFilter.INSTANCE); } private static boolean isSourceFileInExceptionPackage(final File file) { - return file.getAbsolutePath().contains(ExceptionsExtendingAbstractClassTest.PATTERN_EXCEPTION_PACKAGE); + return file.getAbsolutePath().contains(PATTERN_EXCEPTION_PACKAGE); } private static boolean doesClassExtendAbstractKiekerWebGUIException(final Class<?> clazz) { @@ -90,8 +90,8 @@ public class ExceptionsExtendingAbstractClassTest { final String pathName = file.getPath(); String className = pathName.replace("\\", "."); - className = className.substring(ExceptionsExtendingAbstractClassTest.UNNECESSARY_PREFIX.length()); - className = className.substring(0, className.length() - ExceptionsExtendingAbstractClassTest.UNNECESSARY_SUFFIX.length()); + className = className.substring(UNNECESSARY_PREFIX.length()); + className = className.substring(0, className.length() - UNNECESSARY_SUFFIX.length()); return className; } diff --git a/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/Class2ModelInstanceConverterTest.java b/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/Class2ModelInstanceConverterTest.java new file mode 100644 index 00000000..f7193a9e --- /dev/null +++ b/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/Class2ModelInstanceConverterTest.java @@ -0,0 +1,174 @@ +/*************************************************************************** + * Copyright 2012 Kieker Project (http://kieker-monitoring.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ***************************************************************************/ + +package kieker.webgui.persistence.impl.util; + +import java.io.IOException; +import java.net.URL; +import java.util.Collection; + +import org.junit.Assert; +import org.junit.Test; + +import kieker.analysis.model.analysisMetaModel.MIFilter; +import kieker.analysis.model.analysisMetaModel.MIReader; +import kieker.analysis.model.analysisMetaModel.MIRepository; +import kieker.analysis.plugin.filter.forward.CountingFilter; +import kieker.analysis.plugin.reader.timer.TimeReader; +import kieker.tools.traceAnalysis.systemModel.repository.SystemModelRepository; +import kieker.webgui.common.ClassAndMethodContainer; +import kieker.webgui.common.exception.ReflectionException; + +/** + * Test class for {@link Class2ModelInstanceConverter}. + * + * @author Nils Christian Ehmke + */ +public class Class2ModelInstanceConverterTest { + + private static final String KIEKER_LIB = "kieker.jar"; + + /** + * Default constructor. <b>Do not use this constructor. This is just a test class and not to be used outside a JUnit test!</b> + */ + public Class2ModelInstanceConverterTest() { + // No code necessary + } + + /** + * A test which loads dynamically a reader from the Kieker library and checks the converting. + * + * @throws ReflectionException + * If something went wrong. + * @throws IOException + * If something went wrong. + */ + @Test + public void testReaderConverting() throws ReflectionException, IOException { + final PluginFinder pluginFinder = new PluginFinder(); + final URL kiekerURL = Thread.currentThread().getContextClassLoader().getResource(KIEKER_LIB); + final CloseableURLClassLoader classLoader = new CloseableURLClassLoader(new URL[] { kiekerURL }, null); + final ClassAndMethodContainer classAndMethodContainer = new ClassAndMethodContainer(classLoader); + final Class2ModelInstanceConverter converter = new Class2ModelInstanceConverter(); + + final Collection<Class<?>> readers = pluginFinder.getAllReadersWithinJar(kiekerURL, classLoader, classAndMethodContainer); + + // Find the class of the TimeReader + Class<?> timeReaderClass = null; + for (final Class<?> reader : readers) { + if (TimeReader.class.getCanonicalName().equals(reader.getCanonicalName())) { + timeReaderClass = reader; + break; + } + } + if (timeReaderClass == null) { + Assert.fail("TimeReader not available"); + } + + // Convert and check the reader + final MIReader reader = converter.convertReaderClass2ModelInstance(timeReaderClass, classAndMethodContainer); + + Assert.assertEquals("Properties are not loaded correctly", timeReaderClass.getCanonicalName(), reader.getClassname()); + Assert.assertTrue("Properties are not loaded correctly", reader.getDisplays().isEmpty()); + Assert.assertTrue("Properties are not loaded correctly", reader.getRepositories().isEmpty()); + Assert.assertEquals("Properties are not loaded correctly", 2, reader.getOutputPorts().size()); + Assert.assertEquals("Properties are not loaded correctly", 3, reader.getProperties().size()); + + classLoader.close(); + } + + /** + * A test which loads dynamically a filter from the Kieker library and checks the converting. + * + * @throws ReflectionException + * If something went wrong. + * @throws IOException + * If something went wrong. + */ + @Test + public void testFilterConverting() throws ReflectionException, IOException { + final PluginFinder pluginFinder = new PluginFinder(); + final URL kiekerURL = Thread.currentThread().getContextClassLoader().getResource(KIEKER_LIB); + final CloseableURLClassLoader classLoader = new CloseableURLClassLoader(new URL[] { kiekerURL }, null); + final ClassAndMethodContainer classAndMethodContainer = new ClassAndMethodContainer(classLoader); + final Class2ModelInstanceConverter converter = new Class2ModelInstanceConverter(); + + final Collection<Class<?>> filters = pluginFinder.getAllFiltersWithinJar(kiekerURL, classLoader, classAndMethodContainer); + + // Find the class of the CountingFilter + Class<?> countingFilterCLass = null; + for (final Class<?> filter : filters) { + if (CountingFilter.class.getCanonicalName().equals(filter.getCanonicalName())) { + countingFilterCLass = filter; + break; + } + } + if (countingFilterCLass == null) { + Assert.fail("CountingFilter not available"); + } + + // Convert and check the reader + final MIFilter filter = converter.convertFilterClass2ModelInstance(countingFilterCLass, classAndMethodContainer); + + Assert.assertEquals("Properties are not loaded correctly", countingFilterCLass.getCanonicalName(), filter.getClassname()); + Assert.assertEquals("Properties are not loaded correctly", 3, filter.getDisplays().size()); + Assert.assertTrue("Properties are not loaded correctly", filter.getRepositories().isEmpty()); + Assert.assertEquals("Properties are not loaded correctly", 2, filter.getOutputPorts().size()); + Assert.assertEquals("Properties are not loaded correctly", 1, filter.getInputPorts().size()); + Assert.assertTrue("Properties are not loaded correctly", filter.getProperties().isEmpty()); + + classLoader.close(); + } + + /** + * A test which loads dynamically a repository from the Kieker library and checks the converting. + * + * @throws ReflectionException + * If something went wrong. + * @throws IOException + * If something went wrong. + */ + @Test + public void testRepositoryConverting() throws ReflectionException, IOException { + final PluginFinder pluginFinder = new PluginFinder(); + final URL kiekerURL = Thread.currentThread().getContextClassLoader().getResource(KIEKER_LIB); + final CloseableURLClassLoader classLoader = new CloseableURLClassLoader(new URL[] { kiekerURL }, null); + final ClassAndMethodContainer classAndMethodContainer = new ClassAndMethodContainer(classLoader); + final Class2ModelInstanceConverter converter = new Class2ModelInstanceConverter(); + + final Collection<Class<?>> repositories = pluginFinder.getAllRepositoriesWithinJar(kiekerURL, classLoader, classAndMethodContainer); + + // Find the class of the SystemModelRepository + Class<?> systemModelRepository = null; + for (final Class<?> repository : repositories) { + if (SystemModelRepository.class.getCanonicalName().equals(repository.getCanonicalName())) { + systemModelRepository = repository; + break; + } + } + if (systemModelRepository == null) { + Assert.fail("SystemModelRepository not available"); + } + + // Convert and check the reader + final MIRepository repository = converter.convertRepositoryClass2ModelInstance(systemModelRepository, classAndMethodContainer); + + Assert.assertEquals("Properties are not loaded correctly", systemModelRepository.getCanonicalName(), repository.getClassname()); + Assert.assertTrue("Properties are not loaded correctly", repository.getProperties().isEmpty()); + + classLoader.close(); + } +} diff --git a/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/CloseableURLClassLoaderTest.java b/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/CloseableURLClassLoaderTest.java new file mode 100644 index 00000000..b855cb78 --- /dev/null +++ b/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/CloseableURLClassLoaderTest.java @@ -0,0 +1,102 @@ +/*************************************************************************** + * Copyright 2012 Kieker Project (http://kieker-monitoring.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ***************************************************************************/ + +package kieker.webgui.persistence.impl.util; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; + +import org.junit.Assert; +import org.junit.Test; + +import com.google.common.io.Files; + +import kieker.analysis.AnalysisController; + +/** + * Test class for {@link CloseableURLClassLoader}. + * + * @author Nils Christian Ehmke + */ +public class CloseableURLClassLoaderTest { + + private static final String KIEKER_LIB = "kieker.jar"; + + /** + * Default constructor. <b>Do not use this constructor. This is just a test class and not to be used outside a JUnit test!</b> + */ + public CloseableURLClassLoaderTest() { + // No code necessary + } + + /** + * A test making sure that the class loader loads classes correctly. + * + * @throws URISyntaxException + * If something went wrong. + * @throws IOException + * If something went wrong. + */ + @Test + public void testClassLoading() throws IOException, URISyntaxException { + // Copy the kieker jar into a temporary directory + final URL kiekerURL = Thread.currentThread().getContextClassLoader().getResource(KIEKER_LIB); + final File tempDir = Files.createTempDir(); + final File newJar = new File(tempDir, KIEKER_LIB); + + Files.copy(new File(kiekerURL.toURI()), newJar); + + // Create the loader + final CloseableURLClassLoader classLoader = new CloseableURLClassLoader(new URL[] { newJar.toURI().toURL() }, null); + + // It should now be possible to load one of Kieker's classes + try { + classLoader.loadClass(AnalysisController.class.getCanonicalName()); + } catch (final ClassNotFoundException ex) { + Assert.fail("Could not load class"); + } + + classLoader.close(); + + } + + /** + * A test making sure that class loader can be closed. + * + * @throws URISyntaxException + * If something went wrong. + * @throws IOException + * If something went wrong. + */ + @Test + public void testClosing() throws IOException, URISyntaxException { + // Copy the kieker jar into a temporary directory + final URL kiekerURL = Thread.currentThread().getContextClassLoader().getResource(KIEKER_LIB); + final File tempDir = Files.createTempDir(); + final File newJar = new File(tempDir, KIEKER_LIB); + + Files.copy(new File(kiekerURL.toURI()), newJar); + + // Create and close the class loader + final CloseableURLClassLoader classLoader = new CloseableURLClassLoader(new URL[] { newJar.toURI().toURL() }, null); + classLoader.close(); + + // It should be possible to delete the file now. + Assert.assertTrue("Classloader does not close correctly", newJar.delete()); + } +} diff --git a/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/PluginFinderTest.java b/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/PluginFinderTest.java index f7d506e0..cd9186a3 100644 --- a/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/PluginFinderTest.java +++ b/Kieker.WebGUI/src/test/java/kieker/webgui/persistence/impl/util/PluginFinderTest.java @@ -43,32 +43,30 @@ public class PluginFinderTest { } /** - * A test of this test class. + * A test making sure that the kieker plugins are available. * * @throws ReflectionException * If something went wrong. + * @throws IOException + * If something went wrong. */ @Test - public void testKiekerPlugins() throws ReflectionException { + public void testKiekerPlugins() throws ReflectionException, IOException { final PluginFinder pluginFinder = new PluginFinder(); final URL kiekerURL = Thread.currentThread().getContextClassLoader().getResource(PluginFinderTest.KIEKER_LIB); final CloseableURLClassLoader classLoader = new CloseableURLClassLoader(new URL[] { kiekerURL }, null); final ClassAndMethodContainer classAndMethodContainer = new ClassAndMethodContainer(classLoader); - try { - final Collection<Class<?>> filters = pluginFinder.getAllFiltersWithinJar(kiekerURL, classLoader, classAndMethodContainer); - final Collection<Class<?>> readers = pluginFinder.getAllReadersWithinJar(kiekerURL, classLoader, classAndMethodContainer); - final Collection<Class<?>> repositories = pluginFinder.getAllRepositoriesWithinJar(kiekerURL, classLoader, classAndMethodContainer); + final Collection<Class<?>> filters = pluginFinder.getAllFiltersWithinJar(kiekerURL, classLoader, classAndMethodContainer); + final Collection<Class<?>> readers = pluginFinder.getAllReadersWithinJar(kiekerURL, classLoader, classAndMethodContainer); + final Collection<Class<?>> repositories = pluginFinder.getAllRepositoriesWithinJar(kiekerURL, classLoader, classAndMethodContainer); - // There should be at least one element of both types - Assert.assertFalse("No filters found.", filters.isEmpty()); - Assert.assertFalse("No readers found.", readers.isEmpty()); - Assert.assertFalse("No repositories found.", repositories.isEmpty()); + // There should be at least one element of both types + Assert.assertFalse("No filters found.", filters.isEmpty()); + Assert.assertFalse("No readers found.", readers.isEmpty()); + Assert.assertFalse("No repositories found.", repositories.isEmpty()); - classLoader.close(); - } catch (final IOException ex) { - Assert.fail("An exception occured while testing the existence of plugins and repositories."); - } + classLoader.close(); } } diff --git a/Kieker.WebGUI/src/test/java/kieker/webgui/service/impl/GraphLayoutServiceImplTest.java b/Kieker.WebGUI/src/test/java/kieker/webgui/service/impl/GraphLayoutServiceImplTest.java index c77e60bb..975c785b 100644 --- a/Kieker.WebGUI/src/test/java/kieker/webgui/service/impl/GraphLayoutServiceImplTest.java +++ b/Kieker.WebGUI/src/test/java/kieker/webgui/service/impl/GraphLayoutServiceImplTest.java @@ -37,17 +37,16 @@ public class GraphLayoutServiceImplTest { } /** - * A test making sure that a invalid node/edge combination results in an exceptions. + * A test making sure that an invalid node/edge combination results in an exceptions. */ @Test public void testLayoutFail() { final IGraphLayoutService layouter = new GraphLayoutServiceImpl(); - // We assert that something goes wrong here + try { layouter.layoutGraph("", ""); - Assert.fail(); - } catch (final GraphLayoutException ex) { // NOPMD (Empty catch block) - + Assert.fail("Graph layouting failed"); + } catch (final GraphLayoutException ex) { // NOPMD (JUnit Test) } } @@ -66,7 +65,7 @@ public class GraphLayoutServiceImplTest { // We assert that the layout method succeeds final String layoutString = layouter.layoutGraph(inputNodes, inputEdges); - Assert.assertTrue(layoutString.startsWith("autoLayout#")); + Assert.assertTrue("Graph layouting failed", layoutString.startsWith("autoLayout#")); } /** @@ -84,6 +83,6 @@ public class GraphLayoutServiceImplTest { // We assert that the layout method succeeds final String layoutString = layouter.layoutGraph(inputNodes, inputEdges); - Assert.assertTrue(layoutString.startsWith("autoLayout#")); + Assert.assertTrue("Graph layouting failed", layoutString.startsWith("autoLayout#")); } } diff --git a/Kieker.WebGUI/src/test/java/kieker/webgui/web/converter/RoleStringConverterTest.java b/Kieker.WebGUI/src/test/java/kieker/webgui/web/converter/RoleStringConverterTest.java index a778e9e2..fe46dd69 100644 --- a/Kieker.WebGUI/src/test/java/kieker/webgui/web/converter/RoleStringConverterTest.java +++ b/Kieker.WebGUI/src/test/java/kieker/webgui/web/converter/RoleStringConverterTest.java @@ -16,6 +16,8 @@ package kieker.webgui.web.converter; +import javax.faces.convert.ConverterException; + import org.junit.Assert; import org.junit.Test; @@ -36,7 +38,7 @@ public class RoleStringConverterTest { } /** - * A test of this test class. + * A test making sure that the {@code getAsString} method works correctly. */ @Test public void testEqualAsString() { @@ -48,48 +50,54 @@ public class RoleStringConverterTest { } /** - * A test of this test class. + * A test making sure that the {@code getAsObject} method works correctly. */ @Test - public void testNotEqualAsString() { + public void testEqualFromString() { final RoleStringConverter converter = new RoleStringConverter(); - Assert.assertNotSame("Conversion failed.", Role.ROLE_ADMIN, converter.getAsString(null, null, Role.ROLE_USER)); - Assert.assertNotSame("Conversion failed.", Role.ROLE_ADMIN, converter.getAsString(null, null, Role.ROLE_GUEST)); + Assert.assertEquals("Conversion failed.", Role.ROLE_ADMIN, converter.getAsObject(null, null, Role.ROLE_ADMIN.toString())); + Assert.assertEquals("Conversion failed.", Role.ROLE_GUEST, converter.getAsObject(null, null, Role.ROLE_GUEST.toString())); + Assert.assertEquals("Conversion failed.", Role.ROLE_USER, converter.getAsObject(null, null, Role.ROLE_USER.toString())); + } - Assert.assertNotSame("Conversion failed.", Role.ROLE_GUEST, converter.getAsString(null, null, Role.ROLE_ADMIN)); - Assert.assertNotSame("Conversion failed.", Role.ROLE_GUEST, converter.getAsString(null, null, Role.ROLE_USER)); + /** + * A test making sure that the methods of the converter handle null values correctly. This is specified by the API of the converter interface. + */ + @Test + public void testNullHandling() { + final RoleStringConverter converter = new RoleStringConverter(); - Assert.assertNotSame("Conversion failed.", Role.ROLE_USER, converter.getAsString(null, null, Role.ROLE_GUEST)); - Assert.assertNotSame("Conversion failed.", Role.ROLE_USER, converter.getAsString(null, null, Role.ROLE_ADMIN)); + Assert.assertNull("Conversion failed.", converter.getAsObject(null, null, null)); + Assert.assertTrue("Conversion failed.", converter.getAsString(null, null, null).isEmpty()); } /** - * A test of this test class. + * A test making sure that the {@code getAsObject} method handles invalid values correctly. This is specified by the API of the converter interface. */ @Test - public void testEqualFromString() { + public void testInvalidString() { final RoleStringConverter converter = new RoleStringConverter(); - Assert.assertEquals("Conversion failed.", Role.ROLE_ADMIN, converter.getAsObject(null, null, Role.ROLE_ADMIN.toString())); - Assert.assertEquals("Conversion failed.", Role.ROLE_GUEST, converter.getAsObject(null, null, Role.ROLE_GUEST.toString())); - Assert.assertEquals("Conversion failed.", Role.ROLE_USER, converter.getAsObject(null, null, Role.ROLE_USER.toString())); + try { + converter.getAsObject(null, null, ""); + Assert.fail("Conversion failed."); + } catch (final ConverterException ex) { // NOPMD (JUnit Test) + } } /** - * A test of this test class. + * A test making sure that the {@code getAsString} method handles invalid values correctly. This is specified by the API of the converter interface. */ @Test - public void testNotEqualFromString() { + public void testInvalidObject() { final RoleStringConverter converter = new RoleStringConverter(); - Assert.assertNotSame("Conversion failed.", Role.ROLE_ADMIN, converter.getAsObject(null, null, Role.ROLE_USER.toString())); - Assert.assertNotSame("Conversion failed.", Role.ROLE_ADMIN, converter.getAsObject(null, null, Role.ROLE_GUEST.toString())); - - Assert.assertNotSame("Conversion failed.", Role.ROLE_GUEST, converter.getAsObject(null, null, Role.ROLE_ADMIN.toString())); - Assert.assertNotSame("Conversion failed.", Role.ROLE_GUEST, converter.getAsObject(null, null, Role.ROLE_USER.toString())); + try { + converter.getAsString(null, null, new Object()); + Assert.fail("Conversion failed."); + } catch (final ConverterException ex) { // NOPMD (JUnit Test) + } - Assert.assertNotSame("Conversion failed.", Role.ROLE_USER, converter.getAsObject(null, null, Role.ROLE_GUEST.toString())); - Assert.assertNotSame("Conversion failed.", Role.ROLE_USER, converter.getAsObject(null, null, Role.ROLE_ADMIN.toString())); } } -- GitLab