<commit_before>//

import java.net.URL
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.matchers._
import org.scalatest._
import org.codehaus.cargo.container.InstalledLocalContainer
import org.codehaus.cargo.container.installer.ZipURLInstaller
import org.codehaus.cargo.generic.configuration.DefaultConfigurationFactory
import org.codehaus.cargo.container.ContainerType
import org.codehaus.cargo.container.configuration.ConfigurationType
import org.codehaus.cargo.generic.DefaultContainerFactory
import org.codehaus.cargo.container.configuration.LocalConfiguration
import com.gargoylesoftware.htmlunit._
import com.gargoylesoftware.htmlunit.html._
import com.gargoylesoftware.htmlunit.util._
import org.codehaus.cargo.container.deployable.WAR
import org.codehaus.cargo.container.property._
import org.codehaus.cargo.util.log._
import scala.collection.immutable.{ Page => _, _ }
import scala.collection.JavaConverters._
import org.apache.commons.io.FileUtils
import java.io.File
import java.lang.Thread

trait ServletContainer {

  protected val WAR_KEY = "war.servlet"
  
  def keyServletContainer: String
  
  def keyWarPath: String = WAR_KEY + keyServletContainer
  
}

trait Servlet30Container extends ServletContainer {
    def keyServletContainer = "30"
}

trait Servlet25Container extends ServletContainer {
    def keyServletContainer = "25"
}

trait CargoContainerManager extends BeforeAndAfterAll {
  self: Suite =>

  def getContainer: InstalledLocalContainer

  def setContainer(container: InstalledLocalContainer): Unit

  def containerUrl: String

  def containerName: String

  def context = "/"
  
  def keyWarPath: String

  abstract override def beforeAll(configMap: Map[String, Any]) {

    val warPath = configMap.get(keyWarPath).getOrElse(Nil)

    println("WAR file to deploy: " + warPath)

    println("Download container " + containerName + " from " + containerUrl + " ...")
    val installer = new ZipURLInstaller(new URL(containerUrl))
    println("Download container done")

    println("Install container ...")
    installer.install
    println("Install container done")

    val configuration: LocalConfiguration = new DefaultConfigurationFactory().createConfiguration(
      containerName, ContainerType.INSTALLED, ConfigurationType.STANDALONE).asInstanceOf[LocalConfiguration]

    configuration.setProperty(GeneralPropertySet.LOGGING, LoggingLevel.MEDIUM.getLevel);

    val container =
      new DefaultContainerFactory().createContainer(
        containerName, ContainerType.INSTALLED, configuration).asInstanceOf[InstalledLocalContainer]

    println("Configure container")
    container.setHome(installer.getHome)
    container.setLogger(new SimpleLogger)

    val war = new WAR(warPath.toString)
    war.setContext(context)
    configuration.addDeployable(war)

    println("Start the container " + containerName)
    setContainer(container)
    container.start
  }

  abstract override def afterAll {
    println("Stop the container")
    Some(getContainer).map {
      _.stop
    }
  }
}

object AbstractPlay2WarTests {

  private val ROOT_URL = "http://localhost:8080"

}

abstract class AbstractPlay2WarTests extends FeatureSpec with GivenWhenThen with ShouldMatchers with CargoContainerManager with BeforeAndAfter {

  import AbstractPlay2WarTests._

  var webClient: WebClient = null

  var container: InstalledLocalContainer = null

  def getContainer = container

  def setContainer(container: InstalledLocalContainer) = this.container = container

  def containerUrl = ""

  def containerName = ""

  before {
    webClient = new WebClient
    webClient.setJavaScriptEnabled(false)
    webClient.setThrowExceptionOnFailingStatusCode(false)
    webClient.getCookieManager.setCookiesEnabled(true)
    new SkipClockiFrameWrapper(webClient)
  }

  after {
    webClient.closeAllWindows
  }

