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.util;
19  
20  import junit.framework.TestCase;
21  import java.util.*;
22  import java.io.*;
23  import org.apache.hadoop.chukwa.ChukwaArchiveKey;
24  import org.apache.hadoop.chukwa.ChunkImpl;
25  import org.apache.hadoop.chukwa.datacollection.DataFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FSDataOutputStream;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.io.SequenceFile;
31  
32  public class TestDumpChunks extends TestCase {
33    
34    public static void writeSeqFile(Configuration conf, FileSystem fileSys, Path dest,
35        List<ChunkImpl> chunks) throws IOException {
36      FSDataOutputStream out = fileSys.create(dest);
37  
38      Calendar calendar = Calendar.getInstance();
39      SequenceFile.Writer seqFileWriter = SequenceFile.createWriter(conf, out,
40          ChukwaArchiveKey.class, ChunkImpl.class,
41          SequenceFile.CompressionType.NONE, null);
42      
43      for (ChunkImpl chunk: chunks) {
44        ChukwaArchiveKey archiveKey = new ChukwaArchiveKey();
45        
46        calendar.set(Calendar.SECOND, 0);
47        calendar.set(Calendar.MILLISECOND, 0);
48        archiveKey.setTimePartition(calendar.getTimeInMillis());
49        
50        archiveKey.setDataType(chunk.getDataType());
51        archiveKey.setStreamName(chunk.getStreamName());
52        archiveKey.setSeqId(chunk.getSeqID());
53        seqFileWriter.append(archiveKey, chunk);
54      }
55      seqFileWriter.close();
56      out.close();
57    }
58  
59    public void testFilePatternMatching() throws IOException, java.net.URISyntaxException {
60      
61      File tempDir = new File(System.getProperty("test.build.data", "/tmp"));
62  
63      File tmpFile = File.createTempFile("dumpchunkTest", ".seq", tempDir);
64      tmpFile.deleteOnExit();
65      
66      Configuration conf = new Configuration();
67      Path path = new Path(tmpFile.getAbsolutePath());
68      List<ChunkImpl> chunks = new ArrayList<ChunkImpl>();
69      byte[] dat = "test".getBytes();
70      
71      ChunkImpl c = new ChunkImpl("Data", "aname", dat.length, dat, null);
72      chunks.add(c);
73      
74      dat = "ing".getBytes();
75      c = new ChunkImpl("Data", "aname", dat.length+4, dat, null);
76      chunks.add(c);
77      
78      writeSeqFile(conf, FileSystem.getLocal(conf), path, chunks);
79      
80      String[] args = new String[] {"datatype=Data",path.toString()};
81      ByteArrayOutputStream capture = new ByteArrayOutputStream();
82      DumpChunks.dump(args, conf,new PrintStream(capture));
83      
84      assertTrue(new String(capture.toByteArray()).startsWith("testing\n---"));
85      //now test for matches.
86      
87    }
88  
89  }