You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cr...@apache.org on 2020/11/20 23:42:13 UTC
[sling-org-apache-sling-junit-core] 03/05: Added tests for
TestReference(String filter)
This is an automated email from the ASF dual-hosted git repository.
cris pushed a commit to branch SLING-9915-Support-for-SlingAnnotationsTestRunner
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git
commit fa0560c56f41dc8c194e663bbdddf036fc6bbded
Author: Cris Rockwell <cm...@umich.edu>
AuthorDate: Fri Nov 20 18:32:59 2020 -0500
Added tests for TestReference(String filter)
---
bnd.bnd | 1 +
.../org/apache/sling/junit/tests/MyService.java | 25 +++++++++++
.../sling/junit/tests/TestReferenceJTest.java | 49 ++++++++++++++++++++++
.../junit/tests/impl/MyCoolServiceForTesting.java | 42 +++++++++++++++++++
.../junit/tests/impl/MyLameServiceForTesting.java | 41 ++++++++++++++++++
5 files changed, 158 insertions(+)
diff --git a/bnd.bnd b/bnd.bnd
index aea6f89..454a071 100644
--- a/bnd.bnd
+++ b/bnd.bnd
@@ -3,6 +3,7 @@ Export-Package: !org.junit.platform.*, \
junit.*;version=${junit.version}, \
org.junit.*;version=${junit.version}, \
org.hamcrest.*;version=${hamcrest.version};-split-package:=merge-first
+Sling-Test-Regexp: .*JTest
Import-Package: org.junit.platform.*;resolution:=optional, \
*
-includeresource: @org.jacoco.agent-*.jar!/org/jacoco/agent/rt/IAgent*
diff --git a/src/main/java/org/apache/sling/junit/tests/MyService.java b/src/main/java/org/apache/sling/junit/tests/MyService.java
new file mode 100644
index 0000000..fcbba0a
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/MyService.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.sling.junit.tests;
+
+public interface MyService {
+ /** @return Name of the Service which is used to discover the Service by the User **/
+ String getName();
+
+ /** @return Description of the Service **/
+ String getDescription();
+}
diff --git a/src/main/java/org/apache/sling/junit/tests/TestReferenceJTest.java b/src/main/java/org/apache/sling/junit/tests/TestReferenceJTest.java
new file mode 100644
index 0000000..6899158
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/TestReferenceJTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.sling.junit.tests;
+
+import org.apache.sling.junit.annotations.SlingAnnotationsTestRunner;
+import org.apache.sling.junit.annotations.TestReference;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertNotNull;
+import static junit.framework.TestCase.assertNull;
+
+
+@RunWith(SlingAnnotationsTestRunner.class)
+public class TestReferenceJTest {
+
+ @TestReference(filter="(component.name=org.apache.sling.junit.tests.impl.MyCoolServiceForTesting)")
+ MyService myCoolService;
+
+ @TestReference(filter="(component.name=org.apache.sling.junit.tests.impl.MyLameServiceForTesting)")
+ MyService myLameService;
+
+ @TestReference(filter="(component.name=org.apache.sling.junit.tests.impl.MyNonExistingServiceForTesting)")
+ MyService myNullService;
+
+ @Test
+ public void exampleTestReference(){
+ assertNotNull(myCoolService);
+ assertNotNull(myLameService);
+ assertNull(myNullService);
+ assertEquals("Cool Service", myCoolService.getName());
+ assertEquals("Lame Service", myLameService.getName());
+ }
+}
diff --git a/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTesting.java b/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTesting.java
new file mode 100644
index 0000000..471ee64
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTesting.java
@@ -0,0 +1,42 @@
+/*
+ * 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.sling.junit.tests.impl;
+
+import org.apache.sling.junit.tests.MyService;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+ service = MyService.class
+)
+public class MyCoolServiceForTesting implements MyService {
+
+ /**
+ * @return Name of the Service which is used to discover the Service by the User
+ **/
+ @Override
+ public String getName() {
+ return "Cool Service";
+ }
+
+ /**
+ * @return Description of the Service
+ **/
+ @Override
+ public String getDescription() {
+ return "My Cool Service is for testing @TestReference for in running JUnit tests within Sling";
+ }
+}
diff --git a/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTesting.java b/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTesting.java
new file mode 100644
index 0000000..7a473c8
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTesting.java
@@ -0,0 +1,41 @@
+/*
+ * 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.sling.junit.tests.impl;
+
+import org.apache.sling.junit.tests.MyService;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+ service = MyService.class
+)
+public class MyLameServiceForTesting implements MyService{
+ /**
+ * @return Name of the Service which is used to discover the Service by the User
+ **/
+ @Override
+ public String getName() {
+ return "Lame Service";
+ }
+
+ /**
+ * @return Description of the Service
+ **/
+ @Override
+ public String getDescription() {
+ return "My Lame Service is for testing @TestReference for in running JUnit tests within Sling";
+ }
+}