Commit b389645bfff58e508ee2a50110ce7a464fc1a999

Authored by Carlos Morais + Eduardo Morais
Committed by Carlos Morais
2 parents e66b33af 7f902334

Merge branch 'update-client' into merging_configuration

Conflicts:
	plugins/mezuro/views/content_viewer/_module_result.rhtml
plugins/mezuro/lib/kalibro/client/module_result_client.rb
... ... @@ -13,7 +13,7 @@ class Kalibro::Client::ModuleResultClient
13 13 def module_result(project_name, module_name, date)
14 14 hash = @port.request(:get_module_result,
15 15 {:project_name => project_name, :module_name => module_name,
16   - :date => date_with_milliseconds(date)})[:module_result]
  16 + :date => Kalibro::Entities::Entity.date_with_milliseconds(date)})[:module_result]
17 17 Kalibro::Entities::ModuleResult.from_hash(hash)
18 18 end
19 19  
... ... @@ -23,11 +23,4 @@ class Kalibro::Client::ModuleResultClient
23 23 Kalibro::Entities::Entity.new.to_entity_array(value, Kalibro::Entities::ModuleResult)
24 24 end
25 25  
26   - private
27   -
28   - def date_with_milliseconds(date)
29   - milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s
30   - date.to_s[0..18] + milliseconds + date.to_s[19..-1]
31   - end
32   -
33 26 end
34 27 \ No newline at end of file
... ...
plugins/mezuro/lib/kalibro/entities/entity.rb
... ... @@ -2,10 +2,19 @@ class Kalibro::Entities::Entity
2 2  
3 3 def self.from_hash(hash)
4 4 entity = self.new
5   - hash.each { |field, value| entity.set(field, value) if field != :attributes!}
  5 + hash.each { |field, value| entity.set(field, value) if is_valid?(field) }
6 6 entity
7 7 end
8 8  
  9 + def self.is_valid?(field)
  10 + field.to_s[0] != '@' and field != :attributes!
  11 + end
  12 +
  13 + def self.date_with_milliseconds(date)
  14 + milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s
  15 + date.to_s[0..18] + milliseconds + date.to_s[19..-1]
  16 + end
  17 +
9 18 def set(field, value)
10 19 send("#{field}=", value) if not field.to_s.start_with? '@'
11 20 end
... ... @@ -55,6 +64,7 @@ class Kalibro::Entities::Entity
55 64 def convert_to_hash(value)
56 65 return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array)
57 66 return value.to_hash if value.is_a?(Kalibro::Entities::Entity)
  67 + return self.class.date_with_milliseconds(value) if value.is_a?(DateTime)
58 68 return 'INF' if value.is_a?(Float) and value.infinite? == 1
59 69 return '-INF' if value.is_a?(Float) and value.infinite? == -1
60 70 value
... ...
plugins/mezuro/lib/kalibro/entities/error.rb
1 1 class Kalibro::Entities::Error < Kalibro::Entities::Entity
2 2  
3   - attr_accessor :message, :stack_trace_element
  3 + attr_accessor :error_class, :message, :stack_trace_element
4 4  
5 5 def stack_trace_element=(value)
6 6 @stack_trace_element = to_entity_array(value, Kalibro::Entities::StackTraceElement)
... ...
plugins/mezuro/lib/kalibro/entities/metric_result.rb
... ... @@ -14,12 +14,17 @@ class Kalibro::Entities::MetricResult &lt; Kalibro::Entities::Entity
14 14 end
15 15 end
16 16  
  17 + def value=(value)
  18 + @value = value.to_f
  19 + end
  20 +
17 21 def range=(value)
18 22 @range = to_entity(value, Kalibro::Entities::Range)
19 23 end
20 24  
21 25 def descendent_result=(value)
22   - @descendent_result = to_entity_array(value)
  26 + array = value.kind_of?(Array) ? value : [value]
  27 + @descendent_result = array.collect {|element| element.to_f}
23 28 end
24 29  
25 30 def descendent_results
... ...
plugins/mezuro/lib/kalibro/entities/module_result.rb
... ... @@ -6,6 +6,15 @@ class Kalibro::Entities::ModuleResult &lt; Kalibro::Entities::Entity
6 6 @module = to_entity(value, Kalibro::Entities::Module)
7 7 end
8 8  
  9 + def date=(value)
  10 + @date = value
  11 + @date = DateTime.parse(value) if value.is_a?(String)
  12 + end
  13 +
  14 + def grade=(value)
  15 + @grade = value.to_f
  16 + end
  17 +
