From d93c5e6076d904882d5eb13b114a2f5cfb92afb1 Mon Sep 17 00:00:00 2001 From: Leonardo Crauss Daronco Date: Sun, 11 May 2014 17:29:09 -0300 Subject: [PATCH] Add the last missing tests for the changes made for BBB 0.81 refs #1129 --- lib/bigbluebutton_config_xml.rb | 13 +- spec/bigbluebutton_config_xml_spec.rb | 276 ++++++++++++++++++++++++++ 2 files changed, 285 insertions(+), 4 deletions(-) create mode 100644 spec/bigbluebutton_config_xml_spec.rb diff --git a/lib/bigbluebutton_config_xml.rb b/lib/bigbluebutton_config_xml.rb index 44636e1..5db5449 100644 --- a/lib/bigbluebutton_config_xml.rb +++ b/lib/bigbluebutton_config_xml.rb @@ -29,8 +29,12 @@ def initialize(xml) @xml = nil unless xml.nil? opts = { 'ForceArray' => false, 'KeepRoot' => true } - @xml = XmlSimple.xml_in(xml, opts) - @original_xml = Marshal.load(Marshal.dump(@xml)) + begin + @xml = XmlSimple.xml_in(xml, opts) + @original_xml = Marshal.load(Marshal.dump(@xml)) + rescue Exception => e + raise BigBlueButton::BigBlueButtonException.new("Error parsing the config XML. Error: #{e.message}") + end end end @@ -58,10 +62,10 @@ def set_attribute(finder, attr_name, value, is_module=true) if attr tag[attr_name] = value else - false + nil end else - false + nil end end @@ -86,6 +90,7 @@ def find_module(module_name) end end end + nil end def find_tag(name) diff --git a/spec/bigbluebutton_config_xml_spec.rb b/spec/bigbluebutton_config_xml_spec.rb new file mode 100644 index 0000000..e0a0466 --- /dev/null +++ b/spec/bigbluebutton_config_xml_spec.rb @@ -0,0 +1,276 @@ +require 'spec_helper' + +describe BigBlueButton::BigBlueButtonConfigXml do + + let(:default_xml) { # a simplified config.xml file + " + + + + + " + } + + let(:complex_xml) { + "\ + 0.8\ + 4357-2014-02-06\ + \ + \ + + + + + + " + } + + describe "#initialize" do + context "with a valid xml" do + before { + XmlSimple.should_receive(:xml_in) + .with(default_xml, { 'ForceArray' => false, 'KeepRoot' => true }) + .and_return("response") + } + subject { BigBlueButton::BigBlueButtonConfigXml.new(default_xml) } + it("creates and stores a correct internal xml") { subject.xml.should eql("response") } + end + + context "with an empty string as xml" do + it "throws an exception" do + expect { + BigBlueButton::BigBlueButtonConfigXml.new("") + }.to raise_error(BigBlueButton::BigBlueButtonException) + end + end + + context "throws any exception thrown by XmlSimple" do + before { + XmlSimple.should_receive(:xml_in) { raise Exception } + } + it { + expect { + BigBlueButton::BigBlueButtonConfigXml.new(default_xml) + }.to raise_error(BigBlueButton::BigBlueButtonException) + } + end + end + + describe "#get_attribute" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(default_xml) } + + context "searching inside a module" do + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.get_attribute("LayoutModule", "layoutConfig") } + it { should be_nil } + end + + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.get_attribute("LayoutModule", "layoutConfig") } + it { should be_nil } + end + + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.get_attribute("LayoutModule", "layoutConfig") } + it { should be_nil } + end + + context "if the module and attribute are found" do + subject { target.get_attribute("LayoutModule", "layoutConfig") } + it { should eql("http://test-server.org/client/conf/layout.xml") } + end + + context "if the module is not found" do + subject { target.get_attribute("InexistentModule", "layoutConfig") } + it { should be_nil } + end + + context "if the attribute is not found" do + subject { target.get_attribute("LayoutModule", "inexistentAttribute") } + it { should be_nil } + end + + # just to make sure it won't break in a more complete config.xml + context "works with a complex xml" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(complex_xml) } + subject { target.get_attribute("LayoutModule", "layoutConfig") } + it { should eql("http://test-server.org/client/conf/layout.xml") } + end + end + + context "searching outside a module" do + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.get_attribute("help", "url", false) } + it { should be_nil } + end + + context "if the tag and attribute are found" do + subject { target.get_attribute("help", "url", false) } + it { should eql("http://test-server.org/help.html") } + end + + context "if the tag is not found" do + subject { target.get_attribute("inexistent", "url", false) } + it { should be_nil } + end + + context "if the attribute is not found" do + subject { target.get_attribute("help", "inexistent", false) } + it { should be_nil } + end + + # just to make sure it won't break in a more complete config.xml + context "works with a complex xml" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(complex_xml) } + subject { target.get_attribute("help", "url", false) } + it { should eql("http://test-server.org/help.html") } + end + end + end + + describe "#set_attribute" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(default_xml) } + + context "setting an attribute inside a module" do + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.set_attribute("LayoutModule", "layoutConfig", "value") } + it { should be_nil } + end + + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.set_attribute("LayoutModule", "layoutConfig", "value") } + it { should be_nil } + end + + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.set_attribute("LayoutModule", "layoutConfig", "value") } + it { should be_nil } + end + + context "if the module and attribute are found" do + before(:each) { + target.set_attribute("LayoutModule", "layoutConfig", "value").should eql("value") + } + it { target.get_attribute("LayoutModule", "layoutConfig").should eql("value") } + end + + context "if the module is not found" do + subject { target.set_attribute("InexistentModule", "layoutConfig", "value") } + it { should be_nil } + end + + context "if the attribute is not found" do + subject { target.set_attribute("LayoutModule", "inexistentAttribute", "value") } + it { should be_nil } + end + + # just to make sure it won't break in a more complete config.xml + context "works with a complex xml" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(complex_xml) } + before(:each) { + target.set_attribute("LayoutModule", "layoutConfig", "value").should eql("value") + } + it { target.get_attribute("LayoutModule", "layoutConfig").should eql("value") } + end + end + + context "setting an attribute outside of a module" do + context "if the xml has no " do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new("") } + subject { target.set_attribute("help", "url", "value", false) } + it { should be_nil } + end + + context "if the module and attribute are found" do + before(:each) { + target.set_attribute("help", "url", "value", false).should eql("value") + } + it { target.get_attribute("help", "url", false).should eql("value") } + end + + context "if the module is not found" do + subject { target.set_attribute("InexistentModule", "url", "value", false) } + it { should be_nil } + end + + context "if the attribute is not found" do + subject { target.set_attribute("help", "inexistentAttribute", "value", false) } + it { should be_nil } + end + + # just to make sure it won't break in a more complete config.xml + context "works with a complex xml" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(complex_xml) } + before(:each) { + target.set_attribute("help", "url", "value", false).should eql("value") + } + it { target.get_attribute("help", "url", false).should eql("value") } + end + end + + end + + describe "#as_string" do + context "for a simple xml" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(default_xml) } + subject { target.as_string } + it { + # it is the same XML as `default_xml`, just formatted slightly differently + expected = "" + should eql(expected) + } + end + + context "for a complex xml" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(complex_xml) } + subject { target.as_string } + it { + # it is the same XML as `default_xml`, just formatted slightly differently + expected = "0.8" + should eql(expected) + } + end + end + + describe "#is_modified?" do + let(:target) { BigBlueButton::BigBlueButtonConfigXml.new(default_xml) } + + context "before any attribute is set" do + it { target.is_modified?.should be_false } + end + + context "after setting an attribute" do + before(:each) { target.set_attribute("LayoutModule", "layoutConfig", "value") } + it { target.is_modified?.should be_true } + end + + context "if an attribute is set to the same value it already had" do + before(:each) { + value = target.get_attribute("LayoutModule", "layoutConfig") + target.set_attribute("LayoutModule", "layoutConfig", value) + } + it { target.is_modified?.should be_false } + end + end + +end