2016-03-24 10 views
1

Soruma şudur: JAVA8 ile lambda ve filtrelerin kullanımı. Farklı yanıt kodlarını test etmek için Java Selenium ile yapılır.Belirli bir durumda Java Akışı'nı kullanarak, yanıt kodlarını eşleme ve filtreleme ile kullanma

Aşağıdaki işlevleri Streams ile dönüştürmek için Lambda'yı en iyi şekilde nasıl kullanabilirim?

Ben Akışları, Java 8'in lamda içine aşağıdaki gibidir planı ayrı istedi kodu:

 for (int i = 0; i < links.size(); i++) { 
     if (!(links.get(i).getAttribute("href") == null) && !(links.get(i).getAttribute("href").equals(""))) { 
      // Find HTTP Status-Code 
      try { 
       statusCode = getResponseCode(links.get(i).getAttribute("href").trim()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      // Check broken link 
      if (statusCode== 404) { 
       System.out.println("Broken of Link# "+i+" "+links.get(i).getAttribute("href")); 
      } 
      else if (statusCode== 400) { 
       System.out.println("Bad Request# "+i+" "+links.get(i).getAttribute("href")); 
      } 
      else if (statusCode== 401) { 
       System.out.println("Unauthorized# "+i+" "+links.get(i).getAttribute("href")); 
      } 
      else if (statusCode== 403) { 
       System.out.println("Forbidden# "+i+" "+links.get(i).getAttribute("href")); 
      } 
      else if (statusCode== 500) { 
       System.out.println("Internal server error# "+i+" "+links.get(i).getAttribute("href")); 
      } 
     } 

    } 

Şimdi ne için var olduğunu:

List<AbstractMap.SimpleImmutableEntry<String,Integer>> variablename = 
    links.stream().map(WebElement::getAttribute("href")); 

ben birlikte bir şeyler yapmaya çalışıyordu 500,403,401,400,404 olmayan her şeyi filtrelemek ve sadece haritalamayı veya benzer bir çifti (linkString, responseCode), ama Lambda ile tam olarak nasıl yapabildiğimi tam olarak biraz sıkıntı yaşıyorum.

Edit1: , o yüzden, akışı üzerinden şeyi koymak için sadece bu örnek size parça o parçayı almak eğer oldukça basit

+0

Filtreniz bu hata kodlarına özel olmalı ya da aralık kullanabilir mi? (2xx & 3xx yanıtları genellikle başarı dönüş kodları olarak kabul edilir, 4xx & 5xx ise hata dönüş kodlarıdır). Bu, kodu daha jenerikken daha kısa yapar. – Aaron

+0

'System.out' çağrılarınızı tutmak ister misiniz? – tddmonkey

+0

Ben sadece aralıkları kullanarak mükemmel olmalı, çünkü 4xx & 5xx bahsetmişken hata dönüş kodları. @Aaron –

cevap

1

içinde olabildiğince bu kadarını kullanmak anlamına vermedi:

Set<Integer> acceptableCodes = new HashSet<>(Arrays.asList(404, 400, 401, 403, 500)); 
links.stream() 
    .map(link -> link.getAttribute("href")) 
    .filter(href -> href != null) 
    .filter(href -> !href.equals("")) 
    .filter(href -> acceptableCodes.contains(getResponseCode(href)) 
    .map(link -> new LinkWithCode(href , getResponseCode(href)) 
    .collect(toList()); 

LinkWithCode bir sınıftır Eğer ho oluşturmanız gerekir: kolay okunması böylece

// create a set of codes you want to include 
Set<Integer> acceptableCodes = new HashSet<>(Arrays.asList(404, 400, 401, 403, 500)); 

// for (int i = 0; i < links.size(); i++) { 
links.stream() 

// convert to the href value as that's all we need later on 
.map(link -> link.getAttribute("href")) 

// filter out anything without a href 
// if (!(links.get(i).getAttribute("href") == null) && !(links.get(i).getAttribute("href").equals(""))) { 
.filter(href -> href != null) 
.filter(href -> !href.equals("")) 

// filter out non-matching status codes 
.filter(href -> acceptableCodes.contains(getResponseCode(href)) 
.map(link -> new LinkWithCode(href , getResponseCode(href)) 
.collect(toList()); 

yorum içermeyen bir araya getirmek Bağlantıyı ve durum kodunu oku. Bu, getResponseCode'un iki kez gerçekleştiği için ağır bir işlem olmadığını varsayar.

Uyarı: Bunu test etmedim ya da bir IDE'ye koydum, bu yüzden yapmanız gereken daha çok şey olabilir.