text:
package co.cask.cdap.test.app;

import co.cask.cdap.AppUsingNamespace;
import co.cask.cdap.ConfigTestApp;
import co.cask.cdap.api.app.Application;
import co.cask.cdap.api.common.Bytes;
import co.cask.cdap.api.dataset.DatasetProperties;
import co.cask.cdap.api.dataset.lib.CloseableIterator;
import co.cask.cdap.api.dataset.lib.FileSet;
import co.cask.cdap.api.dataset.lib.KeyValue;
import co.cask.cdap.api.dataset.lib.KeyValueTable;
import co.cask.cdap.api.dataset.lib.cube.AggregationFunction;
import co.cask.cdap.api.dataset.table.Get;
import co.cask.cdap.api.dataset.table.Put;
import co.cask.cdap.api.dataset.table.Row;
import co.cask.cdap.api.dataset.table.Scanner;
import co.cask.cdap.api.dataset.table.Table;
import co.cask.cdap.api.flow.flowlet.StreamEvent;
import co.cask.cdap.api.metrics.MetricDataQuery;
import co.cask.cdap.api.metrics.MetricTimeSeries;
import co.cask.cdap.api.metrics.RuntimeMetrics;
import co.cask.cdap.api.schedule.ScheduleSpecification;
import co.cask.cdap.api.workflow.WorkflowToken;
import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.common.utils.Tasks;
import co.cask.cdap.internal.app.runtime.schedule.Scheduler;
import co.cask.cdap.proto.Id;
import co.cask.cdap.proto.ProgramRunStatus;
import co.cask.cdap.proto.RunRecord;
import co.cask.cdap.proto.WorkflowTokenDetail;
import co.cask.cdap.proto.WorkflowTokenNodeDetail;
import co.cask.cdap.proto.artifact.AppRequest;
import co.cask.cdap.proto.artifact.ArtifactSummary;
import co.cask.cdap.test.ApplicationManager;
import co.cask.cdap.test.DataSetManager;
import co.cask.cdap.test.FlowManager;
import co.cask.cdap.test.MapReduceManager;
import co.cask.cdap.test.ServiceManager;
import co.cask.cdap.test.SlowTests;
import co.cask.cdap.test.SparkManager;
import co.cask.cdap.test.StreamManager;
import co.cask.cdap.test.WorkerManager;
import co.cask.cdap.test.WorkflowManager;
import co.cask.cdap.test.XSlowTests;
import co.cask.cdap.test.artifacts.AppWithPlugin;
import co.cask.cdap.test.artifacts.plugins.ToStringPlugin;
import co.cask.cdap.test.base.TestFrameworkTestBase;
import co.cask.common.http.HttpRequest;
import co.cask.common.http.HttpRequests;
import co.cask.common.http.HttpResponse;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 *
 */
@Category(SlowTests.class)
public class TestFrameworkTestRun extends TestFrameworkTestBase {
  private static final Logger LOG = LoggerFactory.getLogger(TestFrameworkTestRun.class);

  @ClassRule
  public static final TemporaryFolder TEMP_FOLDER = new TemporaryFolder();

  private final Id.Namespace testSpace = Id.Namespace.from("testspace");

  @Before
  public void setUp() throws Exception {
    createNamespace(testSpace);
  }

  @Test
  public void testInvalidAppWithDuplicateDatasets() throws Exception {
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "invalid-app", "1.0.0-SNAPSHOT");
    addAppArtifact(artifactId, AppWithDuplicateData.class);

