Lines 21-39
Link Here
|
21 |
package org.netbeans.modules.templates; |
21 |
package org.netbeans.modules.templates; |
22 |
|
22 |
|
23 |
import java.awt.Dialog; |
23 |
import java.awt.Dialog; |
|
|
24 |
import java.io.BufferedReader; |
24 |
import java.io.IOException; |
25 |
import java.io.IOException; |
|
|
26 |
import java.io.InputStreamReader; |
25 |
import java.io.OutputStream; |
27 |
import java.io.OutputStream; |
|
|
28 |
import java.nio.CharBuffer; |
29 |
import java.nio.charset.Charset; |
26 |
import java.util.Collections; |
30 |
import java.util.Collections; |
27 |
import java.util.Enumeration; |
31 |
import java.util.Enumeration; |
28 |
import java.util.Map; |
32 |
import java.util.Map; |
29 |
import javax.swing.JDialog; |
33 |
import javax.swing.JDialog; |
30 |
import org.netbeans.junit.MockServices; |
34 |
import org.netbeans.junit.MockServices; |
31 |
import org.netbeans.junit.NbTestCase; |
35 |
import org.netbeans.junit.NbTestCase; |
|
|
36 |
import org.netbeans.spi.queries.FileEncodingQueryImplementation; |
32 |
import org.openide.DialogDescriptor; |
37 |
import org.openide.DialogDescriptor; |
33 |
import org.openide.DialogDisplayer; |
38 |
import org.openide.DialogDisplayer; |
34 |
import org.openide.NotifyDescriptor; |
39 |
import org.openide.NotifyDescriptor; |
35 |
import org.openide.filesystems.FileObject; |
40 |
import org.openide.filesystems.FileObject; |
|
|
41 |
import org.openide.filesystems.FileStateInvalidException; |
42 |
import org.openide.filesystems.FileSystem; |
36 |
import org.openide.filesystems.FileUtil; |
43 |
import org.openide.filesystems.FileUtil; |
|
|
44 |
import org.openide.filesystems.Repository; |
37 |
import org.openide.loaders.DataFolder; |
45 |
import org.openide.loaders.DataFolder; |
38 |
import org.openide.loaders.DataLoader; |
46 |
import org.openide.loaders.DataLoader; |
39 |
import org.openide.loaders.DataLoaderPool; |
47 |
import org.openide.loaders.DataLoaderPool; |
Lines 43-54
Link Here
|
43 |
import org.openide.loaders.MultiDataObject; |
51 |
import org.openide.loaders.MultiDataObject; |
44 |
import org.openide.loaders.MultiFileLoader; |
52 |
import org.openide.loaders.MultiFileLoader; |
45 |
import org.openide.util.Enumerations; |
53 |
import org.openide.util.Enumerations; |
|
|
54 |
import org.openide.util.Exceptions; |
46 |
|
55 |
|
47 |
/** |
56 |
/** |
48 |
* |
57 |
* |
49 |
* @author Jaroslav Tulach |
58 |
* @author Jaroslav Tulach |
50 |
*/ |
59 |
*/ |
51 |
public class SCFTHandlerTest extends NbTestCase { |
60 |
public class SCFTHandlerTest extends NbTestCase { |
|
|
61 |
static { |
62 |
// confuse the system a bit, if your system runs with UTF-8 default locale... |
63 |
//System.setProperty("file.encoding", "cp1252"); |
64 |
} |
52 |
|
65 |
|
53 |
public SCFTHandlerTest(String testName) { |
66 |
public SCFTHandlerTest(String testName) { |
54 |
super(testName); |
67 |
super(testName); |
Lines 59-65
Link Here
|
59 |
} |
72 |
} |
60 |
|
73 |
|
61 |
protected void setUp() throws Exception { |
74 |
protected void setUp() throws Exception { |
62 |
MockServices.setServices(DD.class, Pool.class); |
75 |
MockServices.setServices(DD.class, Pool.class, FEQI.class); |
63 |
} |
76 |
} |
64 |
|
77 |
|
65 |
protected void tearDown() throws Exception { |
78 |
protected void tearDown() throws Exception { |
Lines 161-166
Link Here
|
161 |
assertEquals("Fully read", arr.length, len); |
174 |
assertEquals("Fully read", arr.length, len); |
162 |
return new String(arr); |
175 |
return new String(arr); |
163 |
} |
176 |
} |
|
|
177 |
|
178 |
private static String readChars(FileObject fo, Charset set) throws IOException { |
179 |
CharBuffer arr = CharBuffer.allocate((int)fo.getSize() * 2); |
180 |
BufferedReader r = new BufferedReader(new InputStreamReader(fo.getInputStream(), set)); |
181 |
while (r.read(arr) != -1) { |
182 |
// again |
183 |
} |
184 |
r.close(); |
185 |
|
186 |
arr.flip(); |
187 |
return arr.toString(); |
188 |
} |
189 |
|
190 |
public void testUTF8() throws Exception { |
191 |
FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); |
192 |
FileObject xmldir = FileUtil.createFolder(root, "xml"); |
193 |
FileObject xml = FileUtil.createData(xmldir, "class.txt"); |
194 |
OutputStream os = xml.getOutputStream(); |
195 |
FileUtil.copy(getClass().getResourceAsStream("utf8.xml"), os); |
196 |
xml.setAttribute("javax.script.ScriptEngine", "freemarker"); |
197 |
os.close(); |
198 |
|
199 |
DataObject obj = DataObject.find(xml); |
200 |
|
201 |
|
202 |
FileObject target = FileUtil.createFolder(FileUtil.createMemoryFileSystem().getRoot(), "dir"); |
203 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(target, "target")); |
204 |
|
205 |
|
206 |
|
207 |
Charset set = Charset.forName("iso-8859-2"); |
208 |
FEQI.fs = target.getFileSystem(); |
209 |
FEQI.result = set; |
210 |
|
211 |
|
212 |
Map<String,String> parameters = Collections.singletonMap("title", "Nazdar"); |
213 |
DataObject n = obj.createFromTemplate(folder, "complex", parameters); |
214 |
|
215 |
assertEquals("Created in right place", folder, n.getFolder()); |
216 |
assertEquals("Created with right name", "complex.txt", n.getName()); |
217 |
|
218 |
|
219 |
String read = readChars(n.getPrimaryFile(), set); |
220 |
|
221 |
String exp = readChars(xml, Charset.forName("utf-8")); |
222 |
assertEquals(exp, read); |
223 |
|
224 |
} |
164 |
|
225 |
|
165 |
public static final class DD extends DialogDisplayer { |
226 |
public static final class DD extends DialogDisplayer { |
166 |
public Object notify(NotifyDescriptor descriptor) { |
227 |
public Object notify(NotifyDescriptor descriptor) { |
Lines 187-192
Link Here
|
187 |
} |
248 |
} |
188 |
}; |
249 |
}; |
189 |
*/ |
250 |
*/ |
|
|
251 |
} |
252 |
} |
253 |
|
254 |
public static final class FEQI extends FileEncodingQueryImplementation { |
255 |
public static FileSystem fs; |
256 |
public static Charset result; |
257 |
|
258 |
public Charset getEncoding(FileObject f) { |
259 |
try { |
260 |
if (f.getFileSystem() == fs) { |
261 |
return result; |
262 |
} |
263 |
return null; |
264 |
} catch (FileStateInvalidException ex) { |
265 |
return null; |
266 |
} |
190 |
} |
267 |
} |
191 |
} |
268 |
} |
192 |
|
269 |
|