Skip to content

Package: PreSetValidationService_Test

PreSetValidationService_Test

nameinstructionbranchcomplexitylinemethod
PreSetValidationService_Test()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
custom()
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
enums()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
invalidCustom()
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
invalidCustomWithTwoValidators()
M: 0 C: 43
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
invalidEnum()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
invalidPattern()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
looseMinInclusive()
M: 0 C: 40
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
looseMinLength()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
looseMinLengthFallsBackToStrict()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
loosePhoneNumberPattern()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
maxLength()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
multiEnum()
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
multiEnumAdditionalConstrains()
M: 0 C: 42
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
pattern()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
strictMinInclusive()
M: 0 C: 43
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
strictMinLength()
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
strictPhoneNumberPattern()
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
validEnum()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
violateMaxLength()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2017 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Edgar Mueller - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.validation.test;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertTrue;
18:
19: import java.util.Arrays;
20: import java.util.Map;
21: import java.util.concurrent.atomic.AtomicBoolean;
22:
23: import org.eclipse.core.runtime.IStatus;
24: import org.eclipse.core.runtime.Status;
25: import org.eclipse.emf.common.util.BasicDiagnostic;
26: import org.eclipse.emf.common.util.Diagnostic;
27: import org.eclipse.emf.ecore.EClassifier;
28: import org.eclipse.emf.ecore.EStructuralFeature;
29: import org.eclipse.emf.ecp.view.validation.test.model.Color;
30: import org.eclipse.emf.ecp.view.validation.test.model.Gender;
31: import org.eclipse.emf.ecp.view.validation.test.model.TestPackage;
32: import org.eclipse.emfforms.internal.common.prevalidation.PreSetValidationServiceImpl;
33: import org.eclipse.emfforms.spi.common.validation.IFeatureConstraint;
34: import org.junit.Test;
35:
36: public class PreSetValidationService_Test {
37:
38:         private final PreSetValidationServiceImpl service = new PreSetValidationServiceImpl();
39:
40:         @Test
41:         public void violateMaxLength() {
42:                 final Diagnostic result = service.validate(
43:                         TestPackage.eINSTANCE.getPerson_FirstName(),
44:                         "more than 10 chars",
45:                         null);
46:                 assertEquals(result.getSeverity(), Diagnostic.ERROR);
47:         }
48:
49:         @Test
50:         public void maxLength() {
51:                 final Diagnostic result = service.validate(
52:                         TestPackage.eINSTANCE.getPerson_FirstName(),
53:                         "valid",
54:                         null);
55:                 assertEquals(result.getSeverity(), Diagnostic.OK);
56:         }
57:
58:         @Test
59:         public void enums() {
60:                 final Diagnostic result = service.validate(
61:                         TestPackage.eINSTANCE.getPerson_Gender(),
62:                         Gender.MALE,
63:                         null);
64:                 assertEquals(result.getSeverity(), Diagnostic.OK);
65:         }
66:
67:         @Test
68:         public void pattern() {
69:                 final Diagnostic result = service.validate(
70:                         TestPackage.eINSTANCE.getPerson_LastName(), "VALID", null);
71:                 assertEquals(result.getSeverity(), Diagnostic.OK);
72:         }
73:
74:         @Test
75:         public void invalidPattern() {
76:                 final Diagnostic result = service.validate(
77:                         TestPackage.eINSTANCE.getPerson_LastName(), "invalid", null);
78:                 assertEquals(result.getSeverity(), Diagnostic.ERROR);
79:         }
80:
81:         @Test
82:         public void validEnum() {
83:                 final Diagnostic result = service.validate(TestPackage.eINSTANCE.getPerson_Gender(), "Male", null);
84:                 assertEquals(result.getSeverity(), Diagnostic.OK);
85:         }
86:
87:         @Test
88:         public void invalidEnum() {
89:                 final Diagnostic result = service.validate(TestPackage.eINSTANCE.getPerson_Gender(), "Mal", null);
90:                 assertEquals(result.getSeverity(), Diagnostic.ERROR);
91:         }
92:
93:         @Test
94:         public void custom() {
95:                 final PreSetValidationServiceImpl s = new PreSetValidationServiceImpl();
96:                 s.addConstraintValidator(TestPackage.eINSTANCE.getCustomDataType(), new IFeatureConstraint() {
97:                         @Override
98:                         public Diagnostic validate(EStructuralFeature eStructuralFeature, Object value,
99:                                 Map<Object, Object> context) {
100:                                 final EClassifier eType = eStructuralFeature.getEType();
101:
102:                                 if (!TestPackage.eINSTANCE.getCustomDataType().isInstance(eType)
103:                                         || value.equals("FOO")) {
104:                                         return new BasicDiagnostic();
105:                                 }
106:
107:                                 return BasicDiagnostic.toDiagnostic(
108:                                         new Status(IStatus.ERROR, "", IStatus.ERROR, "Value is not FOO", null));
109:                         }
110:                 });
111:
112:                 final Diagnostic result = s.validate(
113:                         TestPackage.eINSTANCE.getPerson_Custom(), "FOO", null);
114:                 assertEquals(result.getSeverity(), Diagnostic.OK);
115:         }
116:
117:         @Test
118:         public void invalidCustom() {
119:                 final PreSetValidationServiceImpl s = new PreSetValidationServiceImpl();
120:                 s.addConstraintValidator(TestPackage.eINSTANCE.getCustomDataType(), new IFeatureConstraint() {
121:                         @Override
122:                         public Diagnostic validate(EStructuralFeature eStructuralFeature, Object value,
123:                                 Map<Object, Object> context) {
124:
125:                                 if (value.equals("FOO")) {
126:                                         return new BasicDiagnostic();
127:                                 }
128:
129:                                 return BasicDiagnostic.toDiagnostic(
130:                                         new Status(IStatus.ERROR, "test", IStatus.ERROR, "Value is not FOO", null));
131:                         }
132:                 });
133:
134:                 final Diagnostic result = s.validate(
135:                         TestPackage.eINSTANCE.getPerson_Custom(), "BAR", null);
136:                 assertEquals(result.getSeverity(), Diagnostic.ERROR);
137:         }
138:
139:         @Test // regression test for bug #527891
140:         public void invalidCustomWithTwoValidators() {
141:                 final PreSetValidationServiceImpl s = new PreSetValidationServiceImpl();
142:
143:                 final IFeatureConstraint constraint1 = new IFeatureConstraint() {
144:                         @Override
145:                         public Diagnostic validate(EStructuralFeature eStructuralFeature, Object value,
146:                                 Map<Object, Object> context) {
147:
148:                                 if (value.equals("FOO")) {
149:                                         return new BasicDiagnostic();
150:                                 }
151:
152:                                 return BasicDiagnostic.toDiagnostic(
153:                                         new Status(IStatus.ERROR, "test", IStatus.ERROR, "Value is not FOO", null));
154:                         }
155:                 };
156:
157:                 final IFeatureConstraint constraint2 = new IFeatureConstraint() {
158:                         @Override
159:                         public Diagnostic validate(EStructuralFeature eStructuralFeature, Object value,
160:                                 Map<Object, Object> context) {
161:
162:                                 if (value.equals("FOO")) {
163:                                         return new BasicDiagnostic();
164:                                 }
165:
166:                                 return BasicDiagnostic.toDiagnostic(
167:                                         new Status(IStatus.ERROR, "test", IStatus.ERROR, "Value is still not FOO", null));
168:                         }
169:                 };
170:
171:                 s.addConstraintValidator(TestPackage.eINSTANCE.getCustomDataType(), constraint1);
172:                 s.addConstraintValidator(TestPackage.eINSTANCE.getCustomDataType(), constraint2);
173:
174:                 final Diagnostic result = s.validate(
175:                         TestPackage.eINSTANCE.getPerson_Custom(), "BAR", null);
176:
177:                 assertEquals(result.getSeverity(), Diagnostic.ERROR);
178:                 assertEquals(2, result.getChildren().size());
179:         }
180:
181:         @Test
182:         public void loosePhoneNumberPattern() {
183:                 final Diagnostic result = service.validateLoose(TestPackage.eINSTANCE.getLibrary_PhoneNumber(), "+");
184:                 assertEquals(result.getSeverity(), Diagnostic.OK);
185:         }
186:
187:         @Test
188:         public void strictPhoneNumberPattern() {
189:                 final Diagnostic invalid = service.validate(TestPackage.eINSTANCE.getLibrary_PhoneNumber(), "+", null);
190:                 final Diagnostic valid = service.validate(TestPackage.eINSTANCE.getLibrary_PhoneNumber(), "+123", null);
191:                 assertEquals(invalid.getSeverity(), Diagnostic.ERROR);
192:                 assertEquals(valid.getSeverity(), Diagnostic.OK);
193:         }
194:
195:         @Test
196:         public void looseMinLength() {
197:                 final Diagnostic result = service.validateLoose(TestPackage.eINSTANCE.getWriter_Initials(), "");
198:                 assertEquals(result.getSeverity(), Diagnostic.OK);
199:         }
200:
201:         @Test
202:         public void strictMinLength() {
203:                 // min length of three
204:                 final Diagnostic invalid = service.validate(TestPackage.eINSTANCE.getWriter_Initials(), "", null);
205:                 final Diagnostic valid = service.validate(TestPackage.eINSTANCE.getWriter_Initials(), "foo", null);
206:                 assertEquals(invalid.getSeverity(), Diagnostic.ERROR);
207:                 assertEquals(valid.getSeverity(), Diagnostic.OK);
208:         }
209:
210:         @Test
211:         public void looseMinLengthFallsBackToStrict() {
212:                 // title has no looseMinLength set
213:                 final Diagnostic invalid = service.validateLoose(TestPackage.eINSTANCE.getWriter_Title(), "");
214:                 assertEquals(invalid.getSeverity(), Diagnostic.ERROR);
215:         }
216:
217:         @Test
218:         public void strictMinInclusive() {
219:                 // min length of three
220:                 final Diagnostic invalid = service.validate(TestPackage.eINSTANCE.getPerson_Age(), Integer.valueOf(-1), null);
221:                 final Diagnostic minValid = service.validate(TestPackage.eINSTANCE.getPerson_Age(), Integer.valueOf(0), null);
222:                 final Diagnostic maxValid = service.validate(TestPackage.eINSTANCE.getPerson_Age(), Integer.valueOf(100), null);
223:                 assertEquals(invalid.getSeverity(), Diagnostic.ERROR);
224:                 assertEquals(minValid.getSeverity(), Diagnostic.OK);
225:                 assertEquals(maxValid.getSeverity(), Diagnostic.OK);
226:         }
227:
228:         @Test
229:         public void looseMinInclusive() {
230:                 // min length of three
231:                 final Diagnostic invalid = service.validateLoose(TestPackage.eINSTANCE.getPerson_Age(), Integer.valueOf(-1));
232:                 final Diagnostic minValid = service.validateLoose(TestPackage.eINSTANCE.getPerson_Age(), Integer.valueOf(0));
233:                 final Diagnostic maxValid = service.validateLoose(TestPackage.eINSTANCE.getPerson_Age(), Integer.valueOf(100));
234:                 assertEquals(invalid.getSeverity(), Diagnostic.ERROR);
235:                 assertEquals(minValid.getSeverity(), Diagnostic.OK);
236:                 assertEquals(maxValid.getSeverity(), Diagnostic.OK);
237:         }
238:
239:         @Test
240:         public void multiEnum() {
241:                 final Diagnostic valid = service.validate(TestPackage.eINSTANCE.getComputer_Colors(),
242:                         Arrays.asList(Color.GREEN, Color.BLUE), null);
243:                 assertEquals(valid.getSeverity(), Diagnostic.OK);
244:         }
245:
246:         /**
247:          * Bug 529514.
248:          */
249:         @Test
250:         public void multiEnumAdditionalConstrains() {
251:                 /* setup */
252:                 final AtomicBoolean contraintCalled = new AtomicBoolean(false);
253:                 service.addConstraintValidator(TestPackage.eINSTANCE.getColor(), new IFeatureConstraint() {
254:
255:                         @Override
256:                         public Diagnostic validate(
257:                                 EStructuralFeature eStructuralFeature,
258:                                 Object value,
259:                                 Map<Object, Object> context) {
260:                                 contraintCalled.set(true);
261:                                 return new BasicDiagnostic();
262:                         }
263:                 });
264:
265:                 /* act */
266:                 final Diagnostic valid = service.validate(TestPackage.eINSTANCE.getComputer_Colors(),
267:                         Arrays.asList(Color.GREEN, Color.BLUE), null);
268:
269:                 /* assert */
270:                 assertEquals(valid.getSeverity(), Diagnostic.OK);
271:                 assertTrue(contraintCalled.get());
272:
273:         }
274: }