View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.testtools.common;
17  
18  import java.io.BufferedReader;
19  import java.io.InputStreamReader;
20  import java.net.HttpURLConnection;
21  import java.net.URL;
22  import java.util.Iterator;
23  
24  /**
25   * Prints out status of JiraAware Jiras from JiraAwareContainsFailures.properties and JiraAwareRegexFailures.properties
26   * files.
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class JiraAwareStatusCheck {
31  
32      public static void main(String[] args ) {
33          String baseUrl = "https://jira.kuali.org/browse/";
34          String jira = "";
35          String statusSpan = "<span id=\"status-val\" class=\"value\">";
36  
37          checkJiraMatchesContains(baseUrl, jira, statusSpan);
38          checkJiraMatchesRegex(baseUrl, jira, statusSpan);
39      }
40  
41      private static void checkJiraMatchesContains(String baseUrl, String jira, String statusSpan) {
42          String key;
43          String status;
44          Iterator iter = JiraAwareFailureUtils.jiraMatches.keySet().iterator();
45          while (iter.hasNext()) {
46              key = (String)iter.next();
47              jira = JiraAwareFailureUtils.jiraMatches.getProperty(key);
48              jira = jira.substring(0, jira.indexOf(" "));
49              try {
50                  status = getStatus(baseUrl, jira, statusSpan);
51                  System.out.println(jira + " Status: " + status + " for key: " + key);
52              } catch (Exception e) {
53                  System.out.println("Exception reading " + baseUrl + jira + " " + e.getMessage());
54                  e.printStackTrace();
55              }
56          }
57      }
58  
59      private static void checkJiraMatchesRegex(String baseUrl, String jira, String statusSpan) {
60          String key;
61          String status;
62          Iterator iter = JiraAwareFailureUtils.regexJiraMatches.keySet().iterator();
63          while (iter.hasNext()) {
64              key = (String)iter.next();
65              jira = JiraAwareFailureUtils.regexJiraMatches.getProperty(key);
66              jira = jira.substring(0, jira.indexOf(" "));
67              try {
68                  status = getStatus(baseUrl, jira, statusSpan);
69                  System.out.println(jira + " Status: " + status + " for key: " + key);
70              } catch (Exception e) {
71                  System.out.println("Exception reading " + baseUrl + jira + " " + e.getMessage());
72                  e.printStackTrace();
73              }
74          }
75      }
76  
77      private static String getStatus(String baseUrl, String jira, String statusSpan) throws Exception {
78          String contents = JiraAwareStatusCheck.getText(baseUrl + jira);
79  //        System.out.println("\n" + contents + "\n");
80          contents = contents.substring(contents.indexOf(statusSpan) + statusSpan.length(), contents.length());
81          contents = contents.substring(0, contents.indexOf("</span>"));
82          String status = contents.substring(contents.indexOf("alt=\"") + 5, contents.length());
83          status = status.substring(0, status.indexOf("\""));
84          return status;
85      }
86  
87      public static String getText(String url) throws Exception {
88          URL website = new URL(url);
89          HttpURLConnection connection = (HttpURLConnection)website.openConnection();
90          connection.setInstanceFollowRedirects(true);
91          BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
92  
93          StringBuilder response = new StringBuilder();
94          String inputLine;
95  
96          while ((inputLine = in.readLine()) != null) {
97              response.append(inputLine);
98          }
99  
100         in.close();
101 
102         return response.toString();
103     }
104 }