gravitext-util 1.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,79 @@
1
+ #--
2
+ # Copyright (c) 2007-2010 David Kellum
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
5
+ # may not use this file except in compliance with the License. You
6
+ # may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
+ # implied. See the License for the specific language governing
14
+ # permissions and limitations under the License.
15
+ #++
16
+
17
+ module Gravitext
18
+
19
+ module HTMap
20
+
21
+ import 'com.gravitext.htmap.UniMap'
22
+
23
+ # Extension to com.gravitext.htmap.UniMap providing convenience
24
+ # methods, including ruby accessors for keys registered in
25
+ # UniMap::KEY_SPACE
26
+ class UniMap
27
+
28
+ # Types requiring explicit construction on set
29
+ EXPLICIT_CTOR_TYPES = [ Java::java.lang.Integer,
30
+ Java::java.lang.Double,
31
+ Java::java.lang.Float ]
32
+
33
+ # Create a new key in UniMap::KEY_SPACE. Useful for ruby-only keys
34
+ # or for keys passed as parameters to Java.
35
+ # ==== Parameters
36
+ # :name<~to_s>:: New key name (lower_case by convention)
37
+ # :vtype<~java_class>:: Java class value type (default: java.lang.Object)
38
+ # ==== Returns
39
+ # com.gravitext.htmap.Key
40
+ def self.create_key( name, vtype = Java::java.lang.Object )
41
+ KEY_SPACE.create_generic( name, vtype.java_class )
42
+ end
43
+
44
+ # Define accessors for each key currently in the KEY_SPACE. To
45
+ # define accessors for keys defined in java, statically reference
46
+ # the containing class before calling this method. As Ruby's
47
+ # define_method is not likely thread safe, invoke during
48
+ # initialization in advance of starting threaded execution. May
49
+ # be called multiple times.
50
+ def self.define_accessors
51
+ KEY_SPACE.keys.each do |key|
52
+
53
+ getter = key.name.downcase
54
+ unless method_defined?( getter )
55
+ define_method( getter ) { get( key ) }
56
+ end
57
+
58
+ setter = getter + '='
59
+ unless method_defined?( setter )
60
+ vtype = key.value_type
61
+ ctype = EXPLICIT_CTOR_TYPES.find { |ct| vtype == ct.java_class }
62
+ if ctype
63
+ define_method( setter ) do |value|
64
+ set( key, ( ctype.new( value ) if value ) )
65
+ end
66
+ else
67
+ define_method( setter ) do |value|
68
+ set( key, value )
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,23 @@
1
+ #--
2
+ # Copyright (c) 2007-2010 David Kellum
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
5
+ # may not use this file except in compliance with the License. You
6
+ # may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
+ # implied. See the License for the specific language governing
14
+ # permissions and limitations under the License.
15
+ #++
16
+
17
+ module Gravitext
18
+ module Util
19
+ VERSION = '1.4.0'
20
+
21
+ LIB_DIR = File.dirname(__FILE__) # :nodoc:
22
+ end
23
+ end
data/pom.xml ADDED
@@ -0,0 +1,115 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2
+
3
+ <modelVersion>4.0.0</modelVersion>
4
+ <groupId>com.gravitext</groupId>
5
+ <artifactId>gravitext-util</artifactId>
6
+ <packaging>jar</packaging>
7
+ <version>1.4.0</version>
8
+ <name>Gravitext Utilities</name>
9
+ <url>http://gravitext.com/oss/gravitext-util</url>
10
+
11
+ <description>A set of common utility packages. Most notably a
12
+ concurrent test facility and a htmap.</description>
13
+
14
+ <developers>
15
+ <developer>
16
+ <id>david</id>
17
+ <name>David Kellum</name>
18
+ <email>dek-oss@[org]</email>
19
+ <organization>Gravitext</organization>
20
+ <organizationUrl>http://gravitext.com</organizationUrl>
21
+ </developer>
22
+ </developers>
23
+
24
+ <licenses>
25
+ <license>
26
+ <name>Apache License, Version 2.0</name>
27
+ <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
28
+ <distribution>repo</distribution>
29
+ </license>
30
+ </licenses>
31
+
32
+ <parent>
33
+ <groupId>com.gravitext</groupId>
34
+ <artifactId>gravitext-parent</artifactId>
35
+ <version>1.5</version>
36
+ <relativePath>../gravitext-parent/pom.xml</relativePath>
37
+ </parent>
38
+
39
+ <distributionManagement>
40
+ <site>
41
+ <id>gravitext-site</id>
42
+ <url>file:///home/david/src/oss/site/gravitext-util</url>
43
+ </site>
44
+ <downloadUrl>http://gravitext.com/repo/releases/gravitext-util</downloadUrl>
45
+ </distributionManagement>
46
+
47
+ <dependencies>
48
+
49
+ <dependency>
50
+ <groupId>org.slf4j</groupId>
51
+ <artifactId>slf4j-api</artifactId>
52
+ <scope>test</scope>
53
+ </dependency>
54
+
55
+ <dependency>
56
+ <groupId>junit</groupId>
57
+ <artifactId>junit</artifactId>
58
+ </dependency>
59
+
60
+ <dependency>
61
+ <groupId>ch.qos.logback</groupId>
62
+ <artifactId>logback-classic</artifactId>
63
+ <optional>true</optional>
64
+ </dependency>
65
+
66
+ </dependencies>
67
+
68
+ <build>
69
+ <plugins>
70
+ <plugin>
71
+ <!-- Parent settings -->
72
+ <groupId>org.codehaus.mojo</groupId>
73
+ <artifactId>cobertura-maven-plugin</artifactId>
74
+ </plugin>
75
+ <plugin>
76
+ <artifactId>maven-jar-plugin</artifactId>
77
+ <configuration>
78
+ <archive>
79
+ <manifest>
80
+ <mainClass>com.gravitext.perftest.Harness</mainClass>
81
+ </manifest>
82
+ </archive>
83
+ </configuration>
84
+ </plugin>
85
+ <plugin>
86
+ <!-- Parent settings -->
87
+ <artifactId>maven-source-plugin</artifactId>
88
+ </plugin>
89
+ <plugin>
90
+ <!-- Parent settings -->
91
+ <artifactId>maven-compiler-plugin</artifactId>
92
+ </plugin>
93
+ </plugins>
94
+ </build>
95
+
96
+ <reporting>
97
+ <plugins>
98
+ <plugin>
99
+ <groupId>org.codehaus.mojo</groupId>
100
+ <artifactId>cobertura-maven-plugin</artifactId>
101
+ </plugin>
102
+
103
+ <plugin>
104
+ <artifactId>maven-javadoc-plugin</artifactId>
105
+ <configuration>
106
+ <links>
107
+ <link>http://java.sun.com/javase/6/docs/api/</link>
108
+ </links>
109
+ <encoding>UTF-8</encoding>
110
+ </configuration>
111
+ </plugin>
112
+ </plugins>
113
+ </reporting>
114
+
115
+ </project>
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env jruby
2
+ #--
3
+ # Copyright (c) 2007-2010 David Kellum
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
6
+ # may not use this file except in compliance with the License. You may
7
+ # obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14
+ # implied. See the License for the specific language governing
15
+ # permissions and limitations under the License.
16
+ #++
17
+
18
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), "..", "lib" )
19
+
20
+ require 'test/unit'
21
+
22
+ require 'gravitext-util'
23
+
24
+ require 'java'
25
+ require 'thread'
26
+
27
+ class TestConcurrent < Test::Unit::TestCase
28
+ include Gravitext
29
+
30
+ class RTestFactory
31
+ include Gravitext::Concurrent
32
+ include TestFactory
33
+ include TestRunnable
34
+ def name
35
+ self.class.name
36
+ end
37
+
38
+ def create_test_runnable( seed )
39
+ self.class.new
40
+ end
41
+
42
+ def run_iteration( run )
43
+ run
44
+ end
45
+ end
46
+
47
+ def test_ruby_test_factory
48
+ tsum = Concurrent.execute_test_factory( RTestFactory.new, 100 )
49
+ csum = (1..100).inject { |sum,i| sum + i }
50
+ assert_equal( csum, tsum )
51
+ end
52
+
53
+ class AssertTestRunnable
54
+ include Gravitext::Concurrent::TestRunnable
55
+ include Test::Unit::Assertions
56
+
57
+ def initialize( random )
58
+ @random = random
59
+ end
60
+
61
+ def run_iteration( run )
62
+ Thread.pass if @random.next_int(3).zero?
63
+ assert_not_equal( 101, run, "run == #{run}" )
64
+ 1
65
+ end
66
+ end
67
+
68
+ def test_assert_runnable
69
+ assert_raise( Test::Unit::AssertionFailedError ) do
70
+ Concurrent.execute_runnable( AssertTestRunnable, 1000, 7 )
71
+ end
72
+ end
73
+
74
+ def test_assert_block
75
+ assert_raise( Test::Unit::AssertionFailedError ) do
76
+ Concurrent.execute_test( 1000, 7 ) do |run, random|
77
+ Thread.pass if random.next_int(3).zero?
78
+ assert_not_equal( 101, run, "run == #{run}" )
79
+ 1
80
+ end
81
+ end
82
+ end
83
+
84
+ def test_block_runnable
85
+ tsum = Concurrent.execute_test( 1000, 3 ) do |run,random|
86
+ 1 + random.next_int( run )
87
+ end
88
+ assert( tsum >= 1000, "#{tsum} < 1000" )
89
+ end
90
+
91
+ end
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env jruby
2
+ #--
3
+ # Copyright (c) 2007-2010 David Kellum
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
6
+ # may not use this file except in compliance with the License. You may
7
+ # obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14
+ # implied. See the License for the specific language governing
15
+ # permissions and limitations under the License.
16
+ #++
17
+
18
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), "..", "lib" )
19
+
20
+ require 'test/unit'
21
+
22
+ require 'gravitext-util/perftest'
23
+
24
+ require 'java'
25
+
26
+ require 'rubygems'
27
+
28
+ require 'rjack-slf4j'
29
+ require 'rjack-logback' #Or, turn off logging: require 'rjack-slf4j/nop'
30
+
31
+ RJack::Logback.config_console( :level => Logback::DEBUG )
32
+
33
+ class TestPerfTest < Test::Unit::TestCase
34
+ include Gravitext::PerfTest
35
+
36
+ def test_listener
37
+ factory = com.gravitext.perftest.tests.EmptyPerfTest.new
38
+ harness = Harness.new( [ factory ] )
39
+ harness.listener = LogListener.new( RJack::SLF4J[ 'TestPerfTest' ] )
40
+
41
+ harness.warmup_exec_target = 0.25
42
+ harness.warmup_total_target = 0.5
43
+ harness.warmup_tolerance = 1.0
44
+ harness.final_exec_target = 0.25
45
+ harness.final_iterations = 2
46
+
47
+ sum = harness.execute.first
48
+ assert_same( factory, sum.factory )
49
+ assert( sum.runs_executed > 0 )
50
+ assert( sum.duration.seconds > 0.0 )
51
+ end
52
+
53
+ end
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env jruby
2
+ #.hashdot.profile += jruby-shortlived
3
+
4
+ #--
5
+ # Copyright (c) 2007-2010 David Kellum
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
8
+ # may not use this file except in compliance with the License. You
9
+ # may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #++
19
+
20
+ require 'rubygems'
21
+
22
+ require 'test/unit'
23
+
24
+ require 'rjack-logback'
25
+ RJack::Logback.config_console
26
+
27
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), "..", "lib" )
28
+
29
+ require 'gravitext-util'
30
+
31
+ class TestUniMap < Test::Unit::TestCase
32
+ include Gravitext::HTMap
33
+
34
+ TEST_KEYS = [ [ 'int_key', Java::java.lang.Integer, 33 ],
35
+ [ 'str_key', Java::java.lang.String, "string" ],
36
+ [ 'cseq_key', Java::java.lang.CharSequence, "other" ],
37
+ [ 'list_key', Java::java.util.List, [ 1, 2, 3 ] ],
38
+ [ 'date_key', Java::java.util.Date, Time.now ],
39
+ [ 'dbl_key', Java::java.lang.Double, 3.1415 ],
40
+ [ 'flt_key', Java::java.lang.Float, 1.5625 ] ] #exact
41
+
42
+ WRONG_TYPE_VALUE = Java::java.lang.RuntimeException.new
43
+
44
+ TEST_KEYS.each do |a|
45
+ a << UniMap.create_key( a[0], a[1] )
46
+ end
47
+
48
+ UniMap.define_accessors
49
+
50
+ LOG = RJack::SLF4J[ name ]
51
+
52
+ ## Manual tests
53
+
54
+ # Full integer test for demonstration
55
+ def test_int_key
56
+ c = UniMap.new
57
+ assert_nil( c.int_key )
58
+ c.int_key = 7
59
+ assert_equal( 7, c.int_key )
60
+ c.int_key = nil
61
+ assert_nil( c.int_key )
62
+ assert_raise NameError, NativeException do
63
+ c.int_key = WRONG_TYPE_VALUE
64
+ end
65
+ end
66
+
67
+ # Java.util.Date comparison is tricky
68
+ def test_date_key
69
+ c = UniMap.new
70
+ c.date_key = now = Time.now
71
+ assert( c.date_key.equals( now ) )
72
+ end
73
+
74
+ # Define test_<type_key> for all not manually defined above
75
+ TEST_KEYS.each do |name, vtype, test_value, key|
76
+ get = name
77
+ set = name + '='
78
+ tmethod = "test_" + name
79
+ unless method_defined?( tmethod )
80
+ define_method( tmethod ) do
81
+ c = UniMap.new
82
+ assert_nil( c.send( get ) )
83
+ assert_nil( c.send( set, test_value ) )
84
+ assert_equal( test_value, c.send( get ) )
85
+ assert_equal( test_value, c.get( key ) )
86
+ assert_equal( test_value, c.send( set, nil ) )
87
+ assert_nil( c.send( get ) )
88
+ assert_raise NameError, NativeException do
89
+ c.send( set, WRONG_TYPE_VALUE )
90
+ end
91
+ assert_nil( c.send( get ) )
92
+ end
93
+ end
94
+ end
95
+
96
+ def test_list_setters
97
+ c = UniMap.new
98
+ LOG.info( "UniMap setters defined: " +
99
+ c.methods.sort.select { |m| m =~ /[^=]=$/ }.join( " " ) )
100
+ end
101
+
102
+ end