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/25 21:41:48 UTC
[sling-org-apache-sling-junit-core] branch
SLING-9915-junit-annotation-int-tests updated: WIP Continued PAX-style test
for TestReference annotation
This is an automated email from the ASF dual-hosted git repository.
cris pushed a commit to branch SLING-9915-junit-annotation-int-tests
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git
The following commit(s) were added to refs/heads/SLING-9915-junit-annotation-int-tests by this push:
new 263e196 WIP Continued PAX-style test for TestReference annotation
263e196 is described below
commit 263e19693aadca206e1614b451b045292e93fe93
Author: Cris Rockwell <cm...@umich.edu>
AuthorDate: Wed Nov 25 16:41:25 2020 -0500
WIP Continued PAX-style test for TestReference annotation
---
src/it/annotations-it/bnd.bnd | 8 ++-
src/it/annotations-it/pom.xml | 21 +++++---
.../sling/junit/{tests => it}/MyServiceIT.java | 2 +-
.../apache/sling/junit/it/TestReferenceJITest.java | 57 ++++++++++++++++++++++
.../impl/MyCoolServiceForTestingIT.java | 4 +-
.../impl/MyLameServiceForTestingIT.java | 4 +-
.../sling/junit/annotations/ReferenceIT.java | 23 ++++++---
.../annotations/SlingAnnotationsTestRunner.java | 6 ++-
8 files changed, 106 insertions(+), 19 deletions(-)
diff --git a/src/it/annotations-it/bnd.bnd b/src/it/annotations-it/bnd.bnd
index 4958986..6ba7cba 100644
--- a/src/it/annotations-it/bnd.bnd
+++ b/src/it/annotations-it/bnd.bnd
@@ -1 +1,7 @@
-# Nothing needed here so far for this bundle
\ No newline at end of file
+# Nothing needed here so far for this bundle
+Sling-Test-Regexp: .*JITest
+Import-Package:org.junit.platform.*;resolution:=optional, \
+ *
+-includeresource:org.apache.sling.junit.core-1.1.1-SNAPSHOT.jar;lib:=true
+Provide-Capability:\
+ osgi.contract;osgi.contract=JavaJSONP;uses:="javax.json,javax.json.spi,javax.json.stream";version:List<Version>="1.1,1.0"
\ No newline at end of file
diff --git a/src/it/annotations-it/pom.xml b/src/it/annotations-it/pom.xml
index c469e87..e82c0e2 100644
--- a/src/it/annotations-it/pom.xml
+++ b/src/it/annotations-it/pom.xml
@@ -31,6 +31,8 @@
<properties>
<sling.java.version>8</sling.java.version>
<org.ops4j.pax.exam.version>4.13.4</org.ops4j.pax.exam.version>
+ <junit.version>4.13</junit.version>
+ <hamcrest.version>1.3</hamcrest.version>
<!-- additional options that can be passed to Pax before executing the tests -->
<pax.vm.options />
<bundle.filename>${basedir}/target/${project.build.finalName}.jar</bundle.filename>
@@ -89,7 +91,18 @@
<scope>provided</scope>
</dependency>
-
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-core</artifactId>
+ <version>${hamcrest.version}</version>
+ <scope>compile</scope>
+ </dependency>
<!-- testing dependencies -->
<dependency>
@@ -98,11 +111,7 @@
<version>2.5.2</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
+
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.clients</artifactId>
diff --git a/src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/MyServiceIT.java b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/MyServiceIT.java
similarity index 96%
rename from src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/MyServiceIT.java
rename to src/it/annotations-it/src/main/java/org/apache/sling/junit/it/MyServiceIT.java
index f00c485..54238a9 100644
--- a/src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/MyServiceIT.java
+++ b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/MyServiceIT.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.sling.junit.tests;
+package org.apache.sling.junit.it;
public interface MyServiceIT {
/** @return Name of the Service which is used to discover the Service by the User **/
diff --git a/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/TestReferenceJITest.java b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/TestReferenceJITest.java
new file mode 100644
index 0000000..f9e916d
--- /dev/null
+++ b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/TestReferenceJITest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.it;
+
+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;
+import static junit.framework.TestCase.assertTrue;
+
+@RunWith(SlingAnnotationsTestRunner.class)
+public class TestReferenceJITest {
+ @TestReference(target ="(component.name=org.apache.sling.junit.it.impl.MyCoolServiceForTestingIT)")
+ MyServiceIT myCoolService;
+
+ @TestReference(target ="(component.name=org.apache.sling.junit.it.impl.MyLameServiceForTestingIT)")
+ MyServiceIT myLameService;
+
+ @TestReference(target ="(component.name=org.apache.sling.junit.it.impl.MyNonExistingServiceForTestingIT)")
+ MyServiceIT myNullService;
+
+ @TestReference
+ MyServiceIT myService;
+
+
+ @Test
+ public void exampleTestReference(){
+// assertTrue(true);
+ assertNotNull(myCoolService);
+ assertNotNull(myLameService);
+ assertNull(myNullService);
+ assertNotNull(myService);
+ assertEquals("Cool Service", myCoolService.getName());
+ assertEquals("Lame Service", myLameService.getName());
+ assertTrue(myService.getName().equals(myCoolService.getName()) ||
+ myService.getName().equals(myLameService.getName()));
+ }
+}
diff --git a/src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTestingIT.java b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/impl/MyCoolServiceForTestingIT.java
similarity index 93%
rename from src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTestingIT.java
rename to src/it/annotations-it/src/main/java/org/apache/sling/junit/it/impl/MyCoolServiceForTestingIT.java
index df60b4d..c0a1d71 100644
--- a/src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTestingIT.java
+++ b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/impl/MyCoolServiceForTestingIT.java
@@ -14,9 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.sling.junit.tests.impl;
+package org.apache.sling.junit.it.impl;
-import org.apache.sling.junit.tests.MyServiceIT;
+import org.apache.sling.junit.it.MyServiceIT;
import org.osgi.service.component.annotations.Component;
@Component(
diff --git a/src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTestingIT.java b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/impl/MyLameServiceForTestingIT.java
similarity index 93%
rename from src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTestingIT.java
rename to src/it/annotations-it/src/main/java/org/apache/sling/junit/it/impl/MyLameServiceForTestingIT.java
index 8c61695..8527660 100644
--- a/src/it/annotations-it/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTestingIT.java
+++ b/src/it/annotations-it/src/main/java/org/apache/sling/junit/it/impl/MyLameServiceForTestingIT.java
@@ -14,9 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.sling.junit.tests.impl;
+package org.apache.sling.junit.it.impl;
-import org.apache.sling.junit.tests.MyServiceIT;
+import org.apache.sling.junit.it.MyServiceIT;
import org.osgi.service.component.annotations.Component;
@Component(
diff --git a/src/it/annotations-it/src/test/java/org/apache/sling/junit/annotations/ReferenceIT.java b/src/it/annotations-it/src/test/java/org/apache/sling/junit/annotations/ReferenceIT.java
index 44a6cb2..c0be505 100644
--- a/src/it/annotations-it/src/test/java/org/apache/sling/junit/annotations/ReferenceIT.java
+++ b/src/it/annotations-it/src/test/java/org/apache/sling/junit/annotations/ReferenceIT.java
@@ -17,21 +17,25 @@
package org.apache.sling.junit.annotations;
import org.apache.commons.lang3.StringUtils;
-import org.apache.sling.junit.tests.impl.MyLameServiceForTestingIT;
+import org.apache.http.entity.StringEntity;
+import org.apache.sling.junit.it.impl.MyLameServiceForTestingIT;
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.SlingHttpResponse;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.apache.sling.testing.clients.osgi.OsgiConsoleClient;
+import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.URI;
+import java.util.Collections;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExamServer;
import org.ops4j.pax.exam.options.extra.VMOption;
-import static junit.framework.TestCase.assertTrue;
import static org.apache.sling.testing.paxexam.SlingOptions.logback;
import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar;
import static org.ops4j.pax.exam.CoreOptions.composite;
@@ -40,6 +44,7 @@ import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.CoreOptions.when;
import org.apache.sling.testing.paxexam.TestSupport;
+
public class ReferenceIT extends TestSupport {
protected static int httpPort;
protected static OsgiConsoleClient CLIENT;
@@ -93,7 +98,6 @@ public class ReferenceIT extends TestSupport {
mavenBundle().groupId("log4j").artifactId("log4j").version("1.2.17"),
mavenBundle().groupId("org.apache.aries.spifly").artifactId("org.apache.aries.spifly.dynamic.framework.extension").version("1.3.2"),
mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.webconsole.plugins.ds").version("2.1.0")
-
).getOptions();
}
@@ -124,7 +128,14 @@ public class ReferenceIT extends TestSupport {
}
@Test
- public void basicTest() {
- assertTrue(true);
+ public void testReferenceJITest() throws ClientException, UnsupportedEncodingException {
+ SlingHttpResponse response = CLIENT.doPost("/system/sling/junit/org.apache.sling.junit.it.TestReferenceJITest.html",
+ new StringEntity("some text"),
+ Collections.emptyList(),
+ 200);
+ response.checkContentContains("TEST RUN FINISHED");
+ response.checkContentContains("failures:0");
+ response.checkContentContains("ignored:0");
+ response.checkContentContains("tests:1");
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java b/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java
index 5f708cf..5c5f716 100644
--- a/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java
+++ b/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java
@@ -27,6 +27,8 @@ import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.Objects;
+
/** TestRunner which uses a TestObjectProcessor to
* handle annotations in test classes.
* A test that has RunWith=SlingAnnotationsTestRunner can
@@ -64,7 +66,9 @@ public class SlingAnnotationsTestRunner extends BlockJUnit4ClassRunner {
super.run(notifier);
} finally {
AnnotationsProcessor ap = (AnnotationsProcessor) top;
- ap.closeAllServices();
+ if (Objects.nonNull(ap)){
+ ap.closeAllServices();
+ }
}
}
}