Skip to content

Commit

Permalink
Add plugin for unit testing and automate part of test
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacrivriv committed Jun 6, 2022
1 parent e1f879d commit 14bf3a3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
<artifactId>xalan</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -176,6 +182,13 @@
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>

<!-- Execute unit tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
</plugin>

<!-- Creates the OSGi MANIFEST.MF file -->
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
package org.apache.taglibs.standard.lang.jstl.test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.taglibs.standard.lang.jstl.Evaluator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
*
Expand All @@ -37,6 +41,7 @@ public static void main(String args[]) throws Exception {
Map<String, Method> m = getSampleMethodMap();
Evaluator e = new Evaluator();
Object o;

o = e.evaluate("", "4", Integer.class, null, null, m, "foo");
System.out.println(o);
o = e.evaluate("", "${4}", Integer.class, null, null, m, "foo");
Expand Down Expand Up @@ -66,6 +71,8 @@ public static void main(String args[]) throws Exception {

}



public static int add(int a, int b) {
return a + b;
}
Expand All @@ -82,6 +89,51 @@ public static Integer getInteger(int i) {
return Integer.valueOf(i);
}

@Test
// Method for running static tests and comparing expected with actual
public void staticTest() throws Exception{
// Enable jakarta.servlet.jsp.functions.allowed for functions to be evaluated
Properties props = System.getProperties();
props.setProperty("jakarta.servlet.jsp.functions.allowed", "true");

Map<String, Method> m = getSampleMethodMap();
Evaluator e = new Evaluator();

// Setup expected values from test
Object[] expected = new Object[]{
4, // Expected for attributeValue 4
4, // Expected for attributeValue ${4}
4, // Expected for attributeValue ${2+2}
5, // Expected for attributeValue ${foo:add(2, 3)}
6, // Expected for attributeValue ${foo:multiply(2, 3)}
5, // Expected for attributeValue ${add(2, 3)}
6, // Expected for attributeValue ${multiply(2, 3)}
10, // Expected for attributeValue ${add(2, 3) + 5}
5, // Expected for attributeValue ${getInt(getInteger(getInt(5)))}
5, // Expected for attributeValue ${getInteger(getInt(getInteger(5)))}
5, // Expected for attributeValue ${getInt(getInt(getInt(5)))}
5 // Expected for attributeValue ${getInteger(getInteger(getInteger(5)))}
};

// Evaluate statements
Object[] actual = new Object[12];
actual[0] = e.evaluate("", "4", Integer.class, null, null, m, "foo");
actual[1] = e.evaluate("", "${4}", Integer.class, null, null, m, "foo");
actual[2] = e.evaluate("", "${2+2}", Integer.class, null, null, m, "foo");
actual[3] = e.evaluate("", "${foo:add(2, 3)}", Integer.class, null, null, m, "foo");
actual[4] = e.evaluate("", "${foo:multiply(2, 3)}", Integer.class, null, null, m, "foo");
actual[5] = e.evaluate("", "${add(2, 3)}", Integer.class, null, null, m, "foo");
actual[6] = e.evaluate("", "${multiply(2, 3)}", Integer.class, null, null, m, "foo");
actual[7] = e.evaluate("", "${add(2, 3) + 5}", Integer.class, null, null, m, "foo");
actual[8] = e.evaluate("", "${getInt(getInteger(getInt(5)))}", Integer.class, null, null, m, "foo");
actual[9] = e.evaluate("", "${getInteger(getInt(getInteger(5)))}", Integer.class, null, null, m, "foo");
actual[10] = e.evaluate("", "${getInt(getInt(getInt(5)))}", Integer.class, null, null, m, "foo");
actual[11] = e.evaluate("", "${getInteger(getInteger(getInteger(5)))}", Integer.class, null, null, m, "foo");

// Verify values
Assertions.assertArrayEquals(expected, actual, "Arrays did not match! Expected: " + Arrays.toString(expected) + " but got: " + Arrays.toString(actual));
}

public static Map<String, Method> getSampleMethodMap() throws Exception {
Map<String, Method> m = new HashMap<>();
Class<?> c = StaticFunctionTests.class;
Expand Down

0 comments on commit 14bf3a3

Please sign in to comment.