Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b54b168b0b78e53f1230179bf2d70c3605f0fa0 (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
/*******************************************************************************
 * 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.framework.core.model.fields;

import java.util.ArrayList;
import java.util.Collection;
import org.junit.Assert;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.OseeField;
import org.eclipse.osee.framework.jdk.core.util.Compare;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;

/**
 * @author Roberto E. Escobar
 */
public abstract class BaseOseeFieldTest {
   private final OseeFieldTestData<?> test;

   protected BaseOseeFieldTest(OseeFieldTestData<?> test) {
      this.test = test;
   }

   @Test
   public void testInitialValues() throws OseeCoreException {
      String message = "InitTest";
      checkObjects(message, test.getInitExpectedValue(), test.getField().get());
      Assert.assertEquals(message, test.isInitExpectedDirty(), test.getField().isDirty());
   }

   @SuppressWarnings("unchecked")
   @Test
   public void testSettterGetter() throws OseeCoreException {
      int index = 0;
      for (FieldGetSetTestData testData : test.getTestDatas()) {
         String message = String.format("Test Data [%s]", ++index);
         if (testData.isClearBeforeRun()) {
            test.getField().clearDirty();
            Assert.assertFalse(message, test.getField().isDirty());
         }

         if (testData.throwsError()) {
            try {
               test.doSetValue(testData);
               Assert.assertNull(message, "This line should never be executed");
            } catch (Exception ex) {
               Class<?> clazz = testData.getError();
               Assert.assertTrue(message, ex.getClass().isAssignableFrom(clazz));
            }
         } else {
            test.doSetValue(testData);
         }

         checkObjects(message, testData.getExpectedValue(), test.getField().get());
         Assert.assertEquals(message, testData.isExpectedDirty(), test.getField().isDirty());
      }
   }

   private void checkObjects(String message, Object expected, Object actual) {
      Assert.assertFalse(String.format("%s expected[%s] actual[%s]", message, expected, actual),
         Compare.isDifferent(expected, actual));
   }

   @SuppressWarnings("unchecked")
   @Parameters
   public static Collection<Object[]> data() throws OseeCoreException {
      Collection<Object[]> data = new ArrayList<Object[]>();

      data.add(new Object[] {//
      new OseeFieldTestData<Object>(new OseeField<Object>(), //
         null, false, //
         new FieldGetSetTestData<Object>(true, "one test", "one test", true), //
         new FieldGetSetTestData<Object>(false, "two test", "two test", true), //
         new FieldGetSetTestData<Object>(true, "three test", "three test", true)//
      )});//

      data.add(new Object[] { //
      new OseeFieldTestData<Integer>(new OseeField<Integer>(), //
         null, false, //
         new FieldGetSetTestData<Integer>(true, 1, 1, true), //
         new FieldGetSetTestData<Integer>(false, 5, 5, true), //
         new FieldGetSetTestData<Integer>(true, Integer.MIN_VALUE, Integer.MIN_VALUE, true)//
      )});

      data.add(new Object[] { //
      new OseeFieldTestData<Boolean>(new OseeField<Boolean>(), //
         null, false, //
         new FieldGetSetTestData<Boolean>(true, true, true, true), //
         new FieldGetSetTestData<Boolean>(false, true, true, true), //
         new FieldGetSetTestData<Boolean>(true, false, false, true),//
         new FieldGetSetTestData<Boolean>(false, false, false, true)//
      )});

      data.add(new Object[] {new OseeFieldTestData<Object>(new OseeField<Object>("string1"), "string1", true)});
      data.add(new Object[] {new OseeFieldTestData<Integer>(new OseeField<Integer>(Integer.MIN_VALUE),
         Integer.MIN_VALUE, true)});
      data.add(new Object[] {new OseeFieldTestData<Boolean>(new OseeField<Boolean>(true), true, true)});
      data.add(new Object[] {new OseeFieldTestData<Boolean>(new OseeField<Boolean>(false), false, true)});

      data.add(new Object[] {//
      new OseeFieldTestData<String>(new OseeField<String>("string2"), "string2", true, //
         new FieldGetSetTestData<String>(false, "another", "another", true), //
         new FieldGetSetTestData<String>(true, "something", "something", true)//
      )});

      //      IOseeTypeFactory factory = new OseeTypeFactory();
      //      data.add(createAssociatedArtifactTest(factory));
      return data;
   }

}

Back to the top