    Id.Artifact pluginArtifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "test-plugin", "1.0.0-SNAPSHOT");
    addPluginArtifact(pluginArtifactId, artifactId, ToStringPlugin.class);

    Id.Application appId = Id.Application.from(Id.Namespace.DEFAULT, "InvalidApp");

    for (int choice = 4; choice > 0; choice /= 2) {
      try {
        AppRequest<AppWithDuplicateData.ConfigClass> createRequest = new AppRequest<>(
          new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()),
          new AppWithDuplicateData.ConfigClass((choice == 4), (choice == 2), (choice == 1)));
        deployApplication(appId, createRequest);
        // fail if we succeed with application deployment
        Assert.fail();
      } catch (IllegalStateException e) {
        // expected
      }
    }

    AppRequest<AppWithDuplicateData.ConfigClass> createRequest = new AppRequest<>(
      new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()),
      new AppWithDuplicateData.ConfigClass(false, false, false));
    deployApplication(appId, createRequest);
  }

  @Test
  public void testFlowRuntimeArguments() throws Exception {
    ApplicationManager applicationManager = deployApplication(FilterApp.class);
    Map<String, String> args = Maps.newHashMap();
    args.put("threshold", "10");
    applicationManager.getFlowManager("FilterFlow").start(args);

    StreamManager input = getStreamManager("input");
    input.send("1");
    input.send("11");

    ServiceManager serviceManager = applicationManager.getServiceManager("CountService").start();
    serviceManager.waitForStatus(true, 2, 1);

    Assert.assertEquals("1", new Gson().fromJson(
      callServiceGet(serviceManager.getServiceURL(), "result"), String.class));
  }

  @Test
  public void testNewFlowRuntimeArguments() throws Exception {
    ApplicationManager applicationManager = deployApplication(FilterAppWithNewFlowAPI.class);
    Map<String, String> args = Maps.newHashMap();
    args.put("threshold", "10");
    applicationManager.getFlowManager("FilterFlow").start(args);

    StreamManager input = getStreamManager("input");
    input.send("2");
    input.send("21");

    ServiceManager serviceManager = applicationManager.getServiceManager("CountService").start();
    serviceManager.waitForStatus(true, 2, 1);

    Assert.assertEquals("1", new Gson().fromJson(
      callServiceGet(serviceManager.getServiceURL(), "result"), String.class));
  }

  @Test
  public void testServiceManager() throws Exception {
    ApplicationManager applicationManager = deployApplication(FilterApp.class);
    final ServiceManager countService = applicationManager.getServiceManager("CountService");
    countService.setInstances(2);
    Assert.assertEquals(0, countService.getProvisionedInstances());
    Assert.assertEquals(2, countService.getRequestedInstances());
    Assert.assertFalse(countService.isRunning());

    List<RunRecord> history = countService.getHistory();
    Assert.assertEquals(0, history.size());

    countService.start();
    Assert.assertTrue(countService.isRunning());
    Assert.assertEquals(2, countService.getProvisionedInstances());

    // requesting with ProgramRunStatus.KILLED returns empty list
    history = countService.getHistory(ProgramRunStatus.KILLED);
    Assert.assertEquals(0, history.size());

    // requesting with either RUNNING or ALL will return one record
    Tasks.waitFor(1, new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        return countService.getHistory(ProgramRunStatus.RUNNING).size();
      }
    }, 5, TimeUnit.SECONDS);

    history = countService.getHistory(ProgramRunStatus.RUNNING);
    Assert.assertEquals(ProgramRunStatus.RUNNING, history.get(0).getStatus());

    history = countService.getHistory(ProgramRunStatus.ALL);
    Assert.assertEquals(1, history.size());
    Assert.assertEquals(ProgramRunStatus.RUNNING, history.get(0).getStatus());
  }

  @Test
  public void testNamespaceAvailableAtRuntime() throws Exception {
    ApplicationManager applicationManager = deployApplication(testSpace, AppUsingNamespace.class);
    ServiceManager serviceManager = applicationManager.getServiceManager(AppUsingNamespace.SERVICE_NAME);
    serviceManager.start();
    serviceManager.waitForStatus(true, 1, 10);

    URL serviceURL = serviceManager.getServiceURL(10, TimeUnit.SECONDS);
    Assert.assertEquals(testSpace.getId(), callServiceGet(serviceURL, "ns"));

    serviceManager.stop();
    serviceManager.waitForStatus(false, 1, 10);
  }

  @Test
  public void testAppConfigWithNull() throws Exception {
    testAppConfig(ConfigTestApp.NAME, deployApplication(ConfigTestApp.class), null);
  }

  @Test
  public void testAppConfig() throws Exception {
    ConfigTestApp.ConfigClass conf = new ConfigTestApp.ConfigClass("testStream", "testDataset");
    testAppConfig(ConfigTestApp.NAME, deployApplication(ConfigTestApp.class, conf), conf);
  }

  @Test
  public void testAppWithPlugin() throws Exception {
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "app-with-plugin", "1.0.0-SNAPSHOT");
    addAppArtifact(artifactId, AppWithPlugin.class);

    Id.Artifact pluginArtifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "test-plugin", "1.0.0-SNAPSHOT");
    addPluginArtifact(pluginArtifactId, artifactId, ToStringPlugin.class);

    Id.Application appId = Id.Application.from(Id.Namespace.DEFAULT, "AppWithPlugin");
    AppRequest createRequest = new AppRequest(
      new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()));

    ApplicationManager appManager = deployApplication(appId, createRequest);

    final WorkerManager workerManager = appManager.getWorkerManager(AppWithPlugin.WORKER);
    workerManager.start();
    workerManager.waitForStatus(false, 5, 1);
    Tasks.waitFor(false, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        return workerManager.getHistory(ProgramRunStatus.COMPLETED).isEmpty();
      }
    }, 5, TimeUnit.SECONDS, 10, TimeUnit.MILLISECONDS);

    final ServiceManager serviceManager = appManager.getServiceManager(AppWithPlugin.SERVICE);
    serviceManager.start();
    serviceManager.waitForStatus(true, 1, 10);
    URL serviceURL = serviceManager.getServiceURL(5, TimeUnit.SECONDS);
    callServiceGet(serviceURL, "dummy");
    serviceManager.stop();
    serviceManager.waitForStatus(false, 1, 10);
    Tasks.waitFor(false, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        return serviceManager.getHistory(ProgramRunStatus.KILLED).isEmpty();
      }
    }, 5, TimeUnit.SECONDS, 10, TimeUnit.MILLISECONDS);

    MapReduceManager mrManager = appManager.getMapReduceManager(AppWithPlugin.MAPREDUCE);
    mrManager.start();
    mrManager.waitForFinish(10, TimeUnit.MINUTES);
    List<RunRecord> runRecords = mrManager.getHistory();
    Assert.assertNotEquals(ProgramRunStatus.FAILED, runRecords.get(0).getStatus());

    // Testing Spark Plugins. First send some data to stream for the Spark program to process
    StreamManager streamManager = getStreamManager(AppWithPlugin.SPARK_STREAM);
    for (int i = 0; i < 5; i++) {
      streamManager.send("Message " + i);
    }

    SparkManager sparkManager = appManager.getSparkManager(AppWithPlugin.SPARK).start();
    sparkManager.waitForFinish(2, TimeUnit.MINUTES);

    // Verify the Spark result.
    DataSetManager<Table> dataSetManager = getDataset(AppWithPlugin.SPARK_TABLE);
    Table table = dataSetManager.get();
    Scanner scanner = table.scan(null, null);
    try {
      for (int i = 0; i < 5; i++) {
        Row row = scanner.next();
        Assert.assertNotNull(row);
        String expected = "Message " + i + " " + AppWithPlugin.TEST;
        Assert.assertEquals(expected, Bytes.toString(row.getRow()));
        Assert.assertEquals(expected, Bytes.toString(row.get(expected)));
      }
      // There shouldn't be any more rows in the table.
      Assert.assertNull(scanner.next());

    } finally {
      scanner.close();
    }
  }

  @Test
  public void testAppFromArtifact() throws Exception {
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "cfg-app", "1.0.0-SNAPSHOT");
    addAppArtifact(artifactId, ConfigTestApp.class);

    Id.Application appId = Id.Application.from(Id.Namespace.DEFAULT, "AppFromArtifact");
    AppRequest<ConfigTestApp.ConfigClass> createRequest = new AppRequest<>(
      new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()),
      new ConfigTestApp.ConfigClass("testStream", "testDataset")
    );
    ApplicationManager appManager = deployApplication(appId, createRequest);
    testAppConfig(appId.getId(), appManager, createRequest.getConfig());
  }

  private void testAppConfig(String appName, ApplicationManager appManager,
                             ConfigTestApp.ConfigClass conf) throws Exception {
    String streamName = conf == null ? ConfigTestApp.DEFAULT_STREAM : conf.getStreamName();
    String datasetName = conf == null ? ConfigTestApp.DEFAULT_TABLE : conf.getTableName();

    FlowManager flowManager = appManager.getFlowManager(ConfigTestApp.FLOW_NAME).start();
    StreamManager streamManager = getStreamManager(streamName);
    streamManager.send("abcd");
    streamManager.send("xyz");
    RuntimeMetrics metrics = flowManager.getFlowletMetrics(ConfigTestApp.FLOWLET_NAME);
    metrics.waitForProcessed(2, 1, TimeUnit.MINUTES);
    flowManager.stop();
    DataSetManager<KeyValueTable> dsManager = getDataset(datasetName);
    KeyValueTable table = dsManager.get();
    Assert.assertEquals("abcd", Bytes.toString(table.read(appName + ".abcd")));
    Assert.assertEquals("xyz", Bytes.toString(table.read(appName + ".xyz")));
  }

  @Category(SlowTests.class)
  @Test
  public void testMapperDatasetAccess() throws Exception {
    addDatasetInstance("keyValueTable", "table1").create();
    addDatasetInstance("keyValueTable", "table2").create();
    DataSetManager<KeyValueTable> tableManager = getDataset("table1");
    KeyValueTable inputTable = tableManager.get();
    inputTable.write("hello", "world");
    tableManager.flush();

    ApplicationManager appManager = deployApplication(DatasetWithMRApp.class);
    Map<String, String> argsForMR = ImmutableMap.of(DatasetWithMRApp.INPUT_KEY, "table1",
                                                    DatasetWithMRApp.OUTPUT_KEY, "table2");
    MapReduceManager mrManager = appManager.getMapReduceManager(DatasetWithMRApp.MAPREDUCE_PROGRAM).start(argsForMR);
    mrManager.waitForFinish(5, TimeUnit.MINUTES);
    appManager.stopAll();

    DataSetManager<KeyValueTable> outTableManager = getDataset("table2");
    KeyValueTable outputTable = outTableManager.get();
    Assert.assertEquals("world", Bytes.toString(outputTable.read("hello")));

    // Verify dataset metrics
    String readCountName = "system." + Constants.Metrics.Name.Dataset.READ_COUNT;
    String writeCountName = "system." + Constants.Metrics.Name.Dataset.WRITE_COUNT;
    Collection<MetricTimeSeries> metrics = getMetricsManager().query(
      new MetricDataQuery(
        0, System.currentTimeMillis() / 1000, Integer.MAX_VALUE,
        ImmutableMap.of(
          readCountName, AggregationFunction.SUM,
          writeCountName, AggregationFunction.SUM
        ),
        ImmutableMap.<String, String>of(),
        ImmutableList.<String>of()
      )
    );

    // Transform the collection of metrics into a map from metrics name to aggregated sum
    Map<String, Long> aggs = Maps.transformEntries(Maps.uniqueIndex(metrics, new Function<MetricTimeSeries, String>() {
      @Override
      public String apply(MetricTimeSeries input) {
        return input.getMetricName();
      }
    }), new Maps.EntryTransformer<String, MetricTimeSeries, Long>() {
      @Override
      public Long transformEntry(String key, MetricTimeSeries value) {
        Preconditions.checkArgument(value.getTimeValues().size() == 1,
                                    "Expected one value for aggregated sum for metrics %s", key);
        return value.getTimeValues().get(0).getValue();
      }
    });

    Assert.assertEquals(Long.valueOf(1), aggs.get(readCountName));
    Assert.assertEquals(Long.valueOf(1), aggs.get(writeCountName));
  }

  @Category(SlowTests.class)
  @Test
  public void testCustomActionDatasetAccess() throws Exception {
    addDatasetInstance("keyValueTable", DatasetWithCustomActionApp.CUSTOM_TABLE).create();
    addDatasetInstance("fileSet", DatasetWithCustomActionApp.CUSTOM_FILESET).create();

    ApplicationManager appManager = deployApplication(DatasetWithCustomActionApp.class);
    ServiceManager serviceManager = appManager.getServiceManager(DatasetWithCustomActionApp.CUSTOM_SERVICE).start();
    serviceManager.waitForStatus(true);

    WorkflowManager workflowManager = appManager.getWorkflowManager(DatasetWithCustomActionApp.CUSTOM_WORKFLOW).start();
    workflowManager.waitForFinish(2, TimeUnit.MINUTES);
    appManager.stopAll();

    DataSetManager<KeyValueTable> outTableManager = getDataset(DatasetWithCustomActionApp.CUSTOM_TABLE);
    KeyValueTable outputTable = outTableManager.get();

    Assert.assertEquals("world", Bytes.toString(outputTable.read("hello")));
    Assert.assertEquals("service", Bytes.toString(outputTable.read("hi")));

    DataSetManager<FileSet> outFileSetManager = getDataset(DatasetWithCustomActionApp.CUSTOM_FILESET);
    FileSet fs = outFileSetManager.get();
    try (InputStream in = fs.getLocation("test").getInputStream()) {
      Assert.assertEquals(42, in.read());
    }
  }

  @Category(XSlowTests.class)
  @Test
  public void testDeployWorkflowApp() throws Exception {
    ApplicationManager applicationManager = deployApplication(testSpace, AppWithSchedule.class);
    final WorkflowManager wfmanager = applicationManager.getWorkflowManager("SampleWorkflow");
    List<ScheduleSpecification> schedules = wfmanager.getSchedules();
    Assert.assertEquals(1, schedules.size());
    String scheduleName = schedules.get(0).getSchedule().getName();
    Assert.assertNotNull(scheduleName);
    Assert.assertFalse(scheduleName.isEmpty());
    wfmanager.getSchedule(scheduleName).resume();

    String status = wfmanager.getSchedule(scheduleName).status(200);
    Assert.assertEquals("SCHEDULED", status);

    // Make sure something ran before suspending
    Tasks.waitFor(true, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        return !wfmanager.getHistory().isEmpty();
      }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);

    wfmanager.getSchedule(scheduleName).suspend();
    waitForScheduleState(scheduleName, wfmanager, Scheduler.ScheduleState.SUSPENDED);

    // All runs should be completed
    Tasks.waitFor(true, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        for (RunRecord record : wfmanager.getHistory()) {
          if (record.getStatus() != ProgramRunStatus.COMPLETED) {
            return false;
          }
        }
        return true;
      }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);

    List<RunRecord> history = wfmanager.getHistory();
    int workflowRuns = history.size();
    Assert.assertTrue(workflowRuns > 0);

    //Sleep for some time and verify there are no more scheduled jobs after the suspend.
    TimeUnit.SECONDS.sleep(5);
    final int workflowRunsAfterSuspend = wfmanager.getHistory().size();
    Assert.assertEquals(workflowRuns, workflowRunsAfterSuspend);

    wfmanager.getSchedule(scheduleName).resume();

    //Check that after resume it goes to "SCHEDULED" state
    waitForScheduleState(scheduleName, wfmanager, Scheduler.ScheduleState.SCHEDULED);

    // Make sure new runs happens after resume
    Tasks.waitFor(true, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        return wfmanager.getHistory().size() > workflowRunsAfterSuspend;
      }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);

    //check scheduled state
    Assert.assertEquals("SCHEDULED", wfmanager.getSchedule(scheduleName).status(200));

    //check status of non-existent schedule
    Assert.assertEquals("NOT_FOUND", wfmanager.getSchedule("doesnt exist").status(404));

    //suspend the schedule
    wfmanager.getSchedule(scheduleName).suspend();

    //Check that after suspend it goes to "SUSPENDED" state
    waitForScheduleState(scheduleName, wfmanager, Scheduler.ScheduleState.SUSPENDED);

    // test workflow token while suspended
    String pid = history.get(0).getPid();
    WorkflowTokenDetail workflowToken = wfmanager.getToken(pid, WorkflowToken.Scope.SYSTEM, null);
    Assert.assertEquals(0, workflowToken.getTokenData().size());
    workflowToken = wfmanager.getToken(pid, null, null);
    Assert.assertEquals(2, workflowToken.getTokenData().size());

    // wait till workflow finishes execution
    waitForWorkflowStatus(wfmanager, ProgramRunStatus.COMPLETED);

    // verify workflow token after workflow completion
    WorkflowTokenNodeDetail workflowTokenAtNode =
      wfmanager.getTokenAtNode(pid, AppWithSchedule.DummyAction.class.getSimpleName(),
                               WorkflowToken.Scope.USER, "finished");
    Assert.assertEquals(true, Boolean.parseBoolean(workflowTokenAtNode.getTokenDataAtNode().get("finished")));
    workflowToken = wfmanager.getToken(pid, null, null);
    Assert.assertEquals(false, Boolean.parseBoolean(workflowToken.getTokenData().get("running").get(0).getValue()));
  }

  @Test
  public void testBatchStreamUpload() throws Exception {
    StreamManager batchStream = getStreamManager("batchStream");
    batchStream.createStream();
    String event1 = "this,is,some";
    String event2 = "test,csv,data";
    String event3 = "that,can,be,used,to,test";
    String event4 = "batch,upload,capability";
    String event5 = "for,streams in testbase";
    File testData = TEMP_FOLDER.newFile("test-stream-data.txt");
    try (FileWriter fileWriter = new FileWriter(testData);
         BufferedWriter out = new BufferedWriter(fileWriter)) {
      out.write(String.format("%s\n", event1));
      out.write(String.format("%s\n", event2));
      out.write(String.format("%s\n", event3));
      out.write(String.format("%s\n", event4));
      out.write(String.format("%s\n", event5));
    }

    // batch upload file containing 10 events
    batchStream.send(testData, "text/csv");
    // verify upload
    List<StreamEvent> uploadedEvents = batchStream.getEvents(0, System.currentTimeMillis(), 100);
    Assert.assertEquals(5, uploadedEvents.size());
    Assert.assertEquals(event1, Bytes.toString(uploadedEvents.get(0).getBody()));
    Assert.assertEquals(event2, Bytes.toString(uploadedEvents.get(1).getBody()));
    Assert.assertEquals(event3, Bytes.toString(uploadedEvents.get(2).getBody()));
    Assert.assertEquals(event4, Bytes.toString(uploadedEvents.get(3).getBody()));
    Assert.assertEquals(event5, Bytes.toString(uploadedEvents.get(4).getBody()));
  }

  private void waitForWorkflowStatus(final WorkflowManager wfmanager, ProgramRunStatus expected) throws Exception {
    Tasks.waitFor(expected, new Callable<ProgramRunStatus>() {
      @Override
      public ProgramRunStatus call() throws Exception {
        List<RunRecord> history = wfmanager.getHistory();
        RunRecord runRecord = history.get(history.size() - 1);
        return runRecord.getStatus();
      }
    }, 5, TimeUnit.SECONDS, 30, TimeUnit.MILLISECONDS);
  }

  private void waitForWorkflowRuns(final WorkflowManager wfmanager, int expected) throws Exception {
    Tasks.waitFor(expected, new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        return wfmanager.getHistory().size();
      }
    }, 5, TimeUnit.SECONDS, 30, TimeUnit.MILLISECONDS);
  }

  private void waitForScheduleState(final String scheduleId, final WorkflowManager wfmanager,
                                    Scheduler.ScheduleState expected) throws Exception {
    Tasks.waitFor(expected, new Callable<Scheduler.ScheduleState>() {
      @Override
      public Scheduler.ScheduleState call() throws Exception {
        return Scheduler.ScheduleState.valueOf(wfmanager.getSchedule(scheduleId).status(200));
      }
    }, 5, TimeUnit.SECONDS, 30, TimeUnit.MILLISECONDS);
  }

  @Category(XSlowTests.class)
  @Test(timeout = 240000)
  @Ignore
  // TODO: Investigate why this fails in Bamboo, but not locally
  public void testMultiInput() throws Exception {
    ApplicationManager applicationManager = deployApplication(JoinMultiStreamApp.class);
    FlowManager flowManager = applicationManager.getFlowManager("JoinMultiFlow").start();

    StreamManager s1 = getStreamManager("s1");
    StreamManager s2 = getStreamManager("s2");
    StreamManager s3 = getStreamManager("s3");

    s1.send("testing 1");
    s2.send("testing 2");
    s3.send("testing 3");

    RuntimeMetrics terminalMetrics = flowManager.getFlowletMetrics("Terminal");
    terminalMetrics.waitForProcessed(3, 60, TimeUnit.SECONDS);
    TimeUnit.SECONDS.sleep(1);

    ServiceManager queryManager = applicationManager.getServiceManager("QueryService").start();
    queryManager.waitForStatus(true, 2, 1);
    URL serviceURL = queryManager.getServiceURL();
    Gson gson = new Gson();

    Assert.assertEquals("testing 1", gson.fromJson(callServiceGet(serviceURL, "input1"), String.class));
    Assert.assertEquals("testing 2", gson.fromJson(callServiceGet(serviceURL, "input2"), String.class));
    Assert.assertEquals("testing 3", gson.fromJson(callServiceGet(serviceURL, "input3"), String.class));
  }

  @Category(XSlowTests.class)
  @Test(timeout = 360000)
  public void testApp() throws Exception {
    testApp(WordCountApp.class, "text");
  }

  @Category(SlowTests.class)
  @Test
  public void testGetServiceURL() throws Exception {
    ApplicationManager applicationManager = deployApplication(AppUsingGetServiceURL.class);
    ServiceManager centralServiceManager =
      applicationManager.getServiceManager(AppUsingGetServiceURL.CENTRAL_SERVICE).start();
    centralServiceManager.waitForStatus(true);

    WorkerManager pingingWorker = applicationManager.getWorkerManager(AppUsingGetServiceURL.PINGING_WORKER).start();
    pingingWorker.waitForStatus(true);

    // Test service's getServiceURL
    ServiceManager serviceManager = applicationManager.getServiceManager(AppUsingGetServiceURL.FORWARDING).start();
    String result = callServiceGet(serviceManager.getServiceURL(), "ping");
    String decodedResult = new Gson().fromJson(result, String.class);
    // Verify that the service was able to hit the CentralService and retrieve the answer.
    Assert.assertEquals(AppUsingGetServiceURL.ANSWER, decodedResult);

    result = callServiceGet(serviceManager.getServiceURL(), "read/" + AppUsingGetServiceURL.DATASET_KEY);
    decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppUsingGetServiceURL.ANSWER, decodedResult);

    serviceManager.stop();

    // Program manager is not notified when worker stops on its own, hence suppressing the exception.
    // JIRA - CDAP-3656
    try {
      pingingWorker.stop();
    } catch (Throwable e) {
      LOG.error("Got exception while stopping pinging worker", e);
    }
    pingingWorker.waitForStatus(false);

    centralServiceManager.stop();
    centralServiceManager.waitForStatus(false);
  }

  /**
   * Checks to ensure that a particular  {@param workerManager} has {@param expected} number of
   * instances while retrying every 50 ms for 15 seconds.
   *
   * @throws Exception if the worker does not have the specified number of instances after 15 seconds.
   */
  private void workerInstancesCheck(final WorkerManager workerManager, int expected) throws Exception {
    Tasks.waitFor(expected, new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        return workerManager.getInstances();
      }
    }, 15, TimeUnit.SECONDS);
  }

  /**
   * Checks to ensure that a particular key is present in a {@link KeyValueTable}
   * @param namespace {@link Id.Namespace}
   * @param datasetName name of the dataset
   * @param expectedKey expected key byte array
   *
   * @throws Exception if the key is not found even after 15 seconds of timeout
   */
  private void kvTableKeyCheck(final Id.Namespace namespace, final String datasetName, final byte[] expectedKey)
    throws Exception {
    Tasks.waitFor(true, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        DataSetManager<KeyValueTable> datasetManager = getDataset(namespace, datasetName);
        KeyValueTable kvTable = datasetManager.get();
        return kvTable.read(expectedKey) != null;
      }
    }, 15, TimeUnit.SECONDS);
  }

  @Category(SlowTests.class)
  @Test
  public void testWorkerInstances() throws Exception {
    ApplicationManager applicationManager = deployApplication(testSpace, AppUsingGetServiceURL.class);
    WorkerManager workerManager = applicationManager.getWorkerManager(AppUsingGetServiceURL.PINGING_WORKER).start();
    workerManager.waitForStatus(true);

    // Should be 5 instances when first started.
    workerInstancesCheck(workerManager, 5);

    // Test increasing instances.
    workerManager.setInstances(10);
    workerInstancesCheck(workerManager, 10);

    // Test decreasing instances.
    workerManager.setInstances(2);
    workerInstancesCheck(workerManager, 2);

    // Test requesting same number of instances.
    workerManager.setInstances(2);
    workerInstancesCheck(workerManager, 2);

    WorkerManager lifecycleWorkerManager =
      applicationManager.getWorkerManager(AppUsingGetServiceURL.LIFECYCLE_WORKER).start();
    lifecycleWorkerManager.waitForStatus(true);

    // Set 5 instances for the LifecycleWorker
    lifecycleWorkerManager.setInstances(5);
    workerInstancesCheck(lifecycleWorkerManager, 5);

    // Make sure all the keys have been written before stopping the worker
    for (int i = 0; i < 5; i++) {
      kvTableKeyCheck(testSpace, AppUsingGetServiceURL.WORKER_INSTANCES_DATASET,
                      Bytes.toBytes(String.format("init.%d", i)));
    }

    lifecycleWorkerManager.stop();
    lifecycleWorkerManager.waitForStatus(false);

    if (workerManager.isRunning()) {
      workerManager.stop();
    }
    workerManager.waitForStatus(false);

    // Should be same instances after being stopped.
    workerInstancesCheck(lifecycleWorkerManager, 5);
    workerInstancesCheck(workerManager, 2);

    // Assert the LifecycleWorker dataset writes
    // 3 workers should have started with 3 total instances. 2 more should later start with 5 total instances.
    assertWorkerDatasetWrites(Bytes.toBytes("init"),
                              Bytes.stopKeyForPrefix(Bytes.toBytes("init.2")), 3, 3);
    assertWorkerDatasetWrites(Bytes.toBytes("init.3"),
                              Bytes.stopKeyForPrefix(Bytes.toBytes("init")), 2, 5);

    // Test that the worker had 5 instances when stopped, and each knew that there were 5 instances
    byte[] startRow = Bytes.toBytes("stop");
    assertWorkerDatasetWrites(startRow, Bytes.stopKeyForPrefix(startRow), 5, 5);
  }

  private void assertWorkerDatasetWrites(byte[] startRow, byte[] endRow,
                                         int expectedCount, int expectedTotalCount) throws Exception {
    DataSetManager<KeyValueTable> datasetManager =
      getDataset(testSpace, AppUsingGetServiceURL.WORKER_INSTANCES_DATASET);
    KeyValueTable instancesTable = datasetManager.get();
    CloseableIterator<KeyValue<byte[], byte[]>> instancesIterator = instancesTable.scan(startRow, endRow);
    try {
      List<KeyValue<byte[], byte[]>> workerInstances = Lists.newArrayList(instancesIterator);
      // Assert that the worker starts with expectedCount instances
      Assert.assertEquals(expectedCount, workerInstances.size());
      // Assert that each instance of the worker knows the total number of instances
      for (KeyValue<byte[], byte[]> keyValue : workerInstances) {
        Assert.assertEquals(expectedTotalCount, Bytes.toInt(keyValue.getValue()));
      }
    } finally {
      instancesIterator.close();
    }
  }

  // AppFabricClient returns IllegalStateException if the app fails to deploy
  @Category(SlowTests.class)
  @Test(expected = IllegalStateException.class)
  public void testServiceWithInvalidHandler() throws Exception {
    deployApplication(AppWithInvalidHandler.class);
  }

  @Category(SlowTests.class)
  @Test
  public void testAppWithWorker() throws Exception {
    ApplicationManager applicationManager = deployApplication(testSpace, AppWithWorker.class);
    LOG.info("Deployed.");
    WorkerManager manager = applicationManager.getWorkerManager(AppWithWorker.WORKER).start();

    // Wait for initialize and run states
    Tasks.waitFor(true, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        DataSetManager<KeyValueTable> dataSetManager = getDataset(testSpace, AppWithWorker.DATASET);
        KeyValueTable table = dataSetManager.get();
        return AppWithWorker.INITIALIZE.equals(Bytes.toString(table.read(AppWithWorker.INITIALIZE))) &&
          AppWithWorker.RUN.equals(Bytes.toString(table.read(AppWithWorker.RUN)));
      }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);

    manager.stop();
    applicationManager.stopAll();

    // Wait for stop state
    Tasks.waitFor(true, new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        DataSetManager<KeyValueTable> dataSetManager = getDataset(testSpace, AppWithWorker.DATASET);
        KeyValueTable table = dataSetManager.get();
        return AppWithWorker.STOP.equals(Bytes.toString(table.read(AppWithWorker.STOP)));
      }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
  }

  @Test
  public void testWorkerStop() throws Exception {
    // Test to make sure the worker program's status goes to stopped after the run method finishes
    ApplicationManager manager = deployApplication(NoOpWorkerApp.class);
    WorkerManager workerManager = manager.getWorkerManager("NoOpWorker");
    workerManager.start();
    workerManager.waitForStatus(false, 5, 1);
  }

  @Category(SlowTests.class)
  @Test
  public void testAppWithServices() throws Exception {
    ApplicationManager applicationManager = deployApplication(AppWithServices.class);
    LOG.info("Deployed.");
    ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.SERVICE_NAME).start();
    serviceManager.waitForStatus(true);

    LOG.info("Service Started");

    URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
    Assert.assertNotNull(serviceURL);

    // Call the ping endpoint
    URL url = new URL(serviceURL, "ping2");
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());

    // Call the failure endpoint
    url = new URL(serviceURL, "failure");
    request = HttpRequest.get(url).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(500, response.getResponseCode());
    Assert.assertTrue(response.getResponseBodyAsString().contains("Exception"));

    // Call the verify ClassLoader endpoint
    url = new URL(serviceURL, "verifyClassLoader");
    request = HttpRequest.get(url).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());

    RuntimeMetrics serviceMetrics = serviceManager.getMetrics();
    serviceMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
    Assert.assertEquals(3, serviceMetrics.getInput());
    Assert.assertEquals(2, serviceMetrics.getProcessed());
    Assert.assertEquals(1, serviceMetrics.getException());

    // in the AppWithServices the handlerName is same as the serviceName - "ServerService" handler
    RuntimeMetrics handlerMetrics = getMetricsManager().getServiceHandlerMetrics(Id.Namespace.DEFAULT.getId(),
                                                                                AppWithServices.APP_NAME,
                                                                                AppWithServices.SERVICE_NAME,
                                                                                AppWithServices.SERVICE_NAME);
    handlerMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
    Assert.assertEquals(3, handlerMetrics.getInput());
    Assert.assertEquals(2, handlerMetrics.getProcessed());
    Assert.assertEquals(1, handlerMetrics.getException());

    // we can verify metrics, by adding getServiceMetrics in MetricsManager and then disabling the system scope test in
    // TestMetricsCollectionService

    LOG.info("DatasetUpdateService Started");
    Map<String, String> args
      = ImmutableMap.of(AppWithServices.WRITE_VALUE_RUN_KEY, AppWithServices.DATASET_TEST_VALUE,
                        AppWithServices.WRITE_VALUE_STOP_KEY, AppWithServices.DATASET_TEST_VALUE_STOP);
    ServiceManager datasetWorkerServiceManager = applicationManager
      .getServiceManager(AppWithServices.DATASET_WORKER_SERVICE_NAME).start(args);
    WorkerManager datasetWorker =
      applicationManager.getWorkerManager(AppWithServices.DATASET_UPDATE_WORKER).start(args);
    datasetWorkerServiceManager.waitForStatus(true);

    ServiceManager noopManager = applicationManager.getServiceManager("NoOpService").start();
    serviceManager.waitForStatus(true, 2, 1);

    String result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY);
    String decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE, decodedResult);

    handlerMetrics = getMetricsManager().getServiceHandlerMetrics(Id.Namespace.DEFAULT.getId(),
                                                                  AppWithServices.APP_NAME,
                                                                  "NoOpService",
                                                                  "NoOpHandler");
    handlerMetrics.waitForinput(1, 5, TimeUnit.SECONDS);
    Assert.assertEquals(1, handlerMetrics.getInput());
    Assert.assertEquals(1, handlerMetrics.getProcessed());
    Assert.assertEquals(0, handlerMetrics.getException());

    // Test that a service can discover another service
    String path = String.format("discover/%s/%s",
                                AppWithServices.APP_NAME, AppWithServices.DATASET_WORKER_SERVICE_NAME);
    url = new URL(serviceURL, path);
    request = HttpRequest.get(url).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());

    datasetWorker.stop();
    datasetWorkerServiceManager.stop();
    datasetWorkerServiceManager.waitForStatus(false);
    LOG.info("DatasetUpdateService Stopped");
    serviceManager.stop();
    serviceManager.waitForStatus(false);
    LOG.info("ServerService Stopped");

    result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP);
    decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP, decodedResult);

    result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP_2);
    decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP_2, decodedResult);
  }

  @Test
  public void testTransactionHandlerService() throws Exception {
    ApplicationManager applicationManager = deployApplication(testSpace, AppWithServices.class);
    LOG.info("Deployed.");

    ServiceManager serviceManager =
      applicationManager.getServiceManager(AppWithServices.TRANSACTIONS_SERVICE_NAME).start();
    serviceManager.waitForStatus(true);
    LOG.info("Service Started");

    final URL baseUrl = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
    Assert.assertNotNull(baseUrl);

    // Make a request to write in a separate thread and wait for it to return.
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<Integer> requestFuture = executorService.submit(new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        try {
          URL url = new URL(String.format("%s/write/%s/%s/%d",
                                          baseUrl,
                                          AppWithServices.DATASET_TEST_KEY,
                                          AppWithServices.DATASET_TEST_VALUE,
                                          10000));
          HttpRequest request = HttpRequest.get(url).build();
          HttpResponse response = HttpRequests.execute(request);
          return response.getResponseCode();
        } catch (Exception e) {
          LOG.error("Request thread got exception.", e);
          throw Throwables.propagate(e);
        }
      }
    });

    // The dataset should not be written by the time this request is made, since the transaction to write
    // has not been committed yet.
    URL url = new URL(String.format("%s/read/%s", baseUrl, AppWithServices.DATASET_TEST_KEY));
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(204, response.getResponseCode());

    // Wait for the transaction to commit.
    Integer writeStatusCode = requestFuture.get();
    Assert.assertEquals(200, writeStatusCode.intValue());

    // Make the same request again. By now the transaction should've completed.
    request = HttpRequest.get(url).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());
    Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE,
                        new Gson().fromJson(response.getResponseBodyAsString(), String.class));

    executorService.shutdown();
    serviceManager.stop();
    serviceManager.waitForStatus(false);

    DataSetManager<KeyValueTable> dsManager = getDataset(testSpace, AppWithServices.TRANSACTIONS_DATASET_NAME);
    String value = Bytes.toString(dsManager.get().read(AppWithServices.DESTROY_KEY));
    Assert.assertEquals(AppWithServices.VALUE, value);
  }

  // todo: passing stream name as a workaround for not cleaning up streams during reset()
  private void testApp(Class<? extends Application> app, String streamName) throws Exception {

    ApplicationManager applicationManager = deployApplication(app);
    FlowManager flowManager = applicationManager.getFlowManager("WordCountFlow").start();

    // Send some inputs to streams
    StreamManager streamManager = getStreamManager(streamName);
    for (int i = 0; i < 100; i++) {
      streamManager.send(ImmutableMap.of("title", "title " + i), "testing message " + i);
    }

    // Check the flowlet metrics
    RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("CountByField");

    flowletMetrics.waitForProcessed(500, 10, TimeUnit.SECONDS);
    Assert.assertEquals(0L, flowletMetrics.getException());

    // Query the result
    ServiceManager serviceManager = applicationManager.getServiceManager("WordFrequency").start();
    serviceManager.waitForStatus(true, 2, 1);

    // Verify the query result
    Type resultType = new TypeToken<Map<String, Long>>() { }.getType();
    Map<String, Long> result = new Gson().fromJson(
      callServiceGet(serviceManager.getServiceURL(), "wordfreq/" + streamName + ":testing"), resultType);
    Assert.assertNotNull(result);
    Assert.assertEquals(100L, result.get(streamName + ":testing").longValue());

    // check the metrics
    RuntimeMetrics serviceMetrics = serviceManager.getMetrics();
    serviceMetrics.waitForProcessed(1, 5, TimeUnit.SECONDS);
    Assert.assertEquals(0L, serviceMetrics.getException());

    // Run mapreduce job
    MapReduceManager mrManager = applicationManager.getMapReduceManager("countTotal").start();
    mrManager.waitForFinish(1800L, TimeUnit.SECONDS);

    long totalCount = Long.valueOf(callServiceGet(serviceManager.getServiceURL(), "total"));
    // every event has 5 tokens
    Assert.assertEquals(5 * 100L, totalCount);

    // Run mapreduce from stream
    mrManager = applicationManager.getMapReduceManager("countFromStream").start();
    mrManager.waitForFinish(120L, TimeUnit.SECONDS);

    totalCount = Long.valueOf(callServiceGet(serviceManager.getServiceURL(), "stream_total"));
    // The stream MR only consume the body, not the header.
    Assert.assertEquals(3 * 100L, totalCount);

    DataSetManager<MyKeyValueTableDefinition.KeyValueTable> mydatasetManager = getDataset("mydataset");
    Assert.assertEquals(100L, Long.valueOf(mydatasetManager.get().get("title:title")).longValue());

    // also test the deprecated version of getDataset(). This can be removed when we remove the method
    mydatasetManager = getDataset("mydataset");
    Assert.assertEquals(100L, Long.valueOf(mydatasetManager.get().get("title:title")).longValue());
  }

  @Category(SlowTests.class)
  @Test
  public void testGenerator() throws InterruptedException, IOException, TimeoutException {
    ApplicationManager applicationManager = deployApplication(testSpace, GenSinkApp2.class);
    FlowManager flowManager = applicationManager.getFlowManager("GenSinkFlow").start();

    // Check the flowlet metrics
    RuntimeMetrics genMetrics = flowManager.getFlowletMetrics("GenFlowlet");

    RuntimeMetrics sinkMetrics = flowManager.getFlowletMetrics("SinkFlowlet");

    RuntimeMetrics batchSinkMetrics = flowManager.getFlowletMetrics("BatchSinkFlowlet");

    // Generator generators 99 events + 99 batched events
    sinkMetrics.waitFor("system.process.events.in", 198, 5, TimeUnit.SECONDS);
    sinkMetrics.waitForProcessed(198, 5, TimeUnit.SECONDS);
    Assert.assertEquals(0L, sinkMetrics.getException());

    // Batch sink only get the 99 batch events
    batchSinkMetrics.waitFor("system.process.events.in", 99, 5, TimeUnit.SECONDS);
    batchSinkMetrics.waitForProcessed(99, 5, TimeUnit.SECONDS);
    Assert.assertEquals(0L, batchSinkMetrics.getException());

    Assert.assertEquals(1L, genMetrics.getException());
  }

  @Test
  public void testAppRedeployKeepsData() throws Exception {
    deployApplication(testSpace, AppWithTable.class);
    DataSetManager<Table> myTableManager = getDataset(testSpace, "my_table");
    myTableManager.get().put(new Put("key1", "column1", "value1"));
    myTableManager.flush();

    // Changes should be visible to other instances of datasets
    DataSetManager<Table> myTableManager2 = getDataset(testSpace, "my_table");
    Assert.assertEquals("value1", myTableManager2.get().get(new Get("key1", "column1")).getString("column1"));

    // Even after redeploy of an app: changes should be visible to other instances of datasets
    deployApplication(AppWithTable.class);
    DataSetManager<Table> myTableManager3 = getDataset(testSpace, "my_table");
    Assert.assertEquals("value1", myTableManager3.get().get(new Get("key1", "column1")).getString("column1"));

    // Calling commit again (to test we can call it multiple times)
    myTableManager.get().put(new Put("key1", "column1", "value2"));
    myTableManager.flush();

    Assert.assertEquals("value1", myTableManager3.get().get(new Get("key1", "column1")).getString("column1"));
  }

  @Test(timeout = 60000L)
  public void testFlowletMetricsReset() throws Exception {
    ApplicationManager appManager = deployApplication(DataSetInitApp.class);
    FlowManager flowManager = appManager.getFlowManager("DataSetFlow").start();
    RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("Consumer");
    flowletMetrics.waitForProcessed(1, 5, TimeUnit.SECONDS);
    flowManager.stop();
    Assert.assertEquals(1, flowletMetrics.getProcessed());
    getMetricsManager().resetAll();
    // check the metrics were deleted after reset
    Assert.assertEquals(0, flowletMetrics.getProcessed());
  }

  @Test(timeout = 60000L)
  public void testFlowletInitAndSetInstances() throws Exception {
    ApplicationManager appManager = deployApplication(testSpace, DataSetInitApp.class);
    FlowManager flowManager = appManager.getFlowManager("DataSetFlow").start();

    RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("Consumer");

    flowletMetrics.waitForProcessed(1, 5, TimeUnit.SECONDS);

    String generator = "Generator";
    Assert.assertEquals(1, flowManager.getFlowletInstances(generator));
    // Now change generator to 3 instances
    flowManager.setFlowletInstances(generator, 3);
    Assert.assertEquals(3, flowManager.getFlowletInstances(generator));

    // Now should have 3 processed from the consumer flowlet
    flowletMetrics.waitForProcessed(3, 10, TimeUnit.SECONDS);

    // Now reset to 1 instances
    flowManager.setFlowletInstances(generator, 1);
    Assert.assertEquals(1, flowManager.getFlowletInstances(generator));

    // Shouldn't have new item
    TimeUnit.SECONDS.sleep(3);
    Assert.assertEquals(3, flowletMetrics.getProcessed());

    // Now set to 2 instances again. Since there is a new instance, expect one new item emitted
    flowManager.setFlowletInstances(generator, 2);
    Assert.assertEquals(2, flowManager.getFlowletInstances(generator));
    flowletMetrics.waitForProcessed(4, 10, TimeUnit.SECONDS);

    flowManager.stop();

    DataSetManager<Table> dataSetManager = getDataset(testSpace, "conf");
    Table confTable = dataSetManager.get();

    Assert.assertEquals("generator", confTable.get(new Get("key", "column")).getString("column"));

    dataSetManager.flush();
  }

  @Test(timeout = 60000L)
  public void testAppWithAutoDeployDatasetModule() throws Exception {
    testAppWithDataset(AppsWithDataset.AppWithAutoDeploy.class, "MyService");
  }

  @Test(timeout = 60000L)
  public void testAppWithAutoDeployDataset() throws Exception {
    deployDatasetModule("my-kv", AppsWithDataset.KeyValueTableDefinition.Module.class);
    // we should be fine if module is already there. Deploy of module should not happen
    testAppWithDataset(AppsWithDataset.AppWithAutoDeploy.class, "MyService");
  }

  @Test(timeout = 60000L)
  public void testAppWithAutoCreateDataset() throws Exception {
    deployDatasetModule("my-kv", AppsWithDataset.KeyValueTableDefinition.Module.class);
    testAppWithDataset(AppsWithDataset.AppWithAutoCreate.class, "MyService");
  }

  @Test(timeout = 60000L)
  public void testAppWithExistingDataset() throws Exception {
    deployDatasetModule("my-kv", AppsWithDataset.KeyValueTableDefinition.Module.class);
    addDatasetInstance("myKeyValueTable", "myTable", DatasetProperties.EMPTY).create();
    testAppWithDataset(AppsWithDataset.AppWithExisting.class, "MyService");
  }

  @Test(timeout = 60000L)
  public void testAppWithExistingDatasetInjectedByAnnotation() throws Exception {
    deployDatasetModule("my-kv", AppsWithDataset.KeyValueTableDefinition.Module.class);
    addDatasetInstance("myKeyValueTable", "myTable", DatasetProperties.EMPTY).create();
    testAppWithDataset(AppsWithDataset.AppUsesAnnotation.class, "MyServiceWithUseDataSetAnnotation");
  }

  @Test(timeout = 60000L)
  public void testDatasetWithoutApp() throws Exception {
    // TODO: Although this has nothing to do with this testcase, deploying a dummy app to create the default namespace
    deployApplication(testSpace, DummyApp.class);
    deployDatasetModule(testSpace, "my-kv", AppsWithDataset.KeyValueTableDefinition.Module.class);
    addDatasetInstance(testSpace, "myKeyValueTable", "myTable", DatasetProperties.EMPTY).create();
    DataSetManager<AppsWithDataset.KeyValueTableDefinition.KeyValueTable> dataSetManager =
      getDataset(testSpace, "myTable");
    AppsWithDataset.KeyValueTableDefinition.KeyValueTable kvTable = dataSetManager.get();
    kvTable.put("test", "hello");
    dataSetManager.flush();
    Assert.assertEquals("hello", dataSetManager.get().get("test"));
  }

  @Test(timeout = 60000L)
  public void testAppWithAutoDeployDatasetType() throws Exception {
    testAppWithDataset(AppsWithDataset.AppWithAutoDeployType.class, "MyService");
  }

  @Test(timeout = 60000L)
  public void testAppWithAutoDeployDatasetTypeShortcut() throws Exception {
    testAppWithDataset(AppsWithDataset.AppWithAutoDeployTypeShortcut.class, "MyService");
  }

  private void testAppWithDataset(Class<? extends Application> app, String serviceName) throws Exception {
    ApplicationManager applicationManager = deployApplication(app);
    // Query the result
    ServiceManager serviceManager = applicationManager.getServiceManager(serviceName).start();
    serviceManager.waitForStatus(true, 2, 1);
    callServicePut(serviceManager.getServiceURL(), "key1", "value1");
    String response = callServiceGet(serviceManager.getServiceURL(), "key1");
    Assert.assertEquals("value1", new Gson().fromJson(response, String.class));
  }

  @Test(timeout = 90000L)
  public void testSQLQuery() throws Exception {
    // Deploying app makes sure that the default namespace is available.
    deployApplication(testSpace, DummyApp.class);
    deployDatasetModule(testSpace, "my-kv", AppsWithDataset.KeyValueTableDefinition.Module.class);
    deployApplication(testSpace, AppsWithDataset.AppWithAutoCreate.class);
    DataSetManager<AppsWithDataset.KeyValueTableDefinition.KeyValueTable> myTableManager =
      getDataset(testSpace, "myTable");
    AppsWithDataset.KeyValueTableDefinition.KeyValueTable kvTable = myTableManager.get();
    kvTable.put("a", "1");
    kvTable.put("b", "2");
    kvTable.put("c", "1");
    myTableManager.flush();

    try (
      Connection connection = getQueryClient(testSpace);
      ResultSet results = connection.prepareStatement("select first from dataset_mytable where second = '1'")
                                    .executeQuery()
    ) {
      // run a query over the dataset
      Assert.assertTrue(results.next());
      Assert.assertEquals("a", results.getString(1));
      Assert.assertTrue(results.next());
      Assert.assertEquals("c", results.getString(1));
      Assert.assertFalse(results.next());
    }
  }

  @Category(XSlowTests.class)
  @Test
  public void testByteCodeClassLoader() throws Exception {
    // This test verify bytecode generated classes ClassLoading

    ApplicationManager appManager = deployApplication(testSpace, ClassLoaderTestApp.class);
    FlowManager flowManager = appManager.getFlowManager("BasicFlow").start();

    // Wait for at least 10 records being generated
    RuntimeMetrics flowMetrics = flowManager.getFlowletMetrics("Sink");
    flowMetrics.waitForProcessed(10, 5000, TimeUnit.MILLISECONDS);
    flowManager.stop();

    ServiceManager serviceManager = appManager.getServiceManager("RecordQuery").start();
    URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
    Assert.assertNotNull(serviceURL);

    // Query record
    URL url = new URL(serviceURL, "query?type=public");
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());

    long count = Long.parseLong(response.getResponseBodyAsString());
    serviceManager.stop();

    // Verify the record count with dataset
    DataSetManager<KeyValueTable> recordsManager = getDataset(testSpace, "records");
    KeyValueTable records = recordsManager.get();
    Assert.assertTrue(count == Bytes.toLong(records.read("PUBLIC")));
  }

  private String callServiceGet(URL serviceURL, String path) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(serviceURL.toString() + path).openConnection();
    try (
      BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8))
    ) {
      Assert.assertEquals(200, connection.getResponseCode());
      return reader.readLine();
    }
  }

  private String callServicePut(URL serviceURL, String path, String body) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(serviceURL.toString() + path).openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("PUT");
    try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream())) {
      out.write(body);
    }
    try (
      BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8))
    ) {
      Assert.assertEquals(200, connection.getResponseCode());
      return reader.readLine();
    }
  }
}


