aboutsummaryrefslogtreecommitdiff
path: root/subsonic-main/src/test/java/net/sourceforge/subsonic/dao/PodcastDaoTestCase.java
blob: f5952c3e5adafe491ecd877e976539f19f998348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package net.sourceforge.subsonic.dao;

import java.util.Date;
import java.util.List;

import net.sourceforge.subsonic.domain.PodcastChannel;
import net.sourceforge.subsonic.domain.PodcastEpisode;
import net.sourceforge.subsonic.domain.PodcastStatus;

/**
 * Unit test of {@link PodcastDao}.
 *
 * @author Sindre Mehus
 */
public class PodcastDaoTestCase extends DaoTestCaseBase {

    @Override
    protected void setUp() throws Exception {
        getJdbcTemplate().execute("delete from podcast_channel");
    }

    public void testCreateChannel() {
        PodcastChannel channel = new PodcastChannel("http://foo");
        podcastDao.createChannel(channel);

        PodcastChannel newChannel = podcastDao.getAllChannels().get(0);
        assertNotNull("Wrong ID.", newChannel.getId());
        assertChannelEquals(channel, newChannel);
    }

    public void testChannelId() {
        int channelId = podcastDao.createChannel(new PodcastChannel("http://foo"));

        assertEquals("Error in createChannel.", channelId + 1, podcastDao.createChannel(new PodcastChannel("http://foo")));
        assertEquals("Error in createChannel.", channelId + 2, podcastDao.createChannel(new PodcastChannel("http://foo")));
        assertEquals("Error in createChannel.", channelId + 3, podcastDao.createChannel(new PodcastChannel("http://foo")));

        podcastDao.deleteChannel(channelId + 1);
        assertEquals("Error in createChannel.", channelId + 4, podcastDao.createChannel(new PodcastChannel("http://foo")));

        podcastDao.deleteChannel(channelId + 4);
        assertEquals("Error in createChannel.", channelId + 5, podcastDao.createChannel(new PodcastChannel("http://foo")));
    }

    public void testUpdateChannel() {
        PodcastChannel channel = new PodcastChannel("http://foo");
        podcastDao.createChannel(channel);
        channel = podcastDao.getAllChannels().get(0);

        channel.setUrl("http://bar");
        channel.setTitle("Title");
        channel.setDescription("Description");
        channel.setStatus(PodcastStatus.ERROR);
        channel.setErrorMessage("Something went terribly wrong.");

        podcastDao.updateChannel(channel);
        PodcastChannel newChannel = podcastDao.getAllChannels().get(0);

        assertEquals("Wrong ID.", channel.getId(), newChannel.getId());
        assertChannelEquals(channel, newChannel);
    }

    public void testDeleteChannel() {
        assertEquals("Wrong number of channels.", 0, podcastDao.getAllChannels().size());

        PodcastChannel channel = new PodcastChannel("http://foo");
        podcastDao.createChannel(channel);
        assertEquals("Wrong number of channels.", 1, podcastDao.getAllChannels().size());

        podcastDao.createChannel(channel);
        assertEquals("Wrong number of channels.", 2, podcastDao.getAllChannels().size());

        podcastDao.deleteChannel(podcastDao.getAllChannels().get(0).getId());
        assertEquals("Wrong number of channels.", 1, podcastDao.getAllChannels().size());

        podcastDao.deleteChannel(podcastDao.getAllChannels().get(0).getId());
        assertEquals("Wrong number of channels.", 0, podcastDao.getAllChannels().size());
    }

    public void testCreateEpisode() {
        int channelId = createChannel();
        PodcastEpisode episode = new PodcastEpisode(null, channelId, "http://bar", "path", "title", "description",
                new Date(), "12:34", null, null, PodcastStatus.NEW, null);
        podcastDao.createEpisode(episode);

        PodcastEpisode newEpisode = podcastDao.getEpisodes(channelId).get(0);
        assertNotNull("Wrong ID.", newEpisode.getId());
        assertEpisodeEquals(episode, newEpisode);
    }

    public void testGetEpisode() {
        assertNull("Error in getEpisode()", podcastDao.getEpisode(23));

        int channelId = createChannel();
        PodcastEpisode episode = new PodcastEpisode(null, channelId, "http://bar", "path", "title", "description",
                new Date(), "12:34", 3276213L, 2341234L, PodcastStatus.NEW, "error");
        podcastDao.createEpisode(episode);

        int episodeId = podcastDao.getEpisodes(channelId).get(0).getId();
        PodcastEpisode newEpisode = podcastDao.getEpisode(episodeId);
        assertEpisodeEquals(episode, newEpisode);
    }

