codemodels-java 0.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,174 @@
1
+ require "test_helper"
2
+
3
+ class TestParsing < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include CodeModels::Java
7
+
8
+ class << self
9
+ #include JavaModel
10
+ end
11
+
12
+ def setup
13
+ @dir = File.dirname(__FILE__)+'/data'
14
+ @example_basic = CodeModels.parse_file(@dir+'/example_basic.java')
15
+ @example_accessors = CodeModels.parse_file(@dir+'/example_accessors.java')
16
+ @reorder_stories_form = CodeModels.parse_file(@dir+'/ReorderStoriesForm.java')
17
+ @metaclass_class = CodeModels::Java::ClassOrInterfaceDeclaration
18
+ end
19
+
20
+ def test_model_contains_class
21
+ assert_not_nil @example_basic.only_child_deep_of_type(@metaclass_class)
22
+ end
23
+
24
+ def test_myclass_has_right_name
25
+ my_class = @example_basic.only_child_deep_of_type(@metaclass_class)
26
+ assert_equal 'MyClass', my_class.name
27
+ end
28
+
29
+ # def test_myclass_has_right_fullname
30
+ # my_class = @example_basic.only_child_deep_of_type(@metaclass_class)
31
+ # assert_equal('it.polito.MyClass',JavaModel.eobject_class_qname(my_class))
32
+ # end
33
+
34
+ # def test_eobject_contains_fullname
35
+ # my_class = @example_basic.only_child_deep_of_type(@metaclass_class)
36
+ # puts "my_class #{my_class.class}"
37
+ # js = CodeModels::Serialization.jsonize_obj(my_class)
38
+ # assert js.has_key? 'attr_fullname'
39
+ # assert_equal('it.polito.MyClass',js['attr_fullname'])
40
+ # end
41
+
42
+ def test_fields_are_found
43
+ c = @example_accessors.only_child_deep_of_type(@metaclass_class)
44
+ fields = c.all_children_deep_of_type(FieldDeclaration)
45
+ assert_equal 2,fields.count
46
+ assert_not_nil fields.find { |f| f.variables[0].id.name == 'myField'}
47
+ assert_not_nil fields.find { |f| f.variables[0].id.name == 'aFlag'}
48
+ end
49
+
50
+ def test_methods_are_found
51
+ c = @example_accessors.only_child_deep_of_type(@metaclass_class)
52
+ methods = c.all_children_deep_of_type(MethodDeclaration)
53
+ assert_equal(4,methods.count)
54
+ assert_not_nil methods.find { |m| m.name == 'getMyField'}
55
+ assert_not_nil methods.find { |m| m.name == 'setMyField'}
56
+ assert_not_nil methods.find { |m| m.name == 'isAFlag'}
57
+ assert_not_nil methods.find { |m| m.name == 'setAFlag'}
58
+ end
59
+
60
+ def get_field(c,field_name)
61
+ c.children_deep_of_type(FieldDeclaration).select { |f| f.variables[0].id.name == field_name }
62
+ end
63
+
64
+ def get_method(c,method_name)
65
+ c.children_deep_of_type(MethodDeclaration).select { |m| m.name == method_name }
66
+ end
67
+
68
+ def test_package
69
+ assert_not_nil @reorder_stories_form.package
70
+
71
+ # package is com.technoetic.xplanner.forms
72
+ assert_class PackageDeclaration, @reorder_stories_form.package
73
+ curr_part = @reorder_stories_form.package.name
74
+ ['forms','xplanner','technoetic'].each do |name|
75
+ assert_class QualifiedNameExpr, curr_part
76
+ assert_equal name, curr_part.name
77
+ curr_part = curr_part.qualifier
78
+ end
79
+ assert_class NameExpr, curr_part
80
+ assert_equal 'com', curr_part.name
81
+ end
82
+
83
+ # TODO check also comments
84
+
85
+ def assert_name_expr(exp,name_expr)
86
+ parts = (exp.split '.').reverse
87
+ first_parts = parts[0...-1]
88
+
89
+ curr_part = name_expr
90
+ parts[0...-1].each do |name|
91
+ assert_class QualifiedNameExpr, curr_part
92
+ assert_equal name, curr_part.name, "qualified name expected to have #{name} here"
93
+ curr_part = curr_part.qualifier
94
+ end
95
+ assert_class NameExpr, curr_part
96
+ assert_equal parts.last, curr_part.name
97
+ end
98
+
99
+ def assert_import_decl(exp,id)
100
+ assert_class ImportDeclaration, id
101
+ assert_name_expr exp, id.name
102
+ end
103
+
104
+ def test_imports
105
+ assert_equal 6,@reorder_stories_form.imports.count
106
+
107
+ assert_import_decl 'java.util.ArrayList', @reorder_stories_form.imports[0]
108
+ assert_import_decl 'java.util.Iterator', @reorder_stories_form.imports[1]
109
+ assert_import_decl 'java.util.List', @reorder_stories_form.imports[2]
110
+ assert_import_decl 'javax.servlet.http.HttpServletRequest', @reorder_stories_form.imports[3]
111
+ assert_import_decl 'org.apache.struts.action.ActionErrors', @reorder_stories_form.imports[4]
112
+ assert_import_decl 'org.apache.struts.action.ActionMapping', @reorder_stories_form.imports[5]
113
+ end
114
+
115
+ def test_class_name_and_extends
116
+ assert_equal 1,@reorder_stories_form.types.count
117
+
118
+ c = @reorder_stories_form.types[0]
119
+ assert_class ClassOrInterfaceDeclaration, c
120
+ assert_equal 'ReorderStoriesForm',c.name
121
+ assert_equal 0, c.implements.count
122
+ assert_equal 1, c.extends.count
123
+ assert_class ClassOrInterfaceType, c.extends[0]
124
+ assert_equal 'AbstractEditorForm', c.extends[0].name
125
+ end
126
+
127
+ def test_parse_equals_method
128
+ code = %q{
129
+ class A {
130
+ public boolean equals(Object obj){
131
+ if(this == obj) return true;
132
+ if((obj == null) || !(obj instanceof MyBean)) return false;
133
+ MyBean other = (MyBean)obj;
134
+ if (this.notSoGoodFieldName==null && !(other.notSoGoodFieldName==null)) return false;
135
+ return this.notSoGoodFieldName.equals(other.notSoGoodFieldName);
136
+ }
137
+ }
138
+ }
139
+ model = CodeModels::Java.parse_code(code)
140
+ m = model.types[0].members[0]
141
+ assert_class ClassMethodDeclaration,m
142
+ assert_class BlockStmt,m.body
143
+
144
+ assert_class IfStmt,m.body.stmts[0]
145
+ assert_class ReturnStmt,m.body.stmts[0].thenStmt
146
+ assert_class BooleanLiteralExpr,m.body.stmts[0].thenStmt.expr
147
+ assert_equal true,m.body.stmts[0].thenStmt.expr.value
148
+
149
+ assert_class IfStmt,m.body.stmts[1]
150
+ assert_class ReturnStmt,m.body.stmts[1].thenStmt
151
+ assert_class BooleanLiteralExpr,m.body.stmts[1].thenStmt.expr
152
+ assert_equal false,m.body.stmts[1].thenStmt.expr.value
153
+
154
+ assert_class IfStmt,m.body.stmts[3]
155
+ assert_class ReturnStmt,m.body.stmts[3].thenStmt
156
+ assert_class BooleanLiteralExpr,m.body.stmts[3].thenStmt.expr
157
+ assert_equal false,m.body.stmts[3].thenStmt.expr.value
158
+ end
159
+
160
+ # def test_getter_not_boolean_is_marked
161
+ # c = @example_accessors.only_child_deep_of_type(@metaclass_class)
162
+
163
+ # field = get_field c, 'myField'
164
+ # getter = get_method c, 'getMyField'
165
+
166
+ # # field must have relation to getter 'getter'
167
+ # assert_equal (getter['id'], field['relnoncont_getter'])
168
+
169
+ # # getter must have relation to field 'forField'
170
+ # assert_equal (true, getter['attr_getter'])
171
+ # assert_equal (field['id'], getter['relnoncont_getterFor'])
172
+ # end
173
+
174
+ end
@@ -0,0 +1,361 @@
1
+ # Here we test that models of Java code contains all the information
2
+ # that they should
3
+
4
+ require 'test_helper'
5
+
6
+ class TestValues < Test::Unit::TestCase
7
+
8
+ include CodeModels
9
+
10
+ def assert_map(exp,map,model)
11
+ assert_equal exp.count,map.count, "Expected to have keys: #{exp.keys}, it has #{map.keys}. Unexpected keys: #{map.keys - exp.keys}, Missing keys: #{exp.keys - map.keys}. Model: #{model.inspect}"
12
+ exp.each do |k,v|
13
+ assert_equal exp[k],map[k], "Expected #{k} to have #{exp[k]} instances, it has #{map[k]}. Keys of the map: #{map.keys}"
14
+ end
15
+ end
16
+
17
+ def assert_code_map_to(code,exp)
18
+ r = CodeModels::Java.parse_code(code)
19
+ map = r.values_map
20
+ assert_map(exp,map,r.to_json)
21
+ end
22
+
23
+ def assert_method_code_map_to(code,exp)
24
+ r = CodeModels::Java.parse_code("class A { #{code} }")
25
+ m = r.types[0].members[0]
26
+ map = m.values_map
27
+ assert_map(exp,map,m.to_json)
28
+ end
29
+
30
+ def test_local_variables
31
+ code = %q{
32
+ class MyClass {
33
+ void m(){
34
+ int a, b;
35
+ String qwerty;
36
+ }
37
+ }
38
+ }
39
+ assert_code_map_to(code, {'Int'=>1, 'MyClass'=> 1, 'm' => 1, 'a' => 1, 'b' => 1, 'qwerty' => 1, 'String' => 1})
40
+ end
41
+
42
+ def test_random_method_1
43
+ code = %q{
44
+ private void gotoTestProject() {
45
+ if (!tester.isTextPresent(tester.getMessage("project.prefix") + " " + testProjectName)) {
46
+ if (!tester.isLinkPresentWithText(testProjectName)) {
47
+ tester.gotoProjectsPage();
48
+ }
49
+ tester.clickLinkWithText(testProjectName);
50
+ }
51
+ clickLinkWithKeyIfPresent("navigation.project");
52
+ }
53
+ }
54
+ assert_method_code_map_to(code,{
55
+ 'gotoTestProject' => 1,
56
+ 'tester' => 5,
57
+ 'isTextPresent' => 1,
58
+ 'getMessage' => 1,
59
+ 'project.prefix' => 1,
60
+ ' ' => 1,
61
+ 'testProjectName' => 3,
62
+ 'isLinkPresentWithText' => 1,
63
+ 'gotoProjectsPage' => 1,
64
+ 'clickLinkWithText' => 1,
65
+ 'clickLinkWithKeyIfPresent' => 1,
66
+ 'navigation.project' => 1 })
67
+ end
68
+
69
+ def test_random_method_2
70
+ code = %q{
71
+ @Override
72
+ protected void initModuleMessageResources(ModuleConfig config) throws ServletException {
73
+ MessageResources messageResource = (MessageResources) WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()).getBean("strutsMessageSource");
74
+ getServletContext().setAttribute(Globals.MESSAGES_KEY, messageResource);
75
+ }
76
+ }
77
+ assert_method_code_map_to(code,{
78
+ 'initModuleMessageResources' => 1,
79
+ 'ModuleConfig' => 1,
80
+ 'config' => 1,
81
+ 'ServletException' => 1,
82
+ 'MessageResources' => 2,
83
+ 'messageResource' => 2,
84
+ 'strutsMessageSource' => 1,
85
+ 'WebApplicationContextUtils' => 1,
86
+ 'getRequiredWebApplicationContext' => 1,
87
+ 'getServletContext' => 2,
88
+ 'getBean' => 1,
89
+ 'setAttribute' => 1,
90
+ 'Globals' => 1,
91
+ 'MESSAGES_KEY' => 1,
92
+ 'Override' => 1 })
93
+ end
94
+
95
+ def test_random_method_3
96
+ code = %q{
97
+ /**
98
+ * Getter for the <code>PROPERTY_STRING_PAGING_FOUND_SOMEITEMS</code> property.
99
+ * @return String
100
+ */
101
+ public String getPagingFoundSomeItems() {
102
+ return getProperty(PROPERTY_STRING_PAGING_FOUND_SOMEITEMS);
103
+ }
104
+ }
105
+ assert_method_code_map_to(code,{
106
+ 'String' => 1,
107
+ 'getPagingFoundSomeItems' => 1,
108
+ 'getProperty' => 1,
109
+ 'PROPERTY_STRING_PAGING_FOUND_SOMEITEMS' => 1,
110
+ #{}"\n\t\t\t * Getter for the <code>PROPERTY_STRING_PAGING_FOUND_SOMEITEMS</code> property.\n\t\t\t * @return String\n\t\t\t " => 1
111
+ })
112
+
113
+ end
114
+
115
+ def test_random_method_4
116
+ code = %q{
117
+ private void setUpQuery(List results) {
118
+ MockQuery query = new MockQuery();
119
+ support.hibernateSession.getNamedQueryReturn = query;
120
+ query.listReturn = results;
121
+ }
122
+ }
123
+ assert_method_code_map_to(code,{
124
+ 'setUpQuery' => 1,
125
+ 'List' => 1,
126
+ 'results' => 2,
127
+ 'MockQuery' => 2,
128
+ 'query' => 3,
129
+ 'support' => 1,
130
+ 'hibernateSession' => 1,
131
+ 'getNamedQueryReturn' => 1,
132
+ 'listReturn' => 1 })
133
+ end
134
+
135
+ def test_random_method_5
136
+ code = %q{
137
+ public Project newProject() throws HibernateException {
138
+ Project project = new Project();
139
+ project.setName("Test project");
140
+ session.save(project);
141
+ project.setName("Test project " + project.getId());
142
+ return project;
143
+ }
144
+ }
145
+ assert_method_code_map_to(code,{
146
+ 'Project' => 3,
147
+ 'newProject' => 1,
148
+ 'HibernateException' => 1,
149
+ 'project' => 6,
150
+ 'setName' => 2,
151
+ 'Test project' => 1,
152
+ 'Test project ' => 1,
153
+ 'session' => 1,
154
+ 'save' => 1,
155
+ 'getId' => 1 })
156
+ end
157
+
158
+ def test_random_method_6
159
+ code = %q{
160
+ void assertFormElementEquals(String formElementName, String expectedValue);
161
+ }
162
+ assert_method_code_map_to(code,{
163
+ 'assertFormElementEquals' => 1,
164
+ 'String' => 2,
165
+ 'formElementName' => 1,
166
+ 'expectedValue' => 1 })
167
+ end
168
+
169
+ def test_random_method_7
170
+ code = %q{
171
+ @Override
172
+ public LobHelper getLobHelper() {
173
+ return null;
174
+ }
175
+ }
176
+ assert_method_code_map_to(code,{
177
+ 'LobHelper' => 1,
178
+ 'getLobHelper' => 1,
179
+ 'Override' => 1 })
180
+ end
181
+
182
+ def test_random_method_8
183
+ code = %q{
184
+ private int getFontSize() {
185
+ return (int) Math.round(getHeight() * 0.91);
186
+ }
187
+ }
188
+ assert_method_code_map_to(code,{
189
+ 'getFontSize' => 1,
190
+ 'Math' => 1,
191
+ 'round' => 1,
192
+ 'getHeight' => 1,
193
+ '0.91' => 1,
194
+ 'Int' => 2 })
195
+ end
196
+
197
+ def test_random_method_9
198
+ code = %q{
199
+ public Collection getCurrentCompletedTasksForPerson(int personId);
200
+ }
201
+ assert_method_code_map_to(code,{
202
+ 'Collection' => 1,
203
+ 'getCurrentCompletedTasksForPerson' => 1,
204
+ 'personId' => 1,
205
+ 'Int' => 1 })
206
+ end
207
+
208
+ def test_random_method_10
209
+ code = %q{
210
+ public Hashtable getTaskEstimatedHoursByDisposition() {
211
+ if (taskDispositionEstimatedHours == null) {
212
+ taskDispositionEstimatedHours = new DispositionAggregator(true) {
213
+
214
+ @Override
215
+ protected double getValue(Task task) {
216
+ return task.getEstimatedHours();
217
+ }
218
+ }.aggregateByGroup();
219
+ }
220
+ return taskDispositionEstimatedHours;
221
+ }
222
+ }
223
+ assert_method_code_map_to(code,{
224
+ 'Hashtable' => 1,
225
+ 'getTaskEstimatedHoursByDisposition' => 1,
226
+ 'taskDispositionEstimatedHours' => 3,
227
+ 'DispositionAggregator' => 1,
228
+ 'getValue' => 1,
229
+ 'Task' => 1,
230
+ 'task' => 2,
231
+ 'getEstimatedHours' => 1,
232
+ 'aggregateByGroup' => 1,
233
+ 'Override' => 1,
234
+ 'Double' => 1,
235
+ true => 1 })
236
+ end
237
+
238
+ def test_an_entire_class
239
+ code = %q{
240
+ class MyBean {
241
+ private String notSoGoodFieldName;
242
+
243
+ public String getNotSoGoodFieldName(){ return notSoGoodFieldName; }
244
+
245
+ public void setNotSoGoodFieldName(String notSoGoodFieldName){ this.notSoGoodFieldName = notSoGoodFieldName; }
246
+
247
+ public String toString(){
248
+ return "MyBean [notSoGoodFieldName: "+notSoGoodFieldName+"]";
249
+ }
250
+
251
+ public int hashCode(){
252
+ return notSoGoodFieldName.hashCode();
253
+ }
254
+
255
+ public boolean equals(Object obj){
256
+ if(this == obj) return true;
257
+ if((obj == null) || !(obj instanceof MyBean)) return false;
258
+ MyBean other = (MyBean)obj;
259
+ if (this.notSoGoodFieldName==null && !(other.notSoGoodFieldName==null)) return false;
260
+ return this.notSoGoodFieldName.equals(other.notSoGoodFieldName);
261
+ }
262
+ }
263
+ }
264
+ assert_code_map_to(code,{
265
+ 'MyBean' => 4,
266
+ 'Int' => 1,
267
+ 'String' => 4,
268
+ 'notSoGoodFieldName' => 11,
269
+ 'getNotSoGoodFieldName' => 1,
270
+ 'setNotSoGoodFieldName' => 1,
271
+ 'MyBean [notSoGoodFieldName: ' => 1,
272
+ ']' => 1,
273
+ 'hashCode' => 2,
274
+ 'toString' => 1,
275
+ 'Boolean' => 1,
276
+ 'equals' => 2,
277
+ 'Object' => 1,
278
+ 'obj' => 5,
279
+ true => 1,
280
+ false => 2,
281
+ 'other' => 3 })
282
+ end
283
+
284
+ def test_an_entire_class_field_1
285
+ code = %q{
286
+ private String notSoGoodFieldName;
287
+ }
288
+ assert_method_code_map_to(code,{
289
+ 'String' => 1,
290
+ 'notSoGoodFieldName' => 1 })
291
+ end
292
+
293
+ def test_an_entire_class_method_1
294
+ code = %q{
295
+ public String getNotSoGoodFieldName(){ return notSoGoodFieldName; }
296
+ }
297
+ assert_method_code_map_to(code,{
298
+ 'String' => 1,
299
+ 'notSoGoodFieldName' => 1,
300
+ 'getNotSoGoodFieldName' => 1 })
301
+ end
302
+
303
+ def test_an_entire_class_method_2
304
+ code = %q{
305
+ public void setNotSoGoodFieldName(String notSoGoodFieldName){ this.notSoGoodFieldName = notSoGoodFieldName; }
306
+ }
307
+ assert_method_code_map_to(code,{
308
+ 'String' => 1,
309
+ 'notSoGoodFieldName' => 3,
310
+ 'setNotSoGoodFieldName' => 1 })
311
+ end
312
+
313
+ def test_an_entire_class_method_3
314
+ code = %q{
315
+ public String toString(){
316
+ return "MyBean [notSoGoodFieldName: "+notSoGoodFieldName+"]";
317
+ }
318
+ }
319
+ assert_method_code_map_to(code,{
320
+ 'String' => 1,
321
+ 'notSoGoodFieldName' => 1,
322
+ 'MyBean [notSoGoodFieldName: ' => 1,
323
+ ']' => 1,
324
+ 'toString' => 1 })
325
+ end
326
+
327
+ def test_an_entire_class_method_4
328
+ code = %q{
329
+ public int hashCode(){
330
+ return notSoGoodFieldName.hashCode();
331
+ }
332
+ }
333
+ assert_method_code_map_to(code,{
334
+ 'Int' => 1,
335
+ 'notSoGoodFieldName' => 1,
336
+ 'hashCode' => 2 })
337
+ end
338
+
339
+ def test_an_entire_class_method_5
340
+ code = %q{
341
+ public boolean equals(Object obj){
342
+ if(this == obj) return true;
343
+ if((obj == null) || !(obj instanceof MyBean)) return false;
344
+ MyBean other = (MyBean)obj;
345
+ if (this.notSoGoodFieldName==null && !(other.notSoGoodFieldName==null)) return false;
346
+ return this.notSoGoodFieldName.equals(other.notSoGoodFieldName);
347
+ }
348
+ }
349
+ assert_method_code_map_to(code,{
350
+ 'MyBean' => 3,
351
+ 'notSoGoodFieldName' => 4,
352
+ 'Boolean' => 1,
353
+ 'equals' => 2,
354
+ 'Object' => 1,
355
+ 'obj' => 5,
356
+ true => 1,
357
+ false => 2,
358
+ 'other' => 3 })
359
+ end
360
+
361
+ end