Skip to content

Commit

Permalink
Redirect GuardianList#toString to original toString
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Jun 6, 2023
1 parent d60da24 commit 5316536
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ public List<V> subList(int fromIndex, int toIndex) {
final List<V> sublist = guarded.subList(fromIndex, toIndex);
return sublist != null ? new GuardianList<>(sublist, guard) : sublist;
}

@Override
public String toString() {
return guarded.toString();
}

@Override
public boolean equals(Object obj) {
if (GuardianList.class.isInstance(obj)) {
return guarded.equals(((GuardianList) obj).guarded);
}
return guarded.equals(obj);
}

@Override
public int hashCode() {
return guarded.hashCode();
}
}

private static class GuardianIterator<V> extends MutableGuardian<V> implements Iterator<V> {
Expand Down Expand Up @@ -411,6 +429,11 @@ public void remove() {
public void forEachRemaining(Consumer<? super V> action) {
guarded.forEachRemaining(action);
}

@Override
public String toString() {
return guarded.toString();
}
}

private static class GuardianListIterator<V> extends GuardianIterator<V> implements ListIterator<V> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 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
Expand All @@ -16,8 +16,10 @@

package org.glassfish.jersey.tests.e2e;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
Expand All @@ -32,12 +34,14 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -80,6 +84,30 @@ public String contentHeaders(@HeaderParam("custom-header") final String customHe

return sb.toString();
}

@GET
@Path("/tostring")
public String headersGet(@Context HttpHeaders hs) {
StringBuilder sb = new StringBuilder();
List<String> myHeaders = Arrays.asList("Accept", "Content-Type");

try {
MultivaluedMap<String, String> rqhdrs = hs.getRequestHeaders();
Set<String> keys = rqhdrs.keySet();
sb.append("getRequestHeaders= ");
for (String header : myHeaders) {
if (keys.contains(header)) {
sb.append(
"Found " + header + ": " + hs.getRequestHeader(header) + "; ");
}
}
} catch (Throwable ex) {
sb.append("Unexpected exception thrown in getRequestHeaders: "
+ ex.getMessage());
ex.printStackTrace();
}
return sb.toString();
}
}

@Override
Expand All @@ -97,6 +125,26 @@ public void testAccesingHelloworldResource() {
assertEquals(HelloWorldResource.MESSAGE, responseMessage);
}

@Test
public void testHeadersToString() {
try (Response response = target("headers").path("tostring").request()
.header(HttpHeaders.ACCEPT, "text/*, text/html, text/html;level=1, */*")
.header(HttpHeaders.CONTENT_TYPE, "application/xml;charset=utf8")
.get()) {
String content = response.readEntity(String.class);
int index = -1;
Assertions.assertTrue((index = content.indexOf("getRequestHeaders=")) != -1);
Assertions.assertTrue((index = content.indexOf("Accept:")) != -1);
Assertions.assertTrue((index = content.indexOf("text/*")) != -1);
Assertions.assertTrue((index = content.indexOf("text/html")) != -1);
Assertions.assertTrue((index = content.indexOf("text/html")) != -1);
Assertions.assertTrue((index = content.indexOf("*/*")) != -1);
Assertions.assertTrue((index = content.indexOf("Content-Type:")) != -1);
Assertions.assertTrue((index = content.indexOf("application/xml")) != -1);
Assertions.assertTrue((index = content.indexOf("charset=utf8")) != -1);
}
}

@Test
public void testAccesingMissingResource() {
final WebTarget missingResource = target().path("missing");
Expand Down

0 comments on commit 5316536

Please sign in to comment.