1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.console.swingui;
23
24 import java.awt.BorderLayout;
25 import java.io.File;
26 import java.io.FileFilter;
27
28 import javax.swing.JFrame;
29 import javax.swing.JLabel;
30 import javax.swing.JPopupMenu;
31 import javax.swing.SwingUtilities;
32
33 import net.grinder.console.common.ErrorHandler;
34 import net.grinder.console.common.Resources;
35 import net.grinder.console.common.ResourcesImplementation;
36 import net.grinder.console.distribution.AgentCacheState;
37 import net.grinder.console.distribution.FileChangeWatcher;
38 import net.grinder.console.editor.Buffer;
39 import net.grinder.console.editor.EditorModel;
40 import net.grinder.console.editor.StringTextSource;
41 import net.grinder.console.editor.TextSource;
42 import net.grinder.console.editor.StringTextSource.Factory;
43 import net.grinder.testutility.AbstractFileTestCase;
44 import net.grinder.testutility.DelegatingStubFactory;
45 import net.grinder.testutility.RandomStubFactory;
46
47
48 public class TestFileTree extends AbstractFileTestCase {
49 private static final Resources s_resources =
50 new ResourcesImplementation(
51 "net.grinder.console.common.resources.Console");
52 private final RandomStubFactory<ErrorHandler> m_errorHandlerStubFactory =
53 RandomStubFactory.create(ErrorHandler.class);
54 private final ErrorHandler m_errorHandler =
55 m_errorHandlerStubFactory.getStub();
56
57 private final StringTextSource.Factory m_stringTextSourceFactory =
58 new StringTextSource.Factory();
59 private final DelegatingStubFactory<Factory>
60 m_textSourceFactoryStubFactory =
61 DelegatingStubFactory.create(m_stringTextSourceFactory);
62 private final TextSource.Factory m_textSourceFactory =
63 m_textSourceFactoryStubFactory.getStub();
64 private final RandomStubFactory<AgentCacheState>
65 m_agentCacheStateStubFactory =
66 RandomStubFactory.create(AgentCacheState.class);
67 private final AgentCacheState m_agentCacheState =
68 m_agentCacheStateStubFactory.getStub();
69 private final RandomStubFactory<FileChangeWatcher>
70 m_fileChangeWatcherStubFactory =
71 RandomStubFactory.create(FileChangeWatcher.class);
72 private final FileChangeWatcher m_fileChangeWatcher =
73 m_fileChangeWatcherStubFactory.getStub();
74
75 private final FileFilter m_nullFileFilter = new FileFilter() {
76 public boolean accept(File pathname) {
77 return true;
78 }
79 };
80
81 public void testConstruction() throws Exception {
82 final EditorModel editorModel = new EditorModel(s_resources,
83 m_textSourceFactory,
84 m_agentCacheState,
85 m_fileChangeWatcher);
86
87 final BufferTreeModel bufferTreeModel = new BufferTreeModel(editorModel);
88 final FileTreeModel fileTreeModel =
89 new FileTreeModel(editorModel, m_nullFileFilter, new File("c:"));
90
91 final FileTree fileTree = new FileTree(s_resources,
92 m_errorHandler, editorModel, bufferTreeModel, fileTreeModel,
93 new JLabel().getFont(), new JPopupMenu(), null);
94
95
96 SwingUtilities.updateComponentTreeUI(fileTree.getComponent());
97
98 assertNotNull(fileTree.getActions());
99 }
100
101 public void testEditorModelListener() throws Exception {
102 final EditorModel editorModel = new EditorModel(s_resources,
103 m_textSourceFactory,
104 m_agentCacheState,
105 m_fileChangeWatcher);
106
107 final BufferTreeModel bufferTreeModel = new BufferTreeModel(editorModel);
108 final FileTreeModel fileTreeModel =
109 new FileTreeModel(editorModel, m_nullFileFilter, new File("c:"));
110
111 new FileTree(s_resources, m_errorHandler, editorModel, bufferTreeModel,
112 fileTreeModel, new JLabel().getFont(), new JPopupMenu(), null);
113
114
115 editorModel.selectNewBuffer();
116 editorModel.getSelectedBuffer().getTextSource().setText("Foo");
117 editorModel.closeBuffer(editorModel.getSelectedBuffer());
118
119
120 final File f1 = new File(getDirectory(), "file1");
121 assertTrue(f1.createNewFile());
122 final File f2 = new File(getDirectory(), "file2");
123 assertTrue(f2.createNewFile());
124 final Buffer buffer = editorModel.selectBufferForFile(f1);
125 buffer.save(f2);
126 editorModel.closeBuffer(buffer);
127
128 final Buffer buffer2 = editorModel.selectBufferForFile(f1);
129 fileTreeModel.setRootDirectory(getDirectory());
130 fileTreeModel.refresh();
131
132 editorModel.selectBufferForFile(f1);
133 buffer2.save(f2);
134 editorModel.selectBuffer(buffer2);
135
136 buffer2.save(f1);
137 fileTreeModel.refresh();
138 editorModel.selectBuffer(buffer2);
139
140 editorModel.selectBufferForFile(f2);
141 editorModel.closeBuffer(buffer2);
142 }
143
144 public void testDisplay() throws Exception {
145 final EditorModel editorModel = new EditorModel(s_resources,
146 m_textSourceFactory,
147 m_agentCacheState,
148 m_fileChangeWatcher);
149
150 final BufferTreeModel bufferTreeModel = new BufferTreeModel(editorModel);
151 final FileTreeModel fileTreeModel =
152 new FileTreeModel(editorModel, m_nullFileFilter, getDirectory());
153
154 final FileTree fileTree =
155 new FileTree(s_resources, m_errorHandler, editorModel,
156 bufferTreeModel, fileTreeModel, new JLabel().getFont(),
157 new JPopupMenu(), null);
158
159 final JFrame frame = new JFrame();
160
161 frame.getContentPane().add(fileTree.getComponent(), BorderLayout.CENTER);
162 frame.pack();
163 frame.setVisible(true);
164 frame.dispose();
165 }
166 }