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 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   * NOTE July 6, 2010: The following instructions are not rigorously tested but
45   * the class works.
46   * <p/>
47   * JUnit test base class that allows AssetAPI and limited ICS usage in the
48   * absence of the ContentServer web container.
49   * <p/>
50   * Using this class, it is possible to test the DAO layer without requiring
51   * deployment to the web container.
52   * <p/>
53   * To set up, follow the following instructions:
54   * <ol>
55   * <li>mount the shared filesystem on your local machine in the same path that
56   * it is mounted on on the application server</li>
57   * <li>mount (or copy) the Content Server home folder onto your local file
58   * system. It is probably not a bad idea to mount it in the same place that it
59   * is mounted on the application server. TODO: verify</li>
60   * <li>add the path to futuretense.ini to your classpath (this is the home
61   * folder described above)</li>
62   * <li>add a system property for cs.installDir, and set it to the of the Content
63   * Server home folder</li>
64   * <li>add a property file called "datasource.properties" to your classpath that
65   * contains the following properties, set to the appropriate values for the
66   * purposes of setting up a JDBCDataSource (you can probably get these from your
67   * application server administrator: username, password, driverClassName, url,
68   * maxActive, maxIdle)</li>
69   * </ol>
70   * This effectively sets up a local copy of Content Server without a servlet
71   * context. Some operations that require the execution of a JSP element and
72   * related items will fail when using ICS, but core DB operations should
73   * succeed. The ICS object's cache is not reliable in this configuration,
74   * however, and writes to the database will not be noticed on the main server.
75   * An ICS object is available, protected, and as well
76   * <code>SessionFactory.getSession(ics)</code> operates per usual.
77   */
78  public abstract class CSTest extends TestCase {
79      static Log log = LogFactory.getLog(CSTest.class);
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see junit.framework.TestCase#tearDown()
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      //protected ICSLocator locator;
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         // System.setProperty("cs.installDir",
186         // "C:\\DATA\\CS\\zamak\\ContentServer\\");
187         // NEEDS slash at the end
188 
189         //long t = System.nanoTime();
190 
191         // IPS ips = IPSRegistry.getInstance().get();
192         // ics = (ips != null) ? ips.GetICSObject() : null;
193         if (ics == null) {
194             //long t0 = System.nanoTime();
195             ics = Factory.newCS();
196             //DebugHelper.printTime(log, "newICS", t0);
197 
198             if (login) {
199                 FTValList cmds = new FTValList();
200                 cmds.put(ftMessage.verb, ftMessage.login);
201                 cmds.put(ftMessage.username, ftMessage.SiteReader);// "DefaultReader"
202                 cmds.put(ftMessage.password, ftMessage.SiteReaderPassword);// "SomeReader"
203                 // cmds.put(ftMessage.username, "firstsite");
204                 // cmds.put(ftMessage.password, "firstsite");
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         //DebugHelper.printTime(log, "booting ICS", t);
212         //locator = new ICSLocatorSupport(ics);
213 
214     }
215 
216 }