Home | History | Annotate | Download | only in parser
      1 /*
      2  * Copyright (C) 2011 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.BugreportItem;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import java.io.BufferedReader;
     23 import java.io.FileNotFoundException;
     24 import java.io.FileReader;
     25 import java.io.IOException;
     26 
     27 /**
     28  * Functional tests for {@link BugreportParser}
     29  */
     30 public class BugreportParserFuncTest extends TestCase {
     31     // FIXME: Make bugreport file configurable.
     32     private static final String BUGREPORT_PATH = "/tmp/bugreport.txt";
     33 
     34     /**
     35      * A test that is intended to force Brillopad to parse a bugreport. The purpose of this is to
     36      * assist a developer in checking why a given bugreport file might not be parsed correctly by
     37      * Brillopad.
     38      */
     39     public void testParse() {
     40         BufferedReader bugreportReader = null;
     41         try {
     42             bugreportReader = new BufferedReader(new FileReader(BUGREPORT_PATH));
     43         } catch (FileNotFoundException e) {
     44             fail(String.format("File not found at %s", BUGREPORT_PATH));
     45         }
     46         BugreportItem bugreport = null;
     47         try {
     48             long start = System.currentTimeMillis();
     49             bugreport = new BugreportParser().parse(bugreportReader);
     50             long stop = System.currentTimeMillis();
     51             System.out.println(String.format("Bugreport took %d ms to parse.", stop - start));
     52         } catch (IOException e) {
     53             fail(String.format("IOException: %s", e.toString()));
     54         } finally {
     55             if (bugreportReader != null) {
     56                 try {
     57                     bugreportReader.close();
     58                 } catch (IOException e) {
     59                     // Ignore
     60                 }
     61             }
     62         }
     63 
     64         assertNotNull(bugreport);
     65         assertNotNull(bugreport.getTime());
     66 
     67         assertNotNull(bugreport.getSystemProps());
     68         assertTrue(bugreport.getSystemProps().size() > 0);
     69 
     70         assertNotNull(bugreport.getMemInfo());
     71         assertTrue(bugreport.getMemInfo().size() > 0);
     72 
     73         assertNotNull(bugreport.getProcrank());
     74         assertTrue(bugreport.getProcrank().getPids().size() > 0);
     75 
     76         assertNotNull(bugreport.getSystemLog());
     77         assertNotNull(bugreport.getSystemLog().getStartTime());
     78         assertNotNull(bugreport.getSystemLog().getStopTime());
     79 
     80         assertNotNull(bugreport.getLastKmsg());
     81 
     82         System.out.println(String.format("Stats for bugreport:\n" +
     83                 "  Time: %s\n" +
     84                 "  System Properties: %d items\n" +
     85                 "  Mem info: %d items\n" +
     86                 "  Procrank: %d items\n" +
     87                 "  System Log:\n" +
     88                 "    Start time: %s\n" +
     89                 "    Stop time: %s\n" +
     90                 "    %d ANR(s), %d Java Crash(es), %d Native Crash(es)\n" +
     91                 "    %d Kernel Reset(s), %d Kernel Error(s)",
     92                 bugreport.getTime(),
     93                 bugreport.getSystemProps().size(),
     94                 bugreport.getMemInfo().size(),
     95                 bugreport.getProcrank().getPids().size(),
     96                 bugreport.getSystemLog().getStartTime().toString(),
     97                 bugreport.getSystemLog().getStopTime().toString(),
     98                 bugreport.getSystemLog().getAnrs().size(),
     99                 bugreport.getSystemLog().getJavaCrashes().size(),
    100                 bugreport.getSystemLog().getNativeCrashes().size(),
    101                 bugreport.getLastKmsg().getMiscEvents(KernelLogParser.KERNEL_RESET).size(),
    102                 bugreport.getLastKmsg().getMiscEvents(KernelLogParser.KERNEL_ERROR).size()));
    103     }
    104 }
    105 
    106