JUnit test of the same object - java

I want to make a unit test suite of the same object with same variable but different values. However if the object get the same name (created by this.setName("testlaunch"); (we must have the name of a method tested by JUnit), it runs only one test.
If i don't write this.setName("testlaunch"); it complains saying junit.framework.AssertionFailedError: TestCase.fName cannot be null.
I don't know what to do...
public class LanceurRegleGestion extends TestSuite
{
public static Test suite()
{
Class maClasse = null;
TestSuite suite = new TestSuite();
String filtre = ".*.xml";
// on compile le pattern pour l'expression réguliere
Pattern p = Pattern.compile(filtre);
String path = "D:/Documents/workspace/Solipsisme/src/ReglesGestion/XML/";
// on liste les fichiers du repertoire
String [] u = new File(path).list();
// on parcours la liste de fichier
System.out.println("Initialisation");
for (int i=0; i
et le code de l'objet serialisé
public class Application extends TestCase {
private String nomappli;
private String id2_1;
private String id3_1;
private String id4_1;
private String id2_2;
private String id3_2;
private String id4_2;
private String id5_2;
private String id6_2;
private String id7_2;
private String id8_2;
private String id9_2;
private String id2_3;
private String id3_3;
private String id4_3;
private String id2_4;
private String id3_4;
private String id4_4;
private String id2_5;
private String id3_5;
private String id4_5;
private String id5_5;
private String id6_5;
private String id7_5;
private static Selenium selenium;
public Application(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2,String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4,String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5)
{
this.setName("testlaunch");
this.nomappli = nomappli;
this.id2_1 = id2_1;
this.id3_1 = id3_1;
this.id4_1 = id4_1;
this.id2_2 = id2_2;
this.id3_2 = id3_2;
this.id4_2 = id4_2;
this.id5_2 = id5_2;
this.id6_2 = id6_2;
this.id7_2 = id7_2;
this.id8_2 = id8_2;
this.id9_2 = id9_2;
this.id2_3 = id2_3;
this.id3_3 = id3_3;
this.id4_3 = id4_3;
this.id2_4 = id2_4;
this.id3_4 = id3_4;
this.id4_4 = id4_4;
this.id2_5 = id2_5;
this.id3_5 = id3_5;
this.id4_5 = id4_5;
this.id5_5 = id5_5;
this.id6_5 = id6_5;
this.id7_5 = id7_5;
}
public Application(){
}
public String toString()
{
return getNomappli();
}
public void setNomappli(String nomappli)
{
this.nomappli = nomappli;
}
public String getNomappli()
{
return this.nomappli;
}
public void setId2_1(String id2_1)
{
this.id2_1 = id2_1;
}
public String getId2_1()
{
return this.id2_1;
}
public void setId3_1(String id3_1)
{
this.id3_1 = id3_1;
}
public String getId3_1()
{
return this.id3_1;
}
public void setId4_1(String id4_1)
{
this.id4_1 = id4_1;
}
public String getId4_1()
{
return this.id4_1;
}
public void setId2_2(String id2_2)
{
this.id2_2 = id2_2;
}
public String getId2_2()
{
return this.id2_2;
}
public void setId3_2(String id3_2)
{
this.id3_2 = id3_2;
}
public String getId3_2()
{
return this.id3_2;
}
public void setId4_2(String id4_2)
{
this.id4_2 = id4_2;
}
public String getId4_2()
{
return this.id4_2;
}
public void setId5_2(String id5_2)
{
this.id5_2 = id5_2;
}
public String getId5_2()
{
return this.id5_2;
}
public void setId6_2(String id6_2)
{
this.id6_2 = id6_2;
}
public String getId6_2()
{
return this.id6_2;
}
public void setId7_2(String id7_2)
{
this.id7_2 = id7_2;
}
public String getId7_2()
{
return this.id7_2;
}
public void setId8_2(String id8_2)
{
this.id8_2 = id8_2;
}
public String getId8_2()
{
return this.id8_2;
}
public void setId9_2(String id9_2)
{
this.id9_2 = id9_2;
}
public String getId9_2()
{
return this.id9_2;
}
public void setId2_3(String id2_3)
{
this.id2_3 = id2_3;
}
public String getId2_3()
{
return this.id2_3;
}
public void setId3_3(String id3_3)
{
this.id3_3 = id3_3;
}
public String getId3_3()
{
return this.id3_3;
}
public void setId4_3(String id4_3)
{
this.id4_3 = id4_3;
}
public String getId4_3()
{
return this.id4_3;
}
public void setId2_4(String id2_4)
{
this.id2_4 = id2_4;
}
public String getId2_4()
{
return this.id2_4;
}
public void setId3_4(String id3_4)
{
this.id3_4 = id3_4;
}
public String getId3_4()
{
return this.id3_4;
}
public void setId4_4(String id4_4)
{
this.id4_4 = id4_4;
}
public String getId4_4()
{
return this.id4_4;
}
public void setId2_5(String id2_5)
{
this.id2_5 = id2_5;
}
public String getId2_5()
{
return this.id2_5;
}
public void setId3_5( String id3_5)
{
this.id3_5 = id3_5;
}
public String getId3_5()
{
return this.id3_5;
}
public void setId4_5(String id4_5)
{
this.id4_5 = id4_5;
}
public String getId4_5()
{
return this.id4_5;
}
public void setId5_5(String id5_5)
{
this.id5_5 = id5_5;
}
public String getId5_5()
{
return this.id5_5;
}
public void setId6_5(String id6_5)
{
this.id6_5 = id6_5;
}
public String getId6_5()
{
return this.id6_5;
}
public void setId7_5(String id7_5)
{
this.id7_5 = id7_5;
}
public String getId7_5()
{
return this.id7_5;
}
public void setSelenium(Selenium selenium)
{
this.selenium = selenium;
}
public Selenium getSelenium()
{
return this.selenium;
}
public final static void login()
{
selenium.open("apj/ident");
selenium.type("username", "hsuzumiya-cp");
selenium.type("password", "1");
selenium.click("enterButton");
selenium.waitForPageToLoad("9999999");
}
public void testlaunch()
{
generique(this.nomappli,this.id2_1,this.id3_1,this.id4_1,this.id2_2,this.id3_2,this.id4_2,this.id5_2,this.id6_2,this.id7_2,this.id8_2,this.id9_2,this.id2_3,this.id3_3,this.id4_3,this.id2_4,this.id3_4,this.id4_4,this.id2_5,this.id3_5,this.id4_5,this.id5_5,this.id6_5,this.id7_5);
}
public void setUp() throws Exception
{
System.out.println("Initialisation");
selenium = new DefaultSelenium("127.0.0.1",4444,"*iexplore", "http://hsuzumiya/");
selenium.start();
selenium.setTimeout("90000");
selenium.setSpeed("500");
login();
}
public void generique(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2,
String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4,
String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5
)
{
System.out.println(nomappli);
selenium.click("valider");
selenium.waitForPageToLoad("30000");
selenium.click("validertout");
}
public final void tearDown() throws Exception
{
System.out.println("Killing session");
selenium.stop();
}
}

Being new to junit, I stumbled upon this question hoping to solve a problem I had getting the same message. Through further research, I found that it is necessary to pass the name of the test function you want to invoke through addTest to the constructor of the test case class. A simple (and useless, other than illustration) example follows:
JunitTestCases.java
import junit.framework.TestCase;
public class JunitTestCases extends TestCase {
public JunitTestCases(String fnName) {
super(fnName);
}
public void testA() {
assertTrue("assertTrue failed", true);
}
}
JunitTestSuite.java:
import junit.framework.*;
public class JunitTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new JunitTestCases("testA"));
return suite;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
When I compiled with:
javac -cp .:path/to/junit-X.X.X.jar JunitTestSuite.java
and ran with
java -cp .:path/to/junit-X.X.X.jar JunitTestSuite
this worked with no errors, with junit giving me an OK message.

Related

On reload of page, mvp4g history mechanism fails

I have implemented a history mechanism for my mvp4g project. When I traverse through the pages, I can see the url also getting changed. But on reload of any page other than home page, always home page gets displayed instead of the desired page?
This is my implementation:
#History(type = HistoryConverterType.SIMPLE)
public class CustomHistoryConverter implements HistoryConverter<AppEventBus> {
private CustomEventBus eventBus;
#Override
public void convertFromToken(String historyName, String param, CustomEventBus eventBus) {
this.eventBus = eventBus;
eventBus.dispatch(historyName, param);
}
public String convertToToken(String eventName, String name) {
return name;
}
public String convertToToken(String eventName) {
return eventName;
}
public String convertToToken(String eventName, String name, String type) {
return name;
}
public boolean isCrawlable() {
return false;
}
}
and event bus related code :
#Events(startPresenter=PageOnePresenter.class,historyOnStart=true)
public interface CustomEventBus extends EventBusWithLookup {
#Start
#Event(handlers = PageOnePresenter.class)
void start();
#InitHistory
#Event(handlers = PageOnePresenter.class)
void init();
#Event(handlers = PageTwoPresenter.class, name = "page2", historyConverter = CustomHistoryConverter.class)
void getPageTwo();
#Event(handlers = PageThreePresenter.class, name = "page3", historyConverter=CustomHistoryConverter.class)
void getPageThree();
#Event(handlers=PageOnePresenter.class, name = "page1", historyConverter=CustomHistoryConverter.class)
void getPageOne();
#Event(handlers=PageOnePresenter.class)
void setPageTwo(HistoryPageTwoView view);
#Event(handlers=PageOnePresenter.class)
void setPageThree(HistoryPageThreeView view);
}
The HistoryConverter needs to be improved.
In fact, that the event has no parameter, you should return an empty string. Update the HistoryConverter that it looks like that:
#History(type = HistoryConverterType.SIMPLE)
public class CustomHistoryConverter implements HistoryConverter<AppEventBus> {
private CustomEventBus eventBus;
#Override
public void convertFromToken(String historyName, String param, CustomEventBus eventBus) {
this.eventBus = eventBus;
// TODO handle the param in cases where you have more than one parameter
eventBus.dispatch(historyName, param);
}
public String convertToToken(String eventName, String name) {
return name;
}
public String convertToToken(String eventName) {
return "";
}
public String convertToToken(String eventName, String name, String type) {
return name - "-!-" type;
}
public boolean isCrawlable() {
return false;
}
}
Hope that helps.

Finding ConnectivityNumber algorithm

I want find Connectivity number in Java. when i try to execute code,any message is not shown(empty)in console. no error message in window o.s. program execute well but when i try again in Linux, error message is shown that no suitabe method 'main' in class.
ConnectivityNumber.java
public class ConnectivityNumber
{
final private int graph[][]={
{0,1,0,1,0,0,0,0,0,0},
{1,0,1,0,0,0,0,0,0,0},
{0,1,0,1,0,0,0,0,0,0},
{1,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,0,0},
{0,0,0,0,0,1,0,1,1,0},
{0,0,0,0,0,0,1,0,1,0},
{0,0,0,0,0,0,1,1,0,0},
{0,0,0,0,0,0,0,0,0,0}
};
private int vertex[]={1,0,0,0,0,0,0,0,0,0};
private int connectivityNum=0;
private int startVertex=0;
public boolean isFinishCount()
{
for(int i=0;i<vertex.length;i++)
{
if(vertex[i]==0)
return false;
}
return true;
}
public void randomChoose()
{
for(int i=0;i<vertex.length;i++)
{
if(vertex[i]==0)
startVertex=i;
}
}
public void chooseVertex(int index)
{
startVertex=index;
if(vertex[index]==1)
{
connectivityNum++;
randomChoose();
}
else
{
vertex[index]=1;
}
}
public void rowSearch(int row)
{
for(int i=0;i<vertex.length;i++)
{
if(graph[row][i]==1&&vertex[i]==0)
{
chooseVertex(i);
break;
}
}
}
public void findConnectivityNum()
{
while(!isFinishCount())
{
rowSearch(startVertex);
}
}
public void printConnectivityNum()
{
System.out.println(connectivityNum);
}
}
ConnectivityNumberTest.java
public class ConnectivityNumberTest
{
public static void main(String[] args)
{
ConnectivityNumber connect=new ConnectivityNumber();
connect.findConnectivityNum();
connect.printConnectivityNum();
}
}

JAXB unmarshelling returns null

I am trying to unmarshel a XML file that I marshelled, but it returns null when I print it.
The XML file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<presentieLijst>
<les>
<begintijd>15:30</begintijd>
<docent>John Smeets</docent>
<eindtijd>18:00</eindtijd>
<lesDatum>2016-02-02</lesDatum>
<lesNaam>TCIF-V1AUI-15</lesNaam>
<lokaal>D02.08</lokaal>
</les>
</presentieLijst>
The XML wrapper:
#XmlRootElement(name = "presentieLijst")
public class PresentieLijstWrapper {
private Les les;
#XmlElement(name = "les")
public Les getLes() {
return les;
}
public void setles(Les lessen) {
this.les = lessen;
}
}
The "Les" class that gets inserted into PresentieLijstWrapper.set as ObservableList:
package presentie.model;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Les {
private final StringProperty lesNaam,
lokaal,
docent,
eindtijd,
begintijd,
lesDatum;
//private Klas klas;
public Les(){this(null,null,null,null,null,null);}
public Les(String nm, String lok, String doc, String dt, String et, String bt) {
this.lesNaam = new SimpleStringProperty(nm);
this.lokaal = new SimpleStringProperty(lok);
this.docent = new SimpleStringProperty(doc);
this.begintijd = new SimpleStringProperty(et);
this.eindtijd = new SimpleStringProperty(bt);
this.lesDatum = new SimpleStringProperty(dt);
//this.klas = kl;
}
public StringProperty lesNaamProperty() {return lesNaam;}
public StringProperty lokaalProperty() {return lokaal;}
public StringProperty docentProperty() {return docent;}
public StringProperty begintijdProperty(){return begintijd;}
public StringProperty eindtijdProperty() {return eindtijd;}
public StringProperty datumProperty() {return lesDatum;}
public void setLesNaam(String nm) {this.lesNaam.set(nm);}
public void setLokaal(String lk) {this.lokaal.set(lk);}
public void setDocent(String dc) {this.docent.set(dc);}
public void setBegintijd(String bt){this.begintijd.set(bt);}
public void setEindtijd(String et) {this.eindtijd.set(et);}
public void setLesDatum(String dt) {this.lesDatum.set(dt);}
//public void setKlas(Klas kl) {klas = kl;}
public String getLesNaam() {return lesNaam.get();}
public String getLokaal() {return lokaal.get();}
public String getDocent() {return docent.get();}
public String getBegintijd() {return begintijd.get();}
public String getEindtijd() {return eindtijd.get();}
public String getLesDatum() {return lesDatum.get();}
//public Klas getKlas() {return klas;}
}
The unmarshel function:
public void updateAbsentie(ObservableList<Student> st){
List<File>filesInFolder = null;
try {
filesInFolder = Files.walk(Paths.get(path+"/data/saves/"))
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
if(filesInFolder.size() != 0){
for(File file : filesInFolder) {
if(file.getName().contains(".xml")){
try {
JAXBContext context = JAXBContext.newInstance(PresentieLijstWrapper.class);
Unmarshaller um = context.createUnmarshaller();
PresentieLijstWrapper wrapper = (PresentieLijstWrapper) um.unmarshal(file);
System.out.println(wrapper.getLes());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
}
I have looked at many other stackover questions and a couple tutorials. this is the one I am mainly following: http://code.makery.ch/library/javafx-8-tutorial/part5/
This is where it is:
public void setles(Les lessen) { ... }
Adhering to Java Beans conventions is essential. If a property's field is called les, the setter must be called setLes and the getter getLes or isLes for a boolean. Therefore:
public void setLes(Les lessen) { ... }

GWT - How to change NumberConstants in NumberFormat

I need to change NaN value and Minus sign in NumberFormat's NumberConstants.
It's impossible to change NumberConstants "on the fly", so I decided to subclass NumberFormat.
NumberFormat's constructor annotation says:
#param numberConstants the locale-specific number constants to use for
this format -- NOTE subclasses passing their own instance here
should pay attention to {#link #forcedLatinDigits()} and remap
localized symbols using {#link #createLatinNumberConstants(NumberConstants)}
It seemes what I should call setForcedLatinDigits(true) in order method createLatinNumberConstants(NumberConstants) to be executed. Then localized constants will be updated by that (createLatinNumberConstants) method, and I will get NumberConstants which I need.
Here is my code:
public class MyNumberFormat extends NumberFormat {
protected MyNumberFormat(String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
this(defaul, pattern, cdata, userSuppliedPattern);
}
protected MyNumberFormat(NumberConstants numberConstants, String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
super(numberConstants, pattern, cdata, userSuppliedPattern);
}
public static MyNumberFormat getFormat(String pattern) {
return new MyNumberFormat(pattern, CurrencyList.get().getDefault(), true);
}
protected static NumberConstants createLatinNumberConstants(
final NumberConstants orig) {
final String groupingSeparator = remapSeparator(
orig.groupingSeparator());
final String decimalSeparator = remapSeparator(
orig.decimalSeparator());
final String monetaryGroupingSeparator = remapSeparator(
orig.monetaryGroupingSeparator());
final String monetarySeparator = remapSeparator(
orig.monetarySeparator());
return new NumberConstants() {
#Override
public String currencyPattern() {
return orig.currencyPattern();
}
#Override
public String decimalPattern() {
return orig.decimalPattern();
}
#Override
public String decimalSeparator() {
return decimalSeparator;
}
#Override
public String defCurrencyCode() {
return orig.defCurrencyCode();
}
#Override
public String exponentialSymbol() {
return orig.exponentialSymbol();
}
#Override
public String globalCurrencyPattern() {
return orig.globalCurrencyPattern();
}
#Override
public String groupingSeparator() {
return groupingSeparator;
}
#Override
public String infinity() {
return orig.infinity();
}
#Override
public String minusSign() {
return UnicodeSymbols.MINUS;
}
#Override
public String monetaryGroupingSeparator() {
return monetaryGroupingSeparator;
}
#Override
public String monetarySeparator() {
return monetarySeparator;
}
#Override
public String notANumber() {
return "?";
}
#Override
public String percent() {
return orig.percent();
}
#Override
public String percentPattern() {
return orig.percentPattern();
}
#Override
public String perMill() {
return orig.perMill();
}
#Override
public String plusSign() {
return orig.plusSign();
}
#Override
public String scientificPattern() {
return orig.scientificPattern();
}
#Override
public String simpleCurrencyPattern() {
return orig.simpleCurrencyPattern();
}
#Override
public String zeroDigit() {
return "0";
}
};
}
}
So in order to format number i will execute this:
String fmt(final Double x){
MyNumberFormat.setForcedLatinDigits(true);
MyNumberFormat format = MyNumberFormat.getFormat("0.0000");
return format.format(x)
}
But in fact createLatinNumberConstants is protected static method and it can't be overriden or substituted.
So MyNumberFormat.createLatinNumberConstants() is never executed.
What am I doing wrong?
I think you do not need to overwrite createLatinNumberConstants - instead you pass your number constants to the constructor of the super class.
as a base you can use the default number constants of NumberFormat.
Something like that should work:
public class MyNumberFormat extends NumberFormat {
protected MyNumberFormat(String pattern, CurrencyData cdata, boolean userSuppliedPattern) {
super(createMyNumberConstants(NumberFormat.defaultNumberConstants), pattern, cdata, userSuppliedPattern);
}
public static MyNumberFormat getFormat(String pattern) {
return new MyNumberFormat(pattern, CurrencyList.get().getDefault(), true);
}
protected static final NumberConstants createMyNumberConstants(
final NumberConstants orig) {
return new NumberConstants() {
#Override
public String currencyPattern() {
return orig.currencyPattern();
}
#Override
public String decimalPattern() {
return orig.decimalPattern();
}
#Override
public String decimalSeparator() {
return decimalSeparator();
}
#Override
public String defCurrencyCode() {
return orig.defCurrencyCode();
}
#Override
public String exponentialSymbol() {
return orig.exponentialSymbol();
}
#Override
public String globalCurrencyPattern() {
return orig.globalCurrencyPattern();
}
#Override
public String groupingSeparator() {
return orig.groupingSeparator();
}
#Override
public String infinity() {
return orig.infinity();
}
#Override
public String minusSign() {
return orig.minusSign();
}
#Override
public String monetaryGroupingSeparator() {
return orig.monetaryGroupingSeparator();
}
#Override
public String monetarySeparator() {
return orig.monetarySeparator();
}
#Override
public String notANumber() {
return "?";
}
#Override
public String percent() {
return orig.percent();
}
#Override
public String percentPattern() {
return orig.percentPattern();
}
#Override
public String perMill() {
return orig.perMill();
}
#Override
public String plusSign() {
return orig.plusSign();
}
#Override
public String scientificPattern() {
return orig.scientificPattern();
}
#Override
public String simpleCurrencyPattern() {
return orig.simpleCurrencyPattern();
}
#Override
public String zeroDigit() {
return "0";
}
};
}

java- error when i am trying to convert xml response into pojo using xstream

I am getting an error for the following code in a java web app--
XStream xstream = new XStream();
apiresponse myClassObject;
myClassObject= xstream.fromXML(resp);
The error is shown for the line of code just above this line--
error="Type mismatch- cannot convert from Object to apiresponse"
Given below is the XML that I have to parse---
<apiresponse version="1" xmlns="http://ahrefs.com/schemas/api/links/1">
<resultset_links count="2">
<result>
<source_url>http://ahrefs.com/robot/</source_url>
<destination_url>http://blog.ahrefs.com/</destination_url>
<source_ip>50.22.24.236</source_ip>
<source_title>Ahrefs – backlinks research tool</source_title>
<visited>2011-08-31T07:56:53Z</visited>
<anchor>Blog</anchor>
<rating>257.674000</rating>
<link_type>text</link_type>
<is_nofollow>false</is_nofollow>
</result>
<result>
<source_url>http://apps.vc/</source_url>
<destination_url>http://ahrefs.com/robot/</destination_url>
<source_ip>64.20.55.86</source_ip>
<source_title>Device info</source_title>
<visited>2011-08-27T18:59:31Z</visited>
<anchor>http://ahrefs.com/robot/</anchor>
<rating>209.787100</rating>
<link_type>text</link_type>
<is_nofollow>false</is_nofollow>
</result>
</resultset_links>
</apiresponse>
I have created the following java classes to obtain data from above xml---
package com.arvindikchari.linkdatasmith.domain;
final public class apiresponse {
protected resultset_links rlinks;
public apiresponse() {
}
public resultset_links getRlinks()
{
return rlinks;
}
public setRlinks(resultset_links rlinks)
{
this.rlinks=rlinks;
}
}
final public class resultset_links {
protected List<result> indiv_result = new ArrayList<result>();
public resultset_links() {
}
public List<result> getIndiv_result()
{
return List;
}
public void setIndiv_result(List<result> indiv_result)
{
this.indiv_result=indiv_result;
}
}
final public class result {
protected String source_url;
protected String destination_url;
protected String source_ip;
protected String source_title;
protected String visited;
protected String anchor;
protected String rating;
protected String link_type;
public result() {
}
public String getSource_url()
{
return source_url;
}
public void setSource_url(String source_url)
{
this.source_url=source_url;
}
public String getDestination_url()
{
return destination_url;
}
public void setDestination_url(String destination_url)
{
this.destination_url=destination_url;
}
public String getSource_ip()
{
return source_ip;
}
public void setSource_ip(String source_ip)
{
this.source_ip=source_ip;
}
public String getSource_title()
{
return source_title;
}
public void setSource_title(String source_title)
{
this.source_title=source_title;
}
public String getVisited()
{
return visited;
}
public void setVisited(String visited)
{
this.visited=visited;
}
public String getAnchor()
{
return anchor;
}
public void setAnchor(String anchor)
{
this.anchor=anchor;
}
public String getRating()
{
return rating;
}
public void setRating(String rating)
{
this.rating=rating;
}
public String getLink_type()
{
return link_type;
}
public void setLink_type(String link_type)
{
this.link_type=link_type;
}
}
What am I doing wrong here?
You have many errors, but the one corresponding to your message is you have to cast the result of xstream.fromXML to an apiresponse' object :
apiresponse result = (apiresponse)xstream.fromXML(resp);
Moreover, the code you provided (the Java classes) do not compile, there are many errors.
Here are some improvements :
Result.java :
#XStreamAlias("result")
public class Result {
protected String source_url;
protected String destination_url;
protected String source_ip;
protected String source_title;
protected String visited;
protected String anchor;
protected String rating;
protected String link_type;
protected Boolean is_nofollow;
public Result() {
}
public String getSource_url()
{
return source_url;
}
public void setSource_url(String source_url)
{
this.source_url=source_url;
}
public String getDestination_url()
{
return destination_url;
}
public void setDestination_url(String destination_url)
{
this.destination_url=destination_url;
}
public String getSource_ip()
{
return source_ip;
}
public void setSource_ip(String source_ip)
{
this.source_ip=source_ip;
}
public String getSource_title()
{
return source_title;
}
public void setSource_title(String source_title)
{
this.source_title=source_title;
}
public String getVisited()
{
return visited;
}
public void setVisited(String visited)
{
this.visited=visited;
}
public String getAnchor()
{
return anchor;
}
public void setAnchor(String anchor)
{
this.anchor=anchor;
}
public String getRating()
{
return rating;
}
public void setRating(String rating)
{
this.rating=rating;
}
public String getLink_type()
{
return link_type;
}
public void setLink_type(String link_type)
{
this.link_type=link_type;
}
public Boolean getIs_nofollow() {
return is_nofollow;
}
public void setIs_nofollow(Boolean is_nofollow) {
this.is_nofollow = is_nofollow;
}
ResultsetLinks.java :
#XStreamAlias("resultset_links")
public class ResultsetLinks {
#XStreamImplicit(itemFieldName="result")
protected List<Result> indivResult = new ArrayList<Result>();
public ResultsetLinks() {
}
public List<Result> getResult()
{
return indivResult;
}
public void setResult(List<Result> indiv_result)
{
this.indivResult =indiv_result;
}
}
ApiResponse.java :
#XStreamAlias("apiresponse")
public class ApiResponse {
#XStreamAlias("resultset_links")
protected ResultsetLinks rlinks;
public ApiResponse() {
}
public ResultsetLinks getRlinks()
{
return rlinks;
}
public void setRlinks(ResultsetLinks rlinks)
{
this.rlinks=rlinks;
}
}
And finally your code to unmarshall the XML :
XStream xstream = new XStream();
xstream.processAnnotations(ApiResponse.class);
xstream.processAnnotations(ResultsetLinks.class);
xstream.processAnnotations(Result.class);
ApiResponse result = (ApiResponse)xstream.fromXML(resp);
All this code is working fine with Xstream 1.4.2
Try to follow Sun's coding convention for your classes name, attributes names, etc...
Use XstreamAliases to adapt the Java class name to the XML name.

Categories

Resources