1 /* 2 * Copyright (C) 2016 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 17 package android.util; 18 19 import androidx.test.filters.LargeTest; 20 21 import junit.framework.TestCase; 22 23 import java.io.PrintWriter; 24 import java.io.StringWriter; 25 import java.util.Arrays; 26 import java.util.Collections; 27 import java.util.List; 28 29 @LargeTest 30 public class LocalLogTest extends TestCase { 31 testA()32 public void testA() { 33 String[] lines = { 34 "foo", 35 "bar", 36 "baz" 37 }; 38 String[] want = lines; 39 testcase(new LocalLog(10), lines, want); 40 } 41 testB()42 public void testB() { 43 String[] lines = { 44 "foo", 45 "bar", 46 "baz" 47 }; 48 String[] want = {}; 49 testcase(new LocalLog(0), lines, want); 50 } 51 testC()52 public void testC() { 53 String[] lines = { 54 "dropped", 55 "dropped", 56 "dropped", 57 "dropped", 58 "dropped", 59 "dropped", 60 "foo", 61 "bar", 62 "baz", 63 }; 64 String[] want = { 65 "foo", 66 "bar", 67 "baz", 68 }; 69 testcase(new LocalLog(3), lines, want); 70 } 71 testcase(LocalLog logger, String[] input, String[] want)72 void testcase(LocalLog logger, String[] input, String[] want) { 73 for (String l : input) { 74 logger.log(l); 75 } 76 verifyAllLines(want, dump(logger).split("\n")); 77 verifyAllLines(reverse(want), reverseDump(logger).split("\n")); 78 } 79 verifyAllLines(String[] wantLines, String[] gotLines)80 void verifyAllLines(String[] wantLines, String[] gotLines) { 81 for (int i = 0; i < wantLines.length; i++) { 82 String want = wantLines[i]; 83 String got = gotLines[i]; 84 String msg = String.format("%s did not contain %s", quote(got), quote(want)); 85 assertTrue(msg, got.contains(want)); 86 } 87 } 88 dump(LocalLog logger)89 static String dump(LocalLog logger) { 90 StringWriter buffer = new StringWriter(); 91 PrintWriter writer = new PrintWriter(buffer); 92 logger.dump(null, writer, new String[0]); 93 return buffer.toString(); 94 } 95 reverseDump(LocalLog logger)96 static String reverseDump(LocalLog logger) { 97 StringWriter buffer = new StringWriter(); 98 PrintWriter writer = new PrintWriter(buffer); 99 logger.reverseDump(null, writer, new String[0]); 100 return buffer.toString(); 101 } 102 quote(String s)103 static String quote(String s) { 104 return '"' + s + '"'; 105 } 106 reverse(String[] ary)107 static String[] reverse(String[] ary) { 108 List<String> ls = Arrays.asList(ary); 109 Collections.reverse(ls); 110 return ls.toArray(new String[ary.length]); 111 } 112 } 113