guff 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +0 -0
- data/Manifest.txt +21 -0
- data/README.txt +3 -0
- data/Rakefile +54 -0
- data/lib/guff/java_source.rb +576 -0
- data/lib/guff/version.rb +9 -0
- data/lib/guff.rb +2 -0
- data/setup.rb +1585 -0
- data/test/java_source/annotation_declaration_test.rb +49 -0
- data/test/java_source/class_declaration_test.rb +55 -0
- data/test/java_source/field_declaration_test.rb +19 -0
- data/test/java_source/files/ClassWithAnnotatedFields.java +6 -0
- data/test/java_source/files/ClassWithAnnotatedMethods.java +32 -0
- data/test/java_source/files/ClassWithFields.java +10 -0
- data/test/java_source/files/ClassWithMethods.java +17 -0
- data/test/java_source/files/EmptyClass.java +4 -0
- data/test/java_source/files/GenericClass.java +4 -0
- data/test/java_source/method_declaration_test.rb +25 -0
- data/test/java_source/test_helper.rb +80 -0
- data/test/test_helper.rb +1 -0
- metadata +68 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class AnnotationDeclarationTest < Test::Unit::TestCase
|
4
|
+
include JavaSourceTestHelper, Guff::JavaSource::ClientSyntaxSupport
|
5
|
+
|
6
|
+
def test_can_declare_annotations_on_classes_and_methods
|
7
|
+
source = new_source_file.package('guff.test')
|
8
|
+
source.begin_class('ClassWithAnnotatedMethods') {|c|
|
9
|
+
c.add_annotation("javax.persistence.Entity")
|
10
|
+
|
11
|
+
m = c.add_method("getAddressID").returns(:String)
|
12
|
+
m.add_annotation("javax.persistence.Column") {|a|
|
13
|
+
a.add_property("name", '"addressID"')
|
14
|
+
a.add_property("table", '"EMP_DETAIL"')
|
15
|
+
}
|
16
|
+
m.add_annotation("SuppressWarnings",'"unchecked"')
|
17
|
+
m.body {|body|
|
18
|
+
body.line('return "0";')
|
19
|
+
}
|
20
|
+
|
21
|
+
m = c.add_method("getSubscriptions").abstract.returns(:Collection)
|
22
|
+
m.add_annotation("ManyToMany") {|a|
|
23
|
+
a.add_property("fetch", "FetchType.EAGER")
|
24
|
+
}
|
25
|
+
|
26
|
+
m.add_annotation("JoinTable") {|a|
|
27
|
+
a.add_property("name", '"CUSTOMERBEANSUBSCRIPTIONBEAN"')
|
28
|
+
a.add_property("joinColumns", annotation("JoinColumn"){|aa|
|
29
|
+
aa.add_property("name", '"CUSTOMERBEAN_CUSTOMERID96"')
|
30
|
+
aa.add_property("referencedColumnName", '"customerid"')
|
31
|
+
})
|
32
|
+
a.add_property("inverseJoinColumns", annotation("JoinColumn"){|aa|
|
33
|
+
aa.add_property("name", '"SUBSCRIPTION_TITLE"')
|
34
|
+
aa.add_property("referencedColumnName", '"TITLE"')
|
35
|
+
})
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
assert_source_file_equals(source, prepared_file_for_tests('ClassWithAnnotatedMethods'))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_can_declare_annotations_on_fields
|
43
|
+
source = new_source_file.package('guff.test')
|
44
|
+
source.begin_class('ClassWithAnnotatedFields') {|c|
|
45
|
+
c.add_field('id',:int).add_annotation("Id")
|
46
|
+
}
|
47
|
+
assert_source_file_equals(source, prepared_file_for_tests('ClassWithAnnotatedFields'))
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class ClassDeclarationTest < Test::Unit::TestCase
|
4
|
+
include JavaSourceTestHelper
|
5
|
+
|
6
|
+
def test_can_generate_an_empty_class
|
7
|
+
source = new_source_file.package('guff.test')
|
8
|
+
source.begin_class('EmptyClass') {
|
9
|
+
}
|
10
|
+
|
11
|
+
assert_source_file_equals(source, prepared_empty_class.without('extends', 'implements'))
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_can_generate_a_class_extending_another_class
|
15
|
+
source = new_source_file.package('guff.test')
|
16
|
+
source.begin_class('EmptyClass') do |empty|
|
17
|
+
empty.extends('guff.test.SuperClass')
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_source_file_equals(source, prepared_empty_class.without('implements'))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_can_generate_a_class_implementing_interfaces
|
24
|
+
source = new_source_file.package('guff.test')
|
25
|
+
source.begin_class('EmptyClass') do |empty|
|
26
|
+
empty.implements('guff.test.Interface', 'java.io.Serializable')
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_source_file_equals(source, prepared_empty_class.without('extends'))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_can_generate_a_class_extending_another_class_and_implementing_interfaces
|
33
|
+
source = new_source_file.package('guff.test')
|
34
|
+
source.begin_class('EmptyClass') do |empty|
|
35
|
+
empty.implements('guff.test.Interface', 'java.io.Serializable')
|
36
|
+
empty.extends('guff.test.SuperClass')
|
37
|
+
end
|
38
|
+
|
39
|
+
assert_source_file_equals(source, prepared_empty_class)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_can_generate_a_generic_class_declaration
|
43
|
+
source = new_source_file.package('guff.test')
|
44
|
+
source.begin_class('GenericClass') {|c|
|
45
|
+
c.genericized_using('<T extends SomethingElse>')
|
46
|
+
}
|
47
|
+
|
48
|
+
assert_source_file_equals(source,prepared_file_for_tests('GenericClass'))
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def prepared_empty_class
|
53
|
+
prepared_file_for_tests('EmptyClass')
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class FieldDeclarationTest < Test::Unit::TestCase
|
4
|
+
include JavaSourceTestHelper
|
5
|
+
|
6
|
+
def test_can_declare_fields
|
7
|
+
source = new_source_file.package('guff.test')
|
8
|
+
source.begin_class('ClassWithFields') {|c|
|
9
|
+
c.add_field('name',:String).static.final.initial('"Fred"')
|
10
|
+
c.add_field('id',:long).initial('0')
|
11
|
+
c.add_field('user','some.other.pkg.User').initial('new some.other.pkg.User(0,2)')
|
12
|
+
c.add_field('otherId',:int).package_local
|
13
|
+
c.add_field('c',:char).protected
|
14
|
+
c.add_field('flag',:boolean).public.initial('false')
|
15
|
+
}
|
16
|
+
|
17
|
+
assert_source_file_equals(source, prepared_file_for_tests('ClassWithFields'))
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
package guff.test;
|
2
|
+
|
3
|
+
@javax.persistence.Entity
|
4
|
+
public class ClassWithAnnotatedMethods{
|
5
|
+
|
6
|
+
@javax.persistence.Column(
|
7
|
+
name="addressID",
|
8
|
+
table="EMP_DETAIL"
|
9
|
+
)
|
10
|
+
@SuppressWarnings(
|
11
|
+
"unchecked"
|
12
|
+
)
|
13
|
+
public String getAddressID(){
|
14
|
+
return "0";
|
15
|
+
}
|
16
|
+
|
17
|
+
@ManyToMany(
|
18
|
+
fetch=FetchType.EAGER
|
19
|
+
)
|
20
|
+
@JoinTable(
|
21
|
+
name="CUSTOMERBEANSUBSCRIPTIONBEAN",
|
22
|
+
joinColumns=@JoinColumn(
|
23
|
+
name="CUSTOMERBEAN_CUSTOMERID96",
|
24
|
+
referencedColumnName="customerid"
|
25
|
+
),
|
26
|
+
inverseJoinColumns=@JoinColumn(
|
27
|
+
name="SUBSCRIPTION_TITLE",
|
28
|
+
referencedColumnName="TITLE"
|
29
|
+
)
|
30
|
+
)
|
31
|
+
public abstract Collection getSubscriptions();
|
32
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
package guff.test;
|
2
|
+
|
3
|
+
public class ClassWithMethods{
|
4
|
+
|
5
|
+
private static final String getName(){
|
6
|
+
return "Fred";
|
7
|
+
}
|
8
|
+
|
9
|
+
protected boolean isHappy(String name,pkg.Person p,pkg.Friend f){
|
10
|
+
if(f.hasMoney()){
|
11
|
+
if(p.hasHealth()){
|
12
|
+
return true;
|
13
|
+
}
|
14
|
+
}
|
15
|
+
return false;
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class MethodDeclarationTest < Test::Unit::TestCase
|
4
|
+
include JavaSourceTestHelper
|
5
|
+
|
6
|
+
def test_can_declare_methods
|
7
|
+
source = new_source_file.package('guff.test')
|
8
|
+
source.begin_class('ClassWithMethods') {|c|
|
9
|
+
c.add_method("getName").private.static.final.returns(:String).body { |body|
|
10
|
+
body.line('return "Fred";')
|
11
|
+
}
|
12
|
+
|
13
|
+
c.add_method('isHappy').protected.takes('name', :String).takes('p', 'pkg.Person').takes('f', 'pkg.Friend').returns(:boolean).body { |body|
|
14
|
+
body.append('if(f.hasMoney())').body { |nested|
|
15
|
+
nested.append('if(p.hasHealth())').body { |nested2|
|
16
|
+
nested2.line('return true;')
|
17
|
+
}
|
18
|
+
}
|
19
|
+
body.line("return false;")
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
assert_source_file_equals(source, prepared_file_for_tests('ClassWithMethods'))
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
class JavaFileForTests
|
2
|
+
def initialize(testcase,path)
|
3
|
+
@testcase = testcase
|
4
|
+
@path = path
|
5
|
+
@bits_to_exclude = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def without(*bits)
|
9
|
+
@bits_to_exclude.concat(bits)
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def same_as(path)
|
14
|
+
me = File.new(@path,"r")
|
15
|
+
other_lines = read_lines(path)
|
16
|
+
line_count=0
|
17
|
+
me.each_line do |line|
|
18
|
+
line = remove_section_delimiter_comments(exclude_bits(line))
|
19
|
+
@testcase.assert_equal(line, other_lines[line_count])
|
20
|
+
line_count = line_count + 1
|
21
|
+
end
|
22
|
+
me.close
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def remove_section_delimiter_comments(input)
|
27
|
+
r1 = Regexp.new("\/\\*begin .*?\\*/")
|
28
|
+
r2 = Regexp.new("\/\\*end .*?\\*/")
|
29
|
+
input.gsub(r1,"").gsub(r2,"")
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def exclude_bits(input)
|
34
|
+
result = input
|
35
|
+
@bits_to_exclude.each do |bit|
|
36
|
+
result = exclude_bit(result,bit)
|
37
|
+
end
|
38
|
+
result
|
39
|
+
end
|
40
|
+
|
41
|
+
def exclude_bit(input,bit)
|
42
|
+
r = Regexp.new("\/\\*begin #{bit}\\*\/.*?\/\\*end #{bit}\\*\/")
|
43
|
+
input.gsub(r,"")
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
def read_lines(path)
|
49
|
+
result = []
|
50
|
+
f = File.new(path,"r")
|
51
|
+
f.each_line do |line|
|
52
|
+
result << line
|
53
|
+
end
|
54
|
+
f.close
|
55
|
+
result
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
module JavaSourceTestHelper
|
61
|
+
def new_source_file
|
62
|
+
Guff::JavaSource::SourceFile.new
|
63
|
+
end
|
64
|
+
|
65
|
+
def tmp_dir
|
66
|
+
'.'
|
67
|
+
end
|
68
|
+
|
69
|
+
def prepared_file_for_tests(name)
|
70
|
+
JavaFileForTests.new(self,File.dirname(__FILE__) + "/files/#{name}.java")
|
71
|
+
end
|
72
|
+
|
73
|
+
def assert_source_file_equals(source, expected)
|
74
|
+
file_path = source.save_in(tmp_dir)
|
75
|
+
assert expected.same_as(file_path)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
require File.dirname(__FILE__) + '/../test_helper.rb'
|
80
|
+
require File.dirname(__FILE__) + '/../../lib/guff/java_source.rb'
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'test/unit'
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: guff
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.2
|
7
|
+
date: 2007-03-03 00:00:00 +00:00
|
8
|
+
summary: An API to describe and generate Java source code. The idea is to avoid writing all the 'guff' that is typical in java projects by generating it from more succinct ruby models.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: me@mikehogan.net
|
12
|
+
homepage: http://guff.rubyforge.org
|
13
|
+
rubyforge_project: guff
|
14
|
+
description: An API to describe and generate Java source code. The idea is to avoid writing all the 'guff' that is typical in java projects by generating it from more succinct ruby models.
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Mike Hogan
|
31
|
+
files:
|
32
|
+
- Rakefile
|
33
|
+
- README.txt
|
34
|
+
- CHANGELOG.txt
|
35
|
+
- Manifest.txt
|
36
|
+
- setup.rb
|
37
|
+
- lib/guff/version.rb
|
38
|
+
- lib/guff/java_source.rb
|
39
|
+
- lib/guff.rb
|
40
|
+
- test/test_helper.rb
|
41
|
+
- test/java_source/files/ClassWithAnnotatedFields.java
|
42
|
+
- test/java_source/files/ClassWithAnnotatedMethods.java
|
43
|
+
- test/java_source/files/ClassWithFields.java
|
44
|
+
- test/java_source/files/ClassWithMethods.java
|
45
|
+
- test/java_source/files/EmptyClass.java
|
46
|
+
- test/java_source/files/GenericClass.java
|
47
|
+
- test/java_source/annotation_declaration_test.rb
|
48
|
+
- test/java_source/class_declaration_test.rb
|
49
|
+
- test/java_source/field_declaration_test.rb
|
50
|
+
- test/java_source/method_declaration_test.rb
|
51
|
+
- test/java_source/test_helper.rb
|
52
|
+
test_files:
|
53
|
+
- test/java_source/annotation_declaration_test.rb
|
54
|
+
- test/java_source/class_declaration_test.rb
|
55
|
+
- test/java_source/field_declaration_test.rb
|
56
|
+
- test/java_source/method_declaration_test.rb
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
extra_rdoc_files: []
|
60
|
+
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
dependencies: []
|
68
|
+
|