[Minor] [New] Implemented and fixed remaining policy tests

This commit is contained in:
Robert von Burg 2015-08-22 11:56:43 +02:00
parent ac1885883a
commit 0e3b3b2a1e
11 changed files with 110 additions and 12 deletions

View File

@ -38,7 +38,7 @@
</TimedState> </TimedState>
<Policies> <Policies>
<Policy Type="PlanningPolicy" Value="key:SimplePlanningPolicy" /> <Policy Type="PlanningPolicy" Value="key:SimplePlanningPolicy" />
<Policy Type="ExecutionPolicy" Value="java:li.strolch.policy.execution.TestSimulatedExecutionPolicy" /> <Policy Type="ExecutionPolicy" Value="java:li.strolch.policytest.TestSimulatedExecutionPolicy" />
<Policy Type="ConfirmationPolicy" Value="key:NoConfirmation" /> <Policy Type="ConfirmationPolicy" Value="key:NoConfirmation" />
</Policies> </Policies>
</Resource> </Resource>

View File

@ -118,6 +118,9 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle
clone.setDate(this.date); clone.setDate(this.date);
clone.setState(this.state); clone.setState(this.state);
if (this.policyDefs != null)
clone.setPolicyDefs(this.policyDefs.getClone());
return clone; return clone;
} }

View File

@ -132,6 +132,9 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot
} }
} }
if (this.policyDefs != null)
clone.setPolicyDefs(this.policyDefs.getClone());
return clone; return clone;
} }

View File

@ -242,6 +242,10 @@ public class Activity extends GroupedParameterizedElement
for (IActivityElement element : this.elements.values()) { for (IActivityElement element : this.elements.values()) {
clone.addElement(element.getClone()); clone.addElement(element.getClone());
} }
if (this.policyDefs != null)
clone.setPolicyDefs(this.policyDefs.getClone());
return clone; return clone;
} }

View File

@ -109,6 +109,9 @@ public class StrolchElementToDomVisitor {
Element element = document.createElement(Tags.ACTIVITY); Element element = document.createElement(Tags.ACTIVITY);
fillElement(element, activity); fillElement(element, activity);
if (activity.hasPolicyDefs())
fillElement(element, activity.getPolicyDefs());
if (activity.hasElements()) { if (activity.hasElements()) {
Iterator<Entry<String, IActivityElement>> iter = activity.elementIterator(); Iterator<Entry<String, IActivityElement>> iter = activity.elementIterator();
while (iter.hasNext()) { while (iter.hasNext()) {
@ -123,9 +126,6 @@ public class StrolchElementToDomVisitor {
} }
} }
if (activity.hasPolicyDefs())
fillElement(element, activity.getPolicyDefs());
return element; return element;
} }

View File

