Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package org.apache.hadoop.hdds.conf;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* In HDDS-5035, we met the case that ozone-default-generated.xml got
* overwritten in an assemble jar, here we are trying to simulate this case
* by rename the generated config file.
*/
public class TestGeneratedConfigurationOverwrite {

private final Path generatedConfigurationPath =
Paths.get("target/test-classes/ozone-default-generated.xml");
private final Path generatedConfigurationPathBak =
Paths.get("target/test-classes/ozone-default-generated.xml.bak");

private OzoneConfiguration conf;

@Before
public void overwriteConfigFile() throws Exception {
Files.move(generatedConfigurationPath, generatedConfigurationPathBak);
conf = new OzoneConfiguration();
}

@After
public void recoverConfigFile() throws Exception {
Files.move(generatedConfigurationPathBak, generatedConfigurationPath);
}

@Test
public void getConfigurationObject() {
// Check Config Type of String
Assert.assertNotNull(
conf.getObject(SimpleConfiguration.class).getBindHost());
// Check Config Type of Int
Assert.assertNotEquals(
conf.getObject(SimpleConfiguration.class).getPort(), 0);
// Check Config Type of Time
Assert.assertNotEquals(
conf.getObject(SimpleConfiguration.class).getWaitTime(), 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,40 @@ public static <T> void injectConfigurationToObject(ConfigurationSource from,

String key = prefix + "." + configAnnotation.key();

String defaultValue = configAnnotation.defaultValue();

ConfigType type = configAnnotation.type();

if (type == ConfigType.AUTO) {
type = detectConfigType(field.getType(), fieldLocation);
}

//Note: default value is handled by ozone-default.xml. Here we can
//use any default.
try {
switch (type) {
case STRING:
forcedFieldSet(field, configuration, from.get(key));
forcedFieldSet(field, configuration, from.get(key, defaultValue));
break;
case INT:
forcedFieldSet(field, configuration, from.getInt(key, 0));
forcedFieldSet(field, configuration,
from.getInt(key, Integer.parseInt(defaultValue)));
break;
case BOOLEAN:
forcedFieldSet(field, configuration, from.getBoolean(key, false));
forcedFieldSet(field, configuration,
from.getBoolean(key, Boolean.parseBoolean(defaultValue)));
break;
case LONG:
forcedFieldSet(field, configuration, from.getLong(key, 0));
forcedFieldSet(field, configuration,
from.getLong(key, Long.parseLong(defaultValue)));
break;
case TIME:
forcedFieldSet(field, configuration,
from.getTimeDuration(key, "0s", configAnnotation.timeUnit()));
from.getTimeDuration(key, defaultValue,
configAnnotation.timeUnit()));
break;
case SIZE:
final long value =
Math.round(from.getStorageSize(key, "0b", StorageUnit.BYTES));
Math.round(from.getStorageSize(key,
defaultValue, StorageUnit.BYTES));
if (field.getType() == int.class) {
forcedFieldSet(field, configuration, (int) value);
} else {
Expand All @@ -106,13 +111,13 @@ public static <T> void injectConfigurationToObject(ConfigurationSource from,
break;
case CLASS:
forcedFieldSet(field, configuration,
from.getClass(key, Object.class));
from.getClass(key, Class.forName(defaultValue)));
break;
default:
throw new ConfigurationException(
"Unsupported ConfigType " + type + " on " + fieldLocation);
}
} catch (IllegalAccessException e) {
} catch (IllegalAccessException | ClassNotFoundException e) {
throw new ConfigurationException(
"Can't inject configuration to " + fieldLocation, e);
}
Expand Down