View Javadoc

1   package com.atlassian.httpclient.apache.httpcomponents;
2   
3   import com.atlassian.event.api.EventPublisher;
4   import com.atlassian.httpclient.api.HttpClient;
5   import com.atlassian.httpclient.api.factory.HttpClientOptions;
6   import com.atlassian.sal.api.ApplicationProperties;
7   import com.atlassian.sal.api.executor.ThreadLocalContextManager;
8   import org.hamcrest.Matchers;
9   import org.junit.Rule;
10  import org.junit.Test;
11  import org.junit.rules.ExpectedException;
12  import org.junit.runner.RunWith;
13  import org.mockito.Mock;
14  import org.mockito.Mockito;
15  import org.mockito.runners.MockitoJUnitRunner;
16  
17  import static org.hamcrest.Matchers.notNullValue;
18  import static org.junit.Assert.assertThat;
19  
20  @RunWith(MockitoJUnitRunner.class)
21  public class DefaultHttpClientFactoryTest {
22      @Rule
23      public ExpectedException exception = ExpectedException.none();
24  
25      @Mock
26      private EventPublisher eventPublisher;
27  
28      @Mock
29      private ApplicationProperties applicationProperties;
30  
31      @Mock
32      private ThreadLocalContextManager<?> threadLocalContextManager;
33  
34      @Test
35      public void testDisposingHttpClient() throws Exception {
36          DefaultHttpClientFactory<?> factory = new DefaultHttpClientFactory<>(eventPublisher, applicationProperties, threadLocalContextManager);
37          final HttpClient httpClient1 = factory.create(new HttpClientOptions());
38          final HttpClient httpClient2 = factory.create(new HttpClientOptions());
39  
40          assertThat(httpClient1, notNullValue());
41          assertThat(httpClient2, notNullValue());
42          assertThat(factory.getHttpClients(), Matchers.<ApacheAsyncHttpClient>iterableWithSize(2));
43  
44          factory.dispose(httpClient1);
45  
46          assertThat(factory.getHttpClients(), Matchers.<ApacheAsyncHttpClient>iterableWithSize(1));
47  
48          factory.dispose(httpClient2);
49  
50          assertThat(factory.getHttpClients(), Matchers.<ApacheAsyncHttpClient>iterableWithSize(0));
51      }
52  
53      @Test
54      public void testDisposingClientTwice() throws Exception {
55          DefaultHttpClientFactory<?> factory = new DefaultHttpClientFactory<>(eventPublisher, applicationProperties, threadLocalContextManager);
56          final HttpClient httpClient = factory.create(new HttpClientOptions());
57  
58          assertThat(httpClient, notNullValue());
59          assertThat(factory.getHttpClients(), Matchers.<ApacheAsyncHttpClient>iterableWithSize(1));
60  
61          factory.dispose(httpClient);
62  
63          assertThat(factory.getHttpClients(), Matchers.<ApacheAsyncHttpClient>iterableWithSize(0));
64  
65          exception.expect(IllegalStateException.class);
66          exception.expectMessage("Client is already disposed");
67          factory.dispose(httpClient);
68      }
69  
70      @Test
71      public void testNotDisposingNotDisposableClient() throws Exception {
72          DefaultHttpClientFactory<?> factory = new DefaultHttpClientFactory<>(eventPublisher, applicationProperties, threadLocalContextManager);
73  
74          exception.expect(IllegalArgumentException.class);
75          exception.expectMessage("Given client is not disposable");
76          factory.dispose(Mockito.mock(HttpClient.class));
77      }
78  }