1 /* 2 * Copyright (C) 2013 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.util; 17 18 import junit.framework.TestCase; 19 20 import java.util.regex.Pattern; 21 22 /** 23 * Unit tests for {@link LogPatternUtil}. 24 */ 25 public class LogPatternUtilTest extends TestCase { 26 27 /** 28 * Test basic pattern matching. 29 */ testPatternMatching()30 public void testPatternMatching() { 31 LogPatternUtil patternUtil = new LogPatternUtil(); 32 patternUtil.addPattern(Pattern.compile("abc"), "cat1"); 33 patternUtil.addPattern(Pattern.compile("123"), "cat2"); 34 35 assertNull(patternUtil.checkMessage("xyz")); 36 assertEquals("cat1", patternUtil.checkMessage("abc")); 37 assertEquals("cat2", patternUtil.checkMessage("123")); 38 } 39 40 /** 41 * Test pattern matching with extras. 42 */ testExtrasMatching()43 public void testExtrasMatching() { 44 LogPatternUtil patternUtil = new LogPatternUtil(); 45 patternUtil.addPattern(Pattern.compile("abc"), null, "cat1"); 46 patternUtil.addPattern(Pattern.compile("123"), "E/tag1", "cat2"); 47 patternUtil.addPattern(Pattern.compile("123"), "E/tag2", "cat3"); 48 49 assertNull(patternUtil.checkMessage("xyz")); 50 assertEquals("cat1", patternUtil.checkMessage("abc")); 51 assertEquals("cat1", patternUtil.checkMessage("abc", "E/tag1")); 52 assertEquals("cat1", patternUtil.checkMessage("abc", "E/tag2")); 53 assertNull(patternUtil.checkMessage("123")); 54 assertEquals("cat2", patternUtil.checkMessage("123", "E/tag1")); 55 assertEquals("cat3", patternUtil.checkMessage("123", "E/tag2")); 56 } 57 } 58