I'm having a bit of an annoying issue which I cannot figure out.
My goal is that I have an xml file which I am attempting to save. It is doing this saving via FTP, and the file which I am writing to is on a local VM (with Apache and FileZilla enabled on the VM). I can write other files a similar way, but this one file is not working for some reason.
Here is a unit test I have to narrow down the issue:
@Test public void testUpdateConfigurations() { ConfigurationsQuery.readConfigurationsFile(); // Increment the simulation date by one month ConfigurationProcess.setMonthData(); // Get the weather data for the new month Map<WeatherType, WeatherEvent> weatherEvents = WeatherQuery.getWeatherEventsForMonth(Database.getSystemDate().get(Calendar.MONTH)); // Clear the old weather data and set the new weather data Database.getWeather().clear(); ConfigurationProcess.setWeatherData(weatherEvents); logger.info(SimulationDatabase.getWeatherEventsForMonth().toString()); // Set the new simulation dates ConfigurationProcess.setSimDates(); Assert.assertTrue(UpdateDatabaseQuery.updateConfigurations()); }
The updateConfigurations() method can be found here:
public static boolean updateConfigurations() { int count = 0; while(!ConfigurationsQuery.writeConfigurations()) { if(count>=5) { logger.severe("Configurations Failed To Update after 5 attempts..."); return false; } count++; } return true; }
The writeConfigurations() method can be found here:
public static boolean writeConfigurations() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); // Create Root Element root = document.createElement("config"); // I took out all the code which creates the xml file, to keep this short // Append the root document.appendChild(root); URL url = new URL(SharedConstants.HOST_URL+FILE_NAME); URLConnection curlconn = url.openConnection(); curlconn.setDoOutput(true); logger.info("Do Output True"); java.io.OutputStream out = curlconn.getOutputStream(); if(XMLUtilities.printDocument(document, out)) { out.close(); return true; } } catch(Exception ex) { ex.printStackTrace(); } return false; }
And, lastly, the printDocument() method can be found here:
public static boolean printDocument(Document doc, OutputStream out) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); return false; } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); try { transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return false; } catch (TransformerException e) { e.printStackTrace(); return false; } return true; }
I am not getting any exceptions thrown or error messages in the log, and the test passes successfully. The file is just not being updated. Does anyone have any thoughts? I know there is a lot here, so please ask questions if you need something clarified.