summaryrefslogtreecommitdiff
path: root/lib/custodian/testfactory.rb
diff options
context:
space:
mode:
authorSteve Kemp <steve@steve.org.uk>2012-11-22 00:00:36 +0000
committerSteve Kemp <steve@steve.org.uk>2012-11-22 00:00:36 +0000
commitf15db67993d9ea3f85fbabb60c8746d4d4b5f434 (patch)
tree6dd3a117ae3ad6af7b73b1d77bc08a5f70bf2e94 /lib/custodian/testfactory.rb
parenta39ae5f9734d72bc6101597a33f0594b9d41105a (diff)
Renamed
Diffstat (limited to 'lib/custodian/testfactory.rb')
-rw-r--r--lib/custodian/testfactory.rb87
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/custodian/testfactory.rb b/lib/custodian/testfactory.rb
new file mode 100644
index 0000000..c736ac1
--- /dev/null
+++ b/lib/custodian/testfactory.rb
@@ -0,0 +1,87 @@
+require 'json'
+
+
+#
+#
+# Base class for custodian protocol-tests
+#
+# Each subclass will register themselves, via the call
+# to 'register_test_type'.
+#
+# This class is a factory that will return the correct
+# derived class for a given line from our configuration
+# file - or that line encoded as a JSON string.
+#
+#
+class TestFactory
+
+
+ #
+ # The subclasses we have.
+ #
+ @@subclasses = { }
+
+
+ #
+ # Create a test-type object given a line of text from our parser.
+ #
+ # The line will be like "target must run tcp|ssh|ftp|smtp .."
+ #
+ #
+ def self.create( line )
+
+ #
+ # JSON ?
+ #
+ if ( line =~ /^\{(.*)\}$/ )
+ begin
+ obj = JSON.parse( line );
+ raise ArgumentError, "JSON object was not a hash" unless obj.kind_of?(Hash)
+ line = obj["line"]
+ raise ArgumentError, "obj[:line] was nil" unless (!line.nil?)
+ rescue =>ex
+ raise ArgumentError, "Line did not deserialize from JSON: #{line} - #{ex}"
+ end
+ end
+
+
+ #
+ # If this is an obvious protocol test.
+ #
+ if ( line =~ /must\s+run\s+(\S+)(\s+|\.|$)/ )
+ test_type = $1.dup
+ test_type.chomp!( "." )
+
+ c = @@subclasses[test_type]
+ if c
+ c.new( line )
+ else
+ raise ArgumentError, "Bad test type: #{test_type}"
+ end
+ else
+ raise "Unknown line given - Failed to instantiate a suitable protocol-test."
+ end
+ end
+
+
+ #
+ # Register a new test type - this must be called by our derived classes
+ #
+ def self.register_test_type name
+ @@subclasses[name] = self
+ end
+
+
+ #
+ # Return an array of test-types we know about
+ #
+ # i.e. Derived classes that have registered themselves.
+ #
+ #
+ def types
+ @@subclasses
+ end
+
+
+end
+