Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void allListItemsToXML(StringBuilder xml, JList list) {
String s = "";
try {
String text = list.getModel().getElementAt(i).toString();
s = "<!-- " + text + " -->";
s = "<!-- " + asXmlCommentText(text) + " -->";
s = s + System.getProperty("line.separator");
s = s + "<listitem index=\"" + i + "\">" + asEscapedXml(text) + "</listitem>";
}
Expand All @@ -195,6 +195,14 @@ public void allListItemsToXML(StringBuilder xml, JList list) {
}
}

/**
* The string "--" is not allowed inside XML comments (e.g. a name like "Muster--Meier" in a list item). Otherwise the
* whole hierarchy XML is invalid and no XPath can be evaluated any more.
*/
static String asXmlCommentText(String s) {
return s.replaceAll("-(?=-)", "- ");
}

public void allTabsToXML(StringBuilder xml, JTabbedPane tabbedPane) {
for (int i = 0; i < tabbedPane.getComponentCount(); i++) {
String title = asEscapedXml(tabbedPane.getTitleAt(i));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baloise.testautomation.taf.server;

import java.io.StringReader;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathFactory;

import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.InputSource;

import com.baloise.testautomation.taf.swing.server.main.SwApplication;

public class SwApplicationListItemsTest {

// A list item containing "--" (e.g. the message "... required: Muster--Meier") must not make the XML invalid
@Test
public void listItemWithDoubleHyphenKeepsXmlValid() throws Exception {
DefaultListModel<String> model = new DefaultListModel<>();
model.addElement("Basic salary is required: Muster--Meier AG, Basel");
model.addElement("Triple --- and ends with -");
JList<String> list = new JList<>(model);

StringBuilder xml = new StringBuilder("<list>");
new SwApplication().allListItemsToXML(xml, list);
xml.append("</list>");

org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(new StringReader(xml.toString())));
Assert.assertEquals("2", XPathFactory.newInstance().newXPath().evaluate("count(//listitem)", doc));
// the text of the listitem itself stays unchanged
Assert.assertTrue(XPathFactory.newInstance().newXPath()
.evaluate("//listitem[@index='0']", doc).contains("Muster--Meier"));
}
}
Loading