summaryrefslogtreecommitdiffstats
path: root/JLanguageTool/src/test/de/danielnaber/languagetool/MainTest.java
blob: b7c35ad6e475a50b1a740bb318fb51d60e82d912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 Daniel Naber (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 de.danielnaber.languagetool;

import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.io.PrintStream;
import java.net.URISyntaxException;
import java.net.URI;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 * Tests the basic features of the command-line interface.
 *
 * @author Marcin Miłkowski
 */
public class MainTest extends AbstractSecurityTestCase {

  private static final String ENGLISH_TEST_FILE = "test-en.txt";
  
  private ByteArrayOutputStream out;
  private ByteArrayOutputStream err;
  private PrintStream stdout;
  private PrintStream stderr;

  public MainTest(String testName) {
    super(testName);
  }

  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));
  }

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

  public void testUsageMessage() throws IOException, ParserConfigurationException, SAXException {
    try {
      String[] args = new String[] {"-h"};
      Main.main(args);
      fail("LT should have exited with status 0!");
    }
    catch (ExitException e) {
      String output = new String(this.out.toByteArray());
      assertTrue(output.indexOf("Usage: java de.danielnaber.languagetool.Main [-r|--recursive] [-v|--verbose") != -1);
      assertEquals("Exit status", 1, e.status);
    }
  }

  public void testEnglishFile() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    //System.err.println("###"+url);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "en", uri.getPath()};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    //System.out.println("#>"+output);
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.indexOf("1.) Line 1, column 9, Rule ID: EN_A_VS_AN") != -1);
  }
  
  public void testEnglishFileVerbose() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "en", "-v", uri.getPath()};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.indexOf("1.) Line 1, column 9, Rule ID: EN_A_VS_AN") != -1);
    String tagText = new String(this.err.toByteArray());
    assertTrue(tagText.indexOf("<S> This[this/DT]  is[be/VBZ]  an[a/DT]  test[test/NN].[./.,</S>]") != -1);
  }
  
  public void testEnglishFileApplySuggestions() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "en", "--apply", uri.getPath()};

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

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

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

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

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.indexOf("1.) Line 1, column 9, Rule ID: EN_A_VS_AN") != -1);
  }
  
  public void testEnglishStdIn3() throws IOException, ParserConfigurationException, SAXException {
    final String test = "This is an test.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    String[] args = new String[] {"-l", "en", "-a", "-"};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertEquals("This is a test.\n", output);
  }
  
  //test line mode vs. para mode
  //first line mode
  public void testEnglishLineMode() throws IOException, ParserConfigurationException, SAXException {    
    final String test = "This is what I mean\nand you know it.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    String[] args = new String[] {"-l", "en", "-a", "-b", "-"};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertEquals("This is what I mean\nAnd you know it.\n", output);
  }

  //first line mode
  public void testEnglishParaMode() throws IOException, ParserConfigurationException, SAXException {    
    final String test = "This is what I mean\nand you know it.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    String[] args = new String[] {"-l", "en", "-a", "-"};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertEquals("This is what I mean\nand you know it.\n", output);
  }
  
  public void testPolishStdInDefaultOff() throws IOException, ParserConfigurationException, SAXException {
    final String test = "To jest test, który zrobiłem, który mi się podoba.";
    final byte[] b = test.getBytes();
    System.setIn(new ByteArrayInputStream(b));
    String[] args = new String[] {"-l", "pl", "-e", "PL_WORD_REPEAT", "-"};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: Polish") == 0);
    assertTrue(output.indexOf("Working on STDIN...") != -1);
    assertTrue(output.indexOf("1.) Line 1, column 31, Rule ID: PL_WORD_REPEAT") != -1);
  }
  
  public void testEnglishFileRuleDisabled() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "en", "-d", "EN_A_VS_AN", uri.getPath()};

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

  public void testEnglishFileRuleEnabled() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "en", "-e", "EN_A_VS_AN", uri.getPath()};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.indexOf("Rule ID: EN_A_VS_AN") != -1);
  }
  
  public void testEnglishFileAPI() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "en", "--api", uri.getPath()};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") == 0);
    assertTrue(output.indexOf("<error fromy=\"0\" fromx=\"8\" toy=\"0\" tox=\"11\" ruleId=\"EN_A_VS_AN\" msg=\"Use 'a' instead of 'an' if the following word doesn't start with a vowel sound, e.g. 'a sentence', 'a university'\" replacements=\"a\" context=\"This is an test. \" contextoffset=\"8\" errorlength=\"2\"/>") != -1);
  }
  
  public void testPolishFileAPI() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    // Create a simple plain text file.
    File input = File.createTempFile("input", "txt");
    input.deleteOnExit();

    // Populate the file with data.
    PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(input), "UTF-8"));
    w.println("To jest świnia która się ślini.");
    w.close();

    String[] args = new String[] {"-l", "pl", "--api", input.getAbsolutePath()};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") == 0);
    assertTrue(output.indexOf("<error fromy=\"0\" fromx=\"8\" toy=\"0\" tox=\"21\" ruleId=\"BRAK_PRZECINKA_KTORY\" subId=\"5\"") != -1);
    //This tests whether XML encoding is actually UTF-8:
    assertTrue(output.indexOf("msg=\"Brak przecinka w tym fragmencie zdania. Przecinek prawdopodobnie należy postawić tak: 'świnia, która'.\" replacements=\"świnia, która\" ")  != -1);
    assertTrue(output.indexOf("context=\"To jest świnia która się ślini. \" contextoffset=\"8\" errorlength=\"12\"/>") != -1);
  }
  
  public void testPolishLineNumbers() throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    // Create a simple plain text file.
    File input = File.createTempFile("input", "txt");
    input.deleteOnExit();

    // Populate the file with data.
    PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(input), "UTF-8"));
    w.println("Test.");
    w.println("Test.");
    w.println("Test.");
    w.println("Test.");
    w.println("Test.");
    w.println("Test.");
    w.println("");
    w.println("Test który wykaże błąd.");
    w.close();

    String[] args = new String[] {"-l", "pl", input.getAbsolutePath()};

    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: Polish") == 0);
    assertTrue(output.indexOf("Line 8, column 1, Rule ID: BRAK_PRZECINKA_KTORY") != -1);
  }
  
  public void testEnglishTagger()  throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "en", "--taggeronly", uri.getPath()};
    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: English") == 0);
    assertTrue(output.indexOf("<S> This[this/DT]  is[be/VBZ]  an[a/DT]  test[test/NN].[./.,</S>]") != -1);
  }

  public void testBitextMode()  throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    // Create a simple plain text file.
    File input = File.createTempFile("input", "txt");
    input.deleteOnExit();

    // Populate the file with data.
    PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(input), "UTF-8"));
    w.println("This is not actual.\tTo nie jest aktualne.");
    w.println("Test\tTest");
    w.println("ab\tVery strange data indeed, much longer than input");
    w.close();

    String[] args = new String[] {"-l", "pl", "--bitext", "-m", "en", input.getAbsolutePath()};
    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: Polish") == 0);
    assertTrue(output.indexOf(
        "Message: Hint: \"aktualny\" (Polish) means \"current\", \"(the) latest\", \"up-to-date\" (English). Did you mean 'rzeczywisty'?") != -1);
    assertTrue(output.indexOf("Line 1, column 32, Rule ID: ACTUAL") != -1);
    assertTrue(output.indexOf("Line 3, column 4, Rule ID: TRANSLATION_LENGTH") != -1);
  }
  
  public void testBitextModeWithDisabledRule()  throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    // Create a simple plain text file.
    File input = File.createTempFile("input", "txt");
    input.deleteOnExit();

    // Populate the file with data.
    PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(input), "UTF-8"));
    w.println("this is not actual.\tTo nie jest aktualne.");
    w.println("test\tTest");
    w.println("ab\tVery strange data indeed, much longer than input");
    w.close();

    String[] args = new String[] {"-l", "pl", "--bitext", "-m", "en", "-d", "UPPERCASE_SENTENCE_START,TRANSLATION_LENGTH", input.getAbsolutePath()};
    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: Polish") == 0);
    assertTrue(output.indexOf(
        "Message: Hint: \"aktualny\" (Polish) means \"current\", \"(the) latest\", \"up-to-date\" (English). Did you mean 'rzeczywisty'?") != -1);
    assertTrue(output.indexOf("Line 1, column 32, Rule ID: ACTUAL") != -1);
    assertTrue(output.indexOf("Rule ID: TRANSLATION_LENGTH") == -1);
  }
  
  public void testBitextModeWithEnabledRule()  throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    // Create a simple plain text file.
    File input = File.createTempFile("input", "txt");
    input.deleteOnExit();

    // Populate the file with data.
    PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(input), "UTF-8"));
    w.println("this is not actual.\tTo nie jest aktualne.");
    w.println("test\tTest");
    w.println("ab\tVery strange data indeed, much longer than input");
    w.close();

    String[] args = new String[] {"-l", "pl", "--bitext", "-m", "en", "-e", "TRANSLATION_LENGTH", input.getAbsolutePath()};
    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: Polish") == 0);
    assertTrue(output.indexOf(
        "Message: Hint: \"aktualny\" (Polish) means \"current\", \"(the) latest\", \"up-to-date\" (English). Did you mean 'rzeczywisty'?") == -1);
    assertTrue(output.indexOf("Line 1, column 32, Rule ID: ACTUAL") == -1);
    assertTrue(output.indexOf("Rule ID: TRANSLATION_LENGTH") != -1);
  }
  
  public void testBitextModeApply()  throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    // Create a simple plain text file.
    File input = File.createTempFile("input", "txt");
    input.deleteOnExit();

    // Populate the file with data.
    PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(input), "UTF-8"));
    w.println("There is a dog.\tNie ma psa.");    
    w.close();

    String[] args = new String[] {"-l", "pl", "--bitext", "-m", "en", "--apply", input.getAbsolutePath()};
    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.startsWith("Istnieje psa."));
  }
  
  public void testListUnknown()  throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "pl", "-u", uri.getPath()};
    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: Polish") == 0);
    assertTrue(output.indexOf("Unknown words: [This, is]") != -1);
  }
  
  public void testNoListUnknown()  throws URISyntaxException, IOException, ParserConfigurationException, SAXException {
    final URL url = this.getClass().getResource(ENGLISH_TEST_FILE);
    final URI uri = new URI (url.toString());
    String[] args = new String[] {"-l", "pl", uri.getPath()};
    Main.main(args);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Expected text language: Polish") == 0);
    assertTrue(output.indexOf("Unknown words: [This, is]") == -1);
  }

}