Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: dart/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/index/file/FileNodeManagerTest.java

Issue 371913004: Version 1.5.6 (Closed) Base URL: http://dart.googlecode.com/svn/branches/1.5/
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express
11 * or implied. See the License for the specific language governing permissions a nd limitations under
12 * the License.
13 */
14 package com.google.dart.engine.internal.index.file;
15
16 import com.google.common.base.Objects;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.collect.Lists;
19 import com.google.dart.engine.context.AnalysisContext;
20 import com.google.dart.engine.element.Element;
21 import com.google.dart.engine.index.Location;
22 import com.google.dart.engine.index.Relationship;
23 import com.google.dart.engine.utilities.logging.Logger;
24
25 import junit.framework.TestCase;
26
27 import static org.fest.assertions.Assertions.assertThat;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.verifyZeroInteractions;
34 import static org.mockito.Mockito.when;
35
36 import java.io.ByteArrayInputStream;
37 import java.io.ByteArrayOutputStream;
38 import java.io.InputStream;
39 import java.util.List;
40 import java.util.Map;
41
42 public class FileNodeManagerTest extends TestCase {
43 private FileManager fileManager = mock(FileManager.class);
44 private Logger logger = mock(Logger.class);
45 private AnalysisContext context = mock(AnalysisContext.class);
46 private int contextId = 13;
47 private ContextCodec contextCodec = mock(ContextCodec.class);
48 private StringCodec stringCodec = new StringCodec();
49 private ElementCodec elementCodec = mock(ElementCodec.class);
50 private int nextElementId = 0;
51 private RelationshipCodec relationshipCodec = new RelationshipCodec(stringCode c);
52 private FileNodeManager nodeManager = new FileNodeManager(
53 fileManager,
54 logger,
55 stringCodec,
56 contextCodec,
57 elementCodec,
58 relationshipCodec);
59
60 public void test_clear() throws Exception {
61 nodeManager.clear();
62 verify(fileManager, times(1)).clear();
63 }
64
65 public void test_getContextCodec() throws Exception {
66 assertSame(contextCodec, nodeManager.getContextCodec());
67 }
68
69 public void test_getElementCodec() throws Exception {
70 assertSame(elementCodec, nodeManager.getElementCodec());
71 }
72
73 public void test_getLocationCount_empty() throws Exception {
74 assertEquals(0, nodeManager.getLocationCount());
75 }
76
77 public void test_getNode_contextNull() throws Exception {
78 String name = "42.index";
79 // prepare output stream
80 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
81 when(fileManager.openOutputStream(name)).thenReturn(outputStream);
82 // put Node
83 {
84 IndexNode node = new IndexNode(context, elementCodec, relationshipCodec);
85 nodeManager.putNode(name, node);
86 }
87 // force "null" context
88 when(contextCodec.decode(contextId)).thenReturn(null);
89 // prepare input stream
90 InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray( ));
91 when(fileManager.openInputStream(name)).thenReturn(inputStream);
92 // no Node
93 IndexNode node = nodeManager.getNode(name);
94 assertNull(node);
95 // no exceptions
96 verifyZeroInteractions(logger);
97 }
98
99 public void test_getNode_invalidVersion() throws Exception {
100 String name = "42.index";
101 // prepare a stream with an invalid version
102 when(fileManager.openInputStream(name)).thenReturn(
103 new ByteArrayInputStream(new byte[] {0x01, 0x02, 0x03, 0x04}));
104 // no Node
105 IndexNode node = nodeManager.getNode(name);
106 assertNull(node);
107 // failed
108 verify(logger).logError(anyString(), any(IllegalStateException.class));
109 }
110
111 public void test_getNode_streamException() throws Exception {
112 String name = "42.index";
113 when(fileManager.openInputStream(name)).thenThrow(new Exception());
114 // no Node
115 IndexNode node = nodeManager.getNode(name);
116 assertNull(node);
117 // failed
118 verify(logger).logError(anyString(), any(Throwable.class));
119 }
120
121 public void test_getNode_streamNull() throws Exception {
122 String name = "42.index";
123 when(fileManager.openInputStream(name)).thenReturn(null);
124 // no Node
125 IndexNode node = nodeManager.getNode(name);
126 assertNull(node);
127 }
128
129 public void test_getStringCodec() throws Exception {
130 assertSame(stringCodec, nodeManager.getStringCodec());
131 }
132
133 public void test_newNode() throws Exception {
134 IndexNode node = nodeManager.newNode(context);
135 assertSame(context, node.getContext());
136 assertEquals(0, node.getLocationCount());
137 }
138
139 public void test_putNode_getNode() throws Exception {
140 String name = "42.index";
141 // prepare output stream
142 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
143 when(fileManager.openOutputStream(name)).thenReturn(outputStream);
144 // prepare elements
145 Element elementA = mockElement();
146 Element elementB = mockElement();
147 Element elementC = mockElement();
148 Relationship relationship = Relationship.getRelationship("my-relationship");
149 // put Node
150 {
151 // prepare relations
152 int elementIdA = 0;
153 int elementIdB = 1;
154 int elementIdC = 2;
155 int relationshipId = relationshipCodec.encode(relationship);
156 RelationKeyData key = new RelationKeyData(elementIdA, relationshipId);
157 List<LocationData> locations = Lists.newArrayList(
158 new LocationData(elementIdB, 1, 10),
159 new LocationData(elementIdC, 2, 20));
160 Map<RelationKeyData, List<LocationData>> relations = ImmutableMap.of(key, locations);
161 // prepare Node
162 IndexNode node = new IndexNode(context, elementCodec, relationshipCodec);
163 node.setRelations(relations);
164 // put Node
165 nodeManager.putNode(name, node);
166 }
167 // has locations
168 assertEquals(2, nodeManager.getLocationCount());
169 // prepare input stream
170 InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray( ));
171 when(fileManager.openInputStream(name)).thenReturn(inputStream);
172 // get Node
173 IndexNode node = nodeManager.getNode(name);
174 assertEquals(2, node.getLocationCount());
175 {
176 Location[] locations = node.getRelationships(elementA, relationship);
177 assertThat(locations).hasSize(2);
178 assertHasLocation(locations, elementB, 1, 10);
179 assertHasLocation(locations, elementC, 2, 20);
180 }
181 }
182
183 public void test_putNode_streamException() throws Exception {
184 String name = "42.index";
185 when(fileManager.openOutputStream(name)).thenThrow(new Exception());
186 // try to put
187 IndexNode node = mock(IndexNode.class);
188 nodeManager.putNode(name, node);
189 // failed
190 verify(logger).logError(anyString(), any(Throwable.class));
191 }
192
193 public void test_removeNode() throws Exception {
194 String name = "42.index";
195 nodeManager.removeNode(name);
196 verify(fileManager).delete(name);
197 }
198
199 @Override
200 protected void setUp() throws Exception {
201 super.setUp();
202 when(contextCodec.encode(context)).thenReturn(contextId);
203 when(contextCodec.decode(contextId)).thenReturn(context);
204 }
205
206 private void assertHasLocation(Location[] locations, Element element, int offs et, int length) {
207 for (Location location : locations) {
208 if (Objects.equal(location.getElement(), element) && location.getOffset() == offset
209 && location.getLength() == length) {
210 return;
211 }
212 }
213 fail("Expected to find Location(element=" + element + ", offset=" + offset + ", length="
214 + length + ")");
215 }
216
217 private Element mockElement() {
218 int elementId = nextElementId++;
219 Element element = mock(Element.class);
220 when(elementCodec.encode(element)).thenReturn(elementId);
221 when(elementCodec.decode(context, elementId)).thenReturn(element);
222 return element;
223 }
224 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698