|
| 1 | +/*============================================================================= |
| 2 | +
|
| 3 | + Library: CppMicroServices |
| 4 | +
|
| 5 | + Copyright (c) The CppMicroServices developers. See the COPYRIGHT |
| 6 | + file at the top-level directory of this distribution and at |
| 7 | + https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT . |
| 8 | +
|
| 9 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + you may not use this file except in compliance with the License. |
| 11 | + You may obtain a copy of the License at |
| 12 | +
|
| 13 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +
|
| 21 | +=============================================================================*/ |
| 22 | + |
| 23 | +#include "TestFixture.hpp" |
| 24 | +#include "cppmicroservices/Constants.h" |
| 25 | +#include "cppmicroservices/ServiceObjects.h" |
| 26 | +#include "cppmicroservices/ServiceReference.h" |
| 27 | +#include "gtest/gtest.h" |
| 28 | + |
| 29 | +#include "TestInterfaces/Interfaces.hpp" |
| 30 | + |
| 31 | +namespace test |
| 32 | +{ |
| 33 | + struct int1 |
| 34 | + { |
| 35 | + [[nodiscard]] virtual int getValue() const = 0; |
| 36 | + virtual ~int1() = default; |
| 37 | + int1(int1 const&) = default; |
| 38 | + int1& operator=(int1 const&) = default; |
| 39 | + int1(int1&&) noexcept = default; |
| 40 | + int1& operator=(int1&&) noexcept = default; |
| 41 | + |
| 42 | + protected: |
| 43 | + int1() = default; |
| 44 | + }; |
| 45 | + |
| 46 | + struct TestServiceA : public int1 |
| 47 | + { |
| 48 | + TestServiceA(int val) : id(val) {} |
| 49 | + [[nodiscard]] int |
| 50 | + getValue() const override |
| 51 | + { |
| 52 | + return id; |
| 53 | + } |
| 54 | + |
| 55 | + private: |
| 56 | + int id; |
| 57 | + }; |
| 58 | + /* |
| 59 | + * Tests that a user is able to retrieve a serviceReference from a serviceObject from the coreFramework or DS |
| 60 | + */ |
| 61 | + TEST_F(tServiceComponent, testServiceReferenceRetrievalForDSAndManReg) |
| 62 | + { |
| 63 | + auto const id1 = 100; |
| 64 | + auto const id2 = 85; |
| 65 | + auto reg = context.RegisterService<int1>(std::make_shared<TestServiceA>(id1)); |
| 66 | + auto sref = reg.GetReference(); |
| 67 | + auto sref2 = context.RegisterService<int1>(std::make_shared<TestServiceA>(id2)).GetReference(); |
| 68 | + |
| 69 | + auto service1 = context.GetService(sref); |
| 70 | + ASSERT_EQ(service1->getValue(), id1); |
| 71 | + auto service2 = context.GetService(sref2); |
| 72 | + ASSERT_EQ(service2->getValue(), id2); |
| 73 | + |
| 74 | + auto srefFromService1 = cppmicroservices::ServiceReferenceFromService(service1); |
| 75 | + |
| 76 | + ASSERT_EQ(srefFromService1, sref); |
| 77 | + ASSERT_FALSE(cppmicroservices::ServiceReferenceFromService(service1) == sref2); |
| 78 | + ASSERT_EQ(cppmicroservices::ServiceReferenceFromService(service2), sref2); |
| 79 | + |
| 80 | + // Install and start bundle |
| 81 | + auto testBundle = ::test::InstallAndStartBundle(context, "TestBundleDSCA05"); |
| 82 | + ASSERT_TRUE(testBundle); |
| 83 | + |
| 84 | + auto dsRef = context.GetServiceReference<test::CAInterface>(); |
| 85 | + ASSERT_TRUE(dsRef); |
| 86 | + auto dsSvc = context.GetService<test::CAInterface>(dsRef); |
| 87 | + auto retSRef = cppmicroservices::ServiceReferenceFromService(dsSvc); |
| 88 | + ASSERT_EQ(retSRef, dsRef); |
| 89 | + ASSERT_EQ(cppmicroservices::any_cast<std::string>(retSRef.GetProperty("ManifestProp")), "abc"); |
| 90 | + |
| 91 | + ASSERT_THROW(cppmicroservices::ServiceReferenceFromService(std::make_shared<TestServiceA>(id1)), |
| 92 | + std::runtime_error); |
| 93 | + |
| 94 | + reg.Unregister(); |
| 95 | + |
| 96 | + // ensure only 1 sr exists in framework for int1 |
| 97 | + ASSERT_EQ(context.GetServiceReferences<int1>().size(), 1); |
| 98 | + // assert that the serviceReference returned for my service object is not valid after unregistration |
| 99 | + ASSERT_FALSE(cppmicroservices::ServiceReferenceFromService(service1)); |
| 100 | + |
| 101 | + // assert identical behavior from ref from original registration and ref from serviceReferenceFromService |
| 102 | + ASSERT_FALSE(sref); |
| 103 | + ASSERT_FALSE(srefFromService1); |
| 104 | + ASSERT_EQ(sref, srefFromService1); |
| 105 | + } |
| 106 | +} // namespace test |
0 commit comments