1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 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
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
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 }