@Test(groups = {"pulse"}) // test method public void testUserTx() throws Exception { try { String testurl = "http://" + host + ":" + port + "/" + strContextRoot + "/MyServlet?testcase=usertx"; URL url = new URL(testurl); echo("Connecting to: " + url.toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); int responseCode = conn.getResponseCode(); InputStream is = conn.getInputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(is)); String line = null; boolean result = false; String testLine = null; String EXPECTED_RESPONSE = "user-tx-commit:true"; String EXPECTED_RESPONSE2 = "user-tx-rollback:true"; while ((line = input.readLine()) != null) { // echo(line); if (line.indexOf(EXPECTED_RESPONSE) != -1 && line.indexOf(EXPECTED_RESPONSE2) != -1) { testLine = line; echo(testLine); result = true; break; } } Assert.assertEquals(result, true, "Unexpected Results"); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } }
private boolean test(String c) throws Exception { String EXPECTED_RESPONSE = c + ":pass"; boolean result = false; String url = "http://" + host + ":" + port + strContextRoot + "/jpa?testcase=" + c; // System.out.println("url="+url); HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection(); int code = conn.getResponseCode(); if (code != 200) { System.err.println("Unexpected return code: " + code); } else { InputStream is = conn.getInputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = input.readLine()) != null) { if (line.contains(EXPECTED_RESPONSE)) { result = true; break; } } } return result; }
// @Parameters({ "host", "port", "contextroot" }) @Test(groups = {"pulse"}) // test method // public void webtest(String host, String port, String contextroot) throws Exception{ public void executeServlet() throws Exception { try { String testurl = "http://" + host + ":" + port + "/" + strContextRoot + "/test"; // System.out.println("URL is: "+testurl); URL url = new URL(testurl); // echo("Connecting to: " + url.toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); int responseCode = conn.getResponseCode(); InputStream is = conn.getInputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(is)); String line = null; boolean result = false; String testLine = null; String[] regexesToFind = { "(?s)(?m).*Obtained ValidatorFactory: org.hibernate.validator.internal.engine.ValidatorFactoryImpl.*", "(?s)(?m).*case1: No ConstraintViolations found.*", "(?s)(?m).*case2: caught IllegalArgumentException.*", "(?s)(?m).*case3: ConstraintViolation: message: may not be null propertyPath: listOfString.*", "(?s)(?m).*case3: ConstraintViolation: message: may not be null propertyPath: lastName.*", "(?s)(?m).*case3: ConstraintViolation: message: may not be null propertyPath: firstName.*", "(?s)(?m).*case4: No ConstraintViolations found.*" }; final int len = regexesToFind.length; int i; Boolean regexesFound[] = new Boolean[len]; while ((line = input.readLine()) != null) { // for each line in the input, loop through each of the // elements of regexesToFind. At least one must match. boolean found = false; for (i = 0; i < len; i++) { if (found = line.matches(regexesToFind[i])) { regexesFound[i] = Boolean.TRUE; } } } boolean foundMissingRegexMatch = false; String errorMessage = null; for (i = 0; i < len; i++) { if (null == regexesFound[i] || Boolean.FALSE == regexesFound[i]) { foundMissingRegexMatch = true; errorMessage = "Unable to find match for regex " + regexesToFind[i] + " in output from request to " + testurl; break; } } Assert.assertTrue(!foundMissingRegexMatch, errorMessage); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } }