From 0b7d3c75fcdd9235c96a3545569ba4bb6ec2a4d2 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Mon, 4 May 2026 20:51:48 +0200 Subject: [PATCH] Reduce javax E4-Injector tests to the minimum and add Singleton test --- .../.gitignore | 1 - .../org.eclipse.e4.core.javax.tests/.project | 5 - .../org.eclipse.pde.ds.annotations.prefs | 7 - .../META-INF/MANIFEST.MF | 47 +- .../OSGI-INF/l10n/bundle.properties | 11 - .../OSGI-INF/l10n/bundle_de.properties | 10 - .../l10n/bundle_de_ch_test.properties | 10 - .../org.eclipse.e4.core.javax.tests/README.md | 2 +- .../build.properties | 10 +- .../org.eclipse.e4.core.javax.tests/pom.xml | 62 -- .../resources/another.properties | 11 - .../resources/another_de.properties | 10 - .../internal/tests/CoreTestsActivator.java | 99 -- .../tests/contexts/ActivationTest.java | 332 ------ .../tests/contexts/AddContextFunction.java | 37 - .../tests/contexts/ContextDynamicTest.java | 178 ---- .../tests/contexts/ContextFunctionHigh.java | 11 - .../tests/contexts/ContextFunctionLow.java | 11 - .../tests/contexts/DependenciesLeakTest.java | 111 -- .../tests/contexts/EclipseContextTest.java | 397 -------- .../tests/contexts/NeutralValueTest.java | 206 ---- .../tests/contexts/ReparentingTest.java | 387 ------- .../tests/contexts/RunAndTrackTest.java | 430 -------- .../tests/contexts/StrongIterableTest.java | 273 ----- .../inject/ActivationInjectionTest.java | 221 ---- .../inject/AnnotationsInjectionTest.java | 140 ++- .../tests/contexts/inject/Bug317183Test.java | 82 -- .../contexts/inject/ComplexDisposalTest.java | 104 -- .../inject/ContextFunctionDynamicsTest.java | 181 ---- .../inject/ContextInjectionDisposeTest.java | 138 --- .../inject/ContextInjectionFactoryTest.java | 108 -- .../contexts/inject/ContextInjectionTest.java | 325 ------ .../DisposingReferencedContextTest.java | 111 -- .../inject/ExtraDependenciesTest.java | 115 --- .../inject/GenericsInjectionTest.java | 151 --- .../contexts/inject/GenericsListTest.java | 88 -- .../contexts/inject/GroupedUpdatesTest.java | 142 --- .../inject/InjectStaticContextTest.java | 277 ----- .../contexts/inject/InjectionOrderTest.java | 110 -- .../contexts/inject/InjectionUpdateTest.java | 189 ---- .../contexts/inject/InvokeInRATTest.java | 89 -- .../tests/contexts/inject/ObjectBasic.java | 81 -- .../tests/contexts/inject/ObjectSubClass.java | 96 -- .../contexts/inject/ObjectSuperClass.java | 93 -- .../contexts/inject/OptionalAnnotations.java | 50 - .../inject/ParentContextDisposalTest.java | 61 -- .../tests/contexts/inject/PrintService.java | 24 - .../contexts/inject/ServiceContextTest.java | 395 -------- .../contexts/inject/StringPrintService.java | 30 - .../inject/TestConstructorInjection.java | 132 --- .../contexts/inject/TestOtherService.java | 5 - .../tests/contexts/inject/TestService.java | 5 - .../tests/contexts/inject/TestServiceA.java | 8 - .../tests/contexts/inject/TestServiceB.java | 10 - .../inject/TestServiceController.java | 32 - .../performance/ContextPerformanceTest.java | 138 --- .../di/AutoConstructWithCreatableTest.java | 105 -- .../tests/di/DisposeClassLinkTest.java | 160 --- .../internal/tests/di/InjectArraysTest.java | 75 -- .../internal/tests/di/InjectBaseTypeTest.java | 121 --- .../internal/tests/di/InjectBridgeTest.java | 81 -- .../internal/tests/di/InjectionOrderTest.java | 72 -- .../tests/di/InjectionResultLeakTest.java | 83 -- .../e4/core/internal/tests/di/InvokeTest.java | 53 - .../tests/di/InvokeTestMissingAnnotation.java | 65 -- .../tests/di/RecursiveObjectCreationTest.java | 96 -- .../core/internal/tests/di/RequestorTest.java | 60 -- .../tests/di/extensions/ComponentEnabler.java | 32 - .../tests/di/extensions/DisabledServiceA.java | 8 - .../tests/di/extensions/DisabledServiceB.java | 10 - .../ExtendedSupplierInjectionTests.java | 167 --- .../tests/di/extensions/FilterServiceA.java | 10 - .../tests/di/extensions/FilterServiceB.java | 8 - .../di/extensions/InjectionEventTest.java | 288 ------ .../InjectionMixedSuppliersTest.java | 74 -- .../extensions/InjectionOSGiHandlerTest.java | 59 -- .../di/extensions/InjectionOSGiTest.java | 151 --- .../extensions/InjectionPreferencesTest.java | 299 ------ .../extensions/InjectionWithoutOSGITest.java | 65 -- .../tests/di/extensions/SampleServiceA.java | 10 - .../tests/di/extensions/SampleServiceB.java | 10 - .../extensions/ServiceSupplierTestCase.java | 321 ------ .../tests/di/extensions/TestService.java | 5 - .../manual/ComputedValueLimitationTest.java | 123 --- .../internal/tests/manual/ContextExample.java | 161 --- .../manual/InjectionErrorReportingTest.java | 228 ----- .../internal/tests/nls/BundleMessages.java | 45 - .../tests/nls/BundleMessagesRegistry.java | 18 - .../tests/nls/LocaleTransformationTest.java | 168 ---- .../tests/nls/MessageRegistryTest.java | 170 ---- .../e4/core/internal/tests/nls/Messages.java | 42 - .../e4/core/internal/tests/nls/NLSTest.java | 948 ------------------ .../tests/nls/ResourceBundleClass.java | 24 - .../nls/ResourceBundleClassMessages.java | 44 - .../tests/nls/ResourceBundleClass_de.java | 23 - .../internal/tests/nls/ResourcesMessages.java | 44 - .../internal/tests/nls/SimpleMessages.java | 41 - .../tests/nls/SimpleMessages.properties | 11 - .../tests/nls/SimpleMessages_de.properties | 10 - .../tests/nls/SimpleMessages_de_CH.properties | 2 - .../nls/SimpleMessages_de_CH_OTHER.properties | 2 - .../nls/SimpleMessages_de_CH_TEST.properties | 2 - .../internal/tests/nls/messages.properties | 11 - .../internal/tests/nls/messages_de.properties | 10 - .../e4/core/javax/tests/CoreTestSuite.java | 99 -- .../inject/AnnotationsInjectionTest.java | 60 +- .../contexts/inject/OptionalAnnotations.java | 50 - 107 files changed, 172 insertions(+), 11041 deletions(-) delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/.gitignore delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/.settings/org.eclipse.pde.ds.annotations.prefs delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de_ch_test.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/pom.xml delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/resources/another.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/resources/another_de.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/CoreTestsActivator.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ActivationTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/AddContextFunction.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextDynamicTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionHigh.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionLow.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/DependenciesLeakTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/NeutralValueTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ReparentingTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/RunAndTrackTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/StrongIterableTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ActivationInjectionTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/Bug317183Test.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ComplexDisposalTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextFunctionDynamicsTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionDisposeTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionFactoryTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/DisposingReferencedContextTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ExtraDependenciesTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsInjectionTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsListTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GroupedUpdatesTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectStaticContextTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionOrderTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionUpdateTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InvokeInRATTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectBasic.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSubClass.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSuperClass.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ParentContextDisposalTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/PrintService.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ServiceContextTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/StringPrintService.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestConstructorInjection.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestOtherService.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestService.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceA.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceB.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceController.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/performance/ContextPerformanceTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/AutoConstructWithCreatableTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/DisposeClassLinkTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectArraysTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBaseTypeTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBridgeTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionOrderTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionResultLeakTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTestMissingAnnotation.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RecursiveObjectCreationTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RequestorTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ComponentEnabler.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceA.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceB.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ExtendedSupplierInjectionTests.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceA.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceB.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionEventTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionMixedSuppliersTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiHandlerTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionPreferencesTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionWithoutOSGITest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceA.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceB.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ServiceSupplierTestCase.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/TestService.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ComputedValueLimitationTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ContextExample.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/InjectionErrorReportingTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessages.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessagesRegistry.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/LocaleTransformationTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/MessageRegistryTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/Messages.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/NLSTest.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClassMessages.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass_de.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourcesMessages.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.java delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_OTHER.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_TEST.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages_de.properties delete mode 100644 runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/.gitignore b/runtime/tests/org.eclipse.e4.core.javax.tests/.gitignore deleted file mode 100644 index c59ea270aa5..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/.gitignore +++ /dev/null @@ -1 +0,0 @@ -OSGI-INF/*.xml diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/.project b/runtime/tests/org.eclipse.e4.core.javax.tests/.project index bc2c0c6a994..f3421cb94f8 100644 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/.project +++ b/runtime/tests/org.eclipse.e4.core.javax.tests/.project @@ -20,11 +20,6 @@ - - org.eclipse.pde.ds.core.builder - - - org.eclipse.pde.PluginNature diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/.settings/org.eclipse.pde.ds.annotations.prefs b/runtime/tests/org.eclipse.e4.core.javax.tests/.settings/org.eclipse.pde.ds.annotations.prefs deleted file mode 100644 index 5faf08b7d5c..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/.settings/org.eclipse.pde.ds.annotations.prefs +++ /dev/null @@ -1,7 +0,0 @@ -dsVersion=V1_4 -eclipse.preferences.version=1 -enabled=true -generateBundleActivationPolicyLazy=true -path=OSGI-INF -validationErrorLevel=error -validationErrorLevel.missingImplicitUnbindMethod=error diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/META-INF/MANIFEST.MF b/runtime/tests/org.eclipse.e4.core.javax.tests/META-INF/MANIFEST.MF index 5dc79131b96..1202b59de53 100644 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/META-INF/MANIFEST.MF +++ b/runtime/tests/org.eclipse.e4.core.javax.tests/META-INF/MANIFEST.MF @@ -4,54 +4,15 @@ Bundle-Name: E4 Core Tests Bundle-SymbolicName: org.eclipse.e4.core.javax.tests Bundle-Version: 1.3.300.qualifier Bundle-Vendor: Eclipse.org -Bundle-Activator: org.eclipse.e4.core.internal.tests.CoreTestsActivator -Require-Bundle: org.eclipse.equinox.preferences;bundle-version="3.3.0", - org.eclipse.e4.core.di, - org.eclipse.e4.core.di.extensions, - org.eclipse.e4.core.di.extensions.supplier, - org.eclipse.e4.core.contexts, - org.eclipse.equinox.registry;bundle-version="3.5.0", - org.eclipse.core.tests.harness;bundle-version="3.6.0", - org.eclipse.e4.core.services;bundle-version="1.1.0" +Require-Bundle: org.eclipse.e4.core.di, + org.eclipse.e4.core.contexts Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-17 Import-Package: javax.annotation;version="[1.3.0,2.0.0)", javax.inject;version="[1.0.0,2.0.0)", - org.assertj.core.api, - org.eclipse.osgi.service.datalocation, - org.eclipse.osgi.service.debug, org.junit.jupiter.api;version="[5.12.0,6.0.0)", - org.junit.jupiter.api.function;version="[5.12.0,6.0.0)", - org.junit.platform.suite.api;version="[1.12.0,2.0.0)", - org.osgi.framework;version="[1.10.0,2.0.0)", - org.osgi.service.component;version="1.3.0", - org.osgi.service.component.propertytypes;version="[1.4.0,2.0.0)", - org.osgi.service.event;version="1.3.0", - org.osgi.util.tracker;version="[1.5.0,2.0.0)" -Export-Package: org.eclipse.e4.core.internal.tests;x-internal:=true, - org.eclipse.e4.core.internal.tests.contexts;x-internal:=true, - org.eclipse.e4.core.internal.tests.contexts.inject;x-internal:=true, - org.eclipse.e4.core.internal.tests.contexts.performance;x-internal:=true, - org.eclipse.e4.core.internal.tests.di;x-internal:=true, - org.eclipse.e4.core.internal.tests.di.extensions;x-internal:=true, - org.eclipse.e4.core.internal.tests.manual;x-internal:=true, - org.eclipse.e4.core.internal.tests.nls;x-internal:=true, + org.junit.platform.suite.api;version="[1.12.0,2.0.0)" +Export-Package: org.eclipse.e4.core.internal.tests.contexts.inject;x-internal:=true, org.eclipse.e4.core.javax.tests -Service-Component: OSGI-INF/DisabledServiceA.xml, - OSGI-INF/DisabledServiceB.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.contexts.AddContextFunction.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.contexts.inject.TestServiceA.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.contexts.inject.TestServiceB.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.contexts.inject.TestServiceController.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.di.extensions.ComponentEnabler.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.di.extensions.FilterServiceA.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.di.extensions.FilterServiceB.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.di.extensions.SampleServiceA.xml, - OSGI-INF/org.eclipse.e4.core.internal.tests.di.extensions.SampleServiceB.xml Eclipse-BundleShape: dir -Require-Capability: osgi.extender; - filter:="(&(osgi.extender=osgi.component)(version>=1.3)(!(version>=2.0)))", - osgi.service; - filter:="(objectClass=org.osgi.service.event.EventAdmin)"; - effective:="active" Automatic-Module-Name: org.eclipse.e4.core.javax.tests diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle.properties deleted file mode 100644 index ad0b4b93cb0..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle.properties +++ /dev/null @@ -1,11 +0,0 @@ -message=BundleMessage -message_one=BundleMessageUnderscore -messageOne=BundleMessageCamelCase -message.two=BundleMessageUnderscoreDot -message.three=BundleMessageCamelCaseDot -message.four=The idea is from {0} -messageFive_Sub=BundleMessageCamelCaseAndUnderscoreOriginal -message_six__sub=BundleMessageCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=BundleMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=BundleMessageCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=BundleMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de.properties deleted file mode 100644 index c45cde6d1d7..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de.properties +++ /dev/null @@ -1,10 +0,0 @@ -message=BundleNachricht -message_one=BundleNachrichtUnderscore -messageOne=BundleNachrichtCamelCase -message.two=BundleNachrichtUnderscoreDot -message.four=Die Idee ist von {0} -messageFive_Sub=BundleNachrichtCamelCaseAndUnderscoreOriginal -message_six__sub=BundleNachrichtCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=BundleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=BundleNachrichtCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=BundleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de_ch_test.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de_ch_test.properties deleted file mode 100644 index a0582385afb..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/OSGI-INF/l10n/bundle_de_ch_test.properties +++ /dev/null @@ -1,10 +0,0 @@ -message=BundleNachrichtVariant -message_one=BundleNachrichtUnderscoreVariant -messageOne=BundleNachrichtCamelCaseVariant -message.two=BundleNachrichtUnderscoreDotVariant -message.four=Die Idee ist von {0} Variant -messageFive_Sub=BundleNachrichtCamelCaseAndUnderscoreOriginalVariant -message_six__sub=BundleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedVariant -message.seven..sub=BundleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorifiedVariant -messageEight.Sub=BundleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedVariant -message_nine._sub=BundleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasifiedVariant \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/README.md b/runtime/tests/org.eclipse.e4.core.javax.tests/README.md index c15285d1bfc..26865d412c0 100644 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/README.md +++ b/runtime/tests/org.eclipse.e4.core.javax.tests/README.md @@ -1,2 +1,2 @@ -This is a copy of `org.eclipse.e4.core.tests`, that uses javax annotations while the original uses jakarta annotations now. +This is a reduced minimal copy of `org.eclipse.e4.core.tests`, that verifies javax annotations continue to work, not only the mainly used jakarta annotations. When support for javax annotations is removed from the E4-Injector, this project can be deleted entirely as well. diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/build.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/build.properties index 76d226c77dc..298d235e506 100644 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/build.properties +++ b/runtime/tests/org.eclipse.e4.core.javax.tests/build.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2010, 2015 IBM Corporation and others. +# Copyright (c) 2010, 2026 IBM Corporation and others. # # This program and the accompanying materials # are made available under the terms of the Eclipse Public License 2.0 @@ -17,8 +17,8 @@ output.. = bin/ bin.includes = META-INF/,\ .,\ test.xml,\ - OSGI-INF/,\ - resources/,\ - about.html -src.includes = resources/,\ about.html +src.includes = about.html + +# Maven/Tycho POM adjustments, see https://github.com/eclipse-tycho/tycho/wiki/Tycho-Pomless +pom.model.property.testClass = org.eclipse.e4.core.javax.tests.CoreTestSuite diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/pom.xml b/runtime/tests/org.eclipse.e4.core.javax.tests/pom.xml deleted file mode 100644 index 0016e26e239..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/pom.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - - 4.0.0 - - org.eclipse.platform - eclipse.platform.runtime - 4.40.0-SNAPSHOT - ../.. - - org.eclipse.e4.core.javax.tests - 1.3.300-SNAPSHOT - eclipse-test-plugin - - org.eclipse.e4.core.javax.tests.CoreTestSuite - - - src - - - org.apache.maven.plugins - maven-resources-plugin - - - - copy-ds-component-xml - - copy-resources - - process-test-resources - - - - - ${project.build.outputDirectory}/OSGI-INF - *.xml - - - ${project.basedir}/OSGI-INF - - - - - - - diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/resources/another.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/resources/another.properties deleted file mode 100644 index 86e9d9c7baf..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/resources/another.properties +++ /dev/null @@ -1,11 +0,0 @@ -message=ResourcesMessage -message_one=ResourcesMessageUnderscore -messageOne=ResourcesMessageCamelCase -message.two=ResourcesMessageUnderscoreDot -message.three=ResourcesMessageCamelCaseDot -message.four=The idea is from {0} -messageFive_Sub=ResourcesMessageCamelCaseAndUnderscoreOriginal -message_six__sub=ResourcesMessageCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=ResourcesMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=ResourcesMessageCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=ResourcesMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/resources/another_de.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/resources/another_de.properties deleted file mode 100644 index 7c4441b54a6..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/resources/another_de.properties +++ /dev/null @@ -1,10 +0,0 @@ -message=ResourcesNachricht -message_one=ResourcesNachrichtUnderscore -messageOne=ResourcesNachrichtCamelCase -message.two=ResourcesNachrichtUnderscoreDot -message.four=Die Idee ist von {0} -messageFive_Sub=ResourcesNachrichtCamelCaseAndUnderscoreOriginal -message_six__sub=ResourcesNachrichtCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=ResourcesNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=ResourcesNachrichtCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=ResourcesNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/CoreTestsActivator.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/CoreTestsActivator.java deleted file mode 100644 index 6d0c273f3e1..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/CoreTestsActivator.java +++ /dev/null @@ -1,99 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests; - -import java.util.Dictionary; -import java.util.Hashtable; - -import org.eclipse.core.runtime.preferences.IPreferencesService; -import org.eclipse.e4.core.contexts.IContextFunction; -import org.eclipse.e4.core.internal.tests.contexts.ContextFunctionHigh; -import org.eclipse.e4.core.internal.tests.contexts.ContextFunctionLow; -import org.eclipse.osgi.service.debug.DebugOptions; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.service.event.EventAdmin; -import org.osgi.util.tracker.ServiceTracker; - -public class CoreTestsActivator implements BundleActivator { - - static private CoreTestsActivator defaultInstance; - private BundleContext bundleContext; - private ServiceTracker debugTracker = null; - private ServiceTracker preferencesTracker = null; - private ServiceTracker eventAdminTracker; - - public CoreTestsActivator() { - defaultInstance = this; - } - - public static CoreTestsActivator getDefault() { - return defaultInstance; - } - - @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - registerContextFunctions(); - } - - @Override - public void stop(BundleContext context) throws Exception { - if (preferencesTracker != null) { - preferencesTracker.close(); - preferencesTracker = null; - } - if (debugTracker != null) { - debugTracker.close(); - debugTracker = null; - } - if (eventAdminTracker != null) { - eventAdminTracker.close(); - eventAdminTracker = null; - } - bundleContext = null; - } - - private void registerContextFunctions() { - { - Dictionary properties = new Hashtable<>(); - properties.put(IContextFunction.SERVICE_CONTEXT_KEY,"test.contextfunction.ranking"); - properties.put(Constants.SERVICE_RANKING, 0); - bundleContext.registerService(IContextFunction.SERVICE_NAME, new ContextFunctionLow(), properties); - } - - Dictionary properties = new Hashtable<>(); - properties.put(IContextFunction.SERVICE_CONTEXT_KEY,"test.contextfunction.ranking"); - properties.put(Constants.SERVICE_RANKING, 100); - bundleContext.registerService(IContextFunction.SERVICE_NAME, new ContextFunctionHigh(), properties); - - } - - public BundleContext getBundleContext() { - return bundleContext; - } - - public EventAdmin getEventAdmin() { - if (eventAdminTracker == null) { - if (bundleContext == null) { - return null; - } - eventAdminTracker = new ServiceTracker<>(bundleContext, EventAdmin.class.getName(), null); - eventAdminTracker.open(); - } - return eventAdminTracker.getService(); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ActivationTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ActivationTest.java deleted file mode 100644 index 300b411d4b8..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ActivationTest.java +++ /dev/null @@ -1,332 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.junit.jupiter.api.Test; - -public class ActivationTest { - - static public class TestRAT extends ContextFunction { - @Override - public Object compute(IEclipseContext context, String contextKey) { - IEclipseContext activeContext = context.getActiveLeaf(); - // returns name of the context - return activeContext.get("debugString"); - } - } - - @Test - public void testContextActivation() { - IEclipseContext rootContext = EclipseContextFactory.create("root"); - rootContext.set("testRAT", new TestRAT()); - - IEclipseContext child1 = rootContext.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - IEclipseContext child12 = child1.createChild("child12"); - - IEclipseContext child2 = rootContext.createChild("child2"); - IEclipseContext child21 = child2.createChild("child21"); - IEclipseContext child22 = child2.createChild("child22"); - - assertEquals(rootContext, rootContext.getActiveLeaf()); - assertNull(rootContext.getActiveChild()); - assertEquals("root", rootContext.get("testRAT")); - - child12.activateBranch(); - assertEquals(child12, rootContext.getActiveLeaf()); - assertEquals(child1, rootContext.getActiveChild()); - assertEquals("child12", rootContext.get("testRAT")); - - assertEquals(child2, child2.getActiveLeaf()); - assertNull(child2.getActiveChild()); - assertEquals("child2", child2.get("testRAT")); - - child21.activateBranch(); - assertEquals(child21, rootContext.getActiveLeaf()); - assertEquals(child2, rootContext.getActiveChild()); - assertEquals("child21", rootContext.get("testRAT")); - assertEquals(child12, child1.getActiveLeaf()); - assertEquals(child12, child1.getActiveChild()); - assertEquals("child12", child1.get("testRAT")); - assertEquals(child21, child2.getActiveLeaf()); - assertEquals(child21, child2.getActiveChild()); - assertEquals("child21", child2.get("testRAT")); - - child21.deactivate(); - assertEquals(child2, rootContext.getActiveLeaf()); - assertEquals("child2", rootContext.get("testRAT")); - assertEquals(child12, child1.getActiveLeaf()); - assertEquals("child12", child1.get("testRAT")); - assertEquals(child2, child2.getActiveLeaf()); - assertNull(child2.getActiveChild()); - assertEquals("child2", child2.get("testRAT")); - - child22.activateBranch(); - assertEquals(child22, rootContext.getActiveLeaf()); - assertEquals("child22", rootContext.get("testRAT")); - assertEquals(child12, child1.getActiveLeaf()); - assertEquals("child12", child1.get("testRAT")); - assertEquals(child22, child2.getActiveLeaf()); - assertEquals("child22", child2.get("testRAT")); - - child11.activateBranch(); - assertEquals(child11, rootContext.getActiveLeaf()); - assertEquals("child11", rootContext.get("testRAT")); - assertEquals(child11, child1.getActiveLeaf()); - assertEquals("child11", child1.get("testRAT")); - assertEquals(child22, child2.getActiveLeaf()); - assertEquals("child22", child2.get("testRAT")); - - child11.deactivate(); - assertEquals(child1, rootContext.getActiveLeaf()); - assertEquals("child1", rootContext.get("testRAT")); - assertEquals(child1, child1.getActiveLeaf()); - assertEquals("child1", child1.get("testRAT")); - assertEquals(child22, child2.getActiveLeaf()); - assertEquals("child22", child2.get("testRAT")); - - child1.dispose(); - assertNull(rootContext.getActiveChild()); - child2.activateBranch(); - assertEquals(child22, rootContext.getActiveLeaf()); - assertEquals("child22", rootContext.get("testRAT")); - assertEquals(child22, child2.getActiveLeaf()); - assertEquals("child22", child2.get("testRAT")); - } - - @Test - public void testGetActive() { - IEclipseContext root = EclipseContextFactory.create("root"); - - IEclipseContext child1 = root.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - IEclipseContext child12 = child1.createChild("child12"); - - IEclipseContext child2 = root.createChild("child2"); - IEclipseContext child21 = child2.createChild("child21"); - IEclipseContext child22 = child2.createChild("child22"); - - child11.set("var", "1"); - child12.set("var", "2"); - child1.set("var", "3"); - child21.set("var", "4"); - child22.set("var", "5"); - child2.set("var", "6"); - root.set("var", "7"); - - // nothing is active - we get value from the node - assertEquals("3", child1.getActive("var")); - - child11.activateBranch(); - assertEquals("1", child1.getActive("var")); - child12.activateBranch(); - assertEquals("2", child1.getActive("var")); - child22.activateBranch(); - assertEquals("5", child2.getActive("var")); - } - - @Test - public void testGetActiveBug384425() { - IEclipseContext root = EclipseContextFactory.create("root"); - - IEclipseContext child1 = root.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - - IEclipseContext child2 = root.createChild("child2"); - - // nothing is active - we get value from the node - assertNull(root.getActive("var")); - assertNull(child1.getActive("var")); - assertNull(child2.getActive("var")); - - child11.activateBranch(); - child11.set("var", "1"); - - assertEquals("1", root.getActive("var")); - assertEquals("1", child1.getActive("var")); - assertNull(child2.getActive("var")); - } - - @Test - public void testGetActiveRAT() { - IEclipseContext root = EclipseContextFactory.create("root"); - - IEclipseContext child1 = root.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - IEclipseContext child12 = child1.createChild("child12"); - - IEclipseContext child2 = root.createChild("child2"); - IEclipseContext child21 = child2.createChild("child21"); - IEclipseContext child22 = child2.createChild("child22"); - - child11.set("var", "1"); - child12.set("var", "2"); - child1.set("var", "3"); - child21.set("var", "4"); - child22.set("var", "5"); - child2.set("var", "6"); - root.set("var", "7"); - - final String[] result = new String[1]; - - child1.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - result[0] = (String) context.getActive("var"); - return true; - }}); - - // nothing is active - we get value from the node - assertEquals("3", result[0]); - - child11.activateBranch(); - assertEquals("1", result[0]); - child12.activateBranch(); - assertEquals("2", result[0]); - child22.activateBranch(); - assertEquals("2", result[0]); - } - - @Test - public void testGetActiveRATNumberOfCalls() { - IEclipseContext root = EclipseContextFactory.create("root"); - - IEclipseContext child1 = root.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - IEclipseContext child12 = child1.createChild("child12"); - - IEclipseContext child2 = root.createChild("child2"); - IEclipseContext child21 = child2.createChild("child21"); - IEclipseContext child22 = child2.createChild("child22"); - - child11.set("var", "1"); - child12.set("var", "1"); - child1.set("var", "3"); - child21.set("var", "4"); - child22.set("var", "4"); - child2.set("var", "6"); - root.set("var", "7"); - - final String[] result = new String[1]; - final int[] called = new int[1]; - called[0] = 0; - - child1.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - result[0] = (String) context.getActive("var"); - called[0]++; - return true; - }}); - - // nothing is active - we get value from the node - assertEquals("3", result[0]); - assertEquals(1, called[0]); - - child11.activateBranch(); - assertEquals("1", result[0]); - assertEquals(2, called[0]); - - child12.activateBranch(); - assertEquals("1", result[0]); - assertEquals(3, called[0]); - - child22.activateBranch(); - assertEquals("1", result[0]); - assertEquals(3, called[0]); - - child21.activateBranch(); - assertEquals("1", result[0]); - assertEquals(3, called[0]); - } - - /** - * A variation of {@link #testGetActiveRATNumberOfCalls()} that - * uses distinct values in the leaf contexts. - */ - @Test - public void testGetActiveRATNumberOfCalls2() { - IEclipseContext root = EclipseContextFactory.create("root"); - - IEclipseContext child1 = root.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - IEclipseContext child12 = child1.createChild("child12"); - - IEclipseContext child2 = root.createChild("child2"); - IEclipseContext child21 = child2.createChild("child21"); - IEclipseContext child22 = child2.createChild("child22"); - - child11.set("var", "11"); - child12.set("var", "12"); - child1.set("var", "3"); - child21.set("var", "21"); - child22.set("var", "22"); - child2.set("var", "6"); - root.set("var", "7"); - - final String[] result = new String[1]; - final int[] called = new int[1]; - called[0] = 0; - - child1.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - result[0] = (String) context.getActive("var"); - called[0]++; - return true; - }}); - - // nothing is active - we get value from the node - assertEquals("3", result[0]); - assertEquals(1, called[0]); - - child11.activateBranch(); - assertEquals("11", result[0]); - assertEquals(2, called[0]); - - child12.activateBranch(); - assertEquals("12", result[0]); - assertEquals(3, called[0]); - - child22.activateBranch(); - assertEquals("12", result[0]); - assertEquals(3, called[0]); - - child21.activateBranch(); - assertEquals("12", result[0]); - assertEquals(3, called[0]); - } - - public static class ActiveInject { - //@Inject @Named("var") - public String value; - - @Inject - public void setValue(@Named("var") String value) { - this.value = value; - } - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/AddContextFunction.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/AddContextFunction.java deleted file mode 100644 index 1e10cf4003d..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/AddContextFunction.java +++ /dev/null @@ -1,37 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.IContextFunction; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.osgi.service.component.annotations.Component; - - -/** - * A function provided as a declarative service. See OSGI-INF/adder.xml. - */ -@Component(service = IContextFunction.class, property = IContextFunction.SERVICE_CONTEXT_KEY + "=sum") -public class AddContextFunction extends ContextFunction { - - @Override - public Object compute(IEclipseContext context, String contextKey) { - Integer xInt = (Integer) context.get("x"); - Integer yInt = (Integer) context.get("y"); - int sum = xInt == null ? 0 : xInt.intValue(); - sum += yInt == null ? 0 : yInt.intValue(); - return Integer.valueOf(sum); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextDynamicTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextDynamicTest.java deleted file mode 100644 index be8067c592c..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextDynamicTest.java +++ /dev/null @@ -1,178 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.internal.tests.contexts.inject.ObjectBasic; -import org.junit.jupiter.api.Test; - -/** - * Tests for the basic context functionality - */ -public class ContextDynamicTest { - - - @Test - public void testReplaceFunctionWithStaticValue() { - IEclipseContext parent = EclipseContextFactory.create(); - IEclipseContext context = parent.createChild(); - assertNull(context.getLocal("bar")); - context.set("bar", "baz1"); - context.set("bar", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return "baz1"; - } - }); - parent.set("bar", "baz2"); - assertEquals("baz1", context.get("bar")); - context.set("bar", "baz3"); - assertEquals("baz3", context.get("bar")); - } - - /** - * Tests objects being added and removed from the context - */ - @Test - public synchronized void testAddRemove() { - Integer testInt = Integer.valueOf(123); - String testString = "abc"; - Double testDouble = Double.valueOf(1.23); - Float testFloat = Float.valueOf(12.3f); - Character testChar = Character.valueOf('v'); - - // create original context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Integer.class.getName(), testInt); - context.set(String.class.getName(), testString); - context.set(Double.class.getName(), testDouble); - context.set(Float.class.getName(), testFloat); - context.set(Character.class.getName(), testChar); - - ObjectBasic userObject = new ObjectBasic(); - ContextInjectionFactory.inject(userObject, context); - - // check basic injection - assertEquals(testString, userObject.injectedString); - assertEquals(testInt, userObject.getInt()); - assertEquals(context, userObject.context); - assertEquals(1, userObject.setMethodCalled); - assertEquals(1, userObject.setMethodCalled2); - assertEquals(testDouble, userObject.d); - assertEquals(testFloat, userObject.f); - assertEquals(testChar, userObject.c); - - // change value - Double testDouble2 = Double.valueOf(3.45); - Integer testInt2 = Integer.valueOf(123); - context.set(Double.class.getName(), testDouble2); - context.set(Integer.class.getName(), testInt2); - - // and check - assertEquals(testString, userObject.injectedString); - assertEquals(testInt2, userObject.getInt()); - assertEquals(context, userObject.context); - assertEquals(2, userObject.setMethodCalled); - assertEquals(1, userObject.setMethodCalled2); - assertEquals(testDouble2, userObject.d); - assertEquals(testFloat, userObject.f); - assertEquals(testChar, userObject.c); - - // remove element - context.remove(String.class.getName()); - context.remove(Character.class.getName()); - - // and check - assertNull(userObject.injectedString); - assertEquals(testInt2, userObject.getInt()); - assertEquals(context, userObject.context); - assertEquals(2, userObject.setMethodCalled); - assertEquals(2, userObject.setMethodCalled2); - assertEquals(testDouble2, userObject.d); - assertEquals(testFloat, userObject.f); - assertNull(userObject.c); - } - - /** - * Tests objects being added and removed from the context - */ - @Test - public synchronized void testParentAddRemove() { - Integer testInt = Integer.valueOf(123); - String testString = "abc"; - Double testDouble = Double.valueOf(1.23); - Float testFloat = Float.valueOf(12.3f); - Character testChar = Character.valueOf('v'); - - // create original context - IEclipseContext parentContext = EclipseContextFactory.create(); - parentContext.set(Integer.class.getName(), testInt); - parentContext.set(String.class.getName(), testString); - parentContext.set(Double.class.getName(), testDouble); - parentContext.set(Float.class.getName(), testFloat); - parentContext.set(Character.class.getName(), testChar); - IEclipseContext context = parentContext.createChild(); - - ObjectBasic userObject = new ObjectBasic(); - ContextInjectionFactory.inject(userObject, context); - - // check basic injection - assertEquals(testString, userObject.injectedString); - assertEquals(testInt, userObject.getInt()); - assertEquals(context, userObject.context); - assertEquals(1, userObject.setMethodCalled); - assertEquals(1, userObject.setMethodCalled2); - assertEquals(testDouble, userObject.d); - assertEquals(testFloat, userObject.f); - assertEquals(testChar, userObject.c); - - // change value - Double testDouble2 = Double.valueOf(3.45); - Integer testInt2 = Integer.valueOf(123); - context.set(Double.class.getName(), testDouble2); - context.set(Integer.class.getName(), testInt2); - - // and check - assertEquals(testString, userObject.injectedString); - assertEquals(testInt2, userObject.getInt()); - assertEquals(context, userObject.context); - assertEquals(2, userObject.setMethodCalled); - assertEquals(1, userObject.setMethodCalled2); - assertEquals(testDouble2, userObject.d); - assertEquals(testFloat, userObject.f); - assertEquals(testChar, userObject.c); - - // remove element - parentContext.remove(String.class.getName()); - parentContext.remove(Character.class.getName()); - - // and check - assertNull(userObject.injectedString); - assertEquals(testInt2, userObject.getInt()); - assertEquals(context, userObject.context); - assertEquals(2, userObject.setMethodCalled); - assertEquals(2, userObject.setMethodCalled2); - assertEquals(testDouble2, userObject.d); - assertEquals(testFloat, userObject.f); - assertNull(userObject.c); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionHigh.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionHigh.java deleted file mode 100644 index f229460b3e7..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionHigh.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.eclipse.e4.core.internal.tests.contexts; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.IEclipseContext; - -public class ContextFunctionHigh extends ContextFunction { - @Override - public Object compute(IEclipseContext context) { - return "High"; - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionLow.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionLow.java deleted file mode 100644 index 9e04f359f29..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ContextFunctionLow.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.eclipse.e4.core.internal.tests.contexts; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.IEclipseContext; - -public class ContextFunctionLow extends ContextFunction { - @Override - public Object compute(IEclipseContext context) { - return "Low"; - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/DependenciesLeakTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/DependenciesLeakTest.java deleted file mode 100644 index 111294b9c6d..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/DependenciesLeakTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Stefan Mücke - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.internal.contexts.EclipseContext; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("restriction") -public class DependenciesLeakTest { - - final static String LEGACY_H_ID = "legacy::handler::"; //$NON-NLS-1$ - - static class HandlerSelectionFunction extends ContextFunction { - private final String commandId; - public HandlerSelectionFunction(String commandId) { - this.commandId = commandId; - } - @Override - public Object compute(IEclipseContext context, String contextKey) { - return context.get(LEGACY_H_ID + commandId); - } - @Override - public String toString() { - return "HandlerSelectionFunction [commandId=" + commandId + "]"; - } - } - - private IEclipseContext windowContext; - private IEclipseContext perspectiveContext; - private IEclipseContext partContext; - - @BeforeEach - public void setUp() throws Exception { - windowContext = EclipseContextFactory.create("Window"); - perspectiveContext = windowContext.createChild("Perspective"); - partContext = perspectiveContext.createChild("Part"); - } - - @Test - public void testBug() { - // register a handler - Object handler = ""; - windowContext.set("legacy::handler::foo.bar", handler); // fake activate legacy handler - windowContext.set("foo.bar", new HandlerSelectionFunction("foo.bar")); - - // there may be no listeners initially - assertNoListeners(windowContext); - assertNoListeners(perspectiveContext); - assertNoListeners(partContext); - - // cause a ValueComputation to be created - Object object = partContext.get("foo.bar"); - assertEquals(object, handler); - - // now invalidate the name; this should notify the part context - windowContext.set("foo.bar", null); - //windowContext.remove("foo.bar"); - - // all ValueComputation listeners must have been removed - assertNoListeners(windowContext); - assertNoListeners(perspectiveContext); - assertNoListeners(partContext); - } - - @Test - public void testInvalidateDirectly() { - windowContext.set("x", 42); - windowContext.set("y", 11); - windowContext.set("some.handler", new AddContextFunction()); - assertNoListeners(windowContext); - assertNoListeners(perspectiveContext); - assertNoListeners(partContext); - - Object object = partContext.get("some.handler"); - assertEquals(object, 53); - - windowContext.set("some.handler", null); // invalidate - assertNoListeners(windowContext); - assertNoListeners(perspectiveContext); - assertNoListeners(partContext); - } - - private void assertNoListeners(IEclipseContext context) { - EclipseContext c = (EclipseContext) context; - try { - assertTrue(c.getListeners().isEmpty()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java deleted file mode 100644 index cf44025fa3e..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java +++ /dev/null @@ -1,397 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2021 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.eclipse.e4.core.di.IInjector; -import org.eclipse.e4.core.internal.contexts.EclipseContext; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.osgi.framework.FrameworkUtil; - -@SuppressWarnings("restriction") -public class EclipseContextTest { - - private static class ComputedValueBar extends ContextFunction { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return context.get("bar"); - } - } - - private IEclipseContext context; - private IEclipseContext parentContext; - - private int runCounter; - - @BeforeEach - public void setUp() throws Exception { - parentContext = EclipseContextFactory.create("EclipseContextTest" + "-parent"); - context = parentContext.createChild("EclipseContextTest"); - runCounter = 0; - } - - @Test - public void testContainsKey() { - assertFalse(context.containsKey("function")); - assertFalse(context.containsKey("separator")); - - context.set("separator", ","); - assertTrue(context.containsKey("separator")); - - // null value is still a value - context.set("separator", null); - assertTrue(context.containsKey("separator")); - - context.remove("separator"); - assertFalse(context.containsKey("separator")); - } - - @Test - public void testGet() { - assertNull(context.get("foo")); - context.set("foo", "bar"); - assertEquals("bar", context.get("foo")); - assertNull(parentContext.get("foo")); - context.remove("foo"); - assertNull(context.get("foo")); - parentContext.set("foo", "bar"); - assertEquals("bar", context.get("foo")); - context.set("foo", new ComputedValueBar()); - assertNull(context.get("foo")); - context.set("bar", "baz"); - assertEquals("baz", context.get("foo")); - } - - @Test - public void testGetLocal() { - assertNull(context.getLocal("foo")); - context.set("foo", "bar"); - assertEquals("bar", context.getLocal("foo")); - assertNull(parentContext.getLocal("foo")); - context.remove("foo"); - assertNull(context.getLocal("foo")); - parentContext.set("foo", "bar"); - assertNull(context.getLocal("foo")); - context.set("foo", new ComputedValueBar()); - assertNull(context.getLocal("foo")); - context.set("bar", "baz"); - assertEquals("baz", context.getLocal("foo")); - } - - /** - * Tests that a context no longer looks up values from its parent when disposed. - */ - @Test - public void testDisposeRemovesParentReference() { - assertNull(context.get("foo")); - parentContext.set("foo", "bar"); - assertEquals("bar", context.get("foo")); - context.dispose(); - assertNull(context.get("foo")); - assertFalse(((EclipseContext) parentContext).getChildren().iterator().hasNext()); - } - - @Test - public void testDisposeClearsNotifyOnDisposalSet() { - ((EclipseContext) context).notifyOnDisposal(context -> runCounter++); - context.dispose(); - assertEquals(1, runCounter); - context.dispose(); - assertEquals(1, runCounter); - } - - /** - * Tests handling of a context function defined in the parent that uses values defined in the - * child - */ - @Test - public void testContextFunctionInParent() { - IEclipseContext parent = EclipseContextFactory.create(); - final IEclipseContext child = parent.createChild(); - parent.set("sum", new AddContextFunction()); - parent.set("x", Integer.valueOf(3)); - parent.set("y", Integer.valueOf(3)); - child.set("x", Integer.valueOf(1)); - child.set("y", Integer.valueOf(1)); - assertEquals(6, ((Integer) parent.get("sum")).intValue()); - assertEquals(2, ((Integer) child.get("sum")).intValue()); - child.set("x", Integer.valueOf(5)); - assertEquals(6, ((Integer) parent.get("sum")).intValue()); - assertEquals(6, ((Integer) child.get("sum")).intValue()); - child.remove("x"); - assertEquals(6, ((Integer) parent.get("sum")).intValue()); - assertEquals(4, ((Integer) child.get("sum")).intValue()); - parent.set("x", Integer.valueOf(10)); - assertEquals(13, ((Integer) parent.get("sum")).intValue()); - assertEquals(11, ((Integer) child.get("sum")).intValue()); - } - - @Test - public void testRunAndTrack() { - final Object[] value = new Object[1]; - context.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - runCounter++; - value[0] = context.get("foo"); - return true; - } - }); - assertEquals(1, runCounter); - assertEquals(null, value[0]); - context.set("foo", "bar"); - assertEquals(2, runCounter); - assertEquals("bar", value[0]); - context.remove("foo"); - assertEquals(3, runCounter); - assertEquals(null, value[0]); - context.set("foo", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return context.get("bar"); - } - }); - assertEquals(4, runCounter); - assertEquals(null, value[0]); - context.set("bar", "baz"); - assertEquals(5, runCounter); - assertEquals("baz", value[0]); - context.set("bar", "baf"); - assertEquals(6, runCounter); - assertEquals("baf", value[0]); - context.remove("bar"); - assertEquals(7, runCounter); - assertEquals(null, value[0]); - parentContext.set("bar", "bam"); - assertEquals(8, runCounter); - assertEquals("bam", value[0]); - } - - /** - * Tests registering a single run and track instance multiple times with the same context. - */ - @Test - public void testRegisterRunAndTrackTwice() { - final Object[] value = new Object[1]; - RunAndTrack runnable = new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - runCounter++; - value[0] = context.get("foo"); - return true; - } - }; - context.runAndTrack(runnable); - assertEquals(1, runCounter); - context.runAndTrack(runnable); - assertEquals(2, runCounter); - assertEquals(null, value[0]); - context.set("foo", "bar"); - assertEquals(3, runCounter); - assertEquals("bar", value[0]); - context.remove("foo"); - assertEquals(4, runCounter); - - } - - @Test - public void testRunAndTrackMultipleValues() { - IEclipseContext parent = EclipseContextFactory.create("ParentContext"); - final IEclipseContext child = parent.createChild("ChildContext"); - parent.set("parentValue", "x"); - child.set("childValue", "x"); - RunAndTrack runnable = new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - runCounter++; - if (runCounter < 2) { - child.get("childValue"); - return true; - } - if (runCounter < 3) { - child.get("parentValue"); - return true; - } - return false; - } - }; - child.runAndTrack(runnable); - assertEquals(1, runCounter); - child.set("childValue", "z"); - assertEquals(2, runCounter); - parent.set("parentValue", "z"); - assertEquals(3, runCounter); - } - - @Test - public void testModify() { - IEclipseContext grandParent = EclipseContextFactory.create(); - IEclipseContext parent = grandParent.createChild(); - IEclipseContext child = parent.createChild(); - - child.set("a", "a1"); - parent.set("b", "b2"); - grandParent.set("c", "c3"); - - child.declareModifiable("a"); - parent.declareModifiable("b"); - grandParent.declareModifiable("c"); - - // test pre-conditions - assertNull(grandParent.get("b")); - assertEquals("b2", parent.get("b")); - assertEquals("b2", child.get("b")); - assertNull(child.getLocal("b")); - - // modify value on the middle node via its child - child.modify("b", "abc"); - - assertFalse(grandParent.containsKey("b")); - assertEquals("abc", parent.get("b")); - assertEquals("abc", child.get("b")); - assertNull(child.getLocal("b")); - - // modifying non-exist values adds it to the context - child.modify("d", "123"); - - assertFalse(grandParent.containsKey("d")); - assertFalse(parent.containsKey("d")); - assertNull(parent.get("d")); - assertEquals("123", child.get("d")); - - // edge conditions: modify value in the top node - grandParent.modify("c", "cNew"); - assertTrue(grandParent.containsKey("c")); - assertEquals("cNew", grandParent.get("c")); - assertNull(parent.getLocal("c")); - assertNull(child.getLocal("c")); - assertTrue(child.containsKey("c")); - - // edge condition: modify value in the leaf node - child.modify("a", "aNew"); - assertTrue(child.containsKey("a")); - assertFalse(parent.containsKey("a")); - assertFalse(grandParent.containsKey("a")); - assertEquals("aNew", child.get("a")); - assertNull(parent.get("a")); - - // test access rules - child.set("aNo", "a1"); - parent.set("bNo", "b2"); - grandParent.set("cNo", "c3"); - - boolean exception = false; - try { - child.modify("bNo", "new"); - } catch (IllegalArgumentException e) { - exception = true; - } - assertTrue(exception); - - exception = false; - try { - grandParent.modify("cNo", "new"); - } catch (IllegalArgumentException e) { - exception = true; - } - assertTrue(exception); - - exception = false; - try { - child.modify("aNo", "new"); - } catch (IllegalArgumentException e) { - exception = true; - } - assertTrue(exception); - } - - @Test - public void testRemoveValueComputationOnDispose() { - IEclipseContext parent = EclipseContextFactory.create("ParentContext"); - IEclipseContext child = parent.createChild("ChildContext"); - parent.set("x", Integer.valueOf(1)); - parent.set("y", Integer.valueOf(1)); - parent.set("sum", new AddContextFunction()); - - child.get("sum"); - assertEquals(1, listenersCount(child)); - child.dispose(); - assertEquals(0, listenersCount(parent)); - } - - @Test - public void testNullInheritance() { - IEclipseContext parent = EclipseContextFactory.create("ParentContext"); - IEclipseContext child = parent.createChild("ChildContext"); - parent.set("x", Integer.valueOf(1)); - child.set("x", null); - assertNull(child.get("x")); - } - - @Test - public void testGetCFNotAValue() { - IEclipseContext context = EclipseContextFactory.create("ParentContext"); - context.set("x", new ContextFunction() { - - @Override - public Object compute(IEclipseContext context, String contextKey) { - return IInjector.NOT_A_VALUE; - } - }); - - // must call several times as the underlying ValueComputation wrapper is - // created on the first time, but re-used for subsequent calls. - assertNull(context.get("x")); - assertNull(context.get("x")); - assertNull(context.get("x")); - context.dispose(); - } - - @Test - public void testGetCFNotAValueToParent() { - IEclipseContext parent = EclipseContextFactory.create("ParentContext"); - IEclipseContext child = parent.createChild(); - parent.set("x", Integer.valueOf(1)); - child.set("x", new ContextFunction() { - - @Override - public Object compute(IEclipseContext context, String contextKey) { - return IInjector.NOT_A_VALUE; - } - }); - - assertEquals(1, child.get("x")); - parent.dispose(); - } - - @Test - public void testContextFunctionOrdering() { - IEclipseContext osgiContext = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()); - assertEquals("High",osgiContext.get("test.contextfunction.ranking")); - } - - private int listenersCount(IEclipseContext context) { - return ((EclipseContext) context).getListeners().size(); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/NeutralValueTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/NeutralValueTest.java deleted file mode 100644 index 0e78e8797df..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/NeutralValueTest.java +++ /dev/null @@ -1,206 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2021 Joerg Kubitz. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Joerg Kubitz - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.HashSet; -import java.util.Set; - -import org.eclipse.e4.core.internal.contexts.ConcurrentNeutralValueMap; -import org.eclipse.e4.core.internal.contexts.ConcurrentNeutralValueMap.Value; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("restriction") -public class NeutralValueTest { - - @Test - public void testConcurrentNeutralValueMap() { - ConcurrentNeutralValueMap map = new ConcurrentNeutralValueMap<>(Double.NaN); - map.put("nix", null); // modify - map.put("2", 2.0); // modify - map.put("3", 3.0); // modify - map.put("4", 4.0); // modify - map.put("garnix", null); // modify - assertTrue(map.containsKey("garnix")); - map.remove("garnix"); - - assertTrue(map.containsKey("nix")); - assertTrue(map.containsKey("2")); - assertFalse(map.containsKey("1")); - assertFalse(map.containsKey("garnix")); - - assertEquals(4, map.size()); - - assertFalse(map.isEmpty()); - - assertEquals(null, map.get("nix")); - assertEquals(null, map.get("1")); - assertEquals(Double.valueOf(2.0), map.get("2")); - assertEquals(Double.valueOf(3.0), map.get("3")); - assertEquals(Double.valueOf(4.0), map.get("4")); - - Set keys = new HashSet<>(); - Set values = new HashSet<>(); - map.forEach((k, v) -> keys.add(k)); - map.forEach((k, v) -> values.add(v)); - assertEquals(Set.of("nix", "2", "3", "4"), keys); - assertTrue(values.contains(null)); - assertTrue(values.contains(2.0)); - - assertTrue(map.getValue("nix").isPresent()); - assertFalse(map.getValue("1").isPresent()); - assertTrue(map.getValue("2").isPresent()); - assertTrue(map.getValue("3").isPresent()); - assertTrue(map.getValue("4").isPresent()); - - { - Value v = map.getValue("nix"); - assertTrue(v.isPresent()); - assertEquals(null, v.unwrapped()); - } - { - Value v = map.getValue("1"); - assertFalse(v.isPresent()); - assertEquals(null, v.unwrapped()); - } - { - Value v = map.getValue("2"); - assertTrue(v.isPresent()); - assertEquals(Double.valueOf(2.0), v.unwrapped()); - } - - { - Value v = map.putAndGetOld("5", 5555.0); // modify - assertFalse(v.isPresent()); - assertEquals(null, v.unwrapped()); - assertEquals(Double.valueOf(5555.0), map.get("5")); - } - { - Value v = map.putAndGetOld("5", 5.0); // modify - assertTrue(v.isPresent()); - assertEquals(Double.valueOf(5555.0), v.unwrapped()); - assertEquals(Double.valueOf(5.0), map.get("5")); - } - map.putIfAbsent("5", 5555.0); // modify - assertEquals(Double.valueOf(5.0), map.get("5")); - map.remove("5"); // modify - assertFalse(map.containsKey("5")); - - { - Value v = map.putAndGetOld("five", null); // modify - assertFalse(v.isPresent()); - assertEquals(null, v.unwrapped()); - assertEquals(null, map.get("five")); - } - { - Value v = map.putAndGetOld("five", 5.0); // modify - assertTrue(v.isPresent()); - assertEquals(null, v.unwrapped()); - assertEquals(Double.valueOf(5.0), map.get("five")); - } - { - map.putIfAbsent("five", null); // modify - Value v = map.getValue("five"); - assertTrue(v.isPresent()); - assertEquals(Double.valueOf(5.0), v.unwrapped()); - } - map.remove("five"); // modify - { - map.putIfAbsent("five", null); // modify - Value v = map.getValue("five"); - assertTrue(v.isPresent()); - assertEquals(null, v.unwrapped()); - } - map.remove("five"); // modify - assertFalse(map.containsKey("five")); - - map.clear(); // modify - assertEquals(0, map.size()); - assertTrue(map.isEmpty()); - } - - @Test - public void testToString() { - ConcurrentNeutralValueMap map1 = new ConcurrentNeutralValueMap<>(); - map1.put("0", 0f); - map1.put("NULL", null); - map1.put("nothing", null); - map1.put("1", 1f); - map1.put("~2", 2.1f); - assertTrue(map1.toString().contains("0=0.0")); - assertTrue(map1.toString().contains("1=1.0")); - assertTrue(map1.toString().contains("~2=2.1")); - assertTrue(map1.toString().contains("NULL=null")); - assertTrue(map1.toString().contains("nothing=null")); - } - - @Test - public void testCustomToString() { - ConcurrentNeutralValueMap map1 = new ConcurrentNeutralValueMap<>(Float.NaN); - map1.put("0", 0f); - map1.put("NULL", null); - assertTrue(map1.toString().contains("0=0.0")); - assertTrue(map1.toString().contains("NULL=NaN")); - } - - @Test - public void testEquals() { - { - ConcurrentNeutralValueMap map1 = new ConcurrentNeutralValueMap<>(); - ConcurrentNeutralValueMap map2 = new ConcurrentNeutralValueMap<>(); - map1.put("0", 0f); - map1.put("NULL", null); - map2.put("NULL", null); - map2.put("0", 0f); - assertEquals(map1.hashCode(), map2.hashCode()); - assertEquals(map1, map2); - } - { - ConcurrentNeutralValueMap map1 = new ConcurrentNeutralValueMap<>(); - ConcurrentNeutralValueMap map2 = new ConcurrentNeutralValueMap<>(); - map1.put("0", 0f); - map1.put("1", 1f); - map1.put("NULL", null); - map2.put("NULL", null); - map2.put("0", 0f); - assertNotEquals(map1.hashCode(), map2.hashCode()); - assertNotEquals(map1, map2); - } - { - ConcurrentNeutralValueMap map1 = new ConcurrentNeutralValueMap<>(Float.NaN); - ConcurrentNeutralValueMap map2 = new ConcurrentNeutralValueMap<>(Float.NaN); - map1.put("0", 0f); - map1.put("NULL", null); - map2.put("NULL", null); - map2.put("0", 0f); - assertEquals(map1.hashCode(), map2.hashCode()); - assertEquals(map1, map2); - } - { - ConcurrentNeutralValueMap map1 = new ConcurrentNeutralValueMap<>(Float.NaN); - ConcurrentNeutralValueMap map2 = new ConcurrentNeutralValueMap<>(Float.NaN); - map1.put("0", 0f); - map1.put("1", 1f); - map1.put("NULL", null); - map2.put("NULL", null); - map2.put("0", 0f); - assertNotEquals(map1.hashCode(), map2.hashCode()); - assertNotEquals(map1, map2); - } - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ReparentingTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ReparentingTest.java deleted file mode 100644 index 763a6241706..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/ReparentingTest.java +++ /dev/null @@ -1,387 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.eclipse.e4.core.internal.tests.contexts.inject.ObjectSuperClass; -import org.junit.jupiter.api.Test; - -/** - * Test for changing a context's parent. - */ -public class ReparentingTest { - - - /** - * Tests handling of a context function defined in the parent when the parent is changed to no - * longer have the function. - */ - @Test - public void testContextFunctionInParentRemove() { - IEclipseContext parent = EclipseContextFactory.create("parent"); - final IEclipseContext child = parent.createChild("child"); - parent.set("sum", new AddContextFunction()); - parent.set("x", Integer.valueOf(3)); - parent.set("y", Integer.valueOf(3)); - child.set("x", Integer.valueOf(1)); - child.set("y", Integer.valueOf(1)); - assertEquals(6, ((Integer) parent.get("sum")).intValue()); - assertEquals(2, ((Integer) child.get("sum")).intValue()); - child.setParent(EclipseContextFactory.create()); - assertEquals(6, ((Integer) parent.get("sum")).intValue()); - assertNull(child.get("sum"), "Expected null but was: " + child.get("sum")); - } - - /** - * Tests handling of a context function defined in the parent when the parent is changed to have - * the function - */ - @Test - public void testContextFunctionInParentAdd() { - // setup - IEclipseContext parent = EclipseContextFactory.create(); - final IEclipseContext child = parent.createChild(); - child.set("x", Integer.valueOf(1)); - child.set("y", Integer.valueOf(1)); - assertEquals(null, parent.get("sum")); - assertEquals(null, child.get("sum")); - - // switch parent - IEclipseContext newParent = EclipseContextFactory.create(); - child.setParent(newParent); - newParent.set("sum", new AddContextFunction()); - assertEquals(0, ((Integer) newParent.get("sum")).intValue()); - assertEquals(2, ((Integer) child.get("sum")).intValue()); - - // changed values in parent shouldn't affect child - newParent.set("x", Integer.valueOf(3)); - newParent.set("y", Integer.valueOf(3)); - assertEquals(6, ((Integer) newParent.get("sum")).intValue()); - assertEquals(2, ((Integer) child.get("sum")).intValue()); - } - - @Test - public void testContextFunctionNullBecomeParent() { - final IEclipseContext child = EclipseContextFactory.create(); - child.set("sum", new AddContextFunction()); - assertEquals(0, ((Integer) child.get("sum")).intValue()); - IEclipseContext parent = EclipseContextFactory.create(); - parent.set("x", Integer.valueOf(3)); - parent.set("y", Integer.valueOf(3)); - child.setParent(parent); - assertEquals(6, ((Integer) child.get("sum")).intValue()); - - } - - @Test - public void testContextFunctionParentBecomeNull() { - IEclipseContext parent = EclipseContextFactory.create(); - final IEclipseContext child = parent.createChild(); - parent.set("x", Integer.valueOf(3)); - parent.set("y", Integer.valueOf(3)); - child.set("sum", new AddContextFunction()); - assertEquals(6, ((Integer) child.get("sum")).intValue()); - child.setParent(null); - assertEquals(0, ((Integer) child.get("sum")).intValue()); - } - - @Test - public void testContextFunctionSwitchParent() { - IEclipseContext parent = EclipseContextFactory.create(); - final IEclipseContext child = parent.createChild(); - parent.set("x", Integer.valueOf(3)); - parent.set("y", Integer.valueOf(3)); - child.set("sum", new AddContextFunction()); - assertEquals(6, ((Integer) child.get("sum")).intValue()); - IEclipseContext newParent = EclipseContextFactory.create(); - newParent.set("x", Integer.valueOf(1)); - newParent.set("y", Integer.valueOf(1)); - child.setParent(newParent); - assertEquals(2, ((Integer) child.get("sum")).intValue()); - } - - /** - * Tests a child switching from a null parent to a non-null parent. - */ - @Test - public void testRunAndTrackNullBecomesParent() { - final String[] value = new String[1]; - final IEclipseContext child = EclipseContextFactory.create(); - child.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - value[0] = (String) child.get("x"); - return true; - } - }); - assertEquals(null, value[0]); - IEclipseContext parent = EclipseContextFactory.create(); - parent.set("x", "newParent"); - child.setParent(parent); - assertEquals("newParent", value[0]); - } - - /** - * Tests a child switching from a non-null parent to a null parent. - */ - @Test - public void testRunAndTrackParentBecomeNull() { - final String[] value = new String[1]; - IEclipseContext parent = EclipseContextFactory.create(); - final IEclipseContext child = parent.createChild(); - parent.set("x", "oldParent"); - child.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - value[0] = (String) child.get("x"); - return true; - } - }); - assertEquals("oldParent", value[0]); - child.setParent(null); - assertNull(value[0]); - } - - @Test - public void testRunAndTrackSwitchParent() { - final String[] value = new String[1]; - IEclipseContext parent = EclipseContextFactory.create(); - final IEclipseContext child = parent.createChild(); - parent.set("x", "oldParent"); - child.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - value[0] = (String) child.get("x"); - return true; - } - }); - assertEquals("oldParent", value[0]); - IEclipseContext newParent = EclipseContextFactory.create(); - newParent.set("x", "newParent"); - child.setParent(newParent); - assertEquals("newParent", value[0]); - } - - /** - * Tests an object consuming simple values from a parent context, and a parent change causes a - * change in simple values. TODO: Still fails - */ - @Test - public void testInjectSwitchParent() { - - IEclipseContext oldParent = EclipseContextFactory.create(); - oldParent.set("String", "oldField"); - oldParent.set(String.class.getName(), "old"); - oldParent.set(Float.class.getName(), Float.valueOf(12.3f)); - IEclipseContext newParent = EclipseContextFactory.create(); - newParent.set("String", "newField"); - newParent.set(String.class.getName(), "new"); - newParent.set(Float.class.getName(), Float.valueOf(34.5f)); - IEclipseContext child = oldParent.createChild(); - - ObjectSuperClass object = new ObjectSuperClass(); - ContextInjectionFactory.inject(object, child); - assertEquals(1, object.setStringCalled); - assertEquals("old", object.getStringViaMethod()); - - child.setParent(newParent); - assertEquals("new", object.getStringViaMethod()); - assertEquals(2, object.setStringCalled); - - } - - /** - * Tests an object consuming services from a grandparent. A parent switch where the grandparent - * stays unchanged should ideally not cause changes for the injected object. - */ - @Test - public void testInjectSwitchParentSameGrandparent() { - IEclipseContext grandpa = EclipseContextFactory.create(); - grandpa.set("String", "field"); - grandpa.set(String.class.getName(), "s"); - grandpa.set(Float.class.getName(), Float.valueOf(12.3f)); - - IEclipseContext oldParent = grandpa.createChild(); - IEclipseContext newParent = grandpa.createChild(); - IEclipseContext child = oldParent.createChild(); - - ObjectSuperClass object = new ObjectSuperClass(); - ContextInjectionFactory.inject(object, child); - assertEquals(1, object.setStringCalled); - - child.setParent(newParent); - assertEquals(1, object.setStringCalled); - } - - @Test - public void testUpdateSameParent() { - final Boolean[] called = new Boolean[1] ; - IEclipseContext parent = EclipseContextFactory.create("parent"); - IEclipseContext newParent = EclipseContextFactory.create("newParent"); - IEclipseContext child = parent.createChild("child"); - parent.set("x", "1"); - newParent.set("x", "2"); - - child.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - called[0] = true; - context.get("x"); // creates a link - return true; - } - }); - called[0] = false; - - // make sure setting parent to the same value does not trigger updates - child.setParent(parent); - assertFalse(called[0]); - - child.setParent(newParent); - assertTrue(called[0]); - } - - static public class TestService { - // empty - } - - @Test - public void testUpdateSameParentCalculated() { - final int[] testServiceCount = new int[1]; - testServiceCount[0] = 0; - IEclipseContext parentContext = EclipseContextFactory.create("parent"); - parentContext.set(TestService.class.getName(), new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - testServiceCount[0]++; - return ContextInjectionFactory.make(TestService.class, context); - } - }); - - IEclipseContext childContext = parentContext.createChild("child"); - childContext.get(TestService.class); - assertEquals(1, testServiceCount[0]); - - childContext.setParent(childContext.getParent()); - assertEquals(1, testServiceCount[0]); - } - - @Test - public void testBug468048_contextFunction() { - IEclipseContext p1 = EclipseContextFactory.create("parent1"); - p1.set("sample", new ContextFunction() { - @Override - public Object compute(IEclipseContext context) { - return Integer.valueOf(1); - } - }); - - IEclipseContext p2 = EclipseContextFactory.create("parent2"); - p2.set("sample", new ContextFunction() { - @Override - public Object compute(IEclipseContext context) { - return Integer.valueOf(2); - } - }); - - final IEclipseContext intermed = p1.createChild("intermed"); - - final IEclipseContext leaf = intermed.createChild("leaf"); - assertEquals(Integer.valueOf(1), leaf.get("sample")); - intermed.setParent(p2); - assertEquals(Integer.valueOf(2), leaf.get("sample")); - } - - @Test - public void testBug468048_injection() { - IEclipseContext p1 = EclipseContextFactory.create("parent1"); - p1.set("sample", new ContextFunction() { - @Override - public Object compute(IEclipseContext context) { - return Integer.valueOf(1); - } - }); - - IEclipseContext p2 = EclipseContextFactory.create("parent2"); - p2.set("sample", new ContextFunction() { - @Override - public Object compute(IEclipseContext context) { - return Integer.valueOf(2); - } - }); - - final IEclipseContext intermed = p1.createChild("intermed"); - - final IEclipseContext leaf = intermed.createChild("leaf"); - Bug468048 b = ContextInjectionFactory.make(Bug468048.class, leaf); - - assertEquals(Integer.valueOf(1), b.sample); - intermed.setParent(p2); - assertEquals(Integer.valueOf(2), b.sample); - } - - @Test - public void testContextFunctionSwitchParent_2() { - IEclipseContext superParent = EclipseContextFactory.create("root"); - - IEclipseContext parent = superParent.createChild("parent-1"); - final IEclipseContext child = parent.createChild("child-1"); - child.set("x", Integer.valueOf(3)); - child.set("y", Integer.valueOf(3)); - - superParent.set("sum", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - if (context != child) { - throw new IllegalStateException("Invalid context state"); - } - return (Integer) context.get("x") + (Integer) context.get("y"); - } - }); - - Bug541498 bug = ContextInjectionFactory.make(Bug541498.class, child); - assertEquals(Integer.valueOf(6), bug.value); - - IEclipseContext newParent = superParent.createChild("parent-2"); - child.setParent(newParent); - - assertEquals(Integer.valueOf(6), bug.value); - } - - public static class Bug541498 { - @Inject - @Named("sum") - Integer value; - } - - public static class Bug468048 { - @Inject - @Named("sample") - public Integer sample; - - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/RunAndTrackTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/RunAndTrackTest.java deleted file mode 100644 index 8c34e328a9f..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/RunAndTrackTest.java +++ /dev/null @@ -1,430 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link org.eclipse.e4.core.contexts.RunAndTrack}. - */ -public class RunAndTrackTest { - - private static final class TestRAT extends RunAndTrack { - - - private final String varName; - private Object varValue; - private int calls = 0; - - public TestRAT(String varName) { - this.varName = varName; - } - - @Override - public boolean changed(IEclipseContext context) { - ++calls; - varValue = context.get(varName); - return true; - } - - public int getCalls() { - return calls; - } - - public Object getVarValue() { - return varValue; - } - - public void resetCalls() { - calls = 0; - } - - } - - private static class ActivePartLookupFunction extends ContextFunction { - - @Override - public Object compute(IEclipseContext context, String contextKey) { - IEclipseContext childContext = (IEclipseContext) context.getLocal(ACTIVE_CHILD); - if (childContext != null) { - return childContext.get(ACTIVE_PART); - } - return context.get(INTERNAL_LOCAL_PART); - } - - } - - static final String ACTIVE_CHILD = "activeChild"; - - static final String ACTIVE_PART = "activePart"; - - static final String ACTIVE_PART_ID = "activePartId"; - - static final String INTERNAL_LOCAL_PART = "localPart"; - - private final List createdContexts = new ArrayList<>(); - - private IEclipseContext createContext(IEclipseContext parentContext, String level) { - IEclipseContext childContext = parentContext.createChild(level); - createdContexts.add(childContext); - return childContext; - } - - private IEclipseContext getGlobalContext() { - IEclipseContext serviceContext = EclipseContextFactory - .getServiceContext(CoreTestsActivator.getDefault().getBundleContext()); - // global initialization and setup, usually done by workbench - IEclipseContext appContext = createContext(serviceContext, "globalContext"); - - appContext.set("globalContext", appContext); - - return appContext; - } - - private IEclipseContext[] createNextLevel(IEclipseContext parent, String prefix, int num) { - assertTrue(num > 0); - IEclipseContext[] contexts = new IEclipseContext[num]; - for (int i = 0; i < num; i++) { - contexts[i] = createContext(parent, prefix + i); - contexts[i].set(INTERNAL_LOCAL_PART, prefix + i); - } - parent.set(ACTIVE_CHILD, contexts[0]); - return contexts; - } - - @AfterEach - public void tearDown() throws Exception { - for (IEclipseContext context : createdContexts) { - context.dispose(); - } - createdContexts.clear(); - } - - @Test - public void testActiveChain() throws Exception { - final IEclipseContext workbenchContext = getGlobalContext(); - workbenchContext.set("activePart", new ActivePartLookupFunction()); - final IEclipseContext[] windows = createNextLevel(workbenchContext, "window", 1); - createNextLevel(windows[0], "part", 2); - assertEquals("part0", workbenchContext.get(ACTIVE_PART)); - } - - @Test - public void testActiveChange() throws Exception { - final IEclipseContext workbenchContext = getGlobalContext(); - workbenchContext.set("activePart", new ActivePartLookupFunction()); - final IEclipseContext[] windows = createNextLevel(workbenchContext, "window", 1); - final IEclipseContext[] parts = createNextLevel(windows[0], "part", 2); - assertEquals("part0", workbenchContext.get(ACTIVE_PART)); - - windows[0].set(ACTIVE_CHILD, parts[1]); - assertEquals("part1", workbenchContext.get(ACTIVE_PART)); - } - - /** - * There was a failing scenario in the legacy workbench support. This captures the hierarchy and - * function (without any workbench level references). It should be updated when we figure out - * the failing scenario :-) - */ - @Test - public void testRunAndTrackComplex() throws Exception { - final IEclipseContext workbenchContext = getGlobalContext(); - workbenchContext.set("activePart", new ActivePartLookupFunction()); - final IEclipseContext[] windows = createNextLevel(workbenchContext, "window", 1); - windows[0].runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - final Object part = windows[0].get(ACTIVE_PART); - windows[0].set(ACTIVE_PART_ID, part); - return true; - } - - @Override - public String toString() { - return ACTIVE_PART_ID; - } - }); - - final IEclipseContext[] mainSashes = createNextLevel(windows[0], "mainSash", 2); - createNextLevel(mainSashes[1], "editorArea", 1); - final IEclipseContext[] viewSashes = createNextLevel(mainSashes[0], "viewSashes", 2); - - // create package explorer stack - final IEclipseContext[] packageStack = createNextLevel(viewSashes[0], "packageStack", 1); - final IEclipseContext[] packageViews = createNextLevel(packageStack[0], "packageViews", 3); - assertNotNull(packageViews); - assertEquals("packageViews0", windows[0].get(ACTIVE_PART)); - assertEquals("packageViews0", windows[0].get(ACTIVE_PART_ID)); - - // create problems stack - final IEclipseContext[] problemsStack = createNextLevel(viewSashes[1], "problemsStack", 1); - final IEclipseContext[] problemsViews = createNextLevel(problemsStack[0], "problemViews", 5); - assertNotNull(problemsViews); - assertEquals("packageViews0", windows[0].get(ACTIVE_PART)); - assertEquals("packageViews0", windows[0].get(ACTIVE_PART_ID)); - - assertEquals("problemViews0", problemsStack[0].get(ACTIVE_PART)); - // this won't change since it is a "runAndTrack" at the window context - // level - assertEquals("packageViews0", problemsStack[0].get(ACTIVE_PART_ID)); - - // set the "problems view" active, propagating the information up - // the active chain. - problemsStack[0].set(ACTIVE_CHILD, problemsViews[0]); - viewSashes[1].set(ACTIVE_CHILD, problemsStack[0]); - mainSashes[0].set(ACTIVE_CHILD, viewSashes[1]); - windows[0].set(ACTIVE_CHILD, mainSashes[0]); - workbenchContext.set(ACTIVE_CHILD, windows[0]); - - assertEquals("problemViews0", windows[0].get(ACTIVE_PART)); - assertEquals("problemViews0", windows[0].get(ACTIVE_PART_ID)); - - assertEquals("packageViews0", packageStack[0].get(ACTIVE_PART)); - assertEquals("problemViews0", packageStack[0].get(ACTIVE_PART_ID)); - } - - @Test - public void testRunAndTrackSimple() throws Exception { - final IEclipseContext workbenchContext = getGlobalContext(); - workbenchContext.set("activePart", new ActivePartLookupFunction()); - final IEclipseContext[] windows = createNextLevel(workbenchContext, "window", 1); - windows[0].runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - final Object part = windows[0].get(ACTIVE_PART); - windows[0].set(ACTIVE_PART_ID, part); - return true; - } - - @Override - public String toString() { - return ACTIVE_PART_ID; - } - }); - - final IEclipseContext[] parts = createNextLevel(windows[0], "part", 2); - assertEquals("part0", workbenchContext.get(ACTIVE_PART)); - assertEquals("part0", windows[0].get(ACTIVE_PART_ID)); - - windows[0].set(ACTIVE_CHILD, parts[1]); - assertEquals("part1", windows[0].get(ACTIVE_PART)); - assertEquals("part1", windows[0].get(ACTIVE_PART_ID)); - } - - /** - * Test how a RAT responds to a change hidden from it; changed value is == to child value - */ - @Test - public void testSetHiddenValueToChildObject() { - final String newRootValue = "child"; - - doHiddenValueChangeTest(newRootValue); - } - - /** - * Test how a RAT responds to a change hidden from it; changed value is != to child value - */ - @Test - public void testSetHiddenValueToDifferentObject() { - final String newRootValue = "other"; - - doHiddenValueChangeTest(newRootValue); - } - - /** - * Test how a RAT responds to a change hidden from it; changed value is != to child value (but is .equals()) - */ - @Test - public void testSetHiddenValueToObjectEqualToChild() { - // avoid compiler's pushing all my strings into a single string pool - final String newRootValue = new String("child"); - - doHiddenValueChangeTest(newRootValue); - } - - /** - * Test how a RAT responds to a change hidden from it; changed value is == to root value - */ - @Test - public void testSetHiddenValueToRootObject() { - final String newRootValue = "root"; - - doHiddenValueChangeTest(newRootValue); - } - - /** - * Test how a RAT responds to a change hidden from it; changed value is != to root value (but is .equals()) - */ - @Test - public void testSetHiddenValueToEqualRootObject() { - // avoid compiler's pushing all my strings into a single string pool - final String newRootValue = new String("root"); - - doHiddenValueChangeTest(newRootValue); - } - - /** - * Test how a RAT responds to a change hidden from it; changed value is == to root value - */ - @Test - public void testSetHiddenValueToNull() { - final String newRootValue = null; - - doHiddenValueChangeTest(newRootValue); - } - - /** - * Perform a hidden value test that verifies that the test RAT does not run, and - * that has last seen the initial value in the child context (namely "child"). - * @param newRootValue the new value for the variable 'v' in the root context. - * @see #doHiddenValueChangeTest(ITestAction, Object, int) - */ - void doHiddenValueChangeTest(final String newRootValue) { - doHiddenValueChangeTest((IEclipseContext root, String var) -> { - root.set(var, newRootValue); - }, "child", 0); - } - - /** - * Interface defining function - */ - private interface ITestAction { - - void execute(IEclipseContext root, String var); - - } - /** - * Create a two level hierarchy of contexts, each defining a variable 'v' with values 'root' and 'child', respectively. - * Create and install a RAT on the child context that is dependent on 'v'. - * Run testAction. - * Tests whether the RAT ran the expected number of times, - * and tests last value of 'v' that the RAT saw. - * @param testAction the context action to perform as part of the test - * @param expectedValue the expected last value of variable 'v' that the RAT saw. - * @param expectedRATCalls the expected number of times the RAT was run in response to testAction - */ - void doHiddenValueChangeTest(ITestAction testAction, Object expectedValue, int expectedRATCalls) { - final IEclipseContext root = getGlobalContext(); - final IEclipseContext child = root.createChild("child"); - - root.set("v", "root"); - child.set("v", "child"); - final TestRAT testRAT = new TestRAT("v"); - - // install the RAT - child.runAndTrack(testRAT); - assertEquals("child", testRAT.getVarValue()); - assertEquals(1, testRAT.getCalls()); - - testRAT.resetCalls(); - // set the new root value - testAction.execute(root, "v"); - assertEquals(expectedValue, testRAT.getVarValue()); - assertEquals(expectedRATCalls, testRAT.getCalls()); - } - - /** - * Test that a variable change in a context hidden from a RAT in - * a child context does not re-run the RAT. - */ - @Test - public void testRemoveHiddenVariable() { - doHiddenValueChangeTest((IEclipseContext root, String var) -> { - root.remove(var); - }, "child", 0); - } - - /** - * Test that setting a context variable to it's existing - * value does not re-run dependent RATs - */ - @Test - public void testSetContextVarToSameObject() { - doSingleContextChangeTest((IEclipseContext root, String var) -> { - root.set(var, "root"); - }, "root", 0); - } - - /** - * Test that setting a context variable to a value that {@link Object#equals(Object) equals} - * the current value, but is same object DOES re-run dependent RATs. - */ - @Test - public void testSetContextVarToEqualObject() { - doSingleContextChangeTest((IEclipseContext root, String var) -> { - root.set(var, new String("root")); - }, "root", 1); - } - - /** - * Test that setting a context variable to a different object, not equal to the - * current value re-runs dependent RATs. - */ - @Test - public void testSetContextVarToOtherObject() { - doSingleContextChangeTest((IEclipseContext root, String var) -> { - root.set(var, "other"); - }, "other", 1); - } - - /** - * Test that removing a context variable re-runs dependent RATs. - */ - @Test - public void testRemoveContextVar() { - doSingleContextChangeTest(IEclipseContext::remove, null, 1); - - } - - /** - * Creates a context, sets a variable 'v' to "root", creates a RAT dependent on 'v' in the context, - * then executes testAction and tests whether the RAT ran the expected number of times, - * and tests last value of 'v' that the RAT saw. - * @param testAction the context action to perform as part of the test - * @param expectedValue the expected last value of variable 'v' that the RAT saw. - * @param expectedRATCalls the expected number of times the RAT was run in response to testAction - */ - private void doSingleContextChangeTest(ITestAction testAction, Object expectedValue, int expectedRATCalls) { - final IEclipseContext root = getGlobalContext(); - - root.set("v", "root"); - - final TestRAT testRAT = new TestRAT("v"); - // install the RAT - root.runAndTrack(testRAT); - assertEquals("root", testRAT.getVarValue()); - assertEquals(1, testRAT.getCalls()); - - testRAT.resetCalls(); - testAction.execute(root, "v"); - assertEquals(expectedRATCalls, testRAT.getCalls()); - assertEquals(expectedValue, testRAT.getVarValue()); - - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/StrongIterableTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/StrongIterableTest.java deleted file mode 100644 index 33019cebbc5..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/StrongIterableTest.java +++ /dev/null @@ -1,273 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2021 Joerg Kubitz. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Joerg Kubitz - initial API and implementation - *******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; -import java.util.Collection; -import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.concurrent.ConcurrentLinkedDeque; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.function.Supplier; -import java.util.stream.StreamSupport; - -import org.eclipse.e4.core.internal.contexts.StrongIterable; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("restriction") -public class StrongIterableTest { - - @Test - public void testStrongIterableDeque() { - testStrongIterable(ConcurrentLinkedDeque::new); - } - - @Test - public void testStrongIterableQueue() { - testStrongIterable(ConcurrentLinkedQueue::new); - } - - void testStrongIterable(Supplier>> constructor) { - testIterate(constructor.get()); - testRemoveWhileIterate(constructor.get()); - testRemoveAll(constructor.get()); - testRemoveByClear(constructor.get()); - testNextWithoutHasNext(constructor.get()); - } - - private void testNextWithoutHasNext(Collection> iterable) { - WeakReference EMPTY1 = new WeakReference<>(null); - WeakReference EMPTY2 = new WeakReference<>(null); - WeakReference ONE = new WeakReference<>(1); - WeakReference TWO = new WeakReference<>(2); - WeakReference THREE = new WeakReference<>(3); - iterable.add(EMPTY1); // ignored - iterable.add(ONE); - iterable.add(TWO); - iterable.add(EMPTY2); // ignored - iterable.add(THREE); - StrongIterable strongIterable = new StrongIterable<>(iterable); - { - Iterator i = strongIterable.iterator(); - assertEquals(1, i.next().intValue()); - assertEquals(2, i.next().intValue()); - assertEquals(3, i.next().intValue()); - try { - i.next(); - } catch (NoSuchElementException e) { - assertNotNull(e); - } - } - { - Iterator i = strongIterable.iterator(); - assertEquals(1, i.next().intValue()); - assertEquals(3L, count(strongIterable)); - assertEquals(3L, poorMansCount(strongIterable)); - i.remove(); - assertEquals(2L, count(strongIterable)); - assertEquals(2L, poorMansCount(strongIterable)); - assertEquals(2, i.next().intValue()); - i.remove(); - assertEquals(3, i.next().intValue()); - i.remove(); - try { - i.next(); - } catch (NoSuchElementException e) { - assertNotNull(e); - } - } - assertEquals(0L, count(strongIterable)); - assertEquals(0L, poorMansCount(strongIterable)); - } - - void testRemoveWhileIterate(Collection> iterable) { - WeakReference ONE = new WeakReference<>(1); - WeakReference TWO = new WeakReference<>(2); - WeakReference THREE = new WeakReference<>(3); - iterable.add(ONE); - iterable.add(TWO); - iterable.add(THREE); - - assertEquals(3L, poorMansCount(iterable)); - StrongIterable strongIterable = new StrongIterable<>(iterable); - { - Iterator i = strongIterable.iterator(); - assertEquals(3L, count(strongIterable)); - assertEquals(3L, poorMansCount(strongIterable)); - - assertTrue(i.hasNext()); - assertEquals(1, i.next().intValue()); - assertTrue(i.hasNext()); - assertEquals(2, i.next().intValue()); - i.remove(); // remove TWO - assertEquals(2L, count(strongIterable)); - assertEquals(2L, poorMansCount(strongIterable)); - assertTrue(i.hasNext()); - assertEquals(3, i.next().intValue()); - i.remove(); // remove THREE - assertEquals(1L, count(strongIterable)); - assertEquals(1L, poorMansCount(strongIterable)); - assertFalse(i.hasNext()); - try { - i.next(); - } catch (NoSuchElementException e) { - assertNotNull(e); - } - } - } - - void testRemoveAll(Collection> iterable) { - WeakReference ONE = new WeakReference<>(1); - WeakReference TWO = new WeakReference<>(2); - WeakReference THREE = new WeakReference<>(3); - iterable.add(ONE); - iterable.add(TWO); - iterable.add(THREE); - - StrongIterable strongIterable = new StrongIterable<>(iterable); - assertEquals(3L, count(strongIterable)); - for (Iterator i = strongIterable.iterator(); i.hasNext();) { - i.next(); - i.remove(); - } - assertEquals(0L, count(strongIterable)); - assertEquals(0L, poorMansCount(strongIterable)); - } - - void testRemoveByClear(Collection> iterable) { - WeakReference ONE = new WeakReference<>(1); - WeakReference TWO = new WeakReference<>(2); - WeakReference THREE = new WeakReference<>(3); - iterable.add(ONE); - iterable.add(TWO); - iterable.add(THREE); - - StrongIterable strongIterable = new StrongIterable<>(iterable); - assertEquals(3L, count(strongIterable)); - for (Reference ref : iterable) { - ref.clear(); - } - assertEquals(0L, count(strongIterable)); - } - - void testIterate(Collection> iterable) { - WeakReference EMPTY1 = new WeakReference<>(null); - WeakReference EMPTY2 = new WeakReference<>(null); - WeakReference ONE = new WeakReference<>(1); - WeakReference TWO = new WeakReference<>(2); - WeakReference THREE = new WeakReference<>(3); - iterable.add(EMPTY1); // ignored - iterable.add(ONE); - iterable.add(TWO); - iterable.add(EMPTY2); // ignored - iterable.add(THREE); - - StrongIterable strongIterable = new StrongIterable<>(iterable); - { - Iterator i = strongIterable.iterator(); - assertEquals(3L, count(strongIterable)); - assertTrue(i.hasNext()); - assertEquals(1, i.next().intValue()); - assertTrue(i.hasNext()); - assertEquals(2, i.next().intValue()); - assertTrue(i.hasNext()); - assertEquals(3, i.next().intValue()); - assertFalse(i.hasNext()); - try { - i.next(); - } catch (NoSuchElementException e) { - assertNotNull(e); - } - } - for (Integer strong : strongIterable) { - assertNotNull(strong); - } - - assertTrue(iterable.remove(TWO)); // remove - - for (Integer strong : strongIterable) { - assertNotNull(strong); - } - { - Iterator i = strongIterable.iterator(); - assertEquals(2L, count(strongIterable)); - assertTrue(i.hasNext()); - assertEquals(1, i.next().intValue()); - assertTrue(i.hasNext()); - assertEquals(3, i.next().intValue()); - assertFalse(i.hasNext()); - try { - i.next(); - } catch (NoSuchElementException e) { - assertNotNull(e); - } - } - - ONE.clear(); // remove on iterate - - { - Iterator i = strongIterable.iterator(); - assertEquals(1L, count(strongIterable)); - assertTrue(i.hasNext()); - assertEquals(3, i.next().intValue()); - assertFalse(i.hasNext()); - try { - i.next(); - } catch (NoSuchElementException e) { - assertNotNull(e); - } - for (Integer strong : strongIterable) { - assertNotNull(strong); - } - } - - THREE.clear(); // remove on iterate - - { - Iterator i = strongIterable.iterator(); - assertEquals(0L, count(strongIterable)); - assertFalse(i.hasNext()); - try { - i.next(); - } catch (NoSuchElementException e) { - assertNotNull(e); - } - for (Integer strong : strongIterable) { - assertNotNull(strong); - } - } - } - - private long count(Iterable i) { - return StreamSupport.stream(i.spliterator(), false).count(); - } - - private long poorMansCount(Iterable i) { - long count = -1; - try { - for (Iterator it = i.iterator();; it.next()) { - count++; - } - } catch (NoSuchElementException expected) { - } - return count; - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ActivationInjectionTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ActivationInjectionTest.java deleted file mode 100644 index 63b4e5fafec..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ActivationInjectionTest.java +++ /dev/null @@ -1,221 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.Active; -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.Test; - -public class ActivationInjectionTest { - - static public class TestRAT extends ContextFunction { - @Override - public Object compute(IEclipseContext context, String contextKey) { - IEclipseContext activeContext = context.getActiveLeaf(); - // returns name of the context - return "_" + activeContext.get("debugString") + "_"; - } - } - - static public class TestObject { - - public String name; - - @Inject - public TestObject() { - // - } - - @Inject - public void setActiveContextName(@Named("testRAT") String name) { - this.name = name; - } - } - - @Test - public void testContextActivation() { - IEclipseContext rootContext = EclipseContextFactory.create("root"); - rootContext.set("testRAT", new TestRAT()); - - IEclipseContext child1 = rootContext.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - IEclipseContext child12 = child1.createChild("child12"); - - IEclipseContext child2 = rootContext.createChild("child2"); - IEclipseContext child21 = child2.createChild("child21"); - IEclipseContext child22 = child2.createChild("child22"); - - TestObject testObject = ContextInjectionFactory.make(TestObject.class, rootContext); - - child12.activateBranch(); - assertEquals(child12, rootContext.getActiveLeaf()); - assertEquals("_child12_", testObject.name); - - child21.activateBranch(); - assertEquals(child21, rootContext.getActiveLeaf()); - assertEquals("_child21_", testObject.name); - - child21.deactivate(); - assertEquals(child2, rootContext.getActiveLeaf()); - assertEquals("_child2_", testObject.name); - - child22.activateBranch(); - assertEquals(child22, rootContext.getActiveLeaf()); - assertEquals("_child22_", testObject.name); - - child11.activateBranch(); - assertEquals(child11, rootContext.getActiveLeaf()); - assertEquals("_child11_", testObject.name); - - child11.deactivate(); - assertEquals(child1, rootContext.getActiveLeaf()); - assertEquals("_child1_", testObject.name); - - child1.dispose(); - child2.activateBranch(); - assertEquals(child22, rootContext.getActiveLeaf()); - assertEquals("_child22_", testObject.name); - } - - static public class TestObjectInject { - public String name; - @Inject - public TestObjectInject() { - // - } - @Inject - public void setActiveContextName(@Active @Named("debugString") String name) { - this.name = "_" + name + "_"; - } - } - - @Test - public void testActivationInjection() { - IEclipseContext rootContext = EclipseContextFactory.create("root"); - - IEclipseContext child1 = rootContext.createChild("child1"); - IEclipseContext child11 = child1.createChild("child11"); - IEclipseContext child12 = child1.createChild("child12"); - - IEclipseContext child2 = rootContext.createChild("child2"); - IEclipseContext child21 = child2.createChild("child21"); - IEclipseContext child22 = child2.createChild("child22"); - - TestObjectInject testObject = ContextInjectionFactory.make(TestObjectInject.class, rootContext); - - child12.activateBranch(); - assertEquals(child12, rootContext.getActiveLeaf()); - assertEquals("_child12_", testObject.name); - - child21.activateBranch(); - assertEquals(child21, rootContext.getActiveLeaf()); - assertEquals("_child21_", testObject.name); - - child21.deactivate(); - assertEquals(child2, rootContext.getActiveLeaf()); - assertEquals("_child2_", testObject.name); - - child22.activateBranch(); - assertEquals(child22, rootContext.getActiveLeaf()); - assertEquals("_child22_", testObject.name); - - child11.activateBranch(); - assertEquals(child11, rootContext.getActiveLeaf()); - assertEquals("_child11_", testObject.name); - - child11.deactivate(); - assertEquals(child1, rootContext.getActiveLeaf()); - assertEquals("_child1_", testObject.name); - - child1.dispose(); - child2.activateBranch(); - assertEquals(child22, rootContext.getActiveLeaf()); - assertEquals("_child22_", testObject.name); - } - - static public class TestInject { - @Optional @Inject @Active - public Integer number; - } - - @Test - public void testInjection() { - IEclipseContext rootContext = EclipseContextFactory.create("root"); - - IEclipseContext child1 = rootContext.createChild("child1"); - child1.set(Integer.class, Integer.valueOf(1)); - IEclipseContext child11 = child1.createChild("child11"); - child11.set(Integer.class, Integer.valueOf(2)); - IEclipseContext child12 = child1.createChild("child12"); - child12.set(Integer.class, Integer.valueOf(3)); - - IEclipseContext child2 = rootContext.createChild("child2"); - child2.set(Integer.class, Integer.valueOf(4)); - IEclipseContext child21 = child2.createChild("child21"); - child21.set(Integer.class, Integer.valueOf(5)); - IEclipseContext child22 = child2.createChild("child22"); - child22.set(Integer.class, Integer.valueOf(6)); - - TestInject testObjectRoot = ContextInjectionFactory.make(TestInject.class, rootContext); - TestInject testObjectChild1 = ContextInjectionFactory.make(TestInject.class, child1); - TestInject testObjectChild2 = ContextInjectionFactory.make(TestInject.class, child2); - - child12.activateBranch(); - assertEquals(Integer.valueOf(3), testObjectRoot.number); - assertEquals(Integer.valueOf(3), testObjectChild1.number); - assertEquals(Integer.valueOf(4), testObjectChild2.number); - - child21.activateBranch(); - assertEquals(Integer.valueOf(5), testObjectRoot.number); - assertEquals(Integer.valueOf(3), testObjectChild1.number); - assertEquals(Integer.valueOf(5), testObjectChild2.number); - - child21.deactivate(); - assertEquals(Integer.valueOf(4), testObjectRoot.number); - assertEquals(Integer.valueOf(3), testObjectChild1.number); - assertEquals(Integer.valueOf(4), testObjectChild2.number); - - child22.activateBranch(); - assertEquals(Integer.valueOf(6), testObjectRoot.number); - assertEquals(Integer.valueOf(3), testObjectChild1.number); - assertEquals(Integer.valueOf(6), testObjectChild2.number); - - child11.activateBranch(); - assertEquals(Integer.valueOf(2), testObjectRoot.number); - assertEquals(Integer.valueOf(2), testObjectChild1.number); - assertEquals(Integer.valueOf(6), testObjectChild2.number); - - child11.deactivate(); - assertEquals(Integer.valueOf(1), testObjectRoot.number); - assertEquals(Integer.valueOf(1), testObjectChild1.number); - assertEquals(Integer.valueOf(6), testObjectChild2.number); - - child1.dispose(); - child2.activateBranch(); - assertEquals(Integer.valueOf(6), testObjectRoot.number); - assertEquals(Integer.valueOf(6), testObjectChild2.number); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java index bc90c9172dd..c9e2ce141c2 100644 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java +++ b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. + * Copyright (c) 2009, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -16,13 +16,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Singleton; import org.eclipse.e4.core.contexts.ContextInjectionFactory; import org.eclipse.e4.core.contexts.EclipseContextFactory; @@ -30,12 +33,12 @@ import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.core.di.annotations.Optional; import org.junit.jupiter.api.Test; + /** * Tests for the basic context injection functionality */ public class AnnotationsInjectionTest { - @Test public void testContextSetOneArg() { class TestData { @@ -109,6 +112,58 @@ public void setData(TestData arg) { assertEquals(methodValue2, object.value); } + static class ObjectBasic { + // Injected directly + @Inject + @Optional + public String injectedString; + @Inject + private Integer injectedInteger; + + // Injected indirectly + public Double d; + public Float f; + public Character c; + public IEclipseContext context; + + // Test status + public boolean finalized = false; + public boolean disposed = false; + public int setMethodCalled = 0; + public int setMethodCalled2 = 0; + + @Inject + public void objectViaMethod(Double d) { + setMethodCalled++; + this.d = d; + } + + @Inject + public void arguments(Float f, @Optional Character c) { + setMethodCalled2++; + this.f = f; + this.c = c; + } + + @PostConstruct + public void postCreate(IEclipseContext context) { + this.context = context; + finalized = true; + } + + @PreDestroy + public void dispose(IEclipseContext context) { + if (this.context != context) + throw new IllegalArgumentException("Unexpected context"); + this.context = null; + disposed = true; + } + + public Integer getInt() { + return injectedInteger; + } + } + /** * Tests basic context injection */ @@ -157,7 +212,8 @@ class TestData { // empty } class Injected { - @Inject @Named("valueField") + @Inject + @Named("valueField") Object injectedField; Object methodValue; @@ -198,6 +254,33 @@ public void injectedMethod(@Optional @Named("valueMethod") Object arg) { } } + static class OptionalAnnotations { + @Inject + @Optional + public Float f = null; + + public Double d; + public String s = "ouch"; + public Integer i; + + public int methodOptionalCalled = 0; + public int methodRequiredCalled = 0; + + @Inject + @Optional + public void methodOptional(Double d) { + this.d = d; + methodOptionalCalled++; + } + + @Inject + public void methodRequired(@Optional String s, Integer i) { + this.s = s; + this.i = i; + methodRequiredCalled++; + } + } + @Test public void testOptionalInjection() { Integer testInt = Integer.valueOf(123); @@ -259,37 +342,6 @@ public String something(@Optional String param) { assertEquals(2, testObject.called); } - /** - * Tests that a class with multiple inherited post-construct / pre-destroy methods. - */ - @Test - public void testInheritedSpecialMethods() { - IEclipseContext context = EclipseContextFactory.create(); - context.set(Integer.class, Integer.valueOf(123)); - context.set(String.class, "abc"); - context.set(Float.class, Float.valueOf(12.3f)); - - ObjectSubClass userObject = new ObjectSubClass(); - ContextInjectionFactory.inject(userObject, context); - assertEquals(1, userObject.superPostConstructCount); - assertEquals(1, userObject.subPostConstructCount); - assertEquals(0, userObject.superPreDestroyCount); - assertEquals(0, userObject.subPreDestroyCount); - assertEquals(0, userObject.overriddenPreDestroyCount); - - context.set(Float.class, Float.valueOf(45.6f)); - assertEquals(1, userObject.superPostConstructCount); - assertEquals(1, userObject.subPostConstructCount); - assertEquals(0, userObject.superPreDestroyCount); - assertEquals(0, userObject.subPreDestroyCount); - assertEquals(0, userObject.overriddenPreDestroyCount); - - context.dispose(); - assertEquals(1, userObject.superPreDestroyCount); - assertEquals(1, userObject.subPreDestroyCount); - assertEquals(1, userObject.overriddenPreDestroyCount); - } - @Test public void testInvoke() { class TestData { @@ -362,4 +414,24 @@ public void setData(TestData arg) { assertNotNull(object.value); assertNotNull(object.directFieldInjection); } + + @Singleton + public static class SingletonService { + } + + public static class NonSingletonService { + } + + @Test + public void testSingleton() { + IEclipseContext context = EclipseContextFactory.create(); + + SingletonService service1 = ContextInjectionFactory.make(SingletonService.class, context); + SingletonService service2 = ContextInjectionFactory.make(SingletonService.class, context); + assertSame(service1, service2); + + NonSingletonService service3 = ContextInjectionFactory.make(NonSingletonService.class, context); + NonSingletonService service4 = ContextInjectionFactory.make(NonSingletonService.class, context); + assertNotSame(service3, service4); + } } diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/Bug317183Test.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/Bug317183Test.java deleted file mode 100644 index dc530e86b10..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/Bug317183Test.java +++ /dev/null @@ -1,82 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.junit.jupiter.api.Test; - -public class Bug317183Test { - - @Test - public void testX() { - IEclipseContext appContext = EclipseContextFactory.create(); - IEclipseContext windowContext = appContext.createChild(); - IEclipseContext partContextA = windowContext.createChild(); - IEclipseContext partContextB = windowContext.createChild(); - - partContextA.activateBranch(); - - RunAndTrackImpl impl = new RunAndTrackImpl(); - windowContext.runAndTrack(impl); - - impl.called = false; - - partContextA.dispose(); - partContextB.activate(); - assertTrue(impl.called); // this fails - } - - @Test - public void testY() { - IEclipseContext appContext = EclipseContextFactory.create(); - IEclipseContext windowContext = appContext.createChild(); - IEclipseContext partContextA = windowContext.createChild(); - IEclipseContext partContextB = windowContext.createChild(); - IEclipseContext partContextC = windowContext.createChild(); - - partContextA.activateBranch(); - - RunAndTrackImpl impl = new RunAndTrackImpl(); - windowContext.runAndTrack(impl); - - partContextB.activate(); - partContextA.dispose(); - - impl.called = false; - - partContextC.activate(); - assertTrue(impl.called); // this fails - } - - static class RunAndTrackImpl extends RunAndTrack { - - boolean called = false; - - @Override - public boolean changed(IEclipseContext context) { - context.getActiveLeaf(); - called = true; - return true; - } - - } - -} - diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ComplexDisposalTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ComplexDisposalTest.java deleted file mode 100644 index b3ecb38efdc..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ComplexDisposalTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -/** - * See bug 296337: duplicate disposal of an object - */ -public class ComplexDisposalTest { - - public static class MyTest { - private int count = 0; - - @Inject - @Named("aString") - String string; - - public int getCount() { - return count; - } - - @PreDestroy - void preDestroy() { - count++; - } - } - - public static class TestFunction extends ContextFunction { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return ContextInjectionFactory.make(MyTest.class, context); - } - } - - @Test - public void testU() { - IEclipseContext parentContext = EclipseContextFactory.create(); - parentContext.set("aString", ""); - parentContext.set(MyTest.class.getName(), new TestFunction()); - IEclipseContext context = parentContext.createChild(); - - MyTest test = context.get(MyTest.class); - - assertEquals(0, test.getCount()); - context.dispose(); - assertEquals(1, test.getCount(), "Context disposed, @PreDestroy should've been called"); - parentContext.dispose(); - assertEquals(1, test.getCount(), "Parent context disposed, @PreDestroy should not have been called again"); - } - - @Test - public void testV() { - IEclipseContext parentContext = EclipseContextFactory.create(); - parentContext.set("aString", ""); - IEclipseContext context = parentContext.createChild(); - - MyTest test = ContextInjectionFactory.make(MyTest.class, context); - - assertEquals(0, test.getCount()); - context.dispose(); - assertEquals(1, test.getCount(), "Context disposed, @PreDestroy should've been called"); - parentContext.dispose(); - assertEquals(1, test.getCount(), "Parent context disposed, @PreDestroy should not have been called again"); - } - - @Test - public void testW() { - IEclipseContext parentContext = EclipseContextFactory.create(); - parentContext.set("aString", ""); - IEclipseContext context = parentContext.createChild(); - - MyTest test = new MyTest(); - ContextInjectionFactory.inject(test, context); - - assertEquals(0, test.getCount()); - context.dispose(); - assertEquals(1, test.getCount(), "Context disposed, @PreDestroy should've been called"); - parentContext.dispose(); - assertEquals(1, test.getCount(), "Parent context disposed, @PreDestroy should not have been called again"); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextFunctionDynamicsTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextFunctionDynamicsTest.java deleted file mode 100644 index dc2bc3b5a26..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextFunctionDynamicsTest.java +++ /dev/null @@ -1,181 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.Test; - -public class ContextFunctionDynamicsTest { - - private static final String SELECTION = "selection"; //$NON-NLS-1$ - - static class InjectTarget { - Object input; - - @Inject - @Optional - void setInput(@Named(SELECTION) Object input) { - this.input = input; - } - } - - /** - * Changing context function should update injected values - */ - @Test - public void testChangeICF() { - IEclipseContext context1 = EclipseContextFactory.create("context1"); - IEclipseContext context2 = context1.createChild("context2"); - - context1.set(SELECTION, new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return "func1"; - } - }); - - InjectTarget target = new InjectTarget(); - ContextInjectionFactory.inject(target, context2); - - assertEquals("func1", target.input); - - context1.set(SELECTION, new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return "func2"; - } - }); - - assertEquals("func2", target.input); - } - - /** - * Overriding context function with a regular value on a child node - */ - @Test - public void testOverrideICF() { - IEclipseContext context1 = EclipseContextFactory.create("context1"); - IEclipseContext context2 = context1.createChild("context2"); - IEclipseContext context3 = context2.createChild("context3"); - - context1.set(SELECTION, new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return "func1"; - } - }); - - InjectTarget target = new InjectTarget(); - ContextInjectionFactory.inject(target, context3); - - assertEquals("func1", target.input); - - Object o = new Object(); - context2.set(SELECTION, o); - - assertEquals(o, target.input); - } - - /** - * Tests updates in a chain of 4 contexts - */ - @Test - public void testLongChain() { - IEclipseContext context1 = EclipseContextFactory.create("context1"); - IEclipseContext context2 = context1.createChild("context2"); - IEclipseContext context3 = context2.createChild("context3"); - IEclipseContext context4 = context3.createChild("context4"); - - // ICF set on top context - context1.set(SELECTION, new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return "func1"; - } - }); - - InjectTarget target = new InjectTarget(); - ContextInjectionFactory.inject(target, context4); - - assertEquals("func1", target.input); - - // Override ICF set on the 2nd context - context2.set(SELECTION, new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return "func2"; - } - }); - - assertEquals("func2", target.input); - - // Override removed - context2.remove(SELECTION); - assertEquals("func1", target.input); - - // Override simple value set on 3rd context - context3.set(SELECTION, "abc"); - assertEquals("abc", target.input); - - // Simple value override removed from 3rd context - context3.remove(SELECTION); - assertEquals("func1", target.input); - } - - - - @Test - public void testBug315109() throws Exception { - IEclipseContext appContext = EclipseContextFactory.create(); - IEclipseContext windowContext = appContext.createChild(); - IEclipseContext partContext = windowContext.createChild(); - - partContext.activateBranch(); - - appContext.set(SELECTION, new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - IEclipseContext parent = context.getParent(); - while (parent != null) { - context = parent; - parent = context.getParent(); - } - return context.getActiveLeaf().get("out.selection"); - } - }); - - InjectTarget target = new InjectTarget(); - ContextInjectionFactory.inject(target, partContext); - - assertNull(target.input, "No selection has been set, should be null"); //$NON-NLS-1$ - - Object o = new Object(); - windowContext.set(SELECTION, o); - - assertEquals( - o, target.input, - "A selection was set into the window, should have been injected into the part"); //$NON-NLS-1$ - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionDisposeTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionDisposeTest.java deleted file mode 100644 index 23712a9c275..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionDisposeTest.java +++ /dev/null @@ -1,138 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.Test; - -/** - * Tests for injection handling of context dispose, and handling disposal of - * injected objects. - */ -public class ContextInjectionDisposeTest { - - @Test - public void testContextDisposedNoArg() { - class Injected { - - boolean disposeInvoked = false; - - @SuppressWarnings("unused") - @Inject - private String Field; - - @PreDestroy - public void dispose() { - disposeInvoked = true; - } - } - - IEclipseContext context = EclipseContextFactory.create(); - context.set(String.class, "hello"); - Injected object = new Injected(); - ContextInjectionFactory.inject(object, context); - context.dispose(); - assertTrue(object.disposeInvoked); - } - - @Test - public void testDisposeContext() { - class Injected { - boolean disposeInvoked = false; - - @Inject - Object Field; - String methodValue; - - @PreDestroy - public void dispose() { - disposeInvoked = true; - } - - @Inject - public void InjectedMethod(String arg) { - methodValue = arg; - } - } - IEclipseContext context = EclipseContextFactory.create(); - Object fieldValue = new Object(); - Object methodValue = "abc"; - context.set(Object.class, fieldValue); - context.set(String.class.getName(), methodValue); - Injected object = new Injected(); - ContextInjectionFactory.inject(object, context); - - assertEquals(fieldValue, object.Field); - assertEquals(methodValue, object.methodValue); - - // disposing context calls @PreDestory, but does not clear injected - // values - context.dispose(); - assertNotNull(object.Field); - assertNotNull(object.methodValue); - assertTrue(object.disposeInvoked); - } - - @Test - public void testReleaseObject() { - class Injected { - boolean disposeInvoked = false; - - @Inject - Integer Field; - Object methodValue; - - @PreDestroy - public void dispose() { - disposeInvoked = true; - } - - @Inject - public void InjectedMethod(@Optional String arg) { - methodValue = arg; - } - } - IEclipseContext context = EclipseContextFactory.create(); - Integer fieldValue = Integer.valueOf(123); - String methodValue = "abc"; - context.set(Integer.class, fieldValue); - context.set(String.class, methodValue); - Injected object = new Injected(); - ContextInjectionFactory.inject(object, context); - - assertEquals(fieldValue, object.Field); - assertEquals(methodValue, object.methodValue); - - // releasing should have the same effect on the single object as - // disposing the context does. - ContextInjectionFactory.uninject(object, context); - - assertEquals(fieldValue, object.Field); - assertNull(object.methodValue); - assertTrue(object.disposeInvoked); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionFactoryTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionFactoryTest.java deleted file mode 100644 index b84c78039ba..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionFactoryTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.CanExecute; -import org.eclipse.e4.core.di.annotations.Execute; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class ContextInjectionFactoryTest { - - static class TestObject { - - private int executed = 0; - - private int executedWithParams = 0; - - @Execute - public void execute() { - executed++; - } - - @CanExecute - public void executeWithParams(String string) { - executedWithParams++; - } - - public int getExecuted() { - return executed; - } - - public int getExecutedWithParams() { - return executedWithParams; - } - - } - - static class TestConstructorObjectBasic { - public boolean defaultConstructorCalled = false; - - public TestConstructorObjectBasic() { - defaultConstructorCalled = true; - } - } - - private TestObject testObject; - private IEclipseContext context; - - @BeforeEach - public void setUp() throws Exception { - testObject = new TestObject(); - context = EclipseContextFactory.create(); - } - - @Test - public void testInvoke() throws Exception { - ContextInjectionFactory.invoke(testObject, Execute.class, context, null); - - assertEquals(1, testObject.getExecuted()); - assertEquals(0, testObject.getExecutedWithParams()); - } - - @Test - public void testInvokeWithParameters() throws Exception { - context.set(String.class.getName(), ""); - - ContextInjectionFactory.invoke(testObject, CanExecute.class, context, null); - - assertEquals(0, testObject.getExecuted()); - assertEquals(1, testObject.getExecutedWithParams()); - } - - /** - * If no other constructors are available, the default constructor should be used - */ - @Test - public void testConstructorInjectionBasic() { - IEclipseContext context = EclipseContextFactory.create(); - // add an extra argument for the inner class constructors - context.set(ContextInjectionFactoryTest.class.getName(), this); - - Object basicResult = ContextInjectionFactory - .make(TestConstructorObjectBasic.class, context); - assertNotNull(basicResult); - assertTrue(basicResult instanceof TestConstructorObjectBasic); - assertTrue(((TestConstructorObjectBasic) basicResult).defaultConstructorCalled); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionTest.java deleted file mode 100644 index cbf0c1e0cfc..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ContextInjectionTest.java +++ /dev/null @@ -1,325 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - -import javax.annotation.PostConstruct; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.junit.jupiter.api.Test; - -/** - * Tests for the basic context injection functionality - */ -public class ContextInjectionTest { - - - /** - * Test trivial method injection and finalize method with context as an argument - */ - @Test - public void testContextSetOneArg() { - class TestData { - } - class Injected { - int contextSetCalled = 0; - int setMethodCalled = 0; - - public TestData value; - - @Inject - public void contextSet(IEclipseContext context) { - contextSetCalled++; - } - - @Inject - public void InjectedMethod(TestData arg) { - setMethodCalled++; - value = arg; - } - } - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set(TestData.class.getName(), methodValue); - Injected object = new Injected(); - ContextInjectionFactory.inject(object, context); - assertEquals(1, object.setMethodCalled); - assertEquals(1, object.contextSetCalled); - - TestData methodValue2 = new TestData(); - context.set(TestData.class.getName(), methodValue2); - assertEquals(2, object.setMethodCalled); - assertEquals(methodValue2, object.value); - assertEquals(1, object.contextSetCalled); - } - - /** - * Test filnalize method - no args - */ - @Test - public void testContextSetZeroArgs() { - class TestData { - } - class Injected { - int contextSetCalled = 0; - int setMethodCalled = 0; - - public TestData value; - - @Inject - public void contextSet() { - contextSetCalled++; - } - - @Inject - public void InjectedMethod(TestData arg) { - setMethodCalled++; - value = arg; - } - } - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set(TestData.class.getName(), methodValue); - Injected object = new Injected(); - ContextInjectionFactory.inject(object, context); - assertEquals(1, object.setMethodCalled); - assertEquals(1, object.contextSetCalled); - - TestData methodValue2 = new TestData(); - context.set(TestData.class.getName(), methodValue2); - assertEquals(2, object.setMethodCalled); - assertEquals(methodValue2, object.value); - assertEquals(1, object.contextSetCalled); - } - - /** - * Tests basic context injection - */ - @Test - public synchronized void testInjection() { - Integer testInt = Integer.valueOf(123); - String testString = "abc"; - Double testDouble = Double.valueOf(1.23); - Float testFloat = Float.valueOf(12.3f); - Character testChar = Character.valueOf('v'); - - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Integer.class, testInt); - context.set(String.class, testString); - context.set(Double.class, testDouble); - context.set(Float.class, testFloat); - context.set(Character.class, testChar); - - ObjectBasic userObject = new ObjectBasic(); - ContextInjectionFactory.inject(userObject, context); - - // check field injection - assertEquals(testString, userObject.injectedString); - assertEquals(testInt, userObject.getInt()); - // assertEquals(context, userObject.context); - - // check method injection - assertEquals(1, userObject.setMethodCalled); - assertEquals(1, userObject.setMethodCalled2); - assertEquals(testDouble, userObject.d); - assertEquals(testFloat, userObject.f); - assertEquals(testChar, userObject.c); - - // check post processing - assertTrue(userObject.finalized); - } - - /** - * Tests injection of objects from parent context - */ - @Test - public synchronized void testInjectionFromParent() { - Integer testInt = Integer.valueOf(123); - String testString = "abc"; - Double testDouble = Double.valueOf(1.23); - Float testFloat = Float.valueOf(12.3f); - Character testChar = Character.valueOf('v'); - - // create parent context - IEclipseContext parentContext = EclipseContextFactory.create(); - parentContext.set(Integer.class.getName(), testInt); - parentContext.set(String.class.getName(), testString); - - // create child context - IEclipseContext context = parentContext.createChild(); - context.set(Double.class.getName(), testDouble); - context.set(Float.class.getName(), testFloat); - context.set(Character.class.getName(), testChar); - - ObjectBasic userObject = new ObjectBasic(); - ContextInjectionFactory.inject(userObject, context); - - // check field injection - assertEquals(testString, userObject.injectedString); - assertEquals(testInt, userObject.getInt()); - // assertEquals(context, userObject.context); - - // check method injection - assertEquals(1, userObject.setMethodCalled); - assertEquals(1, userObject.setMethodCalled2); - assertEquals(testDouble, userObject.d); - assertEquals(testFloat, userObject.f); - assertEquals(testChar, userObject.c); - - // check post processing - assertTrue(userObject.finalized); - } - - /** - * Tests injection into classes with inheritance - */ - @Test - public synchronized void testInjectionAndInheritance() { - Integer testInt = Integer.valueOf(123); - String testString = "abc"; - Float testFloat = Float.valueOf(12.3f); - - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Integer.class.getName(), testInt); - context.set(String.class.getName(), testString); - context.set(Float.class.getName(), testFloat); - - ObjectSubClass userObject = new ObjectSubClass(); - ContextInjectionFactory.inject(userObject, context); - - // check inherited portion - assertEquals(testString, userObject.getString()); - // assertEquals(context, userObject.getContext()); - assertEquals(testString, userObject.getStringViaMethod()); - assertEquals(1, userObject.setStringCalled); - - // check declared portion - assertEquals(testInt, userObject.getInteger()); - assertEquals(testFloat, userObject.getObjectViaMethod()); - assertEquals(1, userObject.setObjectCalled); - - // make sure overridden injected method was called only once - assertEquals(1, userObject.setOverriddenCalled); - - // check post processing - assertEquals(1, userObject.getFinalizedCalled()); - } - - static public class BaseOverrideTest { - public String selectionString; - public String inputString; - public boolean finishCalled = false; - - @Inject - public void setSelection(String selectionString) { - this.selectionString = selectionString; - } - - @Inject - public void setInput(String inputString) { - this.inputString = inputString; - } - - @PostConstruct - public void finish() { - finishCalled = true; - } - } - - static public class OverrideTest extends BaseOverrideTest { - public Integer selectionNum; - public String inputStringSubclass; - public Double inputDouble; - public Boolean arg; - public boolean finishOverrideCalled = false; - - @Inject - public void setSelection(Integer selectionNum) { - this.selectionNum = selectionNum; - } - - @Inject - public void setInput(String inputString, Double inputDouble) { - this.inputStringSubclass = inputString; - this.inputDouble = inputDouble; - - } - - @PostConstruct - public void finish(Boolean arg) { - finishOverrideCalled = true; - this.arg = arg; - } - } - - /** - * Tests injection of similar, but not overridden methods - */ - @Test - public synchronized void testInjectionCloseOverride() { - Integer testInt = Integer.valueOf(123); - String testString = "abc"; - Double testDouble = Double.valueOf(12.3); - Boolean testBoolean = Boolean.TRUE; - - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Integer.class, testInt); - context.set(String.class, testString); - context.set(Double.class, testDouble); - context.set(Boolean.class, testBoolean); - - OverrideTest userObject = new OverrideTest(); - ContextInjectionFactory.inject(userObject, context); - - // check inherited portion - assertEquals(testString, userObject.selectionString); - assertEquals(testString, userObject.inputString); - assertTrue(userObject.finishCalled); - - // check similar methods portion - assertEquals(testInt, userObject.selectionNum); - assertEquals(testString, userObject.inputStringSubclass); - assertEquals(testDouble, userObject.inputDouble); - assertTrue(userObject.finishOverrideCalled); - } - - @Test - public void testBug374421() { - try { - IEclipseContext context = EclipseContextFactory.create(); - context.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - IEclipseContext staticContext = EclipseContextFactory.create(); - ContextInjectionFactory.make(Object.class, context, staticContext); - return true; - } - }); - } catch (StackOverflowError e) { - fail("See bug 374421 for details."); - } - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/DisposingReferencedContextTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/DisposingReferencedContextTest.java deleted file mode 100644 index 40996f54077..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/DisposingReferencedContextTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.internal.contexts.EclipseContext; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("restriction") -public class DisposingReferencedContextTest { - - static class MandatoryTarget { - @Inject @Named("object") - Object object; - - @Inject - void setActiveContext(@Named(EclipseContext.ACTIVE_CHILD) IEclipseContext partContext) { - if (partContext != null) { - partContext.get("someVar"); - } - } - } - - static class OptionalTarget { - @Inject @Named("object") - Object object; - - @Inject - void setActiveContext(@Optional @Named(EclipseContext.ACTIVE_CHILD) IEclipseContext partContext) { - if (partContext != null) { - partContext.get("someVar"); - } - } - } - - @Test - public void testContextDisposeCausesCompleteUninjection_Mandatory_True() { - testContextDisposeCausesCompleteUninjection_Mandatory(true); - } - - @Test - public void testContextDisposeCausesCompleteUninjection_Mandatory_False() { - testContextDisposeCausesCompleteUninjection_Mandatory(false); - } - - @Test - public void testContextDisposeCausesCompleteUninjection_Optional_True() { - testContextDisposeCausesCompleteUninjection_Optional(true); - } - - @Test - public void testContextDisposeCausesCompleteUninjection_Optional_False() { - testContextDisposeCausesCompleteUninjection_Optional(false); - } - - private void testContextDisposeCausesCompleteUninjection_Mandatory(boolean disposeFirst) { - IEclipseContext windowContext = EclipseContextFactory.create("windowContext"); - IEclipseContext partContext = windowContext.createChild("partContext"); - - partContext.activate(); - - Object o = new Object(); - windowContext.set("object", o); - - MandatoryTarget target = new MandatoryTarget(); - ContextInjectionFactory.inject(target, windowContext); - assertEquals(o, target.object, "The object should have been injected"); - - partContext.dispose(); - - assertEquals(o, target.object, "The object should not have been uninjected"); - } - - private void testContextDisposeCausesCompleteUninjection_Optional(boolean disposeFirst) { - IEclipseContext windowContext = EclipseContextFactory.create(); - IEclipseContext partContext = windowContext.createChild(); - - partContext.activate(); - - Object o = new Object(); - windowContext.set("object", o); - - OptionalTarget target = new OptionalTarget(); - ContextInjectionFactory.inject(target, windowContext); - assertEquals(o, target.object, "The object should have been injected"); - - partContext.dispose(); - - assertEquals(o, target.object, "The object should not have been uninjected"); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ExtraDependenciesTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ExtraDependenciesTest.java deleted file mode 100644 index 015a2ce5a20..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ExtraDependenciesTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.lang.reflect.InvocationTargetException; - -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -public class ExtraDependenciesTest { - - static public class TestObject { - - public String string; - public Integer integer; - public String other; - - public boolean disposed = false; - - @Inject - public void injectedMethod(@Named("arg1") String strValue, @Named("arg2") Integer intValue, IEclipseContext context) { - string = strValue; - integer = intValue; - if (context == null) { - other = null; - return; - } - IEclipseContext otherContext = (IEclipseContext) context.get("otherContext"); - if (otherContext == null) { - other = null; - } else { - other = (String) otherContext.get("arg3"); - } - } - - @PreDestroy - public void finita() { - disposed = true; - } - } - - @Test - public void testExtraDependencies() throws InvocationTargetException, InstantiationException { - IEclipseContext context = EclipseContextFactory.create(); - context.set("arg1", "abc"); - context.set("arg2", Integer.valueOf (123)); - - IEclipseContext otherContext = EclipseContextFactory.create(); - otherContext.set("arg3", "other"); - - context.set("otherContext", otherContext); - - TestObject object = ContextInjectionFactory.make(TestObject.class, context); - - // check that initial values are properly injected - assertEquals("abc", object.string); - assertEquals(Integer.valueOf(123), object.integer); - assertEquals("other", object.other); - - // modify argument value to cause update - bug 308650 - context.set("arg2", Integer.valueOf (789)); - - // change the "other" value; should not be propagated - otherContext.set("arg3", "wrong"); - assertEquals("other", object.other); - - // dispose the other context; should not cause disposal of the test object - otherContext.dispose(); - assertEquals("other", object.other); - assertFalse(object.disposed); - - // remove "other" context, should not be propagated - context.remove("otherContext"); - assertEquals("other", object.other); - - // check that changes in the method arguments are propagated - context.set("arg1", "xyz"); - context.set("arg2", Integer.valueOf (456)); - assertEquals("xyz", object.string); - assertEquals(Integer.valueOf(456), object.integer); - assertNull(object.other); - - // check that disposal of the injected context causes disposal of the injected object - context.dispose(); - assertTrue(object.disposed); - assertNotNull(object.string); - assertNotNull(object.integer); - assertNull(object.other); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsInjectionTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsInjectionTest.java deleted file mode 100644 index 648608d92d9..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsInjectionTest.java +++ /dev/null @@ -1,151 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -/** - * Tests for the generics context injection functionality - */ -public class GenericsInjectionTest { - - static public class Animal { - } - - static public class Bird extends Animal { - } - - static public class Feeder { - } - - static public class BirdHouse extends Feeder { - } - - static public class TestNamedObject { - public Feeder field; - - @Inject - public void setFeeder(@Named("test") Feeder value) { - field = value; - } - } - - static public class TestGenericObject { - public Feeder field; - - @Inject - public void setFeeder(Feeder value) { - field = value; - } - } - - @Test - public synchronized void testNamedInjection() { - Animal testAnimal = new Animal(); - Bird testBird = new Bird(); - BirdHouse testBirdHouse = new BirdHouse(); - - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Animal.class, testAnimal); - context.set(Bird.class, testBird); - context.set("test", testBirdHouse); - - TestNamedObject userObject = new TestNamedObject(); - ContextInjectionFactory.inject(userObject, context); - - // check field injection - assertEquals(testBirdHouse, userObject.field); - } - - @Test - public synchronized void testGenericInjection() { - Animal testAnimal = new Animal(); - Bird testBird = new Bird(); - BirdHouse testBirdHouse = new BirdHouse(); - - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Animal.class, testAnimal); - context.set(Bird.class, testBird); - context.set(Feeder.class, testBirdHouse); // note that BirdHouse is - // added as Feeder class - - TestGenericObject userObject = new TestGenericObject(); - ContextInjectionFactory.inject(userObject, context); - - // check field injection - assertEquals(testBirdHouse, userObject.field); - } - - static public interface Interface { - } - - static public class Implementation implements Interface { - } - - static public class InterfaceTarget { - @Inject - public Interface field; - } - - @Test - public void testInterfaceGenericInjection() { - Implementation implementation = new Implementation(); - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Interface.class, implementation); - - InterfaceTarget target = new InterfaceTarget(); - ContextInjectionFactory.inject(target, context); - - // check field injection - assertEquals(implementation, target.field); - } - - static public class Superclass { - } - - static public class Subclass extends Superclass { - } - - static public class ClassTarget { - @Inject - public Superclass field; - } - - @Test - public void testClassGenericInjection() { - Subclass implementation = new Subclass(); - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(Superclass.class, implementation); - - ClassTarget target = new ClassTarget(); - ContextInjectionFactory.inject(target, context); - - // check field injection - assertEquals(implementation, target.field); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsListTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsListTest.java deleted file mode 100644 index 465175a6d7e..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GenericsListTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015, 2016 vogella GmbH and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Lars Vogel - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.ArrayList; -import java.util.List; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -/** - * Tests for the type erasure in lists - */ -public class GenericsListTest { - - static public class TestNamedObject { - public List field; - - @Inject - public void setList(List value) { - field = value; - } - - public String combineIt() { - StringBuilder stringBuilder = new StringBuilder(); - for (String string : field) { - stringBuilder.append(string); - } - return stringBuilder.toString(); - } - } - - @Test - @SuppressWarnings("rawtypes") - public void testRawListInjection() { - - List list = new ArrayList(); - - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(List.class, list); - - TestNamedObject userObject = new TestNamedObject(); - ContextInjectionFactory.inject(userObject, context); - - // check field injection - assertEquals(list, userObject.field); - } - - @Test - public void testTypeErasure() { - - List list = new ArrayList<>(); - list.add(1); - - // create context - IEclipseContext context = EclipseContextFactory.create(); - context.set(List.class, list); - - TestNamedObject userObject = new TestNamedObject(); - ContextInjectionFactory.inject(userObject, context); - // check field injection, should be successful because - // of the type erasure - assertEquals(list, userObject.field); - - assertThrows(ClassCastException.class, () -> userObject.combineIt()); - } - - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GroupedUpdatesTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GroupedUpdatesTest.java deleted file mode 100644 index 46b275ce18c..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/GroupedUpdatesTest.java +++ /dev/null @@ -1,142 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.GroupUpdates; -import org.junit.jupiter.api.Test; - -public class GroupedUpdatesTest { - - - static class InjectTarget { - public int countMain = 0; - public int countSecondary = 0; - - public String s1; - public String s2; - - public void resetCounters() { - countMain = 0; - countSecondary = 0; - } - } - - - static class InjectTargetImmediate extends InjectTarget { - @Inject - void setInfo(@Named("string1") String s, @Named("string2") String s2) { - countMain++; - this.s1 = s; - this.s2 = s2; - } - @Inject - void setInfoSecondary(@Named("string2") String s2) { - countSecondary++; - this.s2 = s2; - } - } - - static class InjectTargetWait extends InjectTarget { - @Inject @GroupUpdates - void setInfo(@Named("string1") String s, @Named("string2") String s2) { - countMain++; - this.s1 = s; - this.s2 = s2; - } - - @Inject @GroupUpdates - void setInfoSecondary(@Named("string2") String s2) { - countSecondary++; - this.s2 = s2; - } - } - - @Test - public void testNoGrouping() { - IEclipseContext context = EclipseContextFactory.create(); - context.set("string1", "x"); - context.set("string2", "y"); - - InjectTarget target = new InjectTargetImmediate(); - ContextInjectionFactory.inject(target, context); - assertEquals(1, target.countMain); - - target.resetCounters(); - - // I want these two sets to be one transaction - context.set("string1", "a"); - context.set("string2", "b"); - assertEquals(2, target.countMain); - assertEquals(1, target.countSecondary); - context.processWaiting(); - assertEquals(2, target.countMain); - assertEquals(1, target.countSecondary); - } - - @Test - public void testGrouping() { - final IEclipseContext context = EclipseContextFactory.create(); - context.set("string1", "x"); - context.set("string2", "y"); - - InjectTarget target = new InjectTargetWait(); - ContextInjectionFactory.inject(target, context); - assertEquals(1, target.countMain); - - target.resetCounters(); - - context.set("string1", "a"); - context.set("string2", "b"); - - assertEquals(0, target.countMain); - assertEquals(0, target.countSecondary); - - context.processWaiting(); - - assertEquals(1, target.countMain); - assertEquals(1, target.countSecondary); - - // do it again to make sure we properly cleared waiting updates - context.processWaiting(); - assertEquals(1, target.countMain); - assertEquals(1, target.countSecondary); - - // now with 3 updates - target.resetCounters(); - context.set("string1", "x"); - context.set("string2", "y"); - context.set("string2", "z"); - context.set("string1", "delta"); - - assertEquals(0, target.countMain); - assertEquals(0, target.countSecondary); - - context.processWaiting(); - - assertEquals(1, target.countMain); - assertEquals(1, target.countSecondary); - - assertEquals(target.s1, "delta"); - assertEquals(target.s2, "z"); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectStaticContextTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectStaticContextTest.java deleted file mode 100644 index a764598bced..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectStaticContextTest.java +++ /dev/null @@ -1,277 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - * Daniel Kruegler - Bug 493697 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Execute; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.Test; - -/** - * Tests for the context injection functionality using 2 contexts - */ -public class InjectStaticContextTest { - static class TestClass { - public IEclipseContext injectedContext; - public String aString; - public String bString; - public String cString; - - public String aConstructorString; - public String bConstructorString; - - public int postConstructCalled = 0; - public int preDestroyCalled = 0; - - @Inject - public void contextSet(@Optional IEclipseContext context) { - injectedContext = context; - } - - @Inject - public void setA(@Optional @Named("a") String aString) { - this.aString = aString; - } - - @Inject - public void setB(@Named("b") String bString) { - this.bString = bString; - } - - @Inject - public void setC(@Named("c") String cString) { - this.cString = cString; - } - - @Inject - public void InjectedMethod(@Named("aConstructor") String aString, @Named("bConstructor") String bString) { - aConstructorString = aString; - bConstructorString = bString; - } - - @PostConstruct - public void init() { - postConstructCalled++; - } - - @PreDestroy - public void dispose() { - preDestroyCalled++; - } - } - - static class TestInvokeClass { - public String aString; - public String bString; - - public IEclipseContext context; - - @Execute - public String testMethod(@Named("a") String aString, @Named("b") String bString, IEclipseContext context) { - this.aString = aString; - this.bString = bString; - this.context = context; - return aString + bString; - } - } - - @Test - public void testStaticMake() { - IEclipseContext trackedContext = EclipseContextFactory.create(); - trackedContext.set("a", "abc"); - trackedContext.set("aConstructor", "abcConstructor"); - trackedContext.set("b", "bbc"); - - IEclipseContext staticContext = EclipseContextFactory.create(); - staticContext.set("b", "123"); // local values override - staticContext.set("bConstructor", "123Constructor"); - staticContext.set("c", "xyz"); - - TestClass testObject = ContextInjectionFactory.make(TestClass.class, trackedContext, staticContext); - - assertEquals(trackedContext, testObject.injectedContext); - assertEquals("abcConstructor", testObject.aConstructorString); - assertEquals("123Constructor", testObject.bConstructorString); - assertEquals("abc", testObject.aString); - assertEquals("123", testObject.bString); - assertEquals("xyz", testObject.cString); - assertEquals(1, testObject.postConstructCalled); - assertEquals(0, testObject.preDestroyCalled); - - // modify local context -> should have no effect - staticContext.set("b", "_123_"); - staticContext.set("bConstructor", "_123Constructor_"); - staticContext.set("c", "_xyz_"); - - assertEquals("abcConstructor", testObject.aConstructorString); - assertEquals("123Constructor", testObject.bConstructorString); - assertEquals("abc", testObject.aString); - assertEquals("123", testObject.bString); - assertEquals("xyz", testObject.cString); - assertEquals(1, testObject.postConstructCalled); - assertEquals(0, testObject.preDestroyCalled); - - // dispose local context -> should have no effect - staticContext.dispose(); - - assertEquals("abcConstructor", testObject.aConstructorString); - assertEquals("123Constructor", testObject.bConstructorString); - assertEquals("abc", testObject.aString); - assertEquals("123", testObject.bString); - assertEquals("xyz", testObject.cString); - assertEquals(1, testObject.postConstructCalled); - assertEquals(0, testObject.preDestroyCalled); - - // modify parent context -> should propagate - trackedContext.set("a", "_abc_"); - trackedContext.set("b", "_bbc_"); - - assertEquals("_abc_", testObject.aString); - assertEquals("123", testObject.bString); - - // uninject from the parent context - ContextInjectionFactory.uninject(testObject, trackedContext); - - assertNull(testObject.injectedContext); - assertNull(testObject.aString); - - assertEquals(1, testObject.postConstructCalled); - assertEquals(1, testObject.preDestroyCalled); - - // further changes should have no effect - trackedContext.set("a", "+abc+"); - assertNull(testObject.aString); - - trackedContext.dispose(); - assertEquals(1, testObject.postConstructCalled); - assertEquals(1, testObject.preDestroyCalled); - } - - @Test - public void testStaticInject() { - IEclipseContext trackedContext = EclipseContextFactory.create(); - trackedContext.set("a", "abc"); - trackedContext.set("aConstructor", "abcConstructor"); - trackedContext.set("b", "bbc"); - - IEclipseContext staticContext = EclipseContextFactory.create(); - staticContext.set("b", "123"); // local values override - staticContext.set("bConstructor", "123Constructor"); - staticContext.set("c", "xyz"); - - TestClass testObject = new TestClass(); - - assertNull(testObject.injectedContext); - assertNull(testObject.aConstructorString); - assertNull(testObject.bConstructorString); - assertNull(testObject.aString); - assertNull(testObject.bString); - assertNull(testObject.cString); - assertEquals(0, testObject.postConstructCalled); - assertEquals(0, testObject.preDestroyCalled); - - ContextInjectionFactory.inject(testObject, trackedContext, staticContext); - - assertEquals(trackedContext, testObject.injectedContext); - assertEquals("abcConstructor", testObject.aConstructorString); - assertEquals("123Constructor", testObject.bConstructorString); - assertEquals("abc", testObject.aString); - assertEquals("123", testObject.bString); - assertEquals("xyz", testObject.cString); - assertEquals(1, testObject.postConstructCalled); - assertEquals(0, testObject.preDestroyCalled); - - // modify local context -> should have no effect - staticContext.set("b", "_123_"); - staticContext.set("bConstructor", "_123Constructor_"); - staticContext.set("c", "_xyz_"); - - assertEquals("abcConstructor", testObject.aConstructorString); - assertEquals("123Constructor", testObject.bConstructorString); - assertEquals("abc", testObject.aString); - assertEquals("123", testObject.bString); - assertEquals("xyz", testObject.cString); - assertEquals(1, testObject.postConstructCalled); - assertEquals(0, testObject.preDestroyCalled); - - // dispose local context -> should have no effect - staticContext.dispose(); - - assertEquals("abcConstructor", testObject.aConstructorString); - assertEquals("123Constructor", testObject.bConstructorString); - assertEquals("abc", testObject.aString); - assertEquals("123", testObject.bString); - assertEquals("xyz", testObject.cString); - assertEquals(1, testObject.postConstructCalled); - assertEquals(0, testObject.preDestroyCalled); - - // modify parent context -> should propagate - trackedContext.set("a", "_abc_"); - trackedContext.set("b", "_bbc_"); - - assertEquals("_abc_", testObject.aString); - assertEquals("123", testObject.bString); - - // uninject from the parent context - ContextInjectionFactory.uninject(testObject, trackedContext); - - assertNull(testObject.injectedContext); - assertNull(testObject.aString); - - assertEquals(1, testObject.postConstructCalled); - assertEquals(1, testObject.preDestroyCalled); - - // further changes should have no effect - trackedContext.set("a", "+abc+"); - assertNull(testObject.aString); - - trackedContext.dispose(); - assertEquals(1, testObject.postConstructCalled); - assertEquals(1, testObject.preDestroyCalled); - } - - @Test - public void testStaticInvoke() { - IEclipseContext trackedContext = EclipseContextFactory.create("main"); - trackedContext.set("a", "abc"); - - IEclipseContext staticContext = EclipseContextFactory.create("static"); - staticContext.set("b", "123"); - - TestInvokeClass testObject = new TestInvokeClass(); - assertNull(testObject.aString); - assertNull(testObject.bString); - - Object result = ContextInjectionFactory.invoke(testObject, Execute.class, trackedContext, staticContext, null); - - assertEquals("abc123", result); - - assertEquals("abc", testObject.aString); - assertEquals("123", testObject.bString); - - assertEquals(trackedContext, testObject.context); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionOrderTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionOrderTest.java deleted file mode 100644 index 67eba6bd933..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionOrderTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -public class InjectionOrderTest { - - public static class InjectTargetMethod { - - boolean nonNull = false; - - Object o; - - @Inject - void set(@Named("inject") Object o) { - this.o = o; - } - - @PreDestroy - void pd() { - // methods should always be uninjected after @PD is called - nonNull = o != null; - } - } - - public static class InjectTargetField { - - boolean nonNull = false; - - @Inject @Named("inject") - Object o; - - @PreDestroy - void pd() { - // fields should always be uninjected after @PD is called - nonNull = o != null; - } - } - - /** - * Tests to ensure that the injection/uninjection order of fields is correct. - *

