jaxb2ruby 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ require "minitest/autorun"
2
+ require "jaxb2ruby"
3
+
4
+
5
+ def schema(name)
6
+ File.expand_path("../fixtures/#{name}.xsd", __FILE__)
7
+ end
8
+
9
+ def convert(xsd, options = {})
10
+ JAXB2Ruby::Converter.convert(schema(xsd), options)
11
+ end
12
+
13
+ def class_hash(classes)
14
+ # classes can be a Node or a RubyClass
15
+ pairs = classes.map { |klass| [ klass.respond_to?(:basename) ? klass.basename : klass.local_name, klass ] }
16
+ Hash[ pairs ]
17
+ end
18
+
19
+ def node_hash(element)
20
+ Hash[ (element.children + element.attributes).map { |node| [node.local_name, node] } ]
21
+ end
22
+
23
+ def assert_equal_modulo_comments(a, b)
24
+ assert_equal a, b.gsub(/^#.*\n/, '')
25
+ end
@@ -0,0 +1,128 @@
1
+ require "spec_helper"
2
+ require "fileutils"
3
+ require "tmpdir"
4
+ require "tempfile"
5
+
6
+ def write(path, classdef)
7
+ File.open(path, "w") { |io| io.puts(classdef) }
8
+ end
9
+
10
+ describe JAXB2Ruby::Template do
11
+ it "uses the default template if none is given" do
12
+ # use the template's path as the template
13
+ File.stub :read, lambda { |path| path } do
14
+ t = JAXB2Ruby::Template.new
15
+ assert_equal_modulo_comments JAXB2Ruby::Template::DEFAULT, t.build(nil)
16
+ end
17
+ end
18
+
19
+ it "uses the template provided as an argument" do
20
+ file = Tempfile.new("jaxb2ruby")
21
+ file.write("DATA")
22
+ file.close
23
+
24
+ t = JAXB2Ruby::Template.new(file.path)
25
+ assert_equal_modulo_comments "DATA", t.build(nil)
26
+ end
27
+
28
+ it "raises an error if the template can't be read" do
29
+ lambda { JAXB2Ruby::Template.new("blah!") }.must_raise(JAXB2Ruby::Error, /cannot load/)
30
+ end
31
+
32
+ it "passes the given class to the template" do
33
+ file = Tempfile.new("jaxb2ruby")
34
+ file.write("<%= @class.name %>")
35
+ file.close
36
+
37
+ klass = Struct.new(:name).new("sshaw")
38
+ t = JAXB2Ruby::Template.new(file.path)
39
+ assert_equal_modulo_comments "sshaw", t.build(klass)
40
+ end
41
+
42
+ describe "known templates" do
43
+ let(:paths) { JAXB2Ruby::Template::PATHS }
44
+
45
+ %w[happymapper roxml ruby].each do |name|
46
+ it "includes #{name}" do
47
+ paths.must_include(name)
48
+ File.file?(paths[name]).must_equal(true)
49
+ File.basename(paths[name]).must_equal("#{name}.erb")
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "a class created by the ruby template" do
55
+ let(:tmpdir) { Dir.mktmpdir }
56
+ let(:attributes) {
57
+ Hash[
58
+ :user_name => "sshaw",
59
+ :first_name => "skye",
60
+ :last_name => "shaw",
61
+ :numbers => [1, 2, 3],
62
+ :age => 0xFF
63
+ ]
64
+ }
65
+
66
+ before do
67
+ path = File.join(tmpdir, "user.rb")
68
+ classes = convert("class")
69
+
70
+ t = JAXB2Ruby::Template.new("ruby") # template
71
+ write(path, t.build(classes.first))
72
+
73
+ require path
74
+ end
75
+
76
+ after { FileUtils.rm_rf(tmpdir) }
77
+
78
+ it "has singular read/write accessors for bounded elements" do
79
+ methods = Com::Example::User.instance_methods(false)
80
+ methods.must_include(:user_name)
81
+ methods.must_include(:user_name=)
82
+ methods.must_include(:first_name)
83
+ methods.must_include(:first_name=)
84
+ methods.must_include(:last_name)
85
+ methods.must_include(:last_name=)
86
+ end
87
+
88
+ it "has plural read/write accessors for unbounded elements" do
89
+ methods = Com::Example::User.instance_methods(false)
90
+ methods.must_include(:numbers)
91
+ methods.must_include(:numbers=)
92
+ end
93
+
94
+ it "has singular read/write accessors for attributes" do
95
+ methods = Com::Example::User.instance_methods(false)
96
+ methods.must_include(:age)
97
+ methods.must_include(:age=)
98
+ end
99
+
100
+ describe ".new" do
101
+ it "accepts no arguments" do
102
+ user = Com::Example::User.new
103
+ user.must_be_instance_of(Com::Example::User)
104
+ attributes.each { |name, _| user.send(name).must_be_nil }
105
+ end
106
+
107
+ it "accepts a nil argument" do
108
+ user = Com::Example::User.new(nil)
109
+ user.must_be_instance_of(Com::Example::User)
110
+ attributes.each { |name, _| user.send(name).must_be_nil }
111
+ end
112
+
113
+ it "initializes instances using values in a Hash argument" do
114
+ user = Com::Example::User.new(attributes)
115
+ user.must_be_instance_of(Com::Example::User)
116
+ attributes.each { |name, value| user.send(name).must_equal(value) }
117
+ end
118
+
119
+ # Or, should an ArgumentError be raised?
120
+ it "ignores unknown accessors names in the Hash argument"
121
+ end
122
+
123
+ describe "#inspect" do
124
+ it "returns a description of the class' state" do
125
+ end
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jaxb2ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: java
6
+ authors:
7
+ - Skye Shaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: '3.2'
19
+ name: activesupport
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.5.4
33
+ name: cocaine
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.4
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '10.0'
47
+ name: rake
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: |2
56
+ jaxb2ruby generates Java XML mappings via xjc, reads the resulting annotations, and passes the
57
+ extracted info to an ERB template. This allows one to automatically map an XML schema to pure
58
+ Ruby classes (i.e. classes that don't require JRuby) using the mapping framework of their choice.
59
+
60
+ Several templates are included: ROXML, HappyMapper and generic Ruby class (PORO).
61
+ email: skye.shaw@gmail.com
62
+ executables:
63
+ - jaxb2ruby
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - bin/jaxb2ruby
68
+ - lib/jaxb2ruby.rb
69
+ - lib/jaxb2ruby/classes.rb
70
+ - lib/jaxb2ruby/config.xjb
71
+ - lib/jaxb2ruby/converter.rb
72
+ - lib/jaxb2ruby/template.rb
73
+ - lib/jaxb2ruby/type_util.rb
74
+ - lib/jaxb2ruby/version.rb
75
+ - lib/jaxb2ruby/xjc.rb
76
+ - lib/templates/happymapper.erb
77
+ - lib/templates/roxml.erb
78
+ - lib/templates/ruby.erb
79
+ - lib/templates/util/happymapper.rb
80
+ - lib/templates/util/roxml.rb
81
+ - spec/converter_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spec/template_spec.rb
84
+ homepage: http://github.com/sshaw/jaxb2ruby
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options:
90
+ - -m
91
+ - README.rd
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - <
97
+ - !ruby/object:Gem::Version
98
+ version: '2.0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.5
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Generate pure Ruby classes from an XML schema using JAXB and JRuby
110
+ test_files:
111
+ - spec/converter_spec.rb
112
+ - spec/spec_helper.rb
113
+ - spec/template_spec.rb