1 /***************************************************************************
2  *                   (C) Copyright 2003-2016 - Stendhal                    *
3  ***************************************************************************
4  ***************************************************************************
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  ***************************************************************************/
12 package games.stendhal.server.core.config.factory;
13 
14 import static org.hamcrest.CoreMatchers.is;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertThat;
17 import static org.junit.Assert.assertTrue;
18 
19 import java.util.HashMap;
20 import java.util.Map;
21 
22 import org.junit.Before;
23 import org.junit.Test;
24 
25 public class ConfigurableFactoryContextTest {
26 
27 	private Map<String, String> attributes;
28 
29 	@Before
setUp()30 	public void setUp() throws Exception {
31 		attributes = new HashMap<String, String>();
32 		attributes.put("positive", "true");
33 		attributes.put("string", "stringvalue");
34 		attributes.put("negative", "false");
35 		attributes.put("fiveInt", "5");
36 	}
37 
38 	/**
39 	 * Tests for configurableFactoryContext.
40 	 */
41 	@Test
testConfigurableFactoryContext()42 	public void testConfigurableFactoryContext() {
43 		new ConfigurableFactoryContext(attributes);
44 		assertTrue("noexecption", true);
45 	}
46 
47 	/**
48 	 * Tests for getBoolean.
49 	 */
50 	@Test
testGetBoolean()51 	public void testGetBoolean() {
52 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
53 				attributes);
54 		assertTrue(con.getBoolean("nonExistingkey", true));
55 		assertFalse(con.getBoolean("nonExistingkey", false));
56 		assertTrue(con.getBoolean("positive", false));
57 		assertFalse(con.getBoolean("negative", true));
58 
59 	}
60 
61 	/**
62 	 * Tests for getNonExistingRequiredBoolean.
63 	 */
64 	@Test(expected = IllegalArgumentException.class)
testGetNonExistingRequiredBoolean()65 	public void testGetNonExistingRequiredBoolean() {
66 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
67 				attributes);
68 		assertTrue(con.getRequiredBoolean("key"));
69 	}
70 
71 	/**
72 	 * Tests for getNonbooleanRequiredBoolean.
73 	 */
74 	@Test(expected = IllegalArgumentException.class)
testGetNonbooleanRequiredBoolean()75 	public void testGetNonbooleanRequiredBoolean() {
76 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
77 				attributes);
78 		assertTrue(con.getRequiredBoolean("string"));
79 	}
80 
81 	/**
82 	 * Tests for getRequiredBoolean.
83 	 */
84 	@Test
testGetRequiredBoolean()85 	public void testGetRequiredBoolean() {
86 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
87 				attributes);
88 		assertTrue(con.getRequiredBoolean("positive"));
89 		assertFalse(con.getRequiredBoolean("negative"));
90 	}
91 
92 	/**
93 	 * Tests for getInt.
94 	 */
95 	@Test
testGetInt()96 	public void testGetInt() {
97 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
98 				attributes);
99 		assertThat(con.getInt("nonExistingkey", 1), is(1));
100 		assertThat(con.getInt("fiveInt", 1), is(5));
101 
102 	}
103 
104 	/**
105 	 * Tests for getNonExitantRequiredInt.
106 	 */
107 	@Test(expected = IllegalArgumentException.class)
testGetNonExitantRequiredInt()108 	public void testGetNonExitantRequiredInt() {
109 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
110 				attributes);
111 		con.getRequiredInt("nonExistingkey");
112 	}
113 
114 	/**
115 	 * Tests for getRequiredInt.
116 	 */
testGetRequiredInt()117 	public void testGetRequiredInt() {
118 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
119 				attributes);
120 		assertThat(con.getRequiredInt("fiveInt"), is(5));
121 	}
122 
123 	/**
124 	 * Tests for getString.
125 	 */
126 	@Test
testGetString()127 	public void testGetString() {
128 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
129 				attributes);
130 		assertThat(con.getString("nonexistantstring", "default"), is("default"));
131 		assertThat(con.getString("string", "default"), is("stringvalue"));
132 	}
133 
134 	/**
135 	 * Tests for getRequiredString.
136 	 */
137 	@Test(expected = IllegalArgumentException.class)
testGetRequiredString()138 	public void testGetRequiredString() {
139 		ConfigurableFactoryContext con = new ConfigurableFactoryContext(
140 				attributes);
141 		con.getRequiredString("nonexistantstring");
142 	}
143 
144 }
145