Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.tck.factory;
  
  import java.util.List;
  import java.util.TreeSet;
 
 
 import  junit.framework.Test;
 import  junit.framework.TestSuite;
 import  test.tck.TckInternalError;
 import  test.tck.TiUnexpectedError;
 
 public class MessageFactoryTest extends FactoryTestHarness {
 
     protected String method;
 
     protected javax.sip.address.URI tiRequestURI;
     protected CallIdHeader ticallId;
     protected FromHeader tifrom;
     protected ToHeader tito;
     protected MaxForwardsHeader timaxForwards;
     protected ContentTypeHeader ticontentType;
     protected List tivia;
     protected CSeqHeader ticSeq;
     protected ExtensionHeader tiextensionHeader;
 
     protected javax.sip.address.URI riRequestURI;
     protected CallIdHeader ricallId;
     protected FromHeader rifrom;
     protected ToHeader rito;
     protected MaxForwardsHeader rimaxForwards;
     protected ContentTypeHeader ricontentType;
     protected List rivia;
     protected CSeqHeader ricSeq;
     protected ExtensionHeader riextensionHeader;
 
     protected byte[] contentBytes;
     protected Object contentObject;
     protected int statusCode;
 
     public MessageFactoryTest() {
         super("MessageFactoryTest");
     }
 
 
     private void cloneTest(Request request) {
         try {
             Request newRequest = (Requestrequest.clone();
             assertEquals(newRequest,request);
             newRequest.setMethod("FOOBAR");
             assertFalse(newRequest.equals(request));
             newRequest = (Requestrequest.clone();
             // Add the extension header.
             Header extensionHeader = tiHeaderFactory.createHeader("My-Header",
                     "my header value");
             request.addHeader(extensionHeader);
             assertFalsenewRequest.equals(request) );
             request.removeHeader("My-Header");
             assertEquals(newRequestrequest);
         } catch (Exception ex) {
             ex.printStackTrace();
             fail(ex.getMessage());
         } finally {
             logTestCompleted("cloneTest(request)");
         }
     }
 
    private void cloneTest(Response response) {
         try {
             Response newResponse = (Responseresponse.clone();
             assertEquals(newResponseresponse);
             assertNotSame(newResponseresponse);
             newResponse = (Responseresponse.clone();
             // Add the extension header.
             Header extensionHeader = tiHeaderFactory.createHeader("My-Header",
                     "my header value");
             newResponse.addHeader(extensionHeader);
             assertFalse(newResponse.equals(response));
             newResponse.removeHeader("My-Header");
             assertEquals(newResponseresponse);
        } catch (ParseException ex) {
            fail(ex.getMessage());
        } finally {
            logTestCompleted("cloneTest(response)");
        }
    }

   
Tests whether the TI is compliant with the RI for a created message 'Compliant' means that the TI message must at least contain all headers that the RI message contains, and each such header must be equal (according to the RI definition of equals()) Note that the ~order~ in which the headers appear is arbitrary, and that the TI may contain additional headers (e.g. User-Agent)

Parameters:
tiMsg
riMsg
Author(s):
JvB
   private void testAgainstRIMsgMessage tiMsgMessage riMsg ) {
       // Test equality using RI definition of equals
       assertEqualsriMsg.getHeader. ), tiMsg.getHeader. ) );
       assertEqualsriMsg.getHeader. ), tiMsg.getHeader. ) );
       assertEqualsriMsg.getHeader. ), tiMsg.getHeader. ) );
       assertEqualsriMsg.getHeader. ), tiMsg.getHeader. ) );
       assertEqualsriMsg.getHeader. ), tiMsg.getHeader. ) );
   }
   private void testAgainstRIMsgContentMessage tiMsgMessage riMsg ) {
       // Test equality using RI definition of equals
       assertEqualsriMsg.getHeader. ), tiMsg.getHeader. ) );
       assertEqualsriMsg.getContent(), tiMsg.getContent() );
   }
   private void testAgainstRI(Response tiResponseResponse riResponse) {
        try {
            assertEqualsriResponse.getStatusCode(), tiResponse.getStatusCode() );
            testAgainstRIMsgtiResponseriResponse );
        } catch (Throwable ex) {
            ex.printStackTrace();
            fail(ex.getMessage());
        } finally {
            logTestCompleted("testAgainstRI(testResponse)");
        }
    }
   private void testAgainstRI(Request tiRequestRequest riRequest) {
        try {
            assertEquals(riRequest.getMethod(), tiRequest.getMethod());
            assertEquals(riRequest.getRequestURI(), tiRequest.getRequestURI() );
            assertEquals(riRequest.getHeader. ), tiRequest.getHeader. ) );
            testAgainstRIMsg(tiRequestriRequest);
        } catch (Throwable ex) {
            ex.printStackTrace();
            fail(ex.getMessage());
        } finally {
            logTestCompleted("testAgainstRI(testRequest)");
        }
    }
    public void testCreateRequest() {
    try {
        Request tiRequest = tiMessageFactory.createRequest
           (,
            ,,,,,
                    ,
            );
        cloneTest(tiRequest);
        Request riRequest = riMessageFactory.createRequest
           (,
            ,,,,,
                 ,
            );
        testAgainstRItiRequestriRequest );
        testAgainstRIMsgContenttiRequestriRequest );
        tiRequest = tiMessageFactory.createRequest
                   );
        cloneTest(tiRequest);
        riRequest = riMessageFactory.createRequest
                );
        cloneTest(tiRequest);
        testAgainstRI(tiRequest,riRequest);
        testAgainstRIMsgContenttiRequestriRequest );
        Response tiResponse = tiMessageFactory.createResponse(,
                    ,
                    );
        cloneTest(tiResponse);
        Response riResponse = riMessageFactory.createResponse(,
                ,
                );
        testAgainstRI(tiResponse,riResponse);
        testAgainstRIMsgContenttiResponseriResponse );
         tiResponse = tiMessageFactory.createResponse(,
                    ,
                    );
        cloneTest(tiResponse);
         riResponse = riMessageFactory.createResponse(,
                ,
                );
        testAgainstRI(tiResponse,riResponse);
        testAgainstRIMsgContenttiResponseriResponse );
        tiResponse = tiMessageFactory.createResponse(,
                    );
        cloneTest(tiResponse);
        riResponse = riMessageFactory.createResponse(,
                );
        testAgainstRI(tiResponse,riResponse);
        tiResponse = tiMessageFactory.createResponse(,  tiRequest,
                     ,  ) ;
        cloneTest(tiResponse);
        riResponse = riMessageFactory.createResponse(,  riRequest,
                 ,  ) ;
        testAgainstRI(tiResponse,riResponse);
        testAgainstRIMsgContenttiResponseriResponse );
        tiResponse = tiMessageFactory.createResponse(tiRequest,
                 ,  ) ;
        cloneTest(tiResponse);
        riResponse = riMessageFactory.createResponse(riRequest,
                 ,  ) ;
        testAgainstRI(tiResponse,riResponse);
        testAgainstRIMsgContenttiResponseriResponse );
        tiResponse = tiMessageFactory.createResponse(,  tiRequest) ;
        cloneTest(tiResponse);
        riResponse = riMessageFactory.createResponse(,  riRequest) ;
        testAgainstRI(tiResponse,riResponse);
    } catch (Throwable ex) {
        ex.printStackTrace();
        failex.getMessage() );
    } finally {
        logTestCompleted("testCreateRequest()");
    }
    }
    public void testCreateRequest1() {
        try {
            Request request = tiMessageFactory.createRequest(,
                    );
            String msgString = request.toString();
            assertEquals(request, tiMessageFactory.createRequest(msgString));
        } catch (Exception ex) {
            ex.printStackTrace();
            failex.getMessage() );
        } finally {
            logTestCompleted("testCreateRequest1()");
        }
    }
    private void testGetMethods(Request refRequestRequest requestToTest)
    throws IllegalAccessException,
        InvocationTargetException {
    try {
        Class implementationClass;
        implementationClass = refRequest.getClass();
        Class[] implementedInterfaces = implementationClass.getInterfaces();
        int j = 0;
        // Do a TCK consistency check.
        for ( j = 0; j < implementedInterfaces.lengthj++) {
            if ( javax.sip.message.Request.class.isAssignableFrom
            (implementedInterfaces[j]))
                break;
        }
        if (j == implementedInterfaces.length) {
            ..println("Hmm... could not find it" +
            refRequest.getClass());
        throw new TckInternalError("Request not implemented");
        }
        String jainClassName = implementedInterfaces[j].getName();
        // Make sure that all the methods of the interface are implemented
        checkImplementsInterface(requestToTest.getClass(),
        Request.class);
        // Test the get methods of the interface.
        Method methods[] = implementedInterfaces[j].getDeclaredMethods();
        for (int i = 0; i < methods.length;  i++) {
            String methodName = methods[i].getName();
            if  (! methodName.startsWith("get") ) continue;
            // Testing only null argument gets
            if ( methods[i].getParameterTypes().length != 0) continue;
            Class returnType = methods[i].getReturnType();
            Object refType = null;
            try {
                refType = methods[i].invoke(refRequest,(Object[])null);
            } catch (Exception ex) {
                throw new TckInternalError("Invocation failure " + methodName);
            }
            String ftype = returnType.toString();
            if (returnType.isPrimitive()) {
                Object testValue = methods[i].invokerequestToTest, (Object[])null);
                assertEqualsrefTypetestValue );
            } else {
                // Non primitive.
                Object testValue = methods[i].invokerequestToTest,(Object[])null);
                if (refType != null) {
                    assertTrue(testValue != null);
                    // Find the jain type implemented by the interface.
                    Class fclass = refType.getClass();
                    Class[] fInterfaces = fclass.getInterfaces();
                    // Find what JAIN interface this is implementing.
                    int k = 0;
                    for ( k = 0; k < fInterfaces.lengthk++) {
                        if ( javax.sip.header.Header.class.isAssignableFrom(fInterfaces[k]))
                            break;
                    }
                    // If this implements a header interface
                    // check that the same header is returned in both
                    // ti and ri
                    if ( k < fInterfaces.length) {
                        // Make sure that the return type matches.
                        assertTrue(fInterfaces[k].isAssignableFrom(testValue.getClass()));
                        String refhdrString = refType.toString();
                        String testhdrString = testValue.toString();
                        // Use the factory to test for equivalence
                        Header riHeader = createRiHeaderFromString(refhdrString);
                        if (riHeader == null)
                            throw new TckInternalError( "could not parse "  + refhdrString );
                        // Create a RI header from the string to test
                        // for equivalence. Note that we cannot compare
                        // ti header to RI header otherwise.
                        Header tiHeader = createRiHeaderFromString(testhdrString);
                        assertNotNull(tiHeader);
                        assertEquals("Retrieved header did not match RI",riHeader,tiHeader);
                    }
                } else if (refType == null) {
                    assertTrue(testValue == null);
                }
            }
        }
    } catch (Exception ex) {
        throw new TiUnexpectedError(ex.getMessage());
    }
    }
    public void testMessageGetMethods() {
        try {
            Request tiRequest = tiMessageFactory.createRequest();
            Request riRequest = riMessageFactory.createRequest();
            testGetMethods(riRequest,tiRequest);
        } catch (Exception ex) {
            assertTrue(false);
        } finally {
            logTestCompleted("testMessageGetMethods()");
        }
    }
    public void testGetHeaderNames() {
        // BUG report by Ben Evans (opencloud)
        //  This test assumed implementations will store header names
        // using mixed case, eg. "Content-Type" not "content-type".
        // Header names are not case sensitive in RFC3261 so this is not
        // a real requirement.
        try {
            Request request = tiMessageFactory.createRequest
            (,
                    ,,,,,
                    ,
                    );
            ListIterator li = request.getHeaderNames();
            // Use a tree set with case insensitive ordering & comparison
            TreeSet set = new TreeSet(.);
            while (li.hasNext()) {
                String hdrName = (Stringli.next();
                set.add(hdrName);
            }
            assertTrue (  set.contains(.) ) ;
            assertTrue (  set.contains(.) ) ;
            assertTrue (  set.contains(.)   ) ;
            assertTrue ( set.contains(.)   );
            assertTrue (  set.contains(.) ) ;
            assertTrue ( set.contains(.) ) ;
            assertTrue (  set.contains(.) ) ;
            Response response = tiMessageFactory.createResponse
            (   ,  ,
                ,  ,  ,  ,
                ,  );
            li = request.getHeaderNames();
            set.clear();
            while (li.hasNext()) {
                String hdrName = (String)li.next();
                set.add(hdrName);
            }
            //
            // JvB: Headers may use short names too
            //
            assertTrue (  set.contains(.) ) ;
            assertTrue (  set.contains(.) || set.contains("f") ) ;
            assertTrue (  set.contains(.) || set.contains("t")  ) ;
            assertTrue (  set.contains(.) || set.contains("v") ) ;
            assertTrue (  set.contains(.) ) ;
            assertTrue (  set.contains(.) || set.contains("c") ) ;
            assertTrue (  set.contains(.) || set.contains("i") ) ;
        } catch (Exception ex) {
            ex.printStackTrace();
            failex.getMessage() );
        } finally {
            logTestCompleted("testGetHeaderNames()");
        }
    }
    public void testAddRemoveHeader() {
        try{
            Request request = tiMessageFactory.createRequest
                    );
            request.addHeader();
            ListIterator  viaHeaders = request.getHeaders(.);
            ViaHeader viaHeader = (ViaHeaderviaHeaders.next();
            assertTrue ( viaHeader.equals(.iterator().next())) ;
            Header header  = request.getHeader("My-Header");
            assertTrue (header != null && header.equals());
            request.removeHeader("My-Header");
            assertNullrequest.getHeader("My-Header") ) ;
            Response response = tiMessageFactory.createResponse
            (,  ,  ,  ,  ,
                    ,  );
            response.addHeader();
            viaHeaders = request.getHeaders(.);
            viaHeader = (ViaHeader)viaHeaders.next();
            assertEqualsviaHeader.iterator().next() ) ;
            header  = response.getHeader("My-Header");
            assertEquals(header);
            response.removeHeader("My-Header");
            assertNull(response.getHeader("My-Header")) ;
            ContentLengthHeader cl = response.getContentLength();
            assertNotNull(cl) ;
        } catch (Exception ex) {
            ex.printStackTrace();
            failex.getMessage() );
        } finally {
            logTestCompleted("testAddRemoveHeader()");
        }
    }
 
	public void testCharset() {
	try{
		Request request = tiMessageFactory.createRequest( 
		  "MESSAGE sip:127.0.0.1 SIP/2.0\r\n"+
		  "Via: SIP/2.0/TCP 127.0.0.1:5060;rport=5060;branch=z9hG4bKd2c87858eb0a7a09becc7a115c608d27\r\n"+
		  "CSeq: 2 BYE\r\n"+
		  "Call-ID: 84a5c57fd263bcce6fec05edf20c5aba@127.0.0.1\r\n"+
		  "From: \"The Master Blaster\" <sip:BigGuy@here.com>;tag=12345\r\n"+
		  "To: \"The Little Blister\" <sip:LittleGuy@there.com>;tag=2955\r\n"+
		  "Max-Forwards: 70\r\n"+
		  "Content-Type: text/plain;charset=ISO-8859-1\r\n" +
		  "Content-Length: 0\r\n"+
		  "\r\n"
		);
		// JvB: in UTF-8 these character would be encoded as multiple bytes
		byte[] content = "��".getBytes"ISO-8859-1" );
		request.setContentnew String(content,"ISO-8859-1"), 
		assertEquals( 2, request.getRawContent().length );		
catch (Exception t) {
	  fail"ParseException"t );
finally {
		logTestCompleted("testCharset()");
	}
    public void setUp() {
        try {
            String fromName = "BigGuy";
            String fromSipAddress = "here.com";
            String fromDisplayName = "The Master Blaster";
            String toSipAddress = "there.com";
            String toUser = "LittleGuy";
            String toDisplayName = "The Little Blister";
            // create >From Header
            SipURI tifromAddress = tiAddressFactory.createSipURI(fromName,fromSipAddress);
            SipURI rifromAddress = riAddressFactory.createSipURI(fromName,fromSipAddress);
            Address tifromNameAddress = tiAddressFactory.createAddress(tifromAddress);
            Address rifromNameAddress = riAddressFactory.createAddress(rifromAddress);
            tifromNameAddress.setDisplayName(fromDisplayName);
            rifromNameAddress.setDisplayName(fromDisplayName);
             = tiHeaderFactory.createFromHeader(tifromNameAddress"12345");
             = riHeaderFactory.createFromHeader(rifromNameAddress"12345");
            // create To Header
            SipURI titoAddress = tiAddressFactory.createSipURI(toUser,toSipAddress);
            SipURI ritoAddress = riAddressFactory.createSipURI(toUser,toSipAddress);
            Address titoNameAddress = tiAddressFactory.createAddress(titoAddress);
            Address ritoNameAddress = riAddressFactory.createAddress(ritoAddress);
            titoNameAddress.setDisplayName(toDisplayName);
            ritoNameAddress.setDisplayName(toDisplayName);
             = tiHeaderFactory.createToHeader(titoNameAddressnull);
             = riHeaderFactory.createToHeader(ritoNameAddressnull);
            // create Request URIs
             = tiAddressFactory.createSipURI(toUsertoSipAddress);
             = riAddressFactory.createSipURI(toUsertoSipAddress);
             = tiHeaderFactory.createMaxForwardsHeader(70);
             = riHeaderFactory.createMaxForwardsHeader(70);
             = tiHeaderFactory.createContentTypeHeader("application""sdp");
             = riHeaderFactory.createContentTypeHeader("application""sdp");
             = new LinkedList();
             = new LinkedList();
            ViaHeader tiviaHeader = tiHeaderFactory.createViaHeader("127.0.0.1",5060, "udp"null);
            .add(tiviaHeader);
            ViaHeader riviaHeader = riHeaderFactory.createViaHeader("127.0.0.1",5060, "udp"null);
            .add(riviaHeader);
             = tiHeaderFactory.createCallIdHeader("12345@127.0.0.1");
             = riHeaderFactory.createCallIdHeader("12345@127.0.0.1");
             = "v=0\r\n"
                    + "o=4855 13760799956958020 13760799956958020"
                    + " IN IP4  129.6.55.78\r\n" + "s=mysession session\r\n"
                    + "p=+46 8 52018010\r\n" + "c=IN IP4  129.6.55.78\r\n"
                    + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n"
                    + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
                    + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
             = ((String).getBytes();
             = .;
             = tiHeaderFactory.createCSeqHeader(1L, );
             = riHeaderFactory.createCSeqHeader(1L, );
             = 200;
             = (ExtensionHeader) tiHeaderFactory.createHeader(
                    "My-Header""My Header Value");
             = (ExtensionHeader) riHeaderFactory.createHeader(
                    "My-Header""My Header Value");
        } catch (Throwable ex) {
            ex.printStackTrace();
            failex.getMessage() );
        } // finally { assertTrue(true); }
    }
    public void tearDown() {
        riFactory.resetFactory();
        tiFactory.resetFactory();
    }
    public static Test suite()  {
    return new TestSuite(MessageFactoryTest.class);
    }
    public static void main(String[] args) {
    junit.textui.TestRunner.run(MessageFactoryTest.class);
    }
New to GrepCode? Check out our FAQ X