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.
@@ -54,7 +54,7 @@ protected File rootFolder() {
5454 }
5555
5656 /** Returns a new child of the root folder. */
57- protected File newFile (String subpath ) throws IOException {
57+ protected File newFile (String subpath ) {
5858 return new File (rootFolder (), subpath );
5959 }
6060
@@ -85,16 +85,16 @@ protected void replace(String path, String toReplace, String replaceWith) throws
8585 }
8686
8787 /** Returns the contents of the given file from the src/test/resources directory. */
88- protected static String getTestResource (String filename ) throws IOException {
88+ protected static String getTestResource (String filename ) {
8989 URL url = ResourceHarness .class .getResource ("/" + filename );
9090 if (url == null ) {
9191 throw new IllegalArgumentException ("No such resource " + filename );
9292 }
93- return Resources .toString (url , StandardCharsets .UTF_8 );
93+ return ThrowingEx . get (() -> LineEnding . toUnix ( Resources .toString (url , StandardCharsets .UTF_8 )) );
9494 }
9595
9696 /** Returns Files (in a temporary folder) which has the contents of the given file from the src/test/resources directory. */
97- protected List <File > createTestFiles (String ... filenames ) throws IOException {
97+ protected List <File > createTestFiles (String ... filenames ) {
9898 List <File > files = new ArrayList <>(filenames .length );
9999 for (String filename : filenames ) {
100100 files .add (createTestFile (filename ));
@@ -103,47 +103,30 @@ protected List<File> createTestFiles(String... filenames) throws IOException {
103103 }
104104
105105 /** Returns a File (in a temporary folder) which has the contents of the given file from the src/test/resources directory. */
106- protected File createTestFile (String filename ) throws IOException {
106+ protected File createTestFile (String filename ) {
107107 return createTestFile (filename , UnaryOperator .identity ());
108108 }
109109
110110 /**
111111 * Returns a File (in a temporary folder) which has the contents, possibly processed, of the given file from the
112112 * src/test/resources directory.
113113 */
114- protected File createTestFile (String filename , UnaryOperator <String > fileContentsProcessor ) throws IOException {
114+ protected File createTestFile (String filename , UnaryOperator <String > fileContentsProcessor ) {
115115 int lastSlash = filename .lastIndexOf ('/' );
116116 String name = lastSlash >= 0 ? filename .substring (lastSlash ) : filename ;
117117 File file = newFile (name );
118118 file .getParentFile ().mkdirs ();
119- Files .write (file .toPath (), fileContentsProcessor .apply (getTestResource (filename )).getBytes (StandardCharsets .UTF_8 ));
119+ ThrowingEx . run (() -> Files .write (file .toPath (), fileContentsProcessor .apply (getTestResource (filename )).getBytes (StandardCharsets .UTF_8 ) ));
120120 return file ;
121121 }
122122
123- /** Reads the given resource from "before", applies the step, and makes sure the result is "after". */
124- protected void assertOnResources (FormatterStep step , String unformattedPath , String expectedPath ) throws Throwable {
125- assertOnResources (rawUnix -> step .format (rawUnix , new File ("" )), unformattedPath , expectedPath );
126- }
127-
128- /** Reads the given resource from "before", applies the step, and makes sure the result is "after". */
129- protected void assertOnResources (FormatterFunc step , String unformattedPath , String expectedPath ) throws Throwable {
130- String unformatted = LineEnding .toUnix (getTestResource (unformattedPath )); // unix-ified input
131- String formatted = step .apply (unformatted );
132- // no windows newlines
133- assertThat (formatted ).doesNotContain ("\r " );
134-
135- // unix-ify the test resource output in case git screwed it up
136- String expected = LineEnding .toUnix (getTestResource (expectedPath )); // unix-ified output
137- assertThat (formatted ).isEqualTo (expected );
138- }
139-
140123 @ CheckReturnValue
141- protected ReadAsserter assertFile (String path ) throws IOException {
124+ protected ReadAsserter assertFile (String path ) {
142125 return new ReadAsserter (newFile (path ));
143126 }
144127
145128 @ CheckReturnValue
146- protected ReadAsserter assertFile (File file ) throws IOException {
129+ protected ReadAsserter assertFile (File file ) {
147130 return new ReadAsserter (file );
148131 }
149132
@@ -176,7 +159,7 @@ public void matches(Consumer<AbstractCharSequenceAssert<?, String>> conditions)
176159 }
177160 }
178161
179- protected WriteAsserter setFile (String path ) throws IOException {
162+ protected WriteAsserter setFile (String path ) {
180163 return new WriteAsserter (newFile (path ));
181164 }
182165
@@ -188,21 +171,25 @@ private WriteAsserter(File file) {
188171 this .file = file ;
189172 }
190173
191- public File toLines (String ... lines ) throws IOException {
174+ public File toLines (String ... lines ) {
192175 return toContent (String .join ("\n " , Arrays .asList (lines )));
193176 }
194177
195- public File toContent (String content ) throws IOException {
178+ public File toContent (String content ) {
196179 return toContent (content , StandardCharsets .UTF_8 );
197180 }
198181
199- public File toContent (String content , Charset charset ) throws IOException {
200- Files .write (file .toPath (), content .getBytes (charset ));
182+ public File toContent (String content , Charset charset ) {
183+ ThrowingEx .run (() -> {
184+ Files .write (file .toPath (), content .getBytes (charset ));
185+ });
201186 return file ;
202187 }
203188
204- public File toResource (String path ) throws IOException {
205- Files .write (file .toPath (), getTestResource (path ).getBytes (StandardCharsets .UTF_8 ));
189+ public File toResource (String path ) {
190+ ThrowingEx .run (() -> {
191+ Files .write (file .toPath (), getTestResource (path ).getBytes (StandardCharsets .UTF_8 ));
192+ });
206193 return file ;
207194 }
208195
0 commit comments