Add tests for WorkaroundSet. Related fixes/changes.

This commit is contained in:
asofold 2016-01-17 23:37:30 +01:00
parent aacb97b26b
commit 228ed2f74a
3 changed files with 206 additions and 25 deletions

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import fr.neatmonster.nocheatplus.utilities.ds.count.acceptdeny.IAcceptDenyCounter;
@ -88,7 +89,8 @@ public interface IWorkaroundRegistry {
this.stagedGroups.put(groupId, stagedGroup.toArray(new IStagedWorkaround[stagedGroup.size()]));
}
}
} else {
}
else {
this.groups = null;
this.stagedGroups = null;
}
@ -99,11 +101,19 @@ public interface IWorkaroundRegistry {
final IWorkaround present = getWorkaround(id);
if (!workaroundClass.isAssignableFrom(present.getClass())) {
throw new IllegalArgumentException("Wrong type of registered workaround requested: " + workaroundClass.getName() + " instead of " + present.getClass().getName());
} else {
}
else {
return (C) present;
}
}
/**
*
* @param id
* @return
* @throws IllegalArgumentException
* If no workaround is set for the id.
*/
public IWorkaround getWorkaround(final String id) {
final IWorkaround present = workaroundsById.get(id);
if (present == null) {
@ -144,13 +154,19 @@ public interface IWorkaroundRegistry {
*
* @param workaroundId
* @return The result of IWorkaround.use() for the registered instance.
* @throws NullPointerException
* if no workaround is registered for this id.
* @throws IllegalArgumentException
* If no workaround is registered for this id.
*
*/
public boolean use(String workaroundId) {
// TODO: For consistency might throw the same exception everywhere (IllegalArgument?).
return workaroundsById.get(workaroundId).use();
final IWorkaround workaround = workaroundsById.get(workaroundId);
if (workaround == null) {
throw new IllegalArgumentException("Workaround id not registered: " + workaroundId);
}
else {
return workaround.use();
}
}
/**
@ -182,26 +198,13 @@ public interface IWorkaroundRegistry {
/**
* Specify what workaround ids belong to a certain group. Workarounds can be
* in multiple groups.
* in multiple groups. The workaroundIds must exist.
*
* @param groupId
* @param workaroundIds
*/
public void setGroup(String groupId, Collection<String> workaroundIds);
/**
* Define which workarounds and which groups belong to the WorkaroundSet of
* the given workaroundSetId.
*
* @param workaroundSetId
* @param bluePrints
* Lazily registers, if no blueprint is present (calling setWorkaroundBluePrint, note parent counters). Already
* registered blueprints are kept.
* @param groupIds
* Must already be registered.
*/
public void setWorkaroundSet(String workaroundSetId, Collection<IWorkaround> bluePrints, String... groupIds);
/**
* Define which workarounds and which groups belong to the WorkaroundSet of
* the given workaroundSetId.
@ -265,11 +268,32 @@ public interface IWorkaroundRegistry {
public IWorkaround getWorkaround(String id);
/**
* Retrieve an unmodifiable map for all registered global counters. The counters
* are not copies, so they could be altered, discouraged though.
* Retrieve an unmodifiable map for all registered global counters. The
* counters are not copies, so they could be altered, discouraged though.
*
* @return
*/
public Map<String, IAcceptDenyCounter> getGlobalCounters();
/**
* Convenience to get the internally registered id.
*
* @param workaroundId
* @return
* @throws IllegalArgumentException
* If an id is not registered for a given workaround.
*/
public String getCheckedWorkaroundId(String workaroundId);
/**
* Convenience method to get a set of ids, testing if bluePrints exist.
*
* @param workarounds
* @return A set fit for iteration. Contained ids are taken from the
* internally registered instances.
* @throws IllegalArgumentException
* If an id is not registered for a given workaround.
*/
public Set<String> getCheckedIdSet(Collection<? extends IWorkaround> workarounds); // UH.
}

View File

@ -4,8 +4,10 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import fr.neatmonster.nocheatplus.utilities.ds.count.acceptdeny.AcceptDenyCounter;
import fr.neatmonster.nocheatplus.utilities.ds.count.acceptdeny.IAcceptDenyCounter;
@ -53,11 +55,15 @@ public class SimpleWorkaroundRegistry implements IWorkaroundRegistry {
@Override
public void setGroup(final String groupId, final Collection<String> workaroundIds) {
for (final String id : workaroundIds) {
if (!bluePrints.containsKey(id)) {
throw new IllegalArgumentException("No blueprint for id '" + id + "' in group '" + groupId + "'.");
}
}
groups.put(groupId, workaroundIds.toArray(new String[workaroundIds.size()]));
}
@Override
public void setWorkaroundSet(final String workaroundSetId, final Collection<IWorkaround> bluePrints, final String... groupIds) {
private void setWorkaroundSet(final String workaroundSetId, final Collection<IWorkaround> bluePrints, final String... groupIds) {
final String[] ids = new String[bluePrints.size()];
int i = 0;
for (final IWorkaround bluePrint : bluePrints) {
@ -159,4 +165,29 @@ public class SimpleWorkaroundRegistry implements IWorkaroundRegistry {
return Collections.unmodifiableMap(counters);
}
@Override
public String getCheckedWorkaroundId(String workaroundId) {
final IWorkaround bluePrint = this.bluePrints.get(workaroundId);
if (bluePrint == null) {
throw new IllegalArgumentException("No blueprint registered for: " + workaroundId);
} else {
return bluePrint.getId();
}
}
@Override
public Set<String> getCheckedIdSet(Collection<? extends IWorkaround> workarounds) {
final Set<String> ids = new LinkedHashSet<String>();
for (final IWorkaround workaround : workarounds) {
final IWorkaround bluePrint = this.bluePrints.get(workaround.getId());
if (bluePrint == null) {
throw new IllegalArgumentException("No blueprint registered for: " + workaround.getId());
}
else {
ids.add(bluePrint.getId());
}
}
return ids;
}
}

View File

@ -2,6 +2,11 @@ package fr.neatmonster.nocheatplus.test;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import fr.neatmonster.nocheatplus.utilities.ds.count.acceptdeny.AcceptDenyCounter;
@ -10,6 +15,7 @@ import fr.neatmonster.nocheatplus.utilities.ds.count.acceptdeny.ICounterWithPare
import fr.neatmonster.nocheatplus.workaround.IStagedWorkaround;
import fr.neatmonster.nocheatplus.workaround.IWorkaround;
import fr.neatmonster.nocheatplus.workaround.IWorkaroundRegistry;
import fr.neatmonster.nocheatplus.workaround.IWorkaroundRegistry.WorkaroundSet;
import fr.neatmonster.nocheatplus.workaround.SimpleWorkaroundRegistry;
import fr.neatmonster.nocheatplus.workaround.WorkaroundCountDown;
import fr.neatmonster.nocheatplus.workaround.WorkaroundCounter;
@ -125,7 +131,7 @@ public class TestWorkarounds {
// Register a single workaround (no parent counter).
checkSetWorkaroundBluePrint(new WorkaroundCounter("wc.man"), reg);
// Register a single workaround with a parent counter set (createed from registry).
// Register a single workaround with a parent counter set (created from registry).
IWorkaround wrp = new WorkaroundCounter("wc.man.rp"); // With parent counter from registry.
((ICounterWithParent) wrp.getAllTimeCounter()).setParentCounter(c_man);
checkSetWorkaroundBluePrint(wrp, reg);
@ -135,7 +141,127 @@ public class TestWorkarounds {
((ICounterWithParent) wep.getAllTimeCounter()).setParentCounter(new AcceptDenyCounter());
checkSetWorkaroundBluePrint(wep, reg);
// TODO: Test a WorkaroundSet with all types of workarounds. Groups, WorkaroundSet methods.
// WorkaroundSet
// Register workarounds.
List<WorkaroundCounter> wg1 = getWorkaroundCounters("w.man", 15);
List<WorkaroundCountDown> wg2 = getWorkaroundcountDowns("w.man", 15);
List<IWorkaround> wgAll = new ArrayList<IWorkaround>(30);
wgAll.addAll(wg1);
wgAll.addAll(wg2);
try {
reg.getCheckedIdSet(wg1);
fail("Expect IllegalArgumentException for not registered workarounds.");
}
catch (IllegalArgumentException ex) {
// Success.
}
reg.setWorkaroundBluePrint(wgAll.toArray(new IWorkaround[2 * 15]));
List<String> ids1 = new ArrayList<String>(reg.getCheckedIdSet(wg1));
List<String> ids2 = new ArrayList<String>(reg.getCheckedIdSet(wg2));
List<String> idsAll = new ArrayList<String>(reg.getCheckedIdSet(wgAll));
// Register groups.
reg.setGroup("group.mix", Arrays.asList(ids1.get(0), ids2.get(0)));
reg.setGroup("group.wc", ids1);
reg.setGroup("group.wcd", ids2);
// reg.setWorkaroundSet with string ids.
reg.setWorkaroundSetByIds("ws.all", idsAll, "group.mix", "group.wc", "group.wcd");
// reg.getWorkaroundSet.
WorkaroundSet ws = reg.getWorkaroundSet("ws.all");
// Test the WorkaroundSet
for (String id : idsAll) {
ws.getWorkaround(id);
}
// Test reset all.
useAll(idsAll, ws);
int accept = 1;
int deny = 0;
checkAllTimeCount(idsAll, ws, accept, deny);
checkStageCount(ids2, ws, 1, 0);
ws.resetConditions();
checkAllTimeCount(idsAll, ws, accept, deny);
checkStageCount(ids2, ws, 0, 0);
// Reset group.wc.
useAll(idsAll, ws);
accept += 1;
ws.resetConditions("group.wc");
checkAllTimeCount(idsAll, ws, accept, deny);
checkStageCount(ids2, ws, 1, 0);
ws.resetConditions();
// group.wcd
useAll(idsAll, ws);
accept += 1;
ws.resetConditions("group.wcd");
checkAllTimeCount(idsAll, ws, accept, deny);
checkStageCount(ids2, ws, 0, 0);
ws.resetConditions();
// group.mix
useAll(idsAll, ws);
accept += 1;
ws.resetConditions("group.mix");
checkAllTimeCount(idsAll, ws, accept, deny);
TestAcceptDenyCounters.checkCounts(((IStagedWorkaround) (ws.getWorkaround(ids2.get(0)))).getStageCounter(), 0, 0, "stageCounter/" + ids2.get(0));
for (int i = 1; i < ids2.size(); i++) {
TestAcceptDenyCounters.checkCounts(((IStagedWorkaround) (ws.getWorkaround(ids2.get(i)))).getStageCounter(), 1, 0, "stageCounter/" + ids2.get(i));
}
ws.resetConditions();
// TODO: Individual group reset (needs half of group.wcd).
// TODO: More details/cases (also failure cases, exceptions, etc).
}
/**
* Get a collection of new WorkaroundCounter instances.
*
* @param name
* Prefix of naming name.class.count
* @param repeatCount
* @return
*/
public static List<WorkaroundCounter> getWorkaroundCounters(String name, int repeatCount) {
final List<WorkaroundCounter> workarounds = new ArrayList<WorkaroundCounter>();
for (int i = 0; i < repeatCount; i++) {
workarounds.add(new WorkaroundCounter(name + ".WorkaroundCounter." + i));
}
return workarounds;
}
/**
* Get a collection of new WorkaroundCountDown instances, initialized with
* counting up from 1.
*
* @param name
* Prefix of naming name.class.count
* @param repeatCount
* @return
*/
public static List<WorkaroundCountDown> getWorkaroundcountDowns(String name, int repeatCount) {
final List<WorkaroundCountDown> workarounds = new ArrayList<WorkaroundCountDown>();
for (int i = 0; i < repeatCount; i++) {
workarounds.add(new WorkaroundCountDown(name + ".WorkaroundCountDown." + i, i + 1));
}
return workarounds;
}
public static void useAll(Collection<String> ids, WorkaroundSet ws) {
for (String id : ids) {
ws.use(id);
}
}
public static void checkStageCount(Collection<String> ids, WorkaroundSet ws, int acceptCount, int denyCount) {
for (String id : ids) {
IAcceptDenyCounter counter = ((IStagedWorkaround) ws.getWorkaround(id)).getStageCounter();
TestAcceptDenyCounters.checkCounts(counter, acceptCount, denyCount, "stageCounter/" + id);
}
}
public static void checkAllTimeCount(Collection<String> ids, WorkaroundSet ws, int acceptCount, int denyCount) {
for (String id : ids) {
IAcceptDenyCounter counter = ws.getWorkaround(id).getAllTimeCounter();
TestAcceptDenyCounters.checkCounts(counter, acceptCount, denyCount, "allTimeCounter/" + id);
}
}
/**