]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/test/org/apache/thrift/TestUnsafeBinaries.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / test / org / apache / thrift / TestUnsafeBinaries.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.thrift;
21
22 import java.nio.ByteBuffer;
23 import java.util.Arrays;
24
25 import thrift.test.SafeBytes;
26 import thrift.test.UnsafeBytes;
27
28 // test generating types with un-copied byte[]/ByteBuffer input/output
29 //
30 public class TestUnsafeBinaries extends TestStruct {
31
32 private static byte[] input() {
33 return new byte[]{1, 1};
34 }
35
36 //
37 // verify that the unsafe_binaries option modifies behavior
38 //
39
40 // constructor doesn't copy
41 public void testUnsafeConstructor() throws Exception {
42
43 byte[] input = input();
44 UnsafeBytes struct = new UnsafeBytes(ByteBuffer.wrap(input));
45
46 input[0] = 2;
47
48 assertTrue(Arrays.equals(
49 new byte[]{2, 1},
50 struct.getBytes())
51 );
52
53 }
54
55 // getter doesn't copy
56 // note: this behavior is the same with/without the flag, but if this default ever changes, the current behavior
57 // should be retained when using this flag
58 public void testUnsafeGetter(){
59 UnsafeBytes struct = new UnsafeBytes(ByteBuffer.wrap(input()));
60
61 byte[] val = struct.getBytes();
62 val[0] = 2;
63
64 assertTrue(Arrays.equals(
65 new byte[]{2, 1},
66 struct.getBytes())
67 );
68
69 }
70
71 // setter doesn't copy
72 public void testUnsafeSetter(){
73 UnsafeBytes struct = new UnsafeBytes();
74
75 byte[] val = input();
76 struct.setBytes(val);
77
78 val[0] = 2;
79
80 assertTrue(Arrays.equals(
81 new byte[]{2, 1},
82 struct.getBytes())
83 );
84
85 }
86
87 // buffer doens't copy
88 public void testUnsafeBufferFor(){
89 UnsafeBytes struct = new UnsafeBytes(ByteBuffer.wrap(input()));
90
91 ByteBuffer val = struct.bufferForBytes();
92 val.array()[0] = 2;
93
94 assertTrue(Arrays.equals(
95 new byte[]{2, 1},
96 struct.getBytes())
97 );
98
99 }
100
101 //
102 // verify that the default generator does not change behavior
103 //
104
105 public void testSafeConstructor() {
106
107 byte[] input = input();
108 SafeBytes struct = new SafeBytes(ByteBuffer.wrap(input));
109
110 input[0] = 2;
111
112 assertTrue(Arrays.equals(
113 new byte[]{1, 1},
114 struct.getBytes())
115 );
116
117 }
118
119 public void testSafeSetter() {
120
121 byte[] input = input();
122 SafeBytes struct = new SafeBytes(ByteBuffer.wrap(input));
123
124 input[0] = 2;
125
126 assertTrue(Arrays.equals(
127 new byte[]{1, 1},
128 struct.getBytes())
129 );
130
131 }
132
133 public void testSafeBufferFor(){
134 SafeBytes struct = new SafeBytes(ByteBuffer.wrap(input()));
135
136 ByteBuffer val = struct.bufferForBytes();
137 val.array()[0] = 2;
138
139 assertTrue(Arrays.equals(
140 new byte[]{1, 1},
141 struct.getBytes())
142 );
143
144 }
145
146 }