11/*
2- Copyright 2016, Google, Inc.
2+ Copyright 2017 Google Inc.
33
44 Licensed under the Apache License, Version 2.0 (the "License");
55 you may not use this file except in compliance with the License.
1818
1919import static com .google .common .truth .Truth .assertThat ;
2020
21+ import com .google .cloud .MonitoredResource ;
22+ import com .google .cloud .logging .LogEntry ;
2123import com .google .cloud .logging .Logging ;
2224import com .google .cloud .logging .LoggingOptions ;
25+ import com .google .cloud .logging .Payload .StringPayload ;
26+ import java .io .ByteArrayOutputStream ;
27+ import java .io .PrintStream ;
28+ import java .util .Collections ;
2329import org .junit .After ;
2430import org .junit .Before ;
2531import org .junit .Test ;
2632import org .junit .runner .RunWith ;
2733import org .junit .runners .JUnit4 ;
2834
29- import java .io .ByteArrayOutputStream ;
30- import java .io .PrintStream ;
31-
3235/**
3336 * Tests for quickstart sample.
3437 */
3538@ RunWith (JUnit4 .class )
3639@ SuppressWarnings ("checkstyle:abbreviationaswordinname" )
37- public class QuickstartSampleIT {
40+ public class LoggingIT {
3841
3942 private ByteArrayOutputStream bout ;
4043 private PrintStream out ;
44+ private Logging logging = LoggingOptions .getDefaultInstance ().getService ();
4145
42- private static final void deleteMyLog () {
43- Logging logging = LoggingOptions .getDefaultInstance ().getService ();
44-
45- logging .deleteLog ("my-log" );
46+ private void deleteLog (String logName ) {
47+ logging .deleteLog (logName );
4648 }
4749
4850 @ Before
4951 public void setUp () {
50- deleteMyLog ();
51-
5252 bout = new ByteArrayOutputStream ();
5353 out = new PrintStream (bout );
5454 System .setOut (out );
@@ -57,13 +57,36 @@ public void setUp() {
5757 @ After
5858 public void tearDown () {
5959 System .setOut (null );
60- deleteMyLog ();
6160 }
6261
6362 @ Test
6463 public void testQuickstart () throws Exception {
65- QuickstartSample .main ("my-log" );
64+ String logName = "my-log" ;
65+ deleteLog (logName );
66+ QuickstartSample .main (logName );
6667 String got = bout .toString ();
6768 assertThat (got ).contains ("Logged: Hello, world!" );
69+ deleteLog (logName );
70+ }
71+
72+ @ Test (timeout = 10000 )
73+ public void testWriteAndListLogs () throws Exception {
74+ String logName = "test-log" ;
75+ deleteLog (logName );
76+ // write a log entry
77+ LogEntry entry = LogEntry .newBuilder (StringPayload .of ("Hello world again" ))
78+ .setLogName (logName )
79+ .setResource (MonitoredResource .newBuilder ("global" ).build ())
80+ .build ();
81+ logging .write (Collections .singleton (entry ));
82+ // flush out log immediately
83+ logging .flush ();
84+ bout .reset ();
85+ while (bout .toString ().isEmpty ()) {
86+ ListLogs .main (logName );
87+ Thread .sleep (1000 );
88+ }
89+ assertThat (bout .toString ().contains ("Hello world again" )).isTrue ();
90+ deleteLog (logName );
6891 }
6992}
0 commit comments