Browse Source

Add NewServerDialogModel and associated classes.

Depends-On: I4d3d94c3dc60690a958a5b2fc8534c35c1f8b61e
Change-Id: Ifa74a7522db34719b18372846f8b8598fc3152b9
Reviewed-on: http://gerrit.dmdirc.com/3589
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 10 years ago
parent
commit
d5d4d20f0a

+ 215
- 0
src/com/dmdirc/interfaces/ui/NewServerDialogModel.java View File

@@ -0,0 +1,215 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.interfaces.ui;
24
+
25
+import com.dmdirc.interfaces.config.ConfigProvider;
26
+import com.dmdirc.util.validators.Validator;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.List;
31
+
32
+/**
33
+ * Model representing a dialog containing the details required to open a new connection.
34
+ */
35
+public interface NewServerDialogModel {
36
+
37
+    /**
38
+     * Loads the default and required settings and adds the appropriate listeners.
39
+     */
40
+    void loadModel();
41
+
42
+    /**
43
+     * Returns the list of profiles available to the dialog.
44
+     *
45
+     * @return List of available profiles
46
+     */
47
+    List<ConfigProvider> getProfileList();
48
+
49
+    /**
50
+     * Gets the selected profile in the dialog.
51
+     *
52
+     * @return Selected profile
53
+     */
54
+    Optional<ConfigProvider> getSelectedProfile();
55
+
56
+    /**
57
+     * Sets the selected profile in the dialog.
58
+     *
59
+     * @param selectedProfile New selected profile
60
+     */
61
+    void setSelectedProfile(Optional<ConfigProvider> selectedProfile);
62
+
63
+    /**
64
+     * Is the profiles list valid?
65
+     *
66
+     * @return true if value
67
+     */
68
+    boolean isProfileListValid();
69
+
70
+    /**
71
+     * Returns the validator for the profiles list.
72
+     *
73
+     * @return Profile validator
74
+     */
75
+    Validator<List<ConfigProvider>> getProfileListValidator();
76
+
77
+    /**
78
+     * Returns the hostname of the server.
79
+     *
80
+     * @return Server hostname
81
+     */
82
+    Optional<String> getHostname();
83
+
84
+    /**
85
+     * Sets the server hostname.
86
+     *
87
+     * @param hostname New server hostname
88
+     */
89
+    void setHostname(Optional<String> hostname);
90
+
91
+    /**
92
+     * Is the server hostname valid.
93
+     *
94
+     * @return true if valid
95
+     */
96
+    boolean isHostnameValid();
97
+
98
+    /**
99
+     * Returns the validator for the server hostname.
100
+     *
101
+     * @return Server hostname validator
102
+     */
103
+    Validator<String> getHostnameValidator();
104
+
105
+    /**
106
+     * Returns the servers port.
107
+     *
108
+     * @return Servers port
109
+     */
110
+    Optional<Integer> getPort();
111
+
112
+    /**
113
+     * Sets the servers port.
114
+     *
115
+     * @param port New server port
116
+     */
117
+    void setPort(Optional<Integer> port);
118
+
119
+    /**
120
+     * Is the port valid?
121
+     *
122
+     * @return true if valid
123
+     */
124
+    boolean isPortValid();
125
+
126
+    /**
127
+     * Returns the validator for servers port.
128
+     *
129
+     * @return Server port validator
130
+     */
131
+    Validator<Integer> getPortValidator();
132
+
133
+    /**
134
+     * Gets the servers password.
135
+     *
136
+     * @return Servers password
137
+     */
138
+    Optional<String> getPassword();
139
+
140
+    /**
141
+     * Sets the password for the server.
142
+     *
143
+     * @param password Servers password
144
+     */
145
+    void setPassword(Optional<String> password);
146
+
147
+    /**
148
+     * Is the password valid?
149
+     *
150
+     * @return true if valid
151
+     */
152
+    boolean isPasswordValid();
153
+
154
+    /**
155
+     * Returns the validator for the server password.
156
+     *
157
+     * @return Server port validator
158
+     */
159
+    Validator<String> getPasswordValidator();
160
+
161
+    /**
162
+     * Should be use SSL to connect to the server?
163
+     *
164
+     * @return true if yes
165
+     */
166
+    boolean getSSL();
167
+
168
+    /**
169
+     * Sets whether we should connect to the server over SSL.
170
+     *
171
+     * @param ssl New SSL state
172
+     */
173
+    void setSSL(boolean ssl);
174
+
175
+    /**
176
+     * Should we save these settings as the default?
177
+     *
178
+     * @return true if yes
179
+     */
180
+    boolean getSaveAsDefault();
181
+
182
+    /**
183
+     * Sets whether we should save these settings as the default.
184
+     *
185
+     * @param saveAsDefault true if yes
186
+     */
187
+    void setSaveAsDefault(boolean saveAsDefault);
188
+
189
+    /**
190
+     * Saves the defaults as appropriate and connects to the server.
191
+     */
192
+    void save();
193
+
194
+    /**
195
+     * Are we allowed to save the dialog and connect to the server?
196
+     *
197
+     * @return true if all validations pass
198
+     */
199
+    boolean isSaveAllowed();
200
+
201
+    /**
202
+     * Adds a listener for changes on this model.
203
+     *
204
+     * @param listener Listener to add
205
+     */
206
+    void addListener(NewServerDialogModelListener listener);
207
+
208
+    /**
209
+     * Removes a listener for changes on this model.
210
+     *
211
+     * @param listener Listener to remove
212
+     */
213
+    void removeListener(NewServerDialogModelListener listener);
214
+
215
+}

