summaryrefslogtreecommitdiffstats
path: root/das-java/trunk/rdb/src/test/java/org/apache/tuscany/das/rdb/test/CompanyTests.java
blob: 65c2e8659220749e83cfd896231fa3b69fabdf8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.das.rdb.test;

import org.apache.tuscany.das.rdb.Command;
import org.apache.tuscany.das.rdb.DAS;
import org.apache.tuscany.das.rdb.test.company.CompanyFactory;
import org.apache.tuscany.das.rdb.test.company.CompanyType;
import org.apache.tuscany.das.rdb.test.company.DepartmentType;
import org.apache.tuscany.das.rdb.test.company.EmployeeType;
import org.apache.tuscany.das.rdb.test.data.CompanyData;
import org.apache.tuscany.das.rdb.test.data.CompanyDeptData;
import org.apache.tuscany.das.rdb.test.data.DepEmpData;
import org.apache.tuscany.das.rdb.test.data.DepartmentData;
import org.apache.tuscany.das.rdb.test.data.EmployeeData;
import org.apache.tuscany.das.rdb.test.framework.DasTest;

import commonj.sdo.DataObject;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.impl.HelperProvider;

public class CompanyTests extends DasTest {

    protected void setUp() throws Exception {
        super.setUp();

        new CompanyData(getAutoConnection()).refresh();
        new DepartmentData(getAutoConnection()).refresh();
        new EmployeeData(getAutoConnection()).refresh();
        new CompanyDeptData(getAutoConnection()).refresh();
        new DepEmpData(getAutoConnection()).refresh();

    }

    public void testSimple() throws Exception {

        DAS das = DAS.FACTORY.createDAS(getConfig("companyMapping.xml"), getConnection());

        // Build the select command
        Command selectCommand = das.createCommand("select COMPANY.ID, COMPANY.NAME, " 
                + "EMPLOYEE.ID, EMPLOYEE.NAME, EMPLOYEE.SN, EMPLOYEE.MANAGER, "
                + "DEPARTMENT.ID, DEPARTMENT.NAME, DEPARTMENT.LOCATION, DEPARTMENT.DEPNUMBER from COMPANY, DEPARTMENT, EMPLOYEE "
                + "where COMPANY.ID=DEPARTMENT.COMPANYID and DEPARTMENT.ID=EMPLOYEE.DEPARTMENTID");

        // Get the graph
        DataObject root = selectCommand.executeQuery();

        // Get a company
        DataObject company = (DataObject) root.getList("CompanyType").get(0);
        assertEquals("MegaCorp", company.get("NAME"));

        // Get a department
        DataObject department = (DataObject) company.getList("departments").get(0);
        assertEquals("Advanced Technologies", department.get("NAME"));

        DataObject employee = (DataObject) department.getList("employees").get(0);
        assertEquals("John Jones", employee.get("NAME"));
    }

    public void testSimpleStatic() throws Exception {

        DAS das = DAS.FACTORY.createDAS(getConfig("companyMappingWithConverters.xml"), getConnection());
        HelperContext context = HelperProvider.getDefaultContext();
        CompanyFactory.INSTANCE.register(context);
        // Build the select command
        Command selectCommand = das.createCommand("select COMPANY.ID, COMPANY.NAME, " 
                + "EMPLOYEE.ID, EMPLOYEE.NAME, EMPLOYEE.SN, EMPLOYEE.MANAGER, "
                + "DEPARTMENT.ID, DEPARTMENT.NAME, DEPARTMENT.LOCATION, DEPARTMENT.DEPNUMBER from COMPANY, DEPARTMENT, EMPLOYEE "
                + "where COMPANY.ID=DEPARTMENT.COMPANYID and DEPARTMENT.ID=EMPLOYEE.DEPARTMENTID");

        // Get the graph
        DataObject root = selectCommand.executeQuery();

        CompanyType company = (CompanyType) root.getList("CompanyType").get(0);

        assertEquals("MegaCorp", company.getName());

        // Get a department
        DepartmentType department = (DepartmentType) company.getDepartments().get(0);
        assertEquals("Advanced Technologies", department.getName());

        EmployeeType employee = (EmployeeType) department.getEmployees().get(0);

        assertEquals("John Jones", employee.getName());
    }

    /**
     * Default database setup inserts 3 records in COMPANY. This test verifies that these records have auto-gen keys
     * 1,2,3 and the new created record has auto-gen key 4 in COMPANY.ID
     * @throws Exception
     */
    public void testCreateAutoGenKeyStatic() throws Exception {
    	//get DAS and select command
    	DataObject root = null;
    	
    	DAS das = DAS.FACTORY.createDAS(getConfig("companyMappingWithConverters.xml"), getConnection());
    	HelperContext context = HelperProvider.getDefaultContext();
        CompanyFactory.INSTANCE.register(context);
        Command selectCommand = das.getCommand("AllCompanies");
        root = selectCommand.executeQuery();
        assertEquals(3, root.getList("CompanyType").size());
        //verify result
        CompanyType company = (CompanyType) root.getList("CompanyType").get(0);
        int startId = company.getId();
        assertEquals(startId, company.getId());
        assertEquals("ACME Publishing", company.getName());

        company = (CompanyType) root.getList("CompanyType").get(1);
        assertEquals(startId+1, company.getId());
        assertEquals("Do-rite plumbing", company.getName());
        
        company = (CompanyType) root.getList("CompanyType").get(2);
        assertEquals(startId+2, company.getId());
        assertEquals("MegaCorp", company.getName());
        
        //create a new company. auto get keys set in config and supported in table creation statement
        CompanyType newCompany = (CompanyType)root.createDataObject("CompanyType");
        newCompany.setName("MyNewCompany");
        das.applyChanges(root);        

        assertEquals("MyNewCompany", newCompany.getName());
        assertEquals(startId+3, newCompany.getId());
    }
    
    public void testCreateAutoGenKeyDynamic() throws Exception {
    	//get DAS and select command
    	DataObject root = null;
    	
    	DAS das = DAS.FACTORY.createDAS(getConfig("basicCompanyDepartmentMapping.xml"), getConnection());
        Command selectCommand = das.createCommand("select * from COMPANY");
        root = selectCommand.executeQuery();
        assertEquals(3, root.getList("COMPANY").size());
        //verify result
        DataObject company = (DataObject)root.getList("COMPANY").get(0);
        int startId = company.getInt("ID");
        assertEquals(startId, company.getInt("ID"));
        assertEquals("ACME Publishing", company.getString("NAME"));

        company = (DataObject)root.getList("COMPANY").get(1);
        assertEquals(startId+1, company.getInt("ID"));
        assertEquals("Do-rite plumbing", company.getString("NAME"));
        
        company = (DataObject)root.getList("COMPANY").get(2);
        assertEquals(startId+2, company.getInt("ID"));
        assertEquals("MegaCorp", company.getString("NAME"));
        
        //create a new company. auto get keys set in config and supported in table creation statement
        DataObject newCompany = root.createDataObject("COMPANY");
        newCompany.setString("NAME", "MyNewCompany");
        das.applyChanges(root);        

        assertEquals("MyNewCompany", newCompany.getString("NAME"));
        assertEquals(startId+3, newCompany.getInt("ID"));       
    }    
}