source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/compress/TestCodecFactory.java @ 120

Last change on this file since 120 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: 5.0 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 */
18
19package org.apache.hadoop.io.compress;
20
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.OutputStream;
24import java.util.*;
25
26import junit.framework.TestCase;
27import org.apache.hadoop.fs.Path;
28import org.apache.hadoop.conf.Configuration;
29
30public class TestCodecFactory extends TestCase {
31
32  private static class BaseCodec implements CompressionCodec {
33    private Configuration conf;
34   
35    public void setConf(Configuration conf) {
36      this.conf = conf;
37    }
38   
39    public Configuration getConf() {
40      return conf;
41    }
42   
43    public CompressionOutputStream createOutputStream(OutputStream out) 
44    throws IOException {
45      return null;
46    }
47   
48    public Class<? extends Compressor> getCompressorType() {
49      return null;
50    }
51
52    public Compressor createCompressor() {
53      return null;
54    }
55
56    public CompressionInputStream createInputStream(InputStream in, 
57                                                    Decompressor decompressor) 
58    throws IOException {
59      return null;
60    }
61
62    public CompressionInputStream createInputStream(InputStream in) 
63    throws IOException {
64      return null;
65    }
66
67    public CompressionOutputStream createOutputStream(OutputStream out, 
68                                                      Compressor compressor) 
69    throws IOException {
70      return null;
71    }
72
73    public Class<? extends Decompressor> getDecompressorType() {
74      return null;
75    }
76
77    public Decompressor createDecompressor() {
78      return null;
79    }
80
81    public String getDefaultExtension() {
82      return ".base";
83    }
84  }
85 
86  private static class BarCodec extends BaseCodec {
87    public String getDefaultExtension() {
88      return "bar";
89    }
90  }
91 
92  private static class FooBarCodec extends BaseCodec {
93    public String getDefaultExtension() {
94      return ".foo.bar";
95    }
96  }
97 
98  private static class FooCodec extends BaseCodec {
99    public String getDefaultExtension() {
100      return ".foo";
101    }
102  }
103 
104  /**
105   * Returns a factory for a given set of codecs
106   * @param classes the codec classes to include
107   * @return a new factory
108   */
109  private static CompressionCodecFactory setClasses(Class[] classes) {
110    Configuration conf = new Configuration();
111    CompressionCodecFactory.setCodecClasses(conf, Arrays.asList(classes));
112    return new CompressionCodecFactory(conf);
113  }
114 
115  private static void checkCodec(String msg, 
116                                 Class expected, CompressionCodec actual) {
117    assertEquals(msg + " unexpected codec found",
118                 expected.getName(),
119                 actual.getClass().getName());
120  }
121 
122  public static void testFinding() {
123    CompressionCodecFactory factory = 
124      new CompressionCodecFactory(new Configuration());
125    CompressionCodec codec = factory.getCodec(new Path("/tmp/foo.bar"));
126    assertEquals("default factory foo codec", null, codec);
127    codec = factory.getCodec(new Path("/tmp/foo.gz"));
128    checkCodec("default factory for .gz", GzipCodec.class, codec);
129    codec = factory.getCodec(new Path("/tmp/foo.bz2"));
130    checkCodec("default factory for .bz2", BZip2Codec.class, codec);
131    factory = setClasses(new Class[0]);
132    codec = factory.getCodec(new Path("/tmp/foo.bar"));
133    assertEquals("empty codec bar codec", null, codec);
134    codec = factory.getCodec(new Path("/tmp/foo.gz"));
135    assertEquals("empty codec gz codec", null, codec);
136    codec = factory.getCodec(new Path("/tmp/foo.bz2"));
137    assertEquals("default factory for .bz2", null, codec);
138    factory = setClasses(new Class[]{BarCodec.class, FooCodec.class, 
139                                     FooBarCodec.class});
140    codec = factory.getCodec(new Path("/tmp/.foo.bar.gz"));
141    assertEquals("full factory gz codec", null, codec);
142    codec = factory.getCodec(new Path("/tmp/foo.bz2"));
143    assertEquals("default factory for .bz2", null, codec);
144    codec = factory.getCodec(new Path("/tmp/foo.bar"));
145    checkCodec("full factory bar codec", BarCodec.class, codec);
146    codec = factory.getCodec(new Path("/tmp/foo/baz.foo.bar"));
147    checkCodec("full factory foo bar codec", FooBarCodec.class, codec);
148    codec = factory.getCodec(new Path("/tmp/foo.foo"));
149    checkCodec("full factory foo codec", FooCodec.class, codec);
150  }
151}
Note: See TracBrowser for help on using the repository browser.