+ 64
- 0
src/com/dmdirc/interfaces/ui/NewServerDialogModelListener.java View File

@@ -0,0 +1,64 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.interfaces.ui;
24
+
25
+import com.dmdirc.interfaces.config.ConfigProvider;
26
+
27
+import com.google.common.base.Optional;
28
+
29
+import java.util.List;
30
+
31
+/**
32
+ * Listener for various events in an new server dialog model.
33
+ */
34
+public interface NewServerDialogModelListener {
35
+
36
+    /**
37
+     * Called when the selected profile changes.
38
+     *
39
+     * @param oldProfile Old profile
40
+     * @param newProfile New profile
41
+     */
42
+    void selectedProfileChanged(Optional<ConfigProvider> oldProfile,
43
+            Optional<ConfigProvider> newProfile);
44
+
45
+    /**
46
+     * Called when the profile list is changed.
47
+     *
48
+     * @param profiles New profile list
49
+     */
50
+    void profileListChanged(List<ConfigProvider> profiles);
51
+
52
+    /**
53
+     * Called when the details of the server are changed.
54
+     *
55
+     * @param hostname      Server hostname
56
+     * @param port          Server port
57
+     * @param password      Server password
58
+     * @param ssl           Should we connect over SSL
59
+     * @param saveAsDefault Should we save these settings as the default
60
+     */
61
+    void serverDetailsChanged(Optional<String> hostname, Optional<Integer> port,
62
+            Optional<String> password, boolean ssl, boolean saveAsDefault);
63
+
64
+}

+ 311
- 0
src/com/dmdirc/ui/core/newserver/CoreNewServerDialogModel.java View File