9 18 def metric_result=(value)
10 19 @metric_result = to_entity_array(value, Kalibro::Entities::MetricResult)
11 20 end
... ...
plugins/mezuro/lib/kalibro/entities/native_metric.rb
... ... @@ -10,4 +10,8 @@ class Kalibro::Entities::NativeMetric &lt; Kalibro::Entities::Metric
10 10 @language = languages
11 11 end
12 12  
  13 + def language=(value)
  14 + @language = to_entity_array(value)
  15 + end
  16 +
13 17 end
... ...
plugins/mezuro/lib/kalibro/entities/project_result.rb
... ... @@ -6,6 +6,19 @@ class Kalibro::Entities::ProjectResult &lt; Kalibro::Entities::Entity
6 6 @project = to_entity(value, Kalibro::Entities::Project)
7 7 end
8 8  
  9 + def date=(value)
  10 + @date = value
  11 + @date = DateTime.parse(value) if value.is_a?(String)
  12 + end
  13 +
  14 + def load_time=(value)
  15 + @load_time = value.to_i
  16 + end
  17 +
  18 + def analysis_time=(value)
  19 + @analysis_time = value.to_i
  20 + end
  21 +
9 22 def source_tree=(value)
10 23 @source_tree = to_entity(value, Kalibro::Entities::ModuleNode)
11 24 end
... ...
plugins/mezuro/lib/kalibro/entities/range.rb
... ... @@ -12,4 +12,8 @@ class Kalibro::Entities::Range &lt; Kalibro::Entities::Entity
12 12 @end = 1.0/0.0 if value == "INF"
13 13 end
14 14  
  15 + def grade=(value)
  16 + @grade = value.to_f
  17 + end
  18 +
15 19 end
16 20 \ No newline at end of file
... ...
plugins/mezuro/lib/kalibro/entities/stack_trace_element.rb
... ... @@ -2,4 +2,8 @@ class Kalibro::Entities::StackTraceElement &lt; Kalibro::Entities::Entity
2 2  
3 3 attr_accessor :declaring_class, :method_name, :file_name, :line_number
4 4  
  5 + def line_number=(value)
  6 + @line_number = value.to_i
  7 + end
  8 +
5 9 end
6 10 \ No newline at end of file
... ...
plugins/mezuro/test/fixtures/base_tool_fixtures.rb
  1 +require File.dirname(__FILE__) + '/native_metric_fixtures'
  2 +
1 3 class BaseToolFixtures
2 4  
3 5 def self.analizo
... ...
plugins/mezuro/test/fixtures/compound_metric_fixtures.rb
... ... @@ -4,12 +4,12 @@ class CompoundMetricFixtures
4 4 sc = Kalibro::Entities::CompoundMetric.new
5 5 sc.name = 'Structural Complexity'
6 6 sc.scope = 'CLASS'
7   - sc.script = 'return cbo * lcom4;'
  7 + sc.script = 'return 42;'
8 8 sc
9 9 end
10 10  
11 11 def self.sc_hash
12   - {:name => 'Structural Complexity', :scope => 'CLASS', :script => 'return cbo * lcom4;'}
  12 + {:name => 'Structural Complexity', :scope => 'CLASS', :script => 'return 42;'}
13 13 end
14 14  
15 15 end
... ...
plugins/mezuro/test/fixtures/error_fixtures.rb
... ... @@ -4,6 +4,7 @@ class ErrorFixtures
4 4  
5 5 def self.create
6 6 error = Kalibro::Entities::Error.new
  7 + error.error_class = 'java.lang.Exception'
