Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / test / java / com / hardcode / gdbms / engine / data / Tests.java @ 10627

History | View | Annotate | Download (9.93 KB)

1
package com.hardcode.gdbms.engine.data;
2

    
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.PrintWriter;
6

    
7
import org.xml.sax.InputSource;
8
import org.xml.sax.XMLReader;
9
import org.xml.sax.helpers.XMLReaderFactory;
10

    
11
import com.hardcode.driverManager.DriverLoadException;
12
import com.hardcode.gdbms.DataSourceTestCase;
13
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
14
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
15
import com.hardcode.gdbms.engine.data.driver.DriverException;
16
import com.hardcode.gdbms.engine.data.persistence.Handler;
17
import com.hardcode.gdbms.engine.data.persistence.Memento;
18
import com.hardcode.gdbms.engine.data.persistence.MementoContentHandler;
19
import com.hardcode.gdbms.engine.instruction.EvaluationException;
20
import com.hardcode.gdbms.engine.instruction.SemanticException;
21
import com.hardcode.gdbms.engine.values.BooleanValue;
22
import com.hardcode.gdbms.engine.values.ValueFactory;
23
import com.hardcode.gdbms.parser.ParseException;
24

    
25

    
26
/**
27
 * DOCUMENT ME!
28
 *
29
 * @author Fernando Gonz?lez Cort?s
30
 */
