14
14
15
15
package com .google .api .client .xml ;
16
16
17
- import com .google .api .client .http .HttpHeaders ;
18
- import com .google .api .client .xml .atom .Atom ;
17
+ import static org .junit .Assert .assertEquals ;
18
+ import static org .junit .Assert .assertNotNull ;
19
+ import static org .junit .Assert .assertNull ;
20
+ import java .io .ByteArrayInputStream ;
21
+ import java .io .InputStream ;
22
+ import java .io .StringReader ;
23
+ import java .net .URL ;
19
24
import java .util .List ;
20
- import junit .framework .TestCase ;
21
25
import org .junit .Assert ;
26
+ import org .junit .Test ;
27
+ import org .xmlpull .v1 .XmlPullParser ;
28
+ import com .google .api .client .http .HttpHeaders ;
29
+ import com .google .api .client .http .xml .atom .AtomFeedParser ;
30
+ import com .google .api .client .util .Charsets ;
31
+ import com .google .api .client .util .Key ;
32
+ import com .google .api .client .xml .atom .AbstractAtomFeedParser ;
33
+ import com .google .api .client .xml .atom .Atom ;
34
+ import com .google .common .io .Resources ;
22
35
23
36
/**
24
37
* Tests {@link Atom}.
25
38
*
26
39
* @author Yaniv Inbar
40
+ * @author Gerald Madlmayr
27
41
*/
28
- public class AtomTest extends TestCase {
42
+ public class AtomTest {
29
43
30
- @ SuppressWarnings ("unchecked" )
44
+
45
+ private static final String SAMPLE_FEED = "<?xml version=\" 1.0\" encoding=\" utf-8\" ?><feed " +
46
+ "xmlns=\" http://www.w3.org/2005/Atom\" > <title>Example Feed</title> <link href" +
47
+ "=\" http://example.org/\" /> <updated>2003-12-13T18:31:02Z</updated> <author> " +
48
+ "<name>John Doe</name> </author> <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6" +
49
+ "</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link href=\" http" +
50
+ "://example.org/2003/12/13/atom03\" /> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa" +
51
+ "-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text" +
52
+ ".</summary> </entry><entry> <title>Atom-Powered Robots Run Amok!</title> <link" +
53
+ " href=\" http://example.org/2003/12/13/atom02\" /> <id>urn:uuid:1225c695-cfb8-4ebb" +
54
+ "-aaaa-80da344efa62</id> <updated>2003-12-13T18:32:02Z</updated> <summary>Some " +
55
+ "other text.</summary> </entry></feed>" ;
56
+
57
+ /**
58
+ * Test for checking the Slug Header
59
+ */
60
+ @ Test
31
61
public void testSetSlugHeader () {
32
62
HttpHeaders headers = new HttpHeaders ();
33
63
assertNull (headers .get ("Slug" ));
34
64
subtestSetSlugHeader (headers , "value" , "value" );
35
- subtestSetSlugHeader (
36
- headers , " ! \" #$&'()*+,-./:;<=>?@[ \\ ]^_`{|}~" , " ! \" #$&'()*+,-./:; <=>?@[\\ ]^_`{|}~" );
65
+ subtestSetSlugHeader (headers , " ! \" #$&'()*+,-./:;<=>?@[ \\ ]^_`{|}~" , " ! \" #$&'()*+,-./:;" +
66
+ " <=>?@[\\ ]^_`{|}~" );
37
67
subtestSetSlugHeader (headers , "%D7%99%D7%A0%D7%99%D7%91" , "יניב" );
38
68
subtestSetSlugHeader (headers , null , null );
39
69
}
@@ -44,8 +74,230 @@ public void subtestSetSlugHeader(HttpHeaders headers, String expectedValue, Stri
44
74
if (value == null ) {
45
75
assertNull (headers .get ("Slug" ));
46
76
} else {
47
- Assert .assertArrayEquals (
48
- new String [] { expectedValue }, ((List <String >) headers .get ("Slug" )).toArray ());
77
+ Assert .assertArrayEquals (new String []{ expectedValue },
78
+ ((List <String >) headers .get ("Slug" )).toArray ());
49
79
}
50
80
}
81
+
82
+ /**
83
+ * This tests parses a simple Atom Feed given as a constant. All elements are asserted, to see if
84
+ * everything works fine. For parsing a dedicated {@link AtomFeedParser} is used.
85
+ *
86
+ * The purpose of this test is to test the {@link AtomFeedParser#parseFeed} and {@link
87
+ * AtomFeedParser#parseNextEntry} and see if the mapping of the XML element to the entity classes
88
+ * is done correctly.
89
+ */
90
+ @ Test
91
+ public void testAtomFeedUsingCustomizedParser () throws Exception {
92
+ XmlPullParser parser = Xml .createParser ();
93
+ // Wired. Both, the InputStream for the FeedParser and the XPP need to be set (?)
94
+ parser .setInput (new StringReader (SAMPLE_FEED ));
95
+ InputStream stream = new ByteArrayInputStream (SAMPLE_FEED .getBytes ());
96
+ XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary ();
97
+ AbstractAtomFeedParser atomParser = new AtomFeedParser <Feed , FeedEntry >(namespaceDictionary ,
98
+ parser , stream , Feed .class , FeedEntry .class );
99
+
100
+ Feed feed = (Feed ) atomParser .parseFeed ();
101
+ assertEquals ("John Doe" , feed .author .name );
102
+ assertEquals ("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6" , feed .id );
103
+ assertEquals ("2003-12-13T18:31:02Z" , feed .updated );
104
+ assertEquals ("Example Feed" , feed .title );
105
+ assertEquals ("http://example.org/" , feed .link .href );
106
+
107
+ FeedEntry entry1 = (FeedEntry ) atomParser .parseNextEntry ();
108
+ //assertNotNull(feed.entry);
109
+ assertEquals ("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a" , entry1 .id );
110
+ assertEquals ("2003-12-13T18:30:02Z" , entry1 .updated );
111
+ assertEquals ("Some text." , entry1 .summary );
112
+ assertEquals ("Atom-Powered Robots Run Amok" , entry1 .title );
113
+ assertEquals ("http://example.org/2003/12/13/atom03" , entry1 .link .href );
114
+
115
+ FeedEntry entry2 = (FeedEntry ) atomParser .parseNextEntry ();
116
+ assertEquals ("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa62" , entry2 .id );
117
+ assertEquals ("2003-12-13T18:32:02Z" , entry2 .updated );
118
+ assertEquals ("Some other text." , entry2 .summary );
119
+ assertEquals ("Atom-Powered Robots Run Amok!" , entry2 .title );
120
+ assertEquals ("http://example.org/2003/12/13/atom02" , entry2 .link .href );
121
+
122
+ FeedEntry entry3 = (FeedEntry ) atomParser .parseNextEntry ();
123
+ assertNull (entry3 );
124
+
125
+ atomParser .close ();
126
+ }
127
+
128
+ /**
129
+ * Tests of a constant string to see if the data structure can be parsed using the standard
130
+ * method {@link Xml#parseElement}
131
+ *
132
+ * The purpose of this test is to assert, if the parsed elements are correctly parsed using a
133
+ * {@link AtomFeedParser}.
134
+ */
135
+ @ Test
136
+ public void testAtomFeedUsingStandardParser () throws Exception {
137
+ Feed feed = new Feed ();
138
+ XmlPullParser parser = Xml .createParser ();
139
+ parser .setInput (new StringReader (SAMPLE_FEED ));
140
+ XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary ();
141
+ Xml .parseElement (parser , feed , namespaceDictionary , null );
142
+ assertNotNull (feed );
143
+ assertEquals (2 , feed .entry .length );
144
+
145
+ assertEquals ("John Doe" , feed .author .name );
146
+ assertEquals ("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6" , feed .id );
147
+ assertEquals ("2003-12-13T18:31:02Z" , feed .updated );
148
+ assertEquals ("Example Feed" , feed .title );
149
+ assertEquals ("http://example.org/" , feed .link .href );
150
+
151
+ FeedEntry entry1 = feed .entry [0 ];
152
+ //assertNotNull(feed.entry);
153
+ assertEquals ("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a" , entry1 .id );
154
+ assertEquals ("2003-12-13T18:30:02Z" , entry1 .updated );
155
+ assertEquals ("Some text." , entry1 .summary );
156
+ assertEquals ("Atom-Powered Robots Run Amok" , entry1 .title );
157
+ assertEquals ("http://example.org/2003/12/13/atom03" , entry1 .link .href );
158
+
159
+ FeedEntry entry2 = feed .entry [1 ];
160
+ assertEquals ("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa62" , entry2 .id );
161
+ assertEquals ("2003-12-13T18:32:02Z" , entry2 .updated );
162
+ assertEquals ("Some other text." , entry2 .summary );
163
+ assertEquals ("Atom-Powered Robots Run Amok!" , entry2 .title );
164
+ assertEquals ("http://example.org/2003/12/13/atom02" , entry2 .link .href );
165
+ }
166
+
167
+ /**
168
+ * Read an XML ATOM Feed from a file to a string and assert if all the {@link FeedEntry}s are
169
+ * present. No detailed assertion of each element
170
+ *
171
+ * The purpose of this test is to read a bunch of elements which contain additional elements
172
+ * (HTML in this case), that are not part of the {@link FeedEntry} and to see if there is an issue
173
+ * if we parse some more entries.
174
+ */
175
+ @ Test
176
+ public void testSampleFeedParser () throws Exception {
177
+ XmlPullParser parser = Xml .createParser ();
178
+ URL url = Resources .getResource ("sample-atom.xml" );
179
+ String read = Resources .toString (url , Charsets .UTF_8 );
180
+ parser .setInput (new StringReader (read ));
181
+ XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary ();
182
+ AbstractAtomFeedParser atomParser = new AtomFeedParser <Feed , FeedEntry >(namespaceDictionary ,
183
+ parser , new ByteArrayInputStream (read .getBytes ()), Feed .class , FeedEntry .class );
184
+ Feed feed = (Feed ) atomParser .parseFeed ();
185
+ assertNotNull (feed );
186
+
187
+ // validate feed 1 -- Long Content
188
+ FeedEntry entry = (FeedEntry ) atomParser .parseNextEntry ();
189
+ assertNotNull (entry );
190
+ assertNotNull (entry .id );
191
+ assertNotNull (entry .title );
192
+ assertNotNull (entry .summary );
193
+ assertNotNull (entry .link );
194
+ assertNotNull (entry .updated );
195
+ assertNotNull (entry .content );
196
+ assertEquals (5000 , entry .content .length ());
197
+
198
+ // validate feed 2 -- Special Charts
199
+ entry = (FeedEntry ) atomParser .parseNextEntry ();
200
+ assertNotNull (entry );
201
+ assertNotNull (entry .id );
202
+ assertNotNull (entry .title );
203
+ assertNotNull (entry .summary );
204
+ assertNotNull (entry .link );
205
+ assertNotNull (entry .updated );
206
+ assertNotNull (entry .content );
207
+ assertEquals ("aäb cde fgh ijk lmn oöpoöp tuü vwx yz AÄBC DEF GHI JKL MNO ÖPQ RST UÜV WXYZ " +
208
+ "!\" § $%& /() =?* '<> #|; ²³~ @`´ ©«» ¼× {} aäb cde fgh ijk lmn oöp qrsß tuü vwx yz " +
209
+ "AÄBC DEF GHI JKL MNO" , entry .content );
210
+
211
+ // validate feed 3 -- Missing Content
212
+ entry = (FeedEntry ) atomParser .parseNextEntry ();
213
+ assertNotNull (entry );
214
+ assertNotNull (entry .id );
215
+ assertNotNull (entry .title );
216
+ assertNotNull (entry .summary );
217
+ assertNotNull (entry .link );
218
+ assertNotNull (entry .updated );
219
+ assertNull (entry .content );
220
+
221
+ // validate feed 4 -- Missing Updated
222
+ entry = (FeedEntry ) atomParser .parseNextEntry ();
223
+ assertNotNull (entry );
224
+ assertNotNull (entry .id );
225
+ assertNotNull (entry .title );
226
+ assertNotNull (entry .summary );
227
+ assertNotNull (entry .link );
228
+ assertNull (entry .updated );
229
+ assertNotNull (entry .content );
230
+
231
+ // validate feed 5
232
+ entry = (FeedEntry ) atomParser .parseNextEntry ();
233
+ assertNotNull (entry );
234
+ assertNotNull (entry .id );
235
+ assertNotNull (entry .title );
236
+ assertNull (entry .summary );
237
+ assertNotNull (entry .link );
238
+ assertNotNull (entry .updated );
239
+ assertNotNull (entry .content );
240
+
241
+ // validate feed 6
242
+ entry = (FeedEntry ) atomParser .parseNextEntry ();
243
+ assertNull (entry );
244
+
245
+ atomParser .close ();
246
+ }
247
+
248
+ /**
249
+ * Feed Element to map the XML to
250
+ */
251
+ public static class Feed {
252
+ @ Key
253
+ private String title ;
254
+ @ Key
255
+ private Link link ;
256
+ @ Key
257
+ private String updated ;
258
+ @ Key
259
+ private Author author ;
260
+ @ Key
261
+ private String id ;
262
+ @ Key
263
+ private FeedEntry [] entry ;
264
+ }
265
+
266
+ /**
267
+ * Author Element as part of the {@link Feed} Element to map the XML to. As this is sub-element,
268
+ * this needs to be public.
269
+ */
270
+ public static class Author {
271
+ @ Key
272
+ private String name ;
273
+ }
274
+
275
+ /**
276
+ * Link Element as part of the {@link Feed} Element to map the XML to. As this is sub-element,
277
+ * this needs to be public.
278
+ */
279
+ public static class Link {
280
+ @ Key ("@href" )
281
+ private String href ;
282
+ }
283
+
284
+ /**
285
+ * Entry Element to cover the Entries of a Atom {@link Feed}. As this is sub-element,
286
+ * this needs to be public.
287
+ */
288
+ public static class FeedEntry {
289
+ @ Key
290
+ private String title ;
291
+ @ Key
292
+ private Link link ;
293
+ @ Key
294
+ private String updated ;
295
+ @ Key
296
+ private String summary ;
297
+ @ Key
298
+ private String id ;
299
+ @ Key
300
+ private String content ;
301
+ }
51
302
}
303
+
0 commit comments