1   package org.jencks.samples.outbound;
2   
3   import java.sql.Types;
4   
5   import javax.sql.DataSource;
6   
7   import org.springframework.jdbc.core.JdbcTemplate;
8   import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
9   import org.springframework.transaction.PlatformTransactionManager;
10  import org.springframework.transaction.TransactionDefinition;
11  import org.springframework.transaction.TransactionStatus;
12  import org.springframework.transaction.support.DefaultTransactionDefinition;
13  
14  public abstract class AbstractJdbcOutboundMessagingTest extends AbstractDependencyInjectionSpringContextTests {
15  
16  	public final static String TEST_FIELD_VALUE="test value";
17  	public final static String INITIAL_FIELD_VALUE="initial value";
18  	public final static int FIELD_ID=1;
19  	
20  	public final static String CREATE_SCHEMA="create table TEST (" +
21  			"TEST_ID bigint generated by default as identity(start with 1)," +
22  			"TEST_VALUE varchar(255)," +
23  			"primary key (TEST_ID))";
24  	public final static String DROP_SCHEMA="drop table TEST";
25  	public final static String POPULATE_SCHEMA="insert into TEST" +
26  			" (TEST_ID,TEST_VALUE) values("+FIELD_ID+",'"+INITIAL_FIELD_VALUE+"')";
27  	
28  	public final static String UPDATE_FIELD_REQUEST="update TEST" +
29  			" set TEST_VALUE=? where TEST_ID=?";
30  	public final static String SELECT_FIELD_REQUEST="select TEST_VALUE" +
31  			" from TEST where TEST_ID=?";
32  	
33  	private DataSource dataSource;
34  	private PlatformTransactionManager transactionManager;
35  	
36  	public PlatformTransactionManager getTransactionManager() {
37  		return transactionManager;
38  	}
39  
40  	public void setTransactionManager(PlatformTransactionManager transactionManager) {
41  		this.transactionManager = transactionManager;
42  	}
43  
44  	public DataSource getDataSource() {
45  		return dataSource;
46  	}
47  
48  	public void setDataSource(DataSource dataSource) {
49  		this.dataSource = dataSource;
50  	}
51  
52  	private void updateDatabase(String ddlRequest) {
53  		JdbcTemplate template=new JdbcTemplate(getDataSource());
54  		template.update(ddlRequest);
55  	}
56  	
57  	protected void onSetUp() throws Exception {
58  		super.onSetUp();
59  		updateDatabase(CREATE_SCHEMA);
60  		updateDatabase(POPULATE_SCHEMA);
61  	}
62  
63  	protected void onTearDown() throws Exception {
64  		super.onTearDown();
65  		updateDatabase(DROP_SCHEMA);
66  	}
67  
68  	private void checkStoredMessage(String message) {
69  		JdbcTemplate template=new JdbcTemplate(getDataSource());
70  		String storedMessage=(String)template.queryForObject(SELECT_FIELD_REQUEST,
71  				new Object[] {new Integer(FIELD_ID)},
72  				new int[] {Types.INTEGER},String.class);
73  		assertEquals(message,storedMessage);
74  	}
75  
76  	public void testOutboundWithCommit() throws Exception {
77  		//Update the field in a JTA transaction
78  		DefaultTransactionDefinition definition=new DefaultTransactionDefinition();
79  		definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
80  		TransactionStatus status=null;
81  		try {
82  			status=transactionManager.getTransaction(definition);
83  			JdbcTemplate template=new JdbcTemplate(getDataSource());
84  			template.update(UPDATE_FIELD_REQUEST,
85  					new Object[] {TEST_FIELD_VALUE,new Integer(FIELD_ID)},
86  					new int[] {Types.VARCHAR,Types.INTEGER});
87  			transactionManager.commit(status);
88  		} catch(Exception ex) {
89  			ex.printStackTrace();
90  			transactionManager.rollback(status);
91  			fail("Undesired exception.");
92  		}
93  
94  		//Check if the message has been stored in the database
95  		checkStoredMessage(TEST_FIELD_VALUE);
96  	}
97  
98  	public void testOutboundWithRollback() throws Exception {
99  		//Update the field in a JTA transaction
100 		DefaultTransactionDefinition definition=new DefaultTransactionDefinition();
101 		definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
102 		TransactionStatus status=null;
103 		try {
104 			status=transactionManager.getTransaction(definition);
105 			JdbcTemplate template=new JdbcTemplate(getDataSource());
106 			template.update(UPDATE_FIELD_REQUEST,
107 					new Object[] {TEST_FIELD_VALUE,new Integer(FIELD_ID)},
108 					new int[] {Types.VARCHAR,Types.INTEGER});
109 			transactionManager.rollback(status);
110 		} catch(Exception ex) {
111 			ex.printStackTrace();
112 			transactionManager.rollback(status);
113 			fail("Undesired exception.");
114 		}
115 
116 		//Check if the message has not been stored in the database
117 		checkStoredMessage(INITIAL_FIELD_VALUE);
118 	}
119 
120 }