Guest User

gunit exam

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