Skip to content

Commit

Permalink
OS-538: Solved the bug, when PortraitToken path was encoded (#6922)
Browse files Browse the repository at this point in the history
* OS-538: Solved the bug, when PortraitToken path was encoded, and thus the image export was broken.
PortraitTokenTest now uses JUnit5 assertions
TestHelper got a helper method "evaluateToken", that was used by other test-classes. This is a small refactoring to avoid the duplicated code.

Signed-off-by: Vest <[email protected]>

* OS-538: corrected the test assertion calls to support JUnit 5;
Added the "final" keyword to the token.

* OS-538: Removed unused Exception.

---------

Signed-off-by: Vest <[email protected]>
  • Loading branch information
Vest authored Sep 4, 2023
1 parent 4009d33 commit 2b4d8e4
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 144 deletions.
16 changes: 13 additions & 3 deletions code/src/java/plugin/exporttokens/PortraitToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* The Class {@code PortraitToken} supports the PORTRAIT
* token and its and PORTRAIT.THUMB variant.
*
*
*
*/
public class PortraitToken extends AbstractExportToken
{
Expand All @@ -50,6 +50,16 @@ public String getTokenName()
return "PORTRAIT";
}

/**
* True if the token should be encoded during the export
* @return False because the Portrait path must be unchanged
*/
@Override
public boolean isEncoded()
{
return false;
}

//TODO: Move this to a token that has all of the descriptive stuff about a character
@Override
public String getToken(String tokenSource, CharacterDisplay display, ExportHandler eh)
Expand Down Expand Up @@ -113,9 +123,9 @@ private String getThumbnailToken(CharacterDisplay display)
}

/**
* Generate a thumbnail image based on the character's portrait and
* Generate a thumbnail image based on the character's portrait and
* the thumnbnail rectangle.
*
*
* @param display The character being output.
* @return The thumbnail image, or null if not defined.
*/
Expand Down
97 changes: 42 additions & 55 deletions code/src/test/pcgen/io/ExportHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static pcgen.util.TestHelper.evaluateToken;

import java.io.BufferedWriter;
import java.io.File;
Expand Down Expand Up @@ -91,7 +92,7 @@ protected void setUp() throws Exception
LoadContext context = Globals.getContext();

BonusObj aBonus = Bonus.newBonus(context, "MODSKILLPOINTS|NUMBER|INT");

if (aBonus != null)
{
intel.addToListFor(ListKey.BONUS, aBonus);
Expand Down Expand Up @@ -137,13 +138,13 @@ protected void setUp() throws Exception
balance.addToListFor(ListKey.TYPE, Type.getConstant("DEX"));
balance.put(ObjectKey.KEY_STAT, dexRef);
aBonus = Bonus.newBonus(context, "SKILL|Balance|2|PRESKILL:1,Tumble=5|TYPE=Synergy.STACK");

if (aBonus != null)
{
balance.addToListFor(ListKey.BONUS, aBonus);
}
Globals.getContext().getReferenceContext().importObject(balance);

weapon = new Equipment();
weapon.setName("TestWpn");
weapon.addToListFor(ListKey.TYPE, Type.WEAPON);
Expand All @@ -152,7 +153,7 @@ protected void setUp() throws Exception
gem.setName("TestGem");
gem.addToListFor(ListKey.TYPE, Type.getConstant("gem"));
gem.setQty(1);

armor = new Equipment();
armor.setName("TestArmorSuit");
TestHelper.addType(armor, "armor.suit");
Expand Down Expand Up @@ -262,39 +263,39 @@ public void testFor() throws IOException
Ability dummyFeat1 = new Ability();
dummyFeat1.setName("1");
dummyFeat1.setCDOMCategory(BuildUtilities.getFeatCat());

Ability dummyFeat2 = new Ability();
dummyFeat2.setName("2");
dummyFeat2.setCDOMCategory(BuildUtilities.getFeatCat());

Ability dummyFeat3 = new Ability();
dummyFeat3.setName("3");
dummyFeat3.setCDOMCategory(BuildUtilities.getFeatCat());

Ability dummyFeat4 = new Ability();
dummyFeat4.setName("4");
dummyFeat4.setCDOMCategory(BuildUtilities.getFeatCat());

Ability dummyFeat5 = new Ability();
dummyFeat5.setName("5");
dummyFeat5.setCDOMCategory(BuildUtilities.getFeatCat());

Ability dummyFeat6 = new Ability();
dummyFeat6.setName("6");
dummyFeat6.setCDOMCategory(BuildUtilities.getFeatCat());

Ability dummyFeat7 = new Ability();
dummyFeat7.setName("7");
dummyFeat7.setCDOMCategory(BuildUtilities.getFeatCat());
dummyFeat7.setCDOMCategory(BuildUtilities.getFeatCat());

addAbility(BuildUtilities.getFeatCat(), dummyFeat1);
addAbility(BuildUtilities.getFeatCat(), dummyFeat2);
addAbility(BuildUtilities.getFeatCat(), dummyFeat3);
addAbility(BuildUtilities.getFeatCat(), dummyFeat4);
addAbility(BuildUtilities.getFeatCat(), dummyFeat5);
addAbility(BuildUtilities.getFeatCat(), dummyFeat6);
addAbility(BuildUtilities.getFeatCat(), dummyFeat7);

assertEquals("----------------",
evaluateToken(
"FOR.1,((24-STRLEN[SKILL.0])),24,-,NONE,NONE,1", pc), "Test for evaluates correctly"
Expand All @@ -303,7 +304,7 @@ public void testFor() throws IOException
evaluateToken(
"FOR.1,((24-STRLEN[SKILL.0])),24, ,NONE,NONE,1", pc), "Test for evaluates correctly"
);

String tok = "DFOR."
+ "0"
+ ",${((count(\"ABILITIES\";\"CATEGORY=FEAT\")+1)/2)}"
Expand All @@ -314,20 +315,20 @@ public void testFor() throws IOException
+ ",["
+ ",]"
+ ",0";


//Logging.errorPrint( "DFOR Test: " + evaluateToken(tok, pc));
// Test DFOR with alternate syntax for jep passthrough. ie, anything


// Test DFOR with alternate syntax for jep passthrough. ie, anything
// surrounded by ${x} will tbe sent straight to be processed. We
// will assume that x is a well formed type of value. This was to get around
// will assume that x is a well formed type of value. This was to get around
// the problems with DFOR not taking ((count("ABILITIES";"CATEGORY=FEAT")+1)
// since it could not figure out how to parse it to send to the right place.
assertEquals("[ 1 5 ][ 2 6 ][ 3 7 ][ 4 ]",
evaluateToken(tok, pc), "Test for DFOR "
);

}

