Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e81b806451c9dffa76348eea9e01ab0a5619ed3 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package org.eclipse.osee.ote.core.framework.command;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.server.ExportException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Future;
import java.util.logging.Level;

import org.eclipse.osee.framework.jdk.core.type.IPropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.IHealthStatus;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.ote.Configuration;
import org.eclipse.osee.ote.OTEApi;
import org.eclipse.osee.ote.OTEServerFolder;
import org.eclipse.osee.ote.core.ServiceUtility;
import org.eclipse.osee.ote.core.environment.TestEnvironment;
import org.eclipse.osee.ote.core.environment.status.CommandEndedStatusEnum;
import org.eclipse.osee.ote.core.environment.status.OTEStatusBoard;
import org.eclipse.osee.ote.core.framework.IMethodResult;
import org.eclipse.osee.ote.core.framework.ReturnCode;
import org.eclipse.osee.ote.core.framework.saxparse.ProcessOutfileOverview;
import org.eclipse.osee.ote.message.IMessageTestContext;

public class RunTests implements ITestServerCommand, Serializable {

   private static final long serialVersionUID = 7408758537342855854L;
   private static final String DEVELOPMENT = "Development";
   
   private final IPropertyStore global;
   private final List<IPropertyStore> scripts;
   private volatile boolean cancelAll = false;
   private volatile boolean isRunning = false;
   private final UUID sessionKey;
   private final String guid;
   private TestEnvironment environment;
   private final Configuration configuration;

   public RunTests(String guid, UUID uuid, Configuration configuration, IPropertyStore global, List<IPropertyStore> scripts) {
      this.global = global;
      this.scripts = scripts;
      this.sessionKey = uuid;
      this.guid = guid;
      this.configuration = configuration;
   }

   public UUID getSessionKey() {
      return sessionKey;
   }

   @Override
   public ICommandHandle createCommandHandle(Future<ITestCommandResult> result, ITestContext context) throws ExportException {
      return new RunTestsHandle(result, context, this);
   }

   @Override
   public ITestCommandResult execute(TestEnvironment environment, OTEStatusBoard statusBoard) throws Exception {
      ITestCommandResult retVal = TestCommandResult.SUCCESS;
      isRunning = true;
      IMessageTestContext msgContext = (IMessageTestContext) environment;
      this.environment = environment;
      
      OTEApi ote = ServiceUtility.getService(OTEApi.class);
      OTEServerFolder serverFolder = ote.getServerFolder();
      
      String testType = getTestType();
      File batchFolder = serverFolder.getNewBatchFolder(testType);
      batchFolder.mkdirs();
      if(!isFolderToKeep(testType)){
         serverFolder.markFolderForDelete(batchFolder);
      }
      BatchLog batchLog = new BatchLog(serverFolder.getBatchLogFile(batchFolder));
      batchLog.open();
      
      File batchStatusFile = serverFolder.getBatchStatusFile(batchFolder);
      File batchRunList = serverFolder.getBatchRunList(batchFolder);
      
      setBatchStatus(batchStatusFile, "running");
      setBatchRunList(batchRunList, scripts);
      
      environment.setupOutfileDir(batchFolder.getAbsolutePath());
      msgContext.resetScriptLoader(configuration, global.getArray(RunTestsKeys.classpath.name()));
      
      //Override the command line option only if batch abort has been selected in TestManager
      boolean batchAbortFailMode = global.getBoolean(RunTestsKeys.batchFailAbortMode.name());
      if(batchAbortFailMode){
        System.setProperty("ote.abort.on.fail", "true");
      }  
      else {
         System.clearProperty("ote.abort.on.fail"); 
      }
      
      //Override the command line option only if batch pause has been selected in TestManager
      boolean batchPauseFailMode = global.getBoolean(RunTestsKeys.batchFailPauseMode.name());
      if(batchPauseFailMode){
         System.setProperty("ote.pause.on.fail", "true");
      }   
      else {
         System.clearProperty("ote.pause.on.fail");   
      }

      for (IPropertyStore store : scripts) {
         if (cancelAll) {
            statusBoard.onTestComplete(store.get(RunTestsKeys.testClass.name()),
               store.get(RunTestsKeys.serverOutfilePath.name()),
               store.get(RunTestsKeys.clientOutfilePath.name()), CommandEndedStatusEnum.ABORTED,
               new ArrayList<IHealthStatus>());
            retVal = TestCommandResult.CANCEL;
            continue;

         }
         statusBoard.onTestStart(store.get(RunTestsKeys.testClass.name()), store.get(RunTestsKeys.serverOutfilePath.name()), store.get(RunTestsKeys.clientOutfilePath.name()));
         IMethodResult runResults = environment.getRunManager().run(environment, store);

         CommandEndedStatusEnum status = CommandEndedStatusEnum.RAN_TO_COMPLETION;
         if (runResults.getReturnCode() == ReturnCode.ABORTED) {
            status = CommandEndedStatusEnum.ABORTED;
         }
         if (runResults.getReturnCode() == ReturnCode.ERROR) {
            status = CommandEndedStatusEnum.EXCEPTION;
         }

         statusBoard.onTestComplete(store.get(RunTestsKeys.testClass.name()),
            store.get(RunTestsKeys.serverOutfilePath.name()), store.get(RunTestsKeys.clientOutfilePath.name()),
            status, runResults.getStatus());
         batchLog.flush();
         
         File outfile = new File(store.get(RunTestsKeys.serverOutfilePath.name()));
         generateOutfileSummary(outfile, serverFolder.getResultsFile(outfile));
      }
      setBatchStatus(batchStatusFile, "complete");
      isRunning = false;
      batchLog.close();
      return retVal;
   }
   
