Patch { public List<ValidationResult> validate(final File rootDir, UpdaterUI ui) throws IOException, OperationCancelledException { LinkedHashSet<String> files = null; final File toDir = toBaseDir(rootDir); boolean checkWarnings = true; while (checkWarnings) { files = Utils.collectRelativePaths(toDir, myIsStrict); checkWarnings = false; for (String file : files) { String warning = myWarnings.get(file); if (warning != null) { if (!ui.showWarning(warning)) { throw new OperationCancelledException(); } checkWarnings = true; break; } } } final List<ValidationResult> result = new ArrayList<ValidationResult>(); if (myIsStrict) { for (PatchAction action : myActions) { files.remove(action.getPath()); } for (String file : files) { myActions.add(0, new DeleteAction(this, file, Digester.INVALID)); } } Runner.logger.info("Validating installation..."); forEach(myActions, "Validating installation...", ui, true, new ActionsProcessor() { @Override public void forEach(PatchAction each) throws IOException { ValidationResult validationResult = each.validate(toDir); if (validationResult != null) result.add(validationResult); } }); return result; } Patch(PatchSpec spec, UpdaterUI ui);  Patch(InputStream patchIn); List<PatchAction> getActions(); void write(OutputStream out); List<ValidationResult> validate(final File rootDir, UpdaterUI ui); ApplicationResult apply(final ZipFile patchFile,
                                 final File rootDir,
                                 final File backupDir,
                                 final Map<String, ValidationResult.Option> options,
                                 UpdaterUI ui); void revert(List<PatchAction> actions, final File backupDir, final File rootDir, UpdaterUI ui); long digestFile(File toFile, boolean normalize); Map<String, Long> digestFiles(File dir, List<String> ignoredFiles, boolean normalize, UpdaterUI ui); String getOldBuild(); String getNewBuild(); boolean isStrict(); boolean isNormalized(); boolean validateDeletion(String path);  }
@Test public void testValidation() throws Exception { FileUtil.writeToFile(new File(myOlderDir, "bin/idea.bat"), "changed".getBytes()); new File(myOlderDir, "extraDir").mkdirs(); new File(myOlderDir, "extraDir/extraFile.txt").createNewFile(); new File(myOlderDir, "newDir").mkdirs(); new File(myOlderDir, "newDir/newFile.txt").createNewFile(); FileUtil.writeToFile(new File(myOlderDir, "Readme.txt"), "changed".getBytes()); FileUtil.writeToFile(new File(myOlderDir, "lib/annotations.jar"), "changed".getBytes()); FileUtil.delete(new File(myOlderDir, "lib/bootstrap.jar")); assertEquals( new HashSet<ValidationResult>(Arrays.asList( new ValidationResult(ValidationResult.Kind.CONFLICT, "newDir/newFile.txt", ValidationResult.Action.CREATE, ValidationResult.ALREADY_EXISTS_MESSAGE, ValidationResult.Option.REPLACE, ValidationResult.Option.KEEP), new ValidationResult(ValidationResult.Kind.ERROR, "Readme.txt", ValidationResult.Action.UPDATE, ValidationResult.MODIFIED_MESSAGE, ValidationResult.Option.IGNORE), new ValidationResult(ValidationResult.Kind.ERROR, "lib/annotations.jar", ValidationResult.Action.UPDATE, ValidationResult.MODIFIED_MESSAGE, ValidationResult.Option.IGNORE), new ValidationResult(ValidationResult.Kind.ERROR, "lib/bootstrap.jar", ValidationResult.Action.UPDATE, ValidationResult.ABSENT_MESSAGE, ValidationResult.Option.IGNORE), new ValidationResult(ValidationResult.Kind.CONFLICT, "bin/idea.bat", ValidationResult.Action.DELETE, ValidationResult.MODIFIED_MESSAGE, ValidationResult.Option.DELETE, ValidationResult.Option.KEEP))), new HashSet<ValidationResult>(myPatch.validate(myOlderDir, TEST_UI))); }
@Test public void testValidationWithOptionalFiles() throws Exception { FileUtil.writeToFile(new File(myOlderDir, "lib/annotations.jar"), "changed".getBytes()); assertEquals(new HashSet<ValidationResult>(Arrays.asList( new ValidationResult(ValidationResult.Kind.ERROR, "lib/annotations.jar", ValidationResult.Action.UPDATE, ValidationResult.MODIFIED_MESSAGE, ValidationResult.Option.IGNORE))), new HashSet<ValidationResult>(myPatch.validate(myOlderDir, TEST_UI))); PatchSpec spec = new PatchSpec() .setOldFolder(myOlderDir.getAbsolutePath()) .setNewFolder(myNewerDir.getAbsolutePath()) .setOptionalFiles(Arrays.asList("lib/annotations.jar")); myPatch = new Patch(spec, TEST_UI); FileUtil.delete(new File(myOlderDir, "lib/annotations.jar")); assertEquals(Collections.<ValidationResult>emptyList(), myPatch.validate(myOlderDir, TEST_UI)); }
@Test public void testValidatingNonAccessibleFiles() throws Exception { File f = new File(myOlderDir, "Readme.txt"); FileOutputStream s = new FileOutputStream(f, true); try { FileLock lock = s.getChannel().lock(); try { String message = UtilsTest.mIsWindows ? "Locked by: Java(TM) Platform SE binary" : ValidationResult.ACCESS_DENIED_MESSAGE; ValidationResult.Option option = UtilsTest.mIsWindows ? ValidationResult.Option.KILL_PROCESS : ValidationResult.Option.IGNORE; List<ValidationResult> result = myPatch.validate(myOlderDir, TEST_UI); assertEquals( new HashSet<ValidationResult>(Arrays.asList( new ValidationResult(ValidationResult.Kind.ERROR, "Readme.txt", ValidationResult.Action.UPDATE, message, option))), new HashSet<ValidationResult>(result)); } finally { lock.release(); } } finally { s.close(); } }