MrUtil { public static String getCodecString(Configuration configuration) { boolean compress = configuration.getBoolean(FileOutputFormat.COMPRESS, false); String codecType = configuration.get(FileOutputFormat.COMPRESS_TYPE, null); if (compress && (codecType == null || !codecType.equals(CompressionType.NONE.toString()))) { Class<?> codecClass = configuration.getClass(FileOutputFormat.COMPRESS_CODEC, DefaultCodec.class); if (codecClass == null) { return CODEC_NONE; } else { try { return ((CompressionCodec) codecClass.newInstance()).getDefaultExtension().replace(".", ""); } catch (Exception exception) { throw new RuntimeException("Could not determine codec", exception); } } } return CODEC_NONE; }  static String getCodecString(Configuration configuration); static final String CODEC_NONE; }
@Test public void testGetCodecString() { Configuration configuration = dfsServer.getConf(); assertEquals(MrUtil.CODEC_NONE, MrUtil.getCodecString(configuration)); configuration.setBoolean(FileOutputFormat.COMPRESS, false); assertEquals(MrUtil.CODEC_NONE, MrUtil.getCodecString(configuration)); configuration.setBoolean(FileOutputFormat.COMPRESS, true); assertEquals(new DefaultCodec().getDefaultExtension().substring(1, new DefaultCodec().getDefaultExtension().length()), MrUtil.getCodecString(configuration)); configuration.set(FileOutputFormat.COMPRESS_CODEC, SnappyCodec.class.getName()); assertEquals(new SnappyCodec().getDefaultExtension().substring(1, new SnappyCodec().getDefaultExtension().length()), MrUtil.getCodecString(configuration)); configuration.set(FileOutputFormat.COMPRESS_TYPE, CompressionType.BLOCK.toString()); assertEquals(new SnappyCodec().getDefaultExtension().substring(1, new SnappyCodec().getDefaultExtension().length()), MrUtil.getCodecString(configuration)); configuration.set(FileOutputFormat.COMPRESS_TYPE, CompressionType.NONE.toString()); assertEquals(MrUtil.CODEC_NONE, MrUtil.getCodecString(configuration)); configuration.set(FileOutputFormat.COMPRESS_TYPE, CompressionType.BLOCK.toString()); configuration.setBoolean(FileOutputFormat.COMPRESS, false); assertEquals(MrUtil.CODEC_NONE, MrUtil.getCodecString(configuration)); }