rcommons 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/COPYING +202 -0
  2. data/Changes.rdoc +4 -0
  3. data/README.rdoc +21 -0
  4. data/Rakefile +277 -0
  5. data/doc/jamis.rb +589 -0
  6. data/lib/commons.rb +44 -0
  7. data/lib/commons/io/filename_utils.rb +54 -0
  8. data/lib/commons/lang/builder/to_s_builder.rb +124 -0
  9. data/lib/commons/lang/builder/to_s_style.rb +493 -0
  10. data/lib/commons/lang/class_loader.rb +77 -0
  11. data/lib/commons/lang/class_utils.rb +50 -0
  12. data/lib/commons/lang/object_utils.rb +61 -0
  13. data/lib/commons/lang/state_error.rb +41 -0
  14. data/lib/commons/lang/system_utils.rb +113 -0
  15. data/lib/commons/lang/time/date_utils.rb +47 -0
  16. data/lib/commons/lang/time/duration_format_utils.rb +279 -0
  17. data/lib/commons/lang/time/stop_watch.rb +207 -0
  18. data/lib/commons/logging/impl/log4r_logger.rb +159 -0
  19. data/lib/commons/logging/impl/log_factory_impl.rb +253 -0
  20. data/lib/commons/logging/impl/no_op_log.rb +59 -0
  21. data/lib/commons/logging/impl/simple_log.rb +251 -0
  22. data/lib/commons/logging/log.rb +106 -0
  23. data/lib/commons/logging/log_configuration_error.rb +40 -0
  24. data/lib/commons/logging/log_factory.rb +177 -0
  25. data/lib/commons/ruby/exception.rb +46 -0
  26. data/lib/commons/ruby/log4r.rb +34 -0
  27. data/lib/commons/ruby/log4r/logger.rb +116 -0
  28. data/lib/commons/ruby/string.rb +55 -0
  29. data/lib/commons/ruby/test/unit.rb +33 -0
  30. data/lib/commons/ruby/test/unit/assertions.rb +50 -0
  31. data/lib/commons/util/properties.rb +84 -0
  32. data/test/commons/io/filename_utils_test.rb +69 -0
  33. data/test/commons/lang/builder/to_s_builder_test.rb +101 -0
  34. data/test/commons/lang/system_utils_test.rb +75 -0
  35. data/test/commons/lang/time/duration_format_utils_test.rb +261 -0
  36. data/test/commons/lang/time/stop_watch_test.rb +232 -0
  37. data/test/commons/logging/impl/log4r_logger_test.rb +133 -0
  38. data/test/commons/logging/log_factory_test.rb +80 -0
  39. data/test/commons/ruby/string_test.rb +64 -0
  40. data/test/commons/util/properties_test.rb +92 -0
  41. data/test/log4r/logger_test.rb +92 -0
  42. metadata +123 -0
