Skip to content

Commit

Permalink
Sort test members
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Nov 3, 2023
1 parent 98a1c20 commit ac1cbf1
Show file tree
Hide file tree
Showing 81 changed files with 11,872 additions and 11,872 deletions.
354 changes: 177 additions & 177 deletions src/test/java/org/apache/commons/jexl3/AnnotationTest.java

Large diffs are not rendered by default.

138 changes: 69 additions & 69 deletions src/test/java/org/apache/commons/jexl3/AntishCallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,6 @@
@SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
public class AntishCallTest extends JexlTestCase {

public AntishCallTest() {
super("AntishCallTest");
}

/**
* Wraps a class.
*/
public static class ClassReference {
final Class<?> clazz;
ClassReference(final Class<?> c) {
this.clazz = c;
}
}

/**
* Considers any call using a class reference as functor as a call to its constructor.
* <p>Note that before 3.2, a class was not considered a functor.
* @param ref the ClassReference of the class we seek to instantiate
* @param args the constructor arguments
* @return an instance if that was possible
*/
public static Object callConstructor(final JexlEngine engine, final ClassReference ref, final Object... args) {
return callConstructor(engine, ref.clazz, args);
}
public static Object callConstructor(final JexlEngine engine, final Class<?> clazz, final Object... args) {
if (clazz == null || clazz.isPrimitive() || clazz.isInterface()
|| clazz.isMemberClass() || clazz.isAnnotation() || clazz.isArray()) {
throw new ArithmeticException("not a constructible object");
}
JexlEngine jexl = engine;
if (jexl == null) {
jexl = JexlEngine.getThreadEngine();
if (jexl == null) {
throw new ArithmeticException("no engine to solve constructor");
}
}
return jexl.newInstance(clazz, args);
}

/**
* An arithmetic that considers class objects as callable.
*/
Expand All @@ -77,11 +38,11 @@ public CallSupportArithmetic(final boolean strict) {
super(strict);
}

public Object call(final ClassReference clazz, final Object... args) {
public Object call(final Class<?> clazz, final Object... args) {
return callConstructor(null, clazz, args);
}

public Object call(final Class<?> clazz, final Object... args) {
public Object call(final ClassReference clazz, final Object... args) {
return callConstructor(null, clazz, args);
}
}
Expand All @@ -90,10 +51,23 @@ public Object call(final Class<?> clazz, final Object... args) {
* A context that considers class references as callable.
*/
public static class CallSupportContext extends MapContext {
private JexlEngine engine;
CallSupportContext(final Map<String, Object> map) {
super(map);
}
private JexlEngine engine;

public Object call(final Class<?> clazz, final Object... args) {
return callConstructor(engine, clazz, args);
}

public Object call(final ClassReference clazz, final Object... args) {
return callConstructor(engine, clazz, args);
}

CallSupportContext engine(final JexlEngine j) {
engine = j;
return this;
}

@Override public Object get(final String str) {
if (!super.has(str)) {
Expand All @@ -116,30 +90,56 @@ public static class CallSupportContext extends MapContext {
}
return true;
}
}

CallSupportContext engine(final JexlEngine j) {
engine = j;
return this;
/**
* Wraps a class.
*/
public static class ClassReference {
final Class<?> clazz;
ClassReference(final Class<?> c) {
this.clazz = c;
}

public Object call(final ClassReference clazz, final Object... args) {
return callConstructor(engine, clazz, args);
}
public static Object callConstructor(final JexlEngine engine, final Class<?> clazz, final Object... args) {
if (clazz == null || clazz.isPrimitive() || clazz.isInterface()
|| clazz.isMemberClass() || clazz.isAnnotation() || clazz.isArray()) {
throw new ArithmeticException("not a constructible object");
}

public Object call(final Class<?> clazz, final Object... args) {
return callConstructor(engine, clazz, args);
JexlEngine jexl = engine;
if (jexl == null) {
jexl = JexlEngine.getThreadEngine();
if (jexl == null) {
throw new ArithmeticException("no engine to solve constructor");
}
}
return jexl.newInstance(clazz, args);
}

@Test
public void testAntishContextVar() throws Exception {
final Map<String,Object> lmap = new TreeMap<>();
final JexlContext jc = new CallSupportContext(lmap).engine(JEXL);
runTestCall(JEXL, jc);
lmap.put("java.math.BigInteger", new ClassReference(BigInteger.class));
runTestCall(JEXL, jc);
lmap.remove("java.math.BigInteger");
runTestCall(JEXL, jc);
/**
* Considers any call using a class reference as functor as a call to its constructor.
* <p>Note that before 3.2, a class was not considered a functor.
* @param ref the ClassReference of the class we seek to instantiate
* @param args the constructor arguments
* @return an instance if that was possible
*/
public static Object callConstructor(final JexlEngine engine, final ClassReference ref, final Object... args) {
return callConstructor(engine, ref.clazz, args);
}

public AntishCallTest() {
super("AntishCallTest");
}

void runTestCall(final JexlEngine jexl, final JexlContext jc) throws Exception {
final JexlScript check1 = jexl.createScript("var x = java.math.BigInteger; x('1234')");
final JexlScript check2 = jexl.createScript("java.math.BigInteger('4321')");

final Object o1 = check1.execute(jc);
Assert.assertEquals("Result is not 1234", new java.math.BigInteger("1234"), o1);

final Object o2 = check2.execute(jc);
Assert.assertEquals("Result is not 4321", new java.math.BigInteger("4321"), o2);
}

@Test
Expand All @@ -161,15 +161,15 @@ public void testAntishArithmetic() throws Exception {
}
}

void runTestCall(final JexlEngine jexl, final JexlContext jc) throws Exception {
final JexlScript check1 = jexl.createScript("var x = java.math.BigInteger; x('1234')");
final JexlScript check2 = jexl.createScript("java.math.BigInteger('4321')");

final Object o1 = check1.execute(jc);
Assert.assertEquals("Result is not 1234", new java.math.BigInteger("1234"), o1);

final Object o2 = check2.execute(jc);
Assert.assertEquals("Result is not 4321", new java.math.BigInteger("4321"), o2);
@Test
public void testAntishContextVar() throws Exception {
final Map<String,Object> lmap = new TreeMap<>();
final JexlContext jc = new CallSupportContext(lmap).engine(JEXL);
runTestCall(JEXL, jc);
lmap.put("java.math.BigInteger", new ClassReference(BigInteger.class));
runTestCall(JEXL, jc);
lmap.remove("java.math.BigInteger");
runTestCall(JEXL, jc);
}

// JEXL-300
Expand Down
136 changes: 68 additions & 68 deletions src/test/java/org/apache/commons/jexl3/Arithmetic360.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,22 @@ public Arithmetic360(final boolean astrict, final MathContext bigdContext, final
}

/**
* Given a long, attempt to narrow it to an int.
* <p>Narrowing will only occur if no operand is a Long.
* @param lhs the left-hand side operand that lead to the long result
* @param rhs the right-hand side operand that lead to the long result
* @param result the long to narrow
* @return an Integer if narrowing is possible, the original Long otherwise
* Performs a bitwise and.
*
* @param left the left operand
* @param right the right operator
* @return left &amp; right
*/
@Override
protected Number narrowLong(final Object lhs, final Object rhs, final long result) {
if (!(lhs instanceof Long || rhs instanceof Long)) {
final int ir = (int) result;
if (result == ir) {
return ir;
}
public Object and(final Object left, final Object right) {
final Number l = asLongNumber(left);
final Number r = asLongNumber(right);
if (l != null && r != null) {
return narrowLong(left, right, l.longValue() & r.longValue());
}
return result;
return toBigInteger(left).and(toBigInteger(right));
}

/**
* Given a long, attempt to narrow it to an int.
* <p>Narrowing will only occur if the initial operand is not a Long.
* @param operand the operand that lead to the long result
* @param result the long result to narrow
* @return an Integer if narrowing is possible, the original Long otherwise
*/
protected Number narrowLong(final Object operand, final long result) {
if (!(operand instanceof Long)) {
final int ir = (int) result;
if (result == ir) {
return ir;
}
}
return result;
}
/**
* Checks if value class is a number that can be represented exactly in a long.
*
Expand All @@ -80,7 +62,6 @@ protected Number asIntNumber(final Object value) {
? (Number) value
: null;
}

/**
* Casts to Long if possible.
* @param value the Long or else
Expand All @@ -91,66 +72,68 @@ protected Long castLongNumber(final Object value) {
}

/**
* Performs a bitwise and.
* Performs a bitwise complement.
*
* @param left the left operand
* @param right the right operator
* @return left &amp; right
* @param val the operand
* @return ~val
*/
@Override
public Object and(final Object left, final Object right) {
final Number l = asLongNumber(left);
final Number r = asLongNumber(right);
if (l != null && r != null) {
return narrowLong(left, right, l.longValue() & r.longValue());
public Object complement(final Object val) {
final long l = toLong(val);
return narrowLong(val, ~l);
}

/**
* Given a long, attempt to narrow it to an int.
* <p>Narrowing will only occur if the initial operand is not a Long.
* @param operand the operand that lead to the long result
* @param result the long result to narrow
* @return an Integer if narrowing is possible, the original Long otherwise
*/
protected Number narrowLong(final Object operand, final long result) {
if (!(operand instanceof Long)) {
final int ir = (int) result;
if (result == ir) {
return ir;
}
}
return toBigInteger(left).and(toBigInteger(right));
return result;
}

/**
* Performs a bitwise or.
*
* @param left the left operand
* @param right the right operator
* @return left | right
* Given a long, attempt to narrow it to an int.
* <p>Narrowing will only occur if no operand is a Long.
* @param lhs the left-hand side operand that lead to the long result
* @param rhs the right-hand side operand that lead to the long result
* @param result the long to narrow
* @return an Integer if narrowing is possible, the original Long otherwise
*/
@Override
public Object or(final Object left, final Object right) {
final Number l = asLongNumber(left);
final Number r = asLongNumber(right);
if (l != null && r != null) {
return narrowLong(left, right, l.longValue() | r.longValue());
protected Number narrowLong(final Object lhs, final Object rhs, final long result) {
if (!(lhs instanceof Long || rhs instanceof Long)) {
final int ir = (int) result;
if (result == ir) {
return ir;
}
}
return toBigInteger(left).or(toBigInteger(right));
return result;
}

/**
* Performs a bitwise xor.
* Performs a bitwise or.
*
* @param left the left operand
* @param right the right operator
* @return left ^ right
* @return left | right
*/
@Override
public Object xor(final Object left, final Object right) {
public Object or(final Object left, final Object right) {
final Number l = asLongNumber(left);
final Number r = asLongNumber(right);
if (l != null && r != null) {
return narrowLong(left, right, l.longValue() ^ r.longValue());
return narrowLong(left, right, l.longValue() | r.longValue());
}
return toBigInteger(left).xor(toBigInteger(right));
}

/**
* Performs a bitwise complement.
*
* @param val the operand
* @return ~val
*/
@Override
public Object complement(final Object val) {
final long l = toLong(val);
return narrowLong(val, ~l);
return toBigInteger(left).or(toBigInteger(right));
}

/**
Expand Down Expand Up @@ -226,4 +209,21 @@ public Object shiftRightUnsigned(final Object left, final Object right) {
return bl.signum() < 0? bl.negate().shiftRight(r) : bl.shiftRight(r);
}

/**
* Performs a bitwise xor.
*
* @param left the left operand
* @param right the right operator
* @return left ^ right
*/
@Override
public Object xor(final Object left, final Object right) {
final Number l = asLongNumber(left);
final Number r = asLongNumber(right);
if (l != null && r != null) {
return narrowLong(left, right, l.longValue() ^ r.longValue());
}
return toBigInteger(left).xor(toBigInteger(right));
}

}
Loading

0 comments on commit ac1cbf1

Please sign in to comment.