PageRenderTime 3651ms CodeModel.GetById 75ms RepoModel.GetById 12ms app.codeStats 0ms

/swift-codec/src/test/java/com/facebook/swift/codec/guice/TestThriftCodecModule.java

https://gitlab.com/mayakarya/swift
Java | 169 lines | 126 code | 25 blank | 18 comment | 7 complexity | 0a9f97e33020435a3c9f25b9bd0d2f26 MD5 | raw file
  1. /*
  2. * Copyright (C) 2012 Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package com.facebook.swift.codec.guice;
  17. import com.facebook.swift.codec.BonkConstructor;
  18. import com.facebook.swift.codec.ThriftCodec;
  19. import com.facebook.swift.codec.metadata.ThriftType;
  20. import com.google.common.base.Preconditions;
  21. import com.google.common.collect.ImmutableList;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.google.inject.Binder;
  24. import com.google.inject.Guice;
  25. import com.google.inject.Injector;
  26. import com.google.inject.Key;
  27. import com.google.inject.Module;
  28. import com.google.inject.Stage;
  29. import com.google.inject.TypeLiteral;
  30. import org.apache.thrift.protocol.TCompactProtocol;
  31. import org.apache.thrift.protocol.TProtocol;
  32. import org.apache.thrift.transport.TMemoryBuffer;
  33. import org.testng.annotations.Test;
  34. import java.util.List;
  35. import java.util.Map;
  36. import static com.facebook.swift.codec.guice.ThriftCodecBinder.thriftCodecBinder;
  37. import static org.testng.Assert.assertEquals;
  38. import static org.testng.Assert.assertNotNull;
  39. public class TestThriftCodecModule
  40. {
  41. @Test
  42. public void testThriftClientAndServerModules()
  43. throws Exception
  44. {
  45. Injector injector = Guice.createInjector(Stage.PRODUCTION,
  46. new ThriftCodecModule(),
  47. new Module()
  48. {
  49. @Override
  50. public void configure(Binder binder)
  51. {
  52. thriftCodecBinder(binder).bindThriftCodec(BonkConstructor.class);
  53. thriftCodecBinder(binder).bindListThriftCodec(BonkConstructor.class);
  54. thriftCodecBinder(binder).bindMapThriftCodec(String.class, BonkConstructor.class);
  55. thriftCodecBinder(binder).bindThriftCodec(new TypeLiteral<Map<Integer, List<String>>>() {});
  56. thriftCodecBinder(binder).bindCustomThriftCodec(new ThriftCodec<ValueClass>()
  57. {
  58. @Override
  59. public ThriftType getType()
  60. {
  61. return new ThriftType(ThriftType.STRING, ValueClass.class);
  62. }
  63. @Override
  64. public ValueClass read(TProtocol protocol)
  65. throws Exception
  66. {
  67. return new ValueClass(protocol.readString());
  68. }
  69. @Override
  70. public void write(ValueClass value, TProtocol protocol)
  71. throws Exception
  72. {
  73. protocol.writeString(value.getValue());
  74. }
  75. });
  76. }
  77. });
  78. testRoundTripSerialize(injector.getInstance(Key.get(new TypeLiteral<ThriftCodec<BonkConstructor>>() {})),
  79. new BonkConstructor("message", 42));
  80. testRoundTripSerialize(injector.getInstance(Key.get(new TypeLiteral<ThriftCodec<List<BonkConstructor>>>() {})),
  81. ImmutableList.of(new BonkConstructor("one", 1), new BonkConstructor("two", 2)));
  82. testRoundTripSerialize(injector.getInstance(Key.get(new TypeLiteral<ThriftCodec<Map<String, BonkConstructor>>>() {})),
  83. ImmutableMap.of("uno", new BonkConstructor("one", 1), "dos", new BonkConstructor("two", 2)));
  84. testRoundTripSerialize(injector.getInstance(Key.get(new TypeLiteral<ThriftCodec<Map<Integer, List<String>>>>() {})),
  85. ImmutableMap.<Integer, List<String>>of(1, ImmutableList.of("one", "uno"), 2, ImmutableList.of("two", "dos")));
  86. testRoundTripSerialize(injector.getInstance(Key.get(new TypeLiteral<ThriftCodec<ValueClass>>() {})),
  87. new ValueClass("my value"));
  88. }
  89. public static <T> void testRoundTripSerialize(ThriftCodec<T> codec, T value)
  90. throws Exception
  91. {
  92. // write value
  93. TMemoryBuffer transport = new TMemoryBuffer(10 * 1024);
  94. TCompactProtocol protocol = new TCompactProtocol(transport);
  95. codec.write(value, protocol);
  96. // read value back
  97. T copy = codec.read(protocol);
  98. assertNotNull(copy);
  99. // verify they are the same
  100. assertEquals(copy, value);
  101. }
  102. public static class ValueClass
  103. {
  104. private final String value;
  105. public ValueClass(String value)
  106. {
  107. Preconditions.checkNotNull(value, "value is null");
  108. this.value = value;
  109. }
  110. public String getValue()
  111. {
  112. return value;
  113. }
  114. @Override
  115. public boolean equals(Object o)
  116. {
  117. if (this == o) {
  118. return true;
  119. }
  120. if (o == null || getClass() != o.getClass()) {
  121. return false;
  122. }
  123. ValueClass that = (ValueClass) o;
  124. if (!value.equals(that.value)) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. @Override
  130. public int hashCode()
  131. {
  132. return value.hashCode();
  133. }
  134. @Override
  135. public String toString()
  136. {
  137. final StringBuilder sb = new StringBuilder();
  138. sb.append("ValueClass");
  139. sb.append("{value='").append(value).append('\'');
  140. sb.append('}');
  141. return sb.toString();
  142. }
  143. }
  144. }