Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d4d9a4c1c79c98cb20247e78e57cc4f4448632c (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
/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.metatype.tests;

import static org.junit.Assert.assertTrue;

import org.eclipse.equinox.metatype.*;
import org.junit.*;
import org.osgi.framework.Bundle;
import org.osgi.service.metatype.ObjectClassDefinition;

public class GetMinMaxTest extends AbstractTest {
	private Bundle bundle;
	private EquinoxObjectClassDefinition ocd;

	@Before
	@Override
	public void setUp() throws Exception {
		super.setUp();
		bundle = bundleInstaller.installBundle("getMinMax.tb1"); //$NON-NLS-1$
		bundle.start();
		EquinoxMetaTypeInformation mti = metatype.getMetaTypeInformation(bundle);
		ocd = mti.getObjectClassDefinition("org.eclipse.equinox.metatype.tests.getMinMax.tb1", null); //$NON-NLS-1$
	}

	@Test
	public void testGetMax() throws Exception {
		assertMaxValue("getMax", "0"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	@Test
	public void testGetMaxAsNotANumber() {
		assertMaxValue("getMaxAsNotANumber", "1.0.0"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	@Test
	public void testGetMaxNull() {
		assertMaxValue("getMaxNull", null); //$NON-NLS-1$
	}

	@Test
	public void testGetMin() {
		assertMinValue("getMin", "5"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	@Test
	public void testGetMinAsNotANumber() {
		assertMinValue("getMinAsNotANumber", "foo"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	@Test
	public void testGetMinNull() {
		assertMinValue("getMinNull", null); //$NON-NLS-1$
	}

	@Test
	public void testMinMaxStringValidationUsingInteger() {
		String name = "minMaxValidUsingInt"; //$NON-NLS-1$
		assertValid(name, "12345678"); //$NON-NLS-1$
		assertValid(name, "1234567812345678"); //$NON-NLS-1$
		assertInvalid(name, "1234567"); //$NON-NLS-1$
		assertInvalid(name, "12345678123456789"); //$NON-NLS-1$
	}

	@Test
	public void testMinMaxStringValidationUsingString() {
		String name = "minMaxValidUsingString"; //$NON-NLS-1$
		assertValid(name, "1"); //$NON-NLS-1$
		assertValid(name, "1.2"); //$NON-NLS-1$
		assertInvalid(name, "0"); //$NON-NLS-1$
		assertInvalid(name, "1.3"); //$NON-NLS-1$
	}

	private void assertMaxValue(String name, String value) {
		assertMinMaxValue(true, name, value);
	}

	private void assertMinValue(String name, String value) {
		assertMinMaxValue(false, name, value);
	}

	private void assertMinMaxValue(boolean max, String name, String value) {
		EquinoxAttributeDefinition ad = getAttributeDefinition(name);
		Assert.assertEquals("Wrong " + (max ? "max" : "min") + " value", value, max ? ad.getMax() : ad.getMin()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}

	private void assertValid(String name, String value) {
		assertValidation(name, value, true);

	}

	private void assertInvalid(String name, String value) {
		assertValidation(name, value, false);
	}

	private void assertValidation(String name, String value, boolean valid) {
		EquinoxAttributeDefinition ad = getAttributeDefinition(name);
		if (valid) {
			Assert.assertEquals("Should be valid", "", ad.validate(value)); //$NON-NLS-1$ //$NON-NLS-2$
		} else {
			assertTrue("Should be invalid", ad.validate(value).length() > 0); //$NON-NLS-1$
		}
	}

	private EquinoxAttributeDefinition getAttributeDefinition(String name) {
		EquinoxAttributeDefinition[] ads = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
		return (EquinoxAttributeDefinition) findAttributeDefinitionById(name, ads);
	}
}

Back to the top