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