Example #1
0
  /**
   * Set embedded cassandra up and spawn it in a new thread.
   *
   * @throws TTransportException
   * @throws IOException
   * @throws InterruptedException
   */
  public void start()
      throws TTransportException, IOException, InterruptedException, ConfigurationException {

    File dir = Files.createTempDir();
    String dirPath = dir.getAbsolutePath();
    System.out.println("Storing Cassandra files in " + dirPath);

    URL url = Resources.getResource("cassandra.yaml");
    String yaml = Resources.toString(url, Charsets.UTF_8);
    yaml = yaml.replaceAll("REPLACEDIR", dirPath);
    String yamlPath = dirPath + File.separatorChar + "cassandra.yaml";
    org.apache.commons.io.FileUtils.writeStringToFile(new File(yamlPath), yaml);

    // make a tmp dir and copy cassandra.yaml and log4j.properties to it
    copy("/log4j.properties", dir.getAbsolutePath());
    System.setProperty("cassandra.config", "file:" + dirPath + yamlFilePath);
    System.setProperty("log4j.configuration", "file:" + dirPath + "/log4j.properties");
    System.setProperty("cassandra-foreground", "true");

    cleanupAndLeaveDirs();

    try {
      executor.execute(new CassandraRunner());
    } catch (RejectedExecutionException e) {
      log.error("RejectError", e);
      return;
    }

    try {
      TimeUnit.SECONDS.sleep(WAIT_SECONDS);
    } catch (InterruptedException e) {
      log.error("InterrputedError", e);
      throw new AssertionError(e);
    }
  }
  public MongoFindHelper(EzMongoHandler handler) {
    this.ezMongoHandler = handler;

    // Load up the JSON from the resources.  Throw RuntimeExecptions as there's no reason it should
    // barf in prod
    try {
      final URL sevURL =
          Thread.currentThread().getContextClassLoader().getResource("securityExpressionViz.json");
      if (sevURL == null) {
        throw new RuntimeException("The securityExpressionVis was missing from the JAR");
      }
      securityExpressionViz = Resources.toString(sevURL, Charsets.UTF_8);

      final URL seoURL =
          Thread.currentThread()
              .getContextClassLoader()
              .getResource("securityExpressionOperation.json");
      if (seoURL == null) {
        throw new RuntimeException("The securityExpressionVis was missing from the JAR");
      }
      securityExpressionOperation = Resources.toString(seoURL, Charsets.UTF_8);
    } catch (IOException e) {
      appLog.error(e.getMessage());
      throw new RuntimeException(e);
    }
  }
Example #3
0
  /** @return true if already exists/wrote to file, false if writing to file failed */
  protected boolean copyDefaultConfig() {
    File dataFolder = getDataFolder();

    // make data folder if it isn't there
    if (!dataFolder.exists() && !dataFolder.mkdir()) {
      // data folder creation failed
      return false;
    }

    File configFile = new File(getDataFolder(), "config.yml");

    // config file already exists
    if (configFile.exists()) return true;

    // write the defaults
    URL defaultConfig = Resources.getResource(this.getClass(), "/default.yml");
    try {
      // write /default.yml to the config.yml file
      Files.write(Resources.toByteArray(defaultConfig), configFile);
    } catch (IOException e) {
      e.printStackTrace();
      // something failed during writing
      return false;
    }

    // config wrote successfully
    return true;
  }
Example #4
0
  public void createTables() throws IOException, SQLException {
    URL resource = Resources.getResource(SkyWars.class, "/tables.sql");
    String[] databaseStructure = Resources.toString(resource, Charsets.UTF_8).split(";");

    if (databaseStructure.length == 0) {
      return;
    }

    Statement statement = null;

    try {
      connection.setAutoCommit(false);
      statement = connection.createStatement();

      for (String query : databaseStructure) {
        query = query.trim();

        if (query.isEmpty()) {
          continue;
        }

        statement.execute(query);
      }

      connection.commit();

    } finally {
      connection.setAutoCommit(true);

      if (statement != null && !statement.isClosed()) {
        statement.close();
      }
    }
  }
 @Test
 public void testNestedMapWithNull() throws Exception {
   final String script =
       Resources.toString(Resources.getResource("NestedMapWithNull.groovy"), Charsets.UTF_8);
   Processor processor = new GroovyProcessor(ProcessingMode.BATCH, script);
   ScriptingProcessorTestUtil.verifyNestedMap(GroovyProcessor.class, processor);
 }
 @Test
 public void testChangeFieldTypeFromScripting() throws Exception {
   final String script =
       Resources.toString(Resources.getResource("ChangeFieldTypeScript.groovy"), Charsets.UTF_8);
   Processor processor = new GroovyProcessor(ProcessingMode.BATCH, script);
   ScriptingProcessorTestUtil.verifyChangedTypeFromScripting(GroovyProcessor.class, processor);
 }
 @Test
 public void testAssignNullToTypedField() throws Exception {
   final String script =
       Resources.toString(Resources.getResource("AssignNullToTypedField.groovy"), Charsets.UTF_8);
   Processor processor = new GroovyProcessor(ProcessingMode.BATCH, script);
   ScriptingProcessorTestUtil.verifyPreserveTypeForNullValue(GroovyProcessor.class, processor);
 }
