11/*
2- * Copyright 2016-2021 DiffPlug
2+ * Copyright 2016-2023 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2222import java .nio .file .Paths ;
2323import java .util .Arrays ;
2424import java .util .Objects ;
25- import java .util .function .Consumer ;
2625
27- import org .assertj .core .api .AbstractThrowableAssert ;
26+ import org .assertj .core .api .AbstractStringAssert ;
2827import org .assertj .core .api .Assertions ;
2928
3029/** An api for testing a {@code FormatterStep} that doesn't depend on the File path. DO NOT ADD FILE SUPPORT TO THIS, use {@link StepHarnessWithFile} if you need that. */
3130public class StepHarness implements AutoCloseable {
32- private final FormatterFunc formatter ;
31+ private final Formatter formatter ;
3332
34- private StepHarness (FormatterFunc formatter ) {
33+ private StepHarness (Formatter formatter ) {
3534 this .formatter = Objects .requireNonNull (formatter );
3635 }
3736
3837 /** Creates a harness for testing steps which don't depend on the file. */
3938 public static StepHarness forStep (FormatterStep step ) {
40- // We don't care if an individual FormatterStep is misbehaving on line-endings, because
41- // Formatter fixes that. No reason to care in tests either. It's likely to pop up when
42- // running tests on Windows from time-to-time
43- return new StepHarness (FormatterFunc .Closeable .ofDangerous (
44- () -> {
45- if (step instanceof FormatterStepImpl .Standard ) {
46- ((FormatterStepImpl .Standard <?>) step ).cleanupFormatterFunc ();
47- }
48- },
49- input -> LineEnding .toUnix (step .format (input , new File ("" )))));
39+ return forSteps (step );
5040 }
5141
5242 /** Creates a harness for testing steps which don't depend on the file. */
@@ -56,60 +46,68 @@ public static StepHarness forSteps(FormatterStep... steps) {
5646 .lineEndingsPolicy (LineEnding .UNIX .createPolicy ())
5747 .encoding (StandardCharsets .UTF_8 )
5848 .rootDir (Paths .get ("" ))
49+ .exceptionPolicy (new FormatExceptionPolicyStrict ())
5950 .build ());
6051 }
6152
6253 /** Creates a harness for testing a formatter whose steps don't depend on the file. */
6354 public static StepHarness forFormatter (Formatter formatter ) {
64- return new StepHarness (FormatterFunc .Closeable .ofDangerous (
65- formatter ::close ,
66- input -> formatter .compute (input , new File ("" ))));
55+ return new StepHarness (formatter );
6756 }
6857
6958 /** Asserts that the given element is transformed as expected, and that the result is idempotent. */
70- public StepHarness test (String before , String after ) throws Exception {
71- String actual = formatter .apply ( before );
59+ public StepHarness test (String before , String after ) {
60+ String actual = formatter .compute ( LineEnding . toUnix ( before ), new File ( "" ) );
7261 assertEquals (after , actual , "Step application failed" );
7362 return testUnaffected (after );
7463 }
7564
7665 /** Asserts that the given element is idempotent w.r.t the step under test. */
77- public StepHarness testUnaffected (String idempotentElement ) throws Exception {
78- String actual = formatter .apply ( idempotentElement );
66+ public StepHarness testUnaffected (String idempotentElement ) {
67+ String actual = formatter .compute ( LineEnding . toUnix ( idempotentElement ), new File ( "" ) );
7968 assertEquals (idempotentElement , actual , "Step is not idempotent" );
8069 return this ;
8170 }
8271
8372 /** Asserts that the given elements in the resources directory are transformed as expected. */
84- public StepHarness testResource (String resourceBefore , String resourceAfter ) throws Exception {
73+ public StepHarness testResource (String resourceBefore , String resourceAfter ) {
8574 String before = ResourceHarness .getTestResource (resourceBefore );
8675 String after = ResourceHarness .getTestResource (resourceAfter );
8776 return test (before , after );
8877 }
8978
9079 /** Asserts that the given elements in the resources directory are transformed as expected. */
91- public StepHarness testResourceUnaffected (String resourceIdempotent ) throws Exception {
80+ public StepHarness testResourceUnaffected (String resourceIdempotent ) {
9281 String idempotentElement = ResourceHarness .getTestResource (resourceIdempotent );
9382 return testUnaffected (idempotentElement );
9483 }
9584
96- /** Asserts that the given elements in the resources directory are transformed as expected. */
97- public StepHarness testResourceException (String resourceBefore , Consumer <AbstractThrowableAssert <?, ? extends Throwable >> exceptionAssertion ) throws Exception {
98- return testException (ResourceHarness .getTestResource (resourceBefore ), exceptionAssertion );
85+ public AbstractStringAssert <?> testResourceExceptionMsg (String resourceBefore ) {
86+ return testExceptionMsg (ResourceHarness .getTestResource (resourceBefore ));
9987 }
10088
101- /** Asserts that the given elements in the resources directory are transformed as expected. */
102- public StepHarness testException (String before , Consumer <AbstractThrowableAssert <?, ? extends Throwable >> exceptionAssertion ) throws Exception {
103- Throwable t = assertThrows (Throwable .class , () -> formatter .apply (before ));
104- AbstractThrowableAssert <?, ? extends Throwable > abstractAssert = Assertions .assertThat (t );
105- exceptionAssertion .accept (abstractAssert );
106- return this ;
89+ public AbstractStringAssert <?> testExceptionMsg (String before ) {
90+ try {
91+ formatter .compute (LineEnding .toUnix (before ), FormatterStepImpl .SENTINEL );
92+ throw new SecurityException ("Expected exception" );
93+ } catch (Throwable e ) {
94+ if (e instanceof SecurityException ) {
95+ throw new AssertionError (e .getMessage ());
96+ } else {
97+ Throwable rootCause = e ;
98+ while (rootCause .getCause () != null ) {
99+ if (rootCause instanceof IllegalStateException ) {
100+ break ;
101+ }
102+ rootCause = rootCause .getCause ();
103+ }
104+ return Assertions .assertThat (rootCause .getMessage ());
105+ }
106+ }
107107 }
108108
109109 @ Override
110110 public void close () {
111- if (formatter instanceof FormatterFunc .Closeable ) {
112- ((FormatterFunc .Closeable ) formatter ).close ();
113- }
111+ formatter .close ();
114112 }
115113}
0 commit comments