MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FindByPrimaryKey2Test.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package testsuite.clusterj;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Properties;
23 
24 import com.mysql.clusterj.Constants;
25 
26 import testsuite.clusterj.model.Employee2;
27 
29 
30  protected List<Employee2> employees2;
31 
32  private static final int NUMBER_TO_INSERT = 1;
33 
34  @Override
35  protected Properties modifyProperties() {
36  Properties modifiedProperties = new Properties();
37  modifiedProperties.putAll(props);
38  modifiedProperties.put(Constants.PROPERTY_CLUSTER_DATABASE, "test2");
39  return modifiedProperties;
40  }
41 
42  @Override
43  public void localSetUp() {
44  createSessionFactory();
45  session = sessionFactory.getSession();
46  createEmployee2Instances(NUMBER_TO_INSERT);
47  tx = session.currentTransaction();
48  tx.begin();
49  session.deletePersistentAll(Employee2.class);
50  tx.commit();
51  addTearDownClasses(Employee2.class);
52  }
53 
54  public void testFind() {
55  // first, create instances to find
56  tx = session.currentTransaction();
57  tx.begin();
58 
59  int count = 0;
60 
61  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
62  // must be done with an active transaction
63  session.makePersistent(employees2.get(i));
64  ++count;
65  }
66  tx.commit();
67 
68  tx.begin();
69 
70  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
71  // must be done with an active transaction
72  Employee2 e = session.find(Employee2.class, i);
73  // see if it is the right Employee
74  int actualId = e.getId();
75  if (actualId != i) {
76  error("Expected Employee2.id " + i + " but got " + actualId);
77  }
78  }
79  tx.commit();
80  failOnError();
81  }
82 
83  protected void createEmployee2Instances(int count) {
84  employees2 = new ArrayList<Employee2>(count);
85  for (int i = 0; i < count; ++i) {
86  Employee2 emp = session.newInstance(Employee2.class);
87  emp.setId(i);
88  emp.setName("Employee number " + i);
89  emp.setAge(i);
90  emp.setMagic(i);
91  employees2.add(emp);
92  }
93  }
94 
95 }