Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack;
  
  
  
  
  import java.util.Arrays;
  import java.util.List;
 
 
 import  test.tck.msgflow.callflows.ScenarioHarness;
 
 import  junit.framework.Assert;
Test Issue 309 Via.setRPort() creates malformed rport parameter

Author(s):
jean.deruelle@gmail.com
 
 public class ViaRPortTest extends ScenarioHarness {
 
 	
     public ViaRPortTest() {
     	super("ViaRPortTest",true);
 	}
 
 
 	public final int SERVER_PORT = 5600;
 
     public final int CLIENT_PORT = 6500;
     
     protected String testProtocol = "udp";
 
 
 
 
 	public String host;
     	
 	public void setUp() throws Exception {
 		
 	}
 	
 	public void testRPort() throws Exception {
 		Server server = new Server();
 		Client client = new Client();
 		
 		Thread.sleep(1000);
 		
 		Request serverLastRequestReceived = server.getLastRequestReceived();
 		assertNotNull(serverLastRequestReceived);
 		
 		ListIterator<ViaHeaderiterator = serverLastRequestReceived.getHeaders(.);
 		assertTrue(iterator.hasNext());
 		int rport = iterator.next().getRPort();
 		
 		assertEquals(6500, rport);		
 	}
 	
 	
 	public class Server extends SipAdapter {
         protected SipStack sipStack;
 
         protected SipFactory sipFactory = null;
 
         protected SipProvider provider = null;
 
         private Request lastRequestReceived;
         
         public Server() {
            try {
                final Properties defaultProperties = new Properties();
                 = "127.0.0.1";
                defaultProperties.setProperty("javax.sip.STACK_NAME""server");
                defaultProperties.setProperty("gov.nist.javax.sip.TRACE_LEVEL""DEBUG");
                defaultProperties.setProperty("gov.nist.javax.sip.DEBUG_LOG""server_debug_ViaRPortTest.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.SERVER_LOG""server_log_ViaRPortTest.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.READ_TIMEOUT""1000");
                defaultProperties.setProperty("gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS",
                        "false");
                this. = SipFactory.getInstance();
                this..setPathName("gov.nist");
                this. = this..createSipStack(defaultProperties);
                this..start();
                ListeningPoint lp = this..createListeningPoint();
                this. = this..createSipProvider(lp);;
                this..addSipListener(this);
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail("unexpected exception ");
            }
        }
        public void stop() {
            this..stop();
        }
        public void processRequest(RequestEvent requestEvent) {
            = requestEvent.getRequest();
        }
		}                
    }
    public class Client extends SipAdapter {
        private SipFactory sipFactory;
        private SipStack sipStack;
        private SipProvider provider;
        private boolean o_sentInviteo_received180o_sentCancelo_receiver200Cancel,
                o_inviteTxTermo_dialogTerinated;
        public Client() {
            try {
                final Properties defaultProperties = new Properties();
                String host = "127.0.0.1";
                defaultProperties.setProperty("javax.sip.STACK_NAME""client");
                defaultProperties.setProperty("gov.nist.javax.sip.TRACE_LEVEL""DEBUG");
                defaultProperties.setProperty("gov.nist.javax.sip.DEBUG_LOG""client_debug.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.SERVER_LOG""client_log.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.READ_TIMEOUT""1000");
                defaultProperties.setProperty("gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS","false");
                this. = SipFactory.getInstance();
                this..setPathName("gov.nist");
                this. = this..createSipStack(defaultProperties);
                this..start();
                ListeningPoint lp = this..createListeningPoint(host);
                this. = this..createSipProvider(lp);
                 = this..createHeaderFactory();
                 = this..createMessageFactory();
                 = this..createAddressFactory();
                this..addSipListener(this);
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail("unexpected exception ");
            }
        }
        
        public void sendInviteWithRPort() throws Exception {
        	Address fromAddress = .createAddress("here@somewhere:5070");
        	ContactHeader contactHeader1 = .createContactHeader(.createAddress("sip:here@somewhere:5070"));
    		ContactHeader contactHeader2 = .createContactHeader(.createAddress("sip:here@somewhereelse:5080"));
    		
    		
			FromHeader from = .createFromHeader(fromAddress"1234");
			via.setRPort();
			List vias = Arrays.asList(via);			
    		
    		URI requestURI = .createURI("sip:test@"++":"+);
    		Request request = .createRequest(requestURI.callIdcSeqfromtoviasmaxForwards);
    		..println(request);
    		assertTrue(request.toString().indexOf("rport=") == -1);    		
    		
    		request.setRequestURI(requestURI);
    		request.addHeader(contactHeader1);
    		request.addHeader(contactHeader2);
    		ClientTransaction ctx = this..getNewClientTransaction(request);
    		ctx.sendRequest();
        }
    }
	private static class SipAdapter implements SipListener {
		public void processIOException(IOExceptionEvent arg0) {}
		public void processRequest(RequestEvent arg0) {}
		public void processResponse(ResponseEvent arg0) {}
		public void processTimeout(TimeoutEvent arg0) {}
	}
New to GrepCode? Check out our FAQ X