Skip to content

Commit

Permalink
BZ-1071144 - BPMS cluster with deployment unit using PER_PROCESS_INST…
Browse files Browse the repository at this point in the history
…ANCE session strategy throws java.lang.IllegalStateException - support for both XmlAttribute and XmlElement

(cherry picked from commit 3d2c255792ad4aeb7d5a006274dd8aecd8e77c71)
  • Loading branch information
mswiderski committed Mar 11, 2014
1 parent 6e65a3d commit 155a126
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 14 deletions.
Expand Up @@ -25,6 +25,7 @@
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import org.drools.core.command.runtime.SetGlobalCommand;
import org.drools.core.command.runtime.process.CompleteWorkItemCommand;
Expand Down Expand Up @@ -382,6 +383,12 @@ private Long findProcessInstanceId(Command<?> command) {
if ("process-instance-id".equalsIgnoreCase(attributeName)) {
return (Long) field.get(command);
}
} else if (field.isAnnotationPresent(XmlElement.class)) {
String elementName = field.getAnnotation(XmlElement.class).name();

if ("process-instance-id".equalsIgnoreCase(elementName)) {
return (Long) field.get(command);
}
}
}
} catch (Exception e) {
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.util.HashSet;
import java.util.Set;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
Expand All @@ -31,6 +32,8 @@

public class JaxbServicesSerializationTest extends AbstractServicesSerializationTest {

private static final String PROCESS_INSTANCE_ID_NAME = "process-instance-id";

private static Reflections reflections = new Reflections(ClasspathHelper.forPackage("org.kie.services.client"),
new TypeAnnotationsScanner(), new FieldAnnotationsScanner(), new MethodAnnotationsScanner(), new SubTypesScanner());

Expand Down Expand Up @@ -66,18 +69,18 @@ public void acceptedCommandsTest() throws Exception {
}

@Test
public void jaxbClassesAreKnownToJaxbSerializationProvider() throws Exception {
public void jaxbClassesAreKnownToJaxbSerializationProvider() throws Exception {
for (Class<?> jaxbClass : reflections.getTypesAnnotatedWith(XmlRootElement.class)) {
Constructor<?> construct = jaxbClass.getConstructor(new Class [] {});
Object jaxbInst = construct.newInstance(new Object [] {});
testRoundTrip(jaxbInst);
}
}
}

/**
* If you think this test is a mistake: beware, this test is smarter than you. Seriously.
* If you think this test is a mistake: beware, this test is smarter than you. Seriously.
* Heck, this test is smarter than *me*, and I wrote it!
*
*
* @throws Exception
*/
@Test
Expand Down Expand Up @@ -108,8 +111,8 @@ public void acceptedCommandsCanBeSerializedTest() throws Exception {
}

/**
* This test is the above one's little brother, and he takes after him. Damn.. these are some smart tests, yo!
*
* This test is the above one's little brother, and he takes after him. Damn.. these are some smart tests, yo!
*
* (HA!)
*/
@Test
Expand Down Expand Up @@ -216,14 +219,14 @@ public void classListPropertyTest() throws Exception {
assertEquals(out.length, 0);
again = join(out);
assertEquals("", again);

Set<Class<?>> classList = new HashSet<Class<?>>();
classList.add(String.class);
classList.add(Integer.class);
classList.add(Byte.class);
classList.add(Short.class);
classList.add(Long.class);

String commaString = JaxbSerializationProvider.classSetToCommaSeperatedString(classList);
Set<Class<?>> copyClasses = JaxbSerializationProvider.commaSeperatedStringToClassSet(commaString);
assertEquals( classList, copyClasses );
Expand All @@ -244,21 +247,88 @@ private String join(String[] inArr) {
}
return out.toString();
}

@Test
public void jmsSerializationPropertyTest() {
public void jmsSerializationPropertyTest() {
// 0
Set<Class<?>> extraJaxbClasses = new HashSet<Class<?>>();
testRoundTripClassesSet(extraJaxbClasses);

// 1
// 1
extraJaxbClasses.add(JaxbServicesSerializationTest.class);
testRoundTripClassesSet(extraJaxbClasses);
// 2

// 2
extraJaxbClasses.add(JsonServicesSerializationTest.class);
testRoundTripClassesSet(extraJaxbClasses);
}



@Test
public void processInstanceIdFieldInCommands() throws Exception {
Reflections cmdReflections = new Reflections(
ClasspathHelper.forPackage("org.drools.command.*"),
new TypeAnnotationsScanner(), new FieldAnnotationsScanner(), new MethodAnnotationsScanner(), new SubTypesScanner());

Set<Class<?>> classes = cmdReflections.getTypesAnnotatedWith(XmlRootElement.class);
Set<Class> cmdClasses = new HashSet<Class>();
for (Class<?> jaxbClass : classes ) {
if( jaxbClass.getSimpleName().endsWith("Command") ) {
cmdClasses.add(jaxbClass);
}
}
for( Class<?> jaxbCmdClass : cmdClasses ) {
Field procInstIdField = null;
for( Field field : jaxbCmdClass.getDeclaredFields() ) {
String fullFieldName = jaxbCmdClass.getSimpleName() + "." + field.getName();
field.setAccessible(true);
// check that type matches
XmlElement xmlElem = ((XmlElement) field.getAnnotation(XmlElement.class));
XmlAttribute xmlAttribute = ((XmlAttribute) field.getAnnotation(XmlAttribute.class));
if( xmlElem != null ) {
String xmlElemName = xmlElem.name();
if( xmlElemName != null && xmlElemName.equals(PROCESS_INSTANCE_ID_NAME) ) {
assertTrue( fullFieldName + " is an incorrect type! (" + field.getType() + ")",
field.getType().equals(Long.class) || field.getType().equals(long.class) );
}
} else if( xmlAttribute != null ) {
String xmlAttributeName = xmlAttribute.name();
if( xmlAttributeName != null && xmlAttributeName.equals(PROCESS_INSTANCE_ID_NAME) ) {
assertTrue( fullFieldName + " is an incorrect type! (" + field.getType() + ")",
field.getType().equals(Long.class) || field.getType().equals(long.class) );
}
}
// check that field has correct XmlElement name
String name = field.getName().toLowerCase();
if( name.startsWith("proc") && name.contains("inst")
&& ! name.endsWith("s") && ! name.endsWith("list")) {
xmlElem = ((XmlElement) field.getAnnotation(XmlElement.class));
xmlAttribute = ((XmlAttribute) field.getAnnotation(XmlAttribute.class));
String xmlElemName = null;
String xmlAttrName = null;

if( xmlElem != null ) {
xmlElemName = xmlElem.name();
}
if( xmlAttribute != null ) {
xmlAttrName = xmlAttribute.name();
}
if( xmlElemName != null ) {
assertEquals( fullFieldName + " is incorrectly annotated with name '" + xmlElemName + "'",
PROCESS_INSTANCE_ID_NAME, xmlElemName );
} else if( xmlAttrName != null ) {
assertEquals( fullFieldName + " is incorrectly annotated with name '" + xmlAttrName + "'",
PROCESS_INSTANCE_ID_NAME, xmlAttrName );
} else {
logger.error( "Should " + fullFieldName + " have an @XmlElement or @XmlAttribute annotation?");
}

}
}

}
}

private void testRoundTripClassesSet(Set<Class<?>> extraJaxbClasses ) {
boolean emptySet = extraJaxbClasses.isEmpty();
Expand Down

0 comments on commit 155a126

Please sign in to comment.