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 =
37            "^[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]";
38    private static final String TIMESTAMP = "mm-dd HH:MM:SS.xxx";
39
40    @Test
41    public void testBasicOperation() {
42        final SharedLog logTop = new SharedLog("top");
43        logTop.mark("first post!");
44
45        final SharedLog logLevel2a = logTop.forSubComponent("twoA");
46        final SharedLog logLevel2b = logTop.forSubComponent("twoB");
47        logLevel2b.e("2b or not 2b");
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            TIMESTAMP + " - MARK first post!",
57            TIMESTAMP + " - [twoB] ERROR 2b or not 2b",
58            TIMESTAMP + " - [twoA] WARN second post?",
59            TIMESTAMP + " - still logging",
60            TIMESTAMP + " - [twoA.three] 3 >> 2",
61            TIMESTAMP + " - [twoA] MARK ok: last post",
62        };
63        // Verify the logs are all there and in the correct order.
64        verifyLogLines(expected, logTop);
65
66        // In fact, because they all share the same underlying LocalLog,
67        // every subcomponent SharedLog's dump() is identical.
68        verifyLogLines(expected, logLevel2a);
69        verifyLogLines(expected, logLevel2b);
70        verifyLogLines(expected, logLevel3);
71    }
72
73    private static void verifyLogLines(String[] expected, SharedLog log) {
74        final ByteArrayOutputStream ostream = new ByteArrayOutputStream();
75        final PrintWriter pw = new PrintWriter(ostream, true);
76        log.dump(null, pw, null);
77
78        final String dumpOutput = ostream.toString();
79        assertTrue(dumpOutput != null);
80        assertTrue(!"".equals(dumpOutput));
81
82        final String[] lines = dumpOutput.split("\n");
83        assertEquals(expected.length, lines.length);
84
85        for (int i = 0; i < lines.length; i++) {
86            // Fix up the timestamps.
87            lines[i] = lines[i].replaceAll(TIMESTAMP_PATTERN, TIMESTAMP);
88        }
89
90        for (int i = 0; i < expected.length; i++) {
91            assertEquals(expected[i], lines[i]);
92        }
93    }
94}
95