Revision: 7116
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=7116&view=rev
Author:   dnaber
Date:     2012-05-31 15:03:25 +0000 (Thu, 31 May 2012)
Log Message:
-----------
fix throwing exception on invalid short language name, adding tests

Modified Paths:
--------------
    trunk/JLanguageTool/src/java/org/languagetool/Language.java
    trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java

Modified: trunk/JLanguageTool/src/java/org/languagetool/Language.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/Language.java 2012-05-31 
14:36:00 UTC (rev 7115)
+++ trunk/JLanguageTool/src/java/org/languagetool/Language.java 2012-05-31 
15:03:25 UTC (rev 7116)
@@ -280,23 +280,22 @@
     StringTools.assureSet(shortLanguageCode, "shortLanguageCode");
        
     if (shortLanguageCode.indexOf('-') != -1) {
-       String[] l = shortLanguageCode.split("-");
-       if (l.length != 2) {
+       final String[] parts = shortLanguageCode.split("-");
+       if (parts.length != 2) {
                throw new IllegalArgumentException("'" + shortLanguageCode + "' 
isn't a valid language code"); 
        }
        for (Language element : Language.LANGUAGES) {
-               if (l[0].equals(element.getShortName())
+               if (parts[0].equals(element.getShortName())
                                && element.getCountryVariants().length == 1     
                        
-                               && 
l[1].equals(element.getCountryVariants()[0])) {
+                               && 
parts[1].equals(element.getCountryVariants()[0])) {
                        return element;
                }
        }
        throw new IllegalArgumentException("'" + shortLanguageCode + "' isn't a 
valid language code");
     }    
-    if (shortLanguageCode.length() == "xx".length() && 
shortLanguageCode.length() == "xxx".length()) {         
-                                 
+    if (shortLanguageCode.length() != "xx".length() && 
shortLanguageCode.length() != "xxx".length()) {
       throw new IllegalArgumentException("'" + shortLanguageCode + "' isn't a 
two- or three-character code");
-      }
+    }
         
     for (Language element : Language.LANGUAGES) {
       if (shortLanguageCode.equals(element.getShortName())) {

Modified: trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java
===================================================================
--- trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java     
2012-05-31 14:36:00 UTC (rev 7115)
+++ trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java     
2012-05-31 15:03:25 UTC (rev 7116)
@@ -1,3 +1,21 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 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 org.languagetool;
 
 import static org.junit.Assert.*;
@@ -7,13 +25,28 @@
 public class LanguageTest {
 
        @Test
-       public void testgetLanguageForShortName() {
+       public void testGetLanguageForShortName() {
                assertEquals(Language.AMERICAN_ENGLISH, 
Language.getLanguageForShortName("en-US"));
                assertEquals(Language.GERMAN, 
Language.getLanguageForShortName("de"));
        }
-       
+
+  @Test(expected=IllegalArgumentException.class)
+       public void testInvalidShortName1() {
+    Language.getLanguageForShortName("de-");
+  }
+
+  @Test(expected=IllegalArgumentException.class)
+       public void testInvalidShortName2() {
+    Language.getLanguageForShortName("dexx");
+  }
+
+  @Test(expected=IllegalArgumentException.class)
+       public void testInvalidShortName3() {
+    Language.getLanguageForShortName("xyz-xx");
+  }
+
        @Test
-       public void testgetLanguageForName() {
+       public void testGetLanguageForName() {
                assertEquals(Language.AMERICAN_ENGLISH, 
Language.getLanguageForName("American English"));
                assertEquals(Language.GERMAN, 
Language.getLanguageForName("German"));
        }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Languagetool-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-cvs

Reply via email to