~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to editor/settings/storage/test/unit/src/org/netbeans/modules/editor/settings/storage/codetemplates/CodeTemplateSettingsImplTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
 
27
 */
 
28
package org.netbeans.modules.editor.settings.storage.codetemplates;
 
29
 
 
30
import java.net.URL;
 
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;
 
36
import java.util.Map;
 
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;
 
44
 
 
45
/**
 
46
 *
 
47
 * @author vita
 
48
 */
 
49
public class CodeTemplateSettingsImplTest extends NbTestCase {
 
50
 
 
51
    public CodeTemplateSettingsImplTest(String name) {
 
52
        super(name);
 
53
    }
 
54
 
 
55
    @Override
 
56
    protected void setUp() throws Exception {
 
57
        super.setUp();
 
58
    
 
59
        EditorTestLookup.setLookup(
 
60
            new URL[] {
 
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
 
67
            },
 
68
            getWorkDir(),
 
69
            new Object[] {},
 
70
            getClass().getClassLoader()
 
71
        );
 
72
 
 
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();
 
77
    }
 
78
 
 
79
    public void testSimple() {
 
80
        CodeTemplateSettingsImpl ctsi = CodeTemplateSettingsImpl.get(MimePath.EMPTY);
 
81
        Map<String, CodeTemplateDescription> map = ctsi.getCodeTemplates();
 
82
        
 
83
        assertNotNull("CodeTemplates map should not be null", map);
 
84
        assertEquals("Wrong number of code templates", 3, map.size());
 
85
        
 
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");
 
89
    }
 
90
 
 
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();
 
95
        
 
96
        assertNotNull("CodeTemplates map should not be null", map);
 
97
        assertEquals("Wrong number of code templates", 2, map.size());
 
98
        
 
99
        checkCodeTemplate(map, "module1", null, "module1", null);
 
100
        checkCodeTemplate(map, "user1", null, "user1", null);
 
101
        
 
102
        CodeTemplateDescription ct = map.get("module2");
 
103
        assertNull("'module2' code template should be removed", ct);
 
104
    }
 
105
    
 
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);
 
113
        
 
114
        MimePath mimePath = MimePath.parse("text/x-empty");
 
115
        CodeTemplateSettingsImpl ctsi = CodeTemplateSettingsImpl.get(mimePath);
 
116
        
 
117
        // Write the code template
 
118
        ctsi.setCodeTemplates(Collections.singletonMap(abbrev, ct));
 
119
        
 
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()]));
 
125
    }
 
126
 
 
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();
 
131
        
 
132
        assertNotNull("CodeTemplates map should not be null", map);
 
133
        assertTrue("Code templates map should not be empty", map.size() > 0);
 
134
        
 
135
        // Remove all code templates
 
136
        ctsi.setCodeTemplates(Collections.<String, CodeTemplateDescription>emptyMap());
 
137
        
 
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());
 
142
    }
 
143
    
 
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();
 
148
        
 
149
        assertNotNull("CodeTemplates map should not be null", map);
 
150
        assertEquals("Wrong number of code templates", 1, map.size());
 
151
        
 
152
        checkCodeTemplate(map, "tglb", null, "<%@taglib uri=\"${cursor}\"%>", null);
 
153
    }
 
154
    
 
155
    public void testMimeLookup() {
 
156
        MimePath mimePath = MimePath.parse("text/x-type-B");
 
157
        CodeTemplateSettings cts = MimeLookup.getLookup(mimePath).lookup(CodeTemplateSettings.class);
 
158
        
 
159
        assertNotNull("No CodeTemplateSettings in MimeLookup", cts);
 
160
        
 
161
        Collection<CodeTemplateDescription> codeTemplates = cts.getCodeTemplateDescriptions();
 
162
        assertNotNull("CodeTemplates should not be null", codeTemplates);
 
163
        assertEquals("Wrong number of code templates", 2, codeTemplates.size());
 
164
        
 
165
        Map<String, CodeTemplateDescription> map = toMap(codeTemplates);
 
166
        
 
167
        checkCodeTemplate(map, "module1", null, "module1", null);
 
168
        checkCodeTemplate(map, "user1", null, "user1", null);
 
169
        
 
170
        CodeTemplateDescription ct = map.get("module2");
 
171
        assertNull("'module2' code template should be removed", ct);
 
172
    }
 
173
    
 
174
    private void checkCodeTemplate(
 
175
        Map<String, CodeTemplateDescription> map, 
 
176
        String abbrev, 
 
177
        String desc, 
 
178
        String text, 
 
179
        String uuid,
 
180
        String... contexts
 
181
    ) {
 
182
        CodeTemplateDescription ct = map.get(abbrev);
 
183
        
 
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());
 
189
        
 
190
        List<String> ctxs = ct.getContexts();
 
191
        assertEquals("Wrong number of contexts", contexts.length, ctxs.size());
 
192
        
 
193
        for(String c : contexts) {
 
194
            assertTrue("Code template does not contain context '" + c + "'", ctxs.contains(c));
 
195
        }
 
196
    }
 
197
    
 
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);
 
202
        }
 
203
        return m;
 
204
    }
 
205
}