You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by pl...@apache.org on 2017/11/05 13:58:13 UTC
incubator-tamaya-extensions git commit: [TAMAYA-291] Added tests for
CDIAwareServiceContext.
Repository: incubator-tamaya-extensions
Updated Branches:
refs/heads/master 864f02006 -> 0edb58a3b
[TAMAYA-291] Added tests for CDIAwareServiceContext.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/0edb58a3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/0edb58a3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/0edb58a3
Branch: refs/heads/master
Commit: 0edb58a3bba4795ddc635e48e70edf8403db9f81
Parents: 864f020
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Sun Nov 5 14:55:27 2017 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Sun Nov 5 14:58:01 2017 +0100
----------------------------------------------------------------------
modules/injection/cdi/pom.xml | 4 ++
.../tamaya/cdi/CDIAwareServiceContext.java | 14 ++++++-
.../tamaya/cdi/CDIAwareServiceContextTest.java | 39 ++++++++++++++++++++
.../org/apache/tamaya/cdi/DominoService.java | 25 +++++++++++++
.../apache/tamaya/cdi/DominoServiceImpl.java | 22 +++++++++++
.../org/apache/tamaya/cdi/TartanService.java | 25 +++++++++++++
.../apache/tamaya/cdi/TartanServiceOneImpl.java | 25 +++++++++++++
.../apache/tamaya/cdi/TartanServiceTwoImpl.java | 25 +++++++++++++
.../org/apache/tamaya/cdi/VertigoService.java | 25 +++++++++++++
.../apache/tamaya/cdi/VertigoServiceImpl.java | 22 +++++++++++
.../org.apache.tamaya.cdi.TartanService | 20 ++++++++++
.../org.apache.tamaya.cdi.VertigoService | 19 ++++++++++
12 files changed, 264 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/pom.xml
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/pom.xml b/modules/injection/cdi/pom.xml
index c7ecc15..74062d1 100644
--- a/modules/injection/cdi/pom.xml
+++ b/modules/injection/cdi/pom.xml
@@ -78,6 +78,10 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java
index f367f71..20c3bbd 100644
--- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java
+++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java
@@ -61,6 +61,7 @@ public class CDIAwareServiceContext implements ServiceContext {
@Override
public <T> T getService(Class<T> serviceType) {
Object cached = singletons.get(serviceType);
+
if (cached == null) {
Collection<T> services = getServices(serviceType);
if (services.isEmpty()) {
@@ -68,7 +69,7 @@ public class CDIAwareServiceContext implements ServiceContext {
} else {
cached = getServiceWithHighestPriority(services, serviceType);
}
- if(cached!=null) {
+ if (cached != null) {
singletons.put(serviceType, cached);
}
}
@@ -203,4 +204,15 @@ public class CDIAwareServiceContext implements ServiceContext {
return 20;
}
+ /**
+ * <p>Checks the internal used cache contains already instances
+ * of the given type.</p>
+ *
+ * @param clazz type to be checked if it is already in the internal cache.
+ * @return {@code true} if there are cached instances of the requested type,
+ * otherwise {@code false}.
+ */
+ protected boolean isCached(Class<?> clazz) {
+ return singletons.containsKey(clazz);
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/CDIAwareServiceContextTest.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/CDIAwareServiceContextTest.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/CDIAwareServiceContextTest.java
index 5616b00..5679f66 100644
--- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/CDIAwareServiceContextTest.java
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/CDIAwareServiceContextTest.java
@@ -18,6 +18,45 @@
*/
package org.apache.tamaya.cdi;
+import org.junit.Test;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
public class CDIAwareServiceContextTest {
+ private CDIAwareServiceContext context = new CDIAwareServiceContext();
+
+ @Test
+ public void getServiceReturnsNonCachedInstanceAtFirstCall() {
+ assertThat(context.isCached(VertigoService.class)).isFalse();
+
+ VertigoService service = context.getService(VertigoService.class);
+
+ assertThat(service).isNotNull();
+ assertThat(context.isCached(VertigoService.class)).isTrue();
+ }
+
+ @Test
+ public void getServiceReturnsOnSecondCallCachedInstance() throws Exception {
+ VertigoService service1 = context.getService(VertigoService.class);
+ assertThat(context.isCached(VertigoService.class)).isTrue();
+
+ VertigoService service2 = context.getService(VertigoService.class);
+
+ assertThat(service2).isNotNull().isSameAs(service1);
+ }
+
+ @Test
+ public void getServiceReturnsNullIfThereAreNoRegisteredSPIInstancesAvailable() throws Exception {
+ DominoService service = context.getService(DominoService.class);
+
+ assertThat(service).isNull();
+ }
+
+ @Test
+ public void getServiceReturnsInstanceWithHighestPriority() throws Exception {
+ TartanService service = context.getService(TartanService.class);
+ assertThat(service).isNotNull();
+ assertThat(service).isInstanceOf(TartanServiceTwoImpl.class);
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoService.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoService.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoService.java
new file mode 100644
index 0000000..39da349
--- /dev/null
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoService.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.cdi;
+
+public interface DominoService {
+ default String getName() {
+ return "Vertigo";
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoServiceImpl.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoServiceImpl.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoServiceImpl.java
new file mode 100644
index 0000000..27280fe
--- /dev/null
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/DominoServiceImpl.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.cdi;
+
+public class DominoServiceImpl implements DominoService {
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanService.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanService.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanService.java
new file mode 100644
index 0000000..22935d6
--- /dev/null
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanService.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.cdi;
+
+public interface TartanService {
+ default String getName() {
+ return "Tartan";
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceOneImpl.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceOneImpl.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceOneImpl.java
new file mode 100644
index 0000000..9138992
--- /dev/null
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceOneImpl.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.cdi;
+
+import javax.annotation.Priority;
+
+@Priority(100)
+public class TartanServiceOneImpl implements TartanService {
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceTwoImpl.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceTwoImpl.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceTwoImpl.java
new file mode 100644
index 0000000..5fb8766
--- /dev/null
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/TartanServiceTwoImpl.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.cdi;
+
+import javax.annotation.Priority;
+
+@Priority(200)
+public class TartanServiceTwoImpl implements TartanService {
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoService.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoService.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoService.java
new file mode 100644
index 0000000..7e2bc80
--- /dev/null
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoService.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.cdi;
+
+public interface VertigoService {
+ default String getName() {
+ return "Vertigo";
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoServiceImpl.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoServiceImpl.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoServiceImpl.java
new file mode 100644
index 0000000..a61fec1
--- /dev/null
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/VertigoServiceImpl.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.cdi;
+
+public class VertigoServiceImpl implements VertigoService {
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.TartanService
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.TartanService b/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.TartanService
new file mode 100644
index 0000000..b7392c2
--- /dev/null
+++ b/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.TartanService
@@ -0,0 +1,20 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy current the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+org.apache.tamaya.cdi.TartanServiceOneImpl
+org.apache.tamaya.cdi.TartanServiceTwoImpl
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0edb58a3/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.VertigoService
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.VertigoService b/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.VertigoService
new file mode 100644
index 0000000..4c87bcf
--- /dev/null
+++ b/modules/injection/cdi/src/test/resources/META-INF/services/org.apache.tamaya.cdi.VertigoService
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy current the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+org.apache.tamaya.cdi.VertigoServiceImpl
\ No newline at end of file