svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial / org.gvsig.h2spatial.h2gis132 / org.gvsig.h2spatial.h2gis132.provider / src / test / java / org / gvsig / fmap / dal / store / h2 / operations / sql / TestPerformChanges.java @ 47787
History | View | Annotate | Download (4.28 KB)
1 | 45472 | jjdelcerro | package org.gvsig.fmap.dal.store.h2.operations.sql; |
---|---|---|---|
2 | |||
3 | import java.util.Collections; |
||
4 | import java.util.List; |
||
5 | import junit.framework.TestCase; |
||
6 | import org.gvsig.fmap.dal.DALLocator; |
||
7 | import org.gvsig.fmap.dal.DataTypes; |
||
8 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
9 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
10 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureType.FeatureTypeChanged; |
||
13 | import org.gvsig.fmap.dal.store.h2.TestUtils; |
||
14 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
15 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
||
16 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
||
17 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
18 | import org.gvsig.fmap.dal.store.jdbc2.spi.operations.PerformChangesOperation; |
||
19 | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
||
20 | import org.slf4j.Logger; |
||
21 | import org.slf4j.LoggerFactory; |
||
22 | |||
23 | public class TestPerformChanges extends TestCase { |
||
24 | |||
25 | private static final Logger LOGGER = LoggerFactory.getLogger(TestPerformChanges.class); |
||
26 | |||
27 | public TestPerformChanges(String testName) { |
||
28 | super(testName);
|
||
29 | } |
||
30 | |||
31 | @Override
|
||
32 | protected void setUp() throws Exception { |
||
33 | super.setUp();
|
||
34 | new DefaultLibrariesInitializer().fullInitialize();
|
||
35 | } |
||
36 | |||
37 | @Override
|
||
38 | protected void tearDown() throws Exception { |
||
39 | super.tearDown();
|
||
40 | } |
||
41 | |||
42 | // TODO add test methods here. The name must begin with 'test'. For example:
|
||
43 | // public void testHello() {}
|
||
44 | |||
45 | public void testPerformChanges() throws Exception { |
||
46 | 45649 | jjdelcerro | JDBCHelper helper = TestUtils.createJDBCHelper(); |
47 | 45472 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = helper.createSQLBuilder(); |
48 | OperationsFactory operations = helper.getOperations(); |
||
49 | |||
50 | List<String> expectedSQLs = TestUtils.getSQLs("performChanges.sql"); |
||
51 | |||
52 | FeatureStore sourceStore = TestUtils.openSourceStore1(); |
||
53 | |||
54 | TableReference table = operations.createTableReference( |
||
55 | "dbtest",
|
||
56 | sqlbuilder.default_schema(), |
||
57 | "test",
|
||
58 | null
|
||
59 | ); |
||
60 | FeatureType featureType = sourceStore.getDefaultFeatureType(); |
||
61 | |||
62 | |||
63 | FeatureTypeChanged featureTypeChanged = new FeatureTypeChanged() {
|
||
64 | @Override
|
||
65 | public FeatureType getSource() {
|
||
66 | EditableFeatureType ft = DALLocator.getDataManager().createFeatureType(); |
||
67 | EditableFeatureAttributeDescriptor id = ft.add("ID", DataTypes.INT);
|
||
68 | id.copyFrom(featureType.getAttributeDescriptor("ID"));
|
||
69 | return ft;
|
||
70 | } |
||
71 | |||
72 | @Override
|
||
73 | public FeatureType getTarget() {
|
||
74 | return featureType;
|
||
75 | } |
||
76 | }; |
||
77 | List<FeatureTypeChanged> featureTypesChangeds = Collections.singletonList(featureTypeChanged); |
||
78 | |||
79 | PerformChangesOperation performChanges = operations.createPerformChanges( |
||
80 | table, |
||
81 | sourceStore.getDefaultFeatureType(), |
||
82 | null,
|
||
83 | null,
|
||
84 | null,
|
||
85 | featureTypesChangeds.iterator() |
||
86 | ); |
||
87 | String insertSQL = performChanges.getInsertSQL();
|
||
88 | String deleteSQL = performChanges.getDeleteSQL();
|
||
89 | String updateSQL = performChanges.getUpdateSQL();
|
||
90 | List<String> updateTableSQLs = performChanges.getUpdateTableSQLs(); |
||
91 | |||
92 | 46102 | jjdelcerro | System.out.println("###### EXP[insert]:" + expectedSQLs.get(0) + "###"); |
93 | System.out.println("###### SQL[insert]:" + insertSQL + "###"); |
||
94 | System.out.println("###### EXP[delete]:" + expectedSQLs.get(1) + "###"); |
||
95 | System.out.println("###### SQL[delete]:" + deleteSQL + "###"); |
||
96 | System.out.println("###### EXP[update]:" + expectedSQLs.get(2) + "###"); |
||
97 | System.out.println("###### SQL[update]:" + updateSQL + "###"); |
||
98 | |||
99 | for (int i = 0; i < updateTableSQLs.size(); i++) { |
||
100 | System.out.println("###### EXP[updatetable "+i+"]:" + expectedSQLs.get(i+3) + "###"); |
||
101 | System.out.println("###### SQL[updatetable "+i+"]:" + updateTableSQLs.get(i) + "###"); |
||
102 | } |
||
103 | |||
104 | 45472 | jjdelcerro | assertEquals("Insert SQL", expectedSQLs.get(0), insertSQL); |
105 | assertEquals("Delete SQL", expectedSQLs.get(1), deleteSQL); |
||
106 | assertEquals("Update SQL", expectedSQLs.get(2), updateSQL); |
||
107 | assertEquals("UpdateTable num. SQLs", expectedSQLs.size()-3, updateTableSQLs.size()); |
||
108 | for (int i = 0; i < updateTableSQLs.size(); i++) { |
||
109 | assertEquals("UpdateTable SQL "+i, expectedSQLs.get(i+3), updateTableSQLs.get(i)); |
||
110 | } |
||
111 | |||
112 | |||
113 | } |
||
114 | |||
115 | } |