com.gregmarut.support.beangenerator.rule.RuleBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-beangen Show documentation
Show all versions of test-beangen Show documentation
Supports unit testing by dynamically creating bean objects and populating their fields to default values.
/*******************************************************************************
*
* Copyright (c) 2015 Greg Marut.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Greg Marut - initial API and implementation
*
******************************************************************************/
package com.gregmarut.support.beangenerator.rule;
import com.gregmarut.support.beangenerator.rule.condition.Condition;
import com.gregmarut.support.beangenerator.value.StaticValue;
import com.gregmarut.support.beangenerator.value.Value;
public class RuleBuilder
{
// holds the ruleMapping object for this rule builder to add rules to once they are built
private final RuleMapping ruleMapping;
public RuleBuilder(final RuleMapping ruleMapping)
{
this.ruleMapping = ruleMapping;
}
public RBType forType(final Class clazz)
{
return new RBType(clazz);
}
public class RBType
{
private final Class clazz;
public RBType(final Class clazz)
{
this.clazz = clazz;
}
/**
* Set the condition to use for this rule
*
* @param condition
* @return
*/
public RBWhen when(final Condition condition)
{
return new RBWhen(this, condition);
}
}
public class RBWhen
{
private final RBType rbType;
private final Condition condition;
public RBWhen(final RBType rbType, final Condition condition)
{
// make sure the condition is not null
if (null == condition)
{
throw new IllegalArgumentException("condition cannot be null");
}
this.rbType = rbType;
this.condition = condition;
}
/**
* Set the value to be used when the condition evaluates to true
*
* @param value
*/
public void thenReturn(final T value)
{
thenReturn(new StaticValue(value, rbType.clazz));
}
/**
* Set the value to be used when the condition evaluates to true
*
* @param value
*/
public void thenReturn(final Value value)
{
// make sure the value is not null
if (null == value)
{
throw new IllegalArgumentException("value cannot be null");
}
// build the rule
Rule rule = new Rule(condition, value);
// add this rule to the configuration
ruleMapping.add(rule);
}
}
}