@Test
  public void testFailsOnVerifyFile() throws Exception {
    File invalidJsonFile = new File(tempStageDir, "invalidjson2.snappy");
    Files.copy(new File(Resources.getResource("invalidjson2.snappy").toURI()), invalidJsonFile);

    assertTrue(invalidJsonFile.exists());
    uploader =
        new S3Uploader(
            storageSystem,
            serverConfig,
            new EventPartitioner(),
            executor,
            executor,
            s3UploaderStats);
    uploader.start();
    assertFalse(invalidJsonFile.exists());
    assertTrue(new File(tempStageDir.getPath() + "/failed", invalidJsonFile.getName()).exists());
    assertFalse(storageSystem.hasReceivedFile(invalidJsonFile));

    S3UploaderStats s3UploaderStatsArgumentVerifier =
        testingReportCollectionFactory.getArgumentVerifier(S3UploaderStats.class);
    verify(s3UploaderStatsArgumentVerifier).processedFiles(UNKNOWN_EVENT_TYPE, CORRUPT);
    verifyNoMoreInteractions(s3UploaderStatsArgumentVerifier);

    CounterStat processedFilesCounterStat =
        testingReportCollectionFactory
            .getReportCollection(S3UploaderStats.class)
            .processedFiles(UNKNOWN_EVENT_TYPE, CORRUPT);
    verify(processedFilesCounterStat).add(1);
    verifyNoMoreInteractions(processedFilesCounterStat);
  }
  public static void main(String[] args) throws IOException {
    Closer closer = Closer.create();
    // copy a file
    File origin = new File("join_temp");
    File copy = new File("target_temp");

    try {
      BufferedReader reader = new BufferedReader(new FileReader("join_temp"));
      BufferedWriter writer = new BufferedWriter(new FileWriter("target_temp"));

      closer.register(reader);
      closer.register(writer);

      String line;

      while ((line = reader.readLine()) != null) {
        writer.write(line);
      }
    } catch (IOException e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }

    Files.copy(origin, copy);

    File moved = new File("moved");

    // moving renaming
    Files.move(copy, moved);

    // working files as string
    List<String> lines = Files.readLines(origin, Charsets.UTF_8);

    HashCode hashCode = Files.hash(origin, Hashing.md5());
    System.out.println(hashCode);

    // file write and append
    String hamlet = "To be, or not to be it is a question\n";
    File write_and_append = new File("write_and_append");

    Files.write(hamlet, write_and_append, Charsets.UTF_8);

    Files.append(hamlet, write_and_append, Charsets.UTF_8);

    //        write_and_append.deleteOnExit();

    Files.write("OverWrite the file", write_and_append, Charsets.UTF_8);

    // ByteSource ByteSink
    ByteSource fileBytes = Files.asByteSource(write_and_append);
    byte[] readBytes = fileBytes.read();
    // equals to pre line -> Files.toByteArray(write_and_append) == readBytes

    ByteSink fileByteSink = Files.asByteSink(write_and_append);
    fileByteSink.write(Files.toByteArray(write_and_append));

    BaseEncoding base64 = BaseEncoding.base64();
    System.out.println(base64.encode("123456".getBytes()));
  }
  /** Extracts the archive resource and then runs the batch-import process on it. */
  protected void importDataArchive(
      final Resource resource,
      final ArchiveInputStream resourceStream,
      BatchImportOptions options) {

    final File tempDir = Files.createTempDir();
    try {
      ArchiveEntry archiveEntry;
      while ((archiveEntry = resourceStream.getNextEntry()) != null) {
        final File entryFile = new File(tempDir, archiveEntry.getName());
        if (archiveEntry.isDirectory()) {
          entryFile.mkdirs();
        } else {
          entryFile.getParentFile().mkdirs();

          Files.copy(
              new InputSupplier<InputStream>() {
                @Override
                public InputStream getInput() throws IOException {
                  return new CloseShieldInputStream(resourceStream);
                }
              },
              entryFile);
        }
      }

      importDataDirectory(tempDir, null, options);
    } catch (IOException e) {
      throw new RuntimeException(
          "Failed to extract data from '" + resource + "' to '" + tempDir + "' for batch import.",
          e);
    } finally {
      FileUtils.deleteQuietly(tempDir);
    }
  }
  /**
   * Copy ProtocolLib into the plugins folder.
   *
   * @throws IOException If anything went wrong.
   */
  private static void setupPlugins() throws IOException {
    File pluginDirectory = new File("plugins/");
    File srcDirectory = new File("../");
    File bestFile = null;
    int bestLength = Integer.MAX_VALUE;

    for (File file : srcDirectory.listFiles()) {
      String name = file.getName();

      if (file.isFile() && name.startsWith("ProtocolLib") && name.length() < bestLength) {
        bestLength = name.length();
        bestFile = file;
      }
    }

    if (bestFile == null) {
      throw new IllegalStateException("Cannot find ProtocolLib in " + srcDirectory);
    }

    // Copy the ProtocolLib plugin to the server
    if (pluginDirectory.exists()) {
      deleteFolder(pluginDirectory);
    }

    pluginDirectory.mkdirs();

    File destination = new File(pluginDirectory, bestFile.getName()).getAbsoluteFile();
    Files.copy(bestFile, destination);
  }
  @Test
  public void testPulseMetricsForUploadAttempts() throws Exception {
    storageSystem.succeedOnAttempt(2);
    File pendingFile = new File(tempStageDir, "pending.json.snappy");
    Files.copy(new File(Resources.getResource("pending.json.snappy").toURI()), pendingFile);

    // attempt 1 to upload from staging directory fails and file is moved to retry directory
    uploader =
        new S3Uploader(
            storageSystem,
            serverConfig,
            new EventPartitioner(),
            executor,
            executor,
            s3UploaderStats);
    uploader.start();

    S3UploaderStats s3UploaderStatsArgumentVerifier =
        testingReportCollectionFactory.getArgumentVerifier(S3UploaderStats.class);
    verify(s3UploaderStatsArgumentVerifier).uploadAttempts(ARBITRARY_EVENT_TYPE, FAILURE);
    verify(s3UploaderStatsArgumentVerifier).processedTime(ARBITRARY_EVENT_TYPE);
    verifyNoMoreInteractions(s3UploaderStatsArgumentVerifier);

    S3UploaderStats s3UploaderStatsReportCollection =
        testingReportCollectionFactory.getReportCollection(S3UploaderStats.class);
    CounterStat uploadAttemptsCounterStat =
        s3UploaderStatsReportCollection.uploadAttempts(ARBITRARY_EVENT_TYPE, FAILURE);
    TimeStat processedTimeTimerStat =
        s3UploaderStatsReportCollection.processedTime(ARBITRARY_EVENT_TYPE);
    verify(uploadAttemptsCounterStat).add(1);
    verify(processedTimeTimerStat).time();
    verifyNoMoreInteractions(uploadAttemptsCounterStat);
    verifyNoMoreInteractions(processedTimeTimerStat);
  }
