Skip to main content
summaryrefslogtreecommitdiffstats
blob: c6e622cc644ef048ba18fb850a3014777fac974e (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2011 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
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 boolean isTestUtil(File file) {
      return (file.getAbsolutePath().endsWith(".java") && file.getName().contains("Util"));
   }

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

   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;
   }

   private final static Pattern extendsPattern = Pattern.compile("class (.*) extends (.*) ");

   public static int getTestMethodCountFromSuperclass(File file, String text) throws IOException {
      Matcher m = extendsPattern.matcher(text);
      if (m.find()) {
         String superClassName = m.group(2);
         //         System.out.println("Found SUPERCLASS " + superClassName);
         if (Strings.isValid(superClassName)) {
            String fullPath = file.getAbsolutePath();
            File superFile = new File(fullPath.replaceFirst(file.getName(), superClassName + ".java"));
            if (superFile.exists()) {
               return getTestMethodCount(Lib.fileToString(superFile));
            }
         }
      }
      return 0;
   }

}

Back to the top