Skip to content

Commit

Permalink
Cleanup some XML tests (#65290)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmoseley committed Feb 15, 2022
1 parent fdb6d72 commit c1ee3cd
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ public class CTestException : Exception
public object Expected;

//Constructor
public CTestException(string message)
public CTestException(string message!!)
: this(CTestBase.TEST_FAIL, message)
{
}

public CTestException(int result, string message)
public CTestException(int result, string message!!)
: this(result, message, false, true, null)
{
}

public CTestException(int result, string message, object actual, object expected, Exception inner)
public CTestException(int result, string message!!, object actual, object expected, Exception inner)
: base(message, inner)
{
//Note: iResult is the variation result (i.e.: TEST_PASS, TEST_FAIL, etc...)
//Setup the exception
Result = result;
Actual = actual;
Expected = expected;
Actual = actual ?? "";
Expected = expected ?? "";
}

public override string Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public override string XmlLang
}
}

public override void Close()
{
Dispose(disposing:true);
}

protected override void Dispose(bool disposing)
{
if (disposing)
Expand Down
78 changes: 15 additions & 63 deletions src/libraries/Common/tests/System/Xml/XmlCoreTest/FilePathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public static string ExpandVariables(string inputPath)
return resultPath.ToString();
}

private static MyDict<string, Stream> s_XmlFileInMemoryCache = null;
private static MyDict<string, Stream> s_XmlFileInMemoryCacheBackup = null;
private static MyDict<string, MemoryStream> s_XmlFileInMemoryCache = null;

private static readonly object s_XmlFileMemoryCacheLock = new object();
static void initXmlFileCacheIfNotYet()
Expand All @@ -121,35 +120,20 @@ static void initXmlFileCacheIfNotYet()
{
if (s_XmlFileInMemoryCache == null)
{
s_XmlFileInMemoryCache = new MyDict<string, Stream>();
s_XmlFileInMemoryCacheBackup = new MyDict<string, Stream>();
s_XmlFileInMemoryCache = new MyDict<string, MemoryStream>();

foreach (var file in GetDataFiles())
foreach (Tuple<string, byte[]> file in GetDataFiles())
{
addBytes(file.Item1, file.Item2);
var ms = new MemoryStream(file.Item2);
s_XmlFileInMemoryCache[NormalizeFilePath(file.Item1)] = ms;
}
}
}
}

public static void cacheXml(string filename, string content)
{
initXmlFileCacheIfNotYet();
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write(content);
sw.Flush();
s_XmlFileInMemoryCache[NormalizeFilePath(filename)] = ms;

MemoryStream msbak = new MemoryStream();
ms.Position = 0;
ms.CopyTo(msbak);
s_XmlFileInMemoryCacheBackup[NormalizeFilePath(filename)] = msbak;
}

