├── .github └── workflows │ └── maven.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bnd.bnd ├── pom.xml └── src ├── main └── java │ └── net │ └── lr │ └── example │ └── testing │ ├── IntStreamProcessing.java │ ├── PrimeCalculator.java │ ├── PrimeCalculatorServlet.java │ └── PrimeCommand.java └── test ├── java └── net │ └── lr │ └── example │ └── testing │ ├── PrimeCalculatorServletTest.java │ ├── PrimeCalculatorTest.java │ ├── PrimeCommandMockTest.java │ ├── PrimeCommandSpyTest.java │ └── osgi │ ├── BaseTest.java │ ├── HooksTest.java │ ├── PrimeCommandTest.java │ ├── PrimeServletTest.java │ ├── before │ ├── AfterOsgi.java │ ├── BeforeOsgi.java │ └── ExtPaxExam.java │ └── tb │ ├── AdHocComponentsTest.java │ ├── BndDSOptions.java │ └── DoublingComponent.java └── resources ├── exam.properties └── logback.xml /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Set up JDK 1.8 13 | uses: actions/setup-java@v1 14 | with: 15 | java-version: 1.8 16 | - name: Build with Maven 17 | run: mvn package --file pom.xml -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | /target/ 24 | .classpath 25 | .project 26 | .settings 27 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OSGi testing tips and tricks 2 | 3 | [![Build Status](https://travis-ci.org/cschneider/osgi-testing-example.svg?branch=master)](https://travis-ci.org/cschneider/osgi-testing-example) 4 | 5 | In this repo I show some techniques to make testing your OSGi projects a lot simpler. 6 | 7 | ## Import into eclipse 8 | 9 | Use the m2e plugin that is already preinstalled in eclipse. 10 | 11 | ``` 12 | File -> Import -> Existing Maven Projects -> 13 | ``` 14 | 15 | ## Mock based tests outside OSGi using mockito 16 | 17 | Mockito provides some very nice features for testing DS components that use the popular field injection pattern 18 | 19 | ``` 20 | @Reference 21 | private Mydependency; 22 | ``` 23 | 24 | See the package [net.lr.example.testing](src/test/java/net/lr/example/testing) for some hints how to use mockito 25 | to inject dependencies into DS components. This covers mock, spy and capturing arguments. 26 | 27 | ## Creating type safe configs on the fly 28 | 29 | In declarative services type safe configs are defined as annotations. Unfortunately it is quite hard 30 | to instantiate or mock an annotation inside a test. 31 | 32 | [net.lr.example.testing.PrimeCalculatorServletTest](src/test/java/net/lr/example/testing/PrimeCalculatorServletTest.java) shows how to use the new OSGi converter spec to create the config 33 | annotation while retaining defaults defined in the annotation. 34 | 35 | 36 | ## Add hamcrest matchers and awaitility to pax exam tests 37 | 38 | Hamcrest provides a lot of nice and powerful matchers to junit. The example below shows how to compare 39 | elements of a complete array. Similarly you can assert over whole java beans. 40 | 41 | ``` 42 | Stream primes = new PrimeCalculator().until(3); 43 | assertThat(primes.toArray(), arrayContaining(2,3)); 44 | ``` 45 | 46 | Awaitility is another gem you should not miss. It allows to repeat an operation until it succeed or a timeout occurs. 47 | It also can nicely ignore exceptions and only return the last exception. This is ideal if you e.g. have an external rest service that will come up asynchronously to your test. Often this is covered with a sleep that has to be timed carefully to not be too short (causes test failures) or too long (wastes time). Awaitility solves this in a very nice way. 48 | 49 | See [BaseTest.java](src/test/java/net/lr/example/testing/osgi/BaseTest.java) for how to add hamcrest and Awaitility 50 | as bundles. 51 | 52 | See [net.lr.example.testing.osgi.PrimeServletTest](src/test/java/net/lr/example/testing/osgi/PrimeServletTest.java) for 53 | an example of how to use Awaitility. 54 | 55 | ## Create your OSGi test setup seamlessly using pax exam 56 | 57 | Most pax exam based tests require the project to first be built using maven or even need to run in their own project. 58 | Additionally in many cases the tests run in an additional project. So you need to setup remote debugging to step through your code. 59 | 60 | I show a way to run pax exam tests inside the same maven project in a way that does not require the build tool to run. 61 | The tests also run inside the junit process. 62 | 63 | So your workflow simply is edit, save, debug - like in plain java code. 64 | 65 | This works by doing two things: 66 | 67 | 1. Use the bnd-maven-plugin which is integrated with the m2e build and will update the OSGi meta data on the fly 68 | 2. Use bundle("reference:file:target/classes/") to refer to the exploded view of your bundle contents 69 | 70 | See [BaseTest.java](src/test/java/net/lr/example/testing/osgi/BaseTest.java) for how to setup the pax exam config. 71 | 72 | Try this by setting a breakpoint and running debug in eclipse from any of the tests in [src/test/java/net/lr/example/testing/osgi]. 73 | 74 | ## Create bundles including DS components on the fly using TinyBundles 75 | 76 | It is an old story that TinyBundles can create bundles on the fly but now it learned some new tricks. 77 | 78 | Use [BndDSOptions.dsBundle] to create a bundle using bnd by just adding some annotated classes. Bnd will auto create imports, exports and DS component meta data. 79 | 80 | The only "limitation" is that you can not simply @Inject such a component into your test as pax exam tends to embed this class inside the test probe. So it will see a different class. So this approach works best if your ds component either does not need to be directly injected into the test or if it implements an interface that is already defined in the main code. 81 | 82 | ## Switch the pax exam logging to logback 83 | 84 | Some people do not like pax logging. Luckily it is not very difficult to switch the pax exam logging backend to logback. 85 | 86 | See [BaseTest.java](src/test/java/net/lr/example/testing/osgi/BaseTest.java) for how to exchange pax logging with logback. 87 | -------------------------------------------------------------------------------- /bnd.bnd: -------------------------------------------------------------------------------- 1 | Export-Package: net.lr.example.testing -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | org.apache 7 | apache 8 | 19 9 | 10 | 11 | 12 | net.lr.example 13 | net.lr.example.testing 14 | 1.0.0-SNAPSHOT 15 | jar 16 | 17 | 18 | UTF-8 19 | 4.11.0 20 | 21 | 22 | 23 | 24 | org.slf4j 25 | slf4j-api 26 | 1.7.21 27 | 28 | 29 | org.osgi 30 | osgi.core 31 | 6.0.0 32 | provided 33 | 34 | 35 | org.osgi 36 | osgi.cmpn 37 | 6.0.0 38 | provided 39 | 40 | 41 | javax.servlet 42 | servlet-api 43 | 2.5 44 | provided 45 | 46 | 47 | 48 | ch.qos.logback 49 | logback-core 50 | 1.0.13 51 | test 52 | 53 | 54 | ch.qos.logback 55 | logback-classic 56 | 1.0.13 57 | test 58 | 59 | 60 | 61 | junit 62 | junit 63 | 4.12 64 | test 65 | 66 | 67 | org.mockito 68 | mockito-all 69 | 1.9.5 70 | test 71 | 72 | 73 | org.apache.servicemix.bundles 74 | org.apache.servicemix.bundles.hamcrest 75 | 1.3_1 76 | test 77 | 78 | 79 | 80 | org.ops4j.pax.exam 81 | pax-exam-container-native 82 | ${exam.version} 83 | test 84 | 85 | 86 | 87 | org.ops4j.pax.exam 88 | pax-exam-junit4 89 | ${exam.version} 90 | test 91 | 92 | 93 | 94 | org.ops4j.pax.exam 95 | pax-exam-cm 96 | ${exam.version} 97 | test 98 | 99 | 100 | 101 | org.ops4j.pax.exam 102 | pax-exam-link-mvn 103 | ${exam.version} 104 | test 105 | 106 | 107 | 108 | org.apache.felix 109 | org.apache.felix.framework 110 | 5.6.10 111 | test 112 | 113 | 114 | 115 | 116 | javax.inject 117 | javax.inject 118 | 1 119 | test 120 | 121 | 122 | org.ops4j.pax.tinybundles 123 | tinybundles 124 | 3.0.0 125 | test 126 | 127 | 128 | org.awaitility 129 | awaitility 130 | 3.1.0 131 | test 132 | 133 | 134 | org.apache.felix 135 | org.apache.felix.converter 136 | 1.0.0 137 | test 138 | 139 | 140 | 141 | 142 | 143 | 144 | org.apache.maven.plugins 145 | maven-compiler-plugin 146 | 147 | 1.8 148 | 1.8 149 | 150 | 151 | 152 | biz.aQute.bnd 153 | bnd-maven-plugin 154 | 4.2.0 155 | 156 | 157 | 158 | bnd-process 159 | 160 | 161 | 162 | 163 | 164 | org.apache.maven.plugins 165 | maven-jar-plugin 166 | 167 | 168 | ${project.build.outputDirectory}/META-INF/MANIFEST.MF 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /src/main/java/net/lr/example/testing/IntStreamProcessing.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import java.util.stream.Stream; 4 | 5 | /** 6 | * Just an interface to be implemented by an ad hoc class 7 | */ 8 | public interface IntStreamProcessing { 9 | Stream process(Stream stream); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/net/lr/example/testing/PrimeCalculator.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import static java.util.stream.IntStream.rangeClosed; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.osgi.service.component.annotations.Component; 8 | 9 | /** 10 | * Calculate prime numbers 11 | */ 12 | @Component( 13 | service = PrimeCalculator.class 14 | ) 15 | public class PrimeCalculator { 16 | 17 | public Stream until(int max) { 18 | return rangeClosed(2, max) 19 | .filter(this::isPrime) 20 | .mapToObj(Integer::new); 21 | } 22 | 23 | public boolean isPrime(Integer number) { 24 | for (int i = 2; i <= number / 2; i++) { 25 | if (number % i == 0) { 26 | return false; 27 | } 28 | } 29 | return true; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/lr/example/testing/PrimeCalculatorServlet.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.Servlet; 7 | import javax.servlet.ServletException; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | import org.osgi.service.component.annotations.Activate; 13 | import org.osgi.service.component.annotations.Component; 14 | import org.osgi.service.component.annotations.Reference; 15 | import org.osgi.service.metatype.annotations.AttributeDefinition; 16 | import org.osgi.service.metatype.annotations.Designate; 17 | import org.osgi.service.metatype.annotations.ObjectClassDefinition; 18 | 19 | import net.lr.example.testing.PrimeCalculatorServlet.Config; 20 | 21 | /** 22 | * Servlet that displays the prime numbers until the given parameter max. 23 | */ 24 | @Component( 25 | service = Servlet.class 26 | ) 27 | @Designate(ocd=Config.class) 28 | public class PrimeCalculatorServlet extends HttpServlet { 29 | private static final long serialVersionUID = 691594999516730176L; 30 | 31 | @Reference 32 | private PrimeCalculator calculator; 33 | 34 | private String title; 35 | 36 | @Activate 37 | public void activate(Config config) { 38 | this.title = config.title(); 39 | } 40 | 41 | @Override 42 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 43 | Integer max = new Integer(req.getParameter("max")); 44 | PrintWriter writer = resp.getWriter(); 45 | writer.println(title); 46 | calculator.until(max).forEach(writer::println); 47 | } 48 | 49 | @ObjectClassDefinition(name = "Prime servlet config") 50 | public @interface Config { 51 | @AttributeDefinition 52 | String osgi_http_whiteboard_servlet_pattern() default "/prime"; 53 | 54 | @AttributeDefinition 55 | String title() default "Prime numbers"; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/net/lr/example/testing/PrimeCommand.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import org.osgi.service.component.annotations.Component; 4 | import org.osgi.service.component.annotations.Reference; 5 | 6 | /** 7 | * Command to print prime numbers on the felix gogo console 8 | */ 9 | @Component( 10 | service = PrimeCommand.class, 11 | property = { 12 | "osgi.command.scope=prime", // 13 | "osgi.command.function=calc" 14 | } 15 | ) 16 | public class PrimeCommand { 17 | @Reference 18 | private PrimeCalculator calculator; 19 | 20 | public void calc(int maxNum) { 21 | calculator.until(maxNum).forEach(System.out::println); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/PrimeCalculatorServletTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import static org.hamcrest.Matchers.equalTo; 4 | import static org.junit.Assert.assertThat; 5 | import static org.mockito.Mockito.when; 6 | 7 | import java.io.IOException; 8 | import java.io.PrintWriter; 9 | import java.io.StringWriter; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import javax.servlet.ServletException; 14 | import javax.servlet.http.HttpServletRequest; 15 | import javax.servlet.http.HttpServletResponse; 16 | 17 | import org.junit.Before; 18 | import org.junit.Test; 19 | import org.mockito.InjectMocks; 20 | import org.mockito.Mock; 21 | import org.mockito.Mockito; 22 | import org.mockito.MockitoAnnotations; 23 | import org.mockito.Spy; 24 | import org.osgi.util.converter.Converters; 25 | 26 | import net.lr.example.testing.PrimeCalculatorServlet.Config; 27 | 28 | public class PrimeCalculatorServletTest { 29 | 30 | @Mock 31 | private HttpServletRequest req; 32 | 33 | @Mock 34 | private HttpServletResponse resp; 35 | 36 | @Spy 37 | private PrimeCalculator calculator = new PrimeCalculator(); 38 | 39 | @InjectMocks 40 | private PrimeCalculatorServlet servlet; 41 | 42 | @Before 43 | public void before() { 44 | MockitoAnnotations.initMocks(this); 45 | } 46 | 47 | @Test 48 | public void test() throws ServletException, IOException { 49 | when(req.getParameter(Mockito.eq("max"))).thenReturn("10"); 50 | StringWriter stringWriter = new StringWriter(); 51 | PrintWriter writer = new PrintWriter(stringWriter); 52 | when(resp.getWriter()).thenReturn(writer); 53 | 54 | Config config = createConfig(); 55 | servlet.activate(config); 56 | servlet.doGet(req, resp); 57 | 58 | assertThat(stringWriter.toString(), 59 | equalTo("My numbers\n2\n3\n5\n7\n")); 60 | } 61 | 62 | /** 63 | * Before OSGi R7 it was hard to mock a type safe config as each attribute 64 | * had to be populated with behaviour and the default were lost. 65 | * 66 | * Now there is the converter spec which allows to convert a map to a 67 | * type safe config while retaining default values. 68 | */ 69 | private Config createConfig() { 70 | Map props = new HashMap<>(); 71 | props.put("title", "My numbers"); 72 | return Converters.standardConverter().convert(props).to(Config.class); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/PrimeCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import static org.hamcrest.Matchers.arrayContaining; 4 | import static org.junit.Assert.assertThat; 5 | 6 | import java.util.stream.Stream; 7 | 8 | import org.junit.Test; 9 | 10 | /** 11 | * Simple test without any magic. Use this for classes that do not need special dependencies 12 | */ 13 | public class PrimeCalculatorTest { 14 | 15 | @Test 16 | public void test() { 17 | Stream primes = new PrimeCalculator().until(3); 18 | assertThat(primes.toArray(), arrayContaining(2,3)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/PrimeCommandMockTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import static org.hamcrest.Matchers.equalTo; 4 | import static org.junit.Assert.assertThat; 5 | import static org.mockito.Mockito.when; 6 | 7 | import java.util.stream.IntStream; 8 | 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.mockito.ArgumentCaptor; 12 | import org.mockito.Captor; 13 | import org.mockito.InjectMocks; 14 | import org.mockito.Mock; 15 | import org.mockito.MockitoAnnotations; 16 | 17 | /** 18 | * Shows how to inject private service dependencies using mockito. 19 | * In this case we mock the calculator using the @Mock annotation and a behaviour 20 | * definition using when. 21 | */ 22 | public class PrimeCommandMockTest { 23 | 24 | @InjectMocks 25 | PrimeCommand command; 26 | 27 | @Mock 28 | PrimeCalculator calculator; 29 | 30 | // Just to demonstrate how to capture values 31 | @Captor 32 | ArgumentCaptor maxNumCaptor; 33 | 34 | @Before 35 | public void before() { 36 | MockitoAnnotations.initMocks(this); 37 | } 38 | 39 | @Test 40 | public void test() { 41 | when(calculator.until(maxNumCaptor.capture())).thenReturn(IntStream.of(2,3).mapToObj(Integer::new)); 42 | command.calc(3); 43 | assertThat(maxNumCaptor.getValue(), equalTo(3)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/PrimeCommandSpyTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.mockito.InjectMocks; 6 | import org.mockito.MockitoAnnotations; 7 | import org.mockito.Spy; 8 | 9 | /** 10 | * Shows how to inject private service dependencies using mockito. 11 | * In this case we inject the actual calculator using the @Spy annotation 12 | */ 13 | public class PrimeCommandSpyTest { 14 | 15 | @InjectMocks 16 | PrimeCommand command; 17 | 18 | @Spy 19 | PrimeCalculator calculator = new PrimeCalculator(); 20 | 21 | @Before 22 | public void before() { 23 | MockitoAnnotations.initMocks(this); 24 | } 25 | 26 | @Test 27 | public void test() { 28 | command.calc(3); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/BaseTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing.osgi; 2 | /* 3 | * Licensed to the Apache Software Foundation (ASF) under one 4 | * or more contributor license agreements. See the NOTICE file 5 | * distributed with this work for additional information 6 | * regarding copyright ownership. The ASF licenses this file 7 | * to you under the Apache License, Version 2.0 (the 8 | * "License"); you may not use this file except in compliance 9 | * with the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, 14 | * software distributed under the License is distributed on an 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | * KIND, either express or implied. See the License for the 17 | * specific language governing permissions and limitations 18 | * under the License. 19 | */ 20 | 21 | import static org.ops4j.pax.exam.CoreOptions.bundle; 22 | import static org.ops4j.pax.exam.CoreOptions.composite; 23 | import static org.ops4j.pax.exam.CoreOptions.mavenBundle; 24 | import static org.ops4j.pax.exam.CoreOptions.systemProperty; 25 | 26 | import javax.inject.Inject; 27 | 28 | import org.ops4j.pax.exam.CoreOptions; 29 | import org.ops4j.pax.exam.Option; 30 | import org.osgi.framework.BundleContext; 31 | 32 | public class BaseTest { 33 | @Inject 34 | BundleContext context; 35 | 36 | public Option baseConfiguration() { 37 | return CoreOptions.composite( 38 | // This gives a fast fail when any bundle is unresolved 39 | systemProperty("pax.exam.osgi.unresolved.fail").value("true"), 40 | logback(), 41 | junit(), 42 | mavenBundle("org.apache.felix", "org.apache.felix.scr", "2.0.14"), 43 | mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.8.16"), 44 | httpService(), 45 | bundle("reference:file:target/classes/") 46 | ); 47 | } 48 | 49 | /** 50 | * Shows how to use logback in pax exam. 51 | * This only works when the default logging support is disabled in the exam.properties file 52 | */ 53 | public Option logback() { 54 | return composite(systemProperty("logback.configurationFile").value("src/test/resources/logback.xml"), 55 | mavenBundle().groupId("org.slf4j").artifactId("slf4j-api").version("1.7.6"), 56 | mavenBundle().groupId("ch.qos.logback").artifactId("logback-core").version("1.0.13"), 57 | mavenBundle().groupId("ch.qos.logback").artifactId("logback-classic").version("1.0.13")); 58 | } 59 | 60 | /** 61 | * We create our own junit option to also provide hamcrest and Awaitility support 62 | */ 63 | public Option junit() { 64 | return composite(systemProperty("pax.exam.invoker").value("junit"), 65 | bundle("link:classpath:META-INF/links/org.ops4j.pax.tipi.junit.link"), 66 | bundle("link:classpath:META-INF/links/org.ops4j.pax.exam.invoker.junit.link"), 67 | mavenBundle().groupId("org.apache.servicemix.bundles") 68 | .artifactId("org.apache.servicemix.bundles.hamcrest").version("1.3_1"), 69 | mavenBundle().groupId("org.awaitility").artifactId("awaitility").version("3.1.0")); 70 | } 71 | 72 | public Option httpService() { 73 | return CoreOptions.composite( 74 | mavenBundle("org.apache.felix", "org.apache.felix.http.servlet-api", "1.1.2"), 75 | mavenBundle("org.apache.felix", "org.apache.felix.http.jetty", "3.4.8") 76 | ); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/HooksTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing.osgi; 2 | 3 | import javax.inject.Inject; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.ops4j.pax.exam.Configuration; 12 | import org.ops4j.pax.exam.Option; 13 | import org.osgi.framework.BundleContext; 14 | import org.osgi.framework.FrameworkUtil; 15 | 16 | import net.lr.example.testing.osgi.before.AfterOsgi; 17 | import net.lr.example.testing.osgi.before.BeforeOsgi; 18 | import net.lr.example.testing.osgi.before.ExtPaxExam; 19 | 20 | @RunWith(ExtPaxExam.class) 21 | public class HooksTest extends BaseTest { 22 | 23 | @Inject 24 | Runnable ru; 25 | 26 | @Configuration 27 | public Option[] configuration() { 28 | return new Option[] { 29 | baseConfiguration() 30 | }; 31 | } 32 | 33 | /** 34 | * Works because of our ExtPaxExam runner. Will be executed outside OSGi in the process 35 | * that runs junit (not in the forked process). 36 | */ 37 | @BeforeOsgi 38 | public static void startServers() { 39 | System.out.println("Start servers"); 40 | // A typical case is to start other docker or OSGi containers needed for a test here 41 | } 42 | 43 | /** 44 | * Will be executed inisde OSGi but before @Inject so we can register services that are needed for @Inject 45 | * at this point 46 | */ 47 | @BeforeClass 48 | public static void beforeClass() { 49 | BundleContext bContext = FrameworkUtil.getBundle(HooksTest.class).getBundleContext(); 50 | bContext.registerService(Runnable.class, () -> {}, null); 51 | System.out.println("beforeClass"); 52 | } 53 | 54 | /** 55 | * Executed inside OSGi before each test method 56 | */ 57 | @Before 58 | public void before() { 59 | System.out.println("before"); 60 | } 61 | 62 | @Test 63 | public void test() { 64 | System.out.println("In test"); 65 | } 66 | 67 | /** 68 | * Executed inside OSGi after each test method (even if test fails). 69 | * Will not be executed if @Inject times out 70 | */ 71 | @After 72 | public void after() { 73 | System.out.println("after"); 74 | } 75 | 76 | /** 77 | * Will be executed inside OSGi after the whole class. Will also be executed if @Inject times out. 78 | */ 79 | @AfterClass 80 | public static void afterClass() { 81 | System.out.println("afterClass"); 82 | } 83 | 84 | /** 85 | * Works because of our ExtPaxExam runner. Will be executed outside OSGi in the process 86 | * that runs junit (not in the forked process). 87 | */ 88 | @AfterOsgi 89 | public static void stopServers() { 90 | System.out.println("Stop servers"); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/PrimeCommandTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing.osgi; 2 | 3 | import javax.inject.Inject; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.ops4j.pax.exam.Configuration; 8 | import org.ops4j.pax.exam.Option; 9 | import org.ops4j.pax.exam.junit.PaxExam; 10 | 11 | import net.lr.example.testing.PrimeCommand; 12 | 13 | @RunWith(PaxExam.class) 14 | public class PrimeCommandTest extends BaseTest { 15 | @Inject 16 | PrimeCommand command; 17 | 18 | @Configuration 19 | public Option[] configuration() { 20 | return new Option[] { 21 | baseConfiguration() 22 | }; 23 | } 24 | 25 | @Test 26 | public void test() { 27 | command.calc(100); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/PrimeServletTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing.osgi; 2 | 3 | import static org.awaitility.Awaitility.await; 4 | import static org.hamcrest.Matchers.notNullValue; 5 | 6 | import java.io.BufferedReader; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | import java.net.MalformedURLException; 10 | import java.net.URISyntaxException; 11 | import java.net.URL; 12 | import java.net.URLConnection; 13 | import java.nio.charset.StandardCharsets; 14 | import java.util.stream.Collectors; 15 | 16 | import javax.inject.Inject; 17 | 18 | import org.junit.Test; 19 | import org.junit.runner.RunWith; 20 | import org.ops4j.pax.exam.Configuration; 21 | import org.ops4j.pax.exam.Option; 22 | import org.ops4j.pax.exam.junit.PaxExam; 23 | import org.osgi.service.cm.ConfigurationAdmin; 24 | 25 | @RunWith(PaxExam.class) 26 | public class PrimeServletTest extends BaseTest { 27 | @Inject 28 | ConfigurationAdmin configAdmin; 29 | 30 | @Configuration 31 | public Option[] configuration() { 32 | return new Option[] { 33 | baseConfiguration(), 34 | httpService() 35 | }; 36 | } 37 | 38 | @Test 39 | public void test() throws URISyntaxException, MalformedURLException, IOException { 40 | 41 | /* 42 | * Retry to get the content for up to 10 seconds by default. 43 | * Use this if you have timing issues that can not be avoided using explicit service dependencies. 44 | */ 45 | String content = await().ignoreExceptions().until(this::getTestContent, notNullValue()); 46 | System.out.println(content); 47 | } 48 | 49 | private String getTestContent() throws MalformedURLException, IOException { 50 | URL testUrl = new URL("http://localhost:8080/prime?max=10"); 51 | return getContent(testUrl); 52 | } 53 | 54 | private String getContent(URL testUrl) throws IOException, MalformedURLException { 55 | URLConnection connection = testUrl.openConnection(); 56 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { 57 | return reader.lines().collect(Collectors.joining("\n")); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/before/AfterOsgi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | package net.lr.example.testing.osgi.before; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Target(ElementType.METHOD) 28 | public @interface AfterOsgi { 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/before/BeforeOsgi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | package net.lr.example.testing.osgi.before; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Target(ElementType.METHOD) 28 | public @interface BeforeOsgi { 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/before/ExtPaxExam.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | package net.lr.example.testing.osgi.before; 20 | 21 | import static java.util.Arrays.asList; 22 | 23 | import java.lang.annotation.Annotation; 24 | import java.lang.reflect.Method; 25 | import java.lang.reflect.Modifier; 26 | import java.util.Optional; 27 | 28 | import org.junit.runner.notification.RunNotifier; 29 | import org.junit.runners.model.InitializationError; 30 | import org.ops4j.pax.exam.junit.PaxExam; 31 | 32 | /** 33 | * Shows how to extend the PaxExam runner to implement hooks that are executed outside of OSGi in the parent 34 | * junit process 35 | */ 36 | public class ExtPaxExam extends PaxExam { 37 | 38 | private Optional before; 39 | private Optional after; 40 | 41 | public ExtPaxExam(Class klass) throws InitializationError { 42 | super(klass); 43 | this.before = getStaticMethodWith(klass, BeforeOsgi.class); 44 | this.after = getStaticMethodWith(klass, AfterOsgi.class); 45 | } 46 | 47 | @Override 48 | public void run(RunNotifier notifier) { 49 | this.before.ifPresent(this::invoke); 50 | super.run(notifier); 51 | this.after.ifPresent(this::invoke); 52 | } 53 | 54 | private Optional getStaticMethodWith(Class klass, Class annotation) { 55 | Optional foundMethod = asList(klass.getMethods()).stream() 56 | .filter(method -> method.getAnnotation(annotation) != null) 57 | .findFirst(); 58 | if (foundMethod.isPresent()) { 59 | Method m = foundMethod.get(); 60 | if (!Modifier.isStatic(m.getModifiers())) { 61 | throw new IllegalStateException("Method " + m.getName() + " must be static to be used as " + annotation.getName()); 62 | } 63 | } 64 | return foundMethod; 65 | } 66 | 67 | private void invoke(Method method) { 68 | try { 69 | method.invoke(null); 70 | } catch (Exception e) { 71 | throw new RuntimeException("Error calling method " + method, e); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/tb/AdHocComponentsTest.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing.osgi.tb; 2 | 3 | import static org.hamcrest.Matchers.arrayContaining; 4 | import static org.junit.Assert.assertThat; 5 | 6 | import java.util.stream.Stream; 7 | 8 | import javax.inject.Inject; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.ops4j.pax.exam.Configuration; 13 | import org.ops4j.pax.exam.Option; 14 | import org.ops4j.pax.exam.junit.PaxExam; 15 | import org.ops4j.pax.tinybundles.core.TinyBundles; 16 | 17 | import net.lr.example.testing.IntStreamProcessing; 18 | import net.lr.example.testing.PrimeCalculator; 19 | import net.lr.example.testing.osgi.BaseTest; 20 | 21 | /** 22 | * Sometimes you want to create some DS components just for the test. 23 | * This is easy as TinyBundles now allows to create bundles with DS components on the fly. 24 | */ 25 | @RunWith(PaxExam.class) 26 | public class AdHocComponentsTest extends BaseTest { 27 | @Inject 28 | PrimeCalculator calculator; 29 | 30 | @Inject 31 | IntStreamProcessing doubler; 32 | 33 | @Configuration 34 | public Option[] configuration() { 35 | return new Option[] { 36 | baseConfiguration(), 37 | 38 | // Create bundle with DS component on the fly 39 | BndDSOptions.dsBundle("doubling", 40 | TinyBundles.bundle().add(DoublingComponent.class) 41 | ) 42 | }; 43 | } 44 | 45 | @Test 46 | public void test() { 47 | Stream doubleStream = doubler.process(calculator.until(10)); 48 | assertThat(doubleStream.toArray(), arrayContaining(4, 6, 10, 14)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/tb/BndDSOptions.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing.osgi.tb; 2 | 3 | import static org.ops4j.pax.exam.CoreOptions.streamBundle; 4 | import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd; 5 | 6 | import org.ops4j.pax.exam.Option; 7 | import org.ops4j.pax.tinybundles.core.TinyBundle; 8 | 9 | /** 10 | * This must be in its own bundle and static to avoid that TinyBundles has to be deployed in OSGi 11 | */ 12 | public class BndDSOptions { 13 | 14 | private BndDSOptions() { 15 | } 16 | 17 | /** 18 | * Create a bundle with DS support and automatically generated exports and imports 19 | */ 20 | public static Option dsBundle(String symbolicName, TinyBundle bundleDef) { 21 | return streamBundle(bundleDef 22 | .symbolicName(symbolicName) 23 | .build(withBnd())); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/net/lr/example/testing/osgi/tb/DoublingComponent.java: -------------------------------------------------------------------------------- 1 | package net.lr.example.testing.osgi.tb; 2 | 3 | import java.util.stream.Stream; 4 | 5 | import org.osgi.service.component.annotations.Component; 6 | 7 | import net.lr.example.testing.IntStreamProcessing; 8 | 9 | /** 10 | * DS Component that doubles a stream of Integers to demonstrate how to create dummy / test 11 | * compomnents on the fly 12 | */ 13 | @Component 14 | public class DoublingComponent implements IntStreamProcessing { 15 | 16 | @Override 17 | public Stream process(Stream stream) { 18 | return stream.map(num -> num * 2); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/resources/exam.properties: -------------------------------------------------------------------------------- 1 | pax.exam.logging = none 2 | -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | %date %level [%thread] %logger{20} [%file : %line] %msg - %mdc %n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | --------------------------------------------------------------------------------