My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes    
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
*
*/
package org.openiaml.model.tests.codegen.model0_4;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.openiaml.model.tests.codegen.DatabaseCodegenTestCase;

/**
* Test the code generation of domain inheritance.
*
*
* @example ExtendsEdge,DomainType,DomainStore
* {@model DomainType}s can {@model ExtendsEdge extend} other
* {@model DomainType}s to inherit their {@model DomainAttribute attributes}.
*
* @example ExtendsEdge,DomainIterator
* Selecting an {@model DomainIterator instance} of a
* {@model DomainType} which has been {@model ExtendsEdge extended}.
*
* @implementation DomainIteratorDomainObject
* If a {@model DomainIterator} is selecting from a {@model DomainType} which extends
* another {@model DomainType}, the {@model DomainIterator} will contain
* the inherited {@model DomainAttributeInstance attribute instances}.
*
* @example DomainIterator
* Selecting a {@model DomainIterator} with more than one query
* parameter (<code>qualification = :qualification and degree = :degree</code>)
*
* @example DomainIterator
* Selecting a {@model DomainIterator} with attributes across inherited
* {@model DomainType}s.
*
* @implementation DomainIterator,Parameter
* If a {@model DomainIterator} is using a query with more than one parameter,
* and the {@model Parameter}s are not named, the {@model NamedElement name} of the
* data source will be used to match up a query.
*
* @implementation DomainIterator
* A {@model DomainType} which inherits another can be {@model DomainIterator instantiated} by
* the combination of all inherited attributes.
*
* @author jmwright
*
*/
public class DomainInheritance extends DatabaseCodegenTestCase {

@Override
protected void setUp() throws Exception {
super.setUp();
root = loadAndCodegen(DomainInheritance.class);
initialiseDatabase();
}

@Override
protected List<String> getDatabaseInitialisers() {
List<String> s = new ArrayList<String>();
s.add("CREATE TABLE Person (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(64) NOT NULL)");
s.add("INSERT INTO Person (id, name) VALUES (1, 'John')");
s.add("INSERT INTO Person (id, name) VALUES (2, 'Jane')");
s.add("INSERT INTO Person (id, name) VALUES (3, 'Bob')");
s.add("INSERT INTO Person (id, name) VALUES (4, 'Alice')");
s.add("INSERT INTO Person (id, name) VALUES (5, 'Sam')");
s.add("INSERT INTO Person (id, name) VALUES (6, 'Mike')");
s.add("INSERT INTO Person (id, name) VALUES (100, 'Test Name')");
s.add("INSERT INTO Person (id, name) VALUES (101, 'David')");
s.add("INSERT INTO Person (id, name) VALUES (102, 'Sue')");
s.add("INSERT INTO Person (id, name) VALUES (24, 'Tracy')");

s.add("CREATE TABLE Student (generated_primary_key INTEGER PRIMARY KEY AUTOINCREMENT, Person_id INTEGER NOT NULL, enrolled VARCHAR(64) NOT NULL)");
s.add("INSERT INTO Student (generated_primary_key, Person_id, enrolled) VALUES (7, 1, 'today')");
s.add("INSERT INTO Student (generated_primary_key, Person_id, enrolled) VALUES (8, 2, 'yesterday')");
s.add("INSERT INTO Student (generated_primary_key, Person_id, enrolled) VALUES (9, 3, 'Friday')");
s.add("INSERT INTO Student (generated_primary_key, Person_id, enrolled) VALUES (106, 24, 'Thursday')");

s.add("CREATE TABLE Qualified (generated_primary_key INTEGER PRIMARY KEY AUTOINCREMENT, qualification VARCHAR(64) NOT NULL)");
s.add("INSERT INTO Qualified (generated_primary_key, qualification) VALUES (10, 'bsc')");
s.add("INSERT INTO Qualified (generated_primary_key, qualification) VALUES (11, 'ba')");
s.add("INSERT INTO Qualified (generated_primary_key, qualification) VALUES (12, 'binfsci')");
s.add("INSERT INTO Qualified (generated_primary_key, qualification) VALUES (118, 'bsc')");

s.add("CREATE TABLE Teacher (generated_primary_key INTEGER PRIMARY KEY AUTOINCREMENT, Person_id INTEGER NOT NULL, Qualified_generated_primary_key INTEGER NOT NULL, title VARCHAR(64) NOT NULL)");
s.add("INSERT INTO Teacher (generated_primary_key, Person_id, Qualified_generated_primary_key, title) VALUES (15, 4, 10, 'ms')");
s.add("INSERT INTO Teacher (generated_primary_key, Person_id, Qualified_generated_primary_key, title) VALUES (16, 5, 11, 'mr')");
s.add("INSERT INTO Teacher (generated_primary_key, Person_id, Qualified_generated_primary_key, title) VALUES (17, 6, 12, 'mr')");
s.add("INSERT INTO Teacher (generated_primary_key, Person_id, Qualified_generated_primary_key, title) VALUES (107, 24, 118, 'dr')");

s.add("CREATE TABLE Undergraduate (generated_primary_key INTEGER PRIMARY KEY AUTOINCREMENT, Student_generated_primary_key INTEGER NOT NULL, degree VARCHAR(64) NOT NULL)");
s.add("INSERT INTO Undergraduate (generated_primary_key, Student_generated_primary_key, degree) VALUES (18, 8, 'nutrition')");
s.add("INSERT INTO Undergraduate (generated_primary_key, Student_generated_primary_key, degree) VALUES (19, 9, 'music')");

s.add("CREATE TABLE Postgraduate (generated_primary_key INTEGER PRIMARY KEY AUTOINCREMENT, Student_generated_primary_key INTEGER NOT NULL, degree VARCHAR(64) NOT NULL)");
s.add("INSERT INTO Postgraduate (generated_primary_key, Student_generated_primary_key, degree) VALUES (20, 8, 'art')");
s.add("INSERT INTO Postgraduate (generated_primary_key, Student_generated_primary_key, degree) VALUES (21, 106, 'science')");

s.add("CREATE TABLE Doctoral (generated_primary_key INTEGER PRIMARY KEY AUTOINCREMENT, Postgraduate_generated_primary_key INTEGER NOT NULL, Teacher_generated_primary_key INTEGER NOT NULL, thesis_title VARCHAR(64) NOT NULL)");
s.add("INSERT INTO Doctoral (generated_primary_key, Postgraduate_generated_primary_key, Teacher_generated_primary_key, thesis_title) VALUES (119, 21, 107, 'complicated database inheritances')");

return s;
}

/**
* Just visiting the home page should not create a problem.
*
* @throws Exception
*/
public void testHome() throws Exception {
beginAtSitemapThenPage("Home");
assertNoProblem();
}

/**
* Select person: name = 'Test User'.
* This should work without any major problems because it is a
* standard query on the root object of the heirarchy.
*
* @throws Exception
*/
public void testPerson() throws Exception {
beginAtSitemapThenPage("get person");
assertNoProblem();

String name = getLabelIDForText("name");
assertLabeledFieldEquals(name, "Test Name");
}

/**
* Select student: enrolled = 'yesterday'
*
* @throws Exception
*/
public void testStudent() throws Exception {
beginAtSitemapThenPage("get student");
assertNoProblem();

String enrolled = getLabelIDForText("enrolled");
assertLabeledFieldEquals(enrolled, "yesterday");

String name = getLabelIDForText("name");
assertLabeledFieldEquals(name, "Jane");
}

/**
* Select teacher: id = 24
*
* @throws Exception
*/
public void testTeacherById() throws Exception {
beginAtSitemapThenPage("get teacher by id");
assertNoProblem();

String title = getLabelIDForText("title");
assertLabeledFieldEquals(title, "dr");

// derived one way
String qualification = getLabelIDForText("qualification");
assertLabeledFieldEquals(qualification, "bsc");

// derived another way
String name = getLabelIDForText("name");
assertLabeledFieldEquals(name, "Tracy");

// even though this instance is an instance of Doctoral,
// we should not have any labelled fields of the Doctoral fields
assertHasNotLabelIDForText("thesis title");
}

/**
* Select doctoral: qualification = 'bsc' and degree = 'science'
*
* @throws Exception
*/
public void testDoctoral() throws Exception {
beginAtSitemapThenPage("get doctoral");
assertNoProblem();

// it should now be rendered
String thesis = getLabelIDForText("thesis title");
assertLabeledFieldEquals(thesis, "complicated database inheritances");

// derived fields
String title = getLabelIDForText("title", "thesis title");
assertLabeledFieldEquals(title, "dr");

// derived one way
String qualification = getLabelIDForText("qualification");
assertLabeledFieldEquals(qualification, "bsc");

// derived another way
String name = getLabelIDForText("name");
assertLabeledFieldEquals(name, "Tracy");

}

/**
* Check the contents of the database tables, if they were not
* created by {@link #initialiseDatabase()}.
*
* @throws Exception
*/
public void testGeneratedDatabase() throws Exception {
// delete the initialised database created in setUp
IFile database = getProject().getFile( getDatabaseName() );
database.delete(true, new NullProgressMonitor());
assertTrue(getProject().refreshProject().isOK());
assertFalse(database.exists());

// visit the site; this will create the database
try {
beginAtSitemapThenPage("Home");
} catch (Throwable e) {
// ignore
}

// check each SQL table
{
String sql = trimWhitespace(getGeneratedSQL("Person"));
assertContains("name VARCHAR", sql);
assertContains("id INTEGER", sql);
assertNotContains("title VARCHAR", sql);
assertNotContains("generated_primary_key", sql);
}

{
String sql = trimWhitespace(getGeneratedSQL("Student"));
assertContains("enrolled VARCHAR", sql);
assertContains("Person_id INTEGER", sql);
assertContains("generated_primary_key INTEGER", sql);
assertNotContains("name VARCHAR", sql);
}

{
String sql = trimWhitespace(getGeneratedSQL("Doctoral"));
assertContains("thesis_title VARCHAR", sql);
assertContains("Teacher_generated_primary_key INTEGER", sql);
assertContains("generated_primary_key INTEGER", sql);
assertNotContains("name VARCHAR", sql);
assertNotContains("qualification VARCHAR", sql);
assertNotContains("enrolled VARCHAR", sql);
assertNotContains("Person_id INTEGER", sql);
}

}

/**
* Get the SQL used to create the table named tableName.
*
* @param tableName
* @return
* @throws Exception
*/
private String getGeneratedSQL(String tableName) throws Exception {
ResultSet rs = executeQuery("SELECT * FROM sqlite_master WHERE type='table' AND name='" + tableName + "'");
assertTrue("Could not find table '" + tableName + "'", rs.next());
return rs.getString("sql");
}

private String trimWhitespace(String s) {
return s.replaceAll("\\s+", " ");
}

/**
* Assert that the given text does not contains the given needle.
*/
private void assertNotContains(String needle, String text) {
assertFalse("Needle '" + needle + "' found in text '" + text + "'", text.contains(needle));
}

}

Change log

r3072 by soundasleep on Aug 8, 2011   Diff
fixing references to old metamodel
elements across project documentation
Go to: 
Project members, sign in to write a code review

Older revisions

r3068 by soundasleep on Aug 8, 2011   Diff
improving modeldoc documentation
across inference rules, codegen
templates, ecore, and additional
documentation includes
r2383 by soundasleep on Jun 28, 2010   Diff
updating failing test cases
r2154 by soundasleep on May 10, 2010   Diff
removing all explicit definitions of
getDatabaseName(), since 'default.db'
is now used by default (r2152)
All revisions of this file

File info

Size: 10896 bytes, 274 lines
Powered by Google Project Hosting