Example usage for org.apache.commons.io FileUtils readLines

List of usage examples for org.apache.commons.io FileUtils readLines

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils readLines.

Prototype

public static List readLines(File file) throws IOException 

Source Link

Document

Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.

Usage

From source file:de.tudarmstadt.ukp.dkpro.tc.core.util.ReportUtils.java

/**
 * Adds results from a serialized matrix to a map
 * //from  ww w.  j  a v  a2s  .co m
 * @param aggregateMap
 * @param matrix
 *            a csv matrix with the class names in the first row and first column
 * @return updated map
 * @throws IOException
 */
public static Map<List<String>, Double> updateAggregateMatrix(Map<List<String>, Double> aggregateMap,
        File matrix) throws IOException {
    List<String> confMatrixLines = FileUtils.readLines(matrix);
    StrTokenizer l = StrTokenizer.getCSVInstance(confMatrixLines.get(0));
    l.setDelimiterChar(',');
    String[] headline = l.getTokenArray();

    for (int i = 1; i < confMatrixLines.size(); i++) {
        for (int j = 1; j < headline.length; j++) {
            StrTokenizer line = StrTokenizer.getCSVInstance(confMatrixLines.get(i));
            String pred = headline[j];
            line.setDelimiterChar(',');
            String act = line.getTokenArray()[0];
            double value = Double.valueOf(line.getTokenArray()[j]);

            List<String> key = new ArrayList<String>(Arrays.asList(new String[] { pred, act }));

            if (aggregateMap.get(key) != null) {
                aggregateMap.put(key, aggregateMap.get(key) + value);
            } else {
                aggregateMap.put(key, value);
            }
        }
    }
    return aggregateMap;
}

From source file:eu.europeana.sip.gui.SipCreatorGUI.java

private File getFileStoreDirectory() throws FileStoreException {
    File fileStore = new File(System.getProperty("user.home"), "/sip-creator-file-store");
    if (fileStore.isFile()) {
        try {/*from w  w w  .j  a va  2s  .  c  o m*/
            List<String> lines = FileUtils.readLines(fileStore);
            String directory;
            if (lines.size() == 1) {
                directory = lines.get(0);
            } else {
                directory = (String) JOptionPane.showInputDialog(null, "Please choose file store",
                        "Launch SIP-Creator", JOptionPane.PLAIN_MESSAGE, null, lines.toArray(), "");
            }
            if (directory == null) {
                System.exit(0);
            }
            fileStore = new File(directory);
            if (fileStore.exists() && !fileStore.isDirectory()) {
                throw new FileStoreException(
                        String.format("%s is not a directory", fileStore.getAbsolutePath()));
            }
        } catch (IOException e) {
            throw new FileStoreException("Unable to read the file " + fileStore.getAbsolutePath());
        }
    }
    return fileStore;
}

From source file:com.gargoylesoftware.js.CodeStyleTest.java

/**
 * Checks the year in LICENSE.txt.//from  w  w  w  .  j  a v  a  2s  . co  m
 */
private void licenseYear() throws IOException {
    final List<String> lines = FileUtils.readLines(new File("LICENSE.txt"));
    if (!lines.get(1).contains("Copyright (c) " + Calendar.getInstance(Locale.ROOT).get(Calendar.YEAR))) {
        addFailure("Incorrect year in LICENSE.txt");
    }
}

From source file:com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTaskTest.java

