How to eat an elephant in Java
Elephant elephant = ...; // get the elephant from somewhere while (!elephant.isEatenCompletely()) { elephant.haveOneBite(); }
Why am I writing this? Well, simply to test my dzone account ;)
11345 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Elephant elephant = ...; // get the elephant from somewhere while (!elephant.isEatenCompletely()) { elephant.haveOneBite(); }
//+ Jonas Raoni Soares Silva //@ http://jsfromhell.com/classes/is-point-in-poly [v1.0] function isPointInPoly(poly, pt){ for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y)) && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x) && (c = !c); return c; }
<script type="text/javascript"> //<![CDATA[ points = [ {x: 0, y: 0}, {x: 0, y: 50}, {x: 50, y: 10}, {x: -50, y: -10}, {x: 0, y: -50}, {x: 0, y: 0} ]; alert(isPointInPoly(points, {x: 10, y: 10}) ? "In" : "Out"); //]]> </script>
class Time # this code adds a Time.freeze method to make time "stand still" # during execution of a block. This can be helpful during testing of # methods that depend on Time.new or Time.now # # Example: # # Time.freeze do # puts Time.new.to_f # # ... do stuff; real time passes # puts Time.new.to_f # outputs same time as above # end # # ... time returns to normal # # An optional Time object may be passed to freeze to a specific time: # # Time.freeze(Time.at(2007, 11, 15)) do # # ... # end # # While inside the block, Time.frozen? will return true class << self def now @time || orig_new end alias_method :orig_freeze, :freeze alias_method :orig_new, :new alias_method :new, :now # makes time "stand still" during execution of a block. if no time is # supplied, the current time is used. While in the block, Time.new and # Time.now will always return the "frozen" value. def freeze(time = nil) raise "A block is required" unless block_given? begin prev = @time @time = time || now yield ensure @time = prev end end def frozen? !@time.nil? end end end
namespace :db do namespace :fixtures do desc 'Create YAML test fixtures from data in an existing database. Defaults to development database. Set RAILS_ENV to override.' task :dump_all => :environment do sql = "SELECT * FROM %s" skip_tables = ["schema_info"] ActiveRecord::Base.establish_connection(:development) (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| i = "000" File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file| data = ActiveRecord::Base.connection.select_all(sql % table_name) file.write data.inject({}) { |hash, record| hash["#{table_name}_#{i.succ!}"] = record hash }.to_yaml end end end end namespace :fixtures do desc 'Create YAML test fixtures for references. Defaults to development database. Set RAILS_ENV to override.' task :dump_references => :environment do sql = "SELECT * FROM %s" dump_tables = ["areas","countries"] ActiveRecord::Base.establish_connection(:development) dump_tables.each do |table_name| i = "000" file_name = "#{RAILS_ROOT}/test/fixtures/#{table_name}.yml" p "Fixture save for table #{table_name} to #{file_name}" File.open(file_name, 'w') do |file| data = ActiveRecord::Base.connection.select_all(sql % table_name) file.write data.inject({}) { |hash, record| hash["#{table_name}_#{i.succ!}"] = record hash }.to_yaml end end end end end
#file: test-feed.cgi require 'test_feed' puts "Content-Type: text/xml" puts puts "<test_report>" tf = Test_feed.new() puts tf.plan puts "<actual>" tf.print('create_file', tf.tested) puts "</actual>" puts "</test_report>"
#file: test_feed.rb require 'testdata.rb' require 'feed.rb' include REXML class Test_feed < Testdata def initialize() url = 'http://jamesrobertson.eu/test/feed/' testfile = "testfile.xml" load(url + testfile) @in_filepath = get_input('filepath') @in_project = get_input('project') @in_date = get_input('date') @out_filepath = get_output('filepath') @out_file = get_output('file') end def tested feed = Feed.new feed.create_file(@in_filepath, @in_project) File.exist?(@out_filepath + @out_file) end end #test #test_feed = Test_feed.new() #puts test_feed.tested
#file: testdata.rb require 'net/http' require 'rexml/document' include REXML class Testdata def initialize end def load(url) xml_data = Net::HTTP.get_response(URI.parse(url)).body @doc = Document.new(xml_data) true end def get_input(name) @doc.root.elements['test/inputs/input/' + name].text end def get_output(name) @doc.root.elements['test/outputs/output/' + name].text end def tested?() end def print(method, result) puts "<result method='#{method}'>#{result}</result>" end def plan @doc end end #test #url = 'http://m.jamesrobertson.eu/test/testdata/' #testfile = "testfile.xml" #td = Testdata.new #td.load(url + testfile) #print('get_input', #td.get_input('input1').scan('instring').length > 0) #print('get_output', td.get_output('output1').scan('outstring').length > 0) #puts test_feed.tested?
// insert code here..
truncate ~/www/*/log/*.log
4 22 * * * * truncate -s 0k ~/www/*/log/*.log
warum geht das nicht?