Beispiel #6
0
  /**
   * Obfuscates the file
   *
   * @throws IOException
   * @throws org.gradle.api.InvalidUserDataException if the there is insufficient information
   *     available to generate the signature.
   */
  void generate(ReobfExceptor exc, File srg) throws IOException {
    File toObf = getToObf();
    if (toObf == null) {
      throw new InvalidUserDataException(
          "Unable to obfuscate as the file to obfuscate has not been specified");
    }

    File output = getFile();
    File excepted = new File(caller.getTemporaryDir(), "excepted.jar");
    Files.copy(toObf, excepted);

    // copy input somewhere else...
    if (exc != null) {
      exc.toReobfJar = toObf;
      exc.buildSrg();
      srg = exc.outSrg;

      // append SRG
      BufferedWriter writer = new BufferedWriter(new FileWriter(srg, true));
      for (String line : caller.getExtraSrg()) {
        writer.write(line);
        writer.newLine();
      }
      writer.flush();
      writer.close();
    }

    // obfuscate!
    if (caller.getUseRetroGuard()) applyRetroGuard(excepted, output, srg);
    else applySpecialSource(excepted, output, srg);
  }
  protected File makeTestFile(File dir, String name, String relative, final InputStream contents)
      throws IOException {
    if (relative != null) {
      dir = new File(dir, relative);
      if (!dir.exists()) {
        boolean mkdir = dir.mkdirs();
        assertTrue(dir.getPath(), mkdir);
      }
    } else if (!dir.exists()) {
      boolean mkdir = dir.mkdirs();
      assertTrue(dir.getPath(), mkdir);
    }
    File tempFile = new File(dir, name);
    if (tempFile.exists()) {
      tempFile.delete();
    }

    Files.copy(
        new InputSupplier<InputStream>() {
          @Override
          public InputStream getInput() throws IOException {
            return contents;
          }
        },
        tempFile);

    return tempFile;
  }
  private StoredObject createCombinedObjectSmall(CombinedStoredObject combinedObject) {
    ImmutableList.Builder<InputSupplier<InputStream>> builder = ImmutableList.builder();
    List<URI> sourceParts =
        Lists.transform(combinedObject.getSourceParts(), StoredObject.GET_LOCATION_FUNCTION);
    for (URI sourcePart : sourceParts) {
      builder.add(getInputSupplier(sourcePart));
    }
    InputSupplier<InputStream> source = ByteStreams.join(builder.build());

    File tempFile = null;
    try {
      tempFile =
          File.createTempFile(
              S3StorageHelper.getS3FileName(combinedObject.getLocation()), ".small.s3.data");
      Files.copy(source, tempFile);
      StoredObject result = putObject(combinedObject.getLocation(), tempFile);
      return result;
    } catch (IOException e) {
      throw Throwables.propagate(e);
    } finally {
      if (tempFile != null) {
        tempFile.delete();
      }
    }
  }
  /**
   * Creates a jar file which includes all the given classes and all the classes that they depended
   * on. The jar will also include all classes and resources under the packages as given as include
   * packages in the constructor.
   *
   * @param target Where to save the target jar file.
   * @param classes Set of classes to start the dependency traversal.
   * @throws IOException
   */
  public void createBundle(Location target, Iterable<Class<?>> classes) throws IOException {
    // Write the jar to local tmp file first
    File tmpJar = File.createTempFile(target.getName(), ".tmp");
    try {
      Set<String> entries = Sets.newHashSet();
      JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(tmpJar));
      try {
        findDependencies(classes, entries, jarOut);

        // Add all classes under the packages as listed in includePackages
        for (String pkg : includePackages) {
          copyPackage(pkg, entries, jarOut);
        }
      } finally {
        jarOut.close();
      }
      // Copy the tmp jar into destination.
      OutputStream os = new BufferedOutputStream(target.getOutputStream());
      try {
        Files.copy(tmpJar, os);
      } finally {
        os.close();
      }
    } finally {
      tmpJar.delete();
    }
  }
  /** Copies all entries under the file path. */
  private void copyFileEntries(
      File baseDir, String entryRoot, Set<String> entries, JarOutputStream jarOut)
      throws IOException {
    URI baseUri = baseDir.toURI();
    File packageFile = new File(baseDir, entryRoot);
    Queue<File> queue = Lists.newLinkedList();
    queue.add(packageFile);
    while (!queue.isEmpty()) {
      File file = queue.remove();

      String entry = baseUri.relativize(file.toURI()).getPath();
      if (entries.add(entry)) {
        jarOut.putNextEntry(new JarEntry(entry));
        if (file.isFile()) {
          Files.copy(file, jarOut);
        }
        jarOut.closeEntry();
      }

      if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
          queue.addAll(Arrays.asList(files));
        }
      }
    }
  }
  private static RegionFile fixNegativeOffset(File regionFileFile) {
    FMLLog.log(
        Level.WARNING,
        "Region file " + regionFileFile + " is corrupted: negative offset. Attempting to fix.");
    try {
      Files.copy(
          regionFileFile,
          new File(regionFileFile.getParentFile(), regionFileFile.getName() + ".bak"));
    } catch (IOException e) {
      FMLLog.log(Level.SEVERE, e, "Failed to back up corrupt region file.");
    }
    try {
      RandomAccessFile dataFile = new RandomAccessFile(regionFileFile, "rw");
      try {
        int length;

        if (dataFile.length() < 4096L) {
          for (length = 0; length < 1024; ++length) {
            dataFile.writeInt(0);
          }

          for (length = 0; length < 1024; ++length) {
            dataFile.writeInt(0);
          }
        }

        if ((dataFile.length() & 4095L) != 0L) {
          for (length = 0; (long) length < (dataFile.length() & 4095L); ++length) {
            dataFile.write(0);
          }
        }

        length = (int) dataFile.length() / 4096;

        dataFile.seek(0L);

        for (int i = 0; i < 1024; ++i) {
          int offset = dataFile.readInt();

          if (offset != 0 && (offset >> 8) + (offset & 255) <= length) {
            for (int var5 = 0; var5 < (offset & 255); ++var5) {
              if ((offset >> 8) + var5 < 0) {
                dataFile.seek(dataFile.getFilePointer() - 4);
                dataFile.writeInt(0);
                break;
              }
            }
          }
        }
      } finally {
        dataFile.close();
      }
    } catch (Throwable t) {
      FMLLog.log(Level.SEVERE, t, "Failed to fix negative offset index in " + regionFileFile);
      throw UnsafeUtil.throwIgnoreChecked(t);
    }
    return new RegionFile(regionFileFile);
  }
