1
2
3
4
5
6
7 package test.org.asyrinx.joey.gen.command.rdb2java.standard;
8
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import junit.framework.TestCase;
14
15 import org.asyrinx.joey.gen.command.rdb2java.standard.BasicBuilder;
16 import org.asyrinx.joey.gen.command.rdb2java.standard.JavaOptions;
17 import org.asyrinx.joey.gen.model.EnumerationEntry;
18 import org.asyrinx.joey.gen.model.java.AppDomain;
19 import org.asyrinx.joey.gen.model.java.Entity;
20 import org.asyrinx.joey.gen.model.java.EntityKey;
21 import org.asyrinx.joey.gen.model.java.EntityKeyType;
22 import org.asyrinx.joey.gen.model.java.JavaEnumeration;
23 import org.asyrinx.joey.gen.model.java.Parameter;
24 import org.asyrinx.joey.gen.model.java.Property;
25 import org.asyrinx.joey.gen.model.java.Reference;
26 import org.asyrinx.joey.gen.model.java.classes.JavaLangClass;
27 import org.asyrinx.joey.gen.model.java.classes.JavaUtilClass;
28 import org.asyrinx.joey.gen.model.java.classes.JoeyRuntimeClass;
29 import org.asyrinx.joey.gen.model.java.classes.PrimitiveType;
30 import org.asyrinx.joey.gen.model.java.classes.PrimitiveWrapper;
31 import org.asyrinx.joey.gen.model.rdb.Column;
32 import org.asyrinx.joey.gen.model.rdb.Database;
33 import org.asyrinx.joey.gen.model.rdb.Databases;
34 import org.asyrinx.joey.gen.model.rdb.Index;
35 import org.asyrinx.joey.gen.model.rdb.IndexEntry;
36 import org.asyrinx.joey.gen.model.rdb.RdbEnumeration;
37 import org.asyrinx.joey.gen.model.rdb.Table;
38 import org.asyrinx.joey.gen.model.rdb.TablePattern;
39 import org.asyrinx.joey.gen.model.rdb.Unique;
40
41 /***
42 * @author akima
43 */
44 public class BasicBuilderTest extends TestCase {
45
46 public static void main(String[] args) {
47 junit.swingui.TestRunner.run(BasicBuilderTest.class);
48 }
49
50 public void testExecute() {
51
52 final Databases databases = new Databases();
53 final Database db1 = new Database(databases, "db1");
54
55 db1.getOptions().put("category", "package1");
56
57 final RdbEnumeration e_party_type = new RdbEnumeration(db1, "PartyType");
58 e_party_type.setValueType("Integer");
59 e_party_type.setLabel("パーティ型");
60 new EnumerationEntry(e_party_type, "1", "individual", "個人");
61 new EnumerationEntry(e_party_type, "2", "corporation", "法人");
62
63 final Table t_party = new Table(db1, "party", "パーティ");
64 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
65 new Column(t_party, "name", "VARCHAR", "20", true, false, null);
66 new TablePattern(t_party, "logical_deletable", "");
67 new TablePattern(t_party, "created_date", "");
68 new TablePattern(t_party, "updated_date", "");
69 final Column col_party_type = new Column(t_party, "party_type", "PartyType", "0", true,
70 false, "corporation");
71 col_party_type.setEnum("PartyType");
72
73 final BasicBuilder builder = new BasicBuilder();
74 final Map props = new HashMap();
75 props.put("joey-gen.proj.base.package", "org.asyrinx.joey.sample");
76 builder.setProperties(props);
77 final AppDomain domain = builder.execute(databases);
78
79 assertEquals(1, domain.getClasses().size());
80 assertEquals(1, domain.getEnumerations().size());
81
82 final JavaEnumeration je_party_type = domain.getEnumerations().getEnumeration(0);
83 assertEquals("PartyType", je_party_type.getName());
84 assertEquals("パーティ型", je_party_type.getLabel());
85 assertEquals(PrimitiveWrapper.INTEGER, je_party_type.getValueTypeObj());
86 assertEquals(2, je_party_type.size());
87 assertEquals("1", je_party_type.getEntry(0).getValue());
88 assertEquals("individual", je_party_type.getEntry(0).getName());
89 assertEquals("個人", je_party_type.getEntry(0).getLabel());
90 assertEquals("2", je_party_type.getEntry(1).getValue());
91 assertEquals("corporation", je_party_type.getEntry(1).getName());
92 assertEquals("法人", je_party_type.getEntry(1).getLabel());
93
94 final Entity c_party = domain.getClasses().getJavaType(0);
95 assertEquals("Party", c_party.getName());
96 assertEquals("org.asyrinx.joey.sample.om.package1.entity", c_party.getPackage());
97 assertEquals("org.asyrinx.joey.sample.om.package1.entity.Party", c_party.getFqn());
98 assertEquals("パーティ", c_party.getLabel());
99 assertEquals(6, c_party.getProperties().size());
100 final Property p_party_id = c_party.getProperties().getProperty(0);
101 assertEquals("partyId", p_party_id.getName());
102 assertEquals("long", p_party_id.getTypeName());
103 assertEquals(PrimitiveType.LONG, p_party_id.getType());
104 assertEquals(0, p_party_id.getMaxLength());
105 assertEquals(true, p_party_id.isRequired());
106 assertEquals("0", p_party_id.getDefaultValue());
107 final Property p_name = c_party.getProperties().getProperty(1);
108 assertEquals("name", p_name.getName());
109 assertEquals("String", p_name.getTypeName());
110 assertEquals(JavaLangClass.STRING, p_name.getType());
111 assertEquals(20, p_name.getMaxLength());
112 assertEquals(true, p_name.isRequired());
113 assertEquals("null", p_name.getDefaultValue());
114 final Property p_party_type = c_party.getProperties().getProperty(2);
115 assertEquals("partyType", p_party_type.getName());
116 assertEquals("PartyType", p_party_type.getTypeName());
117 assertEquals(je_party_type, p_party_type.getType());
118 assertEquals(0, p_party_type.getMaxLength());
119 assertEquals(true, p_party_type.isRequired());
120 assertEquals("corporation", p_party_type.getDefaultValue());
121 final Property p_party_deleted = c_party.getProperties().getProperty(3);
122 assertEquals("deleted", p_party_deleted.getName());
123 assertEquals("int", p_party_deleted.getTypeName());
124 assertEquals(PrimitiveType.INT, p_party_deleted.getType());
125 assertEquals(0, p_party_deleted.getMaxLength());
126 assertEquals(true, p_party_deleted.isRequired());
127 assertEquals("0", p_party_deleted.getDefaultValue());
128 final Property p_party_created = c_party.getProperties().getProperty(4);
129 assertEquals("createdDate", p_party_created.getName());
130 assertEquals("Date", p_party_created.getTypeName());
131 assertEquals(JavaUtilClass.DATE, p_party_created.getType());
132 assertEquals(0, p_party_created.getMaxLength());
133 assertEquals(true, p_party_created.isRequired());
134 assertEquals("null", p_party_created.getDefaultValue());
135 final Property p_party_updated = c_party.getProperties().getProperty(5);
136 assertEquals("updatedDate", p_party_updated.getName());
137 assertEquals("Date", p_party_updated.getTypeName());
138 assertEquals(JavaUtilClass.DATE, p_party_updated.getType());
139 assertEquals(0, p_party_updated.getMaxLength());
140 assertEquals(true, p_party_updated.isRequired());
141 assertEquals("null", p_party_updated.getDefaultValue());
142
143 assertEquals("partyTypeEnum", p_party_type.getEnumPropertyName());
144 assertEquals(je_party_type, p_party_type.getEnumType());
145 }
146
147 public void testKeys() {
148 final Databases databases = new Databases();
149 final Database db1 = new Database(databases, "db1");
150 db1.getOptions().put("javaPackage", "org.asyrinx.joey.sample");
151
152 final Table t_party = new Table(db1, "party", "パーティ");
153 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
154 new Column(t_party, "name", "VARCHAR", "20", true, false, null);
155
156 final Table t_role = new Table(db1, "role", "ロール");
157 new Column(t_role, "role_id", "BIGINT", "0", true, true, "0");
158 final Column colPartyId = new Column(t_role, "party_id", "BIGINT", "0", true, false, "0");
159 colPartyId.setFk("party.party_id");
160
161 final Table t_user = new Table(db1, "system_user", "システムユーザ");
162 t_user.setExtends("role");
163 new Column(t_user, "system_user_id", "BIGINT", "0", true, true, "0");
164 new Column(t_user, "values", "VARCHAR", "20", true, false, null);
165
166 final BasicBuilder builder = new BasicBuilder();
167 final Map props = new HashMap();
168 props.put("joey-gen.proj.base.package", "org.asyrinx.joey.sample");
169 builder.setProperties(props);
170 final AppDomain domain = builder.execute(databases);
171
172 assertEquals(1, t_role.getForeignKeys().size());
173
174 assertEquals(3, domain.getClasses().size());
175 assertEquals(0, domain.getEnumerations().size());
176
177 final Entity c_party = domain.getClasses().getJavaType(0);
178 assertEquals("Party", c_party.getName());
179 assertEquals("org.asyrinx.joey.sample.om.db1.entity", c_party.getPackage());
180 assertEquals("org.asyrinx.joey.sample.om.db1.entity.Party", c_party.getFqn());
181 assertEquals("パーティ", c_party.getLabel());
182 assertEquals(2, c_party.getProperties().size());
183 final Property p_party_id = c_party.getProperties().getProperty(0);
184 assertEquals("partyId", p_party_id.getName());
185 assertEquals("long", p_party_id.getTypeName());
186 assertEquals(PrimitiveType.LONG, p_party_id.getType());
187 assertEquals(0, p_party_id.getMaxLength());
188 assertEquals(true, p_party_id.isRequired());
189 assertEquals("0", p_party_id.getDefaultValue());
190 final Property p_name = c_party.getProperties().getProperty(1);
191 assertEquals("name", p_name.getName());
192 assertEquals("String", p_name.getTypeName());
193 assertEquals(JavaLangClass.STRING, p_name.getType());
194 assertEquals(20, p_name.getMaxLength());
195 assertEquals(true, p_name.isRequired());
196 assertEquals("null", p_name.getDefaultValue());
197 final EntityKey pk_party = c_party.getPrimaryKey();
198 assertEquals(1, pk_party.size());
199 assertEquals(p_party_id, pk_party.getEntry(0).getProperty());
200
201
202 final Entity c_role = domain.getClasses().getJavaType(1);
203 assertEquals("Role", c_role.getName());
204 assertEquals("org.asyrinx.joey.sample.om.db1.entity", c_role.getPackage());
205 assertEquals("org.asyrinx.joey.sample.om.db1.entity.Role", c_role.getFqn());
206 assertEquals("ロール", c_role.getLabel());
207 assertEquals(2, c_role.getProperties().size());
208 final Property p_role_id = c_role.getProperties().getProperty(0);
209 assertEquals("roleId", p_role_id.getName());
210 assertEquals("long", p_role_id.getTypeName());
211 assertEquals(PrimitiveType.LONG, p_role_id.getType());
212 assertEquals(0, p_role_id.getMaxLength());
213 assertEquals(true, p_role_id.isRequired());
214 assertEquals("0", p_role_id.getDefaultValue());
215 final Property p_role_party_id = c_role.getProperties().getProperty(1);
216 assertEquals("partyId", p_role_party_id.getName());
217 assertEquals("long", p_role_party_id.getTypeName());
218 assertEquals(PrimitiveType.LONG, p_role_party_id.getType());
219 assertEquals(0, p_role_party_id.getMaxLength());
220 assertEquals(true, p_role_party_id.isRequired());
221 assertEquals("0", p_role_party_id.getDefaultValue());
222
223 final List role_allProperties = c_role.getPropertiesAll();
224 assertEquals(2, role_allProperties.size());
225 assertEquals(p_role_id, role_allProperties.get(0));
226 assertEquals(p_role_party_id, role_allProperties.get(1));
227
228 final Entity c_system_user = domain.getClasses().getJavaType(2);
229 assertEquals("SystemUser", c_system_user.getName());
230 assertEquals("org.asyrinx.joey.sample.om.db1.entity", c_system_user.getPackage());
231 assertEquals("org.asyrinx.joey.sample.om.db1.entity.SystemUser", c_system_user.getFqn());
232 assertEquals("システムユーザ", c_system_user.getLabel());
233 assertEquals(3, c_system_user.getProperties().size());
234 assertEquals(c_role, c_system_user.getSuperClass());
235 final Property p_system_user_id = c_system_user.getProperties().getProperty(0);
236 assertEquals("systemUserId", p_system_user_id.getName());
237 assertEquals("long", p_system_user_id.getTypeName());
238 assertEquals(PrimitiveType.LONG, p_system_user_id.getType());
239 assertEquals(0, p_system_user_id.getMaxLength());
240 assertEquals(true, p_system_user_id.isRequired());
241 assertEquals("0", p_system_user_id.getDefaultValue());
242 final Property p_values = c_system_user.getProperties().getProperty(1);
243 assertEquals("values", p_values.getName());
244 assertEquals("String", p_values.getTypeName());
245 assertEquals(JavaLangClass.STRING, p_values.getType());
246 assertEquals(20, p_values.getMaxLength());
247 assertEquals(true, p_values.isRequired());
248 assertEquals("null", p_values.getDefaultValue());
249
250
251
252
253
254
255
256
257
258
259 final List systemUser_allProperties = c_system_user.getPropertiesAll();
260 assertEquals(5, systemUser_allProperties.size());
261 assertEquals(p_role_id, systemUser_allProperties.get(0));
262 assertEquals(p_role_party_id, systemUser_allProperties.get(1));
263 assertEquals(p_system_user_id, systemUser_allProperties.get(2));
264 assertEquals(p_values, systemUser_allProperties.get(3));
265
266 assertEquals(1, c_role.getReferences().size());
267 final Reference ref_role_to_party = c_role.getReferences().getReference(0);
268 assertEquals("Party", ref_role_to_party.getReferenceClassName());
269 assertEquals(c_party, ref_role_to_party.getReferenceClass());
270 assertEquals(p_role_party_id, ref_role_to_party.getEntry(0).getLocal());
271 assertEquals(p_party_id, ref_role_to_party.getEntry(0).getForeign());
272 assertEquals(1, p_role_party_id.getReferencesContainedAsLocal().size());
273 assertEquals(0, p_role_party_id.getReferencesContainedAsForeign().size());
274 assertEquals(0, p_party_id.getReferencesContainedAsLocal().size());
275 assertEquals(1, p_party_id.getReferencesContainedAsForeign().size());
276
277 assertEquals("party", ref_role_to_party.getPropertyNameInLocal());
278 assertEquals("roles", ref_role_to_party.getPropertyNameInReferred());
279 assertEquals("role", ref_role_to_party.getPropertyNameInReferred(false));
280
281 assertEquals(1, c_party.getReferreds().size());
282 assertEquals(ref_role_to_party, c_party.getReferreds().get(0));
283 assertEquals(0, c_role.getReferreds().size());
284 }
285
286 public void testReference() {
287 final Databases databases = new Databases();
288 final Database db1 = new Database(databases, "db1");
289 db1.getOptions().put("javaPackage", "org.asyrinx.joey.sample");
290
291 final Table t_party = new Table(db1, "party", "パーティ");
292 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
293 new Column(t_party, "name", "VARCHAR", "20", true, false, null);
294
295 final Table t_relation = new Table(db1, "relation", "関係");
296 new Column(t_relation, "relation_id", "BIGINT", "0", true, true, "0");
297 final Column colOriginPartyId = new Column(t_relation, "origin_party_id", "BIGINT", "0",
298 true, false, "0");
299 final Column colDestPartyId = new Column(t_relation, "dest_party_id", "BIGINT", "0", true,
300 false, "0");
301 colOriginPartyId.setFk("party.party_id");
302 colDestPartyId.setFk("party.party_id");
303
304 final BasicBuilder builder = new BasicBuilder();
305 final Map props = new HashMap();
306 props.put("joey-gen.proj.base.package", "org.asyrinx.joey.sample");
307 builder.setProperties(props);
308 final AppDomain domain = builder.execute(databases);
309
310 assertEquals(2, t_relation.getForeignKeys().size());
311
312 assertEquals(2, domain.getClasses().size());
313
314 final Entity c_party = domain.getClasses().getJavaType(0);
315 assertEquals("Party", c_party.getName());
316 assertEquals("org.asyrinx.joey.sample.om.db1.entity", c_party.getPackage());
317 assertEquals("org.asyrinx.joey.sample.om.db1.entity.Party", c_party.getFqn());
318 assertEquals("パーティ", c_party.getLabel());
319 assertEquals(2, c_party.getProperties().size());
320 final Property p_party_id = c_party.getProperties().getProperty(0);
321 assertEquals("partyId", p_party_id.getName());
322 assertEquals("long", p_party_id.getTypeName());
323 assertEquals(PrimitiveType.LONG, p_party_id.getType());
324 assertEquals(0, p_party_id.getMaxLength());
325 assertEquals(true, p_party_id.isRequired());
326 assertEquals("0", p_party_id.getDefaultValue());
327 final Property p_name = c_party.getProperties().getProperty(1);
328 assertEquals("name", p_name.getName());
329 assertEquals("String", p_name.getTypeName());
330 assertEquals(JavaLangClass.STRING, p_name.getType());
331 assertEquals(20, p_name.getMaxLength());
332 assertEquals(true, p_name.isRequired());
333 assertEquals("null", p_name.getDefaultValue());
334 final EntityKey pk_party = c_party.getPrimaryKey();
335 assertEquals(1, pk_party.size());
336 assertEquals(p_party_id, pk_party.getEntry(0).getProperty());
337
338 final Entity c_relation = domain.getClasses().getJavaType(1);
339 assertEquals("Relation", c_relation.getName());
340 assertEquals("org.asyrinx.joey.sample.om.db1.entity", c_relation.getPackage());
341 assertEquals("org.asyrinx.joey.sample.om.db1.entity.Relation", c_relation.getFqn());
342 assertEquals("関係", c_relation.getLabel());
343 assertEquals(3, c_relation.getProperties().size());
344 final Property p_role_id = c_relation.getProperties().getProperty(0);
345 assertEquals("relationId", p_role_id.getName());
346 assertEquals("long", p_role_id.getTypeName());
347 assertEquals(PrimitiveType.LONG, p_role_id.getType());
348 assertEquals(0, p_role_id.getMaxLength());
349 assertEquals(true, p_role_id.isRequired());
350 assertEquals("0", p_role_id.getDefaultValue());
351 final Property p_origin_party_id = c_relation.getProperties().getProperty(1);
352 assertEquals("originPartyId", p_origin_party_id.getName());
353 assertEquals("long", p_origin_party_id.getTypeName());
354 assertEquals(PrimitiveType.LONG, p_origin_party_id.getType());
355 assertEquals(0, p_origin_party_id.getMaxLength());
356 assertEquals(true, p_origin_party_id.isRequired());
357 assertEquals("0", p_origin_party_id.getDefaultValue());
358 final Property p_dest_party_id = c_relation.getProperties().getProperty(2);
359 assertEquals("destPartyId", p_dest_party_id.getName());
360 assertEquals("long", p_dest_party_id.getTypeName());
361 assertEquals(PrimitiveType.LONG, p_dest_party_id.getType());
362 assertEquals(0, p_dest_party_id.getMaxLength());
363 assertEquals(true, p_dest_party_id.isRequired());
364 assertEquals("0", p_dest_party_id.getDefaultValue());
365
366 assertEquals(2, c_relation.getReferences().size());
367 assertEquals(1, p_origin_party_id.getReferencesContainedAsLocal().size());
368 assertEquals(0, p_origin_party_id.getReferencesContainedAsForeign().size());
369 assertEquals(0, p_party_id.getReferencesContainedAsLocal().size());
370 assertEquals(2, p_party_id.getReferencesContainedAsForeign().size());
371 final Reference ref_origin_to_party = c_relation.getReferences().getReference(0);
372 assertEquals("Party", ref_origin_to_party.getReferenceClassName());
373 assertEquals(c_party, ref_origin_to_party.getReferenceClass());
374 assertEquals(p_origin_party_id, ref_origin_to_party.getEntry(0).getLocal());
375 assertEquals(p_party_id, ref_origin_to_party.getEntry(0).getForeign());
376 assertEquals("partyRelatedByOriginPartyId", ref_origin_to_party.getPropertyNameInLocal());
377 assertEquals("relationsRelatedByOriginPartyId", ref_origin_to_party
378 .getPropertyNameInReferred());
379 assertEquals("relationRelatedByOriginPartyId", ref_origin_to_party
380 .getPropertyNameInReferred(false));
381
382 final Reference ref_dest_to_party = c_relation.getReferences().getReference(1);
383 assertEquals("Party", ref_dest_to_party.getReferenceClassName());
384 assertEquals(c_party, ref_dest_to_party.getReferenceClass());
385 assertEquals(p_dest_party_id, ref_dest_to_party.getEntry(0).getLocal());
386 assertEquals(p_party_id, ref_dest_to_party.getEntry(0).getForeign());
387 assertEquals("partyRelatedByDestPartyId", ref_dest_to_party.getPropertyNameInLocal());
388 assertEquals("relationsRelatedByDestPartyId", ref_dest_to_party.getPropertyNameInReferred());
389 assertEquals("relationRelatedByDestPartyId", ref_dest_to_party
390 .getPropertyNameInReferred(false));
391
392 assertEquals(1, c_relation.getReferenceEntities().size());
393 assertEquals(c_party, c_relation.getReferenceEntities().iterator().next());
394
395 assertEquals(2, c_party.getReferreds().size());
396 assertEquals(ref_origin_to_party, c_party.getReferreds().get(0));
397 assertEquals(ref_dest_to_party, c_party.getReferreds().get(1));
398 assertEquals(0, c_relation.getReferreds().size());
399 }
400
401 public void testPackaging() {
402
403 final Databases databases = new Databases();
404 final Database db1 = new Database(databases, "db1");
405
406
407 db1.getOptions().put(JavaOptions.CATEGORY, "party");
408 final Table t_party = new Table(db1, "party", "パーティ");
409 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
410
411 final Database db1_ = new Database(databases, "db1");
412
413
414 db1_.getOptions().put(JavaOptions.CATEGORY, "report");
415 final Table t_report = new Table(db1_, "report", "報告");
416 final Column col_reporter_id = new Column(t_report, "reporter_id", "BIGINT", "0", true,
417 true, "0");
418 col_reporter_id.setFk("party.party_id");
419 final Table t_notify = new Table(db1_, "notify", "通知");
420 new Column(t_notify, "notify_id", "BIGINT", "0", true, true);
421
422 final BasicBuilder builder = new BasicBuilder();
423 final Map props = new HashMap();
424 props.put("joey-gen.proj.base.package", "org.asyrinx.joey.sample");
425 builder.setProperties(props);
426 final AppDomain domain = builder.execute(databases);
427
428 assertEquals(1, databases.getDatabases().size());
429 assertEquals(db1, databases.getDatabases().getDatabase(0));
430
431 assertEquals(3, domain.getClasses().size());
432 final Entity ety_party = domain.getClasses().getJavaType(0);
433 final Entity ety_report = domain.getClasses().getJavaType(1);
434 final Entity ety_notify = domain.getClasses().getJavaType(2);
435 assertEquals("Party", ety_party.getName());
436 assertEquals("Report", ety_report.getName());
437 assertEquals("Notify", ety_notify.getName());
438
439 assertEquals("org.asyrinx.joey.sample.om.party.entity", ety_party.getPackageName());
440 assertEquals("org.asyrinx.joey.sample.om.report.entity", ety_report.getPackageName());
441 assertEquals("org.asyrinx.joey.sample.om.report.entity", ety_notify.getPackageName());
442
443 assertEquals("org.asyrinx.joey.sample.om.party.dao", ety_party.getPackage("dao"));
444 assertEquals("org.asyrinx.joey.sample.om.report.dao", ety_report.getPackage("dao"));
445 assertEquals("org.asyrinx.joey.sample.om.report.dao", ety_notify.getPackage("dao"));
446
447 assertEquals(1, ety_party.getImports().size());
448 assertEquals("org.asyrinx.joey.sample.om.report.entity.Report", ety_party.getImports()
449 .iterator().next());
450 assertEquals(1, ety_report.getImports().size());
451 assertEquals("org.asyrinx.joey.sample.om.party.entity.Party", ety_report.getImports()
452 .iterator().next());
453 }
454
455 public void testConstructorParams() {
456 final Databases databases = new Databases();
457 final Database db1 = new Database(databases, "db1");
458
459 final Table t_party = new Table(db1, "party", "パーティ");
460 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
461 new Column(t_party, "name", "VARCHAR", "20", true, false, null);
462
463 final Table t_role = new Table(db1, "role", "ロール");
464 new Column(t_role, "role_id", "BIGINT", "0", true, true, "0");
465 final Column colPartyId = new Column(t_role, "party_id", "BIGINT", "0", true, false, "0");
466 colPartyId.setFk("party.party_id");
467
468 final Table t_user = new Table(db1, "system_user", "システムユーザ");
469 t_user.setExtends("role");
470 new Column(t_user, "values", "VARCHAR", "20", true, false, null);
471
472 final BasicBuilder builder = new BasicBuilder();
473 final AppDomain domain = builder.execute(databases);
474
475 assertEquals(3, domain.getClasses().size());
476 final Entity e_party = domain.getClasses().getJavaType("Party");
477 final Entity e_role = domain.getClasses().getJavaType("Role");
478 final Entity e_user = domain.getClasses().getJavaType("SystemUser");
479 assertNotNull(e_party);
480 assertNotNull(e_role);
481 assertNotNull(e_user);
482 {
483 final List e_party_params = e_party.getConstructorParams();
484 assertEquals(2, e_party_params.size());
485 assertEquals(Property.class, e_party_params.get(0).getClass());
486 assertEquals(Property.class, e_party_params.get(1).getClass());
487 final Parameter e_party_params_id = (Parameter) e_party_params.get(0);
488 assertEquals("partyId", e_party_params_id.getParamName());
489 assertEquals("long", e_party_params_id.getParamType().getName());
490 final Parameter e_party_params_name = (Parameter) e_party_params.get(1);
491 assertEquals("name", e_party_params_name.getParamName());
492 assertEquals("String", e_party_params_name.getParamType().getName());
493 }
494 {
495 final List e_role_params = e_role.getConstructorParams();
496 assertEquals(2, e_role_params.size());
497 assertEquals(Property.class, e_role_params.get(0).getClass());
498 assertEquals(Reference.class, e_role_params.get(1).getClass());
499 final Parameter e_party_params_id = (Parameter) e_role_params.get(0);
500 assertEquals("roleId", e_party_params_id.getParamName());
501 assertEquals("long", e_party_params_id.getParamType().getName());
502 final Parameter e_party_params_party = (Parameter) e_role_params.get(1);
503 assertEquals("party", e_party_params_party.getParamName());
504 assertEquals("Party", e_party_params_party.getParamType().getName());
505 }
506 {
507 final List e_user_params = e_user.getConstructorParams();
508 assertEquals(3, e_user_params.size());
509 assertEquals(Property.class, e_user_params.get(0).getClass());
510 assertEquals(Reference.class, e_user_params.get(1).getClass());
511 assertEquals(Property.class, e_user_params.get(2).getClass());
512 final Parameter e_party_params_id = (Parameter) e_user_params.get(0);
513 assertEquals("roleId", e_party_params_id.getParamName());
514 assertEquals("long", e_party_params_id.getParamType().getName());
515 final Parameter e_party_params_party = (Parameter) e_user_params.get(1);
516 assertEquals("party", e_party_params_party.getParamName());
517 assertEquals("Party", e_party_params_party.getParamType().getName());
518 final Parameter e_party_params_name = (Parameter) e_user_params.get(2);
519 assertEquals("values", e_party_params_name.getParamName());
520 assertEquals("String", e_party_params_name.getParamType().getName());
521 }
522 }
523
524 public void testReferencePropertyName() {
525 final Databases databases = new Databases();
526 final Database db1 = new Database(databases, "db1");
527 db1.getOptions().put("javaPackage", "org.asyrinx.joey.sample");
528
529 final Table t_party = new Table(db1, "party", "パーティ");
530 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
531 new Column(t_party, "name", "VARCHAR", "20", true, false, null);
532
533 final Table t_relation_type = new Table(db1, "relation_type", "関係型");
534 new Column(t_relation_type, "relation_type_id", "BIGINT", "0", true, true, "0");
535 new Column(t_relation_type, "name", "VARCHAR", "20", true, false, null);
536
537 final Table t_relation_type_structure = new Table(db1, "relation_type_structure", "関係型構造");
538 new Column(t_relation_type_structure, "relation_type_structure_id", "BIGINT", "0", true,
539 true, "0");
540 final Column colOriginRtId = new Column(t_relation_type_structure,
541 "origin_relation_type_id", "BIGINT", "0", true, false, "0");
542 final Column colDestRtId = new Column(t_relation_type_structure, "dest_relation_type_id",
543 "BIGINT", "0", true, false, "0");
544 colOriginRtId.setFk("relation_type.relation_type_id");
545 colDestRtId.setFk("relation_type.relation_type_id");
546
547 final Table t_relation = new Table(db1, "relation", "関係");
548 new Column(t_relation, "relation_id", "BIGINT", "0", true, true, "0");
549 final Column colOriginPartyId = new Column(t_relation, "origin_party_id", "BIGINT", "0",
550 true, false, "0");
551 final Column colDestPartyId = new Column(t_relation, "dest_party_id", "BIGINT", "0", true,
552 false, "0");
553 final Column colRelationType = new Column(t_relation, "relation_type_id", "BIGINT", "0",
554 true, false, "0");
555 colOriginPartyId.setFk("party.party_id");
556 colDestPartyId.setFk("party.party_id");
557 colRelationType.setFk("relation_type.relation_type_id");
558
559 final BasicBuilder builder = new BasicBuilder();
560 final AppDomain domain = builder.execute(databases);
561
562 assertEquals(4, domain.getClasses().size());
563
564 final Entity c_party = domain.getClasses().getJavaType("Party");
565 final Entity c_relation_type = domain.getClasses().getJavaType("RelationType");
566 final Entity c_relation_type_struc = domain.getClasses().getJavaType(
567 "RelationTypeStructure");
568 final Entity c_relation = domain.getClasses().getJavaType("Relation");
569
570 final Property p_party_id = c_party.getProperties().getProperty("partyId");
571 final Property p_relation_type_id = c_relation_type.getProperties().getProperty(
572 "relationTypeId");
573
574 final Property p_origin_rt_id = c_relation_type_struc.getProperties().getProperty(
575 "originRelationTypeId");
576 final Property p_dest_rt_id = c_relation_type_struc.getProperties().getProperty(
577 "destRelationTypeId");
578
579 final Reference ref_origin_to_rt = c_relation_type_struc.getReferences().getReference(0);
580 assertEquals("RelationType", ref_origin_to_rt.getReferenceClassName());
581 assertEquals(c_relation_type, ref_origin_to_rt.getReferenceClass());
582 assertEquals(p_origin_rt_id, ref_origin_to_rt.getEntry(0).getLocal());
583 assertEquals(p_relation_type_id, ref_origin_to_rt.getEntry(0).getForeign());
584 assertEquals("relationTypeRelatedByOriginRelationTypeId", ref_origin_to_rt
585 .getPropertyNameInLocal());
586 assertEquals("relationTypeStructuresRelatedByOriginRelationTypeId", ref_origin_to_rt
587 .getPropertyNameInReferred());
588 assertEquals("relationTypeStructureRelatedByOriginRelationTypeId", ref_origin_to_rt
589 .getPropertyNameInReferred(false));
590
591 final Reference ref_dest_to_rt = c_relation_type_struc.getReferences().getReference(1);
592 assertEquals("RelationType", ref_dest_to_rt.getReferenceClassName());
593 assertEquals(c_relation_type, ref_dest_to_rt.getReferenceClass());
594 assertEquals(p_dest_rt_id, ref_dest_to_rt.getEntry(0).getLocal());
595 assertEquals(p_relation_type_id, ref_dest_to_rt.getEntry(0).getForeign());
596 assertEquals("relationTypeRelatedByDestRelationTypeId", ref_dest_to_rt
597 .getPropertyNameInLocal());
598 assertEquals("relationTypeStructuresRelatedByDestRelationTypeId", ref_dest_to_rt
599 .getPropertyNameInReferred());
600 assertEquals("relationTypeStructureRelatedByDestRelationTypeId", ref_dest_to_rt
601 .getPropertyNameInReferred(false));
602
603 final Property p_role_id = c_relation.getProperties().getProperty(0);
604 final Property p_origin_party_id = c_relation.getProperties().getProperty(1);
605 final Property p_dest_party_id = c_relation.getProperties().getProperty(2);
606 final Property p_relation_type_id2 = c_relation.getProperties().getProperty(3);
607
608 final Reference ref_origin_to_party = c_relation.getReferences().getReference(0);
609 assertEquals("Party", ref_origin_to_party.getReferenceClassName());
610 assertEquals(c_party, ref_origin_to_party.getReferenceClass());
611 assertEquals(p_origin_party_id, ref_origin_to_party.getEntry(0).getLocal());
612 assertEquals(p_party_id, ref_origin_to_party.getEntry(0).getForeign());
613 assertEquals("partyRelatedByOriginPartyId", ref_origin_to_party.getPropertyNameInLocal());
614 assertEquals("relationsRelatedByOriginPartyId", ref_origin_to_party
615 .getPropertyNameInReferred());
616 assertEquals("relationRelatedByOriginPartyId", ref_origin_to_party
617 .getPropertyNameInReferred(false));
618
619 final Reference ref_dest_to_party = c_relation.getReferences().getReference(1);
620 assertEquals("Party", ref_dest_to_party.getReferenceClassName());
621 assertEquals(c_party, ref_dest_to_party.getReferenceClass());
622 assertEquals(p_dest_party_id, ref_dest_to_party.getEntry(0).getLocal());
623 assertEquals(p_party_id, ref_dest_to_party.getEntry(0).getForeign());
624 assertEquals("partyRelatedByDestPartyId", ref_dest_to_party.getPropertyNameInLocal());
625 assertEquals("relationsRelatedByDestPartyId", ref_dest_to_party.getPropertyNameInReferred());
626 assertEquals("relationRelatedByDestPartyId", ref_dest_to_party
627 .getPropertyNameInReferred(false));
628
629 final Reference ref_relation_type = c_relation.getReferences().getReference(2);
630 assertEquals("RelationType", ref_relation_type.getReferenceClassName());
631 assertEquals(c_relation_type, ref_relation_type.getReferenceClass());
632 assertEquals(p_relation_type_id2, ref_relation_type.getEntry(0).getLocal());
633 assertEquals(p_relation_type_id, ref_relation_type.getEntry(0).getForeign());
634 assertEquals("relationType", ref_relation_type.getPropertyNameInLocal());
635 assertEquals("relations", ref_relation_type.getPropertyNameInReferred());
636 assertEquals("relation", ref_relation_type.getPropertyNameInReferred(false));
637
638 assertEquals(2, c_party.getReferreds().size());
639 assertEquals(ref_origin_to_party, c_party.getReferreds().get(0));
640 assertEquals(ref_dest_to_party, c_party.getReferreds().get(1));
641 assertEquals(0, c_relation.getReferreds().size());
642 }
643
644 public void testExtendedClassesAndReference() {
645 final Databases databases = new Databases();
646 final Database db1 = new Database(databases, "db1");
647 db1.getOptions().put("javaPackage", "org.asyrinx.joey.sample");
648
649 final Table t_component = new Table(db1, "component", "コンポーネント");
650 new Column(t_component, "component_id", "BIGINT", "0", true, true, "0");
651 new Column(t_component, "name", "VARCHAR", "20", true, false, null);
652
653 final Table t_composite = new Table(db1, "composite", "コンポジット");
654 t_composite.setExtends("component");
655 new Column(t_composite, "dummy", "VARCHAR", "20", true, false, null);
656
657 final Table t_structure = new Table(db1, "structure", "構造");
658 new Column(t_structure, "structure_id", "BIGINT", "0", true, true, "0");
659 final Column compositeId = new Column(t_structure, "composite_id", "BIGINT", "0", true,
660 false, "0");
661 final Column componentId = new Column(t_structure, "component_id", "BIGINT", "0", true,
662 false, "0");
663 compositeId.setFk("composite.component_id");
664 componentId.setFk("component.component_id");
665
666 final BasicBuilder builder = new BasicBuilder();
667 final AppDomain domain = builder.execute(databases);
668
669 final Entity e_component = domain.getClasses().getJavaType("Component");
670 final Entity e_composite = domain.getClasses().getJavaType("Composite");
671 final Entity e_structure = domain.getClasses().getJavaType("Structure");
672
673 final Reference reference1 = e_structure.getReferences().getReference(0);
674 assertEquals(e_composite, reference1.getReferenceClass());
675 assertEquals("composite", reference1.getPropertyNameInLocal());
676
677
678
679
680 assertEquals("structures", reference1.getPropertyNameInReferred());
681 assertEquals("structure", reference1.getPropertyNameInReferred(false));
682
683 final Reference reference2 = e_structure.getReferences().getReference(1);
684 assertEquals(e_component, reference2.getReferenceClass());
685 assertEquals("component", reference2.getPropertyNameInLocal());
686
687
688
689
690 assertEquals("structuresRelatedByComponentId", reference2.getPropertyNameInReferred());
691 assertEquals("structureRelatedByComponentId", reference2.getPropertyNameInReferred(false));
692 }
693
694 public void testExtendedClassesAndReference2() {
695 final Databases databases = new Databases();
696 final Database db1 = new Database(databases, "db1");
697 db1.getOptions().put("javaPackage", "org.asyrinx.joey.sample");
698
699 final Table t_party = new Table(db1, "party", "パーティ");
700 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
701 new Column(t_party, "name", "VARCHAR", "20", true, false, null);
702
703 final Table t_action = new Table(db1, "action", "アクション");
704 new Column(t_action, "action_id", "BIGINT", "0", true, true, "0");
705 final Column c_partyId = new Column(t_action, "target_party_id", "BIGINT", "0", true,
706 false, "0");
707 c_partyId.setFk("party.party_id");
708 new Column(t_action, "descriptions", "VARCHAR", "100", true, false, null);
709
710 final Table t_action_ex = new Table(db1, "action_ex", "アクションEx");
711 t_action_ex.setExtends("action");
712 final Column c_reporter_id = new Column(t_action_ex, "reporter_id", "BIGINT", "0", true,
713 false, "0");
714 c_reporter_id.setFk("party.party_id");
715
716 final BasicBuilder builder = new BasicBuilder();
717 final AppDomain domain = builder.execute(databases);
718
719 final Entity e_party = domain.getClasses().getJavaType("Party");
720 final Entity e_action = domain.getClasses().getJavaType("Action");
721 final Entity e_action_ex = domain.getClasses().getJavaType("ActionEx");
722
723 final Reference reference1 = e_action.getReferences().getReference(0);
724 assertEquals(e_party, reference1.getReferenceClass());
725 assertEquals("party", reference1.getPropertyNameInLocal());
726 assertEquals("actions", reference1.getPropertyNameInReferred());
727 assertEquals("action", reference1.getPropertyNameInReferred(false));
728
729 final Reference reference2 = e_action_ex.getReferences().getReference(0);
730 assertEquals(e_party, reference2.getReferenceClass());
731 assertEquals("partyRelatedByReporterId", reference2.getPropertyNameInLocal());
732 assertEquals("actionExs", reference2.getPropertyNameInReferred());
733 assertEquals("actionEx", reference2.getPropertyNameInReferred(false));
734 }
735
736 public void testIndexAndUnique() {
737 final Databases databases = new Databases();
738 final Database db1 = new Database(databases, "db1");
739 db1.getOptions().put("javaPackage", "org.asyrinx.joey.sample");
740
741 final Table t_party = new Table(db1, "party", "パーティ");
742 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
743 final Column col_party_code = new Column(t_party, "party_code", "VARCHAR", "20", true,
744 false, null);
745 col_party_code.setUnique(true);
746 final Column col_party_name = new Column(t_party, "name", "VARCHAR", "20", true, false,
747 null);
748 col_party_name.setIndexed(true);
749 final Index index0 = new Index(t_party);
750 new IndexEntry(index0, "party_code");
751 new IndexEntry(index0, "name");
752
753 final BasicBuilder builder = new BasicBuilder();
754 final AppDomain domain = builder.execute(databases);
755
756 assertEquals(1, t_party.getUniques().size());
757 final Unique unique = (Unique) t_party.getUniques().getIndex(0);
758 assertEquals(1, unique.size());
759 assertEquals(col_party_code, unique.getEntry(0).getColumn());
760
761 assertEquals(2, t_party.getIndexes().size());
762 assertEquals(index0, t_party.getIndexes().getIndex(0));
763 final Index index1 = t_party.getIndexes().getIndex(1);
764 assertEquals(1, index1.size());
765 assertEquals(col_party_name, index1.getEntry(0).getColumn());
766
767 assertEquals(1, domain.getClasses().size());
768 final Entity ety_party = domain.getClasses().getJavaType(0);
769 assertEquals(4, ety_party.getKeys().size());
770 final EntityKey key0 = ety_party.getKeys().getKey(0);
771 final EntityKey key1 = ety_party.getKeys().getKey(1);
772 final EntityKey key2 = ety_party.getKeys().getKey(2);
773 final EntityKey key3 = ety_party.getKeys().getKey(3);
774
775 assertEquals(ety_party, key0.getParent());
776 assertEquals(ety_party, key1.getParent());
777 assertEquals(ety_party, key2.getParent());
778 assertEquals(ety_party, key3.getParent());
779
780 assertEquals(EntityKeyType.PK, key0.getKeyType());
781 assertEquals(1, key0.size());
782 assertEquals("partyId", key0.getEntry(0).getName());
783 assertEquals("primaryKey", key0.getName());
784
785 assertEquals(EntityKeyType.INDEX, key1.getKeyType());
786 assertEquals(2, key1.size());
787 assertEquals("partyCode", key1.getEntry(0).getName());
788 assertEquals("name", key1.getEntry(1).getName());
789 assertEquals("keyPartyCodeName", key1.getName());
790
791 assertEquals(EntityKeyType.INDEX, key2.getKeyType());
792 assertEquals(1, key2.size());
793 assertEquals("name", key2.getEntry(0).getName());
794 assertEquals("keyName", key2.getName());
795
796 assertEquals(EntityKeyType.UNIQUE, key3.getKeyType());
797 assertEquals(1, key3.size());
798 assertEquals("partyCode", key3.getEntry(0).getName());
799 assertEquals("keyPartyCode", key3.getName());
800 }
801
802 public void testBooleanEnum() {
803 final Databases databases = new Databases();
804 final Database db1 = new Database(databases, "db1");
805 db1.getOptions().put("javaPackage", "org.asyrinx.joey.sample");
806
807 final Table t_party = new Table(db1, "party", "パーティ");
808 new Column(t_party, "party_id", "BIGINT", "0", true, true, "0");
809 new Column(t_party, "name", "VARCHAR", "20", true, false, null);
810 final Column col_deleted = new Column(t_party, "deleted", "INTEGER", "20", true, false,
811 null);
812 col_deleted.setEnum("BooleanEnum");
813
814 final BasicBuilder builder = new BasicBuilder();
815 final Map props = new HashMap();
816 props.put("joey-gen.proj.base.package", "org.asyrinx.joey.sample");
817 builder.setProperties(props);
818 final AppDomain domain = builder.execute(databases);
819
820 assertEquals(1, domain.getClasses().size());
821 final Entity ety_party = domain.getClasses().getJavaType(0);
822 assertEquals(3, ety_party.getProperties().size());
823 final Property prop_deleted = ety_party.getProperties().getProperty(2);
824 assertEquals("deleted", prop_deleted.getName());
825 assertEquals(JoeyRuntimeClass.JAVA_BOOLEAN_ENUM, prop_deleted.getEnumType());
826 }
827
828 }