18 package testsuite.clusterj;
20 import com.mysql.clusterj.ClusterJUserException;
21 import com.mysql.clusterj.ColumnType;
22 import com.mysql.clusterj.DynamicObject;
23 import com.mysql.clusterj.ColumnMetadata;
24 import com.mysql.clusterj.Query;
25 import com.mysql.clusterj.annotation.PersistenceCapable;
26 import com.mysql.clusterj.query.QueryBuilder;
27 import com.mysql.clusterj.query.QueryDomainType;
31 private static final String tablename =
"t_basic";
33 private static final int NUMBER_TO_INSERT = 5;
35 private DynamicObject[] instances =
new TBasic[NUMBER_TO_INSERT];
39 private Object[] expectedTBasicNames =
new Object[] {
"id",
"name",
"age",
"magic"};
43 private Object[] expectedTBasicJavaTypes =
new Object[] {Integer.class,
String.class, Integer.class, Integer.class};
45 private Object[] expectedTBasicMaximumLengths =
new Object[] {1, 32, 1, 1};
47 private Object[] expectedTBasicNumbers =
new Object[] {0, 1, 2, 3};
49 private Object[] expectedTBasicIsPrimaryKeys =
new Object[] {
true,
false,
false,
false};
51 private Object[] expectedTBasicIsPartitionKeys =
new Object[] {
true,
false,
false,
false};
53 private Object[] expectedTBasicPrecisions =
new Object[] {0, 0, 0, 0};
55 private Object[] expectedTBasicScales =
new Object[] {0, 0, 0, 0};
57 private Object[] expectedTBasicNullables =
new Object[] {
false,
true,
true,
false};
59 private Object[] expectedTBasicCharsetNames =
new Object[] {null,
"latin1", null, null};
63 createSessionFactory();
65 createDynamicInstances(TBasic.class, NUMBER_TO_INSERT);
66 tbasic = instances[0];
69 for (
int i = 0;
i < NUMBER_TO_INSERT; ++
i) {
73 }
catch (Exception ex) {
77 addTearDownClasses(TBasic.class);
80 private <T extends DynamicObject>
void createDynamicInstances(Class<T> dynamicClass,
int numberToInsert) {
81 for (
int i = 0;
i < numberToInsert; ++
i) {
83 instance.set(1,
String.valueOf(
i));
86 instances[
i] = instance;
90 private <T> T createInstance(Class<T> cls,
int i) {
95 public static class TBasic
extends DynamicObject {
102 @PersistenceCapable(
table=
"t_basic")
103 public static class AnnotatedTBasic extends DynamicObject {}
111 badClass(DynamicObjectPrivate.class);
112 badClass(DynamicObjectProtectedConstructor.class);
113 badClass(DynamicObjectNonStatic.class);
114 badClass(DynamicObjectPrivateConstructor.class);
115 badClass(DynamicObjectNullTableName.class);
116 badClass(DynamicObjectTableDoesNotExist.class);
120 private void insert() {
124 private void find() {
125 TBasic instance = session.
find(TBasic.class, 0);
126 validateInstance(instance);
129 private void findAnnotated() {
130 AnnotatedTBasic instance = session.
find(AnnotatedTBasic.class, 0);
131 validateInstance(instance);
137 queryTBasic.where(queryTBasic.get(
"magic").equal(queryTBasic.param(
"magic")));
139 query.setParameter(
"magic", 1);
140 TBasic instance = query.getResultList().get(0);
141 validateInstance(instance);
144 private void query() {
147 queryTBasic.where(queryTBasic.get(
"name").equal(queryTBasic.param(
"name")));
148 Query<TBasic> query = session.
createQuery(queryTBasic);
149 query.setParameter(
"name",
"2");
150 TBasic instance = query.getResultList().get(0);
151 validateInstance(instance);
154 private void validateInstance(DynamicObject instance) {
155 int id = (Integer)instance.get(0);
156 errorIfNotEqual(
"validate name",
String.valueOf(
id), instance.get(1));
157 errorIfNotEqual(
"validate age",
id, instance.get(2));
158 errorIfNotEqual(
"validate magic",
id, instance.get(3));
161 private void badClass(Class<?> cls) {
164 }
catch (ClusterJUserException e) {
166 }
catch (Throwable t) {
167 error(cls.getClass().getName() +
" threw wrong exception: " + t.getMessage());
171 public static class DynamicObjectProtectedConstructor
extends DynamicObject {
172 protected DynamicObjectProtectedConstructor() {}
175 return "DynamicObjectProtectedConstructor";
179 private static class DynamicObjectPrivate
extends DynamicObject {
186 return "DynamicObjectProtectedConstructor";
190 public static class DynamicObjectPrivateConstructor
extends DynamicObject {
191 private DynamicObjectPrivateConstructor() {}
194 return "DynamicObjectPrivateConstructor";
198 public static class DynamicObjectTableDoesNotExist
extends DynamicObject {
199 private DynamicObjectTableDoesNotExist() {}
202 return "DynamicObjectTableDoesNotExist";
206 public static class DynamicObjectNullTableName
extends DynamicObject {
207 public DynamicObjectNullTableName() {}
210 protected void checkMetadata() {
211 ColumnMetadata[] metadata = tbasic.columnMetadata();
212 for (
int i = 0; i < metadata.length; ++
i) {
213 errorIfNotEqual(
"t_basic column " + i +
" name", expectedTBasicNames[i], metadata[i].
name());
214 errorIfNotEqual(
"t_basic column " + i +
" type", expectedTBasicTypes[i], metadata[i].columnType());
215 errorIfNotEqual(
"t_basic column " + i +
" javaType", expectedTBasicJavaTypes[i], metadata[i].javaType());
216 errorIfNotEqual(
"t_basic column " + i +
" maximumLength", expectedTBasicMaximumLengths[i], metadata[i].maximumLength());
217 errorIfNotEqual(
"t_basic column " + i +
" charsetName", expectedTBasicCharsetNames [i], metadata[i].charsetName());
218 errorIfNotEqual(
"t_basic column " + i +
" number", expectedTBasicNumbers[i], metadata[i].number());
219 errorIfNotEqual(
"t_basic column " + i +
" isPrimaryKey", expectedTBasicIsPrimaryKeys[i], metadata[i].isPrimaryKey());
220 errorIfNotEqual(
"t_basic column " + i +
" isPartitionKey", expectedTBasicIsPartitionKeys[i], metadata[i].isPartitionKey());
221 errorIfNotEqual(
"t_basic column " + i +
" precision", expectedTBasicPrecisions[i], metadata[i].precision());
222 errorIfNotEqual(
"t_basic column " + i +
" scale", expectedTBasicScales[i], metadata[i].scale());
223 errorIfNotEqual(
"t_basic column " + i +
" nullable", expectedTBasicNullables[i], metadata[i].nullable());