text:
@interface ThreePushViewController : UIViewController

@end

redpajama_set_name: RedPajamaGithub


text:
# - Try to find the Intel Math Kernel Library
#   Forked from: https://github.com/openmeeg/openmeeg/blob/master/macros/FindMKL.cmake
# Once done this will define
#
# MKL_FOUND - system has MKL
# MKL_ROOT_DIR - path to the MKL base directory
# MKL_INCLUDE_DIR - the MKL include directory
# MKL_LIBRARIES - MKL libraries
#
# There are few sets of libraries:
# Array indexes modes:
# LP - 32 bit indexes of arrays
# ILP - 64 bit indexes of arrays
# Threading:
# SEQUENTIAL - no threading
# INTEL - Intel threading library
# GNU - GNU threading library
# MPI support
# NOMPI - no MPI support
# INTEL - Intel MPI library
# OPEN - Open MPI library
# SGI - SGI MPT Library

set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")

# linux
if(UNIX AND NOT APPLE)
    if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
        set(MKL_ARCH_DIR "intel64")
    else()
        set(MKL_ARCH_DIR "ia32")
    endif()
endif()

# macos
if(APPLE)
    set(MKL_ARCH_DIR "em64t")
endif()

IF(FORCE_BUILD_32BITS)
    set(MKL_ARCH_DIR "32")
