2
Hello.,
I’m reading Caelum’s booklet about java tests jsf web services design Patterns.
The problem is that I can’t read the date (I replace the Calendar
for LocalDate
in class Negociacao
) for having these tags
(byte
and int
) that appear when I use xml.
And makes that mistake:
com.thoughtworks.xstream.converters.ConversionException: Cannot convert type java.time.Ser to type java.time.LocalDate
I tried to read how this shows website, but makes that mistake:
com.thoughtworks.xstream.converters.ConversionException: Text '' could not be parsed at index 0 : Text '' could not be parsed at index 0
This is the xml:
<negociacao>
<preco>42.3</preco>
<quantidade>100</quantidade>
<data resolves-to=\"java.time.Ser\">
<byte>3</byte>
<int>2015</int>
<byte>11</byte>
<byte>8</byte>
</data>
</negociacao>
This is the class you read to read the xml:
import java.io.InputStream;
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import br.com.caelum.argentum.modelo.Negociacao;
import br.com.caelum.argentum.util.LocalDateConverter;
public class LeitorXML {
public List<Negociacao> carrega(InputStream inputStream){
if(inputStream == null){
throw new NullPointerException("a lista esta vazia");
}
LocalDateConverter conv = new LocalDateConverter();
XStream stream = new XStream(new DomDriver());
stream.registerConverter(conv);
stream.alias("negociacao", Negociacao.class);
return (List<Negociacao>) stream.fromXML(inputStream);
}
}
This is the class method LeitorXMLTest
:
@Test
public void carregaXmlComUmaNegociacaoEmListaUnitaria() {
String xmlDeTeste = "<list>" +
"<negociacao>" +
"<preco>43.5</preco>" +
"<quantidade>1000</quantidade>" +
"<data resolves-to=\"java.time.Ser\">" +
"<byte>3</byte>" +
"<int>2015</int>" +
"<byte>11</byte>" +
"<byte>8</byte>" +
"</data>" +
"</negociacao>" +
"</list>";
LeitorXML leitor = new LeitorXML();
InputStream xml = new ByteArrayInputStream(xmlDeTeste.getBytes());
List<Negociacao> negociacoes = leitor.carrega(xml);
for (Negociacao negociacao : negociacoes) {
System.out.println(negociacao.getData());
}
assertEquals(1, negociacoes.size());
assertEquals(43.5, negociacoes.get(0).getPreco(), 0.00001);
assertEquals(1000, negociacoes.get(0).getQuantidade(), 0.00001);
}
It seems the code of convert that you picked up on that forum just won’t work because it tries to parse directly from a
String
. You would need a convertmore complex to deal with this case, but unfortunately I don’t have time to get into it. However, an alternative would simply represent the date using a textual format, such as2015-12-30
and create a convert to always work in this format.`– utluiz