package com.fujitsu.iflowqa.testframe;

import com.fujitsu.iflow.common.JSConversions;

public class JSConversionsTest extends BPMTestBase
{
    private void testPositive4(String tVal, String tResult, String comm)
        throws Exception
    {
        //first test the unquoted to quoted
        testAndReport("testQuote4JS_String_Positive          "+comm,
                      JSConversions.quote4JS(tVal), tResult);

        StringBuffer buf = new StringBuffer();
        JSConversions.quote4JS(buf, tVal);
        testAndReport("testQuote4JS_StringBuffer_Positive    "+comm,
                       buf.toString(), tResult);

        //now test turning the quoted back into the unquoted
        testAndReport("testUnQuote4JS_String_Positive        "+comm,
                      JSConversions.unquote4JS(tResult), tVal);

        buf = new StringBuffer();
        JSConversions.unquote4JS(buf, tResult);
        testAndReport("testUnQuote4JS_StringBuffer_Positive  "+comm,
                       buf.toString(), tVal);
    }

    private void testNegative3(String tVal, String details)
    {
        try
        {
            JSConversions.unquote4JS(tVal);
            tr.markFailed("testUnquote4JS_String_Negative           "+details, "No exception was thrown");
        }
        catch (Exception e)
        {
            tr.markPassed("testUnquote4JS_String_Negative           "+details);
        }
        StringBuffer buf = new StringBuffer();
        try
        {
            JSConversions.unquote4JS(buf, tVal);
            tr.markFailed("testUnquote4JS_StringBuffer_Negative     "+details, "No exception was thrown");
        }
        catch (Exception e)
        {
            tr.markPassed("testUnquote4JS_StringBuffer_Negative     "+details);
        }
        try
        {
            JSConversions.unquote4JS(null, tVal);
            tr.markFailed("testUnquote4JS_StringBufferNull_Negative "+details, "No exception was thrown");
        }
        catch (Exception e)
        {
            tr.markPassed("testUnquote4JS_StringBufferNull_Negative "+details);
        }
    }


    public
    void
    runTests(TestRecorder tr)
        throws Exception
    {
        super.init(tr);

        testPositive4("",                  "\"\"",                 "null string");
        testPositive4("ValidName",         "\"ValidName\"",        "simple value");
        testPositive4("ValidName_123",     "\"ValidName_123\"",    "alpha underscore numbers");
        testPositive4("ValidName-123",     "\"ValidName-123\"",    "alpha hyphen numbers");
        testPositive4("1234567980",        "\"1234567980\"",       "allnumbers");
        testPositive4("123,456,798,0",     "\"123,456,798,0\"",    "numbers with commas");
        testPositive4("???????",           "\"???????\"",          "question marks");
        testPositive4("?_!=*@`+.:;{[]}",   "\"?_!=*@`+.:;{[]}\"",  "punctuation");
        testPositive4("#$%()~|-^",         "\"#$%()~|-^\"",        "other punctuation");
        testPositive4("CAT/DOG",           "\"CAT/DOG\"",          "forward slash");
        testPositive4("CAT\\DOG",          "\"CAT\\\\DOG\"",       "back slash");
        testPositive4("CAT!DOG",           "\"CAT!DOG\"",          "exclamation mark");
        testPositive4("CAT?DOG",           "\"CAT?DOG\"",          "question mark");
        testPositive4("CAT<DOG",           "\"CAT<DOG\"",          "less than");
        testPositive4("CAT>DOG",           "\"CAT>DOG\"",          "greater than");
        testPositive4("CAT@DOG",           "\"CAT@DOG\"",          "at sign");
        testPositive4("CAT=DOG",           "\"CAT=DOG\"",          "equals sign");
        testPositive4("CAT*DOG",           "\"CAT*DOG\"",          "asterisk");
        testPositive4("CAT\"DOG",          "\"CAT\\\"DOG\"",       "double quote");
        testPositive4("CAT'DOG",           "\"CAT\\'DOG\"",        "apostrophe");
        testPositive4("\u00A1\u00A2\u00A3","\"\\xA1\\xA2\\xA3\"",  "upper ascii");
        testPositive4("\u30A1\u30A2\u30A3","\"\\u30A1\\u30A2\\u30A3\"", "Kanji characters");

        testNegative3(null,                "null value");
        testNegative3("\"ValidName",       "no trailing quote");
        testNegative3("ValidName\"",       "no leading quote");
        testNegative3("ValidName",         "no quotes at all");
        testNegative3("\"ValidName\\\"",   "slash before final quote");
    }
}
