1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
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
14// distribution.
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.
18//
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.
30
31package com.google.protobuf;
32
33import junit.framework.TestCase;
34
35import java.util.Iterator;
36import java.util.ListIterator;
37
38/**
39 * Tests for {@link UnmodifiableLazyStringList}.
40 *
41 * @author jonp@google.com (Jon Perlow)
42 */
43public class UnmodifiableLazyStringListTest extends TestCase {
44
45  private static String STRING_A = "A";
46  private static String STRING_B = "B";
47  private static String STRING_C = "C";
48
49  private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
50  private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
51  private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
52
53  public void testReadOnlyMethods() {
54    LazyStringArrayList rawList = createSampleList();
55    UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
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));
60    assertEquals(BYTE_STRING_A, list.getByteString(0));
61    assertEquals(BYTE_STRING_B, list.getByteString(1));
62    assertEquals(BYTE_STRING_C, list.getByteString(2));
63  }
64
65  public void testModifyMethods() {
66    LazyStringArrayList rawList = createSampleList();
67    UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
68
69    try {
70      list.remove(0);
71      fail();
72    } catch (UnsupportedOperationException e) {
73      // expected
74    }
75    assertEquals(3, list.size());
76
77    try {
78      list.add(STRING_B);
79      fail();
80    } catch (UnsupportedOperationException e) {
81      // expected
82    }
83    assertEquals(3, list.size());
84
85    try {
86      list.set(1, STRING_B);
87      fail();
88    } catch (UnsupportedOperationException e) {
89      // expected
90    }
91  }
92
93  public void testIterator() {
94    LazyStringArrayList rawList = createSampleList();
95    UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
96
97    Iterator<String> iter = list.iterator();
98    int count = 0;
99    while (iter.hasNext()) {
100      iter.next();
101      count++;
102      try {
103        iter.remove();
104        fail();
105      } catch (UnsupportedOperationException e) {
106        // expected
107      }
108    }
109    assertEquals(3, count);
110
111  }
112
113  public void testListIterator() {
114    LazyStringArrayList rawList = createSampleList();
115    UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
116
117    ListIterator<String> iter = list.listIterator();
118    int count = 0;
119    while (iter.hasNext()) {
120      iter.next();
121      count++;
122      try {
123        iter.remove();
124        fail();
125      } catch (UnsupportedOperationException e) {
126        // expected
127      }
128      try {
129        iter.set("bar");
130        fail();
131      } catch (UnsupportedOperationException e) {
132        // expected
133      }
134      try {
135        iter.add("bar");
136        fail();
137      } catch (UnsupportedOperationException e) {
138        // expected
139      }
140    }
141    assertEquals(3, count);
142
143  }
144
145  private LazyStringArrayList createSampleList() {
146    LazyStringArrayList rawList = new LazyStringArrayList();
147    rawList.add(STRING_A);
148    rawList.add(STRING_B);
149    rawList.add(STRING_C);
150    return rawList;
151  }
152}
153