Beispiel #1
0
 public static void main(String[] args) throws IOException {
   List<SystemProperty> properties = SystemPropertiesParser.parse();
   System.out.println(NORMAL_GSON.toJson(properties));
   Files.write(Paths.get("system_properties_min.json"), NORMAL_GSON.toJson(properties).getBytes());
   Files.write(Paths.get("system_properties.json"), PRETTY_GSON.toJson(properties).getBytes());
   createReadMe(properties);
 }
Beispiel #2
0
  /**
   * By rotating hoge.E and hoge.N, output hoge.R and hoge.T The rotation is done by SAC.
   *
   * @param sacEPath SAC file which component is E. must exist.
   * @param sacNPath SAC file which component is N. must exist.
   * @param outputRPath for output SAC with respect to R
   * @param outputTPath for output SAC with respect to T
   * @return if the output is successful
   * @throws IOException if an I/O error occurs. If sacEPath or sacNPath does not exist, if output
   *     Paths already exist.
   */
  public static boolean rotate(Path sacEPath, Path sacNPath, Path outputRPath, Path outputTPath)
      throws IOException {
    if (Files.exists(outputRPath)) throw new FileAlreadyExistsException(outputRPath.toString());
    if (Files.exists(outputTPath)) throw new FileAlreadyExistsException(outputTPath.toString());

    // read headers of the input files
    Map<SACHeaderEnum, String> mapE = readHeader(sacEPath);
    Map<SACHeaderEnum, String> mapN = readHeader(sacNPath);

    int npts = Integer.parseInt(mapE.get(SACHeaderEnum.NPTS));
    if (npts != Integer.parseInt(mapN.get(SACHeaderEnum.NPTS))) return false;

    double cmpazE = Double.parseDouble(mapE.get(SACHeaderEnum.CMPAZ));
    double cmpazN = Double.parseDouble(mapN.get(SACHeaderEnum.CMPAZ));
    double dCmpaz = Math.abs(cmpazE - cmpazN);
    if (dCmpaz != 90) return false;

    // sacを開く
    try (Sac sacProcess = Sac.createProcess()) {
      Path rPath = outputRPath.getFileName();
      Path tPath = outputTPath.getFileName();
      sacProcess.inputCMD("cd " + sacNPath.toAbsolutePath().getParent());
      sacProcess.inputCMD("r " + sacNPath.getFileName() + " " + sacEPath.getFileName());
      sacProcess.inputCMD("rotate r");
      sacProcess.inputCMD("w " + rPath + " " + tPath);
      sacProcess.inputCMD("r " + rPath);
      sacProcess.inputCMD("chnhdr kcmpnm \"radial\"");
      sacProcess.inputCMD("write over");
      sacProcess.inputCMD("r " + tPath);
      sacProcess.inputCMD("chnhdr kcmpnm \"trnsvers\"");
      sacProcess.inputCMD("write over");
    }
    return true;
  }
  /**
   * Test method for {@link SubtitleProvider#loadSubtitles(Path)}. Check that multiple subtitles are
   * loaded.
   *
   * @throws IOException if there was an I/O error.
   */
  @Test
  public void testLoadSubtitles() throws IOException {
    final SubtitleFormat subtitleFormat = mock(SubtitleFormat.class);
    final SubtitleReader subtitleReader = mock(SubtitleReader.class);
    final SubtitleFile subtitleFile = mock(SubtitleFile.class);
    final SubtitleFile secondSubtitle = mock(SubtitleFile.class);
    final Path folder = subtitleFolder.newFolder().toPath();
    final Path file = folder.resolve("single.srt");
    final Path second = folder.resolve("other.srt");
    Files.createFile(file);
    Files.createFile(second);
    Files.createFile(folder.resolve("test.sub"));

    when(subtitleFormatManager.getFormatByPath(file))
        .thenReturn(new HashSet<>(Arrays.asList(subtitleFormat)));
    when(subtitleFormatManager.getFormatByPath(second))
        .thenReturn(new HashSet<>(Arrays.asList(subtitleFormat)));
    when(subtitleFormat.getReader()).thenReturn(subtitleReader);
    when(subtitleReader.readFile(file)).thenReturn(subtitleFile);
    when(subtitleReader.readFile(second)).thenReturn(secondSubtitle);
    final Map<SubtitleFile, SubtitleFormat> subtitles = subtitleProvider.loadSubtitles(folder);
    assertEquals(2, subtitles.size());
    //		final Entry<SubtitleFile, SubtitleFormat> loaded = subtitles.entrySet().iterator().next();
    //		assertEquals(subtitleFile, loaded.getKey());
    //		assertEquals(subtitleFormat, loaded.getValue());
  }
