Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make getPropertyAsInt throw #3146

Merged
merged 8 commits into from
Nov 18, 2024
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
10 changes: 6 additions & 4 deletions cpp/include/Ice/Properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ namespace Ice
* Get a property as an integer. If the property is not set, 0 is returned.
* @param key The property key.
* @return The property value interpreted as an integer.
* @throws PropertyException If the property value is not a valid integer.
* @see #setProperty
*/
int getPropertyAsInt(std::string_view key) noexcept;
int getPropertyAsInt(std::string_view key);

/**
* Get an Ice property as an integer. If the property is not set, its default value is returned.
* @param key The property key.
* @return The property value interpreted as an integer, or the default value.
* @throws std::invalid_argument If the property is not a known Ice property.
* @throws PropertyException If the property is not a known Ice property or the value is not a valid integer.
* @see #setProperty
*/
int getIcePropertyAsInt(std::string_view key);
Expand All @@ -103,9 +104,10 @@ namespace Ice
* @param key The property key.
* @param value The default value to use if the property does not exist.
* @return The property value interpreted as an integer, or the default value.
* @throws PropertyException If the property value is not a valid integer.
* @see #setProperty
*/
int getPropertyAsIntWithDefault(std::string_view key, int value) noexcept;
int getPropertyAsIntWithDefault(std::string_view key, int value);

/**
* Get a property as a list of strings. The strings must be separated by whitespace or comma. If the property is
Expand All @@ -127,7 +129,7 @@ namespace Ice
* can be written as O'Reilly, "O'Reilly" or 'O\'Reilly'.
* @param key The property key.
* @return The property value interpreted as list of strings, or the default value.
* @throws std::invalid_argument If the property is not a known Ice property.
* @throws PropertyException If the property is not a known Ice property.
* @see #setProperty
*/
StringSeq getIcePropertyAsList(std::string_view key);
Expand Down
12 changes: 6 additions & 6 deletions cpp/src/Ice/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Ice::Properties::getPropertyWithDefault(string_view key, string_view value) noex
}

int32_t
Ice::Properties::getPropertyAsInt(string_view key) noexcept
Ice::Properties::getPropertyAsInt(string_view key)
{
return getPropertyAsIntWithDefault(key, 0);
}
Expand All @@ -224,21 +224,21 @@ Ice::Properties::getIcePropertyAsInt(string_view key)
}

int32_t
Ice::Properties::getPropertyAsIntWithDefault(string_view key, int32_t value) noexcept
Ice::Properties::getPropertyAsIntWithDefault(string_view key, int32_t value)
{
lock_guard lock(_mutex);

map<string, PropertyValue>::iterator p = _properties.find(key);
if (p != _properties.end())
{
int32_t val = value;
p->second.used = true;
istringstream v(p->second.value);
if (!(v >> value) || !v.eof())
{
Warning out(getProcessLogger());
out << "numeric property " << key << " set to non-numeric value, defaulting to " << val;
return val;
throw PropertyException(
__FILE__,
__LINE__,
"property '" + string{key} + "' has an invalid integer value: '" + p->second.value + "'");
}
}

Expand Down
15 changes: 15 additions & 0 deletions cpp/test/Ice/properties/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ Client::run(int, char**)
test(properties->getProperty("Ice.MessageSizeMax") == "20");
cout << "ok" << endl;
}

{
cout << "testing that trying to read a non-numeric value as an int throws... " << flush;
Ice::PropertiesPtr properties = Ice::createProperties();
properties->setProperty("Foo", "bar");
try
{
properties->getPropertyAsInt("Foo");
test(false);
}
catch (const Ice::PropertyException&)
{
}
cout << "ok" << endl;
}
}

DEFINE_TEST(Client)
7 changes: 5 additions & 2 deletions csharp/src/Ice/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public string getPropertyWithDefault(string key, string value)
/// </summary>
/// <param name="key">The property key.</param>
/// <returns>The property value interpreted as an integer.</returns>
/// <exception name="PropertyException">Thrown if the property value is not a valid integer.</exception>
public int getPropertyAsInt(string key) => getPropertyAsIntWithDefault(key, 0);