@Test
public final void testOmitIgnoredModules() throws IOException {
    final CheckstyleAntTask antTask = getCheckstyleAntTask();
    antTask.setFile(new File(getPath(VIOLATED_INPUT)));
    antTask.setFailOnViolation(false);/*from  www . j av a  2 s  .  co  m*/
    antTask.setOmitIgnoredModules(false);

    final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
    final File outputFile = new File("target/ant_task_plain_output.txt");
    formatter.setTofile(outputFile);
    final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
    formatterType.setValue("plain");
    formatter.setType(formatterType);
    formatter.createListener(null);

    antTask.addFormatter(formatter);
    antTask.execute();

    final List<String> output = FileUtils.readLines(outputFile);
    assertEquals("Starting audit...", output.get(0));
    assertTrue(output.get(1).startsWith("[WARN]"));
    assertTrue(output.get(1)
            .endsWith("InputCheckstyleAntTaskError.java:4: " + "@incomplete=Some javadoc [WriteTag]"));
    assertTrue(output.get(2).startsWith("[ERROR]"));
    assertTrue(output.get(2).endsWith("InputCheckstyleAntTaskError.java:7: "
            + "Line is longer than 70 characters (found 80). [LineLength]"));
    assertTrue(output.get(3).startsWith("[ERROR]"));
    assertTrue(output.get(3).endsWith("InputCheckstyleAntTaskError.java:9: "
            + "Line is longer than 70 characters (found 81). [LineLength]"));
    assertEquals("Audit done.", output.get(4));
}

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.util.SVMHMMUtils.java

/**
 * Reads the prediction file (each line is a integer) and converts them into original outcome
 * labels using the mapping provided by the bi-directional map
 *
 * @param predictionsFile         predictions from classifier
 * @param labelsToIntegersMapping mapping outcomeLabel:integer
 * @return list of outcome labels/*  ww  w  .j a v  a 2  s.  c  o m*/
 * @throws IOException
 */
public static List<String> extractOutcomeLabelsFromPredictions(File predictionsFile,
        BidiMap labelsToIntegersMapping) throws IOException {
    List<String> result = new ArrayList<>();

    for (String line : FileUtils.readLines(predictionsFile)) {
        Integer intLabel = Integer.valueOf(line);

        String outcomeLabel = (String) labelsToIntegersMapping.getKey(intLabel);

        result.add(outcomeLabel);
    }

    return result;
}

From source file:mx.itesm.imb.ImbOperationsImpl.java

/**
 * @see mx.itesm.imb.ImbCommands#generateEntitiesSchemas()
 *///from  w w w. j  ava 2 s.  c o m
