1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jdigg.parser;
17  
18  import java.io.InputStream;
19  import java.util.List;
20  
21  import net.sf.jdigg.beans.Comment;
22  import net.sf.jdigg.beans.Comments;
23  import net.sf.jdigg.beans.Container;
24  import net.sf.jdigg.beans.ContainerWithTopics;
25  import net.sf.jdigg.beans.Containers;
26  import net.sf.jdigg.beans.Stories;
27  import net.sf.jdigg.beans.Story;
28  import net.sf.jdigg.beans.Thumbnail;
29  import net.sf.jdigg.beans.Topic;
30  import net.sf.jdigg.beans.User;
31  import net.sf.jdigg.exception.DiggException;
32  import net.sf.jdigg.exception.ResponseParserException;
33  
34  import junit.framework.TestCase;
35  
36  /**
37   * JUnit Test.
38   * @author Philip May
39   * @since 1.0 M1
40   */
41  public class ResponseParserTest extends TestCase {
42  
43      public ResponseParserTest() {
44      }
45  
46      public ResponseParserTest(String name) {
47          super(name);
48      }
49  
50      public void testParseStories() throws ResponseParserException, DiggException {
51          InputStream inputStream = getClass().getResourceAsStream("/stories.xml");
52          Stories stories = ResponseParser.parseStories(inputStream);
53  
54          assertNotNull(stories);
55          assertEquals(1207214373, stories.getTimestamp());
56          assertEquals(1204622370, stories.getMinDate());
57          assertEquals(6795, stories.getTotal());
58          assertEquals(1, stories.getOffset());
59          assertEquals(3, stories.getCount());
60  
61          // test the story list:
62          List storyList = stories.getStoryList();
63  
64          assertNotNull(storyList);
65          assertFalse(storyList.isEmpty());
66          assertEquals(3, storyList.size());
67  
68          // test the story:
69          Story story = (Story) storyList.iterator().next();
70  
71          assertNotNull(story);
72  
73          assertEquals(5970731, story.getId());
74          assertEquals("http://iphonereblogged.blogspot.com/2008/04/pwnage-10-verffentlicht-zunchst-nur-als.html", story.getLink());
75          assertEquals(1207214203, story.getSubmitDate());
76          assertEquals(1, story.getDiggs());
77          assertEquals(2, story.getComments());
78          assertEquals("http://digg.com/apple/Pwnage_1_0_veroffentlicht_zunachst_nur_als_OS_X_Version", story.getHref());
79          assertEquals("upcoming", story.getStatus());
80          assertEquals("news", story.getMedia());
81          assertEquals("Pwnage 1.0 veršffentlicht - zunŠchst nur als OS X-Version", story.getTitle());
82  
83          // test the user:
84          User user = story.getUser();
85  
86          assertNotNull(user);
87          assertEquals("earcandy", user.getName());
88          assertEquals("http://digg.com/img/udl.png", user.getIcon());
89          assertEquals(1196245695, user.getRegistered());
90          assertEquals(183, user.getProfileviews());
91          assertEquals("tj hildebrandt", user.getFullname());
92  
93          // test the topic:
94          Topic topic = story.getTopic();
95  
96          assertNotNull(topic);
97          assertEquals("Apple", topic.getName());
98          assertEquals("apple", topic.getShortName());
99  
100         // test the container:
101         Container container = story.getContainer();
102 
103         assertNotNull(container);
104         assertEquals("Technology", container.getName());
105         assertEquals("technology", container.getShortName());
106 
107         // test the thumbnail:
108         Thumbnail thumbnail = story.getThumbnail();
109 
110         assertNotNull(thumbnail);
111 
112         assertEquals(153, thumbnail.getOriginalWidth());
113         assertEquals(200, thumbnail.getOriginalHeight());
114         assertEquals(80, thumbnail.getWidth());
115         assertEquals(80, thumbnail.getHeight());
116         assertEquals("image/gif", thumbnail.getContentType());
117         assertEquals("http://digg.com/apple/Pwnage_1_0_veroffentlicht_zunachst_nur_als_OS_X_Version/t.gif", thumbnail.getSrc());
118     }
119 
120     public void testParseComments() throws ResponseParserException, DiggException {
121         InputStream inputStream = getClass().getResourceAsStream("/comments.xml");
122         Comments comments = ResponseParser.parseComments(inputStream);
123 
124         assertNotNull(comments);
125         assertEquals(1207211789, comments.getTimestamp());
126         assertEquals(139, comments.getTotal());
127         assertEquals(1, comments.getOffset());
128         assertEquals(11, comments.getCount());
129 
130         // test the comment list:
131         List commentList = comments.getCommentList();
132 
133         assertNotNull(commentList);
134         assertFalse(commentList.isEmpty());
135         assertEquals(10, commentList.size());
136 
137         // test the comment:
138         Comment comment = (Comment) commentList.iterator().next();
139 
140         assertNotNull(comment);
141         assertEquals(1205063572, comment.getDate());
142         assertEquals(13497702, comment.getId());
143         assertEquals(857895, comment.getStory());
144         assertEquals(1, comment.getUp());
145         assertEquals(2, comment.getDown());
146         assertEquals(3, comment.getReplies());
147         assertEquals("tronian", comment.getUser());
148         assertEquals(1, comment.getLevel());
149         assertEquals(13497702, comment.getRoot());
150         assertNotNull(comment.getComment());
151         assertTrue(comment.getComment().length() > 0);
152     }
153 
154     public void testParseContainers() throws ResponseParserException, DiggException {
155         InputStream inputStream = getClass().getResourceAsStream("/containers.xml");
156         Containers containers = ResponseParser.parseContainers(inputStream);
157 
158         assertNotNull(containers);
159         assertEquals(1207217720, containers.getTimestamp());
160         assertEquals(8, containers.getTotal());
161 
162         // test container list:
163         List containerWithTopicsList = containers.getContainerWithTopicsList();
164 
165         assertNotNull(containerWithTopicsList);
166         assertFalse(containerWithTopicsList.isEmpty());
167         assertEquals(8, containerWithTopicsList.size());
168 
169         // test container:
170         ContainerWithTopics containerWithTopics =
171                 (ContainerWithTopics) containerWithTopicsList.iterator().next();
172 
173         assertNotNull(containerWithTopics);
174         assertEquals("Technology", containerWithTopics.getName());
175         assertEquals("technology", containerWithTopics.getShortName());
176         assertEquals(11, containerWithTopics.getTotal());
177 
178         // test topic list:
179         List topicList = containerWithTopics.getTopicList();
180 
181         assertNotNull(topicList);
182         assertFalse(topicList.isEmpty());
183         assertEquals(11, topicList.size());
184 
185         Topic topic = (Topic) topicList.iterator().next();
186 
187         assertNotNull(topic);
188         assertEquals("Apple", topic.getName());
189         assertEquals("apple", topic.getShortName());
190     }
191 
192 }