Browse Source

De-singleton + unit test

Change-Id: Iadcf92d4390034828d005a9c9e92d9a8ba189c99
Depends-On: I17790a0e71a6c004a088a6eb51332293b23062a4
Reviewed-on: http://gerrit.dmdirc.com/2455
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 12 years ago
parent
commit
ab45825e88

+ 2
- 1
src/com/dmdirc/GlobalWindow.java View File

136
             if (configManager.getOptionBool("general", "showglobalwindow")) {
136
             if (configManager.getOptionBool("general", "showglobalwindow")) {
137
                 if (globalWindow == null) {
137
                 if (globalWindow == null) {
138
                     globalWindow = new GlobalWindow(configManager,
138
                     globalWindow = new GlobalWindow(configManager,
139
-                            GlobalCommandParser.getGlobalCommandParser());
139
+                            new GlobalCommandParser(configManager,
140
+                            CommandManager.getCommandManager()));
140
                 }
141
                 }
141
             } else {
142
             } else {
142
                 if (globalWindow != null) {
143
                 if (globalWindow != null) {

+ 1
- 1
src/com/dmdirc/Raw.java View File

53
      */
53
      */
54
     public Raw(final Server newServer) {
54
     public Raw(final Server newServer) {
55
         super("raw", "Raw", "(Raw log)", newServer.getConfigManager(),
55
         super("raw", "Raw", "(Raw log)", newServer.getConfigManager(),
56
-                new ServerCommandParser(),
56
+                new ServerCommandParser(newServer.getConfigManager()),
57
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
57
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
58
                 WindowComponent.INPUTFIELD.getIdentifier()));
58
                 WindowComponent.INPUTFIELD.getIdentifier()));
59
 
59
 

+ 2
- 1
src/com/dmdirc/Server.java View File

194
     public Server(final URI uri, final Identity profile) {
194
     public Server(final URI uri, final Identity profile) {
195
         super("server-disconnected", getHost(uri), getHost(uri),
195
         super("server-disconnected", getHost(uri), getHost(uri),
196
                 new ConfigManager(uri.getScheme(), "", "", uri.getHost()),
196
                 new ConfigManager(uri.getScheme(), "", "", uri.getHost()),
197
-                new ServerCommandParser(),
197
+                new ServerCommandParser(
198
+                new ConfigManager(uri.getScheme(), "", "", uri.getHost())),
198
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
199
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
199
                 WindowComponent.INPUTFIELD.getIdentifier(),
200
                 WindowComponent.INPUTFIELD.getIdentifier(),
200
                 WindowComponent.CERTIFICATE_VIEWER.getIdentifier()));
201
                 WindowComponent.CERTIFICATE_VIEWER.getIdentifier()));

+ 5
- 2
src/com/dmdirc/actions/ActionModel.java View File

25
 import com.dmdirc.Precondition;
25
 import com.dmdirc.Precondition;
26
 import com.dmdirc.ServerManager;
26
 import com.dmdirc.ServerManager;
27
 import com.dmdirc.WritableFrameContainer;
27
 import com.dmdirc.WritableFrameContainer;
28
+import com.dmdirc.commandparser.CommandManager;
28
 import com.dmdirc.commandparser.parsers.CommandParser;
29
 import com.dmdirc.commandparser.parsers.CommandParser;
29
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
30
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
31
+import com.dmdirc.config.IdentityManager;
30
 import com.dmdirc.interfaces.actions.ActionType;
32
 import com.dmdirc.interfaces.actions.ActionType;
31
 
33
 
32
 import java.util.ArrayList;
34
 import java.util.ArrayList;
141
         }
143
         }
142
 
144
 
143
         WritableFrameContainer container = null;
145
         WritableFrameContainer container = null;
144
-        CommandParser cp = null;
146
+        CommandParser cp;
145
 
147
 
146
         if (arguments.length > 0
148
         if (arguments.length > 0
147
                 && arguments[0] instanceof WritableFrameContainer) {
149
                 && arguments[0] instanceof WritableFrameContainer) {
151
         }
153
         }
152
 
154
 
153
         if (container == null) {
155
         if (container == null) {
154
-            cp = GlobalCommandParser.getGlobalCommandParser();
156
+            cp = new GlobalCommandParser(IdentityManager.getIdentityManager()
157
+                    .getGlobalConfiguration(), CommandManager.getCommandManager());
155
         } else {
158
         } else {
156
             cp = container.getCommandParser();
159
             cp = container.getCommandParser();
157
         }
160
         }

+ 1
- 1
src/com/dmdirc/commandparser/CommandArguments.java View File

72
         this.line = line;
72
         this.line = line;
73
     }
73
     }
74
 
74
 
