如圖,界面上選擇了檢查點類型,保存後的配置文件中保存的對應編碼爲:java
//斷言類型對應的編碼 private final static int MATCH = 1 << 0; final static int CONTAINS = 1 << 1; private final static int NOT = 1 << 2; private final static int EQUALS = 1 << 3; private final static int SUBSTRING = 1 << 4; 1:表示匹配 2:表明contains 8:表明equals 16:表明substring 6:不包含 5:不匹配 12:不相等 20:非substring
此類位於: org.apache.jmeter.assertions.ResponseAssertion.java正則表達式
//斷言類型的判斷就是根據配置文件中保存的編碼 boolean contains = isContainsType(); //判斷是不是contains斷言 boolean equals = isEqualsType(); //判斷是不是equals斷言 boolean substring = isSubstringType();//判斷是不是Substring斷言 boolean matches = isMatchType();//判斷是不是Match(匹配)斷言 boolean debugEnabled = log.isDebugEnabled(); if (debugEnabled){ log.debug("Type:" + (contains?"Contains":"Match") + (notTest? "(not)": "")); } try { // Get the Matcher for this thread Perl5Matcher localMatcher = JMeterUtils.getMatcher(); PropertyIterator iter = getTestStrings().iterator(); while (iter.hasNext()) { String stringPattern = iter.next().getStringValue(); Pattern pattern = null; if (contains || matches) { pattern = JMeterUtils.getPatternCache().getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK); } boolean found; if (contains) { //能夠看到,若是斷言爲contains,則直接調的爲String中的contains方法而且支持正則表達式 found = localMatcher.contains(toCheck, pattern); } else if (equals) {//若是斷言爲equals,則直接調的String的equals方法,不支持正則 found = toCheck.equals(stringPattern); } else if (substring) { //若是斷言爲Substring,則調用的是indexOf。若是從源碼看,contains使用的也是indexOf作判斷的。indexOf會作必定的匹配,而後把匹配的第一個字符的位置返回,返回的是int類型,若是沒找到,那麼返回-1。之因此有這兩個方法存在,我的感受應該是爲了匹配jdk低版本。indexOf從java1.0就有,而contains從java5.0纔有。 found = toCheck.indexOf(stringPattern) != -1; } else {//其餘類型的判斷和contains就沒有區別了,匹配走的也是這個分支 found = localMatcher.matches(toCheck, pattern); } pass = notTest ? !found : found; if (!pass) { if (debugEnabled){log.debug("Failed: "+stringPattern);} result.setFailure(true); result.setFailureMessage(getFailText(stringPattern,toCheck)); break; } if (debugEnabled){log.debug("Passed: "+stringPattern);} } } catch (MalformedCachePatternException e) { result.setError(true); result.setFailure(false); result.setFailureMessage("Bad test configuration " + e); } return result;
switch (getTestType()) { case CONTAINS: case SUBSTRING: //能夠看出Contains和substring一樣都是contain sb.append(" expected to contain "); break; case NOT | CONTAINS: case NOT | SUBSTRING: sb.append(" expected not to contain "); break; case MATCH: sb.append(" expected to match "); break; case NOT | MATCH: sb.append(" expected not to match "); break; case EQUALS: sb.append(" expected to equal "); break; case NOT | EQUALS: sb.append(" expected not to equal "); break; default:// should never happen... sb.append(" expected something using "); }