<commit_before># Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with this
# work for additional information regarding copyright ownership.  The ASF
# licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.


require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helpers'))
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'xpath_matchers'))

describe Buildr::IntellijIdea do

  def invoke_generate_task
    task('idea').invoke
  end

  def invoke_clean_task
    task('idea:clean').invoke
  end

  def root_project_filename(project)
    project._("#{project.name}#{Buildr::IntellijIdea::IdeaFile::DEFAULT_SUFFIX}.ipr")
  end

  def root_project_xml(project)
    xml_document(root_project_filename(project))
  end

  def root_module_filename(project)
    project._("#{project.name}#{Buildr::IntellijIdea::IdeaFile::DEFAULT_SUFFIX}.iml")
  end

  def root_module_xml(project)
    xml_document(root_module_filename(project))
  end

  def subproject_module_filename(project, sub_project_name)
    project._("#{sub_project_name}/#{sub_project_name}#{Buildr::IntellijIdea::IdeaFile::DEFAULT_SUFFIX}.iml")
  end

  def subproject_module_xml(project, sub_project_name)
    xml_document(subproject_module_filename(project, sub_project_name))
  end

  def xml_document(filename)
    File.should be_exist(filename)
    REXML::Document.new(File.read(filename))
  end

  def xpath_to_module
    "/project/component[@name='ProjectModuleManager']/modules/module"
  end

  describe "idea:clean" do
    before do
      write "foo.ipr"
      write "foo.iml"
      write "other.ipr"
      write "other.iml"
      mkdir_p 'bar'
      write "bar/bar.iml"
      write "bar/other.ipr"
      write "bar/other.iml"

      @foo = define "foo" do
        define "bar"
      end
      invoke_clean_task
    end

    it "should remove the ipr file" do
      File.exists?("foo.ipr").should be_false
    end

    it "should remove the project iml file" do
      File.exists?("foo.iml").should be_false
    end

    it "should remove the subproject iml file" do
      File.exists?("foo.iml").should be_false
    end

    it "should not remove other iml and ipr files" do
      File.exists?("other.ipr").should be_true
      File.exists?("other.iml").should be_true
      File.exists?("bar/other.ipr").should be_true
      File.exists?("bar/other.iml").should be_true
    end
  end

  describe "idea task" do

    def order_entry_xpath
      "/module/component[@name='NewModuleRootManager']/orderEntry"
    end

    describe "with a single dependency" do
      describe "of type compile" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one exported 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library', @exported='']/library/CLASSES/root", 1)
        end
      end

      describe "with iml.main_dependencies override" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.main_dependencies << 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one exported 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library', @exported='']/library/CLASSES/root", 1)
        end
      end

      describe "of type test" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            test.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one non-exported  test scope 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library' and @exported]/library/CLASSES/root", 0)
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library' and @scope='TEST']/library/CLASSES/root", 1)
        end
      end

      describe "with iml.test_dependencies override" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.test_dependencies << 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one non-exported 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library' and @exported]/library/CLASSES/root", 0)
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library']/library/CLASSES/root", 1)
        end
      end

      describe "with sources artifact present" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          artifact('group:id:jar:sources:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates 'module-library' orderEntry in IML with SOURCES specified" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library', @exported='']/library/SOURCES/root", 1)
        end
      end

      describe "with local_repository_env_override set to nil" do
        before do
          Buildr.repositories.instance_eval do
            @local = @remote = @release_to = nil
          end
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.local_repository_env_override = nil
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates orderEntry with absolute path for classes jar" do
          root_module_xml(@foo).should match_xpath("#{order_entry_xpath}/library/CLASSES/root/@url",
                                                   "jar://$MODULE_DIR$/home/.m2/repository/group/id/1.0/id-1.0.jar!/")
        end
      end
      describe "with local_repository_env_override set to MAVEN_REPOSITORY" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.local_repository_env_override = 'MAVEN_REPOSITORY'
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates orderEntry with absolute path for classes jar" do
          root_module_xml(@foo).should match_xpath("#{order_entry_xpath}/library/CLASSES/root/@url",
                                                   "jar://$MAVEN_REPOSITORY$/group/id/1.0/id-1.0.jar!/")
        end
      end
    end

    describe "with multiple dependencies" do
      before do
        artifact('group:id:jar:1.0') { |t| write t.to_s }
        artifact('group:id2:jar:1.0') { |t| write t.to_s }
        @foo = define "foo" do
          compile.with 'group:id:jar:1.0', 'group:id2:jar:1.0'
        end
        invoke_generate_task
      end

      it "generates multiple 'module-library' orderEntry in IML" do
        root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library']", 2)
      end
    end

    describe "with a single non artifact dependency" do
      before do
        @foo = define "foo" do
          filename = _("foo-dep.jar")
          File.open(filename, "wb") { |t| write "Hello" }
          compile.with filename
        end
        invoke_generate_task
      end

      it "generates one exported 'module-library' orderEntry in IML" do
        root_module_xml(@foo).should match_xpath("#{order_entry_xpath}/library/CLASSES/root/@url",
                                                 "jar://$MODULE_DIR$/foo-dep.jar!/")
      end
    end

    describe "with extra_modules specified" do
      before do
        @foo = define "foo" do
          ipr.extra_modules << 'other.iml'
          ipr.extra_modules << 'other_other.iml'
        end
        invoke_generate_task
      end

      it "generate an IPR with extra modules specified" do
        doc = xml_document(@foo._("foo.ipr"))
        doc.should have_nodes("#{xpath_to_module}", 3)
        module_ref = "$PROJECT_DIR$/foo.iml"
        doc.should have_xpath("#{xpath_to_module}[@fileurl='file://#{module_ref}', @filepath='#{module_ref}']")
        module_ref = "$PROJECT_DIR$/other.iml"
        doc.should have_xpath("#{xpath_to_module}[@fileurl='file://#{module_ref}', @filepath='#{module_ref}']")
        module_ref = "$PROJECT_DIR$/other_other.iml"
        doc.should have_xpath("#{xpath_to_module}[@fileurl='file://#{module_ref}', @filepath='#{module_ref}']")
      end
    end

    describe "with web and webservice facet added to root project" do
      before do
        @foo = define "foo" do
          iml.add_facet("Web", "web") do |facet|
            facet.configuration do |conf|
              conf.descriptors do |desc|
                desc.deploymentDescriptor :name => 'web.xml',
                                          :url => "file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml",
                                          :optional => "false", :version => "2.4"
              end
              conf.webroots do |webroots|
                webroots.root :url => "file://$MODULE_DIR$/src/main/webapp", :relative => "/"
              end
            end
          end
          iml.add_facet("WebServices Client", "WebServicesClient") do |facet|
            facet.configuration "ws.engine" => "Glassfish / JAX-WS 2.X RI / Metro 1.X / JWSDP 2.0"
          end
          define 'bar'
        end
        invoke_generate_task
      end

      it "generates an IML for root project with a web and webservice facet" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 2)
        doc.should have_xpath("#{facet_xpath}[@type='web', @name='Web']")
        doc.should have_xpath("#{facet_xpath}[@type='WebServicesClient', @name='WebServices Client']")
      end
    end

    describe "using add_web_facet" do
      before do
        write "src/main/webapp/WEB-INF/web.xml"
        write "src/main/webapp/WEB-INF/glassfish-web.xml"
        write "src/main/webapp/WEB-INF/context.xml"
        write "src/main/webapp/WEB-INF/faces-config.xml"

        @foo = define "foo" do
          iml.add_web_facet
        end
        invoke_generate_task
      end

      it "generates a web facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='web', @name='Web']"
        doc.should have_xpath(web_facet_xpath)
        deployment_descriptor_xpath = "#{web_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='web.xml',  url='file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml']")
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='glassfish-web.xml',  url='file://$MODULE_DIR$/src/main/webapp/WEB-INF/glassfish-web.xml']")
      end

      it "generates a web facet with derived webroots" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='web', @name='Web']"
        doc.should have_xpath(web_facet_xpath)
        doc.should have_xpath("#{web_facet_xpath}/configuration/webroots/root[@url='file://$MODULE_DIR$/src/main/webapp', @realtive='/']")
      end

      it "generates a web facet with jsf facet auto-detected" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='web', @name='Web']"
        doc.should have_xpath(web_facet_xpath)
        doc.should have_xpath("#{web_facet_xpath}/facet[@type='jsf', @name='JSF']")
      end
    end

    describe "using add_web_facet with specified parameters" do
      before do
        @foo = define "foo" do
          iml.add_web_facet(:deployment_descriptors => ["src/main/webapp2/WEB-INF/web.xml"],
                            :enable_jsf => true,
                            :webroots => {"src/main/webapp2" => "/", "src/main/css" => "/css"})
        end
        invoke_generate_task
      end

      it "generates a web facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='web', @name='Web']"
        doc.should have_xpath(web_facet_xpath)
        deployment_descriptor_xpath = "#{web_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='web.xml',  url='file://$MODULE_DIR$/src/main/webapp2/WEB-INF/web.xml']")
      end

      it "generates a web facet with specified webroots" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='web', @name='Web']"
        doc.should have_xpath(web_facet_xpath)
        doc.should have_xpath("#{web_facet_xpath}/configuration/webroots/root[@url='file://$MODULE_DIR$/src/main/webapp2', @realtive='/']")
        doc.should have_xpath("#{web_facet_xpath}/configuration/webroots/root[@url='file://$MODULE_DIR$/src/main/css', @realtive='/css']")
      end

      it "generates a web facet with jsf facet enabled" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='web', @name='Web']"
        doc.should have_xpath(web_facet_xpath)
        doc.should have_xpath("#{web_facet_xpath}/facet[@type='jsf', @name='JSF']")
      end
    end

    describe "using add_ejb_facet" do
      before do
        write "src/main/java/com/bin/foo.java"
        write "src/main/resources/WEB-INF/ejb-jar.xml"

        @foo = define "foo" do
          iml.add_ejb_facet
        end
        invoke_generate_task
      end

      it "generates an ejb facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='ejb', @name='EJB']"
        doc.should have_xpath(web_facet_xpath)
        deployment_descriptor_xpath = "#{web_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='ejb-jar.xml',  url='file://$MODULE_DIR$/src/main/resources/WEB-INF/ejb-jar.xml']")
      end

      it "generates an ejb facet with derived ejbRoots" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='ejb', @name='EJB']"
        doc.should have_xpath(web_facet_xpath)
        doc.should have_xpath("#{web_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/src/main/java']")
        doc.should have_xpath("#{web_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/src/main/resources']")
      end
    end


    describe "using add_ejb_facet specifying parameters" do
      before do
        @foo = define "foo" do
          iml.add_ejb_facet(:ejb_roots => ["generated/main/java","generated/main/resources"],
          :deployment_descriptors => ["generated/main/resources/WEB-INF/ejb-jar.xml"])
        end
        invoke_generate_task
      end

      it "generates an ejb facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='ejb', @name='EJB']"
        doc.should have_xpath(web_facet_xpath)
        deployment_descriptor_xpath = "#{web_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='ejb-jar.xml',  url='file://$MODULE_DIR$/generated/main/resources/WEB-INF/ejb-jar.xml']")
      end

      it "generates an ejb facet with derived ejbRoots" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        web_facet_xpath = "#{facet_xpath}[@type='ejb', @name='EJB']"
        doc.should have_xpath(web_facet_xpath)
        doc.should have_xpath("#{web_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/generated/main/java']")
        doc.should have_xpath("#{web_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/generated/main/resources']")
      end
    end

    describe "using add_jruby_facet" do
      before do

        @foo = define "foo" do
          iml.add_jruby_facet
        end
        invoke_generate_task
      end

      it "generates a jruby facet with appropriate sdk" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        jruby_facet_xpath = "#{facet_xpath}[@type='JRUBY', @name='JRuby']"
        doc.should have_xpath(jruby_facet_xpath)
        doc.should have_xpath("#{jruby_facet_xpath}/configuration/JRUBY_FACET_CONFIG_ID[@NAME='JRUBY_SDK_NAME', VALUE='jruby-1.6.7.2']")
      end

      it "generates a jruby facet with appropriate paths" do
        doc = xml_document(@foo._("foo.iml"))
        prefix = "/module/component[@name='FacetManager']/facet[@type='JRUBY', @name='JRuby']/configuration"
        doc.should have_xpath("#{prefix}/LOAD_PATH[@number='0']")
        doc.should have_xpath("#{prefix}/I18N_FOLDERS[@number='0']")
      end
    end

    describe "using add_jruby_facet with .ruby-version specified" do
      before do

        write ".ruby-version", "jruby-1.7.2"

        @foo = define "foo" do
          iml.add_jruby_facet
        end
        invoke_generate_task
      end

      it "generates a jruby facet with appropriate sdk" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = "/module/component[@name='FacetManager']/facet"
        doc.should have_nodes(facet_xpath, 1)
        jruby_facet_xpath = "#{facet_xpath}[@type='JRUBY', @name='JRuby']"
        doc.should have_xpath(jruby_facet_xpath)
        doc.should have_xpath("#{jruby_facet_xpath}/configuration/JRUBY_FACET_CONFIG_ID[@NAME='JRUBY_SDK_NAME', VALUE='rbenv: jruby-1.7.2']")
      end

      it "generates a jruby facet with appropriate paths" do
        doc = xml_document(@foo._("foo.iml"))
        prefix = "/module/component[@name='FacetManager']/facet[@type='JRUBY', @name='JRuby']/configuration"
        doc.should have_xpath("#{prefix}/LOAD_PATH[@number='0']")
        doc.should have_xpath("#{prefix}/I18N_FOLDERS[@number='0']")
      end
    end<commit_msg>Add some more tests for the idea project generation<commit_after># Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with this
