/** * We load the report class by name, to be able to use third party report * formats. */ @SuppressWarnings("unchecked") private Class< ? extends Report> loadReportClass() throws ClassNotFoundException { return ( Class< ? extends Report> ) ClassUtils.loadClass(classLoader, reportClass); }
/** * Instantiate the document repository from a string, to be able to use * third party repositories. */ private DocumentRepository instantiateDocumentRepository() { DocumentRepository documentRepository = ClassUtils.createInstanceFromClassNameWithArguments(classLoader, documentRepositoryClass, DocumentRepository.class); return documentRepository; }
public static ClassLoader toClassLoader(Collection<String> classPaths) throws MalformedURLException { return toClassLoader(classPaths, ClassUtils.class.getClassLoader()); }
public T newInstance(Object... args) throws Throwable { Constructor< ? extends T> constructor = ClassUtils.findBestTypedConstructor(klass, args); return ClassUtils.invoke(constructor, args); }
@SuppressWarnings("unchecked") public static <T> Constructor<T> findPossibleConstructor(Class<T> klass, Object... args) throws NoSuchMethodException { for (Constructor< ? > constructor : klass.getConstructors()) { if (arityMatches(constructor, args)) { return ( Constructor<T> ) constructor; } } throw noSuitableConstructorException(klass, args); }
private static boolean typesMatch(Constructor< ? > constructor, Object[] args) { if ( ! arityMatches(constructor, args)) { return false; } Class< ? >[] parameterTypes = constructor.getParameterTypes(); parameterTypes = changePrimitivesTypesToNonOnes(parameterTypes); for (int i = 0; i < args.length; i ++ ) { if ( ! parameterTypes[i].isInstance(args[i])) { return false; } } return true; }
@Override public Object convert(String value) throws IllegalArgumentException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { List<String> parameters = toList(ClassUtils.escapeValues(value.split(";"))); Class< ? > klass = ClassUtils.loadClass(shift(parameters)); if (expectedType != null && ! expectedType.isAssignableFrom(klass)) { throw new IllegalArgumentException("Class " + expectedType.getName() + " is not assignable from" + klass .getName()); } if (parameters.size() == 0) { return klass.newInstance(); } String[] args = parameters.toArray(new String[parameters.size()]); Constructor< ? > constructor = klass.getConstructor(args.getClass()); return constructor.newInstance(new Object[] { args }); } }
classPaths.add(externalDependencyJarPath); ClassLoader customClassLoader = ClassUtils.toClassLoader(classPaths); ClassLoader fixtureClassLoader = ClassUtils.toClassLoaderWithDefaultParent(classPaths); SpecificationRunner runner = builder.classLoader(fixtureClassLoader).build(DocumentRunner.class);
public static <C> C createInstanceFromClassNameWithArguments(ClassLoader classLoader, String classWithArguments, Class<C> expectedType) throws UndeclaredThrowableException { try { List<String> parameters = toList(escapeValues(classWithArguments.split(";"))); Class< ? > klass = ClassUtils.loadClass(classLoader, shift(parameters)); if ( ! expectedType.isAssignableFrom(klass)) { throw new IllegalArgumentException("Class " + expectedType.getName() + " is not assignable from " + klass .getName()); } if (parameters.size() == 0) { return expectedType.cast(klass.newInstance()); } String[] args = parameters.toArray(new String[parameters.size()]); Constructor< ? > constructor = klass.getConstructor(args.getClass()); return expectedType.cast(constructor.newInstance(new Object[] { args })); } catch (ClassNotFoundException e) { throw new UndeclaredThrowableException(e); } catch (InstantiationException e) { throw new UndeclaredThrowableException(e); } catch (IllegalAccessException e) { throw new UndeclaredThrowableException(e); } catch (NoSuchMethodException e) { throw new UndeclaredThrowableException(e); } catch (InvocationTargetException e) { throw new UndeclaredThrowableException(e); } }
/** * We load the interpreter selector by a class name, to be able to use third * party interpreter selectors. */ @SuppressWarnings("unchecked") private Class< ? extends InterpreterSelector> loadInterpreterSelectorClass() throws ClassNotFoundException { return ( Class< ? extends InterpreterSelector> ) ClassUtils.loadClass(classLoader, interpreterSelectorClass); }
@Test public void shouldBuildRunnerWithExternalSystemUnderDevelopmentAndCustomClassLoader() throws Exception { URI specsFolderUri = getClass().getResource("/specs-executor/external").toURI(); File specsFolder = new File(specsFolderUri); String externalDependencyJarPath = new File(specsFolder, "external-calculator.jar").getAbsolutePath(); Set<String> classPaths = new HashSet<String>(); classPaths.add(externalDependencyJarPath); ClassLoader customClassLoader = ClassUtils.toClassLoader(classPaths); new SpecificationRunnerBuilder(DUMMY_REPO).classLoader(customClassLoader).systemUnderDevelopment( "info.novatec.testit.livingdoc.external.fixture.MyCustomSystemUnderDevelopment").build(DocumentRunner.class); }
/** * Instantiate the system under development from a string, to be able to use * third party SuDs. */ private SystemUnderDevelopment instantiateSystemUnderDevelopment() { LOG.debug("Creating SUD " + systemUnderDevelopmentClass); SystemUnderDevelopment systemUnderDevelopment = ClassUtils.createInstanceFromClassNameWithArguments(classLoader, systemUnderDevelopmentClass, SystemUnderDevelopment.class); systemUnderDevelopment.setClassLoader(classLoader); return systemUnderDevelopment; }
private static Class< ? > loadClass(String expectation) { try { return ClassUtils.loadClass(expectation); } catch (ClassNotFoundException ex) { LOG.trace(LOG_ERROR, ex); } return null; }
@Test public void shouldBuildRunnerWithExternalInterpreterSelectorAndCustomClassLoader() throws Exception { URI specsFolderUri = getClass().getResource("/specs-executor/external").toURI(); File specsFolder = new File(specsFolderUri); String externalDependencyJarPath = new File(specsFolder, "external-calculator.jar").getAbsolutePath(); Set<String> classPaths = new HashSet<String>(); classPaths.add(externalDependencyJarPath); ClassLoader customClassLoader = ClassUtils.toClassLoader(classPaths); new SpecificationRunnerBuilder(DUMMY_REPO).classLoader(customClassLoader).interpreterSelector( "info.novatec.testit.livingdoc.external.fixture.MyCustomInterpreterSelector").build(DocumentRunner.class); }
public DocumentRepository asDocumentRepository(ClassLoader classLoader, String user, String pwd) throws UndeclaredThrowableException { return ClassUtils.createInstanceFromClassNameWithArguments(classLoader, type.asFactoryArguments(this, true, user, pwd), DocumentRepository.class); }
public static Class< ? > loadClass(String className) throws ClassNotFoundException { // Instead of the Thread.currentThread().getContextClassLoader() we are // using the current class classLoader, to be compatible with a OSGI // environment (mainly for the confluence plugin). For more information // see: https://wiki.eclipse.org/Context_Class_Loader_Enhancements or // http://njbartlett.name/2010/08/30/osgi-readiness-loading-classes.html return loadClass(ClassUtils.class.getClassLoader(), className); }
ClassLoader currentClassLoader = getClass().getClassLoader(); Collection<String> dependencies = resolveDependentClassPaths(systemUnderTest); ClassLoader dependencyCassLoader = ClassUtils.toClassLoader(dependencies, currentClassLoader);
@Test public void shouldInstantiateClassFromArguments() throws Exception { String className = MySytemUnderDevelopment.class.getName(); String classNameWithArguments = className + ";arg1;arg2;arg3"; SystemUnderDevelopment sud = ClassUtils.createInstanceFromClassNameWithArguments(Thread.currentThread() .getContextClassLoader(), classNameWithArguments, SystemUnderDevelopment.class); assertTrue(sud instanceof MySytemUnderDevelopment); MySytemUnderDevelopment mySud = ( MySytemUnderDevelopment ) sud; assertEquals(mySud.getParam(0), "arg1"); assertEquals(mySud.getParam(1), "arg2"); assertEquals(mySud.getParam(2), "arg3"); }
private DocumentRepository getRepository(List<String> definition) throws SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> klass = ClassUtils.loadClass(definition.get(0)); Constructor<?> constructor = klass.getConstructor(String[].class); return (DocumentRepository) constructor.newInstance(new Object[]{args(definition)}); }
@Test public void shouldExecuteDocumentSpecificationWithExternalFixtureSource() throws Exception { URI specsFolderUri = getClass().getResource("/specs-executor/external").toURI(); File outputDirectory = TestFileUtils.createTempReportsDirectory(); File specsFolder = new File(specsFolderUri); String repository = FileSystemRepository.class.getName() + ";" + specsFolder; // This jar contains a compiled calculator class. If you want to adjust // this calculator, you've to create a new jar by yourself. String externalDependencyJarPath = new File(specsFolder, "external-calculator.jar").getAbsolutePath(); Set<String> classPaths = new HashSet<String>(); classPaths.add(externalDependencyJarPath); ClassLoader customClassLoader = ClassUtils.toClassLoader(classPaths); RecorderMonitor recorderMonitor = new RecorderMonitor(); LoggingMonitor loggingMonitor = new LoggingMonitor(); SpecificationRunner runner = new SpecificationRunnerBuilder(repository).classLoader(customClassLoader) .systemUnderDevelopment(DefaultSystemUnderDevelopment.class.getName()).outputDirectory(outputDirectory).monitors( recorderMonitor, loggingMonitor).build(DocumentRunner.class); new SpecificationRunnerExecutor(runner).execute("AExternalCalculatorSample.html"); Statistics statistics = recorderMonitor.getStatistics(); Assert.assertEquals(6, statistics.totalCount()); Assert.assertEquals(4, statistics.rightCount()); Assert.assertEquals(1, statistics.wrongCount()); Assert.assertEquals(0, statistics.ignoredCount()); Assert.assertEquals(1, statistics.exceptionCount()); }