7 8 error.message = 'Error message from ErrorTest'
8 9 error.stack_trace = [
9 10 StackTraceElementFixtures.create('my method 1', 42),
... ... @@ -12,7 +13,8 @@ class ErrorFixtures
12 13 end
13 14  
14 15 def self.create_hash
15   - {:message => 'Error message from ErrorTest', :stack_trace_element => [
  16 + {:error_class => 'java.lang.Exception', :message => 'Error message from ErrorTest',
  17 + :stack_trace_element => [
16 18 StackTraceElementFixtures.create_hash('my method 1', 42),
17 19 StackTraceElementFixtures.create_hash('my method 2', 84)]}
18 20 end
... ...
plugins/mezuro/test/fixtures/module_node_fixtures.rb
... ... @@ -5,17 +5,17 @@ class ModuleNodeFixtures
5 5 node.module = ModuleFixtures.qt_calculator
6 6 org_node = new_node('org', 'PACKAGE')
7 7 org_node.children = [new_node('org.Window', 'CLASS')]
8   - node.children = [new_node('Dialog', 'CLASS'), new_node('main', 'CLASS'), org_node]
  8 + node.children = [org_node, new_node('Dialog', 'CLASS'), new_node('main', 'CLASS')]
9 9 node
10 10 end
11 11  
12 12 def self.qt_calculator_tree_hash
13 13 {:module => ModuleFixtures.qt_calculator_hash,
14 14 :child => [
15   - {:module => {:name => 'Dialog', :granularity => 'CLASS'}},
16   - {:module => {:name => 'main', :granularity => 'CLASS'}},
17 15 {:module => {:name => 'org', :granularity => 'PACKAGE'},
18   - :child => [{:module => {:name => 'org.Window', :granularity => 'CLASS'}}]}
  16 + :child => [{:module => {:name => 'org.Window', :granularity => 'CLASS'}}]},
  17 + {:module => {:name => 'Dialog', :granularity => 'CLASS'}},
  18 + {:module => {:name => 'main', :granularity => 'CLASS'}}
19 19 ]
20 20 }
21 21 end
... ...
plugins/mezuro/test/fixtures/module_result_fixtures.rb
... ... @@ -18,7 +18,7 @@ class ModuleResultFixtures
18 18  
19 19 def self.create_hash
20 20 {:module => ModuleFixtures.qt_calculator_hash,
21   - :date => DateTime.parse('Thu, 20 Oct 2011 18:26:43.151 +0000'), :grade => 10.0, :metric_result => [
  21 + :date => '2011-10-20T18:26:43.151+00:00', :grade => 10.0, :metric_result => [
22 22 MetricResultFixtures.amloc_result_hash,
23 23 MetricResultFixtures.sc_result_hash],
24 24 :compound_metric_with_error => [CompoundMetricWithErrorFixtures.create_hash]}
... ...
plugins/mezuro/test/fixtures/project_result_fixtures.rb
... ... @@ -15,7 +15,7 @@ class ProjectResultFixtures
15 15 end
16 16  
17 17 def self.qt_calculator_hash
18   - {:project => ProjectFixtures.qt_calculator_hash, :date => ModuleResultFixtures.create.date,
  18 + {:project => ProjectFixtures.qt_calculator_hash, :date => ModuleResultFixtures.create_hash[:date],
19 19 :load_time => 14878, :analysis_time => 1022, :source_tree => ModuleNodeFixtures.qt_calculator_tree_hash}
20 20 end
21 21  
... ...
plugins/mezuro/test/functional/base_tool_client_test.rb
... ... @@ -1,29 +0,0 @@
1   -require "test_helper"
2   -require File.dirname(__FILE__) + '/fake_port'
3   -
4   -class BaseToolClientTest < ActiveSupport::TestCase
5   -
6   - def setup
7   - fake_port = FakePort.new('BaseTool')
8   - Kalibro::Client::Port.expects(:new).with('BaseTool').returns(fake_port)
9   - @client = Kalibro::Client::BaseToolClient.new
10   - end
11   -
12   - should 'get base tool names' do
13   - assert_equal ['Analizo', 'Checkstyle'], @client.base_tool_names
14   - end
15   -
16   - should 'get base tool by name' do
17   - analizo = @client.base_tool('Analizo')
18   - assert_equal 'Analizo', analizo.name
19   - assert_equal 'Analizo description', analizo.description
20   - assert_equal 1, analizo.supported_metrics.size
21   - metric = analizo.supported_metrics[0]
22   - assert_equal 'Analizo', metric.origin
23   - assert_equal 'Analizo metric', metric.name
24   - assert_equal 'Analizo metric description', metric.description
25   - assert_equal 'METHOD', metric.scope
26   - assert_equal ['CPP', 'JAVA'], metric.languages
27   - end
28   -
29   -end
30 0 \ No newline at end of file
plugins/mezuro/test/functional/echo_port_test.rb 0 → 100644
... ... @@ -0,0 +1,88 @@
  1 +require "test_helper"
  2 +
  3 +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/base_tool_fixtures"
  4 +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/configuration_fixtures"
  5 +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/module_result_fixtures"
  6 +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/project_result_fixtures"
  7 +
  8 +class EchoPortTest < ActiveSupport::TestCase
  9 +
  10 + def setup
  11 + @port = Kalibro::Client::Port.new('Echo')
  12 + @port.service_address=('http://valinhos.ime.usp.br:50688/KalibroFake/');
  13 + end
  14 +
  15 + should 'echo base tool' do
  16 + test BaseToolFixtures.analizo, 'BaseTool' do |base_tool|
  17 + base_tool.name = "echo " + base_tool.name
  18 + end
  19 + end
  20 +
  21 + should 'echo configuration' do
  22 + test ConfigurationFixtures.kalibro_configuration, 'Configuration' do |configuration|
  23 + configuration.name = "echo " + configuration.name
  24 + end
  25 + end
  26 +
  27 + should 'echo metric configuration' do
  28 + test_metric_configuration(MetricConfigurationFixtures.amloc_configuration)
  29 + test_metric_configuration(MetricConfigurationFixtures.sc_configuration)
  30 + end
  31 +
  32 + should 'echo module result' do
  33 + test ModuleResultFixtures.create, 'ModuleResult' do |module_result|
  34 + module_result.module.name = "echo." + module_result.module.name
  35 + end
  36 + end
  37 +
  38 + should 'echo project' do
  39 + test(ProjectFixtures.qt_calculator, 'Project') do |project|
  40 + project.name = "echo " + project.name
  41 + end
  42 + end
  43 +
  44 + should 'echo project result' do
  45 + test(ProjectResultFixtures.qt_calculator, 'ProjectResult') do |project_result|
  46 + project_result.project.name = "echo " + project_result.project.name
  47 + end
  48 + end
  49 +
  50 + should 'echo raw project' do
  51 + project = ProjectFixtures.qt_calculator
  52 + echoed = @port.request(:echo_raw_project, {:project => project.to_hash})[:project]
  53 + project.name = "echo " + project.name
  54 + project.state = nil
  55 + project.error = nil
  56 + assert_equal project, Kalibro::Entities::Project.from_hash(echoed)
  57 + end
  58 +
  59 + should 'work with enums' do
  60 + test_granularity("METHOD", "CLASS")
  61 + test_granularity("CLASS", "PACKAGE")
  62 + test_granularity("PACKAGE", "PACKAGE")
  63 + test_granularity("APPLICATION", "APPLICATION")
  64 + end
  65 +
  66 + private
  67 +
  68 + def test_metric_configuration(fixture)
  69 + test fixture, 'MetricConfiguration' do |metric_configuration|
  70 + metric_configuration.code = "echo_" + metric_configuration.code
  71 + end
  72 + end
  73 +
  74 + def test(fixture, entity_name)
  75 + entity_symbol = entity_name.underscore.to_sym
  76 + request_body = {entity_symbol => fixture.to_hash}
  77 + echoed = @port.request("echo_#{entity_symbol}".to_sym, request_body)[entity_symbol]
  78 + yield fixture
  79 + entity_class = "Kalibro::Entities::#{entity_name}".constantize
  80 + assert_equal fixture, entity_class.from_hash(echoed)
  81 + end
  82 +
  83 + def test_granularity(granularity, parent)
  84 + body = {:granularity => granularity}
  85 + assert_equal parent, @port.request(:infer_parent_granularity, body)[:parent_granularity]
  86 + end
  87 +
  88 +end
0 89 \ No newline at end of file
... ...
plugins/mezuro/test/functional/fake_port.rb
... ... @@ -1,7 +0,0 @@
1   -class FakePort < Kalibro::Client::Port
2   -
3   - def initialize(endpoint)
4   - super(endpoint)
5   - self.service_address = 'http://localhost:8080/KalibroFake/'
6   - end
7   -end
plugins/mezuro/test/functional/metric_configuration_client_test.rb
... ... @@ -1,43 +0,0 @@
1   -require "test_helper"
2   -require File.dirname(__FILE__) + '/fake_port'
3   -
4   -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/metric_configuration_fixtures"
5   -
6   -class MetricConfigurationClientTest < ActiveSupport::TestCase
7   -
8   - def setup
9   - fake_port = FakePort.new('MetricConfiguration')
10   - Kalibro::Client::Port.expects(:new).with('MetricConfiguration').returns(fake_port)
11   - @client = Kalibro::Client::MetricConfigurationClient.new
12   - end
13   -
14   - should 'save metric configuration' do
15   - configuration = MetricConfigurationFixtures.amloc_configuration
16   - @client.save(configuration, 'Configuration X')
17   - end
18   -
19   - should 'get metric configuration by name' do
20   - configuration = @client.metric_configuration('C', 'native')
21   - assert_equal 'metricOfC', configuration.code
22   - assert_equal 1.0, configuration.weight
23   - assert_equal 'AVERAGE', configuration.aggregation_form
24   - assert_equal 1, configuration.ranges.size
25   -
26   - range = configuration.ranges[0]
27   - assert_equal -1.0/0.0, range.beginning
28   - assert_equal 1.0/0.0, range.end
29   -
30   - metric = configuration.metric
31   - puts metric
32   - assert metric.is_a?(Kalibro::Entities::NativeMetric)
33   - assert_equal 'Metric of C', metric.name
34   - assert_equal 'METHOD', metric.scope
35   - assert_equal ['JAVA'], metric.languages
36   - assert_equal 'Metric of C description', metric.description
37   - end
38   -
39   - should 'remove metric configuration by name' do
40   - @client.remove('Configuration X', 'Metric X')
41   - end
42   -
43   -end
44 0 \ No newline at end of file
plugins/mezuro/test/unit/kalibro/entities/project_result_test.rb
... ... @@ -27,12 +27,12 @@ class ProjectResultTest &lt; ActiveSupport::TestCase
27 27  
28 28 should 'retrieve module node' do
29 29 node = @result.get_node("main")
30   - assert_equal @hash[:source_tree][:child][1], node.to_hash
  30 + assert_equal @hash[:source_tree][:child][2], node.to_hash
31 31 end
32 32  
33 33 should 'retrive complex module' do
34 34 node = @result.get_node("org.Window")
35   - assert_equal @hash[:source_tree][:child][2][:child].first, node.to_hash
  35 + assert_equal @hash[:source_tree][:child][0][:child].first, node.to_hash
36 36 end
37 37  
38 38 should 'return source tree node when nil is given' do
... ... @@ -44,6 +44,6 @@ class ProjectResultTest &lt; ActiveSupport::TestCase
44 44 end
45 45  
46 46 should 'return correct node when module name is given' do
47   - assert_equal @hash[:source_tree][:child][1], @result.node_of("main").to_hash
  47 + assert_equal @hash[:source_tree][:child][2], @result.node_of("main").to_hash
48 48 end
49 49 end
... ...
plugins/mezuro/views/content_viewer/_module_result.rhtml
... ... @@ -19,7 +19,7 @@
19 19 <% range = metric_result.range %>
20 20 <tr>
21 21 <td><a href="#" data-show=".<%= metric_result.metric.name.delete("() ")%>"><%= metric_result.metric.name %></a></td>
22   - <td><%= metric_result.value.slice(/[0-9]+\.[0-9]{1,2}/) %></td> <!--FIXME: Move to helper eventually-->
  22 + <td><%= "%.02f" % metric_result.value %></td>
23 23 <td><%= metric_result.weight %></td>
24 24 <% if range.nil? %>
25 25 <td></td>
... ... @@ -39,7 +39,7 @@
39 39 <td colspan = "4" align = "right">
40 40 <strong>
41 41 <%= _('Grade:') %>
42   - <%= module_result.grade.slice(/[0-9]+\.[0-9]{1,2}/) %> <!--FIXME: Move to helper eventually -->
  42 + <%= "%.02f" % module_result.grade %>
43 43 </strong>
44 44 </td>
45 45 </tr>
... ...