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,24 @@
1
+ package it.polito;
2
+
3
+ class MyClass {
4
+
5
+ private int myField;
6
+ private boolean aFlag;
7
+
8
+ public int getMyField(){
9
+ return myField;
10
+ }
11
+
12
+ public void setMyField(int value){
13
+ myField = value;
14
+ }
15
+
16
+ public boolean isAFlag(){
17
+ return aFlag;
18
+ }
19
+
20
+ public void setAFlag(boolean value){
21
+ aFlag = value;
22
+ }
23
+
24
+ }
@@ -0,0 +1,5 @@
1
+ package it.polito;
2
+
3
+ class MyClass {
4
+
5
+ }
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+
3
+ class TestCorrespondences < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include CodeModels
7
+ include CodeModels::Java
8
+
9
+ J = CodeModels::Java
10
+
11
+ def test_correspondance_of_field
12
+ code = "class A extends B { int fieldB; int getB(){ return fieldB; } }"
13
+ nt = J.node_tree_from_code(code)
14
+ mt = J.parse_code(code)
15
+ model_field = mt.types[0].members[0]
16
+ node_field = nt.types[0].members[0]
17
+ assert_equal node_field, J.corresponding_node(model_field,nt)
18
+ assert_equal "int fieldB;", J.corresponding_node(model_field,nt).to_s
19
+ end
20
+
21
+ def test_corresponding_node_from_code
22
+ code = "class A extends B { int fieldB; int getB(){ return fieldB; } }"
23
+ nt = J.node_tree_from_code(code)
24
+ mt = J.parse_code(code)
25
+ model_field = mt.types[0].members[0]
26
+ node_field = nt.types[0].members[0]
27
+ assert_equal node_field, J.corresponding_node_from_code(model_field,code)
28
+ assert_equal "int fieldB;", J.corresponding_node_from_code(model_field,code).to_s
29
+ end
30
+
31
+ end
@@ -0,0 +1,57 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/test/"
4
+ end
5
+
6
+ require 'codemodels/java'
7
+ require 'json'
8
+ require 'test/unit'
9
+
10
+ include CodeModels
11
+
12
+ module TestHelper
13
+
14
+ def assert_class(expected_class,node)
15
+ assert node.class==expected_class, "Node expected to have class #{expected_class} instead it has class #{node.class}"
16
+ end
17
+
18
+ def relative_path(path)
19
+ File.join(File.dirname(__FILE__),path)
20
+ end
21
+
22
+ def read_test_data(filename)
23
+ dir = File.dirname(__FILE__)
24
+ dir = File.join(dir,'data')
25
+ path = File.join(dir,filename)
26
+ IO.read(path)
27
+ end
28
+
29
+ def assert_all_attrs(expected,c)
30
+ actual = c.ecore.eAllAttributes
31
+ assert_equal expected.count,actual.count,"Expected #{expected.count} attrs, found #{actual.count}"
32
+ expected.each do |e|
33
+ assert actual.find {|a| a.name==e}, "Attribute #{e} not found"
34
+ end
35
+ end
36
+
37
+ def assert_all_refs(expected,c)
38
+ actual = c.ecore.eAllReferences
39
+ assert_equal expected.count,actual.count,"Expected #{expected.count} refs, found #{actual.count}"
40
+ expected.each do |e|
41
+ assert actual.find {|a| a.name==e}, "Reference #{e} not found"
42
+ end
43
+ end
44
+
45
+ def assert_ref(c,name,type,many=false)
46
+ ref = c.ecore.eAllReferences.find {|r| r.name==name}
47
+ assert_equal type.ecore.name,ref.eType.name
48
+ assert_equal many, ref.many
49
+ end
50
+
51
+ def assert_attr(c,name,type,many=false)
52
+ att = c.ecore.eAllAttributes.find {|a| a.name==name}
53
+ assert_equal type.name,att.eType.name
54
+ assert_equal many, att.many
55
+ end
56
+
57
+ end
@@ -0,0 +1,258 @@
1
+ require 'test_helper'
2
+
3
+ class TestInfoExtraction < Test::Unit::TestCase
4
+
5
+ include CodeModels
6
+ include CodeModels::Java
7
+ include TestHelper
8
+
9
+ def setup
10
+ @actionbuttonscomumntag_model_node = CodeModels::Java.parse_code(read_test_data('ActionButtonsColumnTag.java'))
11
+ @csvexporter_model_node = CodeModels::Java.parse_code(read_test_data('CsvExporter.java'))
12
+ @authconstraint_model_node = CodeModels::Java.parse_code(read_test_data('AuthConstraint.java'))
13
+ @testiteration_model_node = CodeModels::Java.parse_code(read_test_data('TestIteration.java'))
14
+ end
15
+
16
+ def test_camel_to_words_single_word_upcase
17
+ assert_equal ['CIAO'],InfoExtraction.camel_to_words('CIAO')
18
+ end
19
+
20
+ def test_camel_to_words_single_word_downcase
21
+ assert_equal ['ciao'],InfoExtraction.camel_to_words('ciao')
22
+ end
23
+
24
+ def test_camel_to_words_proper_starting_down
25
+ assert_equal ['ciao','Bello'],InfoExtraction.camel_to_words('ciaoBello')
26
+ end
27
+
28
+ def test_camel_to_words_proper_starting_up
29
+ assert_equal ['Ciao','Bello'],InfoExtraction.camel_to_words('CiaoBello')
30
+ end
31
+
32
+ def test_camel_to_words_with_numbers
33
+ assert_equal ['test','1'],InfoExtraction.camel_to_words('test1')
34
+ end
35
+
36
+ def test_camel_to_words_upcase_numbers
37
+ assert_equal ['TEST','1'],InfoExtraction.camel_to_words('TEST1')
38
+ end
39
+
40
+ def test_camel_to_words_with_uppercase_word
41
+ assert_equal ['Ciao','BELLO','Come','Va'],InfoExtraction.camel_to_words('CiaoBELLOComeVa')
42
+ end
43
+
44
+ def test_camel_to_words_empty
45
+ assert_equal [''],InfoExtraction.camel_to_words('')
46
+ end
47
+
48
+ def assert_map_equal(exp,act,model=nil)
49
+ fail "Unexpected keys #{act.keys-exp.keys}. Actual map: #{act}" if (act.keys-exp.keys).count > 0
50
+ fail "Missing keys #{exp.keys-act.keys}. Actual map: #{act}" if (exp.keys-act.keys).count > 0
51
+ exp.each do |k,exp_v|
52
+ fail "For #{k} expected #{exp_v}, found #{act[k]}, model=#{model}" if act[k]!=exp_v
53
+ end
54
+ end
55
+
56
+ def test_proper_terms_extraction_on_bean
57
+ code = %q{
58
+ class MyBean {
59
+ private String notSoGoodFieldName;
60
+
61
+ public String getNotSoGoodFieldName(){ return notSoGoodFieldName; }
62
+
63
+ public void setNotSoGoodFieldName(String notSoGoodFieldName){ this.notSoGoodFieldName = notSoGoodFieldName; }
64
+
65
+ public String toString(){
66
+ return "MyBean [notSoGoodFieldName: "+notSoGoodFieldName+"]";
67
+ }
68
+
69
+ public int hashCode(){
70
+ return notSoGoodFieldName.hashCode();
71
+ }
72
+
73
+ public boolean equals(Object obj){
74
+ if(this == obj) return true;
75
+ if((obj == null) || !(obj instanceof MyBean)) return false;
76
+ MyBean other = (MyBean)obj;
77
+ if (this.notSoGoodFieldName==null && !(other.notSoGoodFieldName==null)) return false;
78
+ return this.notSoGoodFieldName.equals(other.notSoGoodFieldName);
79
+ }
80
+ }
81
+ }
82
+ exp_terms = {
83
+ 'mybean' => 4,
84
+ 'int' => 1,
85
+ 'string' => 5,
86
+ 'notsogoodfieldname' => 13,
87
+ 'get' => 1,
88
+ 'set' => 1,
89
+ 'MyBean [notSoGoodFieldName:' => 1,
90
+ ']' => 1,
91
+ 'hashcode' => 2,
92
+ 'to' => 1,
93
+ 'boolean' => 1,
94
+ 'equals' => 2,
95
+ 'object' => 1,
96
+ 'obj' => 5,
97
+ 'true' => 1,
98
+ 'false' => 2,
99
+ 'other' => 3
100
+ }
101
+ model_node = CodeModels::Java.parse_code(code)
102
+ terms_map = model_node.terms_map
103
+ assert_map_equal exp_terms,terms_map,model_node.to_json
104
+ end
105
+
106
+ def test_actionbutonscomumntag_sanity_check
107
+ assert_class ClassOrInterfaceDeclaration, @actionbuttonscomumntag_model_node.types[0]
108
+ assert_equal 14, @actionbuttonscomumntag_model_node.types[0].members.count
109
+ end
110
+
111
+ def test_info_extraction_actionbuttonscomumntag_method_1
112
+ m = @actionbuttonscomumntag_model_node.types[0].members[2]
113
+ assert_equal 'setActionButtonsTag', m.name
114
+ assert_map_equal({'actionbuttonstag'=>5, 'set'=>1}, m.terms_map)
115
+ end
116
+
117
+ def test_info_extraction_actionbuttonscomumntag_method_2
118
+ m = @actionbuttonscomumntag_model_node.types[0].members[3]
119
+ assert_equal 'ActionButtonsColumnTag', m.name
120
+ # media could be breaken, it is border line...
121
+ # action it is used in separate contexts so sometimes it is broken
122
+ assert_map_equal({'set'=>1, 'media'=>1,
123
+ 'mediatypeenum'=>1, 'html'=>1,'get'=>1,'name'=>1,
124
+ 'action' => 1,
125
+ 'actionbuttons' => 1,
126
+ 'column' => 1,
127
+ 'tag' => 1,
128
+ 'actionbuttonstag'=>3,'showonly'=>1,
129
+ 'withicon' => 1}, m.terms_map)
130
+ end
131
+
132
+ def test_info_extraction_actionbuttonscomumntag_method_3
133
+ m = @actionbuttonscomumntag_model_node.types[0].members[4]
134
+ assert_equal 'setPageContext', m.name
135
+ assert_map_equal({'set'=>3, 'pagecontext'=>4,
136
+ 'context'=>3, 'actionbuttonstag'=>1}, m.terms_map)
137
+ end
138
+
139
+ def test_info_extraction_actionbuttonscomumntag_method_4
140
+ m = @actionbuttonscomumntag_model_node.types[0].members[5]
141
+ assert_equal 'setId', m.name
142
+ assert_map_equal({'set'=>2, 'id'=>3, 'string'=>1,
143
+ 's'=>3,'actionbuttonstag'=>1}, m.terms_map)
144
+ end
145
+
146
+ def test_info_extraction_actionbuttonscomumntag_method_5
147
+ m = @actionbuttonscomumntag_model_node.types[0].members[6]
148
+ assert_equal 'getName', m.name
149
+ assert_map_equal({'get'=>2, 'name'=>2, 'string'=>1,
150
+ 'actionbuttonstag'=>1}, m.terms_map)
151
+ end
152
+
153
+ def test_info_extraction_actionbuttonscomumntag_method_6
154
+ m = @actionbuttonscomumntag_model_node.types[0].members[7]
155
+ assert_equal 'setName', m.name
156
+ assert_map_equal({'set'=>2, 'name'=>4, 'string'=>1,
157
+ 'actionbuttonstag'=>1}, m.terms_map)
158
+ end
159
+
160
+ def test_info_extraction_actionbuttonscomumntag_method_7
161
+ m = @actionbuttonscomumntag_model_node.types[0].members[8]
162
+ assert_equal 'getScope', m.name
163
+ assert_map_equal({'get'=>2, 'scope'=>2, 'string'=>1,
164
+ 'actionbuttonstag'=>1}, m.terms_map)
165
+ end
166
+
167
+ def test_info_extraction_actionbuttonscomumntag_method_8
168
+ m = @actionbuttonscomumntag_model_node.types[0].members[9]
169
+ assert_equal 'setScope', m.name
170
+ assert_map_equal({'set'=>2, 'scope'=>4, 'string'=>1,
171
+ 'actionbuttonstag'=>1}, m.terms_map)
172
+ end
173
+
174
+ def test_info_extraction_actionbuttonscomumntag_method_9
175
+ m = @actionbuttonscomumntag_model_node.types[0].members[10]
176
+ assert_equal 'doStartTag', m.name
177
+ assert_map_equal({'int'=>2, 'do'=>3, 'start'=>3,
178
+ 'tag'=>7,
179
+ 'actionbuttonstag'=>1, 'jspexception'=>2,
180
+ 'writable'=>3, 'parent'=>1,'parenttable'=>2,
181
+ 'get'=>3,'is'=>1,'SKIP_BODY'=>2,'constants'=>2,
182
+ 'ATTRIBUTE_NOWRAP'=>2,'true'=>1,'attributemap'=>2,
183
+ 'containskey'=>1,'put'=>1,
184
+ 'status'=>3,'exception'=>1,'e'=>2}, m.terms_map)
185
+ end
186
+
187
+ def test_info_extraction_actionbuttonscomumntag_method_10
188
+ m = @actionbuttonscomumntag_model_node.types[0].members[11]
189
+ assert_equal 'doAfterBody', m.name
190
+ assert_map_equal({'int'=>1, 'do'=>2, 'afterbody'=>2,
191
+ 'actionbuttonstag'=>1, 'jspexception'=>1}, m.terms_map)
192
+ end
193
+
194
+ def test_info_extraction_actionbuttonscomumntag_method_11
195
+ m = @actionbuttonscomumntag_model_node.types[0].members[12]
196
+ assert_equal 'doEndTag', m.name
197
+ assert_map_equal({'int'=>1, 'do'=>3, 'end'=>3,
198
+ 'actionbuttonstag'=>1, 'jspexception'=>2,
199
+ 'writable'=>3, 'tag'=>5,'is'=>1,
200
+ 'parent'=>1,'parenttable'=>2,
201
+ 'SKIP_BODY'=>1,'exception'=>1,'e'=>2, 'get'=>1}, m.terms_map)
202
+ end
203
+
204
+ def test_info_extraction_actionbuttonscomumntag_method_12
205
+ m = @actionbuttonscomumntag_model_node.types[0].members[13]
206
+ assert_equal 'release', m.name
207
+ assert_map_equal({'release'=>2,
208
+ 'actionbuttonstag'=>1}, m.terms_map)
209
+ end
210
+
211
+ def test_info_extraction_csvexporter_1
212
+ m = @csvexporter_model_node.types[0].members[4]
213
+ assert_equal 'getFileExtension', m.name
214
+ # there is only a getter so it does not recognize it as separate
215
+ assert_map_equal({'getfileextension'=>1,'string'=>1,'csv'=>1}, m.terms_map)
216
+ end
217
+
218
+ def test_info_extraction_csvexporter_2
219
+ m = @csvexporter_model_node.types[0].members[5]
220
+ assert_equal 'export', m.name
221
+ assert_map_equal({
222
+ 'byte'=>1,
223
+ 'bytearray'=>1,
224
+ 'bytearrayoutputstream'=>2,
225
+ 'session'=>2,'object'=>2,
226
+ 'data'=>2,'to'=>1,
227
+ 'export'=>2,'exception'=>1,
228
+ 'override'=>1}, m.terms_map)
229
+ end
230
+
231
+ def test_info_extraction_authconstraint_1
232
+ m = @authconstraint_model_node.types[0].members[1]
233
+ assert_equal 'addRoleName', m.name
234
+ assert_map_equal({'add'=>2,'role'=>3,'name'=>3,'string'=>1,'rolenames'=>1}, m.terms_map)
235
+ end
236
+
237
+ def test_info_extraction_authconstraint_2
238
+ m = @authconstraint_model_node.types[0].members[2]
239
+ assert_equal 'getRoleNames', m.name
240
+ assert_map_equal({'collection'=>1,'string'=>1,'get'=>1,'rolenames'=>2}, m.terms_map)
241
+ end
242
+
243
+ def test_info_extraction_testiteration_1
244
+ m = @testiteration_model_node.types[0].members[4]
245
+ assert_equal 'testGetRemainingHours', m.name
246
+ assert_map_equal({'test'=>1,'get'=>2,'remaining'=>2,'assertequals'=>1,
247
+ '0.0d'=>1,'iteration'=>1,'task'=>1,'0.0'=>1,'exception'=>1,'hours'=>2}, m.terms_map)
248
+ end
249
+
250
+ def test_info_extraction_testiteration_2
251
+ m = @testiteration_model_node.types[0].members[10]
252
+ assert_equal 'testGetAddedHours', m.name
253
+ assert_map_equal({'test'=>2,'get'=>2,'added'=>2,'hours'=>2,'assertequals'=>1,'iteration'=>4,
254
+ 'estimated'=>1,'tasks'=>1,
255
+ '5.0d'=>1,'0.0'=>1,'exception'=>1,'set'=>1,'up'=>1,'of'=>1}, m.terms_map)
256
+ end
257
+
258
+ end
@@ -0,0 +1,133 @@
1
+ require 'test_helper'
2
+ require 'rgen/ecore/ecore'
3
+
4
+ class TestMetamodel < Test::Unit::TestCase
5
+
6
+ RGenString = RGen::ECore::EString
7
+ RGenBoolean = RGen::ECore::EBoolean
8
+ RGenInt = RGen::ECore::EInt
9
+
10
+ def get_relation(rgen_class,rel_name,type=nil,multiplicity=nil)
11
+ type = get_metaclass(type) if type
12
+ rel = rgen_class.ecore.eAllReferences.find {|r| r.name==rel_name}
13
+ if rel and type
14
+ raise "Wrong type: expected #{type.ecore}, found: #{rel.eType}" unless type.ecore===rel.eType
15
+ end
16
+ if rel and multiplicity
17
+ case multiplicity
18
+ when :many
19
+ raise "Wrong type: expected many, found single" unless rel.many
20
+ when :single
21
+ raise "Wrong type: expected single, found many" if rel.many
22
+ else
23
+ raise "Illegal multiplicity: #{multiplicity}"
24
+ end
25
+ end
26
+ rel
27
+ end
28
+
29
+ def get_attr(rgen_class,name,type=nil,multiplicity=nil)
30
+ att = rgen_class.ecore.eAllAttributes.find {|a| a.name==name}
31
+ if att and type
32
+ raise "Wrong type: expected #{type}, found: #{att.eType}" unless type===att.eType
33
+ end
34
+ if att and multiplicity
35
+ case multiplicity
36
+ when :many
37
+ raise "Wrong type: expected many, found single" unless att.many
38
+ when :single
39
+ raise "Wrong type: expected single, found many" if att.many
40
+ else
41
+ raise "Illegal multiplicity: #{multiplicity}"
42
+ end
43
+ end
44
+ att
45
+ end
46
+
47
+ def assert_extend(rgen_class,super_class)
48
+ assert rgen_class.superclass == get_metaclass(super_class)
49
+ end
50
+
51
+ def get_metaclass(name)
52
+ CodeModels::Java.const_get(name)
53
+ end
54
+
55
+ def test_comment_unit_exist
56
+ assert get_metaclass('Comment')
57
+ end
58
+
59
+ def test_comment_has_attr_content
60
+ c = get_metaclass('Comment')
61
+ assert get_attr(c,'content',RGenString)
62
+ end
63
+
64
+ def test_name_expr_exist
65
+ assert get_metaclass('NameExpr')
66
+ end
67
+
68
+ def test_name_expr_has_attr_name
69
+ c = get_metaclass('NameExpr')
70
+ assert get_attr(c,'name',RGenString)
71
+ end
72
+
73
+ def test_compilatioun_unit_exist
74
+ assert get_metaclass('CompilationUnit')
75
+ end
76
+
77
+ def test_compilatioun_unit_has_relations
78
+ cu = get_metaclass('CompilationUnit')
79
+ #assert get_relation(cu,'comments')
80
+ assert get_relation(cu,'imports')
81
+ assert get_relation(cu,'package')
82
+ assert get_relation(cu,'types')
83
+ end
84
+
85
+ def test_import_declaration_has_attr_asterisk
86
+ c = get_metaclass('ImportDeclaration')
87
+ assert get_attr(c,'asterisk',RGenBoolean)
88
+ end
89
+
90
+ def test_import_declaration_has_rel_name
91
+ c = get_metaclass('ImportDeclaration')
92
+ assert get_relation(c,'name','NameExpr')
93
+ end
94
+
95
+ def test_type_declaration
96
+ c = get_metaclass('TypeDeclaration')
97
+ assert get_relation(c,'members','BodyDeclaration',:many)
98
+ #assert get_attr(c,'modifiers',RGenInt,:single) #ignored!
99
+ assert get_attr(c,'name',RGenString,:single)
100
+ end
101
+
102
+ def test_body_declaration
103
+ c = get_metaclass('BodyDeclaration')
104
+ #assert get_relation(c,'javaDoc','JavadocComment',:single)
105
+ assert get_relation(c,'annotations','AnnotationExpr',:many)
106
+ end
107
+
108
+ def test_method_declaration
109
+ c = get_metaclass('MethodDeclaration')
110
+ assert_extend c,'BodyDeclaration'
111
+ # inherited from body declaration
112
+ #assert get_relation(c,'javaDoc','JavadocComment',:single)
113
+ assert get_relation(c,'annotations','AnnotationExpr',:many)
114
+ # declared
115
+ # these two are being ignored
116
+ # assert get_attr(c,'arrayCount',RGenInt,:single)
117
+ # assert get_attr(c,'modifiers',RGenInt,:single)
118
+ assert get_relation(c,'body','BlockStmt',:single)
119
+ assert get_attr(c,'name',RGenString,:single)
120
+ assert get_relation(c,'parameters','Parameter',:many)
121
+ assert get_relation(c,'throws','NameExpr',:many)
122
+ assert get_relation(c,'type','Type',:single)
123
+ assert get_relation(c,'typeParameters','TypeParameter',:many)
124
+ end
125
+
126
+ def test_method_assign_expr
127
+ c = get_metaclass('AssignExpr')
128
+ # assert get_attr(c,'operator',RGenString,:single) # ignored
129
+ assert get_relation(c,'target','Expression')
130
+ assert get_relation(c,'value','Expression')
131
+ end
132
+
133
+ end