Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit b9a497f

Browse files
Chandlerpavelbucek
authored andcommitted
Supporting hashCode and equals calls on jersey client proxy objects.
Change-Id: I7d3814ab15af847c8465b10d56ca4492bd467ece
1 parent 5b113f8 commit b9a497f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

ext/proxy-client/src/main/java/org/glassfish/jersey/client/proxy/WebResourceFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -163,6 +163,16 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
163163
return toString();
164164
}
165165

166+
if (args == null && method.getName().equals("hashCode")) {
167+
//unique instance in the JVM, and no need to override
168+
return hashCode();
169+
}
170+
171+
if (args != null && args.length == 1 && method.getName().equals("equals")) {
172+
//unique instance in the JVM, and no need to override
173+
return equals(args[0]);
174+
}
175+
166176
// get the interface describing the resource
167177
final Class<?> proxyIfc = proxy.getClass().getInterfaces()[0];
168178

ext/proxy-client/src/test/java/org/glassfish/jersey/client/proxy/WebResourceFactoryTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -61,6 +61,8 @@
6161
import org.junit.Ignore;
6262
import org.junit.Test;
6363
import static org.junit.Assert.assertEquals;
64+
import static org.junit.Assert.assertFalse;
65+
import static org.junit.Assert.assertNotEquals;
6466
import static org.junit.Assert.assertTrue;
6567

6668
/**
@@ -69,6 +71,7 @@
6971
public class WebResourceFactoryTest extends JerseyTest {
7072

7173
private MyResourceIfc resource;
74+
private MyResourceIfc resource2;
7275
private MyResourceIfc resourceWithXML;
7376

7477
@Override
@@ -86,6 +89,7 @@ protected ResourceConfig configure() {
8689
public void setUp() throws Exception {
8790
super.setUp();
8891
resource = WebResourceFactory.newResource(MyResourceIfc.class, target());
92+
resource2 = WebResourceFactory.newResource(MyResourceIfc.class, target());
8993

9094
final MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(1);
9195
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
@@ -342,4 +346,16 @@ public void testToString() throws Exception {
342346

343347
assertEquals(expected, actual);
344348
}
349+
350+
@Test
351+
public void testHashCode() throws Exception {
352+
int h1 = resource.hashCode();
353+
int h2 = resource2.hashCode();
354+
assertNotEquals("The hash codes should not match", h1, h2);
355+
}
356+
357+
@Test
358+
public void testEquals() {
359+
assertFalse("The two resource instances should not be considered equals as they are unique", resource.equals(resource2));
360+
}
345361
}

0 commit comments

Comments
 (0)