protected void resultIsFine(String path, Configuration configuration) throws Exception {
    NexusConfigurationXpp3Writer w = new NexusConfigurationXpp3Writer();

    StringWriter sw = new StringWriter();

    w.write(sw, configuration);

    // System.out.println(sw.toString());

    String shouldBe = IOUtil.toString(getClass().getResourceAsStream(path + ".result"));
    shouldBe = shouldBe.replace("\r", "");

    if (!StringUtils.equals(shouldBe, sw.toString())) {
      // write the file out so we can have something to compare

      File expected = FileUtils.toFile(getClass().getResource(path + ".result"));
      File actual = new File("target", expected.getName().replaceFirst("result", "actual"));
      FileOutputStream out = new FileOutputStream(actual);
      try {
        IOUtil.copy(sw.toString(), out);
      } finally {
        IOUtil.close(out);
      }
      String diffMessage = "diff " + expected.getAbsolutePath() + " " + actual.getAbsolutePath();
      String message = "Files differ, you can manually diff them:\n" + diffMessage;

      // the method makes the error pretty, so we can keep it.
      assertEquals(message, shouldBe, sw.toString().replace("\r", ""));
    }
  }
 private void mockToThrows(Throwable throwable) {
   try {
     mockStatic(IOUtil.class);
     when(IOUtil.toString(any(FileReader.class))).thenThrow(throwable);
   } catch (IOException e) {
     fail(e.getMessage());
   }
 }
 /** This method is defined as a JavaScript function. */
 public String readFile(String path) {
   try {
     return IOUtil.toString(new FileInputStream(path));
   } catch (RuntimeException exc) {
     throw exc;
   } catch (Exception exc) {
     throw new RuntimeException("wrap: " + exc.getMessage(), exc);
   }
 }
Example #4
0
  protected void doAssert() throws Exception {
    if (COMPARE) {
      final String callerName = getCallerMethodName();
      final String actualClasspathName = getClass().getSimpleName() + "-" + callerName + ".txt";
      final InputStream actualInputStream = getClass().getResourceAsStream(actualClasspathName);
      final String actual = IOUtil.toString(actualInputStream).replace("\r\n", "\n");

      assertThat(actual, Matchers.equalTo(stringBuilder.toString()));
    }
  }
  protected void assertResource(String name, String expectedContent) throws Exception {
    ResourceLoader resourceLoader = (ResourceLoader) lookup(ResourceLoader.ROLE);

    InputStream is = resourceLoader.getResourceAsInputStream(name);

    assertNotNull("The returned input stream is null, name: '" + name + "'.", is);

    String actialContent = IOUtil.toString(is);

    assertEquals(expectedContent, actialContent);
  }
  /**
   * Send the email.
   *
   * @throws MojoExecutionException if the mail could not be sent
   */
  protected void sendMessage() throws MojoExecutionException {
    File templateFile = new File(templateOutputDirectory, template);
    String email = "";
    final MailSender ms = getActualMailSender();
    final String fromName = ms.getName();
    final String fromAddress = ms.getEmail();
    if (fromAddress == null || fromAddress.equals("")) {
      throw new MojoExecutionException(
          "Invalid mail sender: name and email is mandatory (" + ms + ").");
    }
    getLog()
        .info(
            "Using this sender for email announcement: " + fromAddress + " < " + fromName + " > ");
    try {
      MailMessage mailMsg = new MailMessage();
      mailMsg.setSubject(getSubject());
      mailMsg.setContent(IOUtil.toString(readAnnouncement(templateFile)));
      mailMsg.setContentType(this.mailContentType);
      mailMsg.setFrom(fromAddress, fromName);

      final Iterator it = getToAddresses().iterator();
      while (it.hasNext()) {
        email = it.next().toString();
        getLog().info("Sending mail to " + email + "...");
        mailMsg.addTo(email, "");
      }

      if (getCcAddresses() != null) {
        final Iterator it2 = getCcAddresses().iterator();
        while (it2.hasNext()) {
          email = it2.next().toString();
          getLog().info("Sending cc mail to " + email + "...");
          mailMsg.addCc(email, "");
        }
      }

      if (getBccAddresses() != null) {
        final Iterator it3 = getBccAddresses().iterator();
        while (it3.hasNext()) {
          email = it3.next().toString();
          getLog().info("Sending bcc mail to " + email + "...");
          mailMsg.addBcc(email, "");
        }
      }

      mailer.send(mailMsg);
      getLog().info("Sent...");
    } catch (IOException ioe) {
      throw new MojoExecutionException("Failed to send email.", ioe);
    } catch (MailSenderException e) {
      throw new MojoExecutionException("Failed to send email < " + email + " >", e);
    }
  }
 protected static String getAsString(IFile file) throws IOException, CoreException {
   assert file != null;
   assert file.isAccessible();
   InputStream ins = null;
   String content = null;
   try {
     ins = file.getContents();
     content = IOUtil.toString(ins, 1024);
   } finally {
     IOUtil.close(ins);
   }
   return content;
 }
 private String interpolate(final String text) {
   if (null == text || !text.contains("${")) {
     return text;
   }
   // use same interpolation method as XML for sake of consistency
   final Reader r = new InterpolationFilterReader(new StringReader(text), variables);
   try {
     return IOUtil.toString(r);
   } catch (final IOException e) {
     return text; // should never actually happen, as no actual I/O involved
   } finally {
     IOUtil.close(r);
   }
 }
  protected void securityResultIsFine(String path) throws Exception {

    String shouldBe = IOUtil.toString(getClass().getResourceAsStream(path)).replace("\r", "");

    // we can only compare the string with no encrypted passwords, because the encryption is
    // different every time.
    SecurityConfiguration securityConfig = this.securitySource.loadConfiguration();
    SecurityConfigurationXpp3Writer writer = new SecurityConfigurationXpp3Writer();
    StringWriter stringWriter = new StringWriter();
    writer.write(stringWriter, securityConfig);
    String actual = stringWriter.toString().replace("\r", "");

    if (!StringUtils.equals(shouldBe, actual)) {
      // the method makes the error pretty, so we can keep it.
      assertEquals(shouldBe, actual);
    }
  }
  protected void assertMissingResource(String name) throws Exception {
    ResourceLoader resourceLoader = (ResourceLoader) lookup(ResourceLoader.ROLE);

    try {
      InputStream is = resourceLoader.getResourceAsInputStream(name);

      String content = IOUtil.toString(is);

      fail(
          "Expected ResourceNotFoundException while looking for a resource named '"
              + name
              + "'. Content:\n"
              + content);
    } catch (ResourceNotFoundException e) {
      // expected
    }
  }
  /**
   * Creates the cassandra.yaml conf file for this node.
   *
   * <p>Combines default settings in the resources directory with user-defined settings.
   *
   * @throws IOException if there is a problem writing to the YAML file.
   */
  private void createCassandraYaml() throws IOException {
    getLog().info("Creating YAML for node " + mNodeId + ".");

    // Read the default Cassandra YAML file.
    String defaultYaml = IOUtil.toString(getClass().getResourceAsStream("/cassandra.yaml"));

    // Update some settings based on user configuration through maven.
    // Build a big string and then parse with YAML.
    String customYaml = createCustomYaml();

    // Overwrite any settings in the default YAML with our custom settings.
    String finalYaml = combineYaml(defaultYaml, customYaml);

    // Write out the new YAML.
    File cassandraYaml = new File(mConfDir, "cassandra.yaml");
    FileUtils.fileWrite(cassandraYaml.getAbsolutePath(), finalYaml);
  }
