From 20b7d862154bc238491d536eb8985690ed45d4cb Mon Sep 17 00:00:00 2001 From: wojciech Date: Mon, 3 Jun 2024 14:39:29 +0200 Subject: [PATCH] remove tests --- .../com/r11/tests/GitRepositoryTests.java | 104 ------------ .../ValidateXmlFileByXsdSchemaTests.java | 154 ------------------ 2 files changed, 258 deletions(-) delete mode 100644 src/test/java/com/r11/tests/GitRepositoryTests.java delete mode 100644 src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java diff --git a/src/test/java/com/r11/tests/GitRepositoryTests.java b/src/test/java/com/r11/tests/GitRepositoryTests.java deleted file mode 100644 index ba40d52..0000000 --- a/src/test/java/com/r11/tests/GitRepositoryTests.java +++ /dev/null @@ -1,104 +0,0 @@ -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 deleted file mode 100644 index c4a039e..0000000 --- a/src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java +++ /dev/null @@ -1,154 +0,0 @@ -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; - } -}