<commit_before>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 <NAME> (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.commandline;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URISyntaxException;

/**
 * Tests the basic features of the command-line interface.
 *
 * @author <NAME>
 */
public class MainTest extends AbstractSecurityTestCase {

  private final File enTestFile;

  private ByteArrayOutputStream out;
  private ByteArrayOutputStream err;
  private PrintStream stdout;
  private PrintStream stderr;

  public MainTest(String testName) throws IOException {
    super(testName);
    enTestFile = populateFile("This is an test.\n\n" +
            "This is a test of of language tool.\n\n" +
            "This is is a test of language tool.");
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();
    this.stdout = System.out;
    this.stderr = System.err;
    this.out = new ByteArrayOutputStream();
    this.err = new ByteArrayOutputStream();
    System.setOut(new PrintStream(this.out));
    System.setErr(new PrintStream(this.err));
  }

  @Override
  public void tearDown() throws Exception {
    super.tearDown();
    System.setOut(this.stdout);
    System.setErr(this.stderr);
  }

  public void testUsageMessage() throws Exception {
    try {
      final String[] args = new String[] {"-h"};
      Main.main(args);
      fail("LT should have exited with status 0!");
    } catch (ExitException e) {
      final String output = new String(this.out.toByteArray());
      assertTrue(output.contains("Usage: java -jar LanguageTool.jar"));
      assertEquals("Exit status", 1, e.status);
    }
  }

  public void testPrintLanguages() throws Exception {
    try {
      final String[] args = new String[] {"--list"};
      Main.main(args);
      fail("LT should have exited with status 0!");
    } catch (ExitException e) {
      final String output = new String(this.out.toByteArray());
      assertTrue(output.contains("German"));
      assertTrue(output.contains("de-DE"));
      assertTrue(output.contains("English"));
      assertEquals("Exit status", 0, e.status);
    }
  }

  public void testEnglishFile() throws Exception {
    final String[] args = new String[] {"-l", "en", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishFileAutoDetect() throws Exception {
    final String[] args = new String[] {"-adl", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Using English for file") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishStdInAutoDetect() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-adl"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Working on STDIN...") == 0);
    assertTrue(output.contains("Language used is: English"));
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishFileVerbose() throws Exception {
    final String[] args = new String[] {"-l", "en", "-v", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
    final String tagText = new String(this.err.toByteArray());
    assertTrue(tagText.contains("<S> This[this/DT]  is[be/VBZ]  an[a/DT]  test[test/NN].[./.,</S>]"));
  }

  public void testEnglishFileApplySuggestions() throws Exception {
    final String[] args = new String[] {"-l", "en", "--apply", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertEquals("This is a test.\n\n" +
            "This is a test of language tool.\n\n" +
            "This is a test of language tool.\n", output);
  }


  public void testEnglishStdIn1() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-l", "en"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishStdIn2() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-l", "en", "-"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishStdIn3() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-l", "en", "-a", "-"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertEquals("This is a test.\n", output);
  }

  public void testEnglishStdIn4() throws Exception {
    System.setIn(new FileInputStream(enTestFile));
    final String[] args = new String[] {"-l", "en", "--api", "-"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue("Got: " + output, output.contains("<error fromy=\"4\" fromx=\"5\" toy=\"4\" tox=\"10\" " +
            "ruleId=\"ENGLISH_WORD_REPEAT_RULE\" msg=\"Possible typo: you repeated a word\" replacements=\"is\" " +
            "context=\"This is is a test of language tool. \" contextoffset=\"5\" offset=\"5\" errorlength=\"5\" " +
            "category=\"Miscellaneous\" locqualityissuetype=\"addition\"/>"));
    // note: the offset is relative to the sentence... this seems wrong - it happens because of the way
    // the command line client feeds the data into the check() methods.
  }
<commit_msg>Set locQualityIssueType="duplication" in rule WORD_REPEAT_RULE (I suppose this makes sense).  Also in the tests.<commit_after>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 <NAME> (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.commandline;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URISyntaxException;

/**
 * Tests the basic features of the command-line interface.
 *
 * @author <NAME>
 */
public class MainTest extends AbstractSecurityTestCase {

  private final File enTestFile;

  private ByteArrayOutputStream out;
  private ByteArrayOutputStream err;
  private PrintStream stdout;
  private PrintStream stderr;

  public MainTest(String testName) throws IOException {
    super(testName);
    enTestFile = populateFile("This is an test.\n\n" +
            "This is a test of of language tool.\n\n" +
            "This is is a test of language tool.");
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();
    this.stdout = System.out;
    this.stderr = System.err;
    this.out = new ByteArrayOutputStream();
    this.err = new ByteArrayOutputStream();
    System.setOut(new PrintStream(this.out));
    System.setErr(new PrintStream(this.err));
  }

  @Override
  public void tearDown() throws Exception {
    super.tearDown();
    System.setOut(this.stdout);
    System.setErr(this.stderr);
  }

  public void testUsageMessage() throws Exception {
    try {
      final String[] args = new String[] {"-h"};
      Main.main(args);
      fail("LT should have exited with status 0!");
    } catch (ExitException e) {
      final String output = new String(this.out.toByteArray());
      assertTrue(output.contains("Usage: java -jar LanguageTool.jar"));
      assertEquals("Exit status", 1, e.status);
    }
  }

  public void testPrintLanguages() throws Exception {
    try {
      final String[] args = new String[] {"--list"};
      Main.main(args);
      fail("LT should have exited with status 0!");
    } catch (ExitException e) {
      final String output = new String(this.out.toByteArray());
      assertTrue(output.contains("German"));
      assertTrue(output.contains("de-DE"));
      assertTrue(output.contains("English"));
      assertEquals("Exit status", 0, e.status);
    }
  }

  public void testEnglishFile() throws Exception {
    final String[] args = new String[] {"-l", "en", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishFileAutoDetect() throws Exception {
    final String[] args = new String[] {"-adl", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Using English for file") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishStdInAutoDetect() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-adl"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Working on STDIN...") == 0);
    assertTrue(output.contains("Language used is: English"));
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishFileVerbose() throws Exception {
    final String[] args = new String[] {"-l", "en", "-v", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
    final String tagText = new String(this.err.toByteArray());
    assertTrue(tagText.contains("<S> This[this/DT]  is[be/VBZ]  an[a/DT]  test[test/NN].[./.,</S>]"));
  }

  public void testEnglishFileApplySuggestions() throws Exception {
    final String[] args = new String[] {"-l", "en", "--apply", getTestFilePath()};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertEquals("This is a test.\n\n" +
            "This is a test of language tool.\n\n" +
            "This is a test of language tool.\n", output);
  }


  public void testEnglishStdIn1() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-l", "en"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishStdIn2() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-l", "en", "-"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.contains("1.) Line 1, column 9, Rule ID: EN_A_VS_AN"));
  }

  public void testEnglishStdIn3() throws Exception {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    final String[] args = new String[] {"-l", "en", "-a", "-"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertEquals("This is a test.\n", output);
  }

  public void testEnglishStdIn4() throws Exception {
    System.setIn(new FileInputStream(enTestFile));
    final String[] args = new String[] {"-l", "en", "--api", "-"};

    Main.main(args);
    final String output = new String(this.out.toByteArray());
    assertTrue("Got: " + output, output.contains("<error fromy=\"4\" fromx=\"5\" toy=\"4\" tox=\"10\" " +
            "ruleId=\"ENGLISH_WORD_REPEAT_RULE\" msg=\"Possible typo: you repeated a word\" replacements=\"is\" " +
            "context=\"This is is a test of language tool. \" contextoffset=\"5\" offset=\"5\" errorlength=\"5\" " +
            "category=\"Miscellaneous\" locqualityissuetype=\"duplication\"/>"));
    // note: the offset is relative to the sentence... this seems wrong - it happens because of the way
    // the command line client feeds the data into the check() methods.
  }

{'fraction_non_alphanumeric': 0.13700527704485488, 'fraction_numerical': 0.007519788918205805, 'mean_word_length': 3.576214910956837, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1922246', 'n_tokens_mistral': 4492, 'n_tokens_neox': 4348, 'n_words': 1417}