Package: MockSystemRoleFactory
MockSystemRoleFactory
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
findRoles(Object, Class) |
|
|
|
|
|
||||||||||||||||||||
lambda$findRoles$0(Object, Map.Entry) |
|
|
|
|
|
||||||||||||||||||||
lambda$findRoles$1(Class, Map.Entry) |
|
|
|
|
|
||||||||||||||||||||
lambda$findRoles$2(Object, Map.Entry) |
|
|
|
|
|
||||||||||||||||||||
newInstance() |
|
|
|
|
|
||||||||||||||||||||
with(Class, Class, Function) |
|
|
|
|
|
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.test;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.HashMap;
30: import java.util.List;
31: import java.util.Map;
32: import java.util.function.Function;
33: import org.apiguardian.api.API;
34: import it.tidalwave.util.Pair;
35: import it.tidalwave.role.spi.SystemRoleFactory;
36: import lombok.RequiredArgsConstructor;
37: import static org.apiguardian.api.API.Status.EXPERIMENTAL;
38: import static java.util.stream.Collectors.*;
39: import static lombok.AccessLevel.PRIVATE;
40:
41: /***************************************************************************************************************************************************************
42: *
43: * A mock implementation of {@link SystemRoleFactory} for tests. It should be created by passing triples that associate a class type, a role type and a
44: * factory function:
45: * <pre>
46: * {@code
47: * final var rf = MockSystemRoleFactory.newInstance()
48: * .with(Datum.class, Role1.class, o -> List.of(new MockRole1a(o), new MockRole2(o)))
49: * .with(Datum.class, Role2.class, o -> List.of(new MockRole3(o)));
50: * }
51: * </pre>
52: *
53: * The function argument is the owner of the roles.
54: * This class must be installed before each test run (e.g. in a method annotated with {@code BeforeMethod}):
55: *
56: * <pre>
57: * {@code
58: * SystemRoleFactory.reset();
59: * SystemRoleFactory.set(MockSystemRoleFactory.newInstance().with(...));
60: * }
61: * </pre>
62: * @author Fabrizio Giudici
63: *
64: **************************************************************************************************************************************************************/
65: @API(status = EXPERIMENTAL)
66: @RequiredArgsConstructor(staticName = "of", access = PRIVATE)
67: public final class MockSystemRoleFactory implements SystemRoleFactory
68: {
69: @Nonnull
70: private final Map<Pair<Class<?>, Class<?>>, Function<Object, List<?>>> rolesByType;
71:
72: /***********************************************************************************************************************************************************
73: * {@return a new instance of the factory}.
74: **********************************************************************************************************************************************************/
75: @Nonnull
76: public static MockSystemRoleFactory newInstance()
77: {
78: return of(Map.of());
79: }
80:
81: /***********************************************************************************************************************************************************
82: * {@inheritDoc}
83: **********************************************************************************************************************************************************/
84: @Override @Nonnull @SuppressWarnings("unchecked")
85: public <R> List<R> findRoles (@Nonnull final Object owner, @Nonnull final Class<? extends R> roleType)
86: {
87: return (List<R>)rolesByType.entrySet().stream()
88: .filter(e -> e.getKey().a.isAssignableFrom(owner.getClass()))
89: .filter(e -> e.getKey().b.isAssignableFrom(roleType))
90: .flatMap(e -> e.getValue().apply(owner).stream())
91: .collect(toList());
92: }
93:
94: /***********************************************************************************************************************************************************
95: * {@return a new factory that supports the given owner and role type}.
96: * @param <T> the static type of the owner
97: * @param <R> the static type of the role
98: * @param ownerType the type of the owner
99: * @param roleType the type of the role
100: * @param roleFactory a factory function that instantiates the roles
101: **********************************************************************************************************************************************************/
102: @Nonnull
103: public <T, R> MockSystemRoleFactory with (@Nonnull final Class<T> ownerType,
104: @Nonnull final Class<R> roleType,
105: @Nonnull final Function<T, List<R>> roleFactory)
106: {
107: final var map = new HashMap<>(rolesByType);
108: map.put(Pair.of(ownerType, roleType), (Function<Object, List<?>>)(Function)roleFactory);
109: return of(map);
110: }
111: }