public static Stream getStreamDirect(string filename)
{
foreach (var file in GetDataFiles())
foreach (Tuple<string, byte[]> file in GetDataFiles())
{
if (file.Item1 == filename)
{
Expand All @@ -171,51 +155,23 @@ public static Stream getStream(string filename)

lock (s_XmlFileMemoryCacheLock)
{
Stream s = s_XmlFileInMemoryCache[normalizedFileName];
MemoryStream ms = s_XmlFileInMemoryCache[normalizedFileName];

if (s == null)
if (ms == null)
{
throw new FileNotFoundException("File Not Found: " + filename);
}

if (s.CanSeek)
{
s.Position = 0;
return s;
}
else
{
Stream msbak = s_XmlFileInMemoryCacheBackup[normalizedFileName];
MemoryStream msnew = new MemoryStream();
msbak.Position = 0;
msbak.CopyTo(msnew);

s_XmlFileInMemoryCache[normalizedFileName] = msnew;
msnew.Position = 0;
return msnew;
}
}
}

public static void addBytes(string filename, byte[] bytes)
{
if (null == filename)
return;

initXmlFileCacheIfNotYet();

lock (s_XmlFileMemoryCacheLock)
{
var ms = new MemoryStream(bytes);
s_XmlFileInMemoryCache[NormalizeFilePath(filename)] = ms;
MemoryStream msbak = new MemoryStream();
// Always give out a new stream, so there's no concern about concurrent use
MemoryStream msnew = new MemoryStream();
ms.Position = 0;
ms.CopyTo(msbak);
s_XmlFileInMemoryCacheBackup[NormalizeFilePath(filename)] = msbak;
ms.CopyTo(msnew);
msnew.Position = 0;
return msnew;
}
}

public static void addStream(string filename, Stream s)
public static void addStream(string filename, MemoryStream s!!)
{
if (null == filename)
return;
Expand All @@ -224,12 +180,8 @@ public static void addStream(string filename, Stream s)

lock (s_XmlFileMemoryCacheLock)
{
// overwrite any existing
s_XmlFileInMemoryCache[NormalizeFilePath(filename)] = s;

MemoryStream msbak = new MemoryStream();
s.Position = 0;
s.CopyTo(msbak);
s_XmlFileInMemoryCacheBackup[NormalizeFilePath(filename)] = msbak;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public WriterFactory(WriterType t, bool overrideAsync = false, bool async = fals

private XmlWriterSettings _wSettings = null;
private XmlWriter _xmlWriter = null;
private Stream _writerStream = null;
private MemoryStream _writerStream = null;

XmlWriter CreateWriterImpl()
{
Expand Down Expand Up @@ -90,9 +90,8 @@ XmlWriter CreateWriterImpl()
_wSettings.CloseOutput = false;
if (_overrideAsync)
_wSettings.Async = _async;

_xmlWriter = new CustomWriter(_writerStream, _wSettings);
FilePathUtil.addStream(_fileName, _writerStream);
_xmlWriter = new CustomWriter(_fileName, _wSettings);
break;
case WriterType.UTF8WriterIndent:
_writerStream = new MemoryStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected static void DeleteTestFile(string strFileName)
public static void CreateGenericTestFile(string strFileName)
{
MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms);
using var tw = new StreamWriter(ms, encoding:null, bufferSize:-1, leaveOpen:true);

tw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
tw.WriteLine("<!-- comment1 -->");
Expand Down Expand Up @@ -170,7 +170,6 @@ public static void CreateGenericTestFile(string strFileName)
tw.WriteLine("<CHARS_COMMENT1>xxx<!-- comment1-->zzz</CHARS_COMMENT1>");
tw.WriteLine("<CHARS_COMMENT2><!-- comment1-->zzz</CHARS_COMMENT2>");
tw.WriteLine("<CHARS_COMMENT3>xxx<!-- comment1--></CHARS_COMMENT3>");
tw.Flush();
tw.WriteLine("<ISDEFAULT />");
tw.WriteLine("<ISDEFAULT a1='a1value' />");
tw.WriteLine("<BOOLEAN1>true</BOOLEAN1>");
Expand Down Expand Up @@ -202,7 +201,6 @@ public static void CreateGenericTestFile(string strFileName)
tw.WriteLine("<GRPDESCR>twin brothers, and sons to Aegeon and Aemilia.</GRPDESCR>");
tw.WriteLine("</PGROUP>");
tw.WriteLine("<PGROUP>");
tw.Flush();
tw.WriteLine("<XMLLANG0 xml:lang=\"en-US\">What color e1foo is it?</XMLLANG0>");
tw.Write("<XMLLANG1 xml:lang=\"en-GB\">What color is it?<a><b><c>Language Test</c><PERSONA>DROMIO OF EPHESUS</PERSONA></b></a></XMLLANG1>");
tw.WriteLine("<NOXMLLANG />");
Expand Down Expand Up @@ -269,28 +267,26 @@ public static void CreateGenericTestFile(string strFileName)


tw.Write("</PLAY>");
tw.Flush();

FilePathUtil.addStream(strFileName, ms);
}

public static void CreateBigElementTestFile(string strFileName)
{
MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms);
using var tw = new StreamWriter(ms, encoding:null, bufferSize:-1, leaveOpen:true);

string str = new string('Z', (1 << 20) - 1);
tw.WriteLine("<Root>");
tw.Write("<");
tw.Write(str);
tw.WriteLine("X />");
tw.Flush();

tw.Write("<");
tw.Write(str);
tw.WriteLine("Y />");
tw.WriteLine("</Root>");

tw.Flush();
FilePathUtil.addStream(strFileName, ms);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6352,12 +6352,12 @@ public partial class TCClose
[XmlWriterInlineData]
public void var_1(XmlWriterUtils utils)
{
using (XmlWriter writer = utils.CreateWriter())
{
writer.WriteStartElement("Root");
writer.WriteStartElement("Nesting");
writer.WriteStartElement("SomeDeep");
}
XmlWriter writer = utils.CreateWriter();
writer.WriteStartElement("Root");
writer.WriteStartElement("Nesting");
writer.WriteStartElement("SomeDeep");
writer.Close();

Assert.True(utils.CompareReader("<Root><Nesting><SomeDeep /></Nesting></Root>"));
}

Expand Down

0 comments on commit c1ee3cd

Please sign in to comment.