summaryrefslogtreecommitdiff
path: root/tests/src/com/android/loganalysis/parser/LogcatParserTest.java
blob: 7adeed640fd597f0aecd06341bf23dfa6cf70f65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.loganalysis.parser;

import com.android.loganalysis.item.JavaCrashItem;
import com.android.loganalysis.item.LogcatItem;
import com.android.loganalysis.item.MiscLogcatItem;
import com.android.loganalysis.util.ArrayUtil;

import junit.framework.TestCase;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Unit tests for {@link LogcatParserTest}.
 */
public class LogcatParserTest extends TestCase {

    /**
     * Test that an ANR is parsed in the log.
     */
    public void testParse_anr() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 17:17:08.445   312   366 E ActivityManager: ANR (application not responding) in process: com.android.package",
                "04-25 17:17:08.445   312   366 E ActivityManager: Reason: keyDispatchingTimedOut",
                "04-25 17:17:08.445   312   366 E ActivityManager: Load: 0.71 / 0.83 / 0.51",
                "04-25 17:17:08.445   312   366 E ActivityManager: 33% TOTAL: 21% user + 11% kernel + 0.3% iowait");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getAnrs().size());
        assertEquals(312, logcat.getAnrs().get(0).getPid().intValue());
        assertEquals(366, logcat.getAnrs().get(0).getTid().intValue());
        assertEquals("", logcat.getAnrs().get(0).getLastPreamble());
        assertEquals("", logcat.getAnrs().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getAnrs().get(0).getEventTime());
    }

    /**
     * Test that an ANR is parsed in the log.
     */
    public void testParse_anr_pid() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 17:17:08.445   312   366 E ActivityManager: ANR (application not responding) in process: com.android.package",
                "04-25 17:17:08.445   312   366 E ActivityManager: PID: 1234",
                "04-25 17:17:08.445   312   366 E ActivityManager: Reason: keyDispatchingTimedOut",
                "04-25 17:17:08.445   312   366 E ActivityManager: Load: 0.71 / 0.83 / 0.51",
                "04-25 17:17:08.445   312   366 E ActivityManager: 33% TOTAL: 21% user + 11% kernel + 0.3% iowait");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getAnrs().size());
        assertEquals(1234, logcat.getAnrs().get(0).getPid().intValue());
        assertNull(logcat.getAnrs().get(0).getTid());
        assertEquals("", logcat.getAnrs().get(0).getLastPreamble());
        assertEquals("", logcat.getAnrs().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getAnrs().get(0).getEventTime());
    }

    /**
     * Test that Java crashes can be parsed.
     */
    public void testParse_java_crash() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertEquals(3064, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertEquals(3082, logcat.getJavaCrashes().get(0).getTid().intValue());
        assertEquals("", logcat.getJavaCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getJavaCrashes().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());
    }

    public void testParse_test_exception() {
        List<String> lines = Arrays.asList(
                "11-25 19:26:53.581  5832  7008 I TestRunner: ----- begin exception -----",
                "11-25 19:26:53.589  5832  7008 I TestRunner: ",
                "11-25 19:26:53.589  5832  7008 I TestRunner: java.util.concurrent.TimeoutException",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.support.test.uiautomator.WaitMixin.wait(WaitMixin.java:49)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.support.test.uiautomator.WaitMixin.wait(WaitMixin.java:36)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.support.test.uiautomator.UiDevice.wait(UiDevice.java:169)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at com.android.test.uiautomator.common.helpers.MapsHelper.doSearch(MapsHelper.java:87)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at com.android.test.uiautomator.aupt.MapsTest.testMaps(MapsTest.java:58)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at java.lang.reflect.Method.invoke(Native Method)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at java.lang.reflect.Method.invoke(Method.java:372)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at junit.framework.TestCase.runBare(TestCase.java:134)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at junit.framework.TestResult$1.protect(TestResult.java:115)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at junit.framework.TestResult.runProtected(TestResult.java:133)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at junit.framework.TestResult.run(TestResult.java:118)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at junit.framework.TestCase.run(TestCase.java:124)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.support.test.aupt.AuptTestRunner$AuptPrivateTestRunner.runTest(AuptTestRunner.java:182)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)",
                "11-25 19:26:53.589  5832  7008 I TestRunner:    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1848)",
                "11-25 19:26:53.589  5832  7008 I TestRunner: ----- end exception -----"
                );

        LogcatParser logcatParser = new LogcatParser("2012");
        logcatParser.addJavaCrashTag("I", "TestRunner", LogcatParser.JAVA_CRASH);
        LogcatItem logcat = logcatParser.parse(lines);
        assertNotNull(logcat);
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertEquals(5832, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertEquals(7008, logcat.getJavaCrashes().get(0).getTid().intValue());
        assertEquals("", logcat.getJavaCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getJavaCrashes().get(0).getProcessPreamble());
        assertEquals(LogcatParser.JAVA_CRASH, logcat.getJavaCrashes().get(0).getCategory());
    }

    public void testParse_test_exception_with_exras() {
        List<String> lines = Arrays.asList(
                "12-06 17:19:18.746  6598  7960 I TestRunner: failed: testYouTube(com.android.test.uiautomator.aupt.YouTubeTest)",
                "12-06 17:19:18.746  6598  7960 I TestRunner: ----- begin exception -----",
                "12-06 17:19:18.747  6598  7960 I TestRunner: ",
                "12-06 17:19:18.747  6598  7960 I TestRunner: java.util.concurrent.TimeoutException",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.support.test.uiautomator.WaitMixin.wait(WaitMixin.java:49)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.support.test.uiautomator.WaitMixin.wait(WaitMixin.java:36)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.support.test.uiautomator.UiDevice.wait(UiDevice.java:169)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.support.test.aupt.AppLauncher.launchApp(AppLauncher.java:127)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at com.android.test.uiautomator.common.helpers.YouTubeHelper.open(YouTubeHelper.java:49)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at com.android.test.uiautomator.aupt.YouTubeTest.setUp(YouTubeTest.java:44)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at junit.framework.TestCase.runBare(TestCase.java:132)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at junit.framework.TestResult$1.protect(TestResult.java:115)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at junit.framework.TestResult.runProtected(TestResult.java:133)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at junit.framework.TestResult.run(TestResult.java:118)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at junit.framework.TestCase.run(TestCase.java:124)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.support.test.aupt.AuptTestRunner$AuptPrivateTestRunner.runTest(AuptTestRunner.java:182)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)",
                "12-06 17:19:18.747  6598  7960 I TestRunner:    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1851)",
                "12-06 17:19:18.747  6598  7960 I TestRunner: ----- end exception -----"
                );

        LogcatParser logcatParser = new LogcatParser("2012");
        logcatParser.addJavaCrashTag("I", "TestRunner", LogcatParser.JAVA_CRASH);
        LogcatItem logcat = logcatParser.parse(lines);
        assertNotNull(logcat);
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertEquals(6598, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertEquals(7960, logcat.getJavaCrashes().get(0).getTid().intValue());
        assertEquals("", logcat.getJavaCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getJavaCrashes().get(0).getProcessPreamble());
        // Check that lines not related to java crash are absent
        assertFalse(logcat.getJavaCrashes().get(0).getStack().contains("begin exception"));
        assertFalse(logcat.getJavaCrashes().get(0).getStack().contains("end exception"));
        assertFalse(logcat.getJavaCrashes().get(0).getStack().contains("failed: testYouTube"));
        //System.out.println(logcat.getJavaCrashes().get(0).getStack());
    }

    /**
     * Test that Java crashes from system server can be parsed.
     */
    public void testParse_java_crash_system_server() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: message",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertEquals("system_server", logcat.getJavaCrashes().get(0).getApp());
        assertEquals(3064, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertEquals(3082, logcat.getJavaCrashes().get(0).getTid().intValue());
        assertEquals("", logcat.getJavaCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getJavaCrashes().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());
    }

    /**
     * Test that Java crashes with process and pid can be parsed.
     */
    public void testParse_java_crash_process_pid() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: FATAL EXCEPTION: main",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: Process: com.android.package, PID: 1234",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertEquals("com.android.package", logcat.getJavaCrashes().get(0).getApp());
        assertEquals(1234, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertNull(logcat.getJavaCrashes().get(0).getTid());
        assertEquals("", logcat.getJavaCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getJavaCrashes().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());
    }

    /**
     * Test that Java crashes with pid can be parsed.
     */
    public void testParse_java_crash_pid() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: FATAL EXCEPTION: main",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: PID: 1234",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertNull(logcat.getJavaCrashes().get(0).getApp());
        assertEquals(1234, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertNull(logcat.getJavaCrashes().get(0).getTid());
        assertEquals("", logcat.getJavaCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getJavaCrashes().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());
    }

    /**
     * Test that Java crashes with process and pid without stack can be parsed.
     */
    public void testParse_java_crash_empty() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: FATAL EXCEPTION: main",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: PID: 1234");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(0, logcat.getEvents().size());
        assertEquals(0, logcat.getJavaCrashes().size());
    }

    /**
     * Test that native crashes can be parsed from the info log level.
     */
    public void testParse_native_crash_info() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 18:33:27.273   115   115 I DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 18:33:27.273   115   115 I DEBUG   : pid: 3112, tid: 3112  >>> com.google.android.browser <<<",
                "04-25 18:33:27.273   115   115 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getNativeCrashes().size());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getPid().intValue());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getTid().intValue());
        assertEquals("com.google.android.browser", logcat.getNativeCrashes().get(0).getApp());
        assertEquals("", logcat.getNativeCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getNativeCrashes().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 18:33:27.273"),
                logcat.getNativeCrashes().get(0).getEventTime());
    }

    /**
     * Test that native crashes can be parsed from the fatal log level.
     */
    public void testParse_native_crash_fatal() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 18:33:27.273   115   115 F DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 18:33:27.273   115   115 F DEBUG   : pid: 3112, tid: 3112, name: Name  >>> com.google.android.browser <<<",
                "04-25 18:33:27.273   115   115 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getNativeCrashes().size());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getPid().intValue());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getTid().intValue());
        assertEquals("com.google.android.browser", logcat.getNativeCrashes().get(0).getApp());
        assertEquals("", logcat.getNativeCrashes().get(0).getLastPreamble());
        assertEquals("", logcat.getNativeCrashes().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 18:33:27.273"),
                logcat.getNativeCrashes().get(0).getEventTime());
    }

    /**
     * Test that native crashes can be parsed if they have the same pid/tid.
     */
    public void testParse_native_crash_same_pid() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 18:33:27.273   115   115 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***",
                "04-25 18:33:27.273   115   115 I DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 18:33:27.273   115   115 I DEBUG   : pid: 3112, tid: 3112  >>> com.google.android.browser <<<",
                "04-25 18:33:27.273   115   115 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000",
                "04-25 18:33:27.273   115   115 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***",
                "04-25 18:33:27.273   115   115 I DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 18:33:27.273   115   115 I DEBUG   : pid: 3113, tid: 3113  >>> com.google.android.browser2 <<<",
                "04-25 18:33:27.273   115   115 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStopTime());
        assertEquals(2, logcat.getEvents().size());
        assertEquals(2, logcat.getNativeCrashes().size());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getPid().intValue());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getTid().intValue());
        assertEquals("com.google.android.browser", logcat.getNativeCrashes().get(0).getApp());
        assertEquals(3113, logcat.getNativeCrashes().get(1).getPid().intValue());
        assertEquals(3113, logcat.getNativeCrashes().get(1).getTid().intValue());
        assertEquals("com.google.android.browser2", logcat.getNativeCrashes().get(1).getApp());
    }

    public void testParse_misc_events() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 18:33:27.273  1676  1821 W AudioTrack: obtainBuffer timed out (is the CPU pegged?) 0x361378 user=0000116a, server=00000000",
                "04-25 18:33:28.273  7813  7813 E gralloc : GetBufferLock timed out for thread 7813 buffer 0x61 usage 0x200 LockState 1",
                "04-25 18:33:29.273   395   637 W Watchdog: *** WATCHDOG KILLING SYSTEM PROCESS: null");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 18:33:29.273"), logcat.getStopTime());
        assertEquals(3, logcat.getEvents().size());
        assertEquals(1, logcat.getMiscEvents(LogcatParser.HIGH_CPU_USAGE).size());
        assertEquals(1, logcat.getMiscEvents(LogcatParser.HIGH_MEMORY_USAGE).size());
        assertEquals(1, logcat.getMiscEvents(LogcatParser.RUNTIME_RESTART).size());

        MiscLogcatItem item = logcat.getMiscEvents(LogcatParser.HIGH_CPU_USAGE).get(0);

        assertEquals(1676, item.getPid().intValue());
        assertEquals(1821, item.getTid().intValue());
        assertEquals(parseTime("2012-04-25 18:33:27.273"), item.getEventTime());

        item = logcat.getMiscEvents(LogcatParser.HIGH_MEMORY_USAGE).get(0);

        assertEquals(7813, item.getPid().intValue());
        assertEquals(7813, item.getTid().intValue());
        assertEquals(parseTime("2012-04-25 18:33:28.273"), item.getEventTime());

        item = logcat.getMiscEvents(LogcatParser.RUNTIME_RESTART).get(0);

        assertEquals(395, item.getPid().intValue());
        assertEquals(637, item.getTid().intValue());
        assertEquals(parseTime("2012-04-25 18:33:29.273"), item.getEventTime());
    }

    /**
     * Test that multiple events can be parsed.
     */
    public void testParse_multiple_events() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: \tat class.method3(Class.java:3)",
                "04-25 17:17:08.445   312   366 E ActivityManager: ANR (application not responding) in process: com.android.package",
                "04-25 17:17:08.445   312   366 E ActivityManager: Reason: keyDispatchingTimedOut",
                "04-25 17:17:08.445   312   366 E ActivityManager: Load: 0.71 / 0.83 / 0.51",
                "04-25 17:17:08.445   312   366 E ActivityManager: 33% TOTAL: 21% user + 11% kernel + 0.3% iowait",
                "04-25 17:17:08.445   312   366 E ActivityManager: ANR (application not responding) in process: com.android.package",
                "04-25 17:17:08.445   312   366 E ActivityManager: Reason: keyDispatchingTimedOut",
                "04-25 17:17:08.445   312   366 E ActivityManager: Load: 0.71 / 0.83 / 0.51",
                "04-25 17:17:08.445   312   366 E ActivityManager: 33% TOTAL: 21% user + 11% kernel + 0.3% iowait",
                "04-25 18:33:27.273   115   115 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***",
                "04-25 18:33:27.273   115   115 I DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 18:33:27.273   115   115 I DEBUG   : pid: 3112, tid: 3112  >>> com.google.android.browser <<<",
                "04-25 18:33:27.273   115   115 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000",
                "04-25 18:33:27.273   117   117 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***",
                "04-25 18:33:27.273   117   117 I DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 18:33:27.273   117   117 I DEBUG   : pid: 3113, tid: 3113  >>> com.google.android.browser <<<",
                "04-25 18:33:27.273   117   117 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000");


        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 18:33:27.273"), logcat.getStopTime());
        assertEquals(6, logcat.getEvents().size());
        assertEquals(2, logcat.getAnrs().size());
        assertEquals(2, logcat.getJavaCrashes().size());
        assertEquals(2, logcat.getNativeCrashes().size());

        assertEquals(312, logcat.getAnrs().get(0).getPid().intValue());
        assertEquals(366, logcat.getAnrs().get(0).getTid().intValue());
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getAnrs().get(0).getEventTime());

        assertEquals(312, logcat.getAnrs().get(1).getPid().intValue());
        assertEquals(366, logcat.getAnrs().get(1).getTid().intValue());
        assertEquals(parseTime("2012-04-25 17:17:08.445"), logcat.getAnrs().get(1).getEventTime());

        assertEquals(3064, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertEquals(3082, logcat.getJavaCrashes().get(0).getTid().intValue());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());

        assertEquals(3065, logcat.getJavaCrashes().get(1).getPid().intValue());
        assertEquals(3090, logcat.getJavaCrashes().get(1).getTid().intValue());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(1).getEventTime());

        assertEquals(3112, logcat.getNativeCrashes().get(0).getPid().intValue());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getTid().intValue());
        assertEquals(parseTime("2012-04-25 18:33:27.273"),
                logcat.getNativeCrashes().get(0).getEventTime());

        assertEquals(3113, logcat.getNativeCrashes().get(1).getPid().intValue());
        assertEquals(3113, logcat.getNativeCrashes().get(1).getTid().intValue());
        assertEquals(parseTime("2012-04-25 18:33:27.273"),
                logcat.getNativeCrashes().get(1).getEventTime());
    }

    /**
     * Test that multiple java crashes and native crashes can be parsed even when interleaved.
     */
    public void testParse_multiple_events_interleaved() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799   115   115 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***",
                "04-25 09:55:47.799   117   117 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799   115   115 I DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 09:55:47.799   117   117 I DEBUG   : Build fingerprint: 'product:build:target'",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799   115   115 I DEBUG   : pid: 3112, tid: 3112  >>> com.google.android.browser <<<",
                "04-25 09:55:47.799   117   117 I DEBUG   : pid: 3113, tid: 3113  >>> com.google.android.browser <<<",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)",
                "04-25 09:55:47.799  3065  3090 E AndroidRuntime: \tat class.method3(Class.java:3)",
                "04-25 09:55:47.799   115   115 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000",
                "04-25 09:55:47.799   117   117 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(4, logcat.getEvents().size());
        assertEquals(0, logcat.getAnrs().size());
        assertEquals(2, logcat.getJavaCrashes().size());
        assertEquals(2, logcat.getNativeCrashes().size());

        assertEquals(3064, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertEquals(3082, logcat.getJavaCrashes().get(0).getTid().intValue());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());

        assertEquals(3065, logcat.getJavaCrashes().get(1).getPid().intValue());
        assertEquals(3090, logcat.getJavaCrashes().get(1).getTid().intValue());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(1).getEventTime());

        assertEquals(3112, logcat.getNativeCrashes().get(0).getPid().intValue());
        assertEquals(3112, logcat.getNativeCrashes().get(0).getTid().intValue());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getNativeCrashes().get(0).getEventTime());

        assertEquals(3113, logcat.getNativeCrashes().get(1).getPid().intValue());
        assertEquals(3113, logcat.getNativeCrashes().get(1).getTid().intValue());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getNativeCrashes().get(1).getEventTime());
    }

    /**
     * Test that the preambles are set correctly.
     */
    public void testParse_preambles() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:15:47.799   123  3082 I tag: message 1",
                "04-25 09:20:47.799  3064  3082 I tag: message 2",
                "04-25 09:25:47.799   345  3082 I tag: message 3",
                "04-25 09:30:47.799  3064  3082 I tag: message 4",
                "04-25 09:35:47.799   456  3082 I tag: message 5",
                "04-25 09:40:47.799  3064  3082 I tag: message 6",
                "04-25 09:45:47.799   567  3082 I tag: message 7",
                "04-25 09:50:47.799  3064  3082 I tag: message 8",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");

        List<String> expectedLastPreamble = Arrays.asList(
                "04-25 09:15:47.799   123  3082 I tag: message 1",
                "04-25 09:20:47.799  3064  3082 I tag: message 2",
                "04-25 09:25:47.799   345  3082 I tag: message 3",
                "04-25 09:30:47.799  3064  3082 I tag: message 4",
                "04-25 09:35:47.799   456  3082 I tag: message 5",
                "04-25 09:40:47.799  3064  3082 I tag: message 6",
                "04-25 09:45:47.799   567  3082 I tag: message 7",
                "04-25 09:50:47.799  3064  3082 I tag: message 8");

        List<String> expectedProcPreamble = Arrays.asList(
                "04-25 09:20:47.799  3064  3082 I tag: message 2",
                "04-25 09:30:47.799  3064  3082 I tag: message 4",
                "04-25 09:40:47.799  3064  3082 I tag: message 6",
                "04-25 09:50:47.799  3064  3082 I tag: message 8");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:15:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertEquals(3064, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertEquals(3082, logcat.getJavaCrashes().get(0).getTid().intValue());
        assertEquals(ArrayUtil.join("\n", expectedLastPreamble),
                logcat.getJavaCrashes().get(0).getLastPreamble());
        assertEquals(ArrayUtil.join("\n", expectedProcPreamble),
                logcat.getJavaCrashes().get(0).getProcessPreamble());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());
    }

    /**
     * Test that events while the device is rebooting are ignored.
     */
    public void testParse_reboot() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:15:47.799   123  3082 I ShutdownThread: Rebooting, reason: null",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:15:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(0, logcat.getEvents().size());
    }

    /**
     * Test that events while the device is rebooting are ignored, but devices after the reboot are
     * captured.
     */
    public void testParse_reboot_resume() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:15:47.799   123  3082 I ShutdownThread: Rebooting, reason: null",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)",
                "logcat interrupted. May see duplicated content in log.--------- beginning of /dev/log/main",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: java.lang.Exception2",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");


        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:15:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:59:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals("java.lang.Exception2", logcat.getJavaCrashes().get(0).getException());

        lines = Arrays.asList(
                "04-25 09:15:47.799   123  3082 I ShutdownThread: Rebooting, reason: null",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)",
                "logcat interrupted. May see duplicated content in log.--------- beginning of main",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: java.lang.Exception2",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");


        logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:15:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:59:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals("java.lang.Exception2", logcat.getJavaCrashes().get(0).getException());
    }

    /**
     * Test that the time logcat format can be parsed.
     */
    public void testParse_time() throws ParseException {
        List<String> lines = Arrays.asList(
                "04-25 09:55:47.799  E/AndroidRuntime(3064): java.lang.Exception",
                "04-25 09:55:47.799  E/AndroidRuntime(3064): \tat class.method1(Class.java:1)",
                "04-25 09:55:47.799  E/AndroidRuntime(3064): \tat class.method2(Class.java:2)",
                "04-25 09:55:47.799  E/AndroidRuntime(3064): \tat class.method3(Class.java:3)");

        LogcatItem logcat = new LogcatParser("2012").parse(lines);
        assertNotNull(logcat);
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStartTime());
        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
        assertEquals(1, logcat.getEvents().size());
        assertEquals(1, logcat.getJavaCrashes().size());
        assertEquals(3064, logcat.getJavaCrashes().get(0).getPid().intValue());
        assertNull(logcat.getJavaCrashes().get(0).getTid());
        assertEquals(parseTime("2012-04-25 09:55:47.799"),
                logcat.getJavaCrashes().get(0).getEventTime());
    }

    /**
     * Test that we can add and find custom patterns that match based on logcat Tags only.
     */
    public void testAddPattern_byTagOnly() {
        List<String> lines = Arrays.asList(
                "04-25 18:33:28.273  7813  7813 E HelloTag: Hello everyone!!!1",
                "04-25 18:33:29.273   395   637 I Watchdog: find me!",
                "04-25 18:33:39.273   395   637 W Watchdog: find me!");

        LogcatParser parser = new LogcatParser("2012");
        assertNotNull(parser);
        parser.addPattern(null, null, "HelloTag", "HelloCategory");
        parser.addPattern(null, null, "Watchdog", "WatchdogCategory");
        LogcatItem logcat = parser.parse(lines);
        assertNotNull(logcat);

        /* verify that we find the HelloTag entry */
        List<MiscLogcatItem> matchedEvents = logcat.getMiscEvents("HelloCategory");
        assertEquals(1, matchedEvents.size());
        assertEquals("HelloTag", matchedEvents.get(0).getTag());

        /* verify that we find both Watchdog entries */
        matchedEvents = logcat.getMiscEvents("WatchdogCategory");
        assertEquals(2, matchedEvents.size());
        assertEquals("Watchdog", matchedEvents.get(0).getTag());
    }

    /**
     * Test that we can add and find custom patterns that match based on Level AND Tag.
     */
    public void testAddPattern_byLevelAndTagOnly() {
        List<String> lines = Arrays.asList(
                "04-25 18:33:28.273  7813  7813 E HelloTag: GetBufferLock timed out for thread 7813 buffer 0x61 usage 0x200 LockState 1",
                "04-25 18:33:29.273   395   637 I Watchdog: Info",
                "04-25 18:33:29.273   395   637 W Watchdog: Warning");

        LogcatParser parser = new LogcatParser("2012");
        assertNotNull(parser);
        parser.addPattern(null, "I", "Watchdog", "WatchdogCategory");
        LogcatItem logcat = parser.parse(lines);
        assertNotNull(logcat);

        List<MiscLogcatItem> matchedEvents = logcat.getMiscEvents("WatchdogCategory");
        assertEquals(1, matchedEvents.size());
        assertEquals("Watchdog", matchedEvents.get(0).getTag());
        assertEquals("Info", matchedEvents.get(0).getStack());
    }

    /**
     * Test that we can add and find custom patterns that match based on Level, Tag, AND Message.
     */
    public void testAddPattern_byLevelTagAndMessageOnly() {
        List<String> lines = Arrays.asList(
                "04-25 18:33:29.273   395   637 W Watchdog: I'm the one you need to find!",
                "04-25 18:33:29.273   395   637 W Watchdog: my message doesn't match.",
                "04-25 18:33:29.273   395   637 I Watchdog: my Level doesn't match, try and find me!",
                "04-25 18:33:29.273   395   637 W NotMe: my Tag doesn't match, try and find me!");

        LogcatParser parser = new LogcatParser("2012");
        assertNotNull(parser);
        parser.addPattern(Pattern.compile(".*find*."), "W", "Watchdog", "WatchdogCategory");
        LogcatItem logcat = parser.parse(lines);
        assertNotNull(logcat);

        /* verify that we find the only entry that matches based on Level, Tag, AND Message */
        List<MiscLogcatItem> matchedEvents = logcat.getMiscEvents("WatchdogCategory");
        assertEquals(1, matchedEvents.size());
        assertEquals("Watchdog", matchedEvents.get(0).getTag());
        assertEquals("I'm the one you need to find!", matchedEvents.get(0).getStack());
    }

    public void testFatalException() {
        List<String> lines = Arrays.asList(
                "06-05 06:14:51.529  1712  1712 D AndroidRuntime: Calling main entry com.android.commands.input.Input",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: main",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: java.lang.NullPointerException",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat android.hardware.input.InputManager.injectInputEvent(InputManager.java:641)",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat com.android.commands.input.Input.injectKeyEvent(Input.java:233)",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat com.android.commands.input.Input.sendKeyEvent(Input.java:184)",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat com.android.commands.input.Input.run(Input.java:96)",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat com.android.commands.input.Input.main(Input.java:59)",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)",
                "06-05 06:14:51.709  1712  1712 E AndroidRuntime: \tat dalvik.system.NativeStart.main(Native Method)");
        LogcatParser parser = new LogcatParser("2014");
        LogcatItem logcat = parser.parse(lines);
        assertEquals(1, logcat.getJavaCrashes().size());
        JavaCrashItem crash = logcat.getJavaCrashes().get(0);
        assertEquals("com.android.commands.input.Input", crash.getApp());
    }

    /**
     * Test that an empty input returns {@code null}.
     */
    public void testEmptyInput() {
        LogcatItem item = new LogcatParser().parse(Arrays.asList(""));
        assertNull(item);
    }

    /**
     * Test that after clearing a parser, reusing it produces a new LogcatItem instead of
     * appending to the previous one.
     */
    public void testClear() {
        List<String> lines = Arrays.asList(
                "04-25 18:33:28.273  7813  7813 E HelloTag: GetBufferLock timed out for thread 7813 buffer 0x61 usage 0x200 LockState 1",
                "04-25 18:33:28.273  7813  7813 E GoodbyeTag: GetBufferLock timed out for thread 7813 buffer 0x61 usage 0x200 LockState 1"
                );
        LogcatParser parser = new LogcatParser();
        LogcatItem l1 = parser.parse(lines.subList(0, 1));
        parser.clear();
        LogcatItem l2 = parser.parse(lines.subList(1, 2));
        assertEquals(l1.getEvents().size(), 1);
        assertEquals(l2.getEvents().size(), 1);
        assertFalse(l1.getEvents().get(0).getTag().equals(l2.getEvents().get(0).getTag()));
    }

    private Date parseTime(String timeStr) throws ParseException {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return formatter.parse(timeStr);
    }
}