Example #8
0
 @Test
 public void testBuildSimpleUNIX() throws MalformedURLException, IOException {
   assertEquals(
       testInitBuilder.render(OsFamily.UNIX),
       Resources.toString(
           Resources.getResource("test_init." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8));
 }
  @Test
  public void testDecompressDeflateRequest() throws Exception {
    request.setMethod("POST");
    request.setURI("/banner");
    request.setHeader(HttpHeaders.CONTENT_ENCODING, "deflate");
    request.setHeader(HttpHeaders.CONTENT_TYPE, PLAIN_TEXT_UTF_8);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DeflaterOutputStream deflate = new DeflaterOutputStream(baos)) {
      Resources.copy(Resources.getResource("assets/new-banner.txt"), deflate);
    }
    byte[] output = baos.toByteArray();
    request.setContent(output);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output);

    byte[] result = new byte[4096];
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    System.out.println(new String(result, 0, resultLength, "UTF-8"));

    HttpTester.Response response =
        HttpTester.parseResponse(servletTester.getResponses(request.generate()));
    System.out.println(response.getStatus());
    System.out.println(response.getContent());
  }
Example #10
0
 @Override
 public void execute() throws Exception {
   System.out.println();
   System.out.println(
       Resources.toString(Resources.getResource(App.class, "intro"), Charsets.UTF_8));
   System.out.println();
 }
Example #11
0
  @Test
  public void testSort()
      throws CompileException, ExecutionException, InterruptedException, IOException {
    String qsort =
        Resources.toString(
            Resources.getResource(QSortTest.class, "sortFunc.zel"), Charsets.US_ASCII);
    Program p = Program.compile(qsort, "x");
    Integer[] testArray = new Integer[100000];
    Random random = new Random();
    for (int i = 0; i < testArray.length; i++) {
      testArray[i] = random.nextInt();
    }

    Integer[] resultPar = null;
    for (int i = 0; i < 3; i++) {
      long startTime = System.currentTimeMillis();
      resultPar = testArray.clone();
      p.execute(new Object[] {resultPar});
      System.out.println("Parallel exec time = " + (System.currentTimeMillis() - startTime));
    }

    Integer[] resultSt = null;
    for (int i = 0; i < 3; i++) {
      long startTime = System.currentTimeMillis();
      resultSt = testArray.clone();
      p.executeSingleThreaded(new Object[] {resultSt});
      System.out.println("ST exec time = " + (System.currentTimeMillis() - startTime));
    }

    Arrays.sort(testArray);

    Assert.assertArrayEquals((Object[]) resultSt, (Object[]) resultPar);
    Assert.assertArrayEquals((Object[]) resultSt, testArray);
  }
Example #12
0
 protected String getFile(String resource) throws IOException {
   URL url = Resources.getResource(resource);
   if (url == null) {
     throw new IOException(String.format("Unable to find path %s.", resource));
   }
   return Resources.toString(url, Charsets.UTF_8);
 }
