001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.util.accesslog;
020:
021: import org.apache.jmeter.junit.JMeterTestCase;
022:
023: public class TestLogFilter extends JMeterTestCase {
024:
025: private static final String TESTSTR = "/test/helloworld.html";
026:
027: private static final String TESTSTROUT = "/test/helloworld.jsp";
028:
029: private static class TestData {
030: private final String file;
031:
032: private final boolean exclfile;
033:
034: private final boolean inclfile;
035:
036: private final boolean exclpatt;
037:
038: private final boolean inclpatt;
039:
040: TestData(String f, boolean exf, boolean inf, boolean exp,
041: boolean inp) {
042: file = f;
043: exclfile = exf;
044: inclfile = inf;
045: exclpatt = exp;
046: inclpatt = inp;
047: }
048: }
049:
050: private static final String[] INCL = { "hello.html", "index.html",
051: "/index.jsp" };
052:
053: private static final String[] PATTERNS = { "index", ".jtml" };
054:
055: private static final TestData[] TESTDATA = {
056: // file exclf inclf exclp inclp
057: new TestData("/test/hello.jsp", true, false, true, false),
058: new TestData("/test/one/hello.html", false, true, true,
059: false),
060: new TestData("hello.jsp", true, false, true, false),
061: new TestData("hello.htm", true, false, true, false),
062: new TestData("/test/open.jsp", true, false, true, false),
063: new TestData("/test/open.html", true, false, true, false),
064: new TestData("/index.jsp", false, true, false, true),
065: new TestData("/index.jhtml", true, false, false, true),
066: new TestData("newindex.jsp", true, false, false, true),
067: new TestData("oldindex.jsp", true, false, false, true),
068: new TestData("oldindex1.jsp", true, false, false, true),
069: new TestData("oldindex2.jsp", true, false, false, true),
070: new TestData("oldindex3.jsp", true, false, false, true),
071: new TestData("oldindex4.jsp", true, false, false, true),
072: new TestData("oldindex5.jsp", true, false, false, true),
073: new TestData("oldindex6.jsp", true, false, false, true),
074: new TestData("/test/index.htm", true, false, false, true) };
075:
076: public void testConstruct() {
077: new LogFilter();
078: }
079:
080: private LogFilter testf;
081:
082: public void setUp() {
083: testf = new LogFilter();
084: }
085:
086: public void testReplaceExtension() {
087: testf.setReplaceExtension("html", "jsp");
088: testf.isFiltered(TESTSTR, null);// set the required variables
089: assertEquals(TESTSTROUT, testf.filter(TESTSTR));
090: }
091:
092: public void testExcludeFiles() {
093: testf.excludeFiles(INCL);
094: for (int idx = 0; idx < TESTDATA.length; idx++) {
095: TestData td = TESTDATA[idx];
096: String theFile = td.file;
097: boolean expect = td.exclfile;
098:
099: testf.isFiltered(theFile, null);
100: String line = testf.filter(theFile);
101: if (line != null) {
102: assertTrue("Expect to accept " + theFile, expect);
103: } else {
104: assertFalse("Expect to reject " + theFile, expect);
105: }
106: }
107: }
108:
109: public void testIncludeFiles() {
110: testf.includeFiles(INCL);
111: for (int idx = 0; idx < TESTDATA.length; idx++) {
112: TestData td = TESTDATA[idx];
113: String theFile = td.file;
114: boolean expect = td.inclfile;
115:
116: testf.isFiltered(theFile, null);
117: String line = testf.filter(theFile);
118: if (line != null) {
119: assertTrue("Expect to accept " + theFile, expect);
120: } else {
121: assertFalse("Expect to reject " + theFile, expect);
122: }
123: }
124:
125: }
126:
127: public void testExcludePattern() {
128: testf.excludePattern(PATTERNS);
129: for (int idx = 0; idx < TESTDATA.length; idx++) {
130: TestData td = TESTDATA[idx];
131: String theFile = td.file;
132: boolean expect = td.exclpatt;
133:
134: assertEquals(!expect, testf.isFiltered(theFile, null));
135: String line = testf.filter(theFile);
136: if (line != null) {
137: assertTrue("Expect to accept " + theFile, expect);
138: } else {
139: assertFalse("Expect to reject " + theFile, expect);
140: }
141: }
142: }
143:
144: public void testIncludePattern() {
145: testf.includePattern(PATTERNS);
146: for (int idx = 0; idx < TESTDATA.length; idx++) {
147: TestData td = TESTDATA[idx];
148: String theFile = td.file;
149: boolean expect = td.inclpatt;
150:
151: assertEquals(!expect, testf.isFiltered(theFile, null));
152: String line = testf.filter(theFile);
153: if (line != null) {
154: assertTrue("Expect to accept " + theFile, expect);
155: } else {
156: assertFalse("Expect to reject " + theFile, expect);
157: }
158: }
159: }
160: }
|