mellowdeep

gunit

Apr 24th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6. import java.util.TreeSet;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class GUnit {
  11.  
  12.     public static void main(String[] args) {
  13.        
  14.         List<Framework> data = new ArrayList<>();
  15.        
  16.         try (Scanner scanner = new Scanner(System.in);){
  17.        
  18.             String inputLine = scanner.nextLine();
  19.             String pat = "^([A-Z][a-zA-Z0-9]+)\\s\\|\\s([A-Z][a-zA-Z0-9]+)\\s\\|\\s([A-Z][a-zA-Z0-9]+)$";
  20.                        
  21.             while (true) {
  22.                 if (inputLine.equals("It's testing time!")) {
  23.                     break;
  24.                 }
  25.                 Pattern pattern = Pattern.compile(pat);
  26.                 Matcher match = pattern.matcher(inputLine);
  27.                 if (match.find()) {
  28.                     String className = match.group(1);
  29.                     String methodName = match.group(2);
  30.                     String testName = match.group(3);
  31.                
  32.                     boolean isEmpty = checkForEmptyArray(data, className, methodName, testName);
  33.                     if (!isEmpty) {
  34.                         addObjectsToArray(data, className, methodName, testName);
  35.                     }
  36.                 }
  37.                 inputLine = scanner.nextLine();
  38.             }
  39.    
  40.            
  41.             data.sort((s1, s2) -> s1.getClassName().compareTo(s2.getClassName()));
  42.             data.sort((s1, s2) -> Integer.compare(s1.countMetods , s2.countMetods));
  43.             data.sort((s1, s2) -> Integer.compare(s2.countTests, s1.countTests));
  44.            
  45.             for (Framework framework : data) {
  46.                 System.out.println(framework + ":");
  47.                 framework.getMethods().entrySet()
  48.                                       .stream()
  49.                                       .sorted((s1,s2) -> Integer.compare(s2.getValue().size(),s1.getValue().size()))
  50.                                       .forEach(pair -> {
  51.                     System.out.println("##" + pair.getKey());
  52.                     TreeSet<String> printList = pair.getValue();
  53.                     printList.stream().sorted((s1,s2) -> { return Integer.compare(s1.length(),s2.length()) == 0 ?
  54.                                                s1.compareTo(s2): Integer.compare(s1.length(),s2.length());})
  55.                                       .forEach(str -> System.out.println("####" + str));
  56.                     });;
  57.             }
  58.         } catch (Exception e) {
  59.             System.out.println(e.toString());
  60.         }
  61.        
  62.     }
  63.  
  64.     public static void addObjectsToArray(List<Framework> data,
  65.             String className, String methodName, String testName) {
  66.         boolean isInside = false;
  67.         for (Framework fr : data) {
  68.             if (fr.getClassName().equals(className)) {
  69.                 isInside = true;
  70.                 fr.setMethods(methodName,testName);
  71.                 break;
  72.             }
  73.         }
  74.         if (!isInside) {
  75.             Framework framework = new Framework(className);
  76.             framework.setMethods(methodName,testName);
  77.             data.add(framework);
  78.            
  79.  
  80.         }
  81.     }
  82.  
  83.     public static boolean checkForEmptyArray(List<Framework> data,
  84.             String className, String methodName, String testName) {
  85.  
  86.         if (data.isEmpty()) {
  87.             Framework framework = new Framework(className);
  88.             framework.setMethods(methodName,testName);
  89.             data.add(framework);
  90.             return true;
  91.         }
  92.         return false;
  93.     }
  94.  
  95. }
  96.  
  97. class Framework implements Comparable<Framework> {
  98.  
  99.     private String className;
  100.     private Map<String,TreeSet<String>> methods;
  101.     public int countMetods;
  102.     public int countTests;
  103.  
  104.     public Framework(String className) {
  105.         super();
  106.         this.className = className;
  107.         this.methods = new TreeMap<String,TreeSet<String>>();
  108.        
  109.     }
  110.  
  111.     public String getClassName() {
  112.         return className;
  113.     }
  114.  
  115.     public void setClassName(String className) {
  116.         this.className = className;
  117.     }
  118.    
  119.     public Map<String, TreeSet<String>> getMethods() {
  120.         return methods;
  121.     }
  122.  
  123.     public void setMethods(String method, String test) {
  124.         if (!methods.containsKey(method)) {
  125.             methods.put(method, new TreeSet<String>());
  126.             this.countMetods++;
  127.         }
  128.         int index = methods.get(method).size();
  129.         if (method.contains(method)) {
  130.             methods.get(method).add(test);
  131.             if (methods.get(method).size() != index) {
  132.                 this.countTests++;
  133.             }
  134.         }
  135.        
  136.     }
  137.  
  138.     @Override
  139.     public String toString() {
  140.         return this.className;
  141.     }
  142.  
  143.     public int compareTo(Framework framework) {
  144.         Framework other = framework;
  145.         return Integer.compare(this.countTests, other.countTests);
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment