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

SipHeaderChecker

public class SipHeaderChecker extends Object
SipHeaderChecker is a class to check SipHeader functionality

Fields Summary
private static String
shortForm
Short form of header.
static SipHeaderChecker[]
testCases
Array of testcases.
String
name
Current name being processed.
String
value
Current value being processed.
String
parsedHeaderValue
Parsed header values.
String[]
parameters
Extracted parameters.
String
parsedName
Parsed name.
Constructors Summary
public SipHeaderChecker(String header, String parsedHeaderValue, String[] parameters)
Constructor for basic test case.

param
header the test input string
param
parsedHeaderValue expected value string
param
parameters expected paramaters from parsed header


                                
        
			      
        int i = header.indexOf(':");
        name = header.substring(0, i);
        value = header.substring(i + 1);
        this.parsedHeaderValue = parsedHeaderValue;
        this.parameters = parameters;
        parsedName = toLongForm(name);
    
Methods Summary
booleancheck()
Checks if test passed.

return
true if test passes


	// DEBUG: System.out.println("Header: " + name + ":" + value);

        SipHeader h;
        try {
            h = new SipHeader(name, value);
        } catch (IllegalArgumentException e) {
            System.out.println("Error parsing the header:" + e
			       + " name=" + name
			       + " value=" + value);
            e.printStackTrace();
            return false;
        }

        String strValue = h.toString();

        boolean ok = true;

        ok &= checkName(h);
        ok &= checkValue(h);
        ok &= checkParameters(h);

	// DEBUG: System.out.println("Checking getHeaderValue result");

        try {
            h = new SipHeader(h.getName(), h.getHeaderValue());
        } catch (IllegalArgumentException e) {
            System.out.println("Error parsing values returned by parser " + e
			       + " name=" + h.getName()
			       + " value=" + h.getHeaderValue());
            e.printStackTrace();
            return false;
        }

        ok &= checkName(h);
        ok &= checkValue(h);
        ok &= checkParameters(h);

        // DEBUG: System.out.println("Checking toString result");

        int i = strValue.indexOf(':");

        try {
            h = new SipHeader(strValue.substring(0, i).trim(),
                              strValue.substring(i + 1).trim());
        } catch (IllegalArgumentException e) {
            System.out.println("Error parsing toString result " + e
			       + " name=" + strValue.substring(0, i).trim()
			       + " value=" + strValue.substring(i + 1).trim());
            e.printStackTrace();
            return false;
        }

        ok &= checkName(h);
        ok &= checkValue(h);
        ok &= checkParameters(h);

        return ok;
    
booleancheckName(SipHeader h)
Checks for valid name in hedaer.

param
h the header to be validated
return
true if name is valid


        String pname = h.getName();

        if (! (parsedName.equalsIgnoreCase(pname) ||
               toShortForm(parsedName).equalsIgnoreCase(pname))) {
            System.out.println("Invalid name " + h.getName());
            return false;
        }
        return true;
    
booleancheckParameters(SipHeader h)
Checks for valid parameters in header.

param
h the header to be validated
return
true if parameters are valid


        String[] paramNames = h.getParameterNames();

        if (parameters.length == 0) {
            if (paramNames != null) {
                System.out.println("getParameterNames must return null");
                return false;
            }
            return true;
        }

        if (paramNames == null) {
            System.out.println("getParameterNames returns null");
            return false;
        }
	if (parameters.length != paramNames.length) {
	    System.out.println("getParameterNames returns invalid "+
			       "number of parameters: " + paramNames.length +
			       ", must be " + parameters.length);
	    return false;
	}

        for (int i = 0; i < parameters.length; i++) {

            String paramName = parameters[i][0];
            String paramValue = parameters[i][1];

            boolean found = false;
            for (int j = 0; j < paramNames.length; j++) {
                if (paramName.equalsIgnoreCase(paramNames[j])) {
                    found = true;
                    break;
                }
            }

            if (! found) {
                System.out.println(paramName +
				   "not found in results of getParameterNames");
                return false;
            }

            String v = h.getParameter(paramName);

            if (paramValue == null) {
                if (v != null) {
                    System.out.println("null expected: " + paramName);
                    return false;
                }
            } else {
                if (v == null) {
                    System.out.println("parameter " + paramName +
				       " not found");
                    return false;
                }

                v = fixSpaces(v);

                boolean isSensitive = v.startsWith("\"");

                if (! (isSensitive ? v.equals(paramValue) :
		       v.equalsIgnoreCase(paramValue))) {
                    System.out.println("Invalid parameter value: " +
				       v + ".");
                    return false;
                }
            }
        }
        return true;
    
booleancheckValue(SipHeader h)
Checks for valid value in header.

param
h the header to be validated
return
true if value is valid


        String pValue = h.getValue();

        if ((pValue == null || parsedHeaderValue == null) &&
	    pValue != parsedHeaderValue) {
            System.out.println("getValue(): Invalid value '" + h.getValue() +
			       "', must be '" + parsedHeaderValue + "'");
            return false;
        }

        pValue = fixSpaces(pValue);
        String name = toShortForm(h.getName());

        // In case of "t:"/"T:" header, parsedHeaderValue is
        // a SIP URI, so add "<"/">" around the value.
        if (name.equalsIgnoreCase("t")) {
            if (!pValue.startsWith("<")) {
                pValue = "<" + pValue + ">";
            }

            if (!parsedHeaderValue.startsWith("<")) {
                parsedHeaderValue = "<" + parsedHeaderValue + ">";
            }
        }

        boolean isSensitive = name.equals("i") ||
	    name.equals("cseq") ||
	    name.equals("date");

        if (!(isSensitive ?
	    pValue.equals(parsedHeaderValue) :
	    pValue.equalsIgnoreCase(parsedHeaderValue))) {
            System.out.println("Invalid value '" + h.getValue() +
			       "', must be '" + parsedHeaderValue + "'");
            return false;
        }
        return true;
    
public static java.lang.StringfixSpaces(java.lang.String src)
Inserts white space into string.

param
src input string to be processed
return
expanded whitespace string


        if (src == null) {
            return null;
        }

        StringBuffer dst = new StringBuffer();

        boolean quoted = false;
        boolean prevBlank = false;
        for (int i = 0; i < src.length(); i++) {
            char c = src.charAt(i);

            if (c == '\"") {
                quoted = ! quoted;
            }

            if (c == '\t") {
                c = ' ";
            }
            boolean isBlank = c == ' ";
            if (quoted || ! (prevBlank && isBlank)) {
                dst.append(c);
            }
            prevBlank = isBlank;
        }
        return dst.toString();
    
static java.lang.StringfixString(java.lang.String src)
Extracts quoted strings.

param
src input string to be processed
return
quoted string

        if (src == null) {
            return null;
        }
        if (src.indexOf('\"") == -1) {
            return src;
        }
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < src.length(); i++) {
            char c = src.charAt(i);
            if (c == '\"") {
                s.append('\\");
            }
            if (c == '\t") {
                s.append('\\");
                c = 't";
            }
            s.append(c);
        }
        return s.toString();
    
public static intrunTests()
Runs the set of tests.

return
number of failed tests

        int failed = 0, passed = 0;

        for (int i = 0; i < testCases.length; i++) {
            if (testCases[i].check()) {
                passed++;
            } else {
		System.out.println("\n*** " + i + "  "
				   + testCases[i].name + ":"
				   + testCases[i].value);
                failed++;
            }
        }
	if (failed > 0) {
	    System.out.println("\n*** RESULT: "
			       + passed + " passed, "
			       + failed + " failed; "
			       + "total = " + (passed + failed) + "\n");
	}
        return failed;
    
public static java.lang.StringtoLongForm(java.lang.String name)
Output to long format string.

param
name label to process
return
formatted string


        name = name.toLowerCase();
        int i = shortForm.indexOf(name + " ");
        return i == -1 ? name :
	    shortForm.substring(i + 2, shortForm.indexOf(':", i));
    
public static java.lang.StringtoShortForm(java.lang.String name)
Output from short format string.

param
name label for format string
return
formatted string


                       
         

        name = name.toLowerCase();
        int i = shortForm.indexOf(" " + name + ":");
        return i == -1 ? name : shortForm.substring(i - 1, i);