Example #13
0
 public static String resourceContents(String path) {
   try {
     return Resources.toString(Resources.getResource(path), StandardCharsets.UTF_8);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Example #14
0
  @Test
  public void packingALargeFileShouldGenerateTheSameOutputWhenOverwritingAsWhenAppending()
      throws IOException {
    File reference = File.createTempFile("reference", ".zip");
    String packageName = getClass().getPackage().getName().replace(".", "/");
    URL sample = Resources.getResource(packageName + "/macbeth.properties");
    byte[] input = Resources.toByteArray(sample);

    try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(output, OVERWRITE_EXISTING);
        ZipOutputStream ref = new ZipOutputStream(new FileOutputStream(reference))) {
      CustomZipEntry entry = new CustomZipEntry("macbeth.properties");
      entry.setTime(System.currentTimeMillis());
      out.putNextEntry(entry);
      ref.putNextEntry(entry);
      out.write(input);
      ref.write(input);
    }

    byte[] seen = Files.readAllBytes(output);
    byte[] expected = Files.readAllBytes(reference.toPath());

    // Make sure the output is valid.
    try (ZipInputStream in = new ZipInputStream(Files.newInputStream(output))) {
      ZipEntry entry = in.getNextEntry();
      assertEquals("macbeth.properties", entry.getName());
      assertNull(in.getNextEntry());
    }

    assertArrayEquals(expected, seen);
  }
 private static String fixture(String name) {
   try {
     return Resources.toString(Resources.getResource(name), StandardCharsets.UTF_8);
   } catch (IOException e) {
     throw Throwables.propagate(e);
   }
 }
  /**
   * In ode code, component names are used to identify a component though the variables storing
   * component names appear to be "type". While there's no harm in ode, here in build server, they
   * need to be separated. This method returns a name-type map, mapping the component names used in
   * ode to the corresponding type, aka fully qualified name. The type will be used to build apk.
   */
  private static Map<String, String> createNameTypeMap(File assetsDir)
      throws IOException, JSONException {
    Map<String, String> nameTypeMap = Maps.newHashMap();

    JSONArray simpleCompsJson =
        new JSONArray(
            Resources.toString(
                ProjectBuilder.class.getResource("/files/simple_components.json"), Charsets.UTF_8));
    for (int i = 0; i < simpleCompsJson.length(); ++i) {
      JSONObject simpleCompJson = simpleCompsJson.getJSONObject(i);
      nameTypeMap.put(simpleCompJson.getString("name"), simpleCompJson.getString("type"));
    }

    File extCompsDir = new File(assetsDir, "external_comps");
    if (!extCompsDir.exists()) {
      return nameTypeMap;
    }

    for (File extCompDir : extCompsDir.listFiles()) {
      if (!extCompDir.isDirectory()) {
        continue;
      }

      File extCompJsonFile = new File(extCompDir, "component.json");
      JSONObject extCompJson =
          new JSONObject(Resources.toString(extCompJsonFile.toURI().toURL(), Charsets.UTF_8));
      nameTypeMap.put(extCompJson.getString("name"), extCompJson.getString("type"));
    }

    return nameTypeMap;
  }
  private void testMode(ProcessingMode mode) throws Exception {
    final String script =
        Resources.toString(Resources.getResource("ModeScript.groovy"), Charsets.UTF_8);
    Processor processor = new GroovyProcessor(mode, script);

    ScriptingProcessorTestUtil.verifyMode(GroovyDProcessor.class, processor);
  }
 @Override
 public FullHttpResponse handleRequest(ChannelHandlerContext ctx, HttpRequest request)
     throws Exception {
   URL url = Resources.getResource("org/glowroot/ui/app-dist/index.html");
   String indexHtml = Resources.toString(url, Charsets.UTF_8);
   String layout;
   if (httpSessionManager.hasReadAccess(request)) {
     layout = layoutJsonService.getLayout();
   } else {
     layout = layoutJsonService.getNeedsAuthenticationLayout();
   }
   String authenticatedUser = httpSessionManager.getAuthenticatedUser(request);
   String layoutScript =
       "var layout="
           + layout
           + ";var authenticatedUser = '******'";
   indexHtml =
       indexHtml.replaceFirst(
           "<base href=\"/\">",
           "<base href=\"" + BASE_HREF + "\"><script>" + layoutScript + "</script>");
   // this is to work around an issue with IE10-11 (IE9 is OK)
   // (even without reverse proxy/non-root base href)
   // IE doesn't use the base href when loading the favicon
   indexHtml =
       indexHtml.replaceFirst(
           "<link rel=\"shortcut icon\" href=\"favicon\\.([0-9a-f]+)\\.ico\">",
           "<script>document.write('<link rel=\"shortcut icon\" href=\"'"
               + " + document.getElementsByTagName(\"base\")[0].href"
               + " + 'favicon.$1.ico\">');</script>");
   if (GOOGLE_ANALYTICS_TRACKING_ID != null) {
     // this is for demo.glowroot.org
     indexHtml =
         indexHtml.replaceFirst(
             "<div class=\"navbar-brand\">(\\s*)Glowroot(\\s*)</div>",
             "<a href=\"https://glowroot.org\" class=\"navbar-brand\">$1Glowroot$2</a>");
     indexHtml =
         indexHtml.replaceFirst(
             "</body>",
             "<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]"
                 + "||function(){(i[r].q=i[r].q||[]).push(arguments)},"
                 + "i[r].l=1*new Date();a=s.createElement(o),"
                 + "m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;"
                 + "m.parentNode.insertBefore(a,m)})(window,document,'script',"
                 + "'//www.google-analytics.com/analytics.js','ga');ga('create', '"
                 + GOOGLE_ANALYTICS_TRACKING_ID
                 + "', 'auto');</script>\n</body>");
   }
   ByteBuf content = Unpooled.copiedBuffer(indexHtml, Charsets.ISO_8859_1);
   FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
   HttpServices.preventCaching(response);
   response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
   response.headers().set(HttpHeaderNames.CONTENT_LENGTH, indexHtml.length());
   // X-UA-Compatible must be set via header (as opposed to via meta tag)
   // see https://github.com/h5bp/html5-boilerplate/blob/master/doc/html.md#x-ua-compatible
   response.headers().set("X-UA-Compatible", "IE=edge");
   return response;
 }
 @Test
 public void testTypedNullPassThrough() throws Exception {
   final String script =
       Resources.toString(
           Resources.getResource("PrimitiveTypesPassthroughScript.groovy"), Charsets.UTF_8);
   Processor processor = new GroovyProcessor(ProcessingMode.BATCH, script);
   ScriptingProcessorTestUtil.verifyPreserveTypeForNullValue(GroovyProcessor.class, processor);
 }
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
   resp.setContentType(PLAIN_TEXT_UTF_8);
   Resources.asCharSource(Resources.getResource("assets/banner.txt"), StandardCharsets.UTF_8)
       .copyTo(resp.getWriter());
 }
  private void testRecordModeOnErrorHandling(OnRecordError onRecordError) throws Exception {
    final String script =
        Resources.toString(Resources.getResource("OnErrorHandlingScript.groovy"), Charsets.UTF_8);
    Processor processor = new GroovyProcessor(ProcessingMode.RECORD, script);

    ScriptingProcessorTestUtil.verifyRecordModeOnErrorHandling(
        GroovyDProcessor.class, processor, onRecordError);
  }
  @Test
  public void testGroovyAndMapArray() throws Exception {
    final String script =
        Resources.toString(Resources.getResource("MapAndArrayScript.groovy"), Charsets.UTF_8);
    Processor processor = new GroovyProcessor(ProcessingMode.RECORD, script);

    ScriptingProcessorTestUtil.verifyMapAndArray(GroovyDProcessor.class, processor);
  }
 public static String getResourceContent(String path) {
   try {
     return Resources.toString(
         Resources.getResource(CheckstyleTestUtils.class, path), Charsets.UTF_8);
   } catch (IOException e) {
     throw new IllegalArgumentException("Could not load resource " + path, e);
   }
 }
