/* LanguageTool, a natural language style checker 
 * Copyright (C) 2007 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.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

/**
 * Check if the translations seem to be complete.
 * 
 * @author Daniel Naber
 */
public class TranslationTest extends TestCase {
  
  /**
   * Make sure values are not empty.
   */
  public void testTranslationsAreNotEmpty() throws IOException {
    for (int i = 0; i < Language.LANGUAGES.length; i++) {
      Language lang = Language.LANGUAGES[i];
      File file = new File("src" + File.separator + "java" + File.separator
        + "de" + File.separator + "danielnaber"+ File.separator +"languagetool" 
        + File.separator + "MessagesBundle_" + lang.getShortName()+".properties");
      if (!file.exists()) {
        System.err.println("Note: no translation available for " + lang);
        continue;
      }
      List<String> lines = loadFile(file);
      for (String line : lines) {
        line = line.trim();
        if (line.startsWith("#"))
          continue;
        if (line.equals(""))
          continue;
        String[] parts = line.split("=");
        if (parts.length < 2) {
          System.err.println("***** Empty translation: '" + line + "' in file " + file);
          //fail("Empty translation: '" + line + "' in file " + file);
        }
      }
    }
  }
  
  private List<String> loadFile(File file) throws IOException {
    List<String> l = new ArrayList<String>();
    FileReader fr = null;
    BufferedReader br = null;
    try {
      fr = new FileReader(file);
      br = new BufferedReader(fr);
      String line;
      while ((line = br.readLine()) != null) {
        l.add(line);
      }
    } finally {
      if (br != null) br.close();
      if (fr != null) fr.close();
    }
    return l;
  }

}

@@ -944,16 +944,48 @@
 o.File;%0A
+import java.io.FileInputStream;%0A
 import j
@@ -1081,16 +1081,67 @@
 il.List;
+%0Aimport java.util.Properties;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1306,465 +1306,1219 @@
 e %7B%0A
-  
 %0A  
-/**%0A   * Make sure values are not empty.%0A   */%0A  public void testTranslationsAreNotEmpty() throws IOException %7B%0A    for (int i = 0; i %3C Language.LANGUAGES.length; i++) %7B%0A      Language lang = Language.LANGUAGES%5Bi%5D;%0A      File file = new File(%22src%22 + File.separator + %22java%22 + File.separator%0A        + %22de%22 + File.separator + %22danielnaber%22+ File.separator +%22languagetool%22 %0A        + File.separator + %22MessagesBundle_%22 + lang.getShortName()+%22.properties%22
+public void testTranslationKeyExistence() throws IOException %7B%0A    // use English version as the reference:%0A    File englishFile = getTranslationFile(Language.ENGLISH);%0A    Properties enProps = new Properties();%0A    enProps.load(new FileInputStream(englishFile));%0A    Set%3CObject%3E englishKeys = enProps.keySet();%0A    for (int i = 0; i %3C Language.LANGUAGES.length; i++) %7B%0A      Language lang = Language.LANGUAGES%5Bi%5D;%0A      if (lang == Language.ENGLISH %7C%7C lang == Language.DEMO)%0A        continue;%0A      Properties langProps = new Properties();%0A      File langFile = getTranslationFile(lang);%0A      langProps.load(new FileInputStream(langFile));%0A      Set%3CObject%3E langKeys = langProps.keySet();%0A      for (Object englishKey : englishKeys) %7B%0A        if (!langKeys.contains(englishKey)) %7B%0A          System.err.println(%22***** No key '%22 + englishKey + %22' in file %22 + langFile);%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  /**%0A   * Make sure values are not empty.%0A   */%0A  public void testTranslationsAreNotEmpty() throws IOException %7B%0A    for (int i = 0; i %3C Language.LANGUAGES.length; i++) %7B%0A      Language lang = Language.LANGUAGES%5Bi%5D;%0A      if (lang == Language.DEMO)%0A        continue;%0A      File file = getTranslationFile(lang
 );%0A 
@@ -2785,42 +2785,12 @@
 %22#%22)
-)%0A          continue;%0A        if (
+ %7C%7C 
 line
@@ -3535,10 +3535,303 @@
 l;%0A  %7D%0A%0A
+  private File getTranslationFile(Language lang) %7B%0A    return new File(%22src%22 + File.separator + %22java%22 + File.separator%0A        + %22de%22 + File.separator + %22danielnaber%22 + File.separator + %22languagetool%22 %0A        + File.separator + %22MessagesBundle_%22 + lang.getShortName() + %22.properties%22);%0A  %7D%0A%0A
 %7D%0A

check if the English keys occur in all other translation files, i.e. if there are translations for every English string