Example #12
0
  @Test
  public void testTrust() throws Exception {
    File fpTrustFolder = container.lookup(FlashPlayerTruster.class).getTrustDir();

    if (!fpTrustFolder.isDirectory()) {
      fpTrustFolder.mkdirs();
    }

    File mavenCfg = new File(fpTrustFolder, "maven.cfg");
    if (mavenCfg.exists()) {
      mavenCfg.delete();
    }

    String basedir = standardConceptTester("flashplayertrust").getBasedir();

    String cfg = IOUtil.toString(new FileReader(mavenCfg));

    assertTrue("flexmojos should write trust localtion", cfg.contains(basedir));
  }
  public RepositoryItemUid readLinkContent(final ContentLocator locator)
      throws NoSuchRepositoryException, IOException {
    if (locator != null) {
      InputStream fis = null;

      try {
        fis = locator.getContent();

        final String linkBody = IOUtil.toString(fis, UTF8_CHARSET);

        final String uidStr = linkBody.substring(LINK_PREFIX.length(), linkBody.length());

        return repositoryItemUidFactory.createUid(uidStr);
      } finally {
        IOUtil.close(fis);
      }
    } else {
      return null;
    }
  }
  private File generateRubyFile(
      final String templateName, final Context context, final String stubFilename)
      throws IOException {
    final InputStream input = getClass().getResourceAsStream("/" + templateName + ".rb.vm");

    if (input == null) {
      throw new FileNotFoundException(templateName + ".rb.vm");
    }

    final String rubyTemplate = IOUtil.toString(input);

    final File rubyFile = File.createTempFile(stubFilename, ".rb.tmp");

    final FileWriter fw = new FileWriter(rubyFile);

    this.velocityComponent.getEngine().evaluate(context, fw, "ruby", rubyTemplate);

    fw.flush();

    fw.close();

    return rubyFile;
  }
  @Test
  public void testRemoteReturnsErrorWith200StatusHeadersNotSet()
      throws ItemNotFoundException, IOException {

    String expectedContent = "my cool expected content";
    ErrorServlet.CONTENT = expectedContent;

    // remote request
    ResourceStoreRequest storeRequest = new ResourceStoreRequest("random/file.txt");
    DefaultStorageFileItem item =
        (DefaultStorageFileItem)
            remoteStorage.retrieveItem(aProxyRepository, storeRequest, this.baseUrl);

    // result should be HTML
    InputStream itemInputStrem = item.getInputStream();

    try {
      String content = IOUtil.toString(itemInputStrem);
      Assert.assertEquals(expectedContent, content);
    } finally {
      IOUtil.close(itemInputStrem);
    }
  }