View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.tutorial.property.constraints;
17  
18  import static org.junit.Assert.fail;
19  
20  import javax.validation.groups.Default;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import de.smartics.projectdoc.annotations.DocCategory;
26  import de.smartics.projectdoc.annotations.Document;
27  import de.smartics.projectdoc.annotations.topic.DocSection;
28  import de.smartics.properties.api.config.domain.ConfigurationValidationException;
29  import de.smartics.properties.impl.config.classpath.ClasspathConfigurationProperties;
30  import de.smartics.properties.impl.config.classpath.ClasspathConfigurationPropertiesFactory;
31  import de.smartics.testdoc.categories.type.Integration;
32  import de.smartics.validation.constraints.groups.Startup;
33  
34  /**
35   * This tutorial introduces how to add constraints to the property declaration.
36   * <p>
37   * This feature takes the burden of validation from the developers shoulders. It
38   * is based on <a href="http://jcp.org/en/jsr/detail?id=303">Beans Validation
39   * (JSR-303)</a>, so any constraint defined with this technology can be used for
40   * validating properties.
41   * </p>
42   */
43  @Document(title = "Property Constraints With Groups", sortKey = "basics0060")
44  @DocCategory({ "basics" })
45  // @DocTopic(path="basics", step="60")
46  public class PropertyConstraintsWithGroupsTutorial
47  {
48    private ClasspathConfigurationProperties config; // NOPMD
49  
50    private static final Class<GroupConstraintsProperties> TYPE =
51        GroupConstraintsProperties.class;
52  
53    private GroupConstraintsProperties properties;
54  
55    @Before
56    public void setUp()
57    {
58      config = createConfiguration();
59      properties = config.getProperties(TYPE);
60    }
61  
62    private static ClasspathConfigurationProperties createConfiguration()
63    {
64      final ClasspathConfigurationPropertiesFactory factory =
65          new ClasspathConfigurationPropertiesFactory();
66      final ClasspathConfigurationProperties config = factory.create();
67      config.addClassPathProperties(TYPE);
68      return config;
69    }
70  
71    /**
72     * <p>
73     * Let's have a look at some examples that use groups to activate and
74     * deactivate constraints for a particular validation.
75     * </p>
76     * <p>
77     * In the following example the value <code>11</code> is correct in the
78     * default group, but not in the startup group.
79     * </p>
80     */
81    @Test(expected = ConfigurationValidationException.class)
82    @DocSection
83    public void invalidInStartupGroup()
84    {
85      config.setProperty(properties.startupGroupPropertyKey(), "11");
86  
87      try
88      {
89        config.validate();
90      }
91      catch (final ConfigurationValidationException e)
92      {
93        fail("Validation exception not expected since Startup group not activated.");
94      }
95  
96      config.validate(Startup.class);
97    }
98  
99    /**
100    * <p>
101    * Having no group or explicitly set the constraint in a default group has the
102    * same effect.
103    * </p>
104    */
105   @Test(expected = ConfigurationValidationException.class)
106   @DocSection
107   public void declaredDefaultGroup()
108   {
109     config.setProperty(properties.defaultGroupPropertyKey(), "11");
110     config.validate();
111   }
112 
113   /**
114    * <p>
115    * And it is also the same if you call the validation using the default group.
116    * </p>
117    */
118   @Test(expected = ConfigurationValidationException.class)
119   @DocSection
120   public void declaredDefaultGroupExplictCallWithDefaultGroup()
121   {
122     config.setProperty(properties.defaultGroupPropertyKey(), "2");
123     config.validate(Default.class);
124   }
125 
126   /**
127    * <p>
128    * If there are no constraints in the given group, every value is valid.
129    * </p>
130    */
131   @Test
132   @DocSection
133   public void calledWithUnknownGroup()
134   {
135     config.setProperty(properties.startupGroupPropertyKey(), "11");
136     config.validate(Integration.class);
137   }
138 }