@Test
Expand All @@ -347,7 +348,7 @@ public void testForNoMoreItems() throws IOException
evaluateToken(
"FOR.0,100,1,\\ARMOR.SUIT.ALL.%.NAME\\,S,F,1", pc), "Test for evaluates correctly"
);

}

@Test
Expand All @@ -368,62 +369,62 @@ public void testExpressionOutput() throws IOException
dummyFeat2.setName("DummyFeat2");
dummyFeat2.setCDOMCategory(BuildUtilities.getFeatCat());
final BonusObj aBonus = Bonus.newBonus(context, "VAR|NegLevels|7");

if (aBonus != null)
{
dummyFeat2.addToListFor(ListKey.BONUS, aBonus);
}

AbilityCategory cat = context.getReferenceContext().constructCDOMObject(
AbilityCategory.class, "Maneuver");
AbilityCategory cat2 = context.getReferenceContext().constructCDOMObject(
AbilityCategory.class, "Maneuver(Special)");
Ability dummyFeat3 = new Ability();
dummyFeat3.setName("DummyFeat3");
dummyFeat3.setCDOMCategory(cat);

Ability dummyFeat4 = new Ability();
dummyFeat4.setName("DummyFeat4");
dummyFeat4.setCDOMCategory(cat2);

addAbility(BuildUtilities.getFeatCat(), dummyFeat);
addAbility(BuildUtilities.getFeatCat(), dummyFeat2);
addAbility(cat, dummyFeat3);
addAbility(cat2, dummyFeat4);

assertEquals("7", evaluateToken(
"VAR.NegLevels.INTVAL", pc), "Unsigned output");
assertEquals("+7", evaluateToken(
"VAR.NegLevels.INTVAL.SIGN", pc), "Signed output");

String tok;
tok = "count(\"ABILITIES\", \"CATEGORY=Maneuver\")";

tok = "count(\"ABILITIES\", \"CATEGORY=Maneuver\")";
// if this evaluates math wise, the values should be string "1.0"
assertNotEquals("1.0", evaluateToken(tok, pc), "Token: |" + tok + "| != 1.0: ");

tok = "VAR.count(\"ABILITIES\", \"CATEGORY=Maneuver\")";
assertEquals("1.0", evaluateToken(tok, pc), "Token: |" + tok + "| == 1.0: ");

tok = "COUNT[\"ABILITIES\", \"CATEGORY=Maneuver\"]";
assertNotEquals("1.0", evaluateToken(tok, pc), "Token: |" + tok + "| != 1.0: ");

tok = "count(\"ABILITIES\", \"CATEGORY=Maneuver(Special)\")";
assertNotEquals("1.0", evaluateToken(tok, pc), "Token: |" + tok + "| != 1.0 ");

tok = "${count(\"ABILITIES\", \"CATEGORY=Maneuver(Special)\")+5}";
assertNotEquals("5.0", evaluateToken(tok, pc), "Token: |" + tok + "| == 5.0 ");

tok = "${count(\"ABILITIES\", \"CATEGORY=Maneuver(Special)\")+5}";
assertEquals("6.0", evaluateToken(tok, pc), "Token: |" + tok + "| != 6.0 ");

