source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/mapred/TestFileInputFormatPathFilter.java

Last change on this file was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.5 KB
Line 
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 */
18package org.apache.hadoop.mapred;
19
20import junit.framework.TestCase;
21
22import org.apache.hadoop.fs.FileStatus;
23import org.apache.hadoop.fs.FileSystem;
24import org.apache.hadoop.fs.Path;
25import org.apache.hadoop.fs.PathFilter;
26
27import java.io.IOException;
28import java.io.Writer;
29import java.io.OutputStreamWriter;
30import java.util.Set;
31import java.util.HashSet;
32
33public class TestFileInputFormatPathFilter extends TestCase {
34
35  public static class DummyFileInputFormat extends FileInputFormat {
36
37    public RecordReader getRecordReader(InputSplit split, JobConf job,
38                                        Reporter reporter) throws IOException {
39      return null;
40    }
41
42  }
43
44  private static FileSystem localFs = null;
45
46  static {
47    try {
48      localFs = FileSystem.getLocal(new JobConf());
49    } catch (IOException e) {
50      throw new RuntimeException("init failure", e);
51    }
52  }
53
54  private static Path workDir =
55      new Path(new Path(System.getProperty("test.build.data", "."), "data"),
56          "TestFileInputFormatPathFilter");
57
58
59  public void setUp() throws Exception {
60    tearDown();
61    localFs.mkdirs(workDir);
62  }
63
64  public void tearDown() throws Exception {
65    if (localFs.exists(workDir)) {
66      localFs.delete(workDir, true);
67    }
68  }
69
70  protected Path createFile(String fileName) throws IOException {
71    Path file = new Path(workDir, fileName);
72    Writer writer = new OutputStreamWriter(localFs.create(file));
73    writer.write("");
74    writer.close();
75    return localFs.makeQualified(file);
76  }
77
78  protected Set<Path> createFiles() throws IOException {
79    Set<Path> files = new HashSet<Path>();
80    files.add(createFile("a"));
81    files.add(createFile("b"));
82    files.add(createFile("aa"));
83    files.add(createFile("bb"));
84    files.add(createFile("_hello"));
85    files.add(createFile(".hello"));
86    return files;
87  }
88
89
90  public static class TestPathFilter implements PathFilter {
91
92    public boolean accept(Path path) {
93      String name = path.getName();
94      return name.equals("TestFileInputFormatPathFilter") || name.length() == 1;
95    }
96  }
97
98  private void _testInputFiles(boolean withFilter, boolean withGlob) throws Exception {
99    Set<Path> createdFiles = createFiles();
100    JobConf conf = new JobConf();
101
102    Path inputDir = (withGlob) ? new Path(workDir, "a*") : workDir;
103    FileInputFormat.setInputPaths(conf, inputDir);
104    conf.setInputFormat(DummyFileInputFormat.class);
105
106    if (withFilter) {
107      FileInputFormat.setInputPathFilter(conf, TestPathFilter.class);
108    }
109
110    DummyFileInputFormat inputFormat =
111        (DummyFileInputFormat) conf.getInputFormat();
112    Set<Path> computedFiles = new HashSet<Path>();
113    for (FileStatus file : inputFormat.listStatus(conf)) {
114      computedFiles.add(file.getPath());
115    }
116
117    createdFiles.remove(localFs.makeQualified(new Path(workDir, "_hello")));
118    createdFiles.remove(localFs.makeQualified(new Path(workDir, ".hello")));
119   
120    if (withFilter) {
121      createdFiles.remove(localFs.makeQualified(new Path(workDir, "aa")));
122      createdFiles.remove(localFs.makeQualified(new Path(workDir, "bb")));
123    }
124
125    if (withGlob) {
126      createdFiles.remove(localFs.makeQualified(new Path(workDir, "b")));
127      createdFiles.remove(localFs.makeQualified(new Path(workDir, "bb")));
128    }
129    assertEquals(createdFiles, computedFiles);
130  }
131
132  public void testWithoutPathFilterWithoutGlob() throws Exception {
133    _testInputFiles(false, false);
134  }
135
136  public void testWithoutPathFilterWithGlob() throws Exception {
137    _testInputFiles(false, true);
138  }
139
140  public void testWithPathFilterWithoutGlob() throws Exception {
141    _testInputFiles(true, false);
142  }
143
144  public void testWithPathFilterWithGlob() throws Exception {
145    _testInputFiles(true, true);
146  }
147}
Note: See TracBrowser for help on using the repository browser.