Browse Source

Pass IdentityManager into commands that use it.

Change-Id: Ieb89624746da92690df22b0938384141f42de8dd
Reviewed-on: http://gerrit.dmdirc.com/2676
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10 years ago
parent
commit
68ce22ff7d

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

@@ -134,7 +134,7 @@ public class Main implements LifecycleController {
134 134
 
135 135
         clp.applySettings();
136 136
 
137
-        new CommandLoader(this, serverManager, pluginManager)
137
+        new CommandLoader(this, serverManager, pluginManager, IdentityManager.getIdentityManager())
138 138
                 .loadCommands(CommandManager.initCommandManager(IdentityManager.getIdentityManager(), serverManager));
139 139
 
140 140
         for (String service : new String[]{"ui", "tabcompletion", "parser"}) {

+ 12
- 5
src/com/dmdirc/commandparser/CommandLoader.java View File

@@ -67,6 +67,7 @@ import com.dmdirc.commandparser.commands.server.Raw;
67 67
 import com.dmdirc.commandparser.commands.server.RawServerCommand;
68 68
 import com.dmdirc.commandparser.commands.server.Reconnect;
69 69
 import com.dmdirc.commandparser.commands.server.Umode;
70
+import com.dmdirc.config.IdentityManager;
70 71
 import com.dmdirc.interfaces.CommandController;
71 72
 import com.dmdirc.plugins.PluginManager;
72 73
 
@@ -84,20 +85,26 @@ public class CommandLoader {
84 85
     /** Plugin manager to give to plugin-dependent commands. */
85 86
     private final PluginManager pluginManager;
86 87
 
88
+    /** The identity manager to pass to config-related commands. */
89
+    private final IdentityManager identityManager;
90
+
87 91
     /**
88 92
      * Creates a new instance of {@link CommandLoader}.
89 93
      *
90 94
      * @param lifecycleController Controller to give to commands that want to start/stop the app.
91 95
      * @param serverManager The server manager to pass to server-related commands.
92 96
      * @param pluginManager The plugin manager to pass to plugin-dependent commands.
97
+     * @param identityManager The identity manager to pass to config-related commands.
93 98
      */
94 99
     public CommandLoader(
95 100
             final LifecycleController lifecycleController,
96 101
             final ServerManager serverManager,
97
-            final PluginManager pluginManager) {
102
+            final PluginManager pluginManager,
103
+            final IdentityManager identityManager) {
98 104
         this.lifecycleController = lifecycleController;
99 105
         this.serverManager = serverManager;
100 106
         this.pluginManager = pluginManager;
107
+        this.identityManager = identityManager;
101 108
     }
102 109
 
103 110
     /**
@@ -154,16 +161,16 @@ public class CommandLoader {
154 161
         manager.registerCommand(new Exit(lifecycleController), Exit.INFO);
155 162
         manager.registerCommand(new Help(), Help.INFO);
156 163
         manager.registerCommand(new Ifplugin(pluginManager), Ifplugin.INFO);
157
-        manager.registerCommand(new NewServer(serverManager, pluginManager), NewServer.INFO);
164
+        manager.registerCommand(new NewServer(serverManager, pluginManager, identityManager), NewServer.INFO);
158 165
         manager.registerCommand(new Notify(), Notify.INFO);
159 166
         manager.registerCommand(new LoadPlugin(pluginManager), LoadPlugin.INFO);
160 167
         manager.registerCommand(new UnloadPlugin(pluginManager), UnloadPlugin.INFO);
161 168
         manager.registerCommand(new OpenWindow(), OpenWindow.INFO);
162 169
         manager.registerCommand(new ReloadActions(), ReloadActions.INFO);
163
-        manager.registerCommand(new ReloadIdentities(), ReloadIdentities.INFO);
170
+        manager.registerCommand(new ReloadIdentities(identityManager), ReloadIdentities.INFO);
164 171
         manager.registerCommand(new ReloadPlugin(pluginManager), ReloadPlugin.INFO);
165
-        manager.registerCommand(new SaveConfig(), SaveConfig.INFO);
166
-        manager.registerCommand(new Set(), Set.INFO);
172
+        manager.registerCommand(new SaveConfig(identityManager), SaveConfig.INFO);
173
+        manager.registerCommand(new Set(identityManager, identityManager), Set.INFO);
167 174
     }
168 175
 
169 176
 }

+ 10
- 5
src/com/dmdirc/commandparser/commands/global/NewServer.java View File

@@ -31,7 +31,7 @@ import com.dmdirc.commandparser.CommandType;
31 31
 import com.dmdirc.commandparser.commands.Command;
32 32
 import com.dmdirc.commandparser.commands.IntelligentCommand;
33 33
 import com.dmdirc.commandparser.commands.context.CommandContext;
34
-import com.dmdirc.config.IdentityManager;
34
+import com.dmdirc.interfaces.IdentityController;
35 35
 import com.dmdirc.interfaces.ServerFactory;
36 36
 import com.dmdirc.logger.ErrorLevel;
37 37
 import com.dmdirc.logger.Logger;
@@ -60,18 +60,24 @@ public class NewServer extends Command implements IntelligentCommand {
60 60
     /** Plugin manager to query for available services. */
61 61
     private final PluginManager pluginManager;
62 62
 
63
+    /** Identity controller to use to find profiles. */
64
+    private final IdentityController identityController;
65
+
63 66
     /**
64 67
      * Creates a new newserver command which will use the specified factory
65 68
      * to construct servers.
66 69
      *
67 70
      * @param serverFactory The factory to use to construct servers
68 71
      * @param pluginManager The plugin manager to use to query available services.
72
+     * @param identityController Identity controller to use to find profiles.
69 73
      */
70 74
     public NewServer(
71 75
             final ServerFactory serverFactory,
72
-            final PluginManager pluginManager) {
76
+            final PluginManager pluginManager,
77
+            final IdentityController identityController) {
73 78
         this.serverFactory = serverFactory;
74 79
         this.pluginManager = pluginManager;
80
+        this.identityController = identityController;
75 81
     }
76 82
 
77 83
     /** {@inheritDoc} */
@@ -102,8 +108,7 @@ public class NewServer extends Command implements IntelligentCommand {
102 108
         }
103 109
 
104 110
         final Server server = serverFactory.createServer(address,
105
-                IdentityManager.getIdentityManager()
106
-                .getIdentitiesByType("profile").get(0));
111
+                identityController.getIdentitiesByType("profile").get(0));
107 112
         server.connect();
108 113
     }
109 114
 
@@ -159,7 +164,7 @@ public class NewServer extends Command implements IntelligentCommand {
159 164
             final CommandArguments args) {
160 165
 
161 166
         boolean ssl = false;
162
-        String host = "";
167
+        String host;
163 168
         String pass = null;
164 169
         int port = 6667;
165 170
         int offset = 0;

+ 14
- 2
src/com/dmdirc/commandparser/commands/global/ReloadIdentities.java View File

@@ -30,7 +30,7 @@ import com.dmdirc.commandparser.CommandType;
30 30
 import com.dmdirc.commandparser.commands.Command;
31 31
 import com.dmdirc.commandparser.commands.IntelligentCommand;
32 32
 import com.dmdirc.commandparser.commands.context.CommandContext;
33
-import com.dmdirc.config.IdentityManager;
33
+import com.dmdirc.interfaces.IdentityController;
34 34
 import com.dmdirc.ui.input.AdditionalTabTargets;
35 35
 
36 36
 /**
@@ -43,11 +43,23 @@ public class ReloadIdentities extends Command implements IntelligentCommand {
43 43
             "reloadidentities - reloads user identities (configuration files)",
44 44
             CommandType.TYPE_GLOBAL);
45 45
 
46
+    /** Identity controller cause to reload identities. */
47
+    private final IdentityController identityController;
48
+
49
+    /**
50
+     * Creates a new instance of the {@link ReloadIdentities} command.
51
+     *
52
+     * @param identityController The controller to reload identities on.
53
+     */
54
+    public ReloadIdentities(final IdentityController identityController) {
55
+        this.identityController = identityController;
56
+    }
57
+
46 58
     /** {@inheritDoc} */
47 59
     @Override
48 60
     public void execute(final FrameContainer origin,
49 61
             final CommandArguments args, final CommandContext context) {
50
-        IdentityManager.getIdentityManager().loadUserIdentities();
62
+        identityController.loadUserIdentities();
51 63
 
52 64
         sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Identities reloaded.");
53 65
     }

+ 14
- 2
src/com/dmdirc/commandparser/commands/global/SaveConfig.java View File

@@ -30,7 +30,7 @@ import com.dmdirc.commandparser.CommandType;
30 30
 import com.dmdirc.commandparser.commands.Command;
31 31
 import com.dmdirc.commandparser.commands.IntelligentCommand;
32 32
 import com.dmdirc.commandparser.commands.context.CommandContext;
33
-import com.dmdirc.config.IdentityManager;
33
+import com.dmdirc.interfaces.IdentityController;
34 34
 import com.dmdirc.ui.input.AdditionalTabTargets;
35 35
 
36 36
 /**
@@ -43,11 +43,23 @@ public final class SaveConfig extends Command implements IntelligentCommand {
43 43
             "saveconfig - force the client to save its configuration to disk",
44 44
             CommandType.TYPE_GLOBAL);
45 45
 
46
+    /** Identity controller cause to save identities. */
47
+    private final IdentityController identityController;
48
+
49
+    /**
50
+     * Creates a new instance of the {@link SaveConfig} command.
51
+     *
52
+     * @param identityController The controller to save identities on.
53
+     */
54
+    public SaveConfig(final IdentityController identityController) {
55
+        this.identityController = identityController;
56
+    }
57
+
46 58
     /** {@inheritDoc} */
47 59
     @Override
48 60
     public void execute(final FrameContainer origin,
49 61
             final CommandArguments args, final CommandContext context) {
50
-        IdentityManager.getIdentityManager().saveAll();
62
+        identityController.saveAll();
51 63
 
52 64
         sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Configuration file saved.");
53 65
     }

+ 19
- 5
src/com/dmdirc/commandparser/commands/global/Set.java View File

@@ -37,7 +37,8 @@ import com.dmdirc.commandparser.commands.flags.CommandFlagHandler;
37 37
 import com.dmdirc.commandparser.commands.flags.CommandFlagResult;
38 38
 import com.dmdirc.config.ConfigManager;
39 39
 import com.dmdirc.config.Identity;
40
-import com.dmdirc.config.IdentityManager;
40
+import com.dmdirc.interfaces.IdentityController;
41
+import com.dmdirc.interfaces.IdentityFactory;
41 42
 import com.dmdirc.ui.input.AdditionalTabTargets;
42 43
 
43 44
 import java.util.List;
@@ -65,12 +66,25 @@ public class Set extends Command implements IntelligentCommand {
65 66
     /** The command flag handler for this command. */
66 67
     private final CommandFlagHandler handler;
67 68
 
69
+    /** The controller to use to set settings. */
70
+    private final IdentityController identityController;
71
+    /** The factory to use to create new identities. */
72
+    private final IdentityFactory identityFactory;
73
+
68 74
     /**
69 75
      * Creates a new instance of Set.
76
+     *
77
+     * @param identityController The controller to use to set settings.
78
+     * @param identityFactory The factory to use to create new identities.
70 79
      */
71
-    public Set() {
80
+    public Set(
81
+            final IdentityController identityController,
82
+            final IdentityFactory identityFactory) {
72 83
         super();
73 84
 
85
+        this.identityController = identityController;
86
+        this.identityFactory = identityFactory;
87
+
74 88
         unsetFlag.addDisabled(appendFlag);
75 89
         appendFlag.addDisabled(unsetFlag);
76 90
 
@@ -90,8 +104,8 @@ public class Set extends Command implements IntelligentCommand {
90 104
             return;
91 105
         }
92 106
 
93
-        Identity identity = IdentityManager.getIdentityManager().getGlobalConfigIdentity();
94
-        ConfigManager manager = IdentityManager.getIdentityManager().getGlobalConfiguration();
107
+        Identity identity = identityController.getGlobalConfigIdentity();
108
+        ConfigManager manager = identityController.getGlobalConfiguration();
95 109
 
96 110
         if (res.hasFlag(serverFlag)) {
97 111
             if (origin.getServer() == null) {
@@ -112,7 +126,7 @@ public class Set extends Command implements IntelligentCommand {
112 126
             }
113 127
 
114 128
             final Channel channel = ((ChannelCommandContext) context).getChannel();
115
-            identity = IdentityManager.getIdentityManager().createChannelConfig(
129
+            identity = identityFactory.createChannelConfig(
116 130
                     origin.getServer().getNetwork(), channel.getName());
117 131
             manager = channel.getConfigManager();
118 132
         }

+ 4
- 2
test/com/dmdirc/commandparser/commands/HelpTest.java View File

@@ -28,6 +28,7 @@ import com.dmdirc.commandparser.CommandInfo;
28 28
 import com.dmdirc.commandparser.CommandLoader;
29 29
 import com.dmdirc.commandparser.CommandManager;
30 30
 import com.dmdirc.commandparser.CommandType;
31
+import com.dmdirc.config.IdentityManager;
31 32
 import com.dmdirc.config.InvalidIdentityFileException;
32 33
 import com.dmdirc.plugins.PluginManager;
33 34
 
@@ -58,13 +59,14 @@ public class HelpTest {
58 59
 
59 60
     @Parameterized.Parameters
60 61
     public static List<Object[]> data() throws InvalidIdentityFileException {
61
-        final List<Object[]> res = new LinkedList<Object[]>();
62
+        final List<Object[]> res = new LinkedList<>();
62 63
 
63 64
         TestMain.getTestMain();
64 65
         new CommandLoader(
65 66
                 mock(LifecycleController.class),
66 67
                 mock(ServerManager.class),
67
-                mock(PluginManager.class)).loadCommands(CommandManager.getCommandManager());
68
+                mock(PluginManager.class),
69
+                mock(IdentityManager.class)).loadCommands(CommandManager.getCommandManager());
68 70
 
69 71
         for (CommandType type : CommandType.values()) {
70 72
             for (CommandInfo command : CommandManager.getCommandManager().getCommands(type).keySet()) {

+ 9
- 3
test/com/dmdirc/commandparser/commands/global/NewServerTest.java View File

@@ -27,6 +27,7 @@ import com.dmdirc.TestMain;
27 27
 import com.dmdirc.commandparser.CommandArguments;
28 28
 import com.dmdirc.commandparser.commands.context.CommandContext;
29 29
 import com.dmdirc.config.Identity;
30
+import com.dmdirc.interfaces.IdentityController;
30 31
 import com.dmdirc.interfaces.ServerFactory;
31 32
 import com.dmdirc.plugins.PluginManager;
32 33
 
@@ -35,13 +36,18 @@ import java.net.URISyntaxException;
35 36
 
36 37
 import org.junit.Before;
37 38
 import org.junit.Test;
39
+import org.junit.runner.RunWith;
38 40
 import org.mockito.Mock;
39
-import org.mockito.MockitoAnnotations;
41
+import org.mockito.runners.MockitoJUnitRunner;
40 42
 
43
+import clover.retrotranslator.edu.emory.mathcs.backport.java.util.Arrays;
41 44
 import static org.mockito.Mockito.*;
42 45
 
46
+@RunWith(MockitoJUnitRunner.class)
43 47
 public class NewServerTest {
44 48
 
49
+    @Mock private IdentityController identityController;
50
+    @Mock private Identity identity;
45 51
     @Mock private FrameContainer container;
46 52
     @Mock private PluginManager pluginManager;
47 53
     @Mock private ServerFactory factory;
@@ -51,9 +57,9 @@ public class NewServerTest {
51 57
     @Before
52 58
     public void setup() {
53 59
         TestMain.getTestMain();
54
-        MockitoAnnotations.initMocks(this);
55 60
         when(factory.createServer(any(URI.class), any(Identity.class))).thenReturn(server);
56
-        command = new NewServer(factory, pluginManager);
61
+        when(identityController.getIdentitiesByType("profile")).thenReturn(Arrays.asList(new Identity[] { identity }));
62
+        command = new NewServer(factory, pluginManager, identityController);
57 63
     }
58 64
 
59 65
     @Test

Loading…
Cancel
Save