AsciiDoctorFileReferenceValidator { public void validate(File referenceBaseFileOrFolder, Collection<AsciiDoctorFileReference> references, Collection<AsciiDoctorMarker> errors) { if (errors == null) { return; } if (referenceBaseFileOrFolder == null) { return; } if (!referenceBaseFileOrFolder.exists()) { return; } File referenceBaseDirectory = referenceBaseFileOrFolder; if (!referenceBaseDirectory.isDirectory()) { referenceBaseDirectory = referenceBaseDirectory.getParentFile(); } for (AsciiDoctorFileReference reference : references) { String filePathAsString = reference.getFilePath(); Path referenceBaseDirPath = referenceBaseDirectory.toPath(); Path filePath = Paths.get(filePathAsString); File expectedFile = referenceBaseDirPath.resolve(filePath).toFile(); String problem = null; if (!expectedFile.exists()) { problem = ".. references not existing file:" + expectedFile.getAbsolutePath(); } else if (expectedFile.isDirectory()) { problem = ".. points to a directory not a file:" + expectedFile.getAbsolutePath(); } if (problem != null) { AsciiDoctorMarker marker = new AsciiDoctorMarker(reference.getPosition(), reference.getEnd(), ValidationConstants.VALIDATION_FAILED + reference.getTargetPrefix() + problem); errors.add(marker); } } }  void validate(File referenceBaseFileOrFolder, Collection<AsciiDoctorFileReference> references, Collection<AsciiDoctorMarker> errors);  }
@Test public void valid_include() { File editorFile = TestResourcesLoader.assertTestFile("codeassist/include/test1/editorfile1.adoc"); Collection<AsciiDoctorMarker> errors = new ArrayList<>(); validatorToTest.validate(editorFile, Collections.singleton(new AsciiDoctorFileReference("include::otherfile1.adoc[]", 1, 20, 20)),errors); assertTrue(errors.isEmpty()); }
@Test public void non_existing_include() { File editorFile = TestResourcesLoader.assertTestFile("codeassist/include/test1/editorfile1.adoc"); Collection<AsciiDoctorMarker> errors = new ArrayList<>(); validatorToTest.validate(editorFile, Collections.singleton(new AsciiDoctorFileReference("include::unkknown.adoc[]", 1, 20, 20)),errors); assertEquals(1,errors.size()); }
@Test public void non_existing_include_only_start_tag() { File editorFile = TestResourcesLoader.assertTestFile("codeassist/include/test1/editorfile1.adoc"); Collection<AsciiDoctorMarker> errors = new ArrayList<>(); validatorToTest.validate(editorFile, Collections.singleton(new AsciiDoctorFileReference("include::", 1, 20, 20)),errors); assertEquals(1,errors.size()); }
@Test public void resolve_image_file_path_with_imagedir() { File imageFolder = TestResourcesLoader.assertTestFile("codeassist/include/test1/subfolder2"); Collection<AsciiDoctorMarker> errors = new ArrayList<>(); validatorToTest.validate(imageFolder, Collections.singleton(new AsciiDoctorFileReference("image::asciidoctor-editor.png", 1, 20, 20)),errors); assertEquals(0,errors.size()); }
@Test public void resolve_image_file_path_with_imagedir__with_filename_having_spaces_inside() { File imageFolder = TestResourcesLoader.assertTestFile("codeassist/include/test1/subfolder2"); Collection<AsciiDoctorMarker> errors = new ArrayList<>(); validatorToTest.validate(imageFolder, Collections.singleton(new AsciiDoctorFileReference("image::asciidoctor-editor with spaces.png", 1, 20, 20)),errors); assertEquals(0,errors.size()); }
@Test public void resolved_image_file_path_with_imagedir_not_found() { File imageFolder = TestResourcesLoader.assertTestFile("codeassist/include/test1/subfolder2"); Collection<AsciiDoctorMarker> errors = new ArrayList<>(); validatorToTest.validate(imageFolder, Collections.singleton(new AsciiDoctorFileReference("image::unknown.png", 1, 20, 20)),errors); assertEquals(1,errors.size()); }