podam icon indicating copy to clipboard operation
podam copied to clipboard

PodamFactoryImpl: Support for XML Datatype instantiation.

Open patrodyne opened this issue 11 years ago • 9 comments

Thank you for Podam. I use it in production to generate samples requests for web service APIs. These POJOs often contain XmlGregorianCalendar properties. Here is a patch for PodamFactoryImpl to return values when instantiation requires the use of the XML Datatype factory.

  1. Here are the imports ...
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
  1. A helper method to get a static instance of the XML DatatypeFactory...
// A factory to create objects that map XML to/from Java.
private static DatatypeFactory xmlDatatypeFactory;
public static DatatypeFactory getXmlDatatypeFactory()
{
    if ( xmlDatatypeFactory == null )
    {
        try
        {
            xmlDatatypeFactory = DatatypeFactory.newInstance();
        }
        catch (DatatypeConfigurationException e)
        {
            throw new RuntimeException("Podam factory cannot create XML factory.", e);
        }
    }
    return xmlDatatypeFactory;
}
  1. In the createNewInstanceForClassWithoutConstructors(...) method, within the test for 'no constructors' and after the loop to find a 'getInstance()' method, add this code...
// Did we find a getInstance() method to create the return value? 
if ( retValue == null )
{
    // No return value, so far.
    // Can we use an XML DatatypeFactory to create an instance? 
    if ( XMLGregorianCalendar.class.isAssignableFrom(clazz) )
    {
        retValue = getXmlDatatypeFactory().newXMLGregorianCalendar(new GregorianCalendar());
    }
    else if ( Duration.class.isAssignableFrom(clazz) )
    {
        List<Annotation> annotations = new ArrayList<Annotation>();
        getLongValueWithinRange(annotations, null);
        retValue = getXmlDatatypeFactory().newDuration(0L);
    }
}

And a suggestion: Change the log.info(...) in PodamFactoryImpl to log.debug(...). This would allow my SysAdmin to set the log level to INFO instead of WARN without a flood of details from this class, IMHO.

patrodyne avatar Dec 27 '14 02:12 patrodyne

Most of logging framework allow setting logging level per package. For example, Logback: http://stackoverflow.com/questions/5924089/how-to-configure-logback-to-skip-logging-messages-from-org-package-with-all-le And Log4J: http://stackoverflow.com/questions/5297686/log4j-package-specific-logging Which logging framework do you use?

daivanov avatar Dec 28 '14 08:12 daivanov

Thanks Daniil, that is good advice! I code to slf4j and deploy to containers that are configured for either logback or log4j. Consistent with your advice, I have configured log4j.logger.uk.co.jemos.podam=WARN. That works but an improvement might be to limit INFO messages to life cycle events (start/stop and configuration settings) and push the more verbose messages down to DEBUG or TRACE. By the time a framework hits prod, the cool dev-centric stuff becomes less important while admin stuff like 'did the framework start and how is it configured' becomes the focus. Using INFO is a way to log the life-cycle events without the verbosity of 'per instance' logic flow. In any case, this is not an issue, only a suggestion.

patrodyne avatar Dec 28 '14 15:12 patrodyne

Could please you report logging as a separate new issue?

daivanov avatar Dec 29 '14 10:12 daivanov

I just noticed that pull request https://github.com/mtedone/podam/pull/39 addresses excessive logging and use of slf4j to abstract the logging framework.

patrodyne avatar Dec 30 '14 01:12 patrodyne

Fixed in PODAM 4.5.0.

daivanov avatar Jan 03 '15 17:01 daivanov

Released to Maven central

On 3 Jan 2015, at 17:53, Daniil Ivanov [email protected] wrote:

Fixed in PODAM 4.5.0.

— Reply to this email directly or view it on GitHub https://github.com/mtedone/podam/issues/69#issuecomment-68603207.

mtedone avatar Jan 03 '15 18:01 mtedone

Closing.

daivanov avatar Jan 17 '15 07:01 daivanov

Apparently XMLGregorianCalendar manufacturing test doesn't work properly.

daivanov avatar Mar 27 '15 20:03 daivanov

Fixed in PODAM 5.2.0.

daivanov avatar Mar 27 '15 20:03 daivanov