Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions test/org/apache/catalina/ssi/TestByteArrayServletOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.ssi;

import org.junit.Assert;
import org.junit.Test;

/**
* Tests for {@link ByteArrayServletOutputStream}.
*/
public class TestByteArrayServletOutputStream {

@Test
public void testWriteAndToByteArray() {
ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();

basos.write('H');
basos.write('i');

byte[] result = basos.toByteArray();
Assert.assertEquals(2, result.length);
Assert.assertEquals('H', result[0]);
Assert.assertEquals('i', result[1]);
}


@Test
public void testEmptyStream() {
ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();

byte[] result = basos.toByteArray();
Assert.assertNotNull(result);
Assert.assertEquals(0, result.length);
}


@Test
public void testMultipleWrites() {
ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();

String text = "Hello, World!";
for (char c : text.toCharArray()) {
basos.write(c);
}

byte[] result = basos.toByteArray();
Assert.assertEquals(text.length(), result.length);
Assert.assertEquals(text, new String(result));
}


@Test
public void testIsReady() {
ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
// Default returns false (as per TODO in source)
Assert.assertFalse(basos.isReady());
}


@Test
public void testSetWriteListener() {
ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
// Should not throw — listener is a no-op
basos.setWriteListener(null);
}
}
230 changes: 230 additions & 0 deletions test/org/apache/catalina/ssi/TestExpressionTokenizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.ssi;

import org.junit.Assert;
import org.junit.Test;

