1require "rss-testcase"
2
3require "rss/maker"
4
5module RSS
6  class TestSetupMakerAtomFeed < TestCase
7    def setup
8      t = Time.iso8601("2000-01-01T12:00:05+00:00")
9      class << t
10        alias_method(:to_s, :iso8601)
11      end
12
13      @dc_elems = {
14        :title => "hoge",
15        :description =>
16          " XML is placing increasingly heavy loads on
17          the existing technical infrastructure of the Internet.",
18        :creator => "Rael Dornfest (mailto:rael@oreilly.com)",
19        :subject => "XML",
20        :publisher => "The O'Reilly Network",
21        :contributor => "hogehoge",
22        :type => "fugafuga",
23        :format => "hohoho",
24        :identifier => "fufufu",
25        :source => "barbar",
26        :language => "ja",
27        :relation => "cococo",
28        :rights => "Copyright (c) 2000 O'Reilly &amp; Associates, Inc.",
29        :date => t,
30      }
31    end
32
33    def test_setup_maker_feed(with_dc=true)
34      authors = [
35                 {
36                   :name => "Bob",
37                   :uri => "http://example.com/~bob/",
38                   :email => "bob@example.com",
39                 },
40                 {
41                   :name => "Alice",
42                   :uri => "http://example.com/~alice/",
43                   :email => "alice@example.com",
44                 },
45                ]
46      categories = [
47                    {
48                      :term => "music",
49                      :label => "Music",
50                    },
51                    {
52                      :term => "book",
53                      :scheme => "http://example.com/category/book/",
54                      :label => "Book",
55                    },
56                   ]
57      contributors = [
58                      {
59                        :name => "Chris",
60                        :email => "chris@example.com",
61                      },
62                      {
63                        :name => "Eva",
64                        :uri => "http://example.com/~eva/",
65                      },
66                     ]
67      generator = {
68        :uri => "http://example.com/generator/",
69        :version => "0.0.1",
70        :content => "Feed Generator",
71      }
72      icon = "http://example.com/icon.png"
73      about = "http://hoge.com"
74      title = "fugafuga"
75      link = "http://hoge.com"
76      logo = "http://example.com/logo.png"
77      rights = "Copyrights (c) 2007 Alice and Bob"
78      description = "fugafugafugafuga"
79      updated = Time.now
80
81      feed = RSS::Maker.make("atom") do |maker|
82        authors.each do |author_info|
83          maker.channel.authors.new_author do |author|
84            author_info.each do |key, value|
85              author.__send__("#{key}=", value)
86            end
87          end
88        end
89
90        categories.each do |category_info|
91          maker.channel.categories.new_category do |category|
92            category_info.each do |key, value|
93              category.__send__("#{key}=", value)
94            end
95          end
96        end
97
98        contributors.each do |contributor_info|
99          maker.channel.contributors.new_contributor do |contributor|
100            contributor_info.each do |key, value|
101              contributor.__send__("#{key}=", value)
102            end
103          end
104        end
105
106        generator.each do |key, value|
107          maker.channel.generator do |g|
108            g.__send__("#{key}=", value)
109          end
110        end
111
112        maker.channel.icon = icon
113
114        maker.channel.about = about
115        maker.channel.link = link
116        maker.channel.logo = logo
117        maker.channel.rights = rights
118        maker.channel.title = title
119        maker.channel.description = description
120        maker.channel.updated = updated
121
122        if with_dc
123          @dc_elems.each do |var, value|
124            if var == :date
125              maker.channel.new_dc_date(value)
126            else
127              maker.channel.__send__("dc_#{var}=", value)
128            end
129          end
130        end
131
132        setup_dummy_item_atom(maker)
133      end
134      assert_not_nil(feed)
135
136      new_feed = RSS::Maker.make("atom") do |maker|
137        feed.setup_maker(maker)
138      end
139      assert_not_nil(new_feed)
140
141      new_authors = new_feed.authors.collect do |author|
142        {
143          :name => author.name.content,
144          :uri => author.uri.content,
145          :email => author.email.content,
146        }
147      end
148      assert_equal(authors, new_authors)
149
150      new_categories = new_feed.categories.collect do |category|
151        {
152          :term => category.term,
153          :scheme => category.scheme,
154          :label => category.label,
155        }.reject {|key, value| value.nil?}
156      end
157      assert_equal(categories, new_categories)
158
159      new_contributors = new_feed.contributors.collect do |contributor|
160        info = {}
161        info[:name] = contributor.name.content
162        info[:uri] = contributor.uri.content if contributor.uri
163        info[:email] = contributor.email.content if contributor.email
164        info
165      end
166      assert_equal(contributors, new_contributors)
167
168      new_generator = {
169        :uri => new_feed.generator.uri,
170        :version => new_feed.generator.version,
171        :content => new_feed.generator.content,
172      }
173      assert_equal(generator, new_generator)
174
175      assert_equal(icon, new_feed.icon.content)
176      assert_equal(about, new_feed.id.content)
177      assert_equal(link, new_feed.link.href)
178      assert_equal(logo, new_feed.logo.content)
179      assert_equal(rights, new_feed.rights.content)
180      assert_equal(description, new_feed.subtitle.content)
181      assert_equal(title, new_feed.title.content)
182      assert_equal(updated, new_feed.updated.content)
183
184      if with_dc
185        @dc_elems.each do |var, value|
186          if var == :date
187            assert_equal([updated, value],
188                         new_feed.dc_dates.collect {|date| date.value})
189          else
190            assert_equal(value, new_feed.__send__("dc_#{var}"))
191          end
192        end
193      end
194
195      assert_equal(1, new_feed.items.size)
196    end
197
198    def test_setup_maker_feed_without_dc
199      test_setup_maker_feed(false)
200    end
201
202    def test_setup_maker_items(for_backward_compatibility=false)
203      title = "TITLE"
204      link = "http://hoge.com/"
205      description = "text hoge fuga"
206      updated = Time.now
207
208      item_size = 5
209      feed = RSS::Maker.make("atom") do |maker|
210        setup_dummy_channel_atom(maker)
211
212        item_size.times do |i|
213          maker.items.new_item do |item|
214            item.title = "#{title}#{i}"
215            item.link = "#{link}#{i}"
216            item.description = "#{description}#{i}"
217            item.updated = updated + i * 60
218          end
219        end
220      end
221
222      new_feed = RSS::Maker.make("atom") do |maker|
223        feed.items.each do |item|
224          if for_backward_compatibility
225            item.setup_maker(maker)
226          else
227            item.setup_maker(maker.items)
228          end
229        end
230
231        feed.items.clear
232        feed.setup_maker(maker)
233      end
234
235      assert_equal(item_size, new_feed.items.size)
236      new_feed.items.each_with_index do |item, i|
237        assert_equal("#{title}#{i}", item.title.content)
238        assert_equal("#{link}#{i}", item.link.href)
239        assert_equal("#{description}#{i}", item.summary.content)
240        assert_equal(updated + i * 60, item.updated.content)
241      end
242    end
243
244    def test_setup_maker_items_sort
245      title = "TITLE"
246      link = "http://hoge.com/"
247      summary = "text hoge fuga"
248      updated = Time.now
249
250      feed_size = 5
251      feed = RSS::Maker.make("atom") do |maker|
252        setup_dummy_channel_atom(maker)
253
254        feed_size.times do |i|
255          entry_class = RSS::Atom::Feed::Entry
256          entry = entry_class.new
257          entry.title = entry_class::Title.new(:content => "#{title}#{i}")
258          entry.links << entry_class::Link.new(:href => "#{link}#{i}")
259          entry.summary = entry_class::Summary.new(:content => "#{summary}#{i}")
260          entry.updated = entry_class::Updated.new(:content => updated + i * 60)
261          entry.setup_maker(maker.items)
262        end
263        maker.items.do_sort = false
264      end
265      assert_equal(feed_size, feed.entries.size)
266      feed.entries.each_with_index do |entry, i|
267        assert_equal("#{title}#{i}", entry.title.content)
268        assert_equal("#{link}#{i}", entry.link.href)
269        assert_equal("#{summary}#{i}", entry.summary.content)
270      end
271
272
273      feed = RSS::Maker.make("atom") do |maker|
274        setup_dummy_channel_atom(maker)
275
276        feed_size.times do |i|
277          entry_class = RSS::Atom::Feed::Entry
278          entry = entry_class.new
279          entry.title = entry_class::Title.new(:content => "#{title}#{i}")
280          entry.links << entry_class::Link.new(:href => "#{link}#{i}")
281          entry.summary = entry_class::Summary.new(:content => "#{summary}#{i}")
282          entry.updated = entry_class::Updated.new(:content => updated + i * 60)
283          entry.setup_maker(maker.items)
284        end
285        maker.items.do_sort = true
286       end
287      assert_equal(feed_size, feed.entries.size)
288      feed.entries.reverse.each_with_index do |entry, i|
289        assert_equal("#{title}#{i}", entry.title.content)
290        assert_equal("#{link}#{i}", entry.link.href)
291        assert_equal("#{summary}#{i}", entry.summary.content)
292      end
293    end
294
295    def test_setup_maker_items_backward_compatibility
296      test_setup_maker_items(true)
297    end
298
299    def test_setup_maker
300      encoding = "EUC-JP"
301      standalone = true
302
303      href = 'a.xsl'
304      type = 'text/xsl'
305      title = 'sample'
306      media = 'printer'
307      charset = 'UTF-8'
308      alternate = 'yes'
309
310      feed = RSS::Maker.make("atom") do |maker|
311        maker.encoding = encoding
312        maker.standalone = standalone
313
314        maker.xml_stylesheets.new_xml_stylesheet do |xss|
315          xss.href = href
316          xss.type = type
317          xss.title = title
318          xss.media = media
319          xss.charset = charset
320          xss.alternate = alternate
321        end
322
323        setup_dummy_channel_atom(maker)
324        setup_dummy_item_atom(maker)
325      end
326      assert_not_nil(feed)
327
328      new_feed = RSS::Maker.make("atom") do |maker|
329        feed.setup_maker(maker)
330      end
331
332      assert_equal(["atom", "1.0", "feed"], new_feed.feed_info)
333      assert_equal(encoding, new_feed.encoding)
334      assert_equal(standalone, new_feed.standalone)
335
336      xss = new_feed.xml_stylesheets.first
337      assert_equal(1, new_feed.xml_stylesheets.size)
338      assert_equal(href, xss.href)
339      assert_equal(type, xss.type)
340      assert_equal(title, xss.title)
341      assert_equal(media, xss.media)
342      assert_equal(charset, xss.charset)
343      assert_equal(alternate, xss.alternate)
344    end
345
346    def test_setup_maker_full
347      encoding = "EUC-JP"
348      standalone = true
349
350      href = 'a.xsl'
351      type = 'text/xsl'
352      title = 'sample'
353      media = 'printer'
354      charset = 'UTF-8'
355      alternate = 'yes'
356
357      channel_about = "http://hoge.com"
358      channel_title = "fugafuga"
359      channel_link = "http://hoge.com"
360      channel_description = "fugafugafugafuga"
361      channel_author = "Bob"
362
363      image_url = "http://hoge.com/hoge.png"
364
365      item_title = "TITLE"
366      item_link = "http://hoge.com/"
367      item_description = "text hoge fuga"
368
369      entry_size = 5
370      feed = RSS::Maker.make("atom") do |maker|
371        maker.encoding = encoding
372        maker.standalone = standalone
373
374        maker.xml_stylesheets.new_xml_stylesheet do |xss|
375          xss.href = href
376          xss.type = type
377          xss.title = title
378          xss.media = media
379          xss.charset = charset
380          xss.alternate = alternate
381        end
382
383        maker.channel.about = channel_about
384        maker.channel.title = channel_title
385        maker.channel.link = channel_link
386        maker.channel.description = channel_description
387        maker.channel.author = channel_author
388        @dc_elems.each do |var, value|
389          maker.channel.__send__("dc_#{var}=", value)
390        end
391
392        maker.image.url = image_url
393
394        entry_size.times do |i|
395          maker.items.new_item do |item|
396            item.title = "#{item_title}#{i}"
397            item.link = "#{item_link}#{i}"
398            item.description = "#{item_description}#{i}"
399
400            @dc_elems.each do |var, value|
401              item.__send__("dc_#{var}=", value)
402            end
403          end
404        end
405      end
406
407      new_feed = RSS::Maker.make("atom") do |maker|
408        feed.setup_maker(maker)
409      end
410
411      assert_equal(["atom", "1.0", "feed"], new_feed.feed_info)
412      assert_equal(encoding, new_feed.encoding)
413      assert_equal(standalone, new_feed.standalone)
414
415      xss = new_feed.xml_stylesheets.first
416      assert_equal(1, new_feed.xml_stylesheets.size)
417      assert_equal(href, xss.href)
418      assert_equal(type, xss.type)
419      assert_equal(title, xss.title)
420      assert_equal(media, xss.media)
421      assert_equal(charset, xss.charset)
422      assert_equal(alternate, xss.alternate)
423
424      assert_equal(channel_title, new_feed.title.content)
425      assert_equal(channel_link, new_feed.link.href)
426      assert_equal(channel_description, new_feed.subtitle.content)
427      assert_equal(channel_author, new_feed.author.name.content)
428      assert_equal(image_url, new_feed.logo.content)
429      @dc_elems.each do |var, value|
430        assert_equal(value, new_feed.__send__("dc_#{var}"))
431      end
432
433      assert_equal(entry_size, new_feed.entries.size)
434      new_feed.entries.each_with_index do |entry, i|
435        assert_equal("#{item_title}#{i}", entry.title.content)
436        assert_equal("#{item_link}#{i}", entry.link.href)
437        assert_equal("#{item_description}#{i}", entry.summary.content)
438
439        @dc_elems.each do |var, value|
440          assert_equal(value, entry.__send__("dc_#{var}"))
441        end
442      end
443    end
444  end
445end
446