1 /*
2    Copyright 2010 Sun Microsystems, Inc.
3    All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 package testsuite.clusterj;
27 
28 import com.mysql.clusterj.ClusterJUserException;
29 import com.mysql.clusterj.Query;
30 import com.mysql.clusterj.query.QueryDomainType;
31 import com.mysql.clusterj.query.QueryBuilder;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 import testsuite.clusterj.model.NotPersistentTypes;
37 
38 public class NotPersistentTest extends AbstractClusterJModelTest {
39 
40     private static final int NUMBER_TO_INSERT = 5;
41 
42     protected List<NotPersistentTypes> notPersistentTypes =
43             new ArrayList<NotPersistentTypes>();
44 
45     @Override
localSetUp()46     public void localSetUp() {
47         createSessionFactory();
48         session = sessionFactory.getSession();
49         createNotPersistentTypesInstances(NUMBER_TO_INSERT);
50         int count = session.deletePersistentAll(NotPersistentTypes.class);
51         addTearDownClasses(NotPersistentTypes.class);
52 //        System.out.println("Deleted " + count + " instances.");
53     }
54 
test()55     public void test() {
56         defaultValues();
57         setValues();
58         insert();
59         find();
60         query();
61         badQuery();
62         delete();
63         failOnError();
64     }
65 
defaultValues()66     protected void defaultValues() {
67         NotPersistentTypes npt = session.newInstance(NotPersistentTypes.class);
68         errorIfNotEqual("NotPersistentInteger default value.",
69                 null, npt.getNotPersistentInteger());
70         errorIfNotEqual("NotPersistentChildren default value.",
71                 null, npt.getNotPersistentChildren());
72         errorIfNotEqual("NotPersistentInt default value.",
73                 0, npt.getNotPersistentInt());
74         errorIfNotEqual("Id default value.",
75                 0, npt.getId());
76         errorIfNotEqual("Name default value.",
77                 null, npt.getName());
78         errorIfNotEqual("Magic default value.",
79                 0, npt.getMagic());
80         errorIfNotEqual("Age default value.",
81                 null, npt.getAge());
82     }
83 
setValues()84     protected void setValues() {
85         NotPersistentTypes npt = notPersistentTypes.get(0);
86         List<NotPersistentTypes> children = new ArrayList<NotPersistentTypes>();
87         children.add(notPersistentTypes.get(1));
88         children.add(notPersistentTypes.get(2));
89         npt.setNotPersistentChildren(children);
90         errorIfNotEqual("NotPersistentInteger default value.",
91                 children, npt.getNotPersistentChildren());
92         int intValue = 666;
93         npt.setNotPersistentInt(intValue);
94         errorIfNotEqual("NotPersistentChildren default value.",
95                 intValue, npt.getNotPersistentInt());
96         Integer integerValue = Integer.valueOf(intValue);
97         npt.setNotPersistentInteger(integerValue);
98         errorIfNotEqual("NotPersistentInt default value.",
99                 integerValue, npt.getNotPersistentInteger());
100     }
101 
insert()102     protected void insert() {
103         tx = session.currentTransaction();
104         tx.begin();
105         for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
106             session.makePersistent(notPersistentTypes.get(i));
107         }
108 
109         tx.commit();
110     }
111 
find()112     protected void find() {
113         tx = session.currentTransaction();
114         tx.begin();
115         for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
116             NotPersistentTypes instance = session.find(NotPersistentTypes.class, i);
117             errorIfNotEqual("Wrong instance", i, instance.getId());
118         }
119         tx.commit();
120     }
121 
query()122     protected void query() {
123         tx = session.currentTransaction();
124         tx.begin();
125         QueryBuilder builder = session.getQueryBuilder();
126         QueryDomainType dobj = builder.createQueryDefinition(NotPersistentTypes.class);
127         dobj.where(dobj.get("id").equal(dobj.param("id")));
128         Query query = session.createQuery(dobj);
129         query.setParameter("id", 0);
130         List<NotPersistentTypes> result = query.getResultList();
131         int resultSize = result.size();
132         errorIfNotEqual("Wrong query result size", 1, resultSize);
133         NotPersistentTypes npt = result.get(0);
134         int resultId = npt.getId();
135         tx.commit();
136         errorIfNotEqual("Wrong query result instance id 0", 0, resultId);
137     }
138 
badQuery()139     protected void badQuery() {
140         try {
141             QueryBuilder builder = session.getQueryBuilder();
142             QueryDomainType dobj = builder.createQueryDefinition(NotPersistentTypes.class);
143             dobj.where(dobj.get("notPersistentChildren").equal(dobj.param("id")));
144             error("Failed to catch user exception for not-persistent query field");
145         } catch (ClusterJUserException ex) {
146             // good catch
147         }
148     }
149 
delete()150     protected void delete() {
151         tx = session.currentTransaction();
152         tx.begin();
153         for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
154             session.deletePersistent(notPersistentTypes.get(i));
155         }
156         tx.commit();
157     }
158 
createNotPersistentTypesInstances(int count)159     protected void createNotPersistentTypesInstances(int count) {
160         notPersistentTypes = new ArrayList<NotPersistentTypes>(count);
161         for (int i = 0; i < count; ++i) {
162             NotPersistentTypes npt = session.newInstance(NotPersistentTypes.class);
163             npt.setId(i);
164             npt.setName("Employee number " + i);
165             npt.setAge(i);
166             npt.setMagic(i);
167             notPersistentTypes.add(npt);
168         }
169     }
170 
171 }
172