ENDIF()

if (WIN32)
    if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
        set(MKL_ARCH_DIR "intel64")
    else()
        set(MKL_ARCH_DIR "ia32")
    endif()
endif()

set (MKL_THREAD_VARIANTS SEQUENTIAL GNUTHREAD INTELTHREAD)
set (MKL_MODE_VARIANTS ILP LP)
set (MKL_MPI_VARIANTS NOMPI INTELMPI OPENMPI SGIMPT)

find_path(MKL_ROOT_DIR
    include/mkl_cblas.h
    PATHS
    $ENV{MKLDIR}
    /opt/intel/mkl/*/
    /opt/intel/cmkl/*/
    /Library/Frameworks/Intel_MKL.framework/Versions/Current/lib/universal
    "Program Files (x86)/Intel/ComposerXE-2011/mkl"
)

#MESSAGE("MKL_ROOT_DIR : ${MKL_ROOT_DIR}") # for debug

find_path(MKL_INCLUDE_DIR
  mkl_cblas.h
  PATHS
    ${MKL_ROOT_DIR}/include
    ${INCLUDE_INSTALL_DIR}
)

find_path(MKL_FFTW_INCLUDE_DIR
  fftw3.h
  PATH_SUFFIXES fftw
  PATHS
    ${MKL_ROOT_DIR}/include
    ${INCLUDE_INSTALL_DIR}
  NO_DEFAULT_PATH
)

