/* LanguageTool, a natural language style checker 
 * Copyright (C) 2012 Marcin Miłkowski (http://www.languagetool.org)
 * 
 * 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.rules;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.languagetool.JLanguageTool;
import org.languagetool.Language;
import org.languagetool.TestTools;

import org.languagetool.rules.spelling.hunspell.*;

public class HunspellRuleTest {

  @Test
  public void testRule() throws Exception {

    RuleMatch[] matches;
      
    // Catalan
    HunspellRule catRule =
    		new HunspellRule(TestTools.getMessages("Catalan"), Language.CATALAN);
    JLanguageTool catTool = new JLanguageTool(Language.CATALAN);

    // correct sentences:
    assertEquals(0, catRule.match(catTool.getAnalyzedSentence("Allò que més l'interessa.")).length);
    // checks that "WORDCHARS ·-'" is added to Hunspell .aff file
    assertEquals(0, catRule.match(catTool.getAnalyzedSentence("Porta'n quatre al col·legi.")).length);
    assertEquals(0, catRule.match(catTool.getAnalyzedSentence("Has de portar-me'n moltes.")).length);
    assertEquals(0, catRule.match(catTool.getAnalyzedSentence(",")).length);

    //incorrect sentences:
    matches = catRule.match(catTool.getAnalyzedSentence("Pecra"));
    // check match positions:
    assertEquals(1, matches.length);
    assertEquals(0, matches[0].getFromPos());
    assertEquals(5, matches[0].getToPos());
    assertEquals("Pera", matches[0].getSuggestedReplacements().get(0));
  }

  @Test
  public void testRuleWithGerman() throws Exception {
    final HunspellRule rule = new HunspellRule(TestTools.getMessages("German"), Language.GERMANY_GERMAN);
    final JLanguageTool langTool = new JLanguageTool(Language.GERMAN);
    commonGermanAsserts(rule, langTool);
    assertEquals(0, rule.match(langTool.getAnalyzedSentence("Der äußere Übeltäter.")).length);  // umlauts
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der äussere Übeltäter.")).length);
  }

  @Test
  public void testRuleWithAustrianGerman() throws Exception {
    final HunspellRule rule = new HunspellRule(TestTools.getMessages("German"), Language.AUSTRIAN_GERMAN);
    final JLanguageTool langTool = new JLanguageTool(Language.GERMAN);
    commonGermanAsserts(rule, langTool);
    assertEquals(0, rule.match(langTool.getAnalyzedSentence("Der äußere Übeltäter.")).length);  // umlauts
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der äussere Übeltäter.")).length);
  }

  @Test
  public void testRuleWithSwissGerman() throws Exception {
    final HunspellRule rule = new HunspellRule(TestTools.getMessages("German"), Language.SWISS_GERMAN);
    final JLanguageTool langTool = new JLanguageTool(Language.GERMAN);
    commonGermanAsserts(rule, langTool);
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der äußere Übeltäter.")).length);  // ß not allowed in Swiss
    assertEquals(0, rule.match(langTool.getAnalyzedSentence("Der äussere Übeltäter.")).length);  // ss is used instead of ß
  }

  private void commonGermanAsserts(HunspellRule rule, JLanguageTool langTool) throws IOException {
    assertEquals(0, rule.match(langTool.getAnalyzedSentence("Der Waschmaschinentestversuch")).length);  // compound
    assertEquals(0, rule.match(langTool.getAnalyzedSentence("Der Waschmaschinentest-Versuch")).length);  // compound

    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der Waschmaschinentest-Dftgedgs")).length);
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der Dftgedgs-Waschmaschinentest")).length);
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der Waschmaschinentestdftgedgs")).length);
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der Waschmaschinentestversuch orkt")).length);
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Der Arbeitsnehmer")).length);  // wrong infix
    assertEquals(2, rule.match(langTool.getAnalyzedSentence("Der asdegfue orkt")).length);
  }

  @Ignore("just for internal performance testing, thus ignored by default")
  @Test
  public void testPerformance() throws Exception {
    final List<Language> allLanguages = Language.getAllLanguages();
    for (Language language : allLanguages) {
      final JLanguageTool langTool = new JLanguageTool(language);
      //final HunspellRule rule = new HunspellRule(TestTools.getMessages("German"), language);
      langTool.check("warmup");  // make sure everything is initialized when actually testing
      langTool.check("anotherwarmup");
      final long startTime = System.currentTimeMillis();
      langTool.check("fdfds fdfdsa fdfdsb fdfdsc fdfdsd fdfdse fdfdsf fdfds fdfdsa fdfdsb fdfdsc fdfdsd fdfdse fdfdsf");
      //final AnalyzedSentence analyzedSentence = langTool.getAnalyzedSentence("fdfds fdfdsa fdfdsb fdfdsc fdfdsd fdfdse fdfdsf");
      //rule.match(analyzedSentence);
      final long endTime = System.currentTimeMillis();
      System.out.println((endTime-startTime) + "ms for " + language);
    }
  }

}

@@ -1263,1105 +1263,8 @@
 %0D%0A%0D%0A
-  @Test%0D%0A  public void testRule() throws Exception %7B%0D%0A%0D%0A    RuleMatch%5B%5D matches;%0D%0A      %0D%0A    // Catalan%0D%0A    HunspellRule catRule =%0D%0A    %09%09new HunspellRule(TestTools.getMessages(%22Catalan%22), Language.CATALAN);%0D%0A    JLanguageTool catTool = new JLanguageTool(Language.CATALAN);%0D%0A%0D%0A    // correct sentences:%0D%0A    assertEquals(0, catRule.match(catTool.getAnalyzedSentence(%22All%C3%B2 que m%C3%A9s l'interessa.%22)).length);%0D%0A    // checks that %22WORDCHARS %C2%B7-'%22 is added to Hunspell .aff file%0D%0A    assertEquals(0, catRule.match(catTool.getAnalyzedSentence(%22Porta'n quatre al col%C2%B7legi.%22)).length);%0D%0A    assertEquals(0, catRule.match(catTool.getAnalyzedSentence(%22Has de portar-me'n moltes.%22)).length);%0D%0A    assertEquals(0, catRule.match(catTool.getAnalyzedSentence(%22,%22)).length);%0D%0A%0D%0A    //incorrect sentences:%0D%0A    matches = catRule.match(catTool.getAnalyzedSentence(%22Pecra%22));%0D%0A    // check match positions:%0D%0A    assertEquals(1, matches.length);%0D%0A    assertEquals(0, matches%5B0%5D.getFromPos());%0D%0A    assertEquals(5, matches%5B0%5D.getToPos());%0D%0A    assertEquals(%22Pera%22, matches%5B0%5D.getSuggestedReplacements().get(0));%0D%0A  %7D%0D%0A
 %0D%0A  

make Jenkins happy