/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */

/*
 * Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * $Id: QuotedStringTokenizerTest.java,v 1.2 2005/12/25 03:53:28 tcfujii Exp $
 */

package com.sun.enterprise.admin.util;

import java.util.StringTokenizer;
import junit.framework.*;
import junit.framework.Assert;

/**
 * Depends on junit.jar, admin-core/util.jsr
 */
public class QuotedStringTokenizerTest extends TestCase
{
    public void testCreate()
    {
        new QuotedStringTokenizer("");
        new QuotedStringTokenizer("a");
        try {
            new QuotedStringTokenizer(null);
            Assert.assertTrue(false);
        } catch (IllegalArgumentException iae) {}
    }

    void _countTokens(String s, int expCount)
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer(s, null);
        Assert.assertEquals(expCount, tok.countTokens());
    }

    public void testCountTokens()
    {
        _countTokens("", 0);
        _countTokens(" ", 0);
        _countTokens("  ", 0);
        _countTokens("a", 1);
        _countTokens(" a", 1);
        _countTokens(" a ", 1);
        _countTokens("  a ", 1);
        _countTokens("  ab ", 1);
        _countTokens("  a b ", 2);
    }

    public void testCountTokensWithQuotes()
    {
        _countTokens("\"a\"", 1);
        _countTokens("\"a b\"", 1);
        _countTokens(" \"a\" ", 1);
        _countTokens("\"", 1);
        _countTokens("\"\"", 1);
        _countTokens("\" \"", 1);
        _countTokens("\"a\"b", 1);
        _countTokens("\"a\" b", 2);
        _countTokens("\"a\" \"b\"", 2);
        _countTokens("\"a\"\"b\"", 1);
        _countTokens("\" ", 1);
    }

    public void testNextToken1()
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer("a");
        Assert.assertTrue(tok.hasMoreTokens());
        Assert.assertEquals("a", tok.nextToken());
        Assert.assertFalse(tok.hasMoreTokens());
        try {
            tok.nextToken();
            Assert.assertTrue(false);
        } catch (java.util.NoSuchElementException nsee) {}
    }

    public void testNextToken2()
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer("");
        Assert.assertFalse(tok.hasMoreTokens());
        try {
            tok.nextToken();
            Assert.assertTrue(false);
        } catch (java.util.NoSuchElementException nsee) {}
    }

    public void testNextToken3()
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer("a b c d e f");
        Assert.assertTrue(tok.countTokens() == 6);
        tok.nextToken();
        Assert.assertEquals("b", tok.nextToken());
        Assert.assertTrue(tok.hasMoreTokens());
        while (tok.hasMoreTokens())
            tok.nextToken();
        try {
            tok.nextToken();
            Assert.assertTrue(false);
        } catch (java.util.NoSuchElementException nsee) {}
    }

    public void testNextToken4()
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer("abcdef");
        Assert.assertTrue(tok.countTokens() == 1);
        Assert.assertEquals("abcdef", tok.nextToken());
        tok = new QuotedStringTokenizer("     abcdef       g      ");
        Assert.assertTrue(tok.countTokens() == 2);
        Assert.assertEquals("abcdef", tok.nextToken());
        Assert.assertEquals("g", tok.nextToken());
    }

    public void testNextToken5()
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer("abc \t d \t\t ef");
        Assert.assertEquals(3, tok.countTokens());
        Assert.assertEquals("abc", tok.nextToken());
        Assert.assertEquals("d", tok.nextToken());
        Assert.assertEquals("ef", tok.nextToken());
    }


    public void testNextTokenWithQuotes()
    {
        QuotedStringTokenizer mtok = new QuotedStringTokenizer("\"\"");
        Assert.assertEquals("\"\"", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer("\"");
        Assert.assertEquals("\"", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        //??
        mtok = new QuotedStringTokenizer("\" ");
        Assert.assertEquals("\" ", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        //??
        mtok = new QuotedStringTokenizer(" \" ");
        Assert.assertEquals("\" ", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer("\t\"\t");
        Assert.assertEquals("\"\t", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer("\"a\"");
        Assert.assertEquals("\"a\"", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer(" \"a\" ");
        Assert.assertEquals("\"a\"", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer("\"a");
        Assert.assertEquals("\"a", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer("\"a\" \"b\"");
        Assert.assertEquals("\"a\"", mtok.nextToken());
        Assert.assertEquals("\"b\"", mtok.nextToken());

        mtok = new QuotedStringTokenizer("\"a\"b");
        Assert.assertEquals("\"a\"b", mtok.nextToken());

        //??
        mtok = new QuotedStringTokenizer("\"a ");
        Assert.assertEquals("\"a ", mtok.nextToken());

        mtok = new QuotedStringTokenizer("\"a b\"");
        Assert.assertEquals(1, mtok.countTokens());
        Assert.assertEquals("\"a b\"", mtok.nextToken());
    }

    public void testWithQuoteAsDelimiter()
    {
        QuotedStringTokenizer mtok = new QuotedStringTokenizer("\"\"", "\" ");
        Assert.assertEquals(0, mtok.countTokens());

        mtok = new QuotedStringTokenizer("\"", "\" ");
        Assert.assertEquals(0, mtok.countTokens());

        mtok = new QuotedStringTokenizer("\" ", "\" ");
        Assert.assertEquals(0, mtok.countTokens());

        mtok = new QuotedStringTokenizer(" \" ", "\" ");
        Assert.assertEquals(0, mtok.countTokens());

        mtok = new QuotedStringTokenizer("\t\"\t", "\" ");
        Assert.assertEquals(2, mtok.countTokens());

        mtok = new QuotedStringTokenizer("\"a\"", "\" ");
        Assert.assertEquals("a", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer(" \"a\" ", "\" ");
        Assert.assertEquals("a", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer("\"a", "\" ");
        Assert.assertEquals("a", mtok.nextToken());
        Assert.assertFalse(mtok.hasMoreTokens());

        mtok = new QuotedStringTokenizer("\"a\" \"b\"", "\" ");
        Assert.assertEquals("a", mtok.nextToken());
        Assert.assertEquals("b", mtok.nextToken());

        mtok = new QuotedStringTokenizer("\"a\"b", "\" ");
        Assert.assertEquals("a", mtok.nextToken());
        Assert.assertEquals("b", mtok.nextToken());

        mtok = new QuotedStringTokenizer("ab", "\" ");
        Assert.assertEquals("ab", mtok.nextToken());
    }

    public void testIsDelimiter()
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer("");
        Assert.assertTrue(tok.isDelimiter('\t'));
    }

    public void testQuotesInQuotes()
    {
        QuotedStringTokenizer tok = new QuotedStringTokenizer("\"abc\"def g");
        Assert.assertEquals("\"abc\"def", tok.nextToken());
        Assert.assertEquals("g", tok.nextToken());

        tok = new QuotedStringTokenizer("\"abc\"def\"");
        Assert.assertEquals("\"abc\"def\"", tok.nextToken());

        tok = new QuotedStringTokenizer(" \"abc\"def\" ");
        Assert.assertEquals("\"abc\"def\" ", tok.nextToken());
    }

    public void testJavaStringTokenizer()
    {
        StringTokenizer tok = new StringTokenizer("\" ", " ");
        Assert.assertEquals("\"", tok.nextToken());
        tok = new StringTokenizer("a", "\"");
        Assert.assertEquals("a", tok.nextToken());
        tok = new StringTokenizer("\"\"", "\" ");
        Assert.assertEquals(0, tok.countTokens());
        tok = new StringTokenizer("\t\"\t", "\" ");
        Assert.assertEquals(2, tok.countTokens());
    }

    protected void setUp() {
    }

    protected void tearDown() {
    }

    public static junit.framework.Test suite()
    {
        TestSuite suite = new TestSuite(QuotedStringTokenizerTest.class);
        return suite;
    }

    public static void main(String args[]) throws Exception
    {
        junit.textui.TestRunner.run(suite());
        //junit.swingui.TestRunner.run(suite());
    }
}
