#!/usr/bin/env ruby
# $Id: tc_site.rb,v 1.24 2004/05/10 09:48:34 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

require 'test_common'

class TC_Farm < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @farm = Qwik::Farm.new(@config)
    @dir = @config.data_dir.path+"test"
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end

  def test_farm
    assert_equal('qwik.jp', @config.default_hostname)
    assert_equal(true, @farm.exist?('www'))
    assert_instance_of(Qwik::Site, @farm.get_site('www'))
    assert_equal(false, @farm.exist?('test'))
    assert_equal(nil, @farm.get_site('test'))
    @dir.mkdir
    assert_equal(true, @farm.exist?('test'))
    assert_instance_of(Qwik::Site, @farm.get_site('test'))
  end

  def test_make_site
    assert_equal(false, @farm.exist?('test'))
    assert_equal(nil, @farm.get_site('test'))
    @farm.make_site('test')
    assert_equal(true, @farm.exist?('test'))
    assert_instance_of(Qwik::Site, @farm.get_site('test'))
    assert_raise(RuntimeError){ @farm.make_site('test') }
  end

  def teardown
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end
end

class TC_Site < Test::Unit::TestCase
  include TestSite

  def test_site
    with_dir('www'){ # test_url_www
      site = Qwik::Site.new(@config, 'www')
      assert_equal('http://qwik.jp/', site.domainurl)
      assert_equal('http://qwik.jp/', site.url)
      assert_equal('www@qwik.jp', site.ml)
      assert_equal('qwik.jp', site.title)
    }

    with_dir('example.com'){ # test_url_with_dot
      site = Qwik::Site.new(@config, 'example.com')
      assert_equal('http://example.com/', site.domainurl)
      assert_equal('http://example.com/', site.url)
      assert_equal('info@example.com', site.ml)
      assert_equal('example.com', site.title)
    }
  end

  def with_dir(sitename)
    dir = ('./'+sitename+'/').path
    dir.rmtree if dir.directory?
    dir.rmdir  if dir.directory?
    dir.mkdir if !dir.directory?
    yield
    dir.rmtree if dir.directory?
    dir.rmdir  if dir.directory?
  end
end