  def sendRequest(pageUrl: String, method: String = "GET", parameters: Map[String, String] = Map.empty, howManyTimes: Int = 1): Option[Page] = {

    val strictMethod = HttpMethod.valueOf(method)
    val requestSettings = new WebRequest(new URL(pageUrl), strictMethod)

    val listParam = parameters.map {
      case (param, value) =>
        new NameValuePair(param, value)
    }.toList.asJava

    requestSettings.setRequestParameters(listParam)<commit_msg>Add connection timeout<commit_after>//

import java.net.URL
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.matchers._
import org.scalatest._
import org.codehaus.cargo.container.InstalledLocalContainer
import org.codehaus.cargo.container.installer.ZipURLInstaller
import org.codehaus.cargo.generic.configuration.DefaultConfigurationFactory
import org.codehaus.cargo.container.ContainerType
import org.codehaus.cargo.container.configuration.ConfigurationType
import org.codehaus.cargo.generic.DefaultContainerFactory
import org.codehaus.cargo.container.configuration.LocalConfiguration
import com.gargoylesoftware.htmlunit._
import com.gargoylesoftware.htmlunit.html._
import com.gargoylesoftware.htmlunit.util._
import org.codehaus.cargo.container.deployable.WAR
import org.codehaus.cargo.container.property._
import org.codehaus.cargo.util.log._
import scala.collection.immutable.{ Page => _, _ }
import scala.collection.JavaConverters._
import org.apache.commons.io.FileUtils
import java.io.File
import java.lang.Thread

trait ServletContainer {

  protected val WAR_KEY = "war.servlet"
  
  def keyServletContainer: String
  
  def keyWarPath: String = WAR_KEY + keyServletContainer
  
}

trait Servlet30Container extends ServletContainer {
    def keyServletContainer = "30"
}

trait Servlet25Container extends ServletContainer {
    def keyServletContainer = "25"
}

trait CargoContainerManager extends BeforeAndAfterAll {
  self: Suite =>

  def getContainer: InstalledLocalContainer

  def setContainer(container: InstalledLocalContainer): Unit

  def containerUrl: String

  def containerName: String

  def context = "/"
  
  def keyWarPath: String

  abstract override def beforeAll(configMap: Map[String, Any]) {

    val warPath = configMap.get(keyWarPath).getOrElse(Nil)

    println("WAR file to deploy: " + warPath)

    println("Download container " + containerName + " from " + containerUrl + " ...")
    val installer = new ZipURLInstaller(new URL(containerUrl))
    println("Download container done")

    println("Install container ...")
    installer.install
    println("Install container done")

    val configuration: LocalConfiguration = new DefaultConfigurationFactory().createConfiguration(
      containerName, ContainerType.INSTALLED, ConfigurationType.STANDALONE).asInstanceOf[LocalConfiguration]

    configuration.setProperty(GeneralPropertySet.LOGGING, LoggingLevel.MEDIUM.getLevel);

    val container =
      new DefaultContainerFactory().createContainer(
        containerName, ContainerType.INSTALLED, configuration).asInstanceOf[InstalledLocalContainer]

    println("Configure container")
    container.setHome(installer.getHome)
    container.setLogger(new SimpleLogger)

    val war = new WAR(warPath.toString)
    war.setContext(context)
    configuration.addDeployable(war)

    println("Start the container " + containerName)
    setContainer(container)
    container.start
  }

  abstract override def afterAll {
    println("Stop the container")
    Some(getContainer).map {
      _.stop
    }
  }
}

object AbstractPlay2WarTests {

  private val ROOT_URL = "http://localhost:8080"
  
  // Milliseconds
  private val HTTP_TIMEOUT = 5000

}

abstract class AbstractPlay2WarTests extends FeatureSpec with GivenWhenThen with ShouldMatchers with CargoContainerManager with BeforeAndAfter {

  import AbstractPlay2WarTests._

  var webClient: WebClient = null

  var container: InstalledLocalContainer = null

  def getContainer = container

  def setContainer(container: InstalledLocalContainer) = this.container = container

  def containerUrl = ""

  def containerName = ""

  before {
    webClient = new WebClient
    webClient.setJavaScriptEnabled(false)
    webClient.setThrowExceptionOnFailingStatusCode(false)
    webClient.getCookieManager.setCookiesEnabled(true)
	webClient.setTimeout(HTTP_TIMEOUT)
    new SkipClockiFrameWrapper(webClient)
  }

  after {
    webClient.closeAllWindows
  }

  def sendRequest(pageUrl: String, method: String = "GET", parameters: Map[String, String] = Map.empty, howManyTimes: Int = 1): Option[Page] = {

    val strictMethod = HttpMethod.valueOf(method)
    val requestSettings = new WebRequest(new URL(pageUrl), strictMethod)

    val listParam = parameters.map {
      case (param, value) =>
        new NameValuePair(param, value)
    }.toList.asJava

    requestSettings.setRequestParameters(listParam)
{'fraction_non_alphanumeric': 0.08278221208665906, 'fraction_numerical': 0.004104903078677309, 'mean_word_length': 4.710286458333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2367789', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2419, 'n_words': 531}