Best Citrus code snippet using com.consol.citrus.jms.actions.PurgeJmsQueuesAction.createConnection
Source:PurgeJmsQueuesActionTest.java
...47 TextMessage jmsRequest = new TextMessageImpl("<TestRequest>Hello World!</TestRequest>", requestHeaders);48 49 reset(connectionFactory, connection, session, messageConsumer);50 51 when(connectionFactory.createConnection()).thenReturn(connection);52 when(connection.createSession(anyBoolean(), anyInt())).thenReturn(session);53 when(session.createQueue("myQueue")).thenReturn(queue);54 when(session.createConsumer(queue)).thenReturn(messageConsumer);55 when(messageConsumer.receive(100L)).thenReturn(jmsRequest).thenReturn(null);56 purgeQueuesAction.execute(context);57 verify(connection).start();58 }59 60 @Test61 public void testPurgeWithQueueNamesNoMessages() throws JMSException {62 PurgeJmsQueuesAction purgeQueuesAction = new PurgeJmsQueuesAction();63 purgeQueuesAction.setConnectionFactory(connectionFactory);64 65 List<String> queueNames = new ArrayList<String>();66 queueNames.add("myQueue");67 purgeQueuesAction.setQueueNames(queueNames);68 69 reset(connectionFactory, connection, session, messageConsumer);70 71 when(connectionFactory.createConnection()).thenReturn(connection);72 when(connection.createSession(anyBoolean(), anyInt())).thenReturn(session);73 when(session.createQueue("myQueue")).thenReturn(queue);74 when(session.createConsumer(queue)).thenReturn(messageConsumer);75 when(messageConsumer.receive(100L)).thenReturn(null);76 purgeQueuesAction.execute(context);77 verify(connection).start();78 }79 80 @Test81 public void testPurgeQueueNameList() throws JMSException {82 PurgeJmsQueuesAction purgeQueuesAction = new PurgeJmsQueuesAction();83 purgeQueuesAction.setConnectionFactory(connectionFactory);84 85 List<String> queueNames = new ArrayList<String>();86 queueNames.add("myQueue");87 queueNames.add("anotherQueue");88 queueNames.add("someQueue");89 purgeQueuesAction.setQueueNames(queueNames);90 91 reset(connectionFactory, connection, session, messageConsumer);92 93 when(connectionFactory.createConnection()).thenReturn(connection);94 when(connection.createSession(anyBoolean(), anyInt())).thenReturn(session);95 when(session.createQueue("myQueue")).thenReturn(queue);96 when(session.createQueue("anotherQueue")).thenReturn(queue);97 when(session.createQueue("someQueue")).thenReturn(queue);98 when(session.createConsumer(queue)).thenReturn(messageConsumer);99 when(messageConsumer.receive(100L)).thenReturn(null);100 purgeQueuesAction.execute(context);101 verify(connection).start();102 }103 104 @Test105 public void testPurgeQueueList() throws JMSException {106 PurgeJmsQueuesAction purgeQueuesAction = new PurgeJmsQueuesAction();107 purgeQueuesAction.setConnectionFactory(connectionFactory);108 109 List<Queue> queueNames = new ArrayList<Queue>();110 queueNames.add(queue);111 queueNames.add(queue);112 queueNames.add(queue);113 purgeQueuesAction.setQueues(queueNames);114 115 reset(connectionFactory, connection, session, messageConsumer, queue);116 117 when(connectionFactory.createConnection()).thenReturn(connection);118 when(connection.createSession(anyBoolean(), anyInt())).thenReturn(session);119 when(queue.getQueueName()).thenReturn("myQueue");120 when(session.createConsumer(queue)).thenReturn(messageConsumer);121 when(messageConsumer.receive(100L)).thenReturn(null);122 purgeQueuesAction.execute(context);123 verify(connection).start();124 }125 @Test126 public void testPurgeQueueNameVariable() throws JMSException {127 PurgeJmsQueuesAction purgeQueuesAction = new PurgeJmsQueuesAction();128 purgeQueuesAction.setConnectionFactory(connectionFactory);129 context.getVariables().put("variableQueueName", "queueName");130 context.getVariables().put("secondQueueName", "secondQueue");131 List<String> queueNames = new ArrayList<String>();132 queueNames.add("${variableQueueName}");133 queueNames.add("${secondQueueName}");134 purgeQueuesAction.setQueueNames(queueNames);135 reset(connectionFactory, connection, session, messageConsumer);136 when(connectionFactory.createConnection()).thenReturn(connection);137 when(connection.createSession(anyBoolean(), anyInt())).thenReturn(session);138 when(session.createQueue("queueName")).thenReturn(queue);139 when(session.createQueue("secondQueue")).thenReturn(queue);140 when(session.createConsumer(queue)).thenReturn(messageConsumer);141 when(messageConsumer.receive(100L)).thenReturn(null);142 purgeQueuesAction.execute(context);143 verify(connection).start();144 }145 @Test146 public void testPurgeWithVariableQueueNamesConsumeMessages() throws JMSException {147 PurgeJmsQueuesAction purgeQueuesAction = new PurgeJmsQueuesAction();148 purgeQueuesAction.setConnectionFactory(connectionFactory);149 context.getVariables().put("variableQueueName", "queueName");150 List<String> queueNames = new ArrayList<String>();151 queueNames.add("${variableQueueName}");152 purgeQueuesAction.setQueueNames(queueNames);153 Map<String, Object> requestHeaders = new HashMap<String, Object>();154 TextMessage jmsRequest = new TextMessageImpl("<TestRequest>Hello World!</TestRequest>", requestHeaders);155 reset(connectionFactory, connection, session, messageConsumer);156 when(connectionFactory.createConnection()).thenReturn(connection);157 when(connection.createSession(anyBoolean(), anyInt())).thenReturn(session);158 when(session.createQueue("queueName")).thenReturn(queue);159 when(session.createConsumer(queue)).thenReturn(messageConsumer);160 when(messageConsumer.receive(100L)).thenReturn(jmsRequest).thenReturn(null);161 purgeQueuesAction.execute(context);162 verify(connection).start();163 }164 @Test165 public void testPurgeWithCustomTimeout() throws JMSException {166 PurgeJmsQueuesAction purgeQueuesAction = new PurgeJmsQueuesAction();167 purgeQueuesAction.setConnectionFactory(connectionFactory);168 169 purgeQueuesAction.setReceiveTimeout(500L);170 171 List<String> queueNames = new ArrayList<String>();172 queueNames.add("myQueue");173 purgeQueuesAction.setQueueNames(queueNames);174 175 reset(connectionFactory, connection, session, messageConsumer);176 177 when(connectionFactory.createConnection()).thenReturn(connection);178 when(connection.createSession(anyBoolean(), anyInt())).thenReturn(session);179 when(session.createQueue("myQueue")).thenReturn(queue);180 when(session.createConsumer(queue)).thenReturn(messageConsumer);181 when(messageConsumer.receive(500L)).thenReturn(null);182 purgeQueuesAction.execute(context);183 verify(connection).start();184 }185}...
Source:PurgeJmsQueuesAction.java
...65 Connection connection = null;66 Session session = null;67 68 try {69 connection = createConnection();70 session = createSession(connection);71 connection.start();72 73 for (Queue queue : queues) {74 purgeQueue(queue, session);75 }76 for (String queueName : queueNames) {77 purgeQueue(context.replaceDynamicContentInString(queueName), session);78 }79 } catch (JMSException e) {80 log.error("Error while establishing jms connection", e);81 throw new CitrusRuntimeException(e);82 } finally {83 JmsUtils.closeSession(session);84 JmsUtils.closeConnection(connection, true);85 }86 log.info("Purged JMS queues");87 }88 /**89 * Purges a queue destination identified by its name.90 * @param queueName91 * @param session92 * @throws JMSException93 */94 private void purgeQueue(String queueName, Session session) throws JMSException {95 purgeDestination(getDestination(session, queueName), session, queueName);96 }97 /**98 * Purges a queue destination. 99 * @param queue100 * @param session101 * @throws JMSException102 */103 private void purgeQueue(Queue queue, Session session) throws JMSException {104 purgeDestination(queue, session, queue.getQueueName());105 }106 /**107 * Purge destination by receiving all available messages.108 * @param destination109 * @param session110 * @param destinationName111 * @throws JMSException112 */113 private void purgeDestination(Destination destination, Session session, String destinationName) throws JMSException {114 if (log.isDebugEnabled()) {115 log.debug("Try to purge destination " + destinationName);116 }117 int messagesPurged = 0;118 MessageConsumer messageConsumer = session.createConsumer(destination);119 try {120 javax.jms.Message message;121 do {122 message = (receiveTimeout >= 0) ? messageConsumer.receive(receiveTimeout) : messageConsumer.receive();123 124 if (message != null) {125 log.debug("Removed message from destination " + destinationName);126 messagesPurged++;127 try {128 Thread.sleep(sleepTime);129 } catch (InterruptedException e) {130 log.warn("Interrupted during wait", e);131 }132 }133 } while (message != null);134 if (log.isDebugEnabled()) {135 log.debug("Purged " + messagesPurged + " messages from destination");136 }137 } finally {138 JmsUtils.closeMessageConsumer(messageConsumer);139 }140 }141 142 /**143 * Resolves destination by given name.144 * @param session145 * @param queueName146 * @return147 * @throws JMSException148 */149 private Destination getDestination(Session session, String queueName) throws JMSException {150 return new DynamicDestinationResolver().resolveDestinationName(session, queueName, false);151 }152 /**153 * Create queue connection.154 * @return155 * @throws JMSException156 */157 protected Connection createConnection() throws JMSException {158 if (connectionFactory instanceof QueueConnectionFactory) {159 return ((QueueConnectionFactory) connectionFactory).createQueueConnection();160 }161 return connectionFactory.createConnection();162 }163 164 /**165 * Create queue session.166 * @param connection167 * @return168 * @throws JMSException169 */170 protected Session createSession(Connection connection) throws JMSException {171 if (connection instanceof QueueConnection) {172 return ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);173 }174 return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);175 }...
Source:PurgeJmsQueueTestRunnerTest.java
...37 private Queue queue4 = Mockito.mock(Queue.class);38 @Test39 public void testPurgeJmsQueuesBuilderWithQueueNames() throws JMSException {40 reset(connectionFactory, connection, session, messageConsumer, queue1, queue2, queue3, queue4);41 when(connectionFactory.createConnection()).thenReturn(connection);42 when(connection.createSession(false, Session.AUTO_ACKNOWLEDGE)).thenReturn(session);43 when(session.createConsumer(any(Destination.class))).thenReturn(messageConsumer);44 when(session.createQueue("q1")).thenReturn(queue1);45 when(session.createQueue("q2")).thenReturn(queue2);46 when(session.createQueue("q3")).thenReturn(queue3);47 when(session.createQueue("q4")).thenReturn(queue4);48 when(messageConsumer.receive(200L)).thenReturn(null);49 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), applicationContext, context) {50 @Override51 public void execute() {52 purgeQueues(builder -> builder.connectionFactory(connectionFactory)53 .queueNames("q1", "q2", "q3")54 .queue("q4")55 .timeout(200L)56 .sleep(150L));57 }58 };59 TestCase test = builder.getTestCase();60 Assert.assertEquals(test.getActionCount(), 1);61 Assert.assertEquals(test.getActions().get(0).getClass(), PurgeJmsQueuesAction.class);62 Assert.assertEquals(test.getActions().get(0).getName(), "purge-queue");63 PurgeJmsQueuesAction action = (PurgeJmsQueuesAction)test.getActions().get(0);64 Assert.assertEquals(action.getReceiveTimeout(), 200L);65 Assert.assertEquals(action.getSleepTime(), 150L);66 Assert.assertEquals(action.getConnectionFactory(), connectionFactory);67 Assert.assertEquals(action.getQueueNames().size(), 4);68 Assert.assertEquals(action.getQueueNames().toString(), "[q1, q2, q3, q4]");69 Assert.assertEquals(action.getQueues().size(), 0);70 verify(connection).start();71 }72 @Test73 public void testPurgeJmsQueuesBuilderWithQueues() throws JMSException {74 reset(connectionFactory, connection, session, messageConsumer, queue1, queue2, queue3, queue4);75 when(connectionFactory.createConnection()).thenReturn(connection);76 when(connection.createSession(false, Session.AUTO_ACKNOWLEDGE)).thenReturn(session);77 when(session.createConsumer(any(Destination.class))).thenReturn(messageConsumer);78 when(queue1.getQueueName()).thenReturn("q1");79 when(queue2.getQueueName()).thenReturn("q2");80 when(queue3.getQueueName()).thenReturn("q3");81 when(messageConsumer.receive(200L)).thenReturn(null);82 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), applicationContext, context) {83 @Override84 public void execute() {85 purgeQueues(builder -> builder.connectionFactory(connectionFactory)86 .queues(queue1, queue2)87 .queue(queue3)88 .timeout(200L)89 .sleep(150L));...
createConnection
Using AI Code Generation
1public class 3 {2 public static void main(String[] args) {3 PurgeJmsQueuesAction purgeJmsQueuesAction0 = new PurgeJmsQueuesAction();4 purgeJmsQueuesAction0.setConnection((Connection) null);5 }6}
createConnection
Using AI Code Generation
1package com.consol.citrus;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;5import org.springframework.beans.factory.annotation.Autowired;6import org.springframework.beans.factory.annotation.Qualifier;7import org.testng.annotations.Test;8public class PurgeJmsQueuesActionJavaITest extends TestNGCitrusTestDesigner {9 @Qualifier("jmsConnectionFactory")10 private JmsConnectionFactory connectionFactory;11 public void purgeJmsQueuesActionJavaITest() {12 PurgeJmsQueuesAction.Builder purge = new PurgeJmsQueuesAction.Builder();13 purge.connectionFactory(connectionFactory);14 purge.queueNames("inbound.queue");15 run(purge.build());16 }17}18package com.consol.citrus;19import com.consol.citrus.annotations.CitrusTest;20import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;21import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;22import org.springframework.beans.factory.annotation.Autowired;23import org.springframework.beans.factory.annotation.Qualifier;24import org.testng.annotations.Test;25public class PurgeJmsQueuesActionJavaITest extends TestNGCitrusTestDesigner {26 @Qualifier("jmsConnectionFactory")27 private JmsConnectionFactory connectionFactory;28 public void purgeJmsQueuesActionJavaITest() {29 PurgeJmsQueuesAction.Builder purge = new PurgeJmsQueuesAction.Builder();30 purge.connectionFactory(connectionFactory);31 purge.queueNames("inbound.queue");32 run(purge.build());33 }34}35package com.consol.citrus;36import com.consol.citrus.annotations.CitrusTest;37import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;38import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;39import org.springframework.beans.factory.annotation
createConnection
Using AI Code Generation
1package com.consol.citrus.samples;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;4import org.springframework.beans.factory.annotation.Autowired;5import org.springframework.jms.core.JmsTemplate;6import org.testng.annotations.Test;7import javax.jms.ConnectionFactory;8public class PurgeJmsQueuesActionJavaIT extends TestNGCitrusTestDesigner {9 private JmsTemplate jmsTemplate;10 private ConnectionFactory connectionFactory;11 public void testPurgeJmsQueuesAction() {12 PurgeJmsQueuesAction purgeJmsQueuesAction = new PurgeJmsQueuesAction();13 purgeJmsQueuesAction.setConnectionFactory(connectionFactory);14 purgeJmsQueuesAction.setJmsTemplate(jmsTemplate);15 purgeJmsQueuesAction.setQueueNames("myQueue");16 purgeJmsQueuesAction.execute(context);17 }18}
createConnection
Using AI Code Generation
1package com.consol.citrus;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;4import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;5import com.consol.citrus.message.MessageType;6import com.consol.citrus.testng.CitrusParameters;7import org.springframework.beans.factory.annotation.Autowired;8import org.springframework.jms.core.JmsTemplate;9import org.testng.annotations.Test;10import javax.jms.ConnectionFactory;11public class PurgeJmsQueuesActionTest extends JUnit4CitrusTestRunner {12 private JmsTemplate jmsTemplate;13 private ConnectionFactory connectionFactory;14 @CitrusParameters("queueName")15 public void testPurgeJmsQueuesAction() {16 PurgeJmsQueuesAction purgeJmsQueuesAction = new PurgeJmsQueuesAction();17 purgeJmsQueuesAction.setConnectionFactory(connectionFactory);18 purgeJmsQueuesAction.setQueueNames("testQueue");19 purgeJmsQueuesAction.execute(context);20 }21}
createConnection
Using AI Code Generation
1package com.consol.citrus.samples.jms;2import com.consol.citrus.dsl.junit.JUnit4CitrusTest;3import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;4import com.consol.citrus.message.MessageType;5import com.consol.citrus.testng.CitrusParameters;6import org.testng.annotations.Test;7import javax.jms.ConnectionFactory;8public class PurgeJmsQueuesActionTest extends JUnit4CitrusTest {9 @CitrusParameters({"jmsConnectionFactory"})10 public void purgeJmsQueuesAction(ConnectionFactory connectionFactory) {11 PurgeJmsQueuesAction purgeJmsQueuesAction = new PurgeJmsQueuesAction();12 purgeJmsQueuesAction.setConnectionFactory(connectionFactory);13 purgeJmsQueuesAction.setDestinationName("citrus:queue:orders.queue");14 purgeJmsQueuesAction.setMessageType(MessageType.XML.name());15 purgeJmsQueuesAction.execute(context);16 }17}18package com.consol.citrus.samples.jms;19import com.consol.citrus.dsl.junit.JUnit4CitrusTest;20import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;21import com.consol.citrus.message.MessageType;22import com.consol.citrus.testng.CitrusParameters;23import org.testng.annotations.Test;24import javax.jms.ConnectionFactory;25public class PurgeJmsQueuesActionTest extends JUnit4CitrusTest {26 @CitrusParameters({"jmsConnectionFactory"})27 public void purgeJmsQueuesAction(ConnectionFactory connectionFactory) {28 PurgeJmsQueuesAction purgeJmsQueuesAction = new PurgeJmsQueuesAction();29 purgeJmsQueuesAction.setConnectionFactory(connectionFactory);30 purgeJmsQueuesAction.setDestinationName("citrus:queue:orders.queue");31 purgeJmsQueuesAction.setMessageType(MessageType.XML.name());32 purgeJmsQueuesAction.execute(context);33 }34}35package com.consol.citrus.samples.jms;36import com.consol.citrus.dsl
createConnection
Using AI Code Generation
1package com.consol.citrus.dsl.testng;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.design.TestDesigner;4import com.consol.citrus.dsl.runner.TestRunner;5import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;6import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;7import com.consol.citrus.jms.endpoint.JmsEndpoint;8import com.consol.citrus.message.MessageType;9import org.springframework.beans.factory.annotation.Autowired;10import org.springframework.beans.factory.annotation.Qualifier;11import org.springframework.core.io.ClassPathResource;12import org.springframework.jms.core.JmsTemplate;13import org.springframework.jms.core.MessageCreator;14import org.springframework.test.context.ContextConfiguration;15import org.testng.annotations.Test;16import javax.jms.JMSException;17import javax.jms.Message;18import javax.jms.Session;19@ContextConfiguration(classes = {JmsConfig.class})20public class JmsPurgeTest extends TestNGCitrusTestDesigner {21 @Qualifier("jmsTemplate")22 private JmsTemplate jmsTemplate;23 @Qualifier("jmsEndpoint")24 private JmsEndpoint jmsEndpoint;25 public void jmsPurgeTest() {26 description("JMS purge test");27 echo("Purge all messages from the JMS queue");28 PurgeJmsQueuesAction purgeAction = new PurgeJmsQueuesAction();29 purgeAction.setJmsEndpoint(jmsEndpoint);30 purgeAction.setJmsTemplate(jmsTemplate);31 run(purgeAction);32 echo("Send message to JMS queue");33 send(jmsEndpoint)34 .messageType(MessageType.PLAINTEXT)35 .payload("Hello World!");36 echo("Receive message from JMS queue");37 receive(jmsEndpoint)38 .messageType(MessageType.PLAINTEXT)39 .payload("Hello World!");40 }41}
createConnection
Using AI Code Generation
1package com.consol.citrus.samples.jms;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;4import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;5import org.junit.Test;6import org.springframework.beans.factory.annotation.Autowired;7import org.springframework.core.io.ClassPathResource;8import org.springframework.jms.core.JmsTemplate;9import org.springframework.jms.core.MessageCreator;10import javax.jms.*;11public class 3 extends JUnit4CitrusTestDesigner {12 private JmsTemplate jmsTemplate;13 public void 3() {14 PurgeJmsQueuesAction purgeJmsQueuesAction = new PurgeJmsQueuesAction();15 purgeJmsQueuesAction.setQueueNames("myQueue");16 purgeJmsQueuesAction.createConnection();17 purgeJmsQueuesAction.execute(context);18 purgeJmsQueuesAction.closeConnection();19 }20}21package com.consol.citrus.samples.jms;22import com.consol.citrus.annotations.CitrusTest;23import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;24import com.consol.citrus.jms.actions.PurgeJmsQueuesAction;25import org.junit.Test;26import org.springframework.beans.factory.annotation.Autowired;27import org.springframework.core.io.ClassPathResource;28import org.springframework.jms.core.JmsTemplate;29import org.springframework.jms.core.MessageCreator;30import javax.jms.*;31public class 4 extends JUnit4CitrusTestDesigner {32 private JmsTemplate jmsTemplate;33 public void 4() {34 PurgeJmsQueuesAction purgeJmsQueuesAction = new PurgeJmsQueuesAction();35 purgeJmsQueuesAction.setQueueNames("myQueue");36 purgeJmsQueuesAction.createConnection();37 purgeJmsQueuesAction.execute(context);38 purgeJmsQueuesAction.closeConnection();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!