KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > macros > MacrosModelTest


1 /*
2  * MacrosModelTest.java
3  * JUnit based test
4  *
5  * Created on 27. říjen 2005, 12:28
6  */

7
8 package org.netbeans.modules.options.macros;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Vector JavaDoc;
15 import junit.framework.TestCase;
16 import org.netbeans.modules.options.editor.IDEInitializer;
17 import org.netbeans.modules.options.keymap.KeymapViewModel;
18 import org.openide.util.lookup.Lookups;
19
20
21 /**
22  *
23  * @author Administrator
24  */

25 public class MacrosModelTest extends TestCase {
26     
27     static {
28         IDEInitializer.setup (
29             new String JavaDoc[] {
30                 "org/netbeans/modules/options/editor/mf-layer.xml",
31                 "org/netbeans/modules/java/editor/resources/layer.xml",
32                 "org/netbeans/modules/defaults/mf-layer.xml",
33                 "org/netbeans/core/ui/resources/layer.xml"
34             },
35             new Object JavaDoc[] {}
36         );
37     }
38     
39     public MacrosModelTest (String JavaDoc testName) {
40         super (testName);
41     }
42     
43     public void testAddMacro () {
44         IDEInitializer.cleanWorkDir ();
45         
46         // 1) init model
47
MacrosModel model = new MacrosModel (
48             Lookups.singleton (new KeymapViewModel ())
49         );
50         Iterator JavaDoc it = model.getMacroNames ().iterator ();
51         Map JavaDoc macros = readMacros (model);
52         Vector JavaDoc original = clone (model.getShortcutsTableModel ().getDataVector ());
53         
54         // 2) do some changes
55
model.addMacro ("testName", "testValue");
56         model.setShortcut (getIndex (model, "testName"), "Alt+Shift+H");
57         model.addMacro ("testName2", "testValue2");
58         model.setShortcut (getIndex (model, "testName2"), "Alt+Shift+R");
59         
60         // 3) test changes
61
assertFalse (original.equals (model.getShortcutsTableModel ().getDataVector ()));
62         assertEquals (original.size () + 2, model.getShortcutsTableModel ().getDataVector ().size ());
63         assertEquals (original.size () + 2, model.getMacroNames ().size ());
64         assertEquals ("testValue", model.getMacroText ("testName"));
65         assertEquals ("testValue2", model.getMacroText ("testName2"));
66         assertEquals ("Alt+Shift+H", getShortcut (model, "testName"));
67         assertEquals ("Alt+Shift+R", getShortcut (model, "testName2"));
68         
69         model.applyChanges ();
70         
71         // 3) test changes
72
assertFalse (original.equals (model.getShortcutsTableModel ().getDataVector ()));
73         assertEquals (original.size () + 2, model.getShortcutsTableModel ().getDataVector ().size ());
74         assertEquals (original.size () + 2, model.getMacroNames ().size ());
75         assertEquals ("testValue", model.getMacroText ("testName"));
76         assertEquals ("testValue2", model.getMacroText ("testName2"));
77         assertEquals ("Alt+Shift+H", getShortcut (model, "testName"));
78         assertEquals ("Alt+Shift+R", getShortcut (model, "testName2"));
79         
80         model = new MacrosModel (
81             Lookups.singleton (new KeymapViewModel ())
82         );
83         
84         // 3) test changes
85
assertFalse (original.equals (model.getShortcutsTableModel ().getDataVector ()));
86         assertEquals (original.size () + 2, model.getShortcutsTableModel ().getDataVector ().size ());
87         assertEquals (original.size () + 2, model.getMacroNames ().size ());
88         assertEquals ("testValue", model.getMacroText ("testName"));
89         assertEquals ("testValue2", model.getMacroText ("testName2"));
90         assertEquals ("Alt+Shift+H", getShortcut (model, "testName"));
91         assertEquals ("Alt+Shift+R", getShortcut (model, "testName2"));
92     }
93     
94     private String JavaDoc getShortcut (MacrosModel model, String JavaDoc macroName) {
95         Iterator JavaDoc it = model.getShortcutsTableModel ().getDataVector ().
96             iterator ();
97         while (it.hasNext ()) {
98             Vector JavaDoc line = (Vector JavaDoc) it.next ();
99             if (line.get (0).equals (macroName))
100                 return (String JavaDoc) line.get (1);
101         }
102         return null;
103     }
104     
105     private Vector JavaDoc clone (Vector JavaDoc v) {
106         Iterator JavaDoc it = v.iterator ();
107         Vector JavaDoc result = new Vector JavaDoc ();
108         while (it.hasNext ()) {
109             Vector JavaDoc line = (Vector JavaDoc) it.next ();
110            result.add (line.clone ());
111         }
112         return result;
113     }
114     
115     private int getIndex (MacrosModel model, String JavaDoc macroName) {
116         Vector JavaDoc data = model.getShortcutsTableModel ().getDataVector ();
117         int i, k = data.size ();
118         for (i = 0; i < k; i++) {
119             Vector JavaDoc line = (Vector JavaDoc) data.get (i);
120             if (macroName.equals (line.get (0))) return i;
121         }
122         return -1;
123     }
124     
125     private Map JavaDoc readMacros (MacrosModel model) {
126         Iterator JavaDoc it = model.getMacroNames ().iterator ();
127         Map JavaDoc macros = new HashMap JavaDoc ();
128         while (it.hasNext ()) {
129             String JavaDoc macroName = (String JavaDoc) it.next ();
130             macros.put (
131                 macroName,
132                 model.getMacroText (macroName)
133             );
134         }
135         return macros;
136     }
137 }
138
Popular Tags