@@ -0,0 +1,50 @@
1
+ # = Test::Unit::Assertions Extension
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: assertions.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'test/unit'
34
+
35
+ module Test
36
+ module Unit
37
+
38
+ module Assertions
39
+
40
+ alias assert_true assert
41
+
42
+
43
+ def assert_false(boolean, message = nil)
44
+ assert_true(!boolean, message)
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,84 @@
1
+ # = Properties
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: properties.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ module Commons
34
+ module Util
35
+
36
+ class Properties
37
+ attr_reader :properties
38
+
39
+
40
+ def initialize(defaults = nil)
41
+ @properties = Hash.new
42
+
43
+ if defaults != nil
44
+ @defaults = defaults
45
+ end
46
+ end
47
+
48
+
49
+ def load(file)
50
+ file.each_line {|line|
51
+ line.strip!
52
+ if line != '' && line[0].chr != '#'
53
+ pos = line.index('=')
54
+ if pos != nil && pos > 1
55
+ key = line[0 ... pos].strip
56
+ value = line[pos + 1 .. -1].strip
57
+ if value[0].chr == '"' && value[-1].chr == '"'
58
+ value = value[1 ... -1]
59
+ end
60
+ set_property(key, value)
61
+ end
62
+ end
63
+ }
64
+ end
65
+
66
+
67
+ def property_names
68
+ return @properties.keys
69
+ end
70
+
71
+
72
+ def get_property(key, default_value = nil)
73
+ value = @properties[key]
74
+ return value == nil ? default_value : value
75
+ end
76
+
77
+
78
+ def set_property(key, value)
79
+ @properties[key] = value
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,69 @@
1
+ # = Filename Utilities Test Case
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: filename_utils_test.rb 76 2008-10-12 11:45:33Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/ruby/test/unit'
34
+
35
+ require 'commons/io/filename_utils'
36
+
37
+ module Commons
38
+ module IO
39
+
40
+ class FilenameUtilsTest < Test::Unit::TestCase
41
+
42
+ def setup
43
+ end
44
+
45
+
46
+ def teardown
47
+ end
48
+
49
+
50
+ def test_initialize
51
+ # no test.
52
+ end
53
+
54
+
55
+ def test_absolute?
56
+ assert_true(FilenameUtils.absolute?('/'))
57
+ assert_true(FilenameUtils.absolute?('/var/lib'))
58
+ assert_false(FilenameUtils.absolute?('./bin'))
59
+ assert_false(FilenameUtils.absolute?('../bin'))
60
+ assert_false(FilenameUtils.absolute?('work'))
61
+
62
+ assert_true(FilenameUtils.absolute?('c:/'))
63
+ assert_true(FilenameUtils.absolute?('d:/var/lib'))
64
+ assert_true(FilenameUtils.absolute?('D:\\var\\lib'))
65
+ end
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,101 @@
1
+ # = to_s Builder Test Case
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: to_s_builder_test.rb 76 2008-10-12 11:45:33Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/ruby/test/unit'
34
+
35
+ require 'commons/lang/builder/to_s_builder'
36
+
37
+ module Commons
38
+ module Lang
39
+ module Builder
40
+
41
+ class ToSBuilderTest < Test::Unit::TestCase
42
+
43
+ def setup
44
+ $KCODE = 'UTF8'
45
+ end
46
+
47
+
48
+ def teardown
49
+ $KCODE = 'NONE'
50
+ end
51
+
52
+
53
+ def test_initialize
54
+ # no test.
55
+ end
56
+
57
+
58
+ def test_append
59
+ styles = [
60
+ ToSStyle::DEFAULT_STYLE,
61
+ ToSStyle::MULTI_LINE_STYLE,
62
+ ToSStyle::NO_FIELD_NAMES_STYLE,
63
+ ToSStyle::SHORT_PREFIX_STYLE,
64
+ ToSStyle::SIMPLE_STYLE
65
+ ]
66
+
67
+ styles.each {|style|
68
+ builder = ToSBuilder.new(Object.new, style)
69
+
70
+ cyclic_object = [1,2]
71
+ cyclic_object.push(cyclic_object)
72
+ cyclic_object.push(cyclic_object)
73
+
74
+ builder.append('Nil0', nil)
75
+ builder.append('Nil1', nil, false)
76
+ builder.append('True0', true)
77
+ builder.append('False1', false, false)
78
+ builder.append('Integer0', 100)
79
+ builder.append('Integer1', 101, false)
80
+ builder.append('Float0', 1234.5678)
81
+ builder.append('Float1', 3.141592, false)
82
+ builder.append('String0', 'string0')
83
+ builder.append('String1', 'string1', false)
84
+ builder.append('Hash0', Hash['key1','value1', 'key2','value2'])
85
+ builder.append('Hash1', Hash['key1','value1', 'key2','value2'], false)
86
+ builder.append('Array0', [1,2,3])
87
+ builder.append('Array1', [4,5,6], false)
88
+ builder.append('Array2', [1,2,Hash['name','test', 'flag',true]])
89
+ builder.append('Object0', Object.new)
90
+ builder.append('Object1', Object.new, false)
91
+ builder.append('cyclic_object0', cyclic_object)
92
+ builder.append('cyclic_object1', cyclic_object, false)
93
+
94
+ puts builder.to_s
95
+ }
96
+ end
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,75 @@
1
+ # = System Utilities Test Case
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: system_utils_test.rb 76 2008-10-12 11:45:33Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/ruby/test/unit'
34
+
35
+ require 'commons/lang/system_utils'
36
+
37
+ module Commons
38
+ module Lang
39
+
40
+ class SystemUtilsTest < Test::Unit::TestCase
41
+
42
+ def setup
43
+ end
44
+
45
+
46
+ def teardown
47
+ end
48
+
49
+
50
+ def test_initialize
51
+ # no test.
52
+ end
53
+
54
+
55
+ def test_current_platform
56
+ p SystemUtils.current_platform
57
+ end
58
+
59
+
60
+ def test_line_separator
61
+ p 'line_separator: ' + SystemUtils.line_separator
62
+ end
63
+
64
+
65
+ def test_os_windows?
66
+ if SystemUtils.os_windows?
67
+ p 'windows'
68
+ else
69
+ p 'non-windows'
70
+ end
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,261 @@
1
+ # = Duration Format Utilities Test Case
2
+ #
3
+ # Ruby version 1.8
4
+ #
5
+ # == Authors
6
+ # * Yomei Komiya
7
+ #
8
+ # == Copyright
9
+ # 2008 the original author or authors.
10
+ #
11
+ # == License
12
+ # Apache License 2.0
13
+ #
14
+ # Licensed under the Apache License, Version 2.0 (the "License");
15
+ # you may not use this file except in compliance with the License.
16
+ # You may obtain a copy of the License at
17
+ #
18
+ # http://www.apache.org/licenses/LICENSE-2.0
19
+ #
20
+ # Unless required by applicable law or agreed to in writing, software
21
+ # distributed under the License is distributed on an "AS IS" BASIS,
22
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
+ # See the License for the specific language governing permissions and
24
+ # limitations under the License.
25
+ #
26
+ # == Version
27
+ # SVN: $Id: duration_format_utils_test.rb 68 2008-10-05 05:08:21Z whitestar $
28
+ #
29
+ # == Since
30
+ # File available since Release 0.8.0
31
+
32
+ require 'commons/ruby/test/unit'
33
+
34
+ require 'commons/lang/time/duration_format_utils'
35
+
36
+ module Commons
37
+ module Lang
38
+ module Time
39
+
40
+ class DurationFormatUtilsTest < Test::Unit::TestCase
41
+ Token = DurationFormatUtils::Token
42
+
43
+
44
+ def setup
45
+ $KCODE = 'UTF8'
46
+ end
47
+
48
+
49
+ def teardown
50
+ $KCODE = 'NONE'
51
+ end
52
+
53
+
54
+ def test_initialize
55
+ # no test.
56
+ end
57
+
58
+
59
+ def test_format_duration_hms()
60
+ expected = '9:36:39.504'
61
+ actual = DurationFormatUtils.format_duration_hms(34599504)
62
+ assert_equal(expected, actual)
63
+ end
64
+
65
+
66
+ def test_format_duration()
67
+ format = 'd HH:mm:ss.SSS'
68
+ expected = '13870 09:36:39.504'
69
+ actual = DurationFormatUtils.format_duration(
70
+ 1198402599504, format, true)
71
+ assert_equal(expected, actual)
72
+
73
+ format = 'd日 HH時間mm分ss.SSS秒'
74
+ expected = '13870日 09時間36分39.504秒'
75
+ actual = DurationFormatUtils.format_duration(
76
+ 1198402599504, format, true)
77
+ assert_equal(expected, actual)
78
+ end
79
+
80
+
81
+ def test_format
82
+ tokens = [
83
+ Token.new(DurationFormatUtils::TOKEN_y, 4),
84
+ Token.new('/', 1),
85
+ Token.new(DurationFormatUtils::TOKEN_M, 2),
86
+ Token.new('/', 1),
87
+ Token.new(DurationFormatUtils::TOKEN_d, 2),
88
+ Token.new(' ', 1),
89
+ Token.new(DurationFormatUtils::TOKEN_H, 2),
90
+ Token.new(':', 1),
91
+ Token.new(DurationFormatUtils::TOKEN_m, 2),
92
+ Token.new(':', 1),
93
+ Token.new(DurationFormatUtils::TOKEN_s, 2),
94
+ Token.new('.', 1),
95
+ Token.new(DurationFormatUtils::TOKEN_S, 3)
96
+ ]
97
+
98
+ years = 2005
99
+ months = 3
100
+ days = 4
101
+ hours = 5
102
+ minutes = 5
103
+ seconds = 9
104
+ milliseconds = 25
105
+ pad_with_zeros = true
106
+
107
+ actual = DurationFormatUtils.format(
108
+ tokens,
109
+ years, months, days, hours, minutes, seconds, milliseconds,
110
+ pad_with_zeros)
111
+ assert_equal('2005/03/04 05:05:09.025', actual)
112
+
113
+ milliseconds = 125
114
+ pad_with_zeros = false
115
+ actual = DurationFormatUtils.format(
116
+ tokens,
117
+ years, months, days, hours, minutes, seconds, milliseconds,
118
+ pad_with_zeros)
119
+ assert_equal('2005/3/4 5:5:9.125', actual)
120
+ end
121
+
122
+
123
+ def test_lexx
124
+ actual_tokens = DurationFormatUtils.lexx('H:mm:ss.SSS')
125
+ expected_tokens = [
126
+ Token.new(DurationFormatUtils::TOKEN_H, 1),
127
+ Token.new(':', 1),
128
+ Token.new(DurationFormatUtils::TOKEN_m, 2),
129
+ Token.new(':', 1),
130
+ Token.new(DurationFormatUtils::TOKEN_s, 2),
131
+ Token.new('.', 1),
132
+ Token.new(DurationFormatUtils::TOKEN_S, 3)
133
+ ]
134
+ assert_equal(expected_tokens, actual_tokens)
135
+
136
+ # Multibyte character format
137
+ actual_tokens = DurationFormatUtils.lexx('H時間mm分ss.SSS秒')
138
+ expected_tokens = [
139
+ Token.new(DurationFormatUtils::TOKEN_H, 1),
140
+ Token.new('時間', 1),
141
+ Token.new(DurationFormatUtils::TOKEN_m, 2),
142
+ Token.new('分', 1),
143
+ Token.new(DurationFormatUtils::TOKEN_s, 2),
144
+ Token.new('.', 1),
145
+ Token.new(DurationFormatUtils::TOKEN_S, 3),
146
+ Token.new('秒', 1)
147
+ ]
148
+ assert_equal(expected_tokens, actual_tokens)
149
+ end
150
+ end # DurationFormatUtilsTest
151
+
152
+ end
153
+ end
154
+ end
155
+
156
+ module Commons
157
+ module Lang
158
+ module Time
159
+
160
+ class DurationFormatUtils
161
+
162
+ class TokenTest < Test::Unit::TestCase
163
+ def setup
164
+ end
165
+
166
+
167
+ def teardown
168
+ end
169
+
170
+
171
+ def test_contains_token_with_value?(tokens, value)
172
+ tokens = [
173
+ Token.new('val1'),
174
+ Token.new('val2'),
175
+ Token.new('val3')
176
+ ]
177
+
178
+ assert_true(Token.contains_token_with_value?(tokens, 'val1'))
179
+ assert_true(Token.contains_token_with_value?(tokens, 'val2'))
180
+ assert_true(Token.contains_token_with_value?(tokens, 'val3'))
181
+ assert_false(Token.contains_token_with_value?(tokens, 'noentry'))
182
+ end
183
+
184
+
185
+ def test_initialize(value)
186
+ expected_value = 'dummy'
187
+ expected_count = 5
188
+
189
+ actualToken = Token.new(expected_value, expected_count)
190
+ assert_equal(expected_value, actualToken.value)
191
+ assert_equal(expected_count, actualToken.count)
192
+
193
+ actualToken = Token.new(expected_value)
194
+ assert_equal(expected_value, actualToken.value)
195
+ assert_equal(1, actualToken.count)
196
+ end
197
+
198
+
199
+ def test_increment
200
+ expected_value = 'dummy'
201
+ expected_count = 5
202
+
203
+ actualToken = Token.new(expected_value, expected_count)
204
+ actualToken.increment
205
+ assert_equal(expected_value, actualToken.value)
206
+ assert_equal(expected_count + 1, actualToken.count)
207
+ end
208
+
209
+
210
+ # test ==
211
+ def test_equal_operator
212
+ # no test. see eql? test.
213
+ end
214
+
215
+
216
+ def test_eql?
217
+ token = Token.new('TEST')
218
+ different = Object.new
219
+ assert_false(token.eql?(different))
220
+
221
+ # different count
222
+ other = Token.new('TEST', 5)
223
+ assert_false(token.eql?(other))
224
+
225
+ # value's different class
226
+ one = Token.new(Object.new, 2)
227
+ other = Token.new(100, 2)
228
+ assert_false(one.eql?(other))
229
+
230
+ # value's same class
231
+ value = Object.new
232
+ one = Token.new(value, 2)
233
+ other = Token.new(value, 2)
234
+ assert_true(one.eql?(other))
235
+ end
236
+
237
+
238
+ def test_hash
239
+ expected_value = 'dummy'
240
+ actual_token = Token.new(expected_value)
241
+ assert_equal(expected_value.hash, actual_token.value.hash)
242
+ end
243
+
244
+
245
+ def test_to_s
246
+ actual_token = Token.new(DurationFormatUtils::TOKEN_y, 4)
247
+ assert_equal('yyyy', actual_token.to_s)
248
+ end
249
+
250
+
251
+ def test_to_str
252
+ actual_token = Token.new(DurationFormatUtils::TOKEN_y, 4)
253
+ assert_equal('yyyy', actual_token.to_str)
254
+ end
255
+ end # TokenTest
256
+
257
+ end # DurationFormatUtils
258
+
259
+ end
260
+ end
261
+ end