ExporterRepository { public ExporterDescriptor load(final String id, final Class<? extends Exporter> exporterClass) throws ExporterLoadException { return load(id, exporterClass, null); } ExporterRepository();  ExporterRepository(
      final Map<String, ExporterDescriptor> exporters, final ExporterJarRepository jarRepository); Map<String, ExporterDescriptor> getExporters(); ExporterDescriptor load(final String id, final Class<? extends Exporter> exporterClass); ExporterDescriptor load(
      final String id,
      final Class<? extends Exporter> exporterClass,
      final Map<String, Object> args); ExporterDescriptor load(final String id, final ExporterCfg config);  }
@Test public void shouldCacheDescriptorOnceLoaded() throws ExporterLoadException { final String id = "myExporter"; final Class<? extends Exporter> exporterClass = TestJarExporter.class; final ExporterDescriptor descriptor = repository.load(id, exporterClass, null); assertThat(descriptor).isNotNull(); assertThat(repository.load(id, exporterClass)).isSameAs(descriptor); }
@Test public void shouldFailToLoadIfExporterInvalid() { final String id = "exporter"; final Class<? extends Exporter> exporterClass = InvalidExporter.class; assertThatThrownBy(() -> repository.load(id, exporterClass)) .isInstanceOf(ExporterLoadException.class) .hasCauseInstanceOf(IllegalStateException.class); }
@Test public void shouldLoadInternalExporter() throws ExporterLoadException, ExporterJarLoadException { final ExporterCfg config = new ExporterCfg(); config.setClassName(ControlledTestExporter.class.getCanonicalName()); config.setJarPath(null); final ExporterDescriptor descriptor = repository.load("controlled", config); assertThat(config.isExternal()).isFalse(); assertThat(descriptor.newInstance()).isInstanceOf(ControlledTestExporter.class); }
@Test public void shouldLoadExternalExporter() throws Exception { final Class<? extends Exporter> exportedClass = TestJarExporter.class; final File jarFile = jarCreator.create(exportedClass); final ExporterCfg config = new ExporterCfg(); final Map<String, Object> args = new HashMap<>(); config.setClassName(exportedClass.getCanonicalName()); config.setJarPath(jarFile.getAbsolutePath()); config.setArgs(args); args.put("foo", 1); args.put("bar", false); final ExporterDescriptor descriptor = repository.load("exported", config); assertThat(config.isExternal()).isTrue(); assertThat(descriptor.getConfiguration().getArguments()).isEqualTo(config.getArgs()); assertThat(descriptor.getConfiguration().getId()).isEqualTo("exported"); assertThat(descriptor.newInstance().getClass().getCanonicalName()) .isEqualTo(exportedClass.getCanonicalName()); }
@Test public void shouldFailToLoadNonExporterClasses() throws IOException { final Class exportedClass = ExporterRepository.class; final File jarFile = jarCreator.create(exportedClass); final ExporterCfg config = new ExporterCfg(); final Map<String, Object> args = new HashMap<>(); config.setClassName(exportedClass.getCanonicalName()); config.setJarPath(jarFile.getAbsolutePath()); config.setArgs(args); assertThatThrownBy(() -> repository.load("exported", config)) .isInstanceOf(ExporterLoadException.class) .hasCauseInstanceOf(ClassCastException.class); }
@Test public void shouldFailToLoadNonExistingClass() throws IOException { final Class exportedClass = ExporterRepository.class; final File jarFile = jarCreator.create(exportedClass); final ExporterCfg config = new ExporterCfg(); final Map<String, Object> args = new HashMap<>(); config.setClassName("xyz.i.dont.Exist"); config.setJarPath(jarFile.getAbsolutePath()); config.setArgs(args); assertThatThrownBy(() -> repository.load("exported", config)) .isInstanceOf(ExporterLoadException.class) .hasCauseInstanceOf(ClassNotFoundException.class); }