Revision 47:4e2ac80306c8

View differences:

test/java/net/penril/agro/UPassIT.java
1
/*
2
 * Copyright (c) 2011 Penril Datability (M) Sdn Bhd All rights reserved.
3
 *
4
 * This software is copyrighted. Under the copyright laws, this software
5
 * may not be copied, in whole or in part, without prior written consent
6
 * of Penril Datability (M) Sdn Bhd or its assignees. This software is
7
 * provided under the terms of a license between Penril Datability (M)
8
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
9
 * license.
10
 */
11
package net.penril.agro;
12

  
13
import java.io.InputStream;
14
import java.sql.Connection;
15
import java.sql.ResultSet;
16
import java.sql.Statement;
17

  
18
import junit.framework.TestCase;
19
import my.com.upass.db.DBOperations;
20

  
21
import org.apache.commons.io.IOUtils;
22
import org.apache.log4j.Logger;
23
//import my.com.upass.SelfTestV2;
24

  
25
/*
26
 * <pre>
27
 * PROGRAMMER: Hadi Ahmadi	
28
 * CHANGE-NO:
29
 * TASK-NO:
30
 * DATE CREATED: Mar 6, 2012
31
 * TAG AS:
32
 * REASON(S):
33
 * MODIFICATION:
34
 * </pre>
35
 */
36

  
37
/**
38
 * An integration test (IT) case for Agrobank UPass Web Services.
39
 */
