From 0d347645c1c9b6fb40f4bff07fbe84f8511a39b0 Mon Sep 17 00:00:00 2001 From: wojciech Date: Mon, 3 Jun 2024 14:18:40 +0200 Subject: [PATCH] initial commit --- .gitignore | 28 ++++ pom.xml | 61 +++++++ readme.md | 13 ++ src/main/resources/xml/.gitkeep | 0 src/main/resources/xsd/.gitkeep | 0 .../com/r11/tests/GitRepositoryTests.java | 104 ++++++++++++ .../ValidateXmlFileByXsdSchemaTests.java | 154 ++++++++++++++++++ 7 files changed, 360 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 readme.md create mode 100644 src/main/resources/xml/.gitkeep create mode 100644 src/main/resources/xsd/.gitkeep create mode 100644 src/test/java/com/r11/tests/GitRepositoryTests.java create mode 100644 src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e99bc4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties + +.idea + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a6b7829 --- /dev/null +++ b/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + com.r11.xsd + xsd + 1.0 + + + 11 + 11 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.9.0 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.9.0 + test + + + + org.eclipse.jgit + org.eclipse.jgit + 6.3.0.202209071007-r + + + + + org.eclipse.jgit + org.eclipse.jgit.ssh.jsch + 6.3.0.202209071007-r + + + com.jcraft + jsch + + + + + com.github.mwiede + jsch + 0.2.4 + + + + org.eclipse.jgit + org.eclipse.jgit.ssh.apache + 6.3.0.202209071007-r + + + \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..ecab4d8 --- /dev/null +++ b/readme.md @@ -0,0 +1,13 @@ +# XSD-repository-tasks: + +## 1. Prepare XSD files for library which will fulfill following requirements: + +1. Separate files for Book, Person, Author, Borrow, Reader should be created +2. Please use ComplexType and Elements to define entities +3. Use Extensions, Groups and Restrictions, Any Type +4. Place the finished files in the src/main/resources/xsd folder in your forked repository. + +## 2. Prepare XML files for XSD files from first exercise manually. +1. Use http://tools.release11.com/xml/xsd to check if your xml are correct +2. Place the finished files in the src/main/resources/xml folder in your forked repository. +3. Make the xsd and xml file names the same. diff --git a/src/main/resources/xml/.gitkeep b/src/main/resources/xml/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/xsd/.gitkeep b/src/main/resources/xsd/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/r11/tests/GitRepositoryTests.java b/src/test/java/com/r11/tests/GitRepositoryTests.java new file mode 100644 index 0000000..ba40d52 --- /dev/null +++ b/src/test/java/com/r11/tests/GitRepositoryTests.java @@ -0,0 +1,104 @@ +package com.r11.tests; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.Status; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.junit.jupiter.api.*; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class GitRepositoryTests { + + private static Git git; + private static Status status; + private static Repository repository; + + private final List unwantedFileTypes = List.of("application/java-archive", "application/java-vm"); + + @BeforeAll + static void beforeAll() throws IOException, GitAPIException { + FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder(); + repositoryBuilder.findGitDir(); + + git = Git.wrap(repositoryBuilder.build()); + + status = git.status().call(); + repository = git.getRepository(); + } + + @Test + @Order(1) + void checkHasUncommittedChangesTest() { + Assertions.assertFalse(status.hasUncommittedChanges()); + } + + @Test + @Order(2) + void checkNotPushedCommitsTest() throws GitAPIException, IOException { + String localHeadHash = repository.resolve("HEAD").getName(); + + for (Ref ref : git.lsRemote().call()) { + String refHash = ref.getObjectId().getName(); + if (refHash.equals(localHeadHash)) { + return; + } + } + + Assertions.fail("The local branch repository is not compatible with the branch repository on the server."); + } + + @Test + @Order(3) + void checkUnwantedFilesInUncommittedChangesTest() { + status.getUncommittedChanges().forEach(filePath -> { + if (this.isUnwantedFile(filePath)) { + Assertions.fail("Unwanted uncommitted file: " + filePath); + } + }); + } + + /** + * Get the file type. + * @param filePath The file path. + * @return The file type. + */ + private String getFileTypeFromPath(String filePath) { + File file = new File(filePath); + return this.getFileProbeContentType(file); + } + + /** + * Get the file type by probe content. + * @param file The file. + * @return The file type. + */ + private String getFileProbeContentType(File file) { + try { + return Files.probeContentType(file.toPath()); + } catch (IOException e) { + e.printStackTrace(); + } + return "Undetermined"; + } + + /** + * Check if the file is unwanted. + * @param filePath The file path. + * @return True if the file is unwanted. + */ + private boolean isUnwantedFile(String filePath) { + String type = this.getFileTypeFromPath(filePath); + if (type == null) { + return true; + } + + return this.unwantedFileTypes.contains(type); + } +} diff --git a/src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java b/src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java new file mode 100644 index 0000000..c4a039e --- /dev/null +++ b/src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java @@ -0,0 +1,154 @@ +package com.r11.tests; + +import org.junit.jupiter.api.*; +import org.xml.sax.SAXException; + +import javax.xml.XMLConstants; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; +import java.io.File; +import java.io.IOException; +import java.util.*; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class ValidateXmlFileByXsdSchemaTests { + + private static File xsdFilesDirectory; + private static File xmlFilesDirectory; + + private static final List xsdFiles = new ArrayList<>(); + private static final List xmlFiles = new ArrayList<>(); + + private static final Map xsdXmlFilesMap = new HashMap<>(); + + @BeforeAll + static void beforeAll() { + xsdFilesDirectory = new File("src/main/resources/xsd"); + xmlFilesDirectory = new File("src/main/resources/xml"); + } + + @Test + @Order(1) + void xsdDirectoryExistsAndDirectoryIsEmptyTest() { + if (!xsdFilesDirectory.exists()) { + Assertions.fail("XSD files directory does not exist. Please create id in the src/main/resources with name `xsd`."); + } + + if (directoryIsEmpty(xsdFilesDirectory)) { + Assertions.fail("XSD files directory is empty."); + } + } + + @Test + @Order(2) + void xmlDirectoryExistsAndDirectoryIsEmptyTest() { + if (!xmlFilesDirectory.exists()) { + Assertions.fail("XSD files directory does not exist. Please create id in the src/main/resources with name `xml`."); + } + + if (directoryIsEmpty(xmlFilesDirectory)) { + Assertions.fail("XML files directory is empty."); + } + } + + @Test + @Order(3) + void xsdDirectoryFilesExtensionsAndLoadFilesTest() { + for (File file : Objects.requireNonNull(xsdFilesDirectory.listFiles())) { + if (file.getName().equals(".gitkeep")) { + continue; + } + + if (file.getName().endsWith(".xsd")) { + xsdFiles.add(file); + } else { + Assertions.fail("XSD files directory contains files with an invalid extension."); + } + } + + if (xsdFiles.isEmpty()) { + Assertions.fail("XSD files directory is empty."); + } + } + + @Test + @Order(4) + void xmlDirectoryFilesExtensionsAndLoadFilesTest() { + for (File file : Objects.requireNonNull(xmlFilesDirectory.listFiles())) { + if (file.getName().equals(".gitkeep")) { + continue; + } + + if (file.getName().endsWith(".xml")) { + xmlFiles.add(file); + } else { + Assertions.fail("XML files directory contains files with an invalid extension."); + } + } + + if (xmlFiles.isEmpty()) { + Assertions.fail("XML files directory is empty."); + } + } + + @Test + @Order(5) + void matchXmlFilesWithXsdFilesTest() { + for (File xmlFile : xmlFiles) { + String xmlFileName = xmlFile.getName().substring(0, xmlFile.getName().lastIndexOf(".")); + for (File xsdFile : xsdFiles) { + String xsdFileName = xsdFile.getName().substring(0, xsdFile.getName().lastIndexOf(".")); + if (xmlFileName.equals(xsdFileName)) { + System.out.println("Matched: " + xmlFileName + " with " + xsdFileName); + xsdXmlFilesMap.put(xsdFile, xmlFile); + } + } + } + + if (xsdXmlFilesMap.isEmpty()) { + Assertions.fail("No XML files match the XSD files."); + } + } + + @Test + @Order(6) + void validateXmlFileByXsdSchemaTest() throws SAXException, IOException { + for (Map.Entry entry : xsdXmlFilesMap.entrySet()) { + Validator validator = initValidator(entry.getKey()); + validator.validate(new StreamSource(entry.getValue())); + } + } + + /** + * This method is used to validate XML file by XSD schema. + * @param xsdFile - XSD schema file. + * @return - Validator object. + * @throws SAXException - SAXException. + */ + private Validator initValidator(File xsdFile) throws SAXException { + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + Source schemaFile = new StreamSource(xsdFile); + Schema schema = factory.newSchema(schemaFile); + return schema.newValidator(); + } + + /** + * Check if directory is empty. + * @param file directory + * @return true if directory is empty, false otherwise + */ + boolean directoryIsEmpty(File file) { + if (!file.isDirectory()) { + throw new RuntimeException("The file is not a directory."); + } + + if (file.listFiles() == null) { + return true; + } + + return Objects.requireNonNull(file.listFiles()).length == 0; + } +}