AbstractMessageTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://github.com/jskeet/dotnet-protobufs/
  4. // Original C++/Java/Python code:
  5. // http://code.google.com/p/protobuf/
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. using System;
  33. using System.Collections.Generic;
  34. using Google.ProtocolBuffers.Descriptors;
  35. using NUnit.Framework;
  36. using Google.ProtocolBuffers.TestProtos;
  37. namespace Google.ProtocolBuffers {
  38. [TestFixture]
  39. public class AbstractMessageTest {
  40. [Test]
  41. public void Clear() {
  42. AbstractMessageWrapper message = new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder(TestUtil.GetAllSet())).Clear().Build();
  43. TestUtil.AssertClear((TestAllTypes) message.WrappedMessage);
  44. }
  45. [Test]
  46. public void Copy() {
  47. AbstractMessageWrapper message = new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder()).MergeFrom(TestUtil.GetAllSet()).Build();
  48. TestUtil.AssertAllFieldsSet((TestAllTypes) message.WrappedMessage);
  49. }
  50. [Test]
  51. public void SerializedSize() {
  52. TestAllTypes message = TestUtil.GetAllSet();
  53. IMessage abstractMessage = new AbstractMessageWrapper(TestUtil.GetAllSet());
  54. Assert.AreEqual(message.SerializedSize, abstractMessage.SerializedSize);
  55. }
  56. [Test]
  57. public void Serialization() {
  58. IMessage abstractMessage = new AbstractMessageWrapper(TestUtil.GetAllSet());
  59. TestUtil.AssertAllFieldsSet(TestAllTypes.ParseFrom(abstractMessage.ToByteString()));
  60. Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), abstractMessage.ToByteString());
  61. }
  62. [Test]
  63. public void Parsing() {
  64. IBuilder builder = new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder());
  65. AbstractMessageWrapper message = (AbstractMessageWrapper) builder.WeakMergeFrom(TestUtil.GetAllSet().ToByteString()).WeakBuild();
  66. TestUtil.AssertAllFieldsSet((TestAllTypes) message.WrappedMessage);
  67. }
  68. [Test]
  69. public void OptimizedForSize() {
  70. // We're mostly only Checking that this class was compiled successfully.
  71. TestOptimizedForSize message = TestOptimizedForSize.CreateBuilder().SetI(1).Build();
  72. message = TestOptimizedForSize.ParseFrom(message.ToByteString());
  73. Assert.AreEqual(2, message.SerializedSize);
  74. }
  75. // -----------------------------------------------------------------
  76. // Tests for isInitialized().
  77. private static readonly TestRequired TestRequiredUninitialized = TestRequired.DefaultInstance;
  78. private static readonly TestRequired TestRequiredInitialized = TestRequired.CreateBuilder().SetA(1).SetB(2).SetC(3).Build();
  79. [Test]
  80. public void IsInitialized() {
  81. TestRequired.Builder builder = TestRequired.CreateBuilder();
  82. AbstractMessageWrapper.Builder abstractBuilder = new AbstractMessageWrapper.Builder(builder);
  83. Assert.IsFalse(abstractBuilder.IsInitialized);
  84. builder.A = 1;
  85. Assert.IsFalse(abstractBuilder.IsInitialized);
  86. builder.B = 1;
  87. Assert.IsFalse(abstractBuilder.IsInitialized);
  88. builder.C = 1;
  89. Assert.IsTrue(abstractBuilder.IsInitialized);
  90. }
  91. [Test]
  92. public void ForeignIsInitialized() {
  93. TestRequiredForeign.Builder builder = TestRequiredForeign.CreateBuilder();
  94. AbstractMessageWrapper.Builder abstractBuilder = new AbstractMessageWrapper.Builder(builder);
  95. Assert.IsTrue(abstractBuilder.IsInitialized);
  96. builder.SetOptionalMessage(TestRequiredUninitialized);
  97. Assert.IsFalse(abstractBuilder.IsInitialized);
  98. builder.SetOptionalMessage(TestRequiredInitialized);
  99. Assert.IsTrue(abstractBuilder.IsInitialized);
  100. builder.AddRepeatedMessage(TestRequiredUninitialized);
  101. Assert.IsFalse(abstractBuilder.IsInitialized);
  102. builder.SetRepeatedMessage(0, TestRequiredInitialized);
  103. Assert.IsTrue(abstractBuilder.IsInitialized);
  104. }
  105. // -----------------------------------------------------------------
  106. // Tests for mergeFrom
  107. static readonly TestAllTypes MergeSource = TestAllTypes.CreateBuilder()
  108. .SetOptionalInt32(1)
  109. .SetOptionalString("foo")
  110. .SetOptionalForeignMessage(ForeignMessage.DefaultInstance)
  111. .AddRepeatedString("bar")
  112. .Build();
  113. static readonly TestAllTypes MergeDest = TestAllTypes.CreateBuilder()
  114. .SetOptionalInt64(2)
  115. .SetOptionalString("baz")
  116. .SetOptionalForeignMessage(ForeignMessage.CreateBuilder().SetC(3).Build())
  117. .AddRepeatedString("qux")
  118. .Build();
  119. const string MergeResultText = "optional_int32: 1\n" +
  120. "optional_int64: 2\n" +
  121. "optional_string: \"foo\"\n" +
  122. "optional_foreign_message {\n" +
  123. " c: 3\n" +
  124. "}\n" +
  125. "repeated_string: \"qux\"\n" +
  126. "repeated_string: \"bar\"\n";
  127. [Test]
  128. public void MergeFrom() {
  129. AbstractMessageWrapper result = (AbstractMessageWrapper)
  130. new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder(MergeDest))
  131. .MergeFrom(MergeSource)
  132. .Build();
  133. Assert.AreEqual(MergeResultText, result.ToString());
  134. }
  135. // -----------------------------------------------------------------
  136. // Tests for equals and hashCode
  137. [Test]
  138. public void EqualsAndHashCode() {
  139. TestAllTypes a = TestUtil.GetAllSet();
  140. TestAllTypes b = TestAllTypes.CreateBuilder().Build();
  141. TestAllTypes c = TestAllTypes.CreateBuilder(b).AddRepeatedString("x").Build();
  142. TestAllTypes d = TestAllTypes.CreateBuilder(c).AddRepeatedString("y").Build();
  143. TestAllExtensions e = TestUtil.GetAllExtensionsSet();
  144. TestAllExtensions f = TestAllExtensions.CreateBuilder(e)
  145. .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 999).Build();
  146. CheckEqualsIsConsistent(a);
  147. CheckEqualsIsConsistent(b);
  148. CheckEqualsIsConsistent(c);
  149. CheckEqualsIsConsistent(d);
  150. CheckEqualsIsConsistent(e);
  151. CheckEqualsIsConsistent(f);
  152. CheckNotEqual(a, b);
  153. CheckNotEqual(a, c);
  154. CheckNotEqual(a, d);
  155. CheckNotEqual(a, e);
  156. CheckNotEqual(a, f);
  157. CheckNotEqual(b, c);
  158. CheckNotEqual(b, d);
  159. CheckNotEqual(b, e);
  160. CheckNotEqual(b, f);
  161. CheckNotEqual(c, d);
  162. CheckNotEqual(c, e);
  163. CheckNotEqual(c, f);
  164. CheckNotEqual(d, e);
  165. CheckNotEqual(d, f);
  166. CheckNotEqual(e, f);
  167. }
  168. /// <summary>
  169. /// Asserts that the given protos are equal and have the same hash code.
  170. /// </summary>
  171. private static void CheckEqualsIsConsistent(IMessage message) {
  172. // Object should be equal to itself.
  173. Assert.AreEqual(message, message);
  174. // Object should be equal to a dynamic copy of itself.
  175. DynamicMessage dynamic = DynamicMessage.CreateBuilder(message).Build();
  176. Assert.AreEqual(message, dynamic);
  177. Assert.AreEqual(dynamic, message);
  178. Assert.AreEqual(dynamic.GetHashCode(), message.GetHashCode());
  179. }
  180. /// <summary>
  181. /// Asserts that the given protos are not equal and have different hash codes.
  182. /// </summary>
  183. /// <remarks>
  184. /// It's valid for non-equal objects to have the same hash code, so
  185. /// this test is stricter than it needs to be. However, this should happen
  186. /// relatively rarely. (If this test fails, it's probably still due to a bug.)
  187. /// </remarks>
  188. private static void CheckNotEqual(IMessage m1, IMessage m2) {
  189. String equalsError = string.Format("{0} should not be equal to {1}", m1, m2);
  190. Assert.IsFalse(m1.Equals(m2), equalsError);
  191. Assert.IsFalse(m2.Equals(m1), equalsError);
  192. Assert.IsFalse(m1.GetHashCode() == m2.GetHashCode(),
  193. string.Format("{0} should have a different hash code from {1}", m1, m2));
  194. }
  195. /// <summary>
  196. /// Extends AbstractMessage and wraps some other message object. The methods
  197. /// of the Message interface which aren't explicitly implemented by
  198. /// AbstractMessage are forwarded to the wrapped object. This allows us to
  199. /// test that AbstractMessage's implementations work even if the wrapped
  200. /// object does not use them.
  201. /// </summary>
  202. private class AbstractMessageWrapper : AbstractMessage<AbstractMessageWrapper, AbstractMessageWrapper.Builder> {
  203. private readonly IMessage wrappedMessage;
  204. public IMessage WrappedMessage {
  205. get { return wrappedMessage; }
  206. }
  207. public AbstractMessageWrapper(IMessage wrappedMessage) {
  208. this.wrappedMessage = wrappedMessage;
  209. }
  210. public override MessageDescriptor DescriptorForType {
  211. get { return wrappedMessage.DescriptorForType; }
  212. }
  213. public override AbstractMessageWrapper DefaultInstanceForType {
  214. get { return new AbstractMessageWrapper(wrappedMessage.WeakDefaultInstanceForType); }
  215. }
  216. public override IDictionary<FieldDescriptor, object> AllFields {
  217. get { return wrappedMessage.AllFields; }
  218. }
  219. public override bool HasField(FieldDescriptor field) {
  220. return wrappedMessage.HasField(field);
  221. }
  222. public override object this[FieldDescriptor field] {
  223. get { return wrappedMessage[field]; }
  224. }
  225. public override object this[FieldDescriptor field, int index] {
  226. get { return wrappedMessage[field, index]; }
  227. }
  228. public override int GetRepeatedFieldCount(FieldDescriptor field) {
  229. return wrappedMessage.GetRepeatedFieldCount(field);
  230. }
  231. public override UnknownFieldSet UnknownFields {
  232. get { return wrappedMessage.UnknownFields; }
  233. }
  234. public override Builder CreateBuilderForType() {
  235. return new Builder(wrappedMessage.WeakCreateBuilderForType());
  236. }
  237. internal class Builder : AbstractBuilder<AbstractMessageWrapper, Builder> {
  238. private readonly IBuilder wrappedBuilder;
  239. protected override Builder ThisBuilder {
  240. get { return this; }
  241. }
  242. internal Builder(IBuilder wrappedBuilder) {
  243. this.wrappedBuilder = wrappedBuilder;
  244. }
  245. public override Builder MergeFrom(AbstractMessageWrapper other) {
  246. wrappedBuilder.WeakMergeFrom(other.wrappedMessage);
  247. return this;
  248. }
  249. public override bool IsInitialized {
  250. get { return wrappedBuilder.IsInitialized; }
  251. }
  252. public override IDictionary<FieldDescriptor, object> AllFields {
  253. get { return wrappedBuilder.AllFields; }
  254. }
  255. public override object this[FieldDescriptor field] {
  256. get { return wrappedBuilder[field]; }
  257. set { wrappedBuilder[field] = value; }
  258. }
  259. public override MessageDescriptor DescriptorForType {
  260. get { return wrappedBuilder.DescriptorForType; }
  261. }
  262. public override int GetRepeatedFieldCount(FieldDescriptor field) {
  263. return wrappedBuilder.GetRepeatedFieldCount(field);
  264. }
  265. public override object this[FieldDescriptor field, int index] {
  266. get { return wrappedBuilder[field, index]; }
  267. set { wrappedBuilder[field, index] = value; }
  268. }
  269. public override bool HasField(FieldDescriptor field) {
  270. return wrappedBuilder.HasField(field);
  271. }
  272. public override UnknownFieldSet UnknownFields {
  273. get { return wrappedBuilder.UnknownFields; }
  274. set { wrappedBuilder.UnknownFields = value; }
  275. }
  276. public override AbstractMessageWrapper Build() {
  277. return new AbstractMessageWrapper(wrappedBuilder.WeakBuild());
  278. }
  279. public override AbstractMessageWrapper BuildPartial() {
  280. return new AbstractMessageWrapper(wrappedBuilder.WeakBuildPartial());
  281. }
  282. public override Builder Clone() {
  283. return new Builder(wrappedBuilder.WeakClone());
  284. }
  285. public override AbstractMessageWrapper DefaultInstanceForType {
  286. get { return new AbstractMessageWrapper(wrappedBuilder.WeakDefaultInstanceForType); }
  287. }
  288. public override Builder ClearField(FieldDescriptor field) {
  289. wrappedBuilder.WeakClearField(field);
  290. return this;
  291. }
  292. public override Builder AddRepeatedField(FieldDescriptor field, object value) {
  293. wrappedBuilder.WeakAddRepeatedField(field, value);
  294. return this;
  295. }
  296. public override IBuilder CreateBuilderForField(FieldDescriptor field) {
  297. wrappedBuilder.CreateBuilderForField(field);
  298. return this;
  299. }
  300. public override Builder MergeFrom(IMessage other) {
  301. wrappedBuilder.WeakMergeFrom(other);
  302. return this;
  303. }
  304. public override Builder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry) {
  305. wrappedBuilder.WeakMergeFrom(input, extensionRegistry);
  306. return this;
  307. }
  308. }
  309. }
  310. }
  311. }