1 21 package com.jaspersoft.jasperserver.war; 22 23 import com.meterware.httpunit.*; 24 25 import junit.framework.*; 26 import java.net.*; 27 28 35 public class HttpUnitTest extends TestCase { 36 37 private static String homePageUrl = JasperServerConstants.BASE_URL+"/jasperserver/home.html"; 38 39 private static String homePgText = "The world's most popular open source reporting engine"; 40 private static String intRepLink = "REPORTS"; 41 private static String indRepLink = "AllAccounts"; 42 private static String pageText1 = "Accounts"; 43 private static String pageText2 = "Burnaby"; 44 45 private static WebResponse wResponse ; 46 47 51 public HttpUnitTest(String s) { 52 super(s); 53 } 54 55 60 public static void main(String args[]) { 61 try { 62 junit.textui.TestRunner.run(suite()); 63 } catch (Exception _ex) { 64 _ex.printStackTrace(); 65 } 66 } 67 68 public void setUp() throws Exception { 69 if (wResponse == null){ 70 wResponse = commonLoginFunction(homePageUrl); 71 } 72 } 73 74 78 public static Test suite() throws Exception { 79 TestSuite suite = new TestSuite(); 80 TestCase test1 = new HttpUnitTest("testStartURL"); TestCase test2 = new HttpUnitTest("testHomePage"); TestCase test3 = new HttpUnitTest("testLstRepLink"); 84 suite.addTest(test1); 85 suite.addTest(test2); 86 suite.addTest(test3); 87 88 return suite; 89 } 90 91 97 public void testStartURL() throws Exception { 98 WebResponse response = gettingURLResponse(homePageUrl); 99 assertNotNull(homePageUrl+" Response is NULL ", response); 100 assertNotNull(" Login Page Element is Null ", response.getElementsWithName(homePgText)); 101 } 102 103 109 public void testHomePage() throws Exception { 110 assertNotNull(homePageUrl+"Response is NULL ", wResponse); 111 assertNotNull(" Home Page Element is Null ", wResponse.getElementsWithName("j_username")); 112 } 113 114 120 public void testLstRepLink() throws Exception { 121 assertNotNull(" List Response is Null ", wResponse); 122 WebLink link = wResponse.getLinkWith(intRepLink); 123 assertNotNull(intRepLink +"Link is NULL ", link); 124 wResponse = link.click(); 125 } 126 127 136 public void testIndRepLink() throws Exception { 137 boolean matchFound = false; 138 boolean valueFound = false; 139 140 assertNotNull(" Interactive Reports - Response is NULL ", wResponse); 141 142 WebLink link1 = wResponse.getLinkWith(indRepLink); 143 assertNotNull(indRepLink+" Link is NULL ", link1); 144 wResponse = link1.click(); 145 assertNotNull(indRepLink+" Response is NULL ", wResponse); 146 147 String opt = wResponse.getText(); 148 assertNotNull(" Response is NULL ", opt); 149 if(opt.indexOf(pageText1)!= -1 && opt.indexOf(pageText2) != -1 ) 150 matchFound = true; 151 152 assertEquals(matchFound, true); 153 154 WebLink[] linkArray = null; 155 156 WebTable table = wResponse.getTableWithID("pageNumber"); 157 try{ 158 linkArray = table.getTableCell(0,5).getLinks(); 159 assertNotNull("Button Link is Null",linkArray); 160 161 String pageString = table.getCellAsText(0,1); 162 String lastPageNum = pageString.substring(9).trim(); 163 164 pageString = linkArray[0].click().getTableWithID("pageNumber").getCellAsText(0,1); 165 if(pageString.indexOf(lastPageNum)!= pageString.lastIndexOf(lastPageNum)) 166 valueFound = true; 167 assertEquals(valueFound,true); 168 }catch (NullPointerException e) { 169 e.printStackTrace(); 170 } 171 } 172 173 180 private WebResponse commonLoginFunction(String url) throws Exception { 181 WebResponse response = null; 182 WebConversation wcon = new WebConversation(); 183 WebRequest wreq = new GetMethodWebRequest(new URL(url), ""); 184 response = wcon.getResponse(wreq); 185 WebForm form = response.getForms()[0]; 186 assertEquals("Form Action", "j_acegi_security_check", form.getAction()); 187 wreq = form.getRequest(); 188 wreq.setParameter("j_username", JasperServerConstants.USERNAME); 189 wreq.setParameter("j_password", JasperServerConstants.PASSWORD); 190 response = wcon.getResponse(wreq); 191 return response; 192 } 193 194 198 private WebResponse gettingURLResponse(String url) throws Exception { 199 WebResponse response = null; 200 URL serverUrl = new URL(url); 201 WebConversation conversation = new WebConversation(); 202 WebRequest request = new GetMethodWebRequest(serverUrl, ""); 203 response = conversation.getResponse(request); 204 return response; 205 } 206 } | Popular Tags |