1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using OLEDB.Test.ModuleCore; 6 using System.IO; 7 using System.Text; 8 using XmlCoreTest.Common; 9 10 namespace System.Xml.Tests 11 { 12 /// <summary> 13 /// Summary description for WriterFactory. 14 /// </summary> 15 public class CWriterFactory : CFactory 16 { 17 protected enum WriterType 18 { 19 UTF8Writer, 20 UnicodeWriter 21 }; 22 23 protected enum WriteThru 24 { 25 Stream, 26 StringBuilder, 27 TextWriter, 28 XmlWriter 29 }; 30 31 protected enum WriterOverload 32 { 33 StringBuilder, 34 StringWriter, 35 StreamWriter, 36 MemoryStream, 37 TextWriter, 38 UTF8Writer, 39 UnicodeWriter 40 }; 41 42 private XmlWriter _factoryWriter = null; 43 private XmlWriter _underlyingWriter = null; 44 private Stream _stream = null; 45 private StringBuilder _stringBuilder = null; 46 private TextWriter _textWriter = null; 47 private XmlWriterSettings _settings = new XmlWriterSettings(); 48 private XmlWriterSettings _underlyingSettings = new XmlWriterSettings(); 49 50 private WriterOverload _overload; 51 PreTest()52 protected override void PreTest() 53 { 54 Log("In Pretest..."); 55 SetupSettings(); 56 Log("SetupSettings Done"); 57 SetupWriterOverload(); 58 Log("SetupWriterOverload Done"); 59 60 pstate = TestState.PreTest; 61 } 62 Test()63 protected override void Test() 64 { 65 Log("Testing : " + TestFileName); 66 Log("Overload : " + _overload); 67 switch (_overload) 68 { 69 case WriterOverload.MemoryStream: 70 _stream = new MemoryStream(); 71 CreateWriter(WriteThru.Stream); 72 break; 73 74 case WriterOverload.StreamWriter: 75 _textWriter = new StreamWriter(new MemoryStream()); 76 CreateWriter(WriteThru.TextWriter); 77 break; 78 79 case WriterOverload.StringBuilder: 80 _stringBuilder = new StringBuilder(); 81 CreateWriter(WriteThru.StringBuilder); 82 break; 83 84 case WriterOverload.StringWriter: 85 _textWriter = new StringWriter(); 86 CreateWriter(WriteThru.TextWriter); 87 break; 88 89 case WriterOverload.UnicodeWriter: 90 _underlyingSettings = new XmlWriterSettings(); 91 _underlyingSettings.Encoding = Encoding.Unicode; 92 _underlyingWriter = WriterHelper.Create(TestFileName, _underlyingSettings); 93 CreateWriter(WriteThru.XmlWriter); 94 break; 95 96 case WriterOverload.UTF8Writer: 97 _underlyingSettings = new XmlWriterSettings(); 98 _underlyingSettings.Encoding = Encoding.UTF8; 99 _underlyingWriter = WriterHelper.Create(TestFileName, _underlyingSettings); 100 CreateWriter(WriteThru.XmlWriter); 101 break; 102 103 default: 104 throw new CTestFailedException("Unknown WriterOverload: " + _overload); 105 } 106 107 if (pstate == TestState.Pass) 108 return; 109 110 CError.Compare(pstate, TestState.CreateSuccess, "Invalid State after Create: " + pstate); 111 112 //By this time the factory Reader is already set up correctly. So we must go Consume it now. 113 CError.Compare(pstate != TestState.Pass && pstate == TestState.CreateSuccess, "Invalid state before Consuming Reader: " + pstate); 114 115 //Call TestWriter to Consume Reader; 116 TestWriter(); 117 if (pstate == TestState.Pass) return; 118 CError.Compare(pstate != TestState.Pass && pstate == TestState.Consume, "Invalid state after Consuming Reader: " + pstate); 119 } 120 TestWriter()121 protected void TestWriter() 122 { 123 pstate = TestState.Consume; 124 125 try 126 { 127 WriteNodes(); 128 if (!IsVariationValid) 129 { 130 //Invalid Case didn't throw exception. 131 pstate = TestState.Error; 132 DumpVariationInfo(); 133 throw new CTestFailedException("Invalid Variation didn't throw exception"); 134 } 135 else 136 { 137 pstate = TestState.Pass; 138 } 139 } 140 catch (XmlException writerException) 141 { 142 Log(writerException.Message); 143 Log(writerException.StackTrace); 144 if (!IsVariationValid) 145 { 146 if (!CheckException(writerException)) 147 { 148 pstate = TestState.Error; 149 DumpVariationInfo(); 150 throw new CTestFailedException("Invalid Exception Type thrown"); 151 } 152 else 153 { 154 pstate = TestState.Pass; 155 } 156 } 157 else //Variation was valid 158 { 159 pstate = TestState.Error; 160 DumpVariationInfo(); 161 throw new CTestFailedException("Valid Variation throws Unspecified Exception"); 162 } 163 } 164 finally 165 { 166 if (_factoryWriter != null && _factoryWriter.WriteState != WriteState.Closed && _factoryWriter.WriteState != WriteState.Error) 167 { 168 if (_textWriter == null) 169 { 170 CError.WriteLineIgnore(_factoryWriter.WriteState.ToString()); 171 _factoryWriter.Flush(); 172 _factoryWriter.Dispose(); 173 } 174 else 175 { 176 _textWriter.Flush(); 177 _textWriter.Dispose(); 178 } 179 } 180 if (_underlyingWriter != null && _underlyingWriter.WriteState != WriteState.Closed && _underlyingWriter.WriteState != WriteState.Error) 181 { 182 _underlyingWriter.Flush(); 183 _underlyingWriter.Dispose(); 184 } 185 } 186 187 //If you are not in PASS state at this point you are in Error. 188 if (pstate != TestState.Pass) 189 pstate = TestState.Error; 190 } 191 CompareSettings()192 protected void CompareSettings() 193 { 194 XmlWriterSettings actual = _factoryWriter.Settings; 195 196 CError.Compare(actual.CheckCharacters, _settings.CheckCharacters, "CheckCharacters"); 197 198 //This actually checks Conformance Level DCR to some extent. 199 if (_settings.ConformanceLevel != ConformanceLevel.Auto) 200 CError.Compare(actual.ConformanceLevel, _settings.ConformanceLevel, "ConformanceLevel"); 201 202 CError.Compare(actual.Encoding, _settings.Encoding, "Encoding"); 203 CError.Compare(actual.Indent, _settings.Indent, "Indent"); 204 CError.Compare(actual.IndentChars, _settings.IndentChars, "IndentChars"); 205 CError.Compare(actual.NewLineChars, _settings.NewLineChars, "NewLineChars"); 206 CError.Compare(actual.NewLineOnAttributes, _settings.NewLineOnAttributes, "NewLineOnAttributes"); 207 CError.Compare(actual.NewLineHandling, _settings.NewLineHandling, "NormalizeNewLines"); 208 CError.Compare(actual.OmitXmlDeclaration, _settings.OmitXmlDeclaration, "OmitXmlDeclaration"); 209 } 210 CreateWriter(WriteThru writeThru)211 protected void CreateWriter(WriteThru writeThru) 212 { 213 // Assumption is that the Create method doesn't throw NullReferenceException and 214 // it is not the goal of this framework to test if they are thrown anywhere. 215 // but if they are thrown that's a problem and they shouldn't be caught but exposed. 216 217 Log("Writing thru : " + writeThru); 218 219 try 220 { 221 switch (writeThru) 222 { 223 case WriteThru.Stream: 224 _factoryWriter = WriterHelper.Create(_stream, _settings); 225 break; 226 case WriteThru.StringBuilder: 227 _factoryWriter = WriterHelper.Create(_stringBuilder, _settings); 228 break; 229 case WriteThru.TextWriter: 230 _factoryWriter = WriterHelper.Create(_textWriter, _settings); 231 break; 232 case WriteThru.XmlWriter: 233 _factoryWriter = WriterHelper.Create(_underlyingWriter, _settings); 234 break; 235 } 236 237 pstate = TestState.CreateSuccess; 238 } 239 catch (Exception ane) 240 { 241 Log(ane.ToString()); 242 if (!IsVariationValid) 243 { 244 if (!CheckException(ane)) 245 { 246 pstate = TestState.Error; 247 DumpVariationInfo(); 248 throw new CTestFailedException( 249 "Exception Thrown in CreateMethod, is your variation data correct?"); 250 } 251 else 252 { 253 //This means that the Exception was checked and everything is fine. 254 pstate = TestState.Pass; 255 } 256 }//Else valid variation threw exception 257 else 258 { 259 pstate = TestState.Error; 260 DumpVariationInfo(); 261 throw new CTestFailedException( 262 "Exception Thrown in CreateMethod, is your variation data correct?"); 263 } 264 } 265 } 266 PostTest()267 protected override void PostTest() 268 { 269 pstate = TestState.Complete; 270 } 271 SetupSettings()272 protected void SetupSettings() 273 { 274 _settings.ConformanceLevel = (ConformanceLevel)Enum.Parse(typeof(ConformanceLevel), ReadFilterCriteria("ConformanceLevel", true)); 275 _settings.CheckCharacters = Boolean.Parse(ReadFilterCriteria("CheckCharacters", true)); 276 _settings.CloseOutput = false; 277 278 _settings.Indent = Boolean.Parse(ReadFilterCriteria("Indent", true)); 279 _settings.IndentChars = new String(Convert.ToChar(Int32.Parse(ReadFilterCriteria("IndentChars", true))), 1); 280 _settings.NewLineChars = new String(Convert.ToChar(Int32.Parse(ReadFilterCriteria("NewLineChars", true))), 1); 281 _settings.NewLineOnAttributes = Boolean.Parse(ReadFilterCriteria("NewLineOnAttributes", true)); 282 if (Boolean.Parse(ReadFilterCriteria("NormalizeNewlines", true))) 283 _settings.NewLineHandling = NewLineHandling.Replace; 284 else 285 _settings.NewLineHandling = NewLineHandling.None; 286 287 _settings.OmitXmlDeclaration = Boolean.Parse(ReadFilterCriteria("OmitXmlDeclaration", true)); 288 289 //Reading Writer Type to determine encoding and if the writer type is binary writer. 290 string wt = ReadFilterCriteria("WriterType", true); 291 if (wt == "TextWriter" || wt == "XmlDocumentWriter") 292 { 293 throw new CTestSkippedException("Skipped: WriterType " + wt); 294 } 295 WriterType writerType = (WriterType)Enum.Parse(typeof(WriterType), wt); 296 switch (writerType) 297 { 298 case WriterType.UnicodeWriter: 299 _settings.Encoding = Encoding.Unicode; 300 break; 301 default: 302 break; 303 } 304 } 305 SetupWriterOverload()306 protected void SetupWriterOverload() 307 { 308 string ol = ReadFilterCriteria("Load", true); 309 if (ol == "FileName" || ol == "XmlDocumentWriter" || ol == "InvalidUri") 310 { 311 throw new CTestSkippedException("Skipped: OverLoad " + ol); 312 } 313 _overload = (WriterOverload)Enum.Parse(typeof(WriterOverload), ol); //ReadFilterCriteria("Load", true)); 314 } 315 316 317 /// <summary> 318 /// This function writes the test nodes on the factoryWriter. 319 /// This will be called from Test(). If successful it will just return, 320 /// else it will throw an appropriate XmlException. This function can use 321 /// the knowledge of the current writertype to write appropriate data if 322 /// really needed. 323 /// </summary> WriteNodes()324 protected void WriteNodes() 325 { 326 _factoryWriter.WriteStartElement("a", "b", "c"); 327 _factoryWriter.WriteStartElement("d", "e"); 328 _factoryWriter.WriteStartElement("f"); 329 _factoryWriter.WriteStartAttribute("g", "h", "i"); 330 _factoryWriter.WriteStartAttribute("j", "k"); 331 _factoryWriter.WriteStartAttribute("l"); 332 _factoryWriter.WriteString("Some String"); 333 _factoryWriter.WriteEndElement(); 334 _factoryWriter.WriteRaw("<thisisraw/>"); 335 _factoryWriter.WriteProcessingInstruction("somepiname", "somepitext"); 336 _factoryWriter.WriteValue(1000); 337 _factoryWriter.WriteComment("SomeComment"); 338 _factoryWriter.WriteEndElement(); 339 _factoryWriter.WriteCData("< is not a valid thing"); 340 _factoryWriter.WriteCharEntity('a'); 341 _factoryWriter.WriteWhitespace(" "); 342 _factoryWriter.WriteEndElement(); 343 } 344 } 345 } 346