Java Code Examples for com.google.common.io.Files

The following are top voted examples for showing how to use com.google.common.io.Files. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: bromium   File: DslStepsDumperTest.java   View source code 7 votes vote down vote up
@Test
public void dumpsStepsReplacingAliases() throws IOException {
    Map<String, String> firstStep = new HashMap<>();
    firstStep.put(EVENT, loadPageActionName);
    firstStep.put(aliasUrl, aliasUrlValue);

    Map<String, String> secondStep = new HashMap<>();
    secondStep.put(EVENT, typeInNameInputActionName);
    secondStep.put(aliasText, aliasTextValue);

    TestScenarioSteps testScenarioSteps = new TestScenarioSteps();
    testScenarioSteps.add(firstStep);
    testScenarioSteps.add(secondStep);


    Map<String, ApplicationActionConfiguration> actionConfigurationMap = createAliasesMockConfiguration();

    StepsDumper stepsDumper = new DslStepsDumper(actionConfigurationMap);
    stepsDumper.dump(testScenarioSteps, outputFilename);

    List<String> lines = Files.readLines(outputFile, Charsets.UTF_8);
    assertEquals(2, lines.size());
    assertEquals("loadPage: Load text-field.html page ", lines.get(0));
    assertEquals("typeInNameInput: Type admin in name input ", lines.get(1));
}
 
Example 2
Project: ProjectAres   File: MapDefinition.java   View source code 7 votes vote down vote up
public boolean shouldReload() {
    if(context == null) return true;
    if(!configuration.autoReload()) return false;
    if(context.loadedFiles().isEmpty()) return configuration.reloadWhenError();

    try {
        for(Map.Entry<Path, HashCode> loaded : context.loadedFiles().entrySet()) {
            HashCode latest = Files.hash(loaded.getKey().toFile(), Hashing.sha256());
            if(!latest.equals(loaded.getValue())) return true;
        }

        return false;
    } catch (IOException e) {
        return true;
    }
}
 
Example 3
Project: state-channels   File: FileBlockReader.java   View source code 7 votes vote down vote up
@Override
public Iterator<ChannelTransaction> readAll() throws IOException {
    close();
    ByteBuffer buffer = Files.map(file);
    return new Iterator<ChannelTransaction>() {
        @Override
        public boolean hasNext() {
            return buffer.position() < buffer.limit();
        }

        @Override
        public ChannelTransaction next() {
            int l = buffer.getInt();
            byte[] signature = new byte[CryptoUtil.SIGNATURE_LENGTH];
            buffer.get(signature);
            byte[] data = new byte[l - CryptoUtil.SIGNATURE_LENGTH];
            buffer.get(data);
            return new ChannelTransaction(blockId.getChannel(), blockId.getBlockNumber(), data, signature);
        }
    };
}
 