@@ -0,0 +1,311 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.ui.core.newserver;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.ClientModule.UserConfig;
27
+import com.dmdirc.ServerManager;
28
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
29
+import com.dmdirc.interfaces.config.ConfigProvider;
30
+import com.dmdirc.interfaces.config.ConfigProviderListener;
31
+import com.dmdirc.interfaces.config.IdentityController;
32
+import com.dmdirc.interfaces.ui.NewServerDialogModel;
33
+import com.dmdirc.interfaces.ui.NewServerDialogModelListener;
34
+import com.dmdirc.util.collections.ListenerList;
35
+import com.dmdirc.util.validators.IntegerPortValidator;
36
+import com.dmdirc.util.validators.ListNotEmptyValidator;
37
+import com.dmdirc.util.validators.PermissiveValidator;
38
+import com.dmdirc.util.validators.ServerNameValidator;
39
+import com.dmdirc.util.validators.Validator;
40
+
41
+import com.google.common.base.Optional;
42
+import com.google.common.collect.ImmutableList;
43
+
44
+import java.net.URI;
45
+import java.net.URISyntaxException;
46
+import java.util.ArrayList;
47
+import java.util.List;
48
+
49
+import javax.inject.Inject;
50
+
51
+import static com.google.common.base.Preconditions.checkNotNull;
52
+
53
+/**
54
+ * Default implementation of a new server dialog model.
55
+ */
56
+public class CoreNewServerDialogModel implements NewServerDialogModel, ConfigProviderListener {
57
+
58
+    private final ListenerList listeners;
59
+    private final AggregateConfigProvider globalConfig;
60
+    private final ConfigProvider userConfig;
61
+    private final ServerManager serverManager;
62
+    private final IdentityController controller;
63
+    private final List<ConfigProvider> profiles;
64
+    private Optional<ConfigProvider> selectedProfile;
65
+    private Optional<String> hostname;
66
+    private Optional<Integer> port;
67
+    private Optional<String> password;
68
+    private boolean ssl;
69
+    private boolean saveAsDefault;
70
+
71
+    @Inject
72
+    public CoreNewServerDialogModel(
73
+            @GlobalConfig final AggregateConfigProvider globalConfig,
74
+            @UserConfig final ConfigProvider userConfig,
75
+            final IdentityController identityController,
76
+            final ServerManager serverManager) {
77
+        this.globalConfig = globalConfig;
78
+        this.userConfig = userConfig;
79
+        this.controller = identityController;
80
+        this.serverManager = serverManager;
81
+        listeners = new ListenerList();
82
+        profiles = new ArrayList<>(5);
83
+        selectedProfile = Optional.absent();
84
+        hostname = Optional.absent();
85
+        port = Optional.absent();
86
+        password = Optional.absent();
87
+        ssl = false;
88
+        saveAsDefault = false;
89
+    }
90
+
91
+    @Override
92
+    public void loadModel() {
93
+        controller.registerIdentityListener("profile", this);
94
+        for (ConfigProvider provider : controller.getProvidersByType("profile")) {
95
+            profiles.add(provider);
96
+        }
97
+        hostname = Optional.fromNullable(globalConfig.getOption("newserver", "hostname"));
98
+        port = Optional.fromNullable(globalConfig.getOptionInt("newserver", "port"));
99
+        password = Optional.fromNullable(globalConfig.getOption("newserver", "password"));
100
+        ssl = globalConfig.getOptionBool("newserver", "ssl");
101
+        saveAsDefault = false;
102
+        listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
103
+                port, password, ssl, saveAsDefault);
104
+    }
105
+
106
+    @Override
107
+    public List<ConfigProvider> getProfileList() {
108
+        return ImmutableList.copyOf(profiles);
109
+    }
110
+
111
+    @Override
112
+    public Optional<ConfigProvider> getSelectedProfile() {
113
+        return selectedProfile;
114
+    }
115
+
116
+    @Override
117
+    public void setSelectedProfile(final Optional<ConfigProvider> selectedProfile) {
118
+        checkNotNull(selectedProfile);
119
+        final Optional<ConfigProvider> oldSelectedProfile = this.selectedProfile;
120
+        this.selectedProfile = selectedProfile;
121
+        listeners.getCallable(NewServerDialogModelListener.class).selectedProfileChanged(
122
+                oldSelectedProfile, selectedProfile);
123
+    }
124
+
125
+    @Override
126
+    public boolean isProfileListValid() {
127
+        return !getProfileListValidator().validate(getProfileList()).isFailure();
128
+    }
129
+
130
+    @Override
131
+    public Validator<List<ConfigProvider>> getProfileListValidator() {
132
+        return new ListNotEmptyValidator<ConfigProvider>();
133
+    }
134
+
135
+    @Override
136
+    public Optional<String> getHostname() {
137
+        return hostname;
138
+    }
139
+
140
+    @Override
141
+    public void setHostname(final Optional<String> hostname) {
142
+        checkNotNull(hostname);
143
+        this.hostname = hostname;
144
+        listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
145
+                port, password, ssl, saveAsDefault);
146
+    }
147
+
148
+    @Override
149
+    public boolean isHostnameValid() {
150
+        return getHostname().isPresent() && !getHostnameValidator().validate(getHostname().get()).
151
+                isFailure();
152
+    }
153
+
154
+    @Override
155
+    public Validator<String> getHostnameValidator() {
156
+        return new ServerNameValidator();
157
+    }
158
+
159
+    @Override
160
+    public Optional<Integer> getPort() {
161
+        return port;
162
+    }
163
+
164
+    @Override
165
+    public void setPort(final Optional<Integer> port) {
166
+        checkNotNull(port);
167
+        this.port = port;
168
+        listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
169
+                port, password, ssl, saveAsDefault);
170
+    }
171
+
172
+    @Override
173
+    public boolean isPortValid() {
174
+        return getPort().isPresent() && !getPortValidator().validate(getPort().get()).isFailure();
175
+    }
176
+
177
+    @Override
178
+    public Validator<Integer> getPortValidator() {
179
+        return new IntegerPortValidator();
180
+    }
181
+
182
+    @Override
183
+    public Optional<String> getPassword() {
184
+        return password;
185
+    }
186
+
187
+    @Override
188
+    public void setPassword(final Optional<String> password) {
189
+        checkNotNull(password);
190
+        this.password = password;
191
+        listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
192
+                port, password, ssl, saveAsDefault);
193
+    }
194
+
195
+    @Override
196
+    public boolean isPasswordValid() {
197
+        return !getPassword().isPresent() || !getPasswordValidator().validate(getPassword().get()).
198
+                isFailure();
199
+    }
200
+
201
+    @Override
202
+    public Validator<String> getPasswordValidator() {
203
+        return new PermissiveValidator<>();
204
+    }
205
+
206
+    @Override
207
+    public boolean getSSL() {
208
+        return ssl;
209
+    }
210
+
211
+    @Override
212
+    public void setSSL(final boolean ssl) {
213
+        this.ssl = ssl;
214
+        listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
215
+                port, password, ssl, saveAsDefault);
216
+    }
217
+
218
+    @Override
219
+    public boolean getSaveAsDefault() {
220
+        return saveAsDefault;
221
+    }
222
+
223
+    @Override
224
+    public void setSaveAsDefault(final boolean saveAsDefault) {
225
+        this.saveAsDefault = saveAsDefault;
226
+        listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
227
+                port, password, ssl, saveAsDefault);
228
+    }
229
+
230
+    @Override
231
+    public void save() {
232
+        if (saveAsDefault) {
233
+            userConfig.
234
+                    setOption("newserver", "hostname", hostname.isPresent() ? hostname.get() : "");
235
+            userConfig.setOption("newserver", "port", port.isPresent() ? port.get() : 6667);
236
+            userConfig.setOption("newserver", "password",
237
+                    password.isPresent() ? password.get() : "");
238
+            userConfig.setOption("newserver", "ssl", ssl);
239
+        }
240
+        try {
241
+            if (selectedProfile.isPresent()) {
242
+                serverManager.connectToAddress(getServerURI(), selectedProfile.get());
243
+            } else {
244
+                serverManager.connectToAddress(getServerURI());
245
+            }
246
+        } catch (URISyntaxException ex) {
247
+            //This is tested in isSaveAllowed, shouldn't happen here.
248
+        }
249
+    }
250
+
251
+    @Override
252
+    public boolean isSaveAllowed() {
253
+        try {
254
+            getServerURI();
255
+        } catch (URISyntaxException ex) {
256
+            return false;
257
+        }
258
+        return isHostnameValid() && isPortValid() && isPasswordValid() && isProfileListValid();
259
+    }
260
+
261
+    @Override
262
+    public void addListener(final NewServerDialogModelListener listener) {
263
+        checkNotNull(listener);
264
+        listeners.add(NewServerDialogModelListener.class, listener);
265
+    }
266
+
267
+    @Override
268
+    public void removeListener(final NewServerDialogModelListener listener) {
269
+        checkNotNull(listener);
270
+        listeners.remove(NewServerDialogModelListener.class, listener);
271
+    }
272
+
273
+    @Override
274
+    public void configProviderAdded(final ConfigProvider configProvider) {
275
+        checkNotNull(configProvider);
276
+        profiles.add(configProvider);
277
+        listeners.getCallable(NewServerDialogModelListener.class).profileListChanged(
278
+                ImmutableList.copyOf(profiles));
279
+    }
280
+
281
+    @Override
282
+    public void configProviderRemoved(final ConfigProvider configProvider) {
283
+        checkNotNull(configProvider);
284
+        if (Optional.fromNullable(configProvider).equals(selectedProfile)) {
285
+            final Optional<ConfigProvider> oldSelectedProfile = selectedProfile;
286
+            selectedProfile = Optional.absent();
287
+            listeners.getCallable(NewServerDialogModelListener.class).selectedProfileChanged(
288
+                    oldSelectedProfile, Optional.<ConfigProvider>absent());
289
+
290
+        }
291
+        profiles.remove(configProvider);
292
+        listeners.getCallable(NewServerDialogModelListener.class).profileListChanged(
293
+                ImmutableList.copyOf(profiles));
294
+    }
295
+
296
+    /**
297
+     * Gets the URI for the details in the dialog.
298
+     *
299
+     * @return Returns the URI the details represent
300
+     *
301
+     * @throws URISyntaxException    If the resulting URI is invalid
302
+     */
303
+    private URI getServerURI() throws URISyntaxException {
304
+        return new URI("irc" + (ssl ? "s" : ""),
305
+                password.isPresent() ? password.get() : "",
306
+                hostname.isPresent() ? hostname.get() : "",
307
+                port.isPresent() ? port.get() : 6667,
308
+                null, null, null);
309
+    }
310
+
311
+}

