Skip to main content
summaryrefslogtreecommitdiffstats
blob: bfb31e371f7bc4b83ea0766aef2a8cce12afa91f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Created on Jun 21, 2011
 *
 * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE
 */
package org.eclipse.osee.support.test.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;

public class UnitTestUtil {

   public static boolean isUnitTest(File file) throws IOException {
      String text = Lib.fileToString(file);
      return isUnitTest(text);
   }

   public static boolean isUnitTest(String fileContents) {
      return (fileContents.contains("@Test") || fileContents.contains("@org.junit.Test"));
   }

   public static boolean isSuite(File file) {
      return (file.getAbsolutePath().endsWith(".java") && file.getName().contains("Suite"));
   }

   public static List<String> getAuthors(File file) throws IOException {
      String text = Lib.fileToString(file);
      return getAuthors(text);

   }

   public static List<String> getAuthors(String fileContents) {
      List<String> authors = new ArrayList<String>();
      for (String line : fileContents.split("\n")) {
         if (line.contains("* @author")) {
            String author = line;
            //            System.out.println("  " + author);
            author = author.replaceAll(":", "");
            author = author.replaceAll(System.getProperty("line.separator"), "");
            author = author.replaceFirst("^.*@author *", "");
            author = author.replaceFirst(" *$", "");
            author = author.replaceAll("\\s+$", "");
            if (Strings.isValid(author)) {
               authors.add(author);
            }
         }
      }
      return authors;
   }

   private final static Pattern testMethodPattern = Pattern.compile("(@Test|@org.junit.Test)");

   public static int getTestMethodCount(String fileContents) {
      Matcher m = testMethodPattern.matcher(fileContents);
      int fileTestPointCount = 0;
      while (m.find()) {
         fileTestPointCount++;
      }
      return fileTestPointCount;
   }
}

Back to the top