You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/08/27 09:25:17 UTC
[isis] branch master updated: ISIS-1845 adding tests for _Context
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 445dabb ISIS-1845 adding tests for _Context
445dabb is described below
commit 445dabb41f5641922e9ae1c4b20fc1fc12363786
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Aug 27 11:25:13 2018 +0200
ISIS-1845 adding tests for _Context
also moving some tests from 'applib' to 'commons'
---
.../isis/commons/internal/base/BytesTest.java | 0
.../isis/commons/internal/base/NullSafeTest.java | 0
.../isis/commons/internal/base/ReductionTest.java | 0
.../isis/commons/internal/base/StringsTest.java | 0
.../commons/internal/compare/ComparatorsTest.java | 0
.../isis/commons/internal/context/ContextTest.java | 234 +++++++++++++++++++++
6 files changed, 234 insertions(+)
diff --git a/core/applib/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java
similarity index 100%
rename from core/applib/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java
rename to core/commons/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java
diff --git a/core/applib/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java
similarity index 100%
rename from core/applib/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java
rename to core/commons/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java
diff --git a/core/applib/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java
similarity index 100%
rename from core/applib/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java
rename to core/commons/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java
diff --git a/core/applib/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java
similarity index 100%
rename from core/applib/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java
rename to core/commons/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java
diff --git a/core/applib/src/test/java/org/apache/isis/commons/internal/compare/ComparatorsTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/compare/ComparatorsTest.java
similarity index 100%
rename from core/applib/src/test/java/org/apache/isis/commons/internal/compare/ComparatorsTest.java
rename to core/commons/src/test/java/org/apache/isis/commons/internal/compare/ComparatorsTest.java
diff --git a/core/commons/src/test/java/org/apache/isis/commons/internal/context/ContextTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/context/ContextTest.java
new file mode 100644
index 0000000..f752a9d
--- /dev/null
+++ b/core/commons/src/test/java/org/apache/isis/commons/internal/context/ContextTest.java
@@ -0,0 +1,234 @@
+/*
+ * 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.isis.commons.internal.context;
+
+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.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class ContextTest {
+
+ public static class AClass {
+ String name;
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ _Context.clear();
+ }
+
+ @AfterEach
+ void afterEach() {
+ _Context.clear();
+ }
+
+ @Test
+ void precondition() {
+ final Class<AClass> type = AClass.class;
+
+ final Object actual = _Context.getIfAny(type);
+ assertNull(actual, "pre-condition: context is expected to be empty");
+ }
+
+
+ @Test
+ void testPutSingleton() {
+
+ final Class<AClass> type = AClass.class;
+ final AClass singleton = new AClass();
+
+ _Context.putSingleton(type, singleton);
+
+ final Object actual = _Context.getIfAny(type);
+ assertTrue(singleton==actual, "singleton on context is expected to be the same as the 'local' instance");
+
+ assertThrows(IllegalStateException.class,
+ ()->{_Context.putSingleton(type, singleton);},
+ "expected to throw: you cannot override a singleton that is already on the context");
+
+
+ assertThrows(NullPointerException.class,
+ ()->{_Context.putSingleton(type, null);},
+ "expected to throw: you cannot put null references onto the context");
+ }
+
+ @Test
+ void testPutWithOverride() {
+
+ final Class<AClass> type = AClass.class;
+ final AClass instance1 = new AClass();
+ final AClass instance2 = new AClass();
+
+ {
+ final boolean success = _Context.put(type, instance1, true);
+ assertTrue(success, "expected: successful put");
+
+ final Object actual = _Context.getIfAny(type);
+ assertTrue(instance1==actual, "singleton on context is expected to be the same as the 'local' instance1");
+ }
+
+ {
+ final boolean success = _Context.put(type, instance2, true);
+ assertTrue(success, "expected: successful put");
+
+ final Object actual = _Context.getIfAny(type);
+ assertTrue(instance2==actual, "singleton on context is expected to be the same as the 'local' instance2");
+ }
+
+ assertThrows(NullPointerException.class,
+ ()->{_Context.put(type, null, true);},
+ "expected to throw: you cannot put null references onto the context");
+
+ }
+
+ @Test
+ void testPutWithoutOverride() {
+
+ final Class<AClass> type = AClass.class;
+ final AClass instance1 = new AClass();
+ final AClass instance2 = new AClass();
+
+ {
+ final boolean success = _Context.put(type, instance1, false);
+ assertTrue(success, "expected: successful put");
+
+ final Object actual = _Context.getIfAny(type);
+ assertTrue(instance1==actual, "singleton on context is expected to be the same as the 'local' instance1");
+ }
+
+ {
+ final boolean success = _Context.put(type, instance2, false);
+ assertFalse(success, "expected: failed put");
+
+ final Object actual = _Context.getIfAny(type);
+ assertTrue(instance1==actual, "singleton on context is expected to be the same as the 'local' instance1");
+ }
+
+ assertThrows(NullPointerException.class,
+ ()->{_Context.put(type, null, false);},
+ "expected to throw: you cannot put null references onto the context");
+
+ }
+
+ @Test
+ void testComputeIfAbsent() {
+
+ final Class<AClass> type = AClass.class;
+ final AClass instance1 = new AClass();
+ final AClass instance2 = new AClass();
+
+ _Context.computeIfAbsent(type, __->instance1);
+
+ {
+ final Object actual = _Context.getIfAny(type);
+ assertTrue(instance1==actual, "singleton on context is expected to be the same as the 'local' instance1");
+ }
+
+ _Context.computeIfAbsent(type, __->instance2); // expected: this call does nothing
+
+ {
+ final Object actual = _Context.getIfAny(type);
+ assertTrue(instance1==actual, "singleton on context is expected to be the same as the 'local' instance1");
+ }
+
+ }
+
+ @Test
+ void testGetOrElse() {
+
+ final Class<AClass> type = AClass.class;
+ final AClass singleton = new AClass();
+ final AClass fallback = new AClass();
+
+ {
+ final Object actual = _Context.getOrElse(type, ()->fallback);
+ assertTrue(fallback==actual, "singleton on context is expected to be the same as the 'local' fallback");
+ }
+
+ _Context.putSingleton(type, singleton);
+
+ {
+ final Object actual = _Context.getOrElse(type, ()->fallback);
+ assertTrue(singleton==actual, "singleton on context is expected to be the same as the 'local' singleton");
+ }
+ }
+
+ @Test
+ void testGetOrThrow() {
+
+ final Class<AClass> type = AClass.class;
+ final AClass singleton = new AClass();
+
+ assertThrows(IllegalStateException.class,
+ ()->{_Context.getOrThrow(type, IllegalStateException::new);},
+ "expected to throw: no singleton on context that matches the given type");
+
+ _Context.putSingleton(type, singleton);
+
+ final Object actual = _Context.getOrThrow(type, IllegalStateException::new);
+ assertTrue(singleton==actual, "singleton on context is expected to be the same as the 'local' singleton");
+ }
+
+ @Test
+ void testGetDefaultClassLoader() {
+ assertNotNull(_Context.getDefaultClassLoader(), "expected: even an empty context should provide a non-empty default");
+ }
+
+ @Test
+ void testSetDefaultClassLoaderWithOverride() {
+ assertThrows(NullPointerException.class,
+ ()->{_Context.setDefaultClassLoader(null, true);},
+ "expected to throw: you cannot put null references onto the context");
+
+ _Context.setDefaultClassLoader(AClass.class.getClassLoader(), true);
+
+ final Object actual = _Context.getDefaultClassLoader();
+ assertTrue(AClass.class.getClassLoader()==actual, "singleton on context is expected to be the same as the 'local' singleton");
+ }
+
+ @Test
+ void testSetDefaultClassLoaderWithoutOverride() {
+
+ assertThrows(NullPointerException.class,
+ ()->{_Context.setDefaultClassLoader(null, false);},
+ "expected to throw: you cannot put null references onto the context");
+
+ _Context.setDefaultClassLoader(AClass.class.getClassLoader(), false);
+
+ final Object actual = _Context.getDefaultClassLoader();
+ assertTrue(AClass.class.getClassLoader()==actual, "singleton on context is expected to be the same as the 'local' singleton");
+ }
+
+ @Test
+ void testLoadClass() throws ClassNotFoundException {
+ assertNotNull(_Context.loadClass(AClass.class.getName()));
+ }
+
+ @Test
+ void testLoadClassAndInitialize() throws ClassNotFoundException {
+ assertNotNull(_Context.loadClassAndInitialize(AClass.class.getName()));
+ }
+
+}