@ -23,6 +23,12 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
import li.strolch.model.GroupedParameterizedElement; import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.Order; import li.strolch.model.Order;
import li.strolch.model.ParameterBag; import li.strolch.model.ParameterBag;
@ -37,13 +43,8 @@ import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange; import li.strolch.model.timevalue.IValueChange;
import li.strolch.policy.PolicyDef;
import org.xml.sax.Attributes; import li.strolch.policy.PolicyDefs;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/** /**
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
@ -146,6 +147,9 @@ public abstract class StrolchElementToSaxVisitor {
this.contentHandler.endElement(null, null, Tags.TIMED_STATE); this.contentHandler.endElement(null, null, Tags.TIMED_STATE);
} }
if (resource.hasPolicyDefs())
toSax(resource.getPolicyDefs());
this.contentHandler.endElement(null, null, Tags.RESOURCE); this.contentHandler.endElement(null, null, Tags.RESOURCE);
} }
@ -162,6 +166,9 @@ public abstract class StrolchElementToSaxVisitor {
toSax((GroupedParameterizedElement) order); toSax((GroupedParameterizedElement) order);
if (order.hasPolicyDefs())
toSax(order.getPolicyDefs());
this.contentHandler.endElement(null, null, Tags.ORDER); this.contentHandler.endElement(null, null, Tags.ORDER);
} }
@ -169,6 +176,9 @@ public abstract class StrolchElementToSaxVisitor {
this.contentHandler.startElement(null, null, Tags.ACTIVITY, attributesFor(activity)); this.contentHandler.startElement(null, null, Tags.ACTIVITY, attributesFor(activity));
toSax((GroupedParameterizedElement) activity); toSax((GroupedParameterizedElement) activity);
if (activity.hasPolicyDefs())
toSax(activity.getPolicyDefs());
Iterator<Entry<String, IActivityElement>> iter = activity.elementIterator(); Iterator<Entry<String, IActivityElement>> iter = activity.elementIterator();
while (iter.hasNext()) { while (iter.hasNext()) {
IActivityElement activityElement = iter.next().getValue(); IActivityElement activityElement = iter.next().getValue();
@ -185,6 +195,26 @@ public abstract class StrolchElementToSaxVisitor {
this.contentHandler.endElement(null, null, Tags.ACTIVITY); this.contentHandler.endElement(null, null, Tags.ACTIVITY);
} }
private void toSax(PolicyDefs policyDefs) throws SAXException {
if (!policyDefs.hasPolicyDefs())
return;
this.contentHandler.startElement(null, null, Tags.POLICIES, null);
for (String type : policyDefs.getPolicyTypes()) {
PolicyDef policyDef = policyDefs.getPolicyDef(type);
this.contentHandler.startElement(null, null, Tags.POLICY, attributesFor(policyDef));
this.contentHandler.endElement(null, null, Tags.POLICY);
}
this.contentHandler.endElement(null, null, Tags.POLICIES);
}
protected AttributesImpl attributesFor(PolicyDef policyDef) {
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(null, null, Tags.TYPE, Tags.CDATA, policyDef.getType());
attributes.addAttribute(null, null, Tags.VALUE, Tags.CDATA, policyDef.getValueForXml());
return attributes;
}
protected AttributesImpl attributesFor(Action action) { protected AttributesImpl attributesFor(Action action) {
AttributesImpl attributes = attributesFor((StrolchElement) action); AttributesImpl attributes = attributesFor((StrolchElement) action);
attributes.addAttribute(null, null, Tags.RESOURCE_ID, Tags.CDATA, action.getResourceId()); attributes.addAttribute(null, null, Tags.RESOURCE_ID, Tags.CDATA, action.getResourceId());

View File

@ -37,4 +37,9 @@ public class JavaPolicyDef extends PolicyDef {
public String getValueForXml() { public String getValueForXml() {
return XML_PREFIX + this.value; return XML_PREFIX + this.value;
} }
@Override
public JavaPolicyDef getClone() {
return new JavaPolicyDef(this.type, this.value);
}
} }

View File

@ -39,4 +39,9 @@ public class KeyPolicyDef extends PolicyDef {
public String getValueForXml() { public String getValueForXml() {
return XML_PREFIX + this.value; return XML_PREFIX + this.value;
} }
@Override
public KeyPolicyDef getClone() {
return new KeyPolicyDef(this.type, this.value);
}
} }

View File

@ -75,8 +75,21 @@ public abstract class PolicyDef {
*/ */
public abstract <T> T accept(PolicyDefVisitor visitor); public abstract <T> T accept(PolicyDefVisitor visitor);
/**
* Returns the value formatted for XML marshalling, so that the {@link #valueOf(String, String)} can then again
* parse the value and instantiate a concrete {@link PolicyDef} instance
*
* @return the value formatted for XML marshalling
*/
public abstract String getValueForXml(); public abstract String getValueForXml();
/**
* Return a clone of this {@link PolicyDef} instance
*
* @return a clone of this {@link PolicyDef} instance
*/
public abstract PolicyDef getClone();
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -88,13 +101,35 @@ public abstract class PolicyDef {
return sb.toString(); return sb.toString();
} }
/**
* Returns a {@link PolicyDef} instance which handles the given type of XML Value
*
* @param type
* the type
* @param xmlValue
* the XML formatted value with the prefix denoting the {@link PolicyDef} type
*
* @return a {@link PolicyDef} instance which handles the given type of XML Value
*/
public static PolicyDef valueOf(String type, String xmlValue) { public static PolicyDef valueOf(String type, String xmlValue) {
if (xmlValue.startsWith(JavaPolicyDef.XML_PREFIX)) { if (xmlValue.startsWith(JavaPolicyDef.XML_PREFIX)) {
String value = xmlValue.substring(JavaPolicyDef.XML_PREFIX.length()); String value = xmlValue.substring(JavaPolicyDef.XML_PREFIX.length());
try {
Class.forName(value);
} catch (ClassNotFoundException e) {
throw new StrolchPolicyException("Invalid policy configuration. Policy does not exist: " + value);
}
return new JavaPolicyDef(type, value); return new JavaPolicyDef(type, value);
} else if (xmlValue.startsWith(KeyPolicyDef.XML_PREFIX)) { } else if (xmlValue.startsWith(KeyPolicyDef.XML_PREFIX)) {
String value = xmlValue.substring(KeyPolicyDef.XML_PREFIX.length()); String value = xmlValue.substring(KeyPolicyDef.XML_PREFIX.length());
return new KeyPolicyDef(type, value); return new KeyPolicyDef(type, value);
} else { } else {
throw new StrolchPolicyException("Unhandled PolicyDef from xml value " + xmlValue); throw new StrolchPolicyException("Unhandled PolicyDef from xml value " + xmlValue);
} }

View File

@ -85,4 +85,17 @@ public class PolicyDefs {
sb.append("]"); sb.append("]");
return sb.toString(); return sb.toString();
} }
/**
* Returns a clone of this {@link PolicyDefs}
*
* @return a clone of this {@link PolicyDefs}
*/
public PolicyDefs getClone() {
PolicyDefs clone = new PolicyDefs();
for (PolicyDef policyDef : this.policyDefMap.values()) {
clone.addOrUpdate(policyDef.getClone());
}
return clone;
}
} }

View File

@ -12,7 +12,7 @@
</ParameterBag> </ParameterBag>
<Policies> <Policies>
<Policy Type="PlanningPolicy" Value="key:SimplePlanningPolicy" /> <Policy Type="PlanningPolicy" Value="key:SimplePlanningPolicy" />
<Policy Type="ExecutionPolicy" Value="java:li.strolch.policy.execution.TestSimulatedExecutionPolicy" /> <Policy Type="ExecutionPolicy" Value="java:li.strolch.policytest.TestSimulatedExecutionPolicy" />
<Policy Type="ConfirmationPolicy" Value="key:NoConfirmation" /> <Policy Type="ConfirmationPolicy" Value="key:NoConfirmation" />
</Policies> </Policies>
</Resource> </Resource>