+ 52
- 0
src/com/dmdirc/ui/core/newserver/NewServerDialogModelAdapter.java View File

@@ -0,0 +1,52 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.ui.core.newserver;
24
+
25
+import com.dmdirc.interfaces.config.ConfigProvider;
26
+import com.dmdirc.interfaces.ui.NewServerDialogModelListener;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.List;
31
+
32
+/**
33
+ * An abstract adapter class for receiving new server dialog model events. The methods in this class
34
+ * are empty. This class exists as convenience for creating listener objects.
35
+ */
36
+public class NewServerDialogModelAdapter implements NewServerDialogModelListener {
37
+
38
+    @Override
39
+    public void selectedProfileChanged(final Optional<ConfigProvider> oldProfile,
40
+            final Optional<ConfigProvider> newProfile) {
41
+    }
42
+
43
+    @Override
44
+    public void profileListChanged(final List<ConfigProvider> profiles) {
45
+    }
46
+
47
+    @Override
48
+    public void serverDetailsChanged(final Optional<String> hostname, final Optional<Integer> port,
49
+            final Optional<String> password, final boolean ssl, final boolean saveAsDefault) {
50
+    }
51
+
52
+}

+ 426
- 0
test/com/dmdirc/ui/core/newserver/CoreNewServerDialogModelTest.java View File

