Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2011 Broadcom Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* James Blackburn (Broadcom Corp.) - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.core.tests.resources.regression; |
12 |
|
13 |
import java.io.File; |
14 |
import java.io.InputStream; |
15 |
import junit.framework.Test; |
16 |
import junit.framework.TestSuite; |
17 |
import org.eclipse.core.resources.*; |
18 |
import org.eclipse.core.runtime.CoreException; |
19 |
import org.eclipse.core.runtime.Path; |
20 |
import org.eclipse.core.runtime.jobs.Job; |
21 |
import org.eclipse.core.tests.resources.ResourceTest; |
22 |
|
23 |
/** |
24 |
* Tests that, when the workspace discovery a resource is out-of-sync |
25 |
* it brings the resource back into sync in a timely manner. |
26 |
*/ |
27 |
public class Bug_303517 extends ResourceTest { |
28 |
|
29 |
public static Test suite() { |
30 |
return new TestSuite(Bug_303517.class); |
31 |
} |
32 |
|
33 |
String[] resources = new String[] {"/", "/Bug303517/", "/Bug303517/Folder/", "/Bug303517/Folder/Resource",}; |
34 |
|
35 |
public String[] defineHierarchy() { |
36 |
return resources; |
37 |
} |
38 |
|
39 |
/** |
40 |
* Tests that file deleted is udpated after #getContents |
41 |
* @throws Exception |
42 |
*/ |
43 |
public void testExists() throws Exception { |
44 |
createHierarchy(); |
45 |
IFile f = getWorkspace().getRoot().getFile(new Path(resources[resources.length - 1])); |
46 |
assertTrue("1.0", f.exists()); |
47 |
assertTrue("1.1", f.isSynchronized(IResource.DEPTH_ONE)); |
48 |
|
49 |
// Touch on file-system |
50 |
boolean fail = true; |
51 |
f.getLocation().toFile().delete(); |
52 |
// Core.resources still thinks the file exists |
53 |
assertTrue("1.2", f.exists()); |
54 |
try { |
55 |
InputStream in = f.getContents(); |
56 |
in.close(); |
57 |
} catch (CoreException e) { |
58 |
// File doesn't exist |
59 |
fail = false; |
60 |
} |
61 |
assertFalse("1.3", fail); |
62 |
|
63 |
// Wait for auto-refresh to happen |
64 |
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, getMonitor()); |
65 |
|
66 |
// Core.resources should be aware that the file no longer exists... |
67 |
assertFalse("1.4", f.exists()); |
68 |
} |
69 |
|
70 |
/** |
71 |
* Tests that file discovered out-of-sync during #getContents is updated |
72 |
* @throws Exception |
73 |
*/ |
74 |
public void testGetContents() throws Exception { |
75 |
createHierarchy(); |
76 |
IFile f = getWorkspace().getRoot().getFile(new Path(resources[resources.length - 1])); |
77 |
assertTrue("1.0", f.exists()); |
78 |
assertTrue("1.1", f.isSynchronized(IResource.DEPTH_ONE)); |
79 |
|
80 |
// Touch on file-system |
81 |
boolean fail = true; |
82 |
touchInFilesystem(f); |
83 |
try { |
84 |
InputStream in = f.getContents(false); |
85 |
in.close(); |
86 |
} catch (CoreException e) { |
87 |
// File is out-of-sync, so this is good |
88 |
fail = false; |
89 |
} |
90 |
assertFalse("1.2", fail); |
91 |
|
92 |
// Wait for auto-refresh to happen |
93 |
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, getMonitor()); |
94 |
|
95 |
// File is now in sync. |
96 |
try { |
97 |
InputStream in = f.getContents(false); |
98 |
in.close(); |
99 |
} catch (CoreException e) { |
100 |
// Bad, file shouldn't be out-of-sync |
101 |
fail = true; |
102 |
} |
103 |
assertFalse("1.3", fail); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Tests that file discovered out-of-sync during #getContents is updated |
108 |
* @throws Exception |
109 |
*/ |
110 |
public void testGetContentsTrue() throws Exception { |
111 |
createHierarchy(); |
112 |
IFile f = getWorkspace().getRoot().getFile(new Path(resources[resources.length - 1])); |
113 |
assertTrue("1.0", f.exists()); |
114 |
assertTrue("1.1", f.isSynchronized(IResource.DEPTH_ONE)); |
115 |
|
116 |
// Touch on file-system |
117 |
boolean fail = false; |
118 |
touchInFilesystem(f); |
119 |
try { |
120 |
InputStream in = f.getContents(true); |
121 |
in.close(); |
122 |
} catch (CoreException e) { |
123 |
// File is out-of-sync, so this is good |
124 |
fail = true; |
125 |
} |
126 |
assertFalse("1.2", fail); |
127 |
|
128 |
// Wait for auto-refresh to happen |
129 |
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, getMonitor()); |
130 |
|
131 |
// File is now in sync. |
132 |
try { |
133 |
InputStream in = f.getContents(); |
134 |
in.close(); |
135 |
} catch (CoreException e) { |
136 |
// Bad, file shouldn't be out-of-sync |
137 |
fail = true; |
138 |
} |
139 |
assertFalse("1.3", fail); |
140 |
} |
141 |
|
142 |
/** |
143 |
* Tests that resource discovered out-of-sync during #isSynchronized is updated |
144 |
* @throws Exception |
145 |
*/ |
146 |
public void testIsSynchronized() throws Exception { |
147 |
createHierarchy(); |
148 |
IFile f = getWorkspace().getRoot().getFile(new Path(resources[resources.length - 1])); |
149 |
assertTrue("1.0", f.exists()); |
150 |
assertTrue("1.1", f.isSynchronized(IResource.DEPTH_ONE)); |
151 |
|
152 |
// Touch on file-system |
153 |
touchInFilesystem(f); |
154 |
assertFalse("1.2", f.isSynchronized(IResource.DEPTH_ONE)); |
155 |
|
156 |
// Wait for auto-refresh to happen |
157 |
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, getMonitor()); |
158 |
|
159 |
// File is now in sync. |
160 |
assertTrue("1.3", f.isSynchronized(IResource.DEPTH_ONE)); |
161 |
} |
162 |
|
163 |
/** |
164 |
* Tests that when changing resource gender is correctly picked up. |
165 |
* @throws Exception |
166 |
*/ |
167 |
public void testChangeResourceGender() throws Exception { |
168 |
createHierarchy(); |
169 |
IResource f = getWorkspace().getRoot().getFile(new Path(resources[resources.length - 1])); |
170 |
assertTrue("1.0", f.exists()); |
171 |
assertTrue("1.1", f.isSynchronized(IResource.DEPTH_ONE)); |
172 |
|
173 |
// Replace the file with a folder |
174 |
File osResource = f.getLocation().toFile(); |
175 |
osResource.delete(); |
176 |
osResource.mkdir(); |
177 |
assertTrue(osResource.exists()); |
178 |
File osChild = new File(osResource, "child"); |
179 |
osChild.createNewFile(); |
180 |
assertTrue(osChild.exists()); |
181 |
|
182 |
assertFalse("1.2", f.isSynchronized(IResource.DEPTH_ONE)); |
183 |
|
184 |
// Wait for auto-refresh to happen |
185 |
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, getMonitor()); |
186 |
|
187 |
// File is no longer a file - i.e. still out-of-sync |
188 |
assertFalse("1.3", f.exists()); |
189 |
assertFalse("1.4", f.isSynchronized(IResource.DEPTH_ONE)); |
190 |
// Folder + child are now in-sync |
191 |
f = getWorkspace().getRoot().getFolder(new Path(resources[resources.length - 1])); |
192 |
assertTrue("1.5", f.exists()); |
193 |
assertTrue("1.6", f.isSynchronized(IResource.DEPTH_INFINITE)); |
194 |
} |
195 |
} |