1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
9
// * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
15
// * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
package com.google.protobuf;
33
import junit.framework.TestCase;
36
* Tests for {@link LazyStringArrayList}.
38
* @author jonp@google.com (Jon Perlow)
40
public class LazyStringArrayListTest extends TestCase {
42
private static String STRING_A = "A";
43
private static String STRING_B = "B";
44
private static String STRING_C = "C";
46
private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
47
private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
48
private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
50
public void testJustStrings() {
51
LazyStringArrayList list = new LazyStringArrayList();
56
assertEquals(3, list.size());
57
assertSame(STRING_A, list.get(0));
58
assertSame(STRING_B, list.get(1));
59
assertSame(STRING_C, list.get(2));
61
list.set(1, STRING_C);
62
assertSame(STRING_C, list.get(1));
65
assertSame(STRING_A, list.get(0));
66
assertSame(STRING_C, list.get(1));
69
public void testJustByteString() {
70
LazyStringArrayList list = new LazyStringArrayList();
71
list.add(BYTE_STRING_A);
72
list.add(BYTE_STRING_B);
73
list.add(BYTE_STRING_C);
75
assertEquals(3, list.size());
76
assertSame(BYTE_STRING_A, list.getByteString(0));
77
assertSame(BYTE_STRING_B, list.getByteString(1));
78
assertSame(BYTE_STRING_C, list.getByteString(2));
81
assertSame(BYTE_STRING_A, list.getByteString(0));
82
assertSame(BYTE_STRING_C, list.getByteString(1));
85
public void testConversionBackAndForth() {
86
LazyStringArrayList list = new LazyStringArrayList();
88
list.add(BYTE_STRING_B);
89
list.add(BYTE_STRING_C);
91
// String a should be the same because it was originally a string
92
assertSame(STRING_A, list.get(0));
94
// String b and c should be different because the string has to be computed
95
// from the ByteString
96
String bPrime = list.get(1);
97
assertNotSame(STRING_B, bPrime);
98
assertEquals(STRING_B, bPrime);
99
String cPrime = list.get(2);
100
assertNotSame(STRING_C, cPrime);
101
assertEquals(STRING_C, cPrime);
103
// String c and c should stay the same once cached.
104
assertSame(bPrime, list.get(1));
105
assertSame(cPrime, list.get(2));
107
// ByteString needs to be computed from string for both a and b
108
ByteString aPrimeByteString = list.getByteString(0);
109
assertEquals(BYTE_STRING_A, aPrimeByteString);
110
ByteString bPrimeByteString = list.getByteString(1);
111
assertNotSame(BYTE_STRING_B, bPrimeByteString);
112
assertEquals(BYTE_STRING_B, list.getByteString(1));
114
// Once cached, ByteString should stay cached.
115
assertSame(aPrimeByteString, list.getByteString(0));
116
assertSame(bPrimeByteString, list.getByteString(1));