1+ package io .intercom .api ;
2+
3+ import org .junit .After ;
4+ import org .junit .Test ;
5+
6+ import java .util .Random ;
7+
8+ import static org .junit .Assert .*;
9+
10+ public class IntercomTest {
11+
12+ @ After
13+ public void tearDown () {
14+ Intercom .setUseThreadLocal (false );
15+ }
16+
17+ @ Test
18+ public void testUseThreadLocal () {
19+ Intercom .setUseThreadLocal (false );
20+ Intercom .setToken ("tx" );
21+ assertFalse (Intercom .usesThreadLocal ());
22+ assertEquals ("tx" , Intercom .getToken ());
23+ Intercom .setUseThreadLocal (true );
24+ assertTrue (Intercom .usesThreadLocal ());
25+ assertNotEquals ("tx" , Intercom .getToken ());
26+ Intercom .setUseThreadLocal (false );
27+ assertFalse (Intercom .usesThreadLocal ());
28+ assertEquals ("tx" , Intercom .getToken ());
29+ }
30+
31+ @ Test
32+ public void testApiKey () {
33+ Intercom .setApiKey ("k1" );
34+ assertEquals ("k1" , Intercom .getApiKey ());
35+ assertEquals (Intercom .AuthKeyType .API_KEY , Intercom .getAuthKeyType ());
36+ assertNull (Intercom .getToken ());
37+ }
38+
39+ @ Test
40+ public void testToken () {
41+ Intercom .setToken ("t1" );
42+ assertEquals ("t1" , Intercom .getToken ());
43+ assertEquals (Intercom .AuthKeyType .TOKEN , Intercom .getAuthKeyType ());
44+ assertNull (Intercom .getApiKey ());
45+ }
46+
47+ @ Test
48+ public void testStaticContext () throws Exception {
49+ Intercom .setApiKey ("k1" );
50+ assertEquals ("k1" , Intercom .getApiKey ());
51+ assertNull (Intercom .getToken ());
52+ assertEquals (Intercom .AuthKeyType .API_KEY , Intercom .getAuthKeyType ());
53+ Intercom .setAppID ("app1" );
54+ assertEquals ("app1" , Intercom .getAppID ());
55+ Intercom .setConnectionTimeout (98765 );
56+ assertEquals (98765 , Intercom .getConnectionTimeout ());
57+ Intercom .setRequestTimeout (12345 );
58+ assertEquals (12345 , Intercom .getRequestTimeout ());
59+ Intercom .setRequestUsingCaches (true );
60+ assertTrue (Intercom .isRequestUsingCaches ());
61+
62+ ThreadTester tt1 = new ThreadTester ();
63+ ThreadTester tt2 = new ThreadTester ();
64+ new Thread (tt1 ).run ();
65+ new Thread (tt2 ).run ();
66+ tt1 .waitUntilComplete ();
67+ tt2 .waitUntilComplete ();
68+
69+ assertEquals (Intercom .getApiKey (), tt1 .apiKey );
70+ assertEquals (Intercom .getAuthKeyType (), tt1 .authKeyType );
71+ assertEquals (Intercom .getToken (), tt1 .token );
72+ assertEquals (Intercom .getConnectionTimeout (), tt1 .connectionTimeout );
73+ assertEquals (Intercom .getRequestTimeout (), tt1 .requestTimeout );
74+ assertEquals (Intercom .isRequestUsingCaches (), tt1 .requestUsingCaches );
75+
76+ assertEquals (Intercom .getApiKey (), tt2 .apiKey );
77+ assertEquals (Intercom .getAuthKeyType (), tt2 .authKeyType );
78+ assertEquals (Intercom .getToken (), tt2 .token );
79+ assertEquals (Intercom .getConnectionTimeout (), tt2 .connectionTimeout );
80+ assertEquals (Intercom .getRequestTimeout (), tt2 .requestTimeout );
81+ assertEquals (Intercom .isRequestUsingCaches (), tt2 .requestUsingCaches );
82+ }
83+
84+ @ Test
85+ public void testThreadLocalContext () throws Exception {
86+ Intercom .setUseThreadLocal (true );
87+
88+ ThreadLocalTester1 tt1 = new ThreadLocalTester1 ();
89+ ThreadLocalTester2 tt2 = new ThreadLocalTester2 ();
90+ new Thread (tt1 ).run ();
91+ new Thread (tt2 ).run ();
92+ tt1 .waitUntilComplete ();
93+ tt2 .waitUntilComplete ();
94+
95+ assertEquals (tt1 .localToken , tt1 .token );
96+ assertNull (tt1 .apiKey );
97+ assertEquals (Intercom .AuthKeyType .TOKEN , tt1 .authKeyType );
98+ assertEquals (tt1 .localConnectionTimeout , tt1 .connectionTimeout );
99+ assertEquals (tt1 .localRequestTimeout , tt1 .requestTimeout );
100+ assertEquals (tt1 .localRequestUsingCaches , tt1 .requestUsingCaches );
101+
102+ assertEquals (tt2 .localApiKey , tt2 .apiKey );
103+ assertNull (tt2 .token );
104+ assertEquals (Intercom .AuthKeyType .API_KEY , tt2 .authKeyType );
105+ assertEquals (tt2 .localConnectionTimeout , tt2 .connectionTimeout );
106+ assertEquals (tt2 .localRequestTimeout , tt2 .requestTimeout );
107+ assertEquals (tt2 .localRequestUsingCaches , tt2 .requestUsingCaches );
108+ }
109+
110+ @ Test
111+ public void testClearThreadLocalContexts () throws Exception {
112+ Intercom .setUseThreadLocal (true );
113+
114+ Intercom .setApiKey ("testKey" );
115+ assertEquals ("testKey" , Intercom .getApiKey ());
116+
117+ Intercom .clearThreadLocalContexts ();
118+ assertNull (Intercom .getApiKey ());
119+
120+ Intercom .setApiKey ("testKey2" );
121+ assertEquals ("testKey2" , Intercom .getApiKey ());
122+ }
123+
124+ @ Test
125+ public void testClearThreadLocalContext () throws Exception {
126+ Intercom .setUseThreadLocal (true );
127+
128+ Intercom .setApiKey ("testKey" );
129+ assertEquals ("testKey" , Intercom .getApiKey ());
130+
131+ Intercom .clearThreadLocalContext ();
132+ assertNull (Intercom .getApiKey ());
133+
134+ Intercom .setApiKey ("testKey2" );
135+ assertEquals ("testKey2" , Intercom .getApiKey ());
136+ }
137+
138+ class ThreadTester implements Runnable {
139+ String apiKey , appId , token ;
140+ Intercom .AuthKeyType authKeyType ;
141+ int connectionTimeout = -1 ;
142+ int requestTimeout = -1 ;
143+ Boolean requestUsingCaches ;
144+ boolean completed = false ;
145+
146+ @ Override
147+ public void run () {
148+ apiKey = Intercom .getApiKey ();
149+ authKeyType = Intercom .getAuthKeyType ();
150+ token = Intercom .getToken ();
151+ appId = Intercom .getAppID ();
152+ connectionTimeout = Intercom .getConnectionTimeout ();
153+ requestTimeout = Intercom .getRequestTimeout ();
154+ requestUsingCaches = Intercom .isRequestUsingCaches ();
155+ completed = true ;
156+ synchronized (this ) {
157+ notify ();
158+ }
159+ }
160+
161+ void waitUntilComplete () throws InterruptedException {
162+ synchronized (this ) {
163+ while (!completed ) {
164+ wait (5000 );
165+ }
166+ }
167+ }
168+ }
169+
170+ class ThreadLocalTester1 extends ThreadTester {
171+ final Random rnd = new Random ();
172+ final String localToken = "tx" ;
173+ final String localAppId = "appx" ;
174+ final int localConnectionTimeout = rnd .nextInt ();
175+ final int localRequestTimeout = rnd .nextInt ();
176+ final boolean localRequestUsingCaches = rnd .nextBoolean ();
177+
178+ @ Override
179+ public void run () {
180+ Intercom .clearThreadLocalContext ();
181+ Intercom .setToken (localToken );
182+ Intercom .setAppID (localAppId );
183+ Intercom .setConnectionTimeout (localConnectionTimeout );
184+ Intercom .setRequestTimeout (localRequestTimeout );
185+ Intercom .setRequestUsingCaches (localRequestUsingCaches );
186+ super .run ();
187+ }
188+ }
189+
190+ class ThreadLocalTester2 extends ThreadTester {
191+ final Random rnd = new Random ();
192+ final String localApiKey = "api" ;
193+ final String localAppId = "appId" ;
194+ final int localConnectionTimeout = rnd .nextInt ();
195+ final int localRequestTimeout = rnd .nextInt ();
196+ final boolean localRequestUsingCaches = rnd .nextBoolean ();
197+
198+ @ Override
199+ public void run () {
200+ Intercom .clearThreadLocalContext ();
201+ Intercom .setApiKey (localApiKey );
202+ Intercom .setAppID (localAppId );
203+ Intercom .setConnectionTimeout (localConnectionTimeout );
204+ Intercom .setRequestTimeout (localRequestTimeout );
205+ Intercom .setRequestUsingCaches (localRequestUsingCaches );
206+ super .run ();
207+ }
208+ }
209+ }
0 commit comments