1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.test;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
25 import java.util.Properties;
26
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29
30 import junit.framework.TestCase;
31 import COM.FutureTense.CS.Factory;
32 import COM.FutureTense.Interfaces.FTValList;
33 import COM.FutureTense.Interfaces.ICS;
34 import COM.FutureTense.Util.ftMessage;
35
36 import com.fatwire.gst.foundation.test.jndi.VerySimpleInitialContextFactory;
37
38 import org.apache.commons.dbcp.BasicDataSource;
39 import org.apache.commons.dbcp.BasicDataSourceFactory;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public abstract class CSTest extends TestCase {
79 static Log log = LogFactory.getLog(CSTest.class);
80
81
82
83
84
85
86
87 @Override
88 protected void tearDown() throws Exception {
89 if (ds != null)
90 ds.close();
91 super.tearDown();
92 }
93
94 protected ICS ics;
95
96 private BasicDataSource ds;
97 private boolean login;
98
99 public CSTest() {
100 super();
101 }
102
103 public CSTest(String name) {
104 super(name);
105 }
106
107 private ClassLoader getContextClassLoader() {
108 return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
109 public ClassLoader run() {
110 return Thread.currentThread().getContextClassLoader();
111 }
112 });
113 }
114
115 String retrieveJndiName() throws IOException {
116
117 Properties p;
118
119 p = readProperties("futuretense.ini");
120 String dsn = p.getProperty("cs.dsn");
121 return p.getProperty("cs.dbconnpicture").replace("$dsn", dsn);
122
123 }
124
125 void setUpPool() throws Exception {
126
127 Properties p = readProperties("datasource.properties");
128 BasicDataSource ds = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
129 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, VerySimpleInitialContextFactory.class.getName());
130 InitialContext c = new InitialContext();
131
132 String dsn = this.retrieveJndiName();
133 c.rebind(dsn, ds);
134 this.ds = ds;
135
136 }
137
138 public Properties readProperties(final String name) throws IOException {
139 Properties properties = null;
140 InputStream in;
141
142 in = this.getContextClassLoader().getResourceAsStream(name);
143 if (in == null) {
144 File f = new File(System.getProperty("cs.installDir"), name);
145 if (f.exists()) {
146 in = new FileInputStream(f);
147 }
148 }
149
150 if (in != null) {
151 properties = new Properties();
152 try {
153 properties.load(in);
154 } finally {
155 try {
156 in.close();
157 } catch (IOException e) {
158 log.error(e, e);
159 }
160 }
161
162 } else {
163 throw new IllegalArgumentException(name + " could not be loaded.");
164
165 }
166 return properties;
167 }
168
169 @Override
170 protected void setUp() throws Exception {
171 super.setUp();
172
173 if (System.getProperty("cs.installDir") == null) {
174 throw new IllegalStateException("cs.installDir is not found as a property.");
175 }
176 if (!(System.getProperty("cs.installDir").endsWith("/") || System.getProperty("cs.installDir").endsWith("\\"))) {
177 throw new IllegalStateException("cs.installDir property does not end with a slash or backslash. ("
178 + System.getProperty("cs.installDir") + ")");
179 }
180 if (!new File(System.getProperty("cs.installDir")).exists()) {
181 throw new IllegalStateException("cs.installDir property does not exists. ("
182 + System.getProperty("cs.installDir") + ")");
183 }
184 setUpPool();
185
186
187
188
189
190
191
192
193 if (ics == null) {
194
195 ics = Factory.newCS();
196
197
198 if (login) {
199 FTValList cmds = new FTValList();
200 cmds.put(ftMessage.verb, ftMessage.login);
201 cmds.put(ftMessage.username, ftMessage.SiteReader);
202 cmds.put(ftMessage.password, ftMessage.SiteReaderPassword);
203
204
205 if (!ics.CatalogManager(cmds) || ics.GetErrno() < 0)
206 throw new RuntimeException("Can't log in, errno " + ics.GetErrno());
207 ics.RemoveVar("cshttp");
208 }
209 }
210
211
212
213
214 }
215
216 }