diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/MainTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/MainTest.java index 62dba6dd..9698bd1a 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/MainTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/MainTest.java @@ -3,6 +3,9 @@ package org.bench4q.agent.test; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Properties; import org.bench4q.agent.Main; @@ -151,4 +154,205 @@ public class MainTest { } return num; } + + @Test + public void testRegular() { + assertTrue(isMatch("aab", "c*a*b")); + assertTrue(isMatch("aaaa", ".*")); + assertTrue(isMatch("aa", "a*")); + assertFalse(isMatch("aaaa", "aa")); + assertTrue(isMatch("aaa", "a.a")); + assertTrue(isMatch("aaa", "a*a")); + assertFalse(isMatch("aaba", "ab*a*c*a")); + assertTrue(isMatch("", "a*")); + assertTrue(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s")); + assertFalse(isMatch("", "..*")); + assertTrue(isMatch("ba", ".*.")); + } + + public boolean isMatch(String s, String p) { + if (s == null || p == null) { + return false; + } + if (p.length() == 0 && s.length() == 0) { + return true; + } + + List regularParts = getRegularParts(p); + List startPointRanges = buildOneItemList(new StartPointRange( + 0, 0)); + for (int i = 0; i < regularParts.size(); i++) { + startPointRanges = matchForNextStartRanges(s, startPointRanges, + regularParts.get(i)); + if (startPointRanges.size() == 0) { + return false; + } + } + if (startPointRanges.get(startPointRanges.size() - 1).end != s.length()) { + return false; + } + return true; + } + + private List matchForNextStartRanges(String toBeMatched, + List startPointRanges, RegularPart regularPart) { + List resut = new ArrayList(); + for (StartPointRange startRange : startPointRanges) { + resut.addAll(matchForNextStartRange(toBeMatched, startRange, + regularPart)); + } + return resut; + } + + private List matchForNextStartRange(String toBeMatched, + StartPointRange thisRange, RegularPart regularPart) { + List result = new ArrayList(); + if (!regularPart.hasAsterisk) { + if (regularPart.regularContent.equals(".")) { + if (thisRange.begin > toBeMatched.length() - 1) { + return new ArrayList(); + } + return buildOneItemList(new StartPointRange( + thisRange.begin + 1, thisRange.end + 1)); + } else { + return doForNotAsteriskNotDot(toBeMatched, thisRange, + regularPart); + } + } else { + if (regularPart.regularContent.equals(".")) { + return buildOneItemList(new StartPointRange(thisRange.begin, + toBeMatched.length() - 1)); + } else { + result.addAll(doForAsteriskNotDot(toBeMatched, thisRange, + regularPart)); + return result; + } + } + } + + private List doForNotAsteriskNotDot(String toBeMatched, + StartPointRange thisRange, RegularPart regularPart) { + List result = new ArrayList(); + for (int i = thisRange.begin; i <= thisRange.end; i++) { + int endIndex = i + regularPart.regularContent.length(); + if (endIndex > toBeMatched.length()) { + break; + } + String substring = toBeMatched.substring(i, endIndex); + if (substring.equals(regularPart.regularContent)) { + result.add(new StartPointRange(i + 1, endIndex)); + } + } + return result; + } + + private List doForAsteriskNotDot(String toBeMatched, + StartPointRange thisRange, RegularPart regularPart) { + List result = new ArrayList(); + int begin = -1, end = -1; + for (int i = thisRange.begin; i < toBeMatched.length(); i++) { + if (toBeMatched.charAt(i) == regularPart.regularContent.charAt(0)) { + if (begin == -1 && i <= thisRange.end) { + begin = i; + } + } else { + if (begin == -1) { + continue; + } + end = i; + result.add(new StartPointRange(begin, end)); + begin = -1; + end = -1; + if (i > thisRange.end) { + break; + } + } + } + if (begin > -1 && end == -1) { + result.add(new StartPointRange(begin, toBeMatched.length())); + } + if (result.size() == 0) { + result.add(thisRange); + } + return result; + } + + private List buildOneItemList( + StartPointRange startPointRange) { + List result = new ArrayList(); + result.add(startPointRange); + return result; + } + + /** + * It do it in a reverse way + * + * @param regularExpression + * @return + */ + + private List getRegularParts(String regularExpression) { + List result = new ArrayList(); + if (regularExpression == null || regularExpression.isEmpty()) { + return result; + } + int index = regularExpression.length() - 1; + char[] array = regularExpression.toCharArray(); + StringBuffer temp = new StringBuffer(); + for (; index >= 0; index--) { + if (array[index] == '*') { + if (index == 0) { + temp.append(array[index]); + } else { + if (temp.length() != 0) { + result.add(new RegularPart(temp.reverse().toString(), + false)); + temp.delete(0, temp.length()); + } + result.add(new RegularPart(new String( + new char[] { array[index - 1] }), true)); + index--; + } + } else if (array[index] == '.') { + if (temp.length() != 0) { + result.add(new RegularPart(temp.reverse().toString(), false)); + temp.delete(0, temp.length()); + } + result.add(new RegularPart(new String( + new char[] { array[index] }), false)); + } else { + temp.append(array[index]); + } + } + if (temp.length() > 0) { + result.add(new RegularPart(temp.reverse().toString(), false)); + } + Collections.reverse(result); + return result; + } + + static class StartPointRange { + int begin; + int end; + + StartPointRange() { + StartPointRange.this.begin = -1; + StartPointRange.this.end = -1; + } + + StartPointRange(int begin, int end) { + StartPointRange.this.begin = begin; + StartPointRange.this.end = end; + } + } + + static class RegularPart { + public String regularContent; + public boolean hasAsterisk; + + RegularPart(String content, boolean hasAsterisk) { + this.regularContent = content; + this.hasAsterisk = hasAsterisk; + } + } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java index df72fe2d..b1eab992 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java @@ -4,7 +4,7 @@ import java.util.List; import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.repository.AgentRepository; -import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -16,18 +16,18 @@ public class AgentService { public static int AGENT_STATUS_BreakDown = 4; private AgentRepository agentRepository; private Object AGENT_LOCK; - private HighAvailablePoolImpl highAvailablePool; + private HighAvailablePool highAvailablePool; public AgentService() { this.setAgentLock(new Object()); } - private HighAvailablePoolImpl getHighAvailablePool() { + private HighAvailablePool getHighAvailablePool() { return highAvailablePool; } @Autowired - private void setHighAvailablePool(HighAvailablePoolImpl highAvailablePool) { + private void setHighAvailablePool(HighAvailablePool highAvailablePool) { this.highAvailablePool = highAvailablePool; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java index 203c9302..441bced9 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java @@ -20,7 +20,7 @@ import org.bench4q.master.domain.valueobject.schedulscript.TaskCompleteCallback; import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.bench4q.master.infrastructure.highavailable.CurrentLoadObserver; -import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.models.master.TestPlanModel; import org.hibernate.criterion.Criterion; @@ -32,7 +32,7 @@ import org.springframework.stereotype.Component; @Component public class TestPlanEngine implements TaskCompleteCallback, CurrentLoadObserver { - private HighAvailablePoolImpl haPool; + private HighAvailablePool haPool; private TestPlanRepository testPlanRepository; private TestPlanFactory testPlanFactory; private ScheduledExecutorService scheduleExecutor; @@ -57,12 +57,12 @@ public class TestPlanEngine implements TaskCompleteCallback, this.testPlanFactory = testPlanFactory; } - public HighAvailablePoolImpl getHaPool() { + private HighAvailablePool getHaPool() { return haPool; } @Autowired - private void setHaPool(HighAvailablePoolImpl haPool) { + private void setHaPool(HighAvailablePool haPool) { this.haPool = haPool; this.getHaPool().setObserver(this); } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java index 142abef3..eeb3fa95 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java @@ -16,18 +16,20 @@ import org.bench4q.master.domain.valueobject.transaction.exception.ScriptLoadDis import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.infrastructure.communication.AgentMessenger; +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter; import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.StopTestModel; +import org.springframework.beans.factory.annotation.Autowired; public abstract class ScriptLoadBase implements Transaction { private RunningScriptInterface runningScript; private UUID testPlanRunID; private List agentListThisTime; private AgentMessenger runningAgentService; - private HighAvailablePoolImpl highAvailableAgentPool; + private HighAvailablePool highAvailableAgentPool; private static Logger logger = Logger .getLogger(ScriptLoadApplication.class); @@ -64,12 +66,13 @@ public abstract class ScriptLoadBase implements Transaction { this.runningAgentService = runningAgentService; } - private HighAvailablePoolImpl getHighAvailableAgentPool() { + private HighAvailablePool getHighAvailableAgentPool() { return highAvailableAgentPool; } + @Autowired private void setHighAvailableAgentPool( - HighAvailablePoolImpl highAvailableAgentPool) { + HighAvailablePool highAvailableAgentPool) { this.highAvailableAgentPool = highAvailableAgentPool; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java index d49b47cb..6c455715 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java @@ -5,21 +5,22 @@ import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlanScript; import org.bench4q.master.domain.valueobject.transaction.Transaction; import org.bench4q.master.helper.ApplicationContextHelper; +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; import org.bench4q.share.enums.master.TestPlanStatus; public class TestPlanLoadApplication implements Transaction { private TestPlan testPlan; - private HighAvailablePoolImpl highAvailableAgentPool; + private HighAvailablePool highAvailableAgentPool; private static Logger logger = Logger .getLogger(TestPlanLoadApplication.class); - private HighAvailablePoolImpl getHighAvailableAgentPool() { + private HighAvailablePool getHighAvailableAgentPool() { return highAvailableAgentPool; } private void setHighAvailableAgentPool( - HighAvailablePoolImpl highAvailableAgentPool) { + HighAvailablePool highAvailableAgentPool) { this.highAvailableAgentPool = highAvailableAgentPool; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java index 6a679530..beb32ce0 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java @@ -2,17 +2,18 @@ package org.bench4q.master.infrastructure.faultolerence; import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.helper.ApplicationContextHelper; +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; public class BriefAgentFault implements FaultTolerance { - private HighAvailablePoolImpl haPool; + private HighAvailablePool haPool; private Agent agent; - private HighAvailablePoolImpl getHaPool() { + private HighAvailablePool getHaPool() { return haPool; } - private void setHaPool(HighAvailablePoolImpl haPool) { + private void setHaPool(HighAvailablePool haPool) { this.haPool = haPool; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java index fdcbe597..e32cfa94 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java @@ -2,14 +2,18 @@ package org.bench4q.master.infrastructure.highavailable; import java.util.Collection; import java.util.Map; +import java.util.UUID; import org.bench4q.master.domain.RunningScriptInterface; import org.bench4q.master.domain.entity.Agent; +import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter; import org.bench4q.share.models.agent.ServerStatusModel; public interface HighAvailablePool { public Map getPool(); + public void add(Agent agent); + public Long getMaxAvailableLoad(); public int getCurrentAvailableLoad(); @@ -23,4 +27,7 @@ public interface HighAvailablePool { public void cleanUpAboutTestPlan( final Collection runningScripts); + public void setObserver(CurrentLoadObserver currentLoadObserver); + + public Map getAgentRunBlotters(); } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java index eefa5beb..11fc68fe 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java @@ -19,7 +19,7 @@ import org.bench4q.master.domain.service.TestPlanService; import org.bench4q.master.domain.service.UserService; import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.infrastructure.communication.AgentMessenger; -import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.master.test.controller.TestBase; import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.models.master.MonitorModel; @@ -38,7 +38,7 @@ public class TestBase_MakeUpTestPlan extends TestBase { private UserService userService; private UserRepository userRepository; private ScriptService scriptService; - private HighAvailablePoolImpl haPool; + private HighAvailablePool haPool; private AgentRepository agentRepository; private AgentMessenger agentMessenger; private TestPlanEngine testPlanEngine; @@ -130,12 +130,12 @@ public class TestBase_MakeUpTestPlan extends TestBase { this.testPlanRepository = testPlanRepository; } - protected HighAvailablePoolImpl getHaPool() { + protected HighAvailablePool getHaPool() { return haPool; } @Autowired - protected void setHaPool(HighAvailablePoolImpl haPool) { + protected void setHaPool(HighAvailablePool haPool) { this.haPool = haPool; } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/Test_InterfaceInjection.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/Test_InterfaceInjection.java new file mode 100644 index 00000000..bea9c99f --- /dev/null +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/Test_InterfaceInjection.java @@ -0,0 +1,22 @@ +package org.bench4q.master.test; + +import static org.junit.Assert.*; + +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:service-test-context.xml" }) +public class Test_InterfaceInjection { + @Autowired + private HighAvailablePool highAvailablePool; + + @Test + public void test() { + assertNotNull(this.highAvailablePool); + } +} diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_AgentService.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_AgentService.java index 1bba8d80..3bfa3a90 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_AgentService.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_AgentService.java @@ -5,7 +5,7 @@ import static org.junit.Assert.*; import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.service.AgentService; -import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; +import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -20,7 +20,7 @@ public class Test_AgentService { private static final String hostNameForTest = "133.133.12.5"; private AgentService agentService; private AgentRepository agentRepoitory; - private HighAvailablePoolImpl highAvailablePool; + private HighAvailablePool highAvailablePool; private AgentService getAgentService() { return agentService; @@ -40,12 +40,12 @@ public class Test_AgentService { this.agentRepoitory = agentRepoitory; } - private HighAvailablePoolImpl getHighAvailablePool() { + private HighAvailablePool getHighAvailablePool() { return highAvailablePool; } @Autowired - private void setHighAvailablePool(HighAvailablePoolImpl highAvailablePool) { + private void setHighAvailablePool(HighAvailablePool highAvailablePool) { this.highAvailablePool = highAvailablePool; } diff --git a/Bench4Q-Master/src/test/java/stubs/HighAvailableImpl.java b/Bench4Q-Master/src/test/java/stubs/HighAvailableImpl.java index 0da54ef3..67bdb357 100644 --- a/Bench4Q-Master/src/test/java/stubs/HighAvailableImpl.java +++ b/Bench4Q-Master/src/test/java/stubs/HighAvailableImpl.java @@ -3,10 +3,13 @@ package stubs; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.UUID; import org.bench4q.master.domain.RunningScriptInterface; import org.bench4q.master.domain.entity.Agent; +import org.bench4q.master.infrastructure.highavailable.CurrentLoadObserver; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; +import org.bench4q.master.infrastructure.highavailable.impl.AgentRunBlotter; import org.bench4q.share.models.agent.ServerStatusModel; public class HighAvailableImpl implements HighAvailablePool { @@ -62,4 +65,22 @@ public class HighAvailableImpl implements HighAvailablePool { return null; } + @Override + public void add(Agent agent) { + // TODO Auto-generated method stub + + } + + @Override + public void setObserver(CurrentLoadObserver currentLoadObserver) { + // TODO Auto-generated method stub + + } + + @Override + public Map getAgentRunBlotters() { + // TODO Auto-generated method stub + return null; + } + }