1 /*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002, 2014 Oracle and/or its affiliates. All rights reserved. 5 * 6 */ 7 8 package com.sleepycat.je; 9 10 import static org.junit.Assert.assertEquals; 11 import static org.junit.Assert.assertFalse; 12 import static org.junit.Assert.assertNotSame; 13 import static org.junit.Assert.assertTrue; 14 import static org.junit.Assert.fail; 15 16 import java.io.File; 17 import java.util.ArrayList; 18 import java.util.Properties; 19 20 import org.junit.Test; 21 22 import com.sleepycat.je.config.EnvironmentParams; 23 import com.sleepycat.je.dbi.DbConfigManager; 24 import com.sleepycat.je.dbi.EnvironmentImpl; 25 import com.sleepycat.je.util.TestUtils; 26 import com.sleepycat.util.test.SharedTestUtils; 27 import com.sleepycat.util.test.TestBase; 28 29 public class EnvironmentConfigTest extends TestBase { 30 private final File envHome; 31 EnvironmentConfigTest()32 public EnvironmentConfigTest() { 33 envHome = SharedTestUtils.getTestDir(); 34 } 35 36 /** 37 * Try out the validation in EnvironmentConfig. 38 */ 39 @Test testValidation()40 public void testValidation() { 41 42 /* 43 * This validation should be successfull 44 */ 45 Properties props = new Properties(); 46 props.setProperty(EnvironmentConfig.TXN_TIMEOUT, "10000"); 47 props.setProperty(EnvironmentConfig.TXN_DEADLOCK_STACK_TRACE, 48 "true"); 49 new EnvironmentConfig(props); // Just instantiate a config object. 50 51 /* 52 * Should fail: we should throw because leftover.param is not 53 * a valid parameter. 54 */ 55 props.clear(); 56 props.setProperty("leftover.param", "foo"); 57 checkEnvironmentConfigValidation(props); 58 59 /* 60 * Should fail: we should throw because FileHandlerLimit 61 * is less than its minimum 62 */ 63 props.clear(); 64 props.setProperty(EnvironmentConfig.LOCK_N_LOCK_TABLES, "0"); 65 checkEnvironmentConfigValidation(props); 66 67 /* 68 * Should fail: we should throw because FileHandler.on is not 69 * a valid value. 70 */ 71 props.clear(); 72 props.setProperty(EnvironmentConfig.TXN_DEADLOCK_STACK_TRACE, "xxx"); 73 checkEnvironmentConfigValidation(props); 74 } 75 76 /** 77 * Test single parameter setting. 78 */ 79 @Test testSingleParam()80 public void testSingleParam() 81 throws Exception { 82 83 try { 84 EnvironmentConfig config = new EnvironmentConfig(); 85 config.setConfigParam("foo", "7"); 86 fail("Should fail because of invalid param name"); 87 } catch (IllegalArgumentException e) { 88 // expected. 89 } 90 91 EnvironmentConfig config = new EnvironmentConfig(); 92 config.setConfigParam(EnvironmentParams.MAX_MEMORY_PERCENT.getName(), 93 "81"); 94 assertEquals(81, config.getCachePercent()); 95 } 96 97 /* 98 * Test that replicated config param is wrongly set on a standalone 99 * Environment. 100 */ 101 @Test testRepParam()102 public void testRepParam() 103 throws Exception { 104 105 /* The replicated property name and value. */ 106 final String propName = "je.rep.maxMessageSize"; 107 final String propValue = "1000000"; 108 109 /* 110 * Should fail if set this configuration through the EnvironmentConfig 111 * class. 112 */ 113 EnvironmentConfig envConfig = new EnvironmentConfig(); 114 try { 115 envConfig.setConfigParam(propName, propValue); 116 fail("Should fail because it's a replicated parameter"); 117 } catch (IllegalArgumentException e) { 118 /* Expected exception here. */ 119 } catch (Exception e) { 120 fail("Unexpected exception: " + e); 121 } 122 123 /* Read the je.properties. */ 124 ArrayList<String> formerLines = TestUtils.readWriteJEProperties 125 (envHome, propName + "=" + propValue + "\n"); 126 127 /* Check this behavior is valid. */ 128 Environment env = null; 129 try { 130 envConfig.setAllowCreate(true); 131 env = new Environment(envHome, envConfig); 132 } catch (Exception e) { 133 fail("Unexpected exception: " + e); 134 } 135 136 /* 137 * Check that getting the value for je.rep.maxMessageSize will throw 138 * exception. 139 */ 140 try { 141 EnvironmentImpl envImpl = DbInternal.getEnvironmentImpl(env); 142 envImpl.getConfigManager().get(propName); 143 fail("Expect to see exceptions here."); 144 } catch (IllegalArgumentException e) { 145 /* Expected exception here. */ 146 } catch (Exception e) { 147 fail("Unexpected exception: " + e); 148 } finally { 149 if (env != null) { 150 env.close(); 151 } 152 } 153 154 TestUtils.reWriteJEProperties(envHome, formerLines); 155 } 156 157 @Test testSerialize()158 public void testSerialize() 159 throws Exception { 160 161 final String nodeName = "env1"; 162 163 EnvironmentConfig envConfig = new EnvironmentConfig(); 164 /* Test the seriliazed fileds of EnvironmentConfig. */ 165 envConfig.setAllowCreate(true); 166 envConfig.setNodeName(nodeName); 167 /* Test the transient fields of EnvironmentConfig. */ 168 envConfig.setTxnReadCommitted(true); 169 /* Test the serialized fields of EnvironmentMutableConfig. */ 170 envConfig.setTransactional(true); 171 envConfig.setTxnNoSync(true); 172 envConfig.setCacheSize(100000); 173 envConfig.setConfigParam(EnvironmentConfig.ENV_RUN_CLEANER, 174 "false"); 175 envConfig.setCacheMode(CacheMode.DEFAULT); 176 envConfig.setCacheModeStrategy 177 (new MyCacheModeStrategy(CacheMode.DEFAULT)); 178 /* Test the transient fields in the EnvironmentMutableConfig. */ 179 envConfig.setLoadPropertyFile(true); 180 envConfig.setExceptionListener(new ExceptionListener() { 181 public void exceptionThrown(ExceptionEvent event) { 182 } 183 }); 184 185 EnvironmentConfig newConfig = (EnvironmentConfig) 186 TestUtils.serializeAndReadObject(envHome, envConfig); 187 188 assertTrue(newConfig != envConfig); 189 /* Check the serialized fields of EnvironmentConfig. */ 190 assertEquals(newConfig.getAllowCreate(), envConfig.getAllowCreate()); 191 assertEquals(newConfig.getNodeName(), nodeName); 192 /* Check the transient fields of EnvironmentConfig. */ 193 assertFalse(newConfig.getCreateUP() == envConfig.getCreateUP()); 194 assertNotSame 195 (newConfig.getCheckpointUP(), envConfig.getCheckpointUP()); 196 assertNotSame 197 (newConfig.getTxnReadCommitted(), envConfig.getTxnReadCommitted()); 198 /* Check the serialized fields of EnvironmentMutableConfig. */ 199 assertEquals 200 (newConfig.getTransactional(), envConfig.getTransactional()); 201 assertEquals(newConfig.getTxnNoSync(), envConfig.getTxnNoSync()); 202 assertEquals(newConfig.getCacheSize(), envConfig.getCacheSize()); 203 assertEquals(new DbConfigManager(newConfig). 204 get(EnvironmentConfig.ENV_RUN_CLEANER), 205 "false"); 206 assertEquals(newConfig.getCacheMode(), envConfig.getCacheMode()); 207 assertEquals(newConfig.getCacheModeStrategy().getCacheMode(), 208 envConfig.getCacheModeStrategy().getCacheMode()); 209 /* Check the transient fields of EnvironmentMutableConfig. */ 210 assertFalse(newConfig.getLoadPropertyFile() == 211 envConfig.getLoadPropertyFile()); 212 assertFalse 213 (newConfig.getValidateParams() == envConfig.getValidateParams()); 214 assertEquals(newConfig.getExceptionListener(), null); 215 } 216 217 @Test testInconsistentParams()218 public void testInconsistentParams() 219 throws Exception { 220 221 try { 222 EnvironmentConfig config = new EnvironmentConfig(); 223 config.setAllowCreate(true); 224 config.setLocking(false); 225 config.setTransactional(true); 226 File envHome = SharedTestUtils.getTestDir(); 227 new Environment(envHome, config); 228 fail("Should fail because of inconsistent param values"); 229 } catch (IllegalArgumentException e) { 230 // expected. 231 } 232 } 233 234 /* Helper to catch expected exceptions. */ checkEnvironmentConfigValidation(Properties props)235 private void checkEnvironmentConfigValidation(Properties props) { 236 try { 237 new EnvironmentConfig(props); 238 fail("Should fail because of a parameter validation problem"); 239 } catch (IllegalArgumentException e) { 240 // expected. 241 } 242 } 243 244 private static class MyCacheModeStrategy implements CacheModeStrategy { 245 private CacheMode cacheMode; 246 MyCacheModeStrategy(CacheMode cacheMode)247 public MyCacheModeStrategy(CacheMode cacheMode) { 248 this.cacheMode = cacheMode; 249 } 250 getCacheMode()251 public CacheMode getCacheMode() { 252 return cacheMode; 253 } 254 } 255 } 256