text:
<?php

namespace Cougar\UnitTests\Exceptions;

use Cougar\Exceptions\Errors;

/**
 * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2012-11-06 at 16:26:53.
 */
class ErrorExceptionTest extends \PHPUnit_Framework_TestCase {

    /**
     * @expectedException PHPUnit_Framework_Error
     */
    public function testErrorHandling()
    {
        $fh = fopen("/path/to/some/non/existent/file", "r");
        fclose($fh);
        $this->fail("Expected error was raised");
    }

    /**
     * @covers Cougar\Exceptions\Errors::setErrorHandler
     * @covers Cougar\Exceptions\Errors::exceptionErrorHandler
     * @expectedException \ErrorException
     * @depends testErrorHandling
     */
    public function testSetErrorHandler()
    {
        require_once(__DIR__ . "/../../Cougar/Exceptions/Errors.php");
        Errors::setErrorHandler();
        $fh = fopen("/path/to/some/non/existent/file", "r");
        fclose($fh);
        $this->fail("No exception was thrown");
    }
}


text:
class CreateCiBuildsRunnerSession < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  # Set this constant to true if this migration requires downtime.
  DOWNTIME = false

  def change
    create_table :ci_builds_runner_session, id: :bigserial do |t|
      t.integer :build_id, null: false
      t.string :url, null: false
      t.string :certificate
      t.string :authorization

      t.foreign_key :ci_builds, column: :build_id, on_delete: :cascade
      t.index :build_id, unique: true
    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
package com.navercorp.pinpoint.web.dao.hbase.config;

import com.navercorp.pinpoint.common.server.bo.codec.stat.AgentStatDecoder;
import com.navercorp.pinpoint.common.server.bo.stat.ActiveTraceBo;
import com.navercorp.pinpoint.common.server.bo.stat.AgentStatDataPoint;
import com.navercorp.pinpoint.common.server.bo.stat.AgentStatType;
import com.navercorp.pinpoint.common.server.bo.stat.AgentUriStatBo;
import com.navercorp.pinpoint.common.server.bo.stat.CpuLoadBo;
import com.navercorp.pinpoint.common.server.bo.stat.DataSourceBo;
import com.navercorp.pinpoint.common.server.bo.stat.DataSourceListBo;
import com.navercorp.pinpoint.common.server.bo.stat.DeadlockThreadCountBo;
import com.navercorp.pinpoint.common.server.bo.stat.DirectBufferBo;
import com.navercorp.pinpoint.common.server.bo.stat.EachUriStatBo;
import com.navercorp.pinpoint.common.server.bo.stat.FileDescriptorBo;
import com.navercorp.pinpoint.common.server.bo.stat.JvmGcBo;
import com.navercorp.pinpoint.common.server.bo.stat.JvmGcDetailedBo;
import com.navercorp.pinpoint.common.server.bo.stat.LoadedClassBo;
import com.navercorp.pinpoint.common.server.bo.stat.ResponseTimeBo;
import com.navercorp.pinpoint.common.server.bo.stat.TotalThreadCountBo;
import com.navercorp.pinpoint.common.server.bo.stat.TransactionBo;
import com.navercorp.pinpoint.web.dao.SampledAgentStatDao;
import com.navercorp.pinpoint.web.dao.hbase.stat.DefaultSampledAgentStatDao;
import com.navercorp.pinpoint.web.dao.hbase.stat.HbaseAgentStatDaoOperations;
import com.navercorp.pinpoint.web.dao.hbase.stat.HbaseSampledAgentUriStatDao;
import com.navercorp.pinpoint.web.dao.hbase.stat.HbaseSampledDataSourceDao;
import com.navercorp.pinpoint.web.dao.hbase.stat.SampledAgentStatResultExtractorSupplier;
import com.navercorp.pinpoint.web.dao.hbase.stat.SampledResultsExtractorSupplier;
import com.navercorp.pinpoint.web.mapper.stat.sampling.sampler.AgentStatSampler;
import com.navercorp.pinpoint.web.vo.stat.SampledActiveTrace;
import com.navercorp.pinpoint.web.vo.stat.SampledAgentStatDataPoint;
import com.navercorp.pinpoint.web.vo.stat.SampledAgentUriStat;
import com.navercorp.pinpoint.web.vo.stat.SampledCpuLoad;
import com.navercorp.pinpoint.web.vo.stat.SampledDataSource;
import com.navercorp.pinpoint.web.vo.stat.SampledDataSourceList;
import com.navercorp.pinpoint.web.vo.stat.SampledDeadlock;
import com.navercorp.pinpoint.web.vo.stat.SampledDirectBuffer;
import com.navercorp.pinpoint.web.vo.stat.SampledEachUriStatBo;
import com.navercorp.pinpoint.web.vo.stat.SampledFileDescriptor;
import com.navercorp.pinpoint.web.vo.stat.SampledJvmGc;
import com.navercorp.pinpoint.web.vo.stat.SampledJvmGcDetailed;
import com.navercorp.pinpoint.web.vo.stat.SampledLoadedClassCount;
import com.navercorp.pinpoint.web.vo.stat.SampledResponseTime;
import com.navercorp.pinpoint.web.vo.stat.SampledTotalThreadCount;
import com.navercorp.pinpoint.web.vo.stat.SampledTransaction;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

@Configuration
public class SampledAgentStatDaoConfiguration {

    private final HbaseAgentStatDaoOperations operations;

    public SampledAgentStatDaoConfiguration(HbaseAgentStatDaoOperations operations) {
        this.operations = Objects.requireNonNull(operations, "operations");
    }

    private <IN extends AgentStatDataPoint,
            OUT extends SampledAgentStatDataPoint> SampledAgentStatDao<OUT> newSampledDao(AgentStatType statType,
                                                                                AgentStatDecoder<IN> decoder,
                                                                                AgentStatSampler<IN, OUT> sampler) {
        Objects.requireNonNull(statType, "statType");
        Objects.requireNonNull(decoder, "decoder");
        Objects.requireNonNull(sampler, "sampler");

        SampledResultsExtractorSupplier<IN, OUT> supplier = new SampledAgentStatResultExtractorSupplier<>(sampler);
        return new DefaultSampledAgentStatDao<>(statType, operations, decoder, supplier);
    }

