Example usage for java.nio.file Files lines

List of usage examples for java.nio.file Files lines

Introduction

In this page you can find the example usage for java.nio.file Files lines.

Prototype

public static Stream<String> lines(Path path) throws IOException 

Source Link

Document

Read all lines from a file as a Stream .

Usage

From source file:org.commonvox.hbase_column_manager.TestRepositoryAdmin.java

private void compareResourceFileToExportedFile(String resourceFileString, File exportedFile, String methodName)
        throws IOException, URISyntaxException {
    Path resourcePath = Paths.get(ClassLoader.getSystemResource(resourceFileString).toURI());
    assertEquals(CHANGE_EVENT_FAILURE + "unexpected item count from " + methodName + " method",
            Files.lines(resourcePath).count(), Files.lines(exportedFile.toPath()).count());
    // NOTE: timestamps on sequential events can sometimes be equal (if processing is TOO quick!)
    // so reliable comparison requires stripping initial timestamp from each line and reordering
    // remainder via TreeSets, then doing comparison.
    TreeSet<String> resourceLinesTruncated = new TreeSet<>();
    TreeSet<String> exportedLinesTruncated = new TreeSet<>();
    Iterator<String> resourceLinesIterator = Files.lines(resourcePath).iterator();
    Iterator<String> exportedLinesIterator = Files.lines(exportedFile.toPath()).iterator();
    int skipCount = 0;
    while (resourceLinesIterator.hasNext()) {
        if (skipCount++ < 3) {
            resourceLinesIterator.next(); // skip comment and header lines in both files
            exportedLinesIterator.next(); // skip comment and header lines in both files
            continue;
        }//from w  w  w.  j  a  va 2  s  .  c  o  m
        resourceLinesTruncated.add(resourceLinesIterator.next().substring(14)); // strip timestamp
        exportedLinesTruncated.add(exportedLinesIterator.next().substring(14)); // strip timestamp
    }
    Iterator<String> resourceLinesTruncatedIterator = resourceLinesTruncated.iterator();
    Iterator<String> exportedLinesTruncatedIterator = exportedLinesTruncated.iterator();
    while (resourceLinesTruncatedIterator.hasNext()) {
        assertEquals(CHANGE_EVENT_FAILURE + "unexpected content returned by " + methodName,
                resourceLinesTruncatedIterator.next(), exportedLinesTruncatedIterator.next());
    }
}

From source file:org.commonvox.hbase_column_manager.TestRepositoryAdmin.java

