Browse Source

Add a GlobalAutoCommandsDialogModel.

pull/555/head
Greg Holmes 9 years ago
parent
commit
824c60f17f

+ 78
- 0
src/com/dmdirc/interfaces/ui/GlobalAutoCommandsDialogModel.java View File

@@ -0,0 +1,78 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.commandparser.auto.AutoCommand;
26
+import com.dmdirc.util.validators.Validator;
27
+
28
+/**
29
+ * Represents a dialog that can edit the global {@link AutoCommand} in the client.
30
+ */
31
+public interface GlobalAutoCommandsDialogModel {
32
+
33
+    /**
34
+     * Loads the model.  This should be called before interacting with the model in any way.
35
+     */
36
+    void load();
37
+
38
+    /**
39
+     * Returns the current response to be included in the global {@link AutoCommand}.
40
+     *
41
+     * @return Current response.  Will never be null, may be empty.
42
+     */
43
+    String getResponse();
44
+
45
+    /**
46
+     * Sets the response to be included in the global {@link AutoCommand}.
47
+     *
48
+     * @param response New response.  Should never be null, this may be empty, but the dialog should
49
+     *                 not try to save whilst it is empty.
50
+     */
51
+    void setResponse(final String response);
52
+
53
+    /**
54
+     * Returns the validator for a {@Link AutoCommand}'s response.
55
+     *
56
+     * @return Validator for a response string
57
+     */
58
+    Validator<String> getResponseValidator();
59
+
60
+    /**
61
+     * Is the current response valid?
62
+     *
63
+     * @return true iif the response is valid
64
+     */
65
+    boolean isResponseValid();
66
+
67
+    /**
68
+     * Is the dialog allowed to be saved?
69
+     *
70
+     * @return true iif the dialog can be saved
71
+     */
72
+    boolean isSaveAllowed();
73
+
74
+    /**
75
+     * Saves the current state into the global {@link AutoCommand}.
76
+     */
77
+    void save();
78
+}

+ 121
- 0
src/com/dmdirc/ui/core/autocommands/CoreGlobalAutoCommandsDialogModel.java View File

@@ -0,0 +1,121 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.autocommands;
24
+
25
+import com.dmdirc.commandparser.auto.AutoCommand;
26
+import com.dmdirc.commandparser.auto.AutoCommandManager;
27
+import com.dmdirc.commandparser.auto.AutoCommandType;
28
+import com.dmdirc.interfaces.ui.GlobalAutoCommandsDialogModel;
29
+import com.dmdirc.util.validators.Validator;
30
+
31
+import java.util.Optional;
32
+
33
+import javax.inject.Inject;
34
+
35
+import static com.google.common.base.Preconditions.checkNotNull;
36
+import static com.google.common.base.Preconditions.checkState;
37
+
38
+/**
39
+ * Basic implementation of an {@link GlobalAutoCommandsDialogModel}.
40
+ */
41
+public class CoreGlobalAutoCommandsDialogModel implements GlobalAutoCommandsDialogModel {
42
+
43
+    private final AutoCommandManager autoCommandManager;
44
+    private final ResponseValidator responseValidator;
45
+    private boolean loaded;
46
+    private Optional<AutoCommand> globalAutoCommand;
47
+    private MutableAutoCommand autoCommand;
48
+
49
+    @Inject
50
+    public CoreGlobalAutoCommandsDialogModel(final AutoCommandManager autoCommandManager,
51
+            final ResponseValidator responseValidator) {
52
+        this.autoCommandManager = autoCommandManager;
53
+        this.responseValidator = responseValidator;
54
+        autoCommand = getAutoCommand(Optional.empty());
55
+    }
56
+
57
+    @Override
58
+    public void load() {
59
+        loaded = true;
60
+        globalAutoCommand = autoCommandManager.getGlobalAutoCommand();
61
+        autoCommand = getAutoCommand(globalAutoCommand);
62
+    }
63
+
64
+    @Override
65
+    public String getResponse() {
66
+        checkState(loaded);
67
+        return autoCommand.getResponse();
68
+    }
69
+
70
+    @Override
71
+    public void setResponse(final String response) {
72
+        checkState(loaded);
73
+        checkNotNull(response);
74
+        autoCommand.setResponse(response);
75
+    }
76
+
77
+    @Override
78
+    public Validator<String> getResponseValidator() {
79
+        checkState(loaded);
80
+        return responseValidator;
81
+    }
82
+
83
+    @Override
84
+    public boolean isResponseValid() {
85
+        checkState(loaded);
86
+        return !getResponseValidator().validate(autoCommand.getResponse()).isFailure();
87
+    }
88
+
89
+    @Override
90
+    public boolean isSaveAllowed() {
91
+        checkState(loaded);
92
+        return isResponseValid();
93
+    }
94
+
95
+    @Override
96
+    public void save() {
97
+        checkState(loaded);
98
+        checkState(isSaveAllowed());
99
+        if (globalAutoCommand.isPresent()) {
100
+            autoCommandManager.replaceAutoCommand(globalAutoCommand.get(), getAutoCommand(autoCommand));
101
+        } else {
102
+            autoCommandManager.addAutoCommand(getAutoCommand(autoCommand));
103
+        }
104
+    }
105
+
106
+    private MutableAutoCommand getAutoCommand(final Optional<AutoCommand> command) {
107
+        if (command.isPresent()) {
108
+            final AutoCommand c = command.get();
109
+            return new MutableAutoCommand(c.getServer(),
110
+                    c.getNetwork(), c.getProfile(), c.getResponse(), c.getType());
111
+        } else {
112
+            return new MutableAutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "",
113
+                    AutoCommandType.GLOBAL);
114
+        }
115
+    }
116
+
117
+    private AutoCommand getAutoCommand(final MutableAutoCommand command) {
118
+        return AutoCommand.create(command.getServer(), command.getNetwork(), command.getProfile(),
119
+                command.getResponse());
120
+    }
121
+}