find_library(MKL_CORE_LIBRARY
  mkl_core
  PATHS
    ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
    ${MKL_ROOT_DIR}/lib/
)

# Threading libraries
find_library(MKL_SEQUENTIAL_LIBRARY
  mkl_sequential
  PATHS
    ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
    ${MKL_ROOT_DIR}/lib/
)

find_library(MKL_INTELTHREAD_LIBRARY
  mkl_intel_thread
  PATHS
    ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
    ${MKL_ROOT_DIR}/lib/
)

#find_library(MKL_GNUTHREAD_LIBRARY
#  mkl_gnu_thread
#  PATHS
#    ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
#    ${MKL_ROOT_DIR}/lib/
#)

# Intel Libraries
IF("${MKL_ARCH_DIR}" STREQUAL "32")
    find_library(MKL_LP_LIBRARY
      mkl_intel
      PATHS
        ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
        ${MKL_ROOT_DIR}/lib/
    )

    find_library(MKL_ILP_LIBRARY
      mkl_intel
      PATHS
        ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
        ${MKL_ROOT_DIR}/lib/
    )
else()
    find_library(MKL_LP_LIBRARY
      mkl_intel_lp64
      PATHS
        ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
        ${MKL_ROOT_DIR}/lib/
    )

    find_library(MKL_ILP_LIBRARY
      mkl_intel_ilp64
      PATHS
        ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
        ${MKL_ROOT_DIR}/lib/
    )
ENDIF()

# Lapack
find_library(MKL_LAPACK_LIBRARY
  mkl_lapack
  PATHS
    ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
    ${MKL_ROOT_DIR}/lib/
)

IF(NOT MKL_LAPACK_LIBRARY)
    find_library(MKL_LAPACK_LIBRARY
      mkl_lapack95_lp64
      PATHS
        ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}
        ${MKL_ROOT_DIR}/lib/
    )
ENDIF()

# iomp5
IF("${MKL_ARCH_DIR}" STREQUAL "32")
    IF(UNIX AND NOT APPLE)
        find_library(MKL_IOMP5_LIBRARY
          iomp5
          PATHS
	  ${INTEL_ROOT}/lib/ia32
        )
    ELSE()
        SET(MKL_IOMP5_LIBRARY "") # no need for mac
    ENDIF()
else()
    IF(UNIX AND NOT APPLE)
        find_library(MKL_IOMP5_LIBRARY
          iomp5
          PATHS
	  ${INTEL_ROOT}/lib/intel64
        )
    ELSE()
        SET(MKL_IOMP5_LIBRARY "") # no need for mac
    ENDIF()
ENDIF()

foreach (MODEVAR ${MKL_MODE_VARIANTS})
    foreach (THREADVAR ${MKL_THREAD_VARIANTS})
	if (MKL_CORE_LIBRARY AND MKL_${MODEVAR}_LIBRARY AND MKL_${THREADVAR}_LIBRARY)
	    set(MKL_${MODEVAR}_${THREADVAR}_LIBRARIES
		${MKL_${MODEVAR}_LIBRARY} ${MKL_${THREADVAR}_LIBRARY} ${MKL_CORE_LIBRARY}
		${MKL_LAPACK_LIBRARY} ${MKL_IOMP5_LIBRARY})
	    #message("${MODEVAR} ${THREADVAR} ${MKL_${MODEVAR}_${THREADVAR}_LIBRARIES}") # for debug
	endif()
    endforeach()
endforeach()

set(MKL_LIBRARIES ${MKL_LP_SEQUENTIAL_LIBRARIES})
LINK_DIRECTORIES(${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}) # hack

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MKL DEFAULT_MSG MKL_INCLUDE_DIR MKL_LIBRARIES)

mark_as_advanced(MKL_INCLUDE_DIR MKL_LIBRARIES
    MKL_CORE_LIBRARY MKL_LP_LIBRARY MKL_ILP_LIBRARY
    MKL_SEQUENTIAL_LIBRARY MKL_INTELTHREAD_LIBRARY MKL_GNUTHREAD_LIBRARY
)


