Initial OpenECOMP MSO commit
[so.git] / packages / arquillian-unit-tests / src / test / java / org / openecomp / mso / filesearching / LogFileSearching.java
1 package org.openecomp.mso.filesearching;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.io.UnsupportedEncodingException;
10 import java.util.NoSuchElementException;
11 import java.util.Scanner;
12
13 public class LogFileSearching {
14         
15         private static PrintWriter writer;
16         
17         public static void initFile(String filePath) {
18                 if (writer == null) {
19                         try {
20                                 // This is to reopen an existing file
21                                 writer = new PrintWriter(new FileOutputStream(filePath,true));
22                         } catch ( IOException e) {
23                                 System.out.println("Exception caught when trying to open the file /tmp/mso-log-checker.log to dump the result");
24                                 e.printStackTrace();
25                         }
26                 }
27         }
28         
29         public static void closeFile() {
30                 if (writer != null) {
31                         writer.close();
32                         writer = null;
33                 }
34         }
35         
36         
37         public static boolean searchInFile(final String needle, final File file) throws FileNotFoundException {
38                 Scanner logScanner = new Scanner(file);
39                 try {
40                         writer.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
41                         //System.out.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
42
43                         String filedata = logScanner.useDelimiter("\\Z").next();
44
45                         int occurrences = 0;
46                         int index = 0;
47
48                         while (index < filedata.length() && (index = filedata.indexOf(needle, index)) >= 0) {
49                                 occurrences++;
50                                 
51                                 int separatorIndex = filedata.indexOf(System.getProperty("line.separator"), index);
52                                 if (separatorIndex >=0){
53                                         writer.println("FOUND:" + filedata.substring(index, separatorIndex));
54                                         //System.out.println("FOUND:"
55                                                 //      + filedata.substring(index, separatorIndex));
56                                 } else {
57                                         writer.println("FOUND:" + filedata.substring(index, filedata.length()-1));
58                                         //System.out.println("FOUND:"
59                                                 //      + filedata.substring(index, filedata.length()-1));
60                                 }
61                                 index += needle.length();
62                         }
63                         writer.println("TOTAL:" + occurrences + " FOUND");
64                         //System.out.println("TOTAL:" + occurrences + " FOUND");
65                         if (occurrences > 0) {
66
67                                 return true;
68                         } else {
69
70                                 return false;
71                         }
72                 } catch (NoSuchElementException e) {
73                         writer.println("TOTAL:0 FOUND");
74                         //System.out.println("TOTAL:0 FOUND");
75                         return false;
76                 } finally {
77                         logScanner.close();
78                 }
79         }
80
81         public static boolean searchInDirectory(final String needle, final File directory) throws FileNotFoundException {
82
83                 boolean res = false;
84                 String[] dirFiles = directory.list();
85
86                 if (dirFiles != null) {
87
88                         for (String dir : dirFiles) {
89                                 res = res || searchInDirectory(needle, new File(directory.getAbsolutePath() + "/" + dir));
90                         }
91
92                 } else {
93                         return LogFileSearching.searchInFile(needle, directory);
94                 }
95                 return res;
96         }
97         
98         public static boolean searchInDirectoryForCommonIssues(final String[] needles, final File directory) throws FileNotFoundException {
99                 String[] defaultPatternsToUse = {"ClassNotFound","NullPointer","RuntimeException","IllegalStateException","FATAL"};
100                 
101                 if (needles != null && needles.length>0) {
102                         defaultPatternsToUse=needles;
103                 }
104                 
105                 boolean returnValue=false;
106                 for (String needle:defaultPatternsToUse) {
107                         returnValue |= LogFileSearching.searchInDirectory(needle,directory);
108                 }
109                                 
110                 return returnValue;
111         }
112 }