summaryrefslogtreecommitdiffstats
path: root/sandbox/lresende/sca-1.x/modules/implementation-data-pojo/src/test/java/org/apache/tuscany/sca/implementation/openjpa/CompanyTestCase.java
blob: 1c4d6fb254baa84cfe3562a83921934122b4b796 (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
/*
 * 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.sca.implementation.openjpa;

import junit.framework.*;
import org.apache.tuscany.sca.host.embedded.*;
import org.apache.commons.logging.*;

import company.*;

import javax.persistence.*;
import java.util.*;

public class CompanyTestCase extends TestCase {
    private EntityManager em;
    private Log log = LogFactory.getLog(this.getClass());
    private SCADomain domain;

    public void setUp() {
        domain = SCADomain.newInstance("openjpa.composite");
        em = domain.getService(EntityManager.class, "CompanyServiceComponent");

    }
    
    public void testAccess() {
        Company company = new Company();
        int id = new Random().nextInt();
        
        company.setId(id);
        company.setName("Company " + id);
        
        em.persist(company);
        log.info(em.find(Company.class, id));
        
        Query q = em.createQuery("select company from Company company");
        q.setMaxResults(5);
        log.info("There are " + q.getResultList().size() + " Company in the database now");
    }

    public void testRollback() {
        try {
            Company company1 = new Company();
            Company company2 = new Company();
            
            int id = new Random().nextInt();
            
            company1.setId(id);
            company1.setName("Company " + id);
            em.persist(company1);
            
            company2.setId(id);
            company2.setName("Company " + id);
            em.persist(company2);

        } catch (RuntimeException ex) {
            log.info("An expected exception occured, Tuscany is rolling back...");
        }
    }

    public void tearDown() {
        em.close();
        domain.close();
    }
}