1/*
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 android.net.util;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import android.support.test.filters.SmallTest;
23import android.support.test.runner.AndroidJUnit4;
24
25import org.junit.Test;
26import org.junit.runner.RunWith;
27
28import java.io.ByteArrayOutputStream;
29import java.io.PrintWriter;
30import java.util.ArrayList;
31import java.util.Vector;
32
33@RunWith(AndroidJUnit4.class)
34@SmallTest
35public class SharedLogTest {
36    private static final String TIMESTAMP_PATTERN = "\\d{2}:\\d{2}:\\d{2}";
37    private static final String TIMESTAMP = "HH:MM:SS";
38
39    @Test
40    public void testBasicOperation() {
41        final SharedLog logTop = new SharedLog("top");
42        logTop.mark("first post!");
43
44        final SharedLog logLevel2a = logTop.forSubComponent("twoA");
45        final SharedLog logLevel2b = logTop.forSubComponent("twoB");
46        logLevel2b.e("2b or not 2b");
47        logLevel2a.w("second post?");
48
49        final SharedLog logLevel3 = logLevel2a.forSubComponent("three");
50        logTop.log("still logging");
51        logLevel3.log("3 >> 2");
52        logLevel2a.mark("ok: last post");
53
54        final String[] expected = {
55            " - MARK first post!",
56            " - [twoB] ERROR 2b or not 2b",
57            " - [twoA] WARN second post?",
58            " - still logging",
59            " - [twoA.three] 3 >> 2",
60            " - [twoA] MARK ok: last post",
61        };
62        // Verify the logs are all there and in the correct order.
63        verifyLogLines(expected, logTop);
64
65        // In fact, because they all share the same underlying LocalLog,
66        // every subcomponent SharedLog's dump() is identical.
67        verifyLogLines(expected, logLevel2a);
68        verifyLogLines(expected, logLevel2b);
69        verifyLogLines(expected, logLevel3);
70    }
71
72    private static void verifyLogLines(String[] expected, SharedLog log) {
73        final ByteArrayOutputStream ostream = new ByteArrayOutputStream();
74        final PrintWriter pw = new PrintWriter(ostream, true);
75        log.dump(null, pw, null);
76
77        final String dumpOutput = ostream.toString();
78        assertTrue(dumpOutput != null);
79        assertTrue(!"".equals(dumpOutput));
80
81        final String[] lines = dumpOutput.split("\n");
82        assertEquals(expected.length, lines.length);
83
84        for (int i = 0; i < expected.length; i++) {
85            String got = lines[i];
86            String want = expected[i];
87            assertTrue(String.format("'%s' did not contain '%s'", got, want), got.endsWith(want));
88            assertTrue(String.format("'%s' did not contain a %s timestamp", got, TIMESTAMP),
89                    got.replaceFirst(TIMESTAMP_PATTERN, TIMESTAMP).contains(TIMESTAMP));
90        }
91    }
92}
93