From df582e833227a6f8bc983c891173ceae49849702 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Thu, 3 Nov 2022 10:43:32 +0100 Subject: [PATCH] Modify tests Signed-off-by: Jorge Bescos Gascon --- .../internal/inject/ParamConverters.java | 2 +- .../server/internal/inject/AbstractTest.java | 19 +++++++++++-- .../CookieParamStringConstructorTest.java | 14 +++++++--- .../HeaderParamStringConstructorTest.java | 22 +++++++++------ .../MatrixParamStringConstructorTest.java | 17 ++++++----- .../QueryParamStringConstructorTest.java | 28 +++++++++++++------ 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java b/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java index b0f44469dee..5723273fec9 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2018 Payara Foundation and/or its affiliates. * * This program and the accompanying materials are made available under the diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/AbstractTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/AbstractTest.java index d26743a6555..36fe566cc72 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/AbstractTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/AbstractTest.java @@ -74,14 +74,27 @@ protected ContainerResponse getResponseContext(String requestUri, String accept, return apply(requestBuilder.build()); } - protected void _test(String requestUri, String accept, Cookie... cookies) + protected void _test(int expectedStatus, String requestUri, String accept, Cookie... cookies) throws ExecutionException, InterruptedException { + ContainerResponse response = getResponseContext(requestUri, accept, cookies); + assertEquals(expectedStatus, response.getStatus()); + if (200 == expectedStatus) { + assertEquals("content", response.getEntity()); + } + } - assertEquals("content", getResponseContext(requestUri, accept, cookies).getEntity()); + protected void _test(String requestUri, String accept, Cookie... cookies) + throws ExecutionException, InterruptedException { + _test(200, requestUri, accept, cookies); } protected void _test(String requestUri, Cookie... cookies) throws ExecutionException, InterruptedException { - _test(requestUri, null, cookies); + _test(200, requestUri, null, cookies); + } + + protected void _test(int expectedStatus, String requestUri, Cookie... cookies) + throws ExecutionException, InterruptedException { + _test(expectedStatus, requestUri, null, cookies); } public ApplicationHandler app() { diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/CookieParamStringConstructorTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/CookieParamStringConstructorTest.java index 0d7e5886f82..8c206803132 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/CookieParamStringConstructorTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/CookieParamStringConstructorTest.java @@ -71,8 +71,6 @@ public static class ResourceStringListEmpty { @GET public String doGetString(@CookieParam("args") List args) { - assertEquals(1, args.size()); - assertEquals(null, args.get(0)); return "content"; } } @@ -157,10 +155,18 @@ public void testStringConstructorListGet() throws ExecutionException, Interrupte } @Test - public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + public void testStringConstructorListWrongTypeGet() throws ExecutionException, InterruptedException { initiateWebApplication(ResourceStringListEmpty.class); + // When parameters are wrong, status is not 200 + // FIXME Why 400 instead of 404 like in other cases?. Investigate it. + _test(400, "/", "application/stringlist", new Cookie("args", "")); + } - _test("/", "application/stringlist", new Cookie("args", "")); + @Test + public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + initiateWebApplication(ResourceStringListEmpty.class); + // When no parameters, the list is empty + _test("/"); } @Test diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/HeaderParamStringConstructorTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/HeaderParamStringConstructorTest.java index 7589fd3dc36..087a4d869b7 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/HeaderParamStringConstructorTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/HeaderParamStringConstructorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -72,10 +72,6 @@ public static class ResourceStringListEmpty { @GET public String doGetString(@HeaderParam("args") List args) { - assertEquals(3, args.size()); - assertEquals(null, args.get(0)); - assertEquals(null, args.get(1)); - assertEquals(null, args.get(2)); return "content"; } } @@ -173,17 +169,25 @@ public void testStringConstructorListGet() throws ExecutionException, Interrupte } @Test - public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + public void testStringConstructorListWrongTypeGet() throws ExecutionException, InterruptedException { initiateWebApplication(ResourceStringListEmpty.class); - - assertEquals("content", apply( + // When parameters are wrong, status is not 200 + // FIXME Why 400 instead of 404 like in other cases?. Investigate it. + assertEquals(400, apply( RequestContextBuilder.from("/", "GET") .accept("application/stringlist") .header("args", "") .header("args", "") .header("args", "") .build() - ).getEntity()); + ).getStatus()); + } + + @Test + public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + initiateWebApplication(ResourceStringListEmpty.class); + // When no parameters, the list is empty + _test("/"); } @Test diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/MatrixParamStringConstructorTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/MatrixParamStringConstructorTest.java index b6f4fece186..734e1cfb7b5 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/MatrixParamStringConstructorTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/MatrixParamStringConstructorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -68,10 +68,6 @@ public String doGetString(@MatrixParam("args") List args) { public static class ResourceStringListEmpty { @GET public String doGetString(@MatrixParam("args") List args) { - assertEquals(3, args.size()); - assertEquals(null, args.get(0)); - assertEquals(null, args.get(1)); - assertEquals(null, args.get(2)); return "content"; } } @@ -150,10 +146,17 @@ public void testStringConstructorListGet() throws ExecutionException, Interrupte } @Test - public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + public void testStringConstructorListWrognTypeGet() throws ExecutionException, InterruptedException { initiateWebApplication(ResourceStringListEmpty.class); + // When parameters are wrong, status is not 200 + _test(404, "/;args;args;args", "application/stringlist"); + } - _test("/;args;args;args", "application/stringlist"); + @Test + public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + initiateWebApplication(ResourceStringListEmpty.class); + // When no parameters, the list is empty + _test("/"); } @Test diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/QueryParamStringConstructorTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/QueryParamStringConstructorTest.java index 78654634ed1..c1a13381cb6 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/QueryParamStringConstructorTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/inject/QueryParamStringConstructorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -68,10 +68,6 @@ public String doGetString(@QueryParam("args") List args) { public static class ResourceStringListEmpty { @GET public String doGetString(@QueryParam("args") List args) { - assertEquals(3, args.size()); - assertEquals(null, args.get(0)); - assertEquals(null, args.get(1)); - assertEquals(null, args.get(2)); return "content"; } } @@ -162,17 +158,24 @@ public void testStringConstructorListGet() throws ExecutionException, Interrupte } @Test - public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + public void testStringConstructorListEmptyWrongTypeGet() throws ExecutionException, InterruptedException { initiateWebApplication(ResourceStringListEmpty.class); - - _test("/?args&args&args", "application/stringlist"); + // When parameters are wrong, status is not 200 + _test(404, "/?args&args&args", "application/stringlist"); } @Test public void testStringConstructorNullGet() throws ExecutionException, InterruptedException { initiateWebApplication(ResourceStringNull.class); + // When parameters are wrong, status is not 200 + _test(404, "/?arg1=&arg2=", null); + } - _test("/?arg1=&arg2="); + @Test + public void testStringConstructorListEmptyGet() throws ExecutionException, InterruptedException { + initiateWebApplication(ResourceStringListEmpty.class); + // When no parameters, the list is empty + _test("/", "application/stringlist"); } @Test @@ -203,6 +206,13 @@ public void testStringConstructorListEmptyDefault() throws ExecutionException, I _test("/"); } + @Test + public void testStringConstructorListEmpty() throws ExecutionException, InterruptedException { + initiateWebApplication(ResourceStringListEmptyDefault.class); + // When parameters are wrong, status is not 200 + _test(404, "/?args="); + } + @Test public void testStringConstructorListDefault() throws ExecutionException, InterruptedException { initiateWebApplication(ResourceStringListDefault.class);