text:
<?php
$lang['L_COMMAND']="comando";
$lang['L_IMPORT_NOTABLE']="Non è stato scelta nessuna tabella per l`importazione!";
$lang['L_PASSWORD_STRENGTH']="Password strength";
$lang['L_SQL_WARNING']="L`esucuzione di comandi SQL potrebbe modificare i dati. L`autore non si prende alcuna responsabilità riguarda la perdita di questi dati.";
$lang['L_SQL_EXEC']="Eseguire comandi SQL";
$lang['L_SQL_DATAVIEW']="Visualizzare dati";
$lang['L_SQL_TABLEVIEW']="Riepilogo tabelle";
$lang['L_SQL_VONINS']="di totale";
$lang['L_SQL_NODATA']="nessun record";
$lang['L_SQL_RECORDUPDATED']="Record è stato cambiato";
$lang['L_SQL_RECORDINSERTED']="Record è stato salvato";
$lang['L_SQL_BACKDBOVERVIEW']="torna dal Riepilogo database";
$lang['L_SQL_RECORDDELETED']="Record è stato cancellato";
$lang['L_ASKTABLEEMPTY']="Vuoi svuotare la tabella `%s` ?";
$lang['L_SQL_RECORDEDIT']="edit record";
$lang['L_SQL_RECORDNEW']="inserire nuovo record";
$lang['L_ASKDELETERECORD']="Vuoi cancellare questo record?";
$lang['L_ASKDELETETABLE']="Vuoi cancellare la tabella `%s` ?";
$lang['L_SQL_BEFEHLE']="Comandi-SQL";
$lang['L_SQL_BEFEHLNEU']="nuovo comando";
$lang['L_SQL_BEFEHLSAVED1']="Comando-SQL";
$lang['L_SQL_BEFEHLSAVED2']="è stato aggiunto";
$lang['L_SQL_BEFEHLSAVED3']="è stato salvato";
$lang['L_SQL_BEFEHLSAVED4']="è stato spostato sopra";
$lang['L_SQL_BEFEHLSAVED5']="è stato cancellato";
$lang['L_SQL_QUERYENTRY']="La query contiene";
$lang['L_SQL_COLUMNS']="Colonne";
$lang['L_ASKDBDELETE']="Vuoi cancellare questo database `%s` con tutto il suo contenuto?";
$lang['L_ASKDBEMPTY']="Vuoi svuotare veramente il database `%s` ?";
$lang['L_ASKDBCOPY']="Vuoi copiare il contenuto di questo database `%s` nel database `%s`?";
$lang['L_SQL_TABLENEW']="Edit tabella";
$lang['L_SQL_OUTPUT']="Output SQL";
$lang['L_DO_NOW']="esegui ora";
$lang['L_SQL_NAMEDEST_MISSING']="Nome del database di destinazione mancante!";
$lang['L_ASKDELETEFIELD']="Vuoi cancellare questo campo?";
$lang['L_SQL_COMMANDS_IN']=" riga in ";
$lang['L_SQL_COMMANDS_IN2']="  sec. parsed.";
$lang['L_SQL_OUT1']="Eseguito";
$lang['L_SQL_OUT2']="Comandi";
$lang['L_SQL_OUT3']="C'erano";
$lang['L_SQL_OUT4']="Commenti";
$lang['L_SQL_OUT5']="Poiché il risultato contiene piu di 5000 righe, queste non sono visibili.";
$lang['L_SQL_SELECDB']="Seleziona database";
$lang['L_SQL_TABLESOFDB']="Tabelle del database";
$lang['L_SQL_EDIT']="edit";
$lang['L_SQL_NOFIELDDELETE']="Impossibile cancellare perchè una tabella deve avere almeno un campo.";
$lang['L_SQL_FIELDDELETE1']="Il campo";
$lang['L_SQL_DELETED']="è stato cancellato";
$lang['L_SQL_CHANGED']="è stato cambiato.";
$lang['L_SQL_CREATED']="è stato creato.";
$lang['L_SQL_NODEST_COPY']="Senza destinazione non si può copiare!";
$lang['L_SQL_DESTTABLE_EXISTS']="Tabella di destinazione già esistente!";
$lang['L_SQL_SCOPY']="La struttura della tabella `%s` e stata copiata in `%s`.";
$lang['L_SQL_TCOPY']="La tabella `%s` è stata copiata con dati in tabella `%s`.";
$lang['L_SQL_TABLENONAME']="La tabella ha bisogno di un nome!";
$lang['L_SQL_TBLNAMEEMPTY']="Il nome della tabella non deve essere vuoto!";
$lang['L_SQL_COLLATENOTMATCH']="Set di caratteri(Charset) ed assortimento(Collation) incompatibili!";
$lang['L_SQL_FIELDNAMENOTVALID']="Errore: nome del campo non valido";
$lang['L_SQL_CREATETABLE']="crea tabella";
$lang['L_SQL_COPYTABLE']="copia tabelle";
$lang['L_SQL_STRUCTUREONLY']="solo struttura";
$lang['L_SQL_STRUCTUREDATA']="Struttura e Dati";
$lang['L_SQL_NOTABLESINDB']="Non ci sono tabelle nel database";
$lang['L_SQL_SELECTTABLE']="Seleziona tabella";
$lang['L_SQL_SHOWDATATABLE']="Guarda dati della tabella";
$lang['L_SQL_TBLPROPSOF']="Proprietà della tabella da";
$lang['L_SQL_EDITFIELD']="Edit campo";
$lang['L_SQL_NEWFIELD']="Nuovo campo";
$lang['L_SQL_INDEXES']="Indice";
$lang['L_SQL_ATPOSITION']="inserire nella posizione";
$lang['L_SQL_FIRST']="prima";
$lang['L_SQL_AFTER']="dopo";
$lang['L_SQL_CHANGEFIELD']="cambia campo";
$lang['L_SQL_INSERTFIELD']="inserisci campo";
$lang['L_SQL_INSERTNEWFIELD']="inserisci nuovo campo";
$lang['L_SQL_TABLEINDEXES']="Indice della tabella";
$lang['L_SQL_ALLOWDUPS']="Duplicati permessi";
$lang['L_SQL_CARDINALITY']="Cardinalità";
$lang['L_SQL_TABLENOINDEXES']="La tabella non contiene l`indice";
$lang['L_SQL_CREATEINDEX']="crea nuovo indice";
$lang['L_SQL_WASEMPTIED']="è stato svuotato";
$lang['L_SQL_RENAMEDTO']="è stato rinominato in";
$lang['L_SQL_DBCOPY']="Il contenuto del database `%s` è stato copiato nel database `%s`.";
$lang['L_SQL_DBSCOPY']="La struttura del database `%s` è stata copiata nel database `%s`.";
$lang['L_SQL_WASCREATED']="stato creato";
$lang['L_SQL_RENAMEDB']="Rinomina database";
$lang['L_SQL_ACTIONS']="Azioni";
$lang['L_SQL_CHOOSEACTION']="Seleziona azione";
$lang['L_SQL_DELETEDB']="Cancella database";
$lang['L_SQL_EMPTYDB']="Svuota database";
$lang['L_SQL_COPYDATADB']="Copia l`intero database in";
$lang['L_SQL_COPYSDB']="Copia struttura in database";
$lang['L_SQL_IMEXPORT']="Importa / Esporta";
$lang['L_INFO_RECORDS']="Record";
$lang['L_NAME']="Nome";
$lang['L_ASKTABLEEMPTYKEYS']="Vuoi svuotare la tabella `%s` e cancellare l`indice?";
$lang['L_EDIT']="edit";
$lang['L_DELETE']="elimina";
$lang['L_EMPTY']="svuota";
$lang['L_EMPTYKEYS']="svuota e cancella l`indice";
$lang['L_SQL_TABLEEMPTIED']="La tabella `%s` e stata svuotata.";
$lang['L_SQL_TABLEEMPTIEDKEYS']="La tabella `%s` è stata svuotata e l`ìndice è stato cancellato.";
$lang['L_SQL_LIBRARY']="SQL-Library";
$lang['L_SQL_ATTRIBUTES']="Attributi";
$lang['L_SQL_UPLOADEDFILE']="File caricato: ";
$lang['L_SQL_IMPORT']="Import del database `%s`";
$lang['L_EXPORT']="Esporta";
$lang['L_IMPORT']="Importa";
$lang['L_IMPORTOPTIONS']="Opzioni di importazione";
$lang['L_CSVOPTIONS']="Opzioni-CSV";
$lang['L_IMPORTTABLE']="Importa nella tabella";
$lang['L_NEWTABLE']="tabella nuova";
$lang['L_IMPORTSOURCE']="Sorgente di importazione";
$lang['L_FROMTEXTBOX']="dal campo testo";
$lang['L_FROMFILE']="dal file";
$lang['L_EMPTYTABLEBEFORE']="Svuota prima la tabella";
$lang['L_CREATEAUTOINDEX']="Creare indice automatico";
$lang['L_CSV_NAMEFIRSTLINE']="nome del campo nella prima riga";
$lang['L_CSV_FIELDSEPERATE']="Campi separati con";
$lang['L_CSV_FIELDSENCLOSED']="Campi inclusi in";
$lang['L_CSV_FIELDSESCAPE']="Campi sfuggiti da";
$lang['L_CSV_EOL']="Linee separate con";
$lang['L_CSV_NULL']="Cambia ZERO con";
$lang['L_CSV_FILEOPEN']="aprire file CSV";
$lang['L_IMPORTIEREN']="importare";
$lang['L_SQL_EXPORT']="Esportazione del database `%s`";
$lang['L_EXPORTOPTIONS']="Opzioni di esportazione";
$lang['L_EXCEL2003']="Excel 2003";
$lang['L_SHOWRESULT']="Visualizzare il risultato";
$lang['L_SENDRESULTASFILE']="Inviare risultato come file";
$lang['L_EXPORTLINES']="%s righe da esportare";
$lang['L_CSV_FIELDCOUNT_NOMATCH']="La quantità dei campi della tabella non coincide con i dati da importare (%d al posto di %d).";
$lang['L_CSV_FIELDSLINES']="%d campi trovati, totale %d righe";
$lang['L_CSV_ERRORCREATETABLE']="Errore nella creazione della tabella `%s` !";
$lang['L_FM_UPLOADFILEREQUEST']="Scegli un file.";
$lang['L_CSV_NODATA']="File da importare non trovato!";
$lang['L_SQLLIB_GENERALFUNCTIONS']="funzioni generali";
$lang['L_SQLLIB_RESETAUTO']="ripristina autoincremento";
$lang['L_SQLLIB_BOARDS']="Board";
$lang['L_SQLLIB_DEACTIVATEBOARD']="disattiva Board";
$lang['L_SQLLIB_ACTIVATEBOARD']="attivare Board";
$lang['L_SQL_NOTABLESSELECTED']="Non ci sono tabelle selezionate !";
$lang['L_TOOLS']="Tools";
$lang['L_TOOLS_TOOLBOX']="Scegliere database / Funzione database / Importa e Esporta ";
$lang['L_SQL_OPENFILE']="aprire file - SQL";
$lang['L_SQL_OPENFILE_BUTTON']="Caricare";
$lang['L_MAX_UPLOAD_SIZE']="Grandezza massima del file";
$lang['L_SQL_SEARCH']="Ricerca";
$lang['L_SQL_SEARCHWORDS']="Parola(e) di ricerca";
$lang['L_START_SQL_SEARCH']="esegui ricerca";
$lang['L_RESET_SEARCHWORDS']="resetta ricerca";
$lang['L_SEARCH_OPTIONS']="Opzione di ricerca";
$lang['L_SEARCH_RESULTS']="La ricera di \"%s\" nella tabella \"%s\" ha portato al seguente risultato";
$lang['L_SEARCH_NO_RESULTS']="La ricera \"%s\" nella tabella \"%s\" non ha prodotto risultati!";
$lang['L_NO_ENTRIES']="La tabella \"%s\" è vuota.";
$lang['L_SEARCH_ACCESS_KEYS']="Sfogliare: avanti=ALT+V, indietro=ALT+C";
$lang['L_SEARCH_OPTIONS_OR']="una colonna deve contenere almeno una parola di ricerca";
$lang['L_SEARCH_OPTIONS_CONCAT']="un record deve contenere tutte le parole di ricerca, però possono essere in colonne diverse (Calcolo intensivo!)";
$lang['L_SEARCH_OPTIONS_AND']="una colonna deve contenere tutte le parole di ricerca";
$lang['L_SEARCH_IN_TABLE']="cerca nelle tabelle";
$lang['L_SQL_EDIT_TABLESTRUCTURE']="Edit struttura tabelle";
$lang['L_DEFAULT_CHARSET']="Set di caretteri standard";
$lang['L_TITLE_KEY_PRIMARY']="Chiave primaria";
$lang['L_TITLE_KEY_UNIQUE']="Chiave unica";
$lang['L_TITLE_INDEX']="Indice";
$lang['L_TITLE_KEY_FULLTEXT']="Chiave testo pieno";
$lang['L_TITLE_NOKEY']="Nessuna chiave";
$lang['L_TITLE_SEARCH']="Cerca";
$lang['L_TITLE_MYSQL_HELP']="Documentazione MySQL";
$lang['L_TITLE_UPLOAD']="Carica dati-SQL";
$lang['L_PRIMARYKEY_DELETED']="Chiave primaria cancellata";
$lang['L_PRIMARYKEY_NOTFOUND']="Chiave primaria non trovata";
$lang['L_PRIMARYKEYS_CHANGED']="Chiave primaria cambiata";
$lang['L_PRIMARYKEYS_CHANGINGERROR']="Errore nel cambiare della chiave primaria";
$lang['L_SQL_VIEW_COMPACT']="Visualizza: compatto";
$lang['L_SQL_VIEW_STANDARD']="Visualizza: normale";
$lang['L_FIELDS_OF_TABLE']="Campi della tabella";
$lang['L_ENGINE']="Engine";
$lang['L_USERNAME']="Username";
$lang['L_PASSWORD']="Password";
$lang['L_PASSWORD_REPEAT']="Password (repeat)";
$lang['L_INFO_SIZE']="Grandezza";
$lang['L_TABLE_TYPE']="Tipo";
$lang['L_KEY_DELETED']="Index deleted";
$lang['L_KEY_DELETEERROR']="Error deleting index";
$lang['L_KEY_ADDED']="Index added";
$lang['L_KEY_ADDERROR']="Error adding index";

