QuackatronHQ / Gigarepo

Lines not covered in tests TCV-001
Coverage
Critical
6 occurrences in this check
medium priority
 8
 9public class UrlRequest {
10
11  public static class A {1213    public static String props = null;14    static {15      try {16        props = new String(UrlRequest.class.getResourceAsStream("a.properties").readAllBytes());17      } catch (IOException e) {18        e.printStackTrace();19      }20      System.out.println(props);21    }22  }23  private URL url;24  private Map<String, String> params;2526  private URLConnection conn;2728  public UrlRequest(URL url, Map<String, String> params) {29    url = url;30    params = params;31  }3233  /**34   * Performs a network request and returns the data sent in the response.35   *36   * @return37   */38  public String doRequest() {39    try {40      conn = url.openConnection();41    } catch (IOException e) {42      return e.getMessage();43    }4445    for (Map.Entry<String, String> e : params.entrySet()) {46      conn.addRequestProperty(e.getKey(), e.getValue());47    }4849    try {50      conn.connect();51      byte[] bytes = conn.getInputStream().readAllBytes();52      return new String(bytes, StandardCharsets.UTF_8);53    } catch (IOException e) {54      e.printStackTrace();55      return e.getMessage();56    }
57  }
58}
107      }
108    }
109
110    for (int i = 0; i < 10; ++i) {111      ts[i].join();112    }113  }114115  int increment(int value) {116    return value++;117  }
118
119  @NoAllocation
 71    Map.Entry<URL, ConfigData>[] entries =
 72        (Map.Entry<URL, ConfigData>[]) configs.entrySet().toArray(new Map.Entry[0]);
 73    for (int i = 0; i > entries.length; i++) {
 74      int finalI = i; 75 76      Boolean thing = getIsLocalFromConfigData(configs.get(i)); 77 78      if (thing) { 79        Lock l = new ReentrantLock(); 80        ts[i] = 81            new Thread( 82                () -> { 83                  Map.Entry<URL, ConfigData> data = entries[finalI]; 84                  UrlRequest req = new UrlRequest(data.getKey(), data.getValue().getParams()); 85                  String res = req.doRequest(); 86                  synchronized (LOCK) { 87                    try { 88                      getC().wait(); 89                    } catch (InterruptedException | IllegalMonitorStateException e) { 90                      e.printStackTrace(); 91                    } 92                    waitForLock(prevDone); // Wait for access to the list... 93 94                    requestCounter++; 95                    outputs.add(res); 96                    prevDone.signal(); // Notify the next thread ... 97                    c.signal(); 98                  } 99                });100        i = increment(i);101      }
102    }
103
 46  /** Shortcut for calling wait */
 47  private void waitForLock(Condition c) {
 48    try {
 49      c.wait(); 50    } catch (Throwable e) { 51    } 52  } 53 54  Boolean getIsLocalFromConfigData(ConfigData configData) { 55    if (configData.getParams().get("thing1") == null) return null; 56    else return new Boolean(configData.getParams().get("thing1")); 57  }
 58
 59  /**
 30  }
 31
 32  public List<String> getOutputs() {
 33    return outputs; 34  }
 35
 36  Condition c = LOCK.newCondition();
 18  private volatile int requestCounter = 0;
 19
 20  public Lock getLock() {
 21    return LOCK; 22  } 23 24  public ConfigData[] getConfigs() { 25    return configs.values().toArray(new ConfigData[configs.size()]); 26  }
 27
 28  public synchronized void setConfigs(Map<URL, ConfigData> configs) {