Sunday, April 13, 2008

Myammee Love Money Nackt

XML Serialization - save java.util.Date and enumeration values \u200b\u200b

This article is a continuation thread from the article TestNG - test data from an XML file to use standard (J2SE), XML serialization of objects schemy for the preparation of test data. This article will treat a data entry type java.util.Date and enumerated type - enumeration.
As a reminder in this If it's not about serialization of objects from java, but a manual preparation of XML files containing zserializowane objects that are zdeserializowane using a standard mechanism - java.beans.XMLDecoder and will then be used as input for the unit tests.

However, even when serializing an object from the levels of Java, which has a field of type java.util.Date and the second about the type of enumeration, we obtain the effect of far been disappointing, which certainly do not want to emulate.
field enumeration of the type in general will not be saved. The box with the dates will be saved, but as the number of milliseconds since January 1, 1970. Surely this is not a convenient form for humans, Ana read, let alone to write. In any event, java.beans.XMLEncoder zserializuje java.util.Date as follows:
 \u0026lt;void property="birthDate"> \u0026lt;object class="java.util.Date"> \u0026lt; ; long> 439254000000 \u0026lt;/ long> \u0026lt;/ object> \u0026lt;/ void> 
Not so ... that's pretty logical, because it is the only one not "condemned" the way to set the date which provides an interface java.util.Date.
Normally (in Java), if I had to get the date from a string, I would have benefited from java.text.SimpleDateFormat. But how to use it in that this XML ... now I've included:
 \u0026lt;void id="sdf" class="java.text.SimpleDateFormat"> \u0026lt;string> yyyy-MM-dd \u0026lt;/ string> \u0026lt;void id="date0" method="parse"> ; \u0026lt;string> 1983-12-03 \u0026lt;/ string> \u0026lt;/ void> \u0026lt;/ void> ... \u0026lt;object class="pl.dwalczak.Osoba"> ... \u0026lt;void property="dataUrodzin"> \u0026lt;object idref="date0"/> \u0026lt;/ void> ... \u0026lt;/ Object> 
first part of the structure (a marker of void id = "sdf" ), a declaration, which corresponds in Java:
 java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ("yyyy-MM -dd "); java.util.Date sdf.parse date0 = (" 1983-12-03 "); 
She does not define any zserializowanego object, and only variables that can be used when defining zserializowanych obietków. Can be placed in the main element, or directly in zserializowanym object.
The second part of the structure (the marker object ) defines an object of type pl.dwalczak.Osoba , which sets the field dataUrodzin predefined variable date0 .

Returning to the enumerated type.
Suppose that the class has pl.dwalczak.Osoba typOsoby field of type pl.dwalczak.TypOsoby which is enumeracjom:
 package pl.dwalczak; {public enum TypOsoby OsobaFizyczna, OsobaPrawna;} 
then set the field typOsoby for pl.dwalczak.Osoba object might look like this:
 \u0026lt;void property="type"> \u0026lt;object class="com.dwalczak.TypOsoby" field="OsobaFizyczna"/> \u0026lt;/ void> 
Or this:
 \u0026lt;void property="type"> ; \u0026lt;object class="com.dwalczak.TypOsoby" method="valueOf"> \u0026lt;string> OsobaPrawna \u0026lt;/ string> \u0026lt;/ object> \u0026lt;/ void> 

Summary

While the problem of serializing a value of type The enumeration does not specifically reduce the attractiveness of the standard format of XML serialization of objects in J2SE, as a general format for recording test data manually because it would be in pretty decent shape. This is a problem with the date actually reduces the attractiveness, because the formula in the record of its "human" character is a little too long and complicated. Still, I think that the use of this format may be less time consuming than designing and writing your own code, it deserializującego objects.

Resources Long-Term Persistence of JavaBeans Components: XML Schema
Malenkova Sergey's Blog - How to encode Type-Safe enums?

0 comments:

Post a Comment