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 | |
---|
19 | package org.apache.hadoop.io.compress; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.io.InputStream; |
---|
23 | import java.io.OutputStream; |
---|
24 | import java.util.*; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | import org.apache.hadoop.fs.Path; |
---|
28 | import org.apache.hadoop.conf.Configuration; |
---|
29 | |
---|
30 | public 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 | } |
---|