rubeus 0.0.6-java → 0.0.7-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/Rakefile +26 -0
  2. data/examples/jdbc_example.rb +5 -24
  3. data/lib/rubeus.jar +0 -0
  4. data/lib/rubeus.rb +18 -2
  5. data/lib/rubeus/component_loader.rb +25 -20
  6. data/lib/rubeus/extensions.rb +44 -4
  7. data/lib/rubeus/extensions/java/awt/dimension.rb +8 -2
  8. data/lib/rubeus/extensions/java/sql/driver_manager.rb +240 -1
  9. data/lib/rubeus/extensions/javax/swing/j_table.rb +7 -23
  10. data/lib/rubeus/extensions/javax/swing/table/default_table_model.rb +7 -0
  11. data/lib/rubeus/extensions/javax/swing/table/readonlyable_table_model.rb +10 -0
  12. data/lib/rubeus/jdbc.rb +5 -0
  13. data/lib/rubeus/swing.rb +9 -6
  14. data/lib/rubeus/verboseable.rb +41 -0
  15. data/rmaven.yml +6 -0
  16. data/test/rubeus/awt/test_attributes.rb +58 -0
  17. data/test/rubeus/awt/test_event.rb +216 -0
  18. data/test/rubeus/awt/test_nestable.rb +92 -0
  19. data/test/rubeus/awt/test_setter.rb +42 -0
  20. data/test/rubeus/extensions/java/awt/test_dimension.rb +67 -0
  21. data/test/rubeus/extensions/java/sql/test_connection.rb +50 -0
  22. data/test/rubeus/extensions/java/sql/test_driver_manager.rb +37 -0
  23. data/test/rubeus/extensions/java/sql/test_result_set.rb +67 -0
  24. data/test/rubeus/extensions/java/sql/test_result_set_meta_data.rb +121 -0
  25. data/test/rubeus/extensions/java/sql/test_statement.rb +43 -0
  26. data/test/rubeus/extensions/javax/swing/table/test_default_table_model.rb +359 -0
  27. data/test/rubeus/extensions/javax/swing/table/test_default_table_model/nhk_words.xml +15 -0
  28. data/test/rubeus/extensions/javax/swing/test_box_layout.rb +56 -0
  29. data/test/rubeus/extensions/javax/swing/test_j_component.rb +30 -0
  30. data/test/rubeus/extensions/javax/swing/test_j_frame.rb +42 -0
  31. data/test/rubeus/extensions/javax/swing/test_j_panel.rb +23 -0
  32. data/test/rubeus/extensions/javax/swing/test_j_scroll_pane.rb +24 -0
  33. data/test/rubeus/extensions/javax/swing/test_j_split_pane.rb +54 -0
  34. data/test/rubeus/extensions/javax/swing/test_j_tabbed_pane.rb +204 -0
  35. data/test/rubeus/extensions/javax/swing/test_j_table.rb +71 -0
  36. data/test/rubeus/extensions/javax/swing/test_j_text_field.rb +48 -0
  37. data/test/rubeus/extensions/javax/swing/test_timer.rb +28 -0
  38. data/test/rubeus/test_extensions.rb +44 -0
  39. data/test/test_all.rb +2 -0
  40. metadata +50 -16
@@ -1,6 +1,7 @@
1
1
  Rubeus::Swing.depend_on('JComponent')
2
+ Rubeus::Swing.depend_on('TableModel')
3
+ Rubeus::Swing.depend_on('ReadonlyableTableModel')
2
4
  Rubeus::Swing.depend_on('DefaultTableModel')
3
- require 'delegate'
4
5
 
5
6
  module Rubeus::Extensions::Javax::Swing
6
7
  module JTable
@@ -24,29 +25,12 @@ module Rubeus::Extensions::Javax::Swing
24
25
  end
25
26
 
26
27
  def set_model_with_rubeus(model, *args)
27
- unless model.is_a?(javax.swing.table.TableModel)
28
- model = javax.swing.table.DefaultTableModel.new(model, *args)
29
- end
30
- delegator = DelegatableTableModel.new(model)
31
- set_model_without_rubeus(@model = delegator)
32
- end
33
-
34
- class DelegatableTableModel
35
- # include javax.swing.table.TableModel
36
-
37
- attr_accessor :readonly
38
-
39
- def initialize(source)
40
- @source = source
41
- end
42
-
43
- def method_missing(method, *args, &block)
44
- @source.__send__(method, *args, &block)
45
- end
46
-
47
- def isCellEditable(row, col)
48
- !readonly
28
+ unless model.is_a?(Rubeus::Swing::TableModel)
29
+ model = Rubeus::Swing::DefaultTableModel.new(model, *args)
49
30
  end