    @Bean
    public SampledAgentStatDao<SampledJvmGc> getSampledJvmGcDao(AgentStatDecoder<JvmGcBo> decoder,
                                                                AgentStatSampler<JvmGcBo, SampledJvmGc> sampler) {
        return newSampledDao(AgentStatType.JVM_GC, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledJvmGcDetailed> getSampledJvmGcDetailedDao(AgentStatDecoder<JvmGcDetailedBo> decoder,
                                                                                AgentStatSampler<JvmGcDetailedBo, SampledJvmGcDetailed> sampler) {
        return newSampledDao(AgentStatType.JVM_GC_DETAILED, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledCpuLoad> getSampledCpuLoadDao(AgentStatDecoder<CpuLoadBo> decoder,
                                                                    AgentStatSampler<CpuLoadBo, SampledCpuLoad> sampler) {
        return newSampledDao(AgentStatType.CPU_LOAD, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledTransaction> getSampledTransactionDao(AgentStatDecoder<TransactionBo> decoder,
                                                                            AgentStatSampler<TransactionBo, SampledTransaction> sampler) {
        return newSampledDao(AgentStatType.TRANSACTION, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledActiveTrace> getSampledActiveTraceDao(AgentStatDecoder<ActiveTraceBo> decoder,
                                                                            AgentStatSampler<ActiveTraceBo, SampledActiveTrace> sampler) {
        return newSampledDao(AgentStatType.ACTIVE_TRACE, decoder, sampler);
    }

//    @Bean
    public SampledAgentStatDao<SampledDataSourceList> getSampledDataSourceListDao(AgentStatDecoder<DataSourceListBo> decoder,
                                                                                AgentStatSampler<DataSourceBo, SampledDataSource> sampler) {
        return new HbaseSampledDataSourceDao(operations, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledResponseTime> getSampledResponseTimeDao(AgentStatDecoder<ResponseTimeBo> decoder,
                                                                              AgentStatSampler<ResponseTimeBo, SampledResponseTime> sampler) {
        return newSampledDao(AgentStatType.RESPONSE_TIME, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledDeadlock> getSampledDeadlockDao(AgentStatDecoder<DeadlockThreadCountBo> decoder,
                                                                      AgentStatSampler<DeadlockThreadCountBo, SampledDeadlock> sampler) {
        return newSampledDao(AgentStatType.DEADLOCK, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledFileDescriptor> getSampledFileDescriptorDao(AgentStatDecoder<FileDescriptorBo> decoder,
                                                                                AgentStatSampler<FileDescriptorBo, SampledFileDescriptor> sampler) {
        return newSampledDao(AgentStatType.FILE_DESCRIPTOR, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledDirectBuffer> getSampledDirectBufferDao(AgentStatDecoder<DirectBufferBo> decoder,
                                                                              AgentStatSampler<DirectBufferBo, SampledDirectBuffer> sampler) {
        return newSampledDao(AgentStatType.DIRECT_BUFFER, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledTotalThreadCount> getSampledTotalThreadCountDao(AgentStatDecoder<TotalThreadCountBo> decoder,
                                                                                AgentStatSampler<TotalThreadCountBo, SampledTotalThreadCount> sampler) {
        return newSampledDao(AgentStatType.TOTAL_THREAD, decoder, sampler);
    }

    @Bean
    public SampledAgentStatDao<SampledLoadedClassCount> getSampledLoadedClassCountDao(AgentStatDecoder<LoadedClassBo> decoder,
                                                                                AgentStatSampler<LoadedClassBo, SampledLoadedClassCount> sampler) {
        return newSampledDao(AgentStatType.LOADED_CLASS, decoder, sampler);
    }

//    @Bean
    public SampledAgentStatDao<SampledAgentUriStat> getSampledAgentUriStatDao(@Qualifier("agentUriDaoOperations") HbaseAgentStatDaoOperations operations,
                                                                              AgentStatDecoder<AgentUriStatBo> decoder,
                                                                              AgentStatSampler<EachUriStatBo, SampledEachUriStatBo> sampler) {
        return new HbaseSampledAgentUriStatDao(operations, decoder, sampler);
    }
}


text:
// 11. Describe the difference between C# and .NET Framework.

using System;

class CSharpAndNetFrameworkDifferences
{
    static void Main(string[] args)
    {
        Console.SetWindowSize(78, 25);
        Console.SetBufferSize(78, 27);

        string cSharp =
            @"It is an elegant and type-safe object-oriented language that enables
developers to build a variety of secure and robust applications
that run on the .NET Framework. You can use C# to create traditional
Windows client applications, XML Web services, distributed components,
client-server applications, database applications, and much, much more.";

        string dotNetFramework =
            @"C# programs run on the .NET Framework, an integral component of Windows
that includes a virtual execution system called the common language
runtime (CLR) and a unified set of class libraries. The CLR is the
commercial implementation by Microsoft of the common language
infrastructure (CLI), an international standard that is the basis
for creating execution and development environments in which
languages and libraries work together seamlessly.

In addition to the run time services, the .NET Framework also includes an
extensive library of over 4000 classes organized into namespaces that
provide a wide variety of useful functionality for everything from file
input and output to string manipulation to XML parsing, to Windows Forms
controls. The typical C# application uses the .NET Framework class library
extensively to handle common ""plumbing"" chores.";

        PrintText("C#", cSharp);
        PrintText(".NET Framework", dotNetFramework);
        Console.ReadKey();
    }

    static void PrintText(string title, string bodyText)
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine(title+ ": ");

        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine(bodyText);
        Console.WriteLine();
        Console.ResetColor();
    }
}


text:
package org.apache.zeppelin.rest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterSetting;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.scheduler.Job.Status;
import org.apache.zeppelin.server.ZeppelinServer;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.google.gson.Gson;

/**
 * Test against spark cluster.
 * Spark cluster is started by CI server using testing/startSparkCluster.sh
 */
public class ZeppelinSparkClusterTest extends AbstractTestRestApi {
    Gson gson = new Gson();
    AuthenticationInfo anonymous;

    @BeforeClass
    public static void init() throws Exception {
        AbstractTestRestApi.startUp();
    }

    @AfterClass
    public static void destroy() throws Exception {
        AbstractTestRestApi.shutDown();
    }

    @Before
    public void setUp() {
        anonymous = new AuthenticationInfo("anonymous");
    }

    private void waitForFinish(Paragraph p) {
        while (p.getStatus() != Status.FINISHED
                && p.getStatus() != Status.ERROR
                && p.getStatus() != Status.ABORT) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                LOG.error("Exception in WebDriverManager while getWebDriver ", e);
            }
        }
    }

    @Test
    public void scalaOutputTest() throws IOException {
        // create new note
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        Map config = p.getConfig();
        config.put("enabled", true);
        p.setConfig(config);
        p.setText("%spark import java.util.Date\n" +
            "import java.net.URL\n" +
            "println(\"hello\")\n"
        );
        p.setAuthenticationInfo(anonymous);
        note.run(p.getId());
        waitForFinish(p);
        assertEquals(Status.FINISHED, p.getStatus());
        assertEquals("import java.util.Date\n" +
            "import java.net.URL\n" +
            "hello\n", p.getResult().message().get(0).getData());
        ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    }

    @Test
    public void basicRDDTransformationAndActionTest() throws IOException {
        // create new note
        Note note = ZeppelinServer.notebook.createNote(anonymous);

        // run markdown paragraph, again
        Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        Map config = p.getConfig();
        config.put("enabled", true);
        p.setConfig(config);
        p.setText("%spark print(sc.parallelize(1 to 10).reduce(_ + _))");
        p.setAuthenticationInfo(anonymous);
        note.run(p.getId());
        waitForFinish(p);
        assertEquals(Status.FINISHED, p.getStatus());
        assertEquals("55", p.getResult().message().get(0).getData());
        ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    }

    @Test
    public void sparkSQLTest() throws IOException {
        // create new note
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        int sparkVersion = getSparkVersionNumber(note);
        // DataFrame API is available from spark 1.3
        if (sparkVersion >= 13) {
            // test basic dataframe api
            Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map config = p.getConfig();
            config.put("enabled", true);
            p.setConfig(config);
            p.setText("%spark val df=sqlContext.createDataFrame(Seq((\"hello\",20)))\n" +
                    "df.collect()");
            p.setAuthenticationInfo(anonymous);
            note.run(p.getId());
            waitForFinish(p);
            assertEquals(Status.FINISHED, p.getStatus());
            assertTrue(p.getResult().message().get(0).getData().contains(
                    "Array[org.apache.spark.sql.Row] = Array([hello,20])"));

            // test display DataFrame
            p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            config = p.getConfig();
            config.put("enabled", true);
            p.setConfig(config);
            p.setText("%spark val df=sqlContext.createDataFrame(Seq((\"hello\",20)))\n" +
                    "z.show(df)");
            p.setAuthenticationInfo(anonymous);
            note.run(p.getId());
            waitForFinish(p);
            assertEquals(Status.FINISHED, p.getStatus());
            assertEquals(InterpreterResult.Type.TABLE, p.getResult().message().get(1).getType());
            assertEquals("_1\t_2\nhello\t20\n", p.getResult().message().get(1).getData());

            // test display DataSet
            if (sparkVersion >= 20) {
                p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
                config = p.getConfig();
                config.put("enabled", true);
                p.setConfig(config);
                p.setText("%spark val ds=spark.createDataset(Seq((\"hello\",20)))\n" +
                        "z.show(ds)");
                p.setAuthenticationInfo(anonymous);
                note.run(p.getId());
                waitForFinish(p);
                assertEquals(Status.FINISHED, p.getStatus());
                assertEquals(InterpreterResult.Type.TABLE, p.getResult().message().get(1).getType());
                assertEquals("_1\t_2\nhello\t20\n", p.getResult().message().get(1).getData());
            }
            ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
        }
    }

    @Test
    public void sparkRTest() throws IOException {
      // create new note
      Note note = ZeppelinServer.notebook.createNote(anonymous);
      int sparkVersion = getSparkVersionNumber(note);

      if (isSparkR() && sparkVersion >= 14) {   // sparkr supported from 1.4.0
        // restart spark interpreter
        List<InterpreterSetting> settings =
          ZeppelinServer.notebook.getBindedInterpreterSettings(note.getId());

        for (InterpreterSetting setting : settings) {
          if (setting.getName().equals("spark")) {
            ZeppelinServer.notebook.getInterpreterSettingManager().restart(setting.getId());
            break;
          }
        }

        String sqlContextName = "sqlContext";
        if (sparkVersion >= 20) {
          sqlContextName = "spark";
        }
        Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        Map config = p.getConfig();
        config.put("enabled", true);
        p.setConfig(config);
        p.setText("%r localDF <- data.frame(name=c(\"a\", \"b\", \"c\"), age=c(19, 23, 18))\n" +
            "df <- createDataFrame(" + sqlContextName + ", localDF)\n" +
            "count(df)"
        );
        p.setAuthenticationInfo(anonymous);
        note.run(p.getId());
        waitForFinish(p);
        System.err.println("sparkRTest=" + p.getResult().message().get(0).getData());
        assertEquals(Status.FINISHED, p.getStatus());
        assertEquals("[1] 3", p.getResult().message().get(0).getData().trim());
      }
      ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    }

    @Test
    public void pySparkTest() throws IOException {
        // create new note
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        note.setName("note");
        int sparkVersion = getSparkVersionNumber(note);

        if (isPyspark() && sparkVersion >= 12) {   // pyspark supported from 1.2.1
            // run markdown paragraph, again
            Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map config = p.getConfig();
            config.put("enabled", true);
            p.setConfig(config);
            p.setText("%pyspark print(sc.parallelize(range(1, 11)).reduce(lambda a, b: a + b))");
            p.setAuthenticationInfo(anonymous);
            note.run(p.getId());
            waitForFinish(p);
            assertEquals(Status.FINISHED, p.getStatus());
            assertEquals("55\n", p.getResult().message().get(0).getData());
            if (sparkVersion >= 13) {
                // run sqlContext test
                p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
                config = p.getConfig();
                config.put("enabled", true);
                p.setConfig(config);
                p.setText("%pyspark from pyspark.sql import Row\n" +
                        "df=sqlContext.createDataFrame([Row(id=1, age=20)])\n" +
                        "df.collect()");
                p.setAuthenticationInfo(anonymous);
                note.run(p.getId());
                waitForFinish(p);
                assertEquals(Status.FINISHED, p.getStatus());
                assertEquals("[Row(age=20, id=1)]\n", p.getResult().message().get(0).getData());

                // test display Dataframe
                p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
                config = p.getConfig();
                config.put("enabled", true);
                p.setConfig(config);
                p.setText("%pyspark from pyspark.sql import Row\n" +
                        "df=sqlContext.createDataFrame([Row(id=1, age=20)])\n" +
                        "z.show(df)");
                p.setAuthenticationInfo(anonymous);
                note.run(p.getId());
                waitForFinish(p);
                assertEquals(Status.FINISHED, p.getStatus());
                assertEquals(InterpreterResult.Type.TABLE, p.getResult().message().get(0).getType());
                // TODO (zjffdu), one more \n is appended, need to investigate why.
                assertEquals("age\tid\n20\t1\n", p.getResult().message().get(0).getData());

                // test udf
                p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
                config = p.getConfig();
                config.put("enabled", true);
                p.setConfig(config);
                p.setText("%pyspark sqlContext.udf.register(\"f1\", lambda x: len(x))\n" +
                       "sqlContext.sql(\"select f1(\\\"abc\\\") as len\").collect()");
                p.setAuthenticationInfo(anonymous);
                note.run(p.getId());
                waitForFinish(p);
                assertEquals(Status.FINISHED, p.getStatus());
                assertEquals("[Row(len=u'3')]\n", p.getResult().message().get(0).getData());
            }
            if (sparkVersion >= 20) {
                // run SparkSession test
                p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
                config = p.getConfig();
                config.put("enabled", true);
                p.setConfig(config);
                p.setText("%pyspark from pyspark.sql import Row\n" +
                        "df=sqlContext.createDataFrame([Row(id=1, age=20)])\n" +
                        "df.collect()");
                p.setAuthenticationInfo(anonymous);
                note.run(p.getId());
                waitForFinish(p);
                assertEquals(Status.FINISHED, p.getStatus());
                assertEquals("[Row(age=20, id=1)]\n", p.getResult().message().get(0).getData());

                // test udf
                p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
                config = p.getConfig();
                config.put("enabled", true);
                p.setConfig(config);
                // use SQLContext to register UDF but use this UDF through SparkSession
                p.setText("%pyspark sqlContext.udf.register(\"f1\", lambda x: len(x))\n" +
                        "spark.sql(\"select f1(\\\"abc\\\") as len\").collect()");
                p.setAuthenticationInfo(anonymous);
                note.run(p.getId());
                waitForFinish(p);
                assertEquals(Status.FINISHED, p.getStatus());
                assertEquals("[Row(len=u'3')]\n", p.getResult().message().get(0).getData());
            }
        }
        ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    }

    @Test
    public void pySparkAutoConvertOptionTest() throws IOException {
        // create new note
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        note.setName("note");

        int sparkVersionNumber = getSparkVersionNumber(note);

        if (isPyspark() && sparkVersionNumber >= 14) {   // auto_convert enabled from spark 1.4
            // run markdown paragraph, again
            Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map config = p.getConfig();
            config.put("enabled", true);
            p.setConfig(config);

            String sqlContextName = "sqlContext";
            if (sparkVersionNumber >= 20) {
                sqlContextName = "spark";
            }

            p.setText("%pyspark\nfrom pyspark.sql.functions import *\n"
                    + "print(" + sqlContextName + ".range(0, 10).withColumn('uniform', rand(seed=10) * 3.14).count())");
            p.setAuthenticationInfo(anonymous);
            note.run(p.getId());
            waitForFinish(p);
            assertEquals(Status.FINISHED, p.getStatus());
            assertEquals("10\n", p.getResult().message().get(0).getData());
        }
        ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    }

    @Test
    public void zRunTest() throws IOException {
        // create new note
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        Paragraph p0 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        Map config0 = p0.getConfig();
        config0.put("enabled", true);
        p0.setConfig(config0);
        p0.setText("%spark z.run(1)");
        p0.setAuthenticationInfo(anonymous);
        Paragraph p1 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        Map config1 = p1.getConfig();
        config1.put("enabled", true);
        p1.setConfig(config1);
        p1.setText("%spark val a=10");
        p1.setAuthenticationInfo(anonymous);
        Paragraph p2 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        Map config2 = p2.getConfig();
        config2.put("enabled", true);
        p2.setConfig(config2);
        p2.setText("%spark print(a)");
        p2.setAuthenticationInfo(anonymous);

        note.run(p0.getId());
        waitForFinish(p0);
        assertEquals(Status.FINISHED, p0.getStatus());

        // z.run is not blocking call. So p1 may not be finished when p0 is done.
        waitForFinish(p1);
        note.run(p2.getId());
        waitForFinish(p2);
        assertEquals(Status.FINISHED, p2.getStatus());
        assertEquals("10", p2.getResult().message().get(0).getData());

        Paragraph p3 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        Map config3 = p3.getConfig();
        config3.put("enabled", true);
        p3.setConfig(config3);
        p3.setText("%spark println(new java.util.Date())");
        p3.setAuthenticationInfo(anonymous);

        p0.setText(String.format("%%spark z.runNote(\"%s\")", note.getId()));
        note.run(p0.getId());
        waitForFinish(p0);
        waitForFinish(p1);
        waitForFinish(p2);
        waitForFinish(p3);

        assertEquals(Status.FINISHED, p3.getStatus());
        String p3result = p3.getResult().message().get(0).getData();
        assertNotEquals(null, p3result);
        assertNotEquals("", p3result);

        p0.setText(String.format("%%spark z.run(\"%s\", \"%s\")", note.getId(), p3.getId()));
        p3.setText("%%spark println(\"END\")");

        note.run(p0.getId());
        waitForFinish(p0);
        waitForFinish(p3);

        assertNotEquals(p3result, p3.getResult().message());

        ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    }

    @Test
    public void pySparkDepLoaderTest() throws IOException {
        // create new note
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        int sparkVersionNumber = getSparkVersionNumber(note);

        if (isPyspark() && sparkVersionNumber >= 14) {
            // restart spark interpreter
            List<InterpreterSetting> settings =
                    ZeppelinServer.notebook.getBindedInterpreterSettings(note.getId());

            for (InterpreterSetting setting : settings) {
                if (setting.getName().equals("spark")) {
                    ZeppelinServer.notebook.getInterpreterSettingManager().restart(setting.getId());
                    break;
                }
            }

            // load dep
            Paragraph p0 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map config = p0.getConfig();
            config.put("enabled", true);
            p0.setConfig(config);
            p0.setText("%dep z.load(\"com.databricks:spark-csv_2.11:1.2.0\")");
            p0.setAuthenticationInfo(anonymous);
            note.run(p0.getId());
            waitForFinish(p0);
            assertEquals(Status.FINISHED, p0.getStatus());

            // write test csv file
            File tmpFile = File.createTempFile("test", "csv");
            FileUtils.write(tmpFile, "a,b\n1,2");

            // load data using libraries from dep loader
            Paragraph p1 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setConfig(config);

            String sqlContextName = "sqlContext";
            if (sparkVersionNumber >= 20) {
                sqlContextName = "spark";
            }
            p1.setText("%pyspark\n" +
                    "from pyspark.sql import SQLContext\n" +
                    "print(" + sqlContextName + ".read.format('com.databricks.spark.csv')" +
                    ".load('"+ tmpFile.getAbsolutePath() +"').count())");
            p1.setAuthenticationInfo(anonymous);
            note.run(p1.getId());

            waitForFinish(p1);
            assertEquals(Status.FINISHED, p1.getStatus());
            assertEquals("2\n", p1.getResult().message().get(0).getData());
        }
        ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    }

    /**
     * Get spark version number as a numerical value.
     * eg. 1.1.x => 11, 1.2.x => 12, 1.3.x => 13 ...
     */
    private int getSparkVersionNumber(Note note) {
        Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        note.setName("note");
        Map config = p.getConfig();
        config.put("enabled", true);
        p.setConfig(config);
        p.setText("%spark print(sc.version)");
        p.setAuthenticationInfo(anonymous);
        note.run(p.getId());
        waitForFinish(p);
        assertEquals(Status.FINISHED, p.getStatus());
        String sparkVersion = p.getResult().message().get(0).getData();
        System.out.println("Spark version detected " + sparkVersion);
        String[] split = sparkVersion.split("\\.");
        int version = Integer.parseInt(split[0]) * 10 + Integer.parseInt(split[1]);
        return version;
    }

    @Test
    public void testSparkZeppelinContextDynamicForms() throws IOException {
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        note.setName("note");
        Map config = p.getConfig();
        config.put("enabled", true);
        p.setConfig(config);
        String code = "%spark.spark println(z.textbox(\"my_input\", \"default_name\"))\n" +
            "println(z.select(\"my_select\", \"1\"," +
            "Seq((\"1\", \"select_1\"), (\"2\", \"select_2\"))))\n" +
            "val items=z.checkbox(\"my_checkbox\", Seq(\"2\"), " +
            "Seq((\"1\", \"check_1\"), (\"2\", \"check_2\")))\n" +
            "println(items(0))";
        p.setText(code);
        p.setAuthenticationInfo(anonymous);
        note.run(p.getId());
        waitForFinish(p);

        assertEquals(Status.FINISHED, p.getStatus());
        Iterator<String> formIter = p.settings.getForms().keySet().iterator();
        assert(formIter.next().equals("my_input"));
        assert(formIter.next().equals("my_select"));
        assert(formIter.next().equals("my_checkbox"));

        // check dynamic forms values
        String[] result = p.getResult().message().get(0).getData().split("\n");
        assertEquals(4, result.length);
        assertEquals("default_name", result[0]);
        assertEquals("1", result[1]);
        assertEquals("items: Seq[Object] = Buffer(2)", result[2]);
        assertEquals("2", result[3]);
    }

    @Test
    public void testPySparkZeppelinContextDynamicForms() throws IOException {
        Note note = ZeppelinServer.notebook.createNote(anonymous);
        Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        note.setName("note");
        Map config = p.getConfig();
        config.put("enabled", true);
        p.setConfig(config);
        String code = "%spark.pyspark print(z.input('my_input', 'default_name'))\n" +
            "print(z.select('my_select', " +
            "[('1', 'select_1'), ('2', 'select_2')], defaultValue='1'))\n" +
            "items=z.checkbox('my_checkbox', " +
            "[('1', 'check_1'), ('2', 'check_2')], defaultChecked=['2'])\n" +
            "print(items[0])";
        p.setText(code);
        p.setAuthenticationInfo(anonymous);
        note.run(p.getId());
        waitForFinish(p);

        assertEquals(Status.FINISHED, p.getStatus());
        Iterator<String> formIter = p.settings.getForms().keySet().iterator();
        assert(formIter.next().equals("my_input"));
        assert(formIter.next().equals("my_select"));
        assert(formIter.next().equals("my_checkbox"));

        // check dynamic forms values
        String[] result = p.getResult().message().get(0).getData().split("\n");
        assertEquals(3, result.length);
        assertEquals("default_name", result[0]);
        assertEquals("1", result[1]);
        assertEquals("2", result[2]);
    }
}


text:
__author__ = 'elubin'
import os
import glob
import logging
import subprocess
import tempfile
import tarfile
import re
from packagemanager.packagemanager import MultiRootPackageManager
from dockerfile import DiffBasedDockerBuild, DockerBuild, DockerFile
#from include import RESULTS_LOGGER
from utils.utils import rm_rf, extract_tar
from diff import RSyncDiffTool
from ps import ProcessManager

class LinuxInfoParser(object):
    def __init__(self, path_to_root):
        self.path_to_root = path_to_root

    def _parse_os_info(self, file_paths):
        out = {}
        suspects = ['Mageia', 'CentOS']
        match = re.compile('(%s) (Linux )?release ([\d]+).*' % '|'.join(suspects))
        for path in file_paths:
            with open(path, 'r') as f:
                input = f.read()
                for line in input.splitlines():
                    if '=' in line:
                        key, value = line.split('=', 1)
                        if len(value) > 0:
                            if value[0] == '"' and value[-1] == '"':
                                out[key] = value[1:-1]
                            else:
                                out[key] = value
                    elif match.match(line):
                        m = match.match(line)
                        out['ID'] = m.group(1).lower()
                        out['VERSION_ID'] = m.group(3)

        return out

    def generate_os_info(self):
        etc = os.path.join(self.path_to_root, 'etc', '*-release')
        files = glob.glob(etc)
        return self._parse_os_info(files)

class BaseImageGenerator(object):
    def __init__(self, vm_socket, dclient, process_packages=True, cache=False, filter_deps=False, debug=False):
        self.docker_client = dclient
        self.process_packages = process_packages
        self.vm_socket = vm_socket
        self.cache = cache
        self.filter_deps = filter_deps
        self.debug = debug

    def __enter__(self):
        self.temp_dir = tempfile.mkdtemp()
        base_image_dir = 'root_fs'
        self.base_image_root = os.path.join(self.temp_dir, base_image_dir, '')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.clean_up()
        return False

    def generate_linux_info(self, vm_root):
        self.linux_info = LinuxInfoParser(vm_root).generate_os_info()
        logging.debug('Detected OS: %s' % self.linux_info.get('PRETTY_NAME', self.linux_info.get('DISTRIB_DESCRIPTION')))

    @staticmethod
    def transform_tag(repo, tag):
        if repo == 'centos':
            return repo + tag
        else:
            return tag

    def verify_with_user(self):
        confirm = raw_input('Enter the linux release and version of the VM [%s]: ' % self.linux_info['PRETTY_NAME'])
        return confirm.strip() == ''

    def find_base_image(self, repo, tag):
        logging.debug("Searching for base image with repo %s, tag %s" % (repo, tag))
        repo_tag = DockerFile.format_image_name(repo, tag)
        self.docker_client.pull(repo)
        logging.debug(self.docker_client.images())
        candidate_image = [x for x in self.docker_client.images() if repo_tag in x['RepoTags']]
        if len(candidate_image) == 1:
            return self.start_image_and_generate_container_id(repo_tag)
        else:
            return None  # TODO: this isn't good, need to manually generate base image with debootstrap

    def start_image_and_generate_container_id(self, repo_tag, command='echo FAKECOMMAND'):
        res = self.docker_client.create_container(repo_tag, command=command)
        container_id = res['Id']
        logging.debug('Container ID: %s' % container_id)
        return container_id

    def export_container_to_tar(self, container_id, tar_name='base_linux.tar'):
        # TODO: compare memory usage of python export, i think it loads entire archive into memory which is not what we want
        # TODO; if the python API isn't good enough, then at least use standard python-based piping instead
        abs_tar_path = os.path.join(self.temp_dir, tar_name)
        logging.debug('Writing base image to %s' % abs_tar_path)
        subprocess.check_output('docker export %s > %s' % (container_id, abs_tar_path), shell=True)

        assert tarfile.is_tarfile(abs_tar_path)
        return abs_tar_path

    def extract_tar(self, tar_path, target_dir):

        if not os.path.isabs(target_dir):
            target_dir = os.path.join(self.temp_dir, target_dir, '')

        return extract_tar(tar_path, target_dir, clean_up=not self.debug)

    def extract_base_image_tar(self, base_tar_path):
        self.extract_tar(base_tar_path, self.base_image_root)

    def generate(self, vm_tag, run_locally=False, tar_options='-z', diff_tool=RSyncDiffTool, processes=True):
        # get the filesystem from the socket
        new_vm_root = os.path.join(self.temp_dir, 'vm_root', '') # stupid trailing / hack
        logging.debug('Obtaining filesystem from socket connection')
        tar_path = self.vm_socket.get_filesystem(tar_options)
        assert tarfile.is_tarfile(tar_path)
        self.extract_tar(tar_path, new_vm_root)

        self.generate_linux_info(new_vm_root)

        logging.debug('Version Info: %s' % repr(self.linux_info))
        repo = self.linux_info.get('ID', self.linux_info.get('DISTRIB_ID')).lower()

        tag = self.linux_info.get('VERSION_ID', self.linux_info.get('DISTRIB_RELEASE'))
        tag = self.transform_tag(repo, tag)
        container_id = self.find_base_image(repo, tag)
        assert container_id is not None
        base_tar_path = self.export_container_to_tar(container_id)
        self.extract_base_image_tar(base_tar_path)

        if self.process_packages:
            logging.debug('Generating package manager commands...')

            with MultiRootPackageManager(self.vm_socket, repo, tag, self.docker_client, filter_package_deps=self.filter_deps) as m:
                package_file, cmds = m.prepare_vm()
                repo_files = m.vm.REPO_FILES
                clean_cmd = m.vm.get_clean_cmd()

            if len(cmds) > 0:
                with DockerBuild(repo, tag, self.docker_client) as db:

                    if repo_files is not None and len(repo_files) > 0:
                        db.archive(repo_files, new_vm_root, DockerBuild.REPO_INFO)

                    if package_file is not None:
                        package_list_path = os.path.join(db.dir, DockerBuild.PKG_LIST)
                        with open(package_list_path, 'w') as f:
                            f.write(package_file)

                        db.add_file(DockerBuild.PKG_LIST)

                    db.df.add_build_cmds(cmds)
                    db.df.add_build_cmd(clean_cmd)
                    db.serialize()
                    id = db.build('packages-only')

                if id is None:
                    raise ValueError("One or more of the commands failed to execute successfully")
                # once built, we wanna export it and create a diff
                container_id = self.start_image_and_generate_container_id(id)
                tar_name = 'packages.tar'
                abs_tar_path = self.export_container_to_tar(container_id, tar_name)

                self.base_image_root = self.extract_tar(abs_tar_path, 'packages_container')
                repo = id
                tag = None

        # TODO: trash the kernel
        logging.debug('Generating filesystem diff...')
        with diff_tool(self.base_image_root, new_vm_root, debug=self.debug) as diff_tool_instance:
            diff_tool_instance.do_diff()

            with DiffBasedDockerBuild(diff_tool_instance, repo, tag, self.docker_client) as build:
                ## detect running processes here
                if processes:
                    p_manager = ProcessManager(self.vm_socket)
                    active_processes = p_manager.get_processes()
                    logging.debug("Found the following %d processes running on host: %s", len(active_processes), active_processes)
                    build.set_process(active_processes[1])
                build.serialize()
                logging.debug('Docker build is now located at: %s' % build.dir)
                build.build(vm_tag)
                logging.debug("To run the container execute the following:\n$ docker run -P %s" % vm_tag)
                #if run:

                    #res = self.docker_client.create_container(tag)
                    #logging.debug("Kickstarted Docker image with container ID: %s" % res)

    def clean_up(self):
        # delete the temporary directory
        if not self.debug:
            rm_rf(self.temp_dir)
    #
    # def generate_statistics(self, new_vm_root, base_image_root):
    #     thinned_vm_size = recursive_size(new_vm_root)
    #     base_image_size = recursive_size(base_image_root)
    #     diff_size = recursive_size(self.modified_directory)
    #     logging.getLogger(RESULTS_LOGGER).info('VM size: %sMB, Thin VM size: %sMB, Base image size: %sMB, Diff: %sMB', thinned_vm_size, base_image_size, diff_size)

#TODO: make a verify tool that builds the docker file, exports the image, and then does a diff on the resulting filesystem compared to the original VM


text:
import { Model } from 'sequelize';

// module.exports for Sequelize import
module.exports = (sequelize, DataTypes) => {
  class Article extends Model {}

  Article.init({
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    userId: {
      type: DataTypes.INTEGER,
      field: 'user_id'
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: true,
        len: [1, 100]
      }
    },
    description: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: true,
        len: [1, 250]
      }
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    }
  }, {
    sequelize,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    tableName: 'articles',
    getterMethods : {
      createdAt: function() {
        return this.created_at;
      }
    }
  });

  Article.associate = function(models) {
    Article.belongsTo(models.User, { foreignKey: 'user_id' });
  };

  return Article;
};


text:
const uri = 'mongodb://rb7373:rb7373@ds035786.mlab.com:35786/heroku_xdrvz1pv';

let Mongoose = require("mongoose");
Mongoose.connect(uri);

let db = Mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", function (callback) {
  console.log("Connection succeeded.");

  const topic = require('./models/topics');
  topic.createFunctions();
  topic.createGeometry();

});


text:
package org.elasticsearch.search.aggregations.bucket;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.search.aggregations.bucket.range.DateRangeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.range.Range;
import org.elasticsearch.search.aggregations.bucket.range.Range.Bucket;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.test.ESIntegTestCase;
import org.hamcrest.Matchers;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.search.aggregations.AggregationBuilders.dateRange;
import static org.elasticsearch.search.aggregations.AggregationBuilders.histogram;
import static org.elasticsearch.search.aggregations.AggregationBuilders.sum;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;

@ESIntegTestCase.SuiteScopeTestCase
public class DateRangeIT extends ESIntegTestCase {

    private static IndexRequestBuilder indexDoc(int month, int day, int value) throws Exception {
        return client().prepareIndex("idx", "type").setSource(jsonBuilder()
                .startObject()
                .field("value", value)
                .timeField("date", date(month, day))
                .startArray("dates").timeValue(date(month, day)).timeValue(date(month + 1, day + 1)).endArray()
                .endObject());
    }

    private static DateTime date(int month, int day) {
        return date(month, day, DateTimeZone.UTC);
    }

    private static DateTime date(int month, int day, DateTimeZone timezone) {
        return new DateTime(2012, month, day, 0, 0, timezone);
    }

    private static int numDocs;
    @Override
    public void setupSuiteScopeCluster() throws Exception {
        createIndex("idx");
        createIndex("idx_unmapped");

        numDocs = randomIntBetween(7, 20);

        List<IndexRequestBuilder> docs = new ArrayList<>();
        docs.addAll(Arrays.asList(
                indexDoc(1, 2, 1),  // Jan 2
                indexDoc(2, 2, 2),  // Feb 2
                indexDoc(2, 15, 3), // Feb 15
                indexDoc(3, 2, 4),  // Mar 2
                indexDoc(3, 15, 5), // Mar 15
                indexDoc(3, 23, 6))); // Mar 23

        // dummy docs
        for (int i = docs.size(); i < numDocs; ++i) {
            docs.add(indexDoc(randomIntBetween(6, 10), randomIntBetween(1, 20), randomInt(100)));
        }
        assertAcked(prepareCreate("empty_bucket_idx").addMapping("type", "value", "type=integer"));
        for (int i = 0; i < 2; i++) {
            docs.add(client().prepareIndex("empty_bucket_idx", "type", ""+i).setSource(jsonBuilder()
                    .startObject()
                    .field("value", i*2)
                    .endObject()));
        }
        indexRandom(true, docs);
        ensureSearchable();
    }

    @Override
    protected Collection<Class<? extends Plugin>> nodePlugins() {
        return Collections.singleton(DateScriptMocksPlugin.class);
    }

    public void testDateMath() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("fieldname", "date");
        DateRangeAggregationBuilder rangeBuilder = dateRange("range");
        if (randomBoolean()) {
            rangeBuilder.field("date");
        } else {
            rangeBuilder.script(new Script(ScriptType.INLINE, "mockscript", DateScriptMocksPlugin.EXTRACT_FIELD, params));
        }
        SearchResponse response = client()
                .prepareSearch("idx")
                .addAggregation(
                        rangeBuilder.addUnboundedTo("a long time ago", "now-50y").addRange("recently", "now-50y", "now-1y")
                                .addUnboundedFrom("last year", "now-1y").timeZone(DateTimeZone.forID("EST"))).execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        assertThat(range.getBuckets().size(), equalTo(3));

        List<Range.Bucket> buckets = new ArrayList<>(range.getBuckets());

        Range.Bucket bucket = buckets.get(0);
        assertThat((String) bucket.getKey(), equalTo("a long time ago"));
        assertThat(bucket.getKeyAsString(), equalTo("a long time ago"));
        assertThat(bucket.getDocCount(), equalTo(0L));

        bucket = buckets.get(1);
        assertThat((String) bucket.getKey(), equalTo("recently"));
        assertThat(bucket.getKeyAsString(), equalTo("recently"));
        assertThat(bucket.getDocCount(), equalTo((long) numDocs));

        bucket = buckets.get(2);
        assertThat((String) bucket.getKey(), equalTo("last year"));
        assertThat(bucket.getKeyAsString(), equalTo("last year"));
        assertThat(bucket.getDocCount(), equalTo(0L));
    }

    public void testSingleValueField() throws Exception {
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo(date(2, 15))
                        .addRange(date(2, 15), date(3, 15))
                        .addUnboundedFrom(date(3, 15)))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
    }

    public void testSingleValueFieldWithStringDates() throws Exception {
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo("2012-02-15")
                        .addRange("2012-02-15", "2012-03-15")
                        .addUnboundedFrom("2012-03-15"))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
    }

    public void testSingleValueFieldWithStringDatesWithCustomFormat() throws Exception {
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("date")
                        .format("yyyy-MM-dd")
                        .addUnboundedTo("2012-02-15")
                        .addRange("2012-02-15", "2012-03-15")
                        .addUnboundedFrom("2012-03-15"))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15-2012-03-15"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
    }

    public void testSingleValueFieldWithDateMath() throws Exception {
        DateTimeZone timezone = randomDateTimeZone();
        int timeZoneOffset = timezone.getOffset(date(2, 15));
        // if time zone is UTC (or equivalent), time zone suffix is "Z", else something like "+03:00", which we get with the "ZZ" format
        String feb15Suffix = timeZoneOffset == 0 ? "Z" : date(2,15, timezone).toString("ZZ");
        String mar15Suffix = timeZoneOffset == 0 ? "Z" : date(3,15, timezone).toString("ZZ");
        long expectedFirstBucketCount = timeZoneOffset < 0 ? 3L : 2L;

        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo("2012-02-15")
                        .addRange("2012-02-15", "2012-02-15||+1M")
                        .addUnboundedFrom("2012-02-15||+1M")
                        .timeZone(timezone))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000" + feb15Suffix));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15, timezone).toDateTime(DateTimeZone.UTC)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000" + feb15Suffix));
        assertThat(bucket.getDocCount(), equalTo(expectedFirstBucketCount));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000" + feb15Suffix +
                "-2012-03-15T00:00:00.000" + mar15Suffix));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15, timezone).toDateTime(DateTimeZone.UTC)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15, timezone).toDateTime(DateTimeZone.UTC)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000" + feb15Suffix));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000" + mar15Suffix));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000" + mar15Suffix + "-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15, timezone).toDateTime(DateTimeZone.UTC)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000" + mar15Suffix));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 2L - expectedFirstBucketCount));
    }

    public void testSingleValueFieldWithCustomKey() throws Exception {
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo("r1", date(2, 15))
                        .addRange("r2", date(2, 15), date(3, 15))
                        .addUnboundedFrom("r3", date(3, 15)))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("r1"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("r2"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("r3"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
    }

    /*
        Jan 2,      1
        Feb 2,      2
        Feb 15,     3
        Mar 2,      4
        Mar 15,     5
        Mar 23,     6
     */

    public void testSingleValuedFieldWithSubAggregation() throws Exception {
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo("r1", date(2, 15))
                        .addRange("r2", date(2, 15), date(3, 15))
                        .addUnboundedFrom("r3", date(3, 15))
                        .subAggregation(sum("sum").field("value")))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));
        assertThat(((InternalAggregation)range).getProperty("_bucket_count"), equalTo(3));
        Object[] propertiesKeys = (Object[]) ((InternalAggregation)range).getProperty("_key");
        Object[] propertiesDocCounts = (Object[]) ((InternalAggregation)range).getProperty("_count");
        Object[] propertiesCounts = (Object[]) ((InternalAggregation)range).getProperty("sum.value");

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("r1"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));
        Sum sum = bucket.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        assertThat(sum.getValue(), equalTo((double) 1 + 2));
        assertThat((String) propertiesKeys[0], equalTo("r1"));
        assertThat((long) propertiesDocCounts[0], equalTo(2L));
        assertThat((double) propertiesCounts[0], equalTo((double) 1 + 2));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("r2"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));
        sum = bucket.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        assertThat(sum.getValue(), equalTo((double) 3 + 4));
        assertThat((String) propertiesKeys[1], equalTo("r2"));
        assertThat((long) propertiesDocCounts[1], equalTo(2L));
        assertThat((double) propertiesCounts[1], equalTo((double) 3 + 4));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("r3"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
        sum = bucket.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        assertThat((String) propertiesKeys[2], equalTo("r3"));
        assertThat((long) propertiesDocCounts[2], equalTo(numDocs - 4L));
    }

    /*
        Jan 2,  Feb 3,      1
        Feb 2,  Mar 3,      2
        Feb 15, Mar 16,     3
        Mar 2,  Apr 3,      4
        Mar 15, Apr 16      5
        Mar 23, Apr 24      6
     */

    public void testMultiValuedField() throws Exception {
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("dates")
                        .addUnboundedTo(date(2, 15))
                        .addRange(date(2, 15), date(3, 15))
                        .addUnboundedFrom(date(3, 15)))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(3L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 2L));
    }

    /*
        Feb 2,  Mar 3,      1
        Mar 2,  Apr 3,      2
        Mar 15, Apr 16,     3
        Apr 2,  May 3,      4
        Apr 15, May 16      5
        Apr 23, May 24      6
     */

    public void testMultiValuedFieldWithValueScript() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("fieldname", "dates");
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .field("dates")
                                .script(new Script(ScriptType.INLINE, "mockscript", DateScriptMocksPlugin.DOUBLE_PLUS_ONE_MONTH, params))
                                .addUnboundedTo(date(2, 15)).addRange(date(2, 15), date(3, 15)).addUnboundedFrom(date(3, 15))).execute()
                .actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(1L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 1L));
    }

    /*
        Feb 2,  Mar 3,      1
        Mar 2,  Apr 3,      2
        Mar 15, Apr 16,     3
        Apr 2,  May 3,      4
        Apr 15, May 16      5
        Apr 23, May 24      6
     */

    public void testScriptSingleValue() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("fieldname", "date");
        SearchResponse response = client().prepareSearch("idx")
                .addAggregation(dateRange("range")
                        .script(new Script(ScriptType.INLINE, "mockscript", DateScriptMocksPlugin.EXTRACT_FIELD, params))
                        .addUnboundedTo(date(2, 15))
                        .addRange(date(2, 15), date(3, 15))
                        .addUnboundedFrom(date(3, 15)))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
    }

    /*
        Jan 2,  Feb 3,      1
        Feb 2,  Mar 3,      2
        Feb 15, Mar 16,     3
        Mar 2,  Apr 3,      4
        Mar 15, Apr 16      5
        Mar 23, Apr 24      6
     */

    public void testScriptMultiValued() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("fieldname", "dates");
        SearchResponse response = client()
                .prepareSearch("idx")
                .addAggregation(
                        dateRange("range").script(new Script(ScriptType.INLINE, "mockscript", DateScriptMocksPlugin.EXTRACT_FIELD, params))
                        .addUnboundedTo(date(2, 15)).addRange(date(2, 15), date(3, 15))
                        .addUnboundedFrom(date(3, 15))).execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(3L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 2L));
    }

    public void testUnmapped() throws Exception {
        client().admin().cluster().prepareHealth("idx_unmapped").setWaitForYellowStatus().execute().actionGet();

        SearchResponse response = client().prepareSearch("idx_unmapped")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo(date(2, 15))
                        .addRange(date(2, 15), date(3, 15))
                        .addUnboundedFrom(date(3, 15)))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(0L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(0L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(0L));
    }

    public void testUnmappedWithStringDates() throws Exception {
        SearchResponse response = client().prepareSearch("idx_unmapped")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo("2012-02-15")
                        .addRange("2012-02-15", "2012-03-15")
                        .addUnboundedFrom("2012-03-15"))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(0L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(0L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(0L));
    }

    public void testPartiallyUnmapped() throws Exception {
        SearchResponse response = client().prepareSearch("idx", "idx_unmapped")
                .addAggregation(dateRange("range")
                        .field("date")
                        .addUnboundedTo(date(2, 15))
                        .addRange(date(2, 15), date(3, 15))
                        .addUnboundedFrom(date(3, 15)))
                .execute().actionGet();

        assertSearchResponse(response);

        Range range = response.getAggregations().get("range");
        assertThat(range, notNullValue());
        assertThat(range.getName(), equalTo("range"));
        List<? extends Bucket> buckets = range.getBuckets();
        assertThat(buckets.size(), equalTo(3));

        Range.Bucket bucket = buckets.get(0);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), nullValue());
        assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
        assertThat(bucket.getFromAsString(), nullValue());
        assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(1);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
        assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
        assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getDocCount(), equalTo(2L));

        bucket = buckets.get(2);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
        assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
        assertThat(((DateTime) bucket.getTo()), nullValue());
        assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
        assertThat(bucket.getToAsString(), nullValue());
        assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
    }

    public void testEmptyAggregation() throws Exception {
        SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx")
                .setQuery(matchAllQuery())
                .addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0)
                        .subAggregation(dateRange("date_range").field("value").addRange("0-1", 0, 1)))
                .execute().actionGet();

        assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
        Histogram histo = searchResponse.getAggregations().get("histo");
        assertThat(histo, Matchers.notNullValue());
        Histogram.Bucket bucket = histo.getBuckets().get(1);
        assertThat(bucket, Matchers.notNullValue());

        Range dateRange = bucket.getAggregations().get("date_range");
        List<Range.Bucket> buckets = new ArrayList<>(dateRange.getBuckets());
        assertThat(dateRange, Matchers.notNullValue());
        assertThat(dateRange.getName(), equalTo("date_range"));
        assertThat(buckets.size(), is(1));
        assertThat((String) buckets.get(0).getKey(), equalTo("0-1"));
        assertThat(((DateTime) buckets.get(0).getFrom()).getMillis(), equalTo(0L));
        assertThat(((DateTime) buckets.get(0).getTo()).getMillis(), equalTo(1L));
        assertThat(buckets.get(0).getDocCount(), equalTo(0L));
        assertThat(buckets.get(0).getAggregations().asList().isEmpty(), is(true));
    }

    public void testNoRangesInQuery()  {
        try {
            client().prepareSearch("idx")
                .addAggregation(dateRange("my_date_range_agg").field("value"))
                .execute().actionGet();
            fail();
        } catch (SearchPhaseExecutionException spee){
            Throwable rootCause = spee.getCause().getCause();
            assertThat(rootCause, instanceOf(IllegalArgumentException.class));
            assertEquals(rootCause.getMessage(), "No [ranges] specified for the [my_date_range_agg] aggregation");
        }
    }

    /**
     * Make sure that a request using a script does not get cached and a request
     * not using a script does get cached.
     */
    public void testDontCacheScripts() throws Exception {
        assertAcked(prepareCreate("cache_test_idx").addMapping("type", "date", "type=date")
                .setSettings(Settings.builder().put("requests.cache.enable", true).put("number_of_shards", 1).put("number_of_replicas", 1))
                .get());
        indexRandom(true,
                client().prepareIndex("cache_test_idx", "type", "1")
                        .setSource(jsonBuilder().startObject().timeField("date", date(1, 1)).endObject()),
                client().prepareIndex("cache_test_idx", "type", "2")
                        .setSource(jsonBuilder().startObject().timeField("date", date(2, 1)).endObject()));

        // Make sure we are starting with a clear cache
        assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
                .getHitCount(), equalTo(0L));
        assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
                .getMissCount(), equalTo(0L));

        // Test that a request using a script does not get cached
        Map<String, Object> params = new HashMap<>();
        params.put("fieldname", "date");
        SearchResponse r = client().prepareSearch("cache_test_idx").setSize(0).addAggregation(dateRange("foo").field("date")
                .script(new Script(ScriptType.INLINE, "mockscript", DateScriptMocksPlugin.DOUBLE_PLUS_ONE_MONTH, params))
                .addRange(new DateTime(2012, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC), new DateTime(2013, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC)))
                .get();
        assertSearchResponse(r);

        assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
                .getHitCount(), equalTo(0L));
        assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
                .getMissCount(), equalTo(0L));

        // To make sure that the cache is working test that a request not using
        // a script is cached
        r = client().prepareSearch("cache_test_idx").setSize(0).addAggregation(dateRange("foo").field("date")
                .addRange(new DateTime(2012, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC), new DateTime(2013, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC)))
                .get();
        assertSearchResponse(r);

        assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
                .getHitCount(), equalTo(0L));
        assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
                .getMissCount(), equalTo(1L));
    }

    /**
     * Test querying ranges on date mapping specifying a format with to/from
     * values specified as Strings
     */
    public void testRangeWithFormatStringValue() throws Exception {
        String indexName = "dateformat_test_idx";
        assertAcked(prepareCreate(indexName).addMapping("type", "date", "type=date,format=strict_hour_minute_second"));
        indexRandom(true,
                client().prepareIndex(indexName, "type", "1").setSource(jsonBuilder().startObject().field("date", "00:16:40").endObject()),
                client().prepareIndex(indexName, "type", "2").setSource(jsonBuilder().startObject().field("date", "00:33:20").endObject()),
                client().prepareIndex(indexName, "type", "3").setSource(jsonBuilder().startObject().field("date", "00:50:00").endObject()));

        // using no format should work when to/from is compatible with format in
        // mapping
        SearchResponse searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(dateRange("date_range").field("date").addRange("00:16:40", "00:50:00").addRange("00:50:00", "01:06:40"))
                .get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        List<Range.Bucket> buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "00:16:40-00:50:00", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "00:50:00-01:06:40", 3000000L, 4000000L);

        // using different format should work when to/from is compatible with
        // format in aggregation
        searchResponse = client().prepareSearch(indexName).setSize(0).addAggregation(
                dateRange("date_range").field("date").addRange("00.16.40", "00.50.00").addRange("00.50.00", "01.06.40").format("HH.mm.ss"))
                .get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "00.16.40-00.50.00", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "00.50.00-01.06.40", 3000000L, 4000000L);

        // providing numeric input with format should work, but bucket keys are
        // different now
        searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(
                        dateRange("date_range").field("date").addRange(1000000, 3000000).addRange(3000000, 4000000).format("epoch_millis"))
                .get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "1000000-3000000", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "3000000-4000000", 3000000L, 4000000L);

        // providing numeric input without format should throw an exception
        Exception e = expectThrows(Exception.class, () -> client().prepareSearch(indexName).setSize(0)
                .addAggregation(dateRange("date_range").field("date").addRange(1000000, 3000000).addRange(3000000, 4000000)).get());
        Throwable cause = e.getCause();
        assertThat(cause, instanceOf(ElasticsearchParseException.class));
        assertEquals("failed to parse date field [1000000] with format [strict_hour_minute_second]", cause.getMessage());
    }

    /**
     * Test querying ranges on date mapping specifying a format with to/from
     * values specified as numeric value
     */
    public void testRangeWithFormatNumericValue() throws Exception {
        String indexName = "dateformat_numeric_test_idx";
        assertAcked(prepareCreate(indexName).addMapping("type", "date", "type=date,format=epoch_second"));
        indexRandom(true,
                client().prepareIndex(indexName, "type", "1").setSource(jsonBuilder().startObject().field("date", 1000).endObject()),
                client().prepareIndex(indexName, "type", "2").setSource(jsonBuilder().startObject().field("date", 2000).endObject()),
                client().prepareIndex(indexName, "type", "3").setSource(jsonBuilder().startObject().field("date", 3000).endObject()));

        // using no format should work when to/from is compatible with format in
        // mapping
        SearchResponse searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(dateRange("date_range").field("date").addRange(1000, 3000).addRange(3000, 4000)).get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        List<Bucket> buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L);

        // using no format should also work when and to/from are string values
        searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(dateRange("date_range").field("date").addRange("1000", "3000").addRange("3000", "4000")).get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L);

        // also e-notation should work, fractional parts should be truncated
        searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(dateRange("date_range").field("date").addRange(1.0e3, 3000.8123).addRange(3000.8123, 4.0e3)).get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L);

        // also e-notation and floats provided as string also be truncated (see: #14641)
        searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(dateRange("date_range").field("date").addRange("1.0e3", "3.0e3").addRange("3.0e3", "4.0e3")).get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L);

        searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(dateRange("date_range").field("date").addRange("1000.123", "3000.8").addRange("3000.8", "4000.3")).get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L);

        // using different format should work when to/from is compatible with
        // format in aggregation
        searchResponse = client().prepareSearch(indexName).setSize(0).addAggregation(
                dateRange("date_range").field("date").addRange("00.16.40", "00.50.00").addRange("00.50.00", "01.06.40").format("HH.mm.ss"))
                .get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "00.16.40-00.50.00", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "00.50.00-01.06.40", 3000000L, 4000000L);

        // providing different numeric input with format should work, but bucket
        // keys are different now
        searchResponse = client().prepareSearch(indexName).setSize(0)
                .addAggregation(
                        dateRange("date_range").field("date").addRange(1000000, 3000000).addRange(3000000, 4000000).format("epoch_millis"))
                .get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
        buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2);
        assertBucket(buckets.get(0), 2L, "1000000-3000000", 1000000L, 3000000L);
        assertBucket(buckets.get(1), 1L, "3000000-4000000", 3000000L, 4000000L);
    }

    private static List<Range.Bucket> checkBuckets(Range dateRange, String expectedAggName, long expectedBucketsSize) {
        assertThat(dateRange, Matchers.notNullValue());
        assertThat(dateRange.getName(), equalTo(expectedAggName));
        List<Range.Bucket> buckets = new ArrayList<>(dateRange.getBuckets());
        assertThat(buckets.size(), is(2));
        return buckets;
    }

    private static void assertBucket(Bucket bucket, long bucketSize, String expectedKey, long expectedFrom, long expectedTo) {
        assertThat(bucket.getDocCount(), equalTo(bucketSize));
        assertThat((String) bucket.getKey(), equalTo(expectedKey));
        assertThat(((DateTime) bucket.getFrom()).getMillis(), equalTo(expectedFrom));
        assertThat(((DateTime) bucket.getTo()).getMillis(), equalTo(expectedTo));
        assertThat(bucket.getAggregations().asList().isEmpty(), is(true));
    }
}