blob: 89d011369002789ee5b5f37e5c96d198687bb411 [file] [log] [blame]
Pierre De Ropfaca2892016-01-31 23:27:05 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.dm.lambda.itest;
20
21import static org.apache.felix.dm.lambda.DependencyManagerActivator.aspect;
22import static org.apache.felix.dm.lambda.DependencyManagerActivator.component;
23
24import org.apache.felix.dm.Component;
25import org.apache.felix.dm.DependencyManager;
26
27
28/**
29 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
30 */
31public class AspectServiceDependencyWithSwapCallbackTest extends TestBase {
32 public void testServiceRegistrationAndConsumption() {
33 DependencyManager m = getDM();
34 // helper class that ensures certain steps get executed in sequence
35 Ensure e = new Ensure();
36 // create a service provider and consumer
37 Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
Pierre De Rop11527502016-02-18 21:07:16 +000038 Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s->s.add("add").remove("remove").swap("swap")).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000039 Component asp = aspect(m, ServiceInterface.class).rank(100).impl(ServiceProviderAspect.class).build();
40 m.add(sp);
41 m.add(sc);
42 m.add(asp);
43 m.remove(asp);
44 m.remove(sc);
45 m.remove(sp);
46
47 // ensure we executed all steps inside the component instance
48 e.step(7);
49 }
50
51 public void testServiceRegistrationAndConsumptionRef() {
52 DependencyManager m = getDM();
53 // helper class that ensures certain steps get executed in sequence
54 Ensure e = new Ensure();
55 // create a service provider and consumer
56 ServiceConsumer scimpl = new ServiceConsumer(e);
57 Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
Pierre De Rop11527502016-02-18 21:07:16 +000058 Component sc = component(m).impl(scimpl).withSvc(ServiceInterface.class, s->s.add(scimpl::add).remove(scimpl::remove).swap(scimpl::swap)).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000059 Component asp = aspect(m, ServiceInterface.class).rank(100).impl(ServiceProviderAspect.class).build();
60 m.add(sp);
61 m.add(sc);
62 m.add(asp);
63 m.remove(asp);
64 m.remove(sc);
65 m.remove(sp);
66
67 // ensure we executed all steps inside the component instance
68 e.step(7);
69 }
70
71 static interface ServiceInterface {
72 public void invoke(String caller);
73 }
74
75 static class ServiceProvider implements ServiceInterface {
76 private final Ensure m_ensure;
77 public ServiceProvider(Ensure e) {
78 m_ensure = e;
79 }
80 public void invoke(String caller) {
81 if (caller.equals("consumer.init")) {
82 m_ensure.step(3);
83 } else if (caller.equals("aspect.consumer.add")) {
84 m_ensure.step(5);
85 }
86 }
87 }
88
89 static class ServiceProviderAspect implements ServiceInterface {
90 private volatile ServiceInterface m_service;
91
92 public ServiceProviderAspect() {
93 }
94
95 @Override
96 public void invoke(String caller) {
97 m_service.invoke("aspect." + caller);
98 }
99 }
100
101 static class ServiceConsumer {
102 private volatile ServiceInterface m_service;
103 private final Ensure m_ensure;
104 private int swapCount = 0;
105
106 public ServiceConsumer(Ensure e) {
107 m_ensure = e;
108 }
109
110 public void init() {
111 m_ensure.step(2);
112 m_service.invoke("consumer.init");
113 }
114
115 public void destroy() {
116 m_ensure.step(6);
117 }
118
119 public void add(ServiceInterface service) {
120 m_service = service;
121 m_ensure.step(1);
122 }
123
124 public void remove(ServiceInterface service) {
125 m_service = null;
126 }
127
128 public void swap(ServiceInterface previous, ServiceInterface current) {
129 switch (swapCount) {
130 case 0: m_ensure.step(4);
131 break;
132 case 1: m_ensure.step(5);
133 break;
134 default:
135 }
136 m_service = current;
137 swapCount ++;
138 }
139 }
140
141}