Example #24
0
 @Before
 public void load() throws IOException {
   cswConfiguration =
       new YamlMappingConfiguration(
           Resources.asByteSource(Resources.getResource("mappings/csw-record.yml")).openStream(),
           new XPathHelper());
   cswMapper = new CswToBuilderMapper(cswConfiguration);
 }
  @Test
  public void testStateObject() throws Exception {
    final String script =
        Resources.toString(Resources.getResource("StateObjectScript.groovy"), Charsets.UTF_8);
    Processor processor = new GroovyProcessor(ProcessingMode.RECORD, script);

    ScriptingProcessorTestUtil.verifyStateObject(GroovyDProcessor.class, processor);
  }
Example #26
0
 @Test
 public void test()
     throws CompileException, ExecutionException, InterruptedException, IOException {
   String qsort =
       Resources.toString(Resources.getResource(QSortTest.class, "sort.zel"), Charsets.US_ASCII);
   Program p = Program.compile(qsort);
   System.out.println(p);
   p.execute();
 }
  @Test
  public void testPrimitiveTypesFromScripting() throws Exception {
    final String script =
        Resources.toString(
            Resources.getResource("PrimitiveTypesFromScripting.groovy"), Charsets.UTF_8);
    Processor processor = new GroovyProcessor(ProcessingMode.RECORD, script);

    ScriptingProcessorTestUtil.verifyPrimitiveTypesFromScripting(GroovyDProcessor.class, processor);
  }
 public HttpResponse prepareResponse(int responseStatus, String fileName) throws IOException {
   HttpResponse response =
       new BasicHttpResponse(
           new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), responseStatus, ""));
   response.setStatusCode(responseStatus);
   byte[] bytes = Resources.toByteArray(Resources.getResource(fileName));
   response.setEntity(new ByteArrayEntity(bytes));
   return response;
 }
 @Before
 public void setup() throws Exception {
   tempDir = Files.createTempDir();
   databaseFile = new File(tempDir, "GeoLite2-Country.mmdb");
   BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(databaseFile));
   Resources.copy(Resources.getResource("GeoLite2-Country.mmdb"), out);
   out.flush();
   out.close();
 }
  @Test
  public void testCallPloidyThree() throws Exception {
    Files.write(Resources.toByteArray(getClass().getResource("testfile2.bam")), bamFile);
    Files.write(Resources.toByteArray(getClass().getResource("hla-a.bed")), bedFile);

    new FilterConsensus(bamFile, bedFile, outputFile, gene, cdna, removeGaps, minimumBreadth, 3)
        .call();
    assertEquals(6, countLines(outputFile));
  }