浏览代码

Pass in dependencies to ServerManager explicitly.

Avoid static/singleton references, and use a temporary Provider<>
implementation that will eventually be replaced by Dagger.

Change-Id: Id1bda9437f71cc9ddceb0a9ed1ba38d4787f2226
Reviewed-on: http://gerrit.dmdirc.com/2671
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10 年前
父节点
当前提交
08bac19e14
共有 4 个文件被更改,包括 64 次插入15 次删除
  1. 18
    2
      src/com/dmdirc/Main.java
  2. 25
    11
      src/com/dmdirc/ServerManager.java
  3. 10
    1
      test/com/dmdirc/ServerManagerTest.java
  4. 11
    1
      test/com/dmdirc/TestMain.java

+ 18
- 2
src/com/dmdirc/Main.java 查看文件

57
 import java.util.Timer;
57
 import java.util.Timer;
58
 import java.util.TimerTask;
58
 import java.util.TimerTask;
59
 
59
 
60
+import javax.inject.Provider;
61
+
60
 /**
62
 /**
61
  * Main class, handles initialisation.
63
  * Main class, handles initialisation.
62
  */
64
  */
66
     private final int FEEDBACK_DELAY = 30 * 60 * 1000;
68
     private final int FEEDBACK_DELAY = 30 * 60 * 1000;
67
 
69
 
68
     /** The UI to use for the client. */
70
     /** The UI to use for the client. */
69
-    private final Collection<UIController> CONTROLLERS = new HashSet<UIController>();
71
+    private final Collection<UIController> CONTROLLERS = new HashSet<>();
70
 
72
 
71
     /** The config dir to use for the client. */
73
     /** The config dir to use for the client. */
72
     private String configdir;
74
     private String configdir;
106
         Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
108
         Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
107
 
109
 
108
         IdentityManager.getIdentityManager().loadVersionIdentity();
110
         IdentityManager.getIdentityManager().loadVersionIdentity();
109
-        serverManager = new ServerManager();
111
+        serverManager = new ServerManager(
112
+                new ParserFactoryProvider(),
113
+                IdentityManager.getIdentityManager());
110
         ActionManager.initActionManager(serverManager, IdentityManager.getIdentityManager());
114
         ActionManager.initActionManager(serverManager, IdentityManager.getIdentityManager());
111
 
115
 
112
         final CommandLineParser clp = new CommandLineParser(this, args);
116
         final CommandLineParser clp = new CommandLineParser(this, args);
506
         }
510
         }
507
     }
511
     }
508
 
512
 
513
+    /**
514
+     * Temporary class to lazily provide {@link ParserFactory}s.
515
+     */
516
+    private class ParserFactoryProvider implements Provider<ParserFactory> {
517
+
518
+        /** {@inheritDoc} */
519
+        @Override
520
+        public ParserFactory get() {
521
+            return new ParserFactory(pluginManager);
522
+        }
523
+    }
524
+
509
 }
525
 }

+ 25
- 11
src/com/dmdirc/ServerManager.java 查看文件

27
 import com.dmdirc.commandparser.parsers.ServerCommandParser;
27
 import com.dmdirc.commandparser.parsers.ServerCommandParser;
28
 import com.dmdirc.config.ConfigManager;
28
 import com.dmdirc.config.ConfigManager;
29
 import com.dmdirc.config.Identity;
29
 import com.dmdirc.config.Identity;
30
-import com.dmdirc.config.IdentityManager;
30
+import com.dmdirc.interfaces.IdentityController;
31
 import com.dmdirc.interfaces.ServerFactory;
31
 import com.dmdirc.interfaces.ServerFactory;
32
 import com.dmdirc.logger.ErrorLevel;
32
 import com.dmdirc.logger.ErrorLevel;
33
 import com.dmdirc.logger.Logger;
33
 import com.dmdirc.logger.Logger;
41
 import java.util.Set;
41
 import java.util.Set;
42
 import java.util.concurrent.CopyOnWriteArraySet;
42
 import java.util.concurrent.CopyOnWriteArraySet;
43
 
43
 
44
+import javax.inject.Provider;
45
+
44
 /**
46
 /**
45
  * The ServerManager maintains a list of all servers, and provides methods to
47
  * The ServerManager maintains a list of all servers, and provides methods to
46
  * search or iterate over them.
48
  * search or iterate over them.
48
 public class ServerManager implements ServerFactory {
50
 public class ServerManager implements ServerFactory {
49
 
51
 
50
     /** All servers that currently exist. */
