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.awt.Dialog; |
24 |
import java.io.IOException; |
25 |
import java.io.OutputStream; |
26 |
import java.io.StringWriter; |
27 |
import java.io.Writer; |
28 |
import java.nio.charset.Charset; |
29 |
import java.util.Collections; |
30 |
import java.util.Collections; |
31 |
import java.util.Enumeration; |
32 |
import java.util.Map; |
33 |
import javax.swing.text.Document; |
34 |
import org.netbeans.api.queries.FileEncodingQuery; |
35 |
import org.netbeans.junit.MockServices; |
36 |
import org.netbeans.junit.NbTestCase; |
37 |
import org.openide.DialogDescriptor; |
38 |
import org.openide.DialogDisplayer; |
39 |
import org.openide.NotifyDescriptor; |
40 |
import org.openide.filesystems.FileObject; |
41 |
import org.openide.filesystems.FileUtil; |
42 |
import org.openide.loaders.DataFolder; |
43 |
import org.openide.loaders.DataLoader; |
44 |
import org.openide.loaders.DataLoaderPool; |
45 |
import org.openide.loaders.DataObject; |
46 |
import org.openide.loaders.DataObjectExistsException; |
47 |
import org.openide.loaders.FileEntry; |
48 |
import org.openide.loaders.MultiDataObject; |
49 |
import org.openide.loaders.MultiFileLoader; |
50 |
import org.openide.text.IndentEngine; |
51 |
import org.openide.util.Enumerations; |
52 |
|
53 |
/** |
54 |
* |
55 |
* @author Marek Fukala |
56 |
* @author Jaroslav Tulach |
57 |
*/ |
58 |
public class ScriptingCreateFromTemplateTest extends NbTestCase { |
59 |
|
60 |
public ScriptingCreateFromTemplateTest(String testName) { |
61 |
super(testName); |
62 |
} |
63 |
|
64 |
protected boolean runInEQ() { |
65 |
return true; |
66 |
} |
67 |
|
68 |
@SuppressWarnings("deprecation") |
69 |
protected void setUp() throws Exception { |
70 |
MockServices.setServices(DD.class, Pool.class); |
71 |
} |
72 |
|
73 |
protected void tearDown() throws Exception { |
74 |
super.tearDown(); |
75 |
} |
76 |
|
77 |
public void testCreateFromTemplateEndcodingProperty() throws Exception { |
78 |
FileObject root = FileUtil.createMemoryFileSystem().getRoot(); |
79 |
FileObject fo = FileUtil.createData(root, "simpleObject.txt"); |
80 |
OutputStream os = fo.getOutputStream(); |
81 |
os.write("${encoding}".getBytes()); |
82 |
os.close(); |
83 |
fo.setAttribute ("template", Boolean.TRUE); |
84 |
fo.setAttribute("javax.script.ScriptEngine", "freemarker"); |
85 |
|
86 |
DataObject obj = DataObject.find(fo); |
87 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); |
88 |
|
89 |
Map<String,String> parameters = Collections.emptyMap(); |
90 |
DataObject inst = obj.createFromTemplate(folder, "complex", parameters); |
91 |
|
92 |
FileObject instFO = inst.getPrimaryFile(); |
93 |
|
94 |
Charset targetEnc = FileEncodingQuery.getEncoding(instFO); |
95 |
assertNotNull("Template encoding is null", targetEnc); |
96 |
assertEquals("Encoding in template doesn't match", targetEnc.name(), readFile(instFO)); |
97 |
} |
98 |
|
99 |
private static String readFile(FileObject fo) throws IOException { |
100 |
byte[] arr = new byte[(int)fo.getSize()]; |
101 |
int len = fo.getInputStream().read(arr); |
102 |
assertEquals("Fully read", arr.length, len); |
103 |
return new String(arr); |
104 |
} |
105 |
|
106 |
public static final class DD extends DialogDisplayer { |
107 |
public Object notify(NotifyDescriptor descriptor) { |
108 |
throw new UnsupportedOperationException("Not supported yet."); |
109 |
} |
110 |
|
111 |
public Dialog createDialog(final DialogDescriptor descriptor) { |
112 |
throw new UnsupportedOperationException("Not supported yet."); |
113 |
} |
114 |
} |
115 |
|
116 |
public static final class Pool extends DataLoaderPool { |
117 |
protected Enumeration<DataLoader> loaders() { |
118 |
return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class)); |
119 |
} |
120 |
} |
121 |
|
122 |
public static final class SimpleLoader extends MultiFileLoader { |
123 |
public SimpleLoader() { |
124 |
super(SimpleObject.class.getName()); |
125 |
} |
126 |
protected String displayName() { |
127 |
return "SimpleLoader"; |
128 |
} |
129 |
protected FileObject findPrimaryFile(FileObject fo) { |
130 |
if (fo.hasExt("prima")) { |
131 |
return fo; |
132 |
} |
133 |
return null; |
134 |
} |
135 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
136 |
return new SimpleObject(this, primaryFile); |
137 |
} |
138 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
139 |
return new FE(obj, primaryFile); |
140 |
} |
141 |
protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { |
142 |
return new FileEntry(obj, secondaryFile); |
143 |
} |
144 |
} |
145 |
|
146 |
private static final class FE extends FileEntry { |
147 |
public FE(MultiDataObject mo, FileObject fo) { |
148 |
super(mo, fo); |
149 |
} |
150 |
|
151 |
@Override |
152 |
public FileObject createFromTemplate(FileObject f, String name) throws IOException { |
153 |
fail("I do not want to be called"); |
154 |
return null; |
155 |
} |
156 |
|
157 |
|
158 |
|
159 |
} |
160 |
|
161 |
public static final class SimpleObject extends MultiDataObject { |
162 |
public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { |
163 |
super(fo, l); |
164 |
} |
165 |
|
166 |
public String getName() { |
167 |
return getPrimaryFile().getNameExt(); |
168 |
} |
169 |
} |
170 |
|
171 |
} |