| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.apache.qpid.proton.engine.impl; |
| 22 |
|
| 23 |
import static org.junit.Assert.*; |
| 24 |
|
| 25 |
import org.apache.qpid.proton.amqp.Binary; |
| 26 |
import org.junit.Test; |
| 27 |
|
| 28 |
public class StringUtilsTest |
| 29 |
{ |
| 30 |
@Test |
| 31 |
public void testNullBinary() |
| 32 |
{ |
| 33 |
assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(null, 10, true)); |
| 34 |
} |
| 35 |
|
| 36 |
@Test |
| 37 |
public void testEmptyBinaryEmptyArray() |
| 38 |
{ |
| 39 |
Binary bin = new Binary(new byte[0]); |
| 40 |
assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(bin, 10, true)); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void testEmptyBinaryNonEmptyArray() |
| 45 |
{ |
| 46 |
byte[] bytes = new byte[] {(byte) 0, (byte) 0, (byte) 0}; |
| 47 |
Binary bin = new Binary(bytes, 0, 0); |
| 48 |
assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(bin, 10, true)); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void testEmptyBinaryNonEmptyArrayWithOffset() |
| 53 |
{ |
| 54 |
byte[] bytes = new byte[] {(byte) 0, (byte) 0, (byte) 0}; |
| 55 |
Binary bin = new Binary(bytes, 1, 0); |
| 56 |
assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(bin, 10, true)); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void testBinaryStringifiedSmallerThanGivenMaxLength() |
| 61 |
{ |
| 62 |
byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65, (byte) 152}; |
| 63 |
String expected = "\"\\x00\\x01\\x03A\\x98\""; |
| 64 |
Binary bin = new Binary(bytes); |
| 65 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, true)); |
| 66 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, false)); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void testBinaryStringifiedSmallerThanGivenMaxLengthWithOffset() |
| 71 |
{ |
| 72 |
byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65, (byte) 152}; |
| 73 |
String expected = "\"\\x01\\x03A\\x98\""; |
| 74 |
Binary bin = new Binary(bytes, 1, 4); |
| 75 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, true)); |
| 76 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 100, false)); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void testBinaryStringifiedEqualToGivenMaxLength() |
| 81 |
{ |
| 82 |
byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65}; |
| 83 |
String expected = "\"\\x00\\x01\\x03A\""; |
| 84 |
Binary bin = new Binary(bytes); |
| 85 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, true)); |
| 86 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, false)); |
| 87 |
|
| 88 |
bytes = new byte[] {(byte) 0, (byte) 1, (byte) 65, (byte) 3}; |
| 89 |
expected = "\"\\x00\\x01A\\x03\""; |
| 90 |
bin = new Binary(bytes); |
| 91 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, true)); |
| 92 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 15, false)); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void testBinaryStringifiedEqualToGivenMaxLengthWithOffset() |
| 97 |
{ |
| 98 |
byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3, (byte) 65}; |
| 99 |
String expected = "\"\\x01\\x03A\""; |
| 100 |
Binary bin = new Binary(bytes, 1, 3); |
| 101 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, true)); |
| 102 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, false)); |
| 103 |
|
| 104 |
bytes = new byte[] {(byte) 0, (byte) 1, (byte) 65, (byte) 3}; |
| 105 |
expected = "\"\\x01A\\x03\""; |
| 106 |
bin = new Binary(bytes, 1, 3); |
| 107 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, true)); |
| 108 |
assertEquals("unexpected result", expected, StringUtils.toQuotedString(bin, 11, false)); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void testBinaryStringifiedLargerThanGivenMaxLength() |
| 113 |
{ |
| 114 |
byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3}; |
| 115 |
String expected1 = "\"\\x00\\x01\""; |
| 116 |
Binary bin = new Binary(bytes); |
| 117 |
|
| 118 |
assertEquals("unexpected result", expected1, StringUtils.toQuotedString(bin, 10, false)); |
| 119 |
|
| 120 |
String expected2 = "\"\\x00\\x01\"...(truncated)"; |
| 121 |
assertEquals("unexpected result", expected2, StringUtils.toQuotedString(bin, 10, true)); |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void testBinaryStringifiedLargerThanGivenMaxLengthWithOffset() |
| 126 |
{ |
| 127 |
byte[] bytes = new byte[] {(byte) 0, (byte) 1, (byte) 3}; |
| 128 |
String expected1 = "\"\\x00\\x01\""; |
| 129 |
Binary bin = new Binary(bytes); |
| 130 |
|
| 131 |
assertEquals("unexpected result", expected1, StringUtils.toQuotedString(bin, 10, false)); |
| 132 |
|
| 133 |
String expected2 = "\"\\x00\\x01\"...(truncated)"; |
| 134 |
assertEquals("unexpected result", expected2, StringUtils.toQuotedString(bin, 10, true)); |
| 135 |
} |
| 136 |
} |