How to read two XML files using XMLUnit - java

I am new to XML.After a lot of search i found XMLUnit to do that but the problem am getting is :
whenever i change the contents of xml file to simple string text and try to execute the code ,it still shows green in junit test which it should not do although it is throwing an exception but my requirement is the signal should be red even the files are not in proper xml format.Enclosing my work for so far.
package mypack;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.xml.sax.SAXException;
public class MyXMLTestCase extends XMLTestCase {
enter code here
#Test
public void testMyXmlTestCase() {
FileReader expected = null;
FileReader output = null;
try {
expected = new FileReader("D:/vivek.xml");
output = new FileReader("D:/rahul.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XMLUnit.setNormalizeWhitespace(Boolean.TRUE);
try {
Diff diff = new Diff(expected,output);
// assertTrue("XMLSimilar"+diff.toString(),diff.similar());
//assertTrue("XMLIdentical"+diff.toString(),diff.identical());
DetailedDiff mydiff = new DetailedDiff(diff);
List allDifferences = mydiff.getAllDifferences();
assertEquals(mydiff.toString(), 0, allDifferences.size());
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Related

Read excel data in page object model

Using windows-7 and keep getting errors to write code to ready excel
Trying to read excel data file in java maven Keep getting error on
line#49 sheet= book.getSheet(sheetname); I have added all dependencies and imported but still can not clear this error.
package com.newTour.qa.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import com.newTour.qa.Base.TestBase;
public class TestUtil extends TestBase {
public static String TESTDATA_SHEET_PATH = "C:\\Users\\shahgee\\newtour.qu\\src\\main\\java\\"
+ "com\\qa\\newtour\\testdata\\MercutyTourTestData.xlsx" ;
static Workbook book;
static Sheet sheet;
public static Object[][]getTestData(String sheetname){
FileInputStream file = null;
try {
file = new FileInputStream(TESTDATA_SHEET_PATH);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
book= WorkbookFactory.create(file);
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sheet = book.getSheet(sheetname);
Object[][]data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
for (int i =0; i <sheet.getLastRowNum();i++){
for (int k =0;k <sheet.getRow(0).getLastCellNum(); k++){
data[i][k]= sheet.getRow(i+1).getCell(k).toString();
}
}
return data;
}
}
try to change the import of Sheet class with
org.apache.poi.ss.usermodel.Sheet
what you use right now is Sheet for Powerpoint Document.
here the reference of the library you use right now:
https://www.oschina.net/uploads/doc/poi-3.1-FINAL/org/apache/poi/hslf/model/Sheet.html

Java create DKIM with jDKIM

Can you give me an example for create DKIM with jDKIM?
I don't see any example for this.
http://james.apache.org/jdkim/
Thanks!
Use: james-jdkim
Have a look at this code, This is the answer you are looking for:
lima here is the selector and domain is yahoogroups.com
PublicKeyRecordRetriever publicKeyRecordRetriever = new DNSPublicKeyRecordRetriever();
PublicKeyRecord keys = new DKIMVerifier()
.publicKeySelector(publicKeyRecordRetriever.getRecords("dns/txt", "lima", "yahoogroups.com"));
String signedMIME = "DKIM-Signature: a=rsa-sha256; b=xxxxxxxxxxxxxxxxxx; s=lima; d=yahoogroups.com; v=1; bh=xxxxxxxxx; h=from:to;"
+ "From: test#yahoogroups.com" + "To: test#yahoogroups.com" + "body";
try {
// Verify message against public key
new DKIMVerifier(publicKeyRecordRetriever).verify(new ByteArrayInputStream(signedMIME.getBytes()));
} catch (Exception e) {
throw e;
}
Have a look if this helps. I am also starting with jDKIM. I just wrote a method to list the signature records for the input message. Puzzled how to parse the signature records.
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.api.SignatureRecord;
import org.apache.james.jdkim.exceptions.FailException;
import org.apache.mailet.Mail;
import org.apache.mailet.Mailet;
public class AlgorithmDkimVerification {
public List<SignatureRecord> verifyDkim(InputStream messageStream)
{
DKIMVerifier verifier = new DKIMVerifier();
List<SignatureRecord> records = null;
try {
records = verifier.verify(messageStream);
} catch (IOException | FailException e) {
e.printStackTrace();
}
System.out.println(records);
return records;
}
}

Apache Tika - PrintWriter works on local Windows machine but not Linux box

I'm stumped. This script works great in Eclipse on my windows machine, if I hard code the file paths. If I try to take in arguments and run it on my edge node (a linux box), it throws no particular errors but it just leaves an empty output file. I must be missing something stupid, but I am not seeing it. Anyone have any idea what's going on?
package com.trv.cbia.de.tika;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class DocParser {
public Map<String, Object> processRecord(String path) {
Map<String, Object> map = new HashMap<String, Object>();
String docPath = path;
try{
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
InputStream inputstream = new FileInputStream(new File(docPath));
ParseContext pcontext = new ParseContext();
Parser pdfparser = new AutoDetectParser();
pdfparser.parse(inputstream, handler, metadata, pcontext);
map.put("text", handler.toString().replaceAll("\n|\r|\t", " "));
map.put("title", metadata.get(TikaCoreProperties.TITLE));
map.put("pageCount", metadata.get("xmpTPg:NPages"));
} catch (IOException ex){
System.out.println("Caught IOException:" + ex.getMessage());
}
catch(TikaException tx) {
System.out.println("Caught TikaException: " + tx.getMessage());
}
catch(SAXException sx){
System.out.println("Caught SAXException: " + sx.getMessage());
}
return map;
}
public static void main(String args[]){
String file = args[0];
String out = args[1];
DocParser textExtract = new DocParser();
Map<String, Object> extractedMap = textExtract.processRecord(file);
try {
PrintWriter writer = new PrintWriter(out,"UTF-8");
writer.println(extractedMap.get("text"));
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
Ended up being that I needed to add tika-app-1.13.jar to my classpath. It never divulged any classpath errors. I had to dig through a bunch of the apache mailing list to find someone with a similar problem. Posting the solution here in case anyone else runs across it.

How to render PNG image from DFX file by using kabeja package?

I'm new to this kabeja package so please can some one provide code example or reading material to render PNG from DXF file using Java?
This is the sample code that generate PNG image from DXF file.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.parser.*;
import org.kabeja.parser.ParserBuilder;
import org.kabeja.svg.SVGGenerator;
import org.kabeja.xml.*;
public class MyClass {
public static void main(String[] args) {
MyClas x=new MyClas();
x.parseFile("C:\\Users\\Space\\Desktop\\test2.dxf");
}
public void parseFile(String sourceFile) {
try {
FileOutputStream o=new FileOutputStream("C:\\Users\\Space\\Desktop\\test2.png");
InputStream in = new FileInputStream(sourceFile);//your stream from upload or somewhere
Parser dxfParser = ParserBuilder.createDefaultParser();
dxfParser.parse(in, "");
DXFDocument doc = dxfParser.getDocument();
SVGGenerator generator = new SVGGenerator();
//org.xml.sax.InputSource out = SAXPNGSerializer;
SAXSerializer out = new org.kabeja.batik.tools.SAXPNGSerializer();
out.setOutput(o);
generator.generate(doc,out,new HashMap());
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
Hope you get what you required :)

Append Data to Word using JavatoWord API failing

I'm trying to add data to Word document in my automation using JavatoWord API and Word is getting corrupted when I append the data. Can anyone help?
import java.io.*;
import java.text.*;
import java.util.*;
import word.api.interfaces.IDocument;
import word.w2004.Document2004;
import word.w2004.Document2004.Encoding;
import word.w2004.elements.BreakLine;
import word.w2004.elements.Image;
import word.w2004.elements.ImageLocation;
import word.w2004.elements.Paragraph;
import word.w2004.elements.ParagraphPiece;
public void AppendtoWord() throws Exception
{
String strScrShotsFile = "C:/Test.doc/";
String ScrShotsFile = "C:/test.png";
File fileScrShotsFile = new File (strScrShotsFile);
boolean exists = (fileScrShotsFile).exists();
PrintWriter writer = null;
myDoc = new Document2004();
if (!exists) {
try {
writer = new PrintWriter(fileScrShotsFile);
myDoc.encoding(Encoding.UTF_8);
myDoc.company("Comp Inc");
myDoc.author("Test Automation Teams");
myDoc.title("Application Automation Test Results");
myDoc.subject("Screen Shots");
myDoc.setPageOrientationLandscape();
myDoc.addEle(BreakLine.times(2).create());// Document Header and Footer
myDoc.addEle(BreakLine.times(2).create());
myDoc.getHeader().addEle(
Paragraph.withPieces(
ParagraphPiece.with("Screenshots for test case: "),
ParagraphPiece.with( "Test" ).withStyle().bold().create()
).create()
);
myDoc.getFooter().addEle(Paragraph.with(ScrShotsFile).create()); // Images
myDoc.addEle(BreakLine.times(1).create());
myDoc.addEle(Paragraph.with("Test").withStyle().bgColor("RED").create());
writer.println(myDoc.getContent());
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
writer.close();
}
} else {
PrintWriter writer1= null;
try {
writer1 = new PrintWriter(new FileWriter(fileScrShotsFile,true));
myDoc.addEle(Paragraph.with("This is Important").withStyle().bgColor("RED").create());
writer1.println(myDoc.getContent());
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
writer1.close();
}
}

Categories