40

  
41
public class UPassIT extends TestCase {
42

  
43
	private final static Logger logger = Logger.getLogger(UPassIT.class);
44

  
45
	// private static final PrintStream DEFAULT_OUTPUT = System.out;
46
	// private static final InputStream DEFAULT_INPUT = System.in;
47
	//
48
	// private final TestableOutputStream testableOutput;
49
	// private final PipedInputStream pipedInput;
50
	// private final PrintStream inputMockup;
51
	//
52
	// //
53
	//
54
	// public UPassIT() throws IOException {
55
	// testableOutput = new TestableOutputStream(DEFAULT_OUTPUT);
56
	// PipedOutputStream pipedOutput = new PipedOutputStream();
57
	// pipedInput = new PipedInputStream(pipedOutput);
58
	// inputMockup = new InputMockUp(pipedOutput);
59
	// }
60

  
61
	@Override
62
	protected void setUp() throws Exception {
63
		super.setUp();
64
		// System.setOut(testableOutput);
65
		// System.setIn(pipedInput);
66
	}
67

  
68
	@Override
69
	protected void tearDown() throws Exception {
70
		// System.setOut(DEFAULT_OUTPUT);
71
		// System.setIn(DEFAULT_INPUT);
72
		super.tearDown();
73
	}
74

  
75
	public void testInitDb() throws Exception {
76
		Connection conn = new DBOperations().getConnection();
77
		boolean oldAutoCommit = true;
78
		try {
79
			oldAutoCommit = conn.getAutoCommit();
80
			conn.setAutoCommit(false);
81
			Statement stmt = conn.createStatement();
82
			InputStream iStream = getClass().getClassLoader().getResourceAsStream(//
83
					//"db-scripts/agro-ci-db.upass.script.w-data.sql");
84
					"db-scripts/m2u-upass-ci-oracle.sql");
85
			
86
			String[] sqlStmts = IOUtils.toString(iStream).split(";\\s*\n");
87
			//String[] sqlStmts = IOUtils.toString(iStream).split("\\s*GO\\s*\n");
88
			for (String sqlStmt : sqlStmts) {
89
				if(sqlStmt.contains("DROP")){
90
					try {
91
						stmt.execute(sqlStmt);
92
					} catch (Exception e) {
93
						continue;
94
					}
95
				}else{
96
					stmt.execute(sqlStmt);
97
				}
98
			}
99
			ResultSet rs = stmt.executeQuery("select count(U_USER_ID) from TB_AM_USER");
100
			int userCount = -1;
101
			if (rs.next())
102
				userCount = rs.getInt(1);
103
			assertEquals(584, userCount);
104

  
105
			conn.commit();
106

  
107
		} catch (Exception e) {
108
			e.printStackTrace();
109
			if (conn != null)
110
				conn.rollback();
111
			fail(e.toString());
112

  
113
		} finally {
114
			if (conn != null) {
115
				conn.setAutoCommit(oldAutoCommit);
116
				conn.close();
117
			}
118
		}
119
	}
120

  
121
	// public void testSelfTestV2() throws IOException {
122
	// Thread testThread = new Thread("SelfTestV2 Dedicated Thread") {
123
	// @Override
124
	// public void run() {
125
	// SelfTestV2.main(null);
126
	// }
127
	// };
128
	// testThread.setDaemon(true);
129
	// testThread.start();
130
	//
131
	// String outStr = waitForPrompt("%");
132
	// String actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
133
	// String expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [01] : ";
134
	// assertEquals(expectedPrompt, actualPrompt);
135
	//
136
	// testableOutput.readableStream.reset();
137
	// inputMockup.println("84");
138
	// outStr = waitForPrompt(":");
139
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
140
	// expectedPrompt = "Enter User ID : ";
141
	// assertEquals(expectedPrompt, actualPrompt);
142
	//
143
	// testableOutput.readableStream.reset();
144
	// inputMockup.println("test-user-0");
145
	// outStr = waitForPrompt(":");
146
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
147
	// assertTrue(outStr.contains("RC=0 RT=ERR_SUCCESS SerailNo=1497524840"));
148
	//
149
	// outStr = waitForPrompt("%");
150
	// actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
151
	// expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [84] : ";
152
	// assertEquals(expectedPrompt, actualPrompt);
153
	//
154
	// testableOutput.readableStream.reset();
155
	// inputMockup.println("74");
156
	// outStr = waitForPrompt(":");
157
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
158
	// expectedPrompt = "Enter User ID [test-user-0] : ";
159
	// assertEquals(expectedPrompt, actualPrompt);
160
	//
161
	// testableOutput.readableStream.reset();
162
	// inputMockup.println();
163
	// outStr = waitForPrompt(":");
164
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
165
	// assertTrue(outStr.contains("RC=0 RT=ERR_SUCCESS"));
166
	//
167
	// outStr = waitForPrompt("%");
168
	// actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
169
	// expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [74] : ";
170
	// assertEquals(expectedPrompt, actualPrompt);
171
	//
172
	// testableOutput.readableStream.reset();
173
	// inputMockup.println("17");
174
	// outStr = waitForPrompt(":");
175
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
176
	// expectedPrompt = "Enter User ID [test-user-0] : ";
177
	// assertEquals(expectedPrompt, actualPrompt);
178
	//
179
	// testableOutput.readableStream.reset();
180
	// inputMockup.println();
181
	// outStr = waitForPrompt(":");
182
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
183
	// assertTrue(outStr.contains("test-user-0 RC=0 RT=ERR_SUCCESS Type=2 Desc=[Test User]" //
184
	// + "\n State=0 UseCount=0" //
185
	// + "\n DateCreated"));
186
	//
187
	// outStr = waitForPrompt("%");
188
	// actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
189
	// expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [17] : ";
190
	// assertEquals(expectedPrompt, actualPrompt);
191
	//
192
	// testableOutput.readableStream.reset();
193
	// inputMockup.println("64");
194
	// outStr = waitForPrompt(":");
195
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
196
	// expectedPrompt = "Enter User ID [test-user-0] : ";
197
	// assertEquals(expectedPrompt, actualPrompt);
198
	//
199
	// testableOutput.readableStream.reset();
200
	// inputMockup.println();
201
	// outStr = waitForPrompt(":");
202
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
203
	// assertTrue(outStr.contains("test-user-0 RC=0 RT=ERR_SUCCESS State=0 ExpiryStatus=0 "
204
	// + "UseCount=0 ErrorCount=0"));
205
	// }
206
	//
207
	// private String waitForPrompt(String promptIndicator) throws IOException {
208
	// String outStr;
209
	// do {
210
	// try {
211
	// Thread.sleep(1000);
212
	//
213
	// } catch (InterruptedException e) {
214
	// e.printStackTrace();
215
	// }
216
	// testableOutput.readableStream.flush();
217
	// outStr = testableOutput.readableStream.toString();
218
	//
219
	// } while (!outStr.contains(promptIndicator));
220
	//
221
	// return outStr;
222
	// }
223
	//
224
	// private final class TestableOutputStream extends PrintStream {
225
	//
226
	// private final ByteArrayOutputStream readableStream;
227
	//
228
	// private TestableOutputStream(OutputStream out) {
229
	// this(out, new ByteArrayOutputStream(1000));
230
	// }
231
	//
232
	// private TestableOutputStream(OutputStream out, ByteArrayOutputStream readableStream) {
233
	// super(out);
234
	// this.readableStream = readableStream;
235
	// }
236
	//
237
	// @Override
238
	// public void write(byte[] b, int off, int len) {
239
	// readableStream.write(b, off, len);
240
	// super.write(b, off, len);
241
	// }
242
	// }
243
	//
244
	// private final class InputMockUp extends PrintStream {
245
	//
246
	// private InputMockUp(OutputStream out) {
247
	// super(out);
248
	// }
249
	//
250
	// @Override
251
	// public void write(byte[] b, int off, int len) {
252
	// DEFAULT_OUTPUT.write(b, off, len);
253
	// super.write(b, off, len);
254
	// }
255
	// }
256
}
test/java/net/penril/upass/UPassIT.java
1
/*
2
 * Copyright (c) 2011 Penril Datability (M) Sdn Bhd All rights reserved.
3
 *
4
 * This software is copyrighted. Under the copyright laws, this software
5
 * may not be copied, in whole or in part, without prior written consent
6
 * of Penril Datability (M) Sdn Bhd or its assignees. This software is
7
 * provided under the terms of a license between Penril Datability (M)
8
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
9
 * license.
10
 */