   private void setBatchRunList(File batchRunList, List<IPropertyStore> scripts2) {
      StringBuilder sb = new StringBuilder();
      for (IPropertyStore store : scripts) {
         sb.append(store.get(RunTestsKeys.testClass.name()));
         sb.append("\n");
      }
      try {
         Lib.writeStringToFile(sb.toString(), batchRunList);
      } catch (IOException ex) {
         OseeLog.log(getClass(), Level.SEVERE, "Failed to write batch run list", ex);
      }
   }

   private void setBatchStatus(File batchStatusFile, String string) {
      try {
         Lib.writeStringToFile(string, batchStatusFile);
      } catch (IOException ex) {
         OseeLog.log(getClass(), Level.SEVERE, "Failed to write batch status", ex);
      }
   }

   private void generateOutfileSummary(File outfile, File resultsFile){
      String summary = getResults(outfile);
      writeSummaryToFile(resultsFile, summary);
   }
   
   private void writeSummaryToFile(File file, String summary) {
      try {
         Lib.writeStringToFile(summary, file);
      } catch (IOException ex) {
         OseeLog.log(getClass(), Level.SEVERE, "Failed to write outfile summary", ex);
      }
   }

   private String getResults(File outfile){
      ProcessOutfileOverview overview = new ProcessOutfileOverview();
      FileInputStream fis = null;
      try{
         fis = new FileInputStream(outfile);
         overview.run(fis);
      } catch (Exception ex) {
         OseeLog.log(getClass(), Level.SEVERE, "Failed to write outfile summary", ex);
      } finally {
         if(fis != null){
            try {
               fis.close();
            } catch (IOException e) {
               OseeLog.log(getClass(), Level.SEVERE, "Failed to close outfile", e);
            }
         }
      }
      return String.format("%s,%s,%s", overview.getScriptName(), overview.getResults(), overview.getElapsedTime());
   }

   private String getTestType(){
      IPropertyStore props = scripts.get(0);
      return props.get("FormalTestType");
   }
   
   private boolean isFolderToKeep(String testType) {
      if(testType != null && testType.equalsIgnoreCase(DEVELOPMENT)){
         return false;
      } else {
         return true;
      }
   }

   public boolean cancel() {
      cancelAll = true;
      return environment.getRunManager().abort();
   }
   
   public boolean cancelSingle() {
      return environment.getRunManager().abort();
   }

   @Override
   public String getGUID() {
      return guid;
   }

   @Override
   public UUID getUserSessionKey() {
      return sessionKey;
   }

   boolean isRunning() {
      return isRunning;
   }
   
}

Back to the top