import java.util.HashMap;
import java.util.Map;
+import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.SoneProvider;
private final SoneProvider soneProvider = new TestSoneProvider();
private final TestPostProvider postProvider = new TestPostProvider();
- private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider);
+ private final TestAlbumProvider albumProvider = new TestAlbumProvider();
+ private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider, albumProvider);
/**
* Tests basic plain-text operation of the parser.
assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|old|foo|Post about foo...] is awesome."));
}
+ @Test
+ public void linksToAlbumIsParsedCorrectly() throws IOException {
+ albumProvider.addAlbumTitle("album-id", "Super Album");
+ Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This album://album-id rocks!"));
+ assertThat(convertText(parts, PlainTextPart.class, AlbumPart.class), is("This [album|album-id|Super Album] rocks!"));
+ }
+
//
// PRIVATE METHODS
//
.append('|')
.append(postPart.getPost().getText())
.append(']');
+ } else if (part instanceof AlbumPart) {
+ Album album = ((AlbumPart) part).getAlbum();
+ text.append(String.format("[album|%s|%s]", album.getId(), album.getTitle()));
}
}
return text.toString();
}
+ private static class TestAlbumProvider implements AlbumProvider {
+
+ private final Map<String, String> albumTitles = new HashMap<String, String>();
+
+ public void addAlbumTitle(String albumId, String albumTitle) {
+ albumTitles.put(albumId, albumTitle);
+ }
+
+ @Override
+ public Optional<Album> getAlbum(String albumId) {
+ if (albumTitles.containsKey(albumId)) {
+ Album album = mock(Album.class);
+ when(album.getId()).thenReturn(albumId);
+ when(album.getTitle()).thenReturn(albumTitles.get(albumId));
+ return Optional.of(album);
+ }
+ return Optional.absent();
+ }
+
+ }
+
}