Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6ee96130ad4de904ca75f3ee7bc9f9ee609be153 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*******************************************************************************
 * 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.ote.core;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.codehaus.jackson.annotate.JsonProperty;
import org.eclipse.osee.framework.jdk.core.persistence.Xmlizable;
import org.eclipse.osee.framework.jdk.core.persistence.XmlizableStream;
import org.eclipse.osee.framework.jdk.core.util.xml.Jaxp;
import org.eclipse.osee.framework.jdk.core.util.xml.XMLStreamWriterUtil;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.ote.core.enums.PromptResponseType;
import org.eclipse.osee.ote.core.environment.EnvironmentTask;
import org.eclipse.osee.ote.core.environment.TestEnvironment;
import org.eclipse.osee.ote.core.environment.interfaces.ICancelTimer;
import org.eclipse.osee.ote.core.environment.interfaces.IExecutionUnitManagement;
import org.eclipse.osee.ote.core.environment.interfaces.IScriptControl;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironmentAccessor;
import org.eclipse.osee.ote.core.environment.interfaces.ITestLogger;
import org.eclipse.osee.ote.core.environment.interfaces.ITestStation;
import org.eclipse.osee.ote.core.environment.interfaces.ITimeout;
import org.eclipse.osee.ote.core.environment.interfaces.ITimerControl;
import org.eclipse.osee.ote.core.log.record.RequirementRecord;
import org.eclipse.osee.ote.core.log.record.TestCaseRecord;
import org.eclipse.osee.ote.core.log.record.TestDescriptionRecord;
import org.eclipse.osee.ote.core.log.record.TestRecord;
import org.eclipse.ote.scheduler.Scheduler;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * TestCase is the abstract base class for all test cases. This class provides the interfaces necessary for a TestCase
 * to be part of a {@link org.eclipse.osee.ote.core.TestScript TestScript}.
 * <p>
 * The TestCase class contains the following information:
 * <ul>
 * <li><b>Standalone</b> - This is used to determine if a TestCase can be used in a selective run list.
 * <ul>
 * <li><b>true</b> - The TestCase runs as expected regardless of order.
 * <li><b>false</b> - The TestCase must be run in a given order with other test cases to work as expected.
 * </ul>
 * <li><b>Test Case Number</b> - A serial number automatically assigned at the time of construction.
 * <li><b>Tracability</b> - The requirements that are tested by this TestCase.
 * </ul>
 * <p>
 * When building a new TestCase object it is important to remember that the object contains information describing the
 * test case in addition to the run time code. It is also important to understand that the when writing classes which
 * within a TestScript that the object must be instantiated (typically done in the constructor) with a reference to the
 * TestScript it is within or else it will not be executed.
 * <p>
 * The following sample should be followed closely when building TestCase objects. <br>
 * <br>
 * <code>
 * public class OipCase extends TestCase {
 * 		<ul style="list-style: none">
 * <li>		public OipCase(TestScript parent) {
 * 			<ul style="list-style: none">
 * <li>			</code><i>Use <b>one</b> of the following constructors based on if this TestCase is standalone</i><code>
 * <li>			super(parent);</code> <i>Standalone defaulted to <b>false</b></i><code>
 * <li>			super(parent, true);</code><i>Standalone explicitly set to <b>true</b></i><code>
 * <li>
 * <li>			</code><i>All requirements tested in the test case should be noted here with the </i>
 * <code>{@link org.eclipse.osee.ote.core.TestCase#addTracability(String) addTracability}</code><i> method.</i> <code>
 * 			</ul>
 * <li>		}
 * <li>
 * <li>		</code><i><b>Note:</b>It is very important that </i><code>doTestCase</code><i> always has the </i>
 * <code>throws InterrupedException</code><i> in the method declaration. The ability to abort a script relies on this
 * statement, and without it many calls would have to be wrapped with a try/catch block.</i><code>
 * <li>		public void doTestCase(ITestEnvironmentAccessor environment, ITestLogger logger) throws InterruptedException {
 * 			<ul style="list-style: none">
 * <li>			</code><i>Place all of the runtime code for the test case here.</i><code>
 * 			</ul>
 * <li>		}
 * 		</ul>
 * }
 * </code>
 * 
 * @see org.eclipse.osee.ote.core.TestScript
 * @author Ryan D. Brooks
 * @author Robert A. Fisher
 */
public abstract class TestCase implements ITestEnvironmentAccessor, Xmlizable, XmlizableStream {
   protected ITestLogger logger;
   private ITestEnvironmentAccessor environment;
   private final boolean standAlone;
   private final WeakReference<TestScript> testScript;
   private final TestDescriptionRecord testDescription;
   @JsonProperty
   public int number;
   protected List<RequirementRecord> traceability = new ArrayList<>();

   /**
    * TestCase Constructor.
    */
   public TestCase(TestScript testScript) {
      this(testScript, false);
   }

   /**
    * TestCase Constructor.
    */
   public TestCase(TestScript testScript, boolean standAlone) {
      this(testScript, standAlone, true);
   }

   /**
    * TestCase Constructor.
    */
   protected TestCase(TestScript testScript, boolean standAlone, boolean addToRunList) {
      super();
      this.testDescription = new TestDescriptionRecord(testScript.getTestEnvironment());
      this.standAlone = standAlone;
      if (addToRunList) {
         this.number = testScript.addTestCase(this);
      }
      this.testScript = new WeakReference<>(testScript);
      this.environment = testScript.getTestEnvironment();
      GCHelper.getGCHelper().addRefWatch(this);
   }
   
   protected void setEnv(ITestEnvironmentAccessor env){
      this.environment = env;
   }

   public TestCase(ITestEnvironmentAccessor accessor) {
      this.testDescription = new TestDescriptionRecord(accessor);

      // TODO we have two different traceability tags here.... we need to combine these or get rid
      // of them all together since define and the artifact framework specifies traceability
      // this.tracability = new ArrayList();
      this.traceability = new ArrayList<>();

      this.standAlone = false;
      ;
      this.number = 1;
      this.testScript = null;
      this.environment = accessor;

   }

   /**
    * Called by baseDoTestCase(). This is implemented by the tester's in each test case in the test script.
    * 
    * @param environment The Test environment.
    */
   public abstract void doTestCase(ITestEnvironmentAccessor environment, ITestLogger logger) throws InterruptedException;

   public Element getTastCaseNumberXml(Document doc) {
      return Jaxp.createElement(doc, "Number", String.valueOf(number));
   }

   public void writeTestCaseNumber(XMLStreamWriter writer) throws XMLStreamException {
      XMLStreamWriterUtil.writeElement(writer, "Number", String.valueOf(number));
   }

   public void writeTestCaseClassName(XMLStreamWriter writer) throws XMLStreamException {
      String name = getName();
      if (name == null || name.length() == 0) {
         name = "";
      }
      XMLStreamWriterUtil.writeElement(writer, "Name", name);
   }
   
   @JsonProperty
   public String getName() {
       return this.getClass().getSimpleName();
   }

   public void writeTracability(XMLStreamWriter writer) throws XMLStreamException {
      writer.writeStartElement("Tracability");
      for (RequirementRecord record : traceability) {
         record.toXml(writer);
      }
      writer.writeEndElement();
   }

   public Element getTestCaseClassName(Document doc) {
      String name = this.getClass().getName();
      if (name == null || name.length() == 0) {
         name = "";
      }
      return Jaxp.createElement(doc, "Name", name);
   }
   
   public String getTestCaseClassName() {
	   return this.getClass().getName();
   }

   public TestCase getTestCase() {
      return this;
   }

   /**
    * @return Returns the testCaseNumber.
    */
   public int getTestCaseNumber() {
      return number;
   }

   public ITestEnvironmentAccessor getTestEnvironment() {
      return environment;
   }

   public TestRecord getTestRecord() {
      return new TestCaseRecord(getTestEnvironment(), this);
   }

   /**
    * @return Returns the testScript.
    */
   @Override
   public TestScript getTestScript() {
      return testScript.get();
   }

   public Element getTracabilityXml(Document doc) {
      Element traceElement = doc.createElement("Tracability");
      for (RequirementRecord record : traceability) {
         traceElement.appendChild(record.toXml(doc));

      }
      return traceElement;
   }

   /**
    * @return Returns the standAlone.
    */
   public boolean isStandAlone() {
      return standAlone;
   }

   public void prompt() throws InterruptedException {
      getTestScript().prompt();
   }

   public void prompt(String message) throws InterruptedException {
      getTestScript().prompt(new TestPrompt(message, PromptResponseType.NONE));
   }

   public void promptPassFail(String message) throws InterruptedException {
      getTestScript().getLogger().methodCalled(getTestScript().getTestEnvironment(), new MethodFormatter().add(message));

      getTestScript().promptPassFail(message);

      getTestScript().getLogger().methodEnded(getTestScript().getTestEnvironment());
   }

   public void promptPause(String message) throws InterruptedException {
      getTestScript().promptPause(message);
   }

   public void promptStep(String message) throws InterruptedException {
      getTestScript().prompt(new TestPrompt(message, PromptResponseType.SCRIPT_STEP));
   }

   public String promptInput(String message) throws InterruptedException {
      return getTestScript().prompt(new TestPrompt(message, PromptResponseType.USER_INPUT));
   }

   /**
    * Logs the results of a test point.
    * 
    * @param passed boolean T/F
    * @param expected The expected information
    * @param actual The actual information
    */
   public void testpoint(boolean passed, String testPointName, String expected, String actual) {
      logger.testpoint(this.getTestEnvironment(), this.getTestScript(), this, passed, testPointName, expected, actual);
   }

   @Override
   public String toString() {
      String description = getTestScript().getClass().getName() + "Test Case " + number + ":";
      if (traceability != null) {
         for (RequirementRecord record : traceability) {
            description += "\n\t" + record;
         }
      }
      return description;
   }

   @Override
   public Element toXml(Document doc) {
      Element testCaseElement = doc.createElement("TestCase");
      testCaseElement.appendChild(getTastCaseNumberXml(doc));
      testCaseElement.appendChild(getTestCaseClassName(doc));
      testCaseElement.appendChild(getTracabilityXml(doc));
      return testCaseElement;
   }

   @Override
   public void toXml(XMLStreamWriter writer) throws XMLStreamException {
      writer.writeStartElement("TestCase");
      writeTestCaseNumber(writer);
      writeTestCaseClassName(writer);
      writeTracability(writer);
   }

   /**
    * Starts running the test case. Calls doTestCase(), which is implemented by the tester in each test case.
    * 
    * @param environment The Test Enviornment.
    */
   public void baseDoTestCase(ITestEnvironmentAccessor environment) throws InterruptedException {
      this.logger = environment.getLogger();

      logger.testCaseBegan(this); // This is required for valid outfile.
      //This creates the test case outfile logging.

      environment.getTestScript().setTestCase(this);
      OseeLog.logf(TestEnvironment.class, OteLevel.TEST_EVENT, "Starting Test Case %s.%s",
         this.getTestScript().getClass().getSimpleName(), getName());
      doTestCase(environment, environment.getLogger());
   }

   @Override
   public void abortTestScript() {
      environment.abortTestScript();
   }

   @Override
   public boolean addTask(EnvironmentTask task) {
      return environment.addTask(task);
   }

   @Override
   public void associateObject(Class<?> c, Object obj) {
      environment.associateObject(c, obj);
   }

   @Override
   public Object getAssociatedObject(Class<?> c) {
      return environment.getAssociatedObject(c);
   }

   @Override
   public Set<Class<?>> getAssociatedObjects() {
      return environment.getAssociatedObjects();
   }

   /*
    * public ITestEnvironmentCommandCallback getClientCallback() { return environment.getClientCallback(); }
    */

   // public EnvironmentType getEnvironmentType() {
   // return environment.getEnvironmentType();
   // }
   @Override
   public long getEnvTime() {
      return environment.getEnvTime();
   }

   @Override
   public IExecutionUnitManagement getExecutionUnitManagement() {
      return environment.getExecutionUnitManagement();
   }

   @Override
   public ITestLogger getLogger() {
      return environment.getLogger();
   }

   @Override
   public IScriptControl getScriptCtrl() {
      return environment.getScriptCtrl();
   }

   /*
    * public StatusBoard getStatusBoard() { return environment.getStatusBoard(); }
    */

   @Override
   public ITestStation getTestStation() {
      return environment.getTestStation();
   }

   @Override
   public ITimerControl getTimerCtrl() {
      return environment.getTimerCtrl();
   }

   @Override
   public void onScriptComplete() throws InterruptedException {
      environment.onScriptComplete();
   }

   @Override
   public void onScriptSetup() {
      environment.onScriptSetup();
   }

   @Override
   public ICancelTimer setTimerFor(ITimeout listener, int time) {
      return environment.setTimerFor(listener, time);
   }

   public void logTestPoint(boolean isPassed, String testPointName, String expected, String actual) {
      this.getLogger().testpoint(this.getTestEnvironment(), this.getTestScript(), this, isPassed, testPointName,
         expected, actual);
   }

   public void setPurpose(String purpose) {
      testDescription.setPurpose(purpose);
   }

   public void setPreCondition(String preCondition) {
      testDescription.setPreCondition(preCondition);
   }

   public void setPostCondition(String postCondition) {
      testDescription.setPostCondition(postCondition);
   }

   public List<RequirementRecord> getScriptReqRecordTraceability() {
      return traceability;
   }

   public void addTraceability(String description) {
      this.traceability.add(new RequirementRecord(this.getTestEnvironment(), description));
   }

   public void writeToConsole(String message) {
      TestPrompt p = new TestPrompt(message);

      try {
         testScript.get().prompt(p);
      } catch (Throwable e) {
         OseeLog.log(TestEnvironment.class, Level.INFO, e.getMessage(), e);
      }
   }

   public void setTestScript(TestScript script) {
      throw new IllegalStateException("Why are you calling this one?!?!?!?");
   }

   @Override
   public void abortTestScript(Throwable t) {
      testScript.get().abortDueToThrowable(t);
   }
   
   @JsonProperty
   public List<RequirementRecord> getTraceability() {
       if (traceability.isEmpty()) {
           return null;
       } else {
           return traceability;
       }
   }
   
}

Back to the top