1require "rss-testcase" 2 3require "rss/maker" 4 5module RSS 6 class TestSetupMakerAtomEntry < 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 & Associates, Inc.", 29 :date => t, 30 } 31 end 32 33 def test_setup_maker_entry(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 id = "urn:uuid:8b105336-7e20-45fc-bb78-37fb3e1db25a" 68 link = "http://hoge.com" 69 published = Time.now - 60 * 3600 70 rights = "Copyrights (c) 2007 Alice and Bob" 71 description = "fugafugafugafuga" 72 title = "fugafuga" 73 updated = Time.now 74 75 feed = RSS::Maker.make("atom:entry") do |maker| 76 maker.items.new_item do |item| 77 authors.each do |author_info| 78 item.authors.new_author do |author| 79 author_info.each do |key, value| 80 author.__send__("#{key}=", value) 81 end 82 end 83 end 84 85 categories.each do |category_info| 86 item.categories.new_category do |category| 87 category_info.each do |key, value| 88 category.__send__("#{key}=", value) 89 end 90 end 91 end 92 93 contributors.each do |contributor_info| 94 item.contributors.new_contributor do |contributor| 95 contributor_info.each do |key, value| 96 contributor.__send__("#{key}=", value) 97 end 98 end 99 end 100 101 item.id = id 102 item.link = link 103 item.published = published 104 item.rights = rights 105 item.description = description 106 item.title = title 107 item.updated = updated 108 109 if with_dc 110 @dc_elems.each do |var, value| 111 if var == :date 112 item.new_dc_date(value) 113 else 114 item.__send__("dc_#{var}=", value) 115 end 116 end 117 end 118 end 119 end 120 assert_not_nil(feed) 121 122 new_feed = RSS::Maker.make("atom:entry") do |maker| 123 feed.setup_maker(maker) 124 end 125 assert_not_nil(new_feed) 126 127 new_authors = new_feed.authors.collect do |author| 128 { 129 :name => author.name.content, 130 :uri => author.uri.content, 131 :email => author.email.content, 132 } 133 end 134 assert_equal(authors, new_authors) 135 136 new_categories = new_feed.categories.collect do |category| 137 { 138 :term => category.term, 139 :scheme => category.scheme, 140 :label => category.label, 141 }.reject {|key, value| value.nil?} 142 end 143 assert_equal(categories, new_categories) 144 145 new_contributors = new_feed.contributors.collect do |contributor| 146 info = {} 147 info[:name] = contributor.name.content 148 info[:uri] = contributor.uri.content if contributor.uri 149 info[:email] = contributor.email.content if contributor.email 150 info 151 end 152 assert_equal(contributors, new_contributors) 153 154 assert_equal(id, new_feed.id.content) 155 assert_equal(link, new_feed.link.href) 156 assert_equal(published, new_feed.published.content) 157 assert_equal(rights, new_feed.rights.content) 158 assert_equal(description, new_feed.summary.content) 159 assert_equal(title, new_feed.title.content) 160 assert_equal(updated, new_feed.updated.content) 161 162 if with_dc 163 @dc_elems.each do |var, value| 164 if var == :date 165 assert_equal([updated, value], 166 new_feed.dc_dates.collect {|date| date.value}) 167 else 168 assert_equal(value, new_feed.__send__("dc_#{var}")) 169 end 170 end 171 end 172 173 assert_equal(1, new_feed.items.size) 174 end 175 176 def test_setup_maker_entry_without_dc 177 test_setup_maker_entry(false) 178 end 179 180 def test_setup_maker_items(for_backward_compatibility=false) 181 title = "TITLE" 182 link = "http://hoge.com/" 183 description = "text hoge fuga" 184 updated = Time.now 185 186 item_size = 5 187 feed = RSS::Maker.make("atom:entry") do |maker| 188 setup_dummy_channel_atom(maker) 189 190 item_size.times do |i| 191 maker.items.new_item do |item| 192 item.title = "#{title}#{i}" 193 item.link = "#{link}#{i}" 194 item.description = "#{description}#{i}" 195 item.updated = updated + i * 60 196 end 197 end 198 end 199 200 new_feed = RSS::Maker.make("atom:entry") do |maker| 201 feed.items.each do |item| 202 if for_backward_compatibility 203 item.setup_maker(maker) 204 else 205 item.setup_maker(maker.items) 206 end 207 end 208 209 feed.items.clear 210 feed.setup_maker(maker) 211 end 212 213 assert_equal(1, new_feed.items.size) 214 new_feed.items[0..1].each_with_index do |item, i| 215 assert_equal("#{title}#{i}", item.title.content) 216 assert_equal("#{link}#{i}", item.link.href) 217 assert_equal("#{description}#{i}", item.summary.content) 218 assert_equal(updated + i * 60, item.updated.content) 219 end 220 end 221 222 def test_setup_maker_items_sort 223 title = "TITLE" 224 link = "http://hoge.com/" 225 summary = "text hoge fuga" 226 updated = Time.now 227 228 feed_size = 5 229 feed = RSS::Maker.make("atom:entry") do |maker| 230 setup_dummy_channel_atom(maker) 231 232 feed_size.times do |i| 233 entry_class = RSS::Atom::Entry 234 entry = entry_class.new 235 entry.title = entry_class::Title.new(:content => "#{title}#{i}") 236 entry.links << entry_class::Link.new(:href => "#{link}#{i}") 237 entry.summary = entry_class::Summary.new(:content => "#{summary}#{i}") 238 entry.updated = entry_class::Updated.new(:content => updated + i * 60) 239 entry.setup_maker(maker.items) 240 end 241 maker.items.do_sort = false 242 end 243 assert_equal(1, feed.items.size) 244 245 assert_equal("#{title}0", feed.title.content) 246 assert_equal("#{link}0", feed.link.href) 247 assert_equal("#{summary}0", feed.summary.content) 248 249 250 feed = RSS::Maker.make("atom:entry") do |maker| 251 setup_dummy_channel_atom(maker) 252 253 feed_size.times do |i| 254 entry_class = RSS::Atom::Entry 255 entry = entry_class.new 256 entry.title = entry_class::Title.new(:content => "#{title}#{i}") 257 entry.links << entry_class::Link.new(:href => "#{link}#{i}") 258 entry.summary = entry_class::Summary.new(:content => "#{summary}#{i}") 259 entry.updated = entry_class::Updated.new(:content => updated + i * 60) 260 entry.setup_maker(maker.items) 261 end 262 maker.items.do_sort = true 263 end 264 assert_equal(1, feed.items.size) 265 266 assert_equal("#{title}#{feed_size - 1}", feed.title.content) 267 assert_equal("#{link}#{feed_size - 1}", feed.link.href) 268 assert_equal("#{summary}#{feed_size - 1}", feed.summary.content) 269 end 270 271 def test_setup_maker_items_backward_compatibility 272 test_setup_maker_items(true) 273 end 274 275 def test_setup_maker 276 encoding = "EUC-JP" 277 standalone = true 278 279 href = 'a.xsl' 280 type = 'text/xsl' 281 title = 'sample' 282 media = 'printer' 283 charset = 'UTF-8' 284 alternate = 'yes' 285 286 feed = RSS::Maker.make("atom:entry") do |maker| 287 maker.encoding = encoding 288 maker.standalone = standalone 289 290 maker.xml_stylesheets.new_xml_stylesheet do |xss| 291 xss.href = href 292 xss.type = type 293 xss.title = title 294 xss.media = media 295 xss.charset = charset 296 xss.alternate = alternate 297 end 298 299 setup_dummy_channel_atom(maker) 300 setup_dummy_item_atom(maker) 301 end 302 assert_not_nil(feed) 303 304 new_feed = RSS::Maker.make("atom:entry") do |maker| 305 feed.setup_maker(maker) 306 end 307 308 assert_equal(["atom", "1.0", "entry"], new_feed.feed_info) 309 assert_equal(encoding, new_feed.encoding) 310 assert_equal(standalone, new_feed.standalone) 311 312 xss = new_feed.xml_stylesheets.first 313 assert_equal(1, new_feed.xml_stylesheets.size) 314 assert_equal(href, xss.href) 315 assert_equal(type, xss.type) 316 assert_equal(title, xss.title) 317 assert_equal(media, xss.media) 318 assert_equal(charset, xss.charset) 319 assert_equal(alternate, xss.alternate) 320 end 321 322 def test_setup_maker_full 323 encoding = "EUC-JP" 324 standalone = true 325 326 href = 'a.xsl' 327 type = 'text/xsl' 328 title = 'sample' 329 media = 'printer' 330 charset = 'UTF-8' 331 alternate = 'yes' 332 333 channel_about = "http://hoge.com" 334 channel_title = "fugafuga" 335 channel_link = "http://hoge.com" 336 channel_description = "fugafugafugafuga" 337 channel_author = "Bob" 338 339 image_url = "http://hoge.com/hoge.png" 340 341 item_title = "TITLE" 342 item_link = "http://hoge.com/" 343 item_description = "text hoge fuga" 344 345 entry_size = 5 346 feed = RSS::Maker.make("atom:entry") do |maker| 347 maker.encoding = encoding 348 maker.standalone = standalone 349 350 maker.xml_stylesheets.new_xml_stylesheet do |xss| 351 xss.href = href 352 xss.type = type 353 xss.title = title 354 xss.media = media 355 xss.charset = charset 356 xss.alternate = alternate 357 end 358 359 maker.channel.about = channel_about 360 maker.channel.title = channel_title 361 maker.channel.link = channel_link 362 maker.channel.description = channel_description 363 maker.channel.author = channel_author 364 @dc_elems.each do |var, value| 365 maker.channel.__send__("dc_#{var}=", value) 366 end 367 368 maker.image.url = image_url 369 370 entry_size.times do |i| 371 maker.items.new_item do |item| 372 item.title = "#{item_title}#{i}" 373 item.link = "#{item_link}#{i}" 374 item.description = "#{item_description}#{i}" 375 376 @dc_elems.each do |var, value| 377 item.__send__("dc_#{var}=", value) 378 end 379 end 380 end 381 end 382 383 new_feed = RSS::Maker.make("atom:entry") do |maker| 384 feed.setup_maker(maker) 385 end 386 387 assert_equal(["atom", "1.0", "entry"], new_feed.feed_info) 388 assert_equal(encoding, new_feed.encoding) 389 assert_equal(standalone, new_feed.standalone) 390 391 xss = new_feed.xml_stylesheets.first 392 assert_equal(1, new_feed.xml_stylesheets.size) 393 assert_equal(href, xss.href) 394 assert_equal(type, xss.type) 395 assert_equal(title, xss.title) 396 assert_equal(media, xss.media) 397 assert_equal(charset, xss.charset) 398 assert_equal(alternate, xss.alternate) 399 400 assert_equal("#{item_title}0", new_feed.title.content) 401 assert_equal("#{item_link}0", new_feed.link.href) 402 assert_equal("#{item_description}0", new_feed.summary.content) 403 @dc_elems.each do |var, value| 404 assert_equal(value, new_feed.__send__("dc_#{var}")) 405 end 406 assert_equal(1, new_feed.items.size) 407 end 408 end 409end 410