+ 58
- 0
src/com/dmdirc/ui/core/autocommands/ResponseValidator.java View File

@@ -0,0 +1,58 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.autocommands;
24
+
25
+import com.dmdirc.commandparser.auto.AutoCommand;
26
+import com.dmdirc.interfaces.CommandController;
27
+import com.dmdirc.util.validators.ValidationResponse;
28
+import com.dmdirc.util.validators.Validator;
29
+
30
+import com.google.common.base.Strings;
31
+
32
+import javax.inject.Inject;
33
+
34
+/**
35
+ * Validates the response for an {@link AutoCommand}.
36
+ */
37
+public class ResponseValidator implements Validator<String> {
38
+
39
+    private final String commandChar;
40
+
41
+    @Inject
42
+    public ResponseValidator(final CommandController controller) {
43
+        commandChar = String.valueOf(controller.getCommandChar());
44
+    }
45
+
46
+    @Override
47
+    public ValidationResponse validate(final String object) {
48
+        if (Strings.isNullOrEmpty(object)) {
49
+            return new ValidationResponse("Response cannot be empty");
50
+        }
51
+        for (String line : object.split("\n")) {
52
+            if (line.startsWith(commandChar)) {
53
+                return new ValidationResponse("Lines should not start with a command character");
54
+            }
55
+        }
56
+        return new ValidationResponse();
57
+    }
58
+}

+ 192
- 0
test/com/dmdirc/ui/core/autocommands/CoreGlobalAutoCommandsDialogModelTest.java View File

