Skip to content

Commit

Permalink
gh-685: Initial properties save/load setup
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Apr 1, 2024
1 parent 10473c5 commit 3acdb74
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if (UTILS)
src/version.c
src/version_range.c
src/properties.c
src/properties_serialization.c
src/utils.c
src/filter.c
src/celix_log_level.c
Expand Down
1 change: 1 addition & 0 deletions libs/utils/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_executable(test_utils
src/CelixUtilsTestSuite.cc
src/ConvertUtilsTestSuite.cc
src/PropertiesTestSuite.cc
src/PropertiesSerializationTestSuite.cc
src/VersionTestSuite.cc
src/ErrTestSuite.cc
src/ThreadsTestSuite.cc
Expand Down
64 changes: 64 additions & 0 deletions libs/utils/gtest/src/PropertiesSerializationTestSuite.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 <gtest/gtest.h>

#include "celix_err.h"
#include "celix_properties.h"

using ::testing::MatchesRegex;

class PropertiesSerializationTestSuite : public ::testing::Test {
public:
PropertiesSerializationTestSuite() { celix_err_resetErrors(); }
};

TEST_F(PropertiesSerializationTestSuite, SaveEmptyPropertiesTest) {
//Given an empty properties object
celix_autoptr(celix_properties_t) props = celix_properties_create();

//And an in-memory stream
char* buf = nullptr;
size_t bufLen = 0;
FILE* stream = open_memstream(&buf, &bufLen);

//When saving the properties to the stream
auto status = celix_properties_saveToStream(props, stream);
EXPECT_EQ(CELIX_SUCCESS, status);

//Then the stream contains an empty JSON object
fclose(stream);
EXPECT_STREQ("{}", buf);
}

TEST_F(PropertiesSerializationTestSuite, LoadEmptyPropertiesTest) {
//Given an empty JSON object
const char* json = "{}";
FILE* stream = fmemopen((void*)json, strlen(json), "r");

//When loading the properties from the stream
celix_autoptr(celix_properties_t) props = nullptr;
auto status = celix_properties_loadFromStream(stream, &props);
EXPECT_EQ(CELIX_SUCCESS, status);

//Then the properties object is empty
EXPECT_EQ(0, celix_properties_size(props));

fclose(stream);
}
6 changes: 6 additions & 0 deletions libs/utils/include/celix_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ CELIX_UTILS_EXPORT celix_status_t celix_properties_store(celix_properties_t* pro
const char* file,
const char* header);

//TODO doc
CELIX_UTILS_EXPORT celix_status_t celix_properties_saveToStream(const celix_properties_t* properties, FILE* stream);

//TODO doc
CELIX_UTILS_EXPORT celix_status_t celix_properties_loadFromStream(FILE* stream, celix_properties_t** out);

/**
* @brief Get the entry for a given key in a property set.
*
Expand Down
35 changes: 35 additions & 0 deletions libs/utils/src/properties_serialization.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 "celix_properties.h"

celix_status_t celix_properties_saveToStream(const celix_properties_t* properties, FILE* stream) {
celix_status_t status = CELIX_SUCCESS;
(void)properties;
fprintf(stream, "{}");
return status;
}

celix_status_t celix_properties_loadFromStream(FILE* stream, celix_properties_t** out) {
celix_status_t status = CELIX_SUCCESS;
(void)stream;
celix_autoptr(celix_properties_t) props = celix_properties_create();
*out = celix_steal_ptr(props);
return status;
}

0 comments on commit 3acdb74

Please sign in to comment.