initial commit
This commit is contained in:
		
							
								
								
									
										28
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -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 | ||||
|  | ||||
							
								
								
									
										61
									
								
								pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|  | ||||
|     <groupId>com.r11.xsd</groupId> | ||||
|     <artifactId>xsd</artifactId> | ||||
|     <version>1.0</version> | ||||
|  | ||||
|     <properties> | ||||
|         <maven.compiler.source>11</maven.compiler.source> | ||||
|         <maven.compiler.target>11</maven.compiler.target> | ||||
|         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||
|     </properties> | ||||
|  | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.junit.jupiter</groupId> | ||||
|             <artifactId>junit-jupiter-api</artifactId> | ||||
|             <version>5.9.0</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.junit.jupiter</groupId> | ||||
|             <artifactId>junit-jupiter-engine</artifactId> | ||||
|             <version>5.9.0</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|  | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jgit</groupId> | ||||
|             <artifactId>org.eclipse.jgit</artifactId> | ||||
|             <version>6.3.0.202209071007-r</version> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- Jsch from jgit dependency is not supported [RSA 2048] so we need use mwiede fork. --> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jgit</groupId> | ||||
|             <artifactId>org.eclipse.jgit.ssh.jsch</artifactId> | ||||
|             <version>6.3.0.202209071007-r</version> | ||||
|             <exclusions> | ||||
|                 <exclusion> | ||||
|                     <groupId>com.jcraft</groupId> | ||||
|                     <artifactId>jsch</artifactId> | ||||
|                 </exclusion> | ||||
|             </exclusions> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.github.mwiede</groupId> | ||||
|             <artifactId>jsch</artifactId> | ||||
|             <version>0.2.4</version> | ||||
|         </dependency> | ||||
|  | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jgit</groupId> | ||||
|             <artifactId>org.eclipse.jgit.ssh.apache</artifactId> | ||||
|             <version>6.3.0.202209071007-r</version> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| </project> | ||||
							
								
								
									
										13
									
								
								readme.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								readme.md
									
									
									
									
									
										Normal file
									
								
							| @@ -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. | ||||
							
								
								
									
										0
									
								
								src/main/resources/xml/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/main/resources/xml/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										0
									
								
								src/main/resources/xsd/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/main/resources/xsd/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										104
									
								
								src/test/java/com/r11/tests/GitRepositoryTests.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								src/test/java/com/r11/tests/GitRepositoryTests.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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<String> 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); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										154
									
								
								src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										154
									
								
								src/test/java/com/r11/tests/ValidateXmlFileByXsdSchemaTests.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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<File> xsdFiles = new ArrayList<>(); | ||||
|     private static final List<File> xmlFiles = new ArrayList<>(); | ||||
|  | ||||
|     private static final Map<File, File> 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<File, File> 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; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 wojciech
					wojciech