FileDocCategorySizeDatePackage
TestCSeqHeader.javaAPI DocphoneME MR2 API (J2ME)6383Wed May 02 18:00:40 BST 2007javax.microedition.sip

TestCSeqHeader.java

/*
 *   
 *
 * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 * 
 * This program 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
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 */

package javax.microedition.sip;

import com.sun.midp.i3test.TestCase;

import javax.microedition.io.Connector;
import java.io.IOException;

/**
 * RFC3261, p. 38; BNF: p. 229
 *
 * CSeq = "CSeq" HCOLON 1*DIGIT LWS Method
 *
 * The CSeq header field serves as a way to identify and order
 * transactions.  It consists of a sequence number and a method.  The
 * method MUST match that of the request.  For non-REGISTER requests
 * outside of a dialog, the sequence number value is arbitrary.  The
 * sequence number value MUST be expressible as a 32-bit unsigned
 * integer and MUST be less than 2**31.  As long as it follows the above
 * guidelines, a client may use any mechanism it would like to select
 * CSeq header field values.
 *
 * Section 12.2.1.1 discusses construction of the CSeq for requests
 * within a dialog.
 *
 * Example:
 *
 *    CSeq: 4711 INVITE
 *
 */

public class TestCSeqHeader extends SipHeaderBaseTest {

    /** A name of the header that will be tested */
    private final String headerName = "CSeq";

    /**
     * Body of the test 1.
     *
     * Test for CSeq header field: setName()/getName().
     */
    void Test1() {
        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test1 started ***");

        SipHeader sh;

        // Test the constructor.
        /*
        try {
            sh = new SipHeader(headerName, "Invalid INVITE");
            fail("Constructor: IAE was not thrown!");
        } catch (IllegalArgumentException iae) {
        }
        */

        sh = createSipHeader(headerName, "4711 INVITE");

        if (sh == null) {
            return;
        }

        // Testing getName()...
        String ret_name = sh.getName();
        assertTrue("Invalid header value: " + ret_name,
            ret_name.equals(headerName));

        // Testing setName()...
        try {
           sh.setName(headerName);
        } catch (java.lang.IllegalArgumentException e) {
            fail("setName(" + headerName + ") failed (IAE): " + e);
        } catch (Throwable e) {
            fail("setName(" + headerName + ") failed: " + e);
        }
    }

    /**
     * Body of the test 2.
     *
     * Test for CSeq header field: getValue()/getHeaderValue().
     */
    void Test2() {
        String val;
        String headerValue = "4711 INVITE";

        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test2 started ***");

        SipHeader sh = createSipHeader(headerName, headerValue);

        if (sh != null) {
            val = sh.getValue();
            assertTrue("getValue() returned invalid value: '" +
                       val + "'", val.equals(headerValue));

            val = sh.getHeaderValue();
            assertTrue("(1) getHeaderValue() returned invalid " +
                "value: '" + val + "'", val.equals(headerValue));

            // Test if the value can be changed.
            headerValue = "12345 REGISTER";
            sh.setValue(headerValue);

            val = sh.getHeaderValue();
            assertTrue("(2) getHeaderValue() returned invalid " +
                "value: '" + val + "'", val.equals(headerValue));
        }
    }

    /**
     * Body of the test 4.
     *
     * Test for CSeq header field: getParameterNames()/getParameter().
     */
    void Test4() {
        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test4 started ***");

        SipHeader sh = createSipHeader(headerName, "4711 INVITE");

        if (sh == null) {
            return;
        }

        // Testing getParameterNames()...
        String[] paramList = sh.getParameterNames();

        if (paramList != null) {
            fail("getParameterNames() should return null!");
        }

        // Testing getParameter()...
        String paramVal = sh.getParameter("ttl");
        assertTrue("getParameter() returned '" + paramVal +
            "' for the parameter 'ttl' that doesn't exist.", paramVal == null);
    }

    /**
     * Body of the test 5.
     *
     * Test for CSeq header field: setParameter()/removeParameter().
     */
    void Test5() {
        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test5 started ***");

        SipHeader sh = createSipHeader(headerName, "4711 INVITE");

        if (sh == null) {
            return;
        }

        // Testing setParameter()...
        try {
            sh.setParameter("test", "192.0.0.1");
        } catch (Exception e) {
            fail(e + " was thrown.");
        }

        try {
            sh.removeParameter("test");
        } catch (Exception e) {
            fail("removeParameter(): " + e + " was thrown!");
        }

        assertTrue(true); // to avoid error message from the test framework
    }

    /**
     * Run the tests
     */
    public void runTests() {
        declare("setName()/getName()");
        Test1();

        declare("getValue()/getHeaderValue()");
        Test2();

        declare("setValue()");
        testSetValue(headerName, "4711 INVITE");

        declare("getParameterNames()/getParameter()");
        Test4();

        declare("setParameter()/removeParameter()");
        Test5();

        declare("toString()");
        testToString(headerName, "4711 INVITE");
    }
}