# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
# License:   Apache License, Version 2.0

require File.dirname(__FILE__) + '/spec_helper'

describe RTM do
  # describe "#[]" do
  #   it "should be empty if no TopicMapSystems are created" do
  #     RTM[].should be_empty
  #   end   Fails since already reated tms are found
  # end
  describe "#connect" do
    it "should have a TMAPI engine as default backend" do
      tms_tmapi = RTM.connect(:implementation => implementation_for_spec)
      tms_tmapi.should be_a_kind_of RTM::TopicMapSystem
      tms_tmapi.should be_a_kind_of RTM::JavaTMAPI
      tms_tmapi.should be_a_kind_of org.tmapi.core.TopicMapSystem
    end
    it "should allow multiple backends of the same kind in case of TMAPI" do
      if RTM[implementation_for_spec]
        number_of_already_existing_connections = RTM[implementation_for_spec].length
      else
        number_of_already_existing_connections = 0
      end
      tms1 = RTM.connect(:implementation => implementation_for_spec)
      tms2 = RTM.connect(:implementation => implementation_for_spec)
      tms1.should_not == tms2
      number_of_already_existing_connections.should == RTM[implementation_for_spec].length - 2
    end
    # if ar_available?
    #   it "should allow only one classic RTM Active Record connection" do
    #     pending "extract active record specs to be run only by activerecord"
    #     false.should be true # early exit
    #     tmsar1 = RTM.connect :implementation => :activerecord
    #     RTM[:activerecord].size.should == 1
    #     tmsar2 = RTM.connect :implementation => :activerecord
    #     pending
    #     RTM[:activerecord].size.should == 1
    #     tmsar2.should == tmsar1
    #     tmsar1.should be_a_kind_of RTM::TopicMapSystem
    #     tmsar1.should_not be_a_kind_of RTM::JavaTMAPI
    #     tmsar1.should_not be_a_kind_of org.tmapi.core.TopicMapSystem
    #   end
    # end
  end

  describe "#connections" do
    it "should give back a Hash containing all connections if some exist" do
      connections = RTM.connections
      connections.should be_a_kind_of Hash
      #TODO how to test this?
    end
  end

  describe "#[]" do
    # TODO
  end

end

# if ar_available?
#   describe RTM::AR do
#     require File.dirname(__FILE__) + '/../test/base_unit_test.rb'
#     require 'test/unit/ui/console/testrunner'
#     
#     it "should run the UNIT base_test without errors" do
#       test_connect = BaseTest.new "test_connect"
#       res1 = Test::Unit::UI::Console::TestRunner.run test_connect
#       res1.error_count.should == 0
#       res1.failure_count.should == 0
#       test_sugar_hash_access = BaseTest.new "test_sugar_hash_access"
#       res2 = Test::Unit::UI::Console::TestRunner.run test_sugar_hash_access
#       res2.error_count.should == 0
#       res2.failure_count.should == 0
#     end
#   end
# end

# describe "TMAPI with old base_test" do
#   require "spec/old_base_unit_test_tmapi.rb"
#   require 'test/unit/ui/console/testrunner'
#   it "should run the UNIT base_test without errors for the TMAPI, too" do
#     pending "first is not supported for Java::GnuTrove::THashSet" do
#       test_connect = BaseTest.new "test_connect"
#       res1 = Test::Unit::UI::Console::TestRunner.run test_connect
#       res1.error_count.should == 0
#       res1.failure_count.should == 0
#       test_sugar_hash_access = BaseTest.new "test_sugar_hash_access"
#       res2 = Test::Unit::UI::Console::TestRunner.run test_sugar_hash_access
#       res2.error_count.should == 0
#       res2.failure_count.should == 0
#     end
#   end
# end