1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.naming;
30  
31  import java.io.IOException;
32  import java.io.Reader;
33  
34  import javax.naming.Context;
35  import javax.naming.NamingException;
36  import javax.naming.directory.DirContext;
37  import javax.sql.DataSource;
38  import javax.xml.parsers.DocumentBuilder;
39  import javax.xml.parsers.DocumentBuilderFactory;
40  
41  import org.apache.log4j.LogManager;
42  import org.hsqldb.jdbc.jdbcDataSource;
43  import org.jcontainer.dna.Configuration;
44  import org.jcontainer.dna.Logger;
45  import org.jcontainer.dna.impl.Log4JLogger;
46  import org.objectledge.database.Database;
47  import org.objectledge.database.DatabaseUtils;
48  import org.objectledge.database.DefaultDatabase;
49  import org.objectledge.database.IdGenerator;
50  import org.objectledge.database.JotmTransaction;
51  import org.objectledge.database.persistence.DefaultPersistence;
52  import org.objectledge.database.persistence.Persistence;
53  import org.objectledge.filesystem.FileSystem;
54  import org.objectledge.logging.LedgeDOMConfigurator;
55  import org.objectledge.utils.LedgeTestCase;
56  import org.picocontainer.PicoContainer;
57  import org.picocontainer.defaults.DefaultPicoContainer;
58  import org.w3c.dom.Document;
59  import org.xml.sax.InputSource;
60  
61  /**
62   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
63   *
64   */
65  public class ContextFactoryTest extends LedgeTestCase
66  {
67      private FileSystem fs;
68      
69      private Logger log;
70      
71      private ContextFactory contextFactory;
72  
73      public void setUp()
74          throws Exception
75      {
76          try
77          {
78              fs = FileSystem.getStandardFileSystem("src/test/resources");
79              InputSource source = new InputSource(fs.getInputStream(
80                  "config/org.objectledge.logging.LoggingConfigurator.xml"));
81              DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
82              Document logConfig = builder.parse(source);
83              LedgeDOMConfigurator configurator = new LedgeDOMConfigurator(fs);
84              configurator.doConfigure(logConfig.getDocumentElement(), 
85                  LogManager.getLoggerRepository());
86  
87              log = new Log4JLogger(org.apache.log4j.Logger.
88                  getLogger(ContextFactory.class));
89              PicoContainer container = new DefaultPicoContainer();
90              Configuration config = getConfig("naming/mock.xml"); 
91              contextFactory = new ContextFactory(container, config, log);
92          }
93          catch (Exception e)
94          {
95              throw new RuntimeException(e);
96          }
97      }
98  
99      private Configuration getConfig(String name)
100         throws Exception
101     {
102         return getConfig(fs, name);
103     }
104 
105     public void testGetContext()
106     {
107         try
108         {
109             Context context = contextFactory.getContext("foo");
110             assertNotNull(context);
111             contextFactory.reconnect("foo");
112             context = contextFactory.getContext("foo");
113             assertNotNull(context);
114             context = contextFactory.getContext("bar");
115             assertNotNull(context);
116             contextFactory.reconnect("bar");
117             context = contextFactory.getContext("bar");
118             assertNotNull(context);
119         }
120         catch (NamingException e)
121         {
122             fail("Exception occured: " + e);
123         }
124         try
125         {
126             contextFactory.getContext("unknown");
127             fail("shoud throw the exception");
128         }
129         catch (NamingException e)
130         {
131             //ok!
132         }
133     }
134 
135     public void testGetDirContext()
136     {
137         try
138         {
139             DirContext context = contextFactory.getDirContext("foo");
140             assertNotNull(context);
141             contextFactory.reconnect("foo");
142             context = contextFactory.getDirContext("foo");
143             assertNotNull(context);
144             context = contextFactory.getDirContext("bar");
145             assertNotNull(context);
146             contextFactory.reconnect("bar");
147             context = contextFactory.getDirContext("bar");
148             assertNotNull(context);
149         }
150         catch (NamingException e)
151         {
152             fail("Exception occured: " + e);
153         }
154         try
155         {
156             contextFactory.getDirContext("unknown");
157             fail("shoud throw the exception");
158         }
159         catch (NamingException e)
160         {
161             //ok!
162         }
163     }
164 
165     public void testDbNaming()
166         throws Exception
167     {
168         DataSource ds = getDataSource();
169         DefaultPicoContainer container = new DefaultPicoContainer();
170         IdGenerator idGenerator = new IdGenerator(ds);
171         JotmTransaction transaction = new JotmTransaction(0, 120,
172             new org.objectledge.context.Context(), log, null);
173         Database database = new DefaultDatabase(ds, idGenerator, transaction);
174         Persistence persistence = new DefaultPersistence(database, log);
175         container.registerComponentInstance(Persistence.class, persistence);        
176         
177         container.registerComponentInstance("TestDS", ds);
178         container.registerComponentInstance(DataSource.class, ds);
179         Configuration config = getConfig("naming/dbNaming.xml");
180         contextFactory = new ContextFactory(container, config, log);
181         
182         contextFactory.getContext("byKey");
183         contextFactory.getContext("byClass");
184     }
185     
186     private DataSource getDataSource()
187         throws Exception
188     {
189         jdbcDataSource ds = new jdbcDataSource();
190         ds.setDatabase("jdbc:hsqldb:.");
191         ds.setUser("sa");
192         ds.setPassword("");
193         if(!DatabaseUtils.hasTable(ds, "ledge_id_table"))
194         {
195             DatabaseUtils.runScript(ds, getScript("sql/database/IdGeneratorTables.sql"));
196         }
197         if(!DatabaseUtils.hasTable(ds, "ledge_naming_context"))
198         {
199             DatabaseUtils.runScript(ds, getScript("sql/naming/db/DBNamingTables.sql"));
200         }
201         DatabaseUtils.runScript(ds, getScript("sql/naming/db/DBNamingTest.sql"));
202         return ds;
203     }
204     
205     private Reader getScript(String path)
206         throws IOException
207     {
208         return fs.getReader(path, "ISO-8859-2");
209     }
210 }