1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.remoteprotocol.util;
25
26 import java.io.UnsupportedEncodingException;
27 import java.nio.charset.CharacterCodingException;
28 import java.util.Arrays;
29 import java.util.Random;
30
31 import junit.framework.TestCase;
32
33
34
35
36
37
38
39
40
41 public class ByteUtilsTest extends TestCase {
42
43
44
45
46 private byte[] inputShortBytes = null;
47
48
49
50
51 private short[] inputShort = null;
52
53
54
55
56 private byte[] inputIntBytes = null;
57
58
59
60
61 private int[] inputInt = null;
62
63
64
65
66 private String[] inputStrings = null;
67
68
69
70
71 protected void setUp() throws Exception {
72
73
74
75
76
77
78
79 inputShortBytes = new byte[]{-128, 0,
80 -1, -1,
81 Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2),
82 Byte.parseByte("00000000", 2), Byte.parseByte("00001010", 2),
83 Byte.parseByte("01111111", 2), (byte)Short.parseShort("11111111", 2),
84 Byte.parseByte("00000011", 2), (byte)Short.parseShort("11101100", 2)};
85
86 inputShort = new short[] {-32768, -1, 0 , 10, 32767, 1004};
87
88
89
90
91
92
93
94
95 inputIntBytes = new byte[]{-128, 0, 0, 0,
96 -1, -1, -1, -1,
97 Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2),
98 Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00001010", 2),
99 Byte.parseByte("01111111", 2), (byte)Short.parseShort("11111111", 2), (byte)Short.parseShort("11111111", 2), (byte)Short.parseShort("11111111", 2),
100 Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00000011", 2), (byte)Short.parseShort("11101100", 2)};
101
102 inputInt = new int[] {-2147483648, -1, 0, 10, 2147483647, 1004};
103
104
105 inputStrings = new String[] {
106 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
107 "Donec non sem non velit hendrerit faucibus.",
108 "Phasellus convallis laoreet ligula.",
109 "Sed volutpat auctor turpis",
110 "Phasellus pharetra varius diam.",
111 "Fusce a erat vel lorem luctus rutrum.",
112 "Proin vitae dolor vel enim mattis lobortis.",
113 "Donec eu turpis at mi elementum accumsan."};
114 }
115
116
117
118
119 protected void tearDown() throws Exception {
120 super.tearDown();
121 }
122
123
124
125
126 public void testReadShortFromByteArray() {
127
128 int offSet = 0;
129 for (int i = 0; i < inputShort.length; i++) {
130 short testNum = inputShort[i];
131 assertEquals(testNum, ByteUtils.readShortFromByteArray(inputShortBytes, offSet));
132 offSet += 2;
133 }
134 }
135
136
137
138
139 public void testReadIntFromByteArray() {
140
141 int offset = 0;
142 for (int i = 0; i < inputInt.length; i++) {
143 int testNum = inputInt[i];
144 assertEquals(testNum, ByteUtils.readIntFromByteArray(inputIntBytes, offset));
145 offset += 4;
146 }
147 }
148
149
150
151
152 public void testGetByteArrayForShort() {
153
154 int offset = 0;
155 for (int i = 0; i < inputShort.length; i++) {
156 short testNum = inputShort[i];
157 assertTrue(Arrays.equals(ByteUtils.getByteArrayForShort(testNum), subArray(inputShortBytes, offset, 2)));
158 offset += 2;
159 }
160 }
161
162
163
164
165 public void testPutBytesInArrayForShort() {
166 int offset = 0;
167 Random random = new Random();
168 int bufferLength = 20;
169 for (int i = 0; i < inputShort.length; i++) {
170 short testNum = inputShort[i];
171
172 int pos = random.nextInt(bufferLength - 2);
173 byte[] outputBuf = new byte[bufferLength];
174 random.nextBytes(outputBuf);
175 byte[] testBuf = new byte[bufferLength];
176 System.arraycopy(outputBuf, 0, testBuf, 0, testBuf.length);
177 System.arraycopy(inputShortBytes, offset, testBuf, pos, 2);
178
179 ByteUtils.putBytesInArrayForShort(testNum, outputBuf, pos);
180 assertTrue(Arrays.equals(outputBuf, testBuf));
181 offset += 2;
182 }
183 }
184
185
186
187
188 public void testGetByteArrayForInt() {
189
190 int offset = 0;
191 for (int i = 0; i < inputInt.length; i++) {
192 int testNum = inputInt[i];
193 assertTrue(Arrays.equals(ByteUtils.getByteArrayForInt(testNum), subArray(inputIntBytes, offset, 4)));
194 offset += 4;
195 }
196 }
197
198
199
200
201 public void testPutBytesInArrayForInt() {
202 int offset = 0;
203 Random random = new Random();
204 int bufferLength = 20;
205 for (int i = 0; i < inputInt.length; i++) {
206 int testNum = inputInt[i];
207
208 int pos = random.nextInt(bufferLength - 4);
209 byte[] outputBuf = new byte[bufferLength];
210 random.nextBytes(outputBuf);
211 byte[] testBuf = new byte[bufferLength];
212 System.arraycopy(outputBuf, 0, testBuf, 0, testBuf.length);
213 System.arraycopy(inputIntBytes, offset, testBuf, pos, 4);
214
215 ByteUtils.putBytesInArrayForInt(testNum, outputBuf, pos);
216 assertTrue(Arrays.equals(outputBuf, testBuf));
217 offset += 4;
218 }
219 }
220
221
222
223
224 public void testGetByteArrayForString() {
225 for (int i = 0; i < inputStrings.length; i++) {
226 String inputString = inputStrings[i];
227 try {
228 assertTrue(Arrays.equals(ByteUtils.getByteArrayForString(inputString), inputString.getBytes("UTF-8")));
229 } catch (CharacterCodingException e) {
230 fail("Exception encoding characters");
231 } catch (UnsupportedEncodingException e) {
232 fail("Platform does not support encoding, test will fail");
233 }
234 }
235 }
236
237
238
239
240 public void testGetByteArrayForCharacterArray() {
241 for (int i = 0; i < inputStrings.length; i++) {
242 String inputString = inputStrings[i];
243 try {
244 assertTrue(Arrays.equals(ByteUtils.getByteArrayForCharacterArray(inputString.toCharArray()), inputString.getBytes("UTF-8")));
245 } catch (CharacterCodingException e) {
246 fail("Exception encoding characters");
247 } catch (UnsupportedEncodingException e) {
248 fail("Platform does not support encoding, test will fail");
249 }
250 }
251 }
252
253
254
255
256 public void testReadStringFromByteArray () {
257 for (int i = 0; i < inputStrings.length; i++) {
258 String inputString = inputStrings[i];
259 byte[] encodedString = null;
260 try {
261 encodedString = inputString.getBytes("UTF-8");
262 } catch (UnsupportedEncodingException e) {
263 fail("Platform does not support encoding, test will fail");
264 }
265 try {
266 assertEquals(inputString, ByteUtils.readStringFromByteArray(encodedString, 0, encodedString.length));
267 } catch (CharacterCodingException e) {
268 fail("Exception encoding characters");
269 }
270 }
271 }
272
273
274
275
276
277
278
279
280 private byte[] subArray (byte[] inputArray, int offset, int length) {
281 if (offset < 0) {
282 throw new ArrayIndexOutOfBoundsException("Offset cannot be less than 0");
283 }
284 if (offset > (inputArray.length + 1) || (offset + length) > (inputArray.length)) {
285 throw new ArrayIndexOutOfBoundsException("Range should be withing the input array");
286 }
287
288 byte[] returnArray = new byte[length];
289 System.arraycopy(inputArray, offset, returnArray, 0, length);
290
291 return returnArray;
292 }
293 }