52
     /** All servers that currently exist. */
51
-    private final Set<Server> servers = new CopyOnWriteArraySet<Server>();
53
+    private final Set<Server> servers = new CopyOnWriteArraySet<>();
54
+
55
+    /** Provider of {@link ParserFactory}s for servers. */
56
+    private final Provider<ParserFactory> parserFactoryProvider;
57
+
58
+    /** The identity controller to use to find profiles. */
59
+    private final IdentityController identityController;
52
 
60
 
53
     /**
61
     /**
54
      * Creates a new instance of ServerManager.
62
      * Creates a new instance of ServerManager.
63
+     *
64
+     * @param parserFactoryProvider The provider of {@link ParserFactory}s to give to servers.
65
+     * @param identityController The identity controller to use to find profiles.
55
      */
66
      */
56
-    public ServerManager() {
67
+    public ServerManager(
68
+            final Provider<ParserFactory> parserFactoryProvider,
69
+            final IdentityController identityController) {
70
+        this.parserFactoryProvider = parserFactoryProvider;
71
+        this.identityController = identityController;
57
     }
72
     }
58
 
73
 
59
     /** {@inheritDoc} */
74
     /** {@inheritDoc} */
65
                 this,
80
                 this,
66
                 configManager,
81
                 configManager,
67
                 new ServerCommandParser(configManager),
82
                 new ServerCommandParser(configManager),
68
-                new ParserFactory(Main.mainInstance.getPluginManager()),
83
+                parserFactoryProvider.get(),
69
                 WindowManager.getWindowManager(),
84
                 WindowManager.getWindowManager(),
70
                 AliasWrapper.getAliasWrapper(),
85
                 AliasWrapper.getAliasWrapper(),
71
                 CommandManager.getCommandManager(),
86
                 CommandManager.getCommandManager(),
98
      * @return A list of all servers
113
      * @return A list of all servers
99
      */
114
      */
100
     public List<Server> getServers() {
115
     public List<Server> getServers() {
101
-        return new ArrayList<Server>(servers);
116
+        return new ArrayList<>(servers);
102
     }
117
     }
103
 
118
 
104
     /**
119
     /**
150
      * @return A list of servers connected to the network
165
      * @return A list of servers connected to the network
151
      */
166
      */
