Просмотр исходного кода

Improve tests, tidy up a little.

pull/137/head
Greg Holmes 9 лет назад
Родитель
Сommit
5242fca351

+ 5
- 4
src/com/dmdirc/config/profiles/Profile.java Просмотреть файл

46
     private final List<String> nicknames;
46
     private final List<String> nicknames;
47
 
47
 
48
     public Profile(final String name, final String realname, final Optional<String> ident,
48
     public Profile(final String name, final String realname, final Optional<String> ident,
49
-            final Iterable<String> nicknames) {
49
+            final List<String> nicknames) {
50
         this.name = name;
50
         this.name = name;
51
         this.realname = realname;
51
         this.realname = realname;
52
         this.ident = ident;
52
         this.ident = ident;
69
         return Collections.unmodifiableList(nicknames);
69
         return Collections.unmodifiableList(nicknames);
70
     }
70
     }
71
 
71
 
72
+    @Override
72
     public String toString() {
73
     public String toString() {
73
         return MoreObjects.toStringHelper(this)
74
         return MoreObjects.toStringHelper(this)
74
                 .add("name", name)
75
                 .add("name", name)
89
         final Profile profile = (Profile) o;
90
         final Profile profile = (Profile) o;
90
         return Objects.equals(name, profile.getName())
91
         return Objects.equals(name, profile.getName())
91
                 && Objects.equals(realname, profile.getRealname())
92
                 && Objects.equals(realname, profile.getRealname())
92
-                && Objects.equals(nicknames, profile.getNicknames())
93
-                && Objects.equals(ident, profile.getIdent());
93
+                && Objects.equals(ident, profile.getIdent())
94
+                && Objects.equals(nicknames, profile.getNicknames());
94
     }
95
     }
95
 
96
 
96
     @Override
97
     @Override
97
     public int hashCode() {
98
     public int hashCode() {
98
-        return Objects.hash(name, realname, ident, nicknames);
99
+        return Objects.hash(name, realname, ident);
99
     }
100
     }
100
 }
101
 }

+ 0
- 12
src/com/dmdirc/config/profiles/ProfileManager.java Просмотреть файл

40
         profiles = new ArrayList<>();
40
         profiles = new ArrayList<>();
41
     }
41
     }
42
 
42
 
43
-    /**
44
-     * Starts this manager, registering listeners.
45
-     */
46
-    public void start() {
47
-    }
48
-
49
-    /**
50
-     * Stops this manager, unregistering listeners.
51
-     */
52
-    public void stop() {
53
-    }
54
-
55
     /**
43
     /**
56
      * Adds a profile to the manager.
44
      * Adds a profile to the manager.
57
      *
45
      *

+ 6
- 3
src/com/dmdirc/config/profiles/ProfileManagerLifeCycleManager.java Просмотреть файл

34
 public class ProfileManagerLifeCycleManager implements SystemLifecycleComponent {
34
 public class ProfileManagerLifeCycleManager implements SystemLifecycleComponent {
35
 
35
 
36
     private final ProfileManager profileManager;
36
     private final ProfileManager profileManager;
37
+    private final ProfileStore profileStore;
37
 
38
 
38
     @Inject
39
     @Inject
39
-    public ProfileManagerLifeCycleManager(final ProfileManager profileManager) {
40
+    public ProfileManagerLifeCycleManager(final ProfileManager profileManager,
41
+            final ProfileStore profileStore) {
40
         this.profileManager = profileManager;
42
         this.profileManager = profileManager;
43
+        this.profileStore = profileStore;
41
     }
44
     }
42
 
45
 
43
     @Override
46
     @Override
44
     public void startUp() {
47
     public void startUp() {
45
-        profileManager.start();
48
+        profileStore.readProfiles().forEach(profileManager::addProfile);
46
     }
49
     }
47
 
50
 
48
     @Override
51
     @Override
49
     public void shutDown() {
52
     public void shutDown() {
50
-        profileManager.stop();
53
+        profileStore.writeProfiles(profileManager.getProfiles());
51
     }
54
     }
52
 }
55
 }

+ 4
- 4
src/com/dmdirc/config/profiles/ProfileStore.java Просмотреть файл

22
 
22
 
23
 package com.dmdirc.config.profiles;
23
 package com.dmdirc.config.profiles;
24
 
24
 
25
-import java.util.Set;
25
+import java.util.Collection;
26
 
26
 
27
 /**
27
 /**
28
  * A store of {@link Profile}s that can read and write them to some kind of persistent media.
28
  * A store of {@link Profile}s that can read and write them to some kind of persistent media.
34
      *
34
      *
35
      * @return A set of known profiles, or an empty set if the store is uninitialised.
35
      * @return A set of known profiles, or an empty set if the store is uninitialised.
36
      */
36
      */
37
-    Set<Profile> readProfiles();
37
+    Collection<Profile> readProfiles();
38
 