@SuppressWarnings("unchecked")
public void generateEntitiesSchemas() {
    String tns;
    int tnsIndex;
    File srcFile;
    String antPath;
    int processCode;
    Process process;
    File schemaFile;
    File schemasDir;
    String entityName;
    String packageName;
    File schemagenFile;
    int rootElementIndex;
    String rootElementName;
    List<String> enumNames;
    String schemagenContents;
    String schemaFileContents;
    List<String> outputContents;
    SortedSet<FileDetails> entries;
    List<String> typesSchemaContents;
    List<String> detailsSchemaContents;

    try {
        // Identify the classes that should generate schemas
        antPath = pathResolver.getRoot(Path.SRC_MAIN_JAVA) + File.separatorChar + "**" + File.separatorChar
                + "*_Roo_JavaBean.aj";
        entries = fileManager.findMatchingAntPath(antPath);

        // Clean directory where the schemas will be generated
        schemasDir = new File(pathResolver.getRoot(Path.SRC_MAIN_JAVA) + "/schema_temp");

        // Generate Java Beans for the identified schemas
        for (FileDetails file : entries) {
            srcFile = file.getFile();
            entityName = srcFile.getName().replace("_Roo_JavaBean.aj", "");
            outputContents = new ArrayList<String>();

            // Format file
            packageName = "";
            for (String line : (List<String>) FileUtils.readLines(srcFile)) {
                // Remove annotations
                if (!line.contains("@")) {
                    // Change form aspect to class declaration
                    if (line.startsWith("privileged")) {
                        outputContents.add("@javax.xml.bind.annotation.XmlRootElement(namespace =\"http://"
                                + packageName + "\")\n");
                        outputContents.add(line.replace("privileged", "public").replace("aspect", "class")
                                .replace("_Roo_JavaBean", ""));
                    } else {
                        // Remove aspect details
                        outputContents.add(line.replace(entityName + ".", ""));
                    }
                }

                if (line.startsWith("package")) {
                    packageName = line.replace("package", "").replace(";", "").trim() + ".imb";
                }
            }

            // Write file
            FileUtils.writeLines(new File(schemasDir, entityName + ".java"), outputContents);
            logger.log(Level.INFO, "Generating XML schema for " + srcFile);
        }

        // Copy enum types to avoid compilation errors
        antPath = pathResolver.getRoot(Path.SRC_MAIN_JAVA) + File.separatorChar + "**" + File.separatorChar
                + "*.java";
        entries = fileManager.findMatchingAntPath(antPath);
        enumNames = new ArrayList<String>();
        for (FileDetails file : entries) {
            if (FileUtils.readFileToString(file.getFile()).contains("enum")) {
                enumNames.add(file.getFile().getName().replace(".java", "").toLowerCase());
                FileUtils.copyFile(file.getFile(), new File(schemasDir, file.getFile().getName()));
            }
        }

        // Execute schemagen
        schemagenFile = new File(schemasDir, "build.xml");
        schemagenContents = FileUtils.readFileToString(ImbOperationsImpl.schemagenBuildFile);
        schemagenContents = schemagenContents.replace("${output.dir}", schemasDir.getAbsolutePath());
        schemagenContents = schemagenContents.replace("${src.dir}", schemasDir.getAbsolutePath());
        FileUtils.writeStringToFile(schemagenFile, schemagenContents);
        process = Runtime.getRuntime().exec("ant -buildfile " + schemagenFile.getAbsolutePath());
        processCode = process.waitFor();

        // Error while executing schemagen
        if (processCode != 0) {
            throw new RuntimeException();
        }

        // Merge schemas and clean up
        typesSchemaContents = FileUtils.readLines(new File(schemasDir, "schema1.xsd"));
        detailsSchemaContents = FileUtils.readLines(new File(schemasDir, "schema2.xsd"));
        outputContents = new ArrayList<String>(typesSchemaContents.size() + detailsSchemaContents.size());

        // Elements
        for (String line : typesSchemaContents) {
            if (!line.contains("<xs:import") && !line.contains("</xs:schema")) {
                if (line.contains("<xs:schema")) {
                    tnsIndex = line.indexOf("targetNamespace=") + "targetNamespace=".length() + 1;
                    tns = line.substring(tnsIndex, line.indexOf("\"", tnsIndex));
                    logger.log(Level.INFO, "index: " + tnsIndex + ", tns: " + tns);
                    outputContents.add(line.substring(0, line.length() - 1) + " xmlns:tns=\"" + tns + "\">");
                } else {
                    outputContents.add(line.replace("type=\"", "type=\"tns:"));
                }
            }
        }

        // Details
        for (String line : detailsSchemaContents) {
            if (!line.contains("<xs:import") && !line.contains("<?xml") && !line.contains("<xs:schema")) {
                if (line.contains("type=")) {
                    // References to other types in the same schema
                    if (!line.contains("type=\"xs:")) {
                        outputContents.add(line.replace("type=\"", "type=\"tns:"));
                    } else {
                        outputContents.add(line);
                    }
                } else {
                    outputContents.add(line);
                    // IMB Id
                    if (line.contains("<xs:sequence")) {
                        outputContents.add("<xs:element name=\"imbId\" type=\"xs:long\" minOccurs=\"0\"/>");
                    }
                }
            }
        }

        // Schema file
        schemaFile = new File(pathResolver.getRoot(Path.SRC_MAIN_RESOURCES), "/schema.xsd");
        FileUtils.writeLines(schemaFile, outputContents);

        // Root element
        schemaFileContents = FileUtils.readFileToString(schemaFile);
        rootElementIndex = schemaFileContents.indexOf("xs:element name=\"");
        rootElementName = schemaFileContents.substring(rootElementIndex + "xs:element name=\"".length(),
                schemaFileContents.indexOf("\"", rootElementIndex + "xs:element name=\"".length() + 1));
        schemaFileContents = schemaFileContents.replace("type=\"tns:" + rootElementName + "\"/>", ">");
        schemaFileContents = schemaFileContents.replace("<xs:complexType name=\"" + rootElementName + "\">",
                "<xs:complexType>");
        schemaFileContents = schemaFileContents.replace("</xs:complexType>", "</xs:complexType></xs:element>");
        FileUtils.writeStringToFile(schemaFile, schemaFileContents);

        FileUtils.forceDelete(schemasDir);
        logger.log(Level.INFO, "XML schemas correctly generated");
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error while generating entities schemas: " + e.getMessage(), e);
    }
}