75
-   /**
75
+    /**
76
      * Creates a new command arguments parser for the specified words.
76
      * Creates a new command arguments parser for the specified words.
77
      *
77
      *
78
      * @param words The words which form the line ot be parsed
78
      * @param words The words which form the line ot be parsed

+ 7
- 4
src/com/dmdirc/commandparser/CommandManager.java View File

50
 public class CommandManager implements CommandController {
50
 public class CommandManager implements CommandController {
51
 
51
 
52
     /** A singleton instance of the command manager. */
52
     /** A singleton instance of the command manager. */
53
-    private static final CommandManager INSTANCE
54
-            = new CommandManager(IdentityManager.getIdentityManager().getGlobalConfiguration());
53
+    private static CommandManager instance;
55
 
54
 
56
     /** A list of commands that have been instantiated. */
55
     /** A list of commands that have been instantiated. */
57
     private final Map<CommandInfo, Command> commands
56
     private final Map<CommandInfo, Command> commands
282
      *
281
      *
283
      * @return A singleton instance of the CommandManager.
282
      * @return A singleton instance of the CommandManager.
284
      */
283
      */
285
-    public static CommandManager getCommandManager() {
286
-        return INSTANCE;
284
+    public static synchronized CommandManager getCommandManager() {
285
+        if (instance == null) {
286
+            instance = new CommandManager(IdentityManager.getIdentityManager()
287
+                    .getGlobalConfiguration());
288
+        }
289
+        return instance;
287
     }
290
     }
288
 
291
 
289
 }
292
 }

+ 12
- 0
src/com/dmdirc/commandparser/commands/global/Echo.java View File

35
 import com.dmdirc.commandparser.commands.flags.CommandFlag;
35
 import com.dmdirc.commandparser.commands.flags.CommandFlag;
36
 import com.dmdirc.commandparser.commands.flags.CommandFlagHandler;
36
 import com.dmdirc.commandparser.commands.flags.CommandFlagHandler;
37
 import com.dmdirc.commandparser.commands.flags.CommandFlagResult;
37
 import com.dmdirc.commandparser.commands.flags.CommandFlagResult;
38
+import com.dmdirc.interfaces.CommandController;
38
 import com.dmdirc.ui.WindowManager;
39
 import com.dmdirc.ui.WindowManager;
39
 import com.dmdirc.ui.input.AdditionalTabTargets;
40
 import com.dmdirc.ui.input.AdditionalTabTargets;
40
 
41
 
69
         handler = new CommandFlagHandler(timeStampFlag, targetFlag);
70
         handler = new CommandFlagHandler(timeStampFlag, targetFlag);
70
     }
71
     }
71
 
72
 
73
+    /**
74
+     * Creates a new instance of Echo.
75
+     *
76
+     * @param controller Command controller
77
+     */
78
+    public Echo(final CommandController controller) {
79
+        super(controller);
80
+
81
+        handler = new CommandFlagHandler(timeStampFlag, targetFlag);
82
+    }
83
+
72
     /** {@inheritDoc} */
84
     /** {@inheritDoc} */
73
     @Override
85
     @Override
