Hello,
I am reading Caelum's handbook on java testing jsf web services design patterns .
The problem is that I can not read the date (I replaceCalendar
with LocalDate
in class Negociacao
) by having those tags
( byte
and int
) that appear when I generate xml .
And give this error:
com.thoughtworks.xstream.converters.ConversionException: Cannot convert type java.time.Ser to type java.time.LocalDate
I tried to read this site , but it gives this error:
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 that reads to read 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 method of class 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);
}