View Javadoc

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.chukwa.datacollection.adaptor.filetailer;
19  
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import junit.framework.TestCase;
26  import org.apache.hadoop.chukwa.Chunk;
27  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
28  import org.apache.hadoop.chukwa.datacollection.connector.ChunkCatcherConnector;
29  import org.apache.hadoop.conf.Configuration;
30  import static org.apache.hadoop.chukwa.util.TempFileUtil.*;
31  
32  public class TestCharFileTailingAdaptorUTF8 extends TestCase {
33    ChunkCatcherConnector chunks;
34    File baseDir;
35    public TestCharFileTailingAdaptorUTF8() {
36  
37      baseDir = new File(System.getProperty("test.build.data", "/tmp"));
38      chunks = new ChunkCatcherConnector();
39      chunks.start();
40    }
41  
42    public void testCrSepAdaptor() throws IOException, InterruptedException,
43        ChukwaAgent.AlreadyRunningException {
44      
45      Configuration conf = new Configuration();
46      conf.set("chukwaAgent.control.port", "0");
47      ChukwaAgent agent = new ChukwaAgent(conf);
48      File testFile = makeTestFile("chukwaTest", 80,baseDir);
49      String adaptorId = agent
50          .processAddCommand("add adaptor_test = org.apache.hadoop.chukwa.datacollection.adaptor.filetailer.CharFileTailingAdaptorUTF8"
51              + " lines " + testFile + " 0");
52      assertTrue(adaptorId.equals("adaptor_test"));
53      System.out.println("getting a chunk...");
54      Chunk c = chunks.waitForAChunk();
55      assertTrue(c.getSeqID() == testFile.length());
56  
57      assertTrue(c.getRecordOffsets().length == 80);
58      int recStart = 0;
59      for (int rec = 0; rec < c.getRecordOffsets().length; ++rec) {
60        String record = new String(c.getData(), recStart,
61            c.getRecordOffsets()[rec] - recStart + 1);
62        assertTrue(record.equals(rec + " abcdefghijklmnopqrstuvwxyz\n"));
63        recStart = c.getRecordOffsets()[rec] + 1;
64      }
65      assertTrue(c.getDataType().equals("lines"));
66      agent.stopAdaptor(adaptorId, false);
67      agent.shutdown();
68      Thread.sleep(2000);
69    }
70  
71    
72  
73  }