unit testing with xml in it

i have a existing junit file which basicaly using power mock and validating incoming messages to existings system say XYZ.  I see big XML there with bunch of tags as below

String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>>"
                              + "<Msg><uap>2013-09-2
++ "<id><udd>12

now i need to modify above xml with new xml generated using new xsd.

how to do that?

why xml represented as above with concatenation of + in between
any good ends to end example on this

please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
>>how to do that?

Just set the String message to the new XML.  The "+" you see is string concatenation.

I also do similar things with data that comes to me on multiple lines.  It isn't part of the XML.

If I have letters on three lines like"
a
b
c

Open in new window


And I want a resulting string "abc", I can do:

"a"+
"b"+
"c"

Open in new window


That says: Take the string "a" concatenate it with string "b" and concatenate that with string "c" resulting in "abc".

So if you new xml is
<a>
<b>Hello</b>
<c>world</c>
</a>

Open in new window

to keep it in the same "format" just make it:

String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<a>"+
"<b>Hello</b>"+
"<c>world</c>"+
"</a>"

Open in new window


You could just as easy make it
String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b>Hello</b><c>world</c></a>"

Open in new window


As far as the XML is concerned, they are the same.  Reading them, they are really different.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Miguel OzSenior Software EngineerCommented:
Another alterantive to strings is to create a sample folder in your project and store your test xml files there. The advantage is that later on in life you could troubleshoot your production xml in your unit test by just replacing the file. (depending of hwo dependent the tests are with your file contents  but I found it handy it there are inputs to a more complex logic)
gudii9Author Commented:
any end to end examples around this.

I did not understand where to break xml and concatenae with +

say my example as below
<?xml version="1.0"?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
   <TITLE>Computer Parts</TITLE>
   <PART>
      <ITEM>Motherboard</ITEM>
      <MANUFACTURER>ASUS</MANUFACTURER>
      <MODEL>P3B-F</MODEL>
      <COST> 123.00</COST>
   </PART>
   <PART>
      <ITEM>Video Card</ITEM>
      <MANUFACTURER>ATI</MANUFACTURER>
      <MODEL>All-in-Wonder Pro</MODEL>
      <COST> 160.00</COST>
   </PART>
   <PART>
      <ITEM>Sound Card</ITEM>
      <MANUFACTURER>Creative Labs</MANUFACTURER>
      <MODEL>Sound Blaster Live</MODEL>
      <COST> 80.00</COST>
   </PART>
   <PART>
      <ITEMᡋ inch Monitor</ITEM>
      <MANUFACTURER>LG Electronics</MANUFACTURER>
      <MODEL> 995E</MODEL>
      <COST> 290.00</COST>
   </PART>
</PARTS>

http://www.comptechdoc.org/independent/web/xml/guide/xmlexample.html

how to unit test this is powerpock to parse then validate and generate etc?(before all that how to make it as string)
please advise
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

slightwv (䄆 Netminder) Commented:
It appears you need the ENTIRE XML document to be a string so given the little information you provided, here is a guess.


String message ="<?xml version=\"1.0\"?>"+
"<!DOCTYPE PARTS SYSTEM \"parts.dtd\">"+
"<?xml-stylesheet type=\"text/css\" href=\"xmlpartsstyle.css\"?>"+
"<PARTS>"+
"   <TITLE>Computer Parts</TITLE>"+
"   <PART>"+
"      <ITEM>Motherboard</ITEM>"+
"      <MANUFACTURER>ASUS</MANUFACTURER>"+
"      <MODEL>P3B-F</MODEL>"+
"      <COST> 123.00</COST>"+
"   </PART>"+
"   <PART>"+
"      <ITEM>Video Card</ITEM>"+
"      <MANUFACTURER>ATI</MANUFACTURER>"+
"      <MODEL>All-in-Wonder Pro</MODEL>"+
"      <COST> 160.00</COST>"+
"   </PART>"+
"   <PART>"+
"      <ITEM>Sound Card</ITEM>"+
"      <MANUFACTURER>Creative Labs</MANUFACTURER>"+
"      <MODEL>Sound Blaster Live</MODEL>"+
"      <COST> 80.00</COST>"+
"   </PART>"+
"   <PART>"+
"      <ITEMᡋ inch Monitor</ITEM>"+
"      <MANUFACTURER>LG Electronics</MANUFACTURER>"+
"      <MODEL> 995E</MODEL>"+
"      <COST> 290.00</COST>"+
"   </PART>"+
"</PARTS>"

Open in new window


I do agree with the above suggestion to make it flexible.  If possible, change your test code to read the XML from a file on disk.  Then you never have to change the code again.
Miguel OzSenior Software EngineerCommented:
Given your need my previous comment is correct save the content as XMLfile. You can validate it in code if need when your code reads it. There is no need to make it a string it is just too much work for too little gain.
gudii9Author Commented:
I do agree with the above suggestion to make it flexible.  If possible, change your test code to read the XML from a file on disk.  Then you never have to change the code again.
any end to end example on this?
please advise
slightwv (䄆 Netminder) Commented:
I've never heard of Powermock before but it appears it has a readFile method.  I would start there.

If you have any documentation for Powermock I would see if there is documentation on readFile.

If you are using Powermock I'm sure you have experts working with you that should be able to offer some assistance.

Not sure if this helps but there is a readeFile example here:
https://gist.github.com/fatmind/4110984
gudii9Author Commented:
It appears you need the ENTIRE XML document to be a string so given the little information you provided, here is a guess.

just need to put in  " " and then add + next to it?
slightwv (䄆 Netminder) Commented:
>>just need to put in  " " and then add + next to it?

From the sample you posted, yes.  However you need to make sure you do it in the correct order.  It is simple string concatenation.

https://en.wikipedia.org/wiki/Concatenation

Just follow what I posted in #a42480810
Miguel OzSenior Software EngineerCommented:
Please use the XML reader:
var reader = XmlReader.Create("Your file full path.xml")
//Read  your files

Open in new window

You can find sample code in the links below:
https://www.dotnetperls.com/xmlreader
http://www.c-sharpcorner.com/UploadFile/167ad2/read-xml-file-using-xml-reader-in-C-Sharp/
slightwv (䄆 Netminder) Commented:
@Miguel,

Can you confirm Powermock uses C# or .Net?

If it does, I would probably use XmlDocument.Load instead of XmlReader.
https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load(v=vs.110).aspx
gudii9Author Commented:
to keep it in the same "format" just make it:

eclipse itself is doing it.

So  have made it working as it is without any changes
gudii9Author Commented:
<?xml version='1.1' encoding='UTF-8'?>
<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\"
   targetNamespace=\"aaacm\" xmlns:aaacm=\"aaacm\">

  <xsd:element name=\"BldSt\">

as as i have to replace " with \" everywhere in xml
then paste xml to eclipse and say enter to move each tag to next line to see nice string formatted xml
StringEncode.png
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.