Created
October 7, 2012 15:39
-
-
Save djangofan/3848703 to your computer and use it in GitHub Desktop.
A XStream handler for loading TestNG tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tr.test; | |
import com.thoughtworks.xstream.XStream; | |
import com.thoughtworks.xstream.io.xml.DomDriver; | |
import org.testng.annotations.DataProvider; | |
import org.testng.annotations.Test; | |
import java.io.BufferedWriter; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.List; | |
public class ConfigHandler { | |
private static String configFile = "testconfig.xml"; | |
@DataProvider(name = "testdata") | |
public Object[][] createData() { | |
List<TestCase> tests = getTestCasesList(); | |
return getObjectArray( tests ); | |
} | |
public static <T> Object[][] getObjectArray(List<TestCase> elements) { | |
Object[][] values = new Object[elements.size()][3]; | |
int counter = 0; | |
for (TestCase t : elements) { | |
values[counter][0] = t.getTestNumber(); | |
values[counter][1] = t.getFirstName(); | |
values[counter][2] = t.getLastName(); | |
counter++; | |
} | |
return values; | |
} | |
@Test(testName="configwriter") | |
public void testWriteToFile() { | |
// setup XStream | |
XStream xstream = new XStream(); | |
xstream.processAnnotations(TestConfig.class); | |
xstream.processAnnotations(TestSubject.class); | |
xstream.addImplicitCollection(TestCases.class, "tests","testcase", TestCase.class); | |
xstream.processAnnotations(TestCases.class); | |
xstream.useAttributeFor("lastRun", TestCases.class); | |
xstream.processAnnotations(TestCase.class); | |
// add data | |
TestConfig td = new TestConfig("go@gmail.com", "password", "smtp.mailserver.com", "565" ); | |
TestSubject ts = new TestSubject("https", "domain.com", "3343", "/app/login.jsp", "sa", "password" ); | |
td.setTestSubject( ts ); | |
TestCase one = new TestCase("1", "Jon", "Doe"); | |
TestCase two = new TestCase("2", "Janet", "Smith"); | |
TestCase three = new TestCase("3", "Jack", "Duff"); | |
TestCases tcs = new TestCases("2012-03-01"); | |
tcs.pushTestCase( one ); | |
tcs.pushTestCase( two ); | |
tcs.pushTestCase( three ); | |
td.setTestCases( tcs ); | |
// write file | |
String oXml = xstream.toXML( td ); | |
System.out.println( oXml ); | |
try { | |
FileWriter fw = new FileWriter( configFile ); | |
BufferedWriter bw = new BufferedWriter( fw ); | |
bw.write( oXml ); | |
bw.close(); | |
fw.close(); | |
} catch ( IOException e2 ) { | |
e2.printStackTrace(); | |
} | |
// verify file or assert failure | |
} | |
@Test(testName="configreader", dependsOnMethods="writeToFile", dataProvider = "testdata") | |
public void testReadFromFile( String num, String last, String first ) { | |
System.out.println("Loaded Test: " + num + ", " + first + ", " + last ); | |
XStream xstream = new XStream( new DomDriver() ); | |
xstream.processAnnotations(TestConfig.class); | |
xstream.processAnnotations(TestSubject.class); | |
xstream.addImplicitCollection(TestCases.class, "tests","testcase", TestCase.class); | |
xstream.processAnnotations(TestCases.class); | |
xstream.useAttributeFor("lastRun", TestCases.class); | |
xstream.processAnnotations(TestCase.class); | |
TestConfig td = null; | |
try { | |
FileReader fr = new FileReader( configFile ); | |
td = (TestConfig)xstream.fromXML( fr ); | |
fr.close(); | |
} catch ( FileNotFoundException e1 ) { | |
e1.printStackTrace(); | |
} catch ( IOException e2 ) { | |
e2.printStackTrace(); | |
} | |
System.out.println( "Config: " + td.toString() ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>XStreamPersist</groupId> | |
<artifactId>XStreamPersist</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>ConfigHandler</name> | |
<description>A XStream handler for loading TestNG tests</description> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>org.testng</groupId> | |
<artifactId>testng</artifactId> | |
<version>6.7</version> | |
</dependency> | |
<dependency> | |
<groupId>com.thoughtworks.xstream</groupId> | |
<artifactId>xstream</artifactId> | |
<version>1.4.3</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tr.test; | |
import com.thoughtworks.xstream.annotations.XStreamAlias; | |
@XStreamAlias("testCase") | |
public class TestCase { | |
private String testNumber; | |
private String firstName; | |
private String lastName; | |
public TestCase(String testNum, String firstName, String lastName) { | |
this.setTestNumber(testNum); | |
this.setLastName(lastName); | |
this.setFirstName(firstName); | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getTestNumber() { | |
return testNumber; | |
} | |
public void setTestNumber(String testNumber) { | |
this.testNumber = testNumber; | |
} | |
@Override | |
public String toString() { | |
return "{" + this.testNumber + "," + this.firstName + "," + this.lastName + "}"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tr.test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.w3c.dom.Document; | |
import com.thoughtworks.xstream.annotations.XStreamAlias; | |
@XStreamAlias("testCases") | |
public class TestCases { | |
private String lastRun; | |
private List<TestCase> tests = new ArrayList<TestCase>(); | |
public TestCases( String lr ) { | |
this.lastRun = lr; | |
} | |
public void setLastRun(String lr ) { | |
this.lastRun = lr; | |
} | |
public List<TestCase> getTests() { | |
return tests; | |
} | |
public Document getTestsAsXml() { | |
return null; | |
// nothing yet | |
} | |
public void pushTestCase( TestCase tc ) { | |
this.tests.add( tc ); | |
} | |
public void popTestCase() { | |
this.tests.remove( this.tests.size() - 1 ); | |
System.out.println("Removed last item from testcases. New tests size is " + this.tests.size() ); | |
} | |
public TestCase getTestCaseByIndex( int pos ) { | |
TestCase tc = this.tests.get( pos ); | |
return tc; | |
} | |
public String getLastRun() { | |
return this.lastRun; | |
} | |
@Override | |
public String toString() { | |
String objRep = "{" + this.lastRun + "{"; | |
for ( int i=0; i < this.tests.size(); i++ ) { | |
TestCase ti = tests.get(i); | |
objRep = objRep + "{" + ti.getTestNumber() + "," + ti.getLastName() + "," + ti.getFirstName() + "}"; | |
if ( i != this.tests.size() -1 ) objRep = objRep + ","; | |
} | |
objRep = objRep + "}}"; | |
return objRep; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tr.test; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.List; | |
import com.thoughtworks.xstream.XStream; | |
import com.thoughtworks.xstream.annotations.XStreamAlias; | |
import com.thoughtworks.xstream.io.xml.DomDriver; | |
@XStreamAlias("testconfig") | |
public class TestConfig { | |
private String emailAddress; | |
private String emailPassword; | |
private String smtpServer; | |
private String emailPort; | |
private TestSubject subJect; | |
private TestCases tests; | |
public TestConfig( String emailAddress, String emailPassword, String smtpServer, String emailPort ) { | |
this.setEmailAddress(emailAddress); | |
this.setEmailPassword(emailPassword); | |
this.setSmtpServer(smtpServer); | |
this.setEmailPort(emailPort); | |
} | |
public String getEmailAddress() { | |
return emailAddress; | |
} | |
public void setEmailAddress(String emailAddress) { | |
this.emailAddress = emailAddress; | |
} | |
public String getEmailPassword() { | |
return emailPassword; | |
} | |
public void setEmailPassword(String emailPassword) { | |
this.emailPassword = emailPassword; | |
} | |
public String getSmtpServer() { | |
return smtpServer; | |
} | |
public void setSmtpServer(String smtpServer) { | |
this.smtpServer = smtpServer; | |
} | |
public String getEmailPort() { | |
return emailPort; | |
} | |
public void setEmailPort(String emailPort) { | |
this.emailPort = emailPort; | |
} | |
public TestSubject getTestSubject() { | |
return subJect; | |
} | |
public void setTestSubject(TestSubject subJect) { | |
this.subJect = subJect; | |
} | |
public TestCases getTestCasesObject() { | |
return tests; | |
} | |
public List<TestCase> getTestCasesList() { | |
System.out.println("getTests()"); | |
XStream xstream = new XStream( new DomDriver() ); | |
xstream.processAnnotations(TestConfig.class); | |
xstream.processAnnotations(TestSubject.class); | |
xstream.addImplicitCollection(TestCases.class, "tests","testcase", TestCase.class); | |
xstream.processAnnotations(TestCases.class); | |
xstream.useAttributeFor("lastRun", TestCases.class); | |
xstream.processAnnotations(TestCase.class); | |
TestConfig td = null; | |
try { | |
FileReader fr = new FileReader( configFile ); | |
td = (TestConfig)xstream.fromXML( fr ); | |
fr.close(); | |
} catch ( FileNotFoundException e1 ) { | |
e1.printStackTrace(); | |
} catch ( IOException e2 ) { | |
e2.printStackTrace(); | |
} | |
return td.getTcases().getTests(); | |
} | |
public void setTestCases( TestCases tcs ) { | |
this.tests = tcs; | |
} | |
@Override | |
public String toString() { | |
return "{" + this.emailAddress + "," + this.emailPassword + "," + this.smtpServer + | |
":" + this.emailPort + "," + this.subJect.toString() + "," + this.tests.toString() +"}" ; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<testconfig> | |
<emailAddress>go@gmail.com</emailAddress> | |
<emailPassword>password</emailPassword> | |
<smtpServer>smtp.mailserver.com</smtpServer> | |
<emailPort>565</emailPort> | |
<subJect> | |
<protoCol>https</protoCol> | |
<doMain>domain.com</doMain> | |
<porT>3343</porT> | |
<uRI>/app/login.jsp</uRI> | |
<userName>sa</userName> | |
<passWord>password</passWord> | |
</subJect> | |
<tests> | |
<lastRun>2012-03-01</lastRun> | |
<testcase> | |
<testNumber>1</testNumber> | |
<firstName>Jon</firstName> | |
<lastName>Doe</lastName> | |
</testcase> | |
<testcase> | |
<testNumber>2</testNumber> | |
<firstName>Janet</firstName> | |
<lastName>Smith</lastName> | |
</testcase> | |
<testcase> | |
<testNumber>3</testNumber> | |
<firstName>Jack</firstName> | |
<lastName>Duff</lastName> | |
</testcase> | |
</tests> | |
</testconfig> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > | |
<suite name="TestNGSuite" verbose="2" > | |
<test name="RegressionTests"> | |
<classes> | |
<class name="tr.test.ConfigHandler"> | |
<methods> | |
<include name="configwriter" /> | |
<include name="configreader" /> | |
</methods> | |
</class> | |
</classes> | |
</test> | |
</suite> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tr.test; | |
import com.thoughtworks.xstream.annotations.XStreamAlias; | |
@XStreamAlias("testSubject") | |
public class TestSubject { | |
private String protoCol; | |
private String doMain; | |
private String porT; | |
private String uRI; | |
private String userName; | |
private String passWord; | |
public TestSubject(String protoCol, String doMain, String porT, String uRI, | |
String userName, String passWord ) { | |
this.setProtoCol(protoCol); | |
this.setDoMain(doMain); | |
this.setPorT(porT); | |
this.setURI(uRI); | |
this.setUserName(userName); | |
this.setPassWord(passWord); | |
} | |
public String getProtoCol() { | |
return protoCol; | |
} | |
public void setProtoCol(String protoCol) { | |
this.protoCol = protoCol; | |
} | |
public String getDoMain() { | |
return doMain; | |
} | |
public void setDoMain(String doMain) { | |
this.doMain = doMain; | |
} | |
public String getPorT() { | |
return porT; | |
} | |
public void setPorT(String porT) { | |
this.porT = porT; | |
} | |
public String getURI() { | |
return uRI; | |
} | |
public void setURI(String uRI) { | |
this.uRI = uRI; | |
} | |
public String getUserName() { | |
return userName; | |
} | |
public void setUserName(String userName) { | |
this.userName = userName; | |
} | |
public String getPassWord() { | |
return passWord; | |
} | |
public void setPassWord(String passWord) { | |
this.passWord = passWord; | |
} | |
@Override | |
public String toString() { | |
return "{" + this.protoCol + "://" + | |
this.doMain + this.uRI + "?username=" + | |
this.userName + "&" + this.passWord + "}" ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment