55import me .proxer .library .api .ProxerApi .Builder .LoggingStrategy ;
66import okhttp3 .Request ;
77import okhttp3 .mockwebserver .MockResponse ;
8+ import org .graalvm .compiler .core .common .SuppressFBWarnings ;
89import org .junit .After ;
910import org .junit .Before ;
1011import org .junit .Test ;
@@ -28,22 +29,28 @@ public class LoggingInterceptorTest extends ProxerTest {
2829
2930 private ByteArrayOutputStream loggerStream ;
3031 private Handler loggerHandler ;
32+ private Logger logger ;
3133
34+ @ SuppressFBWarnings (
35+ value = "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE" ,
36+ justification = "False positive? Logger has a hard reference."
37+ )
3238 @ Override
3339 @ Before
3440 public void setUp () throws IOException , GeneralSecurityException {
3541 super .setUp ();
3642
3743 loggerStream = new ByteArrayOutputStream ();
3844 loggerHandler = new StreamHandler (loggerStream , new EchoFormatter ());
45+ logger = Logger .getLogger ("ProxerLibJava" );
3946
40- Logger . getLogger ( "ProxerLibJava" ) .addHandler (loggerHandler );
47+ logger .addHandler (loggerHandler );
4148 }
4249
4350 @ Override
4451 @ After
4552 public void tearDown () throws IOException {
46- Logger . getLogger ( "ProxerLibJava" ) .removeHandler (loggerHandler );
53+ logger .removeHandler (loggerHandler );
4754
4855 super .tearDown ();
4956 }
@@ -59,8 +66,9 @@ public void testLog() throws IOException, ProxerException {
5966 .execute ();
6067
6168 loggerHandler .flush ();
69+ loggerStream .flush ();
6270
63- assertThat (loggerStream .toString ()).isEqualTo ("Requesting https://"
71+ assertThat (loggerStream .toString ("UTF-8" )).isEqualTo ("Requesting https://"
6472 + server .getHostName () + ":" + server .getPort ()
6573 + "/api/v1/notifications/news with method GET and these headers:\n "
6674 + "proxer-api-key: mockKey\n "
@@ -78,8 +86,9 @@ public void testLogWithBody() throws IOException, ProxerException {
7886 .execute ();
7987
8088 loggerHandler .flush ();
89+ loggerStream .flush ();
8190
82- assertThat (loggerStream .toString ()).isEqualTo ("Requesting https://"
91+ assertThat (loggerStream .toString ("UTF-8" )).isEqualTo ("Requesting https://"
8392 + server .getHostName () + ":" + server .getPort ()
8493 + "/api/v1/user/login with method POST, these headers:\n "
8594 + "proxer-api-key: mockKey\n "
@@ -98,8 +107,9 @@ public void testLogWithEmptyBody() throws IOException, ProxerException {
98107 .execute ();
99108
100109 loggerHandler .flush ();
110+ loggerStream .flush ();
101111
102- assertThat (loggerStream .toString ()).isEqualTo ("Requesting https://"
112+ assertThat (loggerStream .toString ("UTF-8" )).isEqualTo ("Requesting https://"
103113 + server .getHostName () + ":" + server .getPort ()
104114 + "/api/v1/user/logout with method POST, these headers:\n "
105115 + "proxer-api-key: mockKey\n "
@@ -117,8 +127,9 @@ public void testLogAllOtherHost() throws IOException {
117127 api .client ().newCall (new Request .Builder ().url ("http://example.com/test" ).build ()).execute ();
118128
119129 loggerHandler .flush ();
130+ loggerStream .flush ();
120131
121- assertThat (loggerStream .toString ()).isEqualTo ("Requesting http://"
132+ assertThat (loggerStream .toString ("UTF-8" )).isEqualTo ("Requesting http://"
122133 + server .getHostName () + ":" + server .getPort ()
123134 + "/test with method GET and no headers." );
124135 }
@@ -134,8 +145,9 @@ public void testLogApiOnly() throws IOException {
134145 api .client ().newCall (new Request .Builder ().url ("http://example.com/test" ).build ()).execute ();
135146
136147 loggerHandler .flush ();
148+ loggerStream .flush ();
137149
138- assertThat (loggerStream .toString ()).isEmpty ();
150+ assertThat (loggerStream .toString ("UTF-8" )).isEmpty ();
139151 }
140152
141153 @ Test
@@ -149,8 +161,9 @@ public void testLogNone() throws IOException {
149161 api .client ().newCall (new Request .Builder ().url ("http://example.com/test" ).build ()).execute ();
150162
151163 loggerHandler .flush ();
164+ loggerStream .flush ();
152165
153- assertThat (loggerStream .toString ()).isEmpty ();
166+ assertThat (loggerStream .toString ("UTF-8" )).isEmpty ();
154167 }
155168
156169 @ Test
@@ -160,15 +173,15 @@ public void testLogWithCustomLogger() throws IOException, InterruptedException {
160173 + server .getHostName () + ":" + server .getPort ()
161174 + "/test with method GET and no headers." ;
162175
163- final CustomLogger logger = message -> {
176+ final CustomLogger customLogger = message -> {
164177 if (message .equals (expectedMessage )) {
165178 lock .countDown ();
166179 }
167180
168181 // Failed: Not the message we want. The lock will never be counted down and timeout.
169182 };
170183
171- api = constructApi ().customLogger (logger ).loggingStrategy (LoggingStrategy .ALL ).build ();
184+ api = constructApi ().customLogger (customLogger ).loggingStrategy (LoggingStrategy .ALL ).build ();
172185
173186 server .enqueue (new MockResponse ());
174187
0 commit comments