From f47bbb0d9e8023590c0fd965acb009719aea6b67 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 12 Aug 2015 19:35:45 +0200 Subject: [PATCH] Polish tests --- .../serviceloader/ServiceLoaderTests.java | 13 ++----- .../annotation/JmxUtilsAnnotationTests.java | 17 +++----- ...hMatchingResourcePatternResolverTests.java | 39 ++++++++----------- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java index 3311b6d634..b471059aeb 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java @@ -26,6 +26,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; import static org.junit.Assert.*; +import static org.junit.Assume.*; /** * @author Juergen Hoeller @@ -35,9 +36,7 @@ public class ServiceLoaderTests { @Test public void testServiceLoaderFactoryBean() { - if (!ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()){ - return; - } + assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceLoaderFactoryBean.class); @@ -49,9 +48,7 @@ public class ServiceLoaderTests { @Test public void testServiceFactoryBean() { - if (!ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()){ - return; - } + assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceFactoryBean.class); @@ -62,9 +59,7 @@ public class ServiceLoaderTests { @Test public void testServiceListFactoryBean() { - if (!ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()){ - return; - } + assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceListFactoryBean.class); diff --git a/spring-context/src/test/java/org/springframework/jmx/export/annotation/JmxUtilsAnnotationTests.java b/spring-context/src/test/java/org/springframework/jmx/export/annotation/JmxUtilsAnnotationTests.java index a28adc857f..75052e84d6 100644 --- a/spring-context/src/test/java/org/springframework/jmx/export/annotation/JmxUtilsAnnotationTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/export/annotation/JmxUtilsAnnotationTests.java @@ -30,25 +30,21 @@ import static org.junit.Assert.*; public class JmxUtilsAnnotationTests { @Test - public void testNotMXBean() throws Exception { - FooNotX foo = new FooNotX(); - assertFalse("MXBean annotation not detected correctly", JmxUtils.isMBean(foo.getClass())); + public void notMXBean() throws Exception { + assertFalse("MXBean annotation not detected correctly", JmxUtils.isMBean(FooNotX.class)); } - @Test - public void testAnnotatedMXBean() throws Exception { - FooX foo = new FooX(); - assertTrue("MXBean annotation not detected correctly", JmxUtils.isMBean(foo.getClass())); + @Test + public void annotatedMXBean() throws Exception { + assertTrue("MXBean annotation not detected correctly", JmxUtils.isMBean(FooX.class)); } @MXBean(false) public interface FooNotMXBean { - String getName(); } - public static class FooNotX implements FooNotMXBean { @Override @@ -57,14 +53,11 @@ public class JmxUtilsAnnotationTests { } } - @MXBean(true) public interface FooIfc { - String getName(); } - public static class FooX implements FooIfc { @Override diff --git a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java index 5f5eba4f24..b53cefa1d8 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java @@ -19,6 +19,7 @@ package org.springframework.core.io.support; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.junit.Ignore; @@ -30,12 +31,13 @@ import org.springframework.util.StringUtils; import static org.junit.Assert.*; /** - * If this test case fails, uncomment diagnostics in - * {@code assertProtocolAndFilenames} method. + * If this test case fails, uncomment diagnostics in the + * {@link #assertProtocolAndFilenames} method. * * @author Oliver Hutchison * @author Juergen Hoeller * @author Chris Beams + * @author Sam Brannen * @since 17.11.2004 */ public class PathMatchingResourcePatternResolverTests { @@ -57,19 +59,13 @@ public class PathMatchingResourcePatternResolverTests { private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); - @Test - public void testInvalidPrefixWithPatternElementInIt() throws IOException { - try { - resolver.getResources("xx**:**/*.xy"); - fail("Should have thrown FileNotFoundException"); - } - catch (FileNotFoundException ex) { - // expected - } + @Test(expected = FileNotFoundException.class) + public void invalidPrefixWithPatternElementInIt() throws IOException { + resolver.getResources("xx**:**/*.xy"); } @Test - public void testSingleResourceOnFileSystem() throws IOException { + public void singleResourceOnFileSystem() throws IOException { Resource[] resources = resolver.getResources("org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class"); assertEquals(1, resources.length); @@ -77,7 +73,7 @@ public class PathMatchingResourcePatternResolverTests { } @Test - public void testSingleResourceInJar() throws IOException { + public void singleResourceInJar() throws IOException { Resource[] resources = resolver.getResources("java/net/URL.class"); assertEquals(1, resources.length); assertProtocolAndFilenames(resources, "jar", "URL.class"); @@ -85,7 +81,7 @@ public class PathMatchingResourcePatternResolverTests { @Ignore // passes under Eclipse, fails under Ant @Test - public void testClasspathStarWithPatternOnFileSystem() throws IOException { + public void classpathStarWithPatternOnFileSystem() throws IOException { Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class"); // Have to exclude Clover-generated class files here, // as we might be running as part of a Clover test run. @@ -101,19 +97,19 @@ public class PathMatchingResourcePatternResolverTests { } @Test - public void testClasspathWithPatternInJar() throws IOException { + public void classpathWithPatternInJar() throws IOException { Resource[] resources = resolver.getResources("classpath:org/apache/commons/logging/*.class"); assertProtocolAndFilenames(resources, "jar", CLASSES_IN_COMMONSLOGGING); } @Test - public void testClasspathStartWithPatternInJar() throws IOException { + public void classpathStartWithPatternInJar() throws IOException { Resource[] resources = resolver.getResources("classpath*:org/apache/commons/logging/*.class"); assertProtocolAndFilenames(resources, "jar", CLASSES_IN_COMMONSLOGGING); } @Test - public void testRootPatternRetrievalInJarFiles() throws IOException { + public void rootPatternRetrievalInJarFiles() throws IOException { Resource[] resources = resolver.getResources("classpath*:*.dtd"); boolean found = false; for (Resource resource : resources) { @@ -156,12 +152,9 @@ public class PathMatchingResourcePatternResolverTests { } private void assertFilenameIn(Resource resource, String... filenames) { - for (String filename : filenames) { - if (resource.getFilename().endsWith(filename)) { - return; - } - } - fail(resource + " does not have a filename that matches any of the specified names"); + String filename = resource.getFilename(); + assertTrue(resource + " does not have a filename that matches any of the specified names", + Arrays.stream(filenames).anyMatch(filename::endsWith)); } } -- GitLab