Skip to content

Commit

Permalink
fix for #4082
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
  • Loading branch information
senivam committed Mar 26, 2019
1 parent 9cfc243 commit 682f66c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2019 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,29 +16,32 @@

package org.glassfish.jersey.client;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import javax.annotation.Priority;
import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Priority;
import javax.net.ssl.SSLContext;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -185,6 +188,7 @@ public void testCreateClientWithConfigFromRequestContext() throws Exception {
_testCreateClientWithAnotherConfig(true);
}


public void _testCreateClientWithAnotherConfig(final boolean clientInFilter) throws Exception {
final Client client = ClientBuilder.newBuilder().register(new ClientFeature()).build();
Response response = client.target("http://localhost")
Expand All @@ -208,6 +212,43 @@ public void _testCreateClientWithAnotherConfig(final boolean clientInFilter) thr
assertThat(response.readEntity(String.class), equalTo("ok"));
}

@Test
public void testRegisterIrrelevantContractsMap() {
final ClientBuilder clientBuilder = ClientBuilder.newBuilder();
final Map<Class<?>, Integer> contracts = new HashMap<>();
contracts.put(Object.class, 500);
contracts.put(String.class, 501);
int sizeBeforeRegister = contracts.size();
clientBuilder.register(ClientConfigTest.MyProvider.class, contracts);
int sizeAfterRegister = contracts.size();

assertThat(sizeBeforeRegister, equalTo(sizeAfterRegister));
}

@Test
public void testRegisterNullMap() {
final ClientBuilder clientBuilder = ClientBuilder.newBuilder();
final Map<Class<?>, Integer> contracts = null;
clientBuilder.register(ClientConfigTest.MyProvider.class, contracts);

assertNull(contracts);
}

@Test(expected = Test.None.class) //no exception shall be thrown
public void testRegisterIrrelevantImmutableContractsMap() {
final ClientBuilder clientBuilder = ClientBuilder.newBuilder();
final Map<Class<?>, Integer> contracts = new HashMap<>();
contracts.put(Object.class, 500);
contracts.put(String.class, 501);
final Map<Class<?>, Integer> immutableContracts = Collections.unmodifiableMap(contracts);
int sizeBeforeRegister = immutableContracts.size();
clientBuilder.register(ClientConfigTest.MyProvider.class, immutableContracts);
int sizeAfterRegister = immutableContracts.size(); //that just proves everything passed OK
// otherwise exception already would been thrown

assertThat(sizeBeforeRegister, equalTo(sizeAfterRegister));
}

@Test
public void testNegativeConnectTimeout() {
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019 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 @@ -18,6 +18,7 @@

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -432,7 +433,7 @@ public boolean register(Object component,
* (if any).
* @param contractMap map of contracts and their binding priorities. If {@code null}, the contracts will
* gathered by introspecting the component class. Content of the contract map
* may be modified during the registration processing.
* is not modified during the registration processing.
* @param modelEnhancer custom contract provider model enhancer.
* @return {@code true} upon successful registration of a contract provider model for a given component class,
* {@code false} otherwise.
Expand Down Expand Up @@ -481,18 +482,19 @@ public static ContractProvider modelFor(final Class<?> componentClass) {
* (if any).
* @param contractMap map of contracts and their binding priorities. If {@code null}, the contracts will
* gathered by introspecting the component class. Content of the contract map
* may be modified during the registration processing.
* is not modified during the registration processing.
* @param modelEnhancer custom contract provider model enhancer.
* @return contract provider model for the class.
*/
private static ContractProvider modelFor(final Class<?> componentClass,
final int defaultPriority,
final Map<Class<?>, Integer> contractMap,
final Inflector<ContractProvider.Builder, ContractProvider> modelEnhancer) {
Map<Class<?>, Integer> contracts = contractMap;
if (contracts == null) { // introspect
Map<Class<?>, Integer> contracts;
if (contractMap == null) { // introspect
contracts = asMap(Providers.getProviderContracts(componentClass));
} else { // filter custom contracts
contracts = new HashMap<>(contractMap);
final Iterator<Class<?>> it = contracts.keySet().iterator();
while (it.hasNext()) {
final Class<?> contract = it.next();
Expand Down

0 comments on commit 682f66c

Please sign in to comment.