?>


text:
<?php

namespace app\modules\Admin\ZhaoPin\controllers;
use app\modules\AppBase\base\appbase\BaseController;

use yii\data\Pagination;
use yii\db\Query;
use yii\helpers\Url;
use Yii;

class ZhaopinController extends BaseController{

    /**
     * 获取园所招生信息接口
     * @return string
     */
    public function actionIndex(){
        $page = Yii::$app->request->get('page','');
        $school_id = $this->getCustomSchool_id();
        $query = new Query();
        $query->select('id,title,img_url,create_time')
            ->from('zhaopin')
            ->where(['school_id'=>$school_id]);
        //$countQuery = clone $query;
        $page_size = 20;
        $offset = $page_size * ($page-1);
        //$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20, 'pageSizeLimit' => 1]);

        $zhaopin_list = $query->orderBy(['id'=>SORT_DESC])->offset($offset)->limit($page_size)->all();
        foreach ($zhaopin_list as &$item) {
            $item['url'] = Url::to('index.php?r=zhaopin/view&id='.$item['id'],true);
            $item['create_time'] = date('Y-m-d H:i',$item['create_time']);
            if(empty($item['img_url'])){
                $item['img_url'] = Url::to('/images/default_zhaopin.png',true);
            }
        }
        $result = ['ErrCode' => 0, 'Message' => '', 'Content' => $zhaopin_list];
        return json_encode($result);
    }

    public function actionDelete(){

    }

}


text:
'use strict';

/**
 * Module dependencies
 */
import rndstr from 'rndstr';
import App from '../../models/app';
import serialize from '../../serializers/app';

/**
 * Create an app
 *
 * @param {Object} params
 * @param {Object} user
 * @return {Promise<object>}
 */
module.exports = async (params, user) =>
	new Promise(async (res, rej) =>
{
	// Get 'name_id' parameter
	const nameId = params.name_id;
	if (nameId == null || nameId == '') {
		return rej('name_id is required');
	}

	// Validate name_id
	if (!/^[a-zA-Z0-9\-]{3,30}$/.test(nameId)) {
		return rej('invalid name_id');
	}

	// Get 'name' parameter
	const name = params.name;
	if (name == null || name == '') {
		return rej('name is required');
	}

	// Get 'description' parameter
	const description = params.description;
	if (description == null || description == '') {
		return rej('description is required');
	}

	// Get 'permission' parameter
	const permission = params.permission;
	if (permission == null || permission == '') {
		return rej('permission is required');
	}

	// Get 'callback_url' parameter
	let callback = params.callback_url;
	if (callback === '') {
		callback = null;
	}

	// Generate secret
	const secret = rndstr('a-zA-Z0-9', 32);

	// Create account
	const inserted = await App.insert({
		created_at: new Date(),
		user_id: user._id,
		name: name,
		name_id: nameId,
		name_id_lower: nameId.toLowerCase(),
		description: description,
		permission: permission.split(','),
		callback_url: callback,
		secret: secret
	});

	const app = inserted.ops[0];

	// Response
	res(await serialize(app));
});


text:
package com.xz.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 可以不手动保留传递cookie
 * HttpClient自动保留转发
 */
public class OraContentViaSSO {

	Map<String, String> ssoparam;
	private String ssousername;
	private String password;

	/**
	 * 设置SSO用户名和密码
	 * @param ssousername
	 * @param password
	 */
	public void setSSOAuth(String ssousername, String password){
		this.ssousername=ssousername;
		this.password=password;
	}

	/**
	 * 获取目标页面内容
	 * @param url
	 * @param reqhost
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public String getOraUrlContent(String url, String reqhost) throws IllegalStateException, IOException{
		CloseableHttpClient httpclient = HttpClientUtils.createSSLClientDefault();
		String redirectUrl;
		redirectUrl=requestBeforeSSO(httpclient, url, false);
		loadSSOpage(httpclient, redirectUrl, false);
		redirectUrl=realSSOLogin(httpclient, false);
		return retrieveRequestPage(httpclient, redirectUrl, reqhost, false);
	}

	/**
	 * call this method to get wifi password directly
	 * @param url
	 * @param reqhost
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public String parseWifiPassword(String url, String reqhost) throws IllegalStateException, IOException {
		String htmlcont=getOraUrlContent(url, reqhost);
		return parseWifiPassword(htmlcont);
	}

	private String parseWifiPassword(String htmlcont) throws IOException{
		BufferedReader br=new BufferedReader(new StringReader(htmlcont));

		String line;
		while ((line=br.readLine())!=null){
			if (line.contains("Password")){
				line=line.trim();
				line=line.substring("Password:".length());
				line=line.trim();
				break;
			}
		}

		return line;
	}

	/**
	 * SSO重定向之前的请求
	 * 该请求的response将包含进入SSO的前提信息
	 * @param httpclient
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	private String requestBeforeSSO(CloseableHttpClient httpclient, String url, boolean debugFlag) throws IllegalStateException, IOException{
		String location=null;

		if (debugFlag)
			System.out.println("---- request before SSO ----");

		HttpGet req = new HttpGet(url);

		//禁止重定向
		req.setConfig(RequestConfig .custom().setRedirectsEnabled(false).build());

		HttpClientUtils.simulateBrower(req);

		CloseableHttpResponse resp = httpclient.execute(req);

		Header header = resp.getFirstHeader("Location");
		if (header != null)
			location=header.getValue();

		HttpEntity entity = resp.getEntity();

		if (debugFlag)
			HttpClientUtils.printDebugInfo(resp, entity);

		EntityUtils.consume(entity);

		resp.close();

		if (debugFlag)
			System.out.println("---- step1 done ----");

		return location;
	}

	/**
	 * 加载SSO页面,传入上级跳转url
	 *
	 * 加载出来的页面发现是一段功能并不完善的页面,
	 * 但是里面的input 参数是接下来成功登录的重要信息
	 * 正式执行时,务必设置debugFlag=FALSE
	 *
	 * @param httpclient
	 * @param preloc
	 * @param debugFlag
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private String loadSSOpage(CloseableHttpClient httpclient, String preloc, boolean debugFlag) throws ClientProtocolException, IOException{
		String location=null;

		if (debugFlag)
			System.out.println("---- in SSO ----");

		HttpGet req = new HttpGet(preloc);
		//禁止重定向
		req.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());

		HttpClientUtils.simulateBrower(req);

		//System.out.println("--set cookie : "+cookie + "---");

		//req.setHeader("Cookie", cookie);
		req.setHeader("Host", "login.oracle.com");
		req.setHeader("Referer", preloc);

		CloseableHttpResponse resp = httpclient.execute(req);

		Header header = resp.getFirstHeader("Location");
		if (header != null)
			location=header.getValue();

		HttpEntity entity = resp.getEntity();

		if (debugFlag)
			HttpClientUtils.printDebugInfo(resp, entity);
		else
			ssoparam=parseSSOlogon(entity, debugFlag);

		EntityUtils.consume(entity);

		resp.close();

		if (debugFlag)
			System.out.println("---- step2 done ----");

		return location;
	}

	//解析SSO登录页面
	private Map<String, String> parseSSOlogon(HttpEntity entity, boolean debugFlag) throws IllegalStateException, IOException{
		if (debugFlag)
			System.out.println("Parsing params---");

		Map<String, String> parammap=new HashMap<String, String>();

		String line=HttpClientUtils.htmlContent(entity);

		if (debugFlag)
			System.out.println(line);

		//起始点
		int startindex=line.indexOf("type=\"hidden\" name=");

		while (true){
			int nameindex=line.indexOf("name", startindex);

			int valueindex=line.indexOf("value",startindex);

			if (nameindex==-1 || valueindex==-1)
				break;

			int nameend=line.indexOf("\"", nameindex+6);
			int valueend=line.indexOf("\"", valueindex+7);
			String name=line.substring(nameindex+6, nameend);
			String value=line.substring(valueindex+7, valueend);

			if (debugFlag)
				System.out.println(name + " --> "+value);

			parammap.put(name, value);

			startindex=nameend>valueend?nameend:valueend;

		}

		return parammap;
	}

	/**
	 * 实验性第一次请求SSO log in
	 * 直接把上一步的表单提交,
	 * 虽然提交失败,因为没用户名和密码,
	 * 但是此时返回的页面保留了实际登录所需的信息
	 *
	private String loadSSOstep3(CloseableHttpClient httpclient) throws ClientProtocolException, IOException{
		String location=null;

		System.out.println("---- in SSO first request----");

		HttpPost req = new HttpPost("https://login.oracle.com/mysso/signon.jsp");
		//禁止重定向
		req.setConfig(RequestConfig .custom().setRedirectsEnabled(false).build());

		HttpClientUtils.simulateBrower(req);

		//System.out.println("--set cookie : "+cookie + "---");

		//req.setHeader("Cookie", cookie);
		req.setHeader("Host", "login.oracle.com");
		//req.setHeader("Referer", preloc);

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		for (Map.Entry<String, String> entry : ssoparam.entrySet()){
			nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		req.setEntity(new UrlEncodedFormEntity(nvps));

		CloseableHttpResponse resp = httpclient.execute(req);

		Header header = resp.getFirstHeader("Location");
		if (header != null)
			location=header.getValue();

		HttpEntity entity = resp.getEntity();

		HttpClientUtils.printDebugInfo(resp, entity);
		//ssoparam=parseSSOlogon(entity);

		EntityUtils.consume(entity);

		resp.close();

		System.out.println("---- step3 done ----");

		return location;
	}*/

	/**
	 * 这步开始正式login
	 * 这步最重要的是要保存cookie信息
	 *
	 * @param httpclient
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private String realSSOLogin(CloseableHttpClient httpclient, boolean debugFlag) throws ClientProtocolException, IOException{
		if (this.ssousername==null || this.password==null)
			throw new ClientProtocolException("you must provide sso username and password!");

		String location=null;

		if (debugFlag)
			System.out.println("---- in SSO first request----");

		HttpPost req = new HttpPost("https://login.oracle.com/oam/server/sso/auth_cred_submit");
		//禁止重定向
		req.setConfig(RequestConfig .custom().setRedirectsEnabled(false).build());

		HttpClientUtils.simulateBrower(req);

		//System.out.println("--set cookie : "+cookie + "---");

		//req.setHeader("Cookie", cookie);
		req.setHeader("Host", "login.oracle.com");
		req.setHeader("Accept-Encoding","gzip, deflate");
		req.setHeader("Content-Type","application/x-www-form-urlencoded");
		//req.setHeader("Referer", preloc);

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		for (Map.Entry<String, String> entry : ssoparam.entrySet()){
			if (entry.getKey().equalsIgnoreCase("v") ||
					entry.getKey().equalsIgnoreCase("request_id") ||
					entry.getKey().equalsIgnoreCase("OAM_REQ") ||
					entry.getKey().equalsIgnoreCase("site2pstoretoken") ||
					entry.getKey().equalsIgnoreCase("locale"))
				nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}

		nvps.add(new BasicNameValuePair("ssousername", this.ssousername));
		nvps.add(new BasicNameValuePair("password", this.password));

		req.setEntity(new UrlEncodedFormEntity(nvps));

		CloseableHttpResponse resp = httpclient.execute(req);

		Header header = resp.getFirstHeader("Location");
		if (header != null)
			location=header.getValue();

		HttpEntity entity = resp.getEntity();

		if (debugFlag)
			HttpClientUtils.printDebugInfo(resp, entity);

		EntityUtils.consume(entity);

		resp.close();

		if (debugFlag)
			System.out.println("---- step3 done ----");

		return location;
	}

	/**
	 * 这步进入到登录成功之后页面
	 * 转发后有可能又是一个重定向页面
	 * 而且包含一个新的cookie信息
	 *
	 * 此时去掉重定向限制,直接获取目标页面
	 */
	private String retrieveRequestPage(CloseableHttpClient httpclient, String preloc, String requestHost, boolean debugFlag) throws IOException{
		String htmlcont=null;

		if (debugFlag)
			System.out.println("---- go to target page ----");

		HttpGet req = new HttpGet(preloc);
		//禁止重定向
		//req.setConfig(RequestConfig .custom().setRedirectsEnabled(false).build());

		HttpClientUtils.simulateBrower(req);

		//System.out.println("--set cookie : "+cookie + "---");

		//req.setHeader("Cookie", cookie);
		req.setHeader("Host", requestHost);
		req.setHeader("Referer", "https://login.oracle.com/mysso/signon.jsp");

		CloseableHttpResponse resp = httpclient.execute(req);

		HttpEntity entity = resp.getEntity();

		htmlcont=HttpClientUtils.htmlContent(entity);

		EntityUtils.consume(entity);

		resp.close();

		if (debugFlag)
			System.out.println("---- final step done ----");

		return htmlcont;
	}
}