# work for additional information regarding copyright ownership.  The ASF
# licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.


require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helpers'))
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'xpath_matchers'))

def ensure_facet_xpath(doc, type, name)
  facet_xpath = "/module/component[@name='FacetManager']/facet"
  doc.should have_xpath(facet_xpath)
  web_facet_xpath = "#{facet_xpath}[@type='#{type}', @name='#{name}']"
  doc.should have_xpath(web_facet_xpath)
  web_facet_xpath
end

describe Buildr::IntellijIdea do

  def invoke_generate_task
    task('idea').invoke
  end

  def invoke_clean_task
    task('idea:clean').invoke
  end

  def root_project_filename(project)
    project._("#{project.name}#{Buildr::IntellijIdea::IdeaFile::DEFAULT_SUFFIX}.ipr")
  end

  def root_project_xml(project)
    xml_document(root_project_filename(project))
  end

  def root_module_filename(project)
    project._("#{project.name}#{Buildr::IntellijIdea::IdeaFile::DEFAULT_SUFFIX}.iml")
  end

  def root_module_xml(project)
    xml_document(root_module_filename(project))
  end

  def subproject_module_filename(project, sub_project_name)
    project._("#{sub_project_name}/#{sub_project_name}#{Buildr::IntellijIdea::IdeaFile::DEFAULT_SUFFIX}.iml")
  end

  def subproject_module_xml(project, sub_project_name)
    xml_document(subproject_module_filename(project, sub_project_name))
  end

  def xml_document(filename)
    File.should be_exist(filename)
    REXML::Document.new(File.read(filename))
  end

  def xpath_to_module
    "/project/component[@name='ProjectModuleManager']/modules/module"
  end

  describe "idea:clean" do
    before do
      write "foo.ipr"
      write "foo.iml"
      write "other.ipr"
      write "other.iml"
      mkdir_p 'bar'
      write "bar/bar.iml"
      write "bar/other.ipr"
      write "bar/other.iml"

      @foo = define "foo" do
        define "bar"
      end
      invoke_clean_task
    end

    it "should remove the ipr file" do
      File.exists?("foo.ipr").should be_false
    end

    it "should remove the project iml file" do
      File.exists?("foo.iml").should be_false
    end

    it "should remove the subproject iml file" do
      File.exists?("foo.iml").should be_false
    end

    it "should not remove other iml and ipr files" do
      File.exists?("other.ipr").should be_true
      File.exists?("other.iml").should be_true
      File.exists?("bar/other.ipr").should be_true
      File.exists?("bar/other.iml").should be_true
    end
  end

  describe "idea task" do

    def order_entry_xpath
      "/module/component[@name='NewModuleRootManager']/orderEntry"
    end

    describe "with a single dependency" do
      describe "of type compile" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one exported 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library', @exported='']/library/CLASSES/root", 1)
        end
      end

      describe "with iml.main_dependencies override" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.main_dependencies << 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one exported 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library', @exported='']/library/CLASSES/root", 1)
        end
      end

      describe "of type test" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            test.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one non-exported  test scope 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library' and @exported]/library/CLASSES/root", 0)
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library' and @scope='TEST']/library/CLASSES/root", 1)
        end
      end

      describe "with iml.test_dependencies override" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.test_dependencies << 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates one non-exported 'module-library' orderEntry in IML" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library' and @exported]/library/CLASSES/root", 0)
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library']/library/CLASSES/root", 1)
        end
      end

      describe "with sources artifact present" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          artifact('group:id:jar:sources:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates 'module-library' orderEntry in IML with SOURCES specified" do
          root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library', @exported='']/library/SOURCES/root", 1)
        end
      end

      describe "with local_repository_env_override set to nil" do
        before do
          Buildr.repositories.instance_eval do
            @local = @remote = @release_to = nil
          end
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.local_repository_env_override = nil
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates orderEntry with absolute path for classes jar" do
          root_module_xml(@foo).should match_xpath("#{order_entry_xpath}/library/CLASSES/root/@url",
                                                   "jar://$MODULE_DIR$/home/.m2/repository/group/id/1.0/id-1.0.jar!/")
        end
      end
      describe "with local_repository_env_override set to MAVEN_REPOSITORY" do
        before do
          artifact('group:id:jar:1.0') { |t| write t.to_s }
          @foo = define "foo" do
            iml.local_repository_env_override = 'MAVEN_REPOSITORY'
            compile.with 'group:id:jar:1.0'
          end
          invoke_generate_task
        end

        it "generates orderEntry with absolute path for classes jar" do
          root_module_xml(@foo).should match_xpath("#{order_entry_xpath}/library/CLASSES/root/@url",
                                                   "jar://$MAVEN_REPOSITORY$/group/id/1.0/id-1.0.jar!/")
        end
      end
    end

    describe "with multiple dependencies" do
      before do
        artifact('group:id:jar:1.0') { |t| write t.to_s }
        artifact('group:id2:jar:1.0') { |t| write t.to_s }
        @foo = define "foo" do
          compile.with 'group:id:jar:1.0', 'group:id2:jar:1.0'
        end
        invoke_generate_task
      end

      it "generates multiple 'module-library' orderEntry in IML" do
        root_module_xml(@foo).should have_nodes("#{order_entry_xpath}[@type='module-library']", 2)
      end
    end

    describe "with a single non artifact dependency" do
      before do
        @foo = define "foo" do
          filename = _("foo-dep.jar")
          File.open(filename, "wb") { |t| write "Hello" }
          compile.with filename
        end
        invoke_generate_task
      end

      it "generates one exported 'module-library' orderEntry in IML" do
        root_module_xml(@foo).should match_xpath("#{order_entry_xpath}/library/CLASSES/root/@url",
                                                 "jar://$MODULE_DIR$/foo-dep.jar!/")
      end
    end

    describe "with extra_modules specified" do
      before do
        @foo = define "foo" do
          ipr.extra_modules << 'other.iml'
          ipr.extra_modules << 'other_other.iml'
        end
        invoke_generate_task
      end

      it "generate an IPR with extra modules specified" do
        doc = xml_document(@foo._("foo.ipr"))
        doc.should have_nodes("#{xpath_to_module}", 3)
        module_ref = "$PROJECT_DIR$/foo.iml"
        doc.should have_xpath("#{xpath_to_module}[@fileurl='file://#{module_ref}', @filepath='#{module_ref}']")
        module_ref = "$PROJECT_DIR$/other.iml"
        doc.should have_xpath("#{xpath_to_module}[@fileurl='file://#{module_ref}', @filepath='#{module_ref}']")
        module_ref = "$PROJECT_DIR$/other_other.iml"
        doc.should have_xpath("#{xpath_to_module}[@fileurl='file://#{module_ref}', @filepath='#{module_ref}']")
      end
    end

    describe "with web and webservice facet added to root project" do
      before do
        @foo = define "foo" do
          iml.add_facet("Web", "web") do |facet|
            facet.configuration do |conf|
              conf.descriptors do |desc|
                desc.deploymentDescriptor :name => 'web.xml',
                                          :url => "file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml",
                                          :optional => "false", :version => "2.4"
              end
              conf.webroots do |webroots|
                webroots.root :url => "file://$MODULE_DIR$/src/main/webapp", :relative => "/"
              end
            end
          end
          iml.add_facet("WebServices Client", "WebServicesClient") do |facet|
            facet.configuration "ws.engine" => "Glassfish / JAX-WS 2.X RI / Metro 1.X / JWSDP 2.0"
          end
          define 'bar'
        end
        invoke_generate_task
      end

      it "generates an IML for root project with a web and webservice facet" do
        doc = xml_document(@foo._("foo.iml"))
        ensure_facet_xpath(doc, 'web', 'Web')
        ensure_facet_xpath(doc, 'WebServicesClient', 'WebServices Client')
      end
    end

    describe "using add_gwt_facet" do
      before do
        @foo = define "foo" do
          iml.add_gwt_facet("com.biz.MyModule" => true, "com.biz.MyOtherModule" => false)
        end
        invoke_generate_task
      end

      it "generates a gwt facet with default settings" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = ensure_facet_xpath(doc, 'gwt', 'GWT')
        setting_xpath = "#{facet_xpath}/configuration/setting"
        doc.should have_xpath("#{setting_xpath}[@name='gwtSdkUrl', value='file://$GWT_TOOLS$']")
        doc.should have_xpath("#{setting_xpath}[@name='gwtScriptOutputStyle', value='PRETTY']")
        doc.should have_xpath("#{setting_xpath}[@name='compilerParameters', value='-draftCompile -localWorkers 2 -strict']")
        doc.should have_xpath("#{setting_xpath}[@name='compilerParameters', value='-draftCompile -localWorkers 2 -strict']")
        doc.should have_xpath("#{setting_xpath}[@name='compilerMaxHeapSize', value='512']")
        doc.should have_xpath("#{setting_xpath}[@name='webFacet', value='Web']")
      end

      it "generates a gwt facet with specified modules" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = ensure_facet_xpath(doc, 'gwt', 'GWT')
        prefix = "#{facet_xpath}/configuration/packaging/module"
        doc.should have_xpath("#{prefix}[@name='com.biz.MyModule', @enabled='true']")
        doc.should have_xpath("#{prefix}[@name='com.biz.MyOtherModule', @enabled='false']")
      end
    end

    describe "using add_gwt_facet that detects gwt sdk" do
      before do
        @foo = define "foo" do
          compile.with 'com.google.gwt:gwt-dev:jar:2.5.1'
          iml.add_gwt_facet("com.biz.MyModule" => true)
        end
        invoke_generate_task
      end

      it "generates a gwt facet with detected gwt sdk settings" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = ensure_facet_xpath(doc, 'gwt', 'GWT')
        setting_xpath = "#{facet_xpath}/configuration/setting"
        doc.should have_xpath("#{setting_xpath}[@name='gwtSdkType', value='maven']")
        doc.should have_xpath("#{setting_xpath}[@name='gwtSdkUrl', value='$MAVEN_REPOSITORY$/com/google/gwt/gwt-dev/2.5.1']")
      end
    end

    describe "using add_gwt_facet that specifies gwt sdk" do
      before do
        artifact('com.example:library:jar:2.0') { |task| write task.name }
        @foo = define "foo" do
          iml.add_gwt_facet({"com.biz.MyModule" => true},:gwt_dev_artifact => 'com.example:library:jar:2.0')
        end
        invoke_generate_task
      end

      it "generates a gwt facet with detected gwt sdk settings" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = ensure_facet_xpath(doc, 'gwt', 'GWT')
        setting_xpath = "#{facet_xpath}/configuration/setting"
        doc.should have_xpath("#{setting_xpath}[@name='gwtSdkType', value='maven']")
        doc.should have_xpath("#{setting_xpath}[@name='gwtSdkUrl', value='$MAVEN_REPOSITORY$/com/google/gwt/gwt-dev/2.5.1']")
      end
    end

    describe "using add_gwt_facet that specifies settings" do
      before do
        @foo = define "foo" do
          iml.add_gwt_facet({"com.biz.MyModule" => true, "com.biz.MyOtherModule" => false},
                            :settings => {:gwtScriptOutputStyle => 'OTHER', :compilerMaxHeapSize => 1024, :zang => 'zang'})
        end
        invoke_generate_task
      end

      it "generates a gwt facet with specified settings" do
        doc = xml_document(@foo._("foo.iml"))
        facet_xpath = ensure_facet_xpath(doc, 'gwt', 'GWT')
        setting_xpath = "#{facet_xpath}/configuration/setting"
        doc.should have_xpath("#{setting_xpath}[@name='zang', value='zang']")
        doc.should have_xpath("#{setting_xpath}[@name='gwtScriptOutputStyle', value='OTHER']")
        doc.should have_xpath("#{setting_xpath}[@name='compilerMaxHeapSize', value='1024']")
      end
    end

    describe "using add_web_facet" do
      before do
        write "src/main/webapp/WEB-INF/web.xml"
        write "src/main/webapp/WEB-INF/glassfish-web.xml"
        write "src/main/webapp/WEB-INF/context.xml"
        write "src/main/webapp/WEB-INF/faces-config.xml"

        @foo = define "foo" do
          iml.add_web_facet
        end
        invoke_generate_task
      end

      it "generates a web facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        web_facet_xpath = ensure_facet_xpath(doc, 'web', 'Web')
        deployment_descriptor_xpath = "#{web_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='web.xml',  url='file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml']")
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='glassfish-web.xml',  url='file://$MODULE_DIR$/src/main/webapp/WEB-INF/glassfish-web.xml']")
      end

      it "generates a web facet with derived webroots" do
        doc = xml_document(@foo._("foo.iml"))
        web_facet_xpath = ensure_facet_xpath(doc, 'web', 'Web')
        doc.should have_xpath("#{web_facet_xpath}/configuration/webroots/root[@url='file://$MODULE_DIR$/src/main/webapp', @realtive='/']")
      end

      it "generates a web facet with jsf facet auto-detected" do
        doc = xml_document(@foo._("foo.iml"))
        web_facet_xpath = ensure_facet_xpath(doc, 'web', 'Web')
        doc.should have_xpath("#{web_facet_xpath}/facet[@type='jsf', @name='JSF']")
      end
    end

    describe "using add_web_facet with specified parameters" do
      before do
        @foo = define "foo" do
          iml.add_web_facet(:deployment_descriptors => ["src/main/webapp2/WEB-INF/web.xml"],
                            :enable_jsf => true,
                            :webroots => {"src/main/webapp2" => "/", "src/main/css" => "/css"})
        end
        invoke_generate_task
      end

      it "generates a web facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        web_facet_xpath = ensure_facet_xpath(doc, 'web', 'Web')
        deployment_descriptor_xpath = "#{web_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='web.xml',  url='file://$MODULE_DIR$/src/main/webapp2/WEB-INF/web.xml']")
      end

      it "generates a web facet with specified webroots" do
        doc = xml_document(@foo._("foo.iml"))
        web_facet_xpath = ensure_facet_xpath(doc, 'web', 'Web')
        doc.should have_xpath("#{web_facet_xpath}/configuration/webroots/root[@url='file://$MODULE_DIR$/src/main/webapp2', @realtive='/']")
        doc.should have_xpath("#{web_facet_xpath}/configuration/webroots/root[@url='file://$MODULE_DIR$/src/main/css', @realtive='/css']")
      end

      it "generates a web facet with jsf facet enabled" do
        doc = xml_document(@foo._("foo.iml"))
        web_facet_xpath = ensure_facet_xpath(doc, 'web', 'Web')
        doc.should have_xpath("#{web_facet_xpath}/facet[@type='jsf', @name='JSF']")
      end
    end

    describe "using add_ejb_facet" do
      before do
        write "src/main/java/com/bin/foo.java"
        write "src/main/resources/WEB-INF/ejb-jar.xml"

        @foo = define "foo" do
          iml.add_ejb_facet
        end
        invoke_generate_task
      end

      it "generates an ejb facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        ejb_facet_xpath = ensure_facet_xpath(doc, 'ejb', 'EJB')
        deployment_descriptor_xpath = "#{ejb_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='ejb-jar.xml',  url='file://$MODULE_DIR$/src/main/resources/WEB-INF/ejb-jar.xml']")
      end

      it "generates an ejb facet with derived ejbRoots" do
        doc = xml_document(@foo._("foo.iml"))
        ejb_facet_xpath = ensure_facet_xpath(doc, 'ejb', 'EJB')
        doc.should have_xpath("#{ejb_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/src/main/java']")
        doc.should have_xpath("#{ejb_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/src/main/resources']")
      end
    end

    describe "using add_ejb_facet specifying parameters" do
      before do
        @foo = define "foo" do
          iml.add_ejb_facet(:ejb_roots => ["generated/main/java","generated/main/resources"],
          :deployment_descriptors => ["generated/main/resources/WEB-INF/ejb-jar.xml"])
        end
        invoke_generate_task
      end

      it "generates an ejb facet with appropriate deployment descriptors" do
        doc = xml_document(@foo._("foo.iml"))
        ejb_facet_xpath = ensure_facet_xpath(doc, 'ejb', 'EJB')
        deployment_descriptor_xpath = "#{ejb_facet_xpath}/configuration/descriptors/deploymentDescriptor"
        doc.should have_xpath("#{deployment_descriptor_xpath}[@name='ejb-jar.xml',  url='file://$MODULE_DIR$/generated/main/resources/WEB-INF/ejb-jar.xml']")
      end

      it "generates an ejb facet with derived ejbRoots" do
        doc = xml_document(@foo._("foo.iml"))
        ejb_facet_xpath = ensure_facet_xpath(doc, 'ejb', 'EJB')
        doc.should have_xpath("#{ejb_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/generated/main/java']")
        doc.should have_xpath("#{ejb_facet_xpath}/configuration/ejbRoots/root[@url='file://$MODULE_DIR$/generated/main/resources']")
      end
    end

    describe "using add_jruby_facet" do
      before do

        @foo = define "foo" do
          iml.add_jruby_facet
        end
        invoke_generate_task
      end

      it "generates a jruby facet with appropriate sdk" do
        doc = xml_document(@foo._("foo.iml"))
        jruby_facet_xpath = ensure_facet_xpath(doc, 'JRUBY', 'JRuby')
        doc.should have_xpath("#{jruby_facet_xpath}/configuration/JRUBY_FACET_CONFIG_ID[@NAME='JRUBY_SDK_NAME', VALUE='jruby-1.6.7.2']")
      end

      it "generates a jruby facet with appropriate paths" do
        doc = xml_document(@foo._("foo.iml"))
        jruby_facet_xpath = ensure_facet_xpath(doc, 'JRUBY', 'JRuby')
        prefix = "#{jruby_facet_xpath}/configuration"
        doc.should have_xpath("#{prefix}/LOAD_PATH[@number='0']")
        doc.should have_xpath("#{prefix}/I18N_FOLDERS[@number='0']")
      end
    end

    describe "using add_jruby_facet with .ruby-version specified" do
      before do

        write ".ruby-version", "jruby-1.7.2"

        @foo = define "foo" do
          iml.add_jruby_facet
        end
        invoke_generate_task
      end

      it "generates a jruby facet with appropriate sdk" do
        doc = xml_document(@foo._("foo.iml"))
        jruby_facet_xpath = ensure_facet_xpath(doc, 'JRUBY', 'JRuby')
        doc.should have_xpath("#{jruby_facet_xpath}/configuration/JRUBY_FACET_CONFIG_ID[@NAME='JRUBY_SDK_NAME', VALUE='rbenv: jruby-1.7.2']")
      end

      it "generates a jruby facet with appropriate paths" do
        doc = xml_document(@foo._("foo.iml"))
        jruby_facet_xpath = ensure_facet_xpath(doc, 'JRUBY', 'JRuby')
        prefix = "#{jruby_facet_xpath}/configuration"
        doc.should have_xpath("#{prefix}/LOAD_PATH[@number='0']")
        doc.should have_xpath("#{prefix}/I18N_FOLDERS[@number='0']")
      end
    end
{'fraction_non_alphanumeric': 0.12993831520459576, 'fraction_numerical': 0.005156095493713801, 'mean_word_length': 2.7995348421146793, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 111}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5862114', 'n_tokens_mistral': 14314, 'n_tokens_neox': 13801, 'n_words': 2691}