/** Asserts that the given elements in the resources directory are transformed as expected. */ public StepHarness testResource(String resourceBefore, String resourceAfter) throws Exception { String before = ResourceHarness.getTestResource(resourceBefore); String after = ResourceHarness.getTestResource(resourceAfter); return test(before, after); }
/** Wraps the given {@link ThrowingEx.Function} as a standard {@link java.util.function.Function}, rethrowing any exceptions as runtime exceptions. */ public static <T, R> java.util.function.Function<T, R> wrap(ThrowingEx.Function<T, R> function) { return input -> { try { return function.apply(input); } catch (Exception t) { throw asRuntime(t); } }; }
@Override protected Formatter create() { return Formatter.builder() .lineEndingsPolicy(lineEndingsPolicy) .encoding(encoding) .rootDir(rootDir) .steps(steps) .exceptionPolicy(exceptionPolicy) .build(); } }.testEquals();
/** Get formatter preferences */ public Properties getPreferences() { //Keep the IllegalArgumentException since it contains detailed information FormatterProperties preferences = FormatterProperties.from(settingsFiles.files()); return preferences.getProperties(); }
/** Creates a harness for testing steps which don't depend on the file. */ public static StepHarness forStep(FormatterStep step) { // We don't care if an individual FormatterStep is misbehaving on line-endings, because // Formatter fixes that. No reason to care in tests either. It's likely to pop up when // running tests on Windows from time-to-time return new StepHarness(FormatterFunc.Closeable.of( () -> { if (step instanceof FormatterStepImpl.Standard) { ((FormatterStepImpl.Standard<?>) step).cleanupFormatterFunc(); } }, input -> LineEnding.toUnix(step.format(input, new File(""))))); }
/** Creates a harness for testing a formatter whose steps don't depend on the file. */ public static StepHarness forFormatter(Formatter formatter) { return new StepHarness(FormatterFunc.Closeable.of( formatter::close, input -> formatter.compute(input, new File("")))); }
@Test public void differntPropertyFileTypes() throws IOException { for (String settingsResource : VALID_SETTINGS_RESOURCES) { File settingsFile = createTestFile(settingsResource); FormatterProperties preferences = FormatterProperties.from(settingsFile); assertFor(preferences) .containsSpecificValuesOf(settingsFile) .containsCommonValueOf(settingsFile); } }
/** State constructor expects that all passed items are not modified afterwards */ protected State(String formatterStepExt, Provisioner jarProvisioner, List<String> dependencies, Iterable<File> settingsFiles) throws IOException { this.jarState = JarState.withoutTransitives(dependencies, jarProvisioner); this.settingsFiles = FileSignature.signAsList(settingsFiles); this.formatterStepExt = formatterStepExt; }
@Override protected FormatterStep create() { FormatterStep baseStep = FormatterStep.create("name", state, state -> input -> state); if (filter == null) { return baseStep; } else { return baseStep.filterByFile(SerializableFileFilter.skipFilesNamed(filter)); } } }.testEquals();
public void sameAsResource(String resource) throws IOException { hasContent(getTestResource(resource)); }
@Override protected String format(State state, String rawUnix, File file) throws Exception { Objects.requireNonNull(state, "state"); Objects.requireNonNull(rawUnix, "rawUnix"); Objects.requireNonNull(file, "file"); if (formatter == null) { formatter = stateToFormatter.apply(state()); } return formatter.apply(rawUnix, file); }
public Formatter build() { return new Formatter(lineEndingsPolicy, encoding, rootDir, steps, exceptionPolicy == null ? FormatExceptionPolicy.failOnlyOnError() : exceptionPolicy); } }
/** Asserts that the given elements in the resources directory are transformed as expected. */ public StepHarness testResourceUnaffected(String resourceIdempotent) throws Exception { String idempotentElement = ResourceHarness.getTestResource(resourceIdempotent); return testUnaffected(idempotentElement); }
/** Creates file signature insensitive to the order of the files. */ public static FileSignature signAsSet(Iterable<File> files) throws IOException { return new FileSignature(toSortedSet(files)); }
/** Creates file signature whereas order of the files remains unchanged. */ public static FileSignature signAsList(Iterable<File> files) throws IOException { return new FileSignature(toNullHostileList(files)); }
@SuppressWarnings("rawtypes") @Override public void close() { for (FormatterStep step : steps) { if (step instanceof FormatterStepImpl.Standard) { ((FormatterStepImpl.Standard) step).cleanupFormatterFunc(); } } } }
@Override protected void setupTest(API api) { settingsFiles = xmlFile; api.areDifferentThan(); settingsFiles = propFile; api.areDifferentThan(); }
/** Creates a FileFilter which will accept all files except files with the given name(s). */ public static SerializableFileFilter skipFilesNamed(String... names) { return new SerializableFileFilterImpl.SkipFilesNamed(names); } }