Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorspingel2011-02-01 02:38:24 +0000
committerspingel2011-02-01 02:38:24 +0000
commit43a767b8d046d41cecf0b9fa30a8f408560d5e2c (patch)
tree760cd53fd1b57365c5a4062421104cd1df6f4fb1 /org.eclipse.mylyn.tasks.tests/src
parent89b6e7c3c2320fd77d41944915a49092737d2c85 (diff)
downloadorg.eclipse.mylyn.tasks-43a767b8d046d41cecf0b9fa30a8f408560d5e2c.tar.gz
org.eclipse.mylyn.tasks-43a767b8d046d41cecf0b9fa30a8f408560d5e2c.tar.xz
org.eclipse.mylyn.tasks-43a767b8d046d41cecf0b9fa30a8f408560d5e2c.zip
RESOLVED - bug 268456: reading of task data fails with fatal error
https://bugs.eclipse.org/bugs/show_bug.cgi?id=268456
Diffstat (limited to 'org.eclipse.mylyn.tasks.tests/src')
-rw-r--r--org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java4
-rw-r--r--org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskDataStoreTest.java55
-rw-r--r--org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/TaskDataExternalizerTest.java213
-rw-r--r--org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/Xml11InputStreamTest.java88
-rw-r--r--org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/XmlExternalizationTest.java160
5 files changed, 383 insertions, 137 deletions
diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java
index 516822fa9..bff563b35 100644
--- a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java
+++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java
@@ -18,6 +18,8 @@ import org.eclipse.mylyn.tasks.tests.core.FileTaskAttachmentSourceTest;
import org.eclipse.mylyn.tasks.tests.core.ITasksCoreConstantsTest;
import org.eclipse.mylyn.tasks.tests.core.TaskListUnmatchedContainerTest;
import org.eclipse.mylyn.tasks.tests.core.TaskRepositoryLocationTest;
+import org.eclipse.mylyn.tasks.tests.data.TaskDataExternalizerTest;
+import org.eclipse.mylyn.tasks.tests.data.Xml11InputStreamTest;
import org.eclipse.mylyn.tasks.tests.ui.ContextPerspectiveManagerTest;
import org.eclipse.mylyn.tasks.tests.ui.RetrieveTitleFromUrlTest;
import org.eclipse.mylyn.tasks.tests.ui.TaskAttachmentPropertyTesterTest;
@@ -110,6 +112,8 @@ public class AllTasksTests {
suite.addTestSuite(TaskMigratorTest.class);
suite.addTestSuite(TaskListViewTest.class);
suite.addTestSuite(AttachmentTableLabelProviderTest.class);
+ suite.addTestSuite(TaskDataExternalizerTest.class);
+ suite.addTestSuite(Xml11InputStreamTest.class);
// XXX long running tests, put back?
//suite.addTestSuite(QueryExportImportTest.class);
//suite.addTestSuite(BackgroundSaveTest.class);
diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskDataStoreTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskDataStoreTest.java
index fa0e34e52..6e1775883 100644
--- a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskDataStoreTest.java
+++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskDataStoreTest.java
@@ -193,4 +193,59 @@ public class TaskDataStoreTest extends TestCase {
assertData(2);
}
+
+ public void testReadWriteInvalidCharacters() throws Exception {
+ setupData();
+ data.getRoot().createAttribute("attribute").setValue("\u0000");
+
+ File file = File.createTempFile("mylyn", null);
+ file.deleteOnExit();
+ storage.putTaskData(file, state);
+
+ try {
+ TaskDataState state2 = storage.getTaskDataState(file);
+ fail("Expected CoreException, got " + state2);
+ } catch (CoreException expected) {
+ }
+ }
+
+ public void testReadWriteC0Characters() throws Exception {
+ setupData();
+ data.getRoot().createAttribute("attribute").setValue("\u0001\u001F");
+
+ File file = File.createTempFile("mylyn", null);
+ file.deleteOnExit();
+ storage.putTaskData(file, state);
+
+ TaskDataState state2 = storage.getTaskDataState(file);
+ assertEquals(state.getRepositoryData().getRoot().toString(), state2.getRepositoryData().getRoot().toString());
+ }
+
+ public void testReadWriteC1Characters() throws Exception {
+ setupData();
+ data.getRoot().createAttribute("attribute").setValue("\u007F\u0080");
+
+ File file = File.createTempFile("mylyn", null);
+ file.deleteOnExit();
+ storage.putTaskData(file, state);
+
+ TaskDataState state2 = storage.getTaskDataState(file);
+ assertEquals(state.getRepositoryData().getRoot().toString(), state2.getRepositoryData().getRoot().toString());
+ }
+
+ public void testReadWriteC0C1Characters() throws Exception {
+ setupData();
+ data.getRoot().createAttribute("attribute").setValue("\u0001\u001F\u007F\u0080");
+
+ File file = File.createTempFile("mylyn", null);
+ file.deleteOnExit();
+ storage.putTaskData(file, state);
+
+ try {
+ TaskDataState state2 = storage.getTaskDataState(file);
+ fail("Expected CoreException, got " + state2);
+ } catch (CoreException expected) {
+ }
+ }
+
}
diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/TaskDataExternalizerTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/TaskDataExternalizerTest.java
index 875d456bc..4a3f58e0b 100644
--- a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/TaskDataExternalizerTest.java
+++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/TaskDataExternalizerTest.java
@@ -13,105 +13,54 @@ package org.eclipse.mylyn.tasks.tests.data;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.util.zip.ZipInputStream;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.sax.SAXTransformerFactory;
-import javax.xml.transform.sax.TransformerHandler;
-import javax.xml.transform.stream.StreamResult;
-
-import junit.framework.Assert;
import junit.framework.TestCase;
-import org.eclipse.mylyn.internal.tasks.core.data.ITaskDataConstants;
+import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager;
import org.eclipse.mylyn.internal.tasks.core.data.TaskDataExternalizer;
+import org.eclipse.mylyn.internal.tasks.core.data.TaskDataExternalizer.Xml11InputStream;
import org.eclipse.mylyn.internal.tasks.core.data.TaskDataState;
-import org.eclipse.mylyn.tasks.core.data.ITaskDataWorkingCopy;
+import org.eclipse.mylyn.tasks.core.TaskRepository;
+import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
-import org.eclipse.mylyn.tasks.core.data.TaskMapper;
import org.eclipse.mylyn.tasks.tests.TaskTestUtil;
-import org.xml.sax.EntityResolver;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.XMLReader;
-import org.xml.sax.helpers.AttributesImpl;
-import org.xml.sax.helpers.DefaultHandler;
-import org.xml.sax.helpers.XMLReaderFactory;
+import org.eclipse.mylyn.tasks.tests.connector.MockRepositoryConnector;
+import org.xml.sax.SAXParseException;
/**
* @author Steffen Pingel
*/
public class TaskDataExternalizerTest extends TestCase {
- private class SimpleCharacterReader extends DefaultHandler {
-
- private char ch;
-
- public SimpleCharacterReader() {
- }
-
- @Override
- public void characters(char[] ch, int start, int length) throws SAXException {
- Assert.assertEquals(1, length);
- this.ch = ch[start];
- }
-
- public char getCharacter() {
- return ch;
- }
-
- }
-
- private final class SimpleCharacterWriter {
- private final TransformerHandler handler;
-
- public SimpleCharacterWriter(TransformerHandler handler) {
- this.handler = handler;
- }
-
- public void write(char character) throws SAXException {
- handler.startDocument();
- AttributesImpl atts = new AttributesImpl();
- handler.startElement("", "", ITaskDataConstants.ELEMENT_VALUE, atts); //$NON-NLS-1$ //$NON-NLS-2$
- ///handler.startCDATA();
- handler.characters(new char[] { character }, 0, 1);
- //handler.endCDATA();
- handler.endElement("", "", ITaskDataConstants.ELEMENT_VALUE);
- handler.endDocument();
- }
- }
+ private TaskDataExternalizer externalizer;
- TaskDataExternalizer externalizer;
+ private TaskRepository repository;
@Override
protected void setUp() throws Exception {
- externalizer = new TaskDataExternalizer(null);
-
+ TaskRepositoryManager taskRepositoryManager = new TaskRepositoryManager();
+ taskRepositoryManager.addRepositoryConnector(new MockRepositoryConnector());
+ //taskRepositoryManager.addRepositoryConnector(new BugzillaRepositoryConnector());
+ repository = TaskTestUtil.createMockRepository();
+ taskRepositoryManager.addRepository(repository);
+ externalizer = new TaskDataExternalizer(taskRepositoryManager);
}
- public void testMapFromLegacy() throws Exception {
- File file = TaskTestUtil.getFile("testdata/taskdata-1.0-bug-219897.zip");
- ZipInputStream in = new ZipInputStream(new FileInputStream(file));
- ITaskDataWorkingCopy state;
- try {
- in.getNextEntry();
- state = externalizer.readState(in);
- } finally {
- in.close();
- }
-
- TaskData taskData = state.getRepositoryData();
- @SuppressWarnings("unused")
- TaskMapper taskScheme = new TaskMapper(taskData);
-
- fail("fixme");
+// public void testMapFromLegacy() throws Exception {
+// File file = TaskTestUtil.getFile("testdata/taskdata-1.0-bug-219897.zip");
+// ZipInputStream in = new ZipInputStream(new FileInputStream(file));
+// ITaskDataWorkingCopy state;
+// try {
+// in.getNextEntry();
+// state = externalizer.readState(in);
+// } finally {
+// in.close();
+// }
+//
+// TaskData taskData = state.getRepositoryData();
+// @SuppressWarnings("unused")
+// TaskMapper taskScheme = new TaskMapper(taskData);
+//
// RepositoryTaskData legacyData = TaskDataUtil.toLegacyData(taskData, IdentityAttributeFactory.getInstance());
// assertEquals(taskData.getConnectorKind(), legacyData.getConnectorKind());
// assertEquals(taskData.getRepositoryUrl(), legacyData.getRepositoryUrl());
@@ -126,69 +75,59 @@ public class TaskDataExternalizerTest extends TestCase {
// assertEquals(taskData.getTaskId(), taskData2.getTaskId());
//
// assertEquals(taskData.getRoot().toString(), taskData2.getRoot().toString());
- }
+// }
+//
+// public void testRead() throws Exception {
+// File file = TaskTestUtil.getFile("testdata/taskdata-1.0-bug-219897.zip");
+// ZipInputStream in = new ZipInputStream(new FileInputStream(file));
+// try {
+// in.getNextEntry();
+// @SuppressWarnings("unused")
+// ITaskDataWorkingCopy state = externalizer.readState(in);
+// } finally {
+// in.close();
+// }
+// }
+//
+// public void testReadWrite() throws Exception {
+// File file = TaskTestUtil.getFile("testdata/taskdata-1.0-bug-219897.zip");
+// ZipInputStream in = new ZipInputStream(new FileInputStream(file));
+// ITaskDataWorkingCopy state;
+// try {
+// in.getNextEntry();
+// state = externalizer.readState(in);
+// } finally {
+// in.close();
+// }
+// ByteArrayOutputStream out = new ByteArrayOutputStream();
+// externalizer.writeState(out, state);
+// TaskDataState state2 = externalizer.readState(new ByteArrayInputStream(out.toByteArray()));
+// assertEquals(state.getConnectorKind(), state2.getConnectorKind());
+// assertEquals(state.getRepositoryUrl(), state2.getRepositoryUrl());
+// assertEquals(state.getTaskId(), state2.getTaskId());
+//
+// assertEquals(state.getRepositoryData().getRoot().toString(), state2.getRepositoryData().getRoot().toString());
+// }
- public void testRead() throws Exception {
- File file = TaskTestUtil.getFile("testdata/taskdata-1.0-bug-219897.zip");
- ZipInputStream in = new ZipInputStream(new FileInputStream(file));
- try {
- in.getNextEntry();
- @SuppressWarnings("unused")
- ITaskDataWorkingCopy state = externalizer.readState(in);
- } finally {
- in.close();
- }
- }
+ public void testReadWriteInvalidCharacters() throws Exception {
+ TaskData data = new TaskData(new TaskAttributeMapper(repository), repository.getConnectorKind(),
+ repository.getRepositoryUrl(), "1");
+ data.getRoot().createAttribute("attribute").setValue("\u0001\u001F");
+
+ TaskDataState state = new TaskDataState(repository.getConnectorKind(), repository.getRepositoryUrl(), "1");
+ state.setRepositoryData(data);
- public void testReadWrite() throws Exception {
- File file = TaskTestUtil.getFile("testdata/taskdata-1.0-bug-219897.zip");
- ZipInputStream in = new ZipInputStream(new FileInputStream(file));
- ITaskDataWorkingCopy state;
- try {
- in.getNextEntry();
- state = externalizer.readState(in);
- } finally {
- in.close();
- }
ByteArrayOutputStream out = new ByteArrayOutputStream();
externalizer.writeState(out, state);
- TaskDataState state2 = externalizer.readState(new ByteArrayInputStream(out.toByteArray()));
- assertEquals(state.getConnectorKind(), state2.getConnectorKind());
- assertEquals(state.getRepositoryUrl(), state2.getRepositoryUrl());
- assertEquals(state.getTaskId(), state2.getTaskId());
+ try {
+ externalizer.readState(new ByteArrayInputStream(out.toByteArray()));
+ fail("Expected SAXParseException");
+ } catch (SAXParseException expected) {
+ }
+ TaskDataState state2 = externalizer.readState(new Xml11InputStream(new ByteArrayInputStream(out.toByteArray())));
assertEquals(state.getRepositoryData().getRoot().toString(), state2.getRepositoryData().getRoot().toString());
-
+ assertEquals("\u0001\u001F", state2.getRepositoryData().getRoot().getAttribute("attribute").getValue());
}
- public void testWriteandReadBadCharacter() throws Exception {
- for (int i = 0; i < 0xFFFF; i++) {
- char badChar = (char) i;
-
- StringWriter stringWriter = new StringWriter();
- SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
- TransformerHandler handler = transformerFactory.newTransformerHandler();
- Transformer serializer = handler.getTransformer();
- serializer.setOutputProperty(OutputKeys.VERSION, "1.0");
- serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
- serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
- handler.setResult(new StreamResult(stringWriter));
- SimpleCharacterWriter writer = new SimpleCharacterWriter(handler);
- writer.write(badChar);
-
- XMLReader parser = XMLReaderFactory.createXMLReader();
- SimpleCharacterReader readHandler = new SimpleCharacterReader();
- parser.setEntityResolver(new EntityResolver() {
- public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
- System.out.println(publicId);
- System.out.println(systemId);
- return null;
- }
- });
- parser.setContentHandler(readHandler);
- parser.parse(new InputSource(new StringReader(stringWriter.getBuffer().toString())));
- char character = readHandler.getCharacter();
- assertEquals(badChar, character);
- }
- }
}
diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/Xml11InputStreamTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/Xml11InputStreamTest.java
new file mode 100644
index 000000000..ae8a1be11
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/Xml11InputStreamTest.java
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tasks.tests.data;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.EOFException;
+import java.io.InputStreamReader;
+
+import junit.framework.TestCase;
+
+import org.eclipse.mylyn.internal.tasks.core.data.TaskDataExternalizer.Xml11InputStream;
+
+/**
+ * @author Steffen Pingel
+ */
+public class Xml11InputStreamTest extends TestCase {
+
+ public void testShortStream() throws Exception {
+ ByteArrayInputStream source = new ByteArrayInputStream("foo bar".getBytes());
+ Xml11InputStream in = new Xml11InputStream(source);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ try {
+ assertEquals("<?xml version=\"1.1\" encoding=\"UTF-8\"?>", reader.readLine());
+ fail("Expected EOFException");
+ } catch (EOFException expected) {
+ }
+ }
+
+ public void testXml10Stream() throws Exception {
+ ByteArrayInputStream source = new ByteArrayInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>".getBytes());
+ Xml11InputStream in = new Xml11InputStream(source);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ assertEquals("<?xml version=\"1.1\" encoding=\"UTF-8\"?>", reader.readLine());
+ assertEquals(-1, in.read());
+ }
+
+ public void testXml10StreamMultiLines() throws Exception {
+ ByteArrayInputStream source = new ByteArrayInputStream(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<abc>\n<def>".getBytes());
+ Xml11InputStream in = new Xml11InputStream(source);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+ assertEquals("<?xml version=\"1.1\" encoding=\"UTF-8\"?>", reader.readLine());
+ assertEquals("<abc>", reader.readLine());
+ assertEquals("<def>", reader.readLine());
+ assertEquals(null, reader.readLine());
+ }
+
+ public void testSkipRead() throws Exception {
+ ByteArrayInputStream source = new ByteArrayInputStream(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes());
+ Xml11InputStream in = new Xml11InputStream(source);
+ assertEquals(0, in.skip(0));
+ assertEquals('<', in.read());
+ assertEquals(5, in.skip(5));
+ assertEquals('v', in.read());
+ }
+
+ public void testSkipReadLine() throws Exception {
+ ByteArrayInputStream source = new ByteArrayInputStream(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes());
+ Xml11InputStream in = new Xml11InputStream(source);
+ assertEquals(3, in.skip(3));
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+ assertEquals("ml version=\"1.1\" encoding=\"UTF-8\"?>", reader.readLine());
+ assertEquals(null, reader.readLine());
+ }
+
+ public void testSkipHeader() throws Exception {
+ ByteArrayInputStream source = new ByteArrayInputStream(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>abc".getBytes());
+ Xml11InputStream in = new Xml11InputStream(source);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+ assertEquals(38, reader.skip(38));
+ assertEquals("abc", reader.readLine());
+ assertEquals(null, reader.readLine());
+ }
+
+}
diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/XmlExternalizationTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/XmlExternalizationTest.java
new file mode 100644
index 000000000..de0cbd45c
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/data/XmlExternalizationTest.java
@@ -0,0 +1,160 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tasks.tests.data;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.eclipse.mylyn.internal.tasks.core.data.ITaskDataConstants;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.DefaultHandler;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+/**
+ * @author Steffen Pingel
+ * @author Shawn Minto
+ */
+public class XmlExternalizationTest extends TestCase {
+
+ private class SimpleCharacterReader extends DefaultHandler {
+
+ private char ch;
+
+ public SimpleCharacterReader() {
+ }
+
+ @Override
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ //System.err.println(Arrays.toString(ch));
+ Assert.assertEquals(1, length);
+ this.ch = ch[start];
+ }
+
+ public char getCharacter() {
+ return ch;
+ }
+
+ }
+
+ private final class SimpleCharacterWriter {
+ private final TransformerHandler handler;
+
+ public SimpleCharacterWriter(TransformerHandler handler) {
+ this.handler = handler;
+ }
+
+ public void write(char character) throws SAXException {
+ handler.startDocument();
+ AttributesImpl atts = new AttributesImpl();
+ handler.startElement("", "", ITaskDataConstants.ELEMENT_VALUE, atts); //$NON-NLS-1$ //$NON-NLS-2$
+ ///handler.startCDATA();
+ handler.characters(new char[] { character }, 0, 1);
+ //handler.endCDATA();
+ handler.endElement("", "", ITaskDataConstants.ELEMENT_VALUE);
+ handler.endDocument();
+ }
+ }
+
+ public void testWriteandReadBadCharacterXml10() throws Exception {
+ for (int i = 0; i < 0xFFFF; i++) {
+ char badChar = (char) i;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
+ TransformerHandler handler = transformerFactory.newTransformerHandler();
+ Transformer serializer = handler.getTransformer();
+ serializer.setOutputProperty(OutputKeys.VERSION, "1.0");
+ serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
+ serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
+ handler.setResult(new StreamResult(out));
+ SimpleCharacterWriter writer = new SimpleCharacterWriter(handler);
+ writer.write(badChar);
+
+ XMLReader parser = XMLReaderFactory.createXMLReader();
+ parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
+ SimpleCharacterReader readHandler = new SimpleCharacterReader();
+ parser.setErrorHandler(new ErrorHandler() {
+ public void warning(SAXParseException exception) throws SAXException {
+ exception.printStackTrace();
+ }
+
+ public void fatalError(SAXParseException exception) throws SAXException {
+ exception.printStackTrace();
+ }
+
+ public void error(SAXParseException exception) throws SAXException {
+ exception.printStackTrace();
+ }
+ });
+ parser.setContentHandler(readHandler);
+ parser.parse(new InputSource(new ByteArrayInputStream(out.toByteArray())));
+ char character = readHandler.getCharacter();
+ assertEquals(badChar, character);
+ }
+ }
+
+ public void testWriteandReadBadCharacterXml11() throws Exception {
+ for (int i = 0; i < 0xFFFF; i++) {
+ char badChar = (char) i;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
+ TransformerHandler handler = transformerFactory.newTransformerHandler();
+ Transformer serializer = handler.getTransformer();
+ serializer.setOutputProperty(OutputKeys.VERSION, "1.1");
+ serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
+ serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
+ handler.setResult(new StreamResult(out));
+ SimpleCharacterWriter writer = new SimpleCharacterWriter(handler);
+ writer.write(badChar);
+
+ XMLReader parser = XMLReaderFactory.createXMLReader();
+ parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
+ SimpleCharacterReader readHandler = new SimpleCharacterReader();
+ parser.setErrorHandler(new ErrorHandler() {
+ public void warning(SAXParseException exception) throws SAXException {
+ exception.printStackTrace();
+ }
+
+ public void fatalError(SAXParseException exception) throws SAXException {
+ exception.printStackTrace();
+ }
+
+ public void error(SAXParseException exception) throws SAXException {
+ exception.printStackTrace();
+ }
+ });
+ parser.setContentHandler(readHandler);
+ parser.parse(new InputSource(new ByteArrayInputStream(out.toByteArray())));
+ char character = readHandler.getCharacter();
+ assertEquals(badChar, character);
+ }
+ }
+
+}

Back to the top