Added
Link Here
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* The Original Software is NetBeans. The Initial Developer of the Original |
16 |
* Software is Sun Microsystems, Inc. |
17 |
* |
18 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
19 |
*/ |
20 |
|
21 |
package org.netbeans.modules.templates; |
22 |
|
23 |
import java.io.IOException; |
24 |
import java.io.OutputStream; |
25 |
import java.nio.charset.Charset; |
26 |
import java.util.Collections; |
27 |
import java.util.Collections; |
28 |
import java.util.Map; |
29 |
import org.netbeans.api.queries.FileEncodingQuery; |
30 |
import org.netbeans.junit.MockServices; |
31 |
import org.netbeans.junit.NbTestCase; |
32 |
import org.openide.filesystems.FileObject; |
33 |
import org.openide.filesystems.FileUtil; |
34 |
import org.openide.loaders.DataFolder; |
35 |
import org.openide.loaders.DataObject; |
36 |
import org.openide.loaders.DataObjectExistsException; |
37 |
import org.openide.loaders.FileEntry; |
38 |
import org.openide.loaders.MultiDataObject; |
39 |
import org.openide.loaders.MultiFileLoader; |
40 |
|
41 |
/** |
42 |
* |
43 |
* @author Marek Fukala |
44 |
* @author Jaroslav Tulach |
45 |
*/ |
46 |
public class ScriptingCreateFromTemplateTest extends NbTestCase { |
47 |
|
48 |
public ScriptingCreateFromTemplateTest(String testName) { |
49 |
super(testName); |
50 |
} |
51 |
|
52 |
@Override |
53 |
protected boolean runInEQ() { |
54 |
return true; |
55 |
} |
56 |
|
57 |
@Override |
58 |
protected void setUp() throws Exception { |
59 |
MockServices.setServices(SimpleLoader.class); |
60 |
} |
61 |
|
62 |
public void testCreateFromTemplateEndcodingProperty() throws Exception { |
63 |
FileObject root = FileUtil.createMemoryFileSystem().getRoot(); |
64 |
FileObject fo = FileUtil.createData(root, "simpleObject.txt"); |
65 |
OutputStream os = fo.getOutputStream(); |
66 |
os.write("${encoding}".getBytes()); |
67 |
os.close(); |
68 |
fo.setAttribute ("template", Boolean.TRUE); |
69 |
fo.setAttribute("javax.script.ScriptEngine", "freemarker"); |
70 |
|
71 |
DataObject obj = DataObject.find(fo); |
72 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); |
73 |
|
74 |
Map<String,String> parameters = Collections.emptyMap(); |
75 |
DataObject inst = obj.createFromTemplate(folder, "complex", parameters); |
76 |
|
77 |
FileObject instFO = inst.getPrimaryFile(); |
78 |
|
79 |
Charset targetEnc = FileEncodingQuery.getEncoding(instFO); |
80 |
assertNotNull("Template encoding is null", targetEnc); |
81 |
assertEquals("Encoding in template doesn't match", targetEnc.name(), readFile(instFO)); |
82 |
} |
83 |
|
84 |
private static String readFile(FileObject fo) throws IOException { |
85 |
byte[] arr = new byte[(int)fo.getSize()]; |
86 |
int len = fo.getInputStream().read(arr); |
87 |
assertEquals("Fully read", arr.length, len); |
88 |
return new String(arr); |
89 |
} |
90 |
|
91 |
public static final class SimpleLoader extends MultiFileLoader { |
92 |
public SimpleLoader() { |
93 |
super(SimpleObject.class.getName()); |
94 |
} |
95 |
protected String displayName() { |
96 |
return "SimpleLoader"; |
97 |
} |
98 |
protected FileObject findPrimaryFile(FileObject fo) { |
99 |
if (fo.hasExt("prima")) { |
100 |
return fo; |
101 |
} |
102 |
return null; |
103 |
} |
104 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
105 |
return new SimpleObject(this, primaryFile); |
106 |
} |
107 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
108 |
return new FE(obj, primaryFile); |
109 |
} |
110 |
protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { |
111 |
return new FileEntry(obj, secondaryFile); |
112 |
} |
113 |
} |
114 |
|
115 |
private static final class FE extends FileEntry { |
116 |
public FE(MultiDataObject mo, FileObject fo) { |
117 |
super(mo, fo); |
118 |
} |
119 |
|
120 |
@Override |
121 |
public FileObject createFromTemplate(FileObject f, String name) throws IOException { |
122 |
fail("I do not want to be called"); |
123 |
return null; |
124 |
} |
125 |
} |
126 |
|
127 |
public static final class SimpleObject extends MultiDataObject { |
128 |
public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { |
129 |
super(fo, l); |
130 |
} |
131 |
|
132 |
public String getName() { |
133 |
return getPrimaryFile().getNameExt(); |
134 |
} |
135 |
} |
136 |
|
137 |
} |