74
     public void execute(final FrameContainer origin,
86
     public void execute(final FrameContainer origin,

+ 4
- 2
src/com/dmdirc/commandparser/commands/global/Ifplugin.java View File

27
 import com.dmdirc.commandparser.BaseCommandInfo;
27
 import com.dmdirc.commandparser.BaseCommandInfo;
28
 import com.dmdirc.commandparser.CommandArguments;
28
 import com.dmdirc.commandparser.CommandArguments;
29
 import com.dmdirc.commandparser.CommandInfo;
29
 import com.dmdirc.commandparser.CommandInfo;
30
+import com.dmdirc.commandparser.CommandManager;
30
 import com.dmdirc.commandparser.CommandType;
31
 import com.dmdirc.commandparser.CommandType;
31
 import com.dmdirc.commandparser.commands.Command;
32
 import com.dmdirc.commandparser.commands.Command;
32
 import com.dmdirc.commandparser.commands.IntelligentCommand;
33
 import com.dmdirc.commandparser.commands.IntelligentCommand;
72
 
73
 
73
         if (result != negative) {
74
         if (result != negative) {
74
             if (origin == null) {
75
             if (origin == null) {
75
-                GlobalCommandParser.getGlobalCommandParser().parseCommand(null,
76
-                        args.getArgumentsAsString(1));
76
+                new GlobalCommandParser(origin.getConfigManager(),
77
+                        CommandManager.getCommandManager()).parseCommand(
78
+                        null, args.getArgumentsAsString(1));
77
             } else {
79
             } else {
78
                 ((WritableFrameContainer) origin).getCommandParser()
80
                 ((WritableFrameContainer) origin).getCommandParser()
79
                         .parseCommand(origin, args.getArgumentsAsString(1));
81
                         .parseCommand(origin, args.getArgumentsAsString(1));

+ 1
- 0
src/com/dmdirc/commandparser/parsers/ChatCommandParser.java View File

56
      * @param server The server which owns this parser's container
56
      * @param server The server which owns this parser's container
57
      */
57
      */
58
     public ChatCommandParser(final Server server) {
58
     public ChatCommandParser(final Server server) {
59
+        super(server.getConfigManager());
59
         super.setOwner(server);
60
         super.setOwner(server);
60
     }
61
     }
61
 
62
 

+ 10
- 6
src/com/dmdirc/commandparser/parsers/CommandParser.java View File

35
 import com.dmdirc.commandparser.commands.CommandOptions;
35
 import com.dmdirc.commandparser.commands.CommandOptions;
36
 import com.dmdirc.commandparser.commands.ExternalCommand;
36
 import com.dmdirc.commandparser.commands.ExternalCommand;
37
 import com.dmdirc.commandparser.commands.PreviousCommand;
37
 import com.dmdirc.commandparser.commands.PreviousCommand;
38
-import com.dmdirc.config.IdentityManager;
38
+import com.dmdirc.config.ConfigManager;
39
 import com.dmdirc.util.collections.RollingList;
39
 import com.dmdirc.util.collections.RollingList;
40
 
40
 
41
 import java.io.Serializable;
41
 import java.io.Serializable;
42
 import java.util.HashMap;
42
 import java.util.HashMap;
43
 import java.util.Map;
43
 import java.util.Map;
44
 
44
 
45
+import lombok.Getter;
46
+
45
 /**
47
 /**
46
  * Represents a generic command parser. A command parser takes a line of input
48
  * Represents a generic command parser. A command parser takes a line of input
47
  * from the user, determines if it is an attempt at executing a command (based
49
  * from the user, determines if it is an attempt at executing a command (based
67
     private final RollingList<PreviousCommand> history;
69
     private final RollingList<PreviousCommand> history;
68
 
70
 
69
     /** Command manager to use. */
71
     /** Command manager to use. */
70
-    protected final CommandManager commandManager = CommandManager.getCommandManager();
72
+    @Getter
73
+    protected final CommandManager commandManager;
71
 
74
 
72
     /** Creates a new instance of CommandParser. */
75
     /** Creates a new instance of CommandParser. */
73
-    protected CommandParser() {
76
+    protected CommandParser(final ConfigManager configManager,
77
+            final CommandManager commandManager) {
74
         commands = new HashMap<String, CommandInfoPair>();
78
         commands = new HashMap<String, CommandInfoPair>();
75
-        history = new RollingList<PreviousCommand>(IdentityManager
76
-                .getIdentityManager().getGlobalConfiguration()
79
+        history = new RollingList<PreviousCommand>(configManager
77
                 .getOptionInt("general", "commandhistory"));
80
                 .getOptionInt("general", "commandhistory"));
81
+        this.commandManager = commandManager;
78
         loadCommands();
82
         loadCommands();
79
     }
83
     }
80
 
84
 
131
      */
135
      */
132
     public final void parseCommand(final FrameContainer origin,
136
     public final void parseCommand(final FrameContainer origin,
133
             final String line, final boolean parseChannel) {
137
             final String line, final boolean parseChannel) {
134
-        final CommandArguments args = new CommandArguments(line);
138
+        final CommandArguments args = new CommandArguments(getCommandManager(), line);
135
 
139
 
136
         if (args.isCommand()) {
140
         if (args.isCommand()) {
137
             if (handleChannelCommand(origin, args, parseChannel)) {
141
             if (handleChannelCommand(origin, args, parseChannel)) {

+ 10
- 14
src/com/dmdirc/commandparser/parsers/GlobalCommandParser.java View File

25
 import com.dmdirc.FrameContainer;
25
 import com.dmdirc.FrameContainer;
26
 import com.dmdirc.commandparser.CommandArguments;
26
 import com.dmdirc.commandparser.CommandArguments;
27
 import com.dmdirc.commandparser.CommandInfo;
27
 import com.dmdirc.commandparser.CommandInfo;
28
+import com.dmdirc.commandparser.CommandManager;
28
 import com.dmdirc.commandparser.CommandType;
29
 import com.dmdirc.commandparser.CommandType;
29
 import com.dmdirc.commandparser.commands.Command;
30
 import com.dmdirc.commandparser.commands.Command;
30
 import com.dmdirc.commandparser.commands.context.CommandContext;
31
 import com.dmdirc.commandparser.commands.context.CommandContext;
32
+import com.dmdirc.config.ConfigManager;
31
 import com.dmdirc.logger.ErrorLevel;
33
 import com.dmdirc.logger.ErrorLevel;
32
 import com.dmdirc.logger.Logger;
34
 import com.dmdirc.logger.Logger;
33
 
35
 
44
     private static final long serialVersionUID = 1;
46
     private static final long serialVersionUID = 1;
45
 
47
 
46
     /**
48
     /**
47
-     * The singleton instance of this command parser.
49
+     * Creates a new command parser for global commands.
50
+     *
51
+     * @param configManager Config manager to read settings from
52
+     * @param commandManager Command manager to load commands from
48
      */
53
      */
49
-    private static GlobalCommandParser me;
54
+    public GlobalCommandParser(final ConfigManager configManager,
55
+            final CommandManager commandManager) {
56
+        super(configManager, commandManager);
57
+    }
50
 
58
 
51
     /** {@inheritDoc} */
59
     /** {@inheritDoc} */
52
     @Override
60
     @Override
54
         // Don't care
62
         // Don't care
55
     }
63
     }
56
 
64
 
57
-    /**
58
-     * Retrieves a singleton instance of the global command parser.
59
-     * @return A GlobalCommandParser
60
-     */
61
-    public static synchronized GlobalCommandParser getGlobalCommandParser() {
62
-        if (me == null) {
63
-            me = new GlobalCommandParser();
64
-        }
65
-
66
-        return me;
67
-    }
68
-
69
     /** Loads the relevant commands into the parser. */
65
     /** Loads the relevant commands into the parser. */
70
     @Override
66
     @Override
71
     protected void loadCommands() {
67
     protected void loadCommands() {

+ 11
- 0
src/com/dmdirc/commandparser/parsers/ServerCommandParser.java View File

27
 import com.dmdirc.ServerState;
27
 import com.dmdirc.ServerState;
28
 import com.dmdirc.commandparser.CommandArguments;
28
 import com.dmdirc.commandparser.CommandArguments;
29
 import com.dmdirc.commandparser.CommandInfo;
29
 import com.dmdirc.commandparser.CommandInfo;
30
+import com.dmdirc.commandparser.CommandManager;
30
 import com.dmdirc.commandparser.CommandType;
31
 import com.dmdirc.commandparser.CommandType;
31
 import com.dmdirc.commandparser.commands.Command;
32
 import com.dmdirc.commandparser.commands.Command;
32
 import com.dmdirc.commandparser.commands.context.ServerCommandContext;
33
 import com.dmdirc.commandparser.commands.context.ServerCommandContext;
34
+import com.dmdirc.config.ConfigManager;
33
 
35
 
34
 /**
36
 /**
35
  * A command parser used in the context of a server.
37
  * A command parser used in the context of a server.
43
      */
45
      */
44
     private static final long serialVersionUID = 1;
46
     private static final long serialVersionUID = 1;
45
 
47
 
48
+    /**
49
+     * Creates a new command parser for server commands.
50
+     *
51
+     * @param configManager Config manager to read settings from
52
+     */
53
+    public ServerCommandParser(final ConfigManager configManager) {
54
+        super(configManager, CommandManager.getCommandManager());
55
+    }
56
+
46
     /**
57
     /**
47
      * The server instance that this parser is attached to.
58
      * The server instance that this parser is attached to.
48
      */
59
      */

+ 18
- 7
test/com/dmdirc/WritableFrameContainerTest.java View File

22
 
22
 
23
 package com.dmdirc;
23
 package com.dmdirc;
24
 
24
 
25
-import com.dmdirc.config.IdentityManager;
25
+import com.dmdirc.commandparser.CommandManager;
26
+import com.dmdirc.config.ConfigBinder;
27
+import com.dmdirc.config.ConfigManager;
26
 import com.dmdirc.config.InvalidIdentityFileException;
28
 import com.dmdirc.config.InvalidIdentityFileException;
27
 import com.dmdirc.harness.TestWritableFrameContainer;
29
 import com.dmdirc.harness.TestWritableFrameContainer;
28
 
30
 
29
 import java.util.Arrays;
31
 import java.util.Arrays;
30
 
32
 
31
-import org.junit.BeforeClass;
33
+import org.junit.Before;
32
 import org.junit.Test;
34
 import org.junit.Test;
33
 
35
 
34
 import static org.junit.Assert.*;
36
 import static org.junit.Assert.*;
37
+import static org.mockito.Mockito.*;
35
 
38
 
36
 public class WritableFrameContainerTest {
39
 public class WritableFrameContainerTest {
37
 
40
 
38
-    @BeforeClass
39
-    public static void setupClass() throws InvalidIdentityFileException {
40
-        IdentityManager.getIdentityManager().initialise();
41
+    private ConfigManager cm;
42
+    private CommandManager commands;
43
+
44
+    @Before
45
+    public void setup() throws InvalidIdentityFileException {
46
+        cm = mock(ConfigManager.class);
47
+        when(cm.getOption("general", "silencechar")).thenReturn(".");
48
+        when(cm.getOption("general", "commandchar")).thenReturn("/");
49
+        final ConfigBinder binder = new ConfigBinder(cm);
50
+        when(cm.getBinder()).thenReturn(binder);
51
+        commands = new CommandManager(cm);
41
     }
52
     }
42
 
53
 
43
     @Test
54
     @Test
44
     public void testGetNumLines() {
55
     public void testGetNumLines() {
45
         final WritableFrameContainer container10
56
         final WritableFrameContainer container10
46
-                = new TestWritableFrameContainer(10);
57
+                = new TestWritableFrameContainer(10, cm, commands);
47
 
58
 
48
         final int res0a = container10.getNumLines("");
59
         final int res0a = container10.getNumLines("");
49
         final int res0b = container10.getNumLines("\r");
60
         final int res0b = container10.getNumLines("\r");
73
     @Test
84
     @Test
74
     public void testSplitLine() {
85
     public void testSplitLine() {
75
         final WritableFrameContainer container10
86
         final WritableFrameContainer container10
76
-                = new TestWritableFrameContainer(10);
87
+                = new TestWritableFrameContainer(10, cm, commands);
77
         final String[][][] tests = new String[][][]{
88
         final String[][][] tests = new String[][][]{
78
             {{""}, {""}},
89
             {{""}, {""}},
79
             {{"0123456789"}, {"0123456789"}},
90
             {{"0123456789"}, {"0123456789"}},

+ 27
- 15
test/com/dmdirc/commandparser/parsers/CommandParserTest.java View File

23
 
23
 
24
 import com.dmdirc.commandparser.CommandManager;
24
 import com.dmdirc.commandparser.CommandManager;
25
 import com.dmdirc.commandparser.commands.global.Echo;
25
 import com.dmdirc.commandparser.commands.global.Echo;
26
-import com.dmdirc.config.IdentityManager;
26
+import com.dmdirc.config.ConfigBinder;
27
+import com.dmdirc.config.ConfigManager;
28
+import com.dmdirc.config.InvalidIdentityFileException;
27
 import com.dmdirc.harness.TestCommandParser;
29
 import com.dmdirc.harness.TestCommandParser;
28
 
30
 
29
-import org.junit.BeforeClass;
31
+import org.junit.Before;
30
 import org.junit.Test;
32
 import org.junit.Test;
31
 
33
 
32
 import static org.junit.Assert.*;
34
 import static org.junit.Assert.*;
35
+import static org.mockito.Mockito.*;
33
 
36
 
34
 public class CommandParserTest {
37
 public class CommandParserTest {
35
 
38
 
36
-    @BeforeClass
37
-    public static void setUpClass() throws Exception {
38
-        IdentityManager.getIdentityManager().initialise();
39
-        CommandManager.getCommandManager().registerCommand(new Echo(), Echo.INFO);
39
+    private ConfigManager cm;
40
+    private CommandManager commands;
41
+
42
+    @Before
43
+    public void setup() throws InvalidIdentityFileException {
44
+        cm = mock(ConfigManager.class);
45
+        when(cm.getOptionChar("general", "silencechar")).thenReturn('.');
46
+        when(cm.getOptionInt("general", "commandhistory")).thenReturn(10);
47
+        when(cm.getOptionChar("general", "commandchar")).thenReturn('/');
48
+        final ConfigBinder binder = new ConfigBinder(cm);
49
+        when(cm.getBinder()).thenReturn(binder);
50
+        commands = new CommandManager(cm);
51
+        commands.registerCommand(new Echo(commands), Echo.INFO);
40
     }
52
     }
41
 
53
 
42
     @Test
54
     @Test
43
     public void testBasicCommand() {
55
     public void testBasicCommand() {
44
-        final TestCommandParser tcp = new TestCommandParser();
56
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
45
         tcp.parseCommand(null, "/echo this is a test");
57
         tcp.parseCommand(null, "/echo this is a test");
46
 
58
 
47
         assertNull(tcp.nonCommandLine);
59
         assertNull(tcp.nonCommandLine);
53
 
65
 
54
     @Test
66
     @Test
55
     public void testBasicNoArgs() {
67
     public void testBasicNoArgs() {
56
-        final TestCommandParser tcp = new TestCommandParser();
68
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
57
         tcp.parseCommand(null, "/echo");
69
         tcp.parseCommand(null, "/echo");
58
 
70
 
59
         assertNull(tcp.nonCommandLine);
71
         assertNull(tcp.nonCommandLine);
65
 
77
 
66
     @Test
78
     @Test
67
     public void testSilentNoArgs() {
79
     public void testSilentNoArgs() {
68
-        final TestCommandParser tcp = new TestCommandParser();
80
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
69
         tcp.parseCommand(null, "/.echo");
81
         tcp.parseCommand(null, "/.echo");
70
 
82
 
71
         assertNull(tcp.nonCommandLine);
83
         assertNull(tcp.nonCommandLine);
77
 
89
 
78
     @Test
90
     @Test
79
     public void testSilentCommand() {
91
     public void testSilentCommand() {
80
-        final TestCommandParser tcp = new TestCommandParser();
92
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
81
         tcp.parseCommand(null, "/.echo this is a test");
93
         tcp.parseCommand(null, "/.echo this is a test");
82
 
94
 
83
         assertNull(tcp.nonCommandLine);
95
         assertNull(tcp.nonCommandLine);
89
 
101
 
90
     @Test
102
     @Test
91
     public void testNonExistantCommand() {
103
     public void testNonExistantCommand() {
92
-        final TestCommandParser tcp = new TestCommandParser();
104
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
93
         tcp.parseCommand(null, "/foobar moo bar");
105
         tcp.parseCommand(null, "/foobar moo bar");
94
 
106
 
95
         assertNull(tcp.nonCommandLine);
107
         assertNull(tcp.nonCommandLine);
101
 
113
 
102
     @Test
114
     @Test
103
     public void testEmptyCommand() {
115
     public void testEmptyCommand() {
104
-        final TestCommandParser tcp = new TestCommandParser();
116
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
105
         tcp.parseCommand(null, "/ moo bar");
117
         tcp.parseCommand(null, "/ moo bar");
106
 
118
 
107
         assertNull(tcp.nonCommandLine);
119
         assertNull(tcp.nonCommandLine);
113
 
125
 
114
     @Test
126
     @Test
115
     public void testEmptySilentCommand() {
127
     public void testEmptySilentCommand() {
116
-        final TestCommandParser tcp = new TestCommandParser();
128
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
117
         tcp.parseCommand(null, "/. moo bar");
129
         tcp.parseCommand(null, "/. moo bar");
118
 
130
 
119
         assertNull(tcp.nonCommandLine);
131
         assertNull(tcp.nonCommandLine);
125
 
137
 
126
     @Test
138
     @Test
127
     public void testNonCommand() {
139
     public void testNonCommand() {
128
-        final TestCommandParser tcp = new TestCommandParser();
140
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
129
         tcp.parseCommand(null, "Foobar baz");
141
         tcp.parseCommand(null, "Foobar baz");
130
 
142
 
131
         assertNotNull(tcp.nonCommandLine);
143
         assertNotNull(tcp.nonCommandLine);
137
 
149
 
138
     @Test
150
     @Test
139
     public void testCommandHistory() {
151
     public void testCommandHistory() {
140
-        final TestCommandParser tcp = new TestCommandParser();
152
+        final TestCommandParser tcp = new TestCommandParser(cm, commands);
141
         tcp.parseCommand(null, "/echo this is a test");
153
         tcp.parseCommand(null, "/echo this is a test");
142
 
154
 
143
         final long time1 = tcp.getCommandTime("echo this is a test");
155
         final long time1 = tcp.getCommandTime("echo this is a test");

+ 0
- 48
test/com/dmdirc/commandparser/parsers/GlobalCommandParserTest.java View File

1
-/*
2
- * Copyright (c) 2006-2012 DMDirc Developers
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in
12
- * all copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
-package com.dmdirc.commandparser.parsers;
24
-
25
-import com.dmdirc.config.IdentityManager;
26
-
27
-import org.junit.BeforeClass;
28
-import org.junit.Test;
29
-
30
-import static org.junit.Assert.*;
31
-
32
-public class GlobalCommandParserTest {
33
-
34
-    @BeforeClass
35
-    public static void setUp() throws Exception {
36
-        IdentityManager.getIdentityManager().initialise();
37
-    }
38
-
39
-    @Test
40
-    public void testGetGlobalCommandParser() {
41
-        final GlobalCommandParser p1 = GlobalCommandParser.getGlobalCommandParser();
42
-
43
-        final GlobalCommandParser p2 = GlobalCommandParser.getGlobalCommandParser();
44
-
45
-        assertSame(p1, p2);
46
-    }
47
-
48
-}

+ 12
- 1
test/com/dmdirc/harness/TestCommandParser.java View File

29
 import com.dmdirc.commandparser.CommandType;
29
 import com.dmdirc.commandparser.CommandType;
30
 import com.dmdirc.commandparser.commands.Command;
30
 import com.dmdirc.commandparser.commands.Command;
31
 import com.dmdirc.commandparser.parsers.*;
31
 import com.dmdirc.commandparser.parsers.*;
32
+import com.dmdirc.config.ConfigManager;
33
+
34
+import lombok.ToString;
32
 
35
 
33
 public class TestCommandParser extends CommandParser {
36
 public class TestCommandParser extends CommandParser {
34
     private static final long serialVersionUID = 7073002401375438532L;
37
     private static final long serialVersionUID = 7073002401375438532L;
35
 
38
 
39
+    private final ConfigManager configManager;
40
+
36
     public String nonCommandLine;
41
     public String nonCommandLine;
37
 
42
 
38
     public Command executedCommand;
43
     public Command executedCommand;
43
 
48
 
44
     public String invalidCommand;
49
     public String invalidCommand;
45
 
50
 
51
+    public TestCommandParser(final ConfigManager configManager,
52
+            final CommandManager commandManager) {
53
+        super(configManager, commandManager);
54
+        this.configManager = configManager;
55
+    }
56
+
46
     @Override
57
     @Override
47
     protected void loadCommands() {
58
     protected void loadCommands() {
48
-        CommandManager.getCommandManager().loadCommands(this, CommandType.TYPE_GLOBAL);
59
+        commandManager.loadCommands(this, CommandType.TYPE_GLOBAL);
49
     }
60
     }
50
 
61
 
51
     @Override
62
     @Override

+ 5
- 9
test/com/dmdirc/harness/TestWritableFrameContainer.java View File

24
 
24
 
25
 import com.dmdirc.Server;
25
 import com.dmdirc.Server;
26
 import com.dmdirc.WritableFrameContainer;
26
 import com.dmdirc.WritableFrameContainer;
27
+import com.dmdirc.commandparser.CommandManager;
27
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
28
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
28
 import com.dmdirc.config.ConfigManager;
29
 import com.dmdirc.config.ConfigManager;
29
-import com.dmdirc.config.IdentityManager;
30
 import com.dmdirc.ui.input.TabCompleter;
30
 import com.dmdirc.ui.input.TabCompleter;
31
 
31
 
32
 import java.util.Collections;
32
 import java.util.Collections;
36
     private final int lineLength;
36
     private final int lineLength;
37
 
37
 
38
     public TestWritableFrameContainer(final int lineLength,
38
     public TestWritableFrameContainer(final int lineLength,
39
-            final ConfigManager cm) {
39
+            final ConfigManager cm, final CommandManager commandManager) {
40
         super("raw", "Raw", "(Raw)", cm,
40
         super("raw", "Raw", "(Raw)", cm,
41
-                GlobalCommandParser.getGlobalCommandParser(),
41
+                new GlobalCommandParser(cm, commandManager),
42
                 Collections.<String>emptySet());
42
                 Collections.<String>emptySet());
43
 
43
 
44
         this.lineLength = lineLength;
44
         this.lineLength = lineLength;
45
     }
45
     }
46
 
46
 
47
-    public TestWritableFrameContainer(final int lineLength) {
48
-        this(lineLength, IdentityManager.getIdentityManager().getGlobalConfiguration());
49
-    }
50
-
51
     @Override
47
     @Override
52
     public void sendLine(final String line) {
48
     public void sendLine(final String line) {
53
         // Do nothing
49
         // Do nothing
60
 
56
 
61
     @Override
57
     @Override
62
     public void windowClosing() {
58
     public void windowClosing() {
63
-        System.out.println("windowClosing");
59
+        // Do nothing
64
     }
60
     }
65
 
61
 
66
     @Override
62
     @Override
67
     public void windowClosed() {
63
     public void windowClosed() {
68
-        // DO nothing
64
+        // Do nothing
69
     }
65
     }
70
 
66
 
71
     @Override
67
     @Override

+ 21
- 13
test/com/dmdirc/ui/WindowManagerTest.java View File

24
 import com.dmdirc.CustomWindow;
24
 import com.dmdirc.CustomWindow;
25
 import com.dmdirc.FrameContainer;
25
 import com.dmdirc.FrameContainer;
26
 import com.dmdirc.addons.ui_dummy.DummyInputWindow;
26
 import com.dmdirc.addons.ui_dummy.DummyInputWindow;
27
-import com.dmdirc.config.IdentityManager;
27
+import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.config.ConfigBinder;
29
+import com.dmdirc.config.ConfigManager;
28
 import com.dmdirc.config.InvalidIdentityFileException;
30
 import com.dmdirc.config.InvalidIdentityFileException;
29
 import com.dmdirc.harness.TestWritableFrameContainer;
31
 import com.dmdirc.harness.TestWritableFrameContainer;
30
 import com.dmdirc.interfaces.ui.FrameListener;
32
 import com.dmdirc.interfaces.ui.FrameListener;
33
 import java.util.Arrays;
35
 import java.util.Arrays;
34
 import java.util.Collection;
36
 import java.util.Collection;
35
 
37
 
36
-import org.junit.BeforeClass;
38
+import org.junit.Before;
37
 import org.junit.Test;
39
 import org.junit.Test;
38
 
40
 
39
 import static org.junit.Assert.*;
41
 import static org.junit.Assert.*;
41
 
43
 
42
 public class WindowManagerTest {
44
 public class WindowManagerTest {
43
 
45
 
44
-    @BeforeClass
45
-    public static void setupClass() throws InvalidIdentityFileException {
46
-        IdentityManager.getIdentityManager().initialise();
46
+    private ConfigManager cm;
47
+    private CommandManager commands;
48
+
49
+    @Before
50
+    public void setup() throws InvalidIdentityFileException {
51
+        cm = mock(ConfigManager.class);
52
+        final ConfigBinder binder = new ConfigBinder(cm);
53
+        when(cm.getBinder()).thenReturn(binder);
54
+        commands = new CommandManager(cm);
47
     }
55
     }
48
 
56
 
49
     @Test
57
     @Test
50
     public void testAddRoot() {
58
     public void testAddRoot() {
51
         final WindowManager manager = new WindowManager();
59
         final WindowManager manager = new WindowManager();
52
         final FrameListener tfm = mock(FrameListener.class);
60
         final FrameListener tfm = mock(FrameListener.class);
53
-        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512));
61
+        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
54
 
62
 
55
         manager.addListener(tfm);
63
         manager.addListener(tfm);
56
 
64
 
67
     public void testAddChild() {
75
     public void testAddChild() {
68
         final WindowManager manager = new WindowManager();
76
         final WindowManager manager = new WindowManager();
69
         final FrameListener tfm = mock(FrameListener.class);
77
         final FrameListener tfm = mock(FrameListener.class);
70
-        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512));
71
-        final Window child = new DummyInputWindow(new TestWritableFrameContainer(512));
78
+        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
79
+        final Window child = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
72
         manager.addWindow(parent.getContainer());
80
         manager.addWindow(parent.getContainer());
73
         manager.addListener(tfm);
81
         manager.addListener(tfm);
74
 
82
 
82
     public void testRemoveRoot() {
90
     public void testRemoveRoot() {
83
         final WindowManager manager = new WindowManager();
91
         final WindowManager manager = new WindowManager();
84
         final FrameListener tfm = mock(FrameListener.class);
92
         final FrameListener tfm = mock(FrameListener.class);
85
-        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512));
93
+        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
86
         manager.addWindow(parent.getContainer());
94
         manager.addWindow(parent.getContainer());
87
         manager.addListener(tfm);
95
         manager.addListener(tfm);
88
 
96
 
95
     public void testRemoveChild() {
103
     public void testRemoveChild() {
96
         final WindowManager manager = new WindowManager();
104
         final WindowManager manager = new WindowManager();
97
         final FrameListener tfm = mock(FrameListener.class);
105
         final FrameListener tfm = mock(FrameListener.class);
98
-        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512));
99
-        final Window child = new DummyInputWindow(new TestWritableFrameContainer(512));
106
+        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
107
+        final Window child = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
100
         manager.addWindow(parent.getContainer());
108
         manager.addWindow(parent.getContainer());
101
         manager.addWindow(parent.getContainer(), child.getContainer());
109
         manager.addWindow(parent.getContainer(), child.getContainer());
102
         manager.addListener(tfm);
110
         manager.addListener(tfm);
113
     public void testRemoveFrameManager() {
121
     public void testRemoveFrameManager() {
114
         final WindowManager manager = new WindowManager();
122
         final WindowManager manager = new WindowManager();
115
         final FrameListener tfm = mock(FrameListener.class);
123
         final FrameListener tfm = mock(FrameListener.class);
116
-        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512));
117
-        final Window child = new DummyInputWindow(new TestWritableFrameContainer(512));
124
+        final Window parent = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
125
+        final Window child = new DummyInputWindow(new TestWritableFrameContainer(512, cm, commands));
118
         manager.addWindow(parent.getContainer());
126
         manager.addWindow(parent.getContainer());
119
 
127
 
120
         manager.addListener(tfm);
128
         manager.addListener(tfm);

Loading…
Cancel
Save