text:
import * as React from 'react';
import { Link } from 'react-router-dom';

export interface HotNew {
  title: string;
  news_id: number;
  url: string;
  thumbnail: string;
}

interface HotListProps {
  hotNews: Array<HotNew>;
}
export default class HotList extends React.Component<HotListProps, any> {
  constructor(props:HotListProps){
    super(props);
  }
  render(){
    return (

      {
        this.props.hotNews.map((item: HotNew)=>{
          return (
            <Link className="new-block" to={{pathname: "/articles/"+item.news_id}} key={item.news_id}>

{item.title}

            </Link>
          );
        })
      }

    );
  }
}


text:
package net.marcelkrcah

import akka.actor.Actor
import net.marcelkrcah.detector.MathUtils.StringVector
import net.marcelkrcah.detector.RandomTextDetector
import spray.routing._
import spray.http._
import MediaTypes._
import spray.json._
import DefaultJsonProtocol._

class DetectorHttpServiceActor(val detector:RandomTextDetector) extends Actor with DetectorHttpService {
  def actorRefFactory = context
  def receive = runRoute(route)
}

trait DetectorHttpService extends HttpService {

  def detector: RandomTextDetector

  val MaxWordLength = 30

  val route =
    path("") {
      getFromResource("webapp/index.html")
    } ~
    pathPrefix("") {
      getFromResourceDirectory("webapp")
    } ~
    path("api" / "detect") {
      get {
        parameters('q) { word =>
          respondWithMediaType(`application/json`) {
            complete {
              val wordNormalized = word.trim.toLowerCase.take(MaxWordLength).mkString("")
              val (score, ngrams) = detector.getScore(wordNormalized)
              val jsonResult = ApiCommands.getScore(wordNormalized, score, ngrams)
              jsonResult.prettyPrint
            }
          }
        }
      }
    }
}

object ApiCommands {

  def getScore(word: String, score: Double, ngrams: StringVector[Double]):JsValue = {
    JsObject(
      "word" -> JsString(word),
      "score" -> JsNumber(score),
      "ngrams" -> JsArray(ngrams.toList.sortBy(_._2).reverse.map{
        case (ngram, ngramScore) => JsObject(
          "ngram" -> JsString(ngram),
          "score" -> JsNumber(ngramScore)
        ).toJson }.toList)
    )
  }
}


text:
"use strict";

var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];

var _createClass = require("babel-runtime/helpers/create-class")["default"];

var _interopRequire = require("babel-runtime/helpers/interop-require")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});

var Bool = require("./bool").Bool;

var _lodash = require("lodash");

var isNull = _lodash.isNull;
var isUndefined = _lodash.isUndefined;

var includeIoMixin = _interopRequire(require("./io-mixin"));

var Option = exports.Option = (function () {
  function Option(childType) {
    _classCallCheck(this, Option);

    this._childType = childType;
  }

  _createClass(Option, {
    read: {
      value: function read(io) {
        if (Bool.read(io)) {
          return this._childType.read(io);
        }
      }
    },
    write: {
      value: function write(value, io) {
        var isPresent = !(isNull(value) || isUndefined(value));

        Bool.write(isPresent, io);

        if (isPresent) {
          this._childType.write(value, io);
        }
      }
    },
    isValid: {
      value: function isValid(value) {
        if (isNull(value)) {
          return true;
        }
        if (isUndefined(value)) {
          return true;
        }

        return this._childType.isValid(value);
      }
    }
  });

  return Option;
})();

includeIoMixin(Option.prototype);


text:
using NUnit.Framework;
using Moq;

namespace AutoPoco.KCL.DataSources
{
	[TestFixture]
	public class RandomBinaryDataSourceTests
	{
		[Test]
		public void Constructor_MinLengthSet()
		{
			RandomBinaryDataSource source = new RandomBinaryDataSource(1, 2);
			Assert.AreEqual(1, source.MinLength);
		}
		[Test]
		public void Constructor_MinLengthSetAgain()
		{
			RandomBinaryDataSource source = new RandomBinaryDataSource(10, 11);
			Assert.AreEqual(10, source.MinLength);
		}
		[Test]
		public void Constructor_MaxLengthSet()
		{
			RandomBinaryDataSource source = new RandomBinaryDataSource(0, 1);
			Assert.AreEqual(1, source.MaxLength);
		}
		[Test]
		public void Constructor_MaxLengthSetAgain()
		{
			RandomBinaryDataSource source = new RandomBinaryDataSource(0, 10);
			Assert.AreEqual(10, source.MaxLength);
		}
		[Test]
		public void Constructor_RandIsSet()
		{
			IRandomExtensions rand = Mock.Of<IRandomExtensions>();
			var target = new RandomBinaryDataSource(rand, 1, 2);

			Assert.AreEqual(rand, target.Random, "The Random Property was not set properly.");
		}
		[Test]
		public void Constructor_MaxLengthLessThanMin()
		{
			try
			{
				new RandomBinaryDataSource(2, 1);
				Assert.Fail("An ArgumentException was expected because the MaxLength value was less than MinLength");
			}
			catch (System.ArgumentException) { }
		}
		[Test]
		public void Constructor_MaxLengthLessThanMin_WithRand()
		{
			try
			{
				new RandomBinaryDataSource(Mock.Of<IRandomExtensions>(), 2, 1);
				Assert.Fail("An ArgumentException was expected because the MaxLength value was less than MinLength");
			}
			catch (System.ArgumentException) { }
		}
		[Test]
		public void Next()
		{
			Mock<IRandomExtensions> randMock = new Mock<IRandomExtensions>();
			var target = new RandomBinaryDataSource(randMock.Object, 1, 2);
			target.Next(null);

			randMock.Verify(a => a.GetBinary(1, 2), @"
The GetBinary method on the IRandomExtensions object was not called.
");
		}
	}
}


text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"  android:drawable="@drawable/icon_friend_pressed" />
    <item android:state_checked="false"  android:drawable="@drawable/icon_friend" />
</selector>


text:
package io.aos.endpoint.url.handler.daytime;

import java.net.*;
import java.io.*;

public class DayTimeHandler extends URLStreamHandler {

  public int getDefaultPort() {
    return 13;
  }

  protected URLConnection openConnection(URL u) throws IOException {
    return new DayTimeUrlConnection(u);
  }

}


text:
require 'adyen/api/xml_querier'

module Adyen
  module Matchers

    module XPathPaymentFormCheck

      def self.build_xpath_query(checks)
        # Start by finding the check for the Adyen form tag
        xpath_query =  "//form[@action='#{Adyen::Form.url}']"

        # Add recurring/single check if specified
        recurring =  checks.delete(:recurring)
        unless recurring.nil?
          if recurring
            xpath_query << "[descendant::input[@type='hidden'][@name='recurringContract']]"
          else
            xpath_query << "[not(descendant::input[@type='hidden'][@name='recurringContract'])]"
          end
        end

        # Add a check for all the other fields specified
        checks.each do |key, value|
          condition  = "\n  descendant::input[@type='hidden'][@name='#{Adyen::Util.camelize(key)}']"
          condition << "[@value='#{value}']" unless value == :anything
          xpath_query << "[#{condition}]"
        end

        return xpath_query
      end

      def self.check(subject, checks = {})
        document = Adyen::API::XMLQuerier.html(subject)
        result = document.xpath(build_xpath_query(checks))
        !result.empty?
      end
    end

    class HaveAdyenPaymentForm

      def initialize(checks)
        @checks = checks
      end

      def matches?(document)
        Adyen::Matchers::XPathPaymentFormCheck.check(document, @checks)
      end

      def description
        "have an adyen payment form"
      end

      def failure_message
        "expected to find a valid Adyen form on this page"
      end

      def negative_failure_message
        "expected not to find a valid Adyen form on this page"
      end
    end

    def have_adyen_payment_form(checks = {})
      default_checks = {:merchant_sig => :anything, :payment_amount => :anything, :currency_code => :anything, :skin_code => :anything }
      HaveAdyenPaymentForm.new(default_checks.merge(checks))
    end

    def have_adyen_recurring_payment_form(checks = {})
      recurring_checks = { :recurring => true, :shopper_email => :anything, :shopper_reference => :anything }
      have_adyen_payment_form(recurring_checks.merge(checks))
    end

    def have_adyen_single_payment_form(checks = {})
      recurring_checks = { :recurring => false }
      have_adyen_payment_form(recurring_checks.merge(checks))
    end

    def assert_adyen_payment_form(subject, checks = {})
      default_checks = {:merchant_sig => :anything, :payment_amount => :anything, :currency_code => :anything, :skin_code => :anything }
      assert Adyen::Matchers::XPathPaymentFormCheck.check(subject, default_checks.merge(checks)), 'No Adyen payment form found'
    end

    def assert_adyen_recurring_payment_form(subject, checks = {})
      recurring_checks = { :recurring => true, :shopper_email => :anything, :shopper_reference => :anything }
      assert_adyen_payment_form(subject, recurring_checks.merge(checks))
    end

    def assert_adyen_single_payment_form(subject, checks = {})
      recurring_checks = { :recurring => false }
      assert_adyen_payment_form(subject, recurring_checks.merge(checks))
    end
  end
end


text:
class FetchOpeningPlanCommand
  String.include CoreExtensions::String::Transcoding
  include ActiveModel::Validations

  attr_accessor :dataset, :organization

  validates :dataset, :organization, presence: true

  def initialize(dataset, organization)
    @dataset = dataset
    @organization = organization
  end

  def execute!
    download_and_create_opening_plan
  end

  private

  def download_and_create_opening_plan
    download_urls.each do |url|
      csv_content = download_opening_plan(url)
      parse_and_create_opening_plan(csv_content)
    end
  end

  def download_urls
    @dataset.distributions.map(&:downloadURL)
  end

  def download_opening_plan(url)
    response = HTTParty.get(url)
    response.body.to_utf8
  end

  def parse_and_create_opening_plan(csv_content)
    CSV.new(csv_content).each do |row|
      create_opening_plan(row)
    end
  end

  def create_opening_plan(row)
    opening_plan = build_opening_plan_from_row(row)
    opening_plan.officials.build(name: row[1], position: row[2], email: row[3], kind: "liaison")
    opening_plan.officials.build(name: row[4], position: row[5], email: row[6], kind: "admin")
    opening_plan.save
  end

  def build_opening_plan_from_row(row)
    @organization.opening_plans.build(
      vision: row[0],
      name: row[7],
      description: row[8],
      publish_date: row[9],
    )
  end
end


text:
(function(e,b){if(!b.__SV){var a,f,i,g;
	window.mixpanel=b;b._i=[];
	b.init=function(a,e,d){function f(b,h){var a=h.split(".");
	2==a.length&&(b=b[a[0]],h=a[1]);
	b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;
	"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";
	c.people=c.people||[];
	c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");
	return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};
	i="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");

for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};
	b.__SV=1.2;a=e.createElement("script");
	a.type="text/javascript";
	a.async=!0;
	a.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";
	f=e.getElementsByTagName("script")[0];
	f.parentNode.insertBefore(a,f)}})(document,window.mixpanel||[]);

mixpanel.init("8a8dca9966063dc6de99fe131333dae5");