11
package net.penril.upass;
12

  
13
import java.io.InputStream;
14
import java.sql.Connection;
15
import java.sql.ResultSet;
16
import java.sql.Statement;
17

  
18
import junit.framework.TestCase;
19
import my.com.upass.db.DBOperations;
20

  
21
import org.apache.commons.io.IOUtils;
22
import org.apache.log4j.Logger;
23
//import my.com.upass.SelfTestV2;
24

  
25
/*
26
 * <pre>
27
 * PROGRAMMER: Hadi Ahmadi	
28
 * CHANGE-NO:
29
 * TASK-NO:
30
 * DATE CREATED: Mar 6, 2012
31
 * TAG AS:
32
 * REASON(S):
33
 * MODIFICATION:
34
 * </pre>
35
 */
36

  
37
/**
38
 * An integration test (IT) case for Agrobank UPass Web Services.
39
 */
40

  
41
public class UPassIT extends TestCase {
42

  
43
	private final static Logger logger = Logger.getLogger(UPassIT.class);
44

  
45
	// private static final PrintStream DEFAULT_OUTPUT = System.out;
46
	// private static final InputStream DEFAULT_INPUT = System.in;
47
	//
48
	// private final TestableOutputStream testableOutput;
49
	// private final PipedInputStream pipedInput;
50
	// private final PrintStream inputMockup;
51
	//
52
	// //
53
	//
54
	// public UPassIT() throws IOException {
55
	// testableOutput = new TestableOutputStream(DEFAULT_OUTPUT);
56
	// PipedOutputStream pipedOutput = new PipedOutputStream();
57
	// pipedInput = new PipedInputStream(pipedOutput);
58
	// inputMockup = new InputMockUp(pipedOutput);
59
	// }
60

  
61
	@Override
62
	protected void setUp() throws Exception {
63
		super.setUp();
64
		// System.setOut(testableOutput);
65
		// System.setIn(pipedInput);
66
	}
67

  
68
	@Override
69
	protected void tearDown() throws Exception {
70
		// System.setOut(DEFAULT_OUTPUT);
71
		// System.setIn(DEFAULT_INPUT);
72
		super.tearDown();
73
	}
74

  
75
	public void testInitDb() throws Exception {
76
		Connection conn = new DBOperations().getConnection();
77
		boolean oldAutoCommit = true;
78
		try {
79
			oldAutoCommit = conn.getAutoCommit();
80
			conn.setAutoCommit(false);
81
			Statement stmt = conn.createStatement();
82
			InputStream iStream = getClass().getClassLoader().getResourceAsStream(//
83
					//"db-scripts/agro-ci-db.upass.script.w-data.sql");
84
					"db-scripts/m2u-upass-ci-oracle.sql");
85
			
86
			String[] sqlStmts = IOUtils.toString(iStream).split(";\\s*\n");
87
			//String[] sqlStmts = IOUtils.toString(iStream).split("\\s*GO\\s*\n");
88
			for (String sqlStmt : sqlStmts) {
89
				if(sqlStmt.contains("DROP")){
90
					try {
91
						stmt.execute(sqlStmt);
92
					} catch (Exception e) {
93
						continue;
94
					}
95
				}else{
96
					stmt.execute(sqlStmt);
97
				}
98
			}
99
			ResultSet rs = stmt.executeQuery("select count(U_USER_ID) from TB_AM_USER");
100
			int userCount = -1;
101
			if (rs.next())
102
				userCount = rs.getInt(1);
103
			assertEquals(584, userCount);
104

  
105
			conn.commit();
106

  
107
		} catch (Exception e) {
108
			e.printStackTrace();
109
			if (conn != null)
110
				conn.rollback();
111
			fail(e.toString());
112

  
113
		} finally {
114
			if (conn != null) {
115
				conn.setAutoCommit(oldAutoCommit);
116
				conn.close();
117
			}
118
		}
119
	}
120

  
121
	// public void testSelfTestV2() throws IOException {
122
	// Thread testThread = new Thread("SelfTestV2 Dedicated Thread") {
123
	// @Override
124
	// public void run() {
125
	// SelfTestV2.main(null);
126
	// }
127
	// };
128
	// testThread.setDaemon(true);
129
	// testThread.start();
130
	//
131
	// String outStr = waitForPrompt("%");
132
	// String actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
133
	// String expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [01] : ";
134
	// assertEquals(expectedPrompt, actualPrompt);
135
	//
136
	// testableOutput.readableStream.reset();
137
	// inputMockup.println("84");
138
	// outStr = waitForPrompt(":");
139
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
140
	// expectedPrompt = "Enter User ID : ";
141
	// assertEquals(expectedPrompt, actualPrompt);
142
	//
143
	// testableOutput.readableStream.reset();
144
	// inputMockup.println("test-user-0");
145
	// outStr = waitForPrompt(":");
146
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
147
	// assertTrue(outStr.contains("RC=0 RT=ERR_SUCCESS SerailNo=1497524840"));
148
	//
149
	// outStr = waitForPrompt("%");
150
	// actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
151
	// expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [84] : ";
152
	// assertEquals(expectedPrompt, actualPrompt);
153
	//
154
	// testableOutput.readableStream.reset();
155
	// inputMockup.println("74");
156
	// outStr = waitForPrompt(":");
157
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
158
	// expectedPrompt = "Enter User ID [test-user-0] : ";
159
	// assertEquals(expectedPrompt, actualPrompt);
160
	//
161
	// testableOutput.readableStream.reset();
162
	// inputMockup.println();
163
	// outStr = waitForPrompt(":");
164
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
165
	// assertTrue(outStr.contains("RC=0 RT=ERR_SUCCESS"));
166
	//
167
	// outStr = waitForPrompt("%");
168
	// actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
169
	// expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [74] : ";
170
	// assertEquals(expectedPrompt, actualPrompt);
171
	//
172
	// testableOutput.readableStream.reset();
173
	// inputMockup.println("17");
174
	// outStr = waitForPrompt(":");
175
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
176
	// expectedPrompt = "Enter User ID [test-user-0] : ";
177
	// assertEquals(expectedPrompt, actualPrompt);
178
	//
179
	// testableOutput.readableStream.reset();
180
	// inputMockup.println();
181
	// outStr = waitForPrompt(":");
182
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
183
	// assertTrue(outStr.contains("test-user-0 RC=0 RT=ERR_SUCCESS Type=2 Desc=[Test User]" //
184
	// + "\n State=0 UseCount=0" //
185
	// + "\n DateCreated"));
186
	//
187
	// outStr = waitForPrompt("%");
188
	// actualPrompt = outStr.substring(outStr.lastIndexOf('%'));
189
	// expectedPrompt = "% Enter Option? | \"99\"-exit | \"e\"-ChkErr | \"s\"-SetSessID | \"m\"-Menu [17] : ";
190
	// assertEquals(expectedPrompt, actualPrompt);
191
	//
192
	// testableOutput.readableStream.reset();
193
	// inputMockup.println("64");
194
	// outStr = waitForPrompt(":");
195
	// actualPrompt = outStr.substring(outStr.lastIndexOf('\n') + 1);
196
	// expectedPrompt = "Enter User ID [test-user-0] : ";
197
	// assertEquals(expectedPrompt, actualPrompt);
198
	//
199
	// testableOutput.readableStream.reset();
200
	// inputMockup.println();
201
	// outStr = waitForPrompt(":");
202
	// // assertTrue(outStr.contains("RC=6 RT=ERR_USERALIAS_NOT_FOUND"));
203
	// assertTrue(outStr.contains("test-user-0 RC=0 RT=ERR_SUCCESS State=0 ExpiryStatus=0 "
204
	// + "UseCount=0 ErrorCount=0"));
205
	// }
206
	//
207
	// private String waitForPrompt(String promptIndicator) throws IOException {
208
	// String outStr;
209
	// do {
210
	// try {
211
	// Thread.sleep(1000);
212
	//
213
	// } catch (InterruptedException e) {
214
	// e.printStackTrace();
215
	// }
216
	// testableOutput.readableStream.flush();
217
	// outStr = testableOutput.readableStream.toString();
218
	//
219
	// } while (!outStr.contains(promptIndicator));
220
	//
221
	// return outStr;
222
	// }
223
	//
224
	// private final class TestableOutputStream extends PrintStream {
225
	//
226
	// private final ByteArrayOutputStream readableStream;
227
	//
228
	// private TestableOutputStream(OutputStream out) {
229
	// this(out, new ByteArrayOutputStream(1000));
230
	// }
231
	//
232
	// private TestableOutputStream(OutputStream out, ByteArrayOutputStream readableStream) {
233
	// super(out);
234
	// this.readableStream = readableStream;
235
	// }
236
	//
237
	// @Override
238
	// public void write(byte[] b, int off, int len) {
239
	// readableStream.write(b, off, len);
240
	// super.write(b, off, len);
241
	// }
242
	// }
243
	//
244
	// private final class InputMockUp extends PrintStream {
245
	//
246
	// private InputMockUp(OutputStream out) {
247
	// super(out);
248
	// }
249
	//
250
	// @Override
251
	// public void write(byte[] b, int off, int len) {
252
	// DEFAULT_OUTPUT.write(b, off, len);
253
	// super.write(b, off, len);
254
	// }
255
	// }
256
}

Also available in: Unified diff