private Object roundTripFromFlatToOrigCmd( Object flatCmd, Class flatCmdClass, Class origCmdClass ) throws Exception { String xmlStr = serialize(flatCmd, flatCmdClass); logger.debug("{}:\n{}---", flatCmdClass.getSimpleName(), xmlStr); return deserialize(xmlStr, origCmdClass); }
private Object fillCommand( Class cmdClass ) throws Exception { Object cmdObj = cmdClass.getConstructor().newInstance(); Field[] fields = cmdClass.getDeclaredFields(); for( Field field : fields ) { if( Modifier.isStatic(field.getModifiers()) ) { continue; } fillField(field, cmdObj); } if( cmdClass.getSuperclass() != null ) { for( Field taskField : cmdClass.getSuperclass().getDeclaredFields() ) { if( Modifier.isStatic(taskField.getModifiers()) ) { continue; } fillField(taskField, cmdObj); } } return cmdObj; }
private <T> T deserialize( String xmlStr, Class<T> cmdClass ) throws Exception { Unmarshaller unmarshaller = null; unmarshaller = getJaxbContext(cmdClass).createUnmarshaller(); ByteArrayInputStream xmlStrInputStream = new ByteArrayInputStream(xmlStr.getBytes(Charset.forName("UTF-8"))); T jaxbObj = (T) unmarshaller.unmarshal(xmlStrInputStream); return jaxbObj; }
private void compareKieClassInstanceToGenClassInstance( Class kieCmdClass, Class genCmdClass ) throws Exception { Object genCmd = fillCommand(genCmdClass); if( DeleteCommand.class.equals(genCmdClass) ) { ((DeleteCommand) genCmd).setFactHandle( ":1:2:3:4:5:TRAIT" ); } Object copyKieCmd = roundTripFromFlatToOrigCmd(genCmd, genCmdClass, kieCmdClass); for( Field field : kieCmdClass.getDeclaredFields() ) { if( field.getAnnotation(XmlTransient.class) != null) { continue; } field.setAccessible(true); Object kieCmdFieldVal = field.get(copyKieCmd); assertNotNull( kieCmdClass.getSimpleName() + "." + field.getName(), kieCmdFieldVal ); } }
@Test public void reflectionMappingTest() throws Exception { Set<Class<?>> classes = reflections.getTypesAnnotatedWith(XmlAccessorType.class); assertFalse(classes.isEmpty()); List<Class<?>> classList = new LinkedList<Class<?>>(classes); Collections.sort(classList, new Comparator<Class<?>>() { @Override public int compare( Class<?> o1, Class<?> o2 ) { return o1.getSimpleName().compareTo(o2.getSimpleName()); } }); classes = new LinkedHashSet<Class<?>>(classList); Map<Class, Class> kieCmdGenCmdClassMap = new LinkedHashMap<Class, Class>(); for( Class<?> cmdClass : classes ) { if( ! cmdClass.getSimpleName().endsWith("Command") ) { continue; } if( ! AcceptedClientCommands.isAcceptedCommandClass(cmdClass) ) { continue; } Class genCmdClass = Class.forName("org.kie.remote.jaxb.gen." + cmdClass.getSimpleName()); assertTrue( genCmdClass.getSimpleName() + " is not an accepted command?", AcceptedClientCommands.isAcceptedCommandClass(genCmdClass) ); kieCmdGenCmdClassMap.put(cmdClass, genCmdClass); } assertFalse("No generated command classes could be found to test.", kieCmdGenCmdClassMap.isEmpty()); for( Entry<Class, Class> classEntry : kieCmdGenCmdClassMap.entrySet() ) { compareKieClassInstanceToGenClassInstance(classEntry.getKey(), classEntry.getValue()); } }
private String serialize( Object object, Class cmdClass ) throws Exception { Marshaller marshaller = null; marshaller = getJaxbContext(cmdClass).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() { @Override public void escape( char[] ac, int i, int j, boolean flag, Writer writer ) throws IOException { writer.write(ac, i, j); } }); StringWriter stringWriter = new StringWriter(); try { marshaller.marshal(object, stringWriter); } catch( Exception e ) { e.printStackTrace(); fail( "Could not marshal " + cmdClass.getSimpleName() ); } String output = stringWriter.toString(); return output; }