String toTag = ((ToHeader) responseEvent.getResponse().getHeader("to")).getTag(); if (toTag!=null) { ((ToHeader) ack.getHeader("to")).setTag( toTag ); }
	public void testProceedingCompletedConfirmedScenario() {		try {			Request invite = createRiInviteRequest(null, null, null);			SipEventCollector responseCollector = new SipEventCollector();			//Before Sending the request we should first register a listener with the			//RI that would catch the TRYING response			try {				responseCollector.collectResponseEvent(riSipProvider);			} catch (TooManyListenersException ex) {				throw new TckInternalError(					"Failed to register a SipListener with an RI SipProvider",					ex);			}			//Send the initial request			try {				eventCollector.collectRequestEvent(tiSipProvider);				riSipProvider.sendRequest(invite);			} catch (SipException ex) {				throw new TckInternalError(					"A SipExceptionOccurred while trying to send request!",					ex);			} catch (TooManyListenersException ex) {				throw new TiUnexpectedError(					"Failed to register a SipListener with a TI SipProvider",					ex);			}			waitForMessage();			RequestEvent inviteReceivedEvent =				eventCollector.extractCollectedRequestEvent();			if (inviteReceivedEvent == null				|| inviteReceivedEvent.getRequest() == null)				throw new TiUnexpectedError("The initial invite request was not received by the TI!");			//Let's create the transaction			ServerTransaction tran = null;			try {				tran =					tiSipProvider.getNewServerTransaction(						inviteReceivedEvent.getRequest());			} catch (Exception ex) {				ex.printStackTrace();				fail(					ex.getClass().getName()						+ "was thrown while trying to "						+ "create the server transaction");			}			assertNotNull(				"tiSipProvider.getNewServerTransaction() returned null",				tran);			//Check whether a TRYING response has been sent.			//wait for the trying response			waitForMessage();			// At this point state must be PROCEEDING			assertEquals(TransactionState.PROCEEDING, tran.getState());			ResponseEvent responseEvent =				responseCollector.extractCollectedResponseEvent();			assertNotNull(				"No TRYING response has been sent by the TI upon reception "					+ "of an INVITE request",				responseEvent);			assertTrue(				"A response different from 100 was sent by the TI upon "					+ "reception of INVITE",				Response.TRYING == responseEvent.getResponse().getStatusCode());			//Resend the invite and see that a TRYING response is resent			try {				//listen for the Trying response				responseCollector.collectResponseEvent(riSipProvider);			} catch (TooManyListenersException ex) {				throw new TckInternalError(					"Failed to register a SipListener with an RI SipProvider",					ex);			}			try {				eventCollector.collectRequestEvent(tiSipProvider);				riSipProvider.sendRequest(invite);			} catch (SipException ex) {				throw new TckInternalError(					"A SipExceptionOccurred while trying to send request!",					ex);			} catch (TooManyListenersException ex) {				throw new TiUnexpectedError(					"Failed to register a SipListener with a TI SipProvider",					ex);			}			//Wait for the INVITE			waitForMessage();			inviteReceivedEvent = eventCollector.extractCollectedRequestEvent();			assertNull(				"Retransmitted INVITEs should not be passed to the TU",				inviteReceivedEvent);			//Wait for a retransmitted TRYING response			waitForMessage();			//Verify whether there was a TRYING response			responseEvent = responseCollector.extractCollectedResponseEvent();			assertNotNull(				"No TRYING response has been sent by the TI upon reception "					+ "of an INVITE request",				responseEvent);			assertTrue(				"A response different from 100 was sent by the TI upon "					+ "reception of INVITE",				Response.TRYING == responseEvent.getResponse().getStatusCode());			//Create & send RINGING. See that it is properly sent			//and that tran state doesn't change			Response ringing = null;			try {				ringing =					tiMessageFactory.createResponse(						Response.RINGING,						tran.getRequest());				((ToHeader) ringing.getHeader(ToHeader.NAME)).setTag(					Integer.toString(hashCode()));				addStatus(tran.getRequest(), ringing);                // BUG report from Ben Evans:				// set contact header on dialog-creating response                ringing.setHeader(createTiContact());			} catch (ParseException ex) {				throw new TiUnexpectedError(					"A ParseException was thrown while trying to create a ringing "						+ "response using TI",					ex);			}			try {				//listen for the RINGING response				responseCollector.collectResponseEvent(riSipProvider);			} catch (TooManyListenersException ex) {				throw new TckInternalError(					"Failed to register a SipListener with an RI SipProvider",					ex);			}			try {				tran.sendResponse(ringing);			} catch (SipException ex) {				ex.printStackTrace();				fail("The TI failed to send a RINGING response");			}			//The Transaction should still be PROCEEDING			assertEquals(				"The Transaction did not remain PROCEEDING after transmitting a RINGING response",				TransactionState.PROCEEDING,				tran.getState());			//Check whether the RINGING is received by the RI.			waitForMessage();			responseEvent = responseCollector.extractCollectedResponseEvent();			assertNotNull(				"The RINGING response was not received by the RI",				responseEvent);			assertTrue(				"A response different from RINGING was sent by the TI",				Response.RINGING					== responseEvent.getResponse().getStatusCode());			//Resend the INVITE, see that it is hidden from the TU and see that			//the _RINGING_ response is resent (and not the TRYING)			try {				//listen for the Trying response				responseCollector.collectResponseEvent(riSipProvider);			} catch (TooManyListenersException ex) {				throw new TckInternalError(					"Failed to register a SipListener with an RI SipProvider",					ex);			}			try {				eventCollector.collectRequestEvent(tiSipProvider);				riSipProvider.sendRequest(invite);			} catch (SipException ex) {				throw new TckInternalError(					"A SipExceptionOccurred while trying to send request!",					ex);			} catch (TooManyListenersException ex) {				throw new TiUnexpectedError(					"Failed to register a SipListener with a TI SipProvider",					ex);			}			//Wait for the INVITE			waitForMessage();			inviteReceivedEvent = eventCollector.extractCollectedRequestEvent();			assertNull(				"Retransmitted INVITEs should not be passed to the TU",				inviteReceivedEvent);			//Wait for a retransmitted RINGING response			waitForMessage();			//Verify whether there was a RINGING response			responseEvent = responseCollector.extractCollectedResponseEvent();			assertNotNull(				"No RINGING response has been sent by the TI upon reception "					+ "of an INVITE request",				responseEvent);			assertTrue(				"A response different from RINGING was sent by the TI upon "					+ "reception of a retransmitted invite INVITE",				Response.RINGING					== responseEvent.getResponse().getStatusCode());			//We should still be proceeding			assertEquals(				"The server transaction left the PROCEEDING state.",				TransactionState.PROCEEDING,				tran.getState());			//Send 300 - 699 from TU and see the tran goes COMPLETED			Response busy = null;			try {				busy =					tiMessageFactory.createResponse(						Response.BUSY_HERE,						tran.getRequest());				addStatus(tran.getRequest(), busy);			} catch (ParseException ex) {				throw new TiUnexpectedError(					"A ParseException was thrown while trying to create a busy_here "						+ "response using TI",					ex);			}			try {				//listen for the BUSY_HERE response				responseCollector.collectResponseEvent(riSipProvider);			} catch (TooManyListenersException ex) {				throw new TckInternalError(					"Failed to register a SipListener with an RI SipProvider",					ex);			}			try {				tran.sendResponse(busy);			} catch (SipException ex) {				ex.printStackTrace();				fail("The TI failed to send a BUSY_HERE response");			}			//The Transaction should now be COMPLETED			assertEquals(				"The Transaction did not remain COMPLETED after transmitting a BUSY_HERE response",				TransactionState.COMPLETED,				tran.getState());			//Check whether the BUSY_HERE is received by the RI.			waitForMessage();			responseEvent = responseCollector.extractCollectedResponseEvent();			assertNotNull(				"The BUSY_HERE response was not received by the RI",				responseEvent);			assertTrue(				"A response different from BUSY_HERE was sent by the TI",				Response.BUSY_HERE					== responseEvent.getResponse().getStatusCode());			//Resend the initial from INVITE from the RI and see that TI			//resends the 300 - 699 (see that tran state remains COMPLETED)			try {				//listen for the Trying response				responseCollector.collectResponseEvent(riSipProvider);			} catch (TooManyListenersException ex) {				throw new TckInternalError(					"Failed to register a SipListener with an RI SipProvider",					ex);			}			try {				eventCollector.collectRequestEvent(tiSipProvider);				riSipProvider.sendRequest(invite);			} catch (SipException ex) {				throw new TckInternalError(					"A SipExceptionOccurred while trying to send request!",					ex);			} catch (TooManyListenersException ex) {				throw new TiUnexpectedError(					"Failed to register a SipListener with a TI SipProvider",					ex);			}			//Wait for the INVITE			waitForMessage();			inviteReceivedEvent = eventCollector.extractCollectedRequestEvent();			assertNull(				"Retransmitted INVITEs should not be passed to the TU",				inviteReceivedEvent);			//Wait for a retransmitted BUSY_HERE response			waitForMessage();			//Verify whether there was a BUSY_HERE response			responseEvent = responseCollector.extractCollectedResponseEvent();			assertNotNull(				"No BUSY_HERE response has been sent by the TI upon reception "					+ "of a retransmitted INVITE request",				responseEvent);			assertTrue(				"A response different from BUSY_HERE was sent by the TI upon "					+ "reception of a retransmitted invite INVITE",				Response.BUSY_HERE					== responseEvent.getResponse().getStatusCode());			//We should still be COMPLETED			assertEquals(				"The server transaction left the COMPLETED state.",				TransactionState.COMPLETED,				tran.getState());			//Send an ack from the RI and see that the tran goes CONFIRMED			//and that response retransmissions cease			Request ack = (Request) invite.clone();			try {				eventCollector.collectRequestEvent(tiSipProvider);				ack.setMethod(Request.ACK);				riSipProvider.sendRequest(ack);			} catch (TooManyListenersException ex) {				throw new TiUnexpectedError(					"Failed to register a SipListener with the TI provider",					ex);			} catch (Exception ex) {				throw new TckInternalError(					"Failed to create an ack request",					ex);			}			waitForMessage();			RequestEvent ackEvent =				eventCollector.extractCollectedRequestEvent();			assertNull(				"ACKs in ServerInviteTransactions shouldn't be passed to the TU.",				ackEvent);			assertEquals(				"The ServerTransaction did not pas into the confirmed state"					+ "after receiving an ACK.",				TransactionState.CONFIRMED,				tran.getState());		} catch (Throwable exc) {			exc.printStackTrace();			fail(exc.getClass().getName() + ": " + exc.getMessage());		}		assertTrue(new Exception().getStackTrace()[0].toString(), true);	}