OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012, the Dart project authors. | 2 * Copyright (c) 2012, the Dart project authors. |
3 * | 3 * |
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except | 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 | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License | 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 | 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 | 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 package com.google.dart.tools.core.analysis; | 14 package com.google.dart.tools.core.analysis; |
15 | 15 |
| 16 import com.google.dart.engine.utilities.io.PrintStringWriter; |
| 17 import com.google.dart.tools.core.test.util.FileUtilities; |
| 18 |
16 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.assertCa
chedLibraries; | 19 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.assertCa
chedLibraries; |
17 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.assertPa
ckageContexts; | 20 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.assertPa
ckageContexts; |
18 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.assertTr
ackedLibraryFiles; | 21 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.assertTr
ackedLibraryFiles; |
19 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.getCache
dLibrary; | 22 import static com.google.dart.tools.core.analysis.AnalysisTestUtilities.getCache
dLibrary; |
20 | 23 |
21 import com.google.dart.engine.utilities.io.PrintStringWriter; | |
22 import com.google.dart.tools.core.test.util.FileUtilities; | |
23 | |
24 public class FileChangedTaskTest extends AbstractDartAnalysisTest { | 24 public class FileChangedTaskTest extends AbstractDartAnalysisTest { |
25 | 25 |
26 /** | 26 /** |
27 * Called once prior to executing the first test in this class | 27 * Called once prior to executing the first test in this class |
28 */ | 28 */ |
29 public static void setUpOnce() throws Exception { | 29 public static void setUpOnce() throws Exception { |
30 setUpBankExample(); | 30 setUpBankExample(); |
31 } | 31 } |
32 | 32 |
33 /** | 33 /** |
34 * Called once after executing the last test in this class | 34 * Called once after executing the last test in this class |
35 */ | 35 */ |
36 public static void tearDownOnce() { | 36 public static void tearDownOnce() { |
37 tearDownBankExample(); | 37 tearDownBankExample(); |
38 } | 38 } |
39 | 39 |
40 private AnalysisServerAdapter server; | 40 private AnalysisServerAdapter server; |
41 private SavedContext savedContext; | 41 private SavedContext savedContext; |
42 private Listener listener; | 42 private Listener listener; |
43 | 43 |
44 /** | 44 /** |
45 * Assert removing #source directive causes sourced file to become analyzed as
library | |
46 * <p> | |
47 * TODO(scheglov) disabled because now 'part of' directive required | |
48 */ | |
49 public void _test_changed_library() throws Exception { | |
50 final String directive = "#source(\"simple_money.dart\");"; | |
51 final String oldContent = FileUtilities.getContents(moneyLibFile); | |
52 int index = oldContent.indexOf(directive); | |
53 assertTrue(index > 0); | |
54 final String newContent = oldContent.substring(0, index) | |
55 + oldContent.substring(index + directive.length()); | |
56 | |
57 server.scan(moneyLibFile, null); | |
58 server.start(); | |
59 listener.waitForIdle(1, FIVE_MINUTES_MS); | |
60 assertTrackedLibraryFiles(server, moneyLibFile); | |
61 Object lib1 = getCachedLibrary(savedContext, moneyLibFile); | |
62 assertNotNull(lib1); | |
63 assertNull(getCachedLibrary(savedContext, simpleMoneySrcFile)); | |
64 | |
65 server.resetAnalyzeContext(); | |
66 final long oldLastModified = moneyLibFile.lastModified(); | |
67 FileUtilities.setContents(moneyLibFile, newContent); | |
68 // Ensure marked as modified... lastModified is only accurate to the second | |
69 moneyLibFile.setLastModified(oldLastModified + 1000); | |
70 try { | |
71 server.changed(moneyLibFile); | |
72 listener.waitForIdle(2, FIVE_MINUTES_MS); | |
73 assertTrackedLibraryFiles(server, moneyLibFile, simpleMoneySrcFile); | |
74 server.assertAnalyzeContext(true); | |
75 Object lib2 = getCachedLibrary(savedContext, moneyLibFile); | |
76 assertNotNull(lib2); | |
77 assertNotSame(lib1, lib2); | |
78 lib1 = lib2; | |
79 assertNotNull(getCachedLibrary(savedContext, simpleMoneySrcFile)); | |
80 } finally { | |
81 FileUtilities.setContents(moneyLibFile, oldContent); | |
82 moneyLibFile.setLastModified(oldLastModified); | |
83 } | |
84 | |
85 server.resetAnalyzeContext(); | |
86 server.changed(moneyLibFile); | |
87 listener.waitForIdle(3, FIVE_MINUTES_MS); | |
88 assertTrackedLibraryFiles(server, moneyLibFile); | |
89 server.assertAnalyzeContext(true); | |
90 Object lib2 = getCachedLibrary(savedContext, moneyLibFile); | |
91 assertNotNull(lib2); | |
92 assertNotSame(lib1, lib2); | |
93 assertNull(getCachedLibrary(savedContext, simpleMoneySrcFile)); | |
94 } | |
95 | |
96 /** | |
97 * Assert cache discarded only if file has changed on disk | 45 * Assert cache discarded only if file has changed on disk |
98 */ | 46 */ |
99 public void test_changed() { | 47 public void test_changed() { |
100 server.start(); | 48 server.start(); |
101 ParseResult parse1 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); | 49 ParseResult parse1 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); |
102 assertNotNull(parse1.getDartUnit()); | 50 assertNotNull(parse1.getDartUnit()); |
103 ParseResult parse2 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); | 51 ParseResult parse2 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); |
104 assertSame(parse1.getDartUnit(), parse2.getDartUnit()); | 52 assertSame(parse1.getDartUnit(), parse2.getDartUnit()); |
105 | 53 |
106 listener.reset(); | 54 listener.reset(); |
107 server.changed(moneyLibFile); | 55 server.changed(moneyLibFile); |
108 ParseResult parse3 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); | 56 ParseResult parse3 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); |
109 assertSame(parse1.getDartUnit(), parse3.getDartUnit()); | 57 assertSame(parse1.getDartUnit(), parse3.getDartUnit()); |
110 server.assertAnalyzeContext(false); | 58 server.assertAnalyzeContext(false); |
111 listener.assertDiscarded(); | 59 listener.assertDiscarded(); |
112 | 60 |
113 listener.reset(); | 61 listener.reset(); |
114 moneyLibFile.setLastModified(System.currentTimeMillis() + 1000); | 62 moneyLibFile.setLastModified(System.currentTimeMillis() + 1000); |
115 server.changed(moneyLibFile); | 63 server.changed(moneyLibFile); |
116 ParseResult parse4 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); | 64 ParseResult parse4 = savedContext.parse(moneyLibFile, moneyLibFile, FIVE_MIN
UTES_MS); |
117 assertNotSame(parse1.getDartUnit(), parse4.getDartUnit()); | 65 assertNotSame(parse1.getDartUnit(), parse4.getDartUnit()); |
118 server.assertAnalyzeContext(true); | 66 server.assertAnalyzeContext(true); |
119 listener.assertDiscarded(moneyLibFile); | 67 listener.assertDiscarded(moneyLibFile); |
120 } | 68 } |
121 | 69 |
122 /** | 70 /** |
| 71 * Assert adding and removing import in application does not change context of
imported nested |
| 72 * library because that library is in the application directory hierarchy. |
| 73 */ |
| 74 public void test_changed_application_importNested() throws Exception { |
| 75 final String directive = "#import('nested/nestedLib.dart');"; |
| 76 final String oldContent = FileUtilities.getContents(bankLibFile); |
| 77 int index = oldContent.indexOf(directive); |
| 78 assertTrue(index > 0); |
| 79 final String newContent = oldContent.substring(0, index) |
| 80 + oldContent.substring(index + directive.length()); |
| 81 |
| 82 server.scan(bankDir, null); |
| 83 server.start(); |
| 84 listener.waitForIdle(1, FIVE_MINUTES_MS); |
| 85 assertPackageContexts(server, bankDir); |
| 86 assertCachedLibraries(server, null); |
| 87 assertCachedLibraries(server, bankDir, bankLibFile, nestedLibFile, nestedApp
File); |
| 88 ParseResult parse1 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MINUT
ES_MS); |
| 89 assertNotNull(parse1.getDartUnit()); |
| 90 listener.waitForIdle(2, FIVE_MINUTES_MS); |
| 91 |
| 92 final long oldLastModified = bankLibFile.lastModified(); |
| 93 FileUtilities.setContents(bankLibFile, newContent); |
| 94 // Ensure marked as modified... lastModified is only accurate to the second |
| 95 bankLibFile.setLastModified(oldLastModified + 1000); |
| 96 ParseResult parse2; |
| 97 try { |
| 98 server.changed(bankLibFile); |
| 99 listener.waitForIdle(3, FIVE_MINUTES_MS); |
| 100 |
| 101 assertPackageContexts(server, bankDir); |
| 102 assertCachedLibraries(server, null); |
| 103 assertCachedLibraries(server, bankDir, bankLibFile, nestedLibFile, nestedA
ppFile); |
| 104 parse2 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MINUTES_MS); |
| 105 listener.waitForIdle(4, FIVE_MINUTES_MS); |
| 106 |
| 107 assertNotNull(parse2.getDartUnit()); |
| 108 assertNotSame(parse2.getDartUnit(), parse1.getDartUnit()); |
| 109 } finally { |
| 110 FileUtilities.setContents(bankLibFile, oldContent); |
| 111 bankLibFile.setLastModified(oldLastModified); |
| 112 } |
| 113 server.changed(bankLibFile); |
| 114 listener.waitForIdle(5, FIVE_MINUTES_MS); |
| 115 assertPackageContexts(server, bankDir); |
| 116 assertCachedLibraries(server, null); |
| 117 assertCachedLibraries(server, bankDir, bankLibFile, nestedLibFile, nestedApp
File); |
| 118 ParseResult parse3 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MINUT
ES_MS); |
| 119 assertNotNull(parse3.getDartUnit()); |
| 120 assertNotSame(parse3.getDartUnit(), parse1.getDartUnit()); |
| 121 assertNotSame(parse3.getDartUnit(), parse2.getDartUnit()); |
| 122 } |
| 123 |
| 124 /** |
123 * Assert adding and removing import in application changes context of importe
d outside library | 125 * Assert adding and removing import in application changes context of importe
d outside library |
124 * because that library is outside the application directory hierarchy. | 126 * because that library is outside the application directory hierarchy. |
125 */ | 127 */ |
126 // TODO (danrubel): Add support for this in subsequent CL | 128 // TODO (danrubel): Add support for this in subsequent CL |
127 // public void test_changed_application_importOutside() throws Exception { | 129 // public void test_changed_application_importOutside() throws Exception { |
128 // final String directive = "#import('../Money/money.dart');"; | 130 // final String directive = "#import('../Money/money.dart');"; |
129 // final String oldContent = FileUtilities.getContents(bankLibFile); | 131 // final String oldContent = FileUtilities.getContents(bankLibFile); |
130 // int index = oldContent.indexOf(directive); | 132 // int index = oldContent.indexOf(directive); |
131 // assertTrue(index > 0); | 133 // assertTrue(index > 0); |
132 // final String newContent = oldContent.substring(0, index) | 134 // final String newContent = oldContent.substring(0, index) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // assertPackageContexts(server, bankDir); | 167 // assertPackageContexts(server, bankDir); |
166 // assertCachedLibraries(server, null); | 168 // assertCachedLibraries(server, null); |
167 // assertCachedLibraries(server, bankDir, nestedLibFile, nestedAppFile); | 169 // assertCachedLibraries(server, bankDir, nestedLibFile, nestedAppFile); |
168 // ParseResult parse3 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MIN
UTES_MS); | 170 // ParseResult parse3 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MIN
UTES_MS); |
169 // assertNotNull(parse3.getDartUnit()); | 171 // assertNotNull(parse3.getDartUnit()); |
170 // assertNotSame(parse3.getDartUnit(), parse1.getDartUnit()); | 172 // assertNotSame(parse3.getDartUnit(), parse1.getDartUnit()); |
171 // assertNotSame(parse3.getDartUnit(), parse2.getDartUnit()); | 173 // assertNotSame(parse3.getDartUnit(), parse2.getDartUnit()); |
172 // } | 174 // } |
173 | 175 |
174 /** | 176 /** |
175 * Assert adding and removing import in application does not change context of
imported nested | 177 * Assert removing #source directive causes sourced file to become analyzed as
library |
176 * library because that library is in the application directory hierarchy. | |
177 */ | 178 */ |
178 public void test_changed_application_importNested() throws Exception { | 179 public void test_changed_library() throws Exception { |
179 final String directive = "#import('nested/nestedLib.dart');"; | 180 final String directive = "#source(\"simple_money.dart\");"; |
180 final String oldContent = FileUtilities.getContents(bankLibFile); | 181 final String oldContent = FileUtilities.getContents(moneyLibFile); |
181 int index = oldContent.indexOf(directive); | 182 int index = oldContent.indexOf(directive); |
182 assertTrue(index > 0); | 183 assertTrue(index > 0); |
183 final String newContent = oldContent.substring(0, index) | 184 final String newContent = oldContent.substring(0, index) |
184 + oldContent.substring(index + directive.length()); | 185 + oldContent.substring(index + directive.length()); |
185 | 186 |
186 server.scan(bankDir, null); | 187 server.scan(moneyLibFile, null); |
187 server.start(); | 188 server.start(); |
188 listener.waitForIdle(1, FIVE_MINUTES_MS); | 189 listener.waitForIdle(1, FIVE_MINUTES_MS); |
189 assertPackageContexts(server, bankDir); | 190 assertTrackedLibraryFiles(server, moneyLibFile); |
190 assertCachedLibraries(server, null); | 191 Object lib1 = getCachedLibrary(savedContext, moneyLibFile); |
191 assertCachedLibraries(server, bankDir, bankLibFile, nestedLibFile, nestedApp
File); | 192 assertNotNull(lib1); |
192 ParseResult parse1 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MINUT
ES_MS); | 193 assertNull(getCachedLibrary(savedContext, simpleMoneySrcFile)); |
193 assertNotNull(parse1.getDartUnit()); | |
194 listener.waitForIdle(2, FIVE_MINUTES_MS); | |
195 | 194 |
196 final long oldLastModified = bankLibFile.lastModified(); | 195 server.resetAnalyzeContext(); |
197 FileUtilities.setContents(bankLibFile, newContent); | 196 final long oldLastModified = moneyLibFile.lastModified(); |
| 197 FileUtilities.setContents(moneyLibFile, newContent); |
198 // Ensure marked as modified... lastModified is only accurate to the second | 198 // Ensure marked as modified... lastModified is only accurate to the second |
199 bankLibFile.setLastModified(oldLastModified + 1000); | 199 moneyLibFile.setLastModified(oldLastModified + 1000); |
200 ParseResult parse2; | |
201 try { | 200 try { |
202 server.changed(bankLibFile); | 201 server.changed(moneyLibFile); |
203 listener.waitForIdle(3, FIVE_MINUTES_MS); | 202 listener.waitForIdle(2, FIVE_MINUTES_MS); |
| 203 assertTrackedLibraryFiles(server, moneyLibFile, simpleMoneySrcFile); |
| 204 server.assertAnalyzeContext(true); |
| 205 Object lib2 = getCachedLibrary(savedContext, moneyLibFile); |
| 206 assertNotNull(lib2); |
| 207 assertNotSame(lib1, lib2); |
| 208 lib1 = lib2; |
| 209 assertNotNull(getCachedLibrary(savedContext, simpleMoneySrcFile)); |
| 210 } finally { |
| 211 FileUtilities.setContents(moneyLibFile, oldContent); |
| 212 moneyLibFile.setLastModified(oldLastModified); |
| 213 } |
204 | 214 |
205 assertPackageContexts(server, bankDir); | 215 server.resetAnalyzeContext(); |
206 assertCachedLibraries(server, null); | 216 server.changed(moneyLibFile); |
207 assertCachedLibraries(server, bankDir, bankLibFile, nestedLibFile, nestedA
ppFile); | 217 listener.waitForIdle(3, FIVE_MINUTES_MS); |
208 parse2 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MINUTES_MS); | 218 assertTrackedLibraryFiles(server, moneyLibFile); |
209 listener.waitForIdle(4, FIVE_MINUTES_MS); | 219 server.assertAnalyzeContext(true); |
210 | 220 Object lib2 = getCachedLibrary(savedContext, moneyLibFile); |
211 assertNotNull(parse2.getDartUnit()); | 221 assertNotNull(lib2); |
212 assertNotSame(parse2.getDartUnit(), parse1.getDartUnit()); | 222 assertNotSame(lib1, lib2); |
213 } finally { | 223 assertNull(getCachedLibrary(savedContext, simpleMoneySrcFile)); |
214 FileUtilities.setContents(bankLibFile, oldContent); | |
215 bankLibFile.setLastModified(oldLastModified); | |
216 } | |
217 server.changed(bankLibFile); | |
218 listener.waitForIdle(5, FIVE_MINUTES_MS); | |
219 assertPackageContexts(server, bankDir); | |
220 assertCachedLibraries(server, null); | |
221 assertCachedLibraries(server, bankDir, bankLibFile, nestedLibFile, nestedApp
File); | |
222 ParseResult parse3 = savedContext.parse(bankLibFile, bankLibFile, FIVE_MINUT
ES_MS); | |
223 assertNotNull(parse3.getDartUnit()); | |
224 assertNotSame(parse3.getDartUnit(), parse1.getDartUnit()); | |
225 assertNotSame(parse3.getDartUnit(), parse2.getDartUnit()); | |
226 } | 224 } |
227 | 225 |
228 /** | 226 /** |
229 * Assert adding #library directive causes sourced file to become analyzed as
library | 227 * Assert adding #library directive causes sourced file to become analyzed as
library |
230 */ | 228 */ |
231 public void test_changed_source() throws Exception { | 229 public void test_changed_source() throws Exception { |
232 final String oldContent = FileUtilities.getContents(simpleMoneySrcFile); | 230 final String oldContent = FileUtilities.getContents(simpleMoneySrcFile); |
233 PrintStringWriter writer = new PrintStringWriter(); | 231 PrintStringWriter writer = new PrintStringWriter(); |
234 writer.println("#library(\"foobar\");"); | 232 writer.println("#library(\"foobar\");"); |
235 writer.append(oldContent); | 233 writer.append(oldContent); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 server = new AnalysisServerAdapter(); | 277 server = new AnalysisServerAdapter(); |
280 savedContext = server.getSavedContext(); | 278 savedContext = server.getSavedContext(); |
281 listener = new Listener(server); | 279 listener = new Listener(server); |
282 } | 280 } |
283 | 281 |
284 @Override | 282 @Override |
285 protected void tearDown() throws Exception { | 283 protected void tearDown() throws Exception { |
286 server.stop(); | 284 server.stop(); |
287 } | 285 } |
288 } | 286 } |
OLD | NEW |