@Test public void createWithOneShouldCreateDefaultHttpClient() throws Exception { DefaultHttpClient expected = mock(DefaultHttpClient.class); ProxySelectorRoutePlanner planner = mock(ProxySelectorRoutePlanner.class); ClientConnectionManager clientConnectionManager = mock(ClientConnectionManager.class); SchemeRegistry schemeRegistry = mock(SchemeRegistry.class); ProxySelector proxySelector = mock(ProxySelector.class); when(expected.getConnectionManager()).thenReturn(clientConnectionManager); when(clientConnectionManager.getSchemeRegistry()).thenReturn(schemeRegistry); mockStatic(ProxySelector.class); when(ProxySelector.getDefault()).thenReturn(proxySelector); whenNew(DefaultHttpClient.class).withNoArguments().thenReturn(expected); whenNew(ProxySelectorRoutePlanner.class) .withArguments(schemeRegistry, proxySelector).thenReturn(planner); HttpClient actual = HttpClientFactory.create(crestConfig, getClass()); assertSame(expected, actual); verify(expected).setRoutePlanner(planner); }
public static HttpClient create(CRestConfig crestConfig, Class<?> source) { HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT); if(httpClient != null){ return httpClient; } int concurrencyLevel = crestConfig.getConcurrencyLevel(); if (concurrencyLevel > 1) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel)); ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); } else { httpClient = new DefaultHttpClient(); } ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault())); return httpClient; }
HttpClientFactory { public static HttpClient create(CRestConfig crestConfig, Class<?> source) { HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT); if(httpClient != null){ return httpClient; } int concurrencyLevel = crestConfig.getConcurrencyLevel(); if (concurrencyLevel > 1) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel)); ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); } else { httpClient = new DefaultHttpClient(); } ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault())); return httpClient; } }
HttpClientFactory { public static HttpClient create(CRestConfig crestConfig, Class<?> source) { HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT); if(httpClient != null){ return httpClient; } int concurrencyLevel = crestConfig.getConcurrencyLevel(); if (concurrencyLevel > 1) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel)); ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); } else { httpClient = new DefaultHttpClient(); } ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault())); return httpClient; } private  HttpClientFactory(); }
HttpClientFactory { public static HttpClient create(CRestConfig crestConfig, Class<?> source) { HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT); if(httpClient != null){ return httpClient; } int concurrencyLevel = crestConfig.getConcurrencyLevel(); if (concurrencyLevel > 1) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel)); ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); } else { httpClient = new DefaultHttpClient(); } ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault())); return httpClient; } private  HttpClientFactory(); static HttpClient create(CRestConfig crestConfig, Class<?> source); }
HttpClientFactory { public static HttpClient create(CRestConfig crestConfig, Class<?> source) { HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT); if(httpClient != null){ return httpClient; } int concurrencyLevel = crestConfig.getConcurrencyLevel(); if (concurrencyLevel > 1) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel)); ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); } else { httpClient = new DefaultHttpClient(); } ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault())); return httpClient; } private  HttpClientFactory(); static HttpClient create(CRestConfig crestConfig, Class<?> source);  }