Pārlūkot izejas kodu

Add events to ProfileManager, add tests.

pull/142/head
Greg Holmes 9 gadus atpakaļ
vecāks
revīzija
570d0910e3

+ 8
- 2
src/com/dmdirc/config/profiles/ProfileManager.java Parādīt failu

@@ -22,7 +22,9 @@
22 22
 
23 23
 package com.dmdirc.config.profiles;
24 24
 
25
-import com.dmdirc.util.validators.Validator;
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.ProfileAddedEvent;
27
+import com.dmdirc.events.ProfileDeletedEvent;
26 28
 
27 29
 import java.util.ArrayList;
28 30
 import java.util.Collection;
@@ -37,10 +39,12 @@ import javax.inject.Singleton;
37 39
 @Singleton
38 40
 public class ProfileManager {
39 41
 
42
+    private final DMDircMBassador eventBus;
40 43
     private final Collection<Profile> profiles;
41 44
 
42 45
     @Inject
43
-    public ProfileManager() {
46
+    public ProfileManager(final DMDircMBassador eventBus) {
47
+        this.eventBus = eventBus;
44 48
         profiles = new ArrayList<>();
45 49
     }
46 50
 
@@ -51,6 +55,7 @@ public class ProfileManager {
51 55
      */
52 56
     public void addProfile(final Profile profile) {
53 57
         profiles.add(profile);
58
+        eventBus.publishAsync(new ProfileAddedEvent(profile));
54 59
     }
55 60
 
56 61
     /**
@@ -60,6 +65,7 @@ public class ProfileManager {
60 65
      */
61 66
     public void deleteProfile(final Profile profile) {
62 67
         profiles.remove(profile);
68
+        eventBus.publishAsync(new ProfileDeletedEvent(profile));
63 69
     }
64 70
 
65 71
     /**

+ 41
- 0
src/com/dmdirc/events/ProfileAddedEvent.java Parādīt failu

@@ -0,0 +1,41 @@
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.events;
24
+
25
+import com.dmdirc.config.profiles.Profile;
26
+
27
+/**
28
+ * Fired when a profile is added to the config.
29
+ */
30
+public class ProfileAddedEvent extends DMDircEvent {
31
+
32
+    private final Profile profile;
33
+
34
+    public ProfileAddedEvent(final Profile profile) {
35
+        this.profile = profile;
36
+    }
37
+
38
+    public Profile getProfile() {
39
+        return profile;
40
+    }
41
+}

+ 41
- 0
src/com/dmdirc/events/ProfileDeletedEvent.java Parādīt failu

@@ -0,0 +1,41 @@
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.events;
24
+
25
+import com.dmdirc.config.profiles.Profile;
26
+
27
+/**
28
+ * Fired when a profile is deleted from the config.
29
+ */
30
+public class ProfileDeletedEvent extends DMDircEvent {
31
+
32
+    private final Profile profile;
33
+
34
+    public ProfileDeletedEvent(final Profile profile) {
35
+        this.profile = profile;
36
+    }
37
+
38
+    public Profile getProfile() {
39
+        return profile;
40
+    }
41
+}

+ 91
- 0
test/com/dmdirc/config/profiles/ProfileManagerTest.java Parādīt failu

@@ -0,0 +1,91 @@
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.config.profiles;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.ProfileAddedEvent;
27
+import com.dmdirc.events.ProfileDeletedEvent;
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 junit.framework.TestCase.assertEquals;
36
+import static junit.framework.TestCase.assertFalse;
37
+import static junit.framework.TestCase.assertTrue;
38
+import static org.mockito.Matchers.any;
39
+import static org.mockito.Mockito.reset;
40
+import static org.mockito.Mockito.verify;
41
+
42
+@RunWith(MockitoJUnitRunner.class)
43
+public class ProfileManagerTest {
44
+
45
+    @Mock private DMDircMBassador eventBus;
46
+    @Mock private Profile profile1;
47
+    @Mock private Profile profile2;
48
+
49
+    private ProfileManager instance;
50
+
51
+    @Before
52
+    public void setUp() throws Exception {
53
+        instance = new ProfileManager(eventBus);
54
+    }
55
+
56
+    @Test
57
+    public void testAddProfile() {
58
+        assertTrue(instance.getProfiles().isEmpty());
59
+        instance.addProfile(profile1);
60
+        assertEquals(1, instance.getProfiles().size());
61
+        assertTrue(instance.getProfiles().contains(profile1));
62
+    }
63
+
64
+    @Test
65
+    public void testAddProfile_Event() {
66
+        instance.addProfile(profile1);
67
+        verify(eventBus).publishAsync(any(ProfileAddedEvent.class));
68
+    }
69
+
70
+    @Test
71
+    public void testDeleteProfile() {
72
+        instance.addProfile(profile1);
73
+        instance.addProfile(profile2);
74
+        assertEquals(2, instance.getProfiles().size());
75
+        assertTrue(instance.getProfiles().contains(profile1));
76
+        assertTrue(instance.getProfiles().contains(profile2));
77
+        instance.deleteProfile(profile2);
78
+        assertEquals(1, instance.getProfiles().size());
79
+        assertTrue(instance.getProfiles().contains(profile1));
80
+        assertFalse(instance.getProfiles().contains(profile2));
81
+    }
82
+
83
+    @Test
84
+    public void testDeleteProfile_Event() {
85
+        instance.addProfile(profile1);
86
+        //Fairly certain this shouldn't be needed, but the any matcher below seems to match the add
87
+        reset(eventBus);
88
+        instance.deleteProfile(profile1);
89
+        verify(eventBus).publishAsync(any(ProfileDeletedEvent.class));
90
+    }
91
+}

Notiek ielāde…
Atcelt
Saglabāt