1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package com.xtremelabs.robolectric.shadows;
import android.util.Log;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(WithTestDefaultsRunner.class)
public class LogTest {
@Test
public void d_shouldLogAppropriately() {
Log.d("tag", "msg");
assertLogged(Log.DEBUG, "tag", "msg", null);
}
@Test
public void d_shouldLogAppropriately_withThrowable() {
Throwable throwable = new Throwable();
Log.d("tag", "msg", throwable);
assertLogged(Log.DEBUG, "tag", "msg", throwable);
}
@Test
public void e_shouldLogAppropriately() {
Log.e("tag", "msg");
assertLogged(Log.ERROR, "tag", "msg", null);
}
@Test
public void e_shouldLogAppropriately_withThrowable() {
Throwable throwable = new Throwable();
Log.e("tag", "msg", throwable);
assertLogged(Log.ERROR, "tag", "msg", throwable);
}
@Test
public void i_shouldLogAppropriately() {
Log.i("tag", "msg");
assertLogged(Log.INFO, "tag", "msg", null);
}
@Test
public void i_shouldLogAppropriately_withThrowable() {
Throwable throwable = new Throwable();
Log.i("tag", "msg", throwable);
assertLogged(Log.INFO, "tag", "msg", throwable);
}
@Test
public void v_shouldLogAppropriately() {
Log.v("tag", "msg");
assertLogged(Log.VERBOSE, "tag", "msg", null);
}
@Test
public void v_shouldLogAppropriately_withThrowable() {
Throwable throwable = new Throwable();
Log.v("tag", "msg", throwable);
assertLogged(Log.VERBOSE, "tag", "msg", throwable);
}
@Test
public void w_shouldLogAppropriately() {
Log.w("tag", "msg");
assertLogged(Log.WARN, "tag", "msg", null);
}
@Test
public void w_shouldLogAppropriately_withThrowable() {
Throwable throwable = new Throwable();
Log.w("tag", "msg", throwable);
assertLogged(Log.WARN, "tag", "msg", throwable);
}
@Test
public void w_shouldLogAppropriately_withJustThrowable() {
Throwable throwable = new Throwable();
Log.w("tag", throwable);
assertLogged(Log.WARN, "tag", null, throwable);
}
@Test
public void wtf_shouldLogAppropriately() {
Log.wtf("tag", "msg");
assertLogged(Log.ASSERT, "tag", "msg", null);
}
@Test
public void wtf_shouldLogAppropriately_withThrowable() {
Throwable throwable = new Throwable();
Log.wtf("tag", "msg", throwable);
assertLogged(Log.ASSERT, "tag", "msg", throwable);
}
@Test
public void shouldLogToProvidedStream() throws Exception {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream old = ShadowLog.stream;
try {
ShadowLog.stream = new PrintStream(bos);
Log.d("tag", "msg");
assertThat(new String(bos.toByteArray()), equalTo("D/tag: msg\n"));
Log.w("tag", new RuntimeException());
assertTrue(new String(bos.toByteArray()).contains("RuntimeException"));
} finally {
ShadowLog.stream = old;
}
}
@Test
public void infoIsDefaultLoggableLevel() throws Exception {
PrintStream old = ShadowLog.stream;
ShadowLog.stream = null;
assertFalse(Log.isLoggable("FOO", Log.VERBOSE));
assertFalse(Log.isLoggable("FOO", Log.DEBUG));
assertTrue(Log.isLoggable("FOO", Log.INFO));
assertTrue(Log.isLoggable("FOO", Log.WARN));
assertTrue(Log.isLoggable("FOO", Log.ERROR));
assertTrue(Log.isLoggable("FOO", Log.ASSERT));
ShadowLog.stream = old;
}
@Test
public void shouldAlwaysBeLoggableIfStreamIsSpecified() throws Exception {
PrintStream old = ShadowLog.stream;
ShadowLog.stream = new PrintStream(new ByteArrayOutputStream());
assertTrue(Log.isLoggable("FOO", Log.VERBOSE));
assertTrue(Log.isLoggable("FOO", Log.DEBUG));
assertTrue(Log.isLoggable("FOO", Log.INFO));
assertTrue(Log.isLoggable("FOO", Log.WARN));
assertTrue(Log.isLoggable("FOO", Log.ERROR));
assertTrue(Log.isLoggable("FOO", Log.ASSERT));
ShadowLog.stream = old;
}
private void assertLogged(int type, String tag, String msg, Throwable throwable) {
ShadowLog.LogItem lastLog = ShadowLog.getLogs().get(0);
assertEquals(type, lastLog.type);
assertEquals(msg, lastLog.msg);
assertEquals(tag, lastLog.tag);
assertEquals(throwable, lastLog.throwable);
}
}
|