<commit_before>/**
 * Copyright 2010-2012 by PHP-maven.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.phpmaven.lint.test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.monitor.logging.DefaultLog;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.phpmaven.core.IComponentFactory;
import org.phpmaven.lint.ILintChecker;
import org.phpmaven.lint.ILintExecution;
import org.phpmaven.test.AbstractTestCase;

/**
 * Base test cases for the line check.
 * 
 * @author <NAME> <<EMAIL>>
 * @since 2.0.3
 */
public class RecheckTest extends AbstractTestCase {

    /**
     * Tests if validation is ok.
     *
     * @throws Exception thrown on errors
     */
    public void testValidationOk() throws Exception {
        final MavenSession session = this.createSimpleSession("lint/empty-pom");
        final IComponentFactory factory = lookup(IComponentFactory.class);
        final DefaultLog logger = new DefaultLog(new ConsoleLogger());
        
        ILintChecker checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "success.php"));
        Iterable<ILintExecution> result = checker.run(logger);
        assertFalse(result.iterator().hasNext());

        // second check makes use of state db
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "success.php"));
        result = checker.run(logger);
        assertFalse(result.iterator().hasNext());
    }

    /**
     * Tests if validation is failing on parse errors.
     *
     * @throws Exception thrown on errors
     */
    public void testValidationFailed() throws Exception {
        final MavenSession session = this.createSimpleSession("lint/empty-pom");
        final IComponentFactory factory = lookup(IComponentFactory.class);
        final DefaultLog logger = new DefaultLog(new ConsoleLogger());
        final File failedFile = new File(session.getCurrentProject().getBasedir(), "failed.php");
        
        ILintChecker checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(failedFile);
        Iterable<ILintExecution> result = checker.run(logger);
        Iterator<ILintExecution> iter = result.iterator();
        assertTrue(iter.hasNext());
        ILintExecution failure = iter.next();
        assertFalse(iter.hasNext());
        assertEquals(failedFile, failure.getFile());
        assertNotNull(failure.getException());
        
        // second check makes use of state db
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(failedFile);
        result = checker.run(logger);
        iter = result.iterator();
        assertTrue(iter.hasNext());
        failure = iter.next();
        assertFalse(iter.hasNext());
        assertEquals(failedFile, failure.getFile());
        assertNotNull(failure.getException());
    }

    /**
     * Tests if validation is failing with succeeding files and parse errors.
     *
     * @throws Exception thrown on errors
     */
    public void testMultiFailure() throws Exception {
        final MavenSession session = this.createSimpleSession("lint/empty-pom");
        final IComponentFactory factory = lookup(IComponentFactory.class);
        final DefaultLog logger = new DefaultLog(new ConsoleLogger());
        final File failedFile = new File(session.getCurrentProject().getBasedir(), "multiple/failed.php");
        final File failed2File = new File(session.getCurrentProject().getBasedir(), "multiple/failed2.php");
        
        ILintChecker checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "multiple/success.php"));
        checker.addFileToCheck(failedFile);
        checker.addFileToCheck(failed2File);
        Iterable<ILintExecution> result = checker.run(logger);
        Map<File, ILintExecution> failures = new HashMap<File, ILintExecution>();
        for (final ILintExecution failure : result) {
            failures.put(failure.getFile(), failure);
        }
        assertEquals(2, failures.size());
        assertNotNull(failures.get(failedFile));
        assertNotNull(failures.get(failed2File));
        
        // second check makes use of state db
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "multiple/success.php"));
        checker.addFileToCheck(failedFile);
        checker.addFileToCheck(failed2File);
        result = checker.run(logger);
        failures = new HashMap<File, ILintExecution>();
        for (final ILintExecution failure : result) {
            failures.put(failure.getFile(), failure);
        }
        assertEquals(2, failures.size());
        assertNotNull(failures.get(failedFile));
        assertNotNull(failures.get(failed2File));
        
        // third check to test override of the state db with new source file
        Thread.sleep(1000); // sleep one second so that lastModified is really new.
        final FileOutputStream fos = new FileOutputStream(failedFile);
        fos.write("<?php echo 'FOO';".getBytes());
        fos.flush();
        fos.close();
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);<commit_msg>test fix<commit_after>/**
 * Copyright 2010-2012 by PHP-maven.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.phpmaven.lint.test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.monitor.logging.DefaultLog;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.phpmaven.core.IComponentFactory;
import org.phpmaven.lint.ILintChecker;
import org.phpmaven.lint.ILintExecution;
import org.phpmaven.test.AbstractTestCase;

/**
 * Base test cases for the line check.
 * 
 * @author <NAME> <<EMAIL>>
 * @since 2.0.3
 */
