Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 IBM 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 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.ui.tests.propertiesfileeditor; |
12 |
|
13 |
import java.io.IOException; |
14 |
import java.io.InputStream; |
15 |
import java.io.Reader; |
16 |
import java.io.StringReader; |
17 |
|
18 |
import junit.framework.Test; |
19 |
import junit.framework.TestCase; |
20 |
import junit.framework.TestSuite; |
21 |
|
22 |
import org.eclipse.jdt.testplugin.JavaProjectHelper; |
23 |
|
24 |
import org.eclipse.core.runtime.CoreException; |
25 |
|
26 |
import org.eclipse.core.resources.IFile; |
27 |
import org.eclipse.core.resources.IFolder; |
28 |
|
29 |
import org.eclipse.jface.preference.IPreferenceStore; |
30 |
|
31 |
import org.eclipse.jface.text.DocumentCommand; |
32 |
|
33 |
import org.eclipse.jdt.core.IJavaProject; |
34 |
import org.eclipse.jdt.core.IPackageFragment; |
35 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
36 |
|
37 |
import org.eclipse.jdt.ui.PreferenceConstants; |
38 |
import org.eclipse.jdt.ui.tests.core.ProjectTestSetup; |
39 |
|
40 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
41 |
import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileAutoEditStrategy; |
42 |
|
43 |
/** |
44 |
* PropertiesFileAutoEditStratergyTest |
45 |
* |
46 |
* @since 3.7 |
47 |
*/ |
48 |
public class PropertiesFileAutoEditStrategyTest extends TestCase { |
49 |
|
50 |
private static final String UTF_8= "UTF-8"; |
51 |
private static final String ISO_8859_1= "ISO-8859-1"; |
52 |
|
53 |
private DocumentCommand fDocumentCommand; |
54 |
|
55 |
private IPreferenceStore fPreferenceStore; |
56 |
|
57 |
private PropertiesFileAutoEditStrategy fPropertiesFileAutoEditStrategyISO_8859_1; |
58 |
private PropertiesFileAutoEditStrategy fPropertiesFileAutoEditStrategyUTF8; |
59 |
|
60 |
private IJavaProject fJProject1; |
61 |
private IPackageFragmentRoot fSourceFolder; |
62 |
private IFile fPropertiesFileISO_8859_1; |
63 |
private IFile fPropertiesFileUTF8; |
64 |
|
65 |
public PropertiesFileAutoEditStrategyTest(String name) { |
66 |
super(name); |
67 |
fPreferenceStore= JavaPlugin.getDefault().getPreferenceStore(); |
68 |
fDocumentCommand= new DocumentCommand() { |
69 |
}; |
70 |
} |
71 |
|
72 |
public static Test suite() { |
73 |
return new ProjectTestSetup(new TestSuite(PropertiesFileAutoEditStrategyTest.class)); |
74 |
} |
75 |
|
76 |
protected void setUp() throws Exception { |
77 |
super.setUp(); |
78 |
setEscapeBackslashIfRequired(true); // make sure that the preference is set to true |
79 |
try { |
80 |
fJProject1= ProjectTestSetup.getProject(); |
81 |
fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
82 |
IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); |
83 |
fPropertiesFileISO_8859_1= write((IFolder)pack.getCorrespondingResource(), "", "Messages_ISO_8859_1.properties"); |
84 |
fPropertiesFileISO_8859_1.setCharset(ISO_8859_1, null); |
85 |
fPropertiesFileUTF8= write((IFolder)pack.getCorrespondingResource(), "", "Messages_UTF8.properties"); |
86 |
fPropertiesFileUTF8.setCharset(UTF_8, null); |
87 |
fPropertiesFileAutoEditStrategyISO_8859_1= new PropertiesFileAutoEditStrategy(fPreferenceStore, fPropertiesFileISO_8859_1); |
88 |
fPropertiesFileAutoEditStrategyUTF8= new PropertiesFileAutoEditStrategy(fPreferenceStore, fPropertiesFileUTF8); |
89 |
} catch (CoreException e) { |
90 |
e.printStackTrace(); |
91 |
} |
92 |
} |
93 |
|
94 |
protected void tearDown() throws Exception { |
95 |
JavaProjectHelper.clear(fJProject1, ProjectTestSetup.getDefaultClasspath()); |
96 |
super.tearDown(); |
97 |
} |
98 |
|
99 |
//utility methods |
100 |
private void performTestISO_8859_1() { |
101 |
fPropertiesFileAutoEditStrategyISO_8859_1.customizeDocumentCommand(null, fDocumentCommand); |
102 |
} |
103 |
|
104 |
private void performTestAutoEscapeSlashDisabledISO_8859_1() { |
105 |
setEscapeBackslashIfRequired(false); |
106 |
fPropertiesFileAutoEditStrategyISO_8859_1.customizeDocumentCommand(null, fDocumentCommand); |
107 |
setEscapeBackslashIfRequired(true); |
108 |
} |
109 |
|
110 |
private void performTestUTF8() { |
111 |
fPropertiesFileAutoEditStrategyUTF8.customizeDocumentCommand(null, fDocumentCommand); |
112 |
} |
113 |
|
114 |
private void performTestAutoEscapeSlashDisabledUTF8() { |
115 |
setEscapeBackslashIfRequired(false); |
116 |
fPropertiesFileAutoEditStrategyUTF8.customizeDocumentCommand(null, fDocumentCommand); |
117 |
setEscapeBackslashIfRequired(true); |
118 |
} |
119 |
|
120 |
private void setEscapeBackslashIfRequired(boolean value) { |
121 |
fPreferenceStore.setValue(PreferenceConstants.PROPERTIES_FILE_WHEN_PASTING_ESCAPE_BACKSLASH_IF_REQUIRED, value); |
122 |
} |
123 |
|
124 |
private IFile write(IFolder folder, final String content, final String fileName) throws CoreException { |
125 |
InputStream stream= new InputStream() { |
126 |
private final Reader fReader= new StringReader(content); |
127 |
|
128 |
public int read() throws IOException { |
129 |
return fReader.read(); |
130 |
} |
131 |
}; |
132 |
IFile file= fJProject1.getProject().getFile(folder.getProjectRelativePath().append(fileName)); |
133 |
file.create(stream, true, null); |
134 |
return file; |
135 |
} |
136 |
|
137 |
//typing ISO-8859-1 |
138 |
public void testTypeISO_8859_1_01() throws Exception { |
139 |
fDocumentCommand.text= "\\"; |
140 |
performTestISO_8859_1(); |
141 |
assertEquals("\\", fDocumentCommand.text); |
142 |
} |
143 |
|
144 |
public void testTypeISO_8859_1_02() throws Exception { |
145 |
fDocumentCommand.text= "\t"; |
146 |
performTestISO_8859_1(); |
147 |
assertEquals("\t", fDocumentCommand.text); |
148 |
} |
149 |
|
150 |
public void testTypeISO_8859_1_03() throws Exception { |
151 |
fDocumentCommand.text= "\f"; |
152 |
performTestISO_8859_1(); |
153 |
assertEquals("\f", fDocumentCommand.text); |
154 |
} |
155 |
|
156 |
public void testTypeISO_8859_1_04() throws Exception { |
157 |
fDocumentCommand.text= "\r"; |
158 |
performTestISO_8859_1(); |
159 |
assertEquals("\r", fDocumentCommand.text); |
160 |
} |
161 |
|
162 |
public void testTypeISO_8859_1_05() throws Exception { |
163 |
fDocumentCommand.text= "\n"; |
164 |
performTestISO_8859_1(); |
165 |
assertEquals("\n", fDocumentCommand.text); |
166 |
} |
167 |
|
168 |
public void testTypeISO_8859_1_06() throws Exception { |
169 |
fDocumentCommand.text= "\u2603"; |
170 |
performTestISO_8859_1(); |
171 |
assertEquals("\\u2603", fDocumentCommand.text); |
172 |
} |
173 |
|
174 |
//typing UTF-8 |
175 |
public void testTypeUTF8_01() throws Exception { |
176 |
fDocumentCommand.text= "\u2603"; |
177 |
performTestUTF8(); |
178 |
assertEquals("\u2603", fDocumentCommand.text); |
179 |
} |
180 |
|
181 |
//paste ISO-8859-1 |
182 |
public void testPasteISO_8859_1_01() throws Exception { |
183 |
fDocumentCommand.text= "C:\\Program Files\\Java"; |
184 |
performTestISO_8859_1(); |
185 |
assertEquals("C:\\\\Program Files\\\\Java", fDocumentCommand.text); |
186 |
} |
187 |
|
188 |
public void testPasteISO_8859_1_02() throws Exception { |
189 |
fDocumentCommand.text= "C:\\new folder\\A.java"; |
190 |
performTestISO_8859_1(); |
191 |
assertEquals("C:\\\\new folder\\\\A.java", fDocumentCommand.text); |
192 |
} |
193 |
|
194 |
public void testPasteISO_8859_1_03() throws Exception { |
195 |
fDocumentCommand.text= "\u0926 \u0905"; |
196 |
performTestISO_8859_1(); |
197 |
assertEquals("\\u0926 \\u0905", fDocumentCommand.text); |
198 |
} |
199 |
|
200 |
public void testPasteISO_8859_1_04() throws Exception { |
201 |
fDocumentCommand.text= "\u0926 \\u0905"; |
202 |
performTestISO_8859_1(); |
203 |
assertEquals("\\u0926 \\\\u0905", fDocumentCommand.text); |
204 |
} |
205 |
|
206 |
public void testPasteISO_8859_1_05() throws Exception { |
207 |
fDocumentCommand.text= "ä \u0926"; |
208 |
performTestISO_8859_1(); |
209 |
assertEquals("ä \\u0926", fDocumentCommand.text); |
210 |
} |
211 |
|
212 |
public void testPasteISO_8859_1_06() throws Exception { |
213 |
fDocumentCommand.text= "some text\\"; |
214 |
performTestISO_8859_1(); |
215 |
assertEquals("some text\\\\", fDocumentCommand.text); |
216 |
} |
217 |
|
218 |
//paste UTF-8 |
219 |
public void testPasteUTF8_01() throws Exception { |
220 |
fDocumentCommand.text= "C:\\Program Files\\Java"; |
221 |
performTestUTF8(); |
222 |
assertEquals("C:\\\\Program Files\\\\Java", fDocumentCommand.text); |
223 |
} |
224 |
|
225 |
public void testPasteUTF8_02() throws Exception { |
226 |
fDocumentCommand.text= "C:\\new folder\\A.java"; |
227 |
performTestUTF8(); |
228 |
assertEquals("C:\\\\new folder\\\\A.java", fDocumentCommand.text); |
229 |
} |
230 |
|
231 |
public void testPasteUTF8_03() throws Exception { |
232 |
fDocumentCommand.text= "\u0926 \u0905"; |
233 |
performTestUTF8(); |
234 |
assertEquals("\u0926 \u0905", fDocumentCommand.text); |
235 |
} |
236 |
|
237 |
public void testPasteUTF8_04() throws Exception { |
238 |
fDocumentCommand.text= "\u0926 \\u0905"; |
239 |
performTestUTF8(); |
240 |
assertEquals("\u0926 \\u0905", fDocumentCommand.text); |
241 |
} |
242 |
|
243 |
public void testPasteUTF8_05() throws Exception { |
244 |
fDocumentCommand.text= "\u0926 \\some text"; |
245 |
performTestUTF8(); |
246 |
assertEquals("\u0926 \\\\some text", fDocumentCommand.text); |
247 |
} |
248 |
|
249 |
//paste from properties file ISO-8859-1 |
250 |
public void testPasteFromPropertiesFileISO_8859_1_01() throws Exception { |
251 |
fDocumentCommand.text= "\t \n \f \r"; |
252 |
performTestISO_8859_1(); |
253 |
assertEquals("\t \n \f \r", fDocumentCommand.text); |
254 |
} |
255 |
|
256 |
public void testPasteFromPropertiesFileISO_8859_1_02() throws Exception { |
257 |
fDocumentCommand.text= "\\u00e4 \\t \\u0926 \\n"; |
258 |
performTestISO_8859_1(); |
259 |
assertEquals("\\u00e4 \\t \\u0926 \\n", fDocumentCommand.text); |
260 |
} |
261 |
|
262 |
public void testPasteFromPropertiesFileISO_8859_1_03() throws Exception { |
263 |
fDocumentCommand.text= "C:\\\\Program Files\\\\Java"; |
264 |
performTestISO_8859_1(); |
265 |
assertEquals("C:\\\\Program Files\\\\Java", fDocumentCommand.text); |
266 |
} |
267 |
|
268 |
public void testPasteFromPropertiesFileISO_8859_1_04() throws Exception { |
269 |
fDocumentCommand.text= "C:\\\\new folder\\\\A.java"; |
270 |
performTestISO_8859_1(); |
271 |
assertEquals("C:\\\\new folder\\\\A.java", fDocumentCommand.text); |
272 |
} |
273 |
|
274 |
public void testPasteFromPropertiesFileISO_8859_1_05() throws Exception { |
275 |
fDocumentCommand.text= "\\u2603 \\\\u2603"; |
276 |
performTestISO_8859_1(); |
277 |
assertEquals("\\u2603 \\\\u2603", fDocumentCommand.text); |
278 |
} |
279 |
|
280 |
// paste ISO-8859-1 and PreferenceConstants.PROPERTIES_FILE_ESCAPE_BACKSLASH_ON_PASTE_IF_REQUIRED disabled |
281 |
public void testAutoEscapeSlashDisabledISO_8859_1_01() throws Exception { |
282 |
fDocumentCommand.text= "C:\\Program Files\\Java"; |
283 |
performTestAutoEscapeSlashDisabledISO_8859_1(); |
284 |
assertEquals("C:\\Program Files\\Java", fDocumentCommand.text); |
285 |
} |
286 |
|
287 |
public void testAutoEscapeSlashDisabledISO_8859_1_02() throws Exception { |
288 |
fDocumentCommand.text= "C:\\new folder\\A.java"; |
289 |
performTestAutoEscapeSlashDisabledISO_8859_1(); |
290 |
assertEquals("C:\\new folder\\A.java", fDocumentCommand.text); |
291 |
} |
292 |
|
293 |
public void testAutoEscapeSlashDisabledISO_8859_1_03() throws Exception { |
294 |
fDocumentCommand.text= "\u0926 \\u0905"; |
295 |
performTestAutoEscapeSlashDisabledISO_8859_1(); |
296 |
assertEquals("\\u0926 \\u0905", fDocumentCommand.text); |
297 |
} |
298 |
|
299 |
//paste UTF-8 and PreferenceConstants.PROPERTIES_FILE_ESCAPE_BACKSLASH_ON_PASTE_IF_REQUIRED disabled |
300 |
public void testAutoEscapeSlashDisabledUTF8_01() throws Exception { |
301 |
fDocumentCommand.text= "\u0926 \\some text"; |
302 |
performTestAutoEscapeSlashDisabledUTF8(); |
303 |
assertEquals("\u0926 \\some text", fDocumentCommand.text); |
304 |
} |
305 |
|
306 |
//change encoding of file |
307 |
public void testChangeEncodingOfFile_01() throws Exception { |
308 |
fDocumentCommand.text= "\u2603"; |
309 |
performTestUTF8(); |
310 |
assertEquals("\u2603", fDocumentCommand.text); |
311 |
|
312 |
fPropertiesFileUTF8.setCharset(ISO_8859_1, null); |
313 |
|
314 |
fDocumentCommand.text= "\u2603"; |
315 |
performTestUTF8(); |
316 |
assertEquals("\\u2603", fDocumentCommand.text); |
317 |
|
318 |
fPropertiesFileUTF8.setCharset(UTF_8, null); |
319 |
} |
320 |
|
321 |
public void testChangeEncodingOfFile_02() throws Exception { |
322 |
fDocumentCommand.text= "\u2603"; |
323 |
performTestISO_8859_1(); |
324 |
assertEquals("\\u2603", fDocumentCommand.text); |
325 |
|
326 |
fPropertiesFileISO_8859_1.setCharset(UTF_8, null); |
327 |
|
328 |
fDocumentCommand.text= "\u2603"; |
329 |
performTestISO_8859_1(); |
330 |
assertEquals("\u2603", fDocumentCommand.text); |
331 |
|
332 |
fPropertiesFileISO_8859_1.setCharset(ISO_8859_1, null); |
333 |
} |
334 |
} |