class Blufin::ScannerJavaTests

This class scans ALL Java classes and generates blank tests for every class which doesn't have a test. Also throws errors if 'rogue' tests exist and/or tests are named/placed incorrectly.

Constants

TEMPLATE

Public Class Methods

new(site, error_handler) click to toggle source

@return void

# File lib/core/code_scanners/scanner_java_tests.rb, line 33
def initialize(site, error_handler)

    @site                  = Blufin::SiteResolver::validate_site(site)
    @site_name             = Blufin::SiteResolver::get_site_name(@site)
    @site_name_camel_cased = Blufin::SiteResolver::get_site_name_camel_cased(@site)
    @site_path             = Blufin::SiteResolver::get_site_location(@site)
    @site_domain           = Blufin::SiteResolver::get_site_domain(@site)

    @error_handler = error_handler

    @infrastructure_classes = {}
    @infrastructure_tests   = {}

    scan_blufin
    scan_app_infrastructure

    tests_create_and_validate
    tests_rogue_check

end

Private Instance Methods

scan_app_infrastructure() click to toggle source

Scans the /app-infrastructure directory. @return void

# File lib/core/code_scanners/scanner_java_tests.rb, line 83
def scan_app_infrastructure
    base_path = "#{@site_name.gsub('-', '/')}\\/(#{Blufin::SiteServices::REGEX_SERVICES})"
    all_files = Blufin::ScannerCommon::get_non_ignored_files(@site_path, ['java'], @error_handler, Blufin::Site::PATH_APP_INFRASTRUCTURE)
    all_files.each do |file|
        next if file =~ /#{base_path}\/#{@site_name_camel_cased}(#{Blufin::SiteServices::REGEX_SERVICES_CAPITALIZED}).java\z/
        next if file =~ /#{base_path}\/#{@site_name_camel_cased}(#{Blufin::SiteServices::REGEX_SERVICES_CAPITALIZED})Config.java\z/
        next if file =~ /#{base_path}\/health\//
        next if file =~ /#{base_path}\/tasks\//
        class_info = Blufin::ScannerJava::scan(file, @site, @error_handler)
        next unless class_info[Blufin::ScannerJava::TYPE] == Blufin::ScannerJava::TYPE_CLASS
        file_path_split = Blufin::YmlCommon::split_to_path_file(file, @site_path)
        if class_info[Blufin::ScannerJava::TEST]
            @infrastructure_tests[file_path_split[1].gsub('.java', '')] = class_info
        else
            @infrastructure_classes[file_path_split[1].gsub('.java', '')] = class_info
        end
    end
end
scan_blufin() click to toggle source

Scans the /blufin parent java library files. @return void

# File lib/core/code_scanners/scanner_java_tests.rb, line 58
def scan_blufin

    all_files = Blufin::ScannerCommon::get_non_ignored_files(Blufin::Config::get_path('Paths', 'BlufinJava'), ['java'], @error_handler)
    all_files.each do |file|

        # TODO - SCAN BLUFIN-JAVA FILES

        # output_classes = []
        # output_tests = []
        # @infrastructure_classes.each do |key, data|
        #     flags = []
        #     flags << (data[Blufin::ScannerJava::TEST_REQUIRED] ? 'Test Required' : nil)
        #     output_classes << "\x1B[38;5;154m#{key} \x1B[38;5;240m\xe2\x80\x94 #{flags.join('|')}"
        # end
        # @infrastructure_tests.each do |key, data|
        #     output_tests << "\x1B[38;5;196m#{key}\x1B[0m"
        # end
        # Blufin::Terminal::info('Classes', output_classes)
        # Blufin::Terminal::info('Tests', output_tests)
    end

end
tests_create_and_validate() click to toggle source

Checks that Tests are correct. Also creates Tests for classes which need them. @return void

# File lib/core/code_scanners/scanner_java_tests.rb, line 105
def tests_create_and_validate

    @infrastructure_classes.each do |clazz, clazz_info|

        # TODO - TESTS SHOULD NOT HAVE @AutoGenerated + @TestNotSomething annotations... 21-02-2017

        if clazz_info[Blufin::ScannerJava::TEST_REQUIRED]
            test_name       = "#{clazz}Test"
            file            = "#{@site_path}/#{clazz_info[Blufin::ScannerJava::PATH]}/#{test_name}.java"
            file            = Blufin::ScannerCommon::swap_classpath_for_testpath(@site, file)
            contents        = TEMPLATE
            contents        = contents.gsub(PLACEHOLDER_PACKAGE, get_package_from_file(@site, file))
            contents        = contents.gsub(PLACEHOLDER_CLASS, test_name)
            contents_ignore = [
                'import org.junit.Before;',
                'import static org.hamcrest.MatcherAssert.assertThat;',
                'import static org.hamcrest.core.Is.is;',
                'import static org.hamcrest.CoreMatchers.is;',
                'import static org.hamcrest.MatcherAssert.assertThat;',
                '    @Before',
                '    public void setUp() {}',
                '    public void testSomething() {',
                '        assertThat(true, is(true));'
            ]
            Blufin::YmlCommon::validate_file_and_contents(@site, file, Blufin::YmlCommon::convert_string_to_line_array(contents), contents_ignore, @error_handler)
        end

    end

end
tests_rogue_check() click to toggle source

Check for Rogue Tests? @return void

# File lib/core/code_scanners/scanner_java_tests.rb, line 138
def tests_rogue_check

    @infrastructure_tests.each do |clazz, test_info|

        file       = "#{test_info[Blufin::ScannerJava::PATH]}/#{clazz}"
        class_info = @infrastructure_classes[clazz.gsub(/Test\z/, '')]

        if class_info.nil?
            @error_handler.add_error(Blufin::YmlErrorHandler::TEST_NOT_EXPECTED, file, nil, nil, clazz) unless test_info[Blufin::ScannerJava::TEST_NOT_RELATED_TO_CLASS]
        else
            @error_handler.add_error(Blufin::YmlErrorHandler::TEST_NOT_EXPECTED, file, nil, nil, clazz) unless class_info[Blufin::ScannerJava::TEST_REQUIRED]
        end

    end

end