    public void testGetEpisodes() {
        int channelId = createChannel();
        PodcastEpisode a = new PodcastEpisode(null, channelId, "a", null, null, null,
                new Date(3000), null, null, null, PodcastStatus.NEW, null);
        PodcastEpisode b = new PodcastEpisode(null, channelId, "b", null, null, null,
                new Date(1000), null, null, null, PodcastStatus.NEW, "error");
        PodcastEpisode c = new PodcastEpisode(null, channelId, "c", null, null, null,
                new Date(2000), null, null, null, PodcastStatus.NEW, null);
        PodcastEpisode d = new PodcastEpisode(null, channelId, "c", null, null, null,
                null, null, null, null, PodcastStatus.NEW, "");
        podcastDao.createEpisode(a);
        podcastDao.createEpisode(b);
        podcastDao.createEpisode(c);
        podcastDao.createEpisode(d);

        List<PodcastEpisode> episodes = podcastDao.getEpisodes(channelId);
        assertEquals("Error in getEpisodes().", 4, episodes.size());
        assertEpisodeEquals(a, episodes.get(0));
        assertEpisodeEquals(c, episodes.get(1));
        assertEpisodeEquals(b, episodes.get(2));
        assertEpisodeEquals(d, episodes.get(3));
    }


    public void testUpdateEpisode() {
        int channelId = createChannel();
        PodcastEpisode episode = new PodcastEpisode(null, channelId, "http://bar", null, null, null,
                null, null, null, null, PodcastStatus.NEW, null);
        podcastDao.createEpisode(episode);
        episode = podcastDao.getEpisodes(channelId).get(0);

        episode.setUrl("http://bar");
        episode.setPath("c:/tmp");
        episode.setTitle("Title");
        episode.setDescription("Description");
        episode.setPublishDate(new Date());
        episode.setDuration("1:20");
        episode.setBytesTotal(87628374612L);
        episode.setBytesDownloaded(9086L);
        episode.setStatus(PodcastStatus.DOWNLOADING);
        episode.setErrorMessage("Some error");

        podcastDao.updateEpisode(episode);
        PodcastEpisode newEpisode = podcastDao.getEpisodes(channelId).get(0);
        assertEquals("Wrong ID.", episode.getId(), newEpisode.getId());
        assertEpisodeEquals(episode, newEpisode);
    }

    public void testDeleteEpisode() {
        int channelId = createChannel();

        assertEquals("Wrong number of episodes.", 0, podcastDao.getEpisodes(channelId).size());

        PodcastEpisode episode = new PodcastEpisode(null, channelId, "http://bar", null, null, null,
                null, null, null, null, PodcastStatus.NEW, null);

        podcastDao.createEpisode(episode);
        assertEquals("Wrong number of episodes.", 1, podcastDao.getEpisodes(channelId).size());

        podcastDao.createEpisode(episode);
        assertEquals("Wrong number of episodes.", 2, podcastDao.getEpisodes(channelId).size());

        podcastDao.deleteEpisode(podcastDao.getEpisodes(channelId).get(0).getId());
        assertEquals("Wrong number of episodes.", 1, podcastDao.getEpisodes(channelId).size());

        podcastDao.deleteEpisode(podcastDao.getEpisodes(channelId).get(0).getId());
        assertEquals("Wrong number of episodes.", 0, podcastDao.getEpisodes(channelId).size());
    }


    public void testCascadingDelete() {
        int channelId = createChannel();
        PodcastEpisode episode = new PodcastEpisode(null, channelId, "http://bar", null, null, null,
                null, null, null, null, PodcastStatus.NEW, null);
        podcastDao.createEpisode(episode);
        podcastDao.createEpisode(episode);
        assertEquals("Wrong number of episodes.", 2, podcastDao.getEpisodes(channelId).size());

        podcastDao.deleteChannel(channelId);
        assertEquals("Wrong number of episodes.", 0, podcastDao.getEpisodes(channelId).size());
    }

    private int createChannel() {
        PodcastChannel channel = new PodcastChannel("http://foo");
        podcastDao.createChannel(channel);
        channel = podcastDao.getAllChannels().get(0);
        return channel.getId();
    }

    private void assertChannelEquals(PodcastChannel expected, PodcastChannel actual) {
        assertEquals("Wrong URL.", expected.getUrl(), actual.getUrl());
        assertEquals("Wrong title.", expected.getTitle(), actual.getTitle());
        assertEquals("Wrong description.", expected.getDescription(), actual.getDescription());
        assertSame("Wrong status.", expected.getStatus(), actual.getStatus());
        assertEquals("Wrong error message.", expected.getErrorMessage(), actual.getErrorMessage());
    }

    private void assertEpisodeEquals(PodcastEpisode expected, PodcastEpisode actual) {
        assertEquals("Wrong URL.", expected.getUrl(), actual.getUrl());
        assertEquals("Wrong path.", expected.getPath(), actual.getPath());
        assertEquals("Wrong title.", expected.getTitle(), actual.getTitle());
        assertEquals("Wrong description.", expected.getDescription(), actual.getDescription());
        assertEquals("Wrong date.", expected.getPublishDate(), actual.getPublishDate());
        assertEquals("Wrong duration.", expected.getDuration(), actual.getDuration());
        assertEquals("Wrong bytes total.", expected.getBytesTotal(), actual.getBytesTotal());
        assertEquals("Wrong bytes downloaded.", expected.getBytesDownloaded(), actual.getBytesDownloaded());
        assertSame("Wrong status.", expected.getStatus(), actual.getStatus());
        assertEquals("Wrong error message.", expected.getErrorMessage(), actual.getErrorMessage());
    }

}