package org.ovirt.engine.core.utils;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import junit.framework.TestCase;

import org.ovirt.engine.core.common.interfaces.ErrorTranslator;

public class ErrorTranslatorTest extends TestCase {

    private static final String TEST_KEY_NO_REPLACEMENT = "TEST_KEY_NO_REPLACEMENT";
    private static final String TEST_KEY_WITH_REPLACEMENT = "TEST_KEY_WITH_REPLACEMENT";
    private static final String FILENAME = "TestAppErrors";
    private static final String FILENAME_WITH_SUFFIX = FILENAME + ".properties";

    public void testNoStringSubstitutionWithoutSuffix() {
        doTestNoStringSubstitution(FILENAME);
    }

    public void testNoStringSubstitutionWithSuffix() {
        doTestNoStringSubstitution(FILENAME_WITH_SUFFIX);
    }

    private static void doTestNoStringSubstitution(String name) {
        ErrorTranslator et = new ErrorTranslatorImpl(name);
        String error = et.TranslateErrorTextSingle(TEST_KEY_NO_REPLACEMENT);
        assertEquals("String should equal", "VM not found", error);
    }

    public void testNoStringSubstitutionWithList() {
        ErrorTranslator et = new ErrorTranslatorImpl(FILENAME);
        List<String> error = et.TranslateErrorText(Arrays.asList(TEST_KEY_NO_REPLACEMENT));
        assertTrue("Size", error.size() == 1);
        assertEquals("String should equal", "VM not found", error.get(0));
    }

    public void testStringSubstitutionWithList() {
        ErrorTranslator et = new ErrorTranslatorImpl(FILENAME);
        List<String> error = et.TranslateErrorText(Arrays.asList(TEST_KEY_WITH_REPLACEMENT,
                "$action SOMEACTION", "$type SOME Type"));
        String result = "Cannot SOMEACTION SOME Type. VM's Image doesn't exist.";
        assertTrue("Size", error.size() == 1);
        assertEquals("String should equal", result, error.get(0));
    }

    public void testLocaleSpecificWithoutSuffix() {
        doTestLocaleSpecific(FILENAME);
    }

    public void testLocaleSpecificWithSuffix() {
        doTestLocaleSpecific(FILENAME_WITH_SUFFIX);
    }

    private static void doTestLocaleSpecific(String name) {
        Locale locale = Locale.getDefault();
        try {
            Locale.setDefault(Locale.GERMAN);
            ErrorTranslator et = new ErrorTranslatorImpl(name);
            List<String> errors = et.TranslateErrorText(Arrays.asList(TEST_KEY_NO_REPLACEMENT));
            assertEquals("Unexpected Size", 1, errors.size());
            assertEquals("String should equal", "Desktop nicht gefunden", errors.get(0));
            String error = et.TranslateErrorTextSingle(TEST_KEY_NO_REPLACEMENT, Locale.GERMAN);
            assertEquals("String should equal", "Desktop nicht gefunden", error);
        } finally {
            Locale.setDefault(locale);
        }
    }

    public void testLocaleOverrideWithoutSuffix() {
        doTestLocaleOverride(FILENAME);
    }

    public void testLocaleOverrideWithSuffix() {
        doTestLocaleOverride(FILENAME_WITH_SUFFIX);
    }

    private static void doTestLocaleOverride(String name) {
        ErrorTranslator et = new ErrorTranslatorImpl(name);
        List<String> errors = et.TranslateErrorText(Arrays.asList(TEST_KEY_NO_REPLACEMENT), Locale.ITALIAN);
        assertEquals("Unexpected Size", 1, errors.size());
        assertEquals("String should equal", "Impossibile trovare il desktop", errors.get(0));
        String error = et.TranslateErrorTextSingle(TEST_KEY_NO_REPLACEMENT, Locale.ITALIAN);
        assertEquals("String should equal", "Impossibile trovare il desktop", error);
    }
}

@@ -31,16 +31,105 @@
 utils;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0A%0A
 import j
@@ -145,16 +145,16 @@
 Arrays;%0A
-
 import j
@@ -205,35 +205,24 @@
 ort 
+org.
 junit.
-framework.TestCase
+Test
 ;%0A
-%0A
 impo
@@ -319,25 +319,8 @@
 est 
-extends TestCase 
 %7B%0A%0A 
@@ -634,16 +634,26 @@
 ties%22;%0A%0A
+    @Test%0A
     publ
@@ -747,32 +747,42 @@
 LENAME);%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -1155,32 +1155,42 @@
  error);%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -1503,32 +1503,42 @@
 get(0));%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -1982,32 +1982,42 @@
 get(0));%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -2091,32 +2091,42 @@
 LENAME);%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -2942,32 +2942,42 @@
        %7D%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -3055,24 +3055,34 @@
 ME);%0A    %7D%0A%0A
+    @Test%0A
     public v

Convert ErrorTranslatorTest to JUnit4 syntax