Beispiel #4
0
 /**
  * Unpacks a jar file to a temporary directory that will be removed when the VM exits.
  *
  * @param jarfilePath the path of the jar to unpack
  * @return the path of the temporary directory
  */
 private static String explodeJarToTempDir(File jarfilePath) {
   try {
     final Path jarfileDir = Files.createTempDirectory(jarfilePath.getName());
     Runtime.getRuntime()
         .addShutdownHook(
             new Thread() {
               @Override
               public void run() {
                 delete(jarfileDir.toFile());
               }
             });
     jarfileDir.toFile().deleteOnExit();
     JarFile jarfile = new JarFile(jarfilePath);
     Enumeration<JarEntry> entries = jarfile.entries();
     while (entries.hasMoreElements()) {
       JarEntry e = entries.nextElement();
       if (!e.isDirectory()) {
         File path = new File(jarfileDir.toFile(), e.getName().replace('/', File.separatorChar));
         File dir = path.getParentFile();
         dir.mkdirs();
         assert dir.exists();
         Files.copy(jarfile.getInputStream(e), path.toPath());
       }
     }
     return jarfileDir.toFile().getAbsolutePath();
   } catch (IOException e) {
     throw new AssertionError(e);
   }
 }
  @Test
  public void testRemoveAttachment() throws Exception {

    // CREATING ATTACHMENT FOR REMOVE
    List<MultipartFile> files = new ArrayList<>();
    files.add(multipartFile);

    Mockito.when(attachmentFactory.newAttachment()).thenReturn(null);

    attachmentService.setAttachment(new Attachment());
    attachmentService.setMessageResponse(new MessageResponse());

    MessageResponse messageResponse1 = attachmentService.createAttachment(files);

    Mockito.verify(attachmentDao, Mockito.times(1)).createAttachments(argumentCaptor.capture());

    Attachment attachment = argumentCaptor.getValue().get(0);

    // REMOVE ATTACHMENT
    Mockito.when(attachmentDao.removeAttachment(attachment.getAttachmentId()))
        .thenReturn(attachment);

    MessageResponse messageResponse =
        attachmentService.removeAttachment(attachment.getAttachmentId());

    boolean isExistPreview = Files.exists(Paths.get(storagePath + attachment.getPreviewPath()));
    boolean isExistImage = Files.exists(Paths.get(storagePath + attachment.getFilePathInStorage()));

    Assert.assertTrue(!isExistPreview && !isExistImage && messageResponse.getCode() == 1);
  }
