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