Quellcode durchsuchen

Merge pull request #623 from csmith/tidying

Add format provider that handles multiple sources.
pull/624/head
Greg Holmes vor 8 Jahren
Ursprung
Commit
25de1c7e21

+ 61
- 0
src/com/dmdirc/ui/messages/MultiEventFormatProvider.java Datei anzeigen

@@ -0,0 +1,61 @@
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.messages;
24
+
25
+import com.dmdirc.events.DisplayableEvent;
26
+
27
+import java.util.ArrayList;
28
+import java.util.Arrays;
29
+import java.util.List;
30
+import java.util.Optional;
31
+
32
+/**
33
+ * Provides formats from multiple other {@link EventFormatProvider}.
34
+ */
35
+public class MultiEventFormatProvider implements EventFormatProvider {
36
+
37
+    /** Providers to test for formats, in order. */
38
+    private final List<EventFormatProvider> providers = new ArrayList<>();
39
+
40
+    public MultiEventFormatProvider(final EventFormatProvider ... providers) {
41
+        this.providers.addAll(Arrays.asList(providers));
42
+    }
43
+
44
+    public void addProvider(final EventFormatProvider provider) {
45
+        providers.add(provider);
46
+    }
47
+
48
+    public void removeProvider(final EventFormatProvider provider) {
49
+        providers.remove(provider);
50
+    }
51
+
52
+    @Override
53
+    public Optional<EventFormat> getFormat(final Class<? extends DisplayableEvent> eventType) {
54
+        return providers.stream()
55
+                .map(provider -> provider.getFormat(eventType))
56
+                .filter(Optional::isPresent)
57
+                .findFirst()
58
+                .map(Optional::get);
59
+    }
60
+
61
+}

+ 107
- 0
test/com/dmdirc/ui/messages/MultiEventFormatProviderTest.java Datei anzeigen

@@ -0,0 +1,107 @@
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.messages;
24
+
25
+import com.dmdirc.events.ChannelModesDiscoveredEvent;
26
+
27
+import java.util.Optional;
28
+
29
+import org.junit.Before;
30
+import org.junit.Test;
31
+import org.junit.runner.RunWith;
32
+import org.mockito.Mock;
33
+import org.mockito.runners.MockitoJUnitRunner;
34
+
35
+import static org.junit.Assert.assertFalse;
36
+import static org.junit.Assert.assertSame;
37
+import static org.junit.Assert.assertTrue;
38
+import static org.mockito.Mockito.when;
39
+
40
+@RunWith(MockitoJUnitRunner.class)
41
+public class MultiEventFormatProviderTest {
42
+
43
+    @Mock private EventFormat mockEventFormat1;
44
+    @Mock private EventFormat mockEventFormat2;
45
+    @Mock private EventFormatProvider mockEventFormatProvider1;
46
+    @Mock private EventFormatProvider mockEventFormatProvider2;
47
+
48
+    @Before
49
+    public void setup() {
50
+        when(mockEventFormatProvider1.getFormat(ChannelModesDiscoveredEvent.class))
51
+                .thenReturn(Optional.of(mockEventFormat1));
52
+        when(mockEventFormatProvider2.getFormat(ChannelModesDiscoveredEvent.class))
53
+                .thenReturn(Optional.of(mockEventFormat2));
54
+    }
55
+
56
+    @Test
57
+    public void testReturnsEmptyWithNoProviders() {
58
+        final EventFormatProvider provider = new MultiEventFormatProvider();
59
+        assertFalse(provider.getFormat(ChannelModesDiscoveredEvent.class).isPresent());
60
+    }
61
+
62
+    @Test
63
+    public void testReturnsFormatFromProvider() {
64
+        final EventFormatProvider provider = new MultiEventFormatProvider(mockEventFormatProvider1);
65
+        final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
66
+        assertTrue(res.isPresent());
67
+        assertSame(mockEventFormat1, res.get());
68
+    }
69
+
70
+    @Test
71
+    public void testReturnsFormatFromAlternateProvider() {
72
+        final MultiEventFormatProvider provider =
73
+                new MultiEventFormatProvider(mockEventFormatProvider1);
74
+        provider.addProvider(mockEventFormatProvider2);
75
+
76
+        final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
77
+        assertTrue(res.isPresent());
78
+        assertSame(mockEventFormat1, res.get());
79
+    }
80
+
81
+    @Test
82
+    public void testReturnsFirstFormatIfMultipleProvidersHaveFormats() {
83
+        final MultiEventFormatProvider provider =
84
+                new MultiEventFormatProvider(mockEventFormatProvider1);
85
+        provider.addProvider(mockEventFormatProvider2);
86
+
87
+        final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
88
+        assertTrue(res.isPresent());
89
+        assertSame(mockEventFormat1, res.get());
90
+    }
91
+
92
+    @Test
93
+    public void testRemovesProvider() {
94
+        final MultiEventFormatProvider provider =
95
+                new MultiEventFormatProvider(mockEventFormatProvider1);
96
+        provider.addProvider(mockEventFormatProvider2);
97
+        provider.removeProvider(mockEventFormatProvider1);
98
+
99
+        final Optional<EventFormat> res = provider.getFormat(ChannelModesDiscoveredEvent.class);
100
+        assertTrue(res.isPresent());
101
+        assertSame(mockEventFormat2, res.get());
102
+
103
+        provider.removeProvider(mockEventFormatProvider2);
104
+        assertFalse(provider.getFormat(ChannelModesDiscoveredEvent.class).isPresent());
105
+    }
106
+
107
+}

Laden…
Abbrechen
Speichern