Beispiel #6
0
 public void drop() throws Exception {
   for (int i = 0; i < 16; i++) {
     File subDir = new File(mainDir, String.valueOf(i) + ".dir");
     for (int j = 0; j < 16; j++) {
       if (databases[i][j] != null) {
         File dbFile = new File(subDir, String.valueOf(j) + ".dat");
         if (dbFile.exists()) {
           try {
             Files.delete(dbFile.toPath());
           } catch (SecurityException | IOException e) {
             throw new Exception("Access violation: cannon delete database file");
           }
         }
       }
     }
     if (subDir.exists()) {
       try {
         Files.delete(subDir.toPath());
       } catch (DirectoryNotEmptyException e) {
         throw new Exception("Cannot remove table subdirectory. Redundant files");
       } catch (SecurityException | IOException e) {
         throw new Exception("Access violation: cannot delete database subdirectory");
       }
     }
   }
   try {
     Files.delete(mainDir.toPath());
   } catch (DirectoryNotEmptyException e) {
     throw new Exception("Cannot remove main table directory. Redundant files");
   } catch (SecurityException | IOException e) {
     throw new Exception("Access violation: cannot delete main database directory");
   }
 }
  @Override
  public Path getCommandPath(Path command) {
    if (command.isAbsolute()) {
      return command;
    }

    if (Platform.getOS().equals(Platform.OS_WIN32)) {
      if (!command.toString().endsWith(".exe")) { // $NON-NLS-1$
        command = Paths.get(command.toString() + ".exe"); // $NON-NLS-1$
      }
    }

    if (path != null) {
      for (Path p : path) {
        Path c = p.resolve(command);
        if (Files.isExecutable(c)) {
          return c;
        }
      }
    }

    // Look for it in the path environment var
    IEnvironmentVariable myPath = getVariable("PATH"); // $NON-NLS-1$
    String path = myPath != null ? myPath.getValue() : System.getenv("PATH"); // $NON-NLS-1$
    for (String entry : path.split(File.pathSeparator)) {
      Path entryPath = Paths.get(entry);
      Path cmdPath = entryPath.resolve(command);
      if (Files.isExecutable(cmdPath)) {
        return cmdPath;
      }
    }

    return null;
  }
  private static String getCustomProperties() throws ReporterException {
    if (System.getProperty("reporter.config") == null) {
      return null;
    }
    File propFile = new File(System.getProperty("reporter.config"));
    if (!propFile.exists() || propFile.isDirectory()) {
      return null;
    }

    try {
      File file = Files.createTempFile("reporter", ".properties").toFile();
      file.deleteOnExit();
      FileWriter fw = new FileWriter(file);
      String fileText = new String(Files.readAllBytes(propFile.toPath()));

      // replace variables
      fileText =
          fileText.replace(
              "${file.separator}", Matcher.quoteReplacement(System.getProperty("file.separator")));
      fileText =
          fileText.replace("${user.dir}", Matcher.quoteReplacement(System.getProperty("user.dir")));

      fw.write(fileText);
      fw.close();
      return file.getAbsolutePath();
    } catch (IOException e) {
      throw new ReporterException(
          "Failed to read custom config file at: " + propFile.getAbsolutePath());
    }
  }
 private Path extractIfRequired(final Path buildDir)
     throws MojoFailureException, MojoExecutionException {
   if (jbossHome != null) {
     // we do not need to download WildFly
     return Paths.get(jbossHome);
   }
   final String artifact =
       ArtifactNameSplitter.of(this.artifact)
           .setArtifactId(artifactId)
           .setClassifier(classifier)
           .setGroupId(groupId)
           .setPackaging(packaging)
           .setVersion(version)
           .asString();
   final Path result = artifactResolver.resolve(project, artifact).toPath();
   final Path target = buildDir.resolve(WILDFLY_DIR);
   // Delete the target if it exists
   if (Files.exists(target)) {
     try {
       Archives.deleteDirectory(target);
     } catch (IOException e) {
       throw new MojoFailureException("Could not delete target directory: " + target, e);
     }
   }
   try {
     Archives.unzip(result, target);
     final Iterator<Path> iterator = Files.newDirectoryStream(target).iterator();
     if (iterator.hasNext()) return iterator.next();
   } catch (IOException e) {
     throw new MojoFailureException("Artifact was not successfully extracted: " + result, e);
   }
   throw new MojoFailureException("Artifact was not successfully extracted: " + result);
 }
  @Test
  public void frameworkDependenciesDoNotContainTransitiveDependencies() throws Exception {
    assumeTrue(Platform.detect() == Platform.MACOS);
    assumeTrue(AppleNativeIntegrationTestUtils.isApplePlatformAvailable(ApplePlatform.MACOSX));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_with_library_dependencies", tmp);
    workspace.setUp();
    ProjectFilesystem filesystem = new ProjectFilesystem(workspace.getDestPath());

    BuildTarget target =
        BuildTargetFactory.newInstance(
            "//Libraries/TestLibrary:TestLibrary#framework,macosx-x86_64");
    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand("build", target.getFullyQualifiedName());
    result.assertSuccess();

    Path frameworkPath =
        workspace.getPath(
            BuildTargets.getGenPath(
                    filesystem,
                    BuildTarget.builder(target)
                        .addFlavors(AppleDebugFormat.DWARF.getFlavor())
                        .addFlavors(AppleDescriptions.INCLUDE_FRAMEWORKS_FLAVOR)
                        .build(),
                    "%s")
                .resolve("TestLibrary.framework"));
    assertThat(Files.exists(frameworkPath), is(true));
    Path frameworksPath = frameworkPath.resolve("Frameworks");
    assertThat(Files.exists(frameworksPath), is(true));
    Path depFrameworksPath = frameworksPath.resolve("TestLibraryDep.framework/Frameworks");
    assertThat(Files.exists(depFrameworksPath), is(false));
  }
  @Test
  public void noIncludeFrameworksDoesntContainFrameworkDependencies() throws Exception {
    assumeTrue(Platform.detect() == Platform.MACOS);
    assumeTrue(AppleNativeIntegrationTestUtils.isApplePlatformAvailable(ApplePlatform.MACOSX));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_with_library_dependencies", tmp);
    workspace.setUp();
    ProjectFilesystem filesystem = new ProjectFilesystem(workspace.getDestPath());

    BuildTarget target =
        BuildTargetFactory.newInstance(
            "//Libraries/TestLibrary:TestLibrary#"
                + "dwarf-and-dsym,framework,macosx-x86_64,no-include-frameworks");
    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand("build", target.getFullyQualifiedName());
    result.assertSuccess();

    Path frameworkPath =
        workspace.getPath(
            BuildTargets.getGenPath(filesystem, target, "%s").resolve("TestLibrary.framework"));
    assertThat(Files.exists(frameworkPath), is(true));
    assertThat(Files.exists(frameworkPath.resolve("Resources/Info.plist")), is(true));
    Path libraryPath = frameworkPath.resolve("TestLibrary");
    assertThat(Files.exists(libraryPath), is(true));
    assertThat(
        workspace.runCommand("file", libraryPath.toString()).getStdout().get(),
        containsString("dynamically linked shared library"));
    Path frameworksPath = frameworkPath.resolve("Contents/Frameworks");
    assertThat(Files.exists(frameworksPath), is(false));
  }
  @Test
  public void testAppleDynamicLibraryWithDsym() throws Exception {
    assumeTrue(Platform.detect() == Platform.MACOS);
    assumeTrue(AppleNativeIntegrationTestUtils.isApplePlatformAvailable(ApplePlatform.MACOSX));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(this, "apple_library_shared", tmp);
    workspace.setUp();
    ProjectFilesystem filesystem = new ProjectFilesystem(workspace.getDestPath());

    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand(
            "build",
            "//Libraries/TestLibrary:TestLibrary#shared,macosx-x86_64,dwarf-and-dsym",
            "--config",
            "cxx.cflags=-g");
    result.assertSuccess();

    Path output =
        tmp.getRoot()
            .resolve(filesystem.getBuckPaths().getGenDir())
            .resolve("Libraries/TestLibrary/TestLibrary#macosx-x86_64,shared")
            .resolve("libLibraries_TestLibrary_TestLibrary.dylib");
    assertThat(Files.exists(output), is(true));

    Path dsymPath =
        tmp.getRoot()
            .resolve(filesystem.getBuckPaths().getGenDir())
            .resolve("Libraries/TestLibrary")
            .resolve("TestLibrary#apple-dsym,macosx-x86_64,shared.dSYM");
    assertThat(Files.exists(dsymPath), is(true));
    AppleDsymTestUtil.checkDsymFileHasDebugSymbol("+[TestClass answer]", workspace, dsymPath);
  }
  @Test
  public void testAppleLibraryBuildsFramework() throws Exception {
    assumeTrue(Platform.detect() == Platform.MACOS);
    assumeTrue(AppleNativeIntegrationTestUtils.isApplePlatformAvailable(ApplePlatform.MACOSX));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_builds_something", tmp);
    workspace.setUp();
    ProjectFilesystem filesystem = new ProjectFilesystem(workspace.getDestPath());

    BuildTarget target =
        BuildTargetFactory.newInstance(
            "//Libraries/TestLibrary:TestLibrary#framework,macosx-x86_64,no-debug");
    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand("build", target.getFullyQualifiedName());
    result.assertSuccess();

    Path frameworkPath =
        workspace.getPath(
            BuildTargets.getGenPath(
                    filesystem,
                    BuildTarget.builder(target)
                        .addFlavors(AppleDescriptions.INCLUDE_FRAMEWORKS_FLAVOR)
                        .build(),
                    "%s")
                .resolve("TestLibrary.framework"));
    assertThat(Files.exists(frameworkPath), is(true));
    assertThat(Files.exists(frameworkPath.resolve("Resources/Info.plist")), is(true));
    Path libraryPath = frameworkPath.resolve("TestLibrary");
    assertThat(Files.exists(libraryPath), is(true));
    assertThat(
        workspace.runCommand("file", libraryPath.toString()).getStdout().get(),
        containsString("dynamically linked shared library"));
  }
