From 734f2a5b14a538a434ec9aa288bedca1852d56b3 Mon Sep 17 00:00:00 2001 From: Alvis <37454764+avibn@users.noreply.github.com> Date: Thu, 4 May 2023 01:44:22 +0100 Subject: [PATCH] fix import xml bug --- .../controller/FileController.java | 19 +++++++++++++++++-- .../utility/xml/XMLHandler.java | 6 +++--- .../utility/xml/XMLValidator.java | 3 ++- .../controller/FileControllerTest.java | 3 ++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/team44/runwayredeclarationapp/controller/FileController.java b/src/main/java/com/team44/runwayredeclarationapp/controller/FileController.java index 3b293f0..9e571f5 100644 --- a/src/main/java/com/team44/runwayredeclarationapp/controller/FileController.java +++ b/src/main/java/com/team44/runwayredeclarationapp/controller/FileController.java @@ -10,6 +10,7 @@ import com.team44.runwayredeclarationapp.model.Runway; import com.team44.runwayredeclarationapp.utility.xml.XMLHandler; import com.team44.runwayredeclarationapp.utility.xml.XMLWrapper; +import com.team44.runwayredeclarationapp.view.component.alert.ErrorAlert; import java.io.File; import java.util.ArrayList; import java.util.HashSet; @@ -68,6 +69,16 @@ public void uploadXMLFile(File file, Boolean reset) { try { // Parse the XML file uploadedData = xmlHandler.readXML(file); + + // Check for error + if (uploadedData == null) { + ErrorAlert errorAlert = new ErrorAlert("File could not be found!", + "There seems to be an unexpected error.", "Error: " + file.getName() + + " could not be found/read."); + errorAlert.show(); + return; + } + var airports = uploadedData.getAirports(); var obstacles = uploadedData.getObstacles(); @@ -89,9 +100,9 @@ public void uploadXMLFile(File file, Boolean reset) { // Call the listener to update the GUI if (reset) { callSetListener(airports, obstacles); - return; + } else { + callAddListener(airports, obstacles); } - callAddListener(airports, obstacles); // Call the listener for upload success if (fileUploadSuccessfulListener != null) { @@ -105,6 +116,10 @@ public void uploadXMLFile(File file, Boolean reset) { "Please ensure that the uploaded XML file matches the schema specified.\n\nError:\n" + e.getMessage().replace("cvc-complex-type.2.4.a: ", "")); } + } catch (Exception e) { + ErrorAlert errorAlert = new ErrorAlert("Unexpected Error", + "There seems to be an unexpected error.", "Error: " + e.getMessage()); + errorAlert.show(); } } diff --git a/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLHandler.java b/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLHandler.java index eb5a689..f71e688 100644 --- a/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLHandler.java +++ b/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLHandler.java @@ -17,6 +17,7 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.apache.logging.log4j.LogManager; @@ -60,7 +61,7 @@ public class XMLHandler { /** * The XSD validation file */ - private File validationFile; + private URL validationFile; /** * Create an XML handler @@ -70,8 +71,7 @@ public XMLHandler() { // Set the validation schema try { - this.validationFile = new File( - XMLValidator.class.getResource("/xml/validate.xsd").toURI()); + this.validationFile = XMLValidator.class.getResource("/xml/validate.xsd"); } catch (Exception e) { logger.error("Validation file could not be found: " + e.getMessage()); } diff --git a/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLValidator.java b/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLValidator.java index 05f1d89..8e8fbce 100644 --- a/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLValidator.java +++ b/src/main/java/com/team44/runwayredeclarationapp/utility/xml/XMLValidator.java @@ -2,6 +2,7 @@ import java.io.File; import java.io.IOException; +import java.net.URL; import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.SchemaFactory; @@ -32,7 +33,7 @@ public class XMLValidator { * @throws SAXException when there is a parsing error * @throws IOException when there are other input/output errors from reading the file */ - public void validateWithSchema(File xsdFile, File xmlFile) throws SAXException, IOException { + public void validateWithSchema(URL xsdFile, File xmlFile) throws SAXException, IOException { // Try validate try { // Create the validator with the schema diff --git a/src/main/tests/com/team44/runwayredeclarationapp/controller/FileControllerTest.java b/src/main/tests/com/team44/runwayredeclarationapp/controller/FileControllerTest.java index 5814868..f3ea191 100644 --- a/src/main/tests/com/team44/runwayredeclarationapp/controller/FileControllerTest.java +++ b/src/main/tests/com/team44/runwayredeclarationapp/controller/FileControllerTest.java @@ -6,6 +6,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import com.team44.runwayredeclarationapp.TestConstants; @@ -95,7 +96,7 @@ void uploadXMLFile_Valid() { // Verify the listeners that should be called verify(dataAddListener).load(any(Airport[].class), any(Obstacle[].class)); verify(dataSetListener).load(any(Airport[].class), any(Obstacle[].class)); - verify(fileUploadSuccessfulListener).uploadSuccessful(); + verify(fileUploadSuccessfulListener, times(2)).uploadSuccessful(); // Verify the error listeners that should NOT be called verify(errorListener, never()).alert(anyString(), anyString(), anyString());