38
 
39
     /**
39
     /**
40
      * Writes all the given profiles to the store, replacing any existing commands.
40
      * Writes all the given profiles to the store, replacing any existing commands.
41
      *
41
      *
42
-     * @param profiles The set of profiles to be written to the store.
42
+     * @param profiles The collection of profiles to be written to the store.
43
      */
43
      */
44
-    void writeProfiles(Set<Profile> profiles);
44
+    void writeProfiles(Collection<Profile> profiles);
45
 
45
 
46
 }
46
 }

+ 14
- 0
src/com/dmdirc/config/profiles/ProfilesModule.java Просмотреть файл

22
 
22
 
23
 package com.dmdirc.config.profiles;
23
 package com.dmdirc.config.profiles;
24
 
24
 
25
+import com.dmdirc.commandline.CommandLineOptionsModule;
26
+import com.dmdirc.commandparser.auto.AutoCommandStore;
27
+import com.dmdirc.commandparser.auto.YamlAutoCommandStore;
25
 import com.dmdirc.interfaces.SystemLifecycleComponent;
28
 import com.dmdirc.interfaces.SystemLifecycleComponent;
26
 
29
 
30
+import java.nio.file.Path;
31
+
32
+import javax.inject.Singleton;
33
+
27
 import dagger.Module;
34
 import dagger.Module;
28
 import dagger.Provides;
35
 import dagger.Provides;
29
 
36
 
33
 @Module(library = true, complete = false)
40
 @Module(library = true, complete = false)
