CopyFilter { public static CopyFilter getCopyFilter(Configuration conf) { String filtersClassName = conf .get(DistCpConstants.CONF_LABEL_FILTERS_CLASS); if (filtersClassName != null) { try { Class<? extends CopyFilter> filtersClass = conf .getClassByName(filtersClassName) .asSubclass(CopyFilter.class); filtersClassName = filtersClass.getName(); Constructor<? extends CopyFilter> constructor = filtersClass .getDeclaredConstructor(Configuration.class); return constructor.newInstance(conf); } catch (Exception e) { LOG.error(DistCpConstants.CLASS_INSTANTIATION_ERROR_MSG + filtersClassName, e); throw new RuntimeException( DistCpConstants.CLASS_INSTANTIATION_ERROR_MSG + filtersClassName, e); } } else { return getDefaultCopyFilter(conf); } }  void initialize(); abstract boolean shouldCopy(Path path); static CopyFilter getCopyFilter(Configuration conf);  }
@Test public void testGetCopyFilterTrueCopyFilter() { Configuration configuration = new Configuration(false); CopyFilter copyFilter = CopyFilter.getCopyFilter(configuration); assertTrue("copyFilter should be instance of TrueCopyFilter", copyFilter instanceof TrueCopyFilter); }
@Test public void testGetCopyFilterRegexCopyFilter() { Configuration configuration = new Configuration(false); configuration.set(DistCpConstants.CONF_LABEL_FILTERS_FILE, "random"); CopyFilter copyFilter = CopyFilter.getCopyFilter(configuration); assertTrue("copyFilter should be instance of RegexCopyFilter", copyFilter instanceof RegexCopyFilter); }
@Test public void testGetCopyFilterRegexpInConfigurationFilter() { final String filterName = "org.apache.hadoop.tools.RegexpInConfigurationFilter"; Configuration configuration = new Configuration(false); configuration.set(DistCpConstants.CONF_LABEL_FILTERS_CLASS, filterName); CopyFilter copyFilter = CopyFilter.getCopyFilter(configuration); assertTrue("copyFilter should be instance of RegexpInConfigurationFilter", copyFilter instanceof RegexpInConfigurationFilter); }
@Test public void testGetCopyFilterNonExistingClass() throws Exception { final String filterName = "org.apache.hadoop.tools.RegexpInConfigurationWrongFilter"; Configuration configuration = new Configuration(false); configuration.set(DistCpConstants.CONF_LABEL_FILTERS_CLASS, filterName); intercept(RuntimeException.class, DistCpConstants.CLASS_INSTANTIATION_ERROR_MSG + filterName, () -> CopyFilter.getCopyFilter(configuration)); }
@Test public void testGetCopyFilterWrongClassType() throws Exception { final String filterName = "org.apache.hadoop.tools." + "TestCopyFilter.FilterNotExtendingCopyFilter"; Configuration configuration = new Configuration(false); configuration.set(DistCpConstants.CONF_LABEL_FILTERS_CLASS, filterName); intercept(RuntimeException.class, DistCpConstants.CLASS_INSTANTIATION_ERROR_MSG + filterName, () -> CopyFilter.getCopyFilter(configuration)); }
@Test public void testGetCopyFilterEmptyString() throws Exception { final String filterName = ""; Configuration configuration = new Configuration(false); configuration.set(DistCpConstants.CONF_LABEL_FILTERS_CLASS, filterName); intercept(RuntimeException.class, DistCpConstants.CLASS_INSTANTIATION_ERROR_MSG + filterName, () -> CopyFilter.getCopyFilter(configuration)); }