Beispiel #14
0
  /** Start the logging system. We must define a new log to store everything. */
  public synchronized void createLog() {
    if (logPath == null) {
      String id = String.format("log_%d", System.currentTimeMillis());
      try {
        // Try to close the old channel.
        if (logChannel != null) {
          logChannel.close();
        }

        logPath =
            Paths.get(db.getLogPath() + System.getProperty("file.separator") + id).toAbsolutePath();
        if (!Files.exists(logPath)) {
          // Create the necessary parent directories.
          Files.createDirectories(Paths.get(db.getLogPath()).toAbsolutePath());

          // Make sure that our WAL is created before we return.
          logPath = Files.createFile(logPath);

          // Open up the line of communication.
          String flag = "rw";
          if (flushPolicy == Flush.SAFE) {
            flag += "d";
          }
          logChannel = new RandomAccessFile(logPath.toString(), flag).getChannel();

          // Map the file.
          logBuffer = mapWAL();
        }
      } catch (IOException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  public static void reconstructTurtle(File partFolder, File reconstructed) throws IOException {
    Path tmpOut = Files.createTempFile(partFolder.toPath(), "reconstr", ".tmp");
    FileOutputStream dstOut = new FileOutputStream(tmpOut.toFile());
    FileChannel dstOutChannel = dstOut.getChannel();
    try {
      if (!Files.isDirectory(partFolder.toPath()))
        throw new IOException("Not a directory: " + partFolder);
      File[] fileList =
          FileUtils.listFiles(partFolder, new PrefixFileFilter("part"), TrueFileFilter.TRUE)
              .toArray(new File[0]);
      Arrays.sort(fileList);
      RandomAccessFile inputFile;

      inputFile = new RandomAccessFile(fileList[0], "r");
      inputFile.getChannel().transferTo(0, inputFile.length(), dstOutChannel);
      inputFile.close();
      for (int i = 1; i < fileList.length; i++) {
        inputFile = new RandomAccessFile(fileList[i], "r");
        long lastPrefix = findTurtlePrefixEnd(inputFile);
        inputFile
            .getChannel()
            .transferTo(lastPrefix, inputFile.length() - lastPrefix, dstOutChannel);
        inputFile.close();
      }
    } finally {
      dstOut.close();
    }
    Files.move(
        tmpOut,
        reconstructed.toPath(),
        StandardCopyOption.ATOMIC_MOVE,
        StandardCopyOption.REPLACE_EXISTING);
    FileUtils.deleteQuietly(tmpOut.toFile());
    FileUtils.deleteQuietly(partFolder);
  }
 public static Path storeInTemp(String pluginName, InputStream in, SitePaths sitePaths)
     throws IOException {
   if (!Files.exists(sitePaths.tmp_dir)) {
     Files.createDirectories(sitePaths.tmp_dir);
   }
   return asTemp(in, tempNameFor(pluginName), ".jar", sitePaths.tmp_dir);
 }
  public void event(WatchEvent.Kind kind, Path path) {
    // System.out.println("Log event");

    File file = new File(path.toString());
    String ip = "0.0.0.0", owner = "unknown";
    try {
      ip = Inet4Address.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    try {
      FileOwnerAttributeView ownerAttributeView =
          Files.getFileAttributeView(path, FileOwnerAttributeView.class);
      owner = ownerAttributeView.getOwner().getName();
    } catch (IOException e) {
      e.printStackTrace();
    }

    String entry =
        String.format(
            "[%s] \"%s\" copied on \"%s\" by \"%s\"\n",
            new SimpleDateFormat("dd/MMM/YYYY HH:mm:ss").format(new Date()),
            path.toString(),
            ip,
            owner);

    try {
      Files.write(Paths.get(logFile), entry.getBytes(), StandardOpenOption.APPEND);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Beispiel #18
0
  @Override
  public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
    checkCreate(qc);

    final Path path = toPath(0, qc);
    final B64 archive = toB64(exprs[1], qc, false);
    final TokenSet hs = entries(2, qc);

    try (ArchiveIn in = ArchiveIn.get(archive.input(info), info)) {
      while (in.more()) {
        final ZipEntry ze = in.entry();
        final String name = ze.getName();
        if (hs == null || hs.delete(token(name)) != 0) {
          final Path file = path.resolve(name);
          if (ze.isDirectory()) {
            Files.createDirectories(file);
          } else {
            Files.createDirectories(file.getParent());
            Files.write(file, in.read());
          }
        }
      }
    } catch (final IOException ex) {
      throw ARCH_FAIL_X.get(info, ex);
    }
    return null;
  }
Beispiel #19
0
  private static void pathesAndFileCreation() throws IOException {
    System.out.println("yes\\no");
    System.out.println("c:\\program files\\myProgram");
    System.out.println("c:/program files/myProgram");
    String win = "c:\\HaxLogs.txt";
    String unix = "c:/HaxLogs.txt";

    File winFile = new File(win);
    System.out.println("win file exists: " + winFile.exists());

    File unixFile = new File(unix);
    System.out.println("unix file exists: " + unixFile.exists());

    Path path = Paths.get("myFile.txt");
    System.out.println(path);
    System.out.println(path.toAbsolutePath());

    Path absolutePath = Paths.get("d:/myFile.txt");
    System.out.println(absolutePath);
    System.out.println(absolutePath.toAbsolutePath());

    if (!Files.exists(absolutePath)) {
      Files.createFile(absolutePath);
      System.out.println("file created");
    } else {
      System.out.println("file already exists");
    }
  }
  public void testInvalidFormat(ImageResultVerifier... verifiers) throws IOException {
    Path tempFile = null;
    try {
      tempFile = Files.createTempFile("jtrim", ".test");

      final ContextAwareTaskExecutor taskExecutor =
          TaskExecutors.contextAware(SyncTaskExecutor.getSimpleExecutor());

      AsyncDataLink<ImageResult> link = create(tempFile, taskExecutor);
      ImageCollectorListener listener = new ImageCollectorListener(taskExecutor);
      AsyncDataController controller = link.getData(Cancellation.UNCANCELABLE_TOKEN, listener);
      assertNotNull(controller.getDataState());
      controller.controlData(null);

      AsyncReport report = listener.getReport();
      ImageResult lastResult = listener.getLastResult();
      long imageCount = listener.getImageCount();

      listener.verifyNoTrivialErrors();

      assertEquals("Should not have received an image.", 0L, imageCount);

      ImageResultVerifier verifier =
          combineVerifiers(new FailedVerifier(Throwable.class), combineVerifiers(verifiers));
      verifier.verifyImageResult(imageCount, lastResult, report);
    } finally {
      if (tempFile != null) {
        Files.deleteIfExists(tempFile);
      }
    }
  }
Beispiel #21
0
 public Boolean isCollectorFullBase() throws BotException {
   final int[] attackableElixirs = {0};
   final BufferedImage image = platform.screenshot(ENEMY_BASE);
   try {
     final URI uri = getClass().getResource("elixirs").toURI();
     Utils.withClasspathFolder(
         uri,
         (path) -> {
           final List<Rectangle> matchedElixirs = new ArrayList<>();
           try (Stream<Path> walk = Files.walk(path, 1)) {
             for (final Iterator<Path> it = walk.iterator(); it.hasNext(); ) {
               final Path next = it.next();
               if (Files.isDirectory(next)) {
                 continue;
               }
               final BufferedImage tar =
                   ImageIO.read(Files.newInputStream(next, StandardOpenOption.READ));
               final List<RegionMatch> doFindAll =
                   TemplateMatcher.findMatchesByGrayscaleAtOriginalResolution(image, tar, 7, 0.8);
               attackableElixirs[0] += countAttackableElixirs(doFindAll, matchedElixirs, next);
             }
           } catch (final IOException e) {
             logger.log(Level.SEVERE, e.getMessage(), e);
           }
         });
   } catch (final URISyntaxException e) {
     logger.log(Level.SEVERE, e.getMessage(), e);
   }
   return attackableElixirs[0] >= 0;
 }
  private void moveCorruptedReplayFile(Path replayFile) throws IOException {
    Path corruptedReplaysDirectory = preferencesService.getCorruptedReplaysDirectory();
    Files.createDirectories(corruptedReplaysDirectory);

    Path target = corruptedReplaysDirectory.resolve(replayFile.getFileName());

    logger.debug("Moving corrupted replay file from {} to {}", replayFile, target);

    Files.move(replayFile, target);

    notificationService.addNotification(
        new PersistentNotification(
            i18n.get("corruptedReplayFiles.notification"),
            Severity.WARN,
            Collections.singletonList(
                new Action(
                    i18n.get("corruptedReplayFiles.show"),
                    event -> {
                      try {
                        // Argh, using AWT since JavaFX doesn't provide a proper method :-(
                        Desktop.getDesktop().open(corruptedReplaysDirectory.toFile());
                      } catch (IOException e) {
                        logger.warn("Could not reveal corrupted replay directory", e);
                      }
                    }))));
  }
  /**
   * Check uploading files and create attachments
   *
   * @throws Exception
   */
  @Test
  public void testCreateAttachment() throws Exception {

    List<MultipartFile> files = new ArrayList<>();
    files.add(multipartFile);

    Mockito.when(attachmentFactory.newAttachment()).thenReturn(null);

    attachmentService.setAttachment(new Attachment());
    attachmentService.setMessageResponse(new MessageResponse());

    MessageResponse messageResponse = attachmentService.createAttachment(files);

    Mockito.verify(attachmentDao, Mockito.times(1)).createAttachments(argumentCaptor.capture());

    Attachment attachment = argumentCaptor.getValue().get(0);

    boolean isExistPreview = Files.exists(Paths.get(storagePath + attachment.getPreviewPath()));
    boolean isExistImage = Files.exists(Paths.get(storagePath + attachment.getFilePathInStorage()));

    Files.delete(Paths.get(storagePath + attachment.getPreviewPath()));
    Files.delete(Paths.get(storagePath + attachment.getFilePathInStorage()));

    Assert.assertTrue(
        attachment.getMimeType().equals("image/png")
            && messageResponse.getCode() == 0
            && isExistPreview
            && isExistImage);
  }
  @Test
  public void testDeleteInstanceDirAfterCreateFailure() throws Exception {
    assumeFalse(
        "Ignore test on windows because it does not delete data directory immediately after unload",
        Constants.WINDOWS);
    File solrHomeDirectory =
        new File(initCoreDataDir, getClass().getName() + "-corex-" + System.nanoTime());
    solrHomeDirectory.mkdirs();
    copySolrHomeToTemp(solrHomeDirectory, "corex");
    File corex = new File(solrHomeDirectory, "corex");
    FileUtils.write(new File(corex, "core.properties"), "", StandardCharsets.UTF_8);
    JettySolrRunner runner =
        new JettySolrRunner(solrHomeDirectory.getAbsolutePath(), buildJettyConfig("/solr"));
    runner.start();

    try (HttpSolrClient client = getHttpSolrClient(runner.getBaseUrl() + "/corex")) {
      client.setConnectionTimeout(SolrTestCaseJ4.DEFAULT_CONNECTION_TIMEOUT);
      client.setSoTimeout(SolrTestCaseJ4.DEFAULT_CONNECTION_TIMEOUT);
      SolrInputDocument doc = new SolrInputDocument();
      doc.addField("id", "123");
      client.add(doc);
      client.commit();
    }

    Path dataDir = null;
    try (HttpSolrClient client = getHttpSolrClient(runner.getBaseUrl().toString())) {
      CoreStatus status = CoreAdminRequest.getCoreStatus("corex", true, client);
      String dataDirectory = status.getDataDirectory();
      dataDir = Paths.get(dataDirectory);
      assertTrue(Files.exists(dataDir));
    }

    File subHome = new File(solrHomeDirectory, "corex" + File.separator + "conf");
    String top = SolrTestCaseJ4.TEST_HOME() + "/collection1/conf";
    FileUtils.copyFile(
        new File(top, "bad-error-solrconfig.xml"), new File(subHome, "solrconfig.xml"));

    try (HttpSolrClient client = getHttpSolrClient(runner.getBaseUrl().toString())) {
      client.setConnectionTimeout(SolrTestCaseJ4.DEFAULT_CONNECTION_TIMEOUT);
      client.setSoTimeout(SolrTestCaseJ4.DEFAULT_CONNECTION_TIMEOUT);
      try {
        CoreAdminRequest.reloadCore("corex", client);
      } catch (Exception e) {
        // this is expected because we put a bad solrconfig -- ignore
      }

      CoreAdminRequest.Unload req = new CoreAdminRequest.Unload(false);
      req.setDeleteDataDir(true);
      req.setDeleteInstanceDir(
          false); // important because the data directory is inside the instance directory
      req.setCoreName("corex");
      req.process(client);
    }

    runner.stop();

    assertTrue(
        "The data directory was not cleaned up on unload after a failed core reload",
        Files.notExists(dataDir));
  }
  @After
  public void tearDown() throws Exception {

    // Удаляем тестовый фаил
    Files.delete(Paths.get(pathTempFile));

    // Удаляем тестовые папки и файлы
    Files.walkFileTree(
        Paths.get(storagePath),
        new SimpleFileVisitor<Path>() {

          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
          }
        });
  }
Beispiel #26
0
 @Before
 public void setUp() throws Exception {
   deleteDirectoryIfExists(DIRECTORY);
   deleteDirectoryIfExists(DIRECTORY2);
   Files.createDirectory(DIRECTORY);
   Files.createDirectory(DIRECTORY2);
 }
Beispiel #27
0
  private List<Future<?>> loadSavedMetricsToCaches(String accessLevelName) {
    String storage = metricStorage + "/" + accessLevelName;
    Path storageFile = Paths.get(storage);
    if (!Files.exists(storageFile)) {
      log.info("Cannot pre-load cache {} - file {} doesn't exist", accessLevelName, storage);
      return Collections.emptyList();
    }

    try (BufferedReader fromStorageFile =
        new BufferedReader(
            new InputStreamReader(
                new GZIPInputStream(Files.newInputStream(storageFile)),
                Charset.forName("UTF-8")))) {
      boolean firstLine = true;
      String metricName;
      List<Future<?>> futures = new ArrayList<>();
      while ((metricName = fromStorageFile.readLine()) != null) {
        if (firstLine) {
          firstLine = false;
        } else {
          futures.add(createNewCacheLines(metricName));
        }
      }
      return futures;
    } catch (IOException e) {
      log.info("could not find file for accessLevel " + accessLevelName, e);
      return Collections.emptyList();
    }
  }
  @Test
  public void backupComplexDir() throws IOException {
    Path dirToBkp = Files.createTempDirectory("dirToBkp4");

    Path subDir1 = Files.createTempDirectory(dirToBkp, "subDir1");
    Path subDir2 = Files.createTempDirectory(dirToBkp, "subDir2");
    Path subDir3 = Files.createTempDirectory(subDir1, "subDir3");

    Path parentdirfile1 = createTempFile(dirToBkp, "FILE_1", ".tmp");
    Path parentdirfile2 = createTempFile(subDir1, "FILE_2", ".tmp");
    Path parentdirfile3 = createTempFile(subDir3, "FILE_3", ".tmp");

    new BackupHelper(conf).backupFileOrDir(dirToBkp);

    Path bkp = PathUtils.get(conf.getBackupDir(), dirToBkp);
    assertThat(exists(bkp), is(true));
    assertThat(exists(PathUtils.get(bkp, parentdirfile1.getFileName())), is(true));
    assertThat(exists(PathUtils.get(bkp, subDir1.getFileName())), is(true));
    assertThat(exists(PathUtils.get(bkp, subDir2.getFileName())), is(true));

    assertThat(
        exists(
            PathUtils.get(PathUtils.get(bkp, subDir1.getFileName()), parentdirfile2.getFileName())),
        is(true));
    assertThat(
        exists(PathUtils.get(PathUtils.get(bkp, subDir1.getFileName()), subDir3.getFileName())),
        is(true));
    assertThat(
        exists(
            PathUtils.get(
                PathUtils.get(PathUtils.get(bkp, subDir1.getFileName()), subDir3.getFileName()),
                parentdirfile3.getFileName())),
        is(true));
  }
Beispiel #29
0
  public void split() throws IOException {
    List<ObjectPath> pathTable = asset.getPaths();
    TypeTree typeTree = asset.getTypeTree();

    // assets with just one object can't be split any further
    if (pathTable.size() == 1) {
      L.warning("Asset doesn't contain sub-assets!");
      return;
    }

    for (ObjectPath path : pathTable) {
      // skip filtered classes
      if (cf != null && !cf.accept(path)) {
        continue;
      }

      String className = ClassID.getNameForID(path.getClassID(), true);

      AssetFile subAsset = new AssetFile();
      subAsset.getHeader().setFormat(asset.getHeader().getFormat());

      ObjectPath subFieldPath = new ObjectPath();
      subFieldPath.setClassID1(path.getClassID1());
      subFieldPath.setClassID2(path.getClassID2());
      subFieldPath.setLength(path.getLength());
      subFieldPath.setOffset(0);
      subFieldPath.setPathID(1);
      subAsset.getPaths().add(subFieldPath);

      TypeTree subTypeTree = subAsset.getTypeTree();
      subTypeTree.setEngineVersion(typeTree.getEngineVersion());
      subTypeTree.setVersion(-2);
      subTypeTree.setFormat(typeTree.getFormat());
      subTypeTree.getFields().put(path.getClassID(), typeTree.getFields().get(path.getClassID()));

      subAsset.setDataBuffer(asset.getPathBuffer(path));

      Path subAssetDir = outputDir.resolve(className);
      if (Files.notExists(subAssetDir)) {
        Files.createDirectories(subAssetDir);
      }

      // probe asset name
      String subAssetName = getObjectName(asset, path);
      if (subAssetName != null) {
        // remove any chars that could cause troubles on various file systems
        subAssetName = FilenameSanitizer.sanitizeName(subAssetName);
      } else {
        // use numeric names
        subAssetName = String.format("%06d", path.getPathID());
      }
      subAssetName += ".asset";

      Path subAssetFile = subAssetDir.resolve(subAssetName);
      if (Files.notExists(subAssetFile)) {
        L.log(Level.INFO, "Writing {0}", subAssetFile);
        subAsset.save(subAssetFile);
      }
    }
  }
Beispiel #30
0
  public static Task createDownloadTask(
      ApiSession sess,
      String servicename,
      Path local,
      CommonPath remote,
      Struct storeParams,
      boolean allowResume) {
    DownloadFile work = new DownloadFile();
    work.session = sess;

    RecordStruct params =
        new RecordStruct(
            new FieldStruct("LocalPath", local),
            new FieldStruct("RemotePath", remote),
            new FieldStruct("ServiceName", servicename),
            new FieldStruct("TransferParams", storeParams));

    if (allowResume && Files.exists(local)) {
      try {
        params.setField("Offset", Files.size(local));
      } catch (IOException x) {
        Logger.error("Unable to get file size for: " + local);
        return null;
      }
    }

    return new Task()
        .withTitle("Download file " + local)
        .withWork(work)
        .withSubContext()
        .withParams(params)
        .withTimeout(1)
        .withDeadline(0);
  }