Merge "Repalced StringBuffer with StringBuilder"
[so.git] / packages / arquillian-unit-tests / src / test / java / org / openecomp / mso / filesearching / LogFileSearching.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.filesearching;
22
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.io.UnsupportedEncodingException;
30 import java.util.NoSuchElementException;
31 import java.util.Scanner;
32
33 public class LogFileSearching {
34         
35         private static PrintWriter writer;
36         
37         public static void initFile(String filePath) {
38                 if (writer == null) {
39                         try {
40                                 // This is to reopen an existing file
41                                 writer = new PrintWriter(new FileOutputStream(filePath,true));
42                         } catch ( IOException e) {
43                                 System.out.println("Exception caught when trying to open the file /tmp/mso-log-checker.log to dump the result");
44                                 e.printStackTrace();
45                         }
46                 }
47         }
48         
49         public static void closeFile() {
50                 if (writer != null) {
51                         writer.close();
52                         writer = null;
53                 }
54         }
55         
56         
57         public static boolean searchInFile(final String needle, final File file) throws FileNotFoundException {
58                 Scanner logScanner = new Scanner(file);
59                 try {
60                         writer.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
61                         //System.out.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
62
63                         String filedata = logScanner.useDelimiter("\\Z").next();
64
65                         int occurrences = 0;
66                         int index = 0;
67
68                         while (index < filedata.length() && (index = filedata.indexOf(needle, index)) >= 0) {
69                                 occurrences++;
70                                 
71                                 int separatorIndex = filedata.indexOf(System.getProperty("line.separator"), index);
72                                 if (separatorIndex >=0){
73                                         writer.println("FOUND:" + filedata.substring(index, separatorIndex));
74                                         //System.out.println("FOUND:"
75                                                 //      + filedata.substring(index, separatorIndex));
76                                 } else {
77                                         writer.println("FOUND:" + filedata.substring(index, filedata.length()-1));
78                                         //System.out.println("FOUND:"
79                                                 //      + filedata.substring(index, filedata.length()-1));
80                                 }
81                                 index += needle.length();
82                         }
83                         writer.println("TOTAL:" + occurrences + " FOUND");
84                         //System.out.println("TOTAL:" + occurrences + " FOUND");
85                         if (occurrences > 0) {
86
87                                 return true;
88                         } else {
89
90                                 return false;
91                         }
92                 } catch (NoSuchElementException e) {
93                         writer.println("TOTAL:0 FOUND");
94                         //System.out.println("TOTAL:0 FOUND");
95                         return false;
96                 } finally {
97                         logScanner.close();
98                 }
99         }
100
101         public static boolean searchInDirectory(final String needle, final File directory) throws FileNotFoundException {
102
103                 boolean res = false;
104                 String[] dirFiles = directory.list();
105
106                 if (dirFiles != null) {
107
108                         for (String dir : dirFiles) {
109                                 res = res || searchInDirectory(needle, new File(directory.getAbsolutePath() + "/" + dir));
110                         }
111
112                 } else {
113                         return LogFileSearching.searchInFile(needle, directory);
114                 }
115                 return res;
116         }
117         
118         public static boolean searchInDirectoryForCommonIssues(final String[] needles, final File directory) throws FileNotFoundException {
119                 String[] defaultPatternsToUse = {"ClassNotFound","NullPointer","RuntimeException","IllegalStateException","FATAL"};
120                 
121                 if (needles != null && needles.length>0) {
122                         defaultPatternsToUse=needles;
123                 }
124                 
125                 boolean returnValue=false;
126                 for (String needle:defaultPatternsToUse) {
127                         returnValue |= LogFileSearching.searchInDirectory(needle,directory);
128                 }
129                                 
130                 return returnValue;
131         }
132 }