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.config.key; 17 18 import static org.hamcrest.MatcherAssert.assertThat; 19 import static org.hamcrest.Matchers.is; 20 21 import org.junit.Before; 22 import org.junit.Test; 23 24 import de.smartics.projectdoc.annotations.DocCategory; 25 import de.smartics.projectdoc.annotations.Document; 26 import de.smartics.projectdoc.annotations.topic.DocChapter; 27 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory; 28 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactoryFactory; 29 import de.smartics.properties.api.config.domain.ConfigurationProperties; 30 import de.smartics.sandbox.mail.AnnouncementProperties; 31 import de.smartics.sandbox.mail.MailProperties; 32 33 /** 34 * This tutorial shows how to use plain properties files and access it with a 35 * generic configuration key. This is a solution to choose if you just want to 36 * use the annotation stuff but do not bother with different configuration keys. 37 */ 38 @Document(title = "No Configuration Key Required", sortKey = "basics1020") 39 @DocCategory({ "basics" }) 40 // @DocPrerequisites(ConfigurationKeyTutorial.class) 41 // @DocTopic(path="basics", step="1020") 42 public class NoConfigurationKeyTutorial 43 { 44 private ConfigurationPropertiesFactory factory; 45 46 @Before 47 public void setUp() 48 { 49 factory = ConfigurationPropertiesFactoryFactory.createDefaultFactory(); 50 } 51 52 /** 53 * <p> 54 * In this example we assume that there are multiple modules in a single 55 * application, but there is no need to handle different configurations. 56 * Accessing properties provided to the application via annotations is simply 57 * everything that is required. We show that you do not have to bother with 58 * configuration keys since everything that is required to appease the system 59 * is done automatically behind the scenes. 60 * </p> 61 * <p> 62 * The following modules are declared as dependencies to the application: 63 * </p> 64 * 65 * <pre> 66 * <dependency> 67 * <groupId>de.smartics.sandbox</groupId> 68 * <artifactId>test-module-mail</artifactId> 69 * </dependency> 70 * <dependency> 71 * <groupId>de.smartics.sandbox</groupId> 72 * <artifactId>test-module-announce</artifactId> 73 * </dependency> 74 * </pre> 75 * <p> 76 * The definition for these properties is provided by a dependency like this: 77 * </p> 78 * 79 * <pre> 80 * <dependency> 81 * <groupId>de.smartics.sandbox</groupId> 82 * <artifactId>test-application-config-nokey</artifactId> 83 * </dependency> 84 * </pre> 85 * 86 * {@insertCode} 87 * 88 * <p> 89 * At {@$1} we create the default configuration. This configuration holds all 90 * property definitions that are not associated to a particular key. 91 * </p> 92 * <p> 93 * At {@$2} and {@$3} we only show that accessing the properties really show 94 * the values from the definition files. 95 * </p> 96 */ 97 @DocChapter 98 @Test 99 public void accessConfigurationByHardcodedKey() 100 { 101 final ConfigurationProperties config = factory.createDefault(); // {1} 102 103 // {2} 104 final MailProperties mail = config.getProperties(MailProperties.class); 105 final int mailsPerPage = mail.mailsPerPage(); 106 assertThat(mailsPerPage, is(42)); 107 108 // {3} 109 final AnnouncementProperties announcement = 110 config.getProperties(AnnouncementProperties.class); 111 final int lookup = announcement.lookupIntervalMs(); 112 assertThat(lookup, is(23)); 113 } 114 }