/** * Initialize all the ZooKeeper client properties which are configurable as * java system property */ private void initFromJavaSystemProperties() { setProperty(ZOOKEEPER_REQUEST_TIMEOUT, System.getProperty(ZOOKEEPER_REQUEST_TIMEOUT)); }
@Override protected void handleBackwardCompatibility() { /** * backward compatibility for properties which are common to both client * and server */ super.handleBackwardCompatibility(); /** * backward compatibility for client specific properties */ setProperty(ZK_SASL_CLIENT_USERNAME, System.getProperty(ZK_SASL_CLIENT_USERNAME)); setProperty(LOGIN_CONTEXT_NAME_KEY, System.getProperty(LOGIN_CONTEXT_NAME_KEY)); setProperty(ENABLE_CLIENT_SASL_KEY, System.getProperty(ENABLE_CLIENT_SASL_KEY)); setProperty(ZOOKEEPER_SERVER_REALM, System.getProperty(ZOOKEEPER_SERVER_REALM)); setProperty(DISABLE_AUTO_WATCH_RESET, System.getProperty(DISABLE_AUTO_WATCH_RESET)); setProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET, System.getProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET)); setProperty(SECURE_CLIENT, System.getProperty(SECURE_CLIENT)); }
@Test public void testSetConfiguration() { ZKClientConfig conf = new ZKClientConfig(); String defaultValue = conf.getProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, ZKClientConfig.ENABLE_CLIENT_SASL_DEFAULT); if (defaultValue.equals("true")) { conf.setProperty(ENABLE_CLIENT_SASL_KEY, "false"); } else { conf.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); } assertTrue(conf.getProperty(ENABLE_CLIENT_SASL_KEY) != defaultValue); }
@Test public void testClientNoCanonicalization() throws IOException, InterruptedException { SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); when(addr.getHostName()).thenReturn("zookeeper.apache.org"); when(addr.getAddress()).thenReturn(ia); when(ia.getCanonicalHostName()).thenReturn("zk1.apache.org"); when(ia.getHostAddress()).thenReturn("127.0.0.1"); ZKClientConfig conf = new ZKClientConfig(); conf.setProperty(ZKClientConfig.ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, "false"); String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); Assert.assertEquals("The computed principal does appears to have been canonicalized incorrectly", "zookeeper/zookeeper.apache.org", principal); }
@Test public void testIntegerRetrievalFromProperty() { ZKClientConfig conf = new ZKClientConfig(); String prop = "UnSetProperty" + System.currentTimeMillis(); int defaultValue = 100; // property is not set we should get the default value int result = conf.getInt(prop, defaultValue); assertEquals(defaultValue, result); // property is set but can not be parsed to int, we should get the // NumberFormatException conf.setProperty(ZKConfig.JUTE_MAXBUFFER, "InvlaidIntValue123"); try { result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); fail("NumberFormatException is expected"); } catch (NumberFormatException exception) { // do nothing } assertEquals(defaultValue, result); // property is set to an valid int, we should get the set value int value = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT; conf.setProperty(ZKConfig.JUTE_MAXBUFFER, Integer.toString(value)); result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); assertEquals(value, result); // property is set but with white spaces value = 12345; conf.setProperty(ZKConfig.JUTE_MAXBUFFER, " " + Integer.toString(value) + " "); result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); assertEquals(value, result); }
@Test public void testSystemPropertyValue() { String clientName = "zookeeper1"; System.setProperty(ZK_SASL_CLIENT_USERNAME, clientName); ZKClientConfig conf = new ZKClientConfig(); assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), clientName); String newClientName = "zookeeper2"; conf.setProperty(ZK_SASL_CLIENT_USERNAME, newClientName); assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), newClientName); }
@Test public void testWhenInvalidJuteMaxBufferIsConfiguredIOExceptionIsThrown() { ZKClientConfig clientConfig = new ZKClientConfig(); String value = "SomeInvalidInt"; clientConfig.setProperty(ZKConfig.JUTE_MAXBUFFER, value); // verify ClientCnxnSocketNIO creation try { new ClientCnxnSocketNIO(clientConfig); fail("IOException is expected."); } catch (IOException e) { assertTrue(e.getMessage().contains(value)); } // verify ClientCnxnSocketNetty creation try { new ClientCnxnSocketNetty(clientConfig); fail("IOException is expected."); } catch (IOException e) { assertTrue(e.getMessage().contains(value)); } }