public class RecheckTest extends AbstractTestCase {

    /**
     * Tests if validation is ok.
     *
     * @throws Exception thrown on errors
     */
    public void testValidationOk() throws Exception {
        final MavenSession session = this.createSimpleSession("lint/empty-pom");
        final IComponentFactory factory = lookup(IComponentFactory.class);
        final DefaultLog logger = new DefaultLog(new ConsoleLogger());
        
        ILintChecker checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "success.php"));
        Iterable<ILintExecution> result = checker.run(logger);
        assertFalse(result.iterator().hasNext());

        // second check makes use of state db
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "success.php"));
        result = checker.run(logger);
        assertFalse(result.iterator().hasNext());
    }

    /**
     * Tests if validation is failing on parse errors.
     *
     * @throws Exception thrown on errors
     */
    public void testValidationFailed() throws Exception {
        final MavenSession session = this.createSimpleSession("lint/empty-pom");
        final IComponentFactory factory = lookup(IComponentFactory.class);
        final DefaultLog logger = new DefaultLog(new ConsoleLogger());
        final File failedFile = new File(session.getCurrentProject().getBasedir(), "failed.php");
        
        ILintChecker checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(failedFile);
        Iterable<ILintExecution> result = checker.run(logger);
        Iterator<ILintExecution> iter = result.iterator();
        assertTrue(iter.hasNext());
        ILintExecution failure = iter.next();
        assertFalse(iter.hasNext());
        assertEquals(failedFile, failure.getFile());
        assertNotNull(failure.getException());
        
        // second check makes use of state db
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(failedFile);
        result = checker.run(logger);
        iter = result.iterator();
        assertTrue(iter.hasNext());
        failure = iter.next();
        assertFalse(iter.hasNext());
        assertEquals(failedFile, failure.getFile());
        assertNotNull(failure.getException());
    }

    /**
     * Tests if validation is failing with succeeding files and parse errors.
     *
     * @throws Exception thrown on errors
     */
    public void testMultiFailure() throws Exception {
        final MavenSession session = this.createSimpleSession("lint/empty-pom");
        final IComponentFactory factory = lookup(IComponentFactory.class);
        final DefaultLog logger = new DefaultLog(new ConsoleLogger());
        final File failedFile = new File(session.getCurrentProject().getBasedir(), "multiple/failed.php");
        final File failed2File = new File(session.getCurrentProject().getBasedir(), "multiple/failed2.php");
        
        ILintChecker checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "multiple/success.php"));
        checker.addFileToCheck(failedFile);
        checker.addFileToCheck(failed2File);
        Iterable<ILintExecution> result = checker.run(logger);
        Map<File, ILintExecution> failures = new HashMap<File, ILintExecution>();
        for (final ILintExecution failure : result) {
            failures.put(failure.getFile(), failure);
        }
        assertEquals(2, failures.size());
        assertNotNull(failures.get(failedFile));
        assertNotNull(failures.get(failed2File));
        
        // second check makes use of state db
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
        checker.addFileToCheck(new File(session.getCurrentProject().getBasedir(), "multiple/success.php"));
        checker.addFileToCheck(failedFile);
        checker.addFileToCheck(failed2File);
        result = checker.run(logger);
        failures = new HashMap<File, ILintExecution>();
        for (final ILintExecution failure : result) {
            failures.put(failure.getFile(), failure);
        }
        assertEquals(2, failures.size());
        assertNotNull(failures.get(failedFile));
        assertNotNull(failures.get(failed2File));
        
        // third check to test override of the state db with new source file
        final FileOutputStream fos = new FileOutputStream(failedFile);
        fos.write("<?php echo 'FOO';".getBytes());
        fos.flush();
        fos.close();
        failedFile.setLastModified(failedFile.lastModified() + 1000); // ensure the last modified is changed and causes a recheck
        checker = factory.lookup(ILintChecker.class, IComponentFactory.EMPTY_CONFIG, session);
{'fraction_non_alphanumeric': 0.1060185552295615, 'fraction_numerical': 0.0042819760526524465, 'mean_word_length': 3.3594884203249222, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6725157', 'n_tokens_mistral': 3297, 'n_tokens_neox': 3328, 'n_words': 826}