- * See bug 304859. - *

- */ - @Test - public void testDisposeMethod() throws Exception { - // create a context - IEclipseContext appContext = EclipseContextFactory.create(); - // set a value - appContext.set("inject", "a"); - - // instantiate the object - InjectTargetMethod injectTargetMethod = ContextInjectionFactory.make(InjectTargetMethod.class, appContext); - // change the requested value so another injection occurs - appContext.set("inject", "b"); - - // now we dispose the context - appContext.dispose(); - - // check that the second 'set' invocation did not alter the order of notifications - assertTrue(injectTargetMethod.nonNull, "@PreDestroy was incorrectly called after the method was uninjected"); - } - - /** - * Tests to ensure that the injection/uninjection order of methods is correct. - *

- * See bug 304859. - *

- */ - @Test - public void testDisposeField() throws Exception { - // create a context - IEclipseContext appContext = EclipseContextFactory.create(); - // set a value - appContext.set("inject", "a"); - - // instantiate the object - InjectTargetField injectTargetField = ContextInjectionFactory.make(InjectTargetField.class, appContext); - // change the requested value so another injection occurs - appContext.set("inject", "b"); - - // now we dispose the context - appContext.dispose(); - - assertTrue(injectTargetField.nonNull, "@PreDestroy was incorrectly called after the field was uninjected"); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionUpdateTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionUpdateTest.java deleted file mode 100644 index 13b6c088ab4..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InjectionUpdateTest.java +++ /dev/null @@ -1,189 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; - -import javax.annotation.PostConstruct; -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * Tests updates of injected values and calls to runnables - */ -public class InjectionUpdateTest { - - private IEclipseContext c1; // common root - private IEclipseContext c21; // dependents of root - path 1 - private IEclipseContext c22; // dependents of root - path 2 - - static public class PropagationTest { - - public int called = 0; - public String in; - - @Inject - public PropagationTest() { - // placeholder - } - - @Inject - public void setCalculated(@Named("calculated") String string) { - called++; - in = string; - } - } - - @BeforeEach - public void setUp() throws Exception { - c1 = EclipseContextFactory.create("c1"); - c1.set("id", "c1"); - - c21 = c1.createChild("c21"); - c21.set("id", "c21"); - c1.set("c21", c21); - - c22 = c1.createChild("c22"); - c22.set("id", "c22"); - c1.set("c22", c22); - } - - @Test - public void testPropagation() { - c1.set("base", "abc"); - - c21.set("derived1", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - String baseString = (String) context.get("base"); - return baseString.charAt(0) + "_"; - }}); - - c22.set("derived2", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - String baseString = (String) context.get("base"); - return "_" + baseString.charAt(baseString.length() - 1); - }}); - - c1.set("calculated", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - IEclipseContext context21 = (IEclipseContext) context.get("c21"); - String derived1 = (String) context21.get("derived1"); - - IEclipseContext context22 = (IEclipseContext) context.get("c22"); - String derived2 = (String) context22.get("derived2"); - return derived1 + derived2; - }}); - - PropagationTest testObject = ContextInjectionFactory.make(PropagationTest.class, c1); - assertNotNull(testObject); - assertEquals(1, testObject.called); - assertEquals("a__c", testObject.in); - - c1.set("base", "123"); // this should result in only one injection call - assertEquals(2, testObject.called); - assertEquals("1__3", testObject.in); - - c1.set("base", "xyz"); - assertEquals(3, testObject.called); - assertEquals("x__z", testObject.in); - } - - public static class InjectTarget { - private static final String KEY = "key"; - - @Inject - private IEclipseContext context; - - @PostConstruct - void pc() { - context.containsKey(KEY); - } - - public void modify() { - context.set(KEY, null); - } - } - - @Test - public void testNestedUpdatesPostConstruct() throws Exception { - IEclipseContext appContext = EclipseContextFactory.create(); - appContext.set(InjectTarget.class.getName(), new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return ContextInjectionFactory - .make(InjectTarget.class, context); - } - }); - - InjectTarget targetA = appContext.get(InjectTarget.class); - targetA.modify(); - - InjectTarget targetB = appContext.get(InjectTarget.class); - assertEquals(targetA, targetB); - assertSame(targetA, targetB); - } - - public static class InjectTarget2 { - private static final String KEY = "key"; //$NON-NLS-1$ - - @Inject - private IEclipseContext context; - - public Object key; - - @Inject - public InjectTarget2(@Optional @Named("key") Object key) { - this.key = key; - } - - public void modify() { - context.set(KEY, null); - } - } - - @Test - public void testNestedUpdatesConstructor() throws Exception { - IEclipseContext appContext = EclipseContextFactory.create(); - appContext.set(InjectTarget2.class.getName(), new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return ContextInjectionFactory.make(InjectTarget2.class, - context); - } - }); - - InjectTarget2 targetA = appContext.get(InjectTarget2.class); - targetA.modify(); - - InjectTarget2 targetB = appContext.get(InjectTarget2.class); - assertEquals(targetA, targetB); - assertSame(targetA, targetB); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InvokeInRATTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InvokeInRATTest.java deleted file mode 100644 index c7f4eb62006..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/InvokeInRATTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.eclipse.e4.core.di.annotations.CanExecute; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.Test; - -/** - * Tests for the context injection functionality using 2 contexts - */ -public class InvokeInRATTest { - - static class TestHandler { - - public Object active; - public Object selected; - - @CanExecute - public void testEnablement(@Optional @Named("active") Object active, @Optional @Named("selected") Object selected) { - this.active = active; - this.selected = selected; - } - } - - @Test - public void testStaticInvoke() { - IEclipseContext context = EclipseContextFactory.create(); - final int[] count = new int[1]; - - context.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - TestHandler handler = (TestHandler) context.get("handlerA"); - if (handler != null) { - ContextInjectionFactory.invoke(handler, CanExecute.class, context); - count[0]++; - } - return true; // continue to be notified - }}); - - // check that updates are propagated - context.set("active", Integer.valueOf(123)); - context.set("selected", "abc"); - TestHandler handler = new TestHandler(); - context.set("handlerA", handler); - - assertEquals(Integer.valueOf(123), handler.active); - assertEquals("abc", handler.selected); - - // check handler replacement - count[0] = 0; - TestHandler newHandler = new TestHandler(); - context.set("handlerA", newHandler); - assertEquals(1, count[0]); - - assertEquals(Integer.valueOf(123), newHandler.active); - assertEquals("abc", newHandler.selected); - - // change values in the context; values should not be propagated to handlers - context.set("active", Integer.valueOf(456)); - context.set("selected", "xyz"); - - assertEquals(Integer.valueOf(123), handler.active); - assertEquals("abc", handler.selected); - assertEquals(Integer.valueOf(123), newHandler.active); - assertEquals("abc", newHandler.selected); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectBasic.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectBasic.java deleted file mode 100644 index d8a01110d4e..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectBasic.java +++ /dev/null @@ -1,81 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2010 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; - -/** - * Test class to check injection mechanism - */ -public class ObjectBasic { - - // Injected directly - @Inject @Optional - public String injectedString; - @Inject - private Integer injectedInteger; - - // Injected indirectly - public Double d; - public Float f; - public Character c; - public IEclipseContext context; - - // Test status - public boolean finalized = false; - public boolean disposed = false; - public int setMethodCalled = 0; - public int setMethodCalled2 = 0; - - public ObjectBasic() { - // placeholder - } - - @Inject - public void objectViaMethod(Double d) { - setMethodCalled++; - this.d = d; - } - - @Inject - public void arguments(Float f, @Optional Character c) { - setMethodCalled2++; - this.f = f; - this.c = c; - } - - @PostConstruct - public void postCreate(IEclipseContext context) { - this.context = context; - finalized = true; - } - - @PreDestroy - public void dispose(IEclipseContext context) { - if (this.context != context) - throw new IllegalArgumentException("Unexpected context"); - this.context = null; - disposed = true; - } - - public Integer getInt() { - return injectedInteger; - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSubClass.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSubClass.java deleted file mode 100644 index c1417a1e59d..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSubClass.java +++ /dev/null @@ -1,96 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.IEclipseContext; - -/** - * Test class to check injection mechanism into classes with inheritance - */ -public class ObjectSubClass extends ObjectSuperClass { - @Inject - /* package */Integer Integer; - - private Object myObject; - - public int setObjectCalled = 0; - public int setSubFinalized = 0; - public int postConstructSetObjectCalled; - public int postConstructSetOverriddenCalled; - public int subPostConstructCount; - public int subPreDestroyCount; - public int overriddenPreDestroyCount; - - public ObjectSubClass() { - super(); - } - - @Inject - public void ObjectViaMethod(Float f) { - myObject = f; - setObjectCalled++; - } - - @Override - @Inject - public void OverriddenMethod(Float f) { - setOverriddenCalled++; - } - - @Override - @Inject - public void contextSet(IEclipseContext context) { - super.contextSet(context); - setSubFinalized++; - } - - public Integer getInteger() { - return Integer; - } - - public Object getObjectViaMethod() { - return myObject; - } - - public int getFinalizedCount() { - return setSubFinalized; - } - - public int getOverriddenCount() { - return setOverriddenCalled; - } - - @PostConstruct - public void subPostConstruct() { - postConstructSetObjectCalled = setObjectCalled; - postConstructSetStringCalled = setStringCalled; - postConstructSetOverriddenCalled = setOverriddenCalled; - subPostConstructCount++; - } - - @PreDestroy - public void subPreDestroy() { - subPreDestroyCount++; - } - - @Override - @PreDestroy - public void overriddenPreDestroy() { - overriddenPreDestroyCount++; - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSuperClass.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSuperClass.java deleted file mode 100644 index f144558d2bc..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ObjectSuperClass.java +++ /dev/null @@ -1,93 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2010 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.IEclipseContext; - -/** - * Test class to check injection mechanism into classes with inheritance - */ -public class ObjectSuperClass { - - protected IEclipseContext context; - @Inject - private String String; - private String myString; - - public int postConstructSetStringCalled; - public int setFinalizedCalled = 0; - public int setStringCalled = 0; - public int superPostConstructCount; - public int superPreDestroyCount; - public int setOverriddenCalled = 0; - - public ObjectSuperClass() { - // placeholder - } - - @Inject - public void contextSet(IEclipseContext context) { - this.context = context; - setFinalizedCalled++; - } - - public IEclipseContext getContext() { - return context; - } - - public int getFinalizedCalled() { - return setFinalizedCalled; - } - - public String getString() { - return String; - } - - public String getStringViaMethod() { - return myString; - } - - @Inject - public void OverriddenMethod(Float f) { - setOverriddenCalled++; - } - - @Inject - public void StringViaMethod(String string) { - myString = string; - setStringCalled++; - } - - @PostConstruct - public void superPostConstruct() { - // record setter invocation counts at time of post construct invocation - postConstructSetStringCalled = setStringCalled; - superPostConstructCount++; - } - - @PreDestroy - public void superPreDestroy() { - superPreDestroyCount++; - } - - @PreDestroy() - public void overriddenPreDestroy() { - // - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java deleted file mode 100644 index 1edb3cabffc..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java +++ /dev/null @@ -1,50 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2010 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import javax.inject.Inject; - -import org.eclipse.e4.core.di.annotations.Optional; - -public class OptionalAnnotations { - - @Inject @Optional - public Float f = null; - - public Double d; - public String s = "ouch"; - public Integer i; - - public int methodOptionalCalled = 0; - public int methodRequiredCalled = 0; - - public OptionalAnnotations() { - // placehodler - } - - @Inject @Optional - public void methodOptional(Double d) { - this.d = d; - methodOptionalCalled++; - } - - @Inject - public void methodRequired(@Optional String s, Integer i) { - this.s = s; - this.i = i; - methodRequiredCalled++; - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ParentContextDisposalTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ParentContextDisposalTest.java deleted file mode 100644 index 86265bd6630..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ParentContextDisposalTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -public class ParentContextDisposalTest { - static class Target { - int pc = 0; - int pd = 0; - - @Inject - @Named("o") - Object o; - - @PostConstruct - void pc() { - pc++; - } - - @PreDestroy - void pd() { - pd++; - } - } - - @Test - public void testParentContextDisposal() { - IEclipseContext context = EclipseContextFactory.create(); - IEclipseContext child = context.createChild(); - child.set("o", new Object()); - - Target target = ContextInjectionFactory.make(Target.class, child); - assertEquals(1, target.pc); - - context.dispose(); - assertEquals(1, target.pd); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/PrintService.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/PrintService.java deleted file mode 100644 index 45a1a0647de..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/PrintService.java +++ /dev/null @@ -1,24 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2010 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -/** - * A toy service implementation that prints a message. - */ -public interface PrintService { - public static final String SERVICE_NAME = PrintService.class.getName(); - - public void print(String message); -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ServiceContextTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ServiceContextTest.java deleted file mode 100644 index fa695931d0c..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/ServiceContextTest.java +++ /dev/null @@ -1,395 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2017 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.ArrayList; -import java.util.List; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.eclipse.osgi.service.debug.DebugOptions; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; - -/** - * Tests for contexts used in OSGi services. - */ -public class ServiceContextTest { - static class Crayon { - @Inject - IPaletteService palette; - - String msg; - - public void draw() { - if (palette == null) { - msg = "I'm out of ink!"; - } else { - msg = "My ink is " + palette.getColor(); - } - } - } - - static enum Color { - RED, BLUE, YELLOW, GREEN, ORANGE, PURPLE; - } - - interface IPaletteService { - public Color getColor(); - } - - static class PaletteImpl implements IPaletteService { - private final Color color; - - PaletteImpl(Color color) { - this.color = color; - } - - @Override - public Color getColor() { - return color; - } - } - - static class Printer { - @Inject @Optional - PrintService printer; - - public void print(String message) { - if (printer != null) { - printer.print(message); - } - } - } - - static class TestBean { - @Inject - @Optional - TestService testService; - - @Inject - @Optional - TestOtherService testOtherService; - } - - private IEclipseContext context; - private final List> registrations = new ArrayList<>(); - - @BeforeEach - public void setUp() throws Exception { - //don't use the global shared service context to avoid contamination across tests - BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext(); - context = EclipseContextFactory.getServiceContext(bundleContext); - registrations.clear(); - } - - @AfterEach - public void tearDown() throws Exception { - // Consumers must not dispose OSGi context as it is reused - //context.dispose(); - for (ServiceRegistration reg : registrations) { - try { - reg.unregister(); - } catch (IllegalStateException e) { - //ignore - } - } - registrations.clear(); - context = null; - } - - @Test - public void testDeclarativeService() { - assertTrue(context.containsKey("sum")); - assertEquals(0, context.get("sum")); - context.set("x", 1); - context.set("y", 2); - int result = (Integer) context.get("sum"); - assertEquals(3, result); - context.set("x", 5); - result = (Integer) context.get("sum"); - assertEquals(7, result); - } - - /** - * Tests accessing OSGi services through a child context that is not aware of them. - */ - @Test - public void testServiceContextAsParent() { - IEclipseContext child = context.createChild( "child"); - DebugOptions service = (DebugOptions) child.get(DebugOptions.class.getName()); - assertNotNull(service); - } - - @Test - public void testServiceInjection() { - ServiceRegistration reg1 = null; - ServiceRegistration reg2 = null; - try { - Printer userObject = new Printer(); - - StringPrintService stringPrint1 = new StringPrintService(); - BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext(); - reg1 = bundleContext.registerService(PrintService.SERVICE_NAME, stringPrint1, null); - - ContextInjectionFactory.inject(userObject, context); - userObject.print("test"); - assertEquals("test", stringPrint1.toString()); - - // now remove the service - reg1.unregister(); - reg1 = null; - userObject.print("another test"); - // the string should be unchanged - assertEquals("test", stringPrint1.toString()); - assertNull(userObject.printer); - - // register a different service implementation - StringPrintService stringPrint2 = new StringPrintService(); - reg2 = bundleContext.registerService(PrintService.SERVICE_NAME, stringPrint2, null); - userObject.print("yet another test"); - // the second string should have the value - assertEquals("test", stringPrint1.toString()); - assertEquals("yet another test", stringPrint2.toString()); - reg2.unregister(); - reg2 = null; - assertNull(userObject.printer); - } finally { - if (reg1 != null) { - reg1.unregister(); - reg1 = null; - } - if (reg2 != null) { - reg2.unregister(); - reg2 = null; - } - } - } - - @Test - public void testServiceAddition() { - ServiceRegistration reg1 = null; - try { - Printer userObject = new Printer(); - ContextInjectionFactory.inject(userObject, context); - - StringPrintService stringPrint1 = new StringPrintService(); - BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext(); - reg1 = bundleContext.registerService(PrintService.SERVICE_NAME, stringPrint1, null); - - userObject.print("test"); - assertEquals("test", stringPrint1.toString()); - } finally { - if (reg1 != null) { - reg1.unregister(); - reg1 = null; - } - } - } - - protected void ensureUnregistered(ServiceRegistration reg) { - registrations.add(reg); - } - - /** - * Tests that OSGi services are released when their context is disposed. - */ - @Test - public void testServiceRemovalOnContextDispose() { - StringPrintService stringPrint1 = new StringPrintService(); - BundleContext bundleContext = CoreTestsActivator.getDefault() - .getBundleContext(); - Bundle otherBundle = null; - for (Bundle b : bundleContext.getBundles()) { - if (b.getSymbolicName().equals("org.eclipse.core.tests.harness")) { - otherBundle = b; - break; - } - } - assertNotNull(otherBundle); - IEclipseContext otherServiceContext = EclipseContextFactory - .getServiceContext(otherBundle.getBundleContext()); - ServiceRegistration reg1 = bundleContext.registerService( - PrintService.SERVICE_NAME, stringPrint1, null); - try { - ServiceReference ref = reg1.getReference(); - - PrintService service = (PrintService) otherServiceContext - .get(PrintService.SERVICE_NAME); - assertEquals(stringPrint1, service); - assertThat(ref.getUsingBundles()).hasSize(1); - service = null; - otherServiceContext.dispose(); - assertNull(ref.getUsingBundles()); - } finally { - reg1.unregister(); - } - } - - @Test - public void testRecursiveServiceRemoval() { - BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext(); - ServiceRegistration reg1 = bundleContext.registerService(PrintService.SERVICE_NAME, new StringPrintService(), null); - final IEclipseContext child = context.createChild(); - final IEclipseContext child2 = context.createChild(); - child.get(PrintService.SERVICE_NAME); - child2.get(PrintService.SERVICE_NAME); - ensureUnregistered(reg1); - final boolean[] done = new boolean[] {false}; - context.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - if (context.get(PrintService.SERVICE_NAME) == null) { - child.dispose(); - done[0] = true; - } - return true; - } - }); - reg1.unregister(); - } - - @Test - public void testServiceExample() { - BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext(); - ServiceRegistration reg = bundleContext.registerService(IPaletteService.class.getName(), new PaletteImpl(Color.BLUE), null); - IEclipseContext context = EclipseContextFactory.getServiceContext(bundleContext); - Crayon crayon = new Crayon(); - ContextInjectionFactory.inject(crayon, context); - crayon.draw(); - reg.unregister(); - crayon.draw(); - } - - @Test - public void testOptionalReferences() throws InterruptedException { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context); - TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext); - - assertNull(bean.testService); - - ServiceReference ref = context.getServiceReference(TestServiceController.class); - TestServiceController controller = context.getService(ref); - try { - controller.enableTestServiceA(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNotNull(bean.testService); - assertSame(TestServiceA.class, bean.testService.getClass()); - assertNotNull(bean.testOtherService); - assertSame(TestServiceA.class, bean.testOtherService.getClass()); - - controller.enableTestServiceB(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNotNull(bean.testService); - assertSame(TestServiceB.class, bean.testService.getClass()); - assertNotNull(bean.testOtherService); - assertSame(TestServiceB.class, bean.testOtherService.getClass()); - - controller.disableTestServiceB(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNotNull(bean.testService); - assertSame(TestServiceA.class, bean.testService.getClass()); - assertNotNull(bean.testOtherService); - assertSame(TestServiceA.class, bean.testOtherService.getClass()); - - controller.disableTestServiceA(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNull(bean.testService); - assertNull(bean.testOtherService); - } finally { - controller.disableTestServiceA(); - controller.disableTestServiceB(); - // give the service registry and the injection some time to ensure - // clear state after this test - Thread.sleep(100); - } - } - - @Test - public void testServiceRanking() throws InterruptedException { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context); - TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext); - - assertNull(bean.testService); - - ServiceReference ref = context.getServiceReference(TestServiceController.class); - TestServiceController controller = context.getService(ref); - try { - controller.enableTestServiceB(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNotNull(bean.testService); - assertSame(TestServiceB.class, bean.testService.getClass()); - assertNotNull(bean.testOtherService); - assertSame(TestServiceB.class, bean.testOtherService.getClass()); - - // enable a service with a lower ranking - controller.enableTestServiceA(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNotNull(bean.testService); - // we expect that still the highest ranked service is injected - assertSame(TestServiceB.class, bean.testService.getClass()); - assertNotNull(bean.testOtherService); - assertSame(TestServiceB.class, bean.testOtherService.getClass()); - - controller.disableTestServiceB(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNotNull(bean.testService); - assertSame(TestServiceA.class, bean.testService.getClass()); - assertNotNull(bean.testOtherService); - assertSame(TestServiceA.class, bean.testOtherService.getClass()); - - controller.disableTestServiceA(); - // give the service registry and the injection some time - Thread.sleep(100); - assertNull(bean.testService); - assertNull(bean.testOtherService); - } finally { - controller.disableTestServiceA(); - controller.disableTestServiceB(); - // give the service registry and the injection some time to ensure - // clear state after this test - Thread.sleep(100); - } - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/StringPrintService.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/StringPrintService.java deleted file mode 100644 index aed492b5e92..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/StringPrintService.java +++ /dev/null @@ -1,30 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -public class StringPrintService implements PrintService { - private final StringBuilder buf = new StringBuilder(); - - @Override - public void print(String message) { - buf.append(message); - } - - @Override - public String toString() { - return buf.toString(); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestConstructorInjection.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestConstructorInjection.java deleted file mode 100644 index def95a782d2..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestConstructorInjection.java +++ /dev/null @@ -1,132 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.lang.reflect.InvocationTargetException; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -public class TestConstructorInjection { - - static class TestConstructorObject { - - public boolean defaultConstructorCalled = false; - public boolean constructorIntStrCalled = false; - public boolean constructorIntBoolCalled = false; - public boolean injectedMethodCalled = false; - public boolean nonInjectedMethodCalled = false; - - @Inject - private Character c; - - public Integer i; - public String s; - public Boolean b; - public Double d; - public Float f; - - public boolean orderCorrect = true; - - public TestConstructorObject() { - defaultConstructorCalled = true; - } - - public TestConstructorObject(Integer i, String s) { - constructorIntStrCalled = true; - this.i = i; - this.s = s; - } - - @Inject - public TestConstructorObject(Integer i, Boolean b) { - constructorIntBoolCalled = true; - this.i = i; - this.b = b; - // the constructor should be called first - if ((c != null) || (d != null) || (f != null)) { - orderCorrect = false; - } - } - - @Inject - public void injectedMethod(Double d, Float f) { - injectedMethodCalled = true; - this.d = d; - this.f = f; - // the method injection after constructor and field injection - if ((c == null) || (i == null) || (b == null)) { - orderCorrect = false; - } - } - - public void nonInjectedMethod(Double d) { - nonInjectedMethodCalled = true; - this.d = d; - } - - public Character getChar() { - return c; - } - } - - @Test - public void testConstructorInjection() throws InvocationTargetException, InstantiationException { - IEclipseContext context = EclipseContextFactory.create(); - Integer intValue = Integer.valueOf(123); - context.set(Integer.class.getName(), intValue); - Boolean boolValue = Boolean.TRUE; - context.set(Boolean.class.getName(), boolValue); - Double doubleValue = Double.valueOf(1.23); - context.set(Double.class.getName(), doubleValue); - Float floatValue = Float.valueOf(12.3f); - context.set(Float.class.getName(), floatValue); - Character charValue = Character.valueOf('v'); - context.set(Character.class.getName(), charValue); - - Object result = ContextInjectionFactory.make(TestConstructorObject.class, context); - assertNotNull(result); - assertTrue(result instanceof TestConstructorObject); - - TestConstructorObject testObject = ((TestConstructorObject) result); - - assertFalse(testObject.defaultConstructorCalled); - assertFalse(testObject.constructorIntStrCalled); - assertTrue(testObject.constructorIntBoolCalled); - assertTrue(testObject.injectedMethodCalled); - assertFalse(testObject.nonInjectedMethodCalled); - assertTrue(testObject.orderCorrect); - - assertEquals(intValue, testObject.i); - assertEquals(boolValue, testObject.b); - assertEquals(doubleValue, testObject.d); - assertEquals(floatValue, testObject.f); - assertEquals(charValue, testObject.getChar()); - assertNull(testObject.s); - - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestOtherService.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestOtherService.java deleted file mode 100644 index b983f30846a..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestOtherService.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -public interface TestOtherService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestService.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestService.java deleted file mode 100644 index 11f52fabd22..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestService.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -public interface TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceA.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceA.java deleted file mode 100644 index aa1eac4f02d..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceA.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import org.osgi.service.component.annotations.Component; - -@Component(enabled = false) -public class TestServiceA implements TestService, TestOtherService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceB.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceB.java deleted file mode 100644 index 0334513bd92..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceB.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.propertytypes.ServiceRanking; - -@Component(enabled = false) -@ServiceRanking(5) -public class TestServiceB implements TestService, TestOtherService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceController.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceController.java deleted file mode 100644 index 6873ed63683..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/TestServiceController.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import org.osgi.service.component.ComponentContext; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; - -@Component(service = TestServiceController.class) -public class TestServiceController { - - ComponentContext context; - - @Activate - void activate(ComponentContext context) { - this.context = context; - } - - public void enableTestServiceA() { - this.context.enableComponent(TestServiceA.class.getName()); - } - - public void disableTestServiceA() { - this.context.disableComponent(TestServiceA.class.getName()); - } - - public void enableTestServiceB() { - this.context.enableComponent(TestServiceB.class.getName()); - } - - public void disableTestServiceB() { - this.context.disableComponent(TestServiceB.class.getName()); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/performance/ContextPerformanceTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/performance/ContextPerformanceTest.java deleted file mode 100644 index adbd3176402..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/contexts/performance/ContextPerformanceTest.java +++ /dev/null @@ -1,138 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.performance; - -import org.eclipse.core.runtime.IAdapterManager; -import org.eclipse.core.runtime.IExtensionRegistry; -import org.eclipse.core.runtime.preferences.IPreferencesService; -import org.eclipse.core.tests.harness.PerformanceTestRunner; -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.eclipse.osgi.service.datalocation.Location; -import org.eclipse.osgi.service.debug.DebugOptions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -public class ContextPerformanceTest { - String displayName; - - IEclipseContext parentContext, context; - - @BeforeEach - public void setUp(TestInfo testInfo) throws Exception { - displayName = testInfo.getDisplayName(); - parentContext = EclipseContextFactory.getServiceContext(CoreTestsActivator.getDefault().getBundleContext()); - context = parentContext.createChild(displayName); - - // add some values to the contexts - for (int i = 0; i < 100; i++) { - context.set("Value-" + i, Integer.valueOf(i)); - } - // do some additional service lookups on non-existent keys - for (int i = 0; i < 1000; i++) { - context.get("NonExistentValue-" + i); - } - - // lookup some OSGi services - context.get(DebugOptions.class.getName()); - context.get(IAdapterManager.class.getName()); - context.get(IExtensionRegistry.class.getName()); - context.get(IPreferencesService.class.getName()); - context.get(Location.class.getName()); - } - - @Test - public void testLookup() throws Exception { - new PerformanceTestRunner() { - @Override - protected void test() { - context.get("something"); - } - }.run(getClass(), displayName, 10, 600000); - } - - @Test - public void testLookupContextFunction() throws Exception { - context.set("somefunction", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return "result"; - } - }); - new PerformanceTestRunner() { - @Override - protected void test() { - context.get("somefunction"); - } - }.run(getClass(), displayName, 10, 5000000); - } - - @Test - public void testSetContextFunction() throws Exception { - context.set("somefunction", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return context.get("something"); - } - }); - new PerformanceTestRunner() { - int i = 0; - - @Override - protected void test() { - context.set("something", "value-" + i++); - } - }.run(getClass(), displayName, 10, 600000); - } - - /** - * Tests setting a value in a context that a RAT is listening to. This test mimics what occurs - * when handlers change in e4. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=305038 - */ - @Test - public void testSetValueRunAndTrack() throws Exception { - context.set("somefunction", new ContextFunction() { - @Override - public Object compute(IEclipseContext context, String contextKey) { - // make sure this function has a large number of dependencies - for (int i = 0; i < 1000; i++) { - context.get("NonExistentValue-" + i); - } - return context.get("something"); - } - }); - context.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - context.get("somefunction"); - return true; - } - }); - new PerformanceTestRunner() { - int i = 0; - - @Override - protected void test() { - context.set("something", "value-" + i++); - } - }.run(getClass(), displayName, 10, 400); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/AutoConstructWithCreatableTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/AutoConstructWithCreatableTest.java deleted file mode 100644 index f93b0e7ee98..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/AutoConstructWithCreatableTest.java +++ /dev/null @@ -1,105 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012, 2016 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274, 496305 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.InjectionException; -import org.eclipse.e4.core.di.annotations.Creatable; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class AutoConstructWithCreatableTest { - - private IEclipseContext context; - - @Creatable - static class Dependent1 { - @Inject - public Dependent1() { - // placeholder - } - } - - static class Dependent2 { - @Inject - public Dependent2() { - // placeholder - } - } - - static class Consumer1 { - @Inject - public Consumer1(Dependent1 dep) { - // placeholder - } - } - - static class Consumer2 { - @Inject - public Consumer2(Dependent2 dep) { - // placeholder - } - } - - @BeforeEach - public void createContext() { - context = EclipseContextFactory.create(); - } - - /** - * Checks that classes with @Creatable are auto-constructed - */ - @Test - public void testCreatableIsCreated() { - Consumer1 consumer1 = ContextInjectionFactory.make(Consumer1.class, context); - assertNotNull(consumer1); - - boolean exception = false; - try { - ContextInjectionFactory.make(Consumer2.class, context); - } catch (InjectionException e) { - exception = true; // expected - } - assertTrue(exception); - - context.set(Dependent2.class, new Dependent2()); - Consumer2 consumer2 = ContextInjectionFactory.make(Consumer2.class, context); - assertNotNull(consumer2); - } - - /** - * Checks that only classes with @Creatable are auto-constructed - */ - @Test - public void testNonCreatableInstanceAreNotCreated() { - assertThrows(InjectionException.class, () -> ContextInjectionFactory.make(Consumer2.class, context)); - } - - @Test // ensure "normal" dependency injection - public void testNonCreatableInstancesAreUsedFromContext() { - context.set(Dependent2.class, new Dependent2()); - Consumer2 consumer2 = ContextInjectionFactory.make(Consumer2.class, context); - assertNotNull(consumer2); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/DisposeClassLinkTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/DisposeClassLinkTest.java deleted file mode 100644 index 8d64200d62f..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/DisposeClassLinkTest.java +++ /dev/null @@ -1,160 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.di.extensions.EventTopic; -import org.junit.jupiter.api.Test; -/** - * Checks that injected objects that do not have normal links - * established to the context are still notified on context - * disposal. - * (No links: nothing was actually injected; or only IEclipseContext was injected; - * or constructor injection was used.) - * See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=301462 . - */ -public class DisposeClassLinkTest { - - public static class MyTest { - private int count = 0; - - public int getCount() { - return count; - } - - @PreDestroy - void preDestroy() { - count++; - } - } - - public static class InjectionObject { - - @Inject - IEclipseContext context; - - int postConstruct = 0; - int preDestroy = 0; - - @PostConstruct - void construct() { - postConstruct++; - } - - @PreDestroy - void destroy() { - preDestroy++; - } - } - - public static class TestBug430041 { - int preDestroy = 0; - - @Inject - @Optional - public void inject(@EventTopic("Bla") String bla) { - - } - - @PreDestroy - void destroy() { - preDestroy++; - } - } - - @Test - public void testMake() throws Exception { - IEclipseContext context = EclipseContextFactory.create(); - MyTest test = ContextInjectionFactory.make(MyTest.class, context); - - assertEquals(0, test.getCount()); - context.dispose(); - assertEquals(1, test.getCount()); - } - - @Test - public void testDisposeParent() throws Exception { - IEclipseContext parentContext = EclipseContextFactory.create(); - IEclipseContext context = parentContext.createChild(); - MyTest test = ContextInjectionFactory.make(MyTest.class, context); - - assertEquals(0, test.getCount()); - context.dispose(); - assertEquals(1, test.getCount()); - parentContext.dispose(); - assertEquals(1, test.getCount()); - } - - @Test - public void testInject() throws Exception { - IEclipseContext parentContext = EclipseContextFactory.create(); - IEclipseContext context = parentContext.createChild(); - MyTest test = new MyTest(); - ContextInjectionFactory.inject(test, context); - - assertEquals(0, test.getCount()); - context.dispose(); - assertEquals(1, test.getCount()); - } - - @Test - public void testDisposeParentFirst() throws Exception { - IEclipseContext parentContext = EclipseContextFactory.create(); - IEclipseContext context = parentContext.createChild(); - MyTest test = new MyTest(); - ContextInjectionFactory.inject(test, context); - - assertEquals(0, test.getCount()); - context.dispose(); - assertEquals(1, test.getCount()); - parentContext.dispose(); - assertEquals(1, test.getCount()); - } - - @Test - public void testInjectedWithContext() throws Exception { - IEclipseContext context = EclipseContextFactory.create(); - - InjectionObject obj = ContextInjectionFactory.make(InjectionObject.class, context); - - assertEquals(context, obj.context, "The object has been injected with the context"); - assertEquals(1, obj.postConstruct, "@PostConstruct should have been called once"); - assertEquals(0, obj.preDestroy, "@PreDestroy should not have been called"); - - context.dispose(); - - assertNotNull(obj.context); - assertEquals(1, obj.postConstruct, "@PostConstruct should only have been called once"); - assertEquals(1, obj.preDestroy, "@PreDestroy should have been called during uninjection"); - } - - @Test - public void testBug430041() { - IEclipseContext context = EclipseContextFactory.create(); - TestBug430041 obj = ContextInjectionFactory.make(TestBug430041.class, context); - context.dispose(); - assertEquals(1, obj.preDestroy); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectArraysTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectArraysTest.java deleted file mode 100644 index f5c51b5e8a7..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectArraysTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - -/** - * Checks injection of arrays - */ -public class InjectArraysTest { - - static class TestClass { - @Inject @Named("test_array_String") - public String[] stringArray; - - @Inject - public Integer[] integerArray; - - - public int[] intArray; - public char[] charAray; - - @Inject - public void set(@Named("test_array_int") int[] intArray, @Named("test_array_char") char[] charAray) { - this.intArray = intArray; - this.charAray = charAray; - } - } - - @Test - public void testArrayInjection() { - String[] arrayString = new String[] { "abc", "xyz", "ttt" }; - Integer[] arrayInteger = new Integer[] { 5, 6, 7 }; - int[] arrayInt = new int[] { 1, 2, 3 }; - char[] arrayChar = new char[] { 'a', 'b', 'c' }; - - IEclipseContext context = EclipseContextFactory.create(); - context.set("test_array_String", arrayString); - context.set(Integer[].class, arrayInteger); - context.set("test_array_int", arrayInt); - context.set("test_array_char", arrayChar); - - TestClass testClass = ContextInjectionFactory.make(TestClass.class, context); - checkArraysEqual(arrayString, testClass.stringArray); - checkArraysEqual(arrayInteger, testClass.integerArray); - checkArraysEqual(arrayInt, testClass.intArray); - checkArraysEqual(arrayChar, testClass.charAray); - } - - private void checkArraysEqual(Object array1, Object array2) { - assertNotNull(array2); - assertEquals(array1, array2); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBaseTypeTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBaseTypeTest.java deleted file mode 100644 index f21591e1e63..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBaseTypeTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.Test; - -/** - * Checks conversion of primitive types - */ -public class InjectBaseTypeTest { - - static class TestClass { - @Inject @Named("test_int") - public int intField; - - @Inject @Named("test_int_optional") @Optional - public int intFieldOptional; - - @Inject @Named("test_long") - public long longField; - - @Inject @Named("test_float") - public float floatField; - - @Inject @Named("test_double") - public double doubleField; - - @Inject @Named("test_short") - public short shortField; - - @Inject @Named("test_byte") - public byte byteField; - - @Inject @Optional @Named("test_boolean") - public boolean booleanField; - - @Inject @Named("test_char") - public char charField; - - public int intArg; - public char charArg; - public boolean booleanArg; - - @Inject - public void set(@Named("test_int") int intArg, @Named("test_char") char charArg, @Named("test_boolean") boolean booleanArg) { - this.intArg = intArg; - this.charArg = charArg; - this.booleanArg = booleanArg; - } - - } - - @Test - public void testPrimitiveTypes() { - IEclipseContext context = EclipseContextFactory.create(); - context.set("test_int", 12); - context.set("test_long", 124564523466L); - context.set("test_float", 12.34f); - context.set("test_double", 12.34534534563463466546d); - context.set("test_short", (short)10); - context.set("test_byte", (byte)55); - context.set("test_boolean", true); - context.set("test_char", 'a'); - - TestClass testClass = ContextInjectionFactory.make(TestClass.class, context); - - assertEquals(12, testClass.intField); - assertEquals(0, testClass.intFieldOptional); - assertEquals(124564523466L, testClass.longField); - assertEquals(12.34f, testClass.floatField, 0); - assertEquals(12.34534534563463466546d, testClass.doubleField, 0); - assertEquals((short)10, testClass.shortField); - assertEquals((byte)55, testClass.byteField); - assertEquals(true, testClass.booleanField); - assertEquals('a', testClass.charField); - - assertEquals(12, testClass.intArg); - assertEquals('a', testClass.charArg); - assertEquals(true, testClass.booleanArg); - - // test end-of-life reset of values - ContextInjectionFactory.uninject(testClass, context); - - // optional fields are reset to default; - // non-optional keep their values - assertEquals(12, testClass.intField); - assertEquals(0, testClass.intFieldOptional); // optional - assertEquals(124564523466L, testClass.longField); - assertEquals(12.34f, testClass.floatField, 0); - assertEquals(12.34534534563463466546d, testClass.doubleField, 0); - assertEquals((short)10, testClass.shortField); - assertEquals((byte)55, testClass.byteField); - assertEquals(false, testClass.booleanField); // optional - assertEquals('a', testClass.charField); - - assertEquals(12, testClass.intArg); - assertEquals('a', testClass.charArg); - assertEquals(true, testClass.booleanArg); - - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBridgeTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBridgeTest.java deleted file mode 100644 index 64cdf9d3d67..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectBridgeTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 BestSolution.at and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Tom Schindl - initial API and implementation - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertThrows; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.InjectionException; -import org.eclipse.e4.core.di.annotations.Execute; -import org.junit.jupiter.api.Test; - -public class InjectBridgeTest { - static class Base { - public void testInject(T value) { - - } - - public void testInvoke(T value) { - - } - } - - static class Concrete extends Base { - @Inject - @Override - public void testInject(String value) { - } - - @Override - @Execute - public void testInvoke(String value) { - - } - } - - @Test - public void testInjection() { - IEclipseContext context = EclipseContextFactory.create(); - context.set(String.class, "Value"); - - ContextInjectionFactory.make(Concrete.class, context); - } - - @Test - public void testInjectionFail() { - IEclipseContext context = EclipseContextFactory.create(); - context.set(Object.class, "Value"); - - assertThrows(InjectionException.class, () -> ContextInjectionFactory.make(Concrete.class, context)); - } - - @Test - public void testInvokation() { - IEclipseContext context = EclipseContextFactory.create(); - context.set(String.class, "Value"); - - ContextInjectionFactory.invoke(new Concrete(), Execute.class, context); - } - - @Test - public void testInvokationFail() { - IEclipseContext context = EclipseContextFactory.create(); - context.set(Object.class, "Value"); - assertThrows(InjectionException.class, () -> ContextInjectionFactory.invoke(new Concrete(), Execute.class, context)); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionOrderTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionOrderTest.java deleted file mode 100644 index 2ec946b3474..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionOrderTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.InjectionException; -import org.junit.jupiter.api.Test; - -public class InjectionOrderTest { - - public interface ITestObject { - - } - - static public class InjectUnsatisfied { - - @Inject - ITestObject object; - - static public int count = 0; - - @PostConstruct - public void postConstruct() { - count++; - object.toString(); - } - - @PreDestroy - public void preDestroy() { - count++; - object.toString(); - } - } - - /** - * Make sure special methods are not getting called in case injection failed - */ - @Test - public void testSpecialMethodOnFailure() { - IEclipseContext appContext = EclipseContextFactory.create(); - boolean exception = false; - try { - ContextInjectionFactory.make(InjectUnsatisfied.class, appContext); - } catch (InjectionException e) { - exception = true; - } - assertTrue(exception); - appContext.dispose(); - assertEquals(0, InjectUnsatisfied.count); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionResultLeakTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionResultLeakTest.java deleted file mode 100644 index 6ee8765a7ff..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InjectionResultLeakTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import java.lang.ref.WeakReference; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.junit.jupiter.api.Test; - -/** - * Test that we don't hold on to the values calculated during injection. - * This test relies on VM performing garbage collection in System.gc(). - * The actual VM processing of GC requests is up to the implementation - * so this test might not work on all VMs or might become invalid on - * future VMs. - */ -public class InjectionResultLeakTest { - - static class PartConsumer { - Object part; - - @Inject - void setPart(@Optional @Named("testGC") Object part) { - this.part = part; - } - } - - @Test - public void testLeaks() { - IEclipseContext context = EclipseContextFactory.create(); - WeakReference ref; - - Object part = new Object(); - ref = new WeakReference<>(part); - assertEquals(part, ref.get()); - - context.set("testGC", part); - - PartConsumer consumer = ContextInjectionFactory.make( - PartConsumer.class, context); - assertEquals(part, consumer.part); - - part = null; // another "let's help GC" statement - context.remove("testGC"); - - assertNull(consumer.part); - - // gc a few times - System.runFinalization(); - System.gc(); - System.runFinalization(); - System.gc(); - System.runFinalization(); - System.gc(); - System.runFinalization(); - System.gc(); - - // partA should have been gc'd - assertNull(ref.get(), "The object should have been garbage collected"); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTest.java deleted file mode 100644 index 199275cad78..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.di.annotations.Execute; -import org.junit.jupiter.api.Test; - -public class InvokeTest { - - /** - * Superclass - */ - static class TestSuperclass { - public int saveCount = 0; - @Execute - void something() { - saveCount++; - } - } - - /** - * Subclass - */ - static class TestSubclass extends TestSuperclass { - } - - /** - * Checks that superclass methods are called - */ - @Test - public void testSuperclassMethods() { - TestSubclass editor = new TestSubclass(); - ContextInjectionFactory.invoke(editor, Execute.class, EclipseContextFactory - .create()); - assertEquals(1, editor.saveCount); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTestMissingAnnotation.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTestMissingAnnotation.java deleted file mode 100644 index 1a3ece88a07..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/InvokeTestMissingAnnotation.java +++ /dev/null @@ -1,65 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014, 2016 vogella GmbH and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Lars Vogel - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertThrows; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.di.InjectionException; -import org.eclipse.e4.core.di.annotations.Execute; -import org.junit.jupiter.api.Test; - -/** - * Tests that that no method is called, it the @Execute annotation is not - * present and that an exception is thrown from the DI framework - */ -public class InvokeTestMissingAnnotation { - - /** - * Class to invoke for the test - */ - static class TestSuperclass { - public int saveCount = 0; - - // @Execute annotation missing on purpose - void execute() { - saveCount++; - } - } - - /** - * Checks that no methods is called and that an execution is thrown - */ - @Test - public void testCallMethodsWithMissingAnnotation() { - TestSuperclass editor = new TestSuperclass(); - assertThrows(InjectionException.class, - () -> ContextInjectionFactory.invoke(editor, Execute.class, EclipseContextFactory.create())); - } - - /** - * Checks that no methods is called and that no execution is thrown if a - * default is provide - */ - @Test - public void testCallMethodsWithMissingAnnotationNoExecution() { - TestSuperclass editor = new TestSuperclass(); - ContextInjectionFactory.invoke(editor, Execute.class, - EclipseContextFactory.create(), this); - - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RecursiveObjectCreationTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RecursiveObjectCreationTest.java deleted file mode 100644 index 5a52169967e..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RecursiveObjectCreationTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.annotation.PostConstruct; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.InjectionException; -import org.junit.jupiter.api.Test; - -public class RecursiveObjectCreationTest { - - static public class CheckSelfInject { - - public CheckSelfInject other; - - @Inject - public CheckSelfInject(CheckSelfInject other) { - this.other = other; - } - } - - /** - * Checks a simple case of constructor needing the same class - */ - @Test - public void testSelfInject() { - IEclipseContext context = EclipseContextFactory.create(); - boolean exceptionReceived = false; - try { - CheckSelfInject testInstance = ContextInjectionFactory.make(CheckSelfInject.class, context); - assertNotNull(testInstance); // unreachable - } catch (InjectionException e) { - exceptionReceived = true; - } - assertTrue(exceptionReceived); - } - - /////////////////////////////////////////////////////////////////////// - - static public class TestOuterClass { - - public class TestInnerClassInject { - @Inject - public TestInnerClassInject() { - // placeholder - } - } - - public TestInnerClassInject innerInject; - - @Inject - public TestOuterClass() { - // placeholder - } - - @PostConstruct - public void init(IEclipseContext context) { - innerInject = ContextInjectionFactory.make(TestInnerClassInject.class, context); - } - } - - /** - * Checks inner class using outer class which is still being created - */ - @Test - public void testNested() { - IEclipseContext context = EclipseContextFactory.create(); - boolean exceptionReceived = false; - try { - TestOuterClass outer = ContextInjectionFactory.make(TestOuterClass.class, context); - assertNotNull(outer); // unreachable - } catch (InjectionException e) { - exceptionReceived = true; - } - assertTrue(exceptionReceived); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RequestorTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RequestorTest.java deleted file mode 100644 index 185a009b73e..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/RequestorTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.lang.reflect.Field; - -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.InjectorFactory; -import org.eclipse.e4.core.internal.contexts.ContextObjectSupplier; -import org.eclipse.e4.core.internal.di.FieldRequestor; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("restriction") -public class RequestorTest { - public Object field; - public IEclipseContext context; - - @Test - public void testHashCode() throws Exception { - Field field = getClass().getField("field"); - assertNotNull(field); - FieldRequestor requestor = new FieldRequestor(field, - InjectorFactory.getDefault(), - ContextObjectSupplier.getObjectSupplier(context, - InjectorFactory.getDefault()), null, this, false); - int hash = requestor.hashCode(); - requestor.getReference().clear(); - assertEquals(hash, requestor.hashCode()); - } - - @BeforeEach - public void setUp() throws Exception { - context = EclipseContextFactory.create("RequestorTest"); - } - - @AfterEach - public void tearDown() throws Exception { - context.dispose(); - } - - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ComponentEnabler.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ComponentEnabler.java deleted file mode 100644 index 84288f476d5..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ComponentEnabler.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import org.osgi.service.component.ComponentContext; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; - -@Component(service = ComponentEnabler.class) -public class ComponentEnabler { - - ComponentContext context; - - @Activate - void activate(ComponentContext context) { - this.context = context; - } - - public void enableDisabledServiceA() { - this.context.enableComponent("DisabledServiceA"); - } - - public void disableDisabledServiceA() { - this.context.disableComponent("DisabledServiceA"); - } - - public void enableDisabledServiceB() { - this.context.enableComponent("DisabledServiceB"); - } - - public void disableDisabledServiceB() { - this.context.disableComponent("DisabledServiceB"); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceA.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceA.java deleted file mode 100644 index 02f9a9d6536..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceA.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import org.osgi.service.component.annotations.Component; - -@Component(name = "DisabledServiceA", enabled = false, property = { "component=disabled" }) -public class DisabledServiceA implements TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceB.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceB.java deleted file mode 100644 index 784f559ee16..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/DisabledServiceB.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.propertytypes.ServiceRanking; - -@Component(name = "DisabledServiceB", enabled = false, property = "component=disabled") -@ServiceRanking(5) -public class DisabledServiceB implements TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ExtendedSupplierInjectionTests.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ExtendedSupplierInjectionTests.java deleted file mode 100644 index 0123c02afe5..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ExtendedSupplierInjectionTests.java +++ /dev/null @@ -1,167 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Dictionary; -import java.util.Hashtable; - -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.di.extensions.EventTopic; -import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier; -import org.eclipse.e4.core.di.suppliers.IObjectDescriptor; -import org.eclipse.e4.core.di.suppliers.IRequestor; -import org.eclipse.e4.core.internal.di.osgi.ProviderHelper; -import org.eclipse.e4.core.internal.tests.di.extensions.InjectionEventTest.EventAdminHelper; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceRegistration; - -@SuppressWarnings("restriction") -public class ExtendedSupplierInjectionTests { - static final String TOPIC = "org/eclipse/e4/core/tests/di/extensions/ExtendedSupplierInjectionTests"; - static final String TOPIC_430041 = "org/eclipse/e4/core/tests/di/extensions/ExtendedSupplierInjectionTests430041"; - - static class EventTestObject { - static int count = 0; - - Object injectedObject; - - @Inject - @Optional - void dontExecute(@EventTopic(TOPIC) Object x) { - count++; - injectedObject = x; - } - } - - static class EventTestObject_430041 { - static int count = 0; - - Object injectedObject; - - private boolean destroyed; - - @Inject - @Optional - void dontExecute(@EventTopic(TOPIC_430041) Object x) { - count++; - injectedObject = x; - } - - @PreDestroy - void goDown() { - this.destroyed = true; - } - } - - private EventAdminHelper helper; - - @BeforeEach - public void setUp() { - InjectionEventTest.ensureEventAdminStarted(); - BundleContext bundleContext = FrameworkUtil.getBundle(getClass()) - .getBundleContext(); - IEclipseContext localContext = EclipseContextFactory - .getServiceContext(bundleContext); - helper = ContextInjectionFactory.make( - EventAdminHelper.class, localContext); - } - - /* Ensure extended suppliers are looked up first */ - @Test - public void testBug398728() { - IEclipseContext context = EclipseContextFactory.create(); - context.set(Object.class, new Object()); - - assertEquals(0, EventTestObject.count); - - EventTestObject target = ContextInjectionFactory.make( - EventTestObject.class, context); - // should be 0 since we haven't posted an event with this topic yet - assertEquals(0, EventTestObject.count); - - helper.sendEvent(TOPIC, "event1data"); - - assertEquals(1, EventTestObject.count); - assertEquals("event1data", target.injectedObject); - } - - @Test - public void testBug430041() { - IEclipseContext context = EclipseContextFactory.create(); - context.set(Object.class, new Object()); - - assertEquals(0, EventTestObject_430041.count); - - EventTestObject_430041 target = ContextInjectionFactory.make( - EventTestObject_430041.class, context); - context.set(EventTestObject_430041.class, target); - // should be 0 since we haven't posted an event with this topic yet - assertEquals(0, EventTestObject_430041.count); - - helper.sendEvent(TOPIC_430041, "event1data"); - - assertEquals(1, EventTestObject_430041.count); - assertEquals("event1data", target.injectedObject); - - context.dispose(); - assertTrue(target.destroyed); - - helper.sendEvent(TOPIC_430041, "event1data_disposed"); - - assertEquals(1, EventTestObject_430041.count); - assertEquals("event1data", target.injectedObject); - } - - /** bug 428837: ensure suppliers are ranked by service.ranking */ - @Test - public void testSupplierOrdering() { - BundleContext bc = FrameworkUtil.getBundle(getClass()) - .getBundleContext(); - ExtendedObjectSupplier supplier = new ExtendedObjectSupplier() { - @Override - public Object get(IObjectDescriptor descriptor, - IRequestor requestor, boolean track, boolean group) { - // TODO Auto-generated method stub - return null; - } - }; - Dictionary properties = new Hashtable<>(); - properties.put(ExtendedObjectSupplier.SERVICE_CONTEXT_KEY, - EventTopic.class.getName()); - properties.put(Constants.SERVICE_RANKING, 100); - ServiceRegistration sr = bc.registerService( - ExtendedObjectSupplier.SERVICE_NAME, supplier, properties); - try { - assertEquals(supplier, ProviderHelper.findProvider( - EventTopic.class.getName(), null)); - } finally { - sr.unregister(); - } - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceA.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceA.java deleted file mode 100644 index c480cdbc9b4..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceA.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.propertytypes.ServiceRanking; - -@Component(property = "filtervalue=Test") -@ServiceRanking(1) -public class FilterServiceA implements TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceB.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceB.java deleted file mode 100644 index 25610b5534f..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/FilterServiceB.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import org.osgi.service.component.annotations.Component; - -@Component(property="filtervalue=Test") -public class FilterServiceB implements TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionEventTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionEventTest.java deleted file mode 100644 index 2263b94c196..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionEventTest.java +++ /dev/null @@ -1,288 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - -import java.lang.reflect.InvocationTargetException; -import java.util.Dictionary; -import java.util.Hashtable; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.IInjector; -import org.eclipse.e4.core.di.InjectorFactory; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.di.extensions.EventTopic; -import org.eclipse.e4.core.di.internal.extensions.util.EventUtils; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleException; -import org.osgi.service.event.Event; -import org.osgi.service.event.EventAdmin; -import org.osgi.service.event.EventConstants; - -// TBD add auto-conversion? -@SuppressWarnings("restriction") -public class InjectionEventTest { - - static protected boolean testFailed = false; - - // Class used to test receiving events - static class InjectTarget { - public int counter1 = 0; - public int counter3 = 0; - - public String string1; - public String string3; - - public boolean valid = true; - - public MyBinding myBinding; - - public void resetCounters() { - counter1 = counter3 = 0; - } - - @Inject @Optional - public void receivedEvent1(@EventTopic("e4/test/event1") String string1) { - if (!valid) { - testFailed = true; - } - counter1++; - this.string1 = string1; - } - - @Inject - public void receivedOptionalEvent(MyBinding myBinding, @Optional @EventTopic("e4/test/event3") String string3) { - if (!valid) { - testFailed = true; - } - counter3++; - this.myBinding = myBinding; - this.string3 = string3; - } - } - - // Class used to test receiving events - static class InjectTargetEvent { - public int counter1 = 0; - public Event event; - - @Inject @Optional - public void receivedEvent1(@EventTopic("e4/test/eventInjection") Event event) { - counter1++; - this.event = event; - } - - } - - // Class used to test receiving events using wildcard - static class InjectStarEvent { - public int counter1 = 0; - public Event event; - - @Inject @Optional - public void receivedEvent1(@EventTopic("e4/test/*") Event event) { - counter1++; - this.event = event; - } - } - - // This tests and demos sending events - static public class EventAdminHelper { - @Inject - public EventAdmin eventAdmin; - - public void sendEvent(String topic, Object data) { - EventUtils.send(eventAdmin, topic, data); - } - - public void sendEvent(Event event) { - eventAdmin.sendEvent(event); - } - } - - // Tests mixed injection modes - @Singleton - static class MyBinding { - // static binding for injector - } - - private EventAdminHelper helper; - - @BeforeEach - public void setUp() throws Exception { - ensureEventAdminStarted(); - BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext(); - IEclipseContext localContext = EclipseContextFactory.getServiceContext(bundleContext); - helper = ContextInjectionFactory.make(EventAdminHelper.class, localContext); - } - - @Test - public void testEventInjection() throws InvocationTargetException, InstantiationException { - - IInjector injector = InjectorFactory.getDefault(); - injector.addBinding(MyBinding.class); - - IEclipseContext context = EclipseContextFactory.create(); - InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context); - - // initial state - assertEquals(0, target.counter1); - assertNull(target.string1); - assertEquals(1, target.counter3); - assertNull(target.string3); - assertNotNull(target.myBinding); - - // send event1 - helper.sendEvent("e4/test/event1", "event1data"); - - assertEquals(1, target.counter1); - assertEquals("event1data", target.string1); - assertEquals(1, target.counter3); - assertNull(target.string3); - assertNotNull(target.myBinding); - - // send event2 - helper.sendEvent("e4/test/event2", "event2data"); - - assertEquals(1, target.counter1); - assertEquals("event1data", target.string1); - assertEquals(1, target.counter3); - assertNull(target.string3); - assertNotNull(target.myBinding); - - // send event3 - helper.sendEvent("e4/test/event3", "event3data"); - - assertEquals(1, target.counter1); - assertEquals("event1data", target.string1); - assertEquals(2, target.counter3); - assertEquals("event3data", target.string3); - assertNotNull(target.myBinding); - - // send event1 again - helper.sendEvent("e4/test/event1", "abc"); - - assertEquals(2, target.counter1); - assertEquals("abc", target.string1); - assertEquals(2, target.counter3); - assertEquals("event3data", target.string3); - assertNotNull(target.myBinding); - } - - @Test - public void testInjectType() { - IEclipseContext context = EclipseContextFactory.create(); - InjectTargetEvent target = ContextInjectionFactory.make(InjectTargetEvent.class, context); - - // initial state - assertEquals(0, target.counter1); - assertNull(target.event); - - // send event - String eventTopic = "e4/test/eventInjection"; - Dictionary d = new Hashtable<>(); - d.put(EventConstants.EVENT_TOPIC, eventTopic); - d.put("data1", Integer.valueOf(5)); - d.put("data2", "sample"); - Event event = new Event(eventTopic, d); - helper.sendEvent(event); - - assertEquals(1, target.counter1); - assertEquals(event, target.event); - assertEquals(Integer.valueOf(5), target.event.getProperty("data1")); - assertEquals("sample", target.event.getProperty("data2")); - } - - // NOTE: this test relies on GC being actually done on the test object. - // Java does not guarantee that to happen, so, if this test starts to fail - // intermittently, feel free to comment it - @Test - public void testEventInjectionUnsubscribe() throws InvocationTargetException, InstantiationException { - IInjector injector = InjectorFactory.getDefault(); - injector.addBinding(MyBinding.class); - - wrapSetup(); // do it in a separate method to ease GC - System.gc(); - System.runFinalization(); - System.gc(); - helper.sendEvent("e4/test/event1", "wrong"); - assertFalse(testFailed); // target would have asserted if it is still subscribed - } - - @Test - public void testInjectWildCard() { - IEclipseContext context = EclipseContextFactory.create(); - InjectStarEvent target = ContextInjectionFactory.make(InjectStarEvent.class, context); - - // initial state - assertEquals(0, target.counter1); - assertNull(target.event); - - // send event - String eventTopic = "e4/test/eventInjection"; - Dictionary d = new Hashtable<>(); - d.put(EventConstants.EVENT_TOPIC, eventTopic); - d.put("data1", Integer.valueOf(5)); - d.put("data2", "sample"); - Event event = new Event(eventTopic, d); - helper.sendEvent(event); - - assertEquals(1, target.counter1); - assertEquals(event, target.event); - assertEquals(Integer.valueOf(5), target.event.getProperty("data1")); - assertEquals("sample", target.event.getProperty("data2")); - } - - private void wrapSetup() throws InvocationTargetException, InstantiationException { - IEclipseContext context = EclipseContextFactory.create(); - InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context); - // send event - helper.sendEvent("e4/test/event1", "event1data"); - assertEquals(1, target.counter1); - assertEquals("event1data", target.string1); - target.valid = false; - - } - - static void ensureEventAdminStarted() { - if (CoreTestsActivator.getDefault().getEventAdmin() == null) { - Bundle[] bundles = CoreTestsActivator.getDefault().getBundleContext().getBundles(); - for (Bundle bundle : bundles) { - if (!"org.eclipse.equinox.event".equals(bundle.getSymbolicName())) { - continue; - } - try { - bundle.start(Bundle.START_TRANSIENT); - } catch (BundleException e) { - e.printStackTrace(); - } - break; - } - } - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionMixedSuppliersTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionMixedSuppliersTest.java deleted file mode 100644 index 8cc8a39405a..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionMixedSuppliersTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.lang.reflect.InvocationTargetException; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.eclipse.core.runtime.preferences.InstanceScope; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.extensions.Preference; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.junit.jupiter.api.Test; -import org.osgi.service.prefs.BackingStoreException; - -public class InjectionMixedSuppliersTest { - - static class InjectTarget { - public String pref; - public String other; - - @Inject - public void setPrefs(@Named("testMixed") String otherString, @Preference("injectedPrefs") String string) { - pref = string; - other = otherString; - } - } - - @Test - public void testPreferencesQualifier() throws BackingStoreException, InvocationTargetException, InstantiationException { - IEclipseContext context = EclipseContextFactory.create(); - setPreference("injectedPrefs", "abc"); - context.set("testMixed", "other"); - InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context); - - // test - assertEquals("abc", target.pref); - assertEquals("other", target.other); - - // change - setPreference("injectedPrefs", "xyz"); - context.set("testMixed", "bingo"); - - // re-test - assertEquals("xyz", target.pref); - assertEquals("bingo", target.other); - } - - private void setPreference(String key, String value) throws BackingStoreException { - String nodePath = CoreTestsActivator.getDefault().getBundleContext().getBundle().getSymbolicName(); - IEclipsePreferences node = InstanceScope.INSTANCE.getNode(nodePath); - node.put(key, value); - node.flush(); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiHandlerTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiHandlerTest.java deleted file mode 100644 index e962b4cfc84..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiHandlerTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014, 2015 Markus Alexander Kuppe and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * - * Contributors: - * Markus Alexander Kuppe - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Execute; -import org.eclipse.e4.core.di.extensions.OSGiBundle; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.junit.jupiter.api.Test; -import org.osgi.framework.BundleContext; - -public class InjectionOSGiHandlerTest { - - public static class TestHandler { - - private BundleContext ctx; - - @Execute - public void execute(@OSGiBundle BundleContext ctx) { - this.ctx = ctx; - } - - public BundleContext getCtx() { - return ctx; - } - } - - @Test - public void testInjectBCinExecute() { - - final BundleContext bundleContext = CoreTestsActivator - .getDefault().getBundleContext(); - final IEclipseContext localContext = EclipseContextFactory - .getServiceContext(bundleContext); - - TestHandler handler = new TestHandler(); - ContextInjectionFactory.invoke(handler, Execute.class, localContext); - - assertNotNull(handler.getCtx()); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiTest.java deleted file mode 100644 index 02fe9ec819c..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionOSGiTest.java +++ /dev/null @@ -1,151 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013, 2015 Markus Alexander Kuppe and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Markus Alexander Kuppe - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.di.extensions.OSGiBundle; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleException; - -public class InjectionOSGiTest { - - // classed used as a user of the @OSGiBundle annotation - static class InjectionTarget { - - private BundleContext ctx; - - @Inject - public void setBundleContext( - @OSGiBundle @Optional BundleContext ctx) { - this.ctx = ctx; - } - - public boolean hasContext() { - return this.ctx != null; - } - - public BundleContext getContext() { - return this.ctx; - } - - private Bundle b; - - @Inject - public void setBundle( - @OSGiBundle Bundle b) { - this.b = b; - } - - public Bundle getBundle() { - return this.b; - } - - @Inject - public void setFoo(@OSGiBundle Object o) { - // make sure we don't fail when incompatible type requested - } - } - - // classed used as a user of the @OSGiBundle annotation - static class InjectionBundleTarget extends InjectionTarget { - } - - private InjectionTarget target; - private Bundle bundle; - - @AfterEach - public void tearDown() throws Exception { - bundle.start(); - - final BundleContext bundleContext = CoreTestsActivator - .getDefault().getBundleContext(); - final IEclipseContext localContext = EclipseContextFactory - .getServiceContext(bundleContext); - - ContextInjectionFactory.uninject(target, localContext); - } - - @BeforeEach - public void setUp() throws Exception { - final BundleContext bundleContext = CoreTestsActivator - .getDefault().getBundleContext(); - bundle = bundleContext.getBundle(); - - final IEclipseContext localContext = EclipseContextFactory - .getServiceContext(bundleContext); - - target = ContextInjectionFactory.make(InjectionTarget.class, - localContext); - } - - @Test - public void testInject() { - assertTrue(target.hasContext()); - } - - @Test - public void testUnInject() throws BundleException, InterruptedException { - // inject - assertTrue(target.hasContext()); - - // Check also that the BundleContext instance has indeed changed - final BundleContext firstContext = target - .getContext(); - - // uninject - bundle.stop(); - assertFalse(target.hasContext()); - - // re-inject - bundle.start(); - assertTrue(target.hasContext()); - - final BundleContext secondContext = target - .getContext(); - assertNotSame(firstContext, secondContext); - } - - @Test - public void testBundleInject() throws BundleException { - // inject - assertNotNull(target.getBundle()); - - // Contrary to the BC, the Bundle is available even for RESOLVED bundles - bundle.stop(); - - // not null but resolved _and_ still usable - assertNotNull(target.getBundle()); - assertTrue(target.getBundle().getState() == Bundle.RESOLVED); - assertNotNull(target.getBundle().getSymbolicName()); - - assertNull(target.getContext()); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionPreferencesTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionPreferencesTest.java deleted file mode 100644 index 059699d7592..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionPreferencesTest.java +++ /dev/null @@ -1,299 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - -import javax.inject.Inject; - -import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.eclipse.core.runtime.preferences.InstanceScope; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.di.extensions.Preference; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.junit.jupiter.api.Test; -import org.osgi.service.prefs.BackingStoreException; - -/** - * Note: we do not support byte arrays at this time. - */ -public class InjectionPreferencesTest { - - static final private String TEST_PREFS_KEY = "testPreferencesQualifier"; - static final private String TEST_PREFS_NODE = "org.eclipse.e4.core.javax.tests.ext"; - - static final private String KEY_INT = "testPreferencesInt"; - static final private String KEY_BOOL = "testPreferencesBoolean"; - static final private String KEY_DOUBLE = "testPreferencesDouble"; - static final private String KEY_FLOAT = "testPreferencesFloat"; - static final private String KEY_LONG = "testPreferencesLong"; -// static final private String KEY_BYTE_ARRAY = "testPreferencesByteArray"; - - static class InjectTarget { - public int counter = 0; - public int counterNode = 0; - public int counterOptional = 0; - - public String pref; - public String prefNode; - public String prefOptional1; - public String prefOptional2; - - @Inject - public void setPrefs(@Preference(TEST_PREFS_KEY) String string) { - counter++; - pref = string; - } - - @Inject - public void setPrefsNode(@Preference(value=TEST_PREFS_KEY, nodePath=TEST_PREFS_NODE) String string) { - counterNode++; - prefNode = string; - } - - @Inject - public void setOptionalPrefs(@Optional @Preference("something") String string1, @Preference(TEST_PREFS_KEY) String string2) { - counterOptional++; - prefOptional1 = string1; - prefOptional2 = string2; - } - } - - static class InjectTargetPrimitive { - @Inject @Preference(KEY_INT) - public int intField; - - @Inject @Preference(KEY_BOOL) - public boolean booleanField; - - @Inject @Preference(KEY_DOUBLE) - public double doubleField; - - @Inject @Preference(KEY_FLOAT) - public float floatField; - - @Inject @Preference(KEY_LONG) - public long longField; - -// @Inject @Preference(KEY_BYTE_ARRAY) -// public byte[] byteArrayField; - - public int intArg; - public boolean booleanArg; - - @Inject - public void set(@Preference(KEY_INT) int intArg, @Preference(KEY_BOOL) boolean booleanArg) { - this.intArg = intArg; - this.booleanArg = booleanArg; - } - } - - static class InjectTargetConversion { - @Inject @Preference(KEY_INT) - public Integer intField; - - @Inject @Preference(KEY_BOOL) - public Boolean booleanField; - - @Inject @Preference(KEY_DOUBLE) - public Double doubleField; - - @Inject @Preference(KEY_FLOAT) - public Float floatField; - - @Inject @Preference(KEY_LONG) - public Long longField; - - public IEclipsePreferences preferences; - - public Integer intArg; - public Boolean booleanArg; - - @Inject - public void set(@Preference(KEY_INT) Integer intArg, @Preference(KEY_BOOL) Boolean booleanArg) { - this.intArg = intArg; - this.booleanArg = booleanArg; - } - - @Inject - public void set2(@Preference IEclipsePreferences prefNode) { - preferences = prefNode; - prefNode.put("testOutValue", "abc"); - } - } - - static class InjectTargetConstructor { - public String pref; - public String prefNode; - - @Inject - public InjectTargetConstructor(@Preference(TEST_PREFS_KEY) String string, @Preference(value=TEST_PREFS_KEY, nodePath=TEST_PREFS_NODE) String stringNode) { - pref = string; - prefNode = stringNode; - - } - } - - @Test - public void testPreferencesQualifier() throws BackingStoreException { - setPreference(TEST_PREFS_KEY, "abc"); - setPreference(TEST_PREFS_KEY, TEST_PREFS_NODE, "123"); - IEclipseContext context = EclipseContextFactory.create(); - InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context); - // default node - assertEquals(1, target.counter); - assertEquals("abc", target.pref); - // specific node - assertEquals(1, target.counterNode); - assertEquals("123", target.prefNode); - // optional preference - assertEquals(1, target.counterOptional); - assertNull(target.prefOptional1); - assertEquals("abc", target.prefOptional2); - - // change - setPreference(TEST_PREFS_KEY, "xyz"); - setPreference(TEST_PREFS_KEY, TEST_PREFS_NODE, "456"); - - // default node - assertEquals(2, target.counter); - assertEquals("xyz", target.pref); - // specific node - assertEquals(2, target.counterNode); - assertEquals("456", target.prefNode); - // optional preference - assertEquals(2, target.counterOptional); - assertNull(target.prefOptional1); - assertEquals("xyz", target.prefOptional2); - } - - @Test - public void testBaseTypeConversion() throws BackingStoreException { - // setup preferences - String nodePath = CoreTestsActivator.getDefault().getBundleContext().getBundle().getSymbolicName(); - IEclipsePreferences node = InstanceScope.INSTANCE.getNode(nodePath); - node.putInt(KEY_INT, 12); - node.putBoolean(KEY_BOOL, true); - node.putDouble(KEY_DOUBLE, 12.35345345345d); - node.putFloat(KEY_FLOAT, 5.13f); - node.putLong(KEY_LONG, 131232343453453L); -// node.putByteArray(KEY_BYTE_ARRAY, new byte[] { 12, 34, 45, 67}); - node.flush(); - - IEclipseContext context = EclipseContextFactory.create(); - InjectTargetPrimitive target = ContextInjectionFactory.make(InjectTargetPrimitive.class, context); - - assertEquals(12, target.intField); - assertEquals(true, target.booleanField); - assertEquals(12.35345345345d, target.doubleField, 0); - assertEquals(5.13f, target.floatField, 0); - assertEquals(131232343453453L, target.longField); -// assertNotNull(target.byteArrayField); -// assertEquals(4, target.byteArrayField.length); -// assertEquals(12, target.byteArrayField[0]); -// assertEquals(34, target.byteArrayField[1]); -// assertEquals(45, target.byteArrayField[2]); -// assertEquals(67, target.byteArrayField[3]); - - assertEquals(12, target.intArg); - assertEquals(true, target.booleanArg); - - // change - node.putInt(KEY_INT, 777); - node.putBoolean(KEY_BOOL, false); - - assertEquals(777, target.intField); - assertEquals(false, target.booleanField); - - assertEquals(777, target.intArg); - assertEquals(false, target.booleanArg); - } - - @Test - public void testAutoConversion() throws BackingStoreException { - // setup preferences - String nodePath = CoreTestsActivator.getDefault().getBundleContext().getBundle().getSymbolicName(); - IEclipsePreferences node = InstanceScope.INSTANCE.getNode(nodePath); - node.putInt(KEY_INT, 12); - node.putBoolean(KEY_BOOL, true); - node.putDouble(KEY_DOUBLE, 12.35345345345d); - node.putFloat(KEY_FLOAT, 5.13f); - node.putLong(KEY_LONG, 131232343453453L); - node.flush(); - - IEclipseContext context = EclipseContextFactory.create(); - InjectTargetConversion target = ContextInjectionFactory.make(InjectTargetConversion.class, context); - - assertEquals(Integer.valueOf(12), target.intField); - assertEquals(Boolean.TRUE, target.booleanField); - assertEquals(Double.valueOf(12.35345345345d), target.doubleField); - assertEquals(Float.valueOf(5.13f), target.floatField); - assertEquals(Long.valueOf(131232343453453L), target.longField); - - assertEquals(Integer.valueOf(12), target.intArg); - assertEquals(Boolean.TRUE, target.booleanArg); - - // change - node.putInt(KEY_INT, 777); - node.putBoolean(KEY_BOOL, false); - - assertEquals(Integer.valueOf(777), target.intField); - assertEquals(Boolean.FALSE, target.booleanField); - - assertEquals(Integer.valueOf(777), target.intArg); - assertEquals(Boolean.FALSE, target.booleanArg); - - assertNotNull(target.preferences); - assertEquals("abc", node.get("testOutValue", null)); - } - - private void setPreference(String key, String value) throws BackingStoreException { - String nodePath = CoreTestsActivator.getDefault().getBundleContext().getBundle().getSymbolicName(); - IEclipsePreferences node = InstanceScope.INSTANCE.getNode(nodePath); - node.put(key, value); - node.flush(); - } - - private void setPreference(String key, String nodePath, String value) throws BackingStoreException { - IEclipsePreferences node = InstanceScope.INSTANCE.getNode(nodePath); - node.put(key, value); - node.flush(); - } - - @Test - public void testPreferencesConstructor() throws BackingStoreException { - setPreference(TEST_PREFS_KEY, "abc"); - setPreference(TEST_PREFS_KEY, TEST_PREFS_NODE, "123"); - IEclipseContext context = EclipseContextFactory.create(); - InjectTargetConstructor target = ContextInjectionFactory.make(InjectTargetConstructor.class, context); - // default node - assertEquals("abc", target.pref); - // specific node - assertEquals("123", target.prefNode); - - // change - setPreference(TEST_PREFS_KEY, "xyz"); - setPreference(TEST_PREFS_KEY, TEST_PREFS_NODE, "456"); - - // values should stay the same - no tracking for constructor injection - assertEquals("abc", target.pref); - assertEquals("123", target.prefNode); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionWithoutOSGITest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionWithoutOSGITest.java deleted file mode 100644 index 2f2137df531..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionWithoutOSGITest.java +++ /dev/null @@ -1,65 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.lang.reflect.InvocationTargetException; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; -import org.osgi.service.prefs.BackingStoreException; - -/** - * This test should be execute without an OSGI runtime running to verfiy BR - * 513883 - * - * @author jonas - */ -public class InjectionWithoutOSGITest { - - static class InjectTarget { - public String other; - - @Inject - public void setSth(@Named("testMixed") String otherString) { - other = otherString; - } - } - - @Test - public void testPreferencesQualifier() throws BackingStoreException, InvocationTargetException, InstantiationException { - IEclipseContext context = EclipseContextFactory.create(); - context.set("testMixed", "other"); - InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context); - - // test - assertEquals("other", target.other); - - // change - context.set("testMixed", "bingo"); - - // re-test - // assertEquals("xyz", target.pref); - assertEquals("bingo", target.other); - } - - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceA.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceA.java deleted file mode 100644 index 0f1ff94abdb..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceA.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.propertytypes.ServiceRanking; - -@Component -@ServiceRanking(50) -public class SampleServiceA implements TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceB.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceB.java deleted file mode 100644 index c0dc83f2b41..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/SampleServiceB.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.propertytypes.ServiceRanking; - -@Component -@ServiceRanking(40) -public class SampleServiceB implements TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ServiceSupplierTestCase.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ServiceSupplierTestCase.java deleted file mode 100644 index bf1f37d6a22..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ServiceSupplierTestCase.java +++ /dev/null @@ -1,321 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; - -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; -import java.util.function.BooleanSupplier; -import java.util.function.Supplier; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.di.extensions.Service; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; - -public class ServiceSupplierTestCase { - public static class TestBean { - volatile TestService service; - volatile List serviceList; - volatile int serviceInjectionCount; - volatile int serviceListInjectionCount; - volatile boolean updated; - volatile boolean listUpdated; - - @Inject - public void setService(@Service TestService service) { - this.service = service; - this.serviceInjectionCount++; - updated = true; - } - - @Inject - public void setServiceList(@Service List serviceList) { - this.serviceList = serviceList; - this.serviceListInjectionCount++; - listUpdated = true; - } - } - - public static class TestStaticFilterBean { - TestService service; - List serviceList; - int serviceInjectionCount; - int serviceListInjectionCount; - - @Inject - public void setService(@Service(filterExpression="(filtervalue=Test)") TestService service) { - this.service = service; - this.serviceInjectionCount++; - } - - @Inject - public void setServiceList(@Service(filterExpression="(filtervalue=Test)") List serviceList) { - this.serviceList = serviceList; - this.serviceListInjectionCount++; - } - } - - public static class TestDisabledBean { - @Inject - @Optional - @Service(filterExpression = "(component=disabled)") - volatile TestService disabledService; - - @Inject - @Service(filterExpression = "(component=disabled)") - volatile List services; - } - - private final List> registrations = new ArrayList<>(); - - @AfterEach - public void cleanup() { - this.registrations.forEach( ServiceRegistration::unregister); - } - - @Test - public void testInitialInject() { - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()); - TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext); - assertNotNull(bean.service); - assertNotNull(bean.serviceList); - assertSame(SampleServiceA.class, bean.service.getClass()); - assertEquals(1, bean.serviceInjectionCount); - - assertEquals(4, bean.serviceList.size()); - assertEquals(1, bean.serviceListInjectionCount); - assertSame(SampleServiceA.class, bean.serviceList.get(0).getClass()); - assertSame(SampleServiceB.class, bean.serviceList.get(1).getClass()); - - } - - @Test - public void testStaticFilter() { - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()); - TestStaticFilterBean bean = ContextInjectionFactory.make(TestStaticFilterBean.class, serviceContext); - - assertNotNull(bean.service); - assertNotNull(bean.serviceList); - - assertSame(FilterServiceA.class, bean.service.getClass()); - assertEquals(1, bean.serviceInjectionCount); - - assertEquals(2, bean.serviceList.size()); - assertEquals(1, bean.serviceListInjectionCount); - assertSame(FilterServiceA.class, bean.serviceList.get(0).getClass()); - assertSame(FilterServiceB.class, bean.serviceList.get(1).getClass()); - } - - @Test - @Timeout(value = 30) - public void testDynamicAdd() { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context); - TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext); - - assertEquals(1, bean.serviceInjectionCount); - assertEquals(1, bean.serviceListInjectionCount); - - TestService t = new TestService() { - // nothing todo - }; - - Hashtable properties = new Hashtable<>(); - properties.put("service.ranking", 10000); //$NON-NLS-1$ - bean.updated = false; - bean.listUpdated = false; - System.out.println("-------------------"); - this.registrations.add(context.registerService(TestService.class, t, properties)); - spinWait(() -> bean.updated && bean.listUpdated && bean.service == t); - System.out.println("-------------------"); - System.out.println("Final " + bean.service); - assertSame(t, bean.service); - assertEquals(2, bean.serviceInjectionCount); - - assertEquals(2, bean.serviceListInjectionCount); - assertEquals(5, bean.serviceList.size()); - assertSame(t, bean.serviceList.get(0)); - - TestService t2 = new TestService() { - // nothing todo - }; - - properties = new Hashtable<>(); - properties.put("service.ranking", Integer.valueOf(-1)); //$NON-NLS-1$ - bean.updated = false; - bean.listUpdated = false; - this.registrations.add(context.registerService(TestService.class, t2, properties)); - spinWait(() -> bean.updated && bean.listUpdated); - assertSame(t, bean.service); - assertEquals(3, bean.serviceInjectionCount); - - assertEquals(3, bean.serviceListInjectionCount); - - assertEquals(6, bean.serviceList.size()); - assertSame(t, bean.serviceList.get(0)); - } - - private void spinWait(BooleanSupplier condition) { - while (!condition.getAsBoolean() && !Thread.currentThread().isInterrupted()) { - Thread.onSpinWait(); - } - } - - @Test - public void testDynamicAddRemove() { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context); - TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext); - - assertEquals(1, bean.serviceInjectionCount); - assertEquals(1, bean.serviceListInjectionCount); - - TestService t = new TestService() { - // nothing todo - }; - - Hashtable properties = new Hashtable<>(); - properties.put("service.ranking", 52); //$NON-NLS-1$ - bean.updated = false; - bean.listUpdated = false; - this.registrations.add(context.registerService(TestService.class, t, properties)); - spinWait(() -> bean.updated && bean.listUpdated); - assertSame(t, bean.service); - assertEquals(2, bean.serviceInjectionCount); - - assertEquals(2, bean.serviceListInjectionCount); - assertEquals(5, bean.serviceList.size()); - assertSame(t, bean.serviceList.get(0)); - bean.updated = false; - bean.listUpdated = false; - ServiceRegistration registration = this.registrations.get(0); - registration.unregister(); - this.registrations.remove(registration); - spinWait(() -> bean.updated && bean.listUpdated); - assertEquals(3, bean.serviceInjectionCount); - assertEquals(3, bean.serviceListInjectionCount); - - assertSame(SampleServiceA.class, bean.service.getClass()); - assertEquals(4, bean.serviceList.size()); - assertSame(SampleServiceA.class, bean.serviceList.get(0).getClass()); - assertSame(SampleServiceB.class, bean.serviceList.get(1).getClass()); - } - - @Test - @Timeout(value = 30) - public void testCleanup() { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - IEclipseContext eclipseContext = EclipseContextFactory.getServiceContext(context); - IEclipseContext iec = eclipseContext.createChild(); - TestBean bean = ContextInjectionFactory.make(TestBean.class, iec); - TestBean bean2 = ContextInjectionFactory.make(TestBean.class, eclipseContext); - iec.dispose(); - - TestService t = new TestService() { - // nothing todo - }; - - Hashtable properties = new Hashtable<>(); - properties.put("service.ranking", 2); //$NON-NLS-1$ - bean2.listUpdated = false; - this.registrations.add(context.registerService(TestService.class, t, properties)); - spinWait(() -> bean2.listUpdated); - assertSame(SampleServiceA.class, bean.service.getClass()); - } - - @Test - public void testOptionalReferences() throws Exception { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context); - TestDisabledBean bean = ContextInjectionFactory.make(TestDisabledBean.class, serviceContext); - - assertNull(bean.disabledService); - assertEquals(0, bean.services.size()); - - ServiceReference ref = context.getServiceReference(ComponentEnabler.class); - ComponentEnabler enabler = context.getService(ref); - final int timeoutInMillis = 1000; - try { - enabler.enableDisabledServiceA(); - // wait for asynchronous service registry and injection to finish - waitForCondition(() -> bean.services.size() == 1 && bean.disabledService != null, timeoutInMillis); - assertNotNull(bean.disabledService); - assertEquals(1, bean.services.size()); - assertSame(DisabledServiceA.class, bean.disabledService.getClass()); - - enabler.enableDisabledServiceB(); - // wait for asynchronous service registry and injection to finish - waitForCondition(() -> bean.services.size() == 2 && bean.disabledService instanceof DisabledServiceB, - timeoutInMillis); - assertNotNull(bean.disabledService); - assertEquals(2, bean.services.size()); - assertSame(DisabledServiceB.class, bean.disabledService.getClass()); - - enabler.disableDisabledServiceB(); - // wait for asynchronous service registry and injection to finish - waitForCondition(() -> bean.services.size() == 1 && bean.disabledService instanceof DisabledServiceA, - timeoutInMillis); - assertNotNull(bean.disabledService); - assertEquals(1, bean.services.size()); - assertSame(DisabledServiceA.class, bean.disabledService.getClass()); - - enabler.disableDisabledServiceA(); - // wait for asynchronous service registry and injection to finish - waitForCondition(() -> bean.services.size() == 0 && bean.disabledService == null, timeoutInMillis); - assertNull(bean.disabledService); - assertEquals(0, bean.services.size()); - } finally { - enabler.disableDisabledServiceA(); - enabler.disableDisabledServiceB(); - // wait for asynchronous service registry and injection to ensure - // clear state after this test - waitForCondition(() -> bean.services.size() == 0 && bean.disabledService == null, timeoutInMillis); - } - } - - private void waitForCondition(Supplier condition, int timeoutInMillis) throws Exception { - long startTimeInMillis = System.currentTimeMillis(); - while (!condition.get() && System.currentTimeMillis() - startTimeInMillis < timeoutInMillis) { - Thread.sleep(20); - } - } - - @Test - @Disabled("See Bug 572546") - public void testUselessUpdates() throws InterruptedException { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context); - TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext); - assertEquals(1, bean.serviceInjectionCount); - assertEquals(1, bean.serviceListInjectionCount); - TestService current = bean.service; - TestService t = new TestService() { - // nothing todo - }; - - Hashtable properties = new Hashtable<>(); - properties.put("service.ranking", -1); //$NON-NLS-1$ - bean.updated = false; - bean.listUpdated = false; - this.registrations.add(context.registerService(TestService.class, t, properties)); - spinWait(() -> bean.listUpdated); - Thread.sleep(100); - assertEquals(current, bean.service); - assertEquals(1, bean.serviceInjectionCount); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/TestService.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/TestService.java deleted file mode 100644 index bdcdcecb96d..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/TestService.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.eclipse.e4.core.internal.tests.di.extensions; - -public interface TestService { - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ComputedValueLimitationTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ComputedValueLimitationTest.java deleted file mode 100644 index 5b5bc768e26..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ComputedValueLimitationTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.manual; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.junit.jupiter.api.Test; - - -/** - * This is a demo of a scenario when computed values don't work. The basic idea here is that a - * calculated value depends on something not stored in the context, it won't necessarily be updated. - * - * CalculatedValue = Function(ContextElement1, ..., ContextElement1N, ExtnernalFactor) - * - * In this scenario we deal with the Output = Function(arg1, ..., arg10, Time) - * - * We use a system timer here as an external input, but it can be pretty much anything not stored - * directly in the context. - */ -public class ComputedValueLimitationTest { - - - /** - * Used as an injection target - */ - public static class UserObject { - - private String txt; - - @Inject - public void Computed(String txt) { - this.txt = txt; - } - - public String getComputed() { - return txt; - } - } - - static public class ExtenralFactor { - static public int useChild() { - long time = System.currentTimeMillis(); - return ((int) time % 10); // this is incorrect but works for the example - } - } - - public static class CalcColor extends ContextFunction { - - @Override - public Object compute(IEclipseContext context, String contextKey) { - int useArg = ExtenralFactor.useChild(); - return context.get("arg" + useArg); - } - } - - public static class Time extends ContextFunction { - @Override - public Object compute(IEclipseContext context, String contextKey) { - context.get(String.valueOf(System.currentTimeMillis())); - return Long.valueOf(System.currentTimeMillis()); - } - } - - @Test - public synchronized void testInjection() { - - IEclipseContext context = EclipseContextFactory.create(); - for (int i = 0; i < 10; i++) { - context.set("arg" + i, Integer.toString(i)); - } - context.set("computed", new CalcColor()); - - UserObject userObject = new UserObject(); - ContextInjectionFactory.inject(userObject, context); - - for (int i = 0; i < 20; i++) { - int before = ExtenralFactor.useChild(); - String actual = userObject.getComputed(); - int after = ExtenralFactor.useChild(); - System.out.println("[" + before + "] actual: " + actual + " [" + after + "]"); - try { - wait(1); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - - @Test - public void testVolatileFunction() { - IEclipseContext context = EclipseContextFactory.create(); - context.set("time", new Time()); - long time = ((Long) context.get("time")).longValue(); - try { - Thread.sleep(100); - } catch (InterruptedException e) { - // - } - long newTime = ((Long) context.get("time")).longValue(); - assertTrue(time != newTime); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ContextExample.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ContextExample.java deleted file mode 100644 index efd20c3f4b5..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/ContextExample.java +++ /dev/null @@ -1,161 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.manual; - -import java.text.NumberFormat; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextFunction; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.contexts.RunAndTrack; -import org.eclipse.e4.core.internal.tests.CoreTestsActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; - -public class ContextExample { - static class Crayon { - @Inject - IPaletteService pallete; - - public void draw() { - if (pallete == null) - System.out.println("No palette"); - else - System.out.println("My pen is: " + pallete.getColor()); - } - } - - static enum Color { - RED, BLUE, YELLOW, GREEN, ORANGE, PURPLE; - } - - interface IPaletteService { - public Color getColor(); - } - - static class PaletteImpl implements IPaletteService { - private final Color color; - - PaletteImpl(Color color) { - this.color = color; - } - - @Override - public Color getColor() { - return color; - } - } - - static class ComplementaryColor extends ContextFunction { - @Override - public Object compute(IEclipseContext context, String contextKey) { - switch ((Color) context.get("color")) { - case RED: - return Color.GREEN; - case GREEN: - return Color.RED; - case BLUE: - return Color.ORANGE; - case ORANGE: - return Color.BLUE; - case YELLOW: - return Color.PURPLE; - case PURPLE: - return Color.YELLOW; - default: - return null; - } - } - } - - static class ResourceSelection extends ContextFunction { - @Override - public Object compute(IEclipseContext context, String contextKey) { - return null; - } - } - - public static void main(String[] arguments) { - new ContextExample().price(); - } - - public void run() { - IEclipseContext parent = EclipseContextFactory.create(); - parent.set("complement", new ComplementaryColor()); - IEclipseContext context = parent.createChild(); - context.set("color", Color.YELLOW); - Crayon crayon = new Crayon(); - ContextInjectionFactory.inject(crayon, context); - crayon.draw(); - } - - public void runWithService() { - BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext(); - ServiceRegistration reg = bundleContext.registerService(IPaletteService.class - .getName(), new PaletteImpl(Color.BLUE), null); - IEclipseContext context = EclipseContextFactory.getServiceContext(bundleContext); - Crayon crayon = new Crayon(); - ContextInjectionFactory.inject(crayon, context); - crayon.draw(); - reg.unregister(); - crayon.draw(); - } - - public void run2() { - IEclipseContext parent = EclipseContextFactory.create(); - parent.set("complement", new ComplementaryColor()); - IEclipseContext child = parent.createChild(); - child.set("color", Color.RED); - System.out.println(child.get("color")); - System.out.println(child.get("complement")); - - } - - public void run3() { - // IEclipseContext context = EclipseContextFactory.create(); - // Object[] args = new Object[] {IResource.class}; - // IResource[] resources = context.get("Selection", args); - } - - double total = 0; - - public void price() { - final IEclipseContext context = EclipseContextFactory.create(); - context.set("price", 19.99); - context.set("tax", 0.05); - context.runAndTrack(new RunAndTrack() { - @Override - public boolean changed(IEclipseContext context) { - total = (Double) context.get("price") * (1.0 + (Double) context.get("tax")); - return true; - } - - @Override - public String toString() { - return "calculator"; - } - }); - print(total); - context.set("tax", 0.07); - print(total); - } - - private void print(double price) { - System.out.println(NumberFormat.getCurrencyInstance().format(price)); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/InjectionErrorReportingTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/InjectionErrorReportingTest.java deleted file mode 100644 index 80325e049b1..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/manual/InjectionErrorReportingTest.java +++ /dev/null @@ -1,228 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Lars Vogel - Bug 474274 - *******************************************************************************/ -package org.eclipse.e4.core.internal.tests.manual; - -import static org.junit.jupiter.api.Assertions.assertThrows; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import javax.inject.Named; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.InjectionException; -import org.eclipse.e4.core.di.annotations.Creatable; -import org.junit.jupiter.api.Test; - -/** - * Manual test to observe error reporting. The JUnits in this - * test are expected to produce exceptions in the output stream. - */ -public class InjectionErrorReportingTest { - static class TestData { - } - - static class InjectedMethod { - public int setMethodCalled = 0; - public TestData value; - - @Inject - public void injectedMethod(@Named("testing123") TestData arg) { - setMethodCalled++; - value = arg; - } - } - - static class InjectedMethodNull { - public int setMethodCalled = 0; - public String nullString = null; - - @Inject - public int injectedMethod(@Named("testing") TestData arg) { - setMethodCalled++; - return nullString.length(); - } - } - - static class InjectedConstructor { - public int setMethodCalled = 0; - public TestData value; - - @Inject - public InjectedConstructor(@Named("testing123") TestData arg) { - setMethodCalled++; - value = arg; - } - } - - static class InjectedConstructorCast { - public int setMethodCalled = 0; - public TestData value; - public String nullString = "abc"; - - @Inject - public InjectedConstructorCast(@Named("testing") TestData arg) { - setMethodCalled++; - value = arg; - Object otherObject = new TestData(); - nullString = (String) otherObject; - } - } - - static class InjectedField { - @Inject @Named("testing123") - public TestData data; - } - - static class InjectedPostConstruct { - public int setMethodCalled = 0; - public String nullString = null; - public int length; - - @PostConstruct - public void myMethod() { - setMethodCalled++; - length = nullString.length(); - } - } - - static class InjectedPreDestroy { - public int setMethodCalled = 0; - public String nullString = null; - public int length; - - @Inject - public void injectedMethod(@Named("testing") TestData arg) { - setMethodCalled++; - } - - @PreDestroy - public void myMethod() { - setMethodCalled++; - length = nullString.length(); - } - } - - @Creatable - static class InjectedRecursive { - @Inject - public InjectedRecursive field; - } - - /** - * Shows the error message for an unresolved method argument - */ - @Test - public void testMethodInjectionError() { - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set("testing", methodValue); - InjectedMethod object = new InjectedMethod(); - assertThrows(InjectionException.class, () -> ContextInjectionFactory.inject(object, context)); - } - - /** - * Shows the error message in case method call throws an exception - */ - @Test - public void testMethodInjectionNullError() { - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set("testing", methodValue); - InjectedMethodNull object = new InjectedMethodNull(); - assertThrows(InjectionException.class, () -> ContextInjectionFactory.inject(object, context)); - } - - /** - * Shows the error message for an unresolved constructor argument - */ - @Test - public void testConstructorInjectionError() { - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set("testing", methodValue); - ContextInjectionFactory.make(InjectedConstructor.class, context); - assertThrows(InjectionException.class, - () -> ContextInjectionFactory.inject(InjectedConstructor.class, context)); - - } - - /** - * Shows the error message for an exception in the injected constructor - */ - @Test - public void testConstructorCastError() { - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set("testing", methodValue); - ContextInjectionFactory.make(InjectedConstructorCast.class, context); - assertThrows(InjectionException.class, - () -> ContextInjectionFactory.inject(InjectedConstructor.class, context)); - } - - /** - * Shows the error message for an unresolved field value - */ - @Test - public void testFieldInjectionError() { - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set("testing", methodValue); - InjectedField object = new InjectedField(); - assertThrows(InjectionException.class, () -> ContextInjectionFactory.inject(object, context)); - } - - /** - * Shows the error message in case @PostConstruct method call throws an exception - */ - @Test - public void testPostConstructError() { - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set("testing", methodValue); - ContextInjectionFactory.make(InjectedPostConstruct.class, context); - assertThrows(InjectionException.class, - () -> ContextInjectionFactory.inject(InjectedConstructor.class, context)); - } - - /** - * Shows the error message in case @PreDestory method call throws an exception - */ - @Test - public void testPreDestoryError() { - IEclipseContext context = EclipseContextFactory.create(); - TestData methodValue = new TestData(); - context.set("testing", methodValue); - assertThrows(InjectionException.class, () -> ContextInjectionFactory.inject(InjectedPreDestroy.class, context)); - context.dispose(); - } - - /** - * Manual test to check error message for recursive object creation Although - * bug 377343 disabled throwing InjectionExceptions on recursive creation, - * the fix for bug 457687 now exposes java.lang.Errors (such as - * StackOverflowError) rather than wrapping them in an InjectionException. - */ - @Test - public void testRecursionError() { - IEclipseContext context = EclipseContextFactory.create(); - ContextInjectionFactory.make(InjectedRecursive.class, context); - - context.set(InjectedRecursive.class, new InjectedRecursive()); - assertThrows(StackOverflowError.class, () -> ContextInjectionFactory.inject(InjectedRecursive.class, context)); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessages.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessages.java deleted file mode 100644 index 1b486932a38..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessages.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import java.text.MessageFormat; - -import javax.annotation.PostConstruct; - -/** - * Load messages from the OSGi resource bundle (OSGI-INF/l10n/bundle.properties) - */ -public class BundleMessages { - - //message as is - public String message; - - //message as is with underscore - public String message_one; - - //message as is camel cased - public String messageOne; - - //message with underscore transformed to . separated properties key - public String message_two; - - //camel cased message transformed to . separated properties key - public String messageThree; - - //message with placeholder - public String messageFour; - - // messages with camel case and underscore - public String messageFive_Sub; - public String messageSix_Sub; - public String messageSeven_Sub; - public String messageEight_Sub; - public String messageNine_Sub; - - @PostConstruct - public void format() { - messageFour = MessageFormat.format(messageFour, "Tom"); //$NON-NLS-1$ - } - - public String getMessage() { - return message; - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessagesRegistry.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessagesRegistry.java deleted file mode 100644 index d21c6138257..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/BundleMessagesRegistry.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import javax.inject.Inject; - -import org.eclipse.e4.core.di.annotations.Creatable; -import org.eclipse.e4.core.services.nls.BaseMessageRegistry; -import org.eclipse.e4.core.services.nls.Translation; - -@Creatable -public class BundleMessagesRegistry extends BaseMessageRegistry { - - @Override - @Inject - public void updateMessages(@Translation BundleMessages messages) { - super.updateMessages(messages); - } - -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/LocaleTransformationTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/LocaleTransformationTest.java deleted file mode 100644 index d4b4e5f90ef..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/LocaleTransformationTest.java +++ /dev/null @@ -1,168 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014, 2015 Dirk Fauth and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Dirk Fauth - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ -package org.eclipse.e4.core.internal.tests.nls; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Locale; - -import org.eclipse.e4.core.internal.services.ResourceBundleHelper; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("restriction") -public class LocaleTransformationTest { - - @Test - public void testValidLanguageCountryVariant() { - String localeString = "de_DE_EURO"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("de", locale.getLanguage()); - assertEquals("DE", locale.getCountry()); - assertEquals("EURO", locale.getVariant()); - } - - @Test - public void testValidLanguageCountry() { - String localeString = "de_DE"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("de", locale.getLanguage()); - assertEquals("DE", locale.getCountry()); - assertEquals("", locale.getVariant()); - } - - @Test - public void testValidLanguage() { - String localeString = "de"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("de", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("", locale.getVariant()); - } - - @Test - public void testValidCountry() { - String localeString = "_DE"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("", locale.getLanguage()); - assertEquals("DE", locale.getCountry()); - assertEquals("", locale.getVariant()); - } - - @Test - public void testValidLanguageVariant() { - String localeString = "de__EURO"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("de", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("EURO", locale.getVariant()); - } - - @Test - public void testValidVariant() { - String localeString = "__EURO"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("EURO", locale.getVariant()); - } - - @Test - public void testValidCountryVariant() { - String localeString = "_DE_EURO"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("", locale.getLanguage()); - assertEquals("DE", locale.getCountry()); - assertEquals("EURO", locale.getVariant()); - } - - @Test - public void testInvalidLanguage() { - Locale defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.ENGLISH); - - String localeString = "1234"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("en", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("", locale.getVariant()); - - Locale.setDefault(defaultLocale); - } - - @Test - public void testInvalidOneLetterLanguage() { - Locale defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.ENGLISH); - - String localeString = "a"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("en", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("", locale.getVariant()); - - Locale.setDefault(defaultLocale); - } - - @Test - public void testThreeLetterValidLanguage() { - String localeString = "kok"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("kok", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("", locale.getVariant()); - } - - @Test - public void testInvalidOneLetterCountry() { - Locale defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.ENGLISH); - - String localeString = "_X"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("en", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("", locale.getVariant()); - - Locale.setDefault(defaultLocale); - } - - @Test - public void testInvalidThreeLetterCountry() { - Locale defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.ENGLISH); - - String localeString = "_XXX"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("en", locale.getLanguage()); - assertEquals("", locale.getCountry()); - assertEquals("", locale.getVariant()); - - Locale.setDefault(defaultLocale); - } - - @Test - public void testValidNumericAreaCode() { - Locale defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.ENGLISH); - - String localeString = "_029"; - Locale locale = ResourceBundleHelper.toLocale(localeString); - assertEquals("", locale.getLanguage()); - assertEquals("029", locale.getCountry()); - assertEquals("", locale.getVariant()); - - Locale.setDefault(defaultLocale); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/MessageRegistryTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/MessageRegistryTest.java deleted file mode 100644 index ef111684b45..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/MessageRegistryTest.java +++ /dev/null @@ -1,170 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014, 2020 Dirk Fauth and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Dirk Fauth - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.nls; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.util.Locale; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.services.translation.TranslationService; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.osgi.framework.FrameworkUtil; - -public class MessageRegistryTest { - - static class TestObject { - @Inject - BundleMessagesRegistry registry; - } - - static class TestLocalizableObject { - private String localizableValue; - - public String getLocalizableValue() { - return localizableValue; - } - - public void setLocalizableValue(String localizableValue) { - this.localizableValue = localizableValue; - } - } - - private IEclipseContext context; - - @BeforeEach - public void setUp() { - this.context = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()); - ContextInjectionFactory.setDefault(context); - } - - @Test - public void testRegisterLocalizationByProperty() { - // ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestObject o = ContextInjectionFactory.make(TestObject.class, this.context); - - TestLocalizableObject control = new TestLocalizableObject(); - o.registry.registerProperty(control, "localizableValue", "message"); - - // test value is set - assertNotNull(control.getLocalizableValue()); - - // test the set value - assertEquals("BundleMessage", control.getLocalizableValue()); - } - - @Test - public void testRegisterLocalizationByMethod() { - // ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestObject o = ContextInjectionFactory.make(TestObject.class, this.context); - - TestLocalizableObject control = new TestLocalizableObject(); - o.registry.register(control, "setLocalizableValue", "message"); - - // test value is set - assertNotNull(control.getLocalizableValue()); - - // test the set value - assertEquals("BundleMessage", control.getLocalizableValue()); - } - - @Test - public void testRegisterLocalizationByMethodReference() { - // ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestObject o = ContextInjectionFactory.make(TestObject.class, this.context); - - TestLocalizableObject control = new TestLocalizableObject(); - o.registry.register(control::setLocalizableValue, m -> m.message); - - // test value is set - assertNotNull(control.getLocalizableValue()); - - // test the set value - assertEquals("BundleMessage", control.getLocalizableValue()); - } - - @Test - public void testRegisterLocalizationByPropertyAndChangeLocale() { - // ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestObject o = ContextInjectionFactory.make(TestObject.class, this.context); - - TestLocalizableObject control = new TestLocalizableObject(); - o.registry.registerProperty(control, "localizableValue", "message"); - - // test value is set - assertNotNull(control.getLocalizableValue()); - - // test the set value - assertEquals("BundleMessage", control.getLocalizableValue()); - - // change the locale to GERMAN - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - - assertEquals("BundleNachricht", control.getLocalizableValue()); - } - - @Test - public void testRegisterLocalizationByMethodAndChangeLocale() { - // ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestObject o = ContextInjectionFactory.make(TestObject.class, this.context); - - TestLocalizableObject control = new TestLocalizableObject(); - o.registry.register(control, "setLocalizableValue", "message"); - - // test value is set - assertNotNull(control.getLocalizableValue()); - - // test the set value - assertEquals("BundleMessage", control.getLocalizableValue()); - - // change the locale to GERMAN - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - - assertEquals("BundleNachricht", control.getLocalizableValue()); - } - - @Test - public void testRegisterLocalizationByMethodReferenceAndChangeLocale() { - // ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestObject o = ContextInjectionFactory.make(TestObject.class, this.context); - - TestLocalizableObject control = new TestLocalizableObject(); - o.registry.register(control::setLocalizableValue, BundleMessages::getMessage); - - // test value is set - assertNotNull(control.getLocalizableValue()); - - // test the set value - assertEquals("BundleMessage", control.getLocalizableValue()); - - // change the locale to GERMAN - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - - assertEquals("BundleNachricht", control.getLocalizableValue()); - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/Messages.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/Messages.java deleted file mode 100644 index 037b354098a..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/Messages.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import java.text.MessageFormat; - -import javax.annotation.PostConstruct; - -/** - * Load messages from a relative positioned resource bundle (./messages.properties) - * Used to verify the case sensitive filename handling under Linux - */ -public class Messages { - - //message as is - public String message; - - //message as is with underscore - public String message_one; - - //message as is camel cased - public String messageOne; - - //message with underscore transformed to . separated properties key - public String message_two; - - //camel cased message transformed to . separated properties key - public String messageThree; - - //message with placeholder - public String messageFour; - - // message with camel case and underscore - public String messageFive_Sub; - public String messageSix_Sub; - public String messageSeven_Sub; - public String messageEight_Sub; - public String messageNine_Sub; - - @PostConstruct - public void format() { - messageFour = MessageFormat.format(messageFour, "Tom"); //$NON-NLS-1$ - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/NLSTest.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/NLSTest.java deleted file mode 100644 index dbd673cd38b..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/NLSTest.java +++ /dev/null @@ -1,948 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014, 2015 Dirk Fauth and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Dirk Fauth - initial API and implementation - * Lars Vogel - Bug 474274 - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.nls; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.util.Locale; - -import javax.inject.Inject; - -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.EclipseContextFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.services.nls.Translation; -import org.eclipse.e4.core.services.translation.TranslationService; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.osgi.framework.FrameworkUtil; - -public class NLSTest { - - static class TestSimpleObject { - @Inject - @Translation - SimpleMessages simpleMessages; - } - - static class TestMessagesObject { - @Inject - @Translation - Messages Messages; - } - - static class TestBundleObject { - @Inject - @Translation - BundleMessages bundleMessages; - } - - static class TestResourceBundleClassObject { - @Inject - @Translation - ResourceBundleClassMessages bundleClassMessages; - } - - static class TestResourcesBundleObject { - @Inject - @Translation - ResourcesMessages resourcesMessages; - } - - private IEclipseContext context; - private Locale beforeLocale; - - @BeforeEach - public void setUp() { - this.context = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()); - ContextInjectionFactory.setDefault(context); - - beforeLocale = Locale.getDefault(); - //always set the locale to en prior a test case - Locale.setDefault(new Locale("en")); - } - - @AfterEach - public void tearDown() { - Locale.setDefault(beforeLocale); - } - - @Test - public void testSimpleMessages() { - //ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestSimpleObject o = ContextInjectionFactory.make(TestSimpleObject.class, this.context); - - SimpleMessages messages = o.simpleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("SimpleMessage", messages.message); - assertEquals("SimpleMessageUnderscore", messages.message_one); - assertEquals("SimpleMessageCamelCase", messages.messageOne); - assertEquals("SimpleMessageUnderscoreDot", messages.message_two); - assertEquals("SimpleMessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("SimpleMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", messages.messageSeven_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", messages.messageNine_Sub); - } - - @Test - public void testSimpleMessagesDifferentLocale() { - //set Locale to de - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - TestSimpleObject o = ContextInjectionFactory.make(TestSimpleObject.class, this.context); - - SimpleMessages messages = o.simpleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("SimpleNachricht", messages.message); - assertEquals("SimpleNachrichtUnderscore", messages.message_one); - assertEquals("SimpleNachrichtCamelCase", messages.messageOne); - assertEquals("SimpleNachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("SimpleMessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testSimpleMessagesSkipDefaultLocaleForEquinoxRoot() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set the locale to en - //as there is no _en properties file, by default the _de properties file would be loaded and we would - //get german translations as the default locale is set to "de_DE" - //with checking the equinox.root.locale in the system properties the fallback is skipped as it tells - //that the root properties file is for locale en. - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestSimpleObject o = ContextInjectionFactory.make(TestSimpleObject.class, this.context); - - SimpleMessages messages = o.simpleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //not the default resource bundle should be used (e.g. de when running on a machine with german OS) - //but the default resource bundle - assertEquals("SimpleMessage", messages.message); - assertEquals("SimpleMessageUnderscore", messages.message_one); - assertEquals("SimpleMessageCamelCase", messages.messageOne); - assertEquals("SimpleMessageUnderscoreDot", messages.message_two); - assertEquals("SimpleMessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("SimpleMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", messages.messageSeven_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("SimpleMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", messages.messageNine_Sub); - } - - @Test - public void testSimpleMessagesUseDefaultLocaleForInvalidLocale() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set a locale for which no resource bundle is set - this.context.set(TranslationService.LOCALE, Locale.FRENCH); - TestSimpleObject o = ContextInjectionFactory.make(TestSimpleObject.class, this.context); - - SimpleMessages messages = o.simpleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //the default resource bundle should be used - assertEquals("SimpleNachricht", messages.message); - assertEquals("SimpleNachrichtUnderscore", messages.message_one); - assertEquals("SimpleNachrichtCamelCase", messages.messageOne); - assertEquals("SimpleNachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("SimpleMessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("SimpleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testMessages() { - //ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestMessagesObject o = ContextInjectionFactory.make(TestMessagesObject.class, this.context); - - Messages messages = o.Messages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("Message", messages.message); - assertEquals("MessageUnderscore", messages.message_one); - assertEquals("MessageCamelCase", messages.messageOne); - assertEquals("MessageUnderscoreDot", messages.message_two); - assertEquals("MessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("MessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", messages.messageSeven_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", messages.messageNine_Sub); - } - - @Test - public void testMessagesDifferentLocale() { - //set Locale to de - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - TestMessagesObject o = ContextInjectionFactory.make(TestMessagesObject.class, this.context); - - Messages messages = o.Messages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("Nachricht", messages.message); - assertEquals("NachrichtUnderscore", messages.message_one); - assertEquals("NachrichtCamelCase", messages.messageOne); - assertEquals("NachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("MessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("NachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testMessagesSkipDefaultLocaleForEquinoxRoot() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set the locale to en - //as there is no _en properties file, by default the _de properties file would be loaded and we would - //get german translations as the default locale is set to "de_DE" - //with checking the equinox.root.locale in the system properties the fallback is skipped as it tells - //that the root properties file is for locale en. - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestMessagesObject o = ContextInjectionFactory.make(TestMessagesObject.class, this.context); - - Messages messages = o.Messages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //not the default resource bundle should be used (e.g. de when running on a machine with german OS) - //but the default resource bundle - assertEquals("Message", messages.message); - assertEquals("MessageUnderscore", messages.message_one); - assertEquals("MessageCamelCase", messages.messageOne); - assertEquals("MessageUnderscoreDot", messages.message_two); - assertEquals("MessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("MessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", messages.messageSeven_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("MessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", messages.messageNine_Sub); - } - - @Test - public void testSimpleMessagesRegionCode() { - // set Locale to "de_CH" - - Locale locale = new Locale("de", "CH"); - this.context.set(TranslationService.LOCALE, locale); - TestSimpleObject o = ContextInjectionFactory.make(TestSimpleObject.class, this.context); - - SimpleMessages messages = o.simpleMessages; - - // test if relevant values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.messageOne); - - // test the set relevant values - assertEquals("Region", messages.message); - assertEquals("RegionOne", messages.messageOne); - } - - @Test - public void testSimpleMessagesRegionAndVariantCode() { - // set Locale to "de_CH_TEST" - - Locale locale = new Locale("de", "CH", "TEST"); - this.context.set(TranslationService.LOCALE, locale); - TestSimpleObject o = ContextInjectionFactory.make(TestSimpleObject.class, this.context); - - SimpleMessages messages = o.simpleMessages; - - // test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.messageOne); - - // test the set values - assertEquals("RegionWithTestVariant", messages.message); - assertEquals("RegionWithTestVariantOne", messages.messageOne); - - locale = new Locale("de", "CH", "OTHER"); - this.context.set(TranslationService.LOCALE, locale); - TestSimpleObject otherO = ContextInjectionFactory.make(TestSimpleObject.class, this.context); - - SimpleMessages otherMessages = otherO.simpleMessages; - - // test the set values - assertEquals("RegionWithOtherVariant", otherMessages.message); - assertEquals("RegionWithOtherVariantOne", otherMessages.messageOne); - } - - @Test - public void testMessagesUseDefaultLocaleForInvalidLocale() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set a locale for which no resource bundle is set - this.context.set(TranslationService.LOCALE, Locale.FRENCH); - TestMessagesObject o = ContextInjectionFactory.make(TestMessagesObject.class, this.context); - - Messages messages = o.Messages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //the default resource bundle should be used - assertEquals("Nachricht", messages.message); - assertEquals("NachrichtUnderscore", messages.message_one); - assertEquals("NachrichtCamelCase", messages.messageOne); - assertEquals("NachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("MessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("NachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("NachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testBundleMessages() { - //ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestBundleObject o = ContextInjectionFactory.make(TestBundleObject.class, this.context); - - BundleMessages messages = o.bundleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("BundleMessage", messages.message); - assertEquals("BundleMessageUnderscore", messages.message_one); - assertEquals("BundleMessageCamelCase", messages.messageOne); - assertEquals("BundleMessageUnderscoreDot", messages.message_two); - assertEquals("BundleMessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("BundleMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", messages.messageSeven_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", messages.messageNine_Sub); - } - - @Test - public void testBundleMessagesDifferentLocale() { - //set Locale to de - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - TestBundleObject o = ContextInjectionFactory.make(TestBundleObject.class, this.context); - - BundleMessages messages = o.bundleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("BundleNachricht", messages.message); - assertEquals("BundleNachrichtUnderscore", messages.message_one); - assertEquals("BundleNachrichtCamelCase", messages.messageOne); - assertEquals("BundleNachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("BundleMessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testBundleMessagesSkipDefaultLocaleForEquinoxRoot() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set the locale to en - //as there is no _en properties file, by default the _de properties file would be loaded and we would - //get german translations as the default locale is set to "de_DE" - //with checking the equinox.root.locale in the system properties the fallback is skipped as it tells - //that the root properties file is for locale en. - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestBundleObject o = ContextInjectionFactory.make(TestBundleObject.class, this.context); - - BundleMessages messages = o.bundleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //not the default resource bundle should be used (e.g. de when running on a machine with german OS) - //but the default resource bundle - assertEquals("BundleMessage", messages.message); - assertEquals("BundleMessageUnderscore", messages.message_one); - assertEquals("BundleMessageCamelCase", messages.messageOne); - assertEquals("BundleMessageUnderscoreDot", messages.message_two); - assertEquals("BundleMessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("BundleMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", messages.messageSeven_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("BundleMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", messages.messageNine_Sub); - } - - @Test - public void testBundleMessagesUseDefaultLocaleForInvalidLocale() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set a locale for which no resource bundle is set - this.context.set(TranslationService.LOCALE, Locale.FRENCH); - TestBundleObject o = ContextInjectionFactory.make(TestBundleObject.class, this.context); - - BundleMessages messages = o.bundleMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //the default resource bundle should be used - assertEquals("BundleNachricht", messages.message); - assertEquals("BundleNachrichtUnderscore", messages.message_one); - assertEquals("BundleNachrichtCamelCase", messages.messageOne); - assertEquals("BundleNachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("BundleMessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("BundleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testClassBasedResourceBundle() { - //ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestResourceBundleClassObject o = ContextInjectionFactory.make(TestResourceBundleClassObject.class, this.context); - - ResourceBundleClassMessages messages = o.bundleClassMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("ResourceBundleClassMessage", messages.message); - assertEquals("ResourceBundleClassMessageUnderscore", messages.message_one); - assertEquals("ResourceBundleClassMessageCamelCase", messages.messageOne); - assertEquals("ResourceBundleClassMessageUnderscoreDot", messages.message_two); - assertEquals("ResourceBundleClassCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testClassBasedResourceBundleDifferentLocale() { - //set Locale to de - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - TestResourceBundleClassObject o = ContextInjectionFactory.make(TestResourceBundleClassObject.class, this.context); - - ResourceBundleClassMessages messages = o.bundleClassMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("ResourceBundleClassNachricht", messages.message); - assertEquals("ResourceBundleClassNachrichtUnderscore", messages.message_one); - assertEquals("ResourceBundleClassNachrichtCamelCase", messages.messageOne); - assertEquals("ResourceBundleNachrichtMessageUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("ResourceBundleClassCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testClassBasedResourceBundleSkipDefaultLocaleForEquinoxRoot() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set the locale to en - //as there is no _en properties file, by default the _de properties file would be loaded and we would - //get german translations as the default locale is set to "de_DE" - //with checking the equinox.root.locale in the system properties the fallback is skipped as it tells - //that the root properties file is for locale en. - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestResourceBundleClassObject o = ContextInjectionFactory.make(TestResourceBundleClassObject.class, this.context); - - ResourceBundleClassMessages messages = o.bundleClassMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //not the default resource bundle should be used (e.g. de when running on a machine with german OS) - //but the default resource bundle - assertEquals("ResourceBundleClassMessage", messages.message); - assertEquals("ResourceBundleClassMessageUnderscore", messages.message_one); - assertEquals("ResourceBundleClassMessageCamelCase", messages.messageOne); - assertEquals("ResourceBundleClassMessageUnderscoreDot", messages.message_two); - assertEquals("ResourceBundleClassCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourceBundleClassMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testClassBasedResourceBundleUseDefaultLocaleForInvalidLocale() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set a locale for which no resource bundle is set - this.context.set(TranslationService.LOCALE, Locale.FRENCH); - TestResourceBundleClassObject o = ContextInjectionFactory.make(TestResourceBundleClassObject.class, this.context); - - ResourceBundleClassMessages messages = o.bundleClassMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //the default resource bundle should be used - assertEquals("ResourceBundleClassNachricht", messages.message); - assertEquals("ResourceBundleClassNachrichtUnderscore", messages.message_one); - assertEquals("ResourceBundleClassNachrichtCamelCase", messages.messageOne); - assertEquals("ResourceBundleNachrichtMessageUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("ResourceBundleClassCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testResourcesBundle() { - //ensure the en Locale is set for this test - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestResourcesBundleObject o = ContextInjectionFactory.make(TestResourcesBundleObject.class, this.context); - - ResourcesMessages messages = o.resourcesMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("ResourcesMessage", messages.message); - assertEquals("ResourcesMessageUnderscore", messages.message_one); - assertEquals("ResourcesMessageCamelCase", messages.messageOne); - assertEquals("ResourcesMessageUnderscoreDot", messages.message_two); - assertEquals("ResourcesMessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testResourcesBundleDifferentLocale() { - //set Locale to de - this.context.set(TranslationService.LOCALE, Locale.GERMAN); - TestResourcesBundleObject o = ContextInjectionFactory.make(TestResourcesBundleObject.class, this.context); - - ResourcesMessages messages = o.resourcesMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //test the set values - assertEquals("ResourcesNachricht", messages.message); - assertEquals("ResourcesNachrichtUnderscore", messages.message_one); - assertEquals("ResourcesNachrichtCamelCase", messages.messageOne); - assertEquals("ResourcesNachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("ResourcesMessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testResourcesBundleSkipDefaultLocaleForEquinoxRoot() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set the locale to en - //as there is no _en properties file, by default the _de properties file would be loaded and we would - //get german translations as the default locale is set to "de_DE" - //with checking the equinox.root.locale in the system properties the fallback is skipped as it tells - //that the root properties file is for locale en. - this.context.set(TranslationService.LOCALE, Locale.ENGLISH); - TestResourcesBundleObject o = ContextInjectionFactory.make(TestResourcesBundleObject.class, this.context); - - ResourcesMessages messages = o.resourcesMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //not the default resource bundle should be used (e.g. de when running on a machine with german OS) - //but the default resource bundle - assertEquals("ResourcesMessage", messages.message); - assertEquals("ResourcesMessageUnderscore", messages.message_one); - assertEquals("ResourcesMessageCamelCase", messages.messageOne); - assertEquals("ResourcesMessageUnderscoreDot", messages.message_two); - assertEquals("ResourcesMessageCamelCaseDot", messages.messageThree); - assertEquals("The idea is from Tom", messages.messageFour); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourcesMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - - @Test - public void testResourcesBundleUseDefaultLocaleForInvalidLocale() { - //change the default Locale for this testcase - Locale.setDefault(new Locale("de")); - - //set a locale for which no resource bundle is set - this.context.set(TranslationService.LOCALE, Locale.FRENCH); - TestResourcesBundleObject o = ContextInjectionFactory.make(TestResourcesBundleObject.class, this.context); - - ResourcesMessages messages = o.resourcesMessages; - - //test all values are set - assertNotNull(messages); - assertNotNull(messages.message); - assertNotNull(messages.message_one); - assertNotNull(messages.messageOne); - assertNotNull(messages.message_two); - assertNotNull(messages.messageThree); - assertNotNull(messages.messageFour); - assertNotNull(messages.messageFive_Sub); - assertNotNull(messages.messageSix_Sub); - assertNotNull(messages.messageSeven_Sub); - assertNotNull(messages.messageEight_Sub); - assertNotNull(messages.messageNine_Sub); - - //the default resource bundle should be used - assertEquals("ResourcesNachricht", messages.message); - assertEquals("ResourcesNachrichtUnderscore", messages.message_one); - assertEquals("ResourcesNachrichtCamelCase", messages.messageOne); - assertEquals("ResourcesNachrichtUnderscoreDot", messages.message_two); - //for messageThree there is no key specified in de properties file, so there should be the fallback - assertEquals("ResourcesMessageCamelCaseDot", messages.messageThree); - assertEquals("Die Idee ist von Tom", messages.messageFour); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreOriginal", messages.messageFive_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeCamelCasified", messages.messageSix_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified", - messages.messageSeven_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeUnderscorified", messages.messageEight_Sub); - assertEquals("ResourcesNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified", - messages.messageNine_Sub); - } - -} \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass.java deleted file mode 100644 index 77dc951362a..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import java.util.ListResourceBundle; - -public class ResourceBundleClass extends ListResourceBundle { - @Override - protected Object[][] getContents() { - return new Object[][] { - new Object[] { "message", "ResourceBundleClassMessage" }, - new Object[] { "message_one", "ResourceBundleClassMessageUnderscore" }, - new Object[] { "messageOne", "ResourceBundleClassMessageCamelCase" }, - new Object[] { "message_two", "ResourceBundleClassMessageUnderscoreDot" }, - new Object[] { "messageThree", "ResourceBundleClassCamelCaseDot" }, - new Object[] { "messageFour", "The idea is from {0}" }, - new Object[] { "messageFive_Sub", "ResourceBundleClassMessageCamelCaseAndUnderscoreOriginal" }, - new Object[] { "message_six__sub", "ResourceBundleClassMessageCamelCaseAndUnderscoreDeCamelCasified" }, - new Object[] { "message.seven..sub", - "ResourceBundleClassMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified" }, - new Object[] { "messageEight.Sub", "ResourceBundleClassMessageCamelCaseAndUnderscoreDeUnderscorified" }, - new Object[] { "message_nine._sub", - "ResourceBundleClassMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified" } - }; - } -} \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClassMessages.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClassMessages.java deleted file mode 100644 index 3b99c4eb310..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClassMessages.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import java.text.MessageFormat; - -import javax.annotation.PostConstruct; - -import org.eclipse.e4.core.services.nls.Message; - -/** - * Load messages out of a class based resource bundle specified in the annotation. - */ -@Message(contributionURI = "bundleclass://org.eclipse.e4.core.javax.tests/org.eclipse.e4.core.internal.tests.nls.ResourceBundleClass") -public class ResourceBundleClassMessages { - - //message as is - public String message; - - //message as is with underscore - public String message_one; - - //message as is camel cased - public String messageOne; - - //message with underscore transformed to . separated properties key - public String message_two; - - //camel cased message transformed to . separated properties key - public String messageThree; - - //message with placeholder - public String messageFour; - - // message with camel case and underscore - public String messageFive_Sub; - public String messageSix_Sub; - public String messageSeven_Sub; - public String messageEight_Sub; - public String messageNine_Sub; - - @PostConstruct - public void format() { - messageFour = MessageFormat.format(messageFour, "Tom"); //$NON-NLS-1$ - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass_de.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass_de.java deleted file mode 100644 index 3506e37adbe..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourceBundleClass_de.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import java.util.ListResourceBundle; - -public class ResourceBundleClass_de extends ListResourceBundle { - @Override - protected Object[][] getContents() { - return new Object[][] { - new Object[] { "message", "ResourceBundleClassNachricht" }, - new Object[] { "message_one", "ResourceBundleClassNachrichtUnderscore" }, - new Object[] { "messageOne", "ResourceBundleClassNachrichtCamelCase" }, - new Object[] { "message_two", "ResourceBundleNachrichtMessageUnderscoreDot" }, - new Object[] { "messageFour", "Die Idee ist von {0}" }, - new Object[] { "messageFive_Sub", "ResourceBundleClassNachrichtCamelCaseAndUnderscoreOriginal" }, - new Object[] { "message_six__sub", "ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeCamelCasified" }, - new Object[] { "message.seven..sub", - "ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified" }, - new Object[] { "messageEight.Sub", "ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeUnderscorified" }, - new Object[] { "message_nine._sub", - "ResourceBundleClassNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified" } - }; - } -} \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourcesMessages.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourcesMessages.java deleted file mode 100644 index 4a5643c4789..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/ResourcesMessages.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import java.text.MessageFormat; - -import javax.annotation.PostConstruct; - -import org.eclipse.e4.core.services.nls.Message; - -/** - * Load messages from a resource folder in the plugin - */ -@Message(contributionURI = "platform:/plugin/org.eclipse.e4.core.javax.tests/resources/another") -public class ResourcesMessages { - - //message as is - public String message; - - //message as is with underscore - public String message_one; - - //message as is camel cased - public String messageOne; - - //message with underscore transformed to . separated properties key - public String message_two; - - //camel cased message transformed to . separated properties key - public String messageThree; - - //message with placeholder - public String messageFour; - - // message with camel case and underscore - public String messageFive_Sub; - public String messageSix_Sub; - public String messageSeven_Sub; - public String messageEight_Sub; - public String messageNine_Sub; - - @PostConstruct - public void format() { - messageFour = MessageFormat.format(messageFour, "Tom"); //$NON-NLS-1$ - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.java deleted file mode 100644 index eecea6f2224..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.eclipse.e4.core.internal.tests.nls; - -import java.text.MessageFormat; - -import javax.annotation.PostConstruct; - -/** - * Load messages from a relative positioned resource bundle (./SimpleMessages.properties) - */ -public class SimpleMessages { - - //message as is - public String message; - - //message as is with underscore - public String message_one; - - //message as is camel cased - public String messageOne; - - //message with underscore transformed to . separated properties key - public String message_two; - - //camel cased message transformed to . separated properties key - public String messageThree; - - //message with placeholder - public String messageFour; - - // message with camel case and underscore - public String messageFive_Sub; - public String messageSix_Sub; - public String messageSeven_Sub; - public String messageEight_Sub; - public String messageNine_Sub; - - @PostConstruct - public void format() { - messageFour = MessageFormat.format(messageFour, "Tom"); //$NON-NLS-1$ - } -} diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.properties deleted file mode 100644 index 7a0051d5c1f..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages.properties +++ /dev/null @@ -1,11 +0,0 @@ -message=SimpleMessage -message_one=SimpleMessageUnderscore -messageOne=SimpleMessageCamelCase -message.two=SimpleMessageUnderscoreDot -message.three=SimpleMessageCamelCaseDot -message.four=The idea is from {0} -messageFive_Sub=SimpleMessageCamelCaseAndUnderscoreOriginal -message_six__sub=SimpleMessageCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=SimpleMessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=SimpleMessageCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=SimpleMessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de.properties deleted file mode 100644 index 76c3c89335f..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de.properties +++ /dev/null @@ -1,10 +0,0 @@ -message=SimpleNachricht -message_one=SimpleNachrichtUnderscore -messageOne=SimpleNachrichtCamelCase -message.two=SimpleNachrichtUnderscoreDot -message.four=Die Idee ist von {0} -messageFive_Sub=SimpleNachrichtCamelCaseAndUnderscoreOriginal -message_six__sub=SimpleNachrichtCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=SimpleNachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=SimpleNachrichtCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=SimpleNachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH.properties deleted file mode 100644 index fba770fa7c2..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH.properties +++ /dev/null @@ -1,2 +0,0 @@ -message=Region -messageOne=RegionOne diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_OTHER.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_OTHER.properties deleted file mode 100644 index ba394356302..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_OTHER.properties +++ /dev/null @@ -1,2 +0,0 @@ -message=RegionWithOtherVariant -messageOne=RegionWithOtherVariantOne diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_TEST.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_TEST.properties deleted file mode 100644 index ca232d2609b..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/SimpleMessages_de_CH_TEST.properties +++ /dev/null @@ -1,2 +0,0 @@ -message=RegionWithTestVariant -messageOne=RegionWithTestVariantOne diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages.properties deleted file mode 100644 index 26442f03bd3..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages.properties +++ /dev/null @@ -1,11 +0,0 @@ -message=Message -message_one=MessageUnderscore -messageOne=MessageCamelCase -message.two=MessageUnderscoreDot -message.three=MessageCamelCaseDot -message.four=The idea is from {0} -messageFive_Sub=MessageCamelCaseAndUnderscoreOriginal -message_six__sub=MessageCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=MessageCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=MessageCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=MessageCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages_de.properties b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages_de.properties deleted file mode 100644 index a1b3501cbb1..00000000000 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/internal/tests/nls/messages_de.properties +++ /dev/null @@ -1,10 +0,0 @@ -message=Nachricht -message_one=NachrichtUnderscore -messageOne=NachrichtCamelCase -message.two=NachrichtUnderscoreDot -message.four=Die Idee ist von {0} -messageFive_Sub=NachrichtCamelCaseAndUnderscoreOriginal -message_six__sub=NachrichtCamelCaseAndUnderscoreDeCamelCasified -message.seven..sub=NachrichtCamelCaseAndUnderscoreDeCamelCasifiedAndDeUnderscorified -messageEight.Sub=NachrichtCamelCaseAndUnderscoreDeUnderscorified -message_nine._sub=NachrichtCamelCaseAndUnderscoreDeUnderscorifiedAndDeCamelCasified \ No newline at end of file diff --git a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/javax/tests/CoreTestSuite.java b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/javax/tests/CoreTestSuite.java index ae866893e32..898daf1d178 100644 --- a/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/javax/tests/CoreTestSuite.java +++ b/runtime/tests/org.eclipse.e4.core.javax.tests/src/org/eclipse/e4/core/javax/tests/CoreTestSuite.java @@ -16,53 +16,8 @@ package org.eclipse.e4.core.javax.tests; -import org.eclipse.e4.core.internal.tests.contexts.ActivationTest; -import org.eclipse.e4.core.internal.tests.contexts.ContextDynamicTest; -import org.eclipse.e4.core.internal.tests.contexts.DependenciesLeakTest; -import org.eclipse.e4.core.internal.tests.contexts.EclipseContextTest; -import org.eclipse.e4.core.internal.tests.contexts.NeutralValueTest; -import org.eclipse.e4.core.internal.tests.contexts.ReparentingTest; -import org.eclipse.e4.core.internal.tests.contexts.RunAndTrackTest; -import org.eclipse.e4.core.internal.tests.contexts.StrongIterableTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ActivationInjectionTest; import org.eclipse.e4.core.internal.tests.contexts.inject.AnnotationsInjectionTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.Bug317183Test; -import org.eclipse.e4.core.internal.tests.contexts.inject.ComplexDisposalTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ContextFunctionDynamicsTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ContextInjectionDisposeTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ContextInjectionFactoryTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ContextInjectionTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.DisposingReferencedContextTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ExtraDependenciesTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.GenericsInjectionTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.GroupedUpdatesTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.InjectStaticContextTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.InjectionUpdateTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.InvokeInRATTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ParentContextDisposalTest; import org.eclipse.e4.core.internal.tests.contexts.inject.ProviderInjectionTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.ServiceContextTest; -import org.eclipse.e4.core.internal.tests.contexts.inject.TestConstructorInjection; -import org.eclipse.e4.core.internal.tests.contexts.performance.ContextPerformanceTest; -import org.eclipse.e4.core.internal.tests.di.AutoConstructWithCreatableTest; -import org.eclipse.e4.core.internal.tests.di.DisposeClassLinkTest; -import org.eclipse.e4.core.internal.tests.di.InjectArraysTest; -import org.eclipse.e4.core.internal.tests.di.InjectBaseTypeTest; -import org.eclipse.e4.core.internal.tests.di.InjectBridgeTest; -import org.eclipse.e4.core.internal.tests.di.InjectionOrderTest; -import org.eclipse.e4.core.internal.tests.di.InjectionResultLeakTest; -import org.eclipse.e4.core.internal.tests.di.InvokeTest; -import org.eclipse.e4.core.internal.tests.di.InvokeTestMissingAnnotation; -import org.eclipse.e4.core.internal.tests.di.RecursiveObjectCreationTest; -import org.eclipse.e4.core.internal.tests.di.extensions.ExtendedSupplierInjectionTests; -import org.eclipse.e4.core.internal.tests.di.extensions.InjectionEventTest; -import org.eclipse.e4.core.internal.tests.di.extensions.InjectionMixedSuppliersTest; -import org.eclipse.e4.core.internal.tests.di.extensions.InjectionOSGiHandlerTest; -import org.eclipse.e4.core.internal.tests.di.extensions.InjectionOSGiTest; -import org.eclipse.e4.core.internal.tests.di.extensions.InjectionPreferencesTest; -import org.eclipse.e4.core.internal.tests.di.extensions.ServiceSupplierTestCase; -import org.eclipse.e4.core.internal.tests.nls.MessageRegistryTest; -import org.eclipse.e4.core.internal.tests.nls.NLSTest; import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; import org.junit.platform.suite.api.SuiteDisplayName; @@ -70,64 +25,10 @@ @Suite @SuiteDisplayName("CoreTestSuite (javax)") @SelectClasses({ - ExtendedSupplierInjectionTests.class, - InjectionPreferencesTest.class, - InjectionMixedSuppliersTest.class, - InjectionEventTest.class, - InjectionOSGiTest.class, - InjectionOSGiHandlerTest.class, - ServiceSupplierTestCase.class, - - // DI - InjectionOrderTest.class, - InvokeTest.class, - InjectBaseTypeTest.class, - InvokeTestMissingAnnotation.class, - InjectionResultLeakTest.class, - AutoConstructWithCreatableTest.class, - - // Contexts - StrongIterableTest.class, - EclipseContextTest.class, - ContextInjectionTest.class, - ContextInjectionDisposeTest.class, - ContextInjectionFactoryTest.class, - ContextDynamicTest.class, - ReparentingTest.class, - RunAndTrackTest.class, - ParentContextDisposalTest.class, - ComplexDisposalTest.class, - DisposeClassLinkTest.class, - InjectStaticContextTest.class, - ActivationTest.class, - NeutralValueTest.class, // Contexts injection AnnotationsInjectionTest.class, - TestConstructorInjection.class, - ServiceContextTest.class, ProviderInjectionTest.class, - InjectionUpdateTest.class, - DisposingReferencedContextTest.class, - InjectionOrderTest.class, - GroupedUpdatesTest.class, - ExtraDependenciesTest.class, - ContextFunctionDynamicsTest.class, - InjectArraysTest.class, - InjectBridgeTest.class, - InvokeInRATTest.class, - Bug317183Test.class, - DependenciesLeakTest.class, - ActivationInjectionTest.class, - GenericsInjectionTest.class, - RecursiveObjectCreationTest.class, - - // NLS - NLSTest.class, - MessageRegistryTest.class, - - // Performance - ContextPerformanceTest.class }) public class CoreTestSuite { } diff --git a/runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java b/runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java index c9b9d30d4e4..458c72bb8ff 100644 --- a/runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java +++ b/runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/AnnotationsInjectionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation and others. + * Copyright (c) 2009, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -16,7 +16,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import org.eclipse.e4.core.contexts.ContextInjectionFactory; @@ -30,12 +32,13 @@ import jakarta.annotation.PreDestroy; import jakarta.inject.Inject; import jakarta.inject.Named; +import jakarta.inject.Singleton; + /** * Tests for the basic context injection functionality */ public class AnnotationsInjectionTest { - @Test public void testContextSetOneArg() { class TestData { @@ -157,7 +160,8 @@ class TestData { // empty } class Injected { - @Inject @Named("valueField") + @Inject + @Named("valueField") Object injectedField; Object methodValue; @@ -198,6 +202,33 @@ public void injectedMethod(@Optional @Named("valueMethod") Object arg) { } } + static class OptionalAnnotations { + @Inject + @Optional + public Float f = null; + + public Double d; + public String s = "ouch"; + public Integer i; + + public int methodOptionalCalled = 0; + public int methodRequiredCalled = 0; + + @Inject + @Optional + public void methodOptional(Double d) { + this.d = d; + methodOptionalCalled++; + } + + @Inject + public void methodRequired(@Optional String s, Integer i) { + this.s = s; + this.i = i; + methodRequiredCalled++; + } + } + @Test public void testOptionalInjection() { Integer testInt = Integer.valueOf(123); @@ -260,7 +291,8 @@ public String something(@Optional String param) { } /** - * Tests that a class with multiple inherited post-construct / pre-destroy methods. + * Tests that a class with multiple inherited post-construct / pre-destroy + * methods. */ @Test public void testInheritedSpecialMethods() { @@ -362,4 +394,24 @@ public void setData(TestData arg) { assertNotNull(object.value); assertNotNull(object.directFieldInjection); } + + @Singleton + public static class SingletonService { + } + + public static class NonSingletonService { + } + + @Test + public void testSingleton() { + IEclipseContext context = EclipseContextFactory.create(); + + SingletonService service1 = ContextInjectionFactory.make(SingletonService.class, context); + SingletonService service2 = ContextInjectionFactory.make(SingletonService.class, context); + assertSame(service1, service2); + + NonSingletonService service3 = ContextInjectionFactory.make(NonSingletonService.class, context); + NonSingletonService service4 = ContextInjectionFactory.make(NonSingletonService.class, context); + assertNotSame(service3, service4); + } } diff --git a/runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java b/runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java deleted file mode 100644 index 86664632fe0..00000000000 --- a/runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/inject/OptionalAnnotations.java +++ /dev/null @@ -1,50 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2010 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - ******************************************************************************/ - -package org.eclipse.e4.core.internal.tests.contexts.inject; - -import org.eclipse.e4.core.di.annotations.Optional; - -import jakarta.inject.Inject; - -public class OptionalAnnotations { - - @Inject @Optional - public Float f = null; - - public Double d; - public String s = "ouch"; - public Integer i; - - public int methodOptionalCalled = 0; - public int methodRequiredCalled = 0; - - public OptionalAnnotations() { - // placehodler - } - - @Inject @Optional - public void methodOptional(Double d) { - this.d = d; - methodOptionalCalled++; - } - - @Inject - public void methodRequired(@Optional String s, Integer i) { - this.s = s; - this.i = i; - methodRequiredCalled++; - } - -}