From b1ae3cd70e2ca4656bb28f77ca79fe0efd29dd67 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 27 Jan 2012 14:07:33 +0000 Subject: [PATCH] #8311 Verify that all source files use unix style line delimiters svn changeset:22796/svn branch:6.7 --- .../tests/server/LicenseInJavaFiles.java | 54 --------- .../tests/server/SourceFileChecker.java | 114 ++++++++++++++++++ 2 files changed, 114 insertions(+), 54 deletions(-) delete mode 100644 tests/server-side/com/vaadin/tests/server/LicenseInJavaFiles.java create mode 100644 tests/server-side/com/vaadin/tests/server/SourceFileChecker.java diff --git a/tests/server-side/com/vaadin/tests/server/LicenseInJavaFiles.java b/tests/server-side/com/vaadin/tests/server/LicenseInJavaFiles.java deleted file mode 100644 index 6f3a05562b..0000000000 --- a/tests/server-side/com/vaadin/tests/server/LicenseInJavaFiles.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.vaadin.tests.server; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.HashSet; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import org.apache.commons.io.IOUtils; - -public class LicenseInJavaFiles extends TestCase { - - /** - * The tests are run in the build directory. - */ - public static String SRC_DIR = "../src"; - - public void testJavaFilesContainsLicense() throws IOException { - File srcDir = new File(SRC_DIR); - System.out.println(new File(".").getAbsolutePath()); - HashSet missing = new HashSet(); - checkForLicense(srcDir, missing); - if (!missing.isEmpty()) { - throw new RuntimeException( - "The following files are missing license information:\n" - + missing.toString()); - } - } - - private void checkForLicense(File srcDir, HashSet missing) - throws IOException { - Assert.assertTrue("Source directory " + srcDir + " does not exist", - srcDir.exists()); - - for (File f : srcDir.listFiles()) { - if (f.isDirectory()) { - checkForLicense(f, missing); - } else if (f.getName().endsWith(".java")) { - checkForLicenseInFile(f, missing); - } - } - } - - private void checkForLicenseInFile(File f, HashSet missing) - throws IOException { - String contents = IOUtils.toString(new FileInputStream(f)); - if (!contents.contains("@" + "VaadinApache2LicenseForJavaFiles" + "@")) { - missing.add(f.getPath()); - } - - } -} diff --git a/tests/server-side/com/vaadin/tests/server/SourceFileChecker.java b/tests/server-side/com/vaadin/tests/server/SourceFileChecker.java new file mode 100644 index 0000000000..91aaf131f5 --- /dev/null +++ b/tests/server-side/com/vaadin/tests/server/SourceFileChecker.java @@ -0,0 +1,114 @@ +package com.vaadin.tests.server; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashSet; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.commons.io.IOUtils; + +public class SourceFileChecker extends TestCase { + + /** + * The tests are run in the build directory. + */ + public static String baseDirectory = null; + public static final String SRC_DIR = getBaseDir() + "src"; + public static final String TESTBENCH_SRC_DIR = getBaseDir() + + "tests/testbench"; + public static final String SERVERSIDE_SRC_DIR = getBaseDir() + + "tests/server-side"; + public static final String CLIENTSIDE_SRC_DIR = getBaseDir() + + "tests/client-side"; + + public static String getBaseDir() { + if (baseDirectory != null) { + return baseDirectory; + } + // Run in the "build" directory by build, in the project root by Eclipse + for (File f : new File("..").listFiles()) { + if (f.getName().equals("buildhelpers")) { + // We are in "build" + baseDirectory = "../"; + return baseDirectory; + } + } + + baseDirectory = ""; + return baseDirectory; + } + + private static final String[] ALL_SRC_DIRS = new String[] { SRC_DIR, + TESTBENCH_SRC_DIR, SERVERSIDE_SRC_DIR, CLIENTSIDE_SRC_DIR }; + + public void testJavaFilesContainsLicense() throws IOException { + validateJavaFiles(SRC_DIR, new LicenseChecker(), + "The following files are missing license information:\n{0}"); + } + + public void testJavaFilesUseUnixNewline() throws IOException { + for (String dir : ALL_SRC_DIRS) { + validateJavaFiles(dir, new DosNewlineDetector(), + "The following files contain CRLF instead of LF:\n{0}"); + } + } + + public interface FileValidator { + void validateFile(File f) throws Exception; + } + + private void validateJavaFiles(String directory, FileValidator validator, + String errorMessage) { + File srcDir = new File(directory); + System.out.println(new File(".").getAbsolutePath()); + HashSet missing = new HashSet(); + validateFiles(srcDir, missing, validator, ".java"); + if (!missing.isEmpty()) { + throw new RuntimeException(errorMessage.replace("{0}", + missing.toString())); + } + + } + + private void validateFiles(File srcDir, HashSet missing, + FileValidator validator, String suffix) { + Assert.assertTrue("Directory " + srcDir + " does not exist", + srcDir.exists()); + + for (File f : srcDir.listFiles()) { + if (f.isDirectory()) { + validateFiles(f, missing, validator, suffix); + } else if (f.getName().endsWith(suffix)) { + try { + validator.validateFile(f); + } catch (Throwable t) { + missing.add(f.getPath()); + } + } + } + } + + class DosNewlineDetector implements FileValidator { + + public void validateFile(File f) throws Exception { + String contents = IOUtils.toString(new FileInputStream(f)); + if (contents.contains("\r\n")) { + throw new IllegalArgumentException(); + } + + } + } + + class LicenseChecker implements FileValidator { + public void validateFile(File f) throws Exception { + String contents = IOUtils.toString(new FileInputStream(f)); + if (!contents.contains("@" + "VaadinApache2LicenseForJavaFiles" + + "@")) { + throw new IllegalArgumentException(); + } + } + } +} -- 2.39.5