/** * {@inheritDoc} */ @Override public void addOperand(AbstractExpression operand) { if (canAddOperand()) { setOperand(operand); } }
/** * {@inheritDoc} */ @Override public boolean canAddOperand() { return getNumberOfChildExpressions() < 1; }
/** * {@inheritDoc} */ @Override public void removeOperand(AbstractExpression operand) { if (Objects.equal(operand, getOperand())) { setOperand(null); } }
@Test public void addDuplicate() { NotExpression orExpression = new NotExpression(); BooleanExpression operand = new BooleanExpression(); orExpression.addOperand(operand); orExpression.addOperand(operand); assertThat(orExpression.getOperands(), hasSize(1)); } }
@Test public void removeTwice() { NotExpression orExpression = new NotExpression(expr1); orExpression.removeOperand(expr1); orExpression.removeOperand(expr1); assertThat(orExpression.getOperands(), hasSize(0)); } }
@Test public void empty() { NotExpression orExpression = new NotExpression(); assertThat(orExpression.getNumberOfChildExpressions(), is(0)); }
@Test public void canAddIfEmpty() { NotExpression orExpression = new NotExpression(); assertThat(orExpression.canAddOperand(), is(true)); }
@Test public void withElements() { NotExpression orExpression = new NotExpression(expr1); assertThat(orExpression.getOperands(), hasSize(1)); assertThat(orExpression.getOperands(), contains((AbstractExpression) expr1)); } }
@Test public void testFalse() { when(expr.evaluate(invocation, cachedDataService)).thenReturn(false); NotExpression notExpression = new NotExpression(expr); boolean evaluationResult = notExpression.evaluate(invocation, cachedDataService); assertThat(evaluationResult, is(true)); } }
/** * Creates an {@link AbstractExpression} for the given {@link BooleanExpressionType}. * * @param type * {@link BooleanExpressionType} determining the specific {@link AbstractExpression}. * @return an {@link AbstractExpression}. */ private static AbstractExpression createBooleanExpression(BooleanExpressionType type) { switch (type) { case AND: return new AndExpression(); case OR: return new OrExpression(); case NOT: return new NotExpression(); case BOOLEAN: return new BooleanExpression(false); default: throw new RuntimeException("Unsupported type!"); } }
/** * {@inheritDoc} */ @Override public List<AbstractExpression> getOperands() { if (null != getOperand()) { return Collections.singletonList(getOperand()); } else { return Collections.emptyList(); } }
@Test public void addTwo() { NotExpression orExpression = new NotExpression(); BooleanExpression firstExpr = new BooleanExpression(); orExpression.addOperand(firstExpr); orExpression.addOperand(new BooleanExpression()); assertThat(orExpression.getOperands(), hasSize(1)); assertThat(orExpression.getOperands(), contains((AbstractExpression) firstExpr)); }
@Test public void removeOne() { NotExpression orExpression = new NotExpression(expr1); orExpression.removeOperand(expr1); assertThat(orExpression.getOperands(), hasSize(0)); }
@Test public void threeElements() { NotExpression orExpression = new NotExpression(expr1); assertThat(orExpression.getNumberOfChildExpressions(), is(1)); } }
@Test public void canAddWithExistingElements() { NotExpression orExpression = new NotExpression(expr1); assertThat(orExpression.canAddOperand(), is(false)); } }
@Test public void empty() { NotExpression orExpression = new NotExpression(); assertThat(orExpression.getOperands(), hasSize(0)); }
@Test public void testTrue() { when(expr.evaluate(invocation, cachedDataService)).thenReturn(true); NotExpression notExpression = new NotExpression(expr); boolean evaluationResult = notExpression.evaluate(invocation, cachedDataService); assertThat(evaluationResult, is(false)); }
/** * {@inheritDoc} */ @Override public boolean evaluate(InvocationSequenceData invocSequence, ICachedDataService cachedDataService) { return null == getOperand() ? false : !getOperand().evaluate(invocSequence, cachedDataService); }
@Test public void add() { NotExpression orExpression = new NotExpression(); orExpression.addOperand(new BooleanExpression()); assertThat(orExpression.getOperands(), hasSize(1)); }
@Test public void removeNotExistent() { NotExpression orExpression = new NotExpression(expr1); orExpression.removeOperand(expr2); assertThat(orExpression.getOperands(), hasSize(1)); }