svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / test / java / org / gvsig / fmap / dal / store / jdbc2 / AbstractTestWhereWithComputedField.java @ 46517
History | View | Annotate | Download (4.31 KB)
1 | 46101 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2; |
---|---|---|---|
2 | 45472 | jjdelcerro | |
3 | 46050 | omartinez | import java.util.ArrayList; |
4 | 45472 | jjdelcerro | import java.util.List; |
5 | import junit.framework.TestCase; |
||
6 | import static junit.framework.TestCase.assertEquals; |
||
7 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
8 | import org.gvsig.fmap.dal.DataTypes; |
||
9 | 46050 | omartinez | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
10 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
11 | import org.gvsig.fmap.dal.feature.Feature; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
13 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
14 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
15 | 46050 | omartinez | import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureAttributeEmulatorExpression; |
16 | import org.gvsig.tools.dispose.DisposeUtils; |
||
17 | 45472 | jjdelcerro | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
18 | import org.slf4j.Logger; |
||
19 | import org.slf4j.LoggerFactory; |
||
20 | |||
21 | 46170 | omartinez | public abstract class AbstractTestWhereWithComputedField extends TestCase { |
22 | 46105 | omartinez | |
23 | 46170 | omartinez | protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestWhereWithComputedField.class); |
24 | 46101 | jjdelcerro | |
25 | protected AbstractTestUtils utils;
|
||
26 | |||
27 | 46170 | omartinez | public AbstractTestWhereWithComputedField(String testName) { |
28 | 45472 | jjdelcerro | super(testName);
|
29 | } |
||
30 | 46105 | omartinez | |
31 | 45472 | jjdelcerro | @Override
|
32 | protected void setUp() throws Exception { |
||
33 | super.setUp();
|
||
34 | new DefaultLibrariesInitializer().fullInitialize();
|
||
35 | } |
||
36 | 46105 | omartinez | |
37 | 45472 | jjdelcerro | @Override
|
38 | protected void tearDown() throws Exception { |
||
39 | super.tearDown();
|
||
40 | } |
||
41 | 46105 | omartinez | |
42 | 46101 | jjdelcerro | public AbstractTestUtils utils() {
|
43 | if (this.utils == null) { |
||
44 | this.utils = this.createUtils(); |
||
45 | 45472 | jjdelcerro | } |
46 | 46101 | jjdelcerro | return this.utils; |
47 | 45472 | jjdelcerro | } |
48 | |||
49 | 46101 | jjdelcerro | protected abstract AbstractTestUtils createUtils(); |
50 | 46105 | omartinez | |
51 | 46101 | jjdelcerro | // TODO add test methods here. The name must begin with 'test'. For example:
|
52 | // public void testHello() {}
|
||
53 | 46170 | omartinez | protected void testWhereWithComputedField() throws Exception { |
54 | 46105 | omartinez | try {
|
55 | if (!utils().isTheDatabaseAvailable()) {
|
||
56 | return;
|
||
57 | } |
||
58 | FeatureStore sourceStore = utils().openSourceStore2(); |
||
59 | JDBCServerExplorer explorer = utils().openServerExplorer("testCreate");
|
||
60 | utils().initWorkspace("testCreate");
|
||
61 | |||
62 | utils().info_jdbc(explorer); |
||
63 | 45472 | jjdelcerro | |
64 | 46105 | omartinez | utils().drop_tables(explorer, "testCreateTarget2");
|
65 | utils().create_table_from(explorer, "testCreateTarget2", sourceStore);
|
||
66 | utils().insert_into_from(explorer, "testCreateTarget2", sourceStore, FeatureStore.MODE_APPEND);
|
||
67 | 46101 | jjdelcerro | |
68 | 46105 | omartinez | FeatureStore dbstore = utils().openStore(explorer, "testCreateTarget2");
|
69 | dbstore.edit(); |
||
70 | FeatureType featureType = dbstore.getDefaultFeatureType(); |
||
71 | EditableFeatureType eFeatureType = featureType.getEditable(); |
||
72 | eFeatureType.add("Compu1",
|
||
73 | DataTypes.INTEGER, |
||
74 | new DefaultFeatureAttributeEmulatorExpression(
|
||
75 | eFeatureType, |
||
76 | ExpressionUtils.createExpression("ID*2")
|
||
77 | )); |
||
78 | eFeatureType.add("Compu2",
|
||
79 | DataTypes.INTEGER, |
||
80 | new DefaultFeatureAttributeEmulatorExpression(
|
||
81 | eFeatureType, |
||
82 | ExpressionUtils.createExpression("Poblacion+10000+Compu1")
|
||
83 | )); |
||
84 | dbstore.update(eFeatureType); |
||
85 | dbstore.finishEditing(); |
||
86 | 46170 | omartinez | List<Feature> features1 = dbstore.getFeatures("Compu1>8"); |
87 | System.out.println("## in output"); |
||
88 | assertEquals("Features 1 size:", 5, features1.size()); |
||
89 | for (int i = 0; i < features1.size(); i++) { |
||
90 | Feature feature = features1.get(i); |
||
91 | System.out.println("field Compu1: "+feature.format("Compu1") + " feature "+i+": "+feature.toString()); |
||
92 | 46050 | omartinez | } |
93 | 46170 | omartinez | List<Feature> features2 = dbstore.getFeatures("Compu1<=8"); |
94 | System.out.println("## in output"); |
||
95 | assertEquals("Features 2 size:", 5, features2.size()); |
||
96 | for (int i = 0; i < features2.size(); i++) { |
||
97 | Feature feature = features2.get(i); |
||
98 | System.out.println("field Compu1: "+feature.format("Compu1") + " feature "+i+": "+feature.toString()); |
||
99 | 46105 | omartinez | } |
100 | 46170 | omartinez | |
101 | System.out.println("## end output"); |
||
102 | 46105 | omartinez | DisposeUtils.dispose(dbstore); |
103 | } catch (Throwable th) { |
||
104 | LOGGER.warn("", th);
|
||
105 | throw th;
|
||
106 | 46050 | omartinez | } |
107 | 45472 | jjdelcerro | } |
108 | |||
109 | } |