[xwiki-dev] Let JUnit test work with files
Hi all... At the moment I working to get a high quality html 2 xwiki converter ready. And here is testing a very important point. The current situation is that I have the whole html and xwiki code directly in the java file. And I want to change it because its easier to writteand maintain tests. I have tried the following: public void testInlineStyle2() { String html = readTextFile("inlineStyle_1.html"); String xwiki = readTextFile("inlineStyle_1.xwiki"); String result = Converter.convertHtml2XWiki(html); assertEquals(xwiki, result); } private static String readTextFile(String fullPathFilename) { StringBuffer sb = new StringBuffer(1024); try { BufferedReader reader = null;reader = new BufferedReader(new FileReader(fullPathFilename)); char[] chars = new char[1024]; int numRead = 0; while ((numRead = reader.read(chars)) > -1) { sb.append(String.valueOf(chars)); } reader.close(); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } inlineStyle_1.html and inlineStyle_1.xwiki are in the same dir/package as the ConverterTest, but I get a FileNotFoundException. Maybe somebody has an idea how to fix it. If you want you can check in the fix in the wysiwyg new architecture branch. Oh.. in the next days I will post here the proposal of the new architecture to get as much as possible feedback. thanks, Christian
Hi Christian, Why do you have to work with files? If I were you I'd work with content in memory, not files. That's much better because: 1/ The test and the input data are in the same location making it easier to understand and maintain the test 2/ No IO thus less complex (your post proves it) 3/ Much faster WDYT? Thanks -Vincent On Mar 29, 2007, at 2:29 PM, Christian Gmeiner wrote:
Hi all...
At the moment I working to get a high quality html 2 xwiki converter ready. And here is testing a very important point. The current situation is that I have the whole html and xwiki code directly in the java file. And I want to change it because its easier to writteand maintain tests. I have tried the following:
public void testInlineStyle2() {
String html = readTextFile("inlineStyle_1.html"); String xwiki = readTextFile("inlineStyle_1.xwiki");
String result = Converter.convertHtml2XWiki(html); assertEquals(xwiki, result); }
private static String readTextFile(String fullPathFilename) {
StringBuffer sb = new StringBuffer(1024); try { BufferedReader reader = null;reader = new BufferedReader(new FileReader(fullPathFilename)); char[] chars = new char[1024]; int numRead = 0; while ((numRead = reader.read(chars)) > -1) { sb.append(String.valueOf(chars)); } reader.close();
} catch (Exception e) { e.printStackTrace(); }
return sb.toString(); }
inlineStyle_1.html and inlineStyle_1.xwiki are in the same dir/package as the ConverterTest, but I get a FileNotFoundException. Maybe somebody has an idea how to fix it. If you want you can check in the fix in the wysiwyg new architecture branch.
Oh.. in the next days I will post here the proposal of the new architecture to get as much as possible feedback.
thanks, Christian
Hi,
Why do you have to work with files? If I were you I'd work with content in memory, not files. That's much better because:
I load the files into memory :)
1/ The test and the input data are in the same location making it easier to understand and maintain the test
Adding about 150 lines (html and xwiki) makes the test file very very big. I have here laying much bigger html files (400 lines because of the <style> stuff) and I dont want to add them.
2/ No IO thus less complex (your post proves it)
But is very hard to add 150 lines of a String to the source. Have a look at: html = "<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;" + "border:none;mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 3.5pt 0cm 3.5pt'>" + " <tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Date <o:p></o:p></span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Version<o:p></o:p></span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Description<o:p></o:p></span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Author<o:p></o:p></span></p>" + " </td>" + " </tr>" + "<tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>09.04.2006</span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>1.0</span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:v" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>Bla</span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Christian<o:p></o:p></span></p>" + " </td>" + " </tr>" + "</table>"; Doing copy and paste in a file is much easier to handle...
3/ Much faster
For me its no problem if the test is a little bit slower... the quality of tests are much important. Greets, Christian
On Mar 29, 2007, at 3:01 PM, Christian Gmeiner wrote:
Hi,
Why do you have to work with files? If I were you I'd work with content in memory, not files. That's much better because:
I load the files into memory :)
1/ The test and the input data are in the same location making it easier to understand and maintain the test
Adding about 150 lines (html and xwiki) makes the test file very very big. I have here laying much bigger html files (400 lines because of the <style> stuff) and I dont want to add them.
2/ No IO thus less complex (your post proves it)
But is very hard to add 150 lines of a String to the source. Have a look at:
Why do you need 150 lines? I'd say 1-3 lines should be enough no? You're writing unit tests right? [snip]
3/ Much faster
For me its no problem if the test is a little bit slower... the quality of tests are much important.
I agree that quality is the most important and I don't see how you'll get more quality by having a big file with fixed content. I would have though it would be better to have small chunks of text with lots of variations to test all cases. Also if you have a big text it means you'll have a single test. Not good for debugging/finding issues. So instead of doing: testParsing() with a big file I think it would be better to have: testParsingTable() testParsingParaggrpah() testParsingParagraphWhenBadlyFormed() etc WDYT? Am I not understanding what you're doing? :-) (very likely) Thanks -Vincent
Hi 2007/3/29, Vincent Massol <[email protected]>:
On Mar 29, 2007, at 3:01 PM, Christian Gmeiner wrote:
Hi,
Why do you have to work with files? If I were you I'd work with content in memory, not files. That's much better because:
I load the files into memory :)
1/ The test and the input data are in the same location making it easier to understand and maintain the test
Adding about 150 lines (html and xwiki) makes the test file very very big. I have here laying much bigger html files (400 lines because of the <style> stuff) and I dont want to add them.
2/ No IO thus less complex (your post proves it)
But is very hard to add 150 lines of a String to the source. Have a look at:
Why do you need 150 lines?
Because I am not wokring with special tests - I am working with real html files, mostly exported from Office dcuments. I have small tests to validate the simple stuff, but when I have a "real-life" file a lot of stuff can be different.
I'd say 1-3 lines should be enough no?
I depends.. if I am testing tables or styles 1-3 lines are not enought. Have a look at this simple one: String html = "<table>" + " <col/>" + " <col/>" + " <col/>" + " <thead>" + " <tr>" + " <th>Col 1</th>" + " <th>Col 2</th>" + " <th>Col 3</th>" + " </tr>" + " </thead>" + " <tbody>" + " <tr>" + " <td>Some content</td>" + " <td>in this table, not lots, but</td>" + " <td>it varies.</td>" + " </tr>" + " <tr>" + " <td>This row has significantly more words in it</td>" + " <td>Although not every cell does, like the following cell is empty.</td>" + " <td> </td>" + " </tr>" + " </tbody>" + "</table>"; If you can tell me, how to do this in 3 lines.. than I am impressed.
You're writing unit tests right?
yes..
[snip]
3/ Much faster
For me its no problem if the test is a little bit slower... the quality of tests are much important.
I agree that quality is the most important and I don't see how you'll get more quality by having a big file with fixed content. I would have though it would be better to have small chunks of text with lots of variations to test all cases.
Because the "faked" html can not be compared to real life html.
Also if you have a big text it means you'll have a single test. Not good for debugging/finding issues. So instead of doing:
testParsing() with a big file
I think it would be better to have:
testParsingTable() testParsingParaggrpah() testParsingParagraphWhenBadlyFormed() etc
This is what i am doing... but as I wrote in this answer, test can be huge.
WDYT? Am I not understanding what you're doing? :-) (very likely)
I a few words: Produce a good output (xwiki syntax) of html files, which are exported from office documents. Greets, Christian
Hi, I'm not a commiter, but if I may share may point of view, I also prefer working with files when they are of certain size. It's easier to edit it with you IDE :) Instead of placing the file somewhere in the path, I place it within the package path. It can be fetched as a resource from the class. A bonus is that the location if the file does not matter, as long as the classpath is correct. Example (not tested, view it as pseudo code:) ) public byte[] readResourceExample() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); InputStream in = null; try { // (option 1) in = getClass().getResourceAsStream( "/rootPackage/myfile.html" ); // (option 2) in = getClass().getResourceAsStream( "childPackageOfCurrentClassPackage/myfile.html" ); BufferedInputStream bin = new BufferedInputStream( in ); int read; byte[] buffer = new byte[1024]; while ( (read = bin.read( buffer )) >= 0 ) { bout.write( buffer, 0, read ); } } finally { if ( in != null ) { try { in.close(); } catch ( IOException e ) { // ignore } } } return bout.toByteArray(); } Regards, Melv On 3/29/07, Christian Gmeiner <[email protected]> wrote:
Hi,
Why do you have to work with files? If I were you I'd work with content in memory, not files. That's much better because:
I load the files into memory :)
1/ The test and the input data are in the same location making it easier to understand and maintain the test
Adding about 150 lines (html and xwiki) makes the test file very very big. I have here laying much bigger html files (400 lines because of the <style> stuff) and I dont want to add them.
2/ No IO thus less complex (your post proves it)
But is very hard to add 150 lines of a String to the source. Have a look at:
html = "<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;" + "border:none;mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 3.5pt 0cm 3.5pt'>" + " <tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>"
+ " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Date <o:p></o:p></span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Version<o:p></o:p></span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Description<o:p></o:p></span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Author<o:p></o:p></span></p>" + " </td>" + " </tr>" + "<tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>09.04.2006</span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>1.0</span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:v" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>Bla</span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Christian<o:p></o:p></span></p>" + " </td>" + " </tr>" + "</table>";
Doing copy and paste in a file is much easier to handle...
3/ Much faster
For me its no problem if the test is a little bit slower... the quality of tests are much important.
Greets, Christian
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help ObjectWeb mailing lists service home page: http://www.objectweb.org/wws
On Mar 29, 2007, at 3:38 PM, Melv Ng wrote:
Hi,
I'm not a commiter, but if I may share may point of view, I also prefer working with files when they are of certain size. It's easier to edit it with you IDE :)
Sure. My question was what types of tests you are doing. I think Christian is doing integration/functional tests and not unit tests. The distinction is probably blurred by the fact that he's using JUnit for both types
Instead of placing the file somewhere in the path, I place it within the package path. It can be fetched as a resource from the class. A bonus is that the location if the file does not matter, as long as the classpath is correct.
I would write something like (see http://jakarta.apache.org/commons/io/api-release/org/apache/ commons/io/IOUtils.html): String content = IOUtils.toString(getClass().getResourceAsStream("/ path/to/file")); Sounds simpler to me ;-) Thanks -Vincent
Example (not tested, view it as pseudo code:) ) public byte[] readResourceExample() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream();
InputStream in = null; try { // (option 1) in = getClass().getResourceAsStream( "/rootPackage/ myfile.html" ); // (option 2) in = getClass().getResourceAsStream( "childPackageOfCurrentClassPackage/myfile.html" ); BufferedInputStream bin = new BufferedInputStream( in ); int read; byte[] buffer = new byte[1024]; while ( (read = bin.read( buffer )) >= 0 ) { bout.write( buffer, 0, read ); } } finally { if ( in != null ) { try { in.close(); } catch ( IOException e ) { // ignore } } }
return bout.toByteArray(); }
Regards, Melv
On 3/29/07, Christian Gmeiner <[email protected]> wrote:
Hi,
Why do you have to work with files? If I were you I'd work with content in memory, not files. That's much better because:
I load the files into memory :)
1/ The test and the input data are in the same location making it easier to understand and maintain the test
Adding about 150 lines (html and xwiki) makes the test file very very big. I have here laying much bigger html files (400 lines because of the <style> stuff) and I dont want to add them.
2/ No IO thus less complex (your post proves it)
But is very hard to add 150 lines of a String to the source. Have a look at:
html = "<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;" + "border:none;mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 3.5pt 0cm 3.5pt'>" + " <tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>"
+ " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Date <o:p></o:p></span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Version<o:p></o:p></ span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Description<o:p></o:p></ span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Author<o:p></o:p></span></ p>" + " </td>" + " </tr>" + "<tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>09.04.2006</span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>1.0</span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:v" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>Bla</span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Christian<o:p></o:p></ span></p>" + " </td>" + " </tr>" + "</table>";
Doing copy and paste in a file is much easier to handle...
3/ Much faster
For me its no problem if the test is a little bit slower... the quality of tests are much important.
Greets, Christian
-- You receive this message as a subscriber of the xwiki- [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help ObjectWeb mailing lists service home page: http:// www.objectweb.org/wws
-- You receive this message as a subscriber of the xwiki- [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help ObjectWeb mailing lists service home page: http://www.objectweb.org/ wws
On Mar 29, 2007, at 3:50 PM, Vincent Massol wrote:
On Mar 29, 2007, at 3:38 PM, Melv Ng wrote:
Hi,
I'm not a commiter, but if I may share may point of view, I also prefer working with files when they are of certain size. It's easier to edit it with you IDE :)
Sure. My question was what types of tests you are doing. I think Christian is doing integration/functional tests and not unit tests. The distinction is probably blurred by the fact that he's using JUnit for both types
Actually it doesn't really matter what types of tests it is... so strike that. I didn't read my email again before sending it :) I still believe the strategy should be: 1) Try to work with test data in the test class itself 2) Only use files for large content but then after questioning whether such large files are required
Instead of placing the file somewhere in the path, I place it within the package path. It can be fetched as a resource from the class. A bonus is that the location if the file does not matter, as long as the classpath is correct.
I would write something like (see http://jakarta.apache.org/commons/io/api-release/org/apache/ commons/io/IOUtils.html):
String content = IOUtils.toString(getClass().getResourceAsStream("/ path/to/file"));
Sounds simpler to me ;-)
Thanks -Vincent
-Vinent
Example (not tested, view it as pseudo code:) ) public byte[] readResourceExample() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream();
InputStream in = null; try { // (option 1) in = getClass().getResourceAsStream( "/rootPackage/ myfile.html" ); // (option 2) in = getClass().getResourceAsStream( "childPackageOfCurrentClassPackage/myfile.html" ); BufferedInputStream bin = new BufferedInputStream( in ); int read; byte[] buffer = new byte[1024]; while ( (read = bin.read( buffer )) >= 0 ) { bout.write( buffer, 0, read ); } } finally { if ( in != null ) { try { in.close(); } catch ( IOException e ) { // ignore } } }
return bout.toByteArray(); }
Regards, Melv
On 3/29/07, Christian Gmeiner <[email protected]> wrote:
Hi,
Why do you have to work with files? If I were you I'd work with content in memory, not files. That's much better because:
I load the files into memory :)
1/ The test and the input data are in the same location making it easier to understand and maintain the test
Adding about 150 lines (html and xwiki) makes the test file very very big. I have here laying much bigger html files (400 lines because of the <style> stuff) and I dont want to add them.
2/ No IO thus less complex (your post proves it)
But is very hard to add 150 lines of a String to the source. Have a look at:
html = "<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;" + "border:none;mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 3.5pt 0cm 3.5pt'>" + " <tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>"
+ " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Date <o:p></o:p></span></ p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Version<o:p></o:p></ span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Description<o:p></o:p></ span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border:solid windowtext .5pt;" + " border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Author<o:p></o:p></ span></p>" + " </td>" + " </tr>" + "<tr>" + " <td width=179 valign=top style='width:107.15pt;border:solid windowtext .5pt;" + " border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>09.04.2006</span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>1.0</span></p>" + " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:v" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=ProjektStandard style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE>Bla</span></p>"
+ " </td>" + " <td width=179 valign=top style='width:107.2pt;border-top:none;border-left:" + " none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;" + " mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;" + " padding:0cm 3.5pt 0cm 3.5pt'>" + " <p class=MsoNormal style='margin-top:3.0pt;margin-right:0cm;margin-bottom:" + " 3.0pt;margin-left:0cm;line-height:150%'><span lang=DE style='color:black;" + " layout-grid-mode:line'>Christian<o:p></o:p></ span></p>" + " </td>" + " </tr>" + "</table>";
Doing copy and paste in a file is much easier to handle...
3/ Much faster
For me its no problem if the test is a little bit slower... the quality of tests are much important.
Greets, Christian
-- You receive this message as a subscriber of the xwiki- [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help ObjectWeb mailing lists service home page: http:// www.objectweb.org/wws
-- You receive this message as a subscriber of the xwiki- [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help ObjectWeb mailing lists service home page: http:// www.objectweb.org/wws
participants (3)
-
Christian Gmeiner -
Melv Ng -
Vincent Massol