/// <summary>
Expand All @@ -177,6 +178,8 @@ public string getPropertyWithDefault(string key, string value)
/// </summary>
/// <param name="key">The property key.</param>
/// <returns>The property value interpreted as an integer, or the default value.</returns>
/// <exception name="PropertyException">Thrown if the property is not a known Ice property or the value is not a
/// valid integer.</exception>
public int getIcePropertyAsInt(string key)
{
string defaultValueString = getDefaultProperty(key);
Expand All @@ -196,6 +199,7 @@ public int getIcePropertyAsInt(string key)
/// <param name="key">The property key.</param>
/// <param name="value">The default value to use if the property does not exist.</param>
/// <returns>The property value interpreted as an integer, or the default value.</returns>
/// <exception name="PropertyException">Thrown if the property value is not a valid integer.</exception>
public int getPropertyAsIntWithDefault(string key, int value)
{
lock (_mutex)
Expand All @@ -211,8 +215,7 @@ public int getPropertyAsIntWithDefault(string key, int value)
}
catch (FormatException)
{
Util.getProcessLogger().warning($"numeric property {key} set to non-numeric value, defaulting to {value}");
return value;
throw new PropertyException($"property '{key}' has an invalid integer value: '{pv.value}'");
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions csharp/test/Ice/properties/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ public override void run(string[] args)
test(properties.getIceProperty("Ice.MessageSizeMax") == "20");
Console.Out.WriteLine("ok");
}

{
Console.Out.Write("testing that trying to read a non-numeric value as an int throws... ");
var properties = new Ice.Properties();
properties.setProperty("Foo", "bar");
try
{
properties.getIcePropertyAsInt("Foo");
test(false);
}
catch (Ice.PropertyException)
{
}
Console.Out.WriteLine("ok");
}
}

public static Task<int> Main(string[] args) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public synchronized String getPropertyWithDefault(String key, String value) {
*
* @param key The property key.
* @return The property value interpreted as an integer.
* @throws PropertyException Raised if the property value is not a valid integer.
* @see #setProperty
*/
public int getPropertyAsInt(String key) {
Expand All @@ -215,6 +216,8 @@ public int getPropertyAsInt(String key) {
*
* @param key The property key.
* @return The property value interpreted as an integer, or the default value.
* @throws PropertyException Raised if the property is not a known Ice property or the value is
* not a valid integer.
* @see #setProperty
*/
public synchronized int getIcePropertyAsInt(String key) {
Expand All @@ -234,6 +237,7 @@ public synchronized int getIcePropertyAsInt(String key) {
* @param key The property key.
* @param value The default value to use if the property does not exist.
* @return The property value interpreted as an integer, or the default value.
* @throws PropertyException Raised if the property value is not a valid integer.
* @see #setProperty
*/
public synchronized int getPropertyAsIntWithDefault(String key, int value) {
Expand All @@ -244,12 +248,8 @@ public synchronized int getPropertyAsIntWithDefault(String key, int value) {
try {
return Integer.parseInt(pv.value);
} catch (NumberFormatException ex) {
Util.getProcessLogger()
.warning(
"numeric property "
+ key
+ " set to non-numeric value, defaulting to "
+ value);
throw new PropertyException(
"property '" + key + "' has an invalid integer value: '" + pv.value + "'");
}
}

Expand Down
15 changes: 15 additions & 0 deletions java/test/src/main/java/test/Ice/properties/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ public void run(String[] args) {
test(properties.getIceProperty("Ice.MessageSizeMax").equals("20"));
System.out.println("ok");
}

{
System.out.print(
"testing that trying to read a non-numeric value as an int throws... ");
System.out.flush();

Properties properties = new Properties();
properties.setProperty("Foo", "bar");
try {
properties.getIcePropertyAsInt("Foo");
test(false);
} catch (PropertyException ex) {
}
System.out.println("ok");
}
}

private static final String configPath = "./config/\u4E2D\u56FD_client.config";
Expand Down
8 changes: 6 additions & 2 deletions js/src/Ice/Properties.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare module "ice" {
*
* @param key - The property key.
* @returns The property value interpreted as an integer.
* @throws {@link PropertyException} - Thrown if the property value is not a valid integer.
*
* @see {@link setProperty}
*/
Expand All @@ -55,6 +56,8 @@ declare module "ice" {
*
* @param key - The property key.
* @returns The property value interpreted as an integer.
* @throws {@link PropertyException} - Thrown if the property is not a known Ice property or the value is
* not a valid integer.
*
* @see {@link setProperty}
*/
Expand All @@ -64,12 +67,13 @@ declare module "ice" {
* Get a property as an integer. If the property is not set, the given default value is returned.
*
* @param key The property key.
* @param value The default value to use if the property does not exist.
* @param defaultValue The default value to use if the property does not exist.
* @returns The property value interpreted as an integer, or the default value.
* @throws {@link PropertyException} - Thrown if the property value is not a valid integer.
*
* @see {@link setProperty}
*/
getPropertyAsIntWithDefault(key: string, value: number): number;
getPropertyAsIntWithDefault(key: string, defaultValue: number): number;

/**
* Retrieves a property value as a list of strings. The strings must be separated by whitespace or commas.
Expand Down
11 changes: 8 additions & 3 deletions js/src/Ice/Properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,22 @@ export class Properties {
let defaultValue = 0;
if (defaultValueString != "") {
defaultValue = parseInt(defaultValueString);
DEV: console.assert(!isNaN(defaultValue));
}
return this.getPropertyAsIntWithDefault(key, defaultValue);
}

getPropertyAsIntWithDefault(key, value) {
getPropertyAsIntWithDefault(key, defaultValue) {
const pv = this._properties.get(key);
if (pv !== undefined) {
pv.used = true;
return parseInt(pv.value);
} else {
const value = parseInt(pv.value);
if (isNaN(value)) {
throw new PropertyException(`property '${key}' has an invalid integer value: '${pv.value}'`);
}
return value;
} else {
return defaultValue;
}
}

Expand Down
13 changes: 13 additions & 0 deletions js/test/Ice/properties/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ export class Client extends TestHelper {
test(properties.getIceProperty("Ice.MessageSizeMax") == "20");
out.writeLine("ok");
}

{
out.write("testing that trying to read a non-numeric value as an int throws... ");
const properties = Ice.createProperties();
try {
properties.setProperty("Foo", "bar");
properties.getPropertyAsInt("Foo");
test(false);
} catch (ex) {
test(ex instanceof Ice.PropertyException);
}
out.writeLine("ok");
}
}

async run(args: string[]) {
Expand Down
10 changes: 10 additions & 0 deletions matlab/test/Ice/properties/Client.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@ function client(args)
end
fprintf('ok\n');

fprintf('testing that trying to read a non-numeric value as an int throws... ');
try
props.setProperty('Foo', 'bar');
props.getPropertyAsInt('Foo');
assert(false);
catch ex
assert(isa(ex, 'Ice.PropertyException'));
end
fprintf('ok\n');

clear('classes'); % Avoids conflicts with tests that define the same symbols.
end
20 changes: 15 additions & 5 deletions php/test/Ice/properties/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function run($args)
test($properties->getIceProperty("Ice.Trace.Protocol"), "1");
echo "ok\n";

echo "testing ice properties with set default values...";
echo "testing ice properties with set default values... ";
$properties = Ice\createProperties();

$toStringMode = $properties->getIceProperty("Ice.ToStringMode");
Expand All @@ -37,7 +37,7 @@ function run($args)

echo "ok\n";

echo "testing ice properties with unset default values...";
echo "testing ice properties with unset default values... ";
$properties = Ice\createProperties();

$stringValue = $properties->getIceProperty("Ice.Admin.Router");
Expand All @@ -52,23 +52,33 @@ function run($args)
echo "ok\n";

echo "testing load properties exception... ";
$properties = Ice\createProperties();
try {
$properties = Ice\createProperties();
$properties->load("./config/xxxx.config");
test(False);
} catch (\Ice\LocalException $ex) {
test(str_contains($ex->getMessage(), "error while accessing file './config/xxxx.config'"));
}
echo "ok\n";

echo "testing that getting an unknown ice property throws an exception...";
echo "testing that getting an unknown ice property throws an exception... ";
$properties = Ice\createProperties();
try {
$properties = Ice\createProperties();
$properties->getIceProperty("Ice.UnknownProperty");
test(False);
} catch (\Ice\PropertyException $ex) {
test($ex->getMessage() == "unknown Ice property: Ice.UnknownProperty");
}
echo "ok\n";

echo "testing that trying to read a non-numeric value as an int throws... ";
$properties = Ice\createProperties();
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try block should not include the "arrange" part of the test.

$properties->setProperty("Foo", "bar");
$properties->getPropertyAsInt("Foo");
test(False);
} catch (\Ice\PropertyException $ex) {
}
echo "ok\n";
}
}
14 changes: 14 additions & 0 deletions python/test/Ice/properties/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,17 @@ def run(sef, args):
except Ice.PropertyException:
pass
print("ok")

sys.stdout.write(
"testing that trying to read a non-numeric value as an int throws... "
)
sys.stdout.flush()

properties = Ice.createProperties()
properties.setProperty("Test", "foo")
try:
properties.getPropertyAsInt("Test")
test(False)
except Ice.PropertyException:
pass
print("ok")
Loading