Beispiel #12
0
 @Override
 public void pushTaskLog(final String taskid, File file) throws IOException {
   if (!config.getDirectory().exists()) {
     config.getDirectory().mkdir();
   }
   final File outputFile = fileForTask(taskid);
   Files.copy(file, outputFile);
   log.info("Wrote task log to: %s", outputFile);
 }
 private File prepareInstallationDirectory() throws IOException {
   File tempDir = Files.createTempDir();
   assertTrue(new File(tempDir, "bin").mkdir());
   assertTrue(new File(tempDir, "conf").mkdir());
   Files.copy(
       new File(resourcesDirectory, "conf" + File.separator + "server.xml"),
       new File(tempDir, "conf" + File.separator + "server.xml"));
   return tempDir;
 }
Beispiel #14
0
 protected static void setupAdapter(Class<? extends ApplicationTemplate> clz) throws IOException {
   // Create a temp file to be included in the jar so that the jar is different every time even the
   // same
   // template class is given.
   File randomFile = tmpFolder.newFile();
   Location adapterJar = AppJarHelper.createDeploymentJar(locationFactory, clz, randomFile);
   File destination =
       new File(String.format("%s/%s.jar", adapterDir.getAbsolutePath(), clz.getSimpleName()));
   Files.copy(Locations.newInputSupplier(adapterJar), destination);
 }
 private File save(File src, File target) {
   try {
     Files.createParentDirs(target);
     Files.copy(src, target);
     return target;
   } catch (IOException e) {
     LOG.error(e.getMessage(), e);
     throw new RuntimeException(e);
   }
 }