Example 4
Project: drift   File: PemReader.java   View source code 6 votes vote down vote up
private static PKCS8EncodedKeySpec readPrivateKey(File keyFile, Optional<String> keyPassword)
        throws IOException, GeneralSecurityException
{
    String content = Files.toString(keyFile, US_ASCII);

    Matcher matcher = KEY_PATTERN.matcher(content);
    if (!matcher.find()) {
        throw new KeyStoreException("found no private key: " + keyFile);
    }
    byte[] encodedKey = base64Decode(matcher.group(1));

    if (!keyPassword.isPresent()) {
        return new PKCS8EncodedKeySpec(encodedKey);
    }

    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(encodedKey);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    SecretKey secretKey = keyFactory.generateSecret(new PBEKeySpec(keyPassword.get().toCharArray()));

    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(DECRYPT_MODE, secretKey, encryptedPrivateKeyInfo.getAlgParameters());

    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
 
Example 5
Project: tac-kbp-eal   File: ScoreKBPAgainstERE.java   View source code 6 votes vote down vote up
public void finish() throws IOException {
  outputDir.mkdirs();

  final ImmutableSetMultimap<String, String> mentionAlignmentFailures =
      mentionAlignmentFailuresB.build();
  log.info("Of {} system responses, got {} mention alignment failures",
      numResponses.size(), mentionAlignmentFailures.size());

  final File serializedFailuresFile = new File(outputDir, "alignmentFailures.json");
  final JacksonSerializer serializer =
      JacksonSerializer.builder().forJson().prettyOutput().build();
  serializer.serializeTo(mentionAlignmentFailures, Files.asByteSink(serializedFailuresFile));

  final File failuresCount = new File(outputDir, "alignmentFailures.count.txt");
  serializer.serializeTo(mentionAlignmentFailures.size(), Files.asByteSink(failuresCount));
}
 
Example 6
Project: cmakeify   File: TestCmakeify.java   View source code 6 votes vote down vote up
@Test
public void complicatedSelfHost() throws IOException {
  File yaml = new File("test-files/complicatedSelfHost/cmakeify.yml");
  yaml.getParentFile().mkdirs();
  Files.write("includes: [extra-includes]\n" +
          "android:\n" +
          "  flavors:\n" +
          "    myflags:\n" +
          "      - -DANDROID\n" +
          "  lib: libbob.a\n" +
          "  ndk:\n" +
          "    platforms: [21, 22]\n",
      yaml, StandardCharsets.UTF_8);
  String result1 = main("-wf", yaml.getParent(), "--dump");
  yaml.delete();
  Files.write(result1, yaml, StandardCharsets.UTF_8);
  System.out.print(result1);
  String result2 = main("-wf", yaml.getParent(), "--dump");
  assertThat(result2).isEqualTo(result1);
  assertThat(result2).contains("-DANDROID");
  assertThat(result2).doesNotContain("default-flavor");
}
 
Example 7
Project: Reer   File: SimpleTemplateOperation.java   View source code 6 votes vote down vote up
@Override
public void generate() {
    try {
        target.getParentFile().mkdirs();
        SimpleTemplateEngine templateEngine = new SimpleTemplateEngine();
        String templateText = Resources.asCharSource(templateURL, CharsetToolkit.getDefaultSystemCharset()).read();
        Template template = templateEngine.createTemplate(templateText);
        Writer writer = Files.asCharSink(target, Charsets.UTF_8).openStream();
        try {
            template.make(bindings).writeTo(writer);
        } finally {
            writer.close();
        }
    } catch (Exception ex) {
        throw new GradleException("Could not generate file " + target + ".", ex);
    }
}
 
Example 8
Project: SkyDocs   File: ProjectsFrame.java   View source code 6 votes vote down vote up
/**
 * Saves the projects history.
 */

public final void saveHistory() {
	try {
		final StringBuilder builder = new StringBuilder();
		for(int i = 0; i < projectsModel.size(); i++) {
			builder.append(projectsModel.getElementAt(i) + System.lineSeparator());
		}
		Files.write(builder.toString(), new File(Utils.getParentFolder(), Constants.FILE_GUI_HISTORY), StandardCharsets.UTF_8);
	}
	catch(final Exception ex) {
		ex.printStackTrace(guiPrintStream);
		ex.printStackTrace();
		JOptionPane.showMessageDialog(ProjectsFrame.this, String.format(Constants.GUI_DIALOG_ERROR_MESSAGE, ex.getMessage()), ex.getClass().getName(), JOptionPane.ERROR_MESSAGE);
	}
}
 
Example 9
Project: QDrill   File: TestSimpleLimit.java   View source code 6 votes vote down vote up
private void verifyLimitCount(DrillbitContext bitContext, UserServer.UserClientConnection connection, String testPlan, int expectedCount) throws Throwable {
  final PhysicalPlanReader reader = new PhysicalPlanReader(c, c.getMapper(), CoordinationProtos.DrillbitEndpoint.getDefaultInstance());
  final PhysicalPlan plan = reader.readPhysicalPlan(Files.toString(FileUtils.getResourceAsFile("/limit/" + testPlan), Charsets.UTF_8));
  final FunctionImplementationRegistry registry = new FunctionImplementationRegistry(c);
  final FragmentContext context = new FragmentContext(bitContext, PlanFragment.getDefaultInstance(), connection, registry);
  final SimpleRootExec exec = new SimpleRootExec(ImplCreator.getExec(context, (FragmentRoot) plan.getSortedOperators(false).iterator().next()));
  int recordCount = 0;
  while(exec.next()) {
    recordCount += exec.getRecordCount();
  }

  assertEquals(expectedCount, recordCount);

  if(context.getFailureCause() != null) {
    throw context.getFailureCause();
  }

  assertTrue(!context.isFailed());
}
 
Example 10
Project: flume-release-1.7.0   File: TestTaildirSource.java   View source code 6 votes vote down vote up
@Test
public void testLifecycle() throws IOException, InterruptedException {
  File f1 = new File(tmpDir, "file1");
  Files.write("file1line1\nfile1line2\n", f1, Charsets.UTF_8);

  Context context = new Context();
  context.put(POSITION_FILE, posFilePath);
  context.put(FILE_GROUPS, "f1");
  context.put(FILE_GROUPS_PREFIX + "f1", tmpDir.getAbsolutePath() + "/file1$");
  Configurables.configure(source, context);

  for (int i = 0; i < 3; i++) {
    source.start();
    source.process();
    assertTrue("Reached start or error", LifecycleController.waitForOneOf(
        source, LifecycleState.START_OR_ERROR));
    assertEquals("Server is started", LifecycleState.START,
        source.getLifecycleState());

    source.stop();
    assertTrue("Reached stop or error",
        LifecycleController.waitForOneOf(source, LifecycleState.STOP_OR_ERROR));
    assertEquals("Server is stopped", LifecycleState.STOP,
        source.getLifecycleState());
  }
}
 
Example 11
Project: dble   File: DbleServer.java   View source code 6 votes vote down vote up
private void initZkDnindex() {
    //upload the dnindex data to zk
    try {
        if (dnIndexLock.acquire(30, TimeUnit.SECONDS)) {
            try {
                File file = new File(SystemConfig.getHomePath(), "conf" + File.separator + "dnindex.properties");
                String path = KVPathUtil.getDnIndexNode();
                CuratorFramework zk = ZKUtils.getConnection();
                if (zk.checkExists().forPath(path) == null) {
                    zk.create().creatingParentsIfNeeded().forPath(path, Files.toByteArray(file));
                }
            } finally {
                dnIndexLock.release();
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Project: cdep   File: TestCDep.java   View source code 6 votes vote down vote up
@Test
public void sqlite() throws Exception {
  CDepYml config = new CDepYml();
  System.out.printf(new Yaml().dump(config));
  File yaml = new File(".test-files/firebase/cdep.yml");
  yaml.getParentFile().mkdirs();
  Files.write("builders: [cmake, cmakeExamples]\n"
          + "dependencies:\n"
          + "- compile: com.github.jomof:sqlite:3.16.2-rev45\n",
      yaml, StandardCharsets.UTF_8);
  String result1 = main("show", "manifest", "-wf", yaml.getParent());
  yaml.delete();
  Files.write(result1, yaml, StandardCharsets.UTF_8);
  System.out.print(result1);
  String result = main("-wf", yaml.getParent());
  System.out.printf(result);
}
 
Example 13
Project: flow-platform   File: CredentialController.java   View source code 6 votes vote down vote up
private void handleAndroidFile(CredentialDetail detail, MultipartFile file) throws IOException {
    if (!(detail instanceof AndroidCredentialDetail)) {
        return;
    }

    if (file == null || file.isEmpty()) {
        return;
    }

    AndroidCredentialDetail androidDetail = (AndroidCredentialDetail) detail;
    String extension = Files.getFileExtension(file.getOriginalFilename());

    if (!ANDROID_EXTENSIONS.contains(extension)) {
        throw new IllegalParameterException("Illegal android cert file");
    }

    String destFileName = getFileName(file.getOriginalFilename());
    Path destPath = credentailFilePath(destFileName);

    file.transferTo(destPath.toFile());
    androidDetail.setFile(new FileResource(file.getOriginalFilename(), destPath.toString()));
}
 
Example 14
Project: CustomWorldGen   File: UserList.java   View source code 6 votes vote down vote up
public void writeChanges() throws IOException
{
    Collection<V> collection = this.values.values();
    String s = this.gson.toJson((Object)collection);
    BufferedWriter bufferedwriter = null;

    try
    {
        bufferedwriter = Files.newWriter(this.saveFile, Charsets.UTF_8);
        bufferedwriter.write(s);
    }
    finally
    {
        IOUtils.closeQuietly((Writer)bufferedwriter);
    }
}
 
Example 15
Project: train-simulator   File: StatisticsController.java   View source code 6 votes vote down vote up
@Deprecated
public void save() {
  String stats = collect();
  String filename = AppConfig.outputDir + "/" + map.getSimpleFileName() + ".csv";

  // if file exists, remove it
  (new File(filename)).delete();

  CharSink sink = Files.asCharSink(new File(filename), Charsets.UTF_8);
  try {
    sink.write(stats);
  } catch (IOException e) {
    e.printStackTrace();
  }

  logger.info("Exported statistics to: {}", filename);
}
 
Example 16
Project: tac-kbp-eal   File: TestLDCIO.java   View source code 6 votes vote down vote up
public void testArgumentRoundtrip(AssessmentSpecFormats.Format format) throws IOException {
  final File tmpDir = Files.createTempDir();
  tmpDir.deleteOnExit();

  final ArgumentStore store1 = AssessmentSpecFormats.createSystemOutputStore(tmpDir, format);
  store1.write(
      ArgumentOutput.from(docid, ImmutableList.of(arg), ImmutableMap.of(arg.item(), argMetadata)));
  store1.close();

  final ArgumentStore source2 = AssessmentSpecFormats.openSystemOutputStore(tmpDir, format);
  final ArgumentOutput rereadArg = source2.read(docid);
  source2.close();

  assertEquals(1, rereadArg.size());
  assertEquals(arg, Iterables.getFirst(rereadArg.scoredResponses(), null));
  assertEquals(argMetadata, rereadArg.metadata(arg.item()));
}
 
Example 17
Project: drift   File: ReloadableSslContext.java   View source code 6 votes vote down vote up
public boolean updateState()
        throws IOException
{
    // only check contents if length or modified time changed
    long newLastModified = file.lastModified();
    long newLength = file.length();
    if (lastModified == newLastModified && length == newLength) {
        return false;
    }

    // update stats
    lastModified = newLastModified;
    length = newLength;

    // check if contents changed
    HashCode newHashCode = Files.hash(file, sha256());
    if (Objects.equals(hashCode, newHashCode)) {
        return false;
    }
    hashCode = newHashCode;
    return true;
}
 
Example 18
Project: flume-release-1.7.0   File: TestLogFile.java   View source code 6 votes vote down vote up
@Test
public void testOperationRecordUpdater() throws Exception {
  File tempDir = Files.createTempDir();
  File temp = new File(tempDir, "temp");
  final RandomAccessFile tempFile = new RandomAccessFile(temp, "rw");
  for (int i = 0; i < 5000; i++) {
    tempFile.write(LogFile.OP_RECORD);
  }
  tempFile.seek(0);
  LogFile.OperationRecordUpdater recordUpdater = new LogFile
      .OperationRecordUpdater(temp);
  //Convert every 10th byte into a noop byte
  for (int i = 0; i < 5000; i += 10) {
    recordUpdater.markRecordAsNoop(i);
  }
  recordUpdater.close();

  tempFile.seek(0);
  // Verify every 10th byte is actually a NOOP
  for (int i = 0; i < 5000; i += 10) {
    tempFile.seek(i);
    Assert.assertEquals(LogFile.OP_NOOP, tempFile.readByte());
  }
}
 
Example 19
Project: AptSpring   File: ConfigurationTests.java   View source code 6 votes vote down vote up
@Test
public void testCycleInImports() throws IOException {
  File outputDir = Files.createTempDir();
  System.out.println("Generating into " + outputDir.getAbsolutePath());

  assertAbout(javaSources())
          .that(Arrays.asList(definitionClass, definitionClass2CausesDefinitionCycle))
          .processedWith(new VerifiedSpringConfiguration())
          .failsToCompile()
          .withErrorContaining("Cycle in @Configuration class @Imports test.TestClass1 -> test.TestClass2")
          .in(definitionClass)
          .onLine(12)
          .and()
          .withErrorContaining("Cycle in @Configuration class @Imports test.TestClass2 -> test.TestClass1")
          .in(definitionClass2CausesDefinitionCycle)
          .onLine(12);   
}
 
Example 20
Project: QDrill   File: TestOptiqPlans.java   View source code 6 votes vote down vote up
private SimpleRootExec doPhysicalTest(final DrillbitContext bitContext, UserClientConnection connection, String file)
    throws Exception {
  new NonStrictExpectations() {
    {
      bitContext.getMetrics();
      result = new MetricRegistry();
      bitContext.getAllocator();
      result = RootAllocatorFactory.newRoot(config);
      bitContext.getConfig();
      result = config;
    }
  };

  final StoragePluginRegistry reg = new StoragePluginRegistry(bitContext);

  final PhysicalPlanReader reader = new PhysicalPlanReader(config, config.getMapper(),
      CoordinationProtos.DrillbitEndpoint.getDefaultInstance(), reg);
  final PhysicalPlan plan = reader.readPhysicalPlan(Files.toString(FileUtils.getResourceAsFile(file), Charsets.UTF_8));
  final FunctionImplementationRegistry registry = new FunctionImplementationRegistry(config);
  final FragmentContext context = new FragmentContext(bitContext, PlanFragment.getDefaultInstance(), connection, registry);
  final SimpleRootExec exec = new SimpleRootExec(ImplCreator.getExec(context, (FragmentRoot) plan.getSortedOperators(false)
      .iterator().next()));
  return exec;
}
 
Example 21
Project: QDrill   File: TestUnionExchange.java   View source code 6 votes vote down vote up
@Test
public void twoBitTwoExchangeTwoEntryRun() throws Exception {
  RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();

  try (Drillbit bit1 = new Drillbit(CONFIG, serviceSet);
      Drillbit bit2 = new Drillbit(CONFIG, serviceSet);
      DrillClient client = new DrillClient(CONFIG, serviceSet.getCoordinator());) {

    bit1.run();
    bit2.run();
    client.connect();
    List<QueryDataBatch> results = client.runQuery(org.apache.drill.exec.proto.UserBitShared.QueryType.PHYSICAL,
        Files.toString(FileUtils.getResourceAsFile("/sender/union_exchange.json"),
            Charsets.UTF_8));
    int count = 0;
    for (QueryDataBatch b : results) {
      if (b.getHeader().getRowCount() != 0) {
        count += b.getHeader().getRowCount();
      }
      b.release();
    }
    assertEquals(150, count);
  }
}
 
Example 22
Project: test.rockitizer   File: FileAssertion.java   View source code 6 votes vote down vote up
@Override
public void proceed() {
	File recordFolder = new File( recordPath );
	File replayFolder = new File( replayPath );
	
	for (File recordFile : Files.fileTreeTraverser().preOrderTraversal(recordFolder)) {
		
		String relativePath = recordFolder.toURI().relativize(recordFile.toURI()).getPath();
		File replayFile = new File(replayFolder + "/" + relativePath );
		
		LOGGER.debug("asserting: source " + recordFile.getAbsolutePath() + " with target "  + replayFile.getAbsolutePath() );
		
		try { 
			assertTrue(" matching file "  + replayFile.getAbsolutePath() + " must exist", replayFile.exists()==true);
		} catch (AssertionError e) {
			throw new AssertionException(" matching file "  + replayFile.getAbsolutePath() + " must exist "  , e);
		}
			
		
	}		
}
 
Example 23
Project: QDrill   File: TestBroadcastExchange.java   View source code 6 votes vote down vote up
@Test
public void TestMultipleSendLocationBroadcastExchange() throws Exception {
  RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();

  try (Drillbit bit1 = new Drillbit(CONFIG, serviceSet);
      Drillbit bit2 = new Drillbit(CONFIG, serviceSet);
      DrillClient client = new DrillClient(CONFIG, serviceSet.getCoordinator())) {

    bit1.run();
    bit2.run();
    client.connect();

    String physicalPlan = Files.toString(
        FileUtils.getResourceAsFile("/sender/broadcast_exchange_long_run.json"), Charsets.UTF_8);
    List<QueryDataBatch> results = client.runQuery(QueryType.PHYSICAL, physicalPlan);
    int count = 0;
    for (QueryDataBatch b : results) {
      if (b.getHeader().getRowCount() != 0) {
        count += b.getHeader().getRowCount();
      }
      b.release();
    }
    System.out.println(count);
  }
}
 
Example 24
Project: anki-cards-web-browser   File: Exporter.java   View source code 6 votes vote down vote up
private void moveAndRenameImageFiles() throws AnkiExpectedExportingException {
    HashMap<String, String> imageNamesDictionary = parser.getImageNamesDictionary();

    File imagesFolder = new File(destinationFolder, "anki-images");
    //noinspection ResultOfMethodCallIgnored
    imagesFolder.mkdirs();

    imageNamesDictionary.forEach((currentName, targetName) -> {
        File originalFile    = new File(parser.getUnzippedToFolder(), currentName);
        File destinationFile = new File(imagesFolder, targetName);

        try {
            Files.copy(originalFile, destinationFile);
        } catch (IOException e) {
            throw new AnkiExpectedExportingException("Cannot move "+originalFile+" to "+destinationFile);
        }
    });
}
 
Example 25
Project: CoreX   File: ResourcePackManager.java   View source code 5 votes vote down vote up
public ResourcePackManager(File path) {
    if (!path.exists()) {
        path.mkdirs();
    } else if (!path.isDirectory()) {
        throw new IllegalArgumentException(Server.getInstance().getLanguage()
                .translateString("nukkit.resources.invalid-path", path.getName()));
    }

    List<ResourcePack> loadedResourcePacks = new ArrayList<>();
    for (File pack : path.listFiles()) {
        try {
            ResourcePack resourcePack = null;

            if (!pack.isDirectory()) { //directory resource packs temporarily unsupported
                switch (Files.getFileExtension(pack.getName())) {
                    case "zip":
                    case "mcpack":
                        resourcePack = new ZippedResourcePack(pack);
                        break;
                    default:
                        Server.getInstance().getLogger().warning(Server.getInstance().getLanguage()
                                .translateString("nukkit.resources.unknown-format", pack.getName()));
                        break;
                }
            }

            if (resourcePack != null) {
                loadedResourcePacks.add(resourcePack);
                this.resourcePacksById.put(resourcePack.getPackId(), resourcePack);
            }
        } catch (IllegalArgumentException e) {
            Server.getInstance().getLogger().warning(Server.getInstance().getLanguage()
                    .translateString("nukkit.resources.fail", pack.getName(), e.getMessage()));
        }
    }

    this.resourcePacks = loadedResourcePacks.toArray(new ResourcePack[loadedResourcePacks.size()]);
    Server.getInstance().getLogger().info(Server.getInstance().getLanguage()
            .translateString("nukkit.resources.success", String.valueOf(this.resourcePacks.length)));
}
 
Example 26
Project: QDrill   File: TestSimpleUnion.java   View source code 5 votes vote down vote up
@Test
public void testUnion(@Injectable final DrillbitContext bitContext, @Injectable UserServer.UserClientConnection connection) throws Throwable {
  new NonStrictExpectations() {{
    bitContext.getMetrics(); result = new MetricRegistry();
    bitContext.getAllocator(); result = RootAllocatorFactory.newRoot(c);
    bitContext.getOperatorCreatorRegistry(); result = new OperatorCreatorRegistry(c);
    bitContext.getConfig(); result = c;
    bitContext.getCompiler(); result = CodeCompiler.getTestCompiler(c);
  }};

  final PhysicalPlanReader reader = new PhysicalPlanReader(c, c.getMapper(), CoordinationProtos.DrillbitEndpoint.getDefaultInstance());
  final PhysicalPlan plan = reader.readPhysicalPlan(Files.toString(FileUtils.getResourceAsFile("/union/test1.json"), Charsets.UTF_8));
  final FunctionImplementationRegistry registry = new FunctionImplementationRegistry(c);
  final FragmentContext context = new FragmentContext(bitContext, PlanFragment.getDefaultInstance(), connection, registry);
  final SimpleRootExec exec = new SimpleRootExec(ImplCreator.getExec(context, (FragmentRoot) plan.getSortedOperators(false).iterator().next()));

  final int[] counts = new int[]{100,50};
  int i = 0;
  while(exec.next()) {
    System.out.println("iteration count:" + exec.getRecordCount());
    assertEquals(counts[i++], exec.getRecordCount());
  }

  if(context.getFailureCause() != null) {
    throw context.getFailureCause();
  }
  assertTrue(!context.isFailed());
}
 
Example 27
Project: flume-release-1.7.0   File: TestIntegration.java   View source code 5 votes vote down vote up
@Before
public void setup() {
  baseDir = Files.createTempDir();
  checkpointDir = new File(baseDir, "chkpt");
  Assert.assertTrue(checkpointDir.mkdirs() || checkpointDir.isDirectory());
  dataDirs = new File[3];
  dataDir = "";
  for (int i = 0; i < dataDirs.length; i++) {
    dataDirs[i] = new File(baseDir, "data" + (i + 1));
    Assert.assertTrue(dataDirs[i].mkdirs() || dataDirs[i].isDirectory());
    dataDir += dataDirs[i].getAbsolutePath() + ",";
  }
  dataDir = dataDir.substring(0, dataDir.length() - 1);
}
 
Example 28
Project: hype   File: ZipFiles.java   View source code 5 votes vote down vote up
/**
 * Private helper function for zipping files. This one goes recursively
 * through the input directory and all of its subdirectories and adds the
 * single zip entries.
 *
 * @param inputFile the file or directory to be added to the zip file
 * @param directoryName the string-representation of the parent directory
 *     name. Might be an empty name, or a name containing multiple directory
 *     names separated by "/". The directory name must be a valid name
 *     according to the file system limitations. The directory name should be
 *     empty or should end in "/".
 * @param zos the zipstream to write to
 * @throws IOException the zipping failed, e.g. because the output was not
 *     writeable.
 */
private static void zipDirectoryInternal(
    File inputFile,
    String directoryName,
    ZipOutputStream zos) throws IOException {
  String entryName = directoryName + inputFile.getName();
  if (inputFile.isDirectory()) {
    entryName += "/";

    // We are hitting a sub-directory. Recursively add children to zip in deterministic,
    // sorted order.
    File[] childFiles = inputFile.listFiles();
    if (childFiles.length > 0) {
      Arrays.sort(childFiles);
      // loop through the directory content, and zip the files
      for (File file : childFiles) {
        zipDirectoryInternal(file, entryName, zos);
      }

      // Since this directory has children, exit now without creating a zipentry specific to
      // this directory. The entry for a non-entry directory is incompatible with certain
      // implementations of unzip.
      return;
    }
  }

  // Put the zip-entry for this file or empty directory into the zipoutputstream.
  ZipEntry entry = new ZipEntry(entryName);
  entry.setTime(inputFile.lastModified());
  zos.putNextEntry(entry);

  // Copy file contents into zipoutput stream.
  if (inputFile.isFile()) {
    Files.asByteSource(inputFile).copyTo(zos);
  }
}
 
Example 29
Project: flume-release-1.7.0   File: TestResettableFileInputStream.java   View source code 5 votes vote down vote up
private static List<String> multiLineFileInit(File file, Charset charset)
    throws IOException {
  List<String> lines = Lists.newArrayList();
  lines.add("1. On the planet of Mars\n");
  lines.add("2. They have clothes just like ours,\n");
  lines.add("3. And they have the same shoes and same laces,\n");
  lines.add("4. And they have the same charms and same graces...\n");
  StringBuilder sb = new StringBuilder();
  for (String line : lines) {
    sb.append(line);
  }
  Files.write(sb.toString().getBytes(charset), file);
  return lines;
}
 
Example 30
Project: circus-train   File: CircusTrainTest.java   View source code 5 votes vote down vote up
@Test
public void twoYmlFiles() throws Exception {
  exit.expectSystemExitWithStatus(0);
  File ymlFile1 = temp.newFile("test-application1.yml");
  File ymlFile2 = temp.newFile("test-application2.yml");

  List<String> lines = ImmutableList
      .<String> builder()
      .add("source-catalog:")
      .add("  name: source")
      .add("  configuration-properties:")
      .add("    " + ConfVars.METASTOREURIS.varname + ": " + hive.getThriftConnectionUri())
      .add("replica-catalog:")
      .add("  name: replica")
      .add("  hive-metastore-uris: " + hive.getThriftConnectionUri())
      .build();
  Files.asCharSink(ymlFile1, UTF_8).writeLines(lines);

  lines = ImmutableList
      .<String> builder()
      .add("table-replications:")
      .add("  -")
      .add("    source-table:")
      .add("      database-name: " + DATABASE)
      .add("      table-name: source_" + TABLE)
      .add("    replica-table:")
      .add("      table-name: replica_" + TABLE)
      .add("      table-location: " + temp.newFolder("replica"))
      .build();
  Files.asCharSink(ymlFile2, UTF_8).writeLines(lines);

  exit.checkAssertionAfterwards(new Assertion() {
    @Override
    public void checkAssertion() throws Exception {
      assertTrue(hive.client().tableExists(DATABASE, "replica_" + TABLE));
    }
  });

  CircusTrain.main(new String[] { "--config=" + ymlFile1.getAbsolutePath() + "," + ymlFile2.getAbsolutePath() });
}
 
Example 31
Project: filestack-java   File: TestTransforms.java   View source code 5 votes vote down vote up
@Test
public void testAvTransform() throws Exception {
  ClassLoader loader = Thread.currentThread().getContextClassLoader();
  String oggPath = loader.getResource("com/filestack/sample_music.ogg").getPath();
  File oggFile = new File(oggPath);

  FileLink oggFileLink = client.upload(oggPath, false);
  HANDLES.add(oggFileLink.getHandle());

  AvTransformOptions options = new AvTransformOptions.Builder()
      .preset("mp3")
      .build();

  AvTransform transform = oggFileLink.avTransform(options);

  FileLink mp3FileLink;
  while ((mp3FileLink = transform.getFileLink()) == null) {
    Thread.sleep(5 * 1000);
  }
  HANDLES.add(mp3FileLink.getHandle());

  String mp3Path = loader.getResource("com/filestack/sample_music.mp3").getPath();
  File mp3File = new File(mp3Path);

  String correct = Files.asByteSource(mp3File).hash(Hashing.sha256()).toString();
  byte[] bytes = mp3FileLink.getContent().bytes();
  String output = Hashing.sha256().hashBytes(bytes).toString();

  Assert.assertEquals(correct, output);
}
 
Example 32
Project: datarouter   File: CopyFileConfigStrategy.java   View source code 5 votes vote down vote up
@Override
public void configure(String configDirectory){
	File sourceFile = new File(sourceFileLocation);
	File destinationFile = new File(configDirectory + "/" + destinationFileName);
	if(destinationFile.exists()){
		logger.warn("replacing {} with {}", destinationFile.getAbsolutePath(), sourceFile.getAbsolutePath());
	}else{
		logger.warn("creating {} from {}", destinationFile.getAbsolutePath(), sourceFile.getAbsolutePath());
	}
	try{
		Files.copy(sourceFile, destinationFile);
	}catch(IOException e){
		throw new RuntimeException(e);
	}
}
 
Example 33
Project: BaseClient   File: PlayerProfileCache.java   View source code 5 votes vote down vote up
/**
 * Load the cached profiles from disk
 */
public void load()
{
    BufferedReader bufferedreader = null;

    try
    {
        bufferedreader = Files.newReader(this.usercacheFile, Charsets.UTF_8);
        List<PlayerProfileCache.ProfileEntry> list = (List)this.gson.fromJson((Reader)bufferedreader, TYPE);
        this.usernameToProfileEntryMap.clear();
        this.uuidToProfileEntryMap.clear();
        this.gameProfiles.clear();

        for (PlayerProfileCache.ProfileEntry playerprofilecache$profileentry : Lists.reverse(list))
        {
            if (playerprofilecache$profileentry != null)
            {
                this.addEntry(playerprofilecache$profileentry.getGameProfile(), playerprofilecache$profileentry.getExpirationDate());
            }
        }
    }
    catch (FileNotFoundException var9)
    {
        ;
    }
    catch (JsonParseException var10)
    {
        ;
    }
    finally
    {
        IOUtils.closeQuietly((Reader)bufferedreader);
    }
}
 
Example 34
Project: Reer   File: JavaInstallationProbe.java   View source code 5 votes vote down vote up
private EnumMap<SysProp, String> getMetadataInternal(File jdkPath) {
    JavaExecAction exec = factory.newJavaExecAction();
    exec.executable(javaExe(jdkPath, "java"));
    File workingDir = Files.createTempDir();
    exec.setWorkingDir(workingDir);
    exec.setClasspath(new SimpleFileCollection(workingDir));
    try {
        writeProbe(workingDir);
        exec.setMain(JavaProbe.CLASSNAME);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        exec.setStandardOutput(baos);
        ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
        exec.setErrorOutput(errorOutput);
        exec.setIgnoreExitValue(true);
        ExecResult result = exec.execute();
        int exitValue = result.getExitValue();
        if (exitValue == 0) {
            return parseExecOutput(baos.toString());
        }
        return error("Command returned unexpected result code: " + exitValue + "\nError output:\n" + errorOutput);
    } catch (ExecException ex) {
        return error(ex.getMessage());
    } finally {
        try {
            FileUtils.deleteDirectory(workingDir);
        } catch (IOException e) {
            throw new GradleException("Unable to delete temp directory", e);
        }
    }
}
 
Example 35
Project: Reer   File: TestScenarioSelector.java   View source code 5 votes vote down vote up
private void addToScenarioList(String testId, Set<String> templates, File scenarioList, ResultsStore resultsStore) {
    try {
        long estimatedRuntime = getEstimatedRuntime(testId, resultsStore);
        List<String> args = Lists.newArrayList();
        args.add(testId);
        args.add(String.valueOf(estimatedRuntime));
        args.addAll(templates);
        Files.touch(scenarioList);
        Files.append(Joiner.on(';').join(args) + '\n', scenarioList, Charsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException("Could not write to scenario list at " + scenarioList, e);
    }
}
 
Example 36
Project: UltimateSpawn   File: FileUtils.java   View source code 5 votes vote down vote up
/**
 * Move the specified file to the specified location.
 *
 * @param from The original file
 * @param to The destination file for the output
 */
public static void move(File from, File to) {
    try {
        Files.move(from, to);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 37
Project: dremio-oss   File: TestNewAggregateFunctions.java   View source code 5 votes vote down vote up
public void runTest(String physicalPlan, String inputDataFile,
    Object[] expected) throws Exception {
  try (ClusterCoordinator clusterCoordinator = LocalClusterCoordinator.newRunningCoordinator();
      SabotNode bit = new SabotNode(DEFAULT_SABOT_CONFIG, clusterCoordinator, CLASSPATH_SCAN_RESULT);
      DremioClient client = new DremioClient(DEFAULT_SABOT_CONFIG,
          clusterCoordinator)) {

    // run query.
    bit.run();
    client.connect();
    List<QueryDataBatch> results = client.runQuery(
        QueryType.PHYSICAL,
        Files.toString(FileUtils.getResourceAsFile(physicalPlan),
            Charsets.UTF_8).replace("#{TEST_FILE}",
            inputDataFile));

    try(RecordBatchLoader batchLoader = new RecordBatchLoader(bit
        .getContext().getAllocator())) {

      QueryDataBatch batch = results.get(1);
      assertTrue(batchLoader.load(batch.getHeader().getDef(),
          batch.getData()));

      int i = 0;
      for (VectorWrapper<?> v : batchLoader) {
        ValueVector.Accessor accessor = v.getValueVector().getAccessor();
        System.out.println((accessor.getObject(0)));
        assertEquals(expected[i++], (accessor.getObject(0)));
      }
    }
    for (QueryDataBatch b : results) {
      b.release();
    }
  }
}
 
Example 38
Project: QDrill   File: TestTraceMultiRecordBatch.java   View source code 5 votes vote down vote up
@Test
public void testFilter(@Injectable final DrillbitContext bitContext, @Injectable UserClientConnection connection) throws Throwable {
  new NonStrictExpectations() {{
    bitContext.getMetrics(); result = new MetricRegistry();
    bitContext.getAllocator(); result = RootAllocatorFactory.newRoot(c);
    bitContext.getConfig(); result = c;
    bitContext.getOperatorCreatorRegistry(); result = new OperatorCreatorRegistry(c);
    bitContext.getCompiler(); result = CodeCompiler.getTestCompiler(c);
  }};

  final PhysicalPlanReader reader = new PhysicalPlanReader(c, c.getMapper(), CoordinationProtos.DrillbitEndpoint.getDefaultInstance());
  final PhysicalPlan plan = reader.readPhysicalPlan(Files.toString(FileUtils.getResourceAsFile("/trace/multi_record_batch_trace.json"), Charsets.UTF_8));
  final FunctionImplementationRegistry registry = new FunctionImplementationRegistry(c);
  final FragmentContext context = new FragmentContext(bitContext, PlanFragment.getDefaultInstance(), connection, registry);
  final SimpleRootExec exec = new SimpleRootExec(ImplCreator.getExec(context, (FragmentRoot) plan.getSortedOperators(false).iterator().next()));

  while(exec.next()) {
    for(final ValueVector vv: exec){
      vv.clear();
    }
  }

  exec.close();

  if(context.getFailureCause() != null) {
    throw context.getFailureCause();
  }
  assertTrue(!context.isFailed());
}
 
Example 39
Project: Reer   File: FileCollectionBackedTextResource.java   View source code 5 votes vote down vote up
public Reader asReader() {
    File file = asFile();
    try {
        return Files.newReader(asFile(), charset);
    } catch (FileNotFoundException e) {
        throw ResourceExceptions.readMissing(file, e);
    }
}
 
Example 40
Project: Reer   File: FileCollectionBackedArchiveTextResource.java   View source code 5 votes vote down vote up
public FileCollectionBackedArchiveTextResource(final FileOperations fileOperations,
                                               final TemporaryFileProvider tempFileProvider,
                                               final FileCollection fileCollection,
                                               final String path, Charset charset) {
    super(tempFileProvider, new LazilyInitializedFileCollection() {
        @Override
        public String getDisplayName() {
            return String.format("entry '%s' in archive %s", path, fileCollection);
        }

        @Override
        public FileCollection createDelegate() {
            File archiveFile = fileCollection.getSingleFile();
            String fileExtension = Files.getFileExtension(archiveFile.getName());
            FileTree archiveContents = fileExtension.equals("jar") || fileExtension.equals("zip")
                ? fileOperations.zipTree(archiveFile) : fileOperations.tarTree(archiveFile);
            PatternSet patternSet = new PatternSet();
            patternSet.include(path);
            return archiveContents.matching(patternSet);
        }

        @Override
        public void visitDependencies(TaskDependencyResolveContext context) {
            context.add(fileCollection);
        }
    }, charset);
}