Test the updateXmlElement method.
()
| 107 | |
| 108 | /** Test the updateXmlElement method. */ |
| 109 | public void testUpdateXmlElement() throws IOException { |
| 110 | String nl = Strings.getNl(); |
| 111 | String configElementName = "GATECONFIG"; |
| 112 | String configElement = "<" + configElementName + " FULLSIZE=\"yes\"/>"; |
| 113 | String exampleXmlStart = |
| 114 | "<?xml version=\"1.0\"?>" + nl + |
| 115 | "<!-- a comment -->" + nl + |
| 116 | "<GATE>" + nl + |
| 117 | "" + nl + |
| 118 | "<CREOLE-DIRECTORY>http://on.the.net/</CREOLE-DIRECTORY>" + nl + |
| 119 | "<!--- The next element may be overwritten by the GUI --->" + nl; |
| 120 | String exampleXmlEnd = |
| 121 | "</GATE>" + nl; |
| 122 | String exampleXml = exampleXmlStart + configElement + nl + exampleXmlEnd; |
| 123 | |
| 124 | // check that the getEmptyElement method works |
| 125 | assertTrue( |
| 126 | "the GATECONFIG element doesn't match", |
| 127 | getEmptyElement(exampleXml, configElementName).equals(configElement) |
| 128 | ); |
| 129 | |
| 130 | // a map of values to place in the new element |
| 131 | Map<String,String> newAttrs = new HashMap<String,String>(); |
| 132 | newAttrs.put("a", "1"); |
| 133 | newAttrs.put("b", "2"); |
| 134 | newAttrs.put("c", "3"); |
| 135 | newAttrs.put("d", "needs&escaping"); |
| 136 | |
| 137 | // test the files method |
| 138 | String newXml = Files.updateXmlElement( |
| 139 | new BufferedReader(new StringReader(exampleXml)), |
| 140 | configElementName, |
| 141 | newAttrs |
| 142 | ); |
| 143 | assertTrue( |
| 144 | "newXml doesn't match (1): " + newXml.toString(), |
| 145 | newXml.toString().startsWith(exampleXmlStart) && |
| 146 | newXml.toString().endsWith(exampleXmlEnd) && |
| 147 | newXml.toString().contains("a=\"1\"") && |
| 148 | newXml.toString().contains("d=\"needs&escaping\"") |
| 149 | ); |
| 150 | if(DEBUG) Out.prln(newXml); |
| 151 | |
| 152 | // write the example data into a temp file and try on that |
| 153 | File tempFile = Files.writeTempFile(exampleXml); |
| 154 | newXml = Files.updateXmlElement(tempFile, configElementName, newAttrs); |
| 155 | assertTrue( |
| 156 | "newXml doesn't match (2): " + newXml.toString(), |
| 157 | newXml.toString().startsWith(exampleXmlStart) && |
| 158 | newXml.toString().endsWith(exampleXmlEnd) && |
| 159 | newXml.toString().contains("a=\"1\"") && |
| 160 | newXml.toString().contains("d=\"needs&escaping\"") |
| 161 | ); |
| 162 | if(DEBUG) Out.prln(newXml); |
| 163 | |
| 164 | // check that the file was overwritten successfully |
| 165 | newXml = Files.getString(tempFile); |
| 166 | assertTrue( |
nothing calls this directly
no test coverage detected