1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.tests;
14 import junit.framework.TestCase;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.annotations.Length;
18 import org.simantics.databoard.annotations.Optional;
19 import org.simantics.databoard.annotations.Pattern;
20 import org.simantics.databoard.annotations.Range;
21 import org.simantics.databoard.binding.Binding;
22 import org.simantics.databoard.binding.error.BindingConstructionException;
23 import org.simantics.databoard.binding.error.BindingException;
25 public class TestValidator extends TestCase {
27 public enum Test { Key1, Key2 }
29 public static class Luokka {
30 public @Range("[0..100]") Integer percent = 0;
31 public @Optional Integer optionalValue;
32 public Boolean booleanValue = true;
33 public @Length("7") @Range("[1..39]") int[] lottoNumbers = new int[] {1,2,3,4,5,6,7};
34 public @Length("10") @Pattern("0x\\d{8}") String hexNumber = "0x11223344";
37 public static Binding b = Bindings.getBindingUnchecked(Luokka.class);
40 public void setUp() throws Exception {
44 public void testNumberType() throws Exception {
47 b.assertInstaceIsValid( l );
50 b.assertInstaceIsValid( l );
54 b.assertInstaceIsValid( -1 );
56 } catch ( BindingException e ) {}
60 b.assertInstaceIsValid( 101 );
62 } catch ( BindingException e ) {}
65 b.assertInstaceIsValid( new Integer(5) );
67 } catch ( BindingException e ) {}
71 public void testOptionalType() throws BindingException {
73 b.assertInstaceIsValid( l );
77 b.assertInstaceIsValid( l );
79 } catch ( BindingException e ) {}
82 public void testBooleanType() throws BindingException {
83 b.assertInstaceIsValid( l );
86 b.assertInstaceIsValid( new Integer(5) );
88 } catch ( BindingException e ) {}
92 public void testStringType() throws BindingException {
93 b.assertInstaceIsValid( l );
96 // not according to pattern
97 l.hexNumber = "Hello World";
98 b.assertInstaceIsValid( l );
100 } catch ( BindingException e ) {}
104 l.hexNumber = "0x100";
105 b.assertInstaceIsValid( l );
107 } catch ( BindingException e ) {}
111 public void testRecordType() throws BindingException {
112 b.assertInstaceIsValid( l );
113 // Already thoroughly tested by all other tests
116 public void testUnionType() throws BindingException, BindingConstructionException {
117 Binding eb = Bindings.getBinding( Test.class );
118 eb.assertInstaceIsValid(Test.Key1);
119 eb.assertInstaceIsValid(Test.Key2);
121 b.assertInstaceIsValid( new Integer(5) );
123 } catch ( BindingException e ) {}
127 public void testArrayType() throws BindingException {
128 b.assertInstaceIsValid( l );
132 l.lottoNumbers = new int[] {1,2,3,4,5,6,7, 10, 10, 10, 10};
133 b.assertInstaceIsValid( l );
135 } catch ( BindingException e ) {}
138 // Not enough numbers
139 l.lottoNumbers = new int[] {1, 2, 3};
140 b.assertInstaceIsValid( l );
142 } catch ( BindingException e ) {}
146 l.lottoNumbers = new int[] {100,-2,3,4,5,6,7};
147 b.assertInstaceIsValid( l );
149 } catch ( BindingException e ) {}