/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "configuration_test.h" #include "common/configuration.h" #include #include #include using ::testing::_; using namespace hdfs; namespace hdfs { TEST(ConfigurationTest, TestDegenerateInputs) { /* Completely empty stream */ { std::stringstream stream; optional config = Configuration::Load(""); EXPECT_FALSE(config && "Empty stream"); } /* No values */ { std::string data = ""; optional config = Configuration::Load(data); EXPECT_TRUE(config && "Blank config"); } /* Extraneous values */ { std::string data = ""; optional config = Configuration::Load(data); EXPECT_TRUE(config && "Extraneous values"); } } TEST(ConfigurationTest, TestBasicOperations) { /* Single value */ { std::stringstream stream; simpleConfigStream(stream, "key1", "value1"); optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse single value"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); } /* Multiple values */ { optional config = simpleConfig("key1", "value1", "key2", "value2"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); EXPECT_EQ("value2", config->GetWithDefault("key2", "")); } /* No defaults */ { std::stringstream stream; simpleConfigStream(stream, "key1", "value1"); optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse single value"); optional value = config->Get("key1"); EXPECT_TRUE((bool)value); EXPECT_EQ("value1", *value); EXPECT_FALSE(config->Get("key2")); } } TEST(ConfigurationTest, TestCompactValues) { { std::stringstream stream; stream << ""; optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Compact value parse"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); } } TEST(ConfigurationTest, TestMultipleResources) { /* Single value */ { std::stringstream stream; simpleConfigStream(stream, "key1", "value1"); optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse first stream"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); std::stringstream stream2; simpleConfigStream(stream2, "key2", "value2"); optional config2 = config->OverlayResourceString(stream2.str()); EXPECT_TRUE(config2 && "Parse second stream"); EXPECT_EQ("value1", config2->GetWithDefault("key1", "")); EXPECT_EQ("value2", config2->GetWithDefault("key2", "")); } } TEST(ConfigurationTest, TestStringResource) { /* Single value */ { std::stringstream stream; simpleConfigStream(stream, "key1", "value1"); std::string str = stream.str(); optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse single value"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); } } TEST(ConfigurationTest, TestFinal) { { /* Explicitly non-final non-compact value */ std::stringstream stream; stream << "key1value1false"; optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse first stream"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); std::stringstream stream2; simpleConfigStream(stream2, "key1", "value2"); optional config2 = config->OverlayResourceString(stream2.str()); EXPECT_TRUE(config2 && "Parse second stream"); EXPECT_EQ("value2", config2->GetWithDefault("key1", "")); } { /* Explicitly final non-compact value */ std::stringstream stream; stream << "key1value1true"; optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse first stream"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); std::stringstream stream2; simpleConfigStream(stream2, "key1", "value2"); optional config2 = config->OverlayResourceString(stream2.str()); EXPECT_TRUE(config2 && "Parse second stream"); EXPECT_EQ("value1", config2->GetWithDefault("key1", "")); } { /* Explicitly non-final compact value */ std::stringstream stream; stream << ""; optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse first stream"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); std::stringstream stream2; simpleConfigStream(stream2, "key1", "value2"); optional config2 = config->OverlayResourceString(stream2.str()); EXPECT_TRUE(config2 && "Parse second stream"); EXPECT_EQ("value2", config2->GetWithDefault("key1", "")); } { /* Explicitly final compact value */ std::stringstream stream; stream << ""; optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse first stream"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); std::stringstream stream2; simpleConfigStream(stream2, "key1", "value2"); optional config2 = config->OverlayResourceString(stream2.str()); EXPECT_TRUE(config2 && "Parse second stream"); EXPECT_EQ("value1", config2->GetWithDefault("key1", "")); } { /* Bogus final value */ std::stringstream stream; stream << "key1value1spam"; optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse first stream"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); std::stringstream stream2; simpleConfigStream(stream2, "key1", "value2"); optional config2 = config->OverlayResourceString(stream2.str()); EXPECT_TRUE(config2 && "Parse second stream"); EXPECT_EQ("value2", config2->GetWithDefault("key1", "")); } { /* Blank final value */ std::stringstream stream; stream << "key1value1"; optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse first stream"); EXPECT_EQ("value1", config->GetWithDefault("key1", "")); std::stringstream stream2; simpleConfigStream(stream2, "key1", "value2"); optional config2 = config->OverlayResourceString(stream2.str()); EXPECT_TRUE(config2 && "Parse second stream"); EXPECT_EQ("value2", config2->GetWithDefault("key1", "")); } } TEST(ConfigurationTest, TestIntConversions) { /* No defaults */ { std::stringstream stream; simpleConfigStream(stream, "key1", "1"); optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse single value"); optional value = config->GetInt("key1"); EXPECT_TRUE((bool)value); EXPECT_EQ(1, *value); EXPECT_FALSE(config->GetInt("key2")); } { optional config = simpleConfig("key1", "1"); EXPECT_EQ(1, config->GetIntWithDefault("key1", -1)); } { optional config = simpleConfig("key1", "-100"); EXPECT_EQ(-100, config->GetIntWithDefault("key1", -1)); } { optional config = simpleConfig("key1", " 1 "); EXPECT_EQ(1, config->GetIntWithDefault("key1", -1)); } { optional config = simpleConfig("key1", ""); EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1)); } { optional config = simpleConfig("key1", "spam"); EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1)); } { optional config = simpleConfig("key2", ""); EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1)); } } TEST(ConfigurationTest, TestDoubleConversions) { /* No defaults */ { std::stringstream stream; simpleConfigStream(stream, "key1", "1"); optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse single value"); optional value = config->GetDouble("key1"); EXPECT_TRUE((bool)value); EXPECT_EQ(1, *value); EXPECT_FALSE(config->GetDouble("key2")); } { optional config = simpleConfig("key1", "1"); EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1)); } { optional config = simpleConfig("key1", "-100"); EXPECT_EQ(-100, config->GetDoubleWithDefault("key1", -1)); } { optional config = simpleConfig("key1", " 1 "); EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1)); } { optional config = simpleConfig("key1", ""); EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1)); } { optional config = simpleConfig("key1", "spam"); EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1)); } { optional config = simpleConfig("key2", ""); EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1)); } { /* Out of range */ optional config = simpleConfig("key2", "1e9999"); EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1)); } } TEST(ConfigurationTest, TestBoolConversions) { /* No defaults */ { std::stringstream stream; simpleConfigStream(stream, "key1", "true"); optional config = Configuration::Load(stream.str()); EXPECT_TRUE(config && "Parse single value"); optional value = config->GetBool("key1"); EXPECT_TRUE((bool)value); EXPECT_EQ(true, *value); EXPECT_FALSE(config->GetBool("key2")); } { optional config = simpleConfig("key1", "true"); EXPECT_EQ(true, config->GetBoolWithDefault("key1", false)); } { optional config = simpleConfig("key1", "tRuE"); EXPECT_EQ(true, config->GetBoolWithDefault("key1", false)); } { optional config = simpleConfig("key1", "false"); EXPECT_FALSE(config->GetBoolWithDefault("key1", true)); } { optional config = simpleConfig("key1", "FaLsE"); EXPECT_FALSE(config->GetBoolWithDefault("key1", true)); } { optional config = simpleConfig("key1", " FaLsE "); EXPECT_FALSE(config->GetBoolWithDefault("key1", true)); } { optional config = simpleConfig("key1", ""); EXPECT_EQ(true, config->GetBoolWithDefault("key1", true)); } { optional config = simpleConfig("key1", "spam"); EXPECT_EQ(true, config->GetBoolWithDefault("key1", true)); } { optional config = simpleConfig("key1", ""); EXPECT_EQ(true, config->GetBoolWithDefault("key2", true)); } } int main(int argc, char *argv[]) { /* * The following line must be executed to initialize Google Mock * (and Google Test) before running the tests. */ ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); } }