require File.dirname(__FILE__) + '/../test_setup' require 'set' context 'S33r bucket listing' do setup do @with_bucket_listing_xml = load_test_xml('bucket_listing.xml') @with_bucket_listing_xml2 = load_test_xml('bucket_listing2.xml') @with_empty_bucket_listing_xml = load_test_xml('bucket_listing3.xml') @with_broken_bucket_listing_xml = load_test_xml('bucket_listing_broken.xml') @with_suspect_bl_xml = load_test_xml('suspect_bucket_listing.xml') @bucket_listing = BucketListing.new(@with_bucket_listing_xml) @bucket_properties = %w(name prefix marker max_keys is_truncated) @bucket_property_setters = @bucket_properties.map { |prop| prop + "=" } end specify 'can only be created if bucket listing XML supplied' do lambda { BucketListing.new }.should.raise ArgumentError end specify 'cannot be created from invalid XML' do lambda { BucketListing.new(nil) }.should.raise S3Exception::InvalidBucketListing end specify 'should recover gracefully from broken bucket listing XML' do lambda { BucketListing.new(@with_broken_bucket_listing_xml) }.should.raise S3Exception::InvalidBucketListing end specify 'should cope if bucket is empty (i.e. no elements)' do @bucket_listing.set_listing_xml(@with_empty_bucket_listing_xml) end specify 'can have the bucket listing XML reset' do @bucket_listing.should.respond_to :set_listing_xml end specify 'should present bucket metadata as typed properties' do @bucket_listing.name.should.equal('testingtesting') @bucket_listing.prefix.should.equal('') @bucket_listing.marker.should.equal('') @bucket_listing.max_keys.should.equal(1000) @bucket_listing.is_truncated.should.equal(false) @bucket_listing.delimiter.should.be nil end specify 'when listing XML is reset, should update all properties correctly' do @bucket_listing.set_listing_xml(@with_bucket_listing_xml2) @bucket_listing.name.should.equal('testing2') @bucket_listing.prefix.should.equal('/home/ell/') @bucket_listing.marker.should.equal('') @bucket_listing.max_keys.should.equal(100) @bucket_listing.delimiter.should.equal('/') @bucket_listing.is_truncated.should.equal(false) end specify 'should provide private setters for metadata' do # all private methods methods = @bucket_listing.private_methods.to_set # names of the setters setters = @bucket_property_setters # all methods should be a superset of the private setters methods.superset?(setters.to_set).should.equal true end specify 'should store resources ( elements) in a hash' do @bucket_listing.size.should_be 10 first_obj = @bucket_listing['/home/ell/dir1/four.txt'] first_obj.should_be_instance_of S3Object end # attempting to fix ListBucketResult errors reported by Alex Payne specify 'should handle suspect bucket listing' do puts @with_suspect_bl_xml bl = BucketListing.new(@with_suspect_bl_xml) bl.size.should_be 1 bl.keys.should.include 'orly.jpg' end end