2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* Portions Copyrighted 2007 Sun Microsystems, Inc.
28
package org.netbeans.modules.editor.settings.storage.codetemplates;
31
import java.util.Arrays;
32
import java.util.Collection;
33
import java.util.Collections;
34
import java.util.HashMap;
35
import java.util.List;
37
import org.netbeans.api.editor.mimelookup.MimeLookup;
38
import org.netbeans.api.editor.mimelookup.MimePath;
39
import org.netbeans.api.editor.settings.CodeTemplateDescription;
40
import org.netbeans.api.editor.settings.CodeTemplateSettings;
41
import org.netbeans.core.startup.Main;
42
import org.netbeans.junit.NbTestCase;
43
import org.netbeans.modules.editor.settings.storage.EditorTestLookup;
49
public class CodeTemplateSettingsImplTest extends NbTestCase {
51
public CodeTemplateSettingsImplTest(String name) {
56
protected void setUp() throws Exception {
59
EditorTestLookup.setLookup(
61
getClass().getClassLoader().getResource(
62
"org/netbeans/modules/editor/settings/storage/codetemplates/test-layer.xml"),
63
getClass().getClassLoader().getResource(
64
"org/netbeans/modules/editor/settings/storage/layer.xml"),
65
getClass().getClassLoader().getResource(
66
"org/netbeans/core/resources/mf-layer.xml"), // for MIMEResolverImpl to work
70
getClass().getClassLoader()
73
// This is here to initialize Nb URL factory (org.netbeans.core.startup),
74
// which is needed by Nb EntityCatalog (org.netbeans.core).
75
// Also see the test dependencies in project.xml
76
Main.initializeURLFactory();
79
public void testSimple() {
80
CodeTemplateSettingsImpl ctsi = CodeTemplateSettingsImpl.get(MimePath.EMPTY);
81
Map<String, CodeTemplateDescription> map = ctsi.getCodeTemplates();
83
assertNotNull("CodeTemplates map should not be null", map);
84
assertEquals("Wrong number of code templates", 3, map.size());
86
checkCodeTemplate(map, "sout", "System Out Print Line", "System.out.println(${cursor});", "not-a-real-uuid", "aaa", "bbb");
87
checkCodeTemplate(map, "hello", null, " Hello World! ", null);
88
checkCodeTemplate(map, "xyz", "xyz", "<xyz>${cursor}</xyz>", null, "xyz");
91
public void testUserChangesOverrideDefaults() {
92
MimePath mimePath = MimePath.parse("text/x-type-A");
93
CodeTemplateSettingsImpl ctsi = CodeTemplateSettingsImpl.get(mimePath);
94
Map<String, CodeTemplateDescription> map = ctsi.getCodeTemplates();
96
assertNotNull("CodeTemplates map should not be null", map);
97
assertEquals("Wrong number of code templates", 2, map.size());
99
checkCodeTemplate(map, "module1", null, "module1", null);
100
checkCodeTemplate(map, "user1", null, "user1", null);
102
CodeTemplateDescription ct = map.get("module2");
103
assertNull("'module2' code template should be removed", ct);
106
public void testSimpleWrite() {
107
String abbrev = "ct";
108
String desc = "Code Template";
109
String text = "<code-template>${cursor}";
110
String uuid = "test-uuid";
111
List<String> contexts = Arrays.asList(new String [] { "ct-ctx1" });
112
CodeTemplateDescription ct = new CodeTemplateDescription(abbrev, desc, text, contexts, uuid);
114
MimePath mimePath = MimePath.parse("text/x-empty");
115
CodeTemplateSettingsImpl ctsi = CodeTemplateSettingsImpl.get(mimePath);
117
// Write the code template
118
ctsi.setCodeTemplates(Collections.singletonMap(abbrev, ct));
120
// Force loading from the files
121
Map<String, CodeTemplateDescription> loadedMap = CodeTemplatesStorage.load(mimePath, false);
122
assertNotNull("Can't load the map", loadedMap);
123
assertEquals("Wrong number of code templates", 1, loadedMap.size());
124
checkCodeTemplate(loadedMap, abbrev, desc, text, uuid, contexts.toArray(new String [contexts.size()]));
127
public void testRemoveAll() {
128
MimePath mimePath = MimePath.parse("text/x-type-A");
129
CodeTemplateSettingsImpl ctsi = CodeTemplateSettingsImpl.get(mimePath);
130
Map<String, CodeTemplateDescription> map = ctsi.getCodeTemplates();
132
assertNotNull("CodeTemplates map should not be null", map);
133
assertTrue("Code templates map should not be empty", map.size() > 0);
135
// Remove all code templates
136
ctsi.setCodeTemplates(Collections.<String, CodeTemplateDescription>emptyMap());
138
// Force loading from the files
139
Map<String, CodeTemplateDescription> loadedMap = CodeTemplatesStorage.load(mimePath, false);
140
assertNotNull("Can't load the map", loadedMap);
141
assertEquals("Some template were not removed", 0, loadedMap.size());
144
public void testLoadLegacy() {
145
MimePath mimePath = MimePath.parse("text/x-legacy");
146
CodeTemplateSettingsImpl ctsi = CodeTemplateSettingsImpl.get(mimePath);
147
Map<String, CodeTemplateDescription> map = ctsi.getCodeTemplates();
149
assertNotNull("CodeTemplates map should not be null", map);
150
assertEquals("Wrong number of code templates", 1, map.size());
152
checkCodeTemplate(map, "tglb", null, "<%@taglib uri=\"${cursor}\"%>", null);
155
public void testMimeLookup() {
156
MimePath mimePath = MimePath.parse("text/x-type-B");
157
CodeTemplateSettings cts = MimeLookup.getLookup(mimePath).lookup(CodeTemplateSettings.class);
159
assertNotNull("No CodeTemplateSettings in MimeLookup", cts);
161
Collection<CodeTemplateDescription> codeTemplates = cts.getCodeTemplateDescriptions();
162
assertNotNull("CodeTemplates should not be null", codeTemplates);
163
assertEquals("Wrong number of code templates", 2, codeTemplates.size());
165
Map<String, CodeTemplateDescription> map = toMap(codeTemplates);
167
checkCodeTemplate(map, "module1", null, "module1", null);
168
checkCodeTemplate(map, "user1", null, "user1", null);
170
CodeTemplateDescription ct = map.get("module2");
171
assertNull("'module2' code template should be removed", ct);
174
private void checkCodeTemplate(
175
Map<String, CodeTemplateDescription> map,
182
CodeTemplateDescription ct = map.get(abbrev);
184
assertNotNull("Can't find code template for '" + abbrev + "'", ct);
185
assertEquals("Wrong abbreviation", abbrev, ct.getAbbreviation());
186
assertEquals("Wrong description", desc, ct.getDescription());
187
assertEquals("Wrong text", text, ct.getParametrizedText());
188
assertEquals("Wrong unique id", uuid, ct.getUniqueId());
190
List<String> ctxs = ct.getContexts();
191
assertEquals("Wrong number of contexts", contexts.length, ctxs.size());
193
for(String c : contexts) {
194
assertTrue("Code template does not contain context '" + c + "'", ctxs.contains(c));
198
private static Map<String, CodeTemplateDescription> toMap(Collection<CodeTemplateDescription> c) {
199
Map<String, CodeTemplateDescription> m = new HashMap<String, CodeTemplateDescription>();
200
for(CodeTemplateDescription ct : c) {
201
m.put(ct.getAbbreviation(), ct);