From 9430e3e8aa7173a89d5922cbd3c7b96349852bf6 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Wed, 19 Mar 2014 15:42:22 +0800 Subject: [PATCH] refactor refactor --- .../communication/AgentMessenger.java | 31 ++++++++++++------- .../test/service/Test_TestPlanEngine.java | 8 ++--- .../Test_TestPlanScriptResultService.java | 4 +-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java b/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java index 1376efd4..7c9e0dfb 100644 --- a/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java +++ b/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java @@ -42,7 +42,7 @@ public class AgentMessenger { public RunScenarioResultModel runWithoutParams(Agent agent, RunScenarioModel runScenarioModel) throws IOException { - HttpResponse httpResponse; + HttpResponse httpResponse = null; try { httpResponse = this.httpRequester.sendPostXml(buildBaseUrl(agent) + "/test/runWithoutParams", MarshalHelper.marshal( @@ -53,7 +53,7 @@ public class AgentMessenger { return (RunScenarioResultModel) MarshalHelper.unmarshal( RunScenarioResultModel.class, httpResponse.getContent()); } catch (JAXBException e) { - logger.error(ExceptionLog.getStackTrace(e)); + logIt(httpResponse, e); FaultTolerenceFactory.getRunAgentFault(agent, runScenarioModel) .doTolerance(); return null; @@ -61,15 +61,20 @@ public class AgentMessenger { } + private void logIt(HttpResponse httpResponse, Throwable e) { + logger.error(HttpRequester.isInvalidResponse(httpResponse) ? "" + : httpResponse.getContent(), e); + } + public RunScenarioResultModel submitScenrioWithParams(Agent agent, List paramFiles, final RunScenarioModel runScenarioModel) { + HttpResponse httpResponse = null; try { final String modelCOntent = MarshalHelper .tryMarshal(runScenarioModel); - HttpResponse httpResponse = this.httpRequester.postFiles( - buildBaseUrl(agent) + "/test/submitScenarioWithParams", - "files[]", paramFiles, "scenarioModel", - new LinkedList() { + httpResponse = this.httpRequester.postFiles(buildBaseUrl(agent) + + "/test/submitScenarioWithParams", "files[]", paramFiles, + "scenarioModel", new LinkedList() { private static final long serialVersionUID = 1L; { add(modelCOntent); @@ -81,19 +86,20 @@ public class AgentMessenger { return (RunScenarioResultModel) MarshalHelper.unmarshal( RunScenarioResultModel.class, httpResponse.getContent()); } catch (Exception e) { - logger.error(e, e); + logIt(httpResponse, e); return null; } } public RunScenarioResultModel runWithParams(Agent agent, UUID agentRunId) { + HttpResponse httpResponse = null; try { if (agent == null || agentRunId == null) { return null; } - HttpResponse httpResponse = this.httpRequester.sendPost( - buildBaseUrl(agent) + "/test/runWithParams/" - + agentRunId.toString(), null, null); + httpResponse = this.httpRequester.sendPost(buildBaseUrl(agent) + + "/test/runWithParams/" + agentRunId.toString(), null, + null); if (HttpRequester.isInvalidResponse(httpResponse)) { logger.error("the response is not valid"); return null; @@ -101,13 +107,13 @@ public class AgentMessenger { return (RunScenarioResultModel) MarshalHelper.unmarshal( RunScenarioResultModel.class, httpResponse.getContent()); } catch (Exception e) { - logger.error("runWithParams", e); + logIt(httpResponse, e); return null; } } public AgentBriefStatusModel brief(Agent agent, UUID agentRunId) { - HttpResponse httpResponse; + HttpResponse httpResponse = null; try { if (agent == null || agentRunId == null) { return null; @@ -121,6 +127,7 @@ public class AgentMessenger { return (AgentBriefStatusModel) MarshalHelper.unmarshal( AgentBriefStatusModel.class, httpResponse.getContent()); } catch (Exception e) { + logIt(httpResponse, e); logger.error(e.toString() + " When brief the agent with hostName " + agent.getHostName()); FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance(); diff --git a/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java b/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java index 4bb67fbd..fdd02019 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java +++ b/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java @@ -5,7 +5,6 @@ import java.util.Date; import org.bench4q.master.Main; import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.User; -import org.bench4q.master.domain.service.TestPlanEngine; import org.bench4q.master.test.TestBase_MakeUpTestPlan; import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.helper.TestHelper; @@ -64,7 +63,7 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { TestPlanStatus.valueOf(getTestPlanRepository() .getRunningTestPlanBy(getTestPlanRunIdUuid()) .getCurrentStatus())); - + } @Test @@ -123,9 +122,8 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { getTestPlanRunIdUuid()); testPlan.run(); this.getTestPlanRepository().attachRunningTestPlan(testPlan); - TestHelper.invokePrivate(TestPlanEngine.class, this - .getTestPlanEngine(), "commit" + status.name(), - new Class[] { TestPlan.class }, + TestHelper.invokePrivate(this.getTestPlanEngine(), + "commit" + status.name(), new Class[] { TestPlan.class }, new Object[] { this.getTestPlanRepository() .getRunningTestPlanBy(getTestPlanRunIdUuid()) }); assertEquals(status, diff --git a/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java b/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java index 482de117..8029a755 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.UUID; import org.bench4q.master.domain.entity.User; -import org.bench4q.master.domain.service.TestPlanScriptResultService; import org.bench4q.master.test.TestBase_MakeUpTestPlan; import org.bench4q.share.helper.TestHelper; import org.bench4q.share.models.master.TestPlanModel; @@ -29,7 +28,6 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan { @Test public void testGetSpecficFieldValue() throws Exception { long averageResponseTime = (Long) TestHelper.invokePrivate( - TestPlanScriptResultService.class, this.getTestPlanScriptResultService(), "getSpecificFieldValue", new Class[] { ScriptBriefResultModel.class, String.class }, new Object[] { buildScriptBriefResultModel(0), @@ -122,7 +120,7 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan { this.getTestScriptResultSave().doSaveResult(testPlanRunId, this.getScriptId(), buildScriptBriefResultModel(i), new Date()); - + this.getTestScriptResultSave().doSaveResult(testPlanRunId, this.getScriptId(), new ScriptBehaviorsBriefModel(), new Date());