34
 public class ProfilesModule {
41
 public class ProfilesModule {
35
 
42
 
43
+    @Provides
44
+    @Singleton
45
+    public ProfileStore getProfileStore(
46
+            @CommandLineOptionsModule.Directory(CommandLineOptionsModule.DirectoryType.BASE) final Path directory) {
47
+        return new YamlProfileStore(directory.resolve("auto-commands.yml"));
48
+    }
49
+
36
     @Provides(type = Provides.Type.SET)
50
     @Provides(type = Provides.Type.SET)
37
     public SystemLifecycleComponent getLifecycleComponent(
51
     public SystemLifecycleComponent getLifecycleComponent(
38
             final ProfileManagerLifeCycleManager manager) {
52
             final ProfileManagerLifeCycleManager manager) {

+ 8
- 9
src/com/dmdirc/config/profiles/YamlProfileStore.java Просмотреть файл

73
     }
73
     }
74
 
74
 
75
     @Override
75
     @Override
76
-    public Set<Profile> readProfiles() {
77
-        final Set<Profile> commands = new HashSet<>();
76
+    public Collection<Profile> readProfiles() {
77
+        final Set<Profile> profiles = new HashSet<>();
78
         if (Files.exists(path)) {
78
         if (Files.exists(path)) {
79
             try (final InputStream stream = Files.newInputStream(path);
79
             try (final InputStream stream = Files.newInputStream(path);
80
                     final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
80
                     final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
81
                 final YamlReader yamlReader = new YamlReader(reader);
81
                 final YamlReader yamlReader = new YamlReader(reader);
82
                 final Object root = yamlReader.read();
82
                 final Object root = yamlReader.read();
83
-                commands.addAll(readProfilesFromYaml(asList(root)));
83
+                profiles.addAll(readProfilesFromYaml(asList(root)));
84
                 yamlReader.close();
84
                 yamlReader.close();
85
             } catch (IOException | IllegalArgumentException ex) {
85
             } catch (IOException | IllegalArgumentException ex) {
86
                 LOG.warn("Unable to read auto commands", ex);
86
                 LOG.warn("Unable to read auto commands", ex);
87
             }
87
             }
88
         }
88
         }
89
 
89
 
90
-        return commands;
90
+        return profiles;
91
     }
91
     }
92
 
92
 
93
     @Override
93
     @Override
94
-    public void writeProfiles(final Set<Profile> profiles) {
94
+    public void writeProfiles(final Collection<Profile> profiles) {
95
         final List<Object> list = getProfilesForYaml(profiles);
95
         final List<Object> list = getProfilesForYaml(profiles);
96
         try (final OutputStream stream = Files.newOutputStream(path);
96
         try (final OutputStream stream = Files.newOutputStream(path);
97
                 final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
97
                 final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
117
                 final Map<Object, Object> map = asMap(profile);
117
                 final Map<Object, Object> map = asMap(profile);
118
                 final String name = requiredString(map, "name");
118
                 final String name = requiredString(map, "name");
119
                 final String realname = requiredString(map, "realname");
119
                 final String realname = requiredString(map, "realname");
120
-                final Optional<String> ident =
121
-                        Optional.ofNullable(optionalString(map, "ident"));
122
-                final List<String> nicknames =
123
-                        Splitter.on('\n').splitToList(requiredString(map, "nicknames"));
120
+                final Optional<String> ident = Optional.ofNullable(optionalString(map, "ident"));
121
+                final List<String> nicknames = Splitter.on('\n')
122
+                        .splitToList(requiredString(map, "nicknames"));
124
                 res.add(new Profile(name, realname, ident, nicknames));
123
                 res.add(new Profile(name, realname, ident, nicknames));
125
             } catch (IllegalArgumentException ex) {
124
             } catch (IllegalArgumentException ex) {
126
                 LOG.info("Unable to read alias", ex);
125
                 LOG.info("Unable to read alias", ex);

+ 1
- 1
test-res/com/dmdirc/config/profiles/profiles.yml Просмотреть файл

3
   nicknames: nickname1, nickname2
3
   nicknames: nickname1, nickname2
4
 - name: profile2
4
 - name: profile2
5
   realname: realname2
5
   realname: realname2
6
-  nicknames: nickname1, nickname1
6
+  nicknames: nickname1, nickname3
7
   ident: ident2
7
   ident: ident2
8
 - name: profile3
8
 - name: profile3
9
   realname: profile3
9
   realname: profile3

+ 26
- 5
test/com/dmdirc/config/profiles/YamlProfileStoreTest.java Просмотреть файл

35
 import java.util.Optional;
35
 import java.util.Optional;
36
 import java.util.Set;
36
 import java.util.Set;
37
 
37
 
38
+import org.junit.Before;
38
 import org.junit.Test;
39
 import org.junit.Test;
39
 import org.junit.runner.RunWith;
40
 import org.junit.runner.RunWith;
40
 import org.mockito.runners.MockitoJUnitRunner;
41
 import org.mockito.runners.MockitoJUnitRunner;
41
 
42
 
42
 import static junit.framework.TestCase.assertEquals;
43
 import static junit.framework.TestCase.assertEquals;
44
+import static junit.framework.TestCase.assertTrue;
43
 
45
 
44
 @RunWith(MockitoJUnitRunner.class)
46
 @RunWith(MockitoJUnitRunner.class)
45
 public class YamlProfileStoreTest {
47
 public class YamlProfileStoreTest {
46
 
48
 
49
+    private Profile profile1;
50
+    private Profile profile2;
51
+    private Profile profile3;
52
+
53
+    @Before
54
+    public void setup() {
55
+        profile1 = new Profile("profile1", "realname1", Optional.empty(),
56
+                Lists.newArrayList("nickname1", "nickname2"));
57
+        profile2 = new Profile("profile2", "realname2", Optional.of("ident2"),
58
+                Lists.newArrayList("nickname1", "nickname3"));
59
+        profile3 = new Profile("profile3", "realname3", Optional.empty(),
60
+                Lists.newArrayList("nickname3"));
61
+    }
62
+
47
     @Test
63
     @Test
48
     public void testReadProfiles() throws Exception {
64
     public void testReadProfiles() throws Exception {
49
         final ProfileStore store = new YamlProfileStore(Paths.get(getClass()
65
         final ProfileStore store = new YamlProfileStore(Paths.get(getClass()
50
                 .getResource("profiles.yml").toURI()));
66
                 .getResource("profiles.yml").toURI()));
51
         final Collection<Profile> profiles = store.readProfiles();
67
         final Collection<Profile> profiles = store.readProfiles();
52
         assertEquals(3, profiles.size());
68
         assertEquals(3, profiles.size());
69
+        profiles.forEach(System.out::println);
70
+        assertTrue(profiles.contains(profile1));
71
+        assertTrue(profiles.contains(profile2));
72
+        assertTrue(profiles.contains(profile3));
53
     }
73
     }
54
 
74
 
55
     @Test
75
     @Test
57
         try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
77
         try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
58
             final ProfileStore store = new YamlProfileStore(fs.getPath("/")
78
             final ProfileStore store = new YamlProfileStore(fs.getPath("/")
59
                     .resolve("profiles.yml"));
79
                     .resolve("profiles.yml"));
60
-            final Profile profile1 = new Profile("name", "realname", Optional.of("ident"),
61
-                    Lists.newArrayList("nickname"));
62
-            final Set<Profile> profiles = Sets.newHashSet(profile1);
80
+            final List<Profile> profiles = Lists.newArrayList(profile1, profile2, profile3);
63
             store.writeProfiles(profiles);
81
             store.writeProfiles(profiles);
64
-            final List<String> lines = Files.readAllLines(fs.getPath("/").resolve("profiles.yml"));
65
-            assertEquals(5, lines.size());
82
+            final Collection<Profile> readProfiles = store.readProfiles();
83
+            assertEquals(3, readProfiles.size());
84
+            assertTrue(readProfiles.contains(profile1));
85
+            assertTrue(readProfiles.contains(profile2));
86
+            assertTrue(readProfiles.contains(profile3));
66
         }
87
         }
67
     }
88
     }
68
 }
89
 }

Загрузка…
Отмена
Сохранить