1 /* 2 * Copyright (C) 2012 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 com.android.loganalysis.item.JavaCrashItem; 19 import com.android.loganalysis.util.ArrayUtil; 20 21 import junit.framework.TestCase; 22 23 import java.util.Arrays; 24 import java.util.List; 25 26 /** 27 * Unit tests for {@link JavaCrashParser}. 28 */ 29 public class JavaCrashParserTest extends TestCase { 30 31 /** 32 * Test that Java crashes are parsed with no message. 33 */ testParse_no_message()34 public void testParse_no_message() { 35 List<String> lines = Arrays.asList( 36 "java.lang.Exception", 37 "\tat class.method1(Class.java:1)", 38 "\tat class.method2(Class.java:2)", 39 "\tat class.method3(Class.java:3)"); 40 41 JavaCrashItem jc = new JavaCrashParser().parse(lines); 42 assertNotNull(jc); 43 assertEquals("java.lang.Exception", jc.getException()); 44 assertNull(jc.getMessage()); 45 assertEquals(ArrayUtil.join("\n", lines), jc.getStack()); 46 } 47 48 /** 49 * Test that Java crashes are parsed with a message. 50 */ testParse_message()51 public void testParse_message() { 52 List<String> lines = Arrays.asList( 53 "java.lang.Exception: This is the message", 54 "\tat class.method1(Class.java:1)", 55 "\tat class.method2(Class.java:2)", 56 "\tat class.method3(Class.java:3)"); 57 58 JavaCrashItem jc = new JavaCrashParser().parse(lines); 59 assertNotNull(jc); 60 assertEquals("java.lang.Exception", jc.getException()); 61 assertEquals("This is the message", jc.getMessage()); 62 assertEquals(ArrayUtil.join("\n", lines), jc.getStack()); 63 } 64 65 /** 66 * Test that Java crashes are parsed if the message spans multiple lines. 67 */ testParse_multiline_message()68 public void testParse_multiline_message() { 69 List<String> lines = Arrays.asList( 70 "java.lang.Exception: This message", 71 "is many lines", 72 "long.", 73 "\tat class.method1(Class.java:1)", 74 "\tat class.method2(Class.java:2)", 75 "\tat class.method3(Class.java:3)"); 76 77 JavaCrashItem jc = new JavaCrashParser().parse(lines); 78 assertNotNull(jc); 79 assertEquals("java.lang.Exception", jc.getException()); 80 assertEquals("This message\nis many lines\nlong.", jc.getMessage()); 81 assertEquals(ArrayUtil.join("\n", lines), jc.getStack()); 82 } 83 84 /** 85 * Test that caused by sections of Java crashes are parsed, with no message or single or 86 * multiline messages. 87 */ testParse_caused_by()88 public void testParse_caused_by() { 89 List<String> lines = Arrays.asList( 90 "java.lang.Exception: This is the message", 91 "\tat class.method1(Class.java:1)", 92 "\tat class.method2(Class.java:2)", 93 "\tat class.method3(Class.java:3)", 94 "Caused by: java.lang.Exception", 95 "\tat class.method4(Class.java:4)", 96 "Caused by: java.lang.Exception: This is the caused by message", 97 "\tat class.method5(Class.java:5)", 98 "Caused by: java.lang.Exception: This is a multiline", 99 "caused by message", 100 "\tat class.method6(Class.java:6)"); 101 102 JavaCrashItem jc = new JavaCrashParser().parse(lines); 103 assertNotNull(jc); 104 assertEquals("java.lang.Exception", jc.getException()); 105 assertEquals("This is the message", jc.getMessage()); 106 assertEquals(ArrayUtil.join("\n", lines), jc.getStack()); 107 } 108 109 /** 110 * Test that the Java crash is cutoff if an unexpected line is handled. 111 */ testParse_cutoff()112 public void testParse_cutoff() { 113 List<String> lines = Arrays.asList( 114 "java.lang.Exception: This is the message", 115 "\tat class.method1(Class.java:1)", 116 "\tat class.method2(Class.java:2)", 117 "\tat class.method3(Class.java:3)", 118 "Invalid line", 119 "java.lang.Exception: This is the message"); 120 121 JavaCrashItem jc = new JavaCrashParser().parse(lines); 122 assertNotNull(jc); 123 assertEquals("java.lang.Exception", jc.getException()); 124 assertEquals("This is the message", jc.getMessage()); 125 assertEquals(ArrayUtil.join("\n", lines.subList(0, lines.size()-2)), jc.getStack()); 126 } 127 128 /** 129 * Tests that only parts between the markers are parsed. 130 */ testParse_begin_end_markers()131 public void testParse_begin_end_markers() { 132 List<String> lines = Arrays.asList( 133 "error: this message has begin and end", 134 "----- begin exception -----", 135 "java.lang.Exception: This message", 136 "is many lines", 137 "long.", 138 "\tat class.method1(Class.java:1)", 139 "\tat class.method2(Class.java:2)", 140 "\tat class.method3(Class.java:3)", 141 "----- end exception -----"); 142 143 JavaCrashItem jc = new JavaCrashParser().parse(lines); 144 assertNotNull(jc); 145 assertEquals("java.lang.Exception", jc.getException()); 146 assertEquals("This message\nis many lines\nlong.", jc.getMessage()); 147 assertNotNull(jc.getStack()); 148 assertFalse(jc.getStack().contains("begin exception")); 149 assertFalse(jc.getStack().contains("end exception")); 150 } 151 } 152