Advertisement
matteopelucco

HTML Shortener Test (testng)

May 2nd, 2012
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.52 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.testng.Assert;
  11. import org.testng.annotations.Test;
  12.  
  13. public class HtmlShortenerTest {
  14.  
  15.     private static Logger log = LoggerFactory.getLogger(HtmlShortenerTest.class);
  16.    
  17.     @Test(groups = { "enabled" })
  18.     public void test(){
  19.        
  20.         final List<TestCase> testCases = getTestCases();
  21.         for (TestCase test : testCases){
  22.             log.debug("Doing test for test case: {}", test);
  23.             HtmlShortener htmlShortener = new HtmlShortener(test.getTestString(), test.getCutPoint());
  24.             final String cutString =  htmlShortener.cut();
  25.             Assert.assertEquals(cutString, test.getExpectedString());
  26.         }
  27.        
  28.     }
  29.  
  30.     private List<TestCase> getTestCases() {
  31.         List<TestCase> testCases = new LinkedList<TestCase>();
  32.        
  33.         testCases.add(new TestCase(
  34.                 "<html><head></head><body>123456789</body></html>",
  35.                 "<html><head></head><body>123456</body></html>",
  36.                 6
  37.                 ));
  38.        
  39.         testCases.add(new TestCase(
  40.                 "<h2><a href=\"http://www.repubblica.it/spettacoli-e-cultura/2012/05/01/news/concertone_primo_maggio-34286609/\" target=\"_self\" title=\"\">\"Speranza, passione e futuro\"<br>Primo maggio in musica</a><a href=\"javascript:window.open('http://www.repubblica.it/static/popup/2012/primo-maggio.html','','width=645,height=570,scrollbars=no');void(0);\"><span style=\"color: rgb(153, 0, 0);\"><br></span></a><a href=\"http://www.repubblica.it/spettacoli-e-cultura/2012/05/01/news/concertone_primo_maggio-34286609/\" target=\"_self\" title=\"\"> </a></h2>\"",
  41.                 "<h2><a href=\"http://www.repubblica.it/spettacoli-e-cultura/2012/05/01/news/concertone_primo_maggio-34286609/\" target=\"_self\" title=\"\">\"Speranza, passione </a></h2>",
  42.                 20
  43.                 ));
  44.        
  45.         testCases.add(new TestCase(
  46.                 "<div class=\"s9OtherItems\" style=\"float: left; width: 100%\"><div class=\"fluid asin s9a0\" style=\"float: left; width: 33.1%\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/B007IZ1Q5S/ref=s9_pop_gw_g351_ir01?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"Hunger Games (Italian edition) (Chrysalide)\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/51fVBD%2Bkm4L._SL135_.jpg\" alt=\"\" width=\"88\" height=\"135\"></div><span class=\"s9TitleText\">Hunger Games</span></a><div class=\"t11\">Suzanne Collins, F. Paracchini, S...</div><div class=\"gry t11 nt\">Formato Kindle</div><span class=\"s9Price red t14\">EUR&nbsp;6,99</span></div></div></div><div class=\"fluid asin s9a1\" style=\"float: left; width: 33.1%\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/B004445JHM/ref=s9_pop_gw_g23_ir02?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"Western Digital WDBACY5000ABL MY Passport Essential HardDisk\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/31oyxKno2CL._SL135_.jpg\" alt=\"\" width=\"135\" height=\"77\" style=\"margin-top: 29px; margin-bottom:29px;\"></div><span class=\"s9TitleText\">Western Digital WDBACY5000ABL MY...</span></a><br clear=\"none\"><span class=\"listprice gry t11\">EUR&nbsp;103,27</span> <span class=\"s9Price red t14\">EUR&nbsp;81,65</span></div></div></div><div class=\"fluid asin s9a2\" style=\"float: left; width: 33.1%\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/B005890FUI/ref=s9_pop_gw_g349_ir03?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"Kindle Touch: dispositivo di lettura Wi-Fi, schermo touch da 6&quot; a inchiostro elettronico\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/41JpsttW8CL._SL135_.jpg\" alt=\"\" width=\"135\" height=\"135\"></div><span class=\"s9TitleText\">Kindle Touch: dispositivo di lettura...</span></a><br clear=\"none\"><div class=\"t11\">Amazon</div><span class=\"s9Price red t14\">EUR&nbsp;129,00</span></div></div></div><div class=\"fluid asin s9a3\" style=\"float: left; width: 33.1%; display: none\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/8804614870/ref=s9_pop_gw_g14_ir04?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"I fuochi di Valyria. Le cronache del ghiaccio e del fuoco: 11 (Omnibus)\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/41WLxQ8kYrL._SL135_.jpg\" url=\"\" alt=\"\" width=\"85\" height=\"135\"></div><span class=\"s9TitleText\">I fuochi di Valyria. Le cronache del...</span></a><div class=\"t11\">George R. Martin, S. Altieri, G. L...</div><div class=\"gry t11 nt\">Rilegato</div><span class=\"s9Price red t14\">EUR&nbsp;16,15</span></div></div></div><div class=\"fluid asin s9a4\" style=\"float: left; width: 33.1%; display: none\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/B005890FOO/ref=s9_pop_gw_g349_ir05?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"Kindle Touch 3G: dispositivo di lettura Wi-Fi + 3G gratuito, schermo touch da 6&quot; a inchiostro elettronico, il 3G funziona in tutto il mondo\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/41JpsttW8CL._SL135_.jpg\" url=\"\" alt=\"\" width=\"135\" height=\"135\"></div><span class=\"s9TitleText\">Kindle Touch 3G: dispositivo di...</span></a><br clear=\"none\"><div class=\"t11\">Amazon</div><span class=\"s9Price red t14\">EUR&nbsp;189,00</span></div></div></div><div class=\"fluid asin s9a5\" style=\"float: left; width: 33.1%; display: none\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/B002LSHOK8/ref=s9_pop_gw_g23_ir06?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"Western Digital WDBAAU0015HBK Elements Desktop HardDisk\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/31Jf8batzwL._SL135_.jpg\" url=\"\" alt=\"\" width=\"135\" height=\"135\"></div><span class=\"s9TitleText\">Western Digital WDBAAU0015HBK...</span></a><br clear=\"none\"><span class=\"listprice gry t11\">EUR&nbsp;156,51</span> <span class=\"s9Price red t14\">EUR&nbsp;109,00</span></div></div></div><div class=\"fluid asin s9a6\" style=\"float: left; width: 33.1%; display: none\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/B0037FLUYU/ref=s9_pop_gw_g23_ir07?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"Sandisk Extreme Secure Digital Scheda Memoria 16GB\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/51XFd5pMm%2BL._SL135_.jpg\" url=\"\" alt=\"\" width=\"109\" height=\"135\"></div><span class=\"s9TitleText\">Sandisk Extreme Secure Digital Scheda...</span></a><br clear=\"none\"><span class=\"listprice gry t11\">EUR&nbsp;30,56</span> <span class=\"s9Price red t14\">EUR&nbsp;13,85</span></div></div></div></div>",
  47.                 "<div class=\"s9OtherItems\" style=\"float: left; width: 100%\"><div class=\"fluid asin s9a0\" style=\"float: left; width: 33.1%\"><div class=\"inner\"><div class=\"s9hl\" style=\"position: relative\"><a href=\"/gp/product/B007IZ1Q5S/ref=s9_pop_gw_g351_ir01?pf_rd_m=A11IL2PNWYJU7H&amp;pf_rd_s=center-3&amp;pf_rd_r=1BTC5JHXMDN02TNNHPS5&amp;pf_rd_t=101&amp;pf_rd_p=216204067&amp;pf_rd_i=426865031\" class=\"title ntTitle noLinkDecoration\" title=\"Hunger Games (Italian edition) (Chrysalide)\"><div class=\"imageContainer\"><img src=\"http://ecx.images-amazon.com/images/I/51fVBD%2Bkm4L._SL135_.jpg\" alt=\"\" width=\"88\" height=\"135\"></div><span class=\"s9TitleText\">Hunger Games</span></a><div class=\"t11\">Suzanne Collins, F. Paracchini, S...</div></div></div></div></div>",
  48.                 48
  49.                 ));
  50.        
  51.        
  52.         return testCases;
  53.     }
  54.  
  55.     private class TestCase {
  56.        
  57.         private int cutPoint;
  58.         private String testString;
  59.         private String expectedString;
  60.        
  61.         public TestCase(String testString, String expectedString, int cutPoint){
  62.             this.testString = testString;
  63.             this.expectedString = expectedString;
  64.             this.cutPoint = cutPoint;
  65.         }
  66.  
  67.         public int getCutPoint() {
  68.             return cutPoint;
  69.         }
  70.  
  71.         public void setCutPoint(int cutPoint) {
  72.             this.cutPoint = cutPoint;
  73.         }
  74.  
  75.         public String getTestString() {
  76.             return testString;
  77.         }
  78.  
  79.         public void setTestString(String testString) {
  80.             this.testString = testString;
  81.         }
  82.  
  83.         public String getExpectedString() {
  84.             return expectedString;
  85.         }
  86.  
  87.         public void setExpectedString(String expectedString) {
  88.             this.expectedString = expectedString;
  89.         }
  90.  
  91.         @Override
  92.         public String toString() {
  93.             return "TestCase [cutPoint=" + cutPoint + ", expectedString="
  94.                     + expectedString + ", testString=" + testString + "]";
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement