View Javadoc
1   /*
2    * Copyright 2009 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * NOTE July 6, 2010: The following instructions are not rigorously tested but
42   * the class works.
43   * <p>
44   * JUnit test base class that allows AssetAPI and limited ICS usage in the
45   * absence of the ContentServer web container.
46   * <p>
47   * Using this class, it is possible to test the DAO layer without requiring
48   * deployment to the web container.
49   * <p>
50   * To set up, follow the following instructions:
51   * <ol>
52   * <li>mount the shared filesystem on your local machine in the same path that
53   * it is mounted on on the application server</li>
54   * <li>mount (or copy) the Content Server home folder onto your local file
55   * system. It is probably not a bad idea to mount it in the same place that it
56   * is mounted on the application server. TODO: verify</li>
57   * <li>add the path to futuretense.ini to your classpath (this is the home
58   * folder described above)</li>
59   * <li>add a system property for cs.installDir, and set it to the of the Content
60   * Server home folder</li>
61   * <li>add a property file called "datasource.properties" to your classpath that
62   * contains the following properties, set to the appropriate values for the
63   * purposes of setting up a JDBCDataSource (you can probably get these from your
64   * application server administrator: username, password, driverClassName, url,
65   * maxActive, maxIdle)</li>
66   * </ol>
67   * This effectively sets up a local copy of Content Server without a servlet
68   * context. Some operations that require the execution of a JSP element and
69   * related items will fail when using ICS, but core DB operations should
70   * succeed. The ICS object's cache is not reliable in this configuration,
71   * however, and writes to the database will not be noticed on the main server.
72   * An ICS object is available, protected, and as well
73   * <code>SessionFactory.getSession(ics)</code> operates per usual.
74   */
75  public abstract class CSTest extends TestCase {
76      static Logger log = LoggerFactory.getLogger("tools.gsf.test.CSTest");
77  
78      /*
79       * (non-Javadoc)
80       *
81       * @see junit.framework.TestCase#tearDown()
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      //protected ICSLocator locator;
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         // System.setProperty("cs.installDir",
184         // "C:\\DATA\\CS\\zamak\\ContentServer\\");
185         // NEEDS slash at the end
186 
187         //long t = System.nanoTime();
188 
189         // IPS ips = IPSRegistry.getInstance().get();
190         // ics = (ips != null) ? ips.GetICSObject() : null;
191         if (ics == null) {
192             //long t0 = System.nanoTime();
193             ics = Factory.newCS();
194             //DebugHelper.printTime(log, "newICS", t0);
195 
196             if (login) {
197                 FTValList cmds = new FTValList();
198                 cmds.put(ftMessage.verb, ftMessage.login);
199                 cmds.put(ftMessage.username, ftMessage.SiteReader);// "DefaultReader"
200                 cmds.put(ftMessage.password, ftMessage.SiteReaderPassword);// "SomeReader"
201                 // cmds.put(ftMessage.username, "firstsite");
202                 // cmds.put(ftMessage.password, "firstsite");
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         //DebugHelper.printTime(log, "booting ICS", t);
211         //locator = new ICSLocatorSupport(ics);
212 
213     }
214 
215 }