|
1 /* |
|
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package com.nokia.helium.metadata.tests; |
|
18 |
|
19 |
|
20 import java.io.File; |
|
21 import java.io.IOException; |
|
22 |
|
23 import javax.persistence.EntityManager; |
|
24 import javax.persistence.EntityManagerFactory; |
|
25 |
|
26 import org.apache.commons.io.FileUtils; |
|
27 import org.apache.log4j.Logger; |
|
28 import org.junit.After; |
|
29 import org.junit.Assert; |
|
30 import org.junit.Before; |
|
31 import org.junit.Test; |
|
32 |
|
33 import com.nokia.helium.metadata.FactoryManager; |
|
34 import com.nokia.helium.metadata.MetadataException; |
|
35 import com.nokia.helium.metadata.fmpp.ORMQueryModeModel; |
|
36 import com.nokia.helium.metadata.model.metadata.LogFile; |
|
37 |
|
38 import fmpp.ProgressListener; |
|
39 import freemarker.ext.beans.BeanModel; |
|
40 import freemarker.template.SimpleScalar; |
|
41 import freemarker.template.TemplateCollectionModel; |
|
42 import freemarker.template.TemplateHashModel; |
|
43 import freemarker.template.TemplateModel; |
|
44 import freemarker.template.TemplateModelIterator; |
|
45 import freemarker.template.TemplateSequenceModel; |
|
46 |
|
47 /** |
|
48 * Testing the ORMFMPPLoader loader. |
|
49 * |
|
50 */ |
|
51 public class TestORMFMPPLoader { |
|
52 private File database; |
|
53 private static Logger log = Logger.getLogger(TestORMFMPPLoader.class); |
|
54 |
|
55 /** |
|
56 * Populates the LogFile table with basic data. |
|
57 * @throws MetadataException |
|
58 * @throws IOException |
|
59 */ |
|
60 @Before |
|
61 public void populateDatabase() throws MetadataException, IOException { |
|
62 File tempdir = new File(System.getProperty("test.temp.dir")); |
|
63 tempdir.mkdirs(); |
|
64 database = new File(tempdir, "test_db"); |
|
65 if (database.exists()) { |
|
66 FileUtils.forceDelete(database); |
|
67 } |
|
68 EntityManagerFactory factory = FactoryManager.getFactoryManager().getEntityManagerFactory(database); |
|
69 EntityManager em = factory.createEntityManager(); |
|
70 try { |
|
71 em.getTransaction().begin(); |
|
72 for (int i = 0 ; i < 2000 ; i++) { |
|
73 LogFile lf = new LogFile(); |
|
74 lf.setPath("log" + String.format("%04d", i)); |
|
75 em.persist(lf); |
|
76 } |
|
77 } finally { |
|
78 if (em.getTransaction().isActive()) { |
|
79 em.getTransaction().commit(); |
|
80 } |
|
81 em.close(); |
|
82 factory.close(); |
|
83 } |
|
84 } |
|
85 |
|
86 /** |
|
87 * Delete the database after test completion. |
|
88 * @throws IOException |
|
89 */ |
|
90 @After |
|
91 public void cleanupDatabase() throws IOException { |
|
92 FileUtils.forceDelete(database); |
|
93 } |
|
94 |
|
95 /** |
|
96 * Run a JPA query. |
|
97 * @throws Exception |
|
98 */ |
|
99 @Test |
|
100 public void testJpaSingleQuery() throws Exception { |
|
101 ORMQueryModeModel modeModel = new ORMQueryModeModel(database); |
|
102 try { |
|
103 TemplateHashModel model = modeModel.get("jpasingle"); |
|
104 TemplateModel data = model.get("select l from LogFile l order by l.path"); |
|
105 Assert.assertTrue(data instanceof TemplateSequenceModel); |
|
106 TemplateSequenceModel seq = (TemplateSequenceModel)data; |
|
107 Assert.assertTrue(seq.size() == 2000); |
|
108 |
|
109 // Let's make sure we get null if out of bounds |
|
110 Assert.assertNotNull(seq.get(0)); |
|
111 Assert.assertNotNull(seq.get(750)); |
|
112 Assert.assertNotNull(seq.get(749)); |
|
113 Assert.assertNotNull(seq.get(1999)); |
|
114 Assert.assertNotNull(seq.get(0)); |
|
115 Assert.assertNull(seq.get(2000)); |
|
116 Assert.assertNull(seq.get(2001)); |
|
117 Assert.assertNull(seq.get(2000)); |
|
118 |
|
119 // Check index 0 |
|
120 LogFile lf = (LogFile)((BeanModel)seq.get(0)).getWrappedObject(); |
|
121 log.info("seq.get(0): " + lf.getPath()); |
|
122 Assert.assertTrue("log0000".equals(lf.getPath())); |
|
123 |
|
124 // Check index 999 |
|
125 lf = (LogFile)((BeanModel)seq.get(999)).getWrappedObject(); |
|
126 log.info("seq.get(999): " + lf.getPath()); |
|
127 Assert.assertTrue("log0999".equals(lf.getPath())); |
|
128 |
|
129 // Check index 1999 |
|
130 lf = (LogFile)((BeanModel)seq.get(1999)).getWrappedObject(); |
|
131 log.info("seq.get(1999): " + lf.getPath()); |
|
132 Assert.assertTrue("log1999".equals(lf.getPath())); |
|
133 } finally { |
|
134 modeModel.notifyProgressEvent(null, ProgressListener.EVENT_END_PROCESSING_SESSION, null, 0, null, null); |
|
135 } |
|
136 } |
|
137 |
|
138 /** |
|
139 * Run a native query. |
|
140 * @throws Exception |
|
141 */ |
|
142 @Test |
|
143 public void testNativeStringQuery() throws Exception { |
|
144 ORMQueryModeModel modeModel = new ORMQueryModeModel(database); |
|
145 try { |
|
146 TemplateHashModel model = modeModel.get("native:java.lang.String"); |
|
147 TemplateModel data = model.get("select l.path from LogFile l order by l.path"); |
|
148 Assert.assertTrue(data instanceof TemplateCollectionModel); |
|
149 TemplateCollectionModel collection = (TemplateCollectionModel)data; |
|
150 TemplateModelIterator iterator = collection.iterator(); |
|
151 |
|
152 int i = 0; |
|
153 while (iterator.hasNext()) { |
|
154 TemplateModel next = iterator.next(); |
|
155 SimpleScalar scalar = (SimpleScalar)next; |
|
156 Assert.assertTrue(scalar.getAsString().equals("log" + String.format("%04d", i++))); |
|
157 } |
|
158 Assert.assertFalse(iterator.hasNext()); |
|
159 Assert.assertNull(iterator.next()); |
|
160 } finally { |
|
161 modeModel.notifyProgressEvent(null, ProgressListener.EVENT_END_PROCESSING_SESSION, null, 0, null, null); |
|
162 } |
|
163 } |
|
164 } |