31
+ delegator = Rubeus::Swing::ReadonlyableTableModel.new(model)
32
+ @model = delegator
33
+ set_model_without_rubeus(@model)
50
34
  end
51
35
  end
52
36
  end
@@ -1,3 +1,4 @@
1
+ Rubeus::Swing.depend_on('TableModel')
1
2
  Rubeus::Swing.depend_on('AbstractTableModel')
2
3
 
3
4
  module Rubeus::Extensions::Javax::Swing::Table
@@ -52,7 +53,13 @@ module Rubeus::Extensions::Javax::Swing::Table
52
53
  return new_without_rubeus(*args)
53
54
  end
54
55
  end
56
+
57
+ attr_accessor :readonly
55
58
 
59
+ def isCellEditable(row, col)
60
+ !readonly
61
+ end
62
+
56
63
  def vectorize_if_array(value)
57
64
  self.class.vectorize_if_array(value)
58
65
  end
@@ -0,0 +1,10 @@
1
+ Rubeus::Swing.depend_on('TableModel')
2
+ Rubeus::Swing.depend_on('DelegatableTableModel')
3
+
4
+ module Rubeus::Extensions::Javax::Swing::Table
5
+ module ReadonlyableTableModel
6
+ def method_missing(method, *args, &block)
7
+ self.source.send(method, *args, &block)
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require "rubeus/component_loader"
2
3
 
3
4
  module Rubeus
@@ -10,6 +11,10 @@ module Rubeus
10
11
  'ResultSetMetaData' => 'java.sql',
11
12
  'Statement' => 'java.sql'
12
13
  )
14
+
15
+ def self.irb
16
+ self.extend_with
17
+ end
13
18
  end
14
19
  end
15
20
 
@@ -1,15 +1,18 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require "rubeus/component_loader"
2
3
  require "rubeus/awt"
3
4
 
4
5
  module Rubeus
5
6
  Swing = ComponentLoader.new("javax.swing") do
6
- class_to_package.update(
7
+ class_to_package.update({
7
8
  # $JAVA_HOME/lib/classlistにないものリスト
8
- 'JTextPane' => 'javax.swing',
9
- 'RTFEditorKit' => 'javax.swing.text.rtf',
10
- 'DefaultTableModel' => 'javax.swing.table'
11
- )
12
- class_to_package['DefaultStyledDocument'] ||= 'javax.swing.text' # Windowsにない
9
+ 'JTextPane' => 'javax.swing',
10
+ 'RTFEditorKit' => 'javax.swing.text.rtf',
11
+ 'DefaultTableModel' => 'javax.swing.table',
12
+ 'DelegatableTableModel' => 'jp.rubybizcommons.rubeus.extensions.javax.swing.table',
13
+ 'ReadonlyableTableModel' => 'jp.rubybizcommons.rubeus.extensions.javax.swing.table',
14
+ })
15
+ class_to_package['DefaultStyledDocument'] ||= 'javax.swing.text' # Windowsにない?
13
16
 
14
17
  def self.irb
15
18
  Rubeus::Awt.extend_with
