summaryrefslogtreecommitdiff
path: root/M4-RCs/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/model/QpidStringPropertyTest.java
blob: 8aeb7c85501ad6a92145b55ee90a3acdae83595c (plain)
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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.management.domain.model;

import junit.framework.TestCase;

import org.apache.qpid.management.configuration.Configurator;
import org.apache.qpid.management.domain.model.type.Str16;

public class QpidStringPropertyTest extends TestCase
{
    private QpidProperty _property;
    private final String _5LettersString = "12345";
    
    @Override
    protected void setUp () throws Exception
    {
        Configurator configurator = new Configurator();
        configurator.configure();
        _property = new QpidProperty();
        _property.setName("name");
        _property.setAccessMode(AccessMode.RW);
        _property.setType(new Str16());
    }
    
    /**
     * Tests the validation of a qpid property when the type is a string and a max length constraint hasn't been set.
     * 
     * <br>precondition : property type is a string, max length hasn't been set.
     * <br>postcondition : no exception is thrown. That is : the validation succeeds.
     */    
    public void testValidationWithoutMaxLength() {
        try
        {
            _property.validate(_5LettersString);
        } catch (ValidationException notExpected)
        {
            fail("No max length has been set on property so validation must succeed.");
        }
    }
    
    /**
     * Tests the validation of a qpid property when the type is a string and a max length constraint has been set.
     * 
     * <br>precondition : property type is a string, max length has been set and property value is longer than max length.
     * <br>postcondition : an exception is thrown indicating the validation failure.
     */
    public void testValidationKO_withMaxLength() {
        int maxLength = 2;
        _property.setMaxLength(maxLength);
        
        try
        {
            _property.validate(_5LettersString);
            fail("No max length has been set on property so validation must proceed.");
        } catch (ValidationException expected)
        {
            assertEquals(ValidationException.MAX_LENGTH,expected.getConstraintName());
            assertEquals(maxLength,expected.getConstraintValue());
            assertEquals(_5LettersString.length(),expected.getFeatureValue());
            assertEquals(_property.getName(),expected.getFeatureName());
        }
    }    
    
    /**
     * Tests the validation of a qpid property when the type is a string and the property value is null.
     * 
     * <br>precondition : property type is a string and property value is null..
     * <br>postcondition : no exception is thrown. That is : the validation succeeds.
     */
    public void testValidationOK_withNullValue() {        
        try
        {
            _property.validate(null);
        } catch (ValidationException notExpected)
        {
            fail("No constraint has been violated so validate() shouldn't raise an exception.");
        }
        
        _property.setMaxLength(1);
        
        try
        {
            _property.validate(null);
        } catch (ValidationException notExpected)
        {
            fail("No constraint has been violated so validate() shouldn't raise an exception.");
        }
    }    
    
    /**
     * Tests the validation of a qpid property when the type is a string and a max length constraint has been set.
     * 
     * <br>precondition : property type is a string, max length has been set and property value is not violating that.
     * <br>postcondition : no exception is thrown. That is : the validation succeeds.
     */
    public void testValidationOK_withMaxLength() {
        int maxLength = (_5LettersString.length()+1);
        _property.setMaxLength(maxLength);
        
        try
        {
            _property.validate(_5LettersString);
        } catch (ValidationException notExpected)
        {
            fail("No constraint has been violated so validate() shouldn't raise an exception.");
        }
    }        
}