Statistics
| Revision:

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 / operations / sql / AbstractTestCount.java @ 46101

History | View | Annotate | Download (4.41 KB)

1
package org.gvsig.fmap.dal.store.jdbc2.operations.sql;
2

    
3
import junit.framework.TestCase;
4
import static junit.framework.TestCase.assertEquals;
5
import org.gvsig.fmap.dal.feature.FeatureQuery;
6
import org.gvsig.fmap.dal.feature.FeatureStore;
7
import org.gvsig.fmap.dal.feature.FeatureType;
8
import org.gvsig.fmap.dal.store.jdbc2.AbstractTestUtils;
9
import org.gvsig.fmap.dal.store.jdbc2.AbstractTestUtils.Expecteds;
10
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
11
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
12
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
13
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
14
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CountOperation;
15
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18

    
19
@SuppressWarnings("UseSpecificCatch")
20
public abstract class AbstractTestCount extends TestCase {
21

    
22
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestCount.class);
23

    
24
    protected AbstractTestUtils utils;
25

    
26
    public AbstractTestCount(String testName) {
27
        super(testName);
28
    }
29

    
30
    @Override
31
    protected void setUp() throws Exception {
32
        super.setUp();
33
        new DefaultLibrariesInitializer().fullInitialize();
34
    }
35

    
36
    @Override
37
    protected void tearDown() throws Exception {
38
        super.tearDown();
39
    }
40

    
41
    public AbstractTestUtils utils() {
42
        if (this.utils == null) {
43
            this.utils = this.createUtils();
44
        }
45
        return this.utils;
46
    }
47

    
48
    protected abstract AbstractTestUtils createUtils();
49

    
50
    public void testCount() throws Exception {
51
        try {
52
            JDBCHelper helper = utils().createJDBCHelper();
53
            JDBCSQLBuilderBase sqlbuilder = helper.createSQLBuilder();
54
            OperationsFactory operations = helper.getOperations();
55

    
56
            Expecteds expectedSQLs = utils().getExpecteds("count.txt");
57

    
58
            FeatureStore sourceStore = utils().openSourceStore1();
59

    
60
            TableReference table = operations.createTableReference(
61
                    "dbtest",
62
                    sqlbuilder.default_schema(),
63
                    "test",
64
                    null
65
            );
66
            FeatureType featureType = sourceStore.getDefaultFeatureType();
67
            CountOperation count = operations.createCount(
68
                    featureType,
69
                    table,
70
                    null,
71
                    null
72
            );
73

    
74
            String sql = count.getSQL();
75
            System.out.println("###### SQL:" + sql+"###");
76
            System.out.println("###### EXP:" + expectedSQLs.get("count")+"###");
77

    
78
            assertEquals("Count SQL", expectedSQLs.get("count"), sql);
79
        } catch (Throwable th) {
80
            LOGGER.warn("", th);
81
            throw th;
82
        }
83
    }
84

    
85
    public void testSimpleGroup() throws Exception {
86
        try {
87
            JDBCHelper helper = utils().createJDBCHelper();
88
            JDBCSQLBuilderBase sqlbuilder = helper.createSQLBuilder();
89
            OperationsFactory operations = helper.getOperations();
90

    
91
            Expecteds expectedSQLs = utils().getExpecteds("count.txt");
92

    
93
            FeatureStore sourceStore = utils().openSourceStore1();
94

    
95
            TableReference table = operations.createTableReference(
96
                    "dbtest",
97
                    sqlbuilder.default_schema(),
98
                    "test",
99
                    null
100
            );
101
            FeatureType featureType = sourceStore.getDefaultFeatureType();
102
            FeatureQuery query = sourceStore.createFeatureQuery();
103
            query.getGroupByColumns().add("Long");
104
            query.getAggregateFunctions().put("ID", "MIN");
105
            query.getAggregateFunctions().put("Byte", "MAX");
106
            query.getAggregateFunctions().put("Double", "SUM");
107
            query.getOrder().add("Long");
108
            CountOperation count = operations.createCount(
109
                    featureType,
110
                    table,
111
                    null,
112
                    query
113
            );
114
            String sql = count.getSQL();
115
            System.out.println("###### SQL:" + sql+"###");
116
            System.out.println("###### EXP:" + expectedSQLs.get("SimpleGroup")+"###");
117

    
118
            assertEquals("Count with group SQL", expectedSQLs.get("SimpleGroup"), sql);
119
        } catch (Throwable th) {
120
            LOGGER.warn("", th);
121
            throw th;
122
        }
123
    }
124

    
125
    // TODO: a?adir un test con where, group y order.
126
}