Skip to main content
summaryrefslogtreecommitdiffstats
blob: e9196f56a7dfcae0c348893cfefa41d46216c3b2 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
 * Copyright (c) 2010 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.framework.core.test.exception;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.eclipse.osee.framework.jdk.core.text.tool.Find;
import org.eclipse.osee.framework.jdk.core.text.tool.FindResultsIterator;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.junit.Test;

public class TestStringFormatMessages {
   private static final String formatSuffix = "\\s*\\(\".*?%.*?\"(?<!\\\\\")[^;]+;";
   private static final String throwPattern = "throw new\\s*\\w+" + formatSuffix;
   private static final String formatPattern = "String.format" + formatSuffix;
   private static final Pattern outerPattern = Pattern.compile("\"(.*?)\"(?<!\\\\\")([^;]+);");
   private static final Pattern specifierPattern = Pattern.compile("%-?[\\d.]*(\\w)");

   private FindResultsIterator getResultsIterator() {
      List<String> patterns = new ArrayList<String>(2);
      patterns.add(throwPattern);
      patterns.add(formatPattern);

      File rootSrcDir = new File("c:/UserData/git");
      List<File> files = Lib.recursivelyListFiles(rootSrcDir, Pattern.compile(".+\\.java"));
      Find app = new Find(patterns, files);
      app.find(999999, true);
      return app.getResults().iterator();
   }

   @Test
   public void testFormatMessages() {
      FindResultsIterator iterator = getResultsIterator();
      while (iterator.hasNext()) {
         Matcher matcher = outerPattern.matcher(iterator.currentRegion);
         if (matcher.find()) {
            examineArgs(matcher);
         } else {
            Assert.fail(iterator.currentRegion);
         }
      }
   }

   private void examineArgs(Matcher matcher) {
      String formatMessage = matcher.group(1);
      int argCount = countArguments(matcher.group(2));
      Object[] args = new Object[argCount];

      Matcher specifierMatcher = specifierPattern.matcher(formatMessage);
      int index = 0;
      while (specifierMatcher.find()) {
         if (index >= argCount) {
            Assert.fail(formatMessage);
         }
         String specifier = specifierMatcher.group(1);
         if (specifier.equals("d")) {
            args[index++] = 0;
         } else if (specifier.equals("f")) {
            args[index++] = 0.0f;
         } else if (specifier.equalsIgnoreCase("s")) {
            args[index++] = new Object();
         } else if (specifier.equalsIgnoreCase("x")) {
            args[index++] = Byte.valueOf((byte) 0);
         } else if (specifier.equalsIgnoreCase("b")) {
            args[index++] = true;
         } else if (specifier.equalsIgnoreCase("c")) {
            args[index++] = 'x';
         } else if (specifier.equalsIgnoreCase("t")) {
            args[index++] = new Date();
         } else {
            Assert.fail(String.format("[%s] has unknown format specifier [%s]", formatMessage, specifier));
         }
      }
      if (argCount != index) {
         Assert.assertEquals(formatMessage, index, argCount);
      }

      try {
         String.format(formatMessage, args);
      } catch (RuntimeException ex) {
         Assert.fail(ex.toString());
      }
   }

   private int countArguments(String argString) {
      int argCount = 0;
      int balance = 1;
      char[] chars = argString.toCharArray();

      for (int i = 0; i < chars.length; i++) {
         char c = chars[i];
         if (c == '(') {
            balance++;
         } else if (c == ')') {
            balance--;
         } else if (c == ',' && balance == 1) {
            argCount++;
         }
         if (balance == 0) {
            break;
         }
      }
      return argCount;
   }
}

Back to the top