1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.tools;
19 
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.util.EnumSet;
26 
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.fs.CreateFlag;
30 import org.apache.hadoop.fs.FSDataOutputStream;
31 import org.apache.hadoop.fs.FileContext;
32 import org.apache.hadoop.fs.Path;
33 import org.junit.Assert;
34 import org.junit.Test;
35 
36 public class TestLogalyzer {
37   private static String EL = System.getProperty("line.separator");
38   private static String TAB = "\t";
39   private static final Log LOG = LogFactory.getLog(TestLogalyzer.class);
40 
41   private static File workSpace = new File("target",
42       TestLogalyzer.class.getName() + "-workSpace");
43   private static File outdir = new File(workSpace.getAbsoluteFile()
44       + File.separator + "out");
45 
46   @Test
testLogalyzer()47   public void testLogalyzer() throws Exception {
48     Path f = createLogFile();
49 
50     String[] args = new String[10];
51 
52     args[0] = "-archiveDir";
53     args[1] = f.toString();
54     args[2] = "-grep";
55     args[3] = "44";
56     args[4] = "-sort";
57     args[5] = "0";
58     args[6] = "-analysis";
59     args[7] = outdir.getAbsolutePath();
60     args[8] = "-separator";
61     args[9] = " ";
62 
63     Logalyzer.main(args);
64     checkResult();
65 
66   }
67 
checkResult()68   private void checkResult() throws Exception {
69     File result = new File(outdir.getAbsolutePath() + File.separator
70         + "part-00000");
71     File success = new File(outdir.getAbsolutePath() + File.separator
72         + "_SUCCESS");
73     Assert.assertTrue(success.exists());
74 
75     FileInputStream fis = new FileInputStream(result);
76     BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
77     String line = br.readLine();
78     Assert.assertTrue(("1 44" + TAB + "2").equals(line));
79     line = br.readLine();
80 
81     Assert.assertTrue(("3 44" + TAB + "1").equals(line));
82     line = br.readLine();
83 
84     Assert.assertTrue(("4 44" + TAB + "1").equals(line));
85 
86     br.close();
87 
88   }
89 
90   /**
91    * Create simple log file
92    *
93    * @return
94    * @throws IOException
95    */
96 
createLogFile()97   private Path createLogFile() throws IOException {
98 
99     FileContext files = FileContext.getLocalFSFileContext();
100 
101     Path ws = new Path(workSpace.getAbsoluteFile().getAbsolutePath());
102 
103     files.delete(ws, true);
104     Path workSpacePath = new Path(workSpace.getAbsolutePath(), "log");
105     files.mkdir(workSpacePath, null, true);
106 
107     LOG.info("create logfile.log");
108     Path logfile1 = new Path(workSpacePath, "logfile.log");
109 
110     FSDataOutputStream os = files.create(logfile1,
111         EnumSet.of(CreateFlag.CREATE));
112     os.writeBytes("4 3" + EL + "1 3" + EL + "4 44" + EL);
113     os.writeBytes("2 3" + EL + "1 3" + EL + "0 45" + EL);
114     os.writeBytes("4 3" + EL + "1 3" + EL + "1 44" + EL);
115 
116     os.flush();
117     os.close();
118     LOG.info("create logfile1.log");
119 
120     Path logfile2 = new Path(workSpacePath, "logfile1.log");
121 
122     os = files.create(logfile2, EnumSet.of(CreateFlag.CREATE));
123     os.writeBytes("4 3" + EL + "1 3" + EL + "3 44" + EL);
124     os.writeBytes("2 3" + EL + "1 3" + EL + "0 45" + EL);
125     os.writeBytes("4 3" + EL + "1 3" + EL + "1 44" + EL);
126 
127     os.flush();
128     os.close();
129 
130     return workSpacePath;
131   }
132 }
133