Skip to main content
summaryrefslogtreecommitdiffstats
blob: e5eb459494682617799cd7b768898431c3c39226 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.logging.Level;
import junit.framework.Assert;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.database.core.OseeInfo;
import org.eclipse.osee.framework.logging.IHealthStatus;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.logging.SevereLoggingMonitor;

/**
 * @author Donald G. Dunne
 */
public class TestUtil {
   private static final String DEMO_DB_TYPE = "demo";
   public static final Collection<String> ignoreLogging = Arrays.asList("No image was defined for art type",
      "Unable to load the image for [SAVED]");
   public static boolean isInTest = false;

   public static boolean isProductionDb() throws OseeCoreException {
      return ClientSessionManager.isProductionDataStore();
   }

   public static boolean isTestDb() throws OseeCoreException {
      return !isProductionDb(); // && !isDemoDb();
   }

   public static boolean isDemoDb() throws OseeCoreException {
      return DEMO_DB_TYPE.equals(OseeInfo.getCachedValue(OseeInfo.DB_TYPE_KEY));
   }

   /**
    * Need to match methods in OseeProperties
    */
   public static boolean isInTest() {
      return Boolean.valueOf(System.getProperty("osee.isInTest"));
   }

   /**
    * Need to match methods in OseeProperties
    */
   public static void setIsInTest(boolean isInTest) {
      System.setProperty("osee.isInTest", String.valueOf(isInTest));
   }

   public static void sleep(long milliseconds) throws Exception {
      System.out.println("Sleeping " + milliseconds);
      Thread.sleep(milliseconds);
      System.out.println("Awake");
   }

   public static SevereLoggingMonitor severeLoggingStart() throws Exception {
      SevereLoggingMonitor monitorLog = new SevereLoggingMonitor();
      OseeLog.registerLoggerListener(monitorLog);
      return monitorLog;
   }

   public static int getNumberOfLogsAtLevel(SevereLoggingMonitor monitorLog, Level level) {
      int count = 0;
      for (IHealthStatus hStatus : monitorLog.getLogsAtLevel(level)) {
         // do not count the valid ignored logs
         for (String str : ignoreLogging) {
            if (hStatus.getMessage().startsWith(str) == false) {
               count++;
            }
         }
      }
      return count++;
   }

   public static void severeLoggingStop(SevereLoggingMonitor monitorLog) {
      OseeLog.unregisterLoggerListener(monitorLog);
   }

   public static void severeLoggingEnd(SevereLoggingMonitor monitorLog) throws Exception {
      severeLoggingEnd(monitorLog, ignoreLogging);
   }

   public static void severeLoggingEnd(SevereLoggingMonitor monitorLog, Collection<String> ignoreLogging) throws Exception {
      OseeLog.unregisterLoggerListener(monitorLog);
      Collection<IHealthStatus> healthStatuses = monitorLog.getAllLogs();
      int numExceptions = 0;
      if (healthStatuses.size() > 0) {
         for (IHealthStatus status : healthStatuses) {
            if (status.getLevel() != Level.INFO) {
               boolean ignoreIt = false;
               for (String str : ignoreLogging) {
                  if (status.getMessage().startsWith(str)) {
                     ignoreIt = true;
                     break;
                  }
               }
               if (ignoreIt) {
                  continue;
               }
               if (status.getException() != null) {
                  StringBuilder sb = new StringBuilder();
                  exceptionToString(status.getException(), sb);
                  System.err.println("SevereLogging Exception: " + sb.toString());
               } else {
                  System.err.println("SevereLogging Exception: " + status.getMessage());
               }
               numExceptions++;
            }
         }
         if (numExceptions > 0) {
            throw new OseeStateException("SevereLoggingMonitor found [%d] exceptions (see console for details)!",
               numExceptions);
         }
      }
   }

   private static void exceptionToString(Throwable ex, StringBuilder sb) {
      if (ex == null) {
         sb.append("Exception == null; can't display stack");
         return;
      }
      sb.append(ex.getMessage() + "\n");
      StackTraceElement st[] = ex.getStackTrace();
      for (int i = 0; i < st.length; i++) {
         StackTraceElement ste = st[i];
         sb.append("   at " + ste.toString() + "\n");
      }
      Throwable cause = ex.getCause();
      if (cause != null) {
         sb.append("   caused by ");
         exceptionToString(cause, sb);
      }
   }

   public static void checkThatIncreased(Map<String, Integer> prevCount, Map<String, Integer> postCount) {
      for (String name : prevCount.keySet()) {
         String incStr = postCount.get(name) > prevCount.get(name) ? "Increased" : "ERROR, Not Increased";
         System.out.println(String.format(incStr + ": [%s] pre[%d] vs post[%d]", name, prevCount.get(name),
            postCount.get(name)));
      }
      for (String name : prevCount.keySet()) {
         Assert.assertTrue(String.format("[%s] did not increase as expected: pre[%d] vs post[%d]", name,
            prevCount.get(name), postCount.get(name)), postCount.get(name) > prevCount.get(name));
      }
   }

   public static void checkThatEqual(Map<String, Integer> prevCount, Map<String, Integer> postCount) {
      for (String tableName : prevCount.keySet()) {
         String equalStr = postCount.get(tableName).equals(prevCount.get(tableName)) ? "Equal" : "ERROR, NotEqual";
         System.out.println(String.format(equalStr + ": [%s] pre[%d] post[%d]", tableName, prevCount.get(tableName),
            postCount.get(tableName)));
      }
      for (String tableName : prevCount.keySet()) {
         Assert.assertTrue(
            String.format("[%s] count not equal pre[%d] post[%d]", tableName, prevCount.get(tableName),
               postCount.get(tableName)), postCount.get(tableName).equals(prevCount.get(tableName)));
      }
   }

}

Back to the top