@@ -0,0 +1,192 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.autocommands;
24
+
25
+import com.dmdirc.commandparser.auto.AutoCommand;
26
+import com.dmdirc.commandparser.auto.AutoCommandManager;
27
+import com.dmdirc.interfaces.CommandController;
28
+
29
+import java.util.Optional;
30
+
31
+import org.junit.Before;
32
+import org.junit.Test;
33
+import org.junit.runner.RunWith;
34
+import org.mockito.Mock;
35
+import org.mockito.runners.MockitoJUnitRunner;
36
+
37
+import static org.junit.Assert.assertEquals;
38
+import static org.junit.Assert.assertFalse;
39
+import static org.junit.Assert.assertNotNull;
40
+import static org.junit.Assert.assertTrue;
41
+import static org.mockito.Matchers.eq;
42
+import static org.mockito.Mockito.verify;
43
+import static org.mockito.Mockito.when;
44
+
45
+@RunWith(MockitoJUnitRunner.class)
46
+public class CoreGlobalAutoCommandsDialogModelTest {
47
+
48
+    @Mock private AutoCommandManager autoCommandManager;
49
+    @Mock private CommandController commandController;
50
+    @Mock private AutoCommand autoCommand;
51
+    private CoreGlobalAutoCommandsDialogModel instance;
52
+
53
+    @Before
54
+    public void setUp() throws Exception {
55
+        when(autoCommand.getServer()).thenReturn(Optional.empty());
56
+        when(autoCommand.getNetwork()).thenReturn(Optional.empty());
57
+        when(autoCommand.getProfile()).thenReturn(Optional.empty());
58
+        when(autoCommand.getResponse()).thenReturn("response");
59
+        when(commandController.getCommandChar()).thenReturn('/');
60
+        when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.of(autoCommand));
61
+        instance = new CoreGlobalAutoCommandsDialogModel(autoCommandManager,
62
+                new ResponseValidator(commandController));
63
+    }
64
+
65
+    @Test(expected = IllegalStateException.class)
66
+    public void testLoad_NotCalled() throws Exception {
67
+        instance = new CoreGlobalAutoCommandsDialogModel(autoCommandManager,
68
+                new ResponseValidator(commandController));
69
+        instance.getResponse();
70
+    }
71
+
72
+    @Test
73
+    public void testGetResponse_Empty() throws Exception {
74
+        when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
75
+        instance.load();
76
+        assertEquals("", instance.getResponse());
77
+    }
78
+
79
+    @Test
80
+    public void testGetResponse_Complete() throws Exception {
81
+        instance.load();
82
+        assertEquals("response", instance.getResponse());
83
+    }
84
+
85
+    @Test(expected = NullPointerException.class)
86
+    public void testSetResponse_Null() throws Exception {
87
+        instance.load();
88
+        assertEquals("response", instance.getResponse());
89
+        instance.setResponse(null);
90
+    }
91
+
92
+    @Test
93
+    public void testSetResponse_empty() throws Exception {
94
+        instance.load();
95
+        assertEquals("response", instance.getResponse());
96
+        instance.setResponse("");
97
+        assertEquals("", instance.getResponse());
98
+    }
99
+
100
+    @Test
101
+    public void testSetResponse() throws Exception {
102
+        instance.load();
103
+        assertEquals("response", instance.getResponse());
104
+        instance.setResponse("test");
105
+        assertEquals("test", instance.getResponse());
106
+    }
107
+
108
+    @Test
109
+    public void testGetResponseValidator() throws Exception {
110
+        instance.load();
111
+        assertNotNull(instance.getResponseValidator());
112
+    }
113
+
114
+    @Test
115
+    public void testIsResponseValid_Initially() throws Exception {
116
+        instance.load();
117
+        assertTrue(instance.isResponseValid());
118
+    }
119
+
120
+    @Test
121
+    public void testIsResponseValid_EmptyGlobal() throws Exception {
122
+        when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
123
+        instance.load();
124
+        assertFalse(instance.isResponseValid());
125
+    }
126
+
127
+    @Test
128
+    public void testIsResponseValid_Empty() throws Exception {
129
+        instance.load();
130
+        instance.setResponse("");
131
+        assertFalse(instance.isResponseValid());
132
+    }
133
+
134
+    @Test
135
+    public void testIsResponseValid_CommandChar() throws Exception {
136
+        instance.load();
137
+        instance.setResponse("/moo");
138
+        assertFalse(instance.isResponseValid());
139
+    }
140
+
141
+    @Test
142
+    public void testIsResponseValid() throws Exception {
143
+        instance.load();
144
+        instance.setResponse("moo");
145
+        assertTrue(instance.isResponseValid());
146
+    }
147
+
148
+    @Test
149
+    public void testIsSaveAllowed_InvalidResponse() throws Exception {
150
+        instance.load();
151
+        assertTrue(instance.isSaveAllowed());
152
+        instance.setResponse("");
153
+        assertFalse(instance.isSaveAllowed());
154
+    }
155
+
156
+    @Test
157
+    public void testIsSaveAllowed() throws Exception {
158
+        instance.load();
159
+        assertTrue(instance.isSaveAllowed());
160
+        instance.setResponse("test");
161
+        assertTrue(instance.isSaveAllowed());
162
+    }
163
+
164
+    @Test(expected = IllegalStateException.class)
165
+    public void testSave_NotAllowed() throws Exception {
166
+        instance.load();
167
+        instance.setResponse("");
168
+        assertFalse(instance.isSaveAllowed());
169
+        instance.save();
170
+    }
171
+
172
+    @Test
173
+    public void testSave_EmptyGlobal() throws Exception {
174
+        when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
175
+        instance.load();
176
+        instance.setResponse("test");
177
+        assertTrue(instance.isSaveAllowed());
178
+        instance.save();
179
+        verify(autoCommandManager).addAutoCommand(eq(AutoCommand.create(Optional.empty(),
180
+                Optional.empty(), Optional.empty(), "test")));
181
+    }
182
+
183
+    @Test
184
+    public void testSave() throws Exception {
185
+        instance.load();
186
+        instance.setResponse("test");
187
+        assertTrue(instance.isSaveAllowed());
188
+        instance.save();
189
+        verify(autoCommandManager).replaceAutoCommand(eq(autoCommand), eq(AutoCommand
190
+                .create(Optional.empty(), Optional.empty(), Optional.empty(), "test")));
191
+    }
192
+}

Loading…
Cancel
Save