MultiResponseTest.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.zookeeper;
  18. import junit.framework.TestCase;
  19. import org.apache.jute.BinaryInputArchive;
  20. import org.apache.jute.BinaryOutputArchive;
  21. import org.apache.zookeeper.data.Stat;
  22. import org.apache.zookeeper.server.ByteBufferInputStream;
  23. import org.junit.Test;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.IOException;
  26. import java.nio.ByteBuffer;
  27. public class MultiResponseTest extends TestCase {
  28. public void testRoundTrip() throws IOException {
  29. MultiResponse response = new MultiResponse();
  30. response.add(new OpResult.CheckResult());
  31. response.add(new OpResult.CreateResult("foo-bar"));
  32. response.add(new OpResult.DeleteResult());
  33. Stat s = new Stat();
  34. s.setCzxid(546);
  35. response.add(new OpResult.SetDataResult(s));
  36. MultiResponse decodedResponse = codeDecode(response);
  37. assertEquals(response, decodedResponse);
  38. assertEquals(response.hashCode(), decodedResponse.hashCode());
  39. }
  40. @Test
  41. public void testEmptyRoundTrip() throws IOException {
  42. MultiResponse result = new MultiResponse();
  43. MultiResponse decodedResult = codeDecode(result);
  44. assertEquals(result, decodedResult);
  45. assertEquals(result.hashCode(), decodedResult.hashCode());
  46. }
  47. private MultiResponse codeDecode(MultiResponse request) throws IOException {
  48. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  49. BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
  50. request.serialize(boa, "result");
  51. baos.close();
  52. ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
  53. bb.rewind();
  54. BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb));
  55. MultiResponse decodedRequest = new MultiResponse();
  56. decodedRequest.deserialize(bia, "result");
  57. return decodedRequest;
  58. }
  59. }