From source file:eu.sisob.uma.restserver.restservices.RESTSERVICETask.java

/**
 * POST method for updating or creating an instance of HelloWorld
 * @param input //from  www.j av a2s.  c o m
 * @return an instance of RESTResult
 */
@POST
@Produces("application/json")
@Path("/relaunch")
public OutputTaskOperationResult relaunchTask(InputLaunchTask input) {
    OutputTaskOperationResult result = new OutputTaskOperationResult();

    synchronized (AuthorizationManager.getLocker(input.user)) {
        OutputTaskStatus task_status = TaskManager.getTaskStatus(input.user, input.pass, input.task_code, false,
                false, false);

        result.message = task_status.message.toString();
        if (task_status.status.equals(OutputTaskStatus.TASK_STATUS_NO_AUTH)) {
            result.success = false;
            result.message = task_status.message;
        } else if (task_status.status.equals(OutputTaskStatus.TASK_STATUS_NO_ACCESS)) {
            result.success = false;
            result.message = task_status.message;
        } else if (task_status.status.equals(OutputTaskStatus.TASK_STATUS_EXECUTING)) {
            result.success = false;
            result.message = task_status.message;
        } else if (task_status.status.equals(OutputTaskStatus.TASK_STATUS_TO_EXECUTE)) {
            result.success = false;
            result.message = task_status.message;

        } else if (task_status.status.equals(OutputTaskStatus.TASK_STATUS_EXECUTED)) {
            String code_task_folder = AuthorizationManager.TASKS_USERS_PATH + File.separator + input.user
                    + File.separator + input.task_code;

            try {
                File params_file = (new File(
                        code_task_folder + File.separator + AuthorizationManager.params_flag_file));

                List<String> params = FileUtils.readLines(params_file);

                input.parameters = new InputParameter[params.size()];

                int i = 0;
                for (String l : params) {
                    String[] values = l.split("\\$");
                    if (values.length == 2) {
                        InputParameter ip = new InputParameter();
                        ip.key = values[0];
                        ip.value = values[1];
                        input.parameters[i] = ip;
                        i++;
                    }
                }

            } catch (Exception ex) {
                ProjectLogger.LOGGER.error("Error reading params filename "
                        + AuthorizationManager.params_flag_file + "(" + code_task_folder + ")", ex); //FIXME
            }

            if (input.task_kind.equals("crawler")) {
                StringWriter message = new StringWriter();

                result.success = CrawlerTask.launch(input.user, input.pass, input.task_code, code_task_folder,
                        input.user, message);
                result.message = message.toString();

            } else if (input.task_kind.equals("websearcher")) {

                StringWriter message = new StringWriter();

                WebSearchersExtractor.SearchPatterns pattern = WebSearchersExtractor.SearchPatterns.P2;
                String value_mode = null;

                try {
                    value_mode = InputParameter.get(TasksParams.PARAM_CRAWLER_P1, input.parameters);
                    if (value_mode != null && value_mode.equals(TasksParams.PARAM_TRUE))
                        pattern = WebSearchersExtractor.SearchPatterns.P1;

                    value_mode = InputParameter.get(TasksParams.PARAM_CRAWLER_P2, input.parameters);
                    if (value_mode != null && value_mode.equals(TasksParams.PARAM_TRUE))
                        pattern = WebSearchersExtractor.SearchPatterns.P2;

                    value_mode = InputParameter.get(TasksParams.PARAM_CRAWLER_P3, input.parameters);
                    if (value_mode != null && value_mode.equals(TasksParams.PARAM_TRUE))
                        pattern = WebSearchersExtractor.SearchPatterns.P3;

                    value_mode = InputParameter.get(TasksParams.PARAM_CRAWLER_P4, input.parameters);
                    if (value_mode != null && value_mode.equals(TasksParams.PARAM_TRUE))
                        pattern = WebSearchersExtractor.SearchPatterns.P4;

                    value_mode = InputParameter.get(TasksParams.PARAM_CRAWLER_P5, input.parameters);
                    if (value_mode != null && value_mode.equals(TasksParams.PARAM_TRUE))
                        pattern = WebSearchersExtractor.SearchPatterns.P5;

                } catch (Exception ex) {
                    pattern = WebSearchersExtractor.SearchPatterns.P2;
                }

                result.success = WebSearcherTask.launch(input.user, input.pass, input.task_code,
                        code_task_folder, input.user, pattern, message);

                result.message = message.toString();

            } else if (input.task_kind.equals("websearcher_cv")) {

                StringWriter message = new StringWriter();

                result.success = WebSearcherCVTask.launch(input.user, input.pass, input.task_code,
                        code_task_folder, input.user, message);
                result.message = message.toString();

            } else if (input.task_kind.equals("internalcvfiles")) {

                StringWriter message = new StringWriter();

                result.success = InternalCVFilesTask.launch(input.user, input.pass, input.task_code,
                        code_task_folder, input.user, message);
                result.message = message.toString();

            } else if (input.task_kind.equals("email")) {

                StringWriter message = new StringWriter();

                String value_filters = InputParameter.get(TasksParams.PARAM_EMAIL_FILTERS, input.parameters);
                List<String> filters = new ArrayList<String>();
                if (value_filters != null && !value_filters.equals("")) {
                    String[] filters_string = value_filters.split(",");
                    for (String filter : filters_string) {
                        filters.add(filter.trim());
                    }
                }

                result.success = EmailTask.launch(input.user, input.pass, input.task_code, code_task_folder,
                        input.user, filters, message);
                result.message = message.toString();

            } else if (input.task_kind.equals("gate")) {

                StringWriter message = new StringWriter();

                boolean verbose = false;
                String value_verbose = null;
                try {
                    value_verbose = InputParameter.get(TasksParams.PARAM_GATE_VERBOSE, input.parameters);
                    if (value_verbose != null && value_verbose.equals(TasksParams.PARAM_TRUE))
                        verbose = true;
                } catch (Exception ex) {
                    verbose = true;
                }

                boolean split = false;
                String value_split = null;
                try {
                    value_split = InputParameter.get(TasksParams.PARAM_GATE_SPLIT, input.parameters);
                    if (value_split != null && value_split.equals(TasksParams.PARAM_TRUE))
                        split = true;
                } catch (Exception ex) {
                    split = false;
                }

                result.success = GateTask.launch(input.user, input.pass, input.task_code, code_task_folder,
                        input.user, message, verbose, split);
                result.message = message.toString();
            } else {
                result.success = false;
                result.message = TheResourceBundle.getString("Jsp Task Unknowed Msg");
            }

            /*
             * Delete delete flags
             */
            if (result.success) {
                try {
                    (new File(code_task_folder + File.separator + AuthorizationManager.begin_flag_file))
                            .delete();
                } catch (Exception ex) {
                    ProjectLogger.LOGGER.error("Error deleting " + AuthorizationManager.begin_flag_file + "("
                            + code_task_folder + ")", ex); //FIXME
                }

                try {
                    (new File(code_task_folder + File.separator + AuthorizationManager.begin_flag_file))
                            .createNewFile();
                } catch (IOException ex) {
                    ProjectLogger.LOGGER.error("Error creating " + AuthorizationManager.begin_flag_file + "("
                            + code_task_folder + ")", ex); //FIXME
                }

                try {
                    (new File(code_task_folder + File.separator + AuthorizationManager.end_flag_file)).delete();
                } catch (Exception ex) {
                    ProjectLogger.LOGGER.error("Error deleting " + AuthorizationManager.end_flag_file + "("
                            + code_task_folder + ")", ex); //FIXME
                }

            }
        }
    }

    return result;
}

