Przeglądaj źródła

Remove singleton IdentityManager accessors.

Woohoo.

Change-Id: Ib5dfa40b37670633791a39cc166ef53aeb2c2e79
Fixes-Issue: CLIENT-423
Reviewed-on: http://gerrit.dmdirc.com/3145
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10 lat temu
rodzic
commit
912140c077

+ 0
- 1
src/com/dmdirc/ClientModule.java Wyświetl plik

@@ -120,7 +120,6 @@ public class ClientModule {
120 120
         final IdentityManager identityManager =
121 121
                 new IdentityManager(baseDirectory, identitiesDirectory);
122 122
         ErrorManager.getErrorManager().initialise(identityManager);
123
-        IdentityManager.setIdentityManager(identityManager);
124 123
         identityManager.loadVersionIdentity();
125 124
 
126 125
         try {

+ 27
- 21
src/com/dmdirc/config/ConfigFileBackedConfigProvider.java Wyświetl plik

@@ -42,6 +42,8 @@ import java.util.List;
42 42
 import java.util.Map;
43 43
 import java.util.Set;
44 44
 
45
+import javax.annotation.Nullable;
46
+
45 47
 import org.slf4j.LoggerFactory;
46 48
 
47 49
 /**
@@ -57,6 +59,9 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
57 59
     private static final String PROFILE_DOMAIN = "profile";
58 60
     /** The target for this identity. */
59 61
     protected final ConfigTarget myTarget;
62
+    /** The identity manager to use for writable configs. */
63
+    @Nullable
64
+    private final IdentityManager identityManager;
60 65
     /** The configuration details for this identity. */
61 66
     protected final ConfigFile file;
62 67
     /** The global config manager. */
@@ -69,17 +74,16 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
69 74
     /**
70 75
      * Creates a new instance of Identity.
71 76
      *
72
-     * @param file         The file to load this identity from
73
-     * @param forceDefault Whether to force this identity to be loaded as default identity or not
77
+     * @param identityManager The manager to use for hackily reading global state.
78
+     * @param file            The file to load this identity from
79
+     * @param forceDefault    Whether to force this identity to be loaded as default identity or not
74 80
      *
75 81
      * @throws InvalidIdentityFileException Missing required properties
76 82
      * @throws IOException                  Input/output exception
77 83
      */
78
-    public ConfigFileBackedConfigProvider(final File file, final boolean forceDefault) throws
79
-            IOException,
80
-            InvalidIdentityFileException {
81
-        super();
82
-
84
+    public ConfigFileBackedConfigProvider(final IdentityManager identityManager, final File file,
85
+            final boolean forceDefault) throws IOException, InvalidIdentityFileException {
86
+        this.identityManager = identityManager;
83 87
         this.file = new ConfigFile(file);
84 88
         this.file.setAutomake(true);
85 89
         initFile(forceDefault);
@@ -100,10 +104,8 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
100 104
      * @throws IOException                  Input/output exception
101 105
      */
102 106
     public ConfigFileBackedConfigProvider(final InputStream stream, final boolean forceDefault)
103
-            throws IOException,
104
-            InvalidIdentityFileException {
105
-        super();
106
-
107
+            throws IOException, InvalidIdentityFileException {
108
+        this.identityManager = null;
107 109
         this.file = new ConfigFile(stream);
108 110
         this.file.setAutomake(true);
109 111
         initFile(forceDefault);
@@ -117,12 +119,13 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
117 119
     /**
118 120
      * Creates a new identity from the specified config file.
119 121
      *
120
-     * @param configFile The config file to use
121
-     * @param target     The target of this identity
122
+     * @param identityManager The manager to use for hackily reading global state.
123
+     * @param configFile      The config file to use
124
+     * @param target          The target of this identity
122 125
      */
123
-    public ConfigFileBackedConfigProvider(final ConfigFile configFile, final ConfigTarget target) {
124
-        super();
125
-
126
+    public ConfigFileBackedConfigProvider(final IdentityManager identityManager,
127
+            final ConfigFile configFile, final ConfigTarget target) {
128
+        this.identityManager = identityManager;
126 129
         this.file = configFile;
127 130
         this.file.setAutomake(true);
128 131
         this.myTarget = target;
@@ -329,7 +332,9 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
329 332
                 // covered by global defaults.
330 333
 
331 334
                 if (globalConfig == null) {
332
-                    globalConfig = new ConfigManager("", "", "", "");
335
+                    // TODO: This is horrible. Filtering of saves should be abstracted.
336
+                    globalConfig = (ConfigManager) identityManager
337
+                            .createAggregateConfig("", "", "", "");
333 338
                 }
334 339
 
335 340
                 globalConfig.removeIdentity(this);
@@ -436,14 +441,15 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
436 441
                 // like that until you manually change it again, as opposed
437 442
                 // to being removed as soon as you use a build from that
438 443
                 // channel.
444
+                // TODO: This behaviour should be managed by something else.
439 445
 
440 446
                 if (globalConfig == null) {
441
-                    globalConfig = new ConfigManager("", "", "", "");
447
+                    globalConfig = (ConfigManager) identityManager
448
+                            .createAggregateConfig("", "", "", "");
442 449
                 }
443 450
 
444 451
                 globalConfig.removeIdentity(this);
445
-                globalConfig.removeIdentity(IdentityManager.getIdentityManager().
446
-                        getVersionSettings());
452
+                globalConfig.removeIdentity(identityManager.getVersionSettings());
447 453
 
448 454
                 if (log.isTraceEnabled()) {
449 455
                     for (ConfigProvider source : globalConfig.getSources()) {
@@ -493,7 +499,7 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
493 499
             file.delete();
494 500
         }
495 501
 
496
-        IdentityManager.getIdentityManager().removeConfigProvider(this);
502
+        identityManager.removeConfigProvider(this);
497 503
     }
498 504
 
499 505
     /** {@inheritDoc} */

+ 13
- 8
src/com/dmdirc/config/ConfigManager.java Wyświetl plik

@@ -58,6 +58,8 @@ class ConfigManager extends BaseConfigProvider implements ConfigChangeListener,
58 58
     private final MapList<String, ConfigChangeListener> listeners = new MapList<>();
59 59
     /** The config binder to use for this manager. */
60 60
     private final ConfigBinder binder = new ConfigBinder(this);
61
+    /** The manager to use to fetch global state. */
62
+    private final IdentityManager manager;
61 63
     /** The protocol this manager is for. */
62 64
     private String protocol;
63 65
     /** The ircd this manager is for. */
@@ -72,6 +74,7 @@ class ConfigManager extends BaseConfigProvider implements ConfigChangeListener,
72 74
     /**
73 75
      * Creates a new instance of ConfigManager.
74 76
      *
77
+     * @param manager  The manager to use to retrieve global state horribly.
75 78
      * @param protocol The protocol for this manager
76 79
      * @param ircd     The name of the ircd for this manager
77 80
      * @param network  The name of the network for this manager
@@ -80,14 +83,16 @@ class ConfigManager extends BaseConfigProvider implements ConfigChangeListener,
80 83
      * @since 0.6.3
81 84
      */
82 85
     ConfigManager(
86
+            final IdentityManager manager,
83 87
             final String protocol, final String ircd,
84 88
             final String network, final String server) {
85
-        this(protocol, ircd, network, server, "<Unknown>");
89
+        this(manager, protocol, ircd, network, server, "<Unknown>");
86 90
     }
87 91
 
88 92
     /**
89 93
      * Creates a new instance of ConfigManager.
90 94
      *
95
+     * @param manager  The manager to use to retrieve global state horribly.
91 96
      * @param protocol The protocol for this manager
92 97
      * @param ircd     The name of the ircd for this manager
93 98
      * @param network  The name of the network for this manager
@@ -96,10 +101,13 @@ class ConfigManager extends BaseConfigProvider implements ConfigChangeListener,
96 101
      *
97 102
      * @since 0.6.3
98 103
      */
99
-    ConfigManager(final String protocol, final String ircd,
104
+    ConfigManager(
105
+            final IdentityManager manager,
106
+            final String protocol, final String ircd,
100 107
             final String network, final String server, final String channel) {
101 108
         final String chanName = channel + "@" + network;
102 109
 
110
+        this.manager = manager;
103 111
         this.protocol = protocol;
104 112
         this.ircd = ircd;
105 113
         this.network = network;
@@ -163,8 +171,7 @@ class ConfigManager extends BaseConfigProvider implements ConfigChangeListener,
163 171
     @Override
164 172
     public Map<String, String> getOptions(final String domain) {
165 173
         if (VERSION_DOMAIN.equals(domain)) {
166
-            return IdentityManager.getIdentityManager()
167
-                    .getVersionSettings().getOptions(domain);
174
+            return manager.getVersionSettings().getOptions(domain);
168 175
         }
169 176
 
170 177
         final Map<String, String> res = new HashMap<>();
@@ -220,8 +227,7 @@ class ConfigManager extends BaseConfigProvider implements ConfigChangeListener,
220 227
      */
221 228
     protected ConfigProvider getScope(final String domain, final String option) {
222 229
         if (VERSION_DOMAIN.equals(domain)) {
223
-            return IdentityManager.getIdentityManager()
224
-                    .getVersionSettings();
230
+            return manager.getVersionSettings();
225 231
         }
226 232
 
227 233
         synchronized (sources) {
@@ -375,8 +381,7 @@ class ConfigManager extends BaseConfigProvider implements ConfigChangeListener,
375 381
             }
376 382
         }
377 383
 
378
-        final List<ConfigProvider> newSources = IdentityManager.getIdentityManager()
379
-                .getIdentitiesForManager(this);
384
+        final List<ConfigProvider> newSources = manager.getIdentitiesForManager(this);
380 385
         for (ConfigProvider identity : newSources) {
381 386
             log.trace("Testing new identity: {}", identity);
382 387
             checkIdentity(identity);

+ 13
- 38
src/com/dmdirc/config/IdentityManager.java Wyświetl plik

@@ -65,8 +65,6 @@ public class IdentityManager implements IdentityFactory, IdentityController {
65 65
     private static final String IDENTITY_DOMAIN = "identity";
66 66
     /** The domain used for profile settings. */
67 67
     private static final String PROFILE_DOMAIN = "profile";
68
-    /** A singleton instance of IdentityManager. */
69
-    private static IdentityManager instance;
70 68
     /** Base configuration directory where the main configuration file will be located. */
71 69
     private final String configDirectory;
72 70
     /** Directory to save and load identities in. */
@@ -105,7 +103,6 @@ public class IdentityManager implements IdentityFactory, IdentityController {
105 103
         this.identitiesDirectory = identitiesDirectory;
106 104
     }
107 105
 
108
-    /** {@inheritDoc} */
109 106
     @Override
110 107
     @Deprecated
111 108
     public String getConfigurationDirectory() {
@@ -139,7 +136,7 @@ public class IdentityManager implements IdentityFactory, IdentityController {
139 136
         addonSettings.put("name", "Addon defaults");
140 137
         addonConfigFile.addDomain("identity", addonSettings);
141 138
 
142
-        addonConfig = new ConfigFileBackedConfigProvider(addonConfigFile, target);
139
+        addonConfig = new ConfigFileBackedConfigProvider(this, addonConfigFile, target);
143 140
         addConfigProvider(addonConfig);
144 141
 
145 142
         if (!getGlobalConfiguration().hasOptionString("identity", "defaultsversion")) {
@@ -301,7 +298,7 @@ public class IdentityManager implements IdentityFactory, IdentityController {
301 298
         }
302 299
 
303 300
         try {
304
-            addConfigProvider(new ConfigFileBackedConfigProvider(file, false));
301
+            addConfigProvider(new ConfigFileBackedConfigProvider(this, file, false));
305 302
         } catch (InvalidIdentityFileException ex) {
306 303
             Logger.userError(ErrorLevel.MEDIUM,
307 304
                     "Invalid identity file: " + file.getAbsolutePath()
@@ -371,7 +368,7 @@ public class IdentityManager implements IdentityFactory, IdentityController {
371 368
                 file.createNewFile();
372 369
             }
373 370
 
374
-            config = new ConfigFileBackedConfigProvider(file, true);
371
+            config = new ConfigFileBackedConfigProvider(this, file, true);
375 372
             config.setOption("identity", "name", "Global config");
376 373
             addConfigProvider(config);
377 374
         } catch (IOException ex) {
@@ -692,37 +689,13 @@ public class IdentityManager implements IdentityFactory, IdentityController {
692 689
 
693 690
         configFile.write();
694 691
 
695
-        final ConfigFileBackedConfigProvider identity = new ConfigFileBackedConfigProvider(file,
696
-                false);
692
+        final ConfigFileBackedConfigProvider identity = new ConfigFileBackedConfigProvider(this,
693
+                file, false);
697 694
         addConfigProvider(identity);
698 695
 
699 696
         return identity;
700 697
     }
701 698
 
702
-    /**
703
-     * Gets a singleton instance of the Identity Manager.
704
-     *
705
-     * @return A singleton instance of the IdentityManager.
706
-     *
707
-     * @deprecated Shouldn't use global state.
708
-     */
709
-    @Deprecated
710
-    public static IdentityManager getIdentityManager() {
711
-        return instance;
712
-    }
713
-
714
-    /**
715
-     * Sets the singleton instance of the Identity Manager.
716
-     *
717
-     * @param identityManager The identity manager to use.
718
-     *
719
-     * @deprecated Shouldn't use global state.
720
-     */
721
-    @Deprecated
722
-    public static void setIdentityManager(final IdentityManager identityManager) {
723
-        instance = identityManager;
724
-    }
725
-
726 699
     /**
727 700
      * Finds and adds sources for the given manager, and adds it as an identity listener.
728 701
      *
@@ -743,7 +716,8 @@ public class IdentityManager implements IdentityFactory, IdentityController {
743 716
     @Override
744 717
     public ConfigProviderMigrator createMigratableConfig(final String protocol,
745 718
             final String ircd, final String network, final String server) {
746
-        final ConfigManager configManager = new ConfigManager(protocol, ircd, network, server);
719
+        final ConfigManager configManager =
720
+                new ConfigManager(this, protocol, ircd, network, server);
747 721
         setUpConfigManager(configManager);
748 722
         return new ConfigManagerMigrator(configManager);
749 723
     }
@@ -752,8 +726,8 @@ public class IdentityManager implements IdentityFactory, IdentityController {
752 726
     @Override
753 727
     public ConfigProviderMigrator createMigratableConfig(final String protocol,
754 728
             final String ircd, final String network, final String server, final String channel) {
755
-        final ConfigManager configManager = new ConfigManager(protocol, ircd, network, server,
756
-                channel);
729
+        final ConfigManager configManager =
730
+                new ConfigManager(this, protocol, ircd, network, server, channel);
757 731
         setUpConfigManager(configManager);
758 732
         return new ConfigManagerMigrator(configManager);
759 733
     }
@@ -761,7 +735,8 @@ public class IdentityManager implements IdentityFactory, IdentityController {
761 735
     @Override
762 736
     public AggregateConfigProvider createAggregateConfig(final String protocol, final String ircd,
763 737
             final String network, final String server) {
764
-        final ConfigManager configManager = new ConfigManager(protocol, ircd, network, server);
738
+        final ConfigManager configManager =
739
+                new ConfigManager(this, protocol, ircd, network, server);
765 740
         setUpConfigManager(configManager);
766 741
         return configManager;
767 742
     }
@@ -769,8 +744,8 @@ public class IdentityManager implements IdentityFactory, IdentityController {
769 744
     @Override
770 745
     public AggregateConfigProvider createAggregateConfig(final String protocol, final String ircd,
771 746
             final String network, final String server, final String channel) {
772
-        final ConfigManager configManager = new ConfigManager(protocol, ircd, network, server,
773
-                channel);
747
+        final ConfigManager configManager =
748
+                new ConfigManager(this, protocol, ircd, network, server, channel);
774 749
         setUpConfigManager(configManager);
775 750
         return configManager;
776 751
     }

+ 9
- 4
test/com/dmdirc/config/ConfigManagerTest.java Wyświetl plik

@@ -19,6 +19,7 @@
19 19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 20
  * SOFTWARE.
21 21
  */
22
+
22 23
 package com.dmdirc.config;
23 24
 
24 25
 import com.dmdirc.interfaces.config.ConfigChangeListener;
@@ -26,6 +27,7 @@ import com.dmdirc.util.validators.PermissiveValidator;
26 27
 
27 28
 import org.junit.Test;
28 29
 import org.junit.runner.RunWith;
30
+import org.mockito.Mock;
29 31
 import org.mockito.runners.MockitoJUnitRunner;
30 32
 
31 33
 import static org.junit.Assert.*;
@@ -34,14 +36,17 @@ import static org.mockito.Mockito.*;
34 36
 @RunWith(MockitoJUnitRunner.class)
35 37
 public class ConfigManagerTest {
36 38
 
39
+    @Mock private IdentityManager identityManager;
40
+
37 41
     @Test
38 42
     public void testNonExistantOption() {
39
-        assertNull(new ConfigManager("", "", "", "").getOption("unit-test123", "foobar"));
43
+        assertNull(new ConfigManager(identityManager, "", "", "", "")
44
+                .getOption("unit-test123", "foobar"));
40 45
     }
41 46
 
42 47
     @Test
43 48
     public void testStats() {
44
-        final ConfigManager cm = new ConfigManager("", "", "", "");
49
+        final ConfigManager cm = new ConfigManager(identityManager, "", "", "", "");
45 50
         assertNull(ConfigManager.getStats().get("unit-test123.baz"));
46 51
         cm.hasOption("unit-test123", "baz", new PermissiveValidator<String>());
47 52
         assertNotNull(ConfigManager.getStats().get("unit-test123.baz"));
@@ -51,7 +56,7 @@ public class ConfigManagerTest {
51 56
     @Test
52 57
     public void testDomainListener() {
53 58
         final ConfigChangeListener listener = mock(ConfigChangeListener.class);
54
-        final ConfigManager cm = new ConfigManager("", "", "", "");
59
+        final ConfigManager cm = new ConfigManager(identityManager, "", "", "", "");
55 60
         cm.addChangeListener("unit-test", listener);
56 61
 
57 62
         cm.configChanged("foo", "bar");
@@ -64,7 +69,7 @@ public class ConfigManagerTest {
64 69
     @Test
65 70
     public void testDomainKeyListener() {
66 71
         final ConfigChangeListener listener = mock(ConfigChangeListener.class);
67
-        final ConfigManager cm = new ConfigManager("", "", "", "");
72
+        final ConfigManager cm = new ConfigManager(identityManager, "", "", "", "");
68 73
         cm.addChangeListener("unit-test", "foo", listener);
69 74
 
70 75
         cm.configChanged("foo", "bar");

+ 13
- 5
test/com/dmdirc/config/IdentityTest.java Wyświetl plik

@@ -32,12 +32,17 @@ import java.util.Map;
32 32
 import org.junit.Before;
33 33
 import org.junit.Ignore;
34 34
 import org.junit.Test;
35
+import org.junit.runner.RunWith;
36
+import org.mockito.Mock;
37
+import org.mockito.runners.MockitoJUnitRunner;
35 38
 
36 39
 import static org.junit.Assert.*;
37 40
 import static org.mockito.Mockito.*;
38 41
 
42
+@RunWith(MockitoJUnitRunner.class)
39 43
 public class IdentityTest {
40 44
 
45
+    @Mock private IdentityManager identityManager;
41 46
     private ConfigFileBackedConfigProvider myIdent;
42 47
     private ConfigTarget target;
43 48
 
@@ -46,7 +51,8 @@ public class IdentityTest {
46 51
         target = new ConfigTarget();
47 52
         target.setChannel("#unittest@unittest");
48 53
 
49
-        myIdent = new ConfigFileBackedConfigProvider(new ConfigFile(getClass().getResourceAsStream("identity2")), target);
54
+        myIdent = new ConfigFileBackedConfigProvider(identityManager,
55
+                new ConfigFile(getClass().getResourceAsStream("identity2")), target);
50 56
     }
51 57
 
52 58
     @Test
@@ -134,7 +140,8 @@ public class IdentityTest {
134 140
 
135 141
         myIdent.save();
136 142
 
137
-        myIdent = new ConfigFileBackedConfigProvider(myIdent.file.getFile(), false);
143
+        myIdent = new ConfigFileBackedConfigProvider(identityManager,
144
+                myIdent.file.getFile(), false);
138 145
 
139 146
         assertEquals("baz!", myIdent.getOption("foo", "bar"));
140 147
     }
@@ -145,19 +152,20 @@ public class IdentityTest {
145 152
         assertEquals(target.getType(), myIdent.getTarget().getType());
146 153
     }
147 154
 
148
-    @Test(expected=InvalidIdentityFileException.class)
155
+    @Test(expected = InvalidIdentityFileException.class)
149 156
     public void testNoName() throws IOException, InvalidIdentityFileException {
150 157
         new ConfigFileBackedConfigProvider(getClass().getResourceAsStream("identity1"), false);
151 158
     }
152 159
 
153
-    @Test(expected=InvalidIdentityFileException.class)
160
+    @Test(expected = InvalidIdentityFileException.class)
154 161
     public void testNoTarget() throws IOException, InvalidIdentityFileException {
155 162
         new ConfigFileBackedConfigProvider(getClass().getResourceAsStream("identity2"), false);
156 163
     }
157 164
 
158 165
     @Test
159 166
     public void testMigrate() throws IOException, InvalidIdentityFileException {
160
-        final ConfigFileBackedConfigProvider id = new ConfigFileBackedConfigProvider(getClass().getResourceAsStream("identity3"), false);
167
+        final ConfigFileBackedConfigProvider id = new ConfigFileBackedConfigProvider(getClass().
168
+                getResourceAsStream("identity3"), false);
161 169
 
162 170
         assertTrue(id.file.isKeyDomain("identity"));
163 171
         assertTrue(id.file.isKeyDomain("meep"));

Ładowanie…
Anuluj
Zapisz