@Test
public void testUtilityRunner() throws Exception {
    // UtilityRunner -u getColumnAuditors --table tableName -f testOutput.txt -h
    System.out.println("#testUtilityRunner has been invoked using WILDCARDED " + "EXCLUDE config properties.");
    // file setup
    final String TARGET_DIRECTORY = "target/"; // for standalone (non-JUnit) execution
    final String TARGET_UTILITY_CHANGE_EVENTS_FILE = "temp.utility.change.events.csv";
    final String TARGET_STANDARD_CHANGE_EVENTS_FILE = "temp.standard.change.events.csv";
    final String TARGET_UTILITY_EXPORT_SCHEMA_FILE = "temp.utility.export.schema.xml";
    final String TARGET_STANDARD_EXPORT_SCHEMA_FILE = "temp.standard.export.schema.xml";
    final String TARGET_UTILITY_QUALIFIERS_DIRECT_FILE = "temp.utility.qualifiers.direct.csv";
    final String TARGET_STANDARD_QUALIFIERS_DIRECT_FILE = "temp.standard.qualifiers.direct.csv";
    final String TARGET_UTILITY_QUALIFIERS_MAPRED_FILE = "temp.utility.qualifiers.mapreduce.csv";
    final String TARGET_STANDARD_QUALIFIERS_MAPRED_FILE = "temp.standard.qualifiers.mapreduce.csv";
    File utilityChangeEventsFile;
    File standardChangeEventsFile;
    File utilityExportSchemaFile;
    File standardExportSchemaFile;
    File utilityQualifiersDirectFile;
    File standardQualifiersDirectFile;
    File utilityQualifiersMapreduceFile;
    File standardQualifiersMapreduceFile;
    try {//  w w w. jav  a  2s .  c  o m
        utilityChangeEventsFile = tempTestFolder.newFile(TARGET_UTILITY_CHANGE_EVENTS_FILE);
        standardChangeEventsFile = tempTestFolder.newFile(TARGET_STANDARD_CHANGE_EVENTS_FILE);
        utilityExportSchemaFile = tempTestFolder.newFile(TARGET_UTILITY_EXPORT_SCHEMA_FILE);
        standardExportSchemaFile = tempTestFolder.newFile(TARGET_STANDARD_EXPORT_SCHEMA_FILE);
        utilityQualifiersDirectFile = tempTestFolder.newFile(TARGET_UTILITY_QUALIFIERS_DIRECT_FILE);
        standardQualifiersDirectFile = tempTestFolder.newFile(TARGET_STANDARD_QUALIFIERS_DIRECT_FILE);
        utilityQualifiersMapreduceFile = tempTestFolder.newFile(TARGET_UTILITY_QUALIFIERS_MAPRED_FILE);
        standardQualifiersMapreduceFile = tempTestFolder.newFile(TARGET_STANDARD_QUALIFIERS_MAPRED_FILE);
    } catch (IllegalStateException e) { // standalone (non-JUnit) execution
        utilityChangeEventsFile = new File(TARGET_DIRECTORY + TARGET_UTILITY_CHANGE_EVENTS_FILE);
        standardChangeEventsFile = new File(TARGET_DIRECTORY + TARGET_STANDARD_CHANGE_EVENTS_FILE);
        utilityExportSchemaFile = new File(TARGET_DIRECTORY + TARGET_UTILITY_EXPORT_SCHEMA_FILE);
        standardExportSchemaFile = new File(TARGET_DIRECTORY + TARGET_STANDARD_EXPORT_SCHEMA_FILE);
        utilityQualifiersDirectFile = new File(TARGET_DIRECTORY + TARGET_UTILITY_QUALIFIERS_DIRECT_FILE);
        standardQualifiersDirectFile = new File(TARGET_DIRECTORY + TARGET_STANDARD_QUALIFIERS_DIRECT_FILE);
        utilityQualifiersMapreduceFile = new File(TARGET_DIRECTORY + TARGET_UTILITY_QUALIFIERS_MAPRED_FILE);
        standardQualifiersMapreduceFile = new File(TARGET_DIRECTORY + TARGET_STANDARD_QUALIFIERS_MAPRED_FILE);
    }

    initializeTestNamespaceAndTableObjects();
    clearTestingEnvironment();

    // NOTE that test/resources/hbase-column-manager.xml contains wildcarded excludedTables entries
    Configuration configuration = MConfiguration.create();
    createSchemaStructuresInHBase(configuration, true, false);
    loadColumnData(configuration, true);

    String[] args = new String[] { "-u", UtilityRunner.GET_CHANGE_EVENTS_UTILITY, "--table",
            NAMESPACE03_TABLE01.getNameAsString(), "-f", utilityChangeEventsFile.getAbsolutePath() };
    UtilityRunner.main(args);

    try (Connection connection = MConnectionFactory.createConnection(configuration)) {
        RepositoryAdmin repositoryAdmin = new RepositoryAdmin(connection);
        ChangeEventMonitor monitor = repositoryAdmin.getChangeEventMonitor();
        ChangeEventMonitor.exportChangeEventListToCsvFile(
                monitor.getChangeEventsForTable(NAMESPACE03_TABLE01, true), standardChangeEventsFile);
        assertEquals(
                UTILITY_RUNNER_FAILURE + "unexpected item count from UtilityRunner invocation of "
                        + UtilityRunner.GET_CHANGE_EVENTS_UTILITY,
                Files.lines(standardChangeEventsFile.toPath()).count(),
                Files.lines(utilityChangeEventsFile.toPath()).count());
    }

    args = new String[] { "--utility", UtilityRunner.EXPORT_SCHEMA_UTILITY, "-t",
            NAMESPACE03_TABLE01.getNameAsString(), "--file", utilityExportSchemaFile.getAbsolutePath(),
            "--help" };
    UtilityRunner.main(args);

    try (Connection connection = MConnectionFactory.createConnection(configuration)) {
        RepositoryAdmin repositoryAdmin = new RepositoryAdmin(connection);
        repositoryAdmin.exportSchema(standardExportSchemaFile, NAMESPACE03_TABLE01);
        // both export files should be identical, except for timestamp in the comments
        final String TIMESTAMP_ATTR_REGEX = "File generated on \\[.....................?.?.?\\]";
        String originalHsaXmlContent = new String(Files.readAllBytes(standardExportSchemaFile.toPath()))
                .replaceFirst(TIMESTAMP_ATTR_REGEX, "");
        String comparisonHsaXmlContent = new String(Files.readAllBytes(utilityExportSchemaFile.toPath()))
                .replaceFirst(TIMESTAMP_ATTR_REGEX, "");
        assertEquals(UTILITY_RUNNER_FAILURE + "unexpected discrepancies in UtilityRunner invocation of "
                + UtilityRunner.EXPORT_SCHEMA_UTILITY, originalHsaXmlContent, comparisonHsaXmlContent);
    }

    args = new String[] { "-u", UtilityRunner.GET_COLUMN_QUALIFIERS_UTILITY_DIRECT_SCAN, "-t",
            NAMESPACE01_TABLE01.getNameAsString(), "-f", utilityQualifiersDirectFile.getAbsolutePath(), "-h" };
    UtilityRunner.main(args);

    try (Connection connection = MConnectionFactory.createConnection(configuration)) {
        RepositoryAdmin repositoryAdmin = new RepositoryAdmin(connection);
        repositoryAdmin.outputReportOnColumnQualifiers(standardQualifiersDirectFile, NAMESPACE01_TABLE01);
        assertEquals(
                UTILITY_RUNNER_FAILURE + "unexpected item count from UtilityRunner invocation of "
                        + UtilityRunner.GET_COLUMN_QUALIFIERS_UTILITY_DIRECT_SCAN,
                Files.lines(standardQualifiersDirectFile.toPath()).count(),
                Files.lines(utilityQualifiersDirectFile.toPath()).count());
    }

    args = new String[] { "-u", UtilityRunner.GET_COLUMN_QUALIFIERS_UTILITY_MAP_REDUCE, "-t",
            NAMESPACE01_TABLE01.getNameAsString(), "-f", utilityQualifiersMapreduceFile.getAbsolutePath(),
            "-h" };
    UtilityRunner.main(args);

    try (Connection connection = MConnectionFactory.createConnection(configuration)) {
        RepositoryAdmin repositoryAdmin = new RepositoryAdmin(connection);
        repositoryAdmin.outputReportOnColumnQualifiers(standardQualifiersMapreduceFile, NAMESPACE01_TABLE01);
        assertEquals(
                UTILITY_RUNNER_FAILURE + "unexpected item count from UtilityRunner invocation of "
                        + UtilityRunner.GET_COLUMN_QUALIFIERS_UTILITY_MAP_REDUCE,
                Files.lines(standardQualifiersMapreduceFile.toPath()).count(),
                Files.lines(utilityQualifiersMapreduceFile.toPath()).count());
    }

    args = new String[] { "-u", UtilityRunner.UNINSTALL_REPOSITORY, };
    UtilityRunner.main(args);

    try (Admin standardAdmin = ConnectionFactory.createConnection().getAdmin()) {
        assertTrue(UTILITY_RUNNER_FAILURE + "processing failure in UtilityRunner invocation of "
                + UtilityRunner.UNINSTALL_REPOSITORY, !Repository.repositoryTableExists(standardAdmin));
    }

    clearTestingEnvironment();
    System.out.println(
            "#testUtilityRunner using WILDCARDED EXCLUDE config properties has " + "run to completion.");
}