blob: 07ad3123bc531fed3d365ee952c3c6fcfb79f149 [file] [log] [blame]
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +09001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.util;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import android.net.util.SharedLog;
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import org.junit.Test;
27import org.junit.runner.RunWith;
28
29import java.io.ByteArrayOutputStream;
30import java.io.PrintWriter;
31
32@RunWith(AndroidJUnit4.class)
33@SmallTest
34public class SharedLogTest {
35 private static final String TIMESTAMP_PATTERN = "\\d{2}:\\d{2}:\\d{2}";
36 private static final String TIMESTAMP = "HH:MM:SS";
37
38 @Test
39 public void testBasicOperation() {
40 final SharedLog logTop = new SharedLog("top");
41 logTop.mark("first post!");
42
43 final SharedLog logLevel2a = logTop.forSubComponent("twoA");
44 final SharedLog logLevel2b = logTop.forSubComponent("twoB");
45 logLevel2b.e("2b or not 2b");
46 logLevel2b.e("No exception", null);
47 logLevel2b.e("Wait, here's one", new Exception("Test"));
48 logLevel2a.w("second post?");
49
50 final SharedLog logLevel3 = logLevel2a.forSubComponent("three");
51 logTop.log("still logging");
52 logLevel3.log("3 >> 2");
53 logLevel2a.mark("ok: last post");
54
55 final String[] expected = {
56 " - MARK first post!",
57 " - [twoB] ERROR 2b or not 2b",
58 " - [twoB] ERROR No exception",
59 // No stacktrace in shared log, only in logcat
60 " - [twoB] ERROR Wait, here's one: Test",
61 " - [twoA] WARN second post?",
62 " - still logging",
63 " - [twoA.three] 3 >> 2",
64 " - [twoA] MARK ok: last post",
65 };
66 // Verify the logs are all there and in the correct order.
67 verifyLogLines(expected, logTop);
68
69 // In fact, because they all share the same underlying LocalLog,
70 // every subcomponent SharedLog's dump() is identical.
71 verifyLogLines(expected, logLevel2a);
72 verifyLogLines(expected, logLevel2b);
73 verifyLogLines(expected, logLevel3);
74 }
75
76 private static void verifyLogLines(String[] expected, SharedLog log) {
77 final ByteArrayOutputStream ostream = new ByteArrayOutputStream();
78 final PrintWriter pw = new PrintWriter(ostream, true);
79 log.dump(null, pw, null);
80
81 final String dumpOutput = ostream.toString();
82 assertTrue(dumpOutput != null);
83 assertTrue(!"".equals(dumpOutput));
84
85 final String[] lines = dumpOutput.split("\n");
86 assertEquals(expected.length, lines.length);
87
88 for (int i = 0; i < expected.length; i++) {
89 String got = lines[i];
90 String want = expected[i];
91 assertTrue(String.format("'%s' did not contain '%s'", got, want), got.endsWith(want));
92 assertTrue(String.format("'%s' did not contain a %s timestamp", got, TIMESTAMP),
93 got.replaceFirst(TIMESTAMP_PATTERN, TIMESTAMP).contains(TIMESTAMP));
94 }
95 }
96}