22
22
import static org .easymock .EasyMock .reset ;
23
23
import static org .easymock .EasyMock .verify ;
24
24
import static org .junit .Assert .assertEquals ;
25
+ import static org .junit .Assert .assertNotNull ;
25
26
import static org .junit .Assert .assertTrue ;
26
27
27
28
import com .google .api .client .util .Strings ;
@@ -52,6 +53,7 @@ public class LoggingHandlerTest {
52
53
private static final String LOG_NAME = "java.log" ;
53
54
private static final String MESSAGE = "message" ;
54
55
private static final String PROJECT = "project" ;
56
+ private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT" ;
55
57
56
58
private static final MonitoredResource DEFAULT_RESOURCE =
57
59
MonitoredResource .of ("global" , ImmutableMap .of ("project_id" , PROJECT ));
@@ -205,9 +207,9 @@ public void setUp() {
205
207
expect (options .getService ()).andStubReturn (logging );
206
208
expect (options .getAutoPopulateMetadata ()).andStubReturn (Boolean .FALSE );
207
209
logging .setFlushSeverity (EasyMock .anyObject (Severity .class ));
208
- expectLastCall ().once ();
210
+ expectLastCall ().anyTimes ();
209
211
logging .setWriteSynchronicity (EasyMock .anyObject (Synchronicity .class ));
210
- expectLastCall ().once ();
212
+ expectLastCall ().anyTimes ();
211
213
}
212
214
213
215
@ After
@@ -221,6 +223,19 @@ private static LogRecord newLogRecord(Level level, String message) {
221
223
return record ;
222
224
}
223
225
226
+ @ Test
227
+ public void testDefaultHandlerCreation () {
228
+ String oldProject = System .getProperty (PROJECT_ENV_NAME );
229
+ System .setProperty (PROJECT_ENV_NAME , PROJECT );
230
+ replay (options , logging );
231
+ assertNotNull (new LoggingHandler ());
232
+ if (oldProject != null ) {
233
+ System .setProperty (PROJECT_ENV_NAME , oldProject );
234
+ } else {
235
+ System .clearProperty (PROJECT_ENV_NAME );
236
+ }
237
+ }
238
+
224
239
@ Test
225
240
public void testPublishLevels () {
226
241
logging .write (ImmutableList .of (FINEST_ENTRY ), DEFAULT_OPTIONS );
@@ -483,6 +498,7 @@ public void testFlushLevel() {
483
498
484
499
@ Test
485
500
public void testSyncWrite () {
501
+ reset (logging );
486
502
LogEntry entry =
487
503
LogEntry .newBuilder (Payload .StringPayload .of (MESSAGE ))
488
504
.setSeverity (Severity .DEBUG )
@@ -491,10 +507,10 @@ public void testSyncWrite() {
491
507
.setTimestamp (123456789L )
492
508
.build ();
493
509
510
+ logging .setWriteSynchronicity (Synchronicity .ASYNC );
494
511
logging .setWriteSynchronicity (Synchronicity .SYNC );
495
- expectLastCall ().once ();
496
512
logging .write (ImmutableList .of (entry ), DEFAULT_OPTIONS );
497
- expectLastCall (). once ( );
513
+ logging . setFlushSeverity ( Severity . ERROR );
498
514
replay (options , logging );
499
515
500
516
LoggingHandler handler = new LoggingHandler (LOG_NAME , options , DEFAULT_RESOURCE );
0 commit comments