31
public class Tests extends DataSourceTestCase {
32
    /**
33
     * Tests the virtual primary key of files and objects
34
     *
35
     * @throws Exception DOCUMENT ME!
36
     */
37
    public void testFileAndObjectPK() throws Exception {
38
        DataSource d = ds.createRandomDataSource("persona",
39
                DataSourceFactory.MANUAL_OPENING);
40
        d.start();
41
        assertTrue(d.getFieldCount() == 7);
42
        assertTrue(((BooleanValue) d.getFieldValue(1, 0).equals(ValueFactory.createValue(
43
                    1))).getValue());
44
        assertTrue(d.getFieldName(0).equals("id"));
45
        assertTrue(d.getFieldName(1).equals("nombre"));
46
        assertTrue(d.getFieldName(2).equals("apellido"));
47
        assertTrue(d.getFieldName(3).equals("fecha"));
48
        assertTrue(d.getFieldName(4).equals("tiempo"));
49
        assertTrue(d.getFieldName(5).equals("marcatiempo"));
50
        assertTrue(d.getFieldName(6).equals("PK"));
51
        d.stop();
52
    }
53

    
54
    /**
55
     * DOCUMENT ME!
56
     *
57
     * @throws Exception
58
     * @throws RuntimeException DOCUMENT ME!
59
     */
60
    public void testDelegation() throws Exception {
61
        ds.setDelegating(false);
62

    
63
        DataSource d;
64

    
65
        try {
66
            d = ds.executeSQL("select apellido from hsqldbpersona;",
67
                    DataSourceFactory.MANUAL_OPENING);
68

    
69
            d.start();
70

    
71
            String aux = d.getAsString();
72
            d.stop();
73
            ds.setDelegating(true);
74

    
75
            d = ds.executeSQL("select apellido from hsqldbpersona;",
76
                    DataSourceFactory.MANUAL_OPENING);
77
            d.start();
78
            assertTrue(aux.equals(d.getAsString()));
79
            d.stop();
80
        } catch (DriverLoadException e) {
81
            throw new RuntimeException(e);
82
        } catch (ParseException e) {
83
            throw new RuntimeException(e);
84
        } catch (ReadDriverException e) {
85
            throw new RuntimeException(e);
86
        } catch (SemanticException e) {
87
            throw new RuntimeException(e);
88
        } finally {
89
            ds.clearViews();
90
        }
91
    }
92

    
93
    /**
94
     * DOCUMENT ME!
95
     *
96
     * @throws DriverException
97
     * @throws RuntimeException DOCUMENT ME!
98
     */
99
    public void testViewRemoving() throws ReadDriverException {
100
        ds.setDelegating(true);
101

    
102
        DataSource d1;
103
        DataSource d2;
104

    
105
        try {
106
            d1 = ds.executeSQL("select apellido from hsqldbpersona;",
107
                    DataSourceFactory.MANUAL_OPENING);
108
            d2 = ds.createRandomDataSource("hsqldbpersona",
109
                    DataSourceFactory.MANUAL_OPENING);
110
        } catch (DriverLoadException e1) {
111
            throw new RuntimeException(e1);
112
        } catch (ParseException e1) {
113
            throw new RuntimeException(e1);
114
        } catch (SemanticException e1) {
115
            throw new RuntimeException(e1);
116
        } catch (NoSuchTableException e) {
117
            throw new RuntimeException(e);
118
        } catch (EvaluationException e) {
119
            throw new RuntimeException(e);
120
        } finally {
121
            ds.clearViews();
122
        }
123

    
124
        try {
125
            d1.start();
126
            d1.stop();
127
            assertTrue("Views not deleted", false);
128
        } catch (ReadDriverException e) {
129
        }
130
    }
131

    
132
    /**
133
     * DOCUMENT ME!
134
     *
135
     * @throws DriverException
136
     * @throws RuntimeException DOCUMENT ME!
137
     */
138
    public void testQueryDataSources() throws ReadDriverException {
139
        DataSource d1;
140

    
141
        try {
142
            d1 = ds.createRandomDataSource("hsqldbapellido",
143
                    DataSourceFactory.MANUAL_OPENING);
144
            ds.setDelegating(true);
145

    
146
            DataSource d2 = ds.executeSQL("select apellido from hsqldbpersona;",
147
                    DataSourceFactory.MANUAL_OPENING);
148

    
149
            d1.start();
150
            d2.start();
151
            assertTrue(d1.getAsString().equals(d2.getAsString()));
152
            d1.stop();
153
            d2.stop();
154
        } catch (DriverLoadException e) {
155
            throw new RuntimeException(e);
156
        } catch (NoSuchTableException e) {
157
            throw new RuntimeException(e);
158
        } catch (ReadDriverException e) {
159
            throw new RuntimeException(e);
160
        } catch (ParseException e) {
161
            throw new RuntimeException(e);
162
        } catch (SemanticException e) {
163
            throw new RuntimeException(e);
164
        } catch (EvaluationException e) {
165
            throw new RuntimeException(e);
166
        } finally {
167
            ds.clearViews();
168
        }
169
    }
170

    
171
    /**
172
     * Tests the DataSource.remove method
173
     *
174
     * @throws RuntimeException DOCUMENT ME!
175
     */
176
    public void testRemoveDataSources() {
177
        DataSource d = null;
178

    
179
        try {
180
            d = ds.createRandomDataSource("persona",
181
                    DataSourceFactory.MANUAL_OPENING);
182
            d.remove();
183
        } catch (DriverLoadException e) {
184
            throw new RuntimeException(e);
185
        } catch (NoSuchTableException e) {
186
            throw new RuntimeException(e);
187
        } catch (ReadDriverException e) {
188
            throw new RuntimeException(e);
189
        } catch (WriteDriverException e) {
190
                throw new RuntimeException(e);
191
                }
192

    
193
        try {
194
            d = ds.createRandomDataSource("persona",
195
                    DataSourceFactory.MANUAL_OPENING);
196
            assertTrue(false);
197
        } catch (DriverLoadException e1) {
198
            throw new RuntimeException(e1);
199
        } catch (NoSuchTableException e1) {
200
            assertTrue(true);
201
        } catch (ReadDriverException e) {
202
            throw new RuntimeException(e);
203
        }
204
    }
205

    
206
    /**
207
     * Tests the DataSourceFactory.removeAllDataSources method
208
     */
209
    public void testRemoveAllDataSources() {
210
        ds.removeAllDataSources();
211
        assertTrue(ds.getDriverInfos().length == 0);
212
    }
213

    
214
    /**
215
     * Tests the naming of operation layer datasource
216
     *
217
     * @throws Throwable DOCUMENT ME!
218
     */
219
    public void testOperationDataSourceName() throws Throwable {
220
        DataSource d = ds.executeSQL("select * from persona;",
221
                DataSourceFactory.MANUAL_OPENING);
222
        assertTrue(ds.createRandomDataSource(d.getName(),
223
                DataSourceFactory.MANUAL_OPENING) != null);
224
    }
225

    
226
    /**
227
     * Tests the persistence
228
     *
229
     * @throws Throwable DOCUMENT ME!
230
     */
231
    public void testXMLMemento() throws Throwable {
232
        DataSource d = ds.executeSQL("select * from persona;",
233
                DataSourceFactory.MANUAL_OPENING);
234
        Memento m = d.getMemento();
235

    
236
        ByteArrayOutputStream out = new ByteArrayOutputStream();
237
        Handler h = new Handler();
238
        PrintWriter pw = new PrintWriter(out);
239
        h.setOut(pw);
240
        m.setContentHandler(h);
241
        m.getXML();
242
        pw.close();
243

    
244
        XMLReader reader = XMLReaderFactory.createXMLReader(
245
                "org.apache.crimson.parser.XMLReaderImpl");
246
        MementoContentHandler mch = new MementoContentHandler();
247
        reader.setContentHandler(mch);
248
        reader.parse(new InputSource(
249
                new ByteArrayInputStream(out.toByteArray())));
250

    
251
        DataSource n = mch.getDataSource(ds,
252
                DataSourceFactory.MANUAL_OPENING);
253

    
254
        n.start();
255
        d.start();
256
        assertTrue("Fallo en la persistencia",
257
            d.getAsString().equals(n.getAsString()));
258
        n.stop();
259
        d.stop();
260
    }
261

    
262
    /**
263
     * Tests the persistence
264
     *
265
     * @throws Throwable DOCUMENT ME!
266
     */
267
    public void testXMLMementoOfQueryDataSource() throws Throwable {
268
        DataSource d = ds.createRandomDataSource("hsqldbapellido",
269
                DataSourceFactory.MANUAL_OPENING);
270
        Memento m = d.getMemento();
271

    
272
        ByteArrayOutputStream out = new ByteArrayOutputStream();
273
        Handler h = new Handler();
274
        PrintWriter pw = new PrintWriter(out);
275
        h.setOut(pw);
276
        m.setContentHandler(h);
277
        m.getXML();
278
        pw.close();
279

    
280
        XMLReader reader = XMLReaderFactory.createXMLReader(
281
                "org.apache.crimson.parser.XMLReaderImpl");
282
        MementoContentHandler mch = new MementoContentHandler();
283
        reader.setContentHandler(mch);
284
        reader.parse(new InputSource(
285
                new ByteArrayInputStream(out.toByteArray())));
286

    
287
        DataSource n = mch.getDataSource(ds,
288
                DataSourceFactory.MANUAL_OPENING);
289

    
290
        n.start();
291
        d.start();
292
        assertTrue("Fallo en la persistencia",
293
            d.getAsString().equals(n.getAsString()));
294
        n.stop();
295
        d.stop();
296

    
297
        ds.clearViews();
298
    }
299

    
300
    public void testChangeDataSourceName() throws Throwable {
301
        ds.changeDataSourceName("persona", "nuevonombre");
302

    
303
        try{
304
            ds.createRandomDataSource("persona");
305
            assertTrue(false);
306
        }catch (NoSuchTableException e) {
307
            assertTrue(true);
308
        }
309
        try{
310
            DataSource d = ds.createRandomDataSource("nuevonombre");
311
            assertTrue(true);
312
            assertTrue(d.getName() == "nuevonombre");
313
        }catch (NoSuchTableException e) {
314
            assertTrue(false);
315
        }
316
    }
317
}