From source file:net.jperf.log4j.AppenderTest.java

public void testCsvRenderer() throws Exception {
    DOMConfigurator.configure(getClass().getResource("log4jWCsv.xml"));

    Logger logger = Logger.getLogger("net.jperf.CsvAppenderTest");

    for (int i = 0; i < 20; i++) {
        StopWatch stopWatch = new Log4JStopWatch(logger);
        Thread.sleep(i * 10);/* w  w w . j  a va 2s. c  o m*/
        stopWatch.stop("csvTest");
    }

    //close the appender
    logger.getAppender("coalescingStatistics").close();

    //verify the statisticsLog.csv file
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    for (Object line : FileUtils.readLines(new File("./target/statisticsLog.csv"))) {
        String[] values = line.toString().split(",");
        //first column is the tag
        assertEquals("\"csvTest\"", values[0]);
        //next 2 columns are the dates - ensure they can be parsed
        assertTrue(dateFormat.parse(values[1]).before(dateFormat.parse(values[2])));
        //next 3 columns are mean, min and max
        double mean = Double.parseDouble(values[3]);
        int min = Integer.parseInt(values[4]);
        int max = Integer.parseInt(values[5]);
        assertTrue(mean >= min && mean <= max);
        //next column is stddev - ust make sure it's parseable
        Double.parseDouble(values[6]);
        //next column is count
        assertTrue(Integer.parseInt(values[7]) < 20);
        //final column is TPS - just make sure it's parseable
        Double.parseDouble(values[8]);
    }

    //verify the pivotedStatisticsLog.csv file
    for (Object line : FileUtils.readLines(new File("./target/pivotedStatisticsLog.csv"))) {
        String[] values = line.toString().split(",");
        //first 2 columns are the dates - ensure they can be parsed
        assertTrue(dateFormat.parse(values[0]).before(dateFormat.parse(values[1])));
        //next column is mean, ensure it can be parsed
        Double.parseDouble(values[2]);
        //last column should be empty, so make sure last char on string is comma
        assertEquals(',', line.toString().charAt(line.toString().length() - 1));
    }
}

