From 62bb6456a7c2667dec2f8b7b88db465eb6c07257 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 8 Sep 2014 19:09:56 +0200 Subject: [PATCH] Move friend parsing to new configuration parser. --- .../pterodactylus/sone/core/ConfigurationSoneParser.java | 13 +++++++++++++ src/main/java/net/pterodactylus/sone/core/Core.java | 9 +-------- .../sone/core/ConfigurationSoneParserTest.java | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java index 1491f87..39dcd12 100644 --- a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java @@ -162,6 +162,19 @@ public class ConfigurationSoneParser { return likedPostReplyIds; } + public Set parseFriends() { + Set friends = new HashSet(); + while (true) { + String friendId = + getString("/Friends/" + friends.size() + "/ID", null); + if (friendId == null) { + break; + } + friends.add(friendId); + } + return friends; + } + public static class InvalidPostFound extends RuntimeException { } public static class InvalidPostReplyFound extends RuntimeException { } diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index a6d46c2..2fdd01a 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1140,14 +1140,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, configurationSoneParser.parseLikedPostReplyIds(); /* load friends. */ - Set friends = new HashSet(); - while (true) { - String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null); - if (friendId == null) { - break; - } - friends.add(friendId); - } + Set friends = configurationSoneParser.parseFriends(); /* load albums. */ List topLevelAlbums = new ArrayList(); diff --git a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java index 93253c4..4aa291b 100644 --- a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java @@ -300,6 +300,20 @@ public class ConfigurationSoneParserTest { setupString("Sone/1/Likes/Reply/3/ID", null); } + @Test + public void friendsAreParsedCorrectly() { + setupFriends(); + Set friends = configurationSoneParser.parseFriends(); + assertThat(friends, containsInAnyOrder("F1", "F2", "F3")); + } + + private void setupFriends() { + setupString("Sone/1/Friends/0/ID", "F1"); + setupString("Sone/1/Friends/1/ID", "F2"); + setupString("Sone/1/Friends/2/ID", "F3"); + setupString("Sone/1/Friends/3/ID", null); + } + private static class TestValue implements Value { private final AtomicReference value = new AtomicReference(); -- 2.7.4