Mono で NUnit テストを記述し、Java で JUnit テストを記述した後、答えは <tag>Hello
\nWorld!</tag> または <tag>Hello
\nWorld!</tag> のいずれかを使用するように見えます。以下のように...
Foo.cs:
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace XmlStringTests
{
public class Foo
{
public string greeting;
public static Foo DeserializeFromXmlString (string xml)
{
Foo result;
using (MemoryStream memoryStream = new MemoryStream()) {
byte[] buffer = Encoding.UTF8.GetBytes (xml);
memoryStream.Write (buffer, 0, buffer.Length);
memoryStream.Seek (0, SeekOrigin.Begin);
XmlSerializer xs = new XmlSerializer (typeof(Foo));
result = (Foo)xs.Deserialize (memoryStream);
}
return result;
}
}
}
XmlStringTests.cs:
using NUnit.Framework;
namespace XmlStringTests
{
[TestFixture]
public class XmlStringTests
{
const string expected = "Hello\u000d\u000aWorld!";
[Test(Description="Fails")]
public void Cdata ()
{
const string test = "<Foo><greeting><![CDATA[Hello\u000d\u000aWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Fails")]
public void CdataWithHash13 ()
{
const string test = "<Foo><greeting><![CDATA[Hello \u000aWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Fails")]
public void CdataWithHashxD ()
{
const string test = "<Foo><greeting><![CDATA[Hello
\u000aWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Fails")]
public void Simple ()
{
const string test = "<Foo><greeting>Hello\u000d\u000aWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Passes")]
public void SimpleWithHash13 ()
{
const string test = "<Foo><greeting>Hello \u000aWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
[Test(Description="Passes")]
public void SimpleWithHashxD ()
{
const string test = "<Foo><greeting>Hello
\u000aWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
Assert.AreEqual (expected, bar.greeting);
}
}
}
Foo.java:
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Foo")
@XmlType(propOrder = { "greeting" })
public class Foo {
public String greeting;
public static Foo DeserializeFromXmlString(String xml) {
try {
JAXBContext context = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Foo foo = (Foo) unmarshaller.unmarshal(new StringReader(xml));
return foo;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
}
XmlStringTests.java:
import static org.junit.Assert.*;
import org.junit.Test;
public class XmlStringTests {
String expected = "Hello\r\nWorld!";
@Test //Fails
public void testCdata ()
{
String test = "<Foo><greeting><![CDATA[Hello\r\nWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Fails
public void testCdataWithHash13 ()
{
String test = "<Foo><greeting><![CDATA[Hello \nWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Fails
public void testCdataWithHashxD ()
{
String test = "<Foo><greeting><![CDATA[Hello
\nWorld!]]></greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Fails
public void testSimple ()
{
String test = "<Foo><greeting>Hello\r\nWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Passes
public void testSimpleWithHash13 ()
{
String test = "<Foo><greeting>Hello \nWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
@Test //Passes
public void testSimpleWithHashxD ()
{
String test = "<Foo><greeting>Hello
\nWorld!</greeting></Foo>";
Foo bar = Foo.DeserializeFromXmlString (test);
assertEquals (expected, bar.greeting);
}
}
これにより、一部の人々が時間を節約できることを願っています。