Beispiel #16
0
 @Override
 public void pushTaskLog(final String taskid, File file) throws IOException {
   if (config.getDirectory().exists() || config.getDirectory().mkdirs()) {
     final File outputFile = fileForTask(taskid);
     Files.copy(file, outputFile);
     log.info("Wrote task log to: %s", outputFile);
   } else {
     throw new IOException(
         String.format("Unable to create task log dir[%s]", config.getDirectory()));
   }
 }
Beispiel #17
0
 public void download(String pathStartingWithSlash, File toFile) {
   try {
     InputSupplier<InputStream> inputSupplier = doRequest(pathStartingWithSlash);
     Files.copy(inputSupplier, toFile);
   } catch (HttpDownloader.HttpException he) {
     throw handleHttpException(he);
   } catch (Exception e) {
     throw new SonarException(
         String.format("Unable to download '%s' to: %s", pathStartingWithSlash, toFile), e);
   }
 }
 @Test
 public void testCopyStream() throws IOException {
   File tempDir = getTestTempDir();
   File subFile1 = new File(tempDir, "subFile1");
   Files.write(SOME_BYTES, subFile1);
   File subFile2 = new File(tempDir, "subFile2");
   IOUtils.copyURLToFile(subFile1.toURI().toURL(), subFile2);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   Files.copy(subFile2, baos);
   assertArrayEquals(SOME_BYTES, baos.toByteArray());
 }
    public void process(File f) throws Exception {
      if (f.isDirectory()) {
        File[] files =
            f.listFiles(
                new FilenameFilter() {

                  @Override
                  public boolean accept(File dir, String name) {
                    return name.matches(FILE_PATTERN);
                  }
                });
        for (File ff : files) {
          byte[] bindingBytes = Files.toByteArray(ff);
          this.addCurrentBinding(bindingBytes, ff.getName(), "file:" + ff.getAbsolutePath());
        }
      } else {
        String digest = new BigInteger(Files.getDigest(f, Digest.MD5.get())).abs().toString(16);
        CURRENT_PROPS.put(BINDING_CACHE_JAR_PREFIX + f.getName(), digest);
        final JarFile jar = new JarFile(f);
        final List<JarEntry> jarList = Collections.list(jar.entries());
        for (final JarEntry j : jarList) {
          try {
            if (j.getName().matches(FILE_PATTERN)) {
              byte[] bindingBytes = ByteStreams.toByteArray(jar.getInputStream(j));
              String bindingName = j.getName();
              String bindingFullPath = "jar:file:" + f.getAbsolutePath() + "!/" + bindingName;
              this.addCurrentBinding(bindingBytes, bindingName, bindingFullPath);
            } else if (j.getName().matches(".*\\.class.{0,1}")) {
              final String classGuess =
                  j.getName().replaceAll("/", ".").replaceAll("\\.class.{0,1}", "");
              final Class candidate = ClassLoader.getSystemClassLoader().loadClass(classGuess);
              if (MSG_BASE_CLASS.isAssignableFrom(candidate)
                  || MSG_DATA_CLASS.isAssignableFrom(candidate)) {
                InputSupplier<InputStream> classSupplier =
                    Resources.newInputStreamSupplier(ClassLoader.getSystemResource(j.getName()));
                File destClassFile = SubDirectory.CLASSCACHE.getChildFile(j.getName());
                if (!destClassFile.exists()) {
                  Files.createParentDirs(destClassFile);
                  Files.copy(classSupplier, destClassFile);
                  Logs.extreme()
                      .debug("Caching: " + j.getName() + " => " + destClassFile.getAbsolutePath());
                }
                BINDING_CLASS_MAP.putIfAbsent(classGuess, candidate);
              }
            }
          } catch (RuntimeException ex) {
            LOG.error(ex, ex);
            jar.close();
            throw ex;
          }
        }
        jar.close();
      }
    }
  @Test
  public void testInvalidFilesInRetryDirectory() throws Exception {
    uploader =
        new S3Uploader(
            storageSystem,
            serverConfig,
            new EventPartitioner(),
            executor,
            executor,
            s3UploaderStats);
    uploader.start();

    executor.elapseTime(1, TimeUnit.MINUTES);
    String retryDir = tempStageDir.getPath() + "/retry";
    String failedDir = tempStageDir.getPath() + "/failed";

    File invalidJsonFile = new File(retryDir, "invalidjson.snappy");
    Files.copy(new File(Resources.getResource("invalidjson.snappy").toURI()), invalidJsonFile);

    File invalidFile = new File(retryDir, "invalidFile.snappy");
    Files.copy(new File(Resources.getResource("invalidjson2.snappy").toURI()), invalidFile);

    File directory = new File(retryDir, "subdir");
    directory.mkdir();

    assertTrue(invalidJsonFile.exists());
    assertTrue(invalidFile.exists());
    assertTrue(directory.exists());

    executor.elapseTime(3, TimeUnit.MINUTES);

    assertTrue(new File(failedDir, invalidJsonFile.getName()).exists());
    assertFalse(new File(retryDir, invalidJsonFile.getName()).exists());
    assertFalse(new File(tempStageDir.getPath(), invalidJsonFile.getName()).exists());

    assertTrue(new File(failedDir, invalidFile.getName()).exists());
    assertFalse(new File(retryDir, invalidFile.getName()).exists());
    assertFalse(new File(tempStageDir.getPath(), invalidFile.getName()).exists());

    assertTrue(directory.exists());
  }
        @Override
        public void apply(OfflineCommandContext ctx) throws CommandFailedException, IOException {
          if (ConfigurationFileBackup.this.backupFile != null) {
            throw new CommandFailedException(
                "Configuration file was already backed up to "
                    + ConfigurationFileBackup.this.backupFile);
          }

          File tempFile = File.createTempFile("creaper-backup", null);
          Files.copy(ctx.configurationFile, tempFile);
          ConfigurationFileBackup.this.backupFile = tempFile;
        }
 public static void copyFileToDirectory(File sourceFile, File targetDirectory) {
   File targetFile = newFile(targetDirectory, sourceFile.getName());
   try {
     Files.copy(sourceFile, targetFile);
   } catch (IOException e) {
     throw new CommandLineExitException(
         format(
             "Error while copying file from %s to %s",
             sourceFile.getAbsolutePath(), targetFile.getAbsolutePath()),
         e);
   }
 }
 public void injectIcon(int id, final InputStream iconStream) throws IOException {
   File f = File.createTempFile("launcher", "ico");
   Files.copy(
       new InputSupplier<InputStream>() {
         @Override
         public InputStream getInput() throws IOException {
           return iconStream;
         }
       },
       f);
   IconResourceInjector iconInjector = new IconResourceInjector();
   iconInjector.injectIcon(f, myRoot, "IRD" + id);
 }
        @Override
        public void apply(OfflineCommandContext ctx) throws CommandFailedException, IOException {
          if (ConfigurationFileBackup.this.backupFile == null) {
            throw new CommandFailedException("There's no configuration file backup to restore");
          }

          Files.copy(ConfigurationFileBackup.this.backupFile, ctx.configurationFile);
          boolean deleted = ConfigurationFileBackup.this.backupFile.delete();
          if (!deleted) {
            log.errorf("Couldn't delete %s, continuing", ConfigurationFileBackup.this.backupFile);
          }
          ConfigurationFileBackup.this.backupFile = null;
        }
  /** 1. 得到需要处理的file,可能多个 2. copy 3. rename 4. 异步压缩 5. 删除之前文件 */
  @Override
  public void run() {
    String lastTime;
    if (StringUtils.equals(rollUnit, "day")) {
      lastTime = FlumeUtil.getLastDayWithDate(fileDateFormat);
    } else {
      lastTime = FlumeUtil.getLastHourWithDate(fileDateFormat);
    }

    List<String> files = FileUtil.getFiles(logDir, filePrefix, fileCompresionMode, lastTime, false);
    if (files == null || files.size() < 1) {
      LOGGER.warn("No matched logs found in {}, fileDateFormat={}", logDir, fileDateFormat);
      return;
    }
    for (String file : files) {
      String src = logDir + "/" + file;
      String copyDst = spoolDir + "/" + file + completedSuffix;
      String moveDst = spoolDir + "/" + file;

      File srcFile = new File(src);
      File copyDstFile = new File(copyDst);
      File moveDstFile = new File(moveDst);

      // copy: 从logDir中拷贝file到spoolDir中,文件名要加上completedSuffix
      try {
        Files.copy(srcFile, copyDstFile);
        LOGGER.info("Copy file {} to {}.", src, copyDst);
      } catch (IOException e) {
        LOGGER.error("Connot copy file {} to {}.", src, copyDst);
      }

      // rename: 复制完成后,将file去掉completeSuffix进行重命名
      try {
        Files.move(copyDstFile, moveDstFile);
        LOGGER.info("Move file {} to {}.", copyDst, moveDst);
      } catch (IOException e) {
        LOGGER.error("Connot move file {} to {}.", copyDst, moveDst);
      }

      // 异步压缩
      if (needCompress()) {
        String innerEntryName = FileUtil.afterLastSlash(src);
        compressFuture = compressAsynchronously(src, src, innerEntryName, fileCompresionMode, 60);
      }
    }

    // 删除之前文件
    if (needDeletePastFile()) {
      deleteFiles(logDir, filePrefix, fileCompresionMode, fileMaxHistory);
    }
  }
  @BeforeClass
  public static void createJavaProjects() throws IOException {
    JavaProjectClient.createJavaProject(PROJECT_NAME_1);

    final String content =
        CharStreams.toString(
            CharStreams.newReaderSupplier(new PMDConfigurationSupplier(), Charsets.UTF_8));
    JavaProjectClient.createFileInProject(PROJECT_NAME_1, PMD_XML, content);

    JavaProjectClient.createJavaProject(PROJECT_NAME_2);

    rules = File.createTempFile(PMDConfigurationsTest.class.getSimpleName() + "-", ".xml");
    Files.copy(new PMDConfigurationSupplier(), rules);
  }
 public static File readResourceToTempFile(String resourceName) throws IOException {
   InputSupplier<? extends InputStream> inSupplier;
   try {
     URL resourceURL = Resources.getResource(GroupLensRecommender.class, resourceName);
     inSupplier = Resources.newInputStreamSupplier(resourceURL);
   } catch (IllegalArgumentException iae) {
     File resourceFile = new File("src/main/java" + resourceName);
     inSupplier = Files.newInputStreamSupplier(resourceFile);
   }
   File tempFile = File.createTempFile("taste", null);
   tempFile.deleteOnExit();
   Files.copy(inSupplier, tempFile);
   return tempFile;
 }
  @Test
  public void testRetryUntilSuccess() throws Exception {
    storageSystem.succeedOnAttempt(3);
    File pendingFile = new File(tempStageDir, "pending.json.snappy");
    Files.copy(new File(Resources.getResource("pending.json.snappy").toURI()), pendingFile);

    String retryDir = tempStageDir.getPath() + "/retry";

    assertEquals(storageSystem.getAttempts(pendingFile), 0);
    // attempt 1 to upload from staging directory fails and file is moved to retry directory
    assertTrue(pendingFile.exists());
    uploader =
        new S3Uploader(
            storageSystem,
            serverConfig,
            new EventPartitioner(),
            executor,
            executor,
            s3UploaderStats);
    uploader.start();
    assertFalse(pendingFile.exists());
    assertTrue(new File(retryDir, pendingFile.getName()).exists());
    assertFalse(storageSystem.hasReceivedFile(pendingFile));
    assertEquals(storageSystem.getAttempts(pendingFile), 1);

    // attempt 2: file is moved to staging from retry directory, and fails and hence gets back to
    // retry directory
    executor.elapseTime(1, TimeUnit.MINUTES);
    assertFalse(pendingFile.exists());
    assertTrue(new File(retryDir, pendingFile.getName()).exists());
    assertFalse(storageSystem.hasReceivedFile(pendingFile));
    assertEquals(storageSystem.getAttempts(pendingFile), 2);

    // retryExecutor hasn't run again
    executor.elapseTime(1, TimeUnit.MINUTES);
    assertFalse(pendingFile.exists());
    assertTrue(new File(retryDir, pendingFile.getName()).exists());
    assertFalse(storageSystem.hasReceivedFile(pendingFile));
    assertEquals(storageSystem.getAttempts(pendingFile), 2);

    // attempt 3: file is moved to staging from retry directory, succeeds and hence is deleted from
    // local directories
    executor.elapseTime(2, TimeUnit.MINUTES);
    assertFalse(pendingFile.exists());
    assertFalse(new File(retryDir, pendingFile.getName()).exists());
    assertTrue(storageSystem.hasReceivedFile(pendingFile));
    assertEquals(storageSystem.getAttempts(pendingFile), 3);
  }
  private void copyFolder(File folder, File folderContainer) {
    folderContainer.mkdirs();

    for (File f : folder.listFiles()) {
      if (f.isDirectory()) {
        File newContainer = new File(folderContainer, f.getName());
        copyFolder(f, newContainer);
      }

      try {
        Files.copy(f, new File(folderContainer, f.getName()));
      } catch (IOException ex) {
        throw new MineCloudException(ex);
      }
    }
  }
  @Test
  public void shouldNotCopyFile() throws IOException {
    // given
    File folder = temporaryFolder.newFolder();
    File file = new File(folder, "config.yml");
    // purposely don't copy config.yml to verify that config.yml isn't copied by the method
    File emailJarFile = TestHelper.getJarFile("/email.html");
    Files.copy(emailJarFile, file);

    // when
    boolean result = FileUtils.copyFileFromResource(file, "config.yml");

    // then
    assertThat(result, equalTo(true));
    assertThat(file.length(), equalTo(emailJarFile.length()));
  }