From source file:com.mtgi.analytics.XmlBehaviorEventPersisterTest.java

@Test
@SuppressWarnings("unchecked")
public void testCompress() throws Exception {
    persister.setBinary(true);/*  w ww  .j  ava2 s  . c  om*/
    persister.setCompress(true);
    persister.rotateLog();
    file = new File(persister.getFile());
    assertTrue("persister switched to binary", persister.isBinary());

    //we reuse the test event creation code from jdbc persister test to get ourselves an interesting dataset.
    ArrayList<BehaviorEvent> events = new ArrayList<BehaviorEvent>();
    int[] counter = { 0 };
    for (int i = 0; i < 3; ++i)
        JdbcBehaviorEventPersisterTest.createEvent(null, 1, 3, 3, counter, events);
    LinkedList<BehaviorEvent> queue = new LinkedList<BehaviorEvent>(events);

    persister.persist(queue);
    assertEquals("queue unmodified by persistence operation", 39, queue.size());
    assertEquals("persister reports correct file size", file.length(), persister.getFileSize());

    //unzip and convert binary to flat xml for comparison.  this also verifies that 'rotateLog'
    //leaves a well-formed XML document in its wake.
    String path = persister.rotateLog();
    File source = new File(path);
    assertTrue("log archive data exists", source.isFile());
    assertFalse("archive does not point to active log",
            source.getCanonicalPath().equals(file.getCanonicalPath()));

    File dest = File.createTempFile("perf", ".xml");
    dest.deleteOnExit();

    InputStream fis = new GZIPInputStream(new FileInputStream(source));
    FileOutputStream fos = new FileOutputStream(dest);
    FI_SAX_XML converter = new FI_SAX_XML();
    converter.parse(fis, fos);
    fis.close();
    fos.close();

    //now perform verification of log data against the expected results.
    List<String> actualLines = (List<String>) FileUtils.readLines(dest);
    assertEquals("every event was written, including log close", 40, actualLines.size());

    final String expectedOpen = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><event-log>";
    assertEquals("well-formed open", expectedOpen, actualLines.get(0).substring(0, expectedOpen.length()));
    assertEquals("well-formed close", "</event-log>", actualLines.get(actualLines.size() - 1));

    //read up the expected results for comparison.
    InputStream expectedData = XmlBehaviorEventPersisterTest.class
            .getResourceAsStream("XmlBehaviorEventPersisterTest.testBinaryXml-result.xml");
    assertNotNull("expected results resource found in test environment", expectedData);
    List<String> expectedLines = (List<String>) readLines(expectedData);
    expectedData.close();

    //compare the logged data line by line.
    assertEquals("expected log count matches actual", expectedLines.size(), actualLines.size());
    for (int i = 0; i < expectedLines.size() - 1; ++i) {

        String actual = actualLines.get(i);
        //we have to strip data that varies from test run to test run out before comparing.
        String expectedStripped = stripVariableData(expectedLines.get(i));
        String actualStripped = stripVariableData(actual);
        assertEquals("log line[" + i + "] matches expected", expectedStripped, actualStripped);

        //now check data against source event so that time-sensitive info is checked.
        BehaviorEvent evt = events.get(i);
        assertNotNull("event was given an id", evt.getId());
        assertTrue("log contains id", actual.contains("id=\"" + evt.getId() + "\""));

        BehaviorEvent parent = evt.getParent();
        if (parent == null)
            assertFalse("log does not contain parent id", actual.contains("parent-id"));
        else
            assertTrue("log contains parent reference",
                    actual.contains("parent-id=\"" + parent.getId() + "\""));

        assertTrue("log records time correctly", DATE_PATTERN.matcher(actual).find());
        assertTrue("log records duration correctly",
                actual.contains("<duration-ns>" + evt.getDurationNs() + "</duration-ns>"));
    }
}

