/*
* Copyright (c) 2014 Tom Parker <thpr@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package plugin.lsttokens.datacontrol;
import java.net.URI;
import java.net.URISyntaxException;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import pcgen.cdom.content.ContentDefinition;
import pcgen.cdom.content.fact.FactDefinition;
import pcgen.core.Campaign;
import pcgen.persistence.PersistenceLayerException;
import pcgen.persistence.lst.CampaignSourceEntry;
import pcgen.rules.context.ConsolidatedListCommitStrategy;
import pcgen.rules.context.LoadContext;
import pcgen.rules.context.RuntimeLoadContext;
import pcgen.rules.context.RuntimeReferenceContext;
import plugin.lsttokens.testsupport.TokenRegistration;
public class RequiredTokenTest extends TestCase
{
static RequiredToken token = new RequiredToken();
ContentDefinition cd;
protected LoadContext context;
private static boolean classSetUpFired = false;
protected static CampaignSourceEntry testCampaign;
@BeforeClass
public static final void classSetUp() throws URISyntaxException
{
testCampaign =
new CampaignSourceEntry(new Campaign(), new URI(
"file:/Test%20Case"));
classSetUpFired = true;
}
@Override
@Before
public void setUp() throws PersistenceLayerException, URISyntaxException
{
if (!classSetUpFired)
{
classSetUp();
}
TokenRegistration.clearTokens();
TokenRegistration.register(token);
resetContext();
}
protected void resetContext()
{
URI testURI = testCampaign.getURI();
context =
new RuntimeLoadContext(new RuntimeReferenceContext(),
new ConsolidatedListCommitStrategy());
context.setSourceURI(testURI);
context.setExtractURI(testURI);
cd = new FactDefinition();
}
@Test
public void testInvalidInputNullString() throws PersistenceLayerException
{
assertFalse(token.parseToken(context, cd, null).passed());
}
@Test
public void testInvalidInputEmptyString() throws PersistenceLayerException
{
assertFalse(token.parseToken(context, cd, "").passed());
}
@Test
public void testValidStringYes() throws PersistenceLayerException
{
assertNull(cd.getRequired());
assertTrue(token.parseToken(context, cd, "YES").passed());
assertNotNull(cd.getRequired());
assertTrue(cd.getRequired().booleanValue());
String[] unparsed = token.unparse(context, cd);
assertNotNull(unparsed);
assertEquals(1, unparsed.length);
assertEquals("YES", unparsed[0]);
}
@Test
public void testValidStringNo() throws PersistenceLayerException
{
assertNull(cd.getRequired());
assertTrue(token.parseToken(context, cd, "NO").passed());
assertNotNull(cd.getRequired());
assertFalse(cd.getRequired().booleanValue());
String[] unparsed = token.unparse(context, cd);
assertNotNull(unparsed);
assertEquals(1, unparsed.length);
assertEquals("NO", unparsed[0]);
}
}