tok = "${(count(\"ABILITIES\", \"CATEGORY=Maneuver(Special)\")+5)/3}";
assertNotEquals("3.0", evaluateToken(tok, pc), "Token: |" + tok + "| == 3.0 ");

tok = "${(count(\"ABILITIES\", \"CATEGORY=Maneuver(Special)\")+5)/3}";
assertEquals("2.0", evaluateToken(tok, pc), "Token: |" + tok + "| != 2.0 ");


}

@Test
Expand All @@ -446,20 +447,6 @@ public void testPartyFor() throws IOException
);
}

private static String evaluateToken(String token, PlayerCharacter pc)
throws IOException
{
StringWriter retWriter = new StringWriter();
BufferedWriter bufWriter = new BufferedWriter(retWriter);
ExportHandler export = ExportHandler.createExportHandler(new File(""));
export.replaceToken(token, bufWriter, pc);
retWriter.flush();

bufWriter.flush();

return retWriter.toString();
}

private static String evaluatePartyToken(String token, List<PlayerCharacter> pcs)
throws IOException
{
Expand Down Expand Up @@ -490,6 +477,6 @@ protected void defaultSetupEnd()
{
//We will handle this locally
}


}
43 changes: 33 additions & 10 deletions code/src/test/pcgen/util/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -64,6 +65,7 @@
import pcgen.core.bonus.Bonus;
import pcgen.core.bonus.BonusObj;
import pcgen.core.spell.Spell;
import pcgen.io.ExportHandler;
import pcgen.persistence.CampaignFileLoader;
import pcgen.persistence.GameModeFileLoader;
import pcgen.persistence.PersistenceLayerException;
Expand Down Expand Up @@ -123,7 +125,7 @@ public static void makeSizeAdjustments()
.silentlyGetConstructedCDOMObject(SizeAdjustment.class, "M").put(
ObjectKey.IS_DEFAULT_SIZE, true);
}

/**
* Make some equipment
* @param input Equipment source line to be parsed
Expand Down Expand Up @@ -316,8 +318,8 @@ public static boolean makeAbilityFromString(final String input)
return false;
}



/**
* Set the important info about a WeaponProf
* @param name The weaponprof name
Expand Down Expand Up @@ -399,7 +401,7 @@ public static Spell makeSpell(final String name)
}

/**
* Set the important info about a Kit. Note the key of the kit created will
* Set the important info about a Kit. Note the key of the kit created will
* be the provided name with KEY_ added at the front. e.g. KEY_name
* @param name The kit name
* @return The kit (which has also been added to global storage)
Expand Down Expand Up @@ -428,7 +430,7 @@ public static PCTemplate makeTemplate(final String name)
Globals.getContext().getReferenceContext().importObject(aTemplate);
return aTemplate;
}

/**
* Get the Ability Category of the Ability object passed in. If it does
* not exist in the game mode, a new object wil be created and added to
Expand All @@ -453,7 +455,7 @@ public static void addType(CDOMObject cdo, String string)

/**
* Checks to see if this PC has the weapon proficiency key aKey
*
*
* @param aKey
* @return boolean
*/
Expand All @@ -466,9 +468,9 @@ public static boolean hasWeaponProfKeyed(PlayerCharacter pc,
}

/**
* Locate the data folder which contains the primary set of LST data. This
* defaults to the data folder under the current directory, but can be
* customised in the config.ini folder.
* Locate the data folder which contains the primary set of LST data. This
* defaults to the data folder under the current directory, but can be
* customised in the config.ini folder.
* @return The path of the data folder.
*/
public static String findDataFolder()
Expand Down Expand Up @@ -522,7 +524,7 @@ public static void createDummySettingsFile(String configFileName,
}

}

public static void loadGameModes(String testConfigFile)
{
String configFolder = "testsuite";
Expand Down Expand Up @@ -585,4 +587,25 @@ public static PCClass parsePCClassText(String classPCCText,
}
return reconstClass;
}

/**
* Evaluate a token, used in several "export" tests. By default, the token encoding is ignored.
* If encoded value is required, use @see pcgen.io.FileAccess#setCurrentOutputFilter(java.lang.String) before
* calling this static method.
*
* @param token the token to evaluate (e.g., any token from @see plugin.exporttokens such as "PORTRAIT")
* @param pc the pc or a PlayerCharacter object
* @return the string containing the evaluated token
* @throws IOException Signals that an I/O exception has occurred.
*/
public static String evaluateToken(String token, PlayerCharacter pc)
throws IOException {
StringWriter retWriter = new StringWriter();
try (BufferedWriter bufWriter = new BufferedWriter(retWriter)) {
ExportHandler export = ExportHandler.createExportHandler(new File(""));
export.replaceTokenSkipMath(pc, token, bufWriter);
}

return retWriter.toString();
}
}
Loading

0 comments on commit 2b4d8e4

Please sign in to comment.