1   package org.jencks.samples.outbound;
2   
3   import java.sql.Types;
4   
5   import javax.jms.ConnectionFactory;
6   import javax.jms.Queue;
7   import javax.sql.DataSource;
8   
9   import org.springframework.jdbc.core.JdbcTemplate;
10  import org.springframework.jms.core.JmsTemplate;
11  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
12  import org.springframework.transaction.PlatformTransactionManager;
13  import org.springframework.transaction.TransactionDefinition;
14  import org.springframework.transaction.TransactionStatus;
15  import org.springframework.transaction.support.DefaultTransactionDefinition;
16  
17  public class MultipleOutboundMessagingTest extends AbstractDependencyInjectionSpringContextTests {
18  
19  	public final static String TEST_FIELD_VALUE="test value";
20  	public final static String INITIAL_FIELD_VALUE="initial value";
21  	public final static int FIELD_ID=1;
22  	
23  	public final static String CREATE_SCHEMA="create table TEST (" +
24  			"TEST_ID bigint generated by default as identity(start with 1)," +
25  			"TEST_VALUE varchar(255)," +
26  			"primary key (TEST_ID))";
27  	public final static String DROP_SCHEMA="drop table TEST";
28  	public final static String POPULATE_SCHEMA="insert into TEST" +
29  			" (TEST_ID,TEST_VALUE) values("+FIELD_ID+",'"+INITIAL_FIELD_VALUE+"')";
30  	
31  	public final static String UPDATE_FIELD_REQUEST="update TEST" +
32  			" set TEST_VALUE=? where TEST_ID=?";
33  	public final static String SELECT_FIELD_REQUEST="select TEST_VALUE" +
34  			" from TEST where TEST_ID=?";
35  	
36  	public final static String TEST_MESSAGE="test message";
37  	
38  	private DataSource dataSource;
39  	private ConnectionFactory connectionFactory;
40  	private Queue queue;
41  	private PlatformTransactionManager transactionManager;
42  	
43  	public PlatformTransactionManager getTransactionManager() {
44  		return transactionManager;
45  	}
46  
47  	public void setTransactionManager(PlatformTransactionManager transactionManager) {
48  		this.transactionManager = transactionManager;
49  	}
50  
51  	public DataSource getDataSource() {
52  		return dataSource;
53  	}
54  
55  	public void setDataSource(DataSource dataSource) {
56  		this.dataSource = dataSource;
57  	}
58  
59  	public Queue getQueue() {
60  		return queue;
61  	}
62  
63  	public void setQueue(Queue queue) {
64  		this.queue = queue;
65  	}
66  
67  	public ConnectionFactory getConnectionFactory() {
68  		return connectionFactory;
69  	}
70  
71  	public void setConnectionFactory(ConnectionFactory connectionFactory) {
72  		this.connectionFactory = connectionFactory;
73  	}
74  
75  	private void updateDatabase(String ddlRequest) {
76  		JdbcTemplate template=new JdbcTemplate(getDataSource());
77  		template.update(ddlRequest);
78  	}
79  	
80  	protected String[] getConfigLocations() {
81  		return new String[] { "org/jencks/samples/outbound/jencks-multiple.xml" };
82  	}
83  
84  	protected void onSetUp() throws Exception {
85  		super.onSetUp();
86  		updateDatabase(CREATE_SCHEMA);
87  		updateDatabase(POPULATE_SCHEMA);
88  	}
89  
90  	protected void onTearDown() throws Exception {
91  		super.onTearDown();
92  		updateDatabase(DROP_SCHEMA);
93  	}
94  
95  	private void checkStoredMessage(String message) {
96  		JdbcTemplate template=new JdbcTemplate(getDataSource());
97  		String storedMessage=(String)template.queryForObject(SELECT_FIELD_REQUEST,
98  				new Object[] {new Integer(FIELD_ID)},
99  				new int[] {Types.INTEGER},String.class);
100 		assertEquals(message,storedMessage);
101 	}
102 
103 	private void checkIfMessageExist(String sentMessage) {
104 		JmsTemplate template=new JmsTemplate(getConnectionFactory());
105 		template.setReceiveTimeout(10);
106 		String receivedMessage=(String)template.receiveAndConvert(queue);
107 		assertEquals(sentMessage,receivedMessage);
108 	}
109 
110 	private void checkIfMessageNotExist() {
111 		JmsTemplate template=new JmsTemplate(getConnectionFactory());
112 		template.setReceiveTimeout(10);
113 		String receivedMessage=null;
114 		receivedMessage=(String)template.receiveAndConvert(queue);
115 		assertNull(receivedMessage);
116 	}
117 
118 	public void testOutboundWithCommit() throws Exception {
119 		//Update the field and send a JMS message in a JTA transaction
120 		DefaultTransactionDefinition definition=new DefaultTransactionDefinition();
121 		definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
122 		TransactionStatus status=null;
123 		try {
124 			status=transactionManager.getTransaction(definition);
125 
126 			//JDBC
127 			JdbcTemplate jdbcTemplate=new JdbcTemplate(getDataSource());
128 			jdbcTemplate.update(UPDATE_FIELD_REQUEST,
129 					new Object[] {TEST_FIELD_VALUE,new Integer(FIELD_ID)},
130 					new int[] {Types.VARCHAR,Types.INTEGER});
131 			//JMS
132 			JmsTemplate jmsTemplate=new JmsTemplate(getConnectionFactory());
133 			jmsTemplate.convertAndSend(queue,TEST_MESSAGE);
134 
135 			transactionManager.commit(status);
136 		} catch(Exception ex) {
137 			ex.printStackTrace();
138 			transactionManager.rollback(status);
139 			fail("Undesired exception.");
140 		}
141 
142 		//Check if the message has been stored in the database
143 		checkStoredMessage(TEST_FIELD_VALUE);
144 		//Check if the message has been sent
145 		checkIfMessageExist(TEST_MESSAGE);
146 	}
147 
148 	public void testOutboundWithRollback() throws Exception {
149 		//Update the field and send a JMS message in a JTA transaction
150 		DefaultTransactionDefinition definition=new DefaultTransactionDefinition();
151 		definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
152 		TransactionStatus status=null;
153 		try {
154 			status=transactionManager.getTransaction(definition);
155 
156 			//JDBC
157 			JdbcTemplate jdbcTemplate=new JdbcTemplate(getDataSource());
158 			jdbcTemplate.update(UPDATE_FIELD_REQUEST,
159 					new Object[] {TEST_FIELD_VALUE,new Integer(FIELD_ID)},
160 					new int[] {Types.VARCHAR,Types.INTEGER});
161 			//JMS
162 			JmsTemplate jmsTemplate=new JmsTemplate(getConnectionFactory());
163 			jmsTemplate.convertAndSend(queue,TEST_MESSAGE);
164 
165 			transactionManager.rollback(status);
166 		} catch(Exception ex) {
167 			ex.printStackTrace();
168 			transactionManager.rollback(status);
169 			fail("Undesired exception.");
170 		}
171 
172 		//Check if the message has not been stored in the database
173 		checkStoredMessage(INITIAL_FIELD_VALUE);
174 		//Check if the message has not been sent
175 		checkIfMessageNotExist();
176 	}
177 
178 }