1
1
/*
2
- * Copyright 2016-2021 DiffPlug
2
+ * Copyright 2016-2023 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ protected File rootFolder() {
54
54
}
55
55
56
56
/** Returns a new child of the root folder. */
57
- protected File newFile (String subpath ) throws IOException {
57
+ protected File newFile (String subpath ) {
58
58
return new File (rootFolder (), subpath );
59
59
}
60
60
@@ -85,16 +85,16 @@ protected void replace(String path, String toReplace, String replaceWith) throws
85
85
}
86
86
87
87
/** 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 ) {
89
89
URL url = ResourceHarness .class .getResource ("/" + filename );
90
90
if (url == null ) {
91
91
throw new IllegalArgumentException ("No such resource " + filename );
92
92
}
93
- return Resources .toString (url , StandardCharsets .UTF_8 );
93
+ return ThrowingEx . get (() -> LineEnding . toUnix ( Resources .toString (url , StandardCharsets .UTF_8 )) );
94
94
}
95
95
96
96
/** 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 ) {
98
98
List <File > files = new ArrayList <>(filenames .length );
99
99
for (String filename : filenames ) {
100
100
files .add (createTestFile (filename ));
@@ -103,47 +103,30 @@ protected List<File> createTestFiles(String... filenames) throws IOException {
103
103
}
104
104
105
105
/** 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 ) {
107
107
return createTestFile (filename , UnaryOperator .identity ());
108
108
}
109
109
110
110
/**
111
111
* Returns a File (in a temporary folder) which has the contents, possibly processed, of the given file from the
112
112
* src/test/resources directory.
113
113
*/
114
- protected File createTestFile (String filename , UnaryOperator <String > fileContentsProcessor ) throws IOException {
114
+ protected File createTestFile (String filename , UnaryOperator <String > fileContentsProcessor ) {
115
115
int lastSlash = filename .lastIndexOf ('/' );
116
116
String name = lastSlash >= 0 ? filename .substring (lastSlash ) : filename ;
117
117
File file = newFile (name );
118
118
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 ) ));
120
120
return file ;
121
121
}
122
122
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
-
140
123
@ CheckReturnValue
141
- protected ReadAsserter assertFile (String path ) throws IOException {
124
+ protected ReadAsserter assertFile (String path ) {
142
125
return new ReadAsserter (newFile (path ));
143
126
}
144
127
145
128
@ CheckReturnValue
146
- protected ReadAsserter assertFile (File file ) throws IOException {
129
+ protected ReadAsserter assertFile (File file ) {
147
130
return new ReadAsserter (file );
148
131
}
149
132
@@ -176,7 +159,7 @@ public void matches(Consumer<AbstractCharSequenceAssert<?, String>> conditions)
176
159
}
177
160
}
178
161
179
- protected WriteAsserter setFile (String path ) throws IOException {
162
+ protected WriteAsserter setFile (String path ) {
180
163
return new WriteAsserter (newFile (path ));
181
164
}
182
165
@@ -188,21 +171,25 @@ private WriteAsserter(File file) {
188
171
this .file = file ;
189
172
}
190
173
191
- public File toLines (String ... lines ) throws IOException {
174
+ public File toLines (String ... lines ) {
192
175
return toContent (String .join ("\n " , Arrays .asList (lines )));
193
176
}
194
177
195
- public File toContent (String content ) throws IOException {
178
+ public File toContent (String content ) {
196
179
return toContent (content , StandardCharsets .UTF_8 );
197
180
}
198
181
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
+ });
201
186
return file ;
202
187
}
203
188
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
+ });
206
193
return file ;
207
194
}
208
195
0 commit comments