@@ -0,0 +1,41 @@
1
+ module Rubeus
2
+ module Verboseable
3
+ class << self
4
+ def out
5
+ @out || $stderr
6
+ end
7
+
8
+ def out=(value)
9
+ @out = value
10
+ end
11
+ end
12
+
13
+ def log_if_verbose(*messages)
14
+ return(block_given? ? yield : nil) unless self.verbose
15
+ name = is_a?(Module) ? self.name : self.class.name
16
+ msg = "#{name} %s" %
17
+ messages.map{|m| m.is_a?(Exception) ? ("#{m.to_s}\n%s" % m.backtrace.join("\n ")) : m }.
18
+ join("\n")
19
+ @@indent ||= 0
20
+ if @@indent > 0
21
+ msg.gsub!(/^/, ' ' * @@indent)
22
+ end
23
+ if block_given?
24
+ indent_backup = @@indent
25
+ @@indent += 1
26
+ Verboseable.out.puts("#{msg} {")
27
+ begin
28
+ return yield
29
+ rescue Exception => err
30
+ Verboseable.out.puts("#{' ' * @@indent} RAISED: #{err.inspect}")
31
+ raise
32
+ ensure
33
+ @@indent = indent_backup
34
+ Verboseable.out.puts("#{' ' * @@indent}}")
35
+ end
36
+ else
37
+ Verboseable.out.puts(msg)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,6 @@
1
+ ---
2
+ java:
3
+ artifact_id: java_supply
4
+ group_id: jp.rubybizcommons.rubeus
5
+ rake_prefix: java
6
+ pom_dir: java
@@ -0,0 +1,58 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'rubeus'
4
+
5
+ # Test for attributes.rb
6
+ class TestAttributes < Test::Unit::TestCase
7
+ include Rubeus::Swing
8
+
9
+ # setup method
10
+ def setup
11
+ end
12
+
13
+ # new with no argument
14
+ def test_default_attributes=
15
+ JFrame.new do |f|
16
+ assert_equal(JFrame.const_get(:EXIT_ON_CLOSE), f.default_close_operation)
17
+ assert_equal(400, f.size.width)
18
+ assert_equal(300, f.size.height)
19
+ end
20
+ end
21
+
22
+ # new with argument
23
+ def test_new
24
+ JFrame.new(:title => 'test') do |f|
25
+ assert_equal(JFrame.const_get(:EXIT_ON_CLOSE), f.default_close_operation)
26
+ assert_equal(400, f.size.width)
27
+ assert_equal(300, f.size.height)
28
+ assert_equal('test', f.title)
29
+ end
30
+ end
31
+
32
+ # new with argument that overrides default attributes
33
+ def test_new_override
34
+ JFrame.new(:size => "500 x 400") do |f|
35
+ assert_equal(JFrame.const_get(:EXIT_ON_CLOSE), f.default_close_operation)
36
+ assert_equal(500, f.size.width)
37
+ assert_equal(400, f.size.height)
38
+ end
39
+ end
40
+
41
+ # new with argument that value is symbol
42
+ def test_new_with_symbol_value
43
+ JFrame.new(:default_close_operation => :DO_NOTHING_ON_CLOSE) do |f|
44
+ assert_equal(JFrame.const_get(:DO_NOTHING_ON_CLOSE), f.default_close_operation)
45
+ assert_equal(400, f.size.width)
46
+ assert_equal(300, f.size.height)
47
+ end
48
+ end
49
+
50
+ # new with argument with error
51
+ def test_new_with_attribute_does_not_exist
52
+ assert_raise(ArgumentError) do
53
+ JFrame.new(:illegal_param => "illegal_value") do |f|
54
+ end
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,216 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'rubeus'
4
+
5
+ # Test for event.rb
6
+ class TestEvent < Test::Unit::TestCase
7
+ include Rubeus::Swing
8
+
9
+ class TestClass
10
+ include Rubeus::Awt::Event
11
+ end
12
+
13
+ # setup method
14
+ def setup
15
+ end
16
+
17
+ # test uncapitalize
18
+ def test_uncapitalize
19
+ assert_equal("aBC", TestClass.uncapitalize("ABC"))
20
+
21
+ assert_raise(TypeError) do
22
+ TestClass.uncapitalize("")
23
+ end
24
+ end
25
+
26
+ # test find_java_method
27
+ def test_find_java_method
28
+ JFrame.new do |f|
29
+ assert_equal("getContentPane", f.find_java_method("getContentPane").name)
30
+ end
31
+ end
32
+
33
+ # test find_java_method with parent class's method
34
+ def test_find_java_method_with_parent_class_method
35
+ JFrame.new do |f|
36
+ assert_equal("getTitle", f.find_java_method("getTitle").name)
37
+ end
38
+ end
39
+
40
+ # test find_java_method with method not exists
41
+ def test_find_java_method_with_method_not_exists
42
+ JFrame.new do |f|
43
+ assert_nil(f.find_java_method("addActionListener"))
44
+ end
45
+ end
46
+
47
+ # test events
48
+ def test_events
49
+ expectedHash = {
50
+ "component"=>["component_hidden", "component_moved", "component_resized", "component_shown"],
51
+ "container"=>["component_added", "component_removed"],
52
+ "focus"=>["focus_gained", "focus_lost"],
53
+ "hierarchy"=>["hierarchy_changed"],
54
+ "hierarchy_bounds"=>["ancestor_moved", "ancestor_resized"],
55
+ "input_method"=>["caret_position_changed", "input_method_text_changed"],
56
+ "key"=>["key_pressed", "key_released", "key_typed"],
57
+ "mouse"=>["mouse_clicked", "mouse_entered", "mouse_exited", "mouse_pressed", "mouse_released"],
58
+ "mouse_motion"=>["mouse_dragged", "mouse_moved"],
59
+ "mouse_wheel"=>["mouse_wheel_moved"],
60
+ "property_change"=>["property_change"],
61
+ "window"=>["window_activated", "window_closed", "window_closing", "window_deactivated", "window_deiconified", "window_iconified", "window_opened"],
62
+ "window_focus"=>["window_gained_focus", "window_lost_focus"],
63
+ "window_state"=>["window_state_changed"]
64
+ }
65
+ JFrame.new do |f|
66
+ expectedHash.keys.each do |key|
67
+ assert_equal(expectedHash[key].sort, f.events[key].sort)
68
+ end
69
+ end
70
+ end
71
+
72
+ # test event_types
73
+ def test_event_types
74
+ expectedArray = ["component", "container", "focus", "hierarchy", "hierarchy_bounds", "input_method", "key", "mouse", "mouse_motion", "mouse_wheel", "property_change", "window", "window_focus", "window_state"]
75
+ JFrame.new do |f|
76
+ assert_equal(expectedArray, f.event_types)
77
+ end
78
+ end
79
+
80
+ # test listen
81
+ def test_listen
82
+ @mouse_event_processed = false
83
+
84
+ jf = JFrame.new do |f|
85
+ f.listen(:mouse, :mouse_clicked) do |mouse_event|
86
+ assert_equal(java.awt.event.MouseEvent::MOUSE_CLICKED, mouse_event.get_id)
87
+ assert_equal(100, mouse_event.x)
88
+ assert_equal(250, mouse_event.y)
89
+ assert_equal(1, mouse_event.click_count)
90
+ assert_equal(false, mouse_event.popup_trigger?)
91
+ assert_equal(0, mouse_event.get_modifiers)
92
+
93
+ @mouse_event_processed = true
94
+ end
95
+ end
96
+
97
+ jf.visible = true
98
+
99
+ # Send mouse clicked event to JFrame
100
+ jf.dispatch_event(
101
+ java.awt.event.MouseEvent.new(
102
+ jf,
103
+ java.awt.event.MouseEvent::MOUSE_CLICKED,
104
+ java.lang.System.current_time_millis,
105
+ 0,
106
+ 100,
107
+ 250,
108
+ 1,
109
+ false
110
+ )
111
+ )
112
+
113
+ jf.dispose
114
+ jf.visible = false
115
+
116
+ assert_equal(true, @mouse_event_processed)
117
+ end
118
+
119
+ # test listen with filters
120
+ def test_listen_with_filters
121
+ @mouse_event_processed_count = 0
122
+
123
+ jf = JFrame.new do |f|
124
+ f.listen(:mouse, :mouse_clicked, :click_count => 2) do |mouse_event|
125
+ assert_equal(java.awt.event.MouseEvent::MOUSE_CLICKED, mouse_event.get_id)
126
+ assert_equal(100, mouse_event.x)
127
+ assert_equal(250, mouse_event.y)
128
+ assert_equal(2, mouse_event.click_count)
129
+ assert_equal(false, mouse_event.popup_trigger?)
130
+ assert_equal(0, mouse_event.get_modifiers)
131
+
132
+ @mouse_event_processed_count = @mouse_event_processed_count + 1
133
+ end
134
+ end
135
+
136
+ jf.visible = true
137
+
138
+ # Send mouse clicked event to JFrame
139
+ 3.times do |i|
140
+ jf.dispatch_event(
141
+ java.awt.event.MouseEvent.new(
142
+ jf,
143
+ java.awt.event.MouseEvent::MOUSE_CLICKED,
144
+ java.lang.System.current_time_millis,
145
+ 0,
146
+ 100,
147
+ 250,
148
+ (i+1), # click_count (1..3)
149
+ false
150
+ )
151
+ )
152
+ end
153
+
154
+ jf.dispose
155
+ jf.visible = false
156
+
157
+ assert_equal(1, @mouse_event_processed_count)
158
+ end
159
+
160
+ # test listen with two filters
161
+ def test_listen_with_two_filters
162
+ @mouse_event_processed_count = 0
163
+
164
+ jf = JFrame.new do |f|
165
+ # left button double clicked
166
+ f.listen(:mouse, :mouse_clicked, :click_count => 2, :button => java.awt.event.MouseEvent::BUTTON1) do |mouse_event|
167
+ assert_equal(java.awt.event.MouseEvent::MOUSE_CLICKED, mouse_event.get_id)
168
+ assert_equal(100, mouse_event.x)
169
+ assert_equal(250, mouse_event.y)
170
+ assert_equal(2, mouse_event.click_count)
171
+ assert_equal(false, mouse_event.popup_trigger?)
172
+ assert_equal(java.awt.event.InputEvent::BUTTON1_MASK, mouse_event.get_modifiers)
173
+ assert_equal(java.awt.event.MouseEvent::BUTTON1, mouse_event.get_button)
174
+
175
+ @mouse_event_processed_count = @mouse_event_processed_count + 1
176
+ end
177
+ end
178
+
179
+ jf.visible = true
180
+
181
+ # Send mouse clicked event to JFrame
182
+ jf.dispatch_event(
183
+ java.awt.event.MouseEvent.new(
184
+ jf,
185
+ java.awt.event.MouseEvent::MOUSE_CLICKED,
186
+ java.lang.System.current_time_millis,
187
+ java.awt.event.InputEvent::BUTTON1_MASK,
188
+ 100,
189
+ 250,
190
+ 2,
191
+ false,
192
+ java.awt.event.MouseEvent::BUTTON1
193
+ )
194
+ )
195
+
196
+ jf.dispatch_event(
197
+ java.awt.event.MouseEvent.new(
198
+ jf,
199
+ java.awt.event.MouseEvent::MOUSE_CLICKED,
200
+ java.lang.System.current_time_millis,
201
+ java.awt.event.InputEvent::BUTTON2_MASK,
202
+ 100,
203
+ 250,
204
+ 2,
205
+ false,
206
+ java.awt.event.MouseEvent::BUTTON2
207
+ )
208
+ )
209
+
210
+ jf.dispose
211
+ jf.visible = false
212
+
213
+ assert_equal(1, @mouse_event_processed_count)
214
+ end
215
+ end
216
+
@@ -0,0 +1,92 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'rubeus'
4
+
5
+ # Test for nestable.rb
6
+ class TestNestable < Test::Unit::TestCase
7
+ include Rubeus::Swing
8
+
9
+ class TestClass
10
+ include Rubeus::Awt::Nestable
11
+ end
12
+
13
+ # setup method
14
+ def setup
15
+ end
16
+
17
+ # test for Nestable::Context#container_class_names=, register_as_container
18
+ def atest_container_class_names=
19
+ original_container_class_names = Rubeus::Awt::Nestable::Context.container_class_names
20
+
21
+ Rubeus::Awt::Nestable::Context.container_class_names = "javax.swing.JTabbedPane"
22
+ assert_equal(["javax.swing.JTabbedPane"], Rubeus::Awt::Nestable::Context.container_class_names)
23
+
24
+ Rubeus::Awt::Nestable::Context.register_as_container "javax.swing.JPanel"
25
+ assert_equal(["javax.swing.JTabbedPane", "javax.swing.JPanel"], Rubeus::Awt::Nestable::Context.container_class_names)
26
+
27
+ Rubeus::Awt::Nestable::Context.container_class_names = *original_container_class_names
28
+ end
29
+
30
+ # test new with component
31
+ def test_new_component
32
+ assert_nothing_raised do
33
+ JTextField.new
34
+ end
35
+ end
36
+
37
+ # test new with block given component
38
+ def test_new_block_given_component
39
+ assert_nothing_raised do
40
+ JTextField.new do |txt|
41
+ # this block will ignore
42
+ flunk "this statement does not executed"
43
+ end
44
+ end
45
+ end
46
+
47
+ # test new with container
48
+ def test_new_container
49
+ assert_nothing_raised do
50
+ JPanel.new
51
+ end
52
+ end
53
+
54
+ # test new with block given container
55
+ def test_new_block_given_container
56
+ is_block_executed = false
57
+
58
+ JPanel.new do |p|
59
+ is_block_executed = true
60
+ end
61
+
62
+ assert_equal(true, is_block_executed)
63
+ end
64
+
65
+ # test new container with component
66
+ def test_new_container_with_component
67
+ assert_nothing_raised do
68
+ JPanel.new do |p|
69
+ txt = JTextField.new
70
+ end
71
+ end
72
+ end
73
+
74
+ # test new nested container with component
75
+ def test_new_nested_container_with_component
76
+ assert_nothing_raised do
77
+ JPanel.new do |p|
78
+ JScrollPane.new do |jsp|
79
+ txt = JTextField.new
80
+ end
81
+ JLabel.new("test")
82
+ end
83
+ end
84
+ end
85
+
86
+ # test container?
87
+ def test_container?
88
+ assert_equal(true, JPanel.container?)
89
+ assert_equal(false, JTextField.container?)
90
+ end
91
+ end
92
+