@@ -0,0 +1,426 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.ui.core.newserver;
24
+
25
+import com.dmdirc.ServerManager;
26
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
27
+import com.dmdirc.interfaces.config.ConfigProvider;
28
+import com.dmdirc.interfaces.config.IdentityController;
29
+import com.dmdirc.interfaces.ui.NewServerDialogModelListener;
30
+
31
+import com.google.common.base.Optional;
32
+import com.google.common.collect.Lists;
33
+
34
+import java.net.URI;
35
+import java.net.URISyntaxException;
36
+
37
+import org.junit.Before;
38
+import org.junit.Test;
39
+import org.junit.runner.RunWith;
40
+import org.mockito.Mock;
41
+import org.mockito.runners.MockitoJUnitRunner;
42
+
43
+import static org.junit.Assert.assertEquals;
44
+import static org.junit.Assert.assertFalse;
45
+import static org.junit.Assert.assertTrue;
46
+import static org.mockito.Matchers.any;
47
+import static org.mockito.Mockito.never;
48
+import static org.mockito.Mockito.verify;
49
+import static org.mockito.Mockito.when;
50
+
51
+@RunWith(MockitoJUnitRunner.class)
52
+public class CoreNewServerDialogModelTest {
53
+
54
+    @Mock private AggregateConfigProvider globalConfig;
55
+    @Mock private ConfigProvider userConfig;
56
+    @Mock private IdentityController controller;
57
+    @Mock private ServerManager serverManager;
58
+    @Mock private ConfigProvider profile1;
59
+    @Mock private ConfigProvider profile2;
60
+    @Mock private NewServerDialogModelListener listener;
61
+
62
+    @Before
63
+    public void setupMocks() {
64
+        when(controller.getProvidersByType("profile")).thenReturn(
65
+                Lists.newArrayList(profile1, profile2));
66
+        when(globalConfig.getOption("newserver", "hostname")).thenReturn("hostname");
67
+        when(globalConfig.getOptionInt("newserver", "port")).thenReturn(1111);
68
+        when(globalConfig.getOption("newserver", "password")).thenReturn("password");
69
+        when(globalConfig.getOptionBool("newserver", "ssl")).thenReturn(true);
70
+    }
71
+
72
+    @Test
73
+    public void testGetProfiles() {
74
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
75
+                controller, serverManager);
76
+        instance.loadModel();
77
+        assertEquals("loadModel: ", Lists.newArrayList(profile1, profile2), instance.
78
+                getProfileList());
79
+    }
80
+
81
+    @Test
82
+    public void testSelectedProfile() {
83
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
84
+                controller, serverManager);
85
+        instance.loadModel();
86
+        assertEquals("testSelectedProfile: ", Optional.absent(), instance.getSelectedProfile());
87
+        instance.setSelectedProfile(Optional.fromNullable(profile1));
88
+        assertEquals("testSelectedProfile", Optional.fromNullable(profile1),
89
+                instance.getSelectedProfile());
90
+    }
91
+
92
+    @Test
93
+    public void testProfileValidatorEmpty() {
94
+        when(controller.getProvidersByType("profile")).thenReturn(
95
+                Lists.<ConfigProvider>newArrayList());
96
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
97
+                controller, serverManager);
98
+        instance.loadModel();
99
+        assertFalse("testProfileValidatorEmpty: ", instance.isProfileListValid());
100
+    }
101
+
102
+    @Test
103
+    public void testProfileValidatorNotEmpty() {
104
+        when(controller.getProvidersByType("profile")).thenReturn(
105
+                Lists.newArrayList(profile1, profile2));
106
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
107
+                controller, serverManager);
108
+        instance.loadModel();
109
+        assertTrue("testProfileValidatorNotEmpty: ", instance.isProfileListValid());
110
+    }
111
+
112
+    @Test
113
+    public void testHostname() {
114
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
115
+                controller, serverManager);
116
+        instance.loadModel();
117
+        assertEquals("testHostname: ", Optional.fromNullable("hostname"), instance.getHostname());
118
+        instance.setHostname(Optional.fromNullable("test"));
119
+        assertEquals("testHostname: ", Optional.fromNullable("test"), instance.getHostname());
120
+    }
121
+
122
+    @Test
123
+    public void testHostnameValidatorValid() {
124
+        when(globalConfig.getOption("newserver", "hostname")).thenReturn("hostname");
125
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
126
+                controller, serverManager);
127
+        instance.loadModel();
128
+        assertTrue("testHostnameValidatorValid: ", instance.isHostnameValid());
129
+    }
130
+
131
+    @Test
132
+    public void testHostnameValidatorInvalid() {
133
+        when(globalConfig.getOption("newserver", "hostname")).thenReturn("~!£$^£$%^&");
134
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
135
+                controller, serverManager);
136
+        instance.loadModel();
137
+        assertFalse("testHostnameValidatorInvalid: ", instance.isHostnameValid());
138
+    }
139
+
140
+    @Test
141
+    public void testPort() {
142
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
143
+                controller, serverManager);
144
+        instance.loadModel();
145
+        assertEquals("testPort: ", Optional.fromNullable(1111), instance.getPort());
146
+        instance.setPort(Optional.fromNullable(5678));
147
+        assertEquals("testPort: ", Optional.fromNullable(5678), instance.getPort());
148
+    }
149
+
150
+    @Test
151
+    public void testPortValidatorValid() {
152
+        when(globalConfig.getOptionInt("newserver", "port")).thenReturn(1111);
153
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
154
+                controller, serverManager);
155
+        instance.loadModel();
156
+        assertTrue("testPortValidatorValid: ", instance.isPortValid());
157
+    }
158
+
159
+    @Test
160
+    public void testPortValidatorTooLow() {
161
+        when(globalConfig.getOptionInt("newserver", "port")).thenReturn(-1);
162
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
163
+                controller, serverManager);
164
+        instance.loadModel();
165
+        assertFalse("testPortValidatorTooLow: ", instance.isPortValid());
166
+    }
167
+
168
+    @Test
169
+    public void testPortValidatorTooHigh() {
170
+        when(globalConfig.getOptionInt("newserver", "port")).thenReturn(65536);
171
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
172
+                controller, serverManager);
173
+        instance.loadModel();
174
+        assertFalse("testPortValidatorTooHigh: ", instance.isPortValid());
175
+    }
176
+
177
+    @Test
178
+    public void testPassword() {
179
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
180
+                controller, serverManager);
181
+        instance.loadModel();
182
+        assertEquals("testPassword: ", Optional.fromNullable("password"), instance.getPassword());
183
+        instance.setPassword(Optional.fromNullable("test"));
184
+        assertEquals("testPassword: ", Optional.fromNullable("test"), instance.getPassword());
185
+    }
186
+
187
+    @Test
188
+    public void testPasswordValidatorValid() {
189
+        when(globalConfig.getOption("newserver", "password")).thenReturn("password");
190
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
191
+                controller, serverManager);
192
+        instance.loadModel();
193
+        assertTrue("testPasswordValidatorValid: ", instance.isPortValid());
194
+    }
195
+
196
+    @Test
197
+    public void testSSL() {
198
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
199
+                controller, serverManager);
200
+        instance.loadModel();
201
+        assertTrue("testSSL: ", instance.getSSL());
202
+        instance.setSSL(false);
203
+        assertFalse("testSSL: ", instance.getSSL());
204
+    }
205
+
206
+    @Test
207
+    public void testSaveAsDefault() {
208
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
209
+                controller, serverManager);
210
+        instance.loadModel();
211
+        assertFalse("testSaveAsDefault:", instance.getSaveAsDefault());
212
+        instance.setSaveAsDefault(true);
213
+        assertTrue("testSaveAsDefault:", instance.getSaveAsDefault());
214
+    }
215
+
216
+    @Test
217
+    public void testIsSaveAllowedValid() {
218
+        when(globalConfig.getOption("newserver", "hostname")).thenReturn("hostname");
219
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
220
+                controller, serverManager);
221
+        instance.loadModel();
222
+        assertTrue("testIsSaveAllowedValid: ", instance.isSaveAllowed());
223
+    }
224
+
225
+    @Test
226
+    public void testIsSaveAllowedInvalidHostname() {
227
+        when(globalConfig.getOption("newserver", "hostname")).thenReturn("~!£$^£$%^&");
228
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
229
+                controller, serverManager);
230
+        instance.loadModel();
231
+        assertFalse("testIsSaveAllowedInvalidHostname: ", instance.isSaveAllowed());
232
+    }
233
+
234
+    @Test
235
+    public void testIsSaveAllowedInvalidPort() {
236
+        when(globalConfig.getOption("newserver", "hostname")).thenReturn("-1");
237
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
238
+                controller, serverManager);
239
+        instance.loadModel();
240
+        assertFalse("testIsSaveAllowedInvalidPort: ", instance.isSaveAllowed());
241
+    }
242
+
243
+    @Test
244
+    public void testSaveSaveDefaults() {
245
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
246
+                controller, serverManager);
247
+        instance.loadModel();
248
+        instance.setSaveAsDefault(true);
249
+        instance.save();
250
+        verify(userConfig).setOption("newserver", "hostname", "hostname");
251
+        verify(userConfig).setOption("newserver", "port", 1111);
252
+        verify(userConfig).setOption("newserver", "password", "password");
253
+        verify(userConfig).setOption("newserver", "ssl", true);
254
+    }
255
+
256
+    @Test
257
+    public void testSaveNotDefaults() {
258
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
259
+                controller, serverManager);
260
+        instance.loadModel();
261
+        instance.setSaveAsDefault(false);
262
+        instance.save();
263
+        verify(userConfig, never()).setOption("newserver", "hostname", "hostname");
264
+        verify(userConfig, never()).setOption("newserver", "port", 1111);
265
+        verify(userConfig, never()).setOption("newserver", "password", "password");
266
+        verify(userConfig, never()).setOption("newserver", "ssl", true);
267
+    }
268
+
269
+    @Test
270
+    public void testSaveConnectWithoutProfile() throws URISyntaxException {
271
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
272
+                controller, serverManager);
273
+        instance.loadModel();
274
+        instance.save();
275
+        verify(serverManager).connectToAddress(new URI("ircs://password@hostname:1111"));
276
+        verify(serverManager, never()).connectToAddress(any(URI.class), any(ConfigProvider.class));
277
+    }
278
+
279
+    @Test
280
+    public void testSaveConnectWithProfile() throws URISyntaxException {
281
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
282
+                controller, serverManager);
283
+        instance.loadModel();
284
+        instance.setSelectedProfile(Optional.fromNullable(profile1));
285
+        instance.save();
286
+        verify(serverManager, never()).connectToAddress(any(URI.class));
287
+        verify(serverManager).connectToAddress(new URI("ircs://password@hostname:1111"), profile1);
288
+    }
289
+
290
+    @Test
291
+    public void testAddConfigProvider() {
292
+        when(controller.getProvidersByType("profile")).thenReturn(Lists.newArrayList(profile1));
293
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
294
+                controller, serverManager);
295
+        instance.loadModel();
296
+        assertEquals("testAddConfigProvider:", Lists.newArrayList(profile1),
297
+                instance.getProfileList());
298
+        instance.configProviderAdded(profile2);
299
+        assertEquals("testAddConfigProvider:", Lists.newArrayList(profile1, profile2),
300
+                instance.getProfileList());
301
+    }
302
+
303
+    @Test
304
+    public void testRemoveConfigProvider() {
305
+        when(controller.getProvidersByType("profile")).thenReturn(
306
+                Lists.newArrayList(profile1, profile2));
307
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
308
+                controller, serverManager);
309
+        instance.loadModel();
310
+        assertEquals("testRemoveConfigProvider:", Lists.newArrayList(profile1, profile2),
311
+                instance.getProfileList());
312
+        instance.configProviderRemoved(profile2);
313
+        assertEquals("testRemoveConfigProvider:", Lists.newArrayList(profile1),
314
+                instance.getProfileList());
315
+    }
316
+
317
+    @Test
318
+    public void testRemoveConfigProviderSelectedProfile() {
319
+        when(controller.getProvidersByType("profile")).thenReturn(
320
+                Lists.newArrayList(profile1, profile2));
321
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
322
+                controller, serverManager);
323
+        instance.loadModel();
324
+        instance.setSelectedProfile(Optional.fromNullable(profile2));
325
+        assertEquals("testRemoveConfigProviderSelectedProfile:",
326
+                Lists.newArrayList(profile1, profile2),
327
+                instance.getProfileList());
328
+        assertEquals("testRemoveConfigProviderSelectedProfile:", Optional.fromNullable(profile2),
329
+                instance.getSelectedProfile());
330
+        instance.configProviderRemoved(profile2);
331
+        assertEquals("testRemoveConfigProviderSelectedProfile:", Lists.newArrayList(profile1),
332
+                instance.getProfileList());
333
+        assertEquals("testRemoveConfigProviderSelectedProfile:", Optional.absent(),
334
+                instance.getSelectedProfile());
335
+    }
336
+
337
+    @Test
338
+    public void testListenerSelectedProfileChanged() {
339
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
340
+                controller, serverManager);
341
+        instance.loadModel();
342
+        assertEquals("testListenerSelectedProfileChanged: ", Optional.<ConfigProvider>absent(),
343
+                instance.getSelectedProfile());
344
+        instance.addListener(listener);
345
+        instance.setSelectedProfile(Optional.fromNullable(profile1));
346
+        assertEquals("testListenerSelectedProfileChanged: ", Optional.fromNullable(profile1),
347
+                instance.getSelectedProfile());
348
+        verify(listener).selectedProfileChanged(Optional.<ConfigProvider>absent(),
349
+                Optional.fromNullable(profile1));
350
+    }
351
+
352
+    @Test
353
+    public void testListenerProfileListChangedRemoved() {
354
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
355
+                controller, serverManager);
356
+        instance.loadModel();
357
+        instance.addListener(listener);
358
+        assertEquals("testListenerProfileListChangedRemoved: ",
359
+                Lists.newArrayList(profile1, profile2), instance.getProfileList());
360
+        instance.configProviderRemoved(profile2);
361
+        assertEquals("testListenerProfileListChangedRemoved:", Lists.newArrayList(profile1),
362
+                instance.getProfileList());
363
+        verify(listener).profileListChanged(Lists.newArrayList(profile1));
364
+    }
365
+
366
+    @Test
367
+    public void testListenerProfileListChangedAdded() {
368
+        when(controller.getProvidersByType("profile"))
369
+                .thenReturn(Lists.newArrayList(profile1));
370
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
371
+                controller, serverManager);
372
+        instance.loadModel();
373
+        instance.addListener(listener);
374
+        assertEquals("testListenerProfileListChangedAdded: ", Lists.newArrayList(profile1),
375
+                instance.getProfileList());
376
+        instance.configProviderAdded(profile2);
377
+        assertEquals("testListenerProfileListChangedAdded:", Lists.newArrayList(profile1, profile2),
378
+                instance.getProfileList());
379
+        verify(listener).profileListChanged(Lists.newArrayList(profile1, profile2));
380
+    }
381
+
382
+    @Test
383
+    public void testListenerServerDetailsChangedPort() {
384
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
385
+                controller, serverManager);
386
+        instance.loadModel();
387
+        instance.addListener(listener);
388
+        instance.setPort(Optional.fromNullable(9999));
389
+        verify(listener).serverDetailsChanged(Optional.fromNullable("hostname"),
390
+                Optional.fromNullable(9999), Optional.fromNullable("password"), true, false);
391
+    }
392
+
393
+    @Test
394
+    public void testListenerServerDetailsChangedPassword() {
395
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
396
+                controller, serverManager);
397
+        instance.loadModel();
398
+        instance.addListener(listener);
399
+        instance.setPassword(Optional.fromNullable("password-test"));
400
+        verify(listener).serverDetailsChanged(Optional.fromNullable("hostname"),
401
+                Optional.fromNullable(1111), Optional.fromNullable("password-test"), true, false);
402
+    }
403
+
404
+    @Test
405
+    public void testListenerServerDetailsChangedSSL() {
406
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
407
+                controller, serverManager);
408
+        instance.loadModel();
409
+        instance.addListener(listener);
410
+        instance.setSSL(false);
411
+        verify(listener).serverDetailsChanged(Optional.fromNullable("hostname"),
412
+                Optional.fromNullable(1111), Optional.fromNullable("password"), false, false);
413
+    }
414
+
415
+    @Test
416
+    public void testListenerServerDetailsChangedSaveAsDefault() {
417
+        CoreNewServerDialogModel instance = new CoreNewServerDialogModel(globalConfig, userConfig,
418
+                controller, serverManager);
419
+        instance.loadModel();
420
+        instance.addListener(listener);
421
+        instance.setSaveAsDefault(true);
422
+        verify(listener).serverDetailsChanged(Optional.fromNullable("hostname"),
423
+                Optional.fromNullable(1111), Optional.fromNullable("password"), true, true);
424
+    }
425
+
426
+}

Loading…
Cancel
Save