/** * Tests that spacing is correct. * * @throws IOException */ @Test public void spacing_no_constructor() throws IOException { // Class structure JClass c = new JClass("sido.test", "Test"); c.addImport(List.class); c.addImport(ArrayList.class); c.addField("String", "name"); c.addField("List<String>", "otherNames").setInitialisation("new ArrayList<String>()"); c.addMethod("getName", "String").addContent("return name;"); c.addMethod("setName").addParam("String", "value").addContent("this.name = value;"); // Output StringWriter s = new StringWriter(); PrintWriter writer = new PrintWriter(s); // Generation c.write(writer); // Output List<String> actual = readLines(s.toString()); // Gets the expected content List<String> expected = readReference("/jclass/SpacingNoConstructor.java"); // Difference between the two sets Patch diff = DiffUtils.diff(expected, actual); List<Delta> deltas = diff.getDeltas(); if (!deltas.isEmpty()) { // Creates the diff String original = StringUtils.join(expected, "\n"); String revised = StringUtils.join(actual, "\n"); List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(original, revised, expected, diff, 3); String diffDisplay = StringUtils.join(unifiedDiff, "\n"); System.err.println(diffDisplay); fail("spacing-no-constructor"); } }
public void assertJClass(JClass c, String referenceResourcePath, String testId) throws IOException { // Output StringWriter s = new StringWriter(); PrintWriter writer = new PrintWriter(s); // Generation c.write(writer); // Output List<String> actual = readLines(s.toString()); // Gets the expected content List<String> expected = readReference(referenceResourcePath); // Difference between the two sets Patch diff = DiffUtils.diff(expected, actual); List<Delta> deltas = diff.getDeltas(); if (!deltas.isEmpty()) { // Creates the diff String original = StringUtils.join(expected, "\n"); String revised = StringUtils.join(actual, "\n"); List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(original, revised, expected, diff, 3); String diffDisplay = StringUtils.join(unifiedDiff, "\n"); System.err.println(diffDisplay); fail(testId); } }
/** * Check formatting of given XML. * * @param name Name of XML file. * @param before XML to check. * @throws ValidationException In case of validation error. */ private static void formatting(final String name, final String before) throws ValidationException { // @checkstyle MultipleStringLiterals (3 lines) final String after = new Prettifier(before).prettify().replace("\r\n", "\n"); final String bnormalized = before.replace("\r\n", "\n"); final List<String> blines = Arrays.asList(bnormalized.split(XmlValidator.ESCAPED_EOL, -1)); final Patch filter = XmlValidator.filter( DiffUtils.diff(blines, Arrays.asList(after.split(XmlValidator.ESCAPED_EOL, -1)))); if (!filter.getDeltas().isEmpty()) { final int context = 5; throw new ValidationException( // @checkstyle LineLength (1 line) "The provided XML %s is not well formatted, it should look like this:\n%s\npatch:\n%s", name, after, StringUtils.join( DiffUtils.generateUnifiedDiff("before", "after", blines, filter, context), "\n")); } }