Make tests pass on JDK 16 and 17 (#635)

* make tests pass on JDK 16 and 17
replace deprecated mockito-all by mockito-core, and updated to latest 3.x
replace deprecated org.mockito.Matchers

* code cleanup from IDE suggestions

* add oracle jdk 16 and 17 to Travis
This commit is contained in:
Gauthier
2021-10-27 00:38:33 +08:00
committed by GitHub
parent 16d0af357d
commit 5617b4323c
20 changed files with 84 additions and 93 deletions

View File

@@ -73,8 +73,8 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -176,8 +176,8 @@ public abstract class FilterAbstractTest extends ObjectAbstractTest {
private final Map<String, String> params;
MockServletContext() {
attributes = new HashMap<String, Object>();
params = new HashMap<String, String>();
attributes = new HashMap<>();
params = new HashMap<>();
}
public Object getAttribute(String s) {
@@ -193,7 +193,7 @@ public abstract class FilterAbstractTest extends ObjectAbstractTest {
}
public String getInitParameter(String s) {
return (String) params.get(s);
return params.get(s);
}
public Enumeration getInitParameterNames() {

View File

@@ -69,7 +69,7 @@ public final class GenericFilterTest extends FilterAbstractTest {
// TRUE
filter = new GenericFilterImpl();
Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
params.put("once-per-request", "true");
try {
@@ -84,7 +84,7 @@ public final class GenericFilterTest extends FilterAbstractTest {
// TRUE
filter = new GenericFilterImpl();
params = new HashMap<String, String>();
params = new HashMap<>();
params.put("oncePerRequest", "true");
try {
@@ -115,10 +115,7 @@ public final class GenericFilterTest extends FilterAbstractTest {
try {
chain.doFilter(makeRequest(), makeResponse());
}
catch (IOException e) {
fail(e.getMessage());
}
catch (ServletException e) {
catch (IOException | ServletException e) {
fail(e.getMessage());
}
@@ -145,10 +142,7 @@ public final class GenericFilterTest extends FilterAbstractTest {
try {
chain.doFilter(makeRequest(), makeResponse());
}
catch (IOException e) {
fail(e.getMessage());
}
catch (ServletException e) {
catch (IOException | ServletException e) {
fail(e.getMessage());
}

View File

@@ -85,7 +85,7 @@ public class ServletAttributesMapAdapterContextTest extends MapAbstractTest {
}
private Map<String, Object> createAttributes(boolean initialValues) {
Map<String, Object> map = new ConcurrentHashMap<String, Object>();
Map<String, Object> map = new ConcurrentHashMap<>();
if (initialValues) {
String[] sampleKeys = (String[]) getSampleKeys();
@@ -110,7 +110,7 @@ public class ServletAttributesMapAdapterContextTest extends MapAbstractTest {
@Override
public Object[] getNewSampleValues() {
// Needs to be same length but different values
return new Object[] {new Date(-1l), "foo/bar", Arrays.asList(2, 3, 4)};
return new Object[] {new Date(-1L), "foo/bar", Arrays.asList(2, 3, 4)};
}
@Test

View File

@@ -85,7 +85,7 @@ public class ServletAttributesMapAdapterRequestTest extends MapAbstractTest {
}
private Map<String, Object> createAttributes(boolean initialValues) {
Map<String, Object> map = new ConcurrentHashMap<String, Object>();
Map<String, Object> map = new ConcurrentHashMap<>();
if (initialValues) {
String[] sampleKeys = (String[]) getSampleKeys();
@@ -110,7 +110,7 @@ public class ServletAttributesMapAdapterRequestTest extends MapAbstractTest {
@Override
public Object[] getNewSampleValues() {
// Needs to be same length but different values
return new Object[] {new Date(-1l), "foo/bar", Arrays.asList(2, 3, 4)};
return new Object[] {new Date(-1L), "foo/bar", Arrays.asList(2, 3, 4)};
}
@Test

View File

@@ -35,7 +35,7 @@ import org.junit.Test;
import java.io.PrintWriter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
/**
@@ -62,8 +62,8 @@ public class ServletConfigExceptionTest {
throw new ServletConfigException("FooBar!");
}
catch (ServletConfigException e) {
assertEquals(null, e.getRootCause()); // Old API
assertEquals(null, e.getCause());
assertNull(e.getRootCause()); // Old API
assertNull(e.getCause());
e.printStackTrace(new PrintWriter(new NullOutputStream()));
}
@@ -77,8 +77,8 @@ public class ServletConfigExceptionTest {
throw e;
}
catch (ServletConfigException e) {
assertEquals(null, e.getRootCause()); // Old API
assertEquals(null, e.getCause());
assertNull(e.getRootCause()); // Old API
assertNull(e.getCause());
e.printStackTrace(new PrintWriter(new NullOutputStream()));
}
@@ -108,8 +108,8 @@ public class ServletConfigExceptionTest {
throw new ServletConfigException("FooBar!", null);
}
catch (ServletConfigException e) {
assertEquals(null, e.getRootCause()); // Old API
assertEquals(null, e.getCause());
assertNull(e.getRootCause()); // Old API
assertNull(e.getCause());
e.printStackTrace(new PrintWriter(new NullOutputStream()));
}

View File

@@ -31,7 +31,6 @@
package com.twelvemonkeys.servlet;
import org.junit.Test;
import org.mockito.Matchers;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
@@ -114,7 +113,7 @@ public class ServletConfiguratorTest {
ServletConfig config = mock(ServletConfig.class);
when(config.getServletName()).thenReturn("FooServlet");
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Arrays.asList("private")));
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("private")));
when(config.getInitParameter("private")).thenReturn("99");
ServletConfigurator.configure(servlet, config);
@@ -134,7 +133,7 @@ public class ServletConfiguratorTest {
ServletConfig config = mock(ServletConfig.class);
when(config.getServletName()).thenReturn("FooServlet");
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Arrays.asList("private")));
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("private")));
when(config.getInitParameter("private")).thenReturn("private");
when(config.getInitParameter("package-private")).thenReturn("package");
@@ -247,7 +246,7 @@ public class ServletConfiguratorTest {
// Verify
verify(servlet, never()).setX(anyInt());
verify(servlet, times(1)).setFoo("Foo");
verify(servlet, times(1)).setFoo(Matchers.<String>any()); // We don't want multiple invocations
verify(servlet, times(1)).setFoo(anyString()); // We don't want multiple invocations
}
// Test interface
@@ -260,7 +259,7 @@ public class ServletConfiguratorTest {
ServletConfig config = mock(ServletConfig.class);
when(config.getServletName()).thenReturn("FooServlet");
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Arrays.asList("foo")));
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("foo")));
when(config.getInitParameter("foo")).thenReturn("Foo");
ServletConfigurator.configure(servlet, config);
@@ -282,14 +281,14 @@ public class ServletConfiguratorTest {
ServletConfig config = mock(ServletConfig.class);
when(config.getServletName()).thenReturn("FooServlet");
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Arrays.asList("required")));
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("required")));
when(config.getInitParameter("required")).thenReturn("the required value");
ServletConfigurator.configure(servlet, config);
// Verify
verify(servlet, times(1)).setRequired("the required value");
verify(servlet, times(1)).setRequired(Matchers.<String>any()); // We don't want multiple invocations
verify(servlet, times(1)).setRequired(anyString()); // We don't want multiple invocations
}
@Test
@@ -303,12 +302,12 @@ public class ServletConfiguratorTest {
ServletConfig config = mock(ServletConfig.class);
when(config.getServletName()).thenReturn("FooServlet");
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.<Object>emptyList()));
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.emptyList()));
ServletConfigurator.configure(servlet, config);
// Verify
verify(servlet, never()).setNonRequired(Matchers.<String>any()); // Simply not configured
verify(servlet, never()).setNonRequired(anyString()); // Simply not configured
}
@Test(expected = ServletConfigException.class)
@@ -323,7 +322,7 @@ public class ServletConfiguratorTest {
ServletConfig config = mock(ServletConfig.class);
when(config.getServletName()).thenReturn("FooServlet");
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.<Object>emptyList()));
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.emptyList()));
ServletConfigurator.configure(servlet, config); // Should throw exception
}
@@ -339,19 +338,19 @@ public class ServletConfiguratorTest {
ServletConfig config = mock(ServletConfig.class);
when(config.getServletName()).thenReturn("FooServlet");
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.<Object>emptyList()));
when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.emptyList()));
ServletConfigurator.configure(servlet, config);
// Verify
verify(servlet, times(1)).setNonRequired(new int[] {1, 2, 3});
verify(servlet, times(1)).setNonRequired(Matchers.<int[]>any());
verify(servlet, times(1)).setNonRequired((int[]) any());
}
public interface Annotated {
@InitParam(name = "foo")
public void annotated(String an);
void annotated(String an);
}
public abstract class AnnotatedServlet implements Servlet, Filter {

View File

@@ -48,8 +48,8 @@ import static org.mockito.Mockito.when;
* @version $Id: ServletHeadersMapAdapterTest.java#1 $
*/
public class ServletHeadersMapAdapterTest extends MapAbstractTest {
private static final List<String> HEADER_VALUE_ETAG = Arrays.asList("\"1234567890abcdef\"");
private static final List<String> HEADER_VALUE_DATE = Arrays.asList(new Date().toString());
private static final List<String> HEADER_VALUE_ETAG = Collections.singletonList("\"1234567890abcdef\"");
private static final List<String> HEADER_VALUE_DATE = Collections.singletonList(new Date().toString());
private static final List<String> HEADER_VALUE_FOO = Arrays.asList("one", "two");
public boolean isPutAddSupported() {
@@ -108,7 +108,7 @@ public class ServletHeadersMapAdapterTest extends MapAbstractTest {
}
protected static <T> ReturnNewEnumeration<T> returnEnumeration(final Collection<T> collection) {
return new ReturnNewEnumeration<T>(collection);
return new ReturnNewEnumeration<>(collection);
}
private static class ReturnNewEnumeration<T> implements Answer<Enumeration<T>> {

View File

@@ -48,8 +48,8 @@ import static org.mockito.Mockito.when;
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-servlet/src/test/java/com/twelvemonkeys/servlet/ServletParametersMapAdapterTest.java#1 $
*/
public class ServletParametersMapAdapterTest extends MapAbstractTest {
private static final List<String> PARAM_VALUE_ETAG = Arrays.asList("\"1234567890abcdef\"");
private static final List<String> PARAM_VALUE_DATE = Arrays.asList(new Date().toString());
private static final List<String> PARAM_VALUE_ETAG = Collections.singletonList("\"1234567890abcdef\"");
private static final List<String> PARAM_VALUE_DATE = Collections.singletonList(new Date().toString());
private static final List<String> PARAM_VALUE_FOO = Arrays.asList("one", "two");
public boolean isPutAddSupported() {
@@ -109,7 +109,7 @@ public class ServletParametersMapAdapterTest extends MapAbstractTest {
}
protected static <T> ReturnNewEnumeration<T> returnEnumeration(final Collection<T> collection) {
return new ReturnNewEnumeration<T>(collection);
return new ReturnNewEnumeration<>(collection);
}
private static class ReturnNewEnumeration<T> implements Answer<Enumeration<T>> {