<commit_before>require File.expand_path('test_helper', File.dirname(__FILE__))

# FIXME(uwe):  Remove check when we stop supporting Android < 4.0.3
if RubotoTest::ANDROID_OS >= 15

class ArjdbcTest < Minitest::Test
  def setup
    generate_app :bundle => [['activerecord', '<4.0.0'], 'activerecord-jdbcsqlite3-adapter', :sqldroid]
  end

  def teardown
    cleanup_app
  end

  def test_arjdbc
    Dir.chdir APP_DIR do
      File.open('src/ruboto_test_app_activity.rb', 'w'){|f| f << <<EOF}
require 'ruboto/widget'
require 'ruboto/util/stack'
with_large_stack do
  require 'active_record'
end

ruboto_import_widgets :LinearLayout, :ListView, :TextView

class MyArrayAdapter < android.widget.ArrayAdapter
  def get_view(position, convert_view, parent)
    @inflater ||= context.getSystemService(Context::LAYOUT_INFLATER_SERVICE)
    row = convert_view ? convert_view : @inflater.inflate(mResource, nil)
    row.findViewById(mFieldId).text = get_item(position)
    row
  rescue Exception
    puts "Exception getting list item view: \#$!"
    puts $!.backtrace.join("\\n")
    convert_view
  end
end

class RubotoTestAppActivity
  def onCreate(bundle)
    super
    setTitle File.basename(__FILE__).chomp('_activity.rb').split('_').map { |s| "\#{s[0..0].upcase}\#{s[1..-1]}" }.join(' ')

    @adapter = MyArrayAdapter.new(self, android.R.layout.simple_list_item_1 , android.R.id.text1, [])

    self.content_view =
        linear_layout :orientation => LinearLayout::VERTICAL do
          @text_view_margins = text_view :text => 'What hath Matz wrought?', :id => 42
          @list_view = list_view :adapter => @adapter, :id => 43
        end
  end

  def onResume
    super

    db_dir = "\#{application_context.files_dir}/sqlite"

    with_large_stack do

      ActiveRecord::Base.establish_connection(
        :adapter => 'jdbcsqlite3',
        :driver => 'org.sqldroid.SQLDroidDriver',
        :url => "jdbc:sqldroid:\#{db_dir}?timeout=60000&retry=1000",
        :database => db_dir,
      )

      begin
        ActiveRecord::Base.connection.execute "DROP TABLE companions"
      rescue ActiveRecord::StatementInvalid
        # Table does not exist
      end
      ActiveRecord::Base.connection.execute "CREATE TABLE companions (id INTEGER PRIMARY KEY, name VARCHAR(20) NOT NULL)"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (1, 'Frodo')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (2, 'Samwise')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (3, 'Meriadoc')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (4, 'Peregrin')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (5, 'Gandalf')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (6, 'Legolas')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (7, 'Gimli')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (8, 'Aragorn')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (9, 'Boromir')"
      companions = ActiveRecord::Base.connection.execute "SELECT name FROM companions"<commit_msg>* Specify "jdbc" as adapter instead of "jdbcsqlite3"<commit_after>require File.expand_path('test_helper', File.dirname(__FILE__))

# FIXME(uwe):  Remove check when we stop supporting Android < 4.0.3
if RubotoTest::ANDROID_OS >= 15

class ArjdbcTest < Minitest::Test
  def setup
    generate_app :bundle => [['activerecord', '<4.0.0'], 'activerecord-jdbcsqlite3-adapter', :sqldroid]
  end

  def teardown
    cleanup_app
  end

  def test_arjdbc
    Dir.chdir APP_DIR do
      File.open('src/ruboto_test_app_activity.rb', 'w'){|f| f << <<EOF}
require 'ruboto/widget'
require 'ruboto/util/stack'
with_large_stack do
  require 'active_record'
end

ruboto_import_widgets :LinearLayout, :ListView, :TextView

class MyArrayAdapter < android.widget.ArrayAdapter
  def get_view(position, convert_view, parent)
    @inflater ||= context.getSystemService(Context::LAYOUT_INFLATER_SERVICE)
    row = convert_view ? convert_view : @inflater.inflate(mResource, nil)
    row.findViewById(mFieldId).text = get_item(position)
    row
  rescue Exception
    puts "Exception getting list item view: \#$!"
    puts $!.backtrace.join("\\n")
    convert_view
  end
end

class RubotoTestAppActivity
  def onCreate(bundle)
    super
    setTitle File.basename(__FILE__).chomp('_activity.rb').split('_').map { |s| "\#{s[0..0].upcase}\#{s[1..-1]}" }.join(' ')

    @adapter = MyArrayAdapter.new(self, android.R.layout.simple_list_item_1 , android.R.id.text1, [])

    self.content_view =
        linear_layout :orientation => LinearLayout::VERTICAL do
          @text_view_margins = text_view :text => 'What hath Matz wrought?', :id => 42
          @list_view = list_view :adapter => @adapter, :id => 43
        end
  end

  def onResume
    super

    db_dir = "\#{application_context.files_dir}/sqlite"

    with_large_stack do

      ActiveRecord::Base.establish_connection(
        :adapter => 'jdbc',
        :driver => 'org.sqldroid.SQLDroidDriver',
        :url => "jdbc:sqldroid:\#{db_dir}?timeout=60000&retry=1000",
        :database => db_dir,
      )

      begin
        ActiveRecord::Base.connection.execute "DROP TABLE companions"
      rescue ActiveRecord::StatementInvalid
        # Table does not exist
      end
      ActiveRecord::Base.connection.execute "CREATE TABLE companions (id INTEGER PRIMARY KEY, name VARCHAR(20) NOT NULL)"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (1, 'Frodo')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (2, 'Samwise')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (3, 'Meriadoc')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (4, 'Peregrin')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (5, 'Gandalf')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (6, 'Legolas')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (7, 'Gimli')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (8, 'Aragorn')"
      ActiveRecord::Base.connection.execute "INSERT INTO companions VALUES (9, 'Boromir')"
      companions = ActiveRecord::Base.connection.execute "SELECT name FROM companions"
{'fraction_non_alphanumeric': 0.12336652146857499, 'fraction_numerical': 0.012445550715619166, 'mean_word_length': 4.046310832025117, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5069074', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1974, 'n_words': 462}