From source file:com.daphne.es.maintain.staticresource.web.controller.StaticResourceVersionController.java

private StaticResource switchStaticResourceContent(String rootRealPath, String versionedResourceRealPath,
        String fileName, String content, boolean isMin) throws IOException {

    StaticResource resource = extractResource(fileName, content);
    String filePath = resource.getUrl();
    filePath = filePath.replace("${ctx}", rootRealPath);

    if (isMin) {//from   ww w. ja va  2  s . c  o  m
        File file = new File(YuiCompressorUtils.getCompressFileName(filePath));
        if (!file.exists()) {
            throw new RuntimeException("" + resource.getUrl());
        }
    } else {
        File file = new File(YuiCompressorUtils.getNoneCompressFileName(filePath));
        if (!file.exists()) {
            throw new RuntimeException("?" + resource.getUrl());
        }
    }

    content = StringEscapeUtils.unescapeXml(content);

    File file = new File(versionedResourceRealPath + fileName);

    List<String> contents = FileUtils.readLines(file);

    for (int i = 0, l = contents.size(); i < l; i++) {
        String fileContent = contents.get(i);
        if (content.equals(fileContent)) {
            Matcher matcher = scriptPattern.matcher(content);
            if (!matcher.matches()) {
                matcher = linkPattern.matcher(content);
            }
            String newUrl = isMin ? YuiCompressorUtils.getCompressFileName(resource.getUrl())
                    : YuiCompressorUtils.getNoneCompressFileName(resource.getUrl());

            content = matcher.replaceAll("$1" + Matcher.quoteReplacement(newUrl) + "$3$4$5");
            contents.set(i, content);

            resource.setContent(content);
            resource.setUrl(newUrl);

            break;
        }
    }
    FileUtils.writeLines(file, contents);

    return resource;
}