public static void TypeConverterStringResources() { var values = TestData.StringConverter; byte[] binaryWriterBuffer; using (MemoryStream ms = new MemoryStream()) using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms)) { foreach (var pair in values) { writer.AddResource(pair.Key, TestData.GetStringValue(pair.Value), TestData.GetSerializationTypeName(pair.Value.GetType())); } writer.Generate(); binaryWriterBuffer = ms.ToArray(); } using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false)) using (DeserializingResourceReader reader = new DeserializingResourceReader(ms)) { IDictionaryEnumerator dictEnum = reader.GetEnumerator(); while (dictEnum.MoveNext()) { ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value); } } }
public static void StreamResources() { var values = TestData.Activator; byte[] binaryWriterBuffer; using (MemoryStream ms = new MemoryStream()) using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms)) { foreach (var pair in values) { pair.Value.stream.Seek(0, SeekOrigin.Begin); writer.AddActivatorResource(pair.Key, pair.Value.stream, TestData.GetSerializationTypeName(pair.Value.type), false); } writer.Generate(); binaryWriterBuffer = ms.ToArray(); } using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false)) using (DeserializingResourceReader reader = new DeserializingResourceReader(ms)) { IDictionaryEnumerator dictEnum = reader.GetEnumerator(); while (dictEnum.MoveNext()) { var expectedTuple = values[(string)dictEnum.Key]; expectedTuple.stream.Seek(0, SeekOrigin.Begin); object expected = Activator.CreateInstance(expectedTuple.type, new object[] { expectedTuple.stream }); ResourceValueEquals(expected, dictEnum.Value); } } }
public static void BinaryFormattedResourcesWithoutTypeName() { var values = TestData.BinaryFormatted; byte[] binaryWriterBuffer; using (MemoryStream ms = new MemoryStream()) using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); foreach (var pair in values) { using (MemoryStream memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, pair.Value); writer.AddBinaryFormattedResource(pair.Key, memoryStream.ToArray()); } } writer.Generate(); binaryWriterBuffer = ms.ToArray(); } // DeserializingResourceReader can read ResourceReader format using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false)) using (DeserializingResourceReader reader = new DeserializingResourceReader(ms)) { IDictionaryEnumerator dictEnum = reader.GetEnumerator(); while (dictEnum.MoveNext()) { ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value); } } }
public static void PrimitiveResourcesAsStrings() { IReadOnlyDictionary <string, object> values = TestData.Primitive; byte[] writerBuffer, binaryWriterBuffer; using (MemoryStream ms = new MemoryStream()) using (ResourceWriter writer = new ResourceWriter(ms)) { foreach (var pair in values) { writer.AddResource(pair.Key, pair.Value); } writer.Generate(); writerBuffer = ms.ToArray(); } using (MemoryStream ms = new MemoryStream()) using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms)) { foreach (var pair in values) { writer.AddResource(pair.Key, TestData.GetStringValue(pair.Value), TestData.GetSerializationTypeName(pair.Value.GetType())); } writer.Generate(); binaryWriterBuffer = ms.ToArray(); } // PreserializedResourceWriter should write ResourceWriter/ResourceReader format Assert.Equal(writerBuffer, binaryWriterBuffer); using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false)) using (ResourceReader reader = new ResourceReader(ms)) { IDictionaryEnumerator dictEnum = reader.GetEnumerator(); while (dictEnum.MoveNext()) { Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value); } } // DeserializingResourceReader can read ResourceReader format using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false)) using (DeserializingResourceReader reader = new DeserializingResourceReader(ms)) { IDictionaryEnumerator dictEnum = reader.GetEnumerator(); while (dictEnum.MoveNext()) { Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value); } } }
public static void EmptyResources() { byte[] writerBuffer, binaryWriterBuffer; using (MemoryStream ms = new MemoryStream()) using (ResourceWriter writer = new ResourceWriter(ms)) { writer.Generate(); writerBuffer = ms.ToArray(); } using (MemoryStream ms = new MemoryStream()) using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms)) { writer.Generate(); binaryWriterBuffer = ms.ToArray(); } Assert.Equal(writerBuffer, binaryWriterBuffer); }
public static void ExceptionforNullResourceId() { using (var writer = new PreserializedResourceWriter(new MemoryStream())) { Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, "value")); Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, new object())); Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, new byte[0])); using (var stream = new MemoryStream()) { Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, stream)); Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, stream, true)); Assert.Throws <ArgumentNullException>("name", () => writer.AddActivatorResource(null, stream, "System.DayOfWeek", false)); } Assert.Throws <ArgumentNullException>("name", () => writer.AddBinaryFormattedResource(null, new byte[1], "System.DayOfWeek")); Assert.Throws <ArgumentNullException>("name", () => writer.AddTypeConverterResource(null, new byte[1], "System.DayOfWeek")); Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, "Monday", "System.DayOfWeek")); } }
public static void ExceptionforDuplicateKey() { using (var writer = new PreserializedResourceWriter(new MemoryStream())) { writer.AddResource("duplicate", "value"); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", "value")); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", new object())); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", new byte[0])); using (var stream = new MemoryStream()) { AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", stream)); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", stream, true)); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false)); } AssertExtensions.Throws <ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek")); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek")); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", "Monday", "System.DayOfWeek")); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("Duplicate", "value")); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("dUplicate", new object())); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duPlicate", new byte[0])); using (var stream = new MemoryStream()) { AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("dupLicate", stream)); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplIcate", stream, true)); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddActivatorResource("dupliCate", stream, "System.DayOfWeek", false)); } AssertExtensions.Throws <ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicAte", new byte[1], "System.DayOfWeek")); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddTypeConverterResource("duplicaTe", new byte[1], "System.DayOfWeek")); AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicatE", "Monday", "System.DayOfWeek")); } }
public static void ExceptionForAddAfterGenerate() { using (var writer = new PreserializedResourceWriter(new MemoryStream())) { writer.AddResource("duplicate", "value"); writer.Generate(); Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", "value")); Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", new object())); Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", new byte[0])); using (var stream = new MemoryStream()) { Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", stream)); Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", stream, true)); Assert.Throws <InvalidOperationException>(() => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false)); } Assert.Throws <InvalidOperationException>(() => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek")); Assert.Throws <InvalidOperationException>(() => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek")); Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", "Monday", "System.DayOfWeek")); } }
public static void BinaryFormattedResources() { var values = TestData.BinaryFormatted; byte[] writerBuffer, binaryWriterBuffer; using (MemoryStream ms = new MemoryStream()) using (ResourceWriter writer = new ResourceWriter(ms)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); foreach (var pair in values) { using (MemoryStream memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, pair.Value); writer.AddResourceData(pair.Key, TestData.GetSerializationTypeName(pair.Value.GetType()), memoryStream.ToArray()); } } writer.Generate(); writerBuffer = ms.ToArray(); } using (MemoryStream ms = new MemoryStream()) using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); foreach (var pair in values) { using (MemoryStream memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, pair.Value); writer.AddBinaryFormattedResource(pair.Key, TestData.GetSerializationTypeName(pair.Value.GetType()), memoryStream.ToArray()); } } writer.Generate(); binaryWriterBuffer = ms.ToArray(); } // PreserializedResourceWriter should write ResourceWriter/ResourceReader format Assert.Equal(writerBuffer, binaryWriterBuffer); using (MemoryStream ms = new MemoryStream(writerBuffer, false)) using (ResourceReader reader = new ResourceReader(ms)) { typeof(ResourceReader).GetField("_permitDeserialization", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(reader, true); IDictionaryEnumerator dictEnum = reader.GetEnumerator(); while (dictEnum.MoveNext()) { ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value); } } // DeserializingResourceReader can read ResourceReader format using (MemoryStream ms = new MemoryStream(writerBuffer, false)) using (DeserializingResourceReader reader = new DeserializingResourceReader(ms)) { IDictionaryEnumerator dictEnum = reader.GetEnumerator(); while (dictEnum.MoveNext()) { ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value); } } }