cb75d74b0c6b34f068653aa85cbd49ea709dca39
[tools/dynpart-tools.git] /
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
31 package com.google.protobuf.test;
32 import com.google.protobuf.*;
33
34 import junit.framework.TestCase;
35
36 import java.util.Iterator;
37 import java.util.ListIterator;
38
39 /**
40  * Tests for {@link UnmodifiableLazyStringList}.
41  *
42  * @author jonp@google.com (Jon Perlow)
43  */
44 public class UnmodifiableLazyStringListTest extends TestCase {
45
46   private static String STRING_A = "A";
47   private static String STRING_B = "B";
48   private static String STRING_C = "C";
49
50   private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
51   private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
52   private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
53
54   public void testReadOnlyMethods() {
55     LazyStringArrayList rawList = createSampleList();
56     UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
57     assertEquals(3, list.size());
58     assertSame(STRING_A, list.get(0));
59     assertSame(STRING_B, list.get(1));
60     assertSame(STRING_C, list.get(2));
61     assertEquals(BYTE_STRING_A, list.getByteString(0));
62     assertEquals(BYTE_STRING_B, list.getByteString(1));
63     assertEquals(BYTE_STRING_C, list.getByteString(2));
64   }
65
66   public void testModifyMethods() {
67     LazyStringArrayList rawList = createSampleList();
68     UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
69
70     try {
71       list.remove(0);
72       fail();
73     } catch (UnsupportedOperationException e) {
74       // expected
75     }
76     assertEquals(3, list.size());
77
78     try {
79       list.add(STRING_B);
80       fail();
81     } catch (UnsupportedOperationException e) {
82       // expected
83     }
84     assertEquals(3, list.size());
85
86     try {
87       list.set(1, STRING_B);
88       fail();
89     } catch (UnsupportedOperationException e) {
90       // expected
91     }
92   }
93
94   public void testIterator() {
95     LazyStringArrayList rawList = createSampleList();
96     UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
97
98     Iterator<String> iter = list.iterator();
99     int count = 0;
100     while (iter.hasNext()) {
101       iter.next();
102       count++;
103       try {
104         iter.remove();
105         fail();
106       } catch (UnsupportedOperationException e) {
107         // expected
108       }
109     }
110     assertEquals(3, count);
111
112   }
113
114   public void testListIterator() {
115     LazyStringArrayList rawList = createSampleList();
116     UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
117
118     ListIterator<String> iter = list.listIterator();
119     int count = 0;
120     while (iter.hasNext()) {
121       iter.next();
122       count++;
123       try {
124         iter.remove();
125         fail();
126       } catch (UnsupportedOperationException e) {
127         // expected
128       }
129       try {
130         iter.set("bar");
131         fail();
132       } catch (UnsupportedOperationException e) {
133         // expected
134       }
135       try {
136         iter.add("bar");
137         fail();
138       } catch (UnsupportedOperationException e) {
139         // expected
140       }
141     }
142     assertEquals(3, count);
143
144   }
145
146   private LazyStringArrayList createSampleList() {
147     LazyStringArrayList rawList = new LazyStringArrayList();
148     rawList.add(STRING_A);
149     rawList.add(STRING_B);
150     rawList.add(STRING_C);
151     return rawList;
152   }
153 }