/**
* Tests for {@link ExpressionTokenizer}.
*/
public class TestExpressionTokenizer {

@Test
public void testEmptyExpression() {
ExpressionTokenizer et = new ExpressionTokenizer("");
Assert.assertFalse(et.hasMoreTokens());
Assert.assertEquals(ExpressionTokenizer.TOKEN_END, et.nextToken());
}


@Test
public void testWhitespaceOnlyExpression() {
ExpressionTokenizer et = new ExpressionTokenizer(" ");
Assert.assertFalse(et.hasMoreTokens());
Assert.assertEquals(ExpressionTokenizer.TOKEN_END, et.nextToken());
}


@Test
public void testSimpleString() {
ExpressionTokenizer et = new ExpressionTokenizer("hello");
Assert.assertTrue(et.hasMoreTokens());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("hello", et.getTokenValue());
Assert.assertFalse(et.hasMoreTokens());
}


@Test
public void testBraces() {
ExpressionTokenizer et = new ExpressionTokenizer("(abc)");
Assert.assertEquals(ExpressionTokenizer.TOKEN_LBRACE, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("abc", et.getTokenValue());
Assert.assertEquals(ExpressionTokenizer.TOKEN_RBRACE, et.nextToken());
}


@Test
public void testEquality() {
ExpressionTokenizer et = new ExpressionTokenizer("a = b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("a", et.getTokenValue());
Assert.assertEquals(ExpressionTokenizer.TOKEN_EQ, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("b", et.getTokenValue());
}


@Test
public void testNotEqual() {
ExpressionTokenizer et = new ExpressionTokenizer("a != b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_NOT_EQ, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
}


@Test
public void testNotOperator() {
ExpressionTokenizer et = new ExpressionTokenizer("!abc");
Assert.assertEquals(ExpressionTokenizer.TOKEN_NOT, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("abc", et.getTokenValue());
}


@Test
public void testAndOperator() {
ExpressionTokenizer et = new ExpressionTokenizer("a && b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_AND, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
}


@Test
public void testOrOperator() {
ExpressionTokenizer et = new ExpressionTokenizer("a || b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_OR, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
}


@Test
public void testComparisonOperators() {
ExpressionTokenizer et;

et = new ExpressionTokenizer("a > b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_GT, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());

et = new ExpressionTokenizer("a < b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_LT, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());

et = new ExpressionTokenizer("a >= b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_GE, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());

et = new ExpressionTokenizer("a <= b");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_LE, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
}


@Test
public void testQuotedString() {
ExpressionTokenizer et = new ExpressionTokenizer("\"hello world\"");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("hello world", et.getTokenValue());
}


@Test
public void testSingleQuotedString() {
ExpressionTokenizer et = new ExpressionTokenizer("'hello world'");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("hello world", et.getTokenValue());
}


@Test
public void testQuotedStringWithEscape() {
ExpressionTokenizer et = new ExpressionTokenizer("\"hello\\\"world\"");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("hello\\\"world", et.getTokenValue());
}


@Test
public void testRegexPattern() {
ExpressionTokenizer et = new ExpressionTokenizer("/pattern/");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("/pattern/", et.getTokenValue());
}


@Test
public void testRegexPatternWithEscape() {
ExpressionTokenizer et = new ExpressionTokenizer("/patt\\/ern/");
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("/patt\\/ern/", et.getTokenValue());
}


@Test
public void testComplexExpression() {
ExpressionTokenizer et = new ExpressionTokenizer(
"\"abc\" = \"def\" && !\"ghi\"");

Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("abc", et.getTokenValue());
Assert.assertEquals(ExpressionTokenizer.TOKEN_EQ, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("def", et.getTokenValue());
Assert.assertEquals(ExpressionTokenizer.TOKEN_AND, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_NOT, et.nextToken());
Assert.assertEquals(ExpressionTokenizer.TOKEN_STRING, et.nextToken());
Assert.assertEquals("ghi", et.getTokenValue());
Assert.assertEquals(ExpressionTokenizer.TOKEN_END, et.nextToken());
}


@Test
public void testGetIndex() {
ExpressionTokenizer et = new ExpressionTokenizer("abc");
Assert.assertEquals(0, et.getIndex());
et.nextToken();
Assert.assertEquals(3, et.getIndex());
}


@Test
public void testIsMetaChar() {
ExpressionTokenizer et = new ExpressionTokenizer("");

Assert.assertTrue(et.isMetaChar(' '));
Assert.assertTrue(et.isMetaChar('\t'));
Assert.assertTrue(et.isMetaChar('('));
Assert.assertTrue(et.isMetaChar(')'));
Assert.assertTrue(et.isMetaChar('!'));
Assert.assertTrue(et.isMetaChar('<'));
Assert.assertTrue(et.isMetaChar('>'));
Assert.assertTrue(et.isMetaChar('|'));
Assert.assertTrue(et.isMetaChar('&'));
Assert.assertTrue(et.isMetaChar('='));

Assert.assertFalse(et.isMetaChar('a'));
Assert.assertFalse(et.isMetaChar('0'));
Assert.assertFalse(et.isMetaChar('"'));
}


@Test
public void testTokenValueNullAfterNonString() {
ExpressionTokenizer et = new ExpressionTokenizer("(");
et.nextToken();
Assert.assertNull(et.getTokenValue());
}
}
62 changes: 62 additions & 0 deletions test/org/apache/catalina/ssi/TestSSIConditionalState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.ssi;

import org.junit.Assert;
import org.junit.Test;

/**
* Tests for {@link SSIConditionalState}.
*/
public class TestSSIConditionalState {

@Test
public void testDefaultValues() {
SSIConditionalState state = new SSIConditionalState();

Assert.assertFalse(state.branchTaken);
Assert.assertEquals(0, state.nestingCount);
Assert.assertFalse(state.processConditionalCommandsOnly);
}


@Test
public void testSetBranchTaken() {
SSIConditionalState state = new SSIConditionalState();

state.branchTaken = true;
Assert.assertTrue(state.branchTaken);
}


@Test
public void testSetNestingCount() {
SSIConditionalState state = new SSIConditionalState();

state.nestingCount = 3;
Assert.assertEquals(3, state.nestingCount);
}


@Test
public void testSetProcessConditionalCommandsOnly() {
SSIConditionalState state = new SSIConditionalState();

state.processConditionalCommandsOnly = true;
Assert.assertTrue(state.processConditionalCommandsOnly);
}
}
Loading