| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package org.devacfr.testing.util; |
| 20 |
|
|
| 21 |
|
import javax.annotation.Nonnull; |
| 22 |
|
import javax.annotation.Nullable; |
| 23 |
|
|
| 24 |
|
import java.io.File; |
| 25 |
|
import java.io.IOException; |
| 26 |
|
import java.io.StringReader; |
| 27 |
|
import java.nio.file.Path; |
| 28 |
|
import java.util.function.Function; |
| 29 |
|
|
| 30 |
|
import com.cloudbees.diff.Diff; |
| 31 |
|
import com.cloudbees.diff.provider.BuiltInDiffProvider; |
| 32 |
|
import com.google.common.base.Charsets; |
| 33 |
|
import com.google.common.base.Strings; |
| 34 |
|
import com.google.common.io.CharSink; |
| 35 |
|
import com.google.common.io.Files; |
| 36 |
|
import com.google.common.io.Resources; |
| 37 |
|
import org.hamcrest.Description; |
| 38 |
|
import org.hamcrest.Matcher; |
| 39 |
|
import org.hamcrest.StringDescription; |
| 40 |
|
import org.hamcrest.TypeSafeMatcher; |
| 41 |
|
|
| 42 |
|
import static com.google.common.base.Throwables.throwIfUnchecked; |
| 43 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
| 44 |
|
import static org.hamcrest.Matchers.equalToCompressingWhiteSpace; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@author |
| 48 |
|
@since |
| 49 |
|
|
| |
|
| 56,2% |
Uncovered Elements: 28 (64) |
Complexity: 19 |
Complexity Density: 0,45 |
|
| 50 |
|
public final class Approvals { |
| 51 |
|
|
| 52 |
|
public static final Function<String, String> REMOVE_CARRIAGE_RETURN_LINEFEED = text -> text.replaceAll("\r", ""); |
| 53 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 54 |
0 |
private Approvals() {... |
| 55 |
0 |
throw new UnsupportedOperationException(); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@param |
| 60 |
|
|
| 61 |
|
@param |
| 62 |
|
|
| 63 |
|
@param |
| 64 |
|
|
| 65 |
|
@param |
| 66 |
|
|
| 67 |
|
@return |
| 68 |
|
@throws |
| 69 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 70 |
31 |
public static String getActualResource(@Nonnull final Path location,... |
| 71 |
|
@Nonnull final Class<?> testClass, |
| 72 |
|
@Nonnull final String testName, |
| 73 |
|
@Nullable final String extension) { |
| 74 |
31 |
final String suf = !Strings.isNullOrEmpty(extension) ? "." + extension : ""; |
| 75 |
31 |
final String fileName = String.format("%s.%s.actual%s", testClass.getSimpleName(), testName, suf); |
| 76 |
31 |
return readFile(location.resolve(fileName)); |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@param |
| 81 |
|
|
| 82 |
|
@param |
| 83 |
|
|
| 84 |
|
@param |
| 85 |
|
|
| 86 |
|
@param |
| 87 |
|
|
| 88 |
|
@return |
| 89 |
|
@throws |
| 90 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0,5 |
|
| 91 |
23 |
public static String getExpectedResource(@Nonnull final Path location,... |
| 92 |
|
@Nonnull final Class<?> testClass, |
| 93 |
|
@Nonnull final String testName, |
| 94 |
|
@Nullable final String extension, |
| 95 |
|
@Nullable final Function<String, String> transformer) { |
| 96 |
23 |
final String ext = !Strings.isNullOrEmpty(extension) ? "." + extension : ""; |
| 97 |
23 |
final String fileName = String.format("%s.%s.approved%s", testClass.getSimpleName(), testName, ext); |
| 98 |
23 |
final String text = Approvals.REMOVE_CARRIAGE_RETURN_LINEFEED.apply(readFile(location.resolve(fileName))); |
| 99 |
23 |
if (transformer != null) { |
| 100 |
0 |
return transformer.apply(text); |
| 101 |
|
} |
| 102 |
23 |
return text; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
@param |
| 109 |
|
|
| 110 |
|
@param |
| 111 |
|
|
| 112 |
|
@param |
| 113 |
|
|
| 114 |
|
@param |
| 115 |
|
|
| 116 |
|
@param |
| 117 |
|
|
| 118 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 119 |
0 |
public static void verify(@Nonnull final Path location,... |
| 120 |
|
@Nonnull final Class<?> testClass, |
| 121 |
|
@Nonnull final String testName, |
| 122 |
|
@Nullable final String actual, |
| 123 |
|
@Nullable final String extension) { |
| 124 |
0 |
final String expected = getExpectedResource(location, testClass, testName, extension, null); |
| 125 |
0 |
assertThat(actual, equalToCompressingWhiteSpace(expected)); |
| 126 |
|
} |
| 127 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0,33 |
|
| 128 |
0 |
public static void verify(final Path actualFile, final Path expectedFile) {... |
| 129 |
0 |
try { |
| 130 |
0 |
final String actual = Files.asCharSource(actualFile.toFile(), Charsets.UTF_8).read(); |
| 131 |
0 |
final String expected = Approvals.REMOVE_CARRIAGE_RETURN_LINEFEED.apply(readFile(expectedFile)); |
| 132 |
0 |
final Matcher<String> matcher = IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace(expected); |
| 133 |
0 |
if (!matcher.matches(actual)) { |
| 134 |
0 |
final Description description = new StringDescription(); |
| 135 |
0 |
matcher.describeMismatch(actual, description); |
| 136 |
|
|
| 137 |
0 |
throw new AssertionError(description.toString()); |
| 138 |
|
} |
| 139 |
|
} catch (final IOException e) { |
| 140 |
0 |
throwIfUnchecked(e); |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
@param |
| 148 |
|
|
| 149 |
|
@param |
| 150 |
|
|
| 151 |
|
@param |
| 152 |
|
|
| 153 |
|
@param |
| 154 |
|
|
| 155 |
|
@param |
| 156 |
|
|
| 157 |
|
|
| |
|
| 73,9% |
Uncovered Elements: 6 (23) |
Complexity: 6 |
Complexity Density: 0,35 |
|
| 158 |
22 |
public static void verify(@Nonnull final Path location,... |
| 159 |
|
@Nonnull final Class<?> testClass, |
| 160 |
|
@Nonnull final String testName, |
| 161 |
|
final Function<String, String> transform, |
| 162 |
|
@Nullable final String extension) { |
| 163 |
22 |
String actual = getActualResource(location, testClass, testName, extension); |
| 164 |
22 |
if (actual != null && transform != null) { |
| 165 |
22 |
actual = transform.apply(actual); |
| 166 |
|
} |
| 167 |
22 |
final String expected = getExpectedResource(location, testClass, testName, extension, null); |
| 168 |
|
|
| 169 |
22 |
final File path = new File("target/approval/actual/"); |
| 170 |
22 |
if (!path.exists()) { |
| 171 |
1 |
path.mkdirs(); |
| 172 |
|
} |
| 173 |
22 |
final File file = new File(path, testClass.getSimpleName() + "." + testName); |
| 174 |
|
|
| 175 |
22 |
final CharSink sink = Files.asCharSink(file, Charsets.UTF_8); |
| 176 |
22 |
try { |
| 177 |
22 |
sink.write(actual); |
| 178 |
|
} catch (final IOException e) { |
| 179 |
0 |
throwIfUnchecked(e); |
| 180 |
|
} |
| 181 |
|
|
| 182 |
22 |
final Matcher<String> matcher = IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace(expected); |
| 183 |
22 |
if (!matcher.matches(actual)) { |
| 184 |
0 |
final Description description = new StringDescription(); |
| 185 |
0 |
matcher.describeMismatch(actual, description); |
| 186 |
|
|
| 187 |
0 |
throw new AssertionError(description.toString()); |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
@param |
| 195 |
|
|
| 196 |
|
@param |
| 197 |
|
|
| 198 |
|
@param |
| 199 |
|
|
| 200 |
|
@param |
| 201 |
|
|
| 202 |
|
@param |
| 203 |
|
|
| 204 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 205 |
0 |
public static void verify(@Nonnull final Path location,... |
| 206 |
|
@Nonnull final Class<?> testClass, |
| 207 |
|
@Nonnull final String testName, |
| 208 |
|
@Nonnull final Path actualFile, |
| 209 |
|
@Nullable final String extension) { |
| 210 |
|
|
| 211 |
0 |
verify(location, testClass, testName, readFile(location.resolve(actualFile)), extension); |
| 212 |
|
} |
| 213 |
|
|
| |
|
| 66,7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 214 |
54 |
private static String readFile(final Path path) {... |
| 215 |
54 |
try { |
| 216 |
54 |
return Resources.toString(Resources.getResource(path.toString()), Charsets.UTF_8); |
| 217 |
|
} catch (final IOException e) { |
| 218 |
0 |
throw new RuntimeException(e.getMessage(), e); |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
|
|
| |
|
| 43,5% |
Uncovered Elements: 13 (23) |
Complexity: 9 |
Complexity Density: 0,64 |
|
| 222 |
|
public static class IsEqualCompressingWhiteSpace extends TypeSafeMatcher<String> { |
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
private final String string; |
| 228 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 229 |
22 |
public IsEqualCompressingWhiteSpace(final String string) {... |
| 230 |
22 |
if (string == null) { |
| 231 |
0 |
throw new IllegalArgumentException("Non-null value required"); |
| 232 |
|
} |
| 233 |
22 |
this.string = string; |
| 234 |
|
} |
| 235 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 236 |
22 |
@Override... |
| 237 |
|
public boolean matchesSafely(final String item) { |
| 238 |
22 |
return stripSpaces(string).equals(stripSpaces(item)); |
| 239 |
|
} |
| 240 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 241 |
0 |
@Override... |
| 242 |
|
public void describeMismatchSafely(final String item, final Description mismatchDescription) { |
| 243 |
0 |
mismatchDescription.appendText(getInternalDiff(item, string)); |
| 244 |
|
} |
| 245 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 246 |
0 |
@Override... |
| 247 |
|
public void describeTo(final Description description) { |
| 248 |
0 |
description.appendText("a string equal to ").appendValue(string).appendText(" compressing white space"); |
| 249 |
|
} |
| 250 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 251 |
44 |
public String stripSpaces(final String toBeStripped) {... |
| 252 |
44 |
return toBeStripped.replaceAll("\\s+", " ").trim(); |
| 253 |
|
} |
| 254 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 255 |
22 |
public static Matcher<String> equalToCompressingWhiteSpace(final String expectedString) {... |
| 256 |
22 |
return new IsEqualCompressingWhiteSpace(expectedString); |
| 257 |
|
} |
| 258 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
|
| 259 |
0 |
private String getInternalDiff(final String expected, final String actual) {... |
| 260 |
0 |
final BuiltInDiffProvider provider = new BuiltInDiffProvider(); |
| 261 |
0 |
Diff diffs = null; |
| 262 |
0 |
try { |
| 263 |
0 |
diffs = provider.computeDiff(new StringReader(actual), new StringReader(expected)); |
| 264 |
0 |
return diffs |
| 265 |
|
.toUnifiedDiff("actual", "expected", new StringReader(actual), new StringReader(expected), 10); |
| 266 |
|
} catch (final IOException e) { |
| 267 |
0 |
throw new RuntimeException(e.getMessage(), e); |
| 268 |
|
} |
| 269 |
|
} |
| 270 |
|
} |
| 271 |
|
} |