/** * Executes a specified task. If an exception occurs it will be wrapped in a RuntimeException * @param runnable * the task to be executed * @return * the result of the task execution */ public static void runUnchecked(Runnable runnable) { //NOSONAR runProtected(runnable).mapException(Exception.class, UncheckedException::new).get(); }
/** * Creates a new type converter for the given value. * * @param value * the value to be converted * * @return a new type converter */ public static TypeConverter convert(String value) { return new TypeConverter(value); }
/** * Constructor for successful outcome of an operation * @param result * the return value of the operation */ public static <RESULTTYPE> ExecutionResult<RESULTTYPE> ofSuccess(RESULTTYPE result) { return new ExecutionResult<>(result, null); }
/** * Defines a ZIP resource on the classpath that provides the static content the server should host. * @param contextRoot * the root path to the content * @param contentResource * the name of the classpath resource denoting a file that should be hosted. If the file denotes a zip file, its * content is hosted instead of the file itself. * The path may be absolute or relative to the caller of the method. * @return * this builder */ public HttpServerBuilder contentFrom(String contextRoot, String contentResource){ URL resource = resolver.resolve(contentResource,CallStack.getCallerClass()); resources.put(contextRoot, resource); return this; }
/** * Scans the specified jars for packages and returns a list of all packages found in that jar. The scanner makes no * distinction between packages and folders. * * @return a collection of all classes names found in the jar */ public Collection<String> scanClasses() { return scanJar(p -> isClassFile(p) && !isIgnored(p.toAbsolutePath())); }
/** * Resolves a URL of the resource specified. The resource path should be absolute, as there is no hint provided, * where to start the search. The resource is looked up using either the context classloader or the classloader * of the resolver itself. * @param resource * the resource to be resolved * @return * the URL of the resource or null if it wasnt found. * @throws java.lang.AssertionError * If the resolver is configured to fail on missing resource */ public URL resolve(String resource){ return resolve(resource, getClass()); }
/** * Flattens the execution result to the actual outcome of the execution. If an exception occured, it is throw, * if it returned any result - including null - it is returned. * @return * the actual return value of the operation * @throws Exception * if the execution produced an exception, it is thrown */ public RESULTTYPE flatten() throws Exception { //NOSONAR if (!wasSuccess()) { throw this.exception.get(); } return get(); }
/** * Executes the specified task, handling a any exception that occurs. * @param runnable * the task to be executed * @return * the execution result monad */ public static ExecutionResult<Void> runProtected(Runnable runnable) { //NOSONAR try { runnable.run(); //NOSONAR return ExecutionResult.ofVoid(); } catch (Exception e) { return ExecutionResult.ofException(e); } }
/** * Retrieves a parameter from the parameter provider set for this rule (or the default provider). If the parameter * is not set, an empty optional is returned. * * @param key * the key of the parameter used to identify the parameter value * @param type * the type of the parameter value * @param <T> * the type of the parameter value. The method performs an auto-conversion of primitive types, their Object * representation and Strings (no conversion). * * @return the parameter value */ public <T> Optional<T> getValue(String key, Class<T> type) { return provider.apply(key).map(value -> TypeUtils.convert(value).to(type)); }
private Result copy(Result old) throws Exception { final Result result = new Result(); copyFields(old, result); copyFailures(old, result); return result; }
public TemporaryFileBuilder(final TemporaryFolder folder, final String fileName) { this.folder = folder; this.filename = fileName; this.resolver = new ResourceResolver(true); }
/** * Creates a stream of classes representing the supertypes (excluding Object.class) of the specified type. * * @param type * the type whose supertypes should be returned as a stream * * @return stream of the supertypes of the specified class */ public static Stream<Class> supertypes(final Class type) { return selfAndSupertypes(type.getSuperclass()); }
/** * Initializes the test context with the provided set of properties * * @param input * the input parameters for the test * @param env * the environment parameters for the test execution. */ public static void init(Properties input, Properties env) { CURRENT_CONTEXT.set(new TestExecutionContext(input, env)); }
/** * Excludes the specified folders from scanning. The ignore pattern checks matches packages and classes * starting with the specified string. * * @param folder * folder(s) to be ignored * * @return this scanner */ public JarScanner ignore(String... folder) { return ignore(Arrays.asList(folder)); }
/** * Adds a jar file to be scanned * * @param jar * jar(s) to be added to the scanner * * @return this scanner */ public JarScanner addJar(URL... jar) { return addJar(Arrays.asList(jar)); }
/** * Defines the classpath resource from where the content of the file should be retrieved * * @param pathToResource * the path to the classpath resource * @return the builder */ public TemporaryFileBuilder fromClasspathResource(final String pathToResource) { final Class<?> callerClass = CallStack.getCallerClass(); this.content = getResolver().resolve(pathToResource, callerClass); return this; }
/** * Executes a specified task, regardless if it throws a checked or unchecked exception. * If an exception occurs it will be caught and wrapped in the execution result. * @param call * the task to be executed * @param <T> * the type of the return value * @return * the result of the task execution */ public static <T> T runUnchecked(Callable<T> call) { return runProtected(call).mapException(Exception.class, UncheckedException::new).get(); }
/** * Constructor for exceptional outcome of an operation * @param exception * the caught exception */ public static <RESULTTYPE> ExecutionResult<RESULTTYPE> ofException(Exception exception) { return new ExecutionResult<>(null, exception); }
public ZipFileBuilder(final TemporaryFolder folder, String filename) { this.folder = folder; this.filename = filename; this.entryMap = new HashMap<>(); this.resolver = new ResourceResolver(true); }
/** * Adds an entry to the zip file from a classpath resource. * * @param zipEntryPath * the path of the entry in the zip file. If the path denotes a path (ends with '/') the resource is put * under its own name on that location. If it denotes a file, it will be put as this file into the zip. Note * that even if the path ist defined absolute, starting with a '/', the created entry in the zip file won't * start with a '/' * @param pathToResource * the path to the resource in the classpath * * @return this builder */ public ZipFileBuilder addClasspathResource(final String zipEntryPath, final String pathToResource) { final Class<?> callerClass = getCallerClass(); final URL resource = resolver.resolve(pathToResource, callerClass); addResource(zipEntryPath, resource); return this; }