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 package com.android.loganalysis.parser; 17 18 import static org.junit.Assert.fail; 19 20 import com.android.loganalysis.item.TraceFormatItem; 21 22 import org.junit.Assert; 23 import org.junit.Before; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 import java.util.Arrays; 29 import java.util.List; 30 import java.util.regex.Matcher; 31 32 /** Test for {@link TraceFormatParser}. */ 33 @RunWith(JUnit4.class) 34 public class TraceFormatParserTest { 35 private TraceFormatParser mParser; 36 37 // "unwrap" the regex strings so that we can compare with the generated regex 38 private static final String MATCH_NUM_UNESCAPED = 39 TraceFormatParser.MATCH_NUM.replaceAll("\\\\\\\\", "\\\\"); 40 private static final String MATCH_HEX_UNESCAPED = 41 TraceFormatParser.MATCH_HEX.replaceAll("\\\\\\\\", "\\\\"); 42 private static final String MATCH_STR_UNESCAPED = 43 TraceFormatParser.MATCH_STR.replaceAll("\\\\\\\\", "\\\\"); 44 45 @Before setUp()46 public void setUp() { 47 mParser = new TraceFormatParser(); 48 } 49 50 @Test testParseFormatLine()51 public void testParseFormatLine() { 52 List<String> formatLine = 53 Arrays.asList("print fmt: \"foo=%llu, bar=%s\", REC->foo, REC->bar"); 54 String expectedRegex = 55 String.format( 56 "foo=(?<foo>%s), bar=(?<bar>%s)", MATCH_NUM_UNESCAPED, MATCH_STR_UNESCAPED); 57 List<String> expectedParameters = Arrays.asList("foo", "bar"); 58 List<String> expectedNumericParameters = Arrays.asList("foo"); 59 List<String> expectedHexParameters = Arrays.asList(); 60 List<String> expectedStringParameters = Arrays.asList("bar"); 61 String shouldMatch = "foo=123, bar=enabled"; 62 63 TraceFormatItem parsedItem = mParser.parse(formatLine); 64 Assert.assertEquals(expectedParameters, parsedItem.getParameters()); 65 Assert.assertEquals(expectedNumericParameters, parsedItem.getNumericParameters()); 66 Assert.assertEquals(expectedHexParameters, parsedItem.getHexParameters()); 67 Assert.assertEquals(expectedStringParameters, parsedItem.getStringParameters()); 68 Assert.assertEquals(expectedRegex, parsedItem.getRegex().toString()); 69 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 70 Assert.assertTrue(m.matches()); 71 Assert.assertEquals(m.group("foo"), "123"); 72 Assert.assertEquals(m.group("bar"), "enabled"); 73 } 74 75 @Test testNoParameters()76 public void testNoParameters() { 77 List<String> formatLine = Arrays.asList("print fmt: \"foo\""); 78 String expectedRegex = "foo"; 79 List<String> expectedParameters = Arrays.asList(); 80 String shouldMatch = "foo"; 81 82 TraceFormatItem parsedItem = mParser.parse(formatLine); 83 Assert.assertEquals(expectedParameters, parsedItem.getParameters()); 84 Assert.assertEquals(expectedRegex, parsedItem.getRegex().toString()); 85 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 86 Assert.assertTrue(m.matches()); 87 } 88 89 @Test testNullInput()90 public void testNullInput() { 91 try { 92 mParser.parse(null); 93 fail("Expected an exception thrown by TraceFormatParser"); 94 } catch (RuntimeException e) { 95 // expected 96 } 97 } 98 99 @Test testEmptyInput()100 public void testEmptyInput() { 101 List<String> formatLine = Arrays.asList(""); 102 try { 103 mParser.parse(formatLine); 104 fail("Expected an exception thrown by TraceFormatParser"); 105 } catch (RuntimeException e) { 106 // expected 107 } 108 } 109 110 @Test testMultiLineInput()111 public void testMultiLineInput() { 112 List<String> formatLine = Arrays.asList("foo", "bar"); 113 try { 114 mParser.parse(formatLine); 115 fail("Expected an exception thrown by TraceFormatParser"); 116 } catch (RuntimeException e) { 117 // expected 118 } 119 } 120 121 @Test testOneLineInvalidInput()122 public void testOneLineInvalidInput() { 123 List<String> formatLine = Arrays.asList("foo bar"); 124 try { 125 mParser.parse(formatLine); 126 fail("Expected an exception thrown by TraceFormatParser"); 127 } catch (RuntimeException e) { 128 // expected 129 } 130 } 131 132 @Test testQuoteInParams()133 public void testQuoteInParams() { 134 List<String> formatLine = 135 Arrays.asList("print fmt: \"foo %s\", REC->foo ? \"online\" : \"offline\""); 136 String expectedRegex = String.format("foo (?<foo>%s)", MATCH_STR_UNESCAPED); 137 String shouldMatch = "foo online"; 138 139 TraceFormatItem parsedItem = mParser.parse(formatLine); 140 Assert.assertEquals(expectedRegex, parsedItem.getRegex().toString()); 141 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 142 Assert.assertTrue(m.matches()); 143 Assert.assertEquals(m.group("foo"), "online"); 144 } 145 146 @Test testCategorizeParameters()147 public void testCategorizeParameters() { 148 List<String> formatLine = 149 Arrays.asList( 150 "print fmt: \"num1=%lu, num2=%f, hex=%08x, str=%s\", REC->num1, REC->num2, REC->hex, REC->str"); 151 List<String> expectedNumericParameters = Arrays.asList("num1", "num2"); 152 List<String> expectedHexParameters = Arrays.asList("hex"); 153 List<String> expectedStringParameters = Arrays.asList("str"); 154 155 TraceFormatItem parsedItem = mParser.parse(formatLine); 156 Assert.assertEquals(expectedNumericParameters, parsedItem.getNumericParameters()); 157 Assert.assertEquals(expectedHexParameters, parsedItem.getHexParameters()); 158 Assert.assertEquals(expectedStringParameters, parsedItem.getStringParameters()); 159 } 160 161 @Test testCaseConvertParameterName()162 public void testCaseConvertParameterName() { 163 List<String> formatLine = Arrays.asList("print fmt: \"foo_bar=%llu\", REC->foo_bar"); 164 List<String> expectedParameters = Arrays.asList("fooBar"); 165 String shouldMatch = "foo_bar=123"; 166 167 TraceFormatItem parsedItem = mParser.parse(formatLine); 168 Assert.assertEquals(expectedParameters, parsedItem.getParameters()); 169 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 170 Assert.assertTrue(m.matches()); 171 Assert.assertEquals(m.group("fooBar"), "123"); 172 } 173 174 @Test testMatchInt()175 public void testMatchInt() { 176 List<String> formatLine = 177 Arrays.asList("print fmt: \"foo=%d, bar=%lu\", REC->foo, REC->bar"); 178 String shouldMatch = "foo=-123, bar=456"; 179 180 TraceFormatItem parsedItem = mParser.parse(formatLine); 181 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 182 Assert.assertTrue(m.matches()); 183 Assert.assertEquals(m.group("foo"), "-123"); 184 Assert.assertEquals(m.group("bar"), "456"); 185 } 186 187 @Test testMatchFloat()188 public void testMatchFloat() { 189 List<String> formatLine = 190 Arrays.asList("print fmt: \"foo=%f, bar=%.2f\", REC->foo, REC->bar"); 191 String shouldMatch = "foo=123.4567, bar=456.78"; 192 193 TraceFormatItem parsedItem = mParser.parse(formatLine); 194 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 195 Assert.assertTrue(m.matches()); 196 Assert.assertEquals(m.group("foo"), "123.4567"); 197 Assert.assertEquals(m.group("bar"), "456.78"); 198 } 199 200 @Test testMatchHex()201 public void testMatchHex() { 202 List<String> formatLine = 203 Arrays.asList( 204 "print fmt: \"foo=0x%04x, bar=0x%08X, baz=%x\", REC->foo, REC->bar, REC->baz"); 205 String shouldMatch = "foo=0x007b, bar=0x000001C8, baz=7b"; 206 207 TraceFormatItem parsedItem = mParser.parse(formatLine); 208 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 209 Assert.assertTrue(m.matches()); 210 Assert.assertEquals(m.group("foo"), "007b"); 211 Assert.assertEquals(m.group("bar"), "000001C8"); 212 Assert.assertEquals(m.group("baz"), "7b"); 213 } 214 215 @Test testMatchString()216 public void testMatchString() { 217 List<String> formatLine = 218 Arrays.asList("print fmt: \"foo=%s, bar=%s\", REC->foo, REC->bar"); 219 String shouldMatch = "foo=oof, bar=123"; 220 221 TraceFormatItem parsedItem = mParser.parse(formatLine); 222 Matcher m = parsedItem.getRegex().matcher(shouldMatch); 223 Assert.assertTrue(m.matches()); 224 Assert.assertEquals(m.group("foo"), "oof"); 225 Assert.assertEquals(m.group("bar"), "123"); 226 } 227 } 228