152
     public List<Server> getServersByNetwork(final String network) {
167
     public List<Server> getServersByNetwork(final String network) {
153
-        final List<Server> res = new ArrayList<Server>();
168
+        final List<Server> res = new ArrayList<>();
154
 
169
 
155
         for (Server server : servers) {
170
         for (Server server : servers) {
156
             if (server.isNetwork(network)) {
171
             if (server.isNetwork(network)) {
168
      * @return A list of servers connected to the network
183
      * @return A list of servers connected to the network
169
      */
184
      */
170
     public List<Server> getServersByAddress(final String address) {
185
     public List<Server> getServersByAddress(final String address) {
171
-        final List<Server> res = new ArrayList<Server>();
186
+        final List<Server> res = new ArrayList<>();
172
 
187
 
173
         for (Server server : servers) {
188
         for (Server server : servers) {
174
             if (server.getAddress().equalsIgnoreCase(address)) {
189
             if (server.getAddress().equalsIgnoreCase(address)) {
188
      * @since 0.6.3
203
      * @since 0.6.3
189
      */
204
      */
190
     public Server connectToAddress(final URI uri) {
205
     public Server connectToAddress(final URI uri) {
191
-        return connectToAddress(uri, IdentityManager.getIdentityManager()
192
-                .getIdentitiesByType("profile").get(0));
206
+        return connectToAddress(uri,
207
+                identityController.getIdentitiesByType("profile").get(0));
193
     }
208
     }
194
 
209
 
195
     /**
210
     /**
249
 
264
 
250
         if (connectedServer == null) {
265
         if (connectedServer == null) {
251
             try {
266
             try {
252
-                connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"),
253
-                        IdentityManager.getIdentityManager().getIdentitiesByType("profile").get(0));
267
+                connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
254
             } catch (URISyntaxException ex) {
268
             } catch (URISyntaxException ex) {
255
                 Logger.appError(ErrorLevel.MEDIUM, "Unable to construct new server", ex);
269
                 Logger.appError(ErrorLevel.MEDIUM, "Unable to construct new server", ex);
256
             }
270
             }

+ 10
- 1
test/com/dmdirc/ServerManagerTest.java 查看文件

22
 
22
 
23
 package com.dmdirc;
23
 package com.dmdirc;
24
 
24
 
25
+import com.dmdirc.interfaces.IdentityController;
25
 import com.dmdirc.parser.common.ChannelJoinRequest;
26
 import com.dmdirc.parser.common.ChannelJoinRequest;
26
 
27
 
28
+import javax.inject.Provider;
29
+
27
 import org.junit.After;
30
 import org.junit.After;
28
 import org.junit.Before;
31
 import org.junit.Before;
29
 import org.junit.Ignore;
32
 import org.junit.Ignore;
30
 import org.junit.Test;
33
 import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
31
 
37
 
32
 import static org.junit.Assert.*;
38
 import static org.junit.Assert.*;
33
 import static org.mockito.Mockito.*;
39
 import static org.mockito.Mockito.*;
34
 
40
 
41
+@RunWith(MockitoJUnitRunner.class)
35
 public class ServerManagerTest {
42
 public class ServerManagerTest {
36
 
43
 
44
+    @Mock private IdentityController identityController;
45
+    @Mock private Provider<ParserFactory> parserFactoryProvider;
37
     private ServerManager serverManager;
46
     private ServerManager serverManager;
38
 
47
 
39
     @Before
48
     @Before
40
     public void setUp() throws Exception {
49
     public void setUp() throws Exception {
41
-        serverManager = new ServerManager();
50
+        serverManager = new ServerManager(parserFactoryProvider, identityController);
42
     }
51
     }
43
 
52
 
44
     @After
53
     @After

+ 11
- 1
test/com/dmdirc/TestMain.java 查看文件

6
 import com.dmdirc.config.InvalidIdentityFileException;
6
 import com.dmdirc.config.InvalidIdentityFileException;
7
 import com.dmdirc.plugins.PluginManager;
7
 import com.dmdirc.plugins.PluginManager;
8
 
8
 
9
+import javax.inject.Provider;
10
+import org.mockito.Mock;
11
+import org.mockito.MockitoAnnotations;
12
+
9
 /**
13
 /**
10
  * Main subclass to init things needed for testing.
14
  * Main subclass to init things needed for testing.
11
  */
15
  */
12
 public class TestMain extends Main {
16
 public class TestMain extends Main {
13
     private static Main instance;
17
     private static Main instance;
14
 
18
 
19
+    @Mock private Provider<ParserFactory> parserFactoryProvider;
20
+
15
     public TestMain() { }
21
     public TestMain() { }
16
 
22
 
17
     /** {@inheritDoc} */
23
     /** {@inheritDoc} */
18
     @Override
24
     @Override
19
     public void init(final String[] args) {
25
     public void init(final String[] args) {
26
+        MockitoAnnotations.initMocks(this);
27
+
20
         // TODO: Tests probably shouldn't rely on a config dir... Who knows
28
         // TODO: Tests probably shouldn't rely on a config dir... Who knows
21
         //       what the user has done with their config.
29
         //       what the user has done with their config.
22
         IdentityManager.getIdentityManager().loadVersionIdentity();
30
         IdentityManager.getIdentityManager().loadVersionIdentity();
28
             // DON'T do anything to the user's configuration... (so no calls
36
             // DON'T do anything to the user's configuration... (so no calls
29
             // to handleInvalidConfigFile(); here)
37
             // to handleInvalidConfigFile(); here)
30
         }
38
         }
31
-        serverManager = new ServerManager();
39
+        serverManager = new ServerManager(
40
+                parserFactoryProvider,
41
+                IdentityManager.getIdentityManager());
32
         ActionManager.initActionManager(serverManager, IdentityManager.getIdentityManager());
42
         ActionManager.initActionManager(serverManager, IdentityManager.getIdentityManager());
33
         pluginManager = new PluginManager(IdentityManager.getIdentityManager(), this);
43
         pluginManager = new PluginManager(IdentityManager.getIdentityManager(), this);
34
         pluginManager.refreshPlugins();
44
         pluginManager.refreshPlugins();

正在加载...
取消
保存