diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000000..b2d7a64ef167 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,16 @@ +# Normalize line endings to LF. +* text eol=lf + +# Ensure that line endings for multipart files in spring-web are not modified. +*.multipart -text + +# Ensure that line endings for DOS batch files are not modified. +*.bat -text + +# Ensure the following are treated as binary. +*.gif binary +*.jar binary +*.jpeg binary +*.jpg binary +*.png binary +*.vsd binary diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 000000000000..5cdaa4ba7117 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,17 @@ + +**Affects:** \ + +--- + \ No newline at end of file diff --git a/.github/workflows/gradle-wrapper-validation.yml b/.github/workflows/gradle-wrapper-validation.yml new file mode 100644 index 000000000000..405a2b306592 --- /dev/null +++ b/.github/workflows/gradle-wrapper-validation.yml @@ -0,0 +1,10 @@ +name: "Validate Gradle Wrapper" +on: [push, pull_request] + +jobs: + validation: + name: "Validation" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: gradle/wrapper-validation-action@v1 diff --git a/.gitignore b/.gitignore index a9136e7ebca5..3f904904f76f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,9 @@ classes/ /build buildSrc/build /spring-*/build +/spring-core/kotlin-coroutines/build +/framework-bom/build +/integration-tests/build /src/asciidoc/build target/ diff --git a/CODE_OF_CONDUCT.adoc b/CODE_OF_CONDUCT.adoc index f013d6f36ba4..17783c7c066b 100644 --- a/CODE_OF_CONDUCT.adoc +++ b/CODE_OF_CONDUCT.adoc @@ -40,5 +40,5 @@ appropriate to the circumstances. Maintainers are obligated to maintain confiden with regard to the reporter of an incident. This Code of Conduct is adapted from the -http://contributor-covenant.org[Contributor Covenant], version 1.3.0, available at -http://contributor-covenant.org/version/1/3/0/[contributor-covenant.org/version/1/3/0/] +https://contributor-covenant.org[Contributor Covenant], version 1.3.0, available at +https://contributor-covenant.org/version/1/3/0/[contributor-covenant.org/version/1/3/0/] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1fd71cd67d57..2295a0928826 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,8 +7,8 @@ First off, thank you for taking the time to contribute! :+1: :tada: * [Code of Conduct](#code-of-conduct) * [How to Contribute](#how-to-contribute) * [Discuss](#discuss) - * [Create a Ticket](#create-a-ticket) - * [Ticket Lifecycle](#ticket-lifecycle) + * [Create an Issue](#create-an-issue) + * [Issue Lifecycle](#issue-lifecycle) * [Submit a Pull Request](#submit-a-pull-request) * [Build from Source](#build-from-source) * [Source Code Style](#source-code-style) @@ -24,72 +24,71 @@ Please report unacceptable behavior to spring-code-of-conduct@pivotal.io. #### Discuss -If you have a question, check StackOverflow using -[this list of tags](https://spring.io/questions), organized by Spring project. -Find an existing discussion or start a new one if necessary. +If you have a question, check Stack Overflow using +[this list of tags](https://stackoverflow.com/questions/tagged/spring+or+spring-mvc+or+spring-aop+or+spring-jdbc+or+spring-transactions+or+spring-annotations+or+spring-jms+or+spring-el+or+spring-test+or+spring+or+spring-remoting+or+spring-orm+or+spring-jmx+or+spring-cache+or+spring-webflux?tab=Newest). +Find an existing discussion, or start a new one if necessary. -If you suspect an issue, perform a search in the -[JIRA issue tracker](https://jira.spring.io/browse/SPR), using a few different keywords. -When you find related issues and discussions, prior or current, it helps you to learn and -it helps us to make a decision. +If you believe there is an issue, search through +[existing issues](https://github.com/spring-projects/spring-framework/issues) trying a +few different ways to find discussions, past or current, that are related to the issue. +Reading those discussions helps you to learn about the issue, and helps us to make a +decision. -#### Create a Ticket -Reporting an issue or making a feature request is a great way to contribute. Your feedback -and the conversations that result from it provide a continuous flow of ideas. +#### Create an Issue -Before you create a ticket, please take the time to [research first](#discuss). +Reporting an issue or making a feature request is a great way to contribute. Your feedback +and the conversations that result from it provide a continuous flow of ideas. However, +before creating a ticket, please take the time to [discuss and research](#discuss) first. -If creating a ticket after a discussion on StackOverflow, please provide a self-sufficient description in the ticket, independent of the details on StackOverview. We understand this is extra work but the issue tracker is an important place of record for design discussions and decisions that can often be referenced long after the fix version, for example to revisit decisions, to understand the origin of a feature, and so on. +If creating an issue after a discussion on Stack Overflow, please provide a description +in the issue instead of simply referring to Stack Overflow. The issue tracker is an +important place of record for design discussions and should be self-sufficient. -When ready create a ticket in the [JIRA issue tracker](https://jira.spring.io/browse/SPR). +Once you're ready, create an issue on +[GitHub](https://github.com/spring-projects/spring-framework/issues). -#### Ticket Lifecycle +#### Issue Lifecycle -When an issue is first created, it may not be assigned and will not have a fix version. -Within a day or two, the issue is assigned to a specific committer and the target -version is set to "Waiting for Triage". The committer will then review the issue, ask for -further information if needed, and based on the findings, the issue is either assigned a fix -version or rejected. +When an issue is first created, it is flagged `waiting-for-triage` waiting for a team +member to triage it. Once the issue has been reviewed, the team may ask for further +information if needed, and based on the findings, the issue is either assigned a target +milestone or is closed with a specific status. -When a fix is ready, the issue is marked "Resolved" and may still be re-opened. Once a fix -is released, the issue is permanently "Closed". If necessary, you will need to create a new, -related ticket with a fresh description. +When a fix is ready, the issue is closed and may still be re-opened until the fix is +released. After that the issue will typically no longer be reopened. In rare cases if the +issue was not at all fixed, the issue may be re-opened. In most cases however any +follow-up reports will need to be created as new issues with a fresh description. #### Submit a Pull Request -You can contribute a source code change by submitting a pull request. - 1. If you have not previously done so, please sign the -[Contributor License Agreement](https://cla.pivotal.io/sign/spring). You will also be reminded -automatically when you submit a pull request. +[Contributor License Agreement](https://cla.pivotal.io/sign/spring). You will be reminded +automatically when you submit the PR. -1. For all but the most trivial of contributions, please [create a ticket](#create-a-ticket). -The purpose of the ticket is to understand and discuss the underlying issue or feature. -We use the JIRA issue tracker as the preferred place of record for conversations and -conclusions. In that sense discussions directly under a PR are more implementation detail -oriented and transient in nature. +1. Should you create an issue first? No, just create the pull request and use the +description to provide context and motivation, as you would for an issue. If you want +to start a discussion first or have already created an issue, once a pull request is +created, we will close the issue as superseded by the pull request, and the discussion +about the issue will continue under the pull request. 1. Always check out the `master` branch and submit pull requests against it (for target version see [settings.gradle](settings.gradle)). Backports to prior versions will be considered on a case-by-case basis and reflected as the fix version in the issue tracker. -1. Use short branch names, preferably based on the JIRA issue (e.g. `SPR-1234`), or -otherwise using succinct, lower-case, dash (-) delimited names, such as `fix-warnings`. - 1. Choose the granularity of your commits consciously and squash commits that represent multiple edits or corrections of the same logical change. See -[Rewriting History section of Pro Git](http://git-scm.com/book/en/Git-Tools-Rewriting-History) -for an overview of streamlining commit history. +[Rewriting History section of Pro Git](https://git-scm.com/book/en/Git-Tools-Rewriting-History) +for an overview of streamlining the commit history. -1. Format commit messages using 55 characters for the subject line, 72 lines for the -description, followed by related issues, e.g. `Issues: SPR-1234, SPR-1235`. -See the -[Commit Guidelines section of Pro Git](http://git-scm.com/book/en/Distributed-Git-Contributing-to-a-Project#Commit-Guidelines) -for best practices around commit messages and use `git log` to see some examples. +1. Format commit messages using 55 characters for the subject line, 72 characters per line +for the description, followed by the issue fixed, e.g. `Closes gh-22276`. See the +[Commit Guidelines section of Pro Git](https://git-scm.com/book/en/Distributed-Git-Contributing-to-a-Project#Commit-Guidelines) +for best practices around commit messages, and use `git log` to see some examples. -1. List the JIRA issue number in the PR description. +1. If there is a prior issue, reference the GitHub issue number in the description of the +pull request. If accepted, your contribution may be heavily modified as needed prior to merging. You will likely retain author attribution for your Git commits granted that the bulk of @@ -99,6 +98,13 @@ If asked to make corrections, simply push the changes against the same branch, a pull request will be updated. In other words, you do not need to create a new pull request when asked to make changes. +#### Participate in Reviews + +Helping to review pull requests is another great way to contribute. Your feedback +can help to shape the implementation of new features. When reviewing pull requests, +however, please refrain from approving or rejecting a PR unless you are a core +committer for the Spring Framework. + ### Build from Source See the [Build from Source](https://github.com/spring-projects/spring-framework/wiki/Build-from-Source) @@ -110,19 +116,19 @@ source code into your IDE. The wiki pages [Code Style](https://github.com/spring-projects/spring-framework/wiki/Code-Style) and [IntelliJ IDEA Editor Settings](https://github.com/spring-projects/spring-framework/wiki/IntelliJ-IDEA-Editor-Settings) -defines the source file coding standards we use along with some IDEA editor settings we customize. +define the source file coding standards we use along with some IDEA editor settings we customize. ### Reference Docs -The reference documentation is in the [src/docs/asciidoc](src/docs/asciidoc) directory and, in -[Asciidoctor](http://asciidoctor.org/) format. For trivial changes, you may be able to browse, -edit source files, and submit directly from Github. +The reference documentation is in the [src/docs/asciidoc](src/docs/asciidoc) directory, in +[Asciidoctor](https://asciidoctor.org/) format. For trivial changes, you may be able to browse, +edit source files, and submit directly from GitHub. -When making changes locally, use `./gradlew asciidoctor` and then browse the result under +When making changes locally, execute `./gradlew asciidoctor` and then browse the result under `build/asciidoc/html5/index.html`. Asciidoctor also supports live editing. For more details read -[Editing AsciiDoc with Live Preview](http://asciidoctor.org/docs/editing-asciidoc-with-live-preview/). +[Editing AsciiDoc with Live Preview](https://asciidoctor.org/docs/editing-asciidoc-with-live-preview/). Note that if you choose the -[System Monitor](http://asciidoctor.org/docs/editing-asciidoc-with-live-preview/#using-a-system-monitor) +[System Monitor](https://asciidoctor.org/docs/editing-asciidoc-with-live-preview/#using-a-system-monitor) option, you can find a Guardfile under `src/docs/asciidoc`. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 000000000000..ff7737963150 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed 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 + + https://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. diff --git a/README.md b/README.md index 6e0d5408b3bc..53a82b78e8b1 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,30 @@ -# Spring Framework +# Spring Framework [![Build Status](https://ci.spring.io/api/v1/teams/spring-framework/pipelines/spring-framework-5.2.x/jobs/build/badge)](https://ci.spring.io/teams/spring-framework/pipelines/spring-framework-5.2.x?groups=Build") -This is the home of the Spring Framework, the foundation for all -[Spring projects](https://spring.io/projects). Together the Spring Framework and the family of Spring projects make up what we call "Spring". +This is the home of the Spring Framework: the foundation for all [Spring projects](https://spring.io/projects). Collectively the Spring Framework and the family of Spring projects are often referred to simply as "Spring". -Spring provides everything you need beyond the Java language to create enterprise -applications in a wide range of scenarios and architectures. Please read the -[Overview](https://docs.spring.io/spring/docs/current/spring-framework-reference/overview.html#spring-introduction) -section in the reference for a more complete introduction. +Spring provides everything required beyond the Java programming language for creating enterprise applications for a wide range of scenarios and architectures. Please read the [Overview](https://docs.spring.io/spring/docs/current/spring-framework-reference/overview.html#spring-introduction) section as reference for a more complete introduction. ## Code of Conduct -This project is governed by the [Spring Code of Conduct](CODE_OF_CONDUCT.adoc). -By participating you are expected to uphold this code. -Please report unacceptable behavior to spring-code-of-conduct@pivotal.io. +This project is governed by the [Spring Code of Conduct](CODE_OF_CONDUCT.adoc). By participating, you are expected to uphold this code of conduct. Please report unacceptable behavior to spring-code-of-conduct@pivotal.io. ## Access to Binaries -For access to artifacts or a distribution zip, see the -[Spring Framework Artifacts](https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Artifacts) -wiki page. +For access to artifacts or a distribution zip, see the [Spring Framework Artifacts](https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Artifacts) wiki page. ## Documentation -The Spring Frameworks maintains reference documentation -([published](http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/) and -[source](src/docs/asciidoc)), -Github [wiki pages](https://github.com/spring-projects/spring-framework/wiki), and an -[API reference](http://docs.spring.io/spring-framework/docs/current/javadoc-api/). -There are also [guides and tutorials](https://spring.io/guides) across Spring projects. +The Spring Framework maintains reference documentation ([published](https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/) and [source](src/docs/asciidoc)), Github [wiki pages](https://github.com/spring-projects/spring-framework/wiki), and an +[API reference](https://docs.spring.io/spring-framework/docs/current/javadoc-api/). There are also [guides and tutorials](https://spring.io/guides) across Spring projects. ## Build from Source -See the [Build from Source](https://github.com/spring-projects/spring-framework/wiki/Build-from-Source) -wiki page and also [CONTRIBUTING.md](CONTRIBUTING.md). +See the [Build from Source](https://github.com/spring-projects/spring-framework/wiki/Build-from-Source) Wiki page and the [CONTRIBUTING.md](CONTRIBUTING.md) file. ## Stay in Touch -Follow [@SpringCentral](https://twitter.com/springcentral), -[@SpringFramework](https://twitter.com/springframework), and its -[team members](https://twitter.com/springframework/lists/team/members) on Twitter. -In-depth articles can be found at [The Spring Blog](http://spring.io/blog/), -and releases are announced via our [news feed](http://spring.io/blog/category/news). +Follow [@SpringCentral](https://twitter.com/springcentral), [@SpringFramework](https://twitter.com/springframework), and its [team members](https://twitter.com/springframework/lists/team/members) on Twitter. In-depth articles can be found at [The Spring Blog](https://spring.io/blog/), and releases are announced via our [news feed](https://spring.io/blog/category/news). ## License -The Spring Framework is released under version 2.0 of the -[Apache License](http://www.apache.org/licenses/LICENSE-2.0). +The Spring Framework is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0). diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000000..038a36b56539 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,11 @@ +# Security Policy + +## Supported Versions + +Please see the +[Spring Framework Versions](https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Versions) +wiki page. + +## Reporting a Vulnerability + +Please see https://spring.io/security-policy. diff --git a/build.gradle b/build.gradle index 83a26047383f..524e93f0a66d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,317 +1,411 @@ -buildscript { - repositories { - maven { url "https://repo.spring.io/plugins-release" } - } - dependencies { - classpath("io.spring.gradle:propdeps-plugin:0.0.9.RELEASE") - classpath("org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16") - } -} - -// 3rd party plugin repositories can be configured in settings.gradle plugins { - id "io.spring.dependency-management" version "1.0.5.RELEASE" apply false - id "org.jetbrains.kotlin.jvm" version "1.2.70" apply false - id "org.jetbrains.dokka" version "0.9.17" - id "org.asciidoctor.convert" version "1.5.7" + id 'io.spring.dependency-management' version '1.0.9.RELEASE' apply false + id 'io.spring.ge.conventions' version '0.0.7' + id 'io.spring.nohttp' version '0.0.5.RELEASE' + id "io.freefair.aspectj" version '4.1.6' apply false + id 'org.jetbrains.dokka' version '0.10.1' apply false + id 'org.jetbrains.kotlin.jvm' version '1.3.72' apply false + id 'org.asciidoctor.jvm.convert' version '2.4.0' + id 'org.asciidoctor.jvm.pdf' version '2.4.0' + id "com.github.ben-manes.versions" version '0.28.0' + id 'com.gradle.build-scan' version '3.2' + id 'de.undercouch.download' version '4.1.1' } +apply from: "$rootDir/gradle/build-scan-user-data.gradle" ext { - linkHomepage = "https://projects.spring.io/spring-framework" - linkCi = "https://build.spring.io/browse/SPR" - linkIssue = "https://jira.spring.io/browse/SPR" - linkScmUrl = "https://github.com/spring-projects/spring-framework" - linkScmConnection = "scm:git:git://github.com/spring-projects/spring-framework.git" - linkScmDevConnection = "scm:git:ssh://git@github.com:spring-projects/spring-framework.git" - - moduleProjects = subprojects.findAll { - !it.name.equals("spring-build-src") && !it.name.equals("spring-framework-bom") - } - - aspectjVersion = "1.9.1" - freemarkerVersion = "2.3.28" - groovyVersion = "2.5.2" - hsqldbVersion = "2.4.1" - jackson2Version = "2.9.7" - jettyVersion = "9.4.12.v20180830" - junit5Version = "5.3.1" - kotlinVersion = "1.2.70" - log4jVersion = "2.11.1" - nettyVersion = "4.1.29.Final" - reactorVersion = "Californium-RELEASE" - rxjavaVersion = "1.3.8" - rxjavaAdapterVersion = "1.2.1" - rxjava2Version = "2.2.2" - slf4jVersion = "1.7.25" // spring-jcl + consistent 3rd party deps - tiles3Version = "3.0.8" - tomcatVersion = "9.0.12" - undertowVersion = "2.0.13.Final" - - gradleScriptDir = "${rootProject.projectDir}/gradle" - withoutJclOverSlf4J = { - exclude group: "org.slf4j", module: "jcl-over-slf4j" + moduleProjects = subprojects.findAll { it.name.startsWith("spring-") } + javaProjects = subprojects - project(":framework-bom") + withoutJclOverSlf4j = { + exclude group: "org.slf4j", name: "jcl-over-slf4j" } } configure(allprojects) { project -> - group = "org.springframework" - version = qualifyVersionIfNecessary(version) - - apply plugin: "java" - apply plugin: "kotlin" - apply plugin: "checkstyle" - apply plugin: "propdeps" - apply plugin: "test-source-set-dependencies" apply plugin: "io.spring.dependency-management" - apply from: "${gradleScriptDir}/ide.gradle" dependencyManagement { - resolutionStrategy { - cacheChangingModulesFor 0, "seconds" - } - applyMavenExclusions = false - generatedPomCustomization { - enabled = false + imports { + mavenBom "com.fasterxml.jackson:jackson-bom:2.10.5" + mavenBom "io.netty:netty-bom:4.1.51.Final" + mavenBom "io.projectreactor:reactor-bom:Dysprosium-SR25" + mavenBom "io.rsocket:rsocket-bom:1.0.4" + mavenBom "org.eclipse.jetty:jetty-bom:9.4.31.v20200723" + mavenBom "org.jetbrains.kotlin:kotlin-bom:1.3.72" + mavenBom "org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.3.5" + mavenBom "org.junit:junit-bom:5.6.3" } - } + dependencies { + dependencySet(group: 'org.apache.logging.log4j', version: '2.17.1') { + entry 'log4j-api' + entry 'log4j-core' + entry 'log4j-jul' + entry 'log4j-slf4j-impl' + } + dependency "org.slf4j:slf4j-api:1.7.32" + dependency "com.google.code.findbugs:jsr305:3.0.2" - configurations.all { - // Check for updates every build - resolutionStrategy.cacheChangingModulesFor 0, "seconds" + dependencySet(group: 'org.aspectj', version: '1.9.7') { + entry 'aspectjrt' + entry 'aspectjtools' + entry 'aspectjweaver' + } + dependencySet(group: 'org.codehaus.groovy', version: '2.5.15') { + entry 'groovy' + entry 'groovy-jsr223' + entry 'groovy-templates' + entry 'groovy-test' + entry 'groovy-xml' + } + + dependency "io.reactivex:rxjava:1.3.8" + dependency "io.reactivex:rxjava-reactive-streams:1.2.1" + dependency "io.reactivex.rxjava2:rxjava:2.2.21" + dependency "io.projectreactor.tools:blockhound:1.0.6.RELEASE" - // Consistent slf4j version (e.g. clashes between slf4j versions) - resolutionStrategy.eachDependency { DependencyResolveDetails details -> - if (details.requested.group == "org.slf4j") { - details.useVersion slf4jVersion + dependency "com.caucho:hessian:4.0.63" + dependency "com.fasterxml:aalto-xml:1.2.2" + dependency("com.fasterxml.woodstox:woodstox-core:6.2.3") { + exclude group: "stax", name: "stax-api" + } + dependency "com.google.code.gson:gson:2.8.6" + dependency "com.google.protobuf:protobuf-java-util:3.11.4" + dependency "com.googlecode.protobuf-java-format:protobuf-java-format:1.4" + dependency("com.thoughtworks.xstream:xstream:1.4.11.1") { + exclude group: "xpp3", name: "xpp3_min" + exclude group: "xmlpull", name: "xmlpull" + } + dependency "org.apache.johnzon:johnzon-jsonb:1.2.10" + dependency("org.codehaus.jettison:jettison:1.3.8") { + exclude group: "stax", name: "stax-api" + } + dependencySet(group: 'org.jibx', version: '1.3.3') { + entry 'jibx-bind' + entry 'jibx-run' + } + dependency "org.ogce:xpp3:1.1.6" + dependency "org.yaml:snakeyaml:1.27" + + dependency "com.h2database:h2:1.4.200" + dependency "com.github.ben-manes.caffeine:caffeine:2.8.8" + dependency "com.github.librepdf:openpdf:1.3.25" + dependency "com.rometools:rome:1.12.2" + dependency "commons-io:commons-io:2.5" + dependency "io.vavr:vavr:0.10.3" + dependency "net.sf.jopt-simple:jopt-simple:5.0.4" + dependencySet(group: 'org.apache.activemq', version: '5.8.0') { + entry 'activemq-broker' + entry('activemq-kahadb-store') { + exclude group: "org.springframework", name: "spring-context" + } + entry 'activemq-stomp' + } + dependency "org.apache.bcel:bcel:6.0" + dependency "org.apache.commons:commons-pool2:2.8.1" + dependencySet(group: 'org.apache.derby', version: '10.14.2.0') { + entry 'derby' + entry 'derbyclient' + } + dependency "org.apache.poi:poi-ooxml:4.1.2" + dependency "org.apache-extras.beanshell:bsh:2.0b6" + dependency "org.freemarker:freemarker:2.3.30" + dependency "org.hsqldb:hsqldb:2.5.1" + dependency "org.quartz-scheduler:quartz:2.3.2" + dependency "org.codehaus.fabric3.api:commonj:1.1.0" + dependency "net.sf.ehcache:ehcache:2.10.6" + dependency "org.ehcache:jcache:1.0.1" + dependency "org.ehcache:ehcache:3.4.0" + dependency "org.hibernate:hibernate-core:5.4.28.Final" + dependency "org.hibernate:hibernate-validator:6.1.7.Final" + dependency "org.webjars:webjars-locator-core:0.46" + dependency "org.webjars:underscorejs:1.8.3" + + dependencySet(group: 'org.apache.tomcat', version: '9.0.37') { + entry 'tomcat-util' + entry('tomcat-websocket') { + exclude group: "org.apache.tomcat", name: "tomcat-websocket-api" + exclude group: "org.apache.tomcat", name: "tomcat-servlet-api" + } + } + dependencySet(group: 'org.apache.tomcat.embed', version: '9.0.37') { + entry 'tomcat-embed-core' + entry 'tomcat-embed-websocket' + } + dependencySet(group: 'io.undertow', version: '2.0.32.Final') { + entry 'undertow-core' + entry('undertow-websockets-jsr') { + exclude group: "org.jboss.spec.javax.websocket", name: "jboss-websocket-api_1.1_spec" + } + entry('undertow-servlet') { + exclude group: "org.jboss.spec.javax.servlet", name: "jboss-servlet-api_3.1_spec" + exclude group: "org.jboss.spec.javax.annotation", name: "jboss-annotations-api_1.2_spec" + } } - } - } - def commonCompilerArgs = - ["-Xlint:serial", "-Xlint:cast", "-Xlint:classfile", "-Xlint:dep-ann", - "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally", "-Xlint:overrides", - "-Xlint:path", "-Xlint:processing", "-Xlint:static", "-Xlint:try", "-Xlint:-options"] + dependencySet(group: 'com.squareup.okhttp3', version: '3.14.9') { + entry 'okhttp' + entry 'mockwebserver' + } + dependency("org.apache.httpcomponents:httpclient:4.5.13") { + exclude group: "commons-logging", name: "commons-logging" + } + dependency("org.apache.httpcomponents:httpasyncclient:4.1.4") { + exclude group: "commons-logging", name: "commons-logging" + } + dependency "org.eclipse.jetty:jetty-reactive-httpclient:1.1.4" - compileJava.options*.compilerArgs = commonCompilerArgs + - ["-Xlint:varargs", "-Xlint:fallthrough", "-Xlint:rawtypes", - "-Xlint:deprecation", "-Xlint:unchecked", "-Werror"] + dependency "org.jruby:jruby:9.2.11.1" + dependency "org.python:jython-standalone:2.7.1" + dependency "org.mozilla:rhino:1.7.11" - compileTestJava.options*.compilerArgs = commonCompilerArgs + - ["-Xlint:-varargs", "-Xlint:-fallthrough", "-Xlint:-rawtypes", - "-Xlint:-deprecation", "-Xlint:-unchecked"] + dependency "commons-fileupload:commons-fileupload:1.4" + dependency "org.synchronoss.cloud:nio-multipart-parser:1.1.0" - compileJava { - sourceCompatibility = 1.8 // can be switched to 10 for testing - targetCompatibility = 1.8 - options.encoding = "UTF-8" - } + dependency("dom4j:dom4j:1.6.1") { + exclude group: "xml-apis", name: "xml-apis" + } + dependency("jaxen:jaxen:1.1.1") { + exclude group: "xml-apis", name: "xml-apis" + exclude group: "xom", name: "xom" + exclude group: "xerces", name: "xercesImpl" + } - compileTestJava { - sourceCompatibility = 1.8 // can be switched to 10 for testing - targetCompatibility = 1.8 - options.encoding = "UTF-8" - options.compilerArgs += "-parameters" - } + dependency("junit:junit:4.12") { + exclude group: "org.hamcrest", name: "hamcrest-core" + } + dependency("de.bechte.junit:junit-hierarchicalcontextrunner:4.12.1") { + exclude group: "junit", name: "junit" + } + dependency "org.testng:testng:6.14.3" + dependency "org.hamcrest:hamcrest:2.1" + dependency "org.awaitility:awaitility:3.1.6" + dependency "org.assertj:assertj-core:3.18.1" + dependencySet(group: 'org.xmlunit', version: '2.6.2') { + entry 'xmlunit-assertj' + entry('xmlunit-matchers') { + exclude group: "org.hamcrest", name: "hamcrest-core" + } + } + dependencySet(group: 'org.mockito', version: '3.3.3') { + entry('mockito-core') { + exclude group: "org.hamcrest", name: "hamcrest-core" + } + entry 'mockito-junit-jupiter' + } + dependency "io.mockk:mockk:1.10.2" + + dependency("net.sourceforge.htmlunit:htmlunit:2.43.0") { + exclude group: "commons-logging", name: "commons-logging" + } + dependency("org.seleniumhq.selenium:htmlunit-driver:2.43.1") { + exclude group: "commons-logging", name: "commons-logging" + } + dependency("org.seleniumhq.selenium:selenium-java:3.141.59") { + exclude group: "commons-logging", name: "commons-logging" + exclude group: "io.netty", name: "netty" + } + dependency "org.skyscreamer:jsonassert:1.5.0" + dependency "com.jayway.jsonpath:json-path:2.4.0" + + dependencySet(group: 'org.apache.tiles', version: '3.0.8') { + entry 'tiles-api' + entry('tiles-core', withoutJclOverSlf4j) + entry('tiles-servlet', withoutJclOverSlf4j) + entry('tiles-jsp', withoutJclOverSlf4j) + entry('tiles-el', withoutJclOverSlf4j) + entry('tiles-extras') { + exclude group: "org.springframework", name: "spring-web" + exclude group: "org.slf4j", name: "jcl-over-slf4j" + } + } + dependency("org.apache.taglibs:taglibs-standard-jstlel:1.2.5") { + exclude group: "org.apache.taglibs", name: "taglibs-standard-spec" + } + + dependency "com.ibm.websphere:uow:6.0.2.17" + dependency "com.jamonapi:jamon:2.82" + dependency "joda-time:joda-time:2.10.10" + dependency "org.eclipse.persistence:org.eclipse.persistence.jpa:2.7.7" + dependency "org.javamoney:moneta:1.3" + + dependency "com.sun.activation:javax.activation:1.2.0" + dependency "com.sun.mail:javax.mail:1.6.2" + dependencySet(group: 'com.sun.xml.bind', version: '2.3.0.1') { + entry 'jaxb-core' + entry 'jaxb-impl' + entry 'jaxb-xjc' + } - compileKotlin { - kotlinOptions { - jvmTarget = "1.8" - freeCompilerArgs = ["-Xjsr305=strict"] - apiVersion = "1.1" - languageVersion = "1.1" + dependency "javax.activation:javax.activation-api:1.2.0" + dependency "javax.annotation:javax.annotation-api:1.3.2" + dependency "javax.cache:cache-api:1.1.0" + dependency "javax.ejb:javax.ejb-api:3.2" + dependency "javax.el:javax.el-api:3.0.1-b04" + dependency "javax.enterprise.concurrent:javax.enterprise.concurrent-api:1.0" + dependency "javax.faces:javax.faces-api:2.2" + dependency "javax.inject:javax.inject:1" + dependency "javax.inject:javax.inject-tck:1" + dependency "javax.interceptor:javax.interceptor-api:1.2.2" + dependency "javax.jms:javax.jms-api:2.0.1" + dependency "javax.json:javax.json-api:1.1.4" + dependency "javax.json.bind:javax.json.bind-api:1.0" + dependency "javax.mail:javax.mail-api:1.6.2" + dependency "javax.money:money-api:1.0.3" + dependency "javax.resource:javax.resource-api:1.7.1" + dependency "javax.servlet:javax.servlet-api:4.0.1" + dependency "javax.servlet.jsp:javax.servlet.jsp-api:2.3.2-b02" + dependency "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1" + dependency "javax.transaction:javax.transaction-api:1.3" + dependency "javax.validation:validation-api:2.0.1.Final" + dependency "javax.websocket:javax.websocket-api:1.1" + dependency "javax.xml.bind:jaxb-api:2.3.1" + dependency "javax.xml.ws:jaxws-api:2.3.1" + + dependency "org.eclipse.persistence:javax.persistence:2.2.0" + + // Substitute for "javax.management:jmxremote_optional:1.0.1_04" which + // is not available on Maven Central + dependency "org.glassfish.external:opendmk_jmxremote_optional_jar:1.0-b01-ea" + dependency "org.glassfish:javax.el:3.0.1-b08" + dependency "org.glassfish.main:javax.jws:4.0-b33" + dependency "org.glassfish.tyrus:tyrus-container-servlet:1.13.1" + } + generatedPomCustomization { + enabled = false + } + resolutionStrategy { + cacheChangingModulesFor 0, "seconds" + } + repositories { + mavenCentral() + maven { url "https://repo.spring.io/libs-spring-framework-build" } } } + configurations.all { + resolutionStrategy { + cacheChangingModulesFor 0, "seconds" + cacheDynamicVersionsFor 0, "seconds" + } + } +} - compileTestKotlin { - kotlinOptions { - jvmTarget = "1.8" - freeCompilerArgs = ["-Xjsr305=strict"] +configure([rootProject] + javaProjects) { project -> + group = "org.springframework" + + apply plugin: "java" + apply plugin: "java-test-fixtures" + apply plugin: "checkstyle" + apply plugin: 'org.springframework.build.compile' + apply from: "${rootDir}/gradle/custom-java-home.gradle" + apply from: "${rootDir}/gradle/ide.gradle" + + pluginManager.withPlugin("kotlin") { + apply plugin: "org.jetbrains.dokka" + compileKotlin { + kotlinOptions { + jvmTarget = "1.8" + freeCompilerArgs = ["-Xjsr305=strict"] + allWarningsAsErrors = true + } + } + compileTestKotlin { + kotlinOptions { + jvmTarget = "1.8" + freeCompilerArgs = ["-Xjsr305=strict"] + } } } test { + useJUnitPlatform() + include(["**/*Tests.class", "**/*Test.class"]) systemProperty("java.awt.headless", "true") systemProperty("testGroups", project.properties.get("testGroups")) - scanForTestClasses = false - include(["**/*Tests.class", "**/*Test.class"]) - // Since we set scanForTestClasses to false, we need to filter out inner - // classes with the "$" pattern; otherwise, using -Dtest.single=MyTests to - // run MyTests by itself will fail if MyTests contains any inner classes. - exclude(["**/Abstract*.class", '**/*$*']) - reports.junitXml.setDestination(file("$buildDir/test-results")) + systemProperty("io.netty.leakDetection.level", "paranoid") } checkstyle { - toolVersion = "8.10.1" + toolVersion = "8.38" configDir = rootProject.file("src/checkstyle") } - repositories { - maven { url "https://repo.spring.io/libs-release" } - } - dependencies { - testCompile("junit:junit:4.12") { - exclude group: "org.hamcrest", module: "hamcrest-core" - } - testCompile("org.mockito:mockito-core:2.22.0") { - exclude group: "org.hamcrest", module: "hamcrest-core" - } - testCompile("com.nhaarman:mockito-kotlin:1.6.0") { - exclude module: "kotlin-stdlib" - exclude module: "kotlin-reflect" - exclude module: "mockito-core" - } - testCompile("org.hamcrest:hamcrest-all:1.3") - testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}") - testRuntime("org.apache.logging.log4j:log4j-slf4j-impl:${log4jVersion}") - testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}") + testCompile("org.junit.jupiter:junit-jupiter-api") + testCompile("org.junit.jupiter:junit-jupiter-params") + testCompile("org.mockito:mockito-core") + testCompile("org.mockito:mockito-junit-jupiter") + testCompile("io.mockk:mockk") + testCompile("org.assertj:assertj-core") + // Pull in the latest JUnit 5 Launcher API to ensure proper support in IDEs. + testRuntime("org.junit.platform:junit-platform-launcher") + testRuntime("org.junit.jupiter:junit-jupiter-engine") + testRuntime("org.apache.logging.log4j:log4j-core") + testRuntime("org.apache.logging.log4j:log4j-slf4j-impl") + testRuntime("org.apache.logging.log4j:log4j-jul") // JSR-305 only used for non-required meta-annotations - compileOnly("com.google.code.findbugs:jsr305:3.0.2") - testCompileOnly("com.google.code.findbugs:jsr305:3.0.2") - checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.5") + compileOnly("com.google.code.findbugs:jsr305") + testCompileOnly("com.google.code.findbugs:jsr305") + checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.15") } ext.javadocLinks = [ - "http://docs.oracle.com/javase/8/docs/api/", - "http://docs.oracle.com/javaee/7/api/", - "http://docs.oracle.com/cd/E13222_01/wls/docs90/javadocs/", // CommonJ - "http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/topic/com.ibm.websphere.javadoc.doc/web/apidocs/", - "http://glassfish.java.net/nonav/docs/v3/api/", - "http://docs.jboss.org/jbossas/javadoc/4.0.5/connector/", - "http://docs.jboss.org/jbossas/javadoc/7.1.2.Final/", - "http://tiles.apache.org/tiles-request/apidocs/", - "http://tiles.apache.org/framework/apidocs/", - "http://www.eclipse.org/aspectj/doc/released/aspectj5rt-api/", - "http://ehcache.org/apidocs/2.10.4", - "http://quartz-scheduler.org/api/2.2.1/", - "http://fasterxml.github.io/jackson-core/javadoc/2.8/", - "http://fasterxml.github.io/jackson-databind/javadoc/2.8/", - "http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.8/", - "http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/" + "https://docs.oracle.com/javase/8/docs/api/", + "https://docs.oracle.com/javaee/7/api/", + "https://docs.oracle.com/cd/E13222_01/wls/docs90/javadocs/", // CommonJ + "https://www.ibm.com/support/knowledgecenter/SS7JFU_8.5.5/com.ibm.websphere.javadoc.doc/web/apidocs/", + "https://glassfish.java.net/nonav/docs/v3/api/", + "https://docs.jboss.org/jbossas/javadoc/4.0.5/connector/", + "https://docs.jboss.org/jbossas/javadoc/7.1.2.Final/", + "https://tiles.apache.org/tiles-request/apidocs/", + "https://tiles.apache.org/framework/apidocs/", + "https://www.eclipse.org/aspectj/doc/released/aspectj5rt-api/", + "https://www.ehcache.org/apidocs/2.10.4/", + "https://www.quartz-scheduler.org/api/2.3.0/", + "https://fasterxml.github.io/jackson-core/javadoc/2.10/", + "https://fasterxml.github.io/jackson-databind/javadoc/2.10/", + "https://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.10/", + "https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/", + "https://junit.org/junit4/javadoc/4.12/", + "https://junit.org/junit5/docs/5.6.3/api/" ] as String[] } -configure(subprojects - project(":spring-build-src")) { subproject -> - apply from: "${gradleScriptDir}/publish-maven.gradle" - - jar { - manifest.attributes["Implementation-Title"] = subproject.name - manifest.attributes["Implementation-Version"] = subproject.version - manifest.attributes["Automatic-Module-Name"] = subproject.name.replace('-', '.') // for Jigsaw - manifest.attributes["Created-By"] = - "${System.getProperty("java.version")} (${System.getProperty("java.specification.vendor")})" - - from("${rootProject.projectDir}/src/docs/dist") { - include "license.txt" - include "notice.txt" - into "META-INF" - expand(copyright: new Date().format("yyyy"), version: project.version) - } - } - - javadoc { - description = "Generates project-level javadoc for use in -javadoc jar" - - options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED - options.author = true - options.header = project.name - options.use = true - options.links(project.ext.javadocLinks) - options.addStringOption("Xdoclint:none", "-quiet") - - // Suppress warnings due to cross-module @see and @link references. - // Note that global 'api' task does display all warnings. - logging.captureStandardError LogLevel.INFO - logging.captureStandardOutput LogLevel.INFO // suppress "## warnings" message - } - - task sourcesJar(type: Jar, dependsOn: classes) { - duplicatesStrategy = DuplicatesStrategy.EXCLUDE - classifier = "sources" - from sourceSets.main.allSource - // Don't include or exclude anything explicitly by default. See SPR-12085. - } - - task javadocJar(type: Jar) { - classifier = "javadoc" - from javadoc - } - - artifacts { - archives sourcesJar - archives javadocJar - } +configure(moduleProjects) { project -> + apply from: "${rootDir}/gradle/spring-module.gradle" } configure(rootProject) { description = "Spring Framework" apply plugin: "groovy" - apply from: "${gradleScriptDir}/jdiff.gradle" - apply from: "${gradleScriptDir}/docs.gradle" - - dependencyManagement { - imports { - mavenBom "io.projectreactor:reactor-bom:${reactorVersion}" - } - } - - // Don't publish the default jar for the root project - configurations.archives.artifacts.clear() - - dependencies { // for integration tests - testCompile(project(":spring-aop")) - testCompile(project(":spring-beans")) - testCompile(project(":spring-context")) - testCompile(project(":spring-core")) - testCompile(project(":spring-expression")) - testCompile(project(":spring-jdbc")) - testCompile(project(":spring-orm")) - testCompile(project(":spring-test")) - testCompile(project(":spring-tx")) - testCompile(project(":spring-web")) - testCompile("javax.inject:javax.inject:1") - testCompile("javax.resource:javax.resource-api:1.7.1") - testCompile("javax.servlet:javax.servlet-api:3.1.0") - testCompile("org.aspectj:aspectjweaver:${aspectjVersion}") - testCompile("org.hsqldb:hsqldb:${hsqldbVersion}") - testCompile("org.hibernate:hibernate-core:5.1.16.Final") - } - - artifacts { - archives docsZip - archives schemaZip - archives distZip - } - - wrapper { - doLast() { - def gradleOpts = "-XX:MaxMetaspaceSize=1024m -Xmx1024m" - def gradleBatOpts = "$gradleOpts -XX:MaxHeapSize=256m" - File wrapperFile = file("gradlew") - wrapperFile.text = wrapperFile.text.replace("DEFAULT_JVM_OPTS=", - "GRADLE_OPTS=\"$gradleOpts \$GRADLE_OPTS\"\nDEFAULT_JVM_OPTS=") - File wrapperBatFile = file("gradlew.bat") - wrapperBatFile.text = wrapperBatFile.text.replace("set DEFAULT_JVM_OPTS=", - "set GRADLE_OPTS=$gradleBatOpts %GRADLE_OPTS%\nset DEFAULT_JVM_OPTS=") + apply plugin: "kotlin" + apply plugin: "io.spring.nohttp" + apply plugin: 'org.springframework.build.api-diff' + apply from: "${rootDir}/gradle/publications.gradle" + apply from: "${rootDir}/gradle/docs.gradle" + + nohttp { + source.exclude "**/test-output/**" + allowlistFile = project.file("src/nohttp/allowlist.lines") + def rootPath = file(rootDir).toPath() + def projectDirs = allprojects.collect { it.projectDir } + "${rootDir}/buildSrc" + projectDirs.forEach { dir -> + [ 'bin', 'build', 'out', '.settings' ] + .collect { rootPath.relativize(new File(dir, it).toPath()) } + .forEach { source.exclude "$it/**" } + [ '.classpath', '.project' ] + .collect { rootPath.relativize(new File(dir, it).toPath()) } + .forEach { source.exclude "$it" } } } -} -/* - * Support publication of artifacts versioned by topic branch. - * CI builds supply `-P BRANCH_NAME=` to gradle at build time. - * If starts with 'SPR-', change version - * from BUILD-SNAPSHOT => -SNAPSHOT - * e.g. 3.2.1.BUILD-SNAPSHOT => 3.2.1.SPR-1234-SNAPSHOT - */ -def qualifyVersionIfNecessary(version) { - if (rootProject.hasProperty("BRANCH_NAME")) { - def qualifier = rootProject.getProperty("BRANCH_NAME") - if (qualifier.startsWith("SPR-")) { - return version.replace("BUILD", qualifier) + publishing { + publications { + mavenJava(MavenPublication) { + artifact docsZip + artifact schemaZip + artifact distZip + } } } - return version } diff --git a/buildSrc/README.md b/buildSrc/README.md new file mode 100644 index 000000000000..f48339e6d61f --- /dev/null +++ b/buildSrc/README.md @@ -0,0 +1,41 @@ +# Spring Framework Build + +This folder contains the custom plugins and conventions for the Spring Framework build. +They are declared in the `build.gradle` file in this folder. + +## Build Conventions + +### Compiler conventions + +The `org.springframework.build.compile` plugin applies the Java compiler conventions to the build. +By default, the build compiles sources with Java `1.8` source and target compatibility. +You can test a different source compatibility version on the CLI with a project property like: + +``` +./gradlew test -PjavaSourceVersion=11 +``` + +## Build Plugins + +## Optional dependencies + +The `org.springframework.build.optional-dependencies` plugin creates a new `optional` +Gradle configuration - it adds the dependencies to the project's compile and runtime classpath +but doesn't affect the classpath of dependent projects. +This plugin does not provide a `provided` configuration, as the native `compileOnly` and `testCompileOnly` +configurations are preferred. + +## API Diff + +This plugin uses the [Gradle JApiCmp](https://github.com/melix/japicmp-gradle-plugin) plugin +to generate API Diff reports for each Spring Framework module. This plugin is applied once on the root +project and creates tasks in each framework module. Unlike previous versions of this part of the build, +there is no need for checking out a specific tag. The plugin will fetch the JARs we want to compare the +current working version with. You can generate the reports for all modules or a single module: + +``` +./gradlew apiDiff -PbaselineVersion=5.1.0.RELEASE +./gradlew :spring-core:apiDiff -PbaselineVersion=5.1.0.RELEASE +``` + +The reports are located under `build/reports/api-diff/$OLDVERSION_to_$NEWVERSION/`. diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle new file mode 100644 index 000000000000..03f629496a28 --- /dev/null +++ b/buildSrc/build.gradle @@ -0,0 +1,30 @@ +plugins { + id 'java-gradle-plugin' +} + +repositories { + mavenCentral() + gradlePluginPortal() +} + +dependencies { + implementation "me.champeau.gradle:japicmp-gradle-plugin:0.2.8" + implementation "com.google.guava:guava:28.2-jre" // required by japicmp-gradle-plugin +} + +gradlePlugin { + plugins { + apiDiffPlugin { + id = "org.springframework.build.api-diff" + implementationClass = "org.springframework.build.api.ApiDiffPlugin" + } + compileConventionsPlugin { + id = "org.springframework.build.compile" + implementationClass = "org.springframework.build.compile.CompilerConventionsPlugin" + } + optionalDependenciesPlugin { + id = "org.springframework.build.optional-dependencies" + implementationClass = "org.springframework.build.optional.OptionalDependenciesPlugin" + } + } +} diff --git a/buildSrc/gradle.properties b/buildSrc/gradle.properties new file mode 100644 index 000000000000..160890028a4d --- /dev/null +++ b/buildSrc/gradle.properties @@ -0,0 +1 @@ +org.gradle.caching=true diff --git a/buildSrc/settings.gradle b/buildSrc/settings.gradle new file mode 100644 index 000000000000..ec93f39d06cc --- /dev/null +++ b/buildSrc/settings.gradle @@ -0,0 +1 @@ +apply from: "../gradle/build-cache-settings.gradle" diff --git a/buildSrc/spring-build-src.gradle b/buildSrc/spring-build-src.gradle deleted file mode 100644 index 295cbf1d5786..000000000000 --- a/buildSrc/spring-build-src.gradle +++ /dev/null @@ -1,10 +0,0 @@ -description = "Exposes gradle buildSrc for IDE support" - -apply plugin: "groovy" - -dependencies { - compile gradleApi() - compile localGroovy() -} - -configurations.archives.artifacts.clear() diff --git a/buildSrc/src/main/groovy/org/springframework/build/gradle/TestSourceSetDependenciesPlugin.groovy b/buildSrc/src/main/groovy/org/springframework/build/gradle/TestSourceSetDependenciesPlugin.groovy deleted file mode 100644 index 2423bb02423e..000000000000 --- a/buildSrc/src/main/groovy/org/springframework/build/gradle/TestSourceSetDependenciesPlugin.groovy +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2002-2014 the original author or authors. - * - * Licensed 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.springframework.build.gradle - -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.artifacts.Configuration; -import org.gradle.api.artifacts.ProjectDependency; - -/** - * Gradle plugin that automatically updates testCompile dependencies to include - * the test source sets of project dependencies. - * - * @author Phillip Webb - */ -class TestSourceSetDependenciesPlugin implements Plugin { - - @Override - public void apply(Project project) { - project.afterEvaluate { - Set projectDependencies = new LinkedHashSet() - collectProjectDependencies(projectDependencies, project) - projectDependencies.each { - project.dependencies.add("testCompile", it.dependencyProject.sourceSets.test.output) - } - } - } - - private void collectProjectDependencies(Set projectDependencies, Project project) { - for (def configurationName in ["compile", "optional", "provided", "testCompile"]) { - Configuration configuration = project.getConfigurations().findByName(configurationName) - if (configuration) { - configuration.dependencies.findAll { it instanceof ProjectDependency }.each { - projectDependencies.add(it) - collectProjectDependencies(projectDependencies, it.dependencyProject) - } - } - } - } - -} diff --git a/buildSrc/src/main/java/org/springframework/build/api/ApiDiffPlugin.java b/buildSrc/src/main/java/org/springframework/build/api/ApiDiffPlugin.java new file mode 100644 index 000000000000..931fd1022116 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/build/api/ApiDiffPlugin.java @@ -0,0 +1,115 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.build.api; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; + +import me.champeau.gradle.japicmp.JapicmpPlugin; +import me.champeau.gradle.japicmp.JapicmpTask; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; +import org.gradle.api.tasks.TaskProvider; +import org.gradle.jvm.tasks.Jar; + +/** + * {@link Plugin} that applies the {@code "japicmp-gradle-plugin"} + * and create tasks for all subprojects, diffing the public API one by one + * and creating the reports in {@code "build/reports/api-diff/$OLDVERSION_to_$NEWVERSION/"}. + *

{@code "./gradlew apiDiff -PbaselineVersion=5.1.0.RELEASE"} will output the + * reports for the API diff between the baseline version and the current one for all modules. + * You can limit the report to a single module with + * {@code "./gradlew :spring-core:apiDiff -PbaselineVersion=5.1.0.RELEASE"}. + * + * @author Brian Clozel + */ +public class ApiDiffPlugin implements Plugin { + + public static final String TASK_NAME = "apiDiff"; + + private static final String BASELINE_VERSION_PROPERTY = "baselineVersion"; + + private static final List PACKAGE_INCLUDES = Collections.singletonList("org.springframework.*"); + + @Override + public void apply(Project project) { + if (project.hasProperty(BASELINE_VERSION_PROPERTY) && project.equals(project.getRootProject())) { + project.getPluginManager().apply(JapicmpPlugin.class); + project.getPlugins().withType(JapicmpPlugin.class, + plugin -> applyApiDiffConventions(project)); + } + } + + private void applyApiDiffConventions(Project project) { + String baselineVersion = project.property(BASELINE_VERSION_PROPERTY).toString(); + project.subprojects(subProject -> createApiDiffTask(baselineVersion, subProject)); + } + + private void createApiDiffTask(String baselineVersion, Project project) { + if (isProjectEligible(project)) { + JapicmpTask apiDiff = project.getTasks().create(TASK_NAME, JapicmpTask.class); + apiDiff.setDescription("Generates an API diff report with japicmp"); + apiDiff.setGroup(JavaBasePlugin.DOCUMENTATION_GROUP); + + apiDiff.setOldClasspath(project.files(createBaselineConfiguration(baselineVersion, project))); + TaskProvider jar = project.getTasks().withType(Jar.class).named("jar"); + apiDiff.setNewArchives(project.getLayout().files(jar.get().getArchiveFile().get().getAsFile())); + apiDiff.setNewClasspath(getRuntimeClassPath(project)); + apiDiff.setPackageIncludes(PACKAGE_INCLUDES); + apiDiff.setOnlyModified(true); + apiDiff.setIgnoreMissingClasses(true); + // Ignore Kotlin metadata annotations since they contain + // illegal HTML characters and fail the report generation + apiDiff.setAnnotationExcludes(Collections.singletonList("@kotlin.Metadata")); + + apiDiff.setHtmlOutputFile(getOutputFile(baselineVersion, project)); + + apiDiff.dependsOn(project.getTasks().getByName("jar")); + } + } + + private boolean isProjectEligible(Project project) { + return project.getPlugins().hasPlugin(JavaPlugin.class) + && project.getPlugins().hasPlugin(MavenPublishPlugin.class); + } + + private Configuration createBaselineConfiguration(String baselineVersion, Project project) { + String baseline = String.join(":", + project.getGroup().toString(), project.getName(), baselineVersion); + Dependency baselineDependency = project.getDependencies().create(baseline + "@jar"); + return project.getRootProject().getConfigurations().detachedConfiguration(baselineDependency); + } + + private Configuration getRuntimeClassPath(Project project) { + return project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME); + } + + private File getOutputFile(String baseLineVersion, Project project) { + Path outDir = Paths.get(project.getRootProject().getBuildDir().getAbsolutePath(), + "reports", "api-diff", + baseLineVersion + "_to_" + project.getRootProject().getVersion()); + return project.file(outDir.resolve(project.getName() + ".html").toString()); + } + +} \ No newline at end of file diff --git a/buildSrc/src/main/java/org/springframework/build/compile/CompilerConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/build/compile/CompilerConventionsPlugin.java new file mode 100644 index 000000000000..db51666f74b5 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/build/compile/CompilerConventionsPlugin.java @@ -0,0 +1,106 @@ +/* + * Copyright 2002-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.build.compile; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.gradle.api.JavaVersion; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.tasks.compile.JavaCompile; + +/** + * {@link Plugin} that applies conventions for compiling Java sources in Spring Framework. + *

One can override the default Java source compatibility version + * with a dedicated property on the CLI: {@code "./gradlew test -PjavaSourceVersion=11"}. + * + * @author Brian Clozel + * @author Sam Brannen + */ +public class CompilerConventionsPlugin implements Plugin { + + /** + * The project property that can be used to switch the Java source + * compatibility version for building source and test classes. + */ + public static final String JAVA_SOURCE_VERSION_PROPERTY = "javaSourceVersion"; + + public static final JavaVersion DEFAULT_COMPILER_VERSION = JavaVersion.VERSION_1_8; + + private static final List COMPILER_ARGS; + + private static final List TEST_COMPILER_ARGS; + + static { + List commonCompilerArgs = Arrays.asList( + "-Xlint:serial", "-Xlint:cast", "-Xlint:classfile", "-Xlint:dep-ann", + "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally", "-Xlint:overrides", + "-Xlint:path", "-Xlint:processing", "-Xlint:static", "-Xlint:try", "-Xlint:-options" + ); + COMPILER_ARGS = new ArrayList<>(); + COMPILER_ARGS.addAll(commonCompilerArgs); + COMPILER_ARGS.addAll(Arrays.asList( + "-Xlint:varargs", "-Xlint:fallthrough", "-Xlint:rawtypes", "-Xlint:deprecation", + "-Xlint:unchecked", "-Werror" + )); + TEST_COMPILER_ARGS = new ArrayList<>(); + TEST_COMPILER_ARGS.addAll(commonCompilerArgs); + TEST_COMPILER_ARGS.addAll(Arrays.asList("-Xlint:-varargs", "-Xlint:-fallthrough", "-Xlint:-rawtypes", + "-Xlint:-deprecation", "-Xlint:-unchecked", "-parameters")); + } + + @Override + public void apply(Project project) { + project.getPlugins().withType(JavaPlugin.class, javaPlugin -> applyJavaCompileConventions(project)); + } + + /** + * Applies the common Java compiler options for main sources, test fixture sources, and + * test sources. + * @param project the current project + */ + private void applyJavaCompileConventions(Project project) { + JavaPluginConvention java = project.getConvention().getPlugin(JavaPluginConvention.class); + if (project.hasProperty(JAVA_SOURCE_VERSION_PROPERTY)) { + JavaVersion javaSourceVersion = JavaVersion.toVersion(project.property(JAVA_SOURCE_VERSION_PROPERTY)); + java.setSourceCompatibility(javaSourceVersion); + } + else { + java.setSourceCompatibility(DEFAULT_COMPILER_VERSION); + } + java.setTargetCompatibility(DEFAULT_COMPILER_VERSION); + + project.getTasks().withType(JavaCompile.class) + .matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_JAVA_TASK_NAME)) + .forEach(compileTask -> { + compileTask.getOptions().setCompilerArgs(COMPILER_ARGS); + compileTask.getOptions().setEncoding("UTF-8"); + }); + project.getTasks().withType(JavaCompile.class) + .matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME) + || compileTask.getName().equals("compileTestFixturesJava")) + .forEach(compileTask -> { + compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS); + compileTask.getOptions().setEncoding("UTF-8"); + }); + } + +} diff --git a/buildSrc/src/main/java/org/springframework/build/optional/OptionalDependenciesPlugin.java b/buildSrc/src/main/java/org/springframework/build/optional/OptionalDependenciesPlugin.java new file mode 100644 index 000000000000..55537c2d8259 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/build/optional/OptionalDependenciesPlugin.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.build.optional; + +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.tasks.SourceSetContainer; +import org.gradle.plugins.ide.eclipse.EclipsePlugin; +import org.gradle.plugins.ide.eclipse.model.EclipseModel; + +/** + * A {@code Plugin} that adds support for Maven-style optional dependencies. Creates a new + * {@code optional} configuration. The {@code optional} configuration is part of the + * project's compile and runtime classpath's but does not affect the classpath of + * dependent projects. + * + * @author Andy Wilkinson + */ +public class OptionalDependenciesPlugin implements Plugin { + + /** + * Name of the {@code optional} configuration. + */ + public static final String OPTIONAL_CONFIGURATION_NAME = "optional"; + + @Override + public void apply(Project project) { + Configuration optional = project.getConfigurations().create("optional"); + project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> { + SourceSetContainer sourceSets = project.getConvention() + .getPlugin(JavaPluginConvention.class).getSourceSets(); + sourceSets.all((sourceSet) -> { + sourceSet.setCompileClasspath( + sourceSet.getCompileClasspath().plus(optional)); + sourceSet.setRuntimeClasspath( + sourceSet.getRuntimeClasspath().plus(optional)); + }); + }); + project.getPlugins().withType(EclipsePlugin.class, (eclipePlugin) -> { + project.getExtensions().getByType(EclipseModel.class) + .classpath((classpath) -> { + classpath.getPlusConfigurations().add(optional); + }); + }); + } + +} \ No newline at end of file diff --git a/buildSrc/src/main/resources/META-INF/gradle-plugins/test-source-set-dependencies.properties b/buildSrc/src/main/resources/META-INF/gradle-plugins/test-source-set-dependencies.properties deleted file mode 100644 index f5df417ad20a..000000000000 --- a/buildSrc/src/main/resources/META-INF/gradle-plugins/test-source-set-dependencies.properties +++ /dev/null @@ -1 +0,0 @@ -implementation-class=org.springframework.build.gradle.TestSourceSetDependenciesPlugin diff --git a/ci/README.adoc b/ci/README.adoc new file mode 100644 index 000000000000..f66fd74a82c8 --- /dev/null +++ b/ci/README.adoc @@ -0,0 +1,57 @@ +== Spring Framework Concourse pipeline + +The Spring Framework uses https://concourse-ci.org/[Concourse] for its CI build and other automated tasks. +The Spring team has a dedicated Concourse instance available at https://ci.spring.io with a build pipeline +for https://ci.spring.io/teams/spring-framework/pipelines/spring-framework-5.2.x[Spring Framework 5.2.x]. + +=== Setting up your development environment + +If you're part of the Spring Framework project on GitHub, you can get access to CI management features. +First, you need to go to https://ci.spring.io and install the client CLI for your platform (see bottom right of the screen). + +You can then login with the instance using: + +[source] +---- +$ fly -t spring login -n spring-framework -c https://ci.spring.io +---- + +Once logged in, you should get something like: + +[source] +---- +$ fly ts +name url team expiry +spring https://ci.spring.io spring-framework Wed, 25 Mar 2020 17:45:26 UTC +---- + +=== Pipeline configuration and structure + +The build pipelines are described in `pipeline.yml` file. + +This file is listing Concourse resources, i.e. build inputs and outputs such as container images, artifact repositories, source repositories, notification services, etc. + +It also describes jobs (a job is a sequence of inputs, tasks and outputs); jobs are organized by groups. + +The `pipeline.yml` definition contains `((parameters))` which are loaded from the `parameters.yml` file or from our https://docs.cloudfoundry.org/credhub/[credhub instance]. + +You'll find in this folder the following resources: + +* `pipeline.yml` the build pipeline +* `parameters.yml` the build parameters used for the pipeline +* `images/` holds the container images definitions used in this pipeline +* `scripts/` holds the build scripts that ship within the CI container images +* `tasks` contains the task definitions used in the main `pipeline.yml` + +=== Updating the build pipeline + +Updating files on the repository is not enough to update the build pipeline, as changes need to be applied. + +The pipeline can be deployed using the following command: + +[source] +---- +$ fly -t spring set-pipeline -p spring-framework-5.2.x -c ci/pipeline.yml -l ci/parameters.yml +---- + +NOTE: This assumes that you have credhub integration configured with the appropriate secrets. diff --git a/ci/config/changelog-generator.yml b/ci/config/changelog-generator.yml new file mode 100644 index 000000000000..a029e25582e4 --- /dev/null +++ b/ci/config/changelog-generator.yml @@ -0,0 +1,17 @@ +changelog: + repository: spring-projects/spring-framework + sections: + - title: ":star: New Features" + labels: + - "type: enhancement" + - title: ":lady_beetle: Bug Fixes" + labels: + - "type: bug" + - "type: regression" + - title: ":notebook_with_decorative_cover: Documentation" + labels: + - "type: documentation" + - title: ":hammer: Dependency Upgrades" + sort: "title" + labels: + - "type: dependency-upgrade" diff --git a/ci/config/release-scripts.yml b/ci/config/release-scripts.yml new file mode 100644 index 000000000000..d31f8cba00dc --- /dev/null +++ b/ci/config/release-scripts.yml @@ -0,0 +1,10 @@ +logging: + level: + io.spring.concourse: DEBUG +spring: + main: + banner-mode: off +sonatype: + exclude: + - 'build-info\.json' + - '.*\.zip' diff --git a/ci/images/README.adoc b/ci/images/README.adoc new file mode 100644 index 000000000000..6da9addd9ca5 --- /dev/null +++ b/ci/images/README.adoc @@ -0,0 +1,21 @@ +== CI Images + +These images are used by CI to run the actual builds. + +To build the image locally run the following from this directory: + +---- +$ docker build --no-cache -f /Dockerfile . +---- + +For example + +---- +$ docker build --no-cache -f spring-framework-ci-image/Dockerfile . +---- + +To test run: + +---- +$ docker run -it --entrypoint /bin/bash +---- diff --git a/ci/images/ci-image-jdk11/Dockerfile b/ci/images/ci-image-jdk11/Dockerfile new file mode 100644 index 000000000000..253720dc2e21 --- /dev/null +++ b/ci/images/ci-image-jdk11/Dockerfile @@ -0,0 +1,8 @@ +FROM ubuntu:focal-20220302 + +ADD setup.sh /setup.sh +ADD get-jdk-url.sh /get-jdk-url.sh +RUN ./setup.sh java11 + +ENV JAVA_HOME /opt/openjdk +ENV PATH $JAVA_HOME/bin:$PATH diff --git a/ci/images/ci-image/Dockerfile b/ci/images/ci-image/Dockerfile new file mode 100644 index 000000000000..157d7f28b848 --- /dev/null +++ b/ci/images/ci-image/Dockerfile @@ -0,0 +1,8 @@ +FROM ubuntu:focal-20220302 + +ADD setup.sh /setup.sh +ADD get-jdk-url.sh /get-jdk-url.sh +RUN ./setup.sh java8 + +ENV JAVA_HOME /opt/openjdk +ENV PATH $JAVA_HOME/bin:$PATH diff --git a/ci/images/get-jdk-url.sh b/ci/images/get-jdk-url.sh new file mode 100755 index 000000000000..fca99d595e5c --- /dev/null +++ b/ci/images/get-jdk-url.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -e + +case "$1" in + java8) + echo "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u322-b06/OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz" + ;; + java11) + echo "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jdk_x64_linux_hotspot_11.0.14.1_1.tar.gz" + ;; + *) + echo $"Unknown java version" + exit 1 +esac diff --git a/ci/images/setup.sh b/ci/images/setup.sh new file mode 100755 index 000000000000..82f777d89fd4 --- /dev/null +++ b/ci/images/setup.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -ex + +########################################################### +# UTILS +########################################################### + +export DEBIAN_FRONTEND=noninteractive +apt-get update +apt-get install --no-install-recommends -y tzdata ca-certificates net-tools libxml2-utils git curl libudev1 libxml2-utils iptables iproute2 jq fontconfig +ln -fs /usr/share/zoneinfo/UTC /etc/localtime +dpkg-reconfigure --frontend noninteractive tzdata +rm -rf /var/lib/apt/lists/* + +curl https://raw.githubusercontent.com/spring-io/concourse-java-scripts/v0.0.4/concourse-java.sh > /opt/concourse-java.sh + +curl --output /opt/concourse-release-scripts.jar https://repo.spring.io/release/io/spring/concourse/releasescripts/concourse-release-scripts/0.3.2/concourse-release-scripts-0.3.2.jar + +########################################################### +# JAVA +########################################################### +JDK_URL=$( ./get-jdk-url.sh $1 ) + +mkdir -p /opt/openjdk +cd /opt/openjdk +curl -L ${JDK_URL} | tar zx --strip-components=1 +test -f /opt/openjdk/bin/java +test -f /opt/openjdk/bin/javac + +########################################################### +# GRADLE ENTERPRISE +########################################################### +cd / +mkdir ~/.gradle +echo 'systemProp.user.name=concourse' > ~/.gradle/gradle.properties diff --git a/ci/parameters.yml b/ci/parameters.yml new file mode 100644 index 000000000000..26579fc60280 --- /dev/null +++ b/ci/parameters.yml @@ -0,0 +1,13 @@ +email-server: "smtp.svc.pivotal.io" +email-from: "ci@spring.io" +email-to: ["spring-framework-dev@pivotal.io"] +github-repo: "https://github.com/spring-projects/spring-framework.git" +github-repo-name: "spring-projects/spring-framework" +docker-hub-organization: "springci" +artifactory-server: "https://repo.spring.io" +branch: "5.2.x" +milestone: "5.2.x" +build-name: "spring-framework" +pipeline-name: "spring-framework" +concourse-url: "https://ci.spring.io" +task-timeout: 1h00m diff --git a/ci/pipeline.yml b/ci/pipeline.yml new file mode 100644 index 000000000000..1f57b3d999b9 --- /dev/null +++ b/ci/pipeline.yml @@ -0,0 +1,391 @@ +anchors: + git-repo-resource-source: &git-repo-resource-source + uri: ((github-repo)) + username: ((github-username)) + password: ((github-ci-release-token)) + branch: ((branch)) + gradle-enterprise-task-params: &gradle-enterprise-task-params + GRADLE_ENTERPRISE_ACCESS_KEY: ((gradle_enterprise_secret_access_key)) + GRADLE_ENTERPRISE_CACHE_USERNAME: ((gradle_enterprise_cache_user.username)) + GRADLE_ENTERPRISE_CACHE_PASSWORD: ((gradle_enterprise_cache_user.password)) + sonatype-task-params: &sonatype-task-params + SONATYPE_USERNAME: ((sonatype-username)) + SONATYPE_PASSWORD: ((sonatype-password)) + SONATYPE_URL: ((sonatype-url)) + SONATYPE_STAGING_PROFILE_ID: ((sonatype-staging-profile-id)) + artifactory-task-params: &artifactory-task-params + ARTIFACTORY_SERVER: ((artifactory-server)) + ARTIFACTORY_USERNAME: ((artifactory-username)) + ARTIFACTORY_PASSWORD: ((artifactory-password)) + build-project-task-params: &build-project-task-params + privileged: true + timeout: ((task-timeout)) + params: + BRANCH: ((branch)) + <<: *gradle-enterprise-task-params + docker-resource-source: &docker-resource-source + username: ((docker-hub-username)) + password: ((docker-hub-password)) + tag: ((milestone)) + slack-fail-params: &slack-fail-params + text: > + :concourse-failed: + [$TEXT_FILE_CONTENT] + text_file: git-repo/build/build-scan-uri.txt + silent: true + icon_emoji: ":concourse:" + username: concourse-ci + changelog-task-params: &changelog-task-params + name: generated-changelog/tag + tag: generated-changelog/tag + body: generated-changelog/changelog.md + github-task-params: &github-task-params + GITHUB_USERNAME: ((github-username)) + GITHUB_TOKEN: ((github-ci-release-token)) + +resource_types: +- name: artifactory-resource + type: registry-image + source: + repository: springio/artifactory-resource + tag: 0.0.17 +- name: github-release + type: registry-image + source: + repository: concourse/github-release-resource + tag: 1.5.5 +- name: github-status-resource + type: registry-image + source: + repository: dpb587/github-status-resource + tag: master +- name: slack-notification + type: registry-image + source: + repository: cfcommunity/slack-notification-resource + tag: latest +resources: +- name: git-repo + type: git + icon: github + source: + <<: *git-repo-resource-source +- name: every-morning + type: time + icon: alarm + source: + start: 8:00 AM + stop: 9:00 AM + location: Europe/Vienna +- name: ci-images-git-repo + type: git + icon: github + source: + uri: ((github-repo)) + branch: ((branch)) + paths: ["ci/images/*"] +- name: ci-image + type: docker-image + icon: docker + source: + <<: *docker-resource-source + repository: ((docker-hub-organization))/spring-framework-ci +- name: ci-image-jdk11 + type: docker-image + icon: docker + source: + <<: *docker-resource-source + repository: ((docker-hub-organization))/spring-framework-ci-jdk11 +- name: artifactory-repo + type: artifactory-resource + icon: package-variant + source: + uri: ((artifactory-server)) + username: ((artifactory-username)) + password: ((artifactory-password)) + build_name: ((build-name)) +- name: repo-status-build + type: github-status-resource + icon: eye-check-outline + source: + repository: ((github-repo-name)) + access_token: ((github-ci-status-token)) + branch: ((branch)) + context: build +- name: repo-status-jdk11-build + type: github-status-resource + icon: eye-check-outline + source: + repository: ((github-repo-name)) + access_token: ((github-ci-status-token)) + branch: ((branch)) + context: jdk11-build +- name: slack-alert + type: slack-notification + icon: slack + source: + url: ((slack-webhook-url)) +- name: github-pre-release + type: github-release + icon: briefcase-download-outline + source: + owner: spring-projects + repository: spring-framework + access_token: ((github-ci-release-token)) + pre_release: true + release: false +- name: github-release + type: github-release + icon: briefcase-download + source: + owner: spring-projects + repository: spring-framework + access_token: ((github-ci-release-token)) + pre_release: false +jobs: +- name: build-ci-images + plan: + - get: ci-images-git-repo + trigger: true + - in_parallel: + - put: ci-image + params: + build: ci-images-git-repo/ci/images + dockerfile: ci-images-git-repo/ci/images/ci-image/Dockerfile + - put: ci-image-jdk11 + params: + build: ci-images-git-repo/ci/images + dockerfile: ci-images-git-repo/ci/images/ci-image-jdk11/Dockerfile +- name: build + serial: true + public: true + plan: + - get: ci-image + - get: git-repo + trigger: true + - put: repo-status-build + params: { state: "pending", commit: "git-repo" } + - do: + - task: build-project + image: ci-image + file: git-repo/ci/tasks/build-project.yml + <<: *build-project-task-params + on_failure: + do: + - put: repo-status-build + params: { state: "failure", commit: "git-repo" } + - put: slack-alert + params: + <<: *slack-fail-params + - put: repo-status-build + params: { state: "success", commit: "git-repo" } + - put: artifactory-repo + params: &artifactory-params + signing_key: ((signing-key)) + signing_passphrase: ((signing-passphrase)) + repo: libs-snapshot-local + folder: distribution-repository + build_uri: "https://ci.spring.io/teams/${BUILD_TEAM_NAME}/pipelines/${BUILD_PIPELINE_NAME}/jobs/${BUILD_JOB_NAME}/builds/${BUILD_NAME}" + build_number: "${BUILD_PIPELINE_NAME}-${BUILD_JOB_NAME}-${BUILD_NAME}" + disable_checksum_uploads: true + threads: 8 + artifact_set: + - include: + - "/**/spring-*.zip" + properties: + "zip.name": "spring-framework" + "zip.displayname": "Spring Framework" + "zip.deployed": "false" + - include: + - "/**/spring-*-docs.zip" + properties: + "zip.type": "docs" + - include: + - "/**/spring-*-dist.zip" + properties: + "zip.type": "dist" + - include: + - "/**/spring-*-schema.zip" + properties: + "zip.type": "schema" + get_params: + threads: 8 +- name: jdk11-build + serial: true + public: true + plan: + - get: ci-image-jdk11 + - get: git-repo + - get: every-morning + trigger: true + - put: repo-status-jdk11-build + params: { state: "pending", commit: "git-repo" } + - do: + - task: check-project + image: ci-image-jdk11 + file: git-repo/ci/tasks/check-project.yml + <<: *build-project-task-params + on_failure: + do: + - put: repo-status-jdk11-build + params: { state: "failure", commit: "git-repo" } + - put: slack-alert + params: + <<: *slack-fail-params + - put: repo-status-jdk11-build + params: { state: "success", commit: "git-repo" } +- name: stage-milestone + serial: true + plan: + - get: ci-image + - get: git-repo + trigger: false + - task: stage + image: ci-image + file: git-repo/ci/tasks/stage-version.yml + params: + RELEASE_TYPE: M + <<: *gradle-enterprise-task-params + - put: artifactory-repo + params: + <<: *artifactory-params + repo: libs-staging-local + - put: git-repo + params: + repository: stage-git-repo +- name: promote-milestone + serial: true + plan: + - get: ci-image + - get: git-repo + trigger: false + - get: artifactory-repo + trigger: false + passed: [stage-milestone] + params: + download_artifacts: false + save_build_info: true + - task: promote + image: ci-image + file: git-repo/ci/tasks/promote-version.yml + params: + RELEASE_TYPE: M + <<: *artifactory-task-params + - task: generate-changelog + file: git-repo/ci/tasks/generate-changelog.yml + params: + RELEASE_TYPE: M + <<: *github-task-params + - put: github-pre-release + params: + <<: *changelog-task-params +- name: stage-rc + serial: true + plan: + - get: ci-image + - get: git-repo + trigger: false + - task: stage + image: ci-image + file: git-repo/ci/tasks/stage-version.yml + params: + RELEASE_TYPE: RC + <<: *gradle-enterprise-task-params + - put: artifactory-repo + params: + <<: *artifactory-params + repo: libs-staging-local + - put: git-repo + params: + repository: stage-git-repo +- name: promote-rc + serial: true + plan: + - get: ci-image + - get: git-repo + trigger: false + - get: artifactory-repo + trigger: false + passed: [stage-rc] + params: + download_artifacts: false + save_build_info: true + - task: promote + image: ci-image + file: git-repo/ci/tasks/promote-version.yml + params: + RELEASE_TYPE: RC + <<: *artifactory-task-params + - task: generate-changelog + file: git-repo/ci/tasks/generate-changelog.yml + params: + RELEASE_TYPE: RC + <<: *github-task-params + - put: github-pre-release + params: + <<: *changelog-task-params +- name: stage-release + serial: true + plan: + - get: ci-image + - get: git-repo + trigger: false + - task: stage + image: ci-image + file: git-repo/ci/tasks/stage-version.yml + params: + RELEASE_TYPE: RELEASE + <<: *gradle-enterprise-task-params + - put: artifactory-repo + params: + <<: *artifactory-params + repo: libs-staging-local + - put: git-repo + params: + repository: stage-git-repo +- name: promote-release + serial: true + plan: + - get: ci-image + - get: git-repo + trigger: false + - get: artifactory-repo + trigger: false + passed: [stage-release] + params: + download_artifacts: true + save_build_info: true + - task: promote + image: ci-image + file: git-repo/ci/tasks/promote-version.yml + params: + RELEASE_TYPE: RELEASE + <<: *artifactory-task-params + <<: *sonatype-task-params +- name: create-github-release + serial: true + plan: + - get: ci-image + - get: git-repo + - get: artifactory-repo + trigger: true + passed: [promote-release] + params: + download_artifacts: false + save_build_info: true + - task: generate-changelog + file: git-repo/ci/tasks/generate-changelog.yml + params: + RELEASE_TYPE: RELEASE + <<: *github-task-params + - put: github-release + params: + <<: *changelog-task-params + +groups: +- name: "builds" + jobs: ["build", "jdk11-build"] +- name: "releases" + jobs: ["stage-milestone", "stage-rc", "stage-release", "promote-milestone", "promote-rc", "promote-release", "create-github-release"] +- name: "ci-images" + jobs: ["build-ci-images"] diff --git a/ci/scripts/build-project.sh b/ci/scripts/build-project.sh new file mode 100755 index 000000000000..3844d1a3ddb4 --- /dev/null +++ b/ci/scripts/build-project.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -e + +source $(dirname $0)/common.sh +repository=$(pwd)/distribution-repository + +pushd git-repo > /dev/null +./gradlew -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false --no-daemon --max-workers=4 -PdeploymentRepository=${repository} build publishAllPublicationsToDeploymentRepository +popd > /dev/null diff --git a/ci/scripts/check-project.sh b/ci/scripts/check-project.sh new file mode 100755 index 000000000000..94c4e8df65b4 --- /dev/null +++ b/ci/scripts/check-project.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +source $(dirname $0)/common.sh + +pushd git-repo > /dev/null +./gradlew -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false --no-daemon --max-workers=4 check +popd > /dev/null diff --git a/ci/scripts/common.sh b/ci/scripts/common.sh new file mode 100644 index 000000000000..1accaa616732 --- /dev/null +++ b/ci/scripts/common.sh @@ -0,0 +1,2 @@ +source /opt/concourse-java.sh +setup_symlinks \ No newline at end of file diff --git a/ci/scripts/generate-changelog.sh b/ci/scripts/generate-changelog.sh new file mode 100755 index 000000000000..49e96c1ff326 --- /dev/null +++ b/ci/scripts/generate-changelog.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -e + +CONFIG_DIR=git-repo/ci/config +version=$( cat artifactory-repo/build-info.json | jq -r '.buildInfo.modules[0].id' | sed 's/.*:.*:\(.*\)/\1/' ) + +milestone=${version} +if [[ $RELEASE_TYPE = "RELEASE" ]]; then + milestone=${version%.RELEASE} +fi + +java -jar /github-changelog-generator.jar \ + --spring.config.location=${CONFIG_DIR}/changelog-generator.yml \ + ${milestone} generated-changelog/changelog.md + +echo ${version} > generated-changelog/version +echo v${version} > generated-changelog/tag diff --git a/ci/scripts/promote-version.sh b/ci/scripts/promote-version.sh new file mode 100755 index 000000000000..44c5ff626f91 --- /dev/null +++ b/ci/scripts/promote-version.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +source $(dirname $0)/common.sh +CONFIG_DIR=git-repo/ci/config + +version=$( cat artifactory-repo/build-info.json | jq -r '.buildInfo.modules[0].id' | sed 's/.*:.*:\(.*\)/\1/' ) +export BUILD_INFO_LOCATION=$(pwd)/artifactory-repo/build-info.json + +java -jar /opt/concourse-release-scripts.jar \ + --spring.config.location=${CONFIG_DIR}/release-scripts.yml \ + publishToCentral $RELEASE_TYPE $BUILD_INFO_LOCATION artifactory-repo || { exit 1; } + +java -jar /opt/concourse-release-scripts.jar \ + --spring.config.location=${CONFIG_DIR}/release-scripts.yml \ + promote $RELEASE_TYPE $BUILD_INFO_LOCATION || { exit 1; } + +echo "Promotion complete" +echo $version > version/version diff --git a/ci/scripts/stage-version.sh b/ci/scripts/stage-version.sh new file mode 100755 index 000000000000..5bb7300e799a --- /dev/null +++ b/ci/scripts/stage-version.sh @@ -0,0 +1,50 @@ +#!/bin/bash +set -e + +source $(dirname $0)/common.sh +repository=$(pwd)/distribution-repository + +pushd git-repo > /dev/null +git fetch --tags --all > /dev/null +popd > /dev/null + +git clone git-repo stage-git-repo > /dev/null + +pushd stage-git-repo > /dev/null + +snapshotVersion=$( awk -F '=' '$1 == "version" { print $2 }' gradle.properties ) +if [[ $RELEASE_TYPE = "M" ]]; then + stageVersion=$( get_next_milestone_release $snapshotVersion) + nextVersion=$snapshotVersion +elif [[ $RELEASE_TYPE = "RC" ]]; then + stageVersion=$( get_next_rc_release $snapshotVersion) + nextVersion=$snapshotVersion +elif [[ $RELEASE_TYPE = "RELEASE" ]]; then + stageVersion=$( get_next_release $snapshotVersion "RELEASE") + nextVersion=$( bump_version_number $snapshotVersion) +else + echo "Unknown release type $RELEASE_TYPE" >&2; exit 1; +fi + +echo "Staging $stageVersion (next version will be $nextVersion)" +sed -i "s/version=$snapshotVersion/version=$stageVersion/" gradle.properties + +git config user.name "Spring Builds" > /dev/null +git config user.email "spring-builds@users.noreply.github.com" > /dev/null +git add gradle.properties > /dev/null +git commit -m"Release v$stageVersion" > /dev/null +git tag -a "v$stageVersion" -m"Release v$stageVersion" > /dev/null + +./gradlew --no-daemon --max-workers=4 -PdeploymentRepository=${repository} build publishAllPublicationsToDeploymentRepository + +git reset --hard HEAD^ > /dev/null +if [[ $nextVersion != $snapshotVersion ]]; then + echo "Setting next development version (v$nextVersion)" + sed -i "s/version=$snapshotVersion/version=$nextVersion/" gradle.properties + git add gradle.properties > /dev/null + git commit -m"Next development version (v$nextVersion)" > /dev/null +fi; + +echo "Staging Complete" + +popd > /dev/null diff --git a/ci/scripts/sync-to-maven-central.sh b/ci/scripts/sync-to-maven-central.sh new file mode 100755 index 000000000000..b42631164ed5 --- /dev/null +++ b/ci/scripts/sync-to-maven-central.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +export BUILD_INFO_LOCATION=$(pwd)/artifactory-repo/build-info.json +version=$( cat artifactory-repo/build-info.json | jq -r '.buildInfo.modules[0].id' | sed 's/.*:.*:\(.*\)/\1/' ) +java -jar /opt/concourse-release-scripts.jar syncToCentral "RELEASE" $BUILD_INFO_LOCATION || { exit 1; } + +echo "Sync complete" +echo $version > version/version diff --git a/ci/tasks/build-project.yml b/ci/tasks/build-project.yml new file mode 100644 index 000000000000..759749ef433f --- /dev/null +++ b/ci/tasks/build-project.yml @@ -0,0 +1,22 @@ +--- +platform: linux +inputs: +- name: git-repo +outputs: +- name: distribution-repository +- name: git-repo +caches: +- path: gradle +params: + BRANCH: + CI: true + GRADLE_ENTERPRISE_ACCESS_KEY: + GRADLE_ENTERPRISE_CACHE_USERNAME: + GRADLE_ENTERPRISE_CACHE_PASSWORD: + GRADLE_ENTERPRISE_URL: https://ge.spring.io +run: + path: bash + args: + - -ec + - | + ${PWD}/git-repo/ci/scripts/build-project.sh diff --git a/ci/tasks/check-project.yml b/ci/tasks/check-project.yml new file mode 100644 index 000000000000..ea6d6ddb94c8 --- /dev/null +++ b/ci/tasks/check-project.yml @@ -0,0 +1,22 @@ +--- +platform: linux +inputs: +- name: git-repo +outputs: +- name: distribution-repository +- name: git-repo +caches: +- path: gradle +params: + BRANCH: + CI: true + GRADLE_ENTERPRISE_ACCESS_KEY: + GRADLE_ENTERPRISE_CACHE_USERNAME: + GRADLE_ENTERPRISE_CACHE_PASSWORD: + GRADLE_ENTERPRISE_URL: https://ge.spring.io +run: + path: bash + args: + - -ec + - | + ${PWD}/git-repo/ci/scripts/check-project.sh diff --git a/ci/tasks/generate-changelog.yml b/ci/tasks/generate-changelog.yml new file mode 100755 index 000000000000..b3f40278c8e6 --- /dev/null +++ b/ci/tasks/generate-changelog.yml @@ -0,0 +1,20 @@ +--- +platform: linux +image_resource: + type: registry-image + source: + repository: springio/github-changelog-generator + tag: '0.0.7' +inputs: +- name: git-repo +- name: artifactory-repo +outputs: +- name: generated-changelog +params: + GITHUB_ORGANIZATION: + GITHUB_REPO: + GITHUB_USERNAME: + GITHUB_TOKEN: + RELEASE_TYPE: +run: + path: git-repo/ci/scripts/generate-changelog.sh diff --git a/ci/tasks/promote-version.yml b/ci/tasks/promote-version.yml new file mode 100644 index 000000000000..abdd8fed5c5c --- /dev/null +++ b/ci/tasks/promote-version.yml @@ -0,0 +1,18 @@ +--- +platform: linux +inputs: +- name: git-repo +- name: artifactory-repo +outputs: +- name: version +params: + RELEASE_TYPE: + ARTIFACTORY_SERVER: + ARTIFACTORY_USERNAME: + ARTIFACTORY_PASSWORD: + SONATYPE_USER: + SONATYPE_PASSWORD: + SONATYPE_URL: + SONATYPE_STAGING_PROFILE_ID: +run: + path: git-repo/ci/scripts/promote-version.sh diff --git a/ci/tasks/stage-version.yml b/ci/tasks/stage-version.yml new file mode 100644 index 000000000000..ded11483a76b --- /dev/null +++ b/ci/tasks/stage-version.yml @@ -0,0 +1,17 @@ +--- +platform: linux +inputs: +- name: git-repo +outputs: +- name: stage-git-repo +- name: distribution-repository +params: + RELEASE_TYPE: + CI: true + GRADLE_ENTERPRISE_CACHE_USERNAME: + GRADLE_ENTERPRISE_CACHE_PASSWORD: + GRADLE_ENTERPRISE_URL: https://ge.spring.io +caches: +- path: gradle +run: + path: git-repo/ci/scripts/stage-version.sh diff --git a/framework-bom/framework-bom.gradle b/framework-bom/framework-bom.gradle new file mode 100644 index 000000000000..bf9d620a807f --- /dev/null +++ b/framework-bom/framework-bom.gradle @@ -0,0 +1,29 @@ +description = "Spring Framework (Bill of Materials)" + +apply plugin: 'java-platform' +apply from: "$rootDir/gradle/publications.gradle" + +group = "org.springframework" + +dependencies { + constraints { + parent.moduleProjects.sort { "$it.name" }.each { + api it + } + } +} + +publishing { + publications { + mavenJava(MavenPublication) { + artifactId = 'spring-framework-bom' + from components.javaPlatform + // remove scope information from published BOM + pom.withXml { + asNode().dependencyManagement.first().dependencies.first().each { + it.remove(it.scope.first()) + } + } + } + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 6adf0ecbeb98..8673a6ed3fd2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,4 @@ -version=5.1.0.BUILD-SNAPSHOT +version=5.2.20.RELEASE +org.gradle.jvmargs=-Xmx1536M +org.gradle.caching=true +org.gradle.parallel=true diff --git a/gradle/build-cache-settings.gradle b/gradle/build-cache-settings.gradle new file mode 100644 index 000000000000..5684b4942b8f --- /dev/null +++ b/gradle/build-cache-settings.gradle @@ -0,0 +1,18 @@ +buildCache { + local { + enabled = true + } + remote(HttpBuildCache) { + enabled = true + url = 'https://ge.spring.io/cache/' + def cacheUsername = System.getenv('GRADLE_ENTERPRISE_CACHE_USERNAME') + def cachePassword = System.getenv('GRADLE_ENTERPRISE_CACHE_PASSWORD') + if (cacheUsername && cachePassword) { + push = true + credentials { + username = cacheUsername + password = cachePassword + } + } + } +} diff --git a/gradle/build-scan-user-data.gradle b/gradle/build-scan-user-data.gradle new file mode 100644 index 000000000000..156191d11526 --- /dev/null +++ b/gradle/build-scan-user-data.gradle @@ -0,0 +1,16 @@ +addCustomJavaHomeMetadata() +addCustomJavaSourceVersionMetadata() + +void addCustomJavaHomeMetadata() { + def customJavaHome = System.getProperty("customJavaHome") + if (customJavaHome) { + buildScan.value "Custom JAVA_HOME", customJavaHome + } +} + +void addCustomJavaSourceVersionMetadata() { + def customJavaSourceVersion = System.getProperty("customJavaSourceVersion") + if (customJavaSourceVersion) { + buildScan.value "Custom Java Source Version", customJavaSourceVersion + } +} diff --git a/gradle/custom-java-home.gradle b/gradle/custom-java-home.gradle new file mode 100644 index 000000000000..54d1de1eb8f9 --- /dev/null +++ b/gradle/custom-java-home.gradle @@ -0,0 +1,80 @@ +// ----------------------------------------------------------------------------- +// +// This script adds support for the following two JVM system properties +// that control the build for alternative JDKs (i.e., a JDK other than +// the one used to launch the Gradle process). +// +// - customJavaHome: absolute path to the alternate JDK installation to +// use to compile Java code and execute tests. This system property +// is also used in spring-oxm.gradle to determine whether JiBX is +// supported. +// +// - customJavaSourceVersion: Java version supplied to the `--release` +// command line flag to control the Java source and target +// compatibility version. Supported versions include 9 or higher. +// Do not set this system property if Java 8 should be used. +// +// Examples: +// +// ./gradlew -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home test +// +// ./gradlew --no-build-cache -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home test +// +// ./gradlew -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home -DcustomJavaSourceVersion=14 test +// +// +// Credits: inspired by work from Marc Philipp and Stephane Nicoll +// +// ----------------------------------------------------------------------------- + +import org.gradle.internal.os.OperatingSystem +// import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile + +def customJavaHome = System.getProperty("customJavaHome") + +if (customJavaHome) { + def customJavaHomeDir = new File(customJavaHome) + def customJavaSourceVersion = System.getProperty("customJavaSourceVersion") + + tasks.withType(JavaCompile) { + logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHomeDir) + options.forkOptions.javaHome = customJavaHomeDir + inputs.property("customJavaHome", customJavaHome) + if (customJavaSourceVersion) { + options.compilerArgs += [ "--release", customJavaSourceVersion] + inputs.property("customJavaSourceVersion", customJavaSourceVersion) + } + } + + tasks.withType(GroovyCompile) { + logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHomeDir) + options.forkOptions.javaHome = customJavaHomeDir + inputs.property("customJavaHome", customJavaHome) + if (customJavaSourceVersion) { + options.compilerArgs += [ "--release", customJavaSourceVersion] + inputs.property("customJavaSourceVersion", customJavaSourceVersion) + } + } + + /* + tasks.withType(KotlinJvmCompile) { + logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHome) + kotlinOptions.jdkHome = customJavaHomeDir + inputs.property("customJavaHome", customJavaHome) + } + */ + + tasks.withType(Test) { + def javaExecutable = customJavaHome + "/bin/java" + if (OperatingSystem.current().isWindows()) { + javaExecutable += ".exe" + } + logger.info("Java executable for " + it.name + " task in " + project.name + ": " + javaExecutable) + executable = javaExecutable + inputs.property("customJavaHome", customJavaHome) + if (customJavaSourceVersion) { + inputs.property("customJavaSourceVersion", customJavaSourceVersion) + } + } + +} diff --git a/gradle/docs.gradle b/gradle/docs.gradle index 8102dfb5509e..27005827312d 100644 --- a/gradle/docs.gradle +++ b/gradle/docs.gradle @@ -1,161 +1,233 @@ -/* - * Copyright 2002-2017 the original author or authors. - * - * Licensed 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. - */ +configurations { + asciidoctorExt +} + +dependencies { + asciidoctorExt("io.spring.asciidoctor:spring-asciidoctor-extensions-block-switch:0.4.0.RELEASE") +} + +repositories { + maven { + url "https://repo.spring.io/release" + mavenContent { + includeGroup "io.spring.asciidoctor" + } + } +} +/** + * Produce Javadoc for all Spring Framework modules in "build/docs/javadoc" + */ task api(type: Javadoc) { group = "Documentation" description = "Generates aggregated Javadoc API documentation." title = "${rootProject.description} ${version} API" dependsOn { - subprojects.collect { + moduleProjects.collect { it.tasks.getByName("jar") } } - options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED - options.author = true - options.header = rootProject.description - options.use = true - options.overview = "src/docs/api/overview.html" - options.stylesheetFile = file("src/docs/api/stylesheet.css") - options.splitIndex = true - options.links(project.ext.javadocLinks) - options.addStringOption('Xdoclint:none', '-quiet') - - source subprojects.collect { project -> - project.sourceSets.main.allJava - } - - maxMemory = "1024m" - destinationDir = new File(buildDir, "api") - doFirst { classpath = files( // ensure the javadoc process can resolve types compiled from .aj sources project(":spring-aspects").sourceSets.main.output ) - classpath += files(subprojects.collect { it.sourceSets.main.compileClasspath }) + classpath += files(moduleProjects.collect { it.sourceSets.main.compileClasspath }) + } + + options { + encoding = "UTF-8" + memberLevel = JavadocMemberLevel.PROTECTED + author = true + header = rootProject.description + use = true + overview = "src/docs/api/overview.html" + stylesheetFile = file("src/docs/api/stylesheet.css") + splitIndex = true + links(project.ext.javadocLinks) + addStringOption('Xdoclint:none', '-quiet') + if(JavaVersion.current().isJava9Compatible()) { + addBooleanOption('html5', true) + } } + source moduleProjects.collect { project -> + project.sourceSets.main.allJava + } + maxMemory = "1024m" + destinationDir = file("$buildDir/docs/javadoc") } +/** + * Produce KDoc for all Spring Framework modules in "build/docs/kdoc" + */ dokka { dependsOn { tasks.getByName("api") } + doFirst { - classpath = subprojects.collect { project -> project.jar.outputs.files.getFiles() }.flatten() - classpath += files(subprojects.collect { it.sourceSets.main.compileClasspath }) + configuration { + classpath = moduleProjects.collect { project -> project.jar.outputs.files.getFiles() }.flatten() + classpath += files(moduleProjects.collect { it.sourceSets.main.compileClasspath }) + moduleProjects.findAll { + it.pluginManager.hasPlugin("kotlin") + }.each { project -> + def kotlinDirs = project.sourceSets.main.kotlin.srcDirs.collect() + kotlinDirs -= project.sourceSets.main.java.srcDirs + kotlinDirs.each { dir -> + if (dir.exists()) { + sourceRoot { + path = dir.path + } + } + } + } + } } - moduleName = "spring-framework" + outputFormat = "html" outputDirectory = "$buildDir/docs/kdoc" - sourceDirs = files(subprojects.collect { project -> - def kotlinDirs = project.sourceSets.main.kotlin.srcDirs.collect() - kotlinDirs -= project.sourceSets.main.java.srcDirs - }) - externalDocumentationLink { - url = new URL("https://docs.spring.io/spring-framework/docs/$version/javadoc-api/") - packageListUrl = new File(buildDir, "api/package-list").toURI().toURL() - } - externalDocumentationLink { - url = new URL("http://projectreactor.io/docs/core/release/api/") + configuration { + moduleName = "spring-framework" + + externalDocumentationLink { + url = new URL("https://docs.spring.io/spring-framework/docs/$version/javadoc-api/") + packageListUrl = new File(buildDir, "docs/javadoc/package-list").toURI().toURL() + } + externalDocumentationLink { + url = new URL("https://projectreactor.io/docs/core/release/api/") + } + externalDocumentationLink { + url = new URL("https://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/") + } } - externalDocumentationLink { - url = new URL("http://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/") +} + +task downloadResources(type: Download) { + def version = "0.2.1.RELEASE" + src "https://repo.spring.io/release/io/spring/docresources/" + + "spring-doc-resources/$version/spring-doc-resources-${version}.zip" + dest project.file("$buildDir/docs/spring-doc-resources.zip") + onlyIfModified true + useETag "all" +} + +task extractDocResources(type: Copy, dependsOn: downloadResources) { + from project.zipTree(downloadResources.dest); + into "$buildDir/docs/spring-docs-resources/" +} + +asciidoctorj { + modules { + pdf { + version '1.5.0-beta.8' + } } + fatalWarnings ".*" + options doctype: 'book', eruby: 'erubis' + attributes([ + icons: 'font', + idprefix: '', + idseparator: '-', + docinfo: 'shared', + revnumber: project.version, + sectanchors: '', + sectnums: '', + 'source-highlighter': 'highlight.js', + highlightjsdir: 'js/highlight', + 'highlightjs-theme': 'github', // 'googlecode', + stylesdir: 'css/', + stylesheet: 'stylesheet.css', + 'spring-version': project.version + ]) } +/** + * Generate the Spring Framework Reference documentation from "src/docs/asciidoc" + * in "build/docs/ref-docs/html5". + */ asciidoctor { + baseDirFollowsSourceDir() + configurations 'asciidoctorExt' sources { include '*.adoc' } + outputDir "$buildDir/docs/ref-docs/html5" + logDocuments = true resources { from(sourceDir) { - include 'images/*', 'stylesheets/*', 'tocbot-3.0.2/*' + include 'images/*', 'css/**', 'js/**' } + from extractDocResources } - logDocuments = true - backends = ["html5"] - // only ouput PDF documentation for non-SNAPSHOT builds - if(!project.getVersion().toString().contains("BUILD-SNAPSHOT")) { - backends += "pdf" - } - options doctype: 'book', eruby: 'erubis' - attributes 'icons': 'font', - 'idprefix': '', - 'idseparator': '-', - docinfo: '', - revnumber: project.version, - sectanchors: '', - sectnums: '', - 'source-highlighter': 'coderay@', // TODO switch to 'rouge' once supported by the html5 backend - stylesdir: 'stylesheets/', - stylesheet: 'main.css', - 'spring-version': project.version +} +/** + * Generate the Spring Framework Reference documentation from "src/docs/asciidoc" + * in "build/docs/ref-docs/pdf". + */ +asciidoctorPdf { + baseDirFollowsSourceDir() + configurations 'asciidoctorExt' + sources { + include '*.adoc' + } + outputDir "$buildDir/docs/ref-docs/pdf" + logDocuments = true } -task docsZip(type: Zip, dependsOn: ['api', 'asciidoctor', 'dokka']) { +/** + * Zip all docs (API and reference) into a single archive + */ +task docsZip(type: Zip, dependsOn: ['api', 'asciidoctor', 'asciidoctorPdf', 'dokka']) { group = "Distribution" - baseName = "spring-framework" - classifier = "docs" - description = "Builds -${classifier} archive containing api and reference " + - "for deployment at http://docs.spring.io/spring-framework/docs." + description = "Builds -${archiveClassifier} archive containing api and reference " + + "for deployment at https://docs.spring.io/spring-framework/docs." + archiveBaseName.set("spring-framework") + archiveClassifier.set("docs") from("src/dist") { include "changelog.txt" } - from (api) { into "javadoc-api" } - - from ("$asciidoctor.outputDir/html5") { + from ("$asciidoctor.outputDir") { into "spring-framework-reference" } - - from ("$asciidoctor.outputDir/pdf") { + from ("$asciidoctorPdf.outputDir") { into "spring-framework-reference/pdf" } - from (dokka) { into "kdoc-api" } } +/** + * Zip all Spring Framework schemas into a single archive + */ task schemaZip(type: Zip) { group = "Distribution" - baseName = "spring-framework" - classifier = "schema" - description = "Builds -${classifier} archive containing all " + - "XSDs for deployment at http://springframework.org/schema." - duplicatesStrategy 'exclude' - moduleProjects.each { subproject -> + archiveBaseName.set("spring-framework") + archiveClassifier.set("schema") + description = "Builds -${archiveClassifier} archive containing all " + + "XSDs for deployment at https://springframework.org/schema." + duplicatesStrategy DuplicatesStrategy.EXCLUDE + moduleProjects.each { module -> def Properties schemas = new Properties(); - subproject.sourceSets.main.resources.find { - it.path.endsWith("META-INF/spring.schemas") + module.sourceSets.main.resources.find { + (it.path.endsWith("META-INF/spring.schemas") || it.path.endsWith("META-INF\\spring.schemas")) }?.withInputStream { schemas.load(it) } for (def key : schemas.keySet()) { def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1') assert shortName != key - File xsdFile = subproject.sourceSets.main.resources.find { - it.path.endsWith(schemas.get(key)) + File xsdFile = module.sourceSets.main.resources.find { + (it.path.endsWith(schemas.get(key)) || it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))) } assert xsdFile != null into (shortName) { @@ -165,15 +237,18 @@ task schemaZip(type: Zip) { } } +/** + * Create a distribution zip with everything: + * docs, schemas, jars, source jars, javadoc jars + */ task distZip(type: Zip, dependsOn: [docsZip, schemaZip]) { - group = "Distribution" - baseName = "spring-framework" - classifier = "dist" - description = "Builds -${classifier} archive, containing all jars and docs, " + + archiveBaseName.set("spring-framework") + archiveClassifier.set("dist") + description = "Builds -${archiveClassifier} archive, containing all jars and docs, " + "suitable for community download page." - ext.baseDir = "${baseName}-${project.version}"; + ext.baseDir = "spring-framework-${project.version}"; from("src/docs/dist") { include "readme.txt" @@ -191,49 +266,17 @@ task distZip(type: Zip, dependsOn: [docsZip, schemaZip]) { into "${baseDir}/schema" } - moduleProjects.each { subproject -> + moduleProjects.each { module -> into ("${baseDir}/libs") { - from subproject.jar - if (subproject.tasks.findByPath("sourcesJar")) { - from subproject.sourcesJar + from module.jar + if (module.tasks.findByPath("sourcesJar")) { + from module.sourcesJar } - if (subproject.tasks.findByPath("javadocJar")) { - from subproject.javadocJar + if (module.tasks.findByPath("javadocJar")) { + from module.javadocJar } } } } -distZip.mustRunAfter subprojects.test - -// Create a distribution that contains all dependencies (required and optional). -// Not published by default; only for use when building from source. -task depsZip(type: Zip, dependsOn: distZip) { zipTask -> - group = "Distribution" - baseName = "spring-framework" - classifier = "dist-with-deps" - description = "Builds -${classifier} archive, containing everything " + - "in the -${distZip.classifier} archive plus all runtime dependencies." - - from zipTree(distZip.archivePath) - - gradle.taskGraph.whenReady { taskGraph -> - if (taskGraph.hasTask(":${zipTask.name}")) { - def projectNames = rootProject.subprojects*.name - def artifacts = new HashSet() - subprojects.each { subproject -> - (subproject.configurations.runtime.resolvedConfiguration.resolvedArtifacts + - subproject.configurations.optional.resolvedConfiguration.resolvedArtifacts).each { artifact -> - def dependency = artifact.moduleVersion.id - if (!projectNames.contains(dependency.name)) { - artifacts << artifact.file - } - } - } - - zipTask.from(artifacts) { - into "${distZip.baseDir}/deps" - } - } - } -} +distZip.mustRunAfter moduleProjects.check diff --git a/gradle/ide.gradle b/gradle/ide.gradle index a643dbbb7f8f..acb37abadbbf 100644 --- a/gradle/ide.gradle +++ b/gradle/ide.gradle @@ -1,9 +1,7 @@ import org.gradle.plugins.ide.eclipse.model.ProjectDependency import org.gradle.plugins.ide.eclipse.model.SourceFolder - -apply plugin: "propdeps-eclipse" -apply plugin: "propdeps-idea" +apply plugin: "eclipse" eclipse.jdt { sourceCompatibility = 1.8 @@ -11,7 +9,7 @@ eclipse.jdt { } // Replace classpath entries with project dependencies (GRADLE-1116) -// http://issues.gradle.org/browse/GRADLE-1116 +// https://issues.gradle.org/browse/GRADLE-1116 eclipse.classpath.file.whenMerged { classpath -> def regexp = /.*?\/([^\/]+)\/build\/([^\/]+\/)+(?:main|test)/ // only match those that end in main or test (avoids removing necessary entries like build/classes/jaxb) def projectOutputDependencies = classpath.entries.findAll { entry -> entry.path =~ regexp } @@ -31,12 +29,11 @@ eclipse.classpath.file.whenMerged { classpath -> classpath.entries.removeAll { entry -> (entry.path =~ /(?!.*?repack.*\.jar).*?\/([^\/]+)\/build\/libs\/[^\/]+\.jar/) } } - // Use separate main/test outputs (prevents WTP from packaging test classes) eclipse.classpath.defaultOutputDir = file(project.name+"/bin/eclipse") eclipse.classpath.file.beforeMerged { classpath -> classpath.entries.findAll{ it instanceof SourceFolder }.each { - if(it.output.startsWith("bin/")) { + if (it.output.startsWith("bin/")) { it.output = null } } diff --git a/gradle/jdiff.gradle b/gradle/jdiff.gradle deleted file mode 100644 index da454e62ab67..000000000000 --- a/gradle/jdiff.gradle +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Generate a JDiff report between the current version and an older version. - * - * Usage: - * gradle jdiff -D OLD_VERSION=3.1.3.RELEASE -D OLD_VERSION_ROOT=/path/to/3.1.3.RELEASE - * - * View generated report at: - * build/reports/jdiff/changes.html - * - * @param OLD_VERSION required - * @param OLD_VERSION_ROOT required, typically pointing to a separate git clone dir - */ -task jdiff { - description = "Generates a JDiff report" - group = "Documentation" - - def jdiffHome = "${rootProject.rootDir}/gradle/jdiff" - - ant.taskdef( - name: "jdiff", - classname: "jdiff.JDiffAntTask", - classpath: "${jdiffHome}/antjdiff.jar") - - def currentVersion = rootProject.version - def currentVersionRoot = rootProject.rootDir - - def oldVersion = System.getProperty("OLD_VERSION") - def oldVersionRoot = System.getProperty("OLD_VERSION_ROOT") - - def outputDir = "${rootProject.buildDir}/reports/jdiff/${oldVersion}_to_${currentVersion}" - - doLast { - if (oldVersion == null) - throw new IllegalArgumentException( - "Set OLD_VERSION property to indicate older of the two versions being compared") - if (oldVersionRoot == null) - throw new IllegalArgumentException( - "Set OLD_VERSION_ROOT property to indicate the root directory for ${oldVersion}") - - oldVersionRoot = new File(oldVersionRoot) - - ant.property(name: "JDIFF_HOME", value: jdiffHome) - ant.mkdir(dir: outputDir) - ant.jdiff( - destdir: outputDir, - source: "1.8", - verbose: "off", - stats: "on", - docchanges: "off") { - old(name: "Spring Framework ${oldVersion}") { - oldVersionRoot.eachDirMatch( { - def candidate = new File(it) - candidate.name.matches("org.springframework.*") || - candidate.name.matches("spring-.*") }) { match -> - match.eachDirRecurse { subdir -> - if (subdir.path ==~ '.*/src/main/java$') { - dirset(dir: subdir.path, includes: "org/**") - } - } - } - } - "new"(name: "Spring Framework ${currentVersion}") { - currentVersionRoot.eachDirMatch( { - def candidate = new File(it) - candidate.name.matches("org.springframework.*") || - candidate.name.matches("spring-.*") }) { match -> - match.eachDirRecurse { subdir -> - if (subdir.path ==~ '.*/src/main/java$') { - dirset(dir: subdir.path, includes: "org/**") - } - } - } - } - } - } -} diff --git a/gradle/jdiff/LICENSE.txt b/gradle/jdiff/LICENSE.txt deleted file mode 100644 index fab48ede1c40..000000000000 --- a/gradle/jdiff/LICENSE.txt +++ /dev/null @@ -1,506 +0,0 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 2.1, February 1999 - - Copyright (C) 1991, 1999 Free Software Foundation, Inc. - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - -[This is the first released version of the Lesser GPL. It also counts - as the successor of the GNU Library Public License, version 2, hence - the version number 2.1.] - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -Licenses are intended to guarantee your freedom to share and change -free software--to make sure the software is free for all its users. - - This license, the Lesser General Public License, applies to some -specially designated software packages--typically libraries--of the -Free Software Foundation and other authors who decide to use it. You -can use it too, but we suggest you first think carefully about whether -this license or the ordinary General Public License is the better -strategy to use in any particular case, based on the explanations below. - - When we speak of free software, we are referring to freedom of use, -not price. Our General Public Licenses are designed to make sure that -you have the freedom to distribute copies of free software (and charge -for this service if you wish); that you receive source code or can get -it if you want it; that you can change the software and use pieces of -it in new free programs; and that you are informed that you can do -these things. - - To protect your rights, we need to make restrictions that forbid -distributors to deny you these rights or to ask you to surrender these -rights. These restrictions translate to certain responsibilities for -you if you distribute copies of the library or if you modify it. - - For example, if you distribute copies of the library, whether gratis -or for a fee, you must give the recipients all the rights that we gave -you. You must make sure that they, too, receive or can get the source -code. If you link other code with the library, you must provide -complete object files to the recipients, so that they can relink them -with the library after making changes to the library and recompiling -it. And you must show them these terms so they know their rights. - - We protect your rights with a two-step method: (1) we copyright the -library, and (2) we offer you this license, which gives you legal -permission to copy, distribute and/or modify the library. - - To protect each distributor, we want to make it very clear that -there is no warranty for the free library. Also, if the library is -modified by someone else and passed on, the recipients should know -that what they have is not the original version, so that the original -author's reputation will not be affected by problems that might be -introduced by others. - - Finally, software patents pose a constant threat to the existence of -any free program. We wish to make sure that a company cannot -effectively restrict the users of a free program by obtaining a -restrictive license from a patent holder. Therefore, we insist that -any patent license obtained for a version of the library must be -consistent with the full freedom of use specified in this license. - - Most GNU software, including some libraries, is covered by the -ordinary GNU General Public License. This license, the GNU Lesser -General Public License, applies to certain designated libraries, and -is quite different from the ordinary General Public License. We use -this license for certain libraries in order to permit linking those -libraries into non-free programs. - - When a program is linked with a library, whether statically or using -a shared library, the combination of the two is legally speaking a -combined work, a derivative of the original library. The ordinary -General Public License therefore permits such linking only if the -entire combination fits its criteria of freedom. The Lesser General -Public License permits more lax criteria for linking other code with -the library. - - We call this license the "Lesser" General Public License because it -does Less to protect the user's freedom than the ordinary General -Public License. It also provides other free software developers Less -of an advantage over competing non-free programs. These disadvantages -are the reason we use the ordinary General Public License for many -libraries. However, the Lesser license provides advantages in certain -special circumstances. - - For example, on rare occasions, there may be a special need to -encourage the widest possible use of a certain library, so that it becomes -a de-facto standard. To achieve this, non-free programs must be -allowed to use the library. A more frequent case is that a free -library does the same job as widely used non-free libraries. In this -case, there is little to gain by limiting the free library to free -software only, so we use the Lesser General Public License. - - In other cases, permission to use a particular library in non-free -programs enables a greater number of people to use a large body of -free software. For example, permission to use the GNU C Library in -non-free programs enables many more people to use the whole GNU -operating system, as well as its variant, the GNU/Linux operating -system. - - Although the Lesser General Public License is Less protective of the -users' freedom, it does ensure that the user of a program that is -linked with the Library has the freedom and the wherewithal to run -that program using a modified version of the Library. - - The precise terms and conditions for copying, distribution and -modification follow. Pay close attention to the difference between a -"work based on the library" and a "work that uses the library". The -former contains code derived from the library, whereas the latter must -be combined with the library in order to run. - - GNU LESSER GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License Agreement applies to any software library or other -program which contains a notice placed by the copyright holder or -other authorized party saying it may be distributed under the terms of -this Lesser General Public License (also called "this License"). -Each licensee is addressed as "you". - - A "library" means a collection of software functions and/or data -prepared so as to be conveniently linked with application programs -(which use some of those functions and data) to form executables. - - The "Library", below, refers to any such software library or work -which has been distributed under these terms. A "work based on the -Library" means either the Library or any derivative work under -copyright law: that is to say, a work containing the Library or a -portion of it, either verbatim or with modifications and/or translated -straightforwardly into another language. (Hereinafter, translation is -included without limitation in the term "modification".) - - "Source code" for a work means the preferred form of the work for -making modifications to it. For a library, complete source code means -all the source code for all modules it contains, plus any associated -interface definition files, plus the scripts used to control compilation -and installation of the library. - - Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running a program using the Library is not restricted, and output from -such a program is covered only if its contents constitute a work based -on the Library (independent of the use of the Library in a tool for -writing it). Whether that is true depends on what the Library does -and what the program that uses the Library does. - - 1. You may copy and distribute verbatim copies of the Library's -complete source code as you receive it, in any medium, provided that -you conspicuously and appropriately publish on each copy an -appropriate copyright notice and disclaimer of warranty; keep intact -all the notices that refer to this License and to the absence of any -warranty; and distribute a copy of this License along with the -Library. - - You may charge a fee for the physical act of transferring a copy, -and you may at your option offer warranty protection in exchange for a -fee. - - 2. You may modify your copy or copies of the Library or any portion -of it, thus forming a work based on the Library, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) The modified work must itself be a software library. - - b) You must cause the files modified to carry prominent notices - stating that you changed the files and the date of any change. - - c) You must cause the whole of the work to be licensed at no - charge to all third parties under the terms of this License. - - d) If a facility in the modified Library refers to a function or a - table of data to be supplied by an application program that uses - the facility, other than as an argument passed when the facility - is invoked, then you must make a good faith effort to ensure that, - in the event an application does not supply such function or - table, the facility still operates, and performs whatever part of - its purpose remains meaningful. - - (For example, a function in a library to compute square roots has - a purpose that is entirely well-defined independent of the - application. Therefore, Subsection 2d requires that any - application-supplied function or table used by this function must - be optional: if the application does not supply it, the square - root function must still compute square roots.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Library, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Library, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote -it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Library. - -In addition, mere aggregation of another work not based on the Library -with the Library (or with a work based on the Library) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may opt to apply the terms of the ordinary GNU General Public -License instead of this License to a given copy of the Library. To do -this, you must alter all the notices that refer to this License, so -that they refer to the ordinary GNU General Public License, version 2, -instead of to this License. (If a newer version than version 2 of the -ordinary GNU General Public License has appeared, then you can specify -that version instead if you wish.) Do not make any other change in -these notices. - - Once this change is made in a given copy, it is irreversible for -that copy, so the ordinary GNU General Public License applies to all -subsequent copies and derivative works made from that copy. - - This option is useful when you wish to copy part of the code of -the Library into a program that is not a library. - - 4. You may copy and distribute the Library (or a portion or -derivative of it, under Section 2) in object code or executable form -under the terms of Sections 1 and 2 above provided that you accompany -it with the complete corresponding machine-readable source code, which -must be distributed under the terms of Sections 1 and 2 above on a -medium customarily used for software interchange. - - If distribution of object code is made by offering access to copy -from a designated place, then offering equivalent access to copy the -source code from the same place satisfies the requirement to -distribute the source code, even though third parties are not -compelled to copy the source along with the object code. - - 5. A program that contains no derivative of any portion of the -Library, but is designed to work with the Library by being compiled or -linked with it, is called a "work that uses the Library". Such a -work, in isolation, is not a derivative work of the Library, and -therefore falls outside the scope of this License. - - However, linking a "work that uses the Library" with the Library -creates an executable that is a derivative of the Library (because it -contains portions of the Library), rather than a "work that uses the -library". The executable is therefore covered by this License. -Section 6 states terms for distribution of such executables. - - When a "work that uses the Library" uses material from a header file -that is part of the Library, the object code for the work may be a -derivative work of the Library even though the source code is not. -Whether this is true is especially significant if the work can be -linked without the Library, or if the work is itself a library. The -threshold for this to be true is not precisely defined by law. - - If such an object file uses only numerical parameters, data -structure layouts and accessors, and small macros and small inline -functions (ten lines or less in length), then the use of the object -file is unrestricted, regardless of whether it is legally a derivative -work. (Executables containing this object code plus portions of the -Library will still fall under Section 6.) - - Otherwise, if the work is a derivative of the Library, you may -distribute the object code for the work under the terms of Section 6. -Any executables containing that work also fall under Section 6, -whether or not they are linked directly with the Library itself. - - 6. As an exception to the Sections above, you may also combine or -link a "work that uses the Library" with the Library to produce a -work containing portions of the Library, and distribute that work -under terms of your choice, provided that the terms permit -modification of the work for the customer's own use and reverse -engineering for debugging such modifications. - - You must give prominent notice with each copy of the work that the -Library is used in it and that the Library and its use are covered by -this License. You must supply a copy of this License. If the work -during execution displays copyright notices, you must include the -copyright notice for the Library among them, as well as a reference -directing the user to the copy of this License. Also, you must do one -of these things: - - a) Accompany the work with the complete corresponding - machine-readable source code for the Library including whatever - changes were used in the work (which must be distributed under - Sections 1 and 2 above); and, if the work is an executable linked - with the Library, with the complete machine-readable "work that - uses the Library", as object code and/or source code, so that the - user can modify the Library and then relink to produce a modified - executable containing the modified Library. (It is understood - that the user who changes the contents of definitions files in the - Library will not necessarily be able to recompile the application - to use the modified definitions.) - - b) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (1) uses at run time a - copy of the library already present on the user's computer system, - rather than copying library functions into the executable, and (2) - will operate properly with a modified version of the library, if - the user installs one, as long as the modified version is - interface-compatible with the version that the work was made with. - - c) Accompany the work with a written offer, valid for at - least three years, to give the same user the materials - specified in Subsection 6a, above, for a charge no more - than the cost of performing this distribution. - - d) If distribution of the work is made by offering access to copy - from a designated place, offer equivalent access to copy the above - specified materials from the same place. - - e) Verify that the user has already received a copy of these - materials or that you have already sent this user a copy. - - For an executable, the required form of the "work that uses the -Library" must include any data and utility programs needed for -reproducing the executable from it. However, as a special exception, -the materials to be distributed need not include anything that is -normally distributed (in either source or binary form) with the major -components (compiler, kernel, and so on) of the operating system on -which the executable runs, unless that component itself accompanies -the executable. - - It may happen that this requirement contradicts the license -restrictions of other proprietary libraries that do not normally -accompany the operating system. Such a contradiction means you cannot -use both them and the Library together in an executable that you -distribute. - - 7. You may place library facilities that are a work based on the -Library side-by-side in a single library together with other library -facilities not covered by this License, and distribute such a combined -library, provided that the separate distribution of the work based on -the Library and of the other library facilities is otherwise -permitted, and provided that you do these two things: - - a) Accompany the combined library with a copy of the same work - based on the Library, uncombined with any other library - facilities. This must be distributed under the terms of the - Sections above. - - b) Give prominent notice with the combined library of the fact - that part of it is a work based on the Library, and explaining - where to find the accompanying uncombined form of the same work. - - 8. You may not copy, modify, sublicense, link with, or distribute -the Library except as expressly provided under this License. Any -attempt otherwise to copy, modify, sublicense, link with, or -distribute the Library is void, and will automatically terminate your -rights under this License. However, parties who have received copies, -or rights, from you under this License will not have their licenses -terminated so long as such parties remain in full compliance. - - 9. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Library or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Library (or any work based on the -Library), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Library or works based on it. - - 10. Each time you redistribute the Library (or any work based on the -Library), the recipient automatically receives a license from the -original licensor to copy, distribute, link with or modify the Library -subject to these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties with -this License. - - 11. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Library at all. For example, if a patent -license would not permit royalty-free redistribution of the Library by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Library. - -If any portion of this section is held invalid or unenforceable under any -particular circumstance, the balance of the section is intended to apply, -and the section as a whole is intended to apply in other circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 12. If the distribution and/or use of the Library is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Library under this License may add -an explicit geographical distribution limitation excluding those countries, -so that distribution is permitted only in or among countries not thus -excluded. In such case, this License incorporates the limitation as if -written in the body of this License. - - 13. The Free Software Foundation may publish revised and/or new -versions of the Lesser General Public License from time to time. -Such new versions will be similar in spirit to the present version, -but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library -specifies a version number of this License which applies to it and -"any later version", you have the option of following the terms and -conditions either of that version or of any later version published by -the Free Software Foundation. If the Library does not specify a -license version number, you may choose any version ever published by -the Free Software Foundation. - - 14. If you wish to incorporate parts of the Library into other free -programs whose distribution conditions are incompatible with these, -write to the author to ask for permission. For software which is -copyrighted by the Free Software Foundation, write to the Free -Software Foundation; we sometimes make exceptions for this. Our -decision will be guided by the two goals of preserving the free status -of all derivatives of our free software and of promoting the sharing -and reuse of software generally. - - NO WARRANTY - - 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO -WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR -OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY -KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE -LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME -THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN -WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY -AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU -FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR -CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE -LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING -RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A -FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH -DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Libraries - - If you develop a new library, and you want it to be of the greatest -possible use to the public, we recommend making it free software that -everyone can redistribute and change. You can do so by permitting -redistribution under these terms (or, alternatively, under the terms of the -ordinary General Public License). - - To apply these terms, attach the following notices to the library. It is -safest to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least the -"copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -Also add information on how to contact you by electronic and paper mail. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the library, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the - library `Frob' (a library for tweaking knobs) written by James Random Hacker. - - , 1 April 1990 - Ty Coon, President of Vice - -That's all there is to it! - - - - diff --git a/gradle/jdiff/Null.java b/gradle/jdiff/Null.java deleted file mode 100644 index 2d8649e544e9..000000000000 --- a/gradle/jdiff/Null.java +++ /dev/null @@ -1,9 +0,0 @@ -/** - * This class is used only as a "null" argument for Javadoc when comparing - * two API files. Javadoc has to have a package, .java or .class file as an - * argument, even though JDiff doesn't use it. - */ -public class Null { - public Null() { - } -} diff --git a/gradle/jdiff/README-SPRING.txt b/gradle/jdiff/README-SPRING.txt deleted file mode 100644 index 430f2bffec18..000000000000 --- a/gradle/jdiff/README-SPRING.txt +++ /dev/null @@ -1,6 +0,0 @@ -This distribution of JDiff 1.1.1 is included in the Spring Framework build -because JDiff has a hard requirement on a JDIFF_HOME directory containing -jdiff.jar and xerces.jar as well as other presentation resources. - -The actual generation of JDiff reports is driven by the `jdiff` task declared -in build.gradle in the project root. diff --git a/gradle/jdiff/README.txt b/gradle/jdiff/README.txt deleted file mode 100644 index 9f7529e2864c..000000000000 --- a/gradle/jdiff/README.txt +++ /dev/null @@ -1,59 +0,0 @@ - - JDiff Doclet - ------------ - - Matthew Doar - mdoar@pobox.com - - -The JDiff doclet is used to generate a report describing the -difference between two public Java APIs. - -The file jdiff.html contains the reference page for JDiff. The latest -version of JDiff can be downloaded at: -http://sourceforge.net/projects/javadiff - -To use the Ant task on your own project, see example.xml. More examples -of using JDiff to compare the public APIs of J2SE1.3 and J2SE1.4 can -be seen at http://www.jdiff.org - -For an example with the source distribution, run "ant" and -look at the HTML output in ./build/reports/example/changes.html -The page at ./build/reports/example/changes/com.acme.sp.SPImpl.html -shows what a typical page of changes looks like. - -System Requirements -------------------- - -JDiff has been tested with all releases of Java since J2SE1.2 but -releases of JDiff after 1.10.0 focus on JDK1.5. - -License -------- - -JDiff is licensed under the Lesser GNU General Public License (LGPL). -See the file LICENSE.txt. - -Acknowledgements ----------------- - -JDiff uses Stuart D. Gathman's Java translation of Gene Myers' O(ND) -difference algorithm. - -JDiff uses Xerces 1.4.2 from http://www.apache.org. - -JDiff also includes a script to use the classdoc application from -http://classdoc.sourceforge.net or http://www.jensgulden.de, by Jens -Gulden, (mail@jensgulden.de), to call a doclet such as jdiff on a .jar -file rather than on source. - -Many thanks to the reviewers at Sun and Vitria who gave feedback on early -versions of JDiff output, and also to the distillers of Laphroaig, and to -Arturo Fuente for his consistently fine cigars which helped inspire -much of this work. - - -Footnote: - -If you are looking for a generalized diff tool for XML, try diffmk from -http://wwws.sun.com/software/xml/developers/diffmk/ diff --git a/gradle/jdiff/antjdiff.jar b/gradle/jdiff/antjdiff.jar deleted file mode 100644 index 063dbee9600e..000000000000 Binary files a/gradle/jdiff/antjdiff.jar and /dev/null differ diff --git a/gradle/jdiff/background.gif b/gradle/jdiff/background.gif deleted file mode 100644 index e6d2dda38971..000000000000 Binary files a/gradle/jdiff/background.gif and /dev/null differ diff --git a/gradle/jdiff/black.gif b/gradle/jdiff/black.gif deleted file mode 100644 index 185d95b110bd..000000000000 Binary files a/gradle/jdiff/black.gif and /dev/null differ diff --git a/gradle/jdiff/jdiff.html b/gradle/jdiff/jdiff.html deleted file mode 100644 index 5c46f967b9ec..000000000000 --- a/gradle/jdiff/jdiff.html +++ /dev/null @@ -1,1032 +0,0 @@ - - - - -JDiff User Documentation - - - - - - - -
-JDiff Logo SourceForge Logo
- - -

-

JDiff User Documentation


-
- -
-JDiff is a Javadoc doclet which generates an -HTML report of all the packages, classes, constructors, methods, and -fields which have been removed, added or changed in any way, including -their documentation, when two APIs are compared. This is very useful -for describing exactly what has changed between two releases of a -product. Only the API (Application Programming Interface) of each -version is compared. It does not compare what the source code does -when executed. -
- -
-

CONTENTS

- -
- - -

OVERVIEW

- -
-The basic sequence of operations is to run JDiff on one set of source -files to create an XML file which represents the API for that version -of a product. Then JDiff is run again on another set of source files -to create an XML file which represents the API for the next version of -a product. Finally, JDiff compares the two APIs as represented in the -XML files and generates an HTML report which describes the differences -between the two APIs, together with comments describing the reasons -for the differences. This whole process can be executed as separate -Javadoc steps (either from Ant or the command line) or by simply using -the Ant JDiff task provided. -
- -
-The results are written into a file called changes.html, -with more files in a subdirectory called changes. These -files can contain links to existing Javadoc documentation. A CSS -stylesheet is also generated in the file -stylesheet-jdiff.css, and this uses a background image in -background.gif. These are the only files which usually -need to be shipped with a product to include a report of what has -changed since the previous release. If the -stats -option was used, then the file black.gif should also be -shipped. -
- -
-There is a working example of how to use JDiff in the examples -directory of the source distribution. -
- -
- - -

INSTALLATION

- -
-Unpack the jdiff-1.1.1.zip file. This will produce a directory named -"jdiff-1.1.1" containing all that is necessary to use JDiff to produce -your own reports. See the file "example.xml" in that directory for an -example of how to use the Ant JDiff task. The file "jdiff.html" -contains more information about using JDiff. -
- -
-If you are using the complete source distribution, then you should be -able to simply type "ant" at the top-level to produce a working -example report. -
- -
-The Ant JDiff task needs Ant 1.6.1 to work correctly. Using Ant -1.5 will produce the error: -
-Error: two projects are needed, one <old> and one <new>
-
-
- -
-No Windows registry entries are changed by JDiff. To remove JDiff, -simply delete the directory where it is was unpacked. -
- -
- -

SYNOPSIS

- -
-The Ant JDiff task has the following parameters: -
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AttributeDescriptionRequired
destdirThe location where the JDiff report will be generated. Defaults to a directory "jdiff_report" in the directory from where Ant was executed.No
statsGenerate an HTML page of statistical information about the - differences between the two APIs. Defaults to "off".No
docchangesEnables comparison of Javadoc documentation. Defaults to "off".No
verboseIncrease the logging vebosity of the task. Defaults to "off".No
-
- -
-Parameters specified as nested elements -
- -
-The old and new elements are used to describe the projects to be compared. -
- -
- - - - - - - - - - - - - - - - - - -
AttributeDescriptionRequired
nameThe name of the project, e.g. "My Project Version 1". The name, with spaces replaced by underscores, is used as the name of the XML file in destdir, -which is generated by JDiff to represent the structure of the source files of this project.Yes
javadocThe location of a Javadoc report for this project. If this attribute is not used, then a Javadoc report for the project will be generated in a subdirectory named name in destdir.No
- -
- -
-Note: the old and new elements only have DirSet nested elements, not FileSet ones. -
- -
-The complete list parameters that can be passed to the JDiff doclet, -either through the Ant Javadoc task or directly at the command line, -is as follows: -
- -
-javadoc -doclet jdiff.JDiff -docletpath jdiff.jar
- [-apiname <API name>]
- [-apidir <optional directory where the API XML file is to be placed>]
- [-oldapi <name of old API>]
- [-oldapidir <optional directory where the old API XML file is located>]
- [-newapi <name of new API>]
- [-newapidir <optional directory where the new API XML file is located>]
- [-sourcepath <source path>]
- [-javadocnew <javadoc files location for the new API>]
- [-javadocold <javadoc files location for the old API>]
- [-baseURI <base>]
- [-excludeclass <exclusion level>]
- [-excludemember <exclusion level>]
- [-nosuggest <suggestion level>]
- [-firstsentence]
- [-docchanges]
- [-checkcomments]
- [-packagesonly]
- [-showallchanges]
- [-retainnonprinting]
- [-excludetag <exclude tag>]
- [-stats]
- [-windowtitle <text>]
- [-doctitle <HTML text>]
- [-version]
- [-help]
-
- -
-NOTE: Either -apiname, or both -oldapi and --newapi must be used. All other arguments are optional. -
- -
-The -d directory argument works just as with Javadoc, redirecting -the HTML output to the given directory. -
- -

The arguments for the JDiff doclet are:

-
-
- -apiname <API name>
-
- Define the name which will be associated - with the specified API. If the name which is given here has space - characters, they will be - replaced by underscore characters. This name with no spaces is used as the name of the XML - file. It is also written into the XML file as an attribute of the top - element. - E.g. "SuperProduct 1.0" generates an XML file named - "SuperProduct_1.0.xml". - The XML file is always generated in the current directory, unless - overridden by the -apidir argument. -
- -
- -oldapi <name of old API>
-
- The name of the old or previous version of an - API or product, e.g. "SuperProduct 1.0", which is to be one of the - APIs compared. - This name is the name which was given to -apiname when - the XML file was generated. -
- -
- -newapi <name of old API>
-
- The name of the new or later version of an - API or product, e.g. "SuperProduct 2.0", which is to be one of the - APIs compared. - This name is the name which was given to -apiname when - the XML file was generated. -
- -
- -apidir <API directory>
-
- Defines the directory where the API XML file is to be placed. Used in - conjunction with the -apiname argument. -
- -
- -oldapidir <old API directory>
-
- Defines the directory where the XML file for the old API is located. - Used in conjunction with the -oldapi argument. Default is the current - directory. -
- -
- -newapidir <new API directory>
-
- Defines the directory where the XML file for the new API is located. - Used in conjunction with the -newapi argument. Default is the current - directory. -
- -
- -sourcepath <source path>
-
- Define the path to the set of Java source - files (the API) - to be scanned, e.g. examples/SuperProduct1.0. The - slashes in this argument should match the local architecture. -
- -
- -javadocnew <javadoc files location for the new API>
-
- The location of existing Javadoc files - for the new API, e.g. "http://java.sun.com/j2se/1.5.0/docs/api/" for the - public documentation for J2SE1.5.0. The default value is "../", which implies - that the documentation directory generated by Javadoc is at the same level as - the "changes.html" file generated by JDiff. Slashes are always - forward in the argument, since this is an HTML link. The argument - should also always end in a forward slash. If a relative value is - given, it should be relative to files in the "changes" directory - generated by JDiff. -
- -
- -javadocold <javadoc files location for the old API>
-
The location of existing - Javadoc files for the old API, e.g. "http://java.sun.com/j2se/1.5.0/docs/API/" - for the public documentation for J2SE1.5.0. The default value is null, which - results in no links to Javadoc-generated documentation for the previous - release. Slashes are always forward in the argument, since this is an HTML - link. The argument should also always end in a forward slash. If a relative - value is given, it should be relative to files in the "changes" directory - generated by JDiff. -
- -
- - -baseURI <base>
-
- Use \"base\" as the base location of the various DTDs used by - JDiff. For example, -baseURI "file:///C:/jdiff/lib" would cause - the XML parser to use the copies which are shipped in the - lib directory, if JDiff is installed in - C:\jdiff. Note that there are three forward slashes - after "file:". -
- -
- -excludeclass <exclusion level>
-
- This can be set to "public", - "protected", "package", or "private". If it is set to "protected", only - classes which are public or protected will be shown. If it is set to - "public", then only public classes are shown. The default is - "protected". If this is changed, the Javadoc -private - argument must also be passed to Javadoc. -
- -
- -excludemember <exclusion level>
-
- This can be set to "public", - "protected", "package", or "private". If it is set to "protected", only - members (constructors, methods and fields) which are public or protected will be shown. If it is set to - "public", then only public members are shown. The default is - "protected". - If this is changed, the Javadoc -private - argument must also be passed to Javadoc. -
- -
- -firstsentence
-
- This argument causes JDiff to save only the first sentence of each - Javadoc comment as part of - the API. This is only necessary when the XML file representing the - API is being generated. See -docchanges for how to - note documentation changes as differences.
-
- -
- -docchanges
-
- This argument enables comparison of Javadoc documentation. - By default, changes in the saved Javadoc documentation - are not noted as changes (or as removals and related - additions). See -firstsentence option for how to compare just - the first sentence in Javadoc documentation. -
- -
- -nosuggest <suggestion level>
-
- This can be set to "all", "remove", "add", - or "change". The effect of setting this to "all" is to stop comments - for any changes at all being suggested. Any comments which are to - appear in the report must then be written by the user (see below). - If it is set to "change", then - comments will not be suggested for changes, but will be suggested for - removals and additions. The default is that comments are suggested for - all possible places. -
- -
- -checkcomments
-
- If this argument is used, JDiff - will warn the user when the report is generated if there are comments - which do not end in a period, question mark or exclamation mark. -
- -
- -packagesonly
-
- If this argument is used, JDiff - will not scan classes specified on the command line. This should - only need to be used with the "jdiffjar" script, when - comparing Jar files. - If this options is not used when comparing Jar files, duplicate - classes with no packages ("anonymous" classes) may be - wrongly reported by JDiff. -
- -
- -showallchanges
-
- If this argument is used, JDiff will show changes in - native and synchronized modifiers. See here for why these are not shown by default. -
- -
- -retainnonprinting
-
- Retain non-printable characters - in comments. By default, JDiff removes non-printable characters - from comments which it uses. - This should only really be necessary if the first line of a - comment has used explicit Unicode character escape sequences which - cannot be printed, or more importantly for JDiff, read in from XML. - If this option is used, JDiff may fail to read in an XML - file, and exit with an error message about "an invalid XML character (Unicode: - 0x....)" on a certain line in the file. Turning off this option does - make creating large XML files a little faster. -
- -
- -excludetag <exclude tag>
-
- The argument passed in with this causes - JDiff to ignore program elements (packages, classes, constructors, - methods, fields) which contain the given exclude tag in their comment blocks, - e.g. " @exclude", " @docset Internal". The extra space in front of "@" is - to stop Javadoc from expanding the name into a file containing commands on - the compile line. White space is trimmed off before the string is used. - - Another solution to passing "@" as part of an argument is to pass @foo, - and then create a file named foo, containing - -excludetag @exclude. - -
- -
- -stats
-
- Generate an HTML page of statistical information about the - differences between the two APIs. -
- -
- -windowtitle <text>
-
- Specify the title used in the browser window for the report. - By default, this is - "API Differences Between <name of old API> and - <name of new API>". -
- -
- -doctitle <HTML text>
-
- Specify the title used on the first page of the report. - By default, this is - "API Differences Between <name of old API> and - <name of new API>". -
- -
- -version
-
- Display the version of JDiff. -
- -
- -help
-
- Display the usage summary for JDiff. -
-
- -
- -

OUTPUT

- -
-
    -
  • Interfaces appear in italics, just as in Javadoc documentation.
  • -
  • When a package or class appears in bold, it has been added in -the new version or API.
  • -
  • When a package or class appears struck through, it has been removed in -the new version or API.
  • -
  • When a constructor is added, two entries are added to the "All -Differences" index: one for the addition of a new constructor, and -one for the change of the class. The constructor entry has -"constructor" after it.
  • -
  • There are some complex changes which can occur between versions, for example, when two or more methods with the same name change simultaneously, or when a method or field is moved into or from a superclass. -In these cases, the change will be seen as a removal and an addition, rather than as a change. Unexpected removals or additions are often part of one of these type of changes.
  • -
  • With large packages, it is often necessary to change the memory parameters for -Javadoc, e.g. pass in -J-Xmx128m to Javadoc.
  • -
  • The api.xsd template describes the format of the XML -for the APIs, and the comments.xsd template describes the format -of the XML for the comments. The API template is independent of any -related work at Sun, but the intention is to adopt whatever becomes -the de facto standard in the future, whilst retaining backward -compatibility. To enable validation, set the boolean validateXML in -the file XMLToAPI.java and recompile.
  • -
  • Comments in the comments XML file do get reordered during -regeneration. This is harmless.
  • -
-
- -
- -

ADDING COMMENTS TO A REPORT

- -
-Comments can be added to a report by using a text editor to add text -to the "user_comments_X_to_Y.xml" file, where "X" and "Y" are the -names of the two APIs being compared. This file is automatically regenerated -each time the report is generated. -If the -d directory argument is used, the user comments XML -file also appears, and is expected, in the named directory. - -

Comments which become unused are -moved to the end of the file and placed inside XML comments. -

- -
The text which is added can be HTML text if necessary, but if the - HTML is incorrect, JDiff may fail to read the comments file and exit. Note that - the required HTML is in fact XHTML. Since this HTML is stored in an XML document, single tags without their closing ("slash") element are not permitted. - For example, most browsers permit HTML which looks like "<p>Here is some - text.", with no closing tag. XML requires that either a closing tag exists ("</p>"), - or that the single tag is closed, e.g. "<p/>Here is some text.". -HTML p, br and hr tags can be single, due to common usage. -
- -
-To write comments for a particular removal, addition or change in the -JDiff-generated report, edit the comments XML file. Your changes will -be automatically incorporated into a new version of this file when the -report is next generated. Search the file for the identifier for the -particular removal, addition or change, using the package name, class -name and member name to find the identifier. Alternatively, look at -the HTML source of a report and note the identifier (an HTML named anchor) -near the intended place for the comment. -
- -
-Adding links to comments can be accomplished in two ways: with the {@link} Javadoc tag, or by using HTML links directly. - -
    - -
  • -To link to a class, use the package and class name, e.g. {@link -packagename.classname}. -
  • - -
  • -To link to a specific method in a class' HTML page, use the package, -class name, a pound sign, and then the method and parameters, or () -e.g. {@link packagename.classname#methodname(params)}. -
  • - -
  • -To link to a specific constructor in a class' HTML page, use the package, -class name, a pound sign, and then the classname and parameters, or () -e.g. {@link packagename.classname#classname(params)}. -
  • - -
  • -To link to a specific field in a class' HTML page, use the package, -class name, a pound sign, and then the name of the field -e.g. {@link packagename.classname#fieldname}. -
  • - -
- -Alternatively, you can use an explicit HTML -<a> element. e.g. -<a href="packagename.classname.html#methodname">link text<a>. -The specific HTML named anchor can be found by looking at the HTML -source of a report. -
- -
-Sometimes you may want to have the same comment text appear in -multiple places in the report. You can do this by having multiple -<identifier> elements in a single <comment> element. This -grouping does not persist after the comments file is regenerated. -
- -
-The first sentence from a comment in the source code for an element is -available in the comments XML file by using the @first tag. This tag -will be replaced (once) in the comments in the report by the first -sentence from the appropriate Javadoc comment. -
- -


- -

TROUBLESHOOTING

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PROBLEMPOSSIBLE SOLUTION
Error: two projects are needed, one
-<old> and one <new>
The Ant JDiff task needs Ant 1.6.1 to work correctly
You are not connected to the Internet, or are behind a firewallSee the documentation for how to use - the -baseURI - optionThis only applies to generating JDiff output, - not to viewing it.
No changes are seen in the report.By default, Javadoc and JDiff only show public - and protected classes and members.
No changes seen for package and private classes.Enable both the correct Javadoc visibility level - (-public, -protected, -package, -private) and the correct JDiff - visibility level (-excludeclass, -excludemember).
No comments were inserted for packages.You need to use the -sourcepath argument to - locate the source code files, so that - JDiff can deduce where the package.html file with - comments about the package may be. If no package.html - file exists or can be found, then no comments can be suggested - for packages. Of course, comments can still be - added by hand.
JDiff takes a long time to load XML, or throws - java.net.NoRouteToHostException: Operation timed out.The validation portion of loading the XML file - currently requires the ability to make an HTTP connection. Check - your network and try again, or see the -baseURI - option and the next suggestion.
From behind a firewall, - - JDiff fails to load one of the required XML DTD files.Use the following settings to tell the Java2 VM - that you are behind a firewall:
- java -DproxySet=true -DproxyHost=PROXYSERVER -DproxyPort=PORT
- where PROXYSERVER is the hostname or IP address of - your proxy server, and PORT is the port number of the - proxy server.

- The other alternative is to use the local copies of the required - files by using the option -baseURI when generating the API XML - files. For example, -baseURI "file:///C:/jdiff/lib" would cause - the XML parser to use the copies which are shipped in the - lib directory, if JDiff is installed in - C:\jdiff. Note that there are three forward slashes - after "file:". - The -baseURI approach has the advantage that it - requires no connectivity to the Internet to be able to run JDiff. -
JDiff fails to handle assert in J2SE1.4Be sure to use the -source 1.4 argument to - Javadoc to handle assertions present in J2SE1.4 source code. -
Using an XML parser other than XercesSet the org.xml.sax.driver system property to - the name of the XML parser class which you wish to use. Setting a system - property is usually done by passing -Dname=value to the JVM. - To cause Javadoc to pass an argument to the underlying JVM, use - -J-Dname=value. To pass an argument to Javadoc from within - an ANT Javadoc task, use the additionalparam attribute, e.g. - additionalparam="-J-Dorg.xml.sax.driver=com.example.my.driver" -
Comparing Jar files results in duplicate class - changes being reported.Be sure to use the -packagesonly - option when using Jar files as the input to JDiff. You should not - need to use -packagesonly otherwise. -
Documentation difference page becomes all changes - part way through.This problem can occur if incorrect HTML is - written in the new documentation. JDiff shows this HTML on the - documentation difference page, and can cause entries later on in - the page to be displayed incorrectly. - -

One solution is to edit the documentation difference page by - hand, but the better solution is to fix the offending HTML in the - new source code. -

The background color of my HTML report is not correct.Check that the file background.gif from the lib is in the same directory as the changes.html file. -
The names of exceptions are too long in the HTML report.To use short names for exceptions, set the - showExceptionTypes boolean to false in - XMLToAPI.java file and recompile. -
-
- -
- -

ERRORS AND WARNING MESSAGES

- -
-The warnings and error messages which can be generated by JDiff are as -follows: -
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ERROR MESSAGEPOSSIBLE CAUSE
Error: unknown element type.The XML file contains an element tag which the - current version of JDiff cannot recognize. This may occur if an - older version of JDiff is used with XML files generated by a newer - version.
Error: IO Error while attempting to create X.Java was unable to open a file for writing. May - occur if the user does not have write permission for the current - directory.
Error: no identifier found in the comments XML file.The XML file for the comments for the report must - contain an identifier to indicate which report of differing APIs - these comments are written for.
Error: unknown program element type.Internal JDiff error.
Error: could not create the subdirectory X.Java was unable to create a directory. May - occur if the user does not have write or execute permission for the current - directory.
Error: file X does not exist for the [old|new] API.The XML files corresponding to the names given to - -oldapi and -newapi are not in the - current directory. This may be because the XML files have not yet been - generated, or were generated elsewhere.
- It can also occur if the - XML file was generated with one API identifier, and is now being - read in with another identifier. Either use the same identifier, - or change the <api> name element value in the XML file to the new - API identifier.
Error: no API identifier found in the XML file X.The given XML file does not have an identifier in - it, probably due to manual modification.
Error: no packages found in the APIs.JDiff was unable to find any packages in the - arguments given to Javadoc.
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WARNING MESSAGEPOSSIBLE CAUSE
Warning: illegal string found in text. Ignoring the comment.The suggested comments from Javadoc are stored in - XML files in a CDATA element, which permits every string except .
Warning: no difference between the APIs.There was no difference between the APIs. You are - probably comparing two identical XML files.
Warning: no classes found in the package X.A package without classes was encountered.
Warning: change from deprecated to undeprecated for class X.A class changed from being deprecated to being - undeprecated in the next release. This is usually either poor - software design or a misplaced @deprecated Javadoc tag.
Warning: change from deprecated to undeprecated - for a constructor in class X.A constructor changed from being deprecated to being - undeprecated in the next release. This is usually either poor - software design or a misplaced @deprecated Javadoc tag.
Warning: change from deprecated to undeprecated for method X.A method changed from being deprecated to being - undeprecated in the next release. This is usually either poor - software design or a misplaced @deprecated Javadoc tag.
Warning: text of comment does not end in a period.Generated when the -checkcomments is - used. The suggested comment does not end in a period, question mark or exclamation mark.
Warning: N identical ids in the existing comments file. Using the first instance.The comments file contains comment for multiple - places in the report, but N of the identifiers for the comment - are non-unique.
Warning: incorrectly formatted @link in text.JDiff was unable to parse the @link in the - suggested comment.
Warning: comment com.acme.sp is no longer used.The comment in the comments file intended for the - given element is no longer needed, since the element is no longer - part of the changes between the APIs. The comment will be moved to - the end of the comments file and preserved, but not used.
Warning: API identifier in the comments XML file differs from the name of the file.The comments file keeps track of which APIs it is - to be used for, and has detected a mismatch with the names of the - current APIs.
Warning: multiple @deprecated tags found in comments for X. Using the first one only.A comment with more than one @deprecated tag was - encountered in the source code. This is considered poor Javadoc style.
Warning: @ tag seen in comment.An @ tag other than @link has somehow made its - way into a suggested comment. This should not occur, but can be - remedied by editing the comments file to use a different comment.
Warning: duplicate class : X found. Using the first instance only.Multiple instances of the same fully qualified - class name were found in the API XML file. Most likely caused by - manual modification of the file after it was generated.
Warning: missing @since tagA class, constructor, method or field was added - in the later API but no @since tag was found in the Javadoc - comment. This information is logged into a file - missingSinces.txt in the same directory as - changes.html. This file is informational only. The - boolean to control this behaviour is in the source file - HTMLIndexes.java. -
Warning: API identifier in the XML file X differs from the name of the file Y.The name given to -apiname when the XML file - is generated is embedded in the XML file as a top-level attribute. This - warning suggests that the XML file has been modified by hand, but that - report generation should proceed using the new API identifier.
-
- -
- -

DIFFERENCE STATISTICS

-
-During the generation of a report, JDiff also reports a percentage -difference between the two APIs being compared, e.g. "Approximately -10% difference between the APIs". This statistic is calculated in the -following way: - -
 
-Percentage change = 100 * (added + removed + 2*changed)
-                    -----------------------------------
-                    sum of public elements in BOTH APIs
-
- -So if there are 15 packages in the old API, and 2 of these are removed, -and 17 packages in the new API, 1 of which is newly added, and only 3 -of which have changed, then the simple percentage difference would be: - -
-100 * (1 + 2 + 2*3)/ (15 + 17) = 28%
-
- -A change of 100% means that there are no packages in common between -the two APIs. A change of 0% indicates that nothing changed between -the two APIs. This formula is applied recursively in JDiff for classes -and their members. That is, the value for the number of packages -changed is not an integer, but instead is the value obtained by -applying the same formula to the all the classes in the changed -packages, and then to all the members of the changed classes. - This results in a lower, but more accurate, percentage difference. -The percentage difference value does not appear anywhere in the HTML -report files generated by JDiff. -The test suite for JDiff v1.0 had a difference value of approximately 63%. -A real-world value is the value for the differences between J2SE1.2 and -J2SE1.3, which is approximately 8%. -
- -
- -

LIMITATIONS

-
-
    -
  1. While Java is highly backward compatible, so that, for example, - the XML for a -J2SE1.2 application can be generated using JDiff with J2SE1.3, there -are a few cases where classes will appear in the XML of the API which are -not present in the source code. These classes appear to be inserted by - javac or javadoc. An example of this is the class -java.awt.Robot, which is inserted into the XML for -J2SE1.2 if javadoc in J2SE1.3 is used, but not does not appear in - the XML if javadoc in J2SE1.2 is used.
    -To avoid these (rare) cases, it is recommended that you use the same version -of the J2SE that the application was written for.
  2. -
  3. JDiff does not tell you how two Javadoc web pages differ in layout, though -it can tell you how the content has changed. -Nor does it -compare what the methods in an API do; if JDiff could tell you what had changed about the way two -versions of an API execute, the Halting -Problem would be solved, and our lives would be very different.
  4. -
  5. On a P3 450MHz machine, to scan all of the J2SE Java -and javax packages and generate XML takes about 2 minutes -per version. To generate a report from the XML files takes about 30s
  6. -
-
- -
- -

FURTHER READING

- -
- - -
- -
-
-This software comes with absolutely NO WARRANTY. See the LGPL in the file LICENSE.txt for -details. -
- -

- -Copyright © 2001-2007 Matthew B. Doar
-
-

- - - diff --git a/gradle/jdiff/jdiff.jar b/gradle/jdiff/jdiff.jar deleted file mode 100644 index 5fc7ed0209d7..000000000000 Binary files a/gradle/jdiff/jdiff.jar and /dev/null differ diff --git a/gradle/jdiff/jdiff_logo.gif b/gradle/jdiff/jdiff_logo.gif deleted file mode 100644 index d098485d3a49..000000000000 Binary files a/gradle/jdiff/jdiff_logo.gif and /dev/null differ diff --git a/gradle/jdiff/new.gif b/gradle/jdiff/new.gif deleted file mode 100644 index c0ef7a4744c9..000000000000 Binary files a/gradle/jdiff/new.gif and /dev/null differ diff --git a/gradle/jdiff/xerces.jar b/gradle/jdiff/xerces.jar deleted file mode 100644 index e75d486c8d99..000000000000 Binary files a/gradle/jdiff/xerces.jar and /dev/null differ diff --git a/gradle/publications.gradle b/gradle/publications.gradle new file mode 100644 index 000000000000..86e0d2221c0b --- /dev/null +++ b/gradle/publications.gradle @@ -0,0 +1,64 @@ +apply plugin: "maven-publish" + +publishing { + publications { + mavenJava(MavenPublication) { + pom { + afterEvaluate { + name = project.description + description = project.description + } + url = "https://github.com/spring-projects/spring-framework" + organization { + name = "Spring IO" + url = "https://spring.io/projects/spring-framework" + } + licenses { + license { + name = "Apache License, Version 2.0" + url = "https://www.apache.org/licenses/LICENSE-2.0" + distribution = "repo" + } + } + scm { + url = "https://github.com/spring-projects/spring-framework" + connection = "scm:git:git://github.com/spring-projects/spring-framework" + developerConnection = "scm:git:git://github.com/spring-projects/spring-framework" + } + developers { + developer { + id = "jhoeller" + name = "Juergen Hoeller" + email = "jhoeller@pivotal.io" + } + } + issueManagement { + system = "GitHub" + url = "https://github.com/spring-projects/spring-framework/issues" + } + } + versionMapping { + usage('java-api') { + fromResolutionResult() + } + usage('java-runtime') { + fromResolutionResult() + } + } + } + } +} + +configureDeploymentRepository(project) + +void configureDeploymentRepository(Project project) { + project.plugins.withType(MavenPublishPlugin.class).all { + PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class); + if (project.hasProperty("deploymentRepository")) { + publishing.repositories.maven { + it.url = project.property("deploymentRepository") + it.name = "deployment" + } + } + } +} \ No newline at end of file diff --git a/gradle/publish-maven.gradle b/gradle/publish-maven.gradle deleted file mode 100644 index 249d23f4ce9f..000000000000 --- a/gradle/publish-maven.gradle +++ /dev/null @@ -1,60 +0,0 @@ -apply plugin: "propdeps-maven" - -install { - repositories.mavenInstaller { - customizePom(pom, project) - } -} - -def customizePom(pom, gradleProject) { - pom.whenConfigured { generatedPom -> - // eliminate test-scoped dependencies (no need in maven central poms) - generatedPom.dependencies.removeAll { dep -> - dep.scope == "test" - } - - // sort to make pom dependencies order consistent to ease comparison of older poms - generatedPom.dependencies = generatedPom.dependencies.sort { dep -> - "$dep.scope:$dep.groupId:$dep.artifactId" - } - - def managedVersions = dependencyManagement.managedVersions - generatedPom.dependencies.findAll{dep -> !dep.version }.each { dep -> - dep.version = managedVersions["${dep.groupId}:${dep.artifactId}"] - } - - // add all items necessary for maven central publication - generatedPom.project { - name = gradleProject.description - description = gradleProject.description - url = "https://github.com/spring-projects/spring-framework" - organization { - name = "Spring IO" - url = "http://projects.spring.io/spring-framework" - } - licenses { - license { - name "Apache License, Version 2.0" - url "http://www.apache.org/licenses/LICENSE-2.0" - distribution "repo" - } - } - scm { - url = "https://github.com/spring-projects/spring-framework" - connection = "scm:git:git://github.com/spring-projects/spring-framework" - developerConnection = "scm:git:git://github.com/spring-projects/spring-framework" - } - developers { - developer { - id = "jhoeller" - name = "Juergen Hoeller" - email = "jhoeller@pivotal.io" - } - } - issueManagement { - system = "Jira" - url = "https://jira.springsource.org/browse/SPR" - } - } - } -} diff --git a/gradle/spring-module.gradle b/gradle/spring-module.gradle new file mode 100644 index 000000000000..048735af1cbb --- /dev/null +++ b/gradle/spring-module.gradle @@ -0,0 +1,71 @@ +apply plugin: 'org.springframework.build.compile' +apply plugin: 'org.springframework.build.optional-dependencies' +apply from: "$rootDir/gradle/publications.gradle" + +jar { + manifest.attributes["Implementation-Title"] = project.name + manifest.attributes["Implementation-Version"] = project.version + manifest.attributes["Automatic-Module-Name"] = project.name.replace('-', '.') // for Jigsaw + manifest.attributes["Created-By"] = + "${System.getProperty("java.version")} (${System.getProperty("java.specification.vendor")})" + + from("${rootDir}/src/docs/dist") { + include "license.txt" + include "notice.txt" + into "META-INF" + expand(copyright: new Date().format("yyyy"), version: project.version) + } +} + +normalization { + runtimeClasspath { + ignore "META-INF/MANIFEST.MF" + } +} + +javadoc { + description = "Generates project-level javadoc for use in -javadoc jar" + + options.encoding = "UTF-8" + options.memberLevel = JavadocMemberLevel.PROTECTED + options.author = true + options.header = project.name + options.use = true + options.links(project.ext.javadocLinks) + options.addStringOption("Xdoclint:none", "-quiet") + + // Suppress warnings due to cross-module @see and @link references. + // Note that global 'api' task does display all warnings. + logging.captureStandardError LogLevel.INFO + logging.captureStandardOutput LogLevel.INFO // suppress "## warnings" message +} + +task sourcesJar(type: Jar, dependsOn: classes) { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + archiveClassifier.set("sources") + from sourceSets.main.allSource + // Don't include or exclude anything explicitly by default. See SPR-12085. +} + +task javadocJar(type: Jar) { + archiveClassifier.set("javadoc") + from javadoc +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + artifact sourcesJar + artifact javadocJar + } + } +} + +// Disable publication of test fixture artifacts. +// +// Once we upgrade to Gradle 6.x, we will need to delete the following line ... +components.java.variants.removeAll { it.outgoingConfiguration.name.startsWith("testFixtures") } +// ... and uncomment the following two lines. +// components.java.withVariantsFromConfiguration(configurations.testFixturesApiElements) { skip() } +// components.java.withVariantsFromConfiguration(configurations.testFixturesRuntimeElements) { skip() } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 135367700529..5c2d1cf016b3 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e0b3fb8d70b1..5028f28f8e47 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index e1f51d4d9f37..83f2acfdc319 100755 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed 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 +# +# https://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. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,8 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -GRADLE_OPTS="-XX:MaxMetaspaceSize=1024m -Xmx1024m $GRADLE_OPTS" -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -110,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` diff --git a/gradlew.bat b/gradlew.bat index 0106f16e1e4f..24467a141f79 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -14,8 +30,7 @@ set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set GRADLE_OPTS=-XX:MaxMetaspaceSize=1024m -Xmx1024m -XX:MaxHeapSize=256m %GRADLE_OPTS% -set DEFAULT_JVM_OPTS= +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/import-into-eclipse.md b/import-into-eclipse.md index 1423db80e028..bb665d788a13 100644 --- a/import-into-eclipse.md +++ b/import-into-eclipse.md @@ -1,46 +1,49 @@ # Spring Framework - Eclipse/STS Project Import Guide This document will guide you through the process of importing the Spring Framework -projects into Eclipse or the Spring Tool Suite (STS). It is recommended that you have a -recent version of Eclipse or STS. As a bare minimum you will need Eclipse with full Java -8 support, the AspectJ Development Tools (AJDT), and the Groovy Compiler. - -The following instructions have been tested against -[Spring Tool Suite](https://spring.io/tools) (_STS_) 3.9.4 and 4.0.0.M11 with -[Eclipse Buildship](http://projects.eclipse.org/projects/tools.buildship) (Eclipse -Plug-ins for Gradle). The instructions should work with the latest Eclipse distribution -as long as you install +projects into Eclipse or the Spring Tool Suite (_STS_). It is recommended that you +have a recent version of Eclipse. As a bare minimum you will need Eclipse with full Java +8 support, Eclipse Buildship, the Kotlin plugin, and the Groovy plugin. + +The following instructions have been tested against [STS](https://spring.io/tools) 4.3.2 +([download](https://github.com/spring-projects/sts4/wiki/Previous-Versions#spring-tools-432-changelog)) +(based on Eclipse 4.12) with [Eclipse Buildship](https://projects.eclipse.org/projects/tools.buildship). +The instructions should work with the latest Eclipse distribution as long as you install [Buildship](https://marketplace.eclipse.org/content/buildship-gradle-integration). Note that STS 4 comes with Buildship preinstalled. ## Steps -_Within your locally cloned `spring-framework` working directory:_ - -1. Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava` -2. Import into Eclipse (File -> Import -> Gradle -> Existing Gradle Project -> Navigate - to directory -> Select Finish) -3. If prompted, exclude the `spring-aspects` module (or after the import by closing or - deleting the project) -4. In the `spring-oxm` project, add the two folders (`castor` and `jaxb`) in - `build/generated-sources` to the build path (right click on them and select - `Build Path -> Use as Source Folder`) -5. To apply project specific settings run `./gradlew eclipseBuildship` -7. Code away +_When instructed to execute `./gradlew` from the command line, be sure to execute it within your locally cloned `spring-framework` working directory._ + +1. Ensure that Eclipse launches with JDK 8. + - For example, on Mac OS this can be configured in the `Info.plist` file located in the `Contents` folder of the installed Eclipse or STS application (e.g., the `Eclipse.app` file). +1. Install the [Kotlin Plugin for Eclipse](https://marketplace.eclipse.org/content/kotlin-plugin-eclipse) in Eclipse. +1. Install the [Eclipse Groovy Development Tools](https://github.com/groovy/groovy-eclipse/wiki) in Eclipse. +1. Switch to Groovy 2.5 (Preferences -> Groovy -> Compiler -> Switch to 2.5...) in Eclipse. +1. Change the _Forbidden reference (access rule)_ in Eclipse from Error to Warning +(Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule)). +1. Optionally install the [AspectJ Development Tools](https://marketplace.eclipse.org/content/aspectj-development-tools) (_AJDT_) if you need to work with the `spring-aspects` project. The AspectJ Development Tools available in the Eclipse Marketplace have been tested with these instructions using STS 4.5 (Eclipse 4.14). +1. Optionally install the [TestNG plugin](https://testng.org/doc/eclipse.html) in Eclipse if you need to execute TestNG tests in the `spring-test` module. +1. Build `spring-oxm` from the command line with `./gradlew :spring-oxm:check`. +1. To apply project specific settings, run `./gradlew eclipseBuildship` from the command line. +1. Import into Eclipse (File -> Import -> Gradle -> Existing Gradle Project -> Navigate to the locally cloned `spring-framework` directory -> Select Finish). + - If you have not installed AJDT, exclude the `spring-aspects` project from the import, if prompted, or close it after the import. + - If you run into errors during the import, you may need to set the _Java home_ for Gradle Buildship to the location of your JDK 8 installation in Eclipse (Preferences -> Gradle -> Java home). +1. If you need to execute JAXB-related tests in the `spring-oxm` project and wish to have the generated sources available, add the `build/generated-sources/jaxb` folder to the build path (right click on the `jaxb` folder and select `Build Path -> Use as Source Folder`). + - If you do not see the `build` folder in the `spring-oxm` project, ensure that the "Gradle build folder" is not filtered out from the view. This setting is available under "Filters" in the configuration of the Package Explorer (available by clicking on the small downward facing arrow in the upper right corner of the Package Explorer). +1. Code away! ## Known Issues 1. `spring-core` and `spring-oxm` should be pre-compiled due to repackaged dependencies. - - See `*RepackJar` tasks in the build. -2. `spring-aspects` does not compile due to references to aspect types unknown to Eclipse. - - If you install [AJDT](https://www.eclipse.org/ajdt/downloads/) into Eclipse it should - work. -3. While JUnit tests pass from the command line with Gradle, some may fail when run from + - See `*RepackJar` tasks in the build. +1. `spring-aspects` does not compile due to references to aspect types unknown to Eclipse. + - If you installed _AJDT_ into Eclipse it should work. +1. While JUnit tests pass from the command line with Gradle, some may fail when run from the IDE. - - Resolving this is a work in progress. - - If attempting to run all JUnit tests from within the IDE, you will likely need to set - the following VM options to avoid out of memory errors: - `-XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m` + - Resolving this is a work in progress. + - If attempting to run all JUnit tests from within the IDE, you may need to set the following VM options to avoid out of memory errors: `-XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m` ## Tips diff --git a/import-into-idea.md b/import-into-idea.md index 942b71f5d32b..1d555305c024 100644 --- a/import-into-idea.md +++ b/import-into-idea.md @@ -14,7 +14,7 @@ _Within your locally cloned spring-framework working directory:_ 1. `spring-core` and `spring-oxm` should be pre-compiled due to repackaged dependencies. See `*RepackJar` tasks in the build and https://youtrack.jetbrains.com/issue/IDEA-160605). 2. `spring-aspects` does not compile due to references to aspect types unknown to -IntelliJ IDEA. See http://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the +IntelliJ IDEA. See https://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the 'spring-aspects' can be excluded from the project to avoid compilation errors. 3. While JUnit tests pass from the command line with Gradle, some may fail when run from IntelliJ IDEA. Resolving this is a work in progress. If attempting to run all JUnit tests from within @@ -31,6 +31,6 @@ You'll notice these files are already intentionally in .gitignore. The same poli ## FAQ -Q. What about IntelliJ IDEA's own [Gradle support](http://confluence.jetbrains.net/display/IDEADEV/Gradle+integration)? +Q. What about IntelliJ IDEA's own [Gradle support](https://confluence.jetbrains.net/display/IDEADEV/Gradle+integration)? -A. Keep an eye on http://youtrack.jetbrains.com/issue/IDEA-53476 +A. Keep an eye on https://youtrack.jetbrains.com/issue/IDEA-53476 diff --git a/integration-tests/integration-tests.gradle b/integration-tests/integration-tests.gradle new file mode 100644 index 000000000000..71e23b906049 --- /dev/null +++ b/integration-tests/integration-tests.gradle @@ -0,0 +1,30 @@ +description = "Spring Integration Tests" + +dependencies { + testCompile(project(":spring-aop")) + testCompile(project(":spring-beans")) + testCompile(project(":spring-context")) + testCompile(project(":spring-core")) + testCompile(testFixtures(project(":spring-aop"))) + testCompile(testFixtures(project(":spring-beans"))) + testCompile(testFixtures(project(":spring-core"))) + testCompile(testFixtures(project(":spring-tx"))) + testCompile(project(":spring-expression")) + testCompile(project(":spring-jdbc")) + testCompile(project(":spring-orm")) + testCompile(project(":spring-test")) + testCompile(project(":spring-tx")) + testCompile(project(":spring-web")) + testCompile("javax.inject:javax.inject") + testCompile("javax.resource:javax.resource-api") + testCompile("javax.servlet:javax.servlet-api") + testCompile("org.aspectj:aspectjweaver") + testCompile("org.hsqldb:hsqldb") + testCompile("org.hibernate:hibernate-core") +} + +normalization { + runtimeClasspath { + ignore "META-INF/MANIFEST.MF" + } +} diff --git a/integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests.java b/integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests.java new file mode 100644 index 000000000000..da32ff120181 --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2002-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.aop.config; + +import java.util.ArrayList; +import java.util.List; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Integration tests for advice invocation order for advice configured via the + * AOP namespace. + * + * @author Sam Brannen + * @since 5.2.7 + * @see org.springframework.aop.framework.autoproxy.AspectJAutoProxyAdviceOrderIntegrationTests + */ +class AopNamespaceHandlerAdviceOrderIntegrationTests { + + @Nested + @SpringJUnitConfig(locations = "AopNamespaceHandlerAdviceOrderIntegrationTests-afterFirst.xml") + @DirtiesContext + class AfterAdviceFirstTests { + + @Test + void afterAdviceIsInvokedFirst(@Autowired Echo echo, @Autowired InvocationTrackingAspect aspect) throws Exception { + assertThat(aspect.invocations).isEmpty(); + assertThat(echo.echo(42)).isEqualTo(42); + assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after returning"); + + aspect.invocations.clear(); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> echo.echo(new Exception())); + assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after throwing"); + } + } + + @Nested + @SpringJUnitConfig(locations = "AopNamespaceHandlerAdviceOrderIntegrationTests-afterLast.xml") + @DirtiesContext + class AfterAdviceLastTests { + + @Test + void afterAdviceIsInvokedLast(@Autowired Echo echo, @Autowired InvocationTrackingAspect aspect) throws Exception { + assertThat(aspect.invocations).isEmpty(); + assertThat(echo.echo(42)).isEqualTo(42); + assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after returning", "after"); + + aspect.invocations.clear(); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> echo.echo(new Exception())); + assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after throwing", "after"); + } + } + + + static class Echo { + + Object echo(Object obj) throws Exception { + if (obj instanceof Exception) { + throw (Exception) obj; + } + return obj; + } + } + + static class InvocationTrackingAspect { + + List invocations = new ArrayList<>(); + + Object around(ProceedingJoinPoint joinPoint) throws Throwable { + invocations.add("around - start"); + try { + return joinPoint.proceed(); + } + finally { + invocations.add("around - end"); + } + } + + void before() { + invocations.add("before"); + } + + void afterReturning() { + invocations.add("after returning"); + } + + void afterThrowing() { + invocations.add("after throwing"); + } + + void after() { + invocations.add("after"); + } + } + +} diff --git a/integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests.java b/integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests.java new file mode 100644 index 000000000000..32ac556fe482 --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests.java @@ -0,0 +1,137 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.aop.config; + +import org.junit.jupiter.api.Test; + +import org.springframework.aop.framework.Advised; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for scoped proxy use in conjunction with aop: namespace. + * Deemed an integration test because .web mocks and application contexts are required. + * + * @author Rob Harrop + * @author Juergen Hoeller + * @author Chris Beams + * @see org.springframework.aop.config.AopNamespaceHandlerTests + */ +@SpringJUnitWebConfig +class AopNamespaceHandlerScopeIntegrationTests { + + @Autowired + ITestBean singletonScoped; + + @Autowired + ITestBean requestScoped; + + @Autowired + ITestBean sessionScoped; + + @Autowired + ITestBean sessionScopedAlias; + + @Autowired + ITestBean testBean; + + + @Test + void testSingletonScoping() throws Exception { + assertThat(AopUtils.isAopProxy(singletonScoped)).as("Should be AOP proxy").isTrue(); + boolean condition = singletonScoped instanceof TestBean; + assertThat(condition).as("Should be target class proxy").isTrue(); + String rob = "Rob Harrop"; + String bram = "Bram Smeets"; + assertThat(singletonScoped.getName()).isEqualTo(rob); + singletonScoped.setName(bram); + assertThat(singletonScoped.getName()).isEqualTo(bram); + ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(singletonScoped); + assertThat(deserialized.getName()).isEqualTo(bram); + } + + @Test + void testRequestScoping() throws Exception { + MockHttpServletRequest oldRequest = new MockHttpServletRequest(); + MockHttpServletRequest newRequest = new MockHttpServletRequest(); + + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest)); + + assertThat(AopUtils.isAopProxy(requestScoped)).as("Should be AOP proxy").isTrue(); + boolean condition = requestScoped instanceof TestBean; + assertThat(condition).as("Should be target class proxy").isTrue(); + + assertThat(AopUtils.isAopProxy(testBean)).as("Should be AOP proxy").isTrue(); + boolean condition1 = testBean instanceof TestBean; + assertThat(condition1).as("Regular bean should be JDK proxy").isFalse(); + + String rob = "Rob Harrop"; + String bram = "Bram Smeets"; + + assertThat(requestScoped.getName()).isEqualTo(rob); + requestScoped.setName(bram); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(newRequest)); + assertThat(requestScoped.getName()).isEqualTo(rob); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest)); + assertThat(requestScoped.getName()).isEqualTo(bram); + + assertThat(((Advised) requestScoped).getAdvisors().length > 0).as("Should have advisors").isTrue(); + } + + @Test + void testSessionScoping() throws Exception { + MockHttpSession oldSession = new MockHttpSession(); + MockHttpSession newSession = new MockHttpSession(); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setSession(oldSession); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + assertThat(AopUtils.isAopProxy(sessionScoped)).as("Should be AOP proxy").isTrue(); + boolean condition1 = sessionScoped instanceof TestBean; + assertThat(condition1).as("Should not be target class proxy").isFalse(); + + assertThat(sessionScopedAlias).isSameAs(sessionScoped); + + assertThat(AopUtils.isAopProxy(testBean)).as("Should be AOP proxy").isTrue(); + boolean condition = testBean instanceof TestBean; + assertThat(condition).as("Regular bean should be JDK proxy").isFalse(); + + String rob = "Rob Harrop"; + String bram = "Bram Smeets"; + + assertThat(sessionScoped.getName()).isEqualTo(rob); + sessionScoped.setName(bram); + request.setSession(newSession); + assertThat(sessionScoped.getName()).isEqualTo(rob); + request.setSession(oldSession); + assertThat(sessionScoped.getName()).isEqualTo(bram); + + assertThat(((Advised) sessionScoped).getAdvisors().length > 0).as("Should have advisors").isTrue(); + } + +} diff --git a/src/test/java/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests.java b/integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests.java similarity index 72% rename from src/test/java/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests.java index 305f8e8be102..cf067e01415c 100644 --- a/src/test/java/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,30 +16,29 @@ package org.springframework.aop.framework.autoproxy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.IOException; import java.lang.reflect.Method; import java.util.List; import javax.servlet.ServletException; -import org.junit.Test; +import org.junit.jupiter.api.Test; + import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; +import org.springframework.aop.testfixture.advice.CountingBeforeAdvice; +import org.springframework.aop.testfixture.advice.MethodCounter; +import org.springframework.aop.testfixture.interceptor.NopInterceptor; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.testfixture.beans.ITestBean; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.lang.Nullable; -import org.springframework.tests.aop.advice.CountingBeforeAdvice; -import org.springframework.tests.aop.advice.MethodCounter; -import org.springframework.tests.aop.interceptor.NopInterceptor; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.transaction.CallCountingTransactionManager; import org.springframework.transaction.NoTransactionException; import org.springframework.transaction.interceptor.TransactionInterceptor; +import org.springframework.transaction.testfixture.CallCountingTransactionManager; + +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests for auto proxy creation by advisor recognition working in @@ -50,7 +49,7 @@ * @author Rod Johnson * @author Chris Beams */ -public class AdvisorAutoProxyCreatorIntegrationTests { +class AdvisorAutoProxyCreatorIntegrationTests { private static final Class CLASS = AdvisorAutoProxyCreatorIntegrationTests.class; private static final String CLASSNAME = CLASS.getSimpleName(); @@ -68,95 +67,95 @@ protected BeanFactory getBeanFactory() throws IOException { } @Test - public void testDefaultExclusionPrefix() throws Exception { + void testDefaultExclusionPrefix() throws Exception { DefaultAdvisorAutoProxyCreator aapc = (DefaultAdvisorAutoProxyCreator) getBeanFactory().getBean(ADVISOR_APC_BEAN_NAME); - assertEquals(ADVISOR_APC_BEAN_NAME + DefaultAdvisorAutoProxyCreator.SEPARATOR, aapc.getAdvisorBeanNamePrefix()); - assertFalse(aapc.isUsePrefix()); + assertThat(aapc.getAdvisorBeanNamePrefix()).isEqualTo((ADVISOR_APC_BEAN_NAME + DefaultAdvisorAutoProxyCreator.SEPARATOR)); + assertThat(aapc.isUsePrefix()).isFalse(); } /** * If no pointcuts match (no attrs) there should be proxying. */ @Test - public void testNoProxy() throws Exception { + void testNoProxy() throws Exception { BeanFactory bf = getBeanFactory(); Object o = bf.getBean("noSetters"); - assertFalse(AopUtils.isAopProxy(o)); + assertThat(AopUtils.isAopProxy(o)).isFalse(); } @Test - public void testTxIsProxied() throws Exception { + void testTxIsProxied() throws Exception { BeanFactory bf = getBeanFactory(); ITestBean test = (ITestBean) bf.getBean("test"); - assertTrue(AopUtils.isAopProxy(test)); + assertThat(AopUtils.isAopProxy(test)).isTrue(); } @Test - public void testRegexpApplied() throws Exception { + void testRegexpApplied() throws Exception { BeanFactory bf = getBeanFactory(); ITestBean test = (ITestBean) bf.getBean("test"); MethodCounter counter = (MethodCounter) bf.getBean("countingAdvice"); - assertEquals(0, counter.getCalls()); + assertThat(counter.getCalls()).isEqualTo(0); test.getName(); - assertEquals(1, counter.getCalls()); + assertThat(counter.getCalls()).isEqualTo(1); } @Test - public void testTransactionAttributeOnMethod() throws Exception { + void testTransactionAttributeOnMethod() throws Exception { BeanFactory bf = getBeanFactory(); ITestBean test = (ITestBean) bf.getBean("test"); CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME); OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction"); - assertEquals(0, txc.getCountingBeforeAdvice().getCalls()); + assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(0); - assertEquals(0, txMan.commits); - assertEquals("Initial value was correct", 4, test.getAge()); + assertThat(txMan.commits).isEqualTo(0); + assertThat(test.getAge()).as("Initial value was correct").isEqualTo(4); int newAge = 5; test.setAge(newAge); - assertEquals(1, txc.getCountingBeforeAdvice().getCalls()); + assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(1); - assertEquals("New value set correctly", newAge, test.getAge()); - assertEquals("Transaction counts match", 1, txMan.commits); + assertThat(test.getAge()).as("New value set correctly").isEqualTo(newAge); + assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1); } /** * Should not roll back on servlet exception. */ @Test - public void testRollbackRulesOnMethodCauseRollback() throws Exception { + void testRollbackRulesOnMethodCauseRollback() throws Exception { BeanFactory bf = getBeanFactory(); Rollback rb = (Rollback) bf.getBean("rollback"); CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME); OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction"); - assertEquals(0, txc.getCountingBeforeAdvice().getCalls()); + assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(0); - assertEquals(0, txMan.commits); + assertThat(txMan.commits).isEqualTo(0); rb.echoException(null); // Fires only on setters - assertEquals(0, txc.getCountingBeforeAdvice().getCalls()); - assertEquals("Transaction counts match", 1, txMan.commits); + assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(0); + assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1); - assertEquals(0, txMan.rollbacks); + assertThat(txMan.rollbacks).isEqualTo(0); Exception ex = new Exception(); try { rb.echoException(ex); } catch (Exception actual) { - assertEquals(ex, actual); + assertThat(actual).isEqualTo(ex); } - assertEquals("Transaction counts match", 1, txMan.rollbacks); + assertThat(txMan.rollbacks).as("Transaction counts match").isEqualTo(1); } @Test - public void testRollbackRulesOnMethodPreventRollback() throws Exception { + void testRollbackRulesOnMethodPreventRollback() throws Exception { BeanFactory bf = getBeanFactory(); Rollback rb = (Rollback) bf.getBean("rollback"); CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME); - assertEquals(0, txMan.commits); + assertThat(txMan.commits).isEqualTo(0); // Should NOT roll back on ServletException try { rb.echoException(new ServletException()); @@ -164,25 +163,26 @@ public void testRollbackRulesOnMethodPreventRollback() throws Exception { catch (ServletException ex) { } - assertEquals("Transaction counts match", 1, txMan.commits); + assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1); } @Test - public void testProgrammaticRollback() throws Exception { + void testProgrammaticRollback() throws Exception { BeanFactory bf = getBeanFactory(); Object bean = bf.getBean(TXMANAGER_BEAN_NAME); - assertTrue(bean instanceof CallCountingTransactionManager); + boolean condition = bean instanceof CallCountingTransactionManager; + assertThat(condition).isTrue(); CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME); Rollback rb = (Rollback) bf.getBean("rollback"); - assertEquals(0, txMan.commits); + assertThat(txMan.commits).isEqualTo(0); rb.rollbackOnly(false); - assertEquals("Transaction counts match", 1, txMan.commits); - assertEquals(0, txMan.rollbacks); + assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1); + assertThat(txMan.rollbacks).isEqualTo(0); // Will cause rollback only rb.rollbackOnly(true); - assertEquals(1, txMan.rollbacks); + assertThat(txMan.rollbacks).isEqualTo(1); } } @@ -290,7 +290,6 @@ class Rollback { /** * Inherits transaction attribute. * Illustrates programmatic rollback. - * @param rollbackOnly */ public void rollbackOnly(boolean rollbackOnly) { if (rollbackOnly) { @@ -311,8 +310,9 @@ protected void setRollbackOnly() { * @org.springframework.transaction.interceptor.NoRollbackRule ( "ServletException" ) */ public void echoException(Exception ex) throws Exception { - if (ex != null) + if (ex != null) { throw ex; + } } } diff --git a/integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AspectJAutoProxyAdviceOrderIntegrationTests.java b/integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AspectJAutoProxyAdviceOrderIntegrationTests.java new file mode 100644 index 000000000000..78d45922be0c --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AspectJAutoProxyAdviceOrderIntegrationTests.java @@ -0,0 +1,233 @@ +/* + * Copyright 2002-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.aop.framework.autoproxy; + +import java.util.ArrayList; +import java.util.List; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Integration tests for advice invocation order for advice configured via + * AspectJ auto-proxy support. + * + * @author Sam Brannen + * @since 5.2.7 + * @see org.springframework.aop.config.AopNamespaceHandlerAdviceOrderIntegrationTests + */ +class AspectJAutoProxyAdviceOrderIntegrationTests { + + /** + * {@link After @After} advice declared as first after method in source code. + */ + @Nested + @SpringJUnitConfig(AfterAdviceFirstConfig.class) + @DirtiesContext + class AfterAdviceFirstTests { + + @Test + void afterAdviceIsInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceFirstAspect aspect) throws Exception { + assertThat(aspect.invocations).isEmpty(); + assertThat(echo.echo(42)).isEqualTo(42); + assertThat(aspect.invocations).containsExactly("around - start", "before", "after returning", "after", "around - end"); + + aspect.invocations.clear(); + assertThatExceptionOfType(Exception.class).isThrownBy( + () -> echo.echo(new Exception())); + assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end"); + } + } + + + /** + * This test class uses {@link AfterAdviceLastAspect} which declares its + * {@link After @After} advice as the last after advice type method + * in its source code. + * + *

On Java versions prior to JDK 7, we would have expected the {@code @After} + * advice method to be invoked before {@code @AfterThrowing} and + * {@code @AfterReturning} advice methods due to the AspectJ precedence + * rules implemented in + * {@link org.springframework.aop.aspectj.autoproxy.AspectJPrecedenceComparator}. + */ + @Nested + @SpringJUnitConfig(AfterAdviceLastConfig.class) + @DirtiesContext + class AfterAdviceLastTests { + + @Test + void afterAdviceIsInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceLastAspect aspect) throws Exception { + assertThat(aspect.invocations).isEmpty(); + assertThat(echo.echo(42)).isEqualTo(42); + assertThat(aspect.invocations).containsExactly("around - start", "before", "after returning", "after", "around - end"); + + aspect.invocations.clear(); + assertThatExceptionOfType(Exception.class).isThrownBy( + () -> echo.echo(new Exception())); + assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end"); + } + } + + + @Configuration + @EnableAspectJAutoProxy(proxyTargetClass = true) + static class AfterAdviceFirstConfig { + + @Bean + AfterAdviceFirstAspect echoAspect() { + return new AfterAdviceFirstAspect(); + } + + @Bean + Echo echo() { + return new Echo(); + } + } + + @Configuration + @EnableAspectJAutoProxy(proxyTargetClass = true) + static class AfterAdviceLastConfig { + + @Bean + AfterAdviceLastAspect echoAspect() { + return new AfterAdviceLastAspect(); + } + + @Bean + Echo echo() { + return new Echo(); + } + } + + static class Echo { + + Object echo(Object obj) throws Exception { + if (obj instanceof Exception) { + throw (Exception) obj; + } + return obj; + } + } + + /** + * {@link After @After} advice declared as first after method in source code. + */ + @Aspect + static class AfterAdviceFirstAspect { + + List invocations = new ArrayList<>(); + + @Pointcut("execution(* echo(*))") + void echo() { + } + + @After("echo()") + void after() { + invocations.add("after"); + } + + @AfterReturning("echo()") + void afterReturning() { + invocations.add("after returning"); + } + + @AfterThrowing("echo()") + void afterThrowing() { + invocations.add("after throwing"); + } + + @Before("echo()") + void before() { + invocations.add("before"); + } + + @Around("echo()") + Object around(ProceedingJoinPoint joinPoint) throws Throwable { + invocations.add("around - start"); + try { + return joinPoint.proceed(); + } + finally { + invocations.add("around - end"); + } + } + } + + /** + * {@link After @After} advice declared as last after method in source code. + */ + @Aspect + static class AfterAdviceLastAspect { + + List invocations = new ArrayList<>(); + + @Pointcut("execution(* echo(*))") + void echo() { + } + + @Around("echo()") + Object around(ProceedingJoinPoint joinPoint) throws Throwable { + invocations.add("around - start"); + try { + return joinPoint.proceed(); + } + finally { + invocations.add("around - end"); + } + } + + @Before("echo()") + void before() { + invocations.add("before"); + } + + @AfterReturning("echo()") + void afterReturning() { + invocations.add("after returning"); + } + + @AfterThrowing("echo()") + void afterThrowing() { + invocations.add("after throwing"); + } + + @After("echo()") + void after() { + invocations.add("after"); + } + } + +} diff --git a/src/test/java/com/foo/Component.java b/integration-tests/src/test/java/org/springframework/beans/factory/xml/Component.java similarity index 86% rename from src/test/java/com/foo/Component.java rename to integration-tests/src/test/java/org/springframework/beans/factory/xml/Component.java index e9670901c6fb..aeb34d25c0fe 100644 --- a/src/test/java/com/foo/Component.java +++ b/integration-tests/src/test/java/org/springframework/beans/factory/xml/Component.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,12 +14,13 @@ * limitations under the License. */ -package com.foo; +package org.springframework.beans.factory.xml; import java.util.ArrayList; import java.util.List; public class Component { + private String name; private List components = new ArrayList<>(); @@ -39,4 +40,5 @@ public String getName() { public void setName(String name) { this.name = name; } + } diff --git a/src/test/java/com/foo/ComponentBeanDefinitionParser.java b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentBeanDefinitionParser.java similarity index 89% rename from src/test/java/com/foo/ComponentBeanDefinitionParser.java rename to integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentBeanDefinitionParser.java index c02eb8f6287f..833c856adfb8 100644 --- a/src/test/java/com/foo/ComponentBeanDefinitionParser.java +++ b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentBeanDefinitionParser.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.foo; +package org.springframework.beans.factory.xml; import java.util.List; @@ -24,8 +24,6 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.ManagedList; -import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; import org.springframework.util.CollectionUtils; import org.springframework.util.xml.DomUtils; diff --git a/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentBeanDefinitionParserTests.java b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentBeanDefinitionParserTests.java new file mode 100644 index 000000000000..fbc3fec5b4b7 --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentBeanDefinitionParserTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.beans.factory.xml; + +import java.util.List; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.core.io.ClassPathResource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Costin Leau + */ +@TestInstance(Lifecycle.PER_CLASS) +class ComponentBeanDefinitionParserTests { + + private final DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + + + @BeforeAll + void setUp() throws Exception { + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + new ClassPathResource("component-config.xml", ComponentBeanDefinitionParserTests.class)); + } + + @AfterAll + void tearDown() { + bf.destroySingletons(); + } + + @Test + void testBionicBasic() { + Component cp = getBionicFamily(); + assertThat("Bionic-1").isEqualTo(cp.getName()); + } + + @Test + void testBionicFirstLevelChildren() { + Component cp = getBionicFamily(); + List components = cp.getComponents(); + assertThat(2).isEqualTo(components.size()); + assertThat("Mother-1").isEqualTo(components.get(0).getName()); + assertThat("Rock-1").isEqualTo(components.get(1).getName()); + } + + @Test + void testBionicSecondLevelChildren() { + Component cp = getBionicFamily(); + List components = cp.getComponents().get(0).getComponents(); + assertThat(2).isEqualTo(components.size()); + assertThat("Karate-1").isEqualTo(components.get(0).getName()); + assertThat("Sport-1").isEqualTo(components.get(1).getName()); + } + + private Component getBionicFamily() { + return bf.getBean("bionic-family", Component.class); + } + +} + diff --git a/src/test/java/com/foo/ComponentFactoryBean.java b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentFactoryBean.java similarity index 88% rename from src/test/java/com/foo/ComponentFactoryBean.java rename to integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentFactoryBean.java index 2b126d4f4e51..ec2479cd196f 100644 --- a/src/test/java/com/foo/ComponentFactoryBean.java +++ b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentFactoryBean.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,13 +14,14 @@ * limitations under the License. */ -package com.foo; +package org.springframework.beans.factory.xml; import java.util.List; import org.springframework.beans.factory.FactoryBean; public class ComponentFactoryBean implements FactoryBean { + private Component parent; private List children; @@ -51,4 +52,5 @@ public Class getObjectType() { public boolean isSingleton() { return true; } + } diff --git a/src/test/java/com/foo/ComponentNamespaceHandler.java b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentNamespaceHandler.java similarity index 69% rename from src/test/java/com/foo/ComponentNamespaceHandler.java rename to integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentNamespaceHandler.java index b1a830c83b70..abd9867c0c4e 100644 --- a/src/test/java/com/foo/ComponentNamespaceHandler.java +++ b/integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentNamespaceHandler.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,14 +14,12 @@ * limitations under the License. */ -package com.foo; - -import org.springframework.beans.factory.xml.NamespaceHandlerSupport; +package org.springframework.beans.factory.xml; public class ComponentNamespaceHandler extends NamespaceHandlerSupport { + @Override public void init() { - registerBeanDefinitionParser("component", - new ComponentBeanDefinitionParser()); + registerBeanDefinitionParser("component", new ComponentBeanDefinitionParser()); } } diff --git a/src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java b/integration-tests/src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java similarity index 76% rename from src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java index d23b77d757ab..645482958a94 100644 --- a/src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,7 +19,7 @@ import java.util.Collections; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.Advisor; import org.springframework.aop.framework.Advised; @@ -33,8 +33,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Repository; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Integration tests for the @EnableCaching annotation. @@ -43,47 +43,45 @@ * @since 3.1 */ @SuppressWarnings("resource") -public class EnableCachingIntegrationTests { +class EnableCachingIntegrationTests { @Test - public void repositoryIsClassBasedCacheProxy() { + void repositoryIsClassBasedCacheProxy() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, ProxyTargetClassCachingConfig.class); ctx.refresh(); assertCacheProxying(ctx); - assertThat(AopUtils.isCglibProxy(ctx.getBean(FooRepository.class)), is(true)); + assertThat(AopUtils.isCglibProxy(ctx.getBean(FooRepository.class))).isTrue(); } @Test - public void repositoryUsesAspectJAdviceMode() { + void repositoryUsesAspectJAdviceMode() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, AspectJCacheConfig.class); - try { - ctx.refresh(); - } - catch (Exception ex) { - // this test is a bit fragile, but gets the job done, proving that an - // attempt was made to look up the AJ aspect. It's due to classpath issues - // in .integration-tests that it's not found. - assertTrue(ex.getMessage().contains("AspectJCachingConfiguration")); - } + // this test is a bit fragile, but gets the job done, proving that an + // attempt was made to look up the AJ aspect. It's due to classpath issues + // in .integration-tests that it's not found. + assertThatExceptionOfType(Exception.class).isThrownBy( + ctx::refresh) + .withMessageContaining("AspectJCachingConfiguration"); } private void assertCacheProxying(AnnotationConfigApplicationContext ctx) { FooRepository repo = ctx.getBean(FooRepository.class); + assertThat(isCacheProxy(repo)).isTrue(); + } - boolean isCacheProxy = false; + private boolean isCacheProxy(FooRepository repo) { if (AopUtils.isAopProxy(repo)) { for (Advisor advisor : ((Advised)repo).getAdvisors()) { if (advisor instanceof BeanFactoryCacheOperationSourceAdvisor) { - isCacheProxy = true; - break; + return true; } } } - assertTrue("FooRepository is not a cache proxy", isCacheProxy); + return false; } diff --git a/src/test/java/org/springframework/context/annotation/jsr330/ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java b/integration-tests/src/test/java/org/springframework/context/annotation/jsr330/ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java similarity index 73% rename from src/test/java/org/springframework/context/annotation/jsr330/ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/context/annotation/jsr330/ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java index 75ed0a63b0c1..bd86cc9041d1 100644 --- a/src/test/java/org/springframework/context/annotation/jsr330/ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/context/annotation/jsr330/ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,13 +20,13 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; + import javax.inject.Named; import javax.inject.Singleton; -import org.junit.After; -import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; @@ -42,12 +42,14 @@ import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.support.GenericWebApplicationContext; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Mark Fisher * @author Juergen Hoeller * @author Chris Beams */ -public class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests { +class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests { private static final String DEFAULT_NAME = "default"; @@ -62,8 +64,8 @@ public class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests { private ServletRequestAttributes newRequestAttributesWithSession; - @Before - public void setUp() { + @BeforeEach + void setup() { this.oldRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest()); this.newRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest()); @@ -76,223 +78,228 @@ public void setUp() { this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession); } - @After - public void tearDown() throws Exception { + @AfterEach + void reset() { RequestContextHolder.setRequestAttributes(null); } @Test - public void testPrototype() { + void testPrototype() { ApplicationContext context = createContext(ScopedProxyMode.NO); ScopedTestBean bean = (ScopedTestBean) context.getBean("prototype"); - assertNotNull(bean); - assertTrue(context.isPrototype("prototype")); - assertFalse(context.isSingleton("prototype")); + assertThat(bean).isNotNull(); + assertThat(context.isPrototype("prototype")).isTrue(); + assertThat(context.isSingleton("prototype")).isFalse(); } @Test - public void testSingletonScopeWithNoProxy() { + void testSingletonScopeWithNoProxy() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(ScopedProxyMode.NO); ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton"); - assertTrue(context.isSingleton("singleton")); - assertFalse(context.isPrototype("singleton")); + assertThat(context.isSingleton("singleton")).isTrue(); + assertThat(context.isPrototype("singleton")).isFalse(); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // singleton bean, so name should be modified even after lookup ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void testSingletonScopeIgnoresProxyInterfaces() { + void testSingletonScopeIgnoresProxyInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(ScopedProxyMode.INTERFACES); ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // singleton bean, so name should be modified even after lookup ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void testSingletonScopeIgnoresProxyTargetClass() { + void testSingletonScopeIgnoresProxyTargetClass() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS); ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // singleton bean, so name should be modified even after lookup ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void testRequestScopeWithNoProxy() { + void testRequestScopeWithNoProxy() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(ScopedProxyMode.NO); ScopedTestBean bean = (ScopedTestBean) context.getBean("request"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // but a newly retrieved bean should have the default name ScopedTestBean bean2 = (ScopedTestBean) context.getBean("request"); - assertEquals(DEFAULT_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(DEFAULT_NAME); } @Test - public void testRequestScopeWithProxiedInterfaces() { + void testRequestScopeWithProxiedInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(ScopedProxyMode.INTERFACES); IScopedTestBean bean = (IScopedTestBean) context.getBean("request"); // should be dynamic proxy, implementing both interfaces - assertTrue(AopUtils.isJdkDynamicProxy(bean)); - assertTrue(bean instanceof AnotherScopeTestInterface); + assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue(); + boolean condition = bean instanceof AnotherScopeTestInterface; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributes); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void testRequestScopeWithProxiedTargetClass() { + void testRequestScopeWithProxiedTargetClass() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS); IScopedTestBean bean = (IScopedTestBean) context.getBean("request"); // should be a class-based proxy - assertTrue(AopUtils.isCglibProxy(bean)); - assertTrue(bean instanceof RequestScopedTestBean); + assertThat(AopUtils.isCglibProxy(bean)).isTrue(); + boolean condition = bean instanceof RequestScopedTestBean; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributes); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void testSessionScopeWithNoProxy() { + void testSessionScopeWithNoProxy() { RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); ApplicationContext context = createContext(ScopedProxyMode.NO); ScopedTestBean bean = (ScopedTestBean) context.getBean("session"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // but a newly retrieved bean should have the default name ScopedTestBean bean2 = (ScopedTestBean) context.getBean("session"); - assertEquals(DEFAULT_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(DEFAULT_NAME); } @Test - public void testSessionScopeWithProxiedInterfaces() { + void testSessionScopeWithProxiedInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); ApplicationContext context = createContext(ScopedProxyMode.INTERFACES); IScopedTestBean bean = (IScopedTestBean) context.getBean("session"); // should be dynamic proxy, implementing both interfaces - assertTrue(AopUtils.isJdkDynamicProxy(bean)); - assertTrue(bean instanceof AnotherScopeTestInterface); + assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue(); + boolean condition = bean instanceof AnotherScopeTestInterface; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); bean2.setName(DEFAULT_NAME); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void testSessionScopeWithProxiedTargetClass() { + void testSessionScopeWithProxiedTargetClass() { RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS); IScopedTestBean bean = (IScopedTestBean) context.getBean("session"); // should be a class-based proxy - assertTrue(AopUtils.isCglibProxy(bean)); - assertTrue(bean instanceof ScopedTestBean); - assertTrue(bean instanceof SessionScopedTestBean); + assertThat(AopUtils.isCglibProxy(bean)).isTrue(); + boolean condition1 = bean instanceof ScopedTestBean; + assertThat(condition1).isTrue(); + boolean condition = bean instanceof SessionScopedTestBean; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); bean2.setName(DEFAULT_NAME); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @@ -335,7 +342,7 @@ else if (type.startsWith("javax.inject")) { } - public static interface IScopedTestBean { + public interface IScopedTestBean { String getName(); @@ -366,7 +373,7 @@ public static class SingletonScopedTestBean extends ScopedTestBean { } - public static interface AnotherScopeTestInterface { + public interface AnotherScopeTestInterface { } @@ -385,14 +392,14 @@ public static class SessionScopedTestBean extends ScopedTestBean implements Anot @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @javax.inject.Scope - public static @interface RequestScoped { + public @interface RequestScoped { } @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @javax.inject.Scope - public static @interface SessionScoped { + public @interface SessionScoped { } } diff --git a/src/test/java/org/springframework/context/annotation/scope/ClassPathBeanDefinitionScannerScopeIntegrationTests.java b/integration-tests/src/test/java/org/springframework/context/annotation/scope/ClassPathBeanDefinitionScannerScopeIntegrationTests.java similarity index 71% rename from src/test/java/org/springframework/context/annotation/scope/ClassPathBeanDefinitionScannerScopeIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/context/annotation/scope/ClassPathBeanDefinitionScannerScopeIntegrationTests.java index 469e2402cf12..c97840f61fb2 100644 --- a/src/test/java/org/springframework/context/annotation/scope/ClassPathBeanDefinitionScannerScopeIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/context/annotation/scope/ClassPathBeanDefinitionScannerScopeIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,9 @@ package org.springframework.context.annotation.scope; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.support.AopUtils; import org.springframework.context.ApplicationContext; @@ -33,9 +33,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.support.GenericWebApplicationContext; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.context.annotation.ScopedProxyMode.DEFAULT; import static org.springframework.context.annotation.ScopedProxyMode.INTERFACES; import static org.springframework.context.annotation.ScopedProxyMode.NO; @@ -47,7 +45,7 @@ * @author Chris Beams * @author Sam Brannen */ -public class ClassPathBeanDefinitionScannerScopeIntegrationTests { +class ClassPathBeanDefinitionScannerScopeIntegrationTests { private static final String DEFAULT_NAME = "default"; private static final String MODIFIED_NAME = "modified"; @@ -59,8 +57,8 @@ public class ClassPathBeanDefinitionScannerScopeIntegrationTests { private ServletRequestAttributes newRequestAttributesWithSession; - @Before - public void setUp() { + @BeforeEach + void setup() { MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest(); oldRequestWithSession.setSession(new MockHttpSession()); this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession); @@ -70,212 +68,217 @@ public void setUp() { this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession); } - @After - public void tearDown() throws Exception { + @AfterEach + void reset() { RequestContextHolder.resetRequestAttributes(); } @Test - public void singletonScopeWithNoProxy() { + void singletonScopeWithNoProxy() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(NO); ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // singleton bean, so name should be modified even after lookup ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void singletonScopeIgnoresProxyInterfaces() { + void singletonScopeIgnoresProxyInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(INTERFACES); ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // singleton bean, so name should be modified even after lookup ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void singletonScopeIgnoresProxyTargetClass() { + void singletonScopeIgnoresProxyTargetClass() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(TARGET_CLASS); ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // singleton bean, so name should be modified even after lookup ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void requestScopeWithNoProxy() { + void requestScopeWithNoProxy() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(NO); ScopedTestBean bean = (ScopedTestBean) context.getBean("request"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // but a newly retrieved bean should have the default name ScopedTestBean bean2 = (ScopedTestBean) context.getBean("request"); - assertEquals(DEFAULT_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(DEFAULT_NAME); } @Test - public void requestScopeWithProxiedInterfaces() { + void requestScopeWithProxiedInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(INTERFACES); IScopedTestBean bean = (IScopedTestBean) context.getBean("request"); // should be dynamic proxy, implementing both interfaces - assertTrue(AopUtils.isJdkDynamicProxy(bean)); - assertTrue(bean instanceof AnotherScopeTestInterface); + assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue(); + boolean condition = bean instanceof AnotherScopeTestInterface; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributes); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void requestScopeWithProxiedTargetClass() { + void requestScopeWithProxiedTargetClass() { RequestContextHolder.setRequestAttributes(oldRequestAttributes); ApplicationContext context = createContext(TARGET_CLASS); IScopedTestBean bean = (IScopedTestBean) context.getBean("request"); // should be a class-based proxy - assertTrue(AopUtils.isCglibProxy(bean)); - assertTrue(bean instanceof RequestScopedTestBean); + assertThat(AopUtils.isCglibProxy(bean)).isTrue(); + boolean condition = bean instanceof RequestScopedTestBean; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributes); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributes); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void sessionScopeWithNoProxy() { + void sessionScopeWithNoProxy() { RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); ApplicationContext context = createContext(NO); ScopedTestBean bean = (ScopedTestBean) context.getBean("session"); // should not be a proxy - assertFalse(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isFalse(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession); // not a proxy so this should not have changed - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); // but a newly retrieved bean should have the default name ScopedTestBean bean2 = (ScopedTestBean) context.getBean("session"); - assertEquals(DEFAULT_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(DEFAULT_NAME); } @Test - public void sessionScopeWithProxiedInterfaces() { + void sessionScopeWithProxiedInterfaces() { RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); ApplicationContext context = createContext(INTERFACES); IScopedTestBean bean = (IScopedTestBean) context.getBean("session"); // should be dynamic proxy, implementing both interfaces - assertTrue(AopUtils.isJdkDynamicProxy(bean)); - assertTrue(bean instanceof AnotherScopeTestInterface); + assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue(); + boolean condition = bean instanceof AnotherScopeTestInterface; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); bean2.setName(DEFAULT_NAME); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @Test - public void sessionScopeWithProxiedTargetClass() { + void sessionScopeWithProxiedTargetClass() { RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); ApplicationContext context = createContext(TARGET_CLASS); IScopedTestBean bean = (IScopedTestBean) context.getBean("session"); // should be a class-based proxy - assertTrue(AopUtils.isCglibProxy(bean)); - assertTrue(bean instanceof ScopedTestBean); - assertTrue(bean instanceof SessionScopedTestBean); + assertThat(AopUtils.isCglibProxy(bean)).isTrue(); + boolean condition1 = bean instanceof ScopedTestBean; + assertThat(condition1).isTrue(); + boolean condition = bean instanceof SessionScopedTestBean; + assertThat(condition).isTrue(); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession); // this is a proxy so it should be reset to default - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); bean.setName(MODIFIED_NAME); IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session"); - assertEquals(MODIFIED_NAME, bean2.getName()); + assertThat(bean2.getName()).isEqualTo(MODIFIED_NAME); bean2.setName(DEFAULT_NAME); - assertEquals(DEFAULT_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(DEFAULT_NAME); RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession); - assertEquals(MODIFIED_NAME, bean.getName()); + assertThat(bean.getName()).isEqualTo(MODIFIED_NAME); } @@ -295,7 +298,7 @@ private ApplicationContext createContext(ScopedProxyMode scopedProxyMode) { } - static interface IScopedTestBean { + interface IScopedTestBean { String getName(); @@ -320,7 +323,7 @@ static class SingletonScopedTestBean extends ScopedTestBean { } - static interface AnotherScopeTestInterface { + interface AnotherScopeTestInterface { } diff --git a/src/test/java/org/springframework/core/env/EnvironmentSystemIntegrationTests.java b/integration-tests/src/test/java/org/springframework/core/env/EnvironmentSystemIntegrationTests.java similarity index 70% rename from src/test/java/org/springframework/core/env/EnvironmentSystemIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/core/env/EnvironmentSystemIntegrationTests.java index 1d4ad4ec807c..20e23ecca310 100644 --- a/src/test/java/org/springframework/core/env/EnvironmentSystemIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/core/env/EnvironmentSystemIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,8 +19,8 @@ import java.io.File; import java.io.IOException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -57,11 +57,19 @@ import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; -import static org.springframework.context.ConfigurableApplicationContext.*; -import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; +import static org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DERIVED_DEV_BEAN_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DERIVED_DEV_ENV_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DEV_BEAN_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DEV_ENV_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.ENVIRONMENT_AWARE_BEAN_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.PROD_BEAN_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.PROD_ENV_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.TRANSITIVE_BEAN_NAME; +import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.XML_PATH; /** * System integration tests for container support of the {@link Environment} API. @@ -91,15 +99,15 @@ public class EnvironmentSystemIntegrationTests { private final ConfigurableEnvironment prodWebEnv = new StandardServletEnvironment(); - @Before - public void setUp() { + @BeforeEach + void setUp() { prodEnv.setActiveProfiles(PROD_ENV_NAME); devEnv.setActiveProfiles(DEV_ENV_NAME); prodWebEnv.setActiveProfiles(PROD_ENV_NAME); } @Test - public void genericApplicationContext_standardEnv() { + void genericApplicationContext_standardEnv() { ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); ctx.refresh(); @@ -109,7 +117,7 @@ public void genericApplicationContext_standardEnv() { } @Test - public void genericApplicationContext_customEnv() { + void genericApplicationContext_customEnv() { GenericApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); ctx.setEnvironment(prodEnv); ctx.refresh(); @@ -120,51 +128,51 @@ public void genericApplicationContext_customEnv() { } @Test - public void xmlBeanDefinitionReader_inheritsEnvironmentFromEnvironmentCapableBDR() { + void xmlBeanDefinitionReader_inheritsEnvironmentFromEnvironmentCapableBDR() { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.setEnvironment(prodEnv); new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(XML_PATH); ctx.refresh(); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void annotatedBeanDefinitionReader_inheritsEnvironmentFromEnvironmentCapableBDR() { + void annotatedBeanDefinitionReader_inheritsEnvironmentFromEnvironmentCapableBDR() { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.setEnvironment(prodEnv); new AnnotatedBeanDefinitionReader(ctx).register(Config.class); ctx.refresh(); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedConfigClasses() { + void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedConfigClasses() { // it's actually ConfigurationClassPostProcessor's Environment that gets the job done here. GenericApplicationContext ctx = new GenericApplicationContext(); ctx.setEnvironment(prodEnv); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx); scanner.scan("org.springframework.core.env.scan1"); ctx.refresh(); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedComponents() { + void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedComponents() { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.setEnvironment(prodEnv); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx); scanner.scan("org.springframework.core.env.scan2"); ctx.refresh(); - assertThat(scanner.getEnvironment(), is((Environment)ctx.getEnvironment())); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(scanner.getEnvironment()).isEqualTo(ctx.getEnvironment()); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void genericXmlApplicationContext() { + void genericXmlApplicationContext() { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); assertHasStandardEnvironment(ctx); ctx.setEnvironment(prodEnv); @@ -174,12 +182,12 @@ public void genericXmlApplicationContext() { assertHasEnvironment(ctx, prodEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodEnv); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void classPathXmlApplicationContext() { + void classPathXmlApplicationContext() { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH); ctx.setEnvironment(prodEnv); ctx.refresh(); @@ -187,12 +195,12 @@ public void classPathXmlApplicationContext() { assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void fileSystemXmlApplicationContext() throws IOException { + void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); @@ -205,12 +213,12 @@ public void fileSystemXmlApplicationContext() throws IOException { assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void annotationConfigApplicationContext_withPojos() { + void annotationConfigApplicationContext_withPojos() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); assertHasStandardEnvironment(ctx); @@ -223,7 +231,7 @@ public void annotationConfigApplicationContext_withPojos() { } @Test - public void annotationConfigApplicationContext_withProdEnvAndProdConfigClass() { + void annotationConfigApplicationContext_withProdEnvAndProdConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); assertHasStandardEnvironment(ctx); @@ -232,11 +240,11 @@ public void annotationConfigApplicationContext_withProdEnvAndProdConfigClass() { ctx.register(ProdConfig.class); ctx.refresh(); - assertThat("should have prod bean", ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void annotationConfigApplicationContext_withProdEnvAndDevConfigClass() { + void annotationConfigApplicationContext_withProdEnvAndDevConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); assertHasStandardEnvironment(ctx); @@ -245,12 +253,12 @@ public void annotationConfigApplicationContext_withProdEnvAndDevConfigClass() { ctx.register(DevConfig.class); ctx.refresh(); - assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(TRANSITIVE_BEAN_NAME)).isFalse(); } @Test - public void annotationConfigApplicationContext_withDevEnvAndDevConfigClass() { + void annotationConfigApplicationContext_withDevEnvAndDevConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); assertHasStandardEnvironment(ctx); @@ -259,12 +267,12 @@ public void annotationConfigApplicationContext_withDevEnvAndDevConfigClass() { ctx.register(DevConfig.class); ctx.refresh(); - assertThat("should have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(true)); - assertThat("should have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isTrue(); + assertThat(ctx.containsBean(TRANSITIVE_BEAN_NAME)).isTrue(); } @Test - public void annotationConfigApplicationContext_withImportedConfigClasses() { + void annotationConfigApplicationContext_withImportedConfigClasses() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); assertHasStandardEnvironment(ctx); @@ -274,13 +282,13 @@ public void annotationConfigApplicationContext_withImportedConfigClasses() { ctx.refresh(); assertEnvironmentAwareInvoked(ctx, prodEnv); - assertThat("should have prod bean", ctx.containsBean(PROD_BEAN_NAME), is(true)); - assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false)); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(TRANSITIVE_BEAN_NAME)).isFalse(); } @Test - public void mostSpecificDerivedClassDrivesEnvironment_withDerivedDevEnvAndDerivedDevConfigClass() { + void mostSpecificDerivedClassDrivesEnvironment_withDerivedDevEnvAndDerivedDevConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); StandardEnvironment derivedDevEnv = new StandardEnvironment(); derivedDevEnv.setActiveProfiles(DERIVED_DEV_ENV_NAME); @@ -288,40 +296,40 @@ public void mostSpecificDerivedClassDrivesEnvironment_withDerivedDevEnvAndDerive ctx.register(DerivedDevConfig.class); ctx.refresh(); - assertThat("should have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(true)); - assertThat("should have derived dev bean", ctx.containsBean(DERIVED_DEV_BEAN_NAME), is(true)); - assertThat("should have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isTrue(); + assertThat(ctx.containsBean(DERIVED_DEV_BEAN_NAME)).isTrue(); + assertThat(ctx.containsBean(TRANSITIVE_BEAN_NAME)).isTrue(); } @Test - public void mostSpecificDerivedClassDrivesEnvironment_withDevEnvAndDerivedDevConfigClass() { + void mostSpecificDerivedClassDrivesEnvironment_withDevEnvAndDerivedDevConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.setEnvironment(devEnv); ctx.register(DerivedDevConfig.class); ctx.refresh(); - assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat("should not have derived dev bean", ctx.containsBean(DERIVED_DEV_BEAN_NAME), is(false)); - assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(DERIVED_DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(TRANSITIVE_BEAN_NAME)).isFalse(); } @Test - public void annotationConfigApplicationContext_withProfileExpressionMatchOr() { + void annotationConfigApplicationContext_withProfileExpressionMatchOr() { testProfileExpression(true, "p3"); } @Test - public void annotationConfigApplicationContext_withProfileExpressionMatchAnd() { + void annotationConfigApplicationContext_withProfileExpressionMatchAnd() { testProfileExpression(true, "p1", "p2"); } @Test - public void annotationConfigApplicationContext_withProfileExpressionNoMatchAnd() { + void annotationConfigApplicationContext_withProfileExpressionNoMatchAnd() { testProfileExpression(false, "p1"); } @Test - public void annotationConfigApplicationContext_withProfileExpressionNoMatchNone() { + void annotationConfigApplicationContext_withProfileExpressionNoMatchNone() { testProfileExpression(false, "p4"); } @@ -332,12 +340,11 @@ private void testProfileExpression(boolean expected, String... activeProfiles) { ctx.setEnvironment(environment); ctx.register(ProfileExpressionConfig.class); ctx.refresh(); - assertThat("wrong presence of expression bean", - ctx.containsBean("expressionBean"), is(expected)); + assertThat(ctx.containsBean("expressionBean")).isEqualTo(expected); } @Test - public void webApplicationContext() { + void webApplicationContext() { GenericWebApplicationContext ctx = new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); assertHasStandardServletEnvironment(ctx); ctx.setEnvironment(prodWebEnv); @@ -349,7 +356,7 @@ public void webApplicationContext() { } @Test - public void xmlWebApplicationContext() { + void xmlWebApplicationContext() { AbstractRefreshableWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("classpath:" + XML_PATH); ctx.setEnvironment(prodWebEnv); @@ -358,12 +365,12 @@ public void xmlWebApplicationContext() { assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); - assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); - assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse(); + assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue(); } @Test - public void staticApplicationContext() { + void staticApplicationContext() { StaticApplicationContext ctx = new StaticApplicationContext(); assertHasStandardEnvironment(ctx); @@ -379,7 +386,7 @@ public void staticApplicationContext() { } @Test - public void staticWebApplicationContext() { + void staticWebApplicationContext() { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); assertHasStandardServletEnvironment(ctx); @@ -395,7 +402,7 @@ public void staticWebApplicationContext() { } @Test - public void annotationConfigWebApplicationContext() { + void annotationConfigWebApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setEnvironment(prodWebEnv); ctx.setConfigLocation(EnvironmentAwareBean.class.getName()); @@ -407,7 +414,7 @@ public void annotationConfigWebApplicationContext() { } @Test - public void registerServletParamPropertySources_AbstractRefreshableWebApplicationContext() { + void registerServletParamPropertySources_AbstractRefreshableWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); @@ -422,23 +429,23 @@ public void registerServletParamPropertySources_AbstractRefreshableWebApplicatio ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); - assertThat(environment, instanceOf(StandardServletEnvironment.class)); + assertThat(environment).isInstanceOf(StandardServletEnvironment.class); MutablePropertySources propertySources = environment.getPropertySources(); - assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); - assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true)); + assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)).isTrue(); + assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)).isTrue(); // ServletConfig gets precedence - assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue")); - assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), - lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)))); + assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonConfigValue"); + assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME))) + .isLessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))); // but all params are available - assertThat(environment.getProperty("pContext1"), is("pContext1Value")); - assertThat(environment.getProperty("pConfig1"), is("pConfig1Value")); + assertThat(environment.getProperty("pContext1")).isEqualTo("pContext1Value"); + assertThat(environment.getProperty("pConfig1")).isEqualTo("pConfig1Value"); // Servlet* PropertySources have precedence over System* PropertySources - assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), - lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); + assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME))) + .isLessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); @@ -447,12 +454,12 @@ public void registerServletParamPropertySources_AbstractRefreshableWebApplicatio propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletconfig params resolve with higher precedence than sysprops - assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue")); - assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); + assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonConfigValue"); + assertThat(environment.getProperty("pSysProps1")).isEqualTo("pSysProps1Value"); } @Test - public void registerServletParamPropertySources_GenericWebApplicationContext() { + void registerServletParamPropertySources_GenericWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); @@ -462,17 +469,17 @@ public void registerServletParamPropertySources_GenericWebApplicationContext() { ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); - assertThat(environment, instanceOf(StandardServletEnvironment.class)); + assertThat(environment).isInstanceOf(StandardServletEnvironment.class); MutablePropertySources propertySources = environment.getPropertySources(); - assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); + assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)).isTrue(); // ServletContext params are available - assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); - assertThat(environment.getProperty("pContext1"), is("pContext1Value")); + assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonContextValue"); + assertThat(environment.getProperty("pContext1")).isEqualTo("pContext1Value"); // Servlet* PropertySources have precedence over System* PropertySources - assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), - lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); + assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))) + .isLessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); @@ -481,12 +488,12 @@ public void registerServletParamPropertySources_GenericWebApplicationContext() { propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletcontext init params resolve with higher precedence than sysprops - assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); - assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); + assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonContextValue"); + assertThat(environment.getProperty("pSysProps1")).isEqualTo("pSysProps1Value"); } @Test - public void registerServletParamPropertySources_StaticWebApplicationContext() { + void registerServletParamPropertySources_StaticWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); @@ -501,21 +508,21 @@ public void registerServletParamPropertySources_StaticWebApplicationContext() { ConfigurableEnvironment environment = ctx.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); - assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); - assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true)); + assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)).isTrue(); + assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)).isTrue(); // ServletConfig gets precedence - assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue")); - assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), - lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)))); + assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonConfigValue"); + assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME))) + .isLessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))); // but all params are available - assertThat(environment.getProperty("pContext1"), is("pContext1Value")); - assertThat(environment.getProperty("pConfig1"), is("pConfig1Value")); + assertThat(environment.getProperty("pContext1")).isEqualTo("pContext1Value"); + assertThat(environment.getProperty("pConfig1")).isEqualTo("pConfig1Value"); // Servlet* PropertySources have precedence over System* PropertySources - assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), - lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); + assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME))) + .isLessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); @@ -524,12 +531,12 @@ public void registerServletParamPropertySources_StaticWebApplicationContext() { propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletconfig params resolve with higher precedence than sysprops - assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue")); - assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); + assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonConfigValue"); + assertThat(environment.getProperty("pSysProps1")).isEqualTo("pSysProps1Value"); } @Test - public void resourceAdapterApplicationContext() { + void resourceAdapterApplicationContext() { ResourceAdapterApplicationContext ctx = new ResourceAdapterApplicationContext(new SimpleBootstrapContext(new SimpleTaskWorkManager())); assertHasStandardEnvironment(ctx); @@ -545,7 +552,7 @@ public void resourceAdapterApplicationContext() { } @Test - public void abstractApplicationContextValidatesRequiredPropertiesOnRefresh() { + void abstractApplicationContextValidatesRequiredPropertiesOnRefresh() { { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.refresh(); @@ -554,12 +561,8 @@ public void abstractApplicationContextValidatesRequiredPropertiesOnRefresh() { { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setRequiredProperties("foo", "bar"); - try { - ctx.refresh(); - fail("expected missing property exception"); - } - catch (MissingRequiredPropertiesException ex) { - } + assertThatExceptionOfType(MissingRequiredPropertiesException.class).isThrownBy( + ctx::refresh); } { @@ -585,33 +588,33 @@ private void registerEnvironmentBeanDefinition(BeanDefinitionRegistry registry) private void assertEnvironmentBeanRegistered( ConfigurableApplicationContext ctx) { // ensure environment is registered as a bean - assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME)).isTrue(); } private void assertHasStandardEnvironment(ApplicationContext ctx) { Environment defaultEnv = ctx.getEnvironment(); - assertThat(defaultEnv, notNullValue()); - assertThat(defaultEnv, instanceOf(StandardEnvironment.class)); + assertThat(defaultEnv).isNotNull(); + assertThat(defaultEnv).isInstanceOf(StandardEnvironment.class); } private void assertHasStandardServletEnvironment(WebApplicationContext ctx) { // ensure a default servlet environment exists Environment defaultEnv = ctx.getEnvironment(); - assertThat(defaultEnv, notNullValue()); - assertThat(defaultEnv, instanceOf(StandardServletEnvironment.class)); + assertThat(defaultEnv).isNotNull(); + assertThat(defaultEnv).isInstanceOf(StandardServletEnvironment.class); } private void assertHasEnvironment(ApplicationContext ctx, Environment expectedEnv) { // ensure the custom environment took Environment actualEnv = ctx.getEnvironment(); - assertThat(actualEnv, notNullValue()); - assertThat(actualEnv, is(expectedEnv)); + assertThat(actualEnv).isNotNull(); + assertThat(actualEnv).isEqualTo(expectedEnv); // ensure environment is registered as a bean - assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME), is(true)); + assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME)).isTrue(); } private void assertEnvironmentAwareInvoked(ConfigurableApplicationContext ctx, Environment expectedEnv) { - assertThat(ctx.getBean(EnvironmentAwareBean.class).environment, is(expectedEnv)); + assertThat(ctx.getBean(EnvironmentAwareBean.class).environment).isEqualTo(expectedEnv); } diff --git a/src/test/java/org/springframework/core/env/PropertyPlaceholderConfigurerEnvironmentIntegrationTests.java b/integration-tests/src/test/java/org/springframework/core/env/PropertyPlaceholderConfigurerEnvironmentIntegrationTests.java similarity index 71% rename from src/test/java/org/springframework/core/env/PropertyPlaceholderConfigurerEnvironmentIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/core/env/PropertyPlaceholderConfigurerEnvironmentIntegrationTests.java index fb95406498ca..248000ce7bc7 100644 --- a/src/test/java/org/springframework/core/env/PropertyPlaceholderConfigurerEnvironmentIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/core/env/PropertyPlaceholderConfigurerEnvironmentIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,22 +16,25 @@ package org.springframework.core.env; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; +import org.junit.jupiter.api.Test; -import org.junit.Test; -import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.context.support.GenericApplicationContext; -@SuppressWarnings("resource") -public class PropertyPlaceholderConfigurerEnvironmentIntegrationTests { +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; + +class PropertyPlaceholderConfigurerEnvironmentIntegrationTests { + @Test - public void test() { + @SuppressWarnings("deprecation") + void test() { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.registerBeanDefinition("ppc", - rootBeanDefinition(PropertyPlaceholderConfigurer.class) + rootBeanDefinition(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.class) .addPropertyValue("searchSystemEnvironment", false) .getBeanDefinition()); ctx.refresh(); ctx.getBean("ppc"); + ctx.close(); } + } diff --git a/integration-tests/src/test/java/org/springframework/core/env/scan1/Config.java b/integration-tests/src/test/java/org/springframework/core/env/scan1/Config.java new file mode 100644 index 000000000000..8e86c302c7d4 --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/core/env/scan1/Config.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.core.env.scan1; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@Import({ DevConfig.class, ProdConfig.class }) +class Config { + +} diff --git a/integration-tests/src/test/java/org/springframework/core/env/scan1/DevConfig.java b/integration-tests/src/test/java/org/springframework/core/env/scan1/DevConfig.java new file mode 100644 index 000000000000..d63e79752e9d --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/core/env/scan1/DevConfig.java @@ -0,0 +1,32 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.core.env.scan1; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +@Profile(org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DEV_ENV_NAME) +@Configuration +class DevConfig { + + @Bean + public Object devBean() { + return new Object(); + } + +} diff --git a/src/test/java/org/springframework/core/env/scan1/package-info.java b/integration-tests/src/test/java/org/springframework/core/env/scan1/ProdConfig.java similarity index 53% rename from src/test/java/org/springframework/core/env/scan1/package-info.java rename to integration-tests/src/test/java/org/springframework/core/env/scan1/ProdConfig.java index 0bfa7e043e7f..eaf7c9a551a4 100644 --- a/src/test/java/org/springframework/core/env/scan1/package-info.java +++ b/integration-tests/src/test/java/org/springframework/core/env/scan1/ProdConfig.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,39 +14,19 @@ * limitations under the License. */ -/** - * Mirrors the structure of beans and environment-specific config files - * in EnvironmentIntegrationTests-context.xml - */ package org.springframework.core.env.scan1; -import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DEV_ENV_NAME; -import static org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.PROD_ENV_NAME; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Profile; -@Configuration -@Import({DevConfig.class, ProdConfig.class}) -class Config { -} - -@Profile(DEV_ENV_NAME) -@Configuration -class DevConfig { - @Bean - public Object devBean() { - return new Object(); - } -} - -@Profile(PROD_ENV_NAME) +@Profile(org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.PROD_ENV_NAME) @Configuration class ProdConfig { + @Bean public Object prodBean() { return new Object(); } + } diff --git a/integration-tests/src/test/java/org/springframework/core/env/scan2/DevBean.java b/integration-tests/src/test/java/org/springframework/core/env/scan2/DevBean.java new file mode 100644 index 000000000000..8142b06b8f62 --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/core/env/scan2/DevBean.java @@ -0,0 +1,25 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.core.env.scan2; + +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Profile(org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DEV_ENV_NAME) +@Component(org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.DEV_BEAN_NAME) +class DevBean { +} diff --git a/integration-tests/src/test/java/org/springframework/core/env/scan2/ProdBean.java b/integration-tests/src/test/java/org/springframework/core/env/scan2/ProdBean.java new file mode 100644 index 000000000000..75ee9d53c1b9 --- /dev/null +++ b/integration-tests/src/test/java/org/springframework/core/env/scan2/ProdBean.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.core.env.scan2; + +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Profile(org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.PROD_ENV_NAME) +@Component(org.springframework.core.env.EnvironmentSystemIntegrationTests.Constants.PROD_BEAN_NAME) +class ProdBean { + +} diff --git a/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java b/integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java similarity index 98% rename from src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java rename to integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java index 366f338f1137..63174d2d3327 100644 --- a/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java +++ b/integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/src/test/java/org/springframework/expression/spel/support/Spr7538Tests.java b/integration-tests/src/test/java/org/springframework/expression/spel/support/Spr7538Tests.java similarity index 79% rename from src/test/java/org/springframework/expression/spel/support/Spr7538Tests.java rename to integration-tests/src/test/java/org/springframework/expression/spel/support/Spr7538Tests.java index 39719739ea64..372d232f7143 100644 --- a/src/test/java/org/springframework/expression/spel/support/Spr7538Tests.java +++ b/integration-tests/src/test/java/org/springframework/expression/spel/support/Spr7538Tests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,28 +18,26 @@ import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; + import org.springframework.core.MethodParameter; import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.MethodExecutor; -public class Spr7538Tests { +class Spr7538Tests { - @Ignore @Test - public void repro() throws Exception { + @Test + void repro() throws Exception { AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy(); BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter(); StandardEvaluationContext context = new StandardEvaluationContext(); context.setTypeConverter(converter); - List arguments = new ArrayList<>(); - - // !!!! With the below line commented you'll get NPE. Uncomment and everything is OK! - //arguments.add(new Foo()); + List arguments = Collections.emptyList(); List paramDescriptors = new ArrayList<>(); Method method = AlwaysTrueReleaseStrategy.class.getMethod("checkCompleteness", List.class); @@ -55,11 +53,11 @@ public void repro() throws Exception { System.out.println("Result: " + result); } - public static class AlwaysTrueReleaseStrategy { + static class AlwaysTrueReleaseStrategy { public boolean checkCompleteness(List messages) { return true; } } - public static class Foo{} + static class Foo{} } diff --git a/src/test/java/org/springframework/scheduling/annotation/ScheduledAndTransactionalAnnotationIntegrationTests.java b/integration-tests/src/test/java/org/springframework/scheduling/annotation/ScheduledAndTransactionalAnnotationIntegrationTests.java similarity index 70% rename from src/test/java/org/springframework/scheduling/annotation/ScheduledAndTransactionalAnnotationIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/scheduling/annotation/ScheduledAndTransactionalAnnotationIntegrationTests.java index 474e41c0a93e..8b49193b8630 100644 --- a/src/test/java/org/springframework/scheduling/annotation/ScheduledAndTransactionalAnnotationIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/scheduling/annotation/ScheduledAndTransactionalAnnotationIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,8 +19,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.aspectj.lang.annotation.Aspect; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator; import org.springframework.aop.support.AopUtils; @@ -29,19 +28,19 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.testfixture.EnabledForTestGroups; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.stereotype.Repository; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; -import org.springframework.tests.transaction.CallCountingTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.testfixture.CallCountingTransactionManager; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.mock; +import static org.springframework.core.testfixture.TestGroup.PERFORMANCE; /** * Integration tests cornering bug SPR-8651, which revealed that @Scheduled methods may @@ -53,29 +52,20 @@ * @since 3.1 */ @SuppressWarnings("resource") -public class ScheduledAndTransactionalAnnotationIntegrationTests { - - @Before - public void assumePerformanceTests() { - Assume.group(TestGroup.PERFORMANCE); - } - +@EnabledForTestGroups(PERFORMANCE) +class ScheduledAndTransactionalAnnotationIntegrationTests { @Test - public void failsWhenJdkProxyAndScheduledMethodNotPresentOnInterface() { + void failsWhenJdkProxyAndScheduledMethodNotPresentOnInterface() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigA.class); - try { - ctx.refresh(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getRootCause() instanceof IllegalStateException); - } + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(ctx::refresh) + .satisfies(ex -> assertThat(ex.getRootCause()).isInstanceOf(IllegalStateException.class)); } @Test - public void succeedsWhenSubclassProxyAndScheduledMethodNotPresentOnInterface() throws InterruptedException { + void succeedsWhenSubclassProxyAndScheduledMethodNotPresentOnInterface() throws InterruptedException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, SubclassProxyTxConfig.class, RepoConfigA.class); ctx.refresh(); @@ -84,13 +74,13 @@ public void succeedsWhenSubclassProxyAndScheduledMethodNotPresentOnInterface() t MyRepository repository = ctx.getBean(MyRepository.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); - assertThat("repository is not a proxy", AopUtils.isCglibProxy(repository), equalTo(true)); - assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0)); - assertThat("no transactions were committed", txManager.commits, greaterThan(0)); + assertThat(AopUtils.isCglibProxy(repository)).isEqualTo(true); + assertThat(repository.getInvocationCount()).isGreaterThan(0); + assertThat(txManager.commits).isGreaterThan(0); } @Test - public void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws InterruptedException { + void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws InterruptedException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigB.class); ctx.refresh(); @@ -99,13 +89,13 @@ public void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); - assertThat("repository is not a proxy", AopUtils.isJdkDynamicProxy(repository), is(true)); - assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0)); - assertThat("no transactions were committed", txManager.commits, greaterThan(0)); + assertThat(AopUtils.isJdkDynamicProxy(repository)).isTrue(); + assertThat(repository.getInvocationCount()).isGreaterThan(0); + assertThat(txManager.commits).isGreaterThan(0); } @Test - public void withAspectConfig() throws InterruptedException { + void withAspectConfig() throws InterruptedException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AspectConfig.class, MyRepositoryWithScheduledMethodImpl.class); ctx.refresh(); @@ -113,8 +103,8 @@ public void withAspectConfig() throws InterruptedException { Thread.sleep(100); // allow @Scheduled method to be called several times MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class); - assertThat("repository is not a proxy", AopUtils.isCglibProxy(repository), is(true)); - assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0)); + assertThat(AopUtils.isCglibProxy(repository)).isTrue(); + assertThat(repository.getInvocationCount()).isGreaterThan(0); } @@ -134,7 +124,7 @@ static class SubclassProxyTxConfig { static class RepoConfigA { @Bean - public MyRepository repository() { + MyRepository repository() { return new MyRepositoryImpl(); } } @@ -144,7 +134,7 @@ public MyRepository repository() { static class RepoConfigB { @Bean - public MyRepositoryWithScheduledMethod repository() { + MyRepositoryWithScheduledMethod repository() { return new MyRepositoryWithScheduledMethodImpl(); } } @@ -155,17 +145,17 @@ public MyRepositoryWithScheduledMethod repository() { static class Config { @Bean - public PlatformTransactionManager txManager() { + PlatformTransactionManager txManager() { return new CallCountingTransactionManager(); } @Bean - public PersistenceExceptionTranslator peTranslator() { + PersistenceExceptionTranslator peTranslator() { return mock(PersistenceExceptionTranslator.class); } @Bean - public static PersistenceExceptionTranslationPostProcessor peTranslationPostProcessor() { + static PersistenceExceptionTranslationPostProcessor peTranslationPostProcessor() { return new PersistenceExceptionTranslationPostProcessor(); } } @@ -176,14 +166,14 @@ public static PersistenceExceptionTranslationPostProcessor peTranslationPostProc static class AspectConfig { @Bean - public static AnnotationAwareAspectJAutoProxyCreator autoProxyCreator() { + static AnnotationAwareAspectJAutoProxyCreator autoProxyCreator() { AnnotationAwareAspectJAutoProxyCreator apc = new AnnotationAwareAspectJAutoProxyCreator(); apc.setProxyTargetClass(true); return apc; } @Bean - public static MyAspect myAspect() { + static MyAspect myAspect() { return new MyAspect(); } } @@ -251,7 +241,7 @@ public void scheduled() { @Override public int getInvocationCount() { if (this.myAspect != null) { - assertEquals(this.count.get(), this.myAspect.count.get()); + assertThat(this.myAspect.count.get()).isEqualTo(this.count.get()); } return this.count.get(); } diff --git a/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java b/integration-tests/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java similarity index 69% rename from src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java rename to integration-tests/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java index 25a21718d8d2..8ba437386b04 100644 --- a/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java +++ b/integration-tests/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,14 +17,14 @@ package org.springframework.transaction.annotation; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; + import javax.sql.DataSource; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.aop.Advisor; import org.springframework.aop.framework.Advised; import org.springframework.aop.support.AopUtils; import org.springframework.cache.Cache; @@ -40,150 +40,121 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.stereotype.Repository; -import org.springframework.tests.transaction.CallCountingTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor; +import org.springframework.transaction.testfixture.CallCountingTransactionManager; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Integration tests for the @EnableTransactionManagement annotation. * * @author Chris Beams + * @author Sam Brannen * @since 3.1 */ @SuppressWarnings("resource") -public class EnableTransactionManagementIntegrationTests { +class EnableTransactionManagementIntegrationTests { @Test - public void repositoryIsNotTxProxy() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(Config.class); - ctx.refresh(); + void repositoryIsNotTxProxy() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); - try { - assertTxProxying(ctx); - fail("expected exception"); - } - catch (AssertionError ex) { - assertThat(ex.getMessage(), equalTo("FooRepository is not a TX proxy")); - } + assertThat(isTxProxy(ctx.getBean(FooRepository.class))).isFalse(); } @Test - public void repositoryIsTxProxy_withDefaultTxManagerName() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(Config.class, DefaultTxManagerNameConfig.class); - ctx.refresh(); + void repositoryIsTxProxy_withDefaultTxManagerName() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, DefaultTxManagerNameConfig.class); assertTxProxying(ctx); } @Test - public void repositoryIsTxProxy_withCustomTxManagerName() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(Config.class, CustomTxManagerNameConfig.class); - ctx.refresh(); + void repositoryIsTxProxy_withCustomTxManagerName() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, CustomTxManagerNameConfig.class); assertTxProxying(ctx); } - @Ignore @Test // TODO SPR-8207 - public void repositoryIsTxProxy_withNonConventionalTxManagerName_fallsBackToByTypeLookup() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(Config.class, NonConventionalTxManagerNameConfig.class); - ctx.refresh(); - - assertTxProxying(ctx); + @Test + void repositoryIsTxProxy_withNonConventionalTxManagerName_fallsBackToByTypeLookup() { + assertTxProxying(new AnnotationConfigApplicationContext(Config.class, NonConventionalTxManagerNameConfig.class)); } @Test - public void repositoryIsClassBasedTxProxy() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(Config.class, ProxyTargetClassTxConfig.class); - ctx.refresh(); + void repositoryIsClassBasedTxProxy() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, ProxyTargetClassTxConfig.class); assertTxProxying(ctx); - assertThat(AopUtils.isCglibProxy(ctx.getBean(FooRepository.class)), is(true)); + assertThat(AopUtils.isCglibProxy(ctx.getBean(FooRepository.class))).isTrue(); } @Test - public void repositoryUsesAspectJAdviceMode() { + void repositoryUsesAspectJAdviceMode() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, AspectJTxConfig.class); - try { - ctx.refresh(); - } - catch (Exception ex) { - // this test is a bit fragile, but gets the job done, proving that an - // attempt was made to look up the AJ aspect. It's due to classpath issues - // in .integration-tests that it's not found. - assertTrue(ex.getMessage().contains("AspectJJtaTransactionManagementConfiguration")); - } + // this test is a bit fragile, but gets the job done, proving that an + // attempt was made to look up the AJ aspect. It's due to classpath issues + // in .integration-tests that it's not found. + assertThatExceptionOfType(Exception.class) + .isThrownBy(ctx::refresh) + .withMessageContaining("AspectJJtaTransactionManagementConfiguration"); } @Test - public void implicitTxManager() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(ImplicitTxManagerConfig.class); - ctx.refresh(); + void implicitTxManager() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImplicitTxManagerConfig.class); FooRepository fooRepository = ctx.getBean(FooRepository.class); fooRepository.findAll(); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); - assertThat(txManager.begun, equalTo(1)); - assertThat(txManager.commits, equalTo(1)); - assertThat(txManager.rollbacks, equalTo(0)); + assertThat(txManager.begun).isEqualTo(1); + assertThat(txManager.commits).isEqualTo(1); + assertThat(txManager.rollbacks).isEqualTo(0); } @Test - public void explicitTxManager() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(ExplicitTxManagerConfig.class); - ctx.refresh(); + void explicitTxManager() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ExplicitTxManagerConfig.class); FooRepository fooRepository = ctx.getBean(FooRepository.class); fooRepository.findAll(); CallCountingTransactionManager txManager1 = ctx.getBean("txManager1", CallCountingTransactionManager.class); - assertThat(txManager1.begun, equalTo(1)); - assertThat(txManager1.commits, equalTo(1)); - assertThat(txManager1.rollbacks, equalTo(0)); + assertThat(txManager1.begun).isEqualTo(1); + assertThat(txManager1.commits).isEqualTo(1); + assertThat(txManager1.rollbacks).isEqualTo(0); CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class); - assertThat(txManager2.begun, equalTo(0)); - assertThat(txManager2.commits, equalTo(0)); - assertThat(txManager2.rollbacks, equalTo(0)); + assertThat(txManager2.begun).isEqualTo(0); + assertThat(txManager2.commits).isEqualTo(0); + assertThat(txManager2.rollbacks).isEqualTo(0); } @Test - public void apcEscalation() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(EnableTxAndCachingConfig.class); - ctx.refresh(); + void apcEscalation() { + new AnnotationConfigApplicationContext(EnableTxAndCachingConfig.class); } private void assertTxProxying(AnnotationConfigApplicationContext ctx) { FooRepository repo = ctx.getBean(FooRepository.class); - - boolean isTxProxy = false; - if (AopUtils.isAopProxy(repo)) { - for (Advisor advisor : ((Advised)repo).getAdvisors()) { - if (advisor instanceof BeanFactoryTransactionAttributeSourceAdvisor) { - isTxProxy = true; - break; - } - } - } - assertTrue("FooRepository is not a TX proxy", isTxProxy); - + assertThat(isTxProxy(repo)).isTrue(); // trigger a transaction repo.findAll(); } + private boolean isTxProxy(FooRepository repo) { + if (!AopUtils.isAopProxy(repo)) { + return false; + } + return Arrays.stream(((Advised) repo).getAdvisors()) + .anyMatch(BeanFactoryTransactionAttributeSourceAdvisor.class::isInstance); + } + @Configuration @EnableTransactionManagement diff --git a/src/test/java/org/springframework/transaction/annotation/ProxyAnnotationDiscoveryTests.java b/integration-tests/src/test/java/org/springframework/transaction/annotation/ProxyAnnotationDiscoveryTests.java similarity index 69% rename from src/test/java/org/springframework/transaction/annotation/ProxyAnnotationDiscoveryTests.java rename to integration-tests/src/test/java/org/springframework/transaction/annotation/ProxyAnnotationDiscoveryTests.java index 6acd90bcb942..67a61e85fab8 100644 --- a/src/test/java/org/springframework/transaction/annotation/ProxyAnnotationDiscoveryTests.java +++ b/integration-tests/src/test/java/org/springframework/transaction/annotation/ProxyAnnotationDiscoveryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,16 +16,14 @@ package org.springframework.transaction.annotation; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; -import org.junit.Test; import org.springframework.aop.support.AopUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests proving that regardless the proxy strategy used (JDK interface-based vs. CGLIB * subclass-based), discovery of advice-oriented annotations is consistent. @@ -37,66 +35,66 @@ * @author Chris Beams */ @SuppressWarnings("resource") -public class ProxyAnnotationDiscoveryTests { +class ProxyAnnotationDiscoveryTests { @Test - public void annotatedServiceWithoutInterface_PTC_true() { + void annotatedServiceWithoutInterface_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, AnnotatedServiceWithoutInterface.class); ctx.refresh(); AnnotatedServiceWithoutInterface s = ctx.getBean(AnnotatedServiceWithoutInterface.class); - assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); - assertThat(s, instanceOf(AnnotatedServiceWithoutInterface.class)); + assertThat(AopUtils.isCglibProxy(s)).isTrue(); + assertThat(s).isInstanceOf(AnnotatedServiceWithoutInterface.class); } @Test - public void annotatedServiceWithoutInterface_PTC_false() { + void annotatedServiceWithoutInterface_PTC_false() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCFalse.class, AnnotatedServiceWithoutInterface.class); ctx.refresh(); AnnotatedServiceWithoutInterface s = ctx.getBean(AnnotatedServiceWithoutInterface.class); - assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); - assertThat(s, instanceOf(AnnotatedServiceWithoutInterface.class)); + assertThat(AopUtils.isCglibProxy(s)).isTrue(); + assertThat(s).isInstanceOf(AnnotatedServiceWithoutInterface.class); } @Test - public void nonAnnotatedService_PTC_true() { + void nonAnnotatedService_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, AnnotatedServiceImpl.class); ctx.refresh(); NonAnnotatedService s = ctx.getBean(NonAnnotatedService.class); - assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); - assertThat(s, instanceOf(AnnotatedServiceImpl.class)); + assertThat(AopUtils.isCglibProxy(s)).isTrue(); + assertThat(s).isInstanceOf(AnnotatedServiceImpl.class); } @Test - public void nonAnnotatedService_PTC_false() { + void nonAnnotatedService_PTC_false() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCFalse.class, AnnotatedServiceImpl.class); ctx.refresh(); NonAnnotatedService s = ctx.getBean(NonAnnotatedService.class); - assertTrue("expected a jdk proxy", AopUtils.isJdkDynamicProxy(s)); - assertThat(s, not(instanceOf(AnnotatedServiceImpl.class))); + assertThat(AopUtils.isJdkDynamicProxy(s)).isTrue(); + assertThat(s).isNotInstanceOf(AnnotatedServiceImpl.class); } @Test - public void annotatedService_PTC_true() { + void annotatedService_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, NonAnnotatedServiceImpl.class); ctx.refresh(); AnnotatedService s = ctx.getBean(AnnotatedService.class); - assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); - assertThat(s, instanceOf(NonAnnotatedServiceImpl.class)); + assertThat(AopUtils.isCglibProxy(s)).isTrue(); + assertThat(s).isInstanceOf(NonAnnotatedServiceImpl.class); } @Test - public void annotatedService_PTC_false() { + void annotatedService_PTC_false() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCFalse.class, NonAnnotatedServiceImpl.class); ctx.refresh(); AnnotatedService s = ctx.getBean(AnnotatedService.class); - assertTrue("expected a jdk proxy", AopUtils.isJdkDynamicProxy(s)); - assertThat(s, not(instanceOf(NonAnnotatedServiceImpl.class))); + assertThat(AopUtils.isJdkDynamicProxy(s)).isTrue(); + assertThat(s).isNotInstanceOf(NonAnnotatedServiceImpl.class); } } diff --git a/integration-tests/src/test/resources/META-INF/spring.handlers b/integration-tests/src/test/resources/META-INF/spring.handlers new file mode 100644 index 000000000000..a075db5cb857 --- /dev/null +++ b/integration-tests/src/test/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.foo.example/schema/component=org.springframework.beans.factory.xml.ComponentNamespaceHandler \ No newline at end of file diff --git a/integration-tests/src/test/resources/META-INF/spring.schemas b/integration-tests/src/test/resources/META-INF/spring.schemas new file mode 100644 index 000000000000..8bad15c291c7 --- /dev/null +++ b/integration-tests/src/test/resources/META-INF/spring.schemas @@ -0,0 +1 @@ +http\://www.foo.example/schema/component/component.xsd=org/springframework/beans/factory/xml/component.xsd \ No newline at end of file diff --git a/src/test/resources/log4j2-test.xml b/integration-tests/src/test/resources/log4j2-test.xml similarity index 94% rename from src/test/resources/log4j2-test.xml rename to integration-tests/src/test/resources/log4j2-test.xml index df3661a24b16..d0ac5e7d6a2a 100644 --- a/src/test/resources/log4j2-test.xml +++ b/integration-tests/src/test/resources/log4j2-test.xml @@ -2,7 +2,7 @@ - + diff --git a/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests-afterFirst.xml b/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests-afterFirst.xml new file mode 100644 index 000000000000..fd98a0c1b407 --- /dev/null +++ b/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests-afterFirst.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + diff --git a/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests-afterLast.xml b/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests-afterLast.xml new file mode 100644 index 000000000000..bcab0df678a4 --- /dev/null +++ b/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests-afterLast.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests-context.xml b/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests-context.xml similarity index 60% rename from src/test/resources/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests-context.xml rename to integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests-context.xml index 49e480a6644a..ab891535b514 100644 --- a/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests-context.xml +++ b/integration-tests/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests-context.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> @@ -11,19 +11,19 @@ - + - + - + - + diff --git a/src/test/resources/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests-context.xml b/integration-tests/src/test/resources/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests-context.xml similarity index 82% rename from src/test/resources/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests-context.xml rename to integration-tests/src/test/resources/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests-context.xml index a35fdce1d048..90ea7dc1f663 100644 --- a/src/test/resources/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests-context.xml +++ b/integration-tests/src/test/resources/org/springframework/aop/framework/autoproxy/AdvisorAutoProxyCreatorIntegrationTests-context.xml @@ -1,5 +1,5 @@ - + - + @@ -74,20 +74,20 @@ - + - + - org.springframework.tests.sample.beans.ITestBean.getName + org.springframework.beans.testfixture.beans.ITestBean.getName - + - + 4 @@ -97,7 +97,7 @@ - + diff --git a/src/test/resources/com/foo/component-config.xml b/integration-tests/src/test/resources/org/springframework/beans/factory/xml/component-config.xml similarity index 63% rename from src/test/resources/com/foo/component-config.xml rename to integration-tests/src/test/resources/org/springframework/beans/factory/xml/component-config.xml index 44471ac7e7a1..1a649ba7f7aa 100644 --- a/src/test/resources/com/foo/component-config.xml +++ b/integration-tests/src/test/resources/org/springframework/beans/factory/xml/component-config.xml @@ -1,10 +1,10 @@ +http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd +http://www.foo.example/schema/component http://www.foo.example/schema/component/component.xsd"> diff --git a/src/test/resources/com/foo/component.xsd b/integration-tests/src/test/resources/org/springframework/beans/factory/xml/component.xsd similarity index 81% rename from src/test/resources/com/foo/component.xsd rename to integration-tests/src/test/resources/org/springframework/beans/factory/xml/component.xsd index 0c33cd4f785f..b98c04f03f65 100644 --- a/src/test/resources/com/foo/component.xsd +++ b/integration-tests/src/test/resources/org/springframework/beans/factory/xml/component.xsd @@ -1,8 +1,8 @@ - diff --git a/src/test/resources/org/springframework/context/annotation/ltw/ComponentScanningWithLTWTests.xml b/integration-tests/src/test/resources/org/springframework/context/annotation/ltw/ComponentScanningWithLTWTests.xml similarity index 75% rename from src/test/resources/org/springframework/context/annotation/ltw/ComponentScanningWithLTWTests.xml rename to integration-tests/src/test/resources/org/springframework/context/annotation/ltw/ComponentScanningWithLTWTests.xml index c63d4ab1eaf4..b012d130efc3 100644 --- a/src/test/resources/org/springframework/context/annotation/ltw/ComponentScanningWithLTWTests.xml +++ b/integration-tests/src/test/resources/org/springframework/context/annotation/ltw/ComponentScanningWithLTWTests.xml @@ -4,8 +4,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation= - "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" + "http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byType"> diff --git a/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-dev.xml b/integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-dev.xml similarity index 81% rename from src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-dev.xml rename to integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-dev.xml index 45c18434e725..f9da4bfb28de 100644 --- a/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-dev.xml +++ b/integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-dev.xml @@ -1,7 +1,7 @@ diff --git a/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-prod.xml b/integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-prod.xml similarity index 81% rename from src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-prod.xml rename to integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-prod.xml index 472a09aed76d..b40902fdd0de 100644 --- a/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-prod.xml +++ b/integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context-prod.xml @@ -1,7 +1,7 @@ diff --git a/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context.xml b/integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context.xml similarity index 89% rename from src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context.xml rename to integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context.xml index 0fcafe09e4e5..dabc2bb3cd4b 100644 --- a/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context.xml +++ b/integration-tests/src/test/resources/org/springframework/core/env/EnvironmentSystemIntegrationTests-context.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/src/test/resources/org/springframework/transaction/annotation/enable-caching.xml b/integration-tests/src/test/resources/org/springframework/transaction/annotation/enable-caching.xml similarity index 64% rename from src/test/resources/org/springframework/transaction/annotation/enable-caching.xml rename to integration-tests/src/test/resources/org/springframework/transaction/annotation/enable-caching.xml index efc9ec29f30b..195822b291cd 100644 --- a/src/test/resources/org/springframework/transaction/annotation/enable-caching.xml +++ b/integration-tests/src/test/resources/org/springframework/transaction/annotation/enable-caching.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsd"> diff --git a/src/test/resources/org/springframework/util/testlog4j.properties b/integration-tests/src/test/resources/org/springframework/util/testlog4j.properties similarity index 100% rename from src/test/resources/org/springframework/util/testlog4j.properties rename to integration-tests/src/test/resources/org/springframework/util/testlog4j.properties diff --git a/src/test/resources/org/springframework/web/util/testlog4j.properties b/integration-tests/src/test/resources/org/springframework/web/util/testlog4j.properties similarity index 100% rename from src/test/resources/org/springframework/web/util/testlog4j.properties rename to integration-tests/src/test/resources/org/springframework/web/util/testlog4j.properties diff --git a/settings.gradle b/settings.gradle index 34f40dc429d8..cf24142b94e6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,10 +1,20 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { url 'https://repo.spring.io/plugins-release' } + } +} +apply from: "$rootDir/gradle/build-cache-settings.gradle" + include "spring-aop" include "spring-aspects" include "spring-beans" include "spring-context" -include "spring-context-support" include "spring-context-indexer" +include "spring-context-support" include "spring-core" +include "kotlin-coroutines" +project(':kotlin-coroutines').projectDir = file('spring-core/kotlin-coroutines') include "spring-expression" include "spring-instrument" include "spring-jcl" @@ -16,14 +26,11 @@ include "spring-oxm" include "spring-test" include "spring-tx" include "spring-web" -include "spring-webmvc" include "spring-webflux" +include "spring-webmvc" include "spring-websocket" -include "spring-framework-bom" - -// Exposes gradle buildSrc for IDE support -include "buildSrc" -rootProject.children.find{ it.name == "buildSrc" }.name = "spring-build-src" +include "framework-bom" +include "integration-tests" rootProject.name = "spring" rootProject.children.each {project -> diff --git a/spring-aop/spring-aop.gradle b/spring-aop/spring-aop.gradle index 6911a3b5f203..73bb378f39a7 100644 --- a/spring-aop/spring-aop.gradle +++ b/spring-aop/spring-aop.gradle @@ -3,7 +3,10 @@ description = "Spring AOP" dependencies { compile(project(":spring-beans")) compile(project(":spring-core")) - optional("org.aspectj:aspectjweaver:${aspectjVersion}") - optional("org.apache.commons:commons-pool2:2.6.0") - optional("com.jamonapi:jamon:2.81") + optional("org.aspectj:aspectjweaver") + optional("org.apache.commons:commons-pool2") + optional("com.jamonapi:jamon") + testCompile(testFixtures(project(":spring-beans"))) + testCompile(testFixtures(project(":spring-core"))) + testFixturesImplementation(testFixtures(project(":spring-core"))) } diff --git a/spring-aop/src/main/java/org/aopalliance/aop/Advice.java b/spring-aop/src/main/java/org/aopalliance/aop/Advice.java index 6dcbc4af2560..38f999945810 100644 --- a/spring-aop/src/main/java/org/aopalliance/aop/Advice.java +++ b/spring-aop/src/main/java/org/aopalliance/aop/Advice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/aopalliance/aop/AspectException.java b/spring-aop/src/main/java/org/aopalliance/aop/AspectException.java index 3b915e6332da..a91c2ac61edc 100644 --- a/spring-aop/src/main/java/org/aopalliance/aop/AspectException.java +++ b/spring-aop/src/main/java/org/aopalliance/aop/AspectException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInterceptor.java b/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInterceptor.java index 7e0ac706a77c..00a42801142e 100644 --- a/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInterceptor.java +++ b/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInvocation.java b/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInvocation.java index cfffeed27c9d..867925b06582 100644 --- a/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInvocation.java +++ b/spring-aop/src/main/java/org/aopalliance/intercept/ConstructorInvocation.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -19,7 +19,7 @@ import java.lang.reflect.Constructor; /** - * Description of an invocation to a constuctor, given to an + * Description of an invocation to a constructor, given to an * interceptor upon constructor-call. * *

A constructor invocation is a joinpoint and can be intercepted diff --git a/spring-aop/src/main/java/org/aopalliance/intercept/Interceptor.java b/spring-aop/src/main/java/org/aopalliance/intercept/Interceptor.java index 9f56db94048f..918e080ff448 100644 --- a/spring-aop/src/main/java/org/aopalliance/intercept/Interceptor.java +++ b/spring-aop/src/main/java/org/aopalliance/intercept/Interceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -33,7 +33,7 @@ * *

  * class DebuggingInterceptor implements MethodInterceptor,
- *     ConstructorInterceptor, FieldInterceptor {
+ *     ConstructorInterceptor {
  *
  *   Object invoke(MethodInvocation i) throws Throwable {
  *     debug(i.getMethod(), i.getThis(), i.getArgs());
@@ -45,16 +45,6 @@
  *     return i.proceed();
  *   }
  *
- *   Object get(FieldAccess fa) throws Throwable {
- *     debug(fa.getField(), fa.getThis(), null);
- *     return fa.proceed();
- *   }
- *
- *   Object set(FieldAccess fa) throws Throwable {
- *     debug(fa.getField(), fa.getThis(), fa.getValueToSet());
- *     return fa.proceed();
- *   }
- *
  *   void debug(AccessibleObject ao, Object this, Object value) {
  *     ...
  *   }
diff --git a/spring-aop/src/main/java/org/aopalliance/intercept/Invocation.java b/spring-aop/src/main/java/org/aopalliance/intercept/Invocation.java
index e785afb43b5f..69db081b1cc3 100644
--- a/spring-aop/src/main/java/org/aopalliance/intercept/Invocation.java
+++ b/spring-aop/src/main/java/org/aopalliance/intercept/Invocation.java
@@ -5,7 +5,7 @@
  * 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
+ *      https://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,
diff --git a/spring-aop/src/main/java/org/aopalliance/intercept/Joinpoint.java b/spring-aop/src/main/java/org/aopalliance/intercept/Joinpoint.java
index 9328f3331ec2..5ede9b738c28 100644
--- a/spring-aop/src/main/java/org/aopalliance/intercept/Joinpoint.java
+++ b/spring-aop/src/main/java/org/aopalliance/intercept/Joinpoint.java
@@ -1,11 +1,11 @@
 /*
- * Copyright 2002-2016 the original author or authors.
+ * Copyright 2002-2021 the original author or authors.
  *
  * Licensed 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
+ *      https://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,
@@ -23,7 +23,7 @@
  * terminology).
  *
  * 

A runtime joinpoint is an event that occurs on a static - * joinpoint (i.e. a location in a the program). For instance, an + * joinpoint (i.e. a location in a program). For instance, an * invocation is the runtime joinpoint on a method (static joinpoint). * The static part of a given joinpoint can be generically retrieved * using the {@link #getStaticPart()} method. diff --git a/spring-aop/src/main/java/org/aopalliance/intercept/MethodInterceptor.java b/spring-aop/src/main/java/org/aopalliance/intercept/MethodInterceptor.java index 006517d34260..3033b8b5fbc0 100644 --- a/spring-aop/src/main/java/org/aopalliance/intercept/MethodInterceptor.java +++ b/spring-aop/src/main/java/org/aopalliance/intercept/MethodInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/aopalliance/intercept/MethodInvocation.java b/spring-aop/src/main/java/org/aopalliance/intercept/MethodInvocation.java index 6314824d7659..3d73f3d12f04 100644 --- a/spring-aop/src/main/java/org/aopalliance/intercept/MethodInvocation.java +++ b/spring-aop/src/main/java/org/aopalliance/intercept/MethodInvocation.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -32,7 +32,7 @@ public interface MethodInvocation extends Invocation { /** * Get the method being called. - *

This method is a frienly implementation of the + *

This method is a friendly implementation of the * {@link Joinpoint#getStaticPart()} method (same result). * @return the method being called */ diff --git a/spring-aop/src/main/java/org/springframework/aop/Advisor.java b/spring-aop/src/main/java/org/springframework/aop/Advisor.java index 2370d032f460..b10911aebf2a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/Advisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/Advisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/AfterAdvice.java b/spring-aop/src/main/java/org/springframework/aop/AfterAdvice.java index e807cbdfedc1..641a7018424a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/AfterAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/AfterAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/AfterReturningAdvice.java b/spring-aop/src/main/java/org/springframework/aop/AfterReturningAdvice.java index 271e82671182..8c2c5d6ef8f0 100644 --- a/spring-aop/src/main/java/org/springframework/aop/AfterReturningAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/AfterReturningAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -33,9 +33,9 @@ public interface AfterReturningAdvice extends AfterAdvice { /** * Callback after a given method successfully returned. * @param returnValue the value returned by the method, if any - * @param method method being invoked - * @param args arguments to the method - * @param target target of the method invocation. May be {@code null}. + * @param method the method being invoked + * @param args the arguments to the method + * @param target the target of the method invocation. May be {@code null}. * @throws Throwable if this object wishes to abort the call. * Any exception thrown will be returned to the caller if it's * allowed by the method signature. Otherwise the exception diff --git a/spring-aop/src/main/java/org/springframework/aop/AopInvocationException.java b/spring-aop/src/main/java/org/springframework/aop/AopInvocationException.java index 8c38ff4b81a2..1acee1559c21 100644 --- a/spring-aop/src/main/java/org/springframework/aop/AopInvocationException.java +++ b/spring-aop/src/main/java/org/springframework/aop/AopInvocationException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/BeforeAdvice.java b/spring-aop/src/main/java/org/springframework/aop/BeforeAdvice.java index 6d8251e68a5e..80123654468f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/BeforeAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/BeforeAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/ClassFilter.java b/spring-aop/src/main/java/org/springframework/aop/ClassFilter.java index 822515f7592d..fa8e2066c07c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/ClassFilter.java +++ b/spring-aop/src/main/java/org/springframework/aop/ClassFilter.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,6 +23,11 @@ *

Can be used as part of a {@link Pointcut} or for the entire * targeting of an {@link IntroductionAdvisor}. * + *

Concrete implementations of this interface typically should provide proper + * implementations of {@link Object#equals(Object)} and {@link Object#hashCode()} + * in order to allow the filter to be used in caching scenarios — for + * example, in proxies generated by CGLIB. + * * @author Rod Johnson * @see Pointcut * @see MethodMatcher diff --git a/spring-aop/src/main/java/org/springframework/aop/DynamicIntroductionAdvice.java b/spring-aop/src/main/java/org/springframework/aop/DynamicIntroductionAdvice.java index 17f7645e7c38..2f46775b9459 100644 --- a/spring-aop/src/main/java/org/springframework/aop/DynamicIntroductionAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/DynamicIntroductionAdvice.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -26,7 +26,7 @@ *

Introductions are often mixins, enabling the building of composite * objects that can achieve many of the goals of multiple inheritance in Java. * - *

Compared to {qlink IntroductionInfo}, this interface allows an advice to + *

Compared to {@link IntroductionInfo}, this interface allows an advice to * implement a range of interfaces that is not necessarily known in advance. * Thus an {@link IntroductionAdvisor} can be used to specify which interfaces * will be exposed in an advised object. diff --git a/spring-aop/src/main/java/org/springframework/aop/IntroductionAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/IntroductionAdvisor.java index b576aa4b219f..cd5f52c37e36 100644 --- a/spring-aop/src/main/java/org/springframework/aop/IntroductionAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/IntroductionAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java index a5ea6e88ce0f..4185c92fa2cc 100644 --- a/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/IntroductionAwareMethodMatcher.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/IntroductionInfo.java b/spring-aop/src/main/java/org/springframework/aop/IntroductionInfo.java index 9f68b0742488..534e2dbc9841 100644 --- a/spring-aop/src/main/java/org/springframework/aop/IntroductionInfo.java +++ b/spring-aop/src/main/java/org/springframework/aop/IntroductionInfo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/IntroductionInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/IntroductionInterceptor.java index 94fbcb155841..5a8ba212d7e3 100644 --- a/spring-aop/src/main/java/org/springframework/aop/IntroductionInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/IntroductionInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/MethodBeforeAdvice.java b/spring-aop/src/main/java/org/springframework/aop/MethodBeforeAdvice.java index a390228202ea..806744d09c31 100644 --- a/spring-aop/src/main/java/org/springframework/aop/MethodBeforeAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/MethodBeforeAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -32,9 +32,9 @@ public interface MethodBeforeAdvice extends BeforeAdvice { /** * Callback before a given method is invoked. - * @param method method being invoked - * @param args arguments to the method - * @param target target of the method invocation. May be {@code null}. + * @param method the method being invoked + * @param args the arguments to the method + * @param target the target of the method invocation. May be {@code null}. * @throws Throwable if this object wishes to abort the call. * Any exception thrown will be returned to the caller if it's * allowed by the method signature. Otherwise the exception diff --git a/spring-aop/src/main/java/org/springframework/aop/MethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/MethodMatcher.java index 9a31e33fda92..29127c73d426 100644 --- a/spring-aop/src/main/java/org/springframework/aop/MethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/MethodMatcher.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -40,6 +40,11 @@ * in an interceptor chain, will have run, so any state changes they have produced in * parameters or ThreadLocal state will be available at the time of evaluation. * + *

Concrete implementations of this interface typically should provide proper + * implementations of {@link Object#equals(Object)} and {@link Object#hashCode()} + * in order to allow the matcher to be used in caching scenarios — for + * example, in proxies generated by CGLIB. + * * @author Rod Johnson * @since 11.11.2003 * @see Pointcut diff --git a/spring-aop/src/main/java/org/springframework/aop/Pointcut.java b/spring-aop/src/main/java/org/springframework/aop/Pointcut.java index 489e7beb820c..ffcf92ef316c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/Pointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/Pointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/PointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/PointcutAdvisor.java index 7b7c1e786477..69eb504eb032 100644 --- a/spring-aop/src/main/java/org/springframework/aop/PointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/PointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/ProxyMethodInvocation.java b/spring-aop/src/main/java/org/springframework/aop/ProxyMethodInvocation.java index 4c8ebeec2f4c..2cc637621c90 100644 --- a/spring-aop/src/main/java/org/springframework/aop/ProxyMethodInvocation.java +++ b/spring-aop/src/main/java/org/springframework/aop/ProxyMethodInvocation.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/RawTargetAccess.java b/spring-aop/src/main/java/org/springframework/aop/RawTargetAccess.java index 4e3c6b787a31..7a15b3495ce4 100644 --- a/spring-aop/src/main/java/org/springframework/aop/RawTargetAccess.java +++ b/spring-aop/src/main/java/org/springframework/aop/RawTargetAccess.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/SpringProxy.java b/spring-aop/src/main/java/org/springframework/aop/SpringProxy.java index 29568ed268bc..42e44ceb0282 100644 --- a/spring-aop/src/main/java/org/springframework/aop/SpringProxy.java +++ b/spring-aop/src/main/java/org/springframework/aop/SpringProxy.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/TargetClassAware.java b/spring-aop/src/main/java/org/springframework/aop/TargetClassAware.java index 28853700f276..d518ddb444a0 100644 --- a/spring-aop/src/main/java/org/springframework/aop/TargetClassAware.java +++ b/spring-aop/src/main/java/org/springframework/aop/TargetClassAware.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/TargetSource.java b/spring-aop/src/main/java/org/springframework/aop/TargetSource.java index 07d42e84c99d..e894c5cb0a10 100644 --- a/spring-aop/src/main/java/org/springframework/aop/TargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/TargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/ThrowsAdvice.java b/spring-aop/src/main/java/org/springframework/aop/ThrowsAdvice.java index 63d4260c333c..ef50fe8b8263 100644 --- a/spring-aop/src/main/java/org/springframework/aop/ThrowsAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/ThrowsAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/TrueClassFilter.java b/spring-aop/src/main/java/org/springframework/aop/TrueClassFilter.java index 8009b3ccad5c..b5bd71f6f1b2 100644 --- a/spring-aop/src/main/java/org/springframework/aop/TrueClassFilter.java +++ b/spring-aop/src/main/java/org/springframework/aop/TrueClassFilter.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/TrueMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/TrueMethodMatcher.java index 966ef7951230..6498627d6fe2 100644 --- a/spring-aop/src/main/java/org/springframework/aop/TrueMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/TrueMethodMatcher.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/TruePointcut.java b/spring-aop/src/main/java/org/springframework/aop/TruePointcut.java index a6c8c9b92d76..f767d75d2f15 100644 --- a/spring-aop/src/main/java/org/springframework/aop/TruePointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/TruePointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java index 57b73ed659eb..32b691f4410b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -63,7 +63,7 @@ public abstract class AbstractAspectJAdvice implements Advice, AspectJPrecedenceInformation, Serializable { /** - * Key used in ReflectiveMethodInvocation userAtributes map for the current joinpoint. + * Key used in ReflectiveMethodInvocation userAttributes map for the current joinpoint. */ protected static final String JOIN_POINT_KEY = JoinPoint.class.getName(); @@ -720,7 +720,7 @@ public boolean matches(Method method, Class targetClass) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -735,6 +735,11 @@ public boolean equals(Object other) { public int hashCode() { return this.adviceMethod.hashCode(); } + + @Override + public String toString() { + return getClass().getName() + ": " + this.adviceMethod; + } } } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectInstanceFactory.java index b0c2c668f3a4..4ddf6303edd5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java index 73eea3bd4ef0..ec58afb29652 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -272,15 +272,7 @@ public String[] getParameterNames(Method method) { } } } - catch (AmbiguousBindingException ambigEx) { - if (this.raiseExceptions) { - throw ambigEx; - } - else { - return null; - } - } - catch (IllegalArgumentException ex) { + catch (AmbiguousBindingException | IllegalArgumentException ex) { if (this.raiseExceptions) { throw ex; } @@ -656,7 +648,7 @@ private PointcutBody getPointcutBody(String[] tokens, int startIndex) { } if (tokens[currentIndex].endsWith(")")) { - sb.append(tokens[currentIndex].substring(0, tokens[currentIndex].length() - 1)); + sb.append(tokens[currentIndex], 0, tokens[currentIndex].length() - 1); return new PointcutBody(numTokensConsumed, sb.toString().trim()); } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterAdvice.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterAdvice.java index 3de3c25aaf08..46ad280a0352 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterReturningAdvice.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterReturningAdvice.java index e679a117ff55..48cedab1be7c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterReturningAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterReturningAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterThrowingAdvice.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterThrowingAdvice.java index fcf89a1215fa..7befd24cf7b6 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterThrowingAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterThrowingAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAopUtils.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAopUtils.java index d14b25e6297c..0d2318092714 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAopUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAopUtils.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAroundAdvice.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAroundAdvice.java index 6e8c1f465133..7eec99f42e8c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAroundAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAroundAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java index ec4f62a2bb97..1cdef3141da1 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -519,7 +519,7 @@ else if (shadowMatch.maybeMatches() && fallbackExpression != null) { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -544,9 +544,7 @@ public int hashCode() { @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("AspectJExpressionPointcut: "); - sb.append("("); + StringBuilder sb = new StringBuilder("AspectJExpressionPointcut: ("); for (int i = 0; i < this.pointcutParameterTypes.length; i++) { sb.append(this.pointcutParameterTypes[i].getName()); sb.append(" "); @@ -555,8 +553,7 @@ public String toString() { sb.append(", "); } } - sb.append(")"); - sb.append(" "); + sb.append(") "); if (getExpression() != null) { sb.append(getExpression()); } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisor.java index d513e910a083..9f4b1e990d8e 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJMethodBeforeAdvice.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJMethodBeforeAdvice.java index 32a2108776c0..207291c51d5a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJMethodBeforeAdvice.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJMethodBeforeAdvice.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java index 108c0eeb8817..b1a12175212b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -93,7 +93,7 @@ public String getAspectName() { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPrecedenceInformation.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPrecedenceInformation.java index cb859d5cf581..88c946887670 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPrecedenceInformation.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPrecedenceInformation.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.java index 329722b5d694..833a109f131e 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -27,27 +27,31 @@ * * @author Rod Johnson * @author Ramnivas Laddad + * @author Juergen Hoeller * @since 2.0 */ public abstract class AspectJProxyUtils { /** - * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors. - * This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching) - * and make available the current AspectJ JoinPoint. The call will have no effect if there are no - * AspectJ advisors in the advisor chain. + * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors: + * concretely, {@link ExposeInvocationInterceptor} at the beginning of the list. + *

This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut + * matching) and make available the current AspectJ JoinPoint. The call will have no effect + * if there are no AspectJ advisors in the advisor chain. * @param advisors the advisors available - * @return {@code true} if any special {@link Advisor Advisors} were added, otherwise {@code false} + * @return {@code true} if an {@link ExposeInvocationInterceptor} was added to the list, + * otherwise {@code false} */ public static boolean makeAdvisorChainAspectJCapableIfNecessary(List advisors) { // Don't add advisors to an empty list; may indicate that proxying is just not required if (!advisors.isEmpty()) { boolean foundAspectJAdvice = false; for (Advisor advisor : advisors) { - // Be careful not to get the Advice without a guard, as - // this might eagerly instantiate a non-singleton AspectJ aspect + // Be careful not to get the Advice without a guard, as this might eagerly + // instantiate a non-singleton AspectJ aspect... if (isAspectJAdvice(advisor)) { foundAspectJAdvice = true; + break; } } if (foundAspectJAdvice && !advisors.contains(ExposeInvocationInterceptor.ADVISOR)) { diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJWeaverMessageHandler.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJWeaverMessageHandler.java index 25d5cca32be1..ed837d8c9415 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJWeaverMessageHandler.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJWeaverMessageHandler.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java index aa020a0756e9..3a4f8992cb14 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/InstantiationModelAwarePointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/InstantiationModelAwarePointcutAdvisor.java index 325b04e24a66..2f3ddab33c30 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/InstantiationModelAwarePointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/InstantiationModelAwarePointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPoint.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPoint.java index e31b0129dcfd..d1c4db25c284 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPoint.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPoint.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -219,10 +219,12 @@ public Class[] getParameterTypes() { @Override @Nullable public String[] getParameterNames() { - if (this.parameterNames == null) { - this.parameterNames = parameterNameDiscoverer.getParameterNames(getMethod()); + String[] parameterNames = this.parameterNames; + if (parameterNames == null) { + parameterNames = parameterNameDiscoverer.getParameterNames(getMethod()); + this.parameterNames = parameterNames; } - return this.parameterNames; + return parameterNames; } @Override diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/RuntimeTestWalker.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/RuntimeTestWalker.java index 59d243764e50..82e13b2bd2bc 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/RuntimeTestWalker.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/RuntimeTestWalker.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java index b002bc86d120..f8a674ab13e6 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/SingletonAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/SingletonAspectInstanceFactory.java index 71fb7145032d..80447912c292 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/SingletonAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/SingletonAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/TypePatternClassFilter.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/TypePatternClassFilter.java index 8b5eec1abe08..cf1dcd929b15 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/TypePatternClassFilter.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/TypePatternClassFilter.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,6 +22,7 @@ import org.springframework.aop.ClassFilter; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** @@ -29,6 +30,7 @@ * * @author Rod Johnson * @author Juergen Hoeller + * @author Sam Brannen * @since 2.0 */ public class TypePatternClassFilter implements ClassFilter { @@ -113,4 +115,21 @@ private String replaceBooleanOperators(String pcExpr) { result = StringUtils.replace(result, " or ", " || "); return StringUtils.replace(result, " not ", " ! "); } + + @Override + public boolean equals(Object other) { + return (this == other || (other instanceof TypePatternClassFilter && + ObjectUtils.nullSafeEquals(this.typePattern, ((TypePatternClassFilter) other).typePattern))); + } + + @Override + public int hashCode() { + return ObjectUtils.nullSafeHashCode(this.typePattern); + } + + @Override + public String toString() { + return getClass().getName() + ": " + this.typePattern; + } + } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java index c5463b146176..58f3c23b459f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -103,10 +103,11 @@ private boolean compiledByAjc(Class clazz) { @Override public void validate(Class aspectClass) throws AopConfigException { // If the parent has the annotation and isn't abstract it's an error - if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null && - !Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) { + Class superclass = aspectClass.getSuperclass(); + if (superclass.getAnnotation(Aspect.class) != null && + !Modifier.isAbstract(superclass.getModifiers())) { throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" + - aspectClass.getSuperclass().getName() + "]"); + superclass.getName() + "]"); } AjType ajType = AjTypeSystem.getAjType(aspectClass); diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AnnotationAwareAspectJAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AnnotationAwareAspectJAutoProxyCreator.java index fa9c0106d7fc..45ea4983644c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AnnotationAwareAspectJAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AnnotationAwareAspectJAutoProxyCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJAdvisorFactory.java index fa37957a740d..ddba1f0c1f10 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJAdvisorFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java index f1b7c86c58c0..521762fb0faa 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java index 937690c7be3a..048cc603ccc1 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -95,7 +95,7 @@ public AspectMetadata(Class aspectClass, String aspectName) { throw new IllegalArgumentException("Class '" + aspectClass.getName() + "' is not an @AspectJ aspect"); } if (ajType.getDeclarePrecedence().length > 0) { - throw new IllegalArgumentException("DeclarePrecendence not presently supported in Spring AOP"); + throw new IllegalArgumentException("DeclarePrecedence not presently supported in Spring AOP"); } this.aspectClass = ajType.getJavaClass(); this.ajType = ajType; @@ -127,9 +127,9 @@ public AspectMetadata(Class aspectClass, String aspectName) { */ private String findPerClause(Class aspectClass) { String str = aspectClass.getAnnotation(Aspect.class).value(); - str = str.substring(str.indexOf('(') + 1); - str = str.substring(0, str.length() - 1); - return str; + int beginIndex = str.indexOf('(') + 1; + int endIndex = str.length() - 1; + return str.substring(beginIndex, endIndex); } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java index 35edb5fd65f3..d5fefcec81d0 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -56,7 +56,7 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst * introspect to create AJType metadata using the type returned for the * given bean name from the BeanFactory. * @param beanFactory the BeanFactory to obtain instance(s) from - * @param name name of the bean + * @param name the name of the bean */ public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) { this(beanFactory, name, null); diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectJAdvisorsBuilder.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectJAdvisorsBuilder.java index 71ccb63a53bb..8896f990ecbb 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectJAdvisorsBuilder.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectJAdvisorsBuilder.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -97,7 +97,7 @@ public List buildAspectJAdvisors() { } // We must be careful not to instantiate beans eagerly as in this case they // would be cached by the Spring container but would not have been weaved. - Class beanType = this.beanFactory.getType(beanName); + Class beanType = this.beanFactory.getType(beanName, false); if (beanType == null) { continue; } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java index db2c9f400737..7d89175c8c2d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -263,7 +263,7 @@ public String toString() { * Note that this is a dynamic pointcut; otherwise it might be optimized out * if it does not at first match statically. */ - private final class PerTargetInstantiationModelPointcut extends DynamicMethodMatcherPointcut { + private static final class PerTargetInstantiationModelPointcut extends DynamicMethodMatcherPointcut { private final AspectJExpressionPointcut declaredPointcut; diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java index 3a6af19a2ea8..73ba36c79dc3 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java index edf16993aab5..5a67f7fbac93 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/NotAnAtAspectException.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/NotAnAtAspectException.java index 57ca507d6f87..7db2a4cb1eb4 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/NotAnAtAspectException.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/NotAnAtAspectException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java index e1e171557be5..ee295523e2d1 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java index 9c7f6161da1f..5355b2bbb379 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -56,14 +56,15 @@ /** * Factory that can create Spring AOP Advisors given AspectJ classes from - * classes honoring the AspectJ 5 annotation syntax, using reflection to - * invoke the corresponding advice methods. + * classes honoring AspectJ's annotation syntax, using reflection to invoke the + * corresponding advice methods. * * @author Rod Johnson * @author Adrian Colyer * @author Juergen Hoeller * @author Ramnivas Laddad * @author Phillip Webb + * @author Sam Brannen * @since 2.0 */ @SuppressWarnings("serial") @@ -72,13 +73,17 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto private static final Comparator METHOD_COMPARATOR; static { + // Note: although @After is ordered before @AfterReturning and @AfterThrowing, + // an @After advice method will actually be invoked after @AfterReturning and + // @AfterThrowing methods due to the fact that AspectJAfterAdvice.invoke(MethodInvocation) + // invokes proceed() in a `try` block and only invokes the @After advice method + // in a corresponding `finally` block. Comparator adviceKindComparator = new ConvertingComparator<>( new InstanceComparator<>( Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class), (Converter) method -> { - AspectJAnnotation annotation = - AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method); - return (annotation != null ? annotation.getAnnotation() : null); + AspectJAnnotation ann = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method); + return (ann != null ? ann.getAnnotation() : null); }); Comparator methodNameComparator = new ConvertingComparator<>(Method::getName); METHOD_COMPARATOR = adviceKindComparator.thenComparing(methodNameComparator); @@ -123,7 +128,15 @@ public List getAdvisors(MetadataAwareAspectInstanceFactory aspectInstan List advisors = new ArrayList<>(); for (Method method : getAdvisorMethods(aspectClass)) { - Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName); + // Prior to Spring Framework 5.2.7, advisors.size() was supplied as the declarationOrderInAspect + // to getAdvisor(...) to represent the "current position" in the declared methods list. + // However, since Java 7 the "current position" is not valid since the JDK no longer + // returns declared methods in the order in which they are declared in the source code. + // Thus, we now hard code the declarationOrderInAspect to 0 for all advice methods + // discovered via reflection in order to support reliable advice ordering across JVM launches. + // Specifically, a value of 0 aligns with the default value used in + // AspectJPrecedenceComparator.getAspectDeclarationOrder(Advisor). + Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, 0, aspectName); if (advisor != null) { advisors.add(advisor); } @@ -153,8 +166,10 @@ private List getAdvisorMethods(Class aspectClass) { if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) { methods.add(method); } - }); - methods.sort(METHOD_COMPARATOR); + }, ReflectionUtils.USER_DECLARED_METHODS); + if (methods.size() > 1) { + methods.sort(METHOD_COMPARATOR); + } return methods; } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java index 2aa2431af009..386d791130ec 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java index 15edfbbd29ac..4dc30e11310b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java index d6032bf1bcf9..2d2aabd07fae 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -50,27 +50,27 @@ public class AspectJAwareAdvisorAutoProxyCreator extends AbstractAdvisorAutoProx /** - * Sort the rest by AspectJ precedence. If two pieces of advice have - * come from the same aspect they will have the same order. - * Advice from the same aspect is then further ordered according to the + * Sort the supplied {@link Advisor} instances according to AspectJ precedence. + *

If two pieces of advice come from the same aspect, they will have the same + * order. Advice from the same aspect is then further ordered according to the * following rules: *

    - *
  • if either of the pair is after advice, then the advice declared - * last gets highest precedence (runs last)
  • - *
  • otherwise the advice declared first gets highest precedence (runs first)
  • + *
  • If either of the pair is after advice, then the advice declared + * last gets highest precedence (i.e., runs last).
  • + *
  • Otherwise the advice declared first gets highest precedence (i.e., runs + * first).
  • *
*

Important: Advisors are sorted in precedence order, from highest * precedence to lowest. "On the way in" to a join point, the highest precedence - * advisor should run first. "On the way out" of a join point, the highest precedence - * advisor should run last. + * advisor should run first. "On the way out" of a join point, the highest + * precedence advisor should run last. */ @Override - @SuppressWarnings("unchecked") protected List sortAdvisors(List advisors) { List partiallyComparableAdvisors = new ArrayList<>(advisors.size()); - for (Advisor element : advisors) { + for (Advisor advisor : advisors) { partiallyComparableAdvisors.add( - new PartiallyComparableAdvisorHolder(element, DEFAULT_PRECEDENCE_COMPARATOR)); + new PartiallyComparableAdvisorHolder(advisor, DEFAULT_PRECEDENCE_COMPARATOR)); } List sorted = PartialOrder.sort(partiallyComparableAdvisors); if (sorted != null) { @@ -86,8 +86,8 @@ protected List sortAdvisors(List advisors) { } /** - * Adds an {@link ExposeInvocationInterceptor} to the beginning of the advice chain. - * These additional advices are needed when using AspectJ expression pointcuts + * Add an {@link ExposeInvocationInterceptor} to the beginning of the advice chain. + *

This additional advice is needed when using AspectJ pointcut expressions * and when using AspectJ-style advice. */ @Override @@ -110,7 +110,7 @@ protected boolean shouldSkip(Class beanClass, String beanName) { /** - * Implements AspectJ PartialComparable interface for defining partial orderings. + * Implements AspectJ's {@link PartialComparable} interface for defining partial orderings. */ private static class PartiallyComparableAdvisorHolder implements PartialComparable { @@ -140,17 +140,19 @@ public Advisor getAdvisor() { @Override public String toString() { - StringBuilder sb = new StringBuilder(); Advice advice = this.advisor.getAdvice(); - sb.append(ClassUtils.getShortName(advice.getClass())); - sb.append(": "); + StringBuilder sb = new StringBuilder(ClassUtils.getShortName(advice.getClass())); + boolean appended = false; if (this.advisor instanceof Ordered) { - sb.append("order ").append(((Ordered) this.advisor).getOrder()).append(", "); + sb.append(": order = ").append(((Ordered) this.advisor).getOrder()); + appended = true; } if (advice instanceof AbstractAspectJAdvice) { + sb.append(!appended ? ": " : ", "); AbstractAspectJAdvice ajAdvice = (AbstractAspectJAdvice) advice; + sb.append("aspect name = "); sb.append(ajAdvice.getAspectName()); - sb.append(", declaration order "); + sb.append(", declaration order = "); sb.append(ajAdvice.getDeclarationOrder()); } return sb.toString(); diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java index fd9fdf48d3ad..2d243fadc726 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -27,20 +27,22 @@ /** * Orders AspectJ advice/advisors by precedence (not invocation order). * - *

Given two pieces of advice, {@code a} and {@code b}: + *

Given two pieces of advice, {@code A} and {@code B}: *

    - *
  • if {@code a} and {@code b} are defined in different aspects, then the advice - * in the aspect with the lowest order value has the highest precedence
  • - *
  • if {@code a} and {@code b} are defined in the same aspect, then if one of - * {@code a} or {@code b} is a form of after advice, then the advice declared last - * in the aspect has the highest precedence. If neither {@code a} nor {@code b} is - * a form of after advice, then the advice declared first in the aspect has the - * highest precedence.
  • + *
  • If {@code A} and {@code B} are defined in different aspects, then the advice + * in the aspect with the lowest order value has the highest precedence.
  • + *
  • If {@code A} and {@code B} are defined in the same aspect, if one of + * {@code A} or {@code B} is a form of after advice, then the advice declared + * last in the aspect has the highest precedence. If neither {@code A} nor {@code B} + * is a form of after advice, then the advice declared first in the aspect + * has the highest precedence.
  • *
* - *

Important: Note that unlike a normal comparator a return of 0 means - * we don't care about the ordering, not that the two elements must be sorted - * identically. Used with AspectJ PartialOrder class. + *

Important: This comparator is used with AspectJ's + * {@link org.aspectj.util.PartialOrder PartialOrder} sorting utility. Thus, unlike + * a normal {@link Comparator}, a return value of {@code 0} from this comparator + * means we don't care about the ordering, not that the two elements must be sorted + * identically. * * @author Adrian Colyer * @author Juergen Hoeller @@ -59,16 +61,16 @@ class AspectJPrecedenceComparator implements Comparator { /** - * Create a default AspectJPrecedenceComparator. + * Create a default {@code AspectJPrecedenceComparator}. */ public AspectJPrecedenceComparator() { this.advisorComparator = AnnotationAwareOrderComparator.INSTANCE; } /** - * Create a AspectJPrecedenceComparator, using the given Comparator + * Create an {@code AspectJPrecedenceComparator}, using the given {@link Comparator} * for comparing {@link org.springframework.aop.Advisor} instances. - * @param advisorComparator the Comparator to use for Advisors + * @param advisorComparator the {@code Comparator} to use for advisors */ public AspectJPrecedenceComparator(Comparator advisorComparator) { Assert.notNull(advisorComparator, "Advisor comparator must not be null"); @@ -125,27 +127,21 @@ private boolean declaredInSameAspect(Advisor advisor1, Advisor advisor2) { getAspectName(advisor1).equals(getAspectName(advisor2))); } - private boolean hasAspectName(Advisor anAdvisor) { - return (anAdvisor instanceof AspectJPrecedenceInformation || - anAdvisor.getAdvice() instanceof AspectJPrecedenceInformation); + private boolean hasAspectName(Advisor advisor) { + return (advisor instanceof AspectJPrecedenceInformation || + advisor.getAdvice() instanceof AspectJPrecedenceInformation); } // pre-condition is that hasAspectName returned true - private String getAspectName(Advisor anAdvisor) { - AspectJPrecedenceInformation pi = AspectJAopUtils.getAspectJPrecedenceInformationFor(anAdvisor); - Assert.state(pi != null, "Unresolvable precedence information"); - return pi.getAspectName(); + private String getAspectName(Advisor advisor) { + AspectJPrecedenceInformation precedenceInfo = AspectJAopUtils.getAspectJPrecedenceInformationFor(advisor); + Assert.state(precedenceInfo != null, () -> "Unresolvable AspectJPrecedenceInformation for " + advisor); + return precedenceInfo.getAspectName(); } - private int getAspectDeclarationOrder(Advisor anAdvisor) { - AspectJPrecedenceInformation precedenceInfo = - AspectJAopUtils.getAspectJPrecedenceInformationFor(anAdvisor); - if (precedenceInfo != null) { - return precedenceInfo.getDeclarationOrder(); - } - else { - return 0; - } + private int getAspectDeclarationOrder(Advisor advisor) { + AspectJPrecedenceInformation precedenceInfo = AspectJAopUtils.getAspectJPrecedenceInformationFor(advisor); + return (precedenceInfo != null ? precedenceInfo.getDeclarationOrder() : 0); } } diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AbstractInterceptorDrivenBeanDefinitionDecorator.java b/spring-aop/src/main/java/org/springframework/aop/config/AbstractInterceptorDrivenBeanDefinitionDecorator.java index cd6a43f8f53a..52a00db50c96 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AbstractInterceptorDrivenBeanDefinitionDecorator.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AbstractInterceptorDrivenBeanDefinitionDecorator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AdviceEntry.java b/spring-aop/src/main/java/org/springframework/aop/config/AdviceEntry.java index 85e59523a761..7d9b2ad2dc82 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AdviceEntry.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AdviceEntry.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -30,13 +30,14 @@ public class AdviceEntry implements ParseState.Entry { /** - * Creates a new instance of the {@link AdviceEntry} class. - * @param kind the kind of advice represented by this entry (before, after, around, etc.) + * Create a new {@code AdviceEntry} instance. + * @param kind the kind of advice represented by this entry (before, after, around) */ public AdviceEntry(String kind) { this.kind = kind; } + @Override public String toString() { return "Advice (" + this.kind + ")"; diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AdvisorComponentDefinition.java b/spring-aop/src/main/java/org/springframework/aop/config/AdvisorComponentDefinition.java index e812e73abffa..25c8fa2c4d3c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AdvisorComponentDefinition.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AdvisorComponentDefinition.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AdvisorEntry.java b/spring-aop/src/main/java/org/springframework/aop/config/AdvisorEntry.java index 13be36d8f3f1..1a8b45c4823f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AdvisorEntry.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AdvisorEntry.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -30,13 +30,14 @@ public class AdvisorEntry implements ParseState.Entry { /** - * Creates a new instance of the {@link AdvisorEntry} class. + * Create a new {@code AdvisorEntry} instance. * @param name the bean name of the advisor */ public AdvisorEntry(String name) { this.name = name; } + @Override public String toString() { return "Advisor '" + this.name + "'"; diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AopConfigUtils.java b/spring-aop/src/main/java/org/springframework/aop/config/AopConfigUtils.java index 8335dbf1ae5f..1bba8f1c2048 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AopConfigUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AopConfigUtils.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceHandler.java b/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceHandler.java index 6fd259a135a8..fa6cc80a1f3c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceHandler.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceHandler.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -61,12 +61,12 @@ public class AopNamespaceHandler extends NamespaceHandlerSupport { */ @Override public void init() { - // In 2.0 XSD as well as in 2.1 XSD. + // In 2.0 XSD as well as in 2.5+ XSDs registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser()); registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser()); registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator()); - // Only in 2.0 XSD: moved to context namespace as of 2.1 + // Only in 2.0 XSD: moved to context namespace in 2.5+ registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser()); } diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java b/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java index c5d6e6efb0cb..5acb1cc5acd9 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AspectComponentDefinition.java b/spring-aop/src/main/java/org/springframework/aop/config/AspectComponentDefinition.java index c2caea873c82..53d0d789a48d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AspectComponentDefinition.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AspectComponentDefinition.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AspectEntry.java b/spring-aop/src/main/java/org/springframework/aop/config/AspectEntry.java index 10d3271d2905..2d4360048cf7 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AspectEntry.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AspectEntry.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -34,7 +34,7 @@ public class AspectEntry implements ParseState.Entry { /** - * Create a new AspectEntry. + * Create a new {@code AspectEntry} instance. * @param id the id of the aspect element * @param ref the bean name referenced by this aspect element */ @@ -43,6 +43,7 @@ public AspectEntry(String id, String ref) { this.ref = ref; } + @Override public String toString() { return "Aspect: " + (StringUtils.hasLength(this.id) ? "id='" + this.id + "'" : "ref='" + this.ref + "'"); diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AspectJAutoProxyBeanDefinitionParser.java b/spring-aop/src/main/java/org/springframework/aop/config/AspectJAutoProxyBeanDefinitionParser.java index 527f0504fb57..4271bec65d5e 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AspectJAutoProxyBeanDefinitionParser.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AspectJAutoProxyBeanDefinitionParser.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java b/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java index 1bd7eac2900d..de31818c70a6 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java b/spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java index 993bad509e84..ebff6ee73e28 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/PointcutComponentDefinition.java b/spring-aop/src/main/java/org/springframework/aop/config/PointcutComponentDefinition.java index 7b82314e64fd..389a5b4216b8 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/PointcutComponentDefinition.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/PointcutComponentDefinition.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/PointcutEntry.java b/spring-aop/src/main/java/org/springframework/aop/config/PointcutEntry.java index 10f6327e5c1e..e6066c513ee9 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/PointcutEntry.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/PointcutEntry.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -28,14 +28,16 @@ public class PointcutEntry implements ParseState.Entry { private final String name; + /** - * Creates a new instance of the {@link PointcutEntry} class. + * Create a new {@code PointcutEntry} instance. * @param name the bean name of the pointcut */ public PointcutEntry(String name) { this.name = name; } + @Override public String toString() { return "Pointcut '" + this.name + "'"; diff --git a/spring-aop/src/main/java/org/springframework/aop/config/ScopedProxyBeanDefinitionDecorator.java b/spring-aop/src/main/java/org/springframework/aop/config/ScopedProxyBeanDefinitionDecorator.java index 7dce843bf3d1..ca940ec518f1 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/ScopedProxyBeanDefinitionDecorator.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/ScopedProxyBeanDefinitionDecorator.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -45,7 +45,7 @@ public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, if (node instanceof Element) { Element ele = (Element) node; if (ele.hasAttribute(PROXY_TARGET_CLASS)) { - proxyTargetClass = Boolean.valueOf(ele.getAttribute(PROXY_TARGET_CLASS)); + proxyTargetClass = Boolean.parseBoolean(ele.getAttribute(PROXY_TARGET_CLASS)); } } diff --git a/spring-aop/src/main/java/org/springframework/aop/config/SimpleBeanFactoryAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/config/SimpleBeanFactoryAwareAspectInstanceFactory.java index b432821bf364..3d89993caddd 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/SimpleBeanFactoryAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/SimpleBeanFactoryAwareAspectInstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/config/SpringConfiguredBeanDefinitionParser.java b/spring-aop/src/main/java/org/springframework/aop/config/SpringConfiguredBeanDefinitionParser.java index 59aed08e0ed2..3a74eca980f9 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/SpringConfiguredBeanDefinitionParser.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/SpringConfiguredBeanDefinitionParser.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java b/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java index b08de06d5c3d..a3a87f2117f8 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java b/spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java index 6e18786d58b4..bb680a3477b5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -189,7 +189,7 @@ else if (!isProxyTargetClass()) { /** * Determine a TargetSource for the given target (or TargetSource). - * @param target target. If this is an implementation of TargetSource it is + * @param target the target. If this is an implementation of TargetSource it is * used as our TargetSource; otherwise it is wrapped in a SingletonTargetSource. * @return a TargetSource for this object */ diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java b/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java index c946034bdec2..035ae8b30f9a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -142,7 +142,7 @@ public interface Advised extends TargetClassAware { /** * Remove the advisor at the given index. - * @param index index of advisor to remove + * @param index the index of advisor to remove * @throws AopConfigException if the index is invalid */ void removeAdvisor(int index) throws AopConfigException; @@ -177,7 +177,7 @@ public interface Advised extends TargetClassAware { *

Note that the given advice will apply to all invocations on the proxy, * even to the {@code toString()} method! Use appropriate advice implementations * or specify appropriate pointcuts to apply to a narrower set of methods. - * @param advice advice to add to the tail of the chain + * @param advice the advice to add to the tail of the chain * @throws AopConfigException in case of invalid advice * @see #addAdvice(int, Advice) * @see org.springframework.aop.support.DefaultPointcutAdvisor @@ -193,7 +193,7 @@ public interface Advised extends TargetClassAware { * even to the {@code toString()} method! Use appropriate advice implementations * or specify appropriate pointcuts to apply to a narrower set of methods. * @param pos index from 0 (head) - * @param advice advice to add at the specified position in the advice chain + * @param advice the advice to add at the specified position in the advice chain * @throws AopConfigException in case of invalid advice */ void addAdvice(int pos, Advice advice) throws AopConfigException; diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java index 54133402e6fb..b4de26beedfe 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -283,16 +283,15 @@ public void removeAdvisor(int index) throws AopConfigException { "This configuration only has " + this.advisors.size() + " advisors."); } - Advisor advisor = this.advisors.get(index); + Advisor advisor = this.advisors.remove(index); if (advisor instanceof IntroductionAdvisor) { IntroductionAdvisor ia = (IntroductionAdvisor) advisor; // We need to remove introduction interfaces. - for (int j = 0; j < ia.getInterfaces().length; j++) { - removeInterface(ia.getInterfaces()[j]); + for (Class ifc : ia.getInterfaces()) { + removeInterface(ifc); } } - this.advisors.remove(index); updateAdvisorArray(); adviceChanged(); } @@ -592,7 +591,7 @@ public MethodCacheKey(Method method) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { return (this == other || (other instanceof MethodCacheKey && this.method == ((MethodCacheKey) other).method)); } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupportListener.java b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupportListener.java index f7c71d5d356f..c9f4dd733e5a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupportListener.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupportListener.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java index 2640101b04b3..3d31b8c7d481 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AopConfigException.java b/spring-aop/src/main/java/org/springframework/aop/framework/AopConfigException.java index 26a45a46571f..b58b0dd0c042 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AopConfigException.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AopConfigException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AopContext.java b/spring-aop/src/main/java/org/springframework/aop/framework/AopContext.java index 23b992a454e5..9653ced6bc8b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AopContext.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AopContext.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -67,7 +67,8 @@ public static Object currentProxy() throws IllegalStateException { Object proxy = currentProxy.get(); if (proxy == null) { throw new IllegalStateException( - "Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available."); + "Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available, and " + + "ensure that AopContext.currentProxy() is invoked in the same thread as the AOP invocation context."); } return proxy; } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AopInfrastructureBean.java b/spring-aop/src/main/java/org/springframework/aop/framework/AopInfrastructureBean.java index 852d05d2ab84..316833787b1f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AopInfrastructureBean.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AopInfrastructureBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxy.java b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxy.java index c25b4e61c730..cc7a3fd099d7 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxy.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxy.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyFactory.java index 9a2e2298e4f2..6365ee3c0d42 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java index e01ce0b2290a..e417f6ebed6b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -180,8 +180,7 @@ public static Class[] proxiedUserInterfaces(Object proxy) { if (proxy instanceof DecoratingProxy) { nonUserIfcCount++; } - Class[] userInterfaces = new Class[proxyInterfaces.length - nonUserIfcCount]; - System.arraycopy(proxyInterfaces, 0, userInterfaces, 0, userInterfaces.length); + Class[] userInterfaces = Arrays.copyOf(proxyInterfaces, proxyInterfaces.length - nonUserIfcCount); Assert.notEmpty(userInterfaces, "JDK proxy must implement one or more interfaces"); return userInterfaces; } @@ -225,8 +224,8 @@ static Object[] adaptArgumentsIfNecessary(Method method, @Nullable Object[] argu return new Object[0]; } if (method.isVarArgs()) { - Class[] paramTypes = method.getParameterTypes(); - if (paramTypes.length == arguments.length) { + if (method.getParameterCount() == arguments.length) { + Class[] paramTypes = method.getParameterTypes(); int varargIndex = paramTypes.length - 1; Class varargType = paramTypes[varargIndex]; if (varargType.isArray()) { diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java b/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java index fb0644ab686b..f37a4076aec1 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -38,7 +38,7 @@ import org.springframework.aop.RawTargetAccess; import org.springframework.aop.TargetSource; import org.springframework.aop.support.AopUtils; -import org.springframework.cglib.core.ClassGenerator; +import org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy; import org.springframework.cglib.core.CodeGenerationException; import org.springframework.cglib.core.SpringNamingPolicy; import org.springframework.cglib.proxy.Callback; @@ -49,12 +49,13 @@ import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import org.springframework.cglib.proxy.NoOp; -import org.springframework.cglib.transform.impl.UndeclaredThrowableStrategy; +import org.springframework.core.KotlinDetector; import org.springframework.core.SmartClassLoader; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; +import org.springframework.util.ReflectionUtils; /** * CGLIB-based {@link AopProxy} implementation for the Spring AOP framework. @@ -112,7 +113,7 @@ class CglibAopProxy implements AopProxy, Serializable { /** Dispatcher used for methods on Advised. */ private final transient AdvisedDispatcher advisedDispatcher; - private transient Map fixedInterceptorMap = Collections.emptyMap(); + private transient Map fixedInterceptorMap = Collections.emptyMap(); private transient int fixedInterceptorOffset; @@ -166,7 +167,7 @@ public Object getProxy(@Nullable ClassLoader classLoader) { Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy"); Class proxySuperClass = rootClass; - if (ClassUtils.isCglibProxyClass(rootClass)) { + if (rootClass.getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)) { proxySuperClass = rootClass.getSuperclass(); Class[] additionalInterfaces = rootClass.getInterfaces(); for (Class additionalInterface : additionalInterfaces) { @@ -189,7 +190,7 @@ public Object getProxy(@Nullable ClassLoader classLoader) { enhancer.setSuperclass(proxySuperClass); enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised)); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); - enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader)); + enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(classLoader)); Callback[] callbacks = getCallbacks(rootClass); Class[] types = new Class[callbacks.length]; @@ -236,7 +237,7 @@ protected Enhancer createEnhancer() { * validates it if not. */ private void validateClassIfNecessary(Class proxySuperClass, @Nullable ClassLoader proxyClassLoader) { - if (logger.isWarnEnabled()) { + if (logger.isInfoEnabled()) { synchronized (validatedClasses) { if (!validatedClasses.containsKey(proxySuperClass)) { doValidateClass(proxySuperClass, proxyClassLoader, @@ -258,15 +259,17 @@ private void doValidateClass(Class proxySuperClass, @Nullable ClassLoader pro int mod = method.getModifiers(); if (!Modifier.isStatic(mod) && !Modifier.isPrivate(mod)) { if (Modifier.isFinal(mod)) { - if (implementsInterface(method, ifcs)) { + if (logger.isInfoEnabled() && implementsInterface(method, ifcs)) { logger.info("Unable to proxy interface-implementing method [" + method + "] because " + "it is marked as final: Consider using interface-based JDK proxies instead!"); } - logger.debug("Final method [" + method + "] cannot get proxied via CGLIB: " + - "Calls to this method will NOT be routed to the target instance and " + - "might lead to NPEs against uninitialized fields in the proxy instance."); + if (logger.isDebugEnabled()) { + logger.debug("Final method [" + method + "] cannot get proxied via CGLIB: " + + "Calls to this method will NOT be routed to the target instance and " + + "might lead to NPEs against uninitialized fields in the proxy instance."); + } } - else if (!Modifier.isPublic(mod) && !Modifier.isProtected(mod) && + else if (logger.isDebugEnabled() && !Modifier.isPublic(mod) && !Modifier.isProtected(mod) && proxyClassLoader != null && proxySuperClass.getClassLoader() != proxyClassLoader) { logger.debug("Method [" + method + "] is package-visible across different ClassLoaders " + "and cannot get proxied via CGLIB: Declare this method as public or protected " + @@ -327,10 +330,11 @@ private Callback[] getCallbacks(Class rootClass) throws Exception { // TODO: small memory optimization here (can skip creation for methods with no advice) for (int x = 0; x < methods.length; x++) { - List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(methods[x], rootClass); + Method method = methods[x]; + List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, rootClass); fixedCallbacks[x] = new FixedChainStaticTargetInterceptor( chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass()); - this.fixedInterceptorMap.put(methods[x].toString(), x); + this.fixedInterceptorMap.put(method, x); } // Now copy both the callbacks from mainCallbacks @@ -348,7 +352,7 @@ private Callback[] getCallbacks(Class rootClass) throws Exception { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { return (this == other || (other instanceof CglibAopProxy && AopProxyUtils.equalsInProxy(this.advised, ((CglibAopProxy) other).advised))); } @@ -364,7 +368,7 @@ public int hashCode() { */ private static boolean implementsInterface(Method method, Set> ifcs) { for (Class ifc : ifcs) { - if (ClassUtils.hasMethod(ifc, method.getName(), method.getParameterTypes())) { + if (ClassUtils.hasMethod(ifc, method)) { return true; } } @@ -525,7 +529,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy private static class StaticDispatcher implements Dispatcher, Serializable { @Nullable - private Object target; + private final Object target; public StaticDispatcher(@Nullable Object target) { this.target = target; @@ -551,7 +555,7 @@ public AdvisedDispatcher(AdvisedSupport advised) { } @Override - public Object loadObject() throws Exception { + public Object loadObject() { return this.advised; } } @@ -633,8 +637,8 @@ public FixedChainStaticTargetInterceptor( @Override @Nullable public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { - MethodInvocation invocation = new CglibMethodInvocation(proxy, this.target, method, args, - this.targetClass, this.adviceChain, methodProxy); + MethodInvocation invocation = new CglibMethodInvocation( + proxy, this.target, method, args, this.targetClass, this.adviceChain, methodProxy); // If we get here, we need to create a MethodInvocation. Object retVal = invocation.proceed(); retVal = processReturnType(proxy, this.target, method, retVal); @@ -675,13 +679,19 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy Object retVal; // Check whether we only have one InvokerInterceptor: that is, // no real advice, but just reflective invocation of the target. - if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) { + if (chain.isEmpty() && CglibMethodInvocation.isMethodProxyCompatible(method)) { // We can skip creating a MethodInvocation: just invoke the target directly. // Note that the final invoker must be an InvokerInterceptor, so we know // it does nothing but a reflective operation on the target, and no hot // swapping or fancy proxying. Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args); - retVal = methodProxy.invoke(target, argsToUse); + try { + retVal = methodProxy.invoke(target, argsToUse); + } + catch (CodeGenerationException ex) { + CglibMethodInvocation.logFastClassGenerationFailure(method); + retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse); + } } else { // We need to create a method invocation... @@ -702,7 +712,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { return (this == other || (other instanceof DynamicAdvisedInterceptor && this.advised.equals(((DynamicAdvisedInterceptor) other).advised))); @@ -723,17 +733,43 @@ public int hashCode() { */ private static class CglibMethodInvocation extends ReflectiveMethodInvocation { + @Nullable private final MethodProxy methodProxy; - private final boolean publicMethod; - public CglibMethodInvocation(Object proxy, @Nullable Object target, Method method, Object[] arguments, @Nullable Class targetClass, List interceptorsAndDynamicMethodMatchers, MethodProxy methodProxy) { super(proxy, target, method, arguments, targetClass, interceptorsAndDynamicMethodMatchers); - this.methodProxy = methodProxy; - this.publicMethod = Modifier.isPublic(method.getModifiers()); + + // Only use method proxy for public methods not derived from java.lang.Object + this.methodProxy = (isMethodProxyCompatible(method) ? methodProxy : null); + } + + @Override + @Nullable + public Object proceed() throws Throwable { + try { + return super.proceed(); + } + catch (RuntimeException ex) { + throw ex; + } + catch (Exception ex) { + if (ReflectionUtils.declaresException(getMethod(), ex.getClass()) || + KotlinDetector.isKotlinType(getMethod().getDeclaringClass())) { + // Propagate original exception if declared on the target method + // (with callers expecting it). Always propagate it for Kotlin code + // since checked exceptions do not have to be explicitly declared there. + throw ex; + } + else { + // Checked exception thrown in the interceptor but not declared on the + // target method signature -> apply an UndeclaredThrowableException, + // aligned with standard JDK dynamic proxy behavior. + throw new UndeclaredThrowableException(ex); + } + } } /** @@ -742,11 +778,26 @@ public CglibMethodInvocation(Object proxy, @Nullable Object target, Method metho */ @Override protected Object invokeJoinpoint() throws Throwable { - if (this.publicMethod && getMethod().getDeclaringClass() != Object.class) { - return this.methodProxy.invoke(this.target, this.arguments); + if (this.methodProxy != null) { + try { + return this.methodProxy.invoke(this.target, this.arguments); + } + catch (CodeGenerationException ex) { + logFastClassGenerationFailure(this.method); + } } - else { - return super.invokeJoinpoint(); + return super.invokeJoinpoint(); + } + + static boolean isMethodProxyCompatible(Method method) { + return (Modifier.isPublic(method.getModifiers()) && + method.getDeclaringClass() != Object.class && !AopUtils.isEqualsMethod(method) && + !AopUtils.isHashCodeMethod(method) && !AopUtils.isToStringMethod(method)); + } + + static void logFastClassGenerationFailure(Method method) { + if (logger.isDebugEnabled()) { + logger.debug("Failed to generate CGLIB fast class for method: " + method); } } } @@ -759,12 +810,12 @@ private static class ProxyCallbackFilter implements CallbackFilter { private final AdvisedSupport advised; - private final Map fixedInterceptorMap; + private final Map fixedInterceptorMap; private final int fixedInterceptorOffset; public ProxyCallbackFilter( - AdvisedSupport advised, Map fixedInterceptorMap, int fixedInterceptorOffset) { + AdvisedSupport advised, Map fixedInterceptorMap, int fixedInterceptorOffset) { this.advised = advised; this.fixedInterceptorMap = fixedInterceptorMap; @@ -795,7 +846,7 @@ public ProxyCallbackFilter( *
For advised methods:
*
If the target is static and the advice chain is frozen then a * FixedChainStaticTargetInterceptor specific to the method is used to - * invoke the advice chain. Otherwise a DyanmicAdvisedInterceptor is + * invoke the advice chain. Otherwise a DynamicAdvisedInterceptor is * used.
*
For non-advised methods:
*
Where it can be determined that the method will not return {@code this} @@ -849,15 +900,14 @@ public int accept(Method method) { } return AOP_PROXY; } - String key = method.toString(); // Check to see if we have fixed interceptor to serve this method. // Else use the AOP_PROXY. - if (isStatic && isFrozen && this.fixedInterceptorMap.containsKey(key)) { + if (isStatic && isFrozen && this.fixedInterceptorMap.containsKey(method)) { if (logger.isTraceEnabled()) { logger.trace("Method has advice and optimizations are enabled: " + method); } // We know that we are optimizing so we can use the FixedStaticChainInterceptors. - int index = this.fixedInterceptorMap.get(key); + int index = this.fixedInterceptorMap.get(method); return (index + this.fixedInterceptorOffset); } else { @@ -895,7 +945,7 @@ public int accept(Method method) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -936,11 +986,11 @@ public boolean equals(Object other) { return true; } - private boolean equalsAdviceClasses(Advisor a, Advisor b) { + private static boolean equalsAdviceClasses(Advisor a, Advisor b) { return (a.getAdvice().getClass() == b.getAdvice().getClass()); } - private boolean equalsPointcuts(Advisor a, Advisor b) { + private static boolean equalsPointcuts(Advisor a, Advisor b) { // If only one of the advisor (but not both) is PointcutAdvisor, then it is a mismatch. // Takes care of the situations where an IntroductionAdvisor is used (see SPR-3959). return (!(a instanceof PointcutAdvisor) || @@ -964,52 +1014,4 @@ public int hashCode() { } } - - /** - * CGLIB GeneratorStrategy variant which exposes the application ClassLoader - * as thread context ClassLoader for the time of class generation - * (in order for ASM to pick it up when doing common superclass resolution). - */ - private static class ClassLoaderAwareUndeclaredThrowableStrategy extends UndeclaredThrowableStrategy { - - @Nullable - private final ClassLoader classLoader; - - public ClassLoaderAwareUndeclaredThrowableStrategy(@Nullable ClassLoader classLoader) { - super(UndeclaredThrowableException.class); - this.classLoader = classLoader; - } - - @Override - public byte[] generate(ClassGenerator cg) throws Exception { - if (this.classLoader == null) { - return super.generate(cg); - } - - Thread currentThread = Thread.currentThread(); - ClassLoader threadContextClassLoader; - try { - threadContextClassLoader = currentThread.getContextClassLoader(); - } - catch (Throwable ex) { - // Cannot access thread context ClassLoader - falling back... - return super.generate(cg); - } - - boolean overrideClassLoader = !this.classLoader.equals(threadContextClassLoader); - if (overrideClassLoader) { - currentThread.setContextClassLoader(this.classLoader); - } - try { - return super.generate(cg); - } - finally { - if (overrideClassLoader) { - // Reset original thread context ClassLoader. - currentThread.setContextClassLoader(threadContextClassLoader); - } - } - } - } - } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java index 31438c78f878..a8f458781d65 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java index 8f46a212fd85..0a072316f4cc 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -43,9 +43,11 @@ * @see AdvisedSupport#setProxyTargetClass * @see AdvisedSupport#setInterfaces */ -@SuppressWarnings("serial") public class DefaultAopProxyFactory implements AopProxyFactory, Serializable { + private static final long serialVersionUID = 7930414337282325166L; + + @Override public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/InterceptorAndDynamicMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/framework/InterceptorAndDynamicMethodMatcher.java index 0a0e1233871d..79f5101f0916 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/InterceptorAndDynamicMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/InterceptorAndDynamicMethodMatcher.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java b/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java index f31cd878e6b0..43223de5a2bd 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -154,7 +154,6 @@ private void findDefinedEqualsAndHashCodeMethods(Class[] proxiedInterfaces) { @Override @Nullable public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - MethodInvocation invocation; Object oldProxy = null; boolean setProxyContext = false; @@ -207,7 +206,8 @@ else if (!this.advised.opaque && method.getDeclaringClass().isInterface() && } else { // We need to create a method invocation... - invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); + MethodInvocation invocation = + new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // Proceed to the joinpoint through the interceptor chain. retVal = invocation.proceed(); } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java b/spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java index 89e47fe29530..ba31e39fe263 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -29,7 +29,7 @@ /** * Objenesis-based extension of {@link CglibAopProxy} to create proxy instances - * without invoking the constructor of the class. + * without invoking the constructor of the class. Used by default as of Spring 4. * * @author Oliver Gierke * @author Juergen Hoeller @@ -53,7 +53,6 @@ public ObjenesisCglibAopProxy(AdvisedSupport config) { @Override - @SuppressWarnings("unchecked") protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) { Class proxyClass = enhancer.createClass(); Object proxyInstance = null; diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyConfig.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyConfig.java index 9847b0de4ed3..38665cafbc61 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyConfig.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyConfig.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java index 59ae03651c14..1295af52b346 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactory.java index 4da126ec44e1..f4b6208e44fe 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java index b91134a2bb5a..5e1ed1603fa6 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -21,9 +21,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.aopalliance.aop.Advice; import org.aopalliance.intercept.Interceptor; @@ -342,11 +340,8 @@ private synchronized Object newPrototypeInstance() { // an independent instance of the configuration. // In this case, no proxy will have an instance of this object's configuration, // but will have an independent copy. - if (logger.isTraceEnabled()) { - logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this); - } - ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory()); + // The copy needs a fresh advisor chain, and a fresh TargetSource. TargetSource targetSource = freshTargetSource(); copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain()); @@ -359,9 +354,6 @@ private synchronized Object newPrototypeInstance() { } copy.setFrozen(this.freezeProxy); - if (logger.isTraceEnabled()) { - logger.trace("Using ProxyCreatorSupport copy: " + copy); - } return getProxy(copy.createAopProxy()); } @@ -395,9 +387,7 @@ private void checkInterceptorNames() { logger.debug("Bean with name '" + finalName + "' concluding interceptor chain " + "is not an advisor class: treating it as a target or TargetSource"); } - String[] newNames = new String[this.interceptorNames.length - 1]; - System.arraycopy(this.interceptorNames, 0, newNames, 0, newNames.length); - this.interceptorNames = newNames; + this.interceptorNames = Arrays.copyOf(this.interceptorNames, this.interceptorNames.length - 1); } } } @@ -431,11 +421,7 @@ private boolean isNamedBeanAnAdvisorOrAdvice(String beanName) { * are unaffected by such changes. */ private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException { - if (this.advisorChainInitialized) { - return; - } - - if (!ObjectUtils.isEmpty(this.interceptorNames)) { + if (!this.advisorChainInitialized && !ObjectUtils.isEmpty(this.interceptorNames)) { if (this.beanFactory == null) { throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " + "- cannot resolve interceptor names " + Arrays.asList(this.interceptorNames)); @@ -449,16 +435,12 @@ private synchronized void initializeAdvisorChain() throws AopConfigException, Be // Materialize interceptor chain from bean names. for (String name : this.interceptorNames) { - if (logger.isTraceEnabled()) { - logger.trace("Configuring advisor or advice '" + name + "'"); - } - if (name.endsWith(GLOBAL_SUFFIX)) { if (!(this.beanFactory instanceof ListableBeanFactory)) { throw new AopConfigException( "Can only use global advisors or interceptors with a ListableBeanFactory"); } - addGlobalAdvisor((ListableBeanFactory) this.beanFactory, + addGlobalAdvisors((ListableBeanFactory) this.beanFactory, name.substring(0, name.length() - GLOBAL_SUFFIX.length())); } @@ -475,12 +457,12 @@ private synchronized void initializeAdvisorChain() throws AopConfigException, Be // Avoid unnecessary creation of prototype bean just for advisor chain initialization. advice = new PrototypePlaceholderAdvisor(name); } - addAdvisorOnChainCreation(advice, name); + addAdvisorOnChainCreation(advice); } } - } - this.advisorChainInitialized = true; + this.advisorChainInitialized = true; + } } @@ -498,11 +480,10 @@ private List freshAdvisorChain() { if (logger.isDebugEnabled()) { logger.debug("Refreshing bean named '" + pa.getBeanName() + "'"); } - // Replace the placeholder with a fresh prototype instance resulting - // from a getBean() lookup + // Replace the placeholder with a fresh prototype instance resulting from a getBean lookup if (this.beanFactory == null) { - throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " + - "- cannot resolve prototype advisor '" + pa.getBeanName() + "'"); + throw new IllegalStateException("No BeanFactory available anymore (probably due to " + + "serialization) - cannot resolve prototype advisor '" + pa.getBeanName() + "'"); } Object bean = this.beanFactory.getBean(pa.getBeanName()); Advisor refreshedAdvisor = namedBeanToAdvisor(bean); @@ -519,28 +500,26 @@ private List freshAdvisorChain() { /** * Add all global interceptors and pointcuts. */ - private void addGlobalAdvisor(ListableBeanFactory beanFactory, String prefix) { + private void addGlobalAdvisors(ListableBeanFactory beanFactory, String prefix) { String[] globalAdvisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Advisor.class); String[] globalInterceptorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Interceptor.class); - List beans = new ArrayList<>(globalAdvisorNames.length + globalInterceptorNames.length); - Map names = new HashMap<>(beans.size()); - for (String name : globalAdvisorNames) { - Object bean = beanFactory.getBean(name); - beans.add(bean); - names.put(bean, name); - } - for (String name : globalInterceptorNames) { - Object bean = beanFactory.getBean(name); - beans.add(bean); - names.put(bean, name); - } - AnnotationAwareOrderComparator.sort(beans); - for (Object bean : beans) { - String name = names.get(bean); - if (name.startsWith(prefix)) { - addAdvisorOnChainCreation(bean, name); + if (globalAdvisorNames.length > 0 || globalInterceptorNames.length > 0) { + List beans = new ArrayList<>(globalAdvisorNames.length + globalInterceptorNames.length); + for (String name : globalAdvisorNames) { + if (name.startsWith(prefix)) { + beans.add(beanFactory.getBean(name)); + } + } + for (String name : globalInterceptorNames) { + if (name.startsWith(prefix)) { + beans.add(beanFactory.getBean(name)); + } + } + AnnotationAwareOrderComparator.sort(beans); + for (Object bean : beans) { + addAdvisorOnChainCreation(bean); } } } @@ -551,17 +530,11 @@ private void addGlobalAdvisor(ListableBeanFactory beanFactory, String prefix) { * Because of these three possibilities, we can't type the signature * more strongly. * @param next advice, advisor or target object - * @param name bean name from which we obtained this object in our owning - * bean factory */ - private void addAdvisorOnChainCreation(Object next, String name) { + private void addAdvisorOnChainCreation(Object next) { // We need to convert to an Advisor if necessary so that our source reference // matches what we find from superclass interceptors. - Advisor advisor = namedBeanToAdvisor(next); - if (logger.isTraceEnabled()) { - logger.trace("Adding advisor with name '" + name + "'"); - } - addAdvisor(advisor); + addAdvisor(namedBeanToAdvisor(next)); } /** @@ -572,9 +545,7 @@ private void addAdvisorOnChainCreation(Object next, String name) { */ private TargetSource freshTargetSource() { if (this.targetName == null) { - if (logger.isTraceEnabled()) { - logger.trace("Not refreshing target: Bean name not specified in 'interceptorNames'."); - } + // Not refreshing target: bean name not specified in 'interceptorNames' return this.targetSource; } else { @@ -602,8 +573,8 @@ private Advisor namedBeanToAdvisor(Object next) { // We expected this to be an Advisor or Advice, // but it wasn't. This is a configuration error. throw new AopConfigException("Unknown advisor type " + next.getClass() + - "; Can only include Advisor or Advice type beans in interceptorNames chain except for last entry," + - "which may also be target or TargetSource", ex); + "; can only include Advisor or Advice type beans in interceptorNames chain " + + "except for last entry which may also be target instance or TargetSource", ex); } } @@ -614,7 +585,7 @@ private Advisor namedBeanToAdvisor(Object next) { protected void adviceChanged() { super.adviceChanged(); if (this.singleton) { - logger.debug("Advice has changed; recaching singleton instance"); + logger.debug("Advice has changed; re-caching singleton instance"); synchronized (this) { this.singletonInstance = null; } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyProcessorSupport.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyProcessorSupport.java index 71cf05e63428..f58e0be379f3 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyProcessorSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyProcessorSupport.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java b/spring-aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java index 3a2134fb4fae..1db34223cb41 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -68,7 +68,7 @@ public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Clonea protected final Method method; - protected Object[] arguments = new Object[0]; + protected Object[] arguments; @Nullable private final Class targetClass; @@ -158,7 +158,7 @@ public void setArguments(Object... arguments) { @Override @Nullable public Object proceed() throws Throwable { - // We start with an index of -1 and increment early. + // We start with an index of -1 and increment early. if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { return invokeJoinpoint(); } @@ -212,8 +212,7 @@ public MethodInvocation invocableClone() { Object[] cloneArguments = this.arguments; if (this.arguments.length > 0) { // Build an independent copy of the arguments array. - cloneArguments = new Object[this.arguments.length]; - System.arraycopy(this.arguments, 0, cloneArguments, 0, this.arguments.length); + cloneArguments = this.arguments.clone(); } return invocableClone(cloneArguments); } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapter.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapter.java index 425c3f87c4c8..d717bbf19f6f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapter.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapter.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistrationManager.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistrationManager.java index 4ef00db86c07..c9a4af847871 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistrationManager.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistrationManager.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistry.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistry.java index 4183ec8c4fea..5a9fb9947a88 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistry.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistry.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceAdapter.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceAdapter.java index de562f4fbedb..ba4b049e2bc4 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceAdapter.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceAdapter.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceInterceptor.java index 82e5856250b6..3e58109be18b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java index ebdd27c514f8..7f2c66144b66 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/GlobalAdvisorAdapterRegistry.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/GlobalAdvisorAdapterRegistry.java index f9a677c5ad6a..705fe9467e86 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/GlobalAdvisorAdapterRegistry.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/GlobalAdvisorAdapterRegistry.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceAdapter.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceAdapter.java index 57e0e16af7a2..7cd7262aec9a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceAdapter.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceAdapter.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceInterceptor.java index a58e27cdc366..342de103da46 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceInterceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -26,9 +26,9 @@ import org.springframework.util.Assert; /** - * Interceptor to wrap am {@link org.springframework.aop.MethodBeforeAdvice}. - * Used internally by the AOP framework; application developers should not need - * to use this class directly. + * Interceptor to wrap a {@link MethodBeforeAdvice}. + *

Used internally by the AOP framework; application developers should not + * need to use this class directly. * * @author Rod Johnson * @see AfterReturningAdviceInterceptor diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceAdapter.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceAdapter.java index 822b789b540d..dd557215c563 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceAdapter.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceAdapter.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptor.java index bea87cfd68f3..541e3a32110b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/UnknownAdviceTypeException.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/UnknownAdviceTypeException.java index e95e513caaab..1f09b8e52ec6 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/UnknownAdviceTypeException.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/UnknownAdviceTypeException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java index 7e6db639550e..4900c3d4657c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -31,16 +31,18 @@ * Generic auto proxy creator that builds AOP proxies for specific beans * based on detected Advisors for each bean. * - *

Subclasses must implement the abstract {@link #findCandidateAdvisors()} - * method to return a list of Advisors applying to any object. Subclasses can + *

Subclasses may override the {@link #findCandidateAdvisors()} method to + * return a custom list of Advisors applying to any object. Subclasses can * also override the inherited {@link #shouldSkip} method to exclude certain * objects from auto-proxying. * - *

Advisors or advices requiring ordering should implement the + *

Advisors or advices requiring ordering should be annotated with + * {@link org.springframework.core.annotation.Order @Order} or implement the * {@link org.springframework.core.Ordered} interface. This class sorts - * Advisors by Ordered order value. Advisors that don't implement the - * Ordered interface will be considered as unordered; they will appear - * at the end of the advisor chain in undefined order. + * advisors using the {@link AnnotationAwareOrderComparator}. Advisors that are + * not annotated with {@code @Order} or don't implement the {@code Ordered} + * interface will be considered as unordered; they will appear at the end of the + * advisor chain in an undefined order. * * @author Rod Johnson * @author Juergen Hoeller diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java index 05eb21780391..2a63c676b7c5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.aop.framework.autoproxy; import java.lang.reflect.Constructor; +import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -133,7 +134,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport private final Set targetSourcedBeans = Collections.newSetFromMap(new ConcurrentHashMap<>(16)); - private final Set earlyProxyReferences = Collections.newSetFromMap(new ConcurrentHashMap<>(16)); + private final Map earlyProxyReferences = new ConcurrentHashMap<>(16); private final Map> proxyTypes = new ConcurrentHashMap<>(16); @@ -236,9 +237,7 @@ public Constructor[] determineCandidateConstructors(Class beanClass, Strin @Override public Object getEarlyBeanReference(Object bean, String beanName) { Object cacheKey = getCacheKey(bean.getClass(), beanName); - if (!this.earlyProxyReferences.contains(cacheKey)) { - this.earlyProxyReferences.add(cacheKey); - } + this.earlyProxyReferences.put(cacheKey, bean); return wrapIfNecessary(bean, beanName, cacheKey); } @@ -297,7 +296,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) { public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) { if (bean != null) { Object cacheKey = getCacheKey(bean.getClass(), beanName); - if (!this.earlyProxyReferences.contains(cacheKey)) { + if (this.earlyProxyReferences.remove(cacheKey) != bean) { return wrapIfNecessary(bean, beanName, cacheKey); } } @@ -451,7 +450,17 @@ protected Object createProxy(Class beanClass, @Nullable String beanName, ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.copyFrom(this); - if (!proxyFactory.isProxyTargetClass()) { + if (proxyFactory.isProxyTargetClass()) { + // Explicit handling of JDK proxy targets (for introduction advice scenarios) + if (Proxy.isProxyClass(beanClass)) { + // Must allow for introductions; can't just set interfaces to the proxy's interfaces only. + for (Class ifc : beanClass.getInterfaces()) { + proxyFactory.addInterface(ifc); + } + } + } + else { + // No proxyTargetClass flag enforced, let's apply our default checks... if (shouldProxyTargetClass(beanClass, beanName)) { proxyFactory.setProxyTargetClass(true); } @@ -515,7 +524,10 @@ protected Advisor[] buildAdvisors(@Nullable String beanName, @Nullable Object[] List allInterceptors = new ArrayList<>(); if (specificInterceptors != null) { - allInterceptors.addAll(Arrays.asList(specificInterceptors)); + if (specificInterceptors.length > 0) { + // specificInterceptors may equals PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS + allInterceptors.addAll(Arrays.asList(specificInterceptors)); + } if (commonInterceptors.length > 0) { if (this.applyCommonInterceptorsFirst) { allInterceptors.addAll(0, Arrays.asList(commonInterceptors)); diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractBeanFactoryAwareAdvisingPostProcessor.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractBeanFactoryAwareAdvisingPostProcessor.java index b8a90d052aba..280eb9b23a6b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractBeanFactoryAwareAdvisingPostProcessor.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractBeanFactoryAwareAdvisingPostProcessor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AutoProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AutoProxyUtils.java index d90552b91057..1de9382a2e2c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AutoProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AutoProxyUtils.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanFactoryAdvisorRetrievalHelper.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanFactoryAdvisorRetrievalHelper.java index 291fa91083bd..4542f117ab5e 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanFactoryAdvisorRetrievalHelper.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanFactoryAdvisorRetrievalHelper.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanNameAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanNameAutoProxyCreator.java index b6bce882afc7..cc2b16101dca 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanNameAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanNameAutoProxyCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/DefaultAdvisorAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/DefaultAdvisorAutoProxyCreator.java index d41a348ac611..07aff4a3f91d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/DefaultAdvisorAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/DefaultAdvisorAutoProxyCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/InfrastructureAdvisorAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/InfrastructureAdvisorAutoProxyCreator.java index 5d4c555a1f53..f283920fca76 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/InfrastructureAdvisorAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/InfrastructureAdvisorAutoProxyCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/ProxyCreationContext.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/ProxyCreationContext.java index d2345e9b2822..314fcc98f236 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/ProxyCreationContext.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/ProxyCreationContext.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/TargetSourceCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/TargetSourceCreator.java index d06933f55073..012c060e98d5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/TargetSourceCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/TargetSourceCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/AbstractBeanFactoryBasedTargetSourceCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/AbstractBeanFactoryBasedTargetSourceCreator.java index d161fa2ddf69..4fd173f2893e 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/AbstractBeanFactoryBasedTargetSourceCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/AbstractBeanFactoryBasedTargetSourceCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/LazyInitTargetSourceCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/LazyInitTargetSourceCreator.java index 47872f6cadab..5761068e8aa2 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/LazyInitTargetSourceCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/LazyInitTargetSourceCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/QuickTargetSourceCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/QuickTargetSourceCreator.java index 8ddcba030d9f..a6a741e0f879 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/QuickTargetSourceCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/QuickTargetSourceCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractMonitoringInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractMonitoringInterceptor.java index 008edb2b2844..536e6e3ade39 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractMonitoringInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractMonitoringInterceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -98,16 +98,13 @@ public void setLogTargetClassInvocation(boolean logTargetClassInvocation) { * @see #setSuffix */ protected String createInvocationTraceName(MethodInvocation invocation) { - StringBuilder sb = new StringBuilder(getPrefix()); Method method = invocation.getMethod(); Class clazz = method.getDeclaringClass(); if (this.logTargetClassInvocation && clazz.isInstance(invocation.getThis())) { clazz = invocation.getThis().getClass(); } - sb.append(clazz.getName()); - sb.append('.').append(method.getName()); - sb.append(getSuffix()); - return sb.toString(); + String className = clazz.getName(); + return getPrefix() + className + '.' + method.getName() + getSuffix(); } } diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractTraceInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractTraceInterceptor.java index ce6c7f264287..dd02d2d8ef63 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractTraceInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractTraceInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -124,6 +124,7 @@ public void setLogExceptionStackTrace(boolean logExceptionStackTrace) { * @see #invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log) */ @Override + @Nullable public Object invoke(MethodInvocation invocation) throws Throwable { Log logger = getLoggerForInvocation(invocation); if (isInterceptorEnabled(invocation, logger)) { @@ -242,6 +243,7 @@ protected void writeToLog(Log logger, String message, @Nullable Throwable ex) { * @see #writeToLog(Log, String) * @see #writeToLog(Log, String, Throwable) */ + @Nullable protected abstract Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable; } diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java index 7eef59f1b8a9..8908cab491d2 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionInterceptor.java index c458723954ff..8d97ad311c70 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncUncaughtExceptionHandler.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncUncaughtExceptionHandler.java index 2914e53ef5dc..a3d7d850eb63 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncUncaughtExceptionHandler.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncUncaughtExceptionHandler.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,7 +23,7 @@ * *

An asynchronous method usually returns a {@link java.util.concurrent.Future} * instance that gives access to the underlying exception. When the method does - * not provide that return type, this handler can be used to managed such + * not provide that return type, this handler can be used to manage such * uncaught exceptions. * * @author Stephane Nicoll diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptor.java index ad57e4c6359d..09e3fa800cd2 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java index b6318c2e4b2b..e6008ee001ea 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -79,7 +79,7 @@ public class CustomizableTraceInterceptor extends AbstractTraceInterceptor { /** * The {@code $[targetClassName]} placeholder. - * Replaced with the fully-qualifed name of the {@code Class} + * Replaced with the fully-qualified name of the {@code Class} * of the method invocation target. */ public static final String PLACEHOLDER_TARGET_CLASS_NAME = "$[targetClassName]"; @@ -147,7 +147,7 @@ public class CustomizableTraceInterceptor extends AbstractTraceInterceptor { /** * The {@code Pattern} used to match placeholders. */ - private static final Pattern PATTERN = Pattern.compile("\\$\\[\\p{Alpha}+\\]"); + private static final Pattern PATTERN = Pattern.compile("\\$\\[\\p{Alpha}+]"); /** * The {@code Set} of allowed placeholders. diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/DebugInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/DebugInterceptor.java index 0a2c2ec8e942..cc7733152031 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/DebugInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/DebugInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java index e12a20d0ff66..351d6940116d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeInvocationInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeInvocationInterceptor.java index 8770607af2d8..6c14533b3959 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeInvocationInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeInvocationInterceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -71,9 +71,11 @@ public static MethodInvocation currentInvocation() throws IllegalStateException MethodInvocation mi = invocation.get(); if (mi == null) { throw new IllegalStateException( - "No MethodInvocation found: Check that an AOP invocation is in progress, and that the " + + "No MethodInvocation found: Check that an AOP invocation is in progress and that the " + "ExposeInvocationInterceptor is upfront in the interceptor chain. Specifically, note that " + - "advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor!"); + "advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor! " + + "In addition, ExposeInvocationInterceptor and ExposeInvocationInterceptor.currentInvocation() " + + "must be invoked from the same thread."); } return mi; } diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptor.java index 2b71b3cbbd05..0476a4f3c7a1 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptor.java index dd72c00f357c..6a0e5315feb2 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleAsyncUncaughtExceptionHandler.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleAsyncUncaughtExceptionHandler.java index 0197d0ddd614..d11f0d90d821 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleAsyncUncaughtExceptionHandler.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleAsyncUncaughtExceptionHandler.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleTraceInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleTraceInterceptor.java index 8ca8baa8aef5..87d6186d2914 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleTraceInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleTraceInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -73,8 +73,8 @@ protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throw * @return the description */ protected String getInvocationDescription(MethodInvocation invocation) { - return "method '" + invocation.getMethod().getName() + "' of class [" + - invocation.getThis().getClass().getName() + "]"; + String className = invocation.getThis().getClass().getName(); + return "method '" + invocation.getMethod().getName() + "' of class [" + className + "]"; } } diff --git a/spring-aop/src/main/java/org/springframework/aop/scope/DefaultScopedObject.java b/spring-aop/src/main/java/org/springframework/aop/scope/DefaultScopedObject.java index 337c660022c6..302cd9e995ca 100644 --- a/spring-aop/src/main/java/org/springframework/aop/scope/DefaultScopedObject.java +++ b/spring-aop/src/main/java/org/springframework/aop/scope/DefaultScopedObject.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedObject.java b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedObject.java index 4b7046e35958..ff5edbb9faef 100644 --- a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedObject.java +++ b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedObject.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java index c2e6f14bb4ee..281b975a4c8c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java +++ b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java index 83dbff299abb..e1899cf8e5bc 100644 --- a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,20 +23,25 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** * Utility class for creating a scoped proxy. - * Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner. + * + *

Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner. * * @author Mark Fisher * @author Juergen Hoeller * @author Rob Harrop + * @author Sam Brannen * @since 2.5 */ public abstract class ScopedProxyUtils { private static final String TARGET_NAME_PREFIX = "scopedTarget."; + private static final int TARGET_NAME_PREFIX_LENGTH = TARGET_NAME_PREFIX.length(); + /** * Generate a scoped proxy for the supplied target bean, registering the target @@ -45,6 +50,8 @@ public abstract class ScopedProxyUtils { * @param registry the bean definition registry * @param proxyTargetClass whether to create a target class proxy * @return the scoped proxy definition + * @see #getTargetBeanName(String) + * @see #getOriginalBeanName(String) */ public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition, BeanDefinitionRegistry registry, boolean proxyTargetClass) { @@ -93,14 +100,32 @@ public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder defini * Generate the bean name that is used within the scoped proxy to reference the target bean. * @param originalBeanName the original name of bean * @return the generated bean to be used to reference the target bean + * @see #getOriginalBeanName(String) */ public static String getTargetBeanName(String originalBeanName) { return TARGET_NAME_PREFIX + originalBeanName; } /** - * Specify if the {@code beanName} is the name of a bean that references the target - * bean within a scoped proxy. + * Get the original bean name for the provided {@linkplain #getTargetBeanName + * target bean name}. + * @param targetBeanName the target bean name for the scoped proxy + * @return the original bean name + * @throws IllegalArgumentException if the supplied bean name does not refer + * to the target of a scoped proxy + * @since 5.1.10 + * @see #getTargetBeanName(String) + * @see #isScopedTarget(String) + */ + public static String getOriginalBeanName(@Nullable String targetBeanName) { + Assert.isTrue(isScopedTarget(targetBeanName), () -> "bean name '" + + targetBeanName + "' does not refer to the target of a scoped proxy"); + return targetBeanName.substring(TARGET_NAME_PREFIX_LENGTH); + } + + /** + * Determine if the {@code beanName} is the name of a bean that references + * the target bean within a scoped proxy. * @since 4.1.4 */ public static boolean isScopedTarget(@Nullable String beanName) { diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AbstractBeanFactoryPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/AbstractBeanFactoryPointcutAdvisor.java index 1a390b26c7fc..ac69e3962b06 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AbstractBeanFactoryPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AbstractBeanFactoryPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AbstractExpressionPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/AbstractExpressionPointcut.java index 2555c4cf1099..5330f2c00d64 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AbstractExpressionPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AbstractExpressionPointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -78,7 +78,7 @@ public void setExpression(@Nullable String expression) { * Called when a new pointcut expression is set. * The expression should be parsed at this point if possible. *

This implementation is empty. - * @param expression expression to set + * @param expression the expression to set * @throws IllegalArgumentException if the expression is invalid * @see #setExpression */ diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AbstractGenericPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/AbstractGenericPointcutAdvisor.java index a13444e72cef..62432231ffe5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AbstractGenericPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AbstractGenericPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java index 46bc77571812..7b7c95a8ac5e 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -65,7 +65,7 @@ public boolean isPerInstance() { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AbstractRegexpMethodPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/AbstractRegexpMethodPointcut.java index d96f65f6e0ac..a6ca47c15c37 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AbstractRegexpMethodPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AbstractRegexpMethodPointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -20,6 +20,7 @@ import java.lang.reflect.Method; import java.util.Arrays; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; @@ -195,7 +196,7 @@ protected boolean matchesPattern(String signatureString) { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AopUtils.java b/spring-aop/src/main/java/org/springframework/aop/support/AopUtils.java index 83358da1594b..8055ec98cb60 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AopUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AopUtils.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -66,8 +66,8 @@ public abstract class AopUtils { * @see #isCglibProxy */ public static boolean isAopProxy(@Nullable Object object) { - return (object instanceof SpringProxy && - (Proxy.isProxyClass(object.getClass()) || ClassUtils.isCglibProxyClass(object.getClass()))); + return (object instanceof SpringProxy && (Proxy.isProxyClass(object.getClass()) || + object.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR))); } /** @@ -91,7 +91,8 @@ public static boolean isJdkDynamicProxy(@Nullable Object object) { * @see ClassUtils#isCglibProxy(Object) */ public static boolean isCglibProxy(@Nullable Object object) { - return (object instanceof SpringProxy && ClassUtils.isCglibProxy(object)); + return (object instanceof SpringProxy && + object.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)); } /** diff --git a/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java b/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java index c58a5bc41a51..6f624ca14b7d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,8 +17,10 @@ package org.springframework.aop.support; import java.io.Serializable; +import java.util.Arrays; import org.springframework.aop.ClassFilter; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -28,6 +30,7 @@ * @author Rod Johnson * @author Rob Harrop * @author Juergen Hoeller + * @author Sam Brannen * @since 11.11.2003 * @see MethodMatchers * @see Pointcuts @@ -89,9 +92,9 @@ public static ClassFilter intersection(ClassFilter[] classFilters) { @SuppressWarnings("serial") private static class UnionClassFilter implements ClassFilter, Serializable { - private ClassFilter[] filters; + private final ClassFilter[] filters; - public UnionClassFilter(ClassFilter[] filters) { + UnionClassFilter(ClassFilter[] filters) { this.filters = filters; } @@ -106,7 +109,7 @@ public boolean matches(Class clazz) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { return (this == other || (other instanceof UnionClassFilter && ObjectUtils.nullSafeEquals(this.filters, ((UnionClassFilter) other).filters))); } @@ -115,6 +118,12 @@ public boolean equals(Object other) { public int hashCode() { return ObjectUtils.nullSafeHashCode(this.filters); } + + @Override + public String toString() { + return getClass().getName() + ": " + Arrays.toString(this.filters); + } + } @@ -124,9 +133,9 @@ public int hashCode() { @SuppressWarnings("serial") private static class IntersectionClassFilter implements ClassFilter, Serializable { - private ClassFilter[] filters; + private final ClassFilter[] filters; - public IntersectionClassFilter(ClassFilter[] filters) { + IntersectionClassFilter(ClassFilter[] filters) { this.filters = filters; } @@ -141,7 +150,7 @@ public boolean matches(Class clazz) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { return (this == other || (other instanceof IntersectionClassFilter && ObjectUtils.nullSafeEquals(this.filters, ((IntersectionClassFilter) other).filters))); } @@ -150,6 +159,12 @@ public boolean equals(Object other) { public int hashCode() { return ObjectUtils.nullSafeHashCode(this.filters); } + + @Override + public String toString() { + return getClass().getName() + ": " + Arrays.toString(this.filters); + } + } } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/ComposablePointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/ComposablePointcut.java index 501d8194b653..4bbcaf333411 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/ComposablePointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/ComposablePointcut.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,15 +21,19 @@ import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * Convenient class for building up pointcuts. All methods return - * ComposablePointcut, so we can use a concise idiom like: + * Convenient class for building up pointcuts. * - * {@code - * Pointcut pc = new ComposablePointcut().union(classFilter).intersection(methodMatcher).intersection(pointcut); - * } + *

All methods return {@code ComposablePointcut}, so we can use concise idioms + * like in the following example. + * + *

Pointcut pc = new ComposablePointcut()
+ *                      .union(classFilter)
+ *                      .intersection(methodMatcher)
+ *                      .intersection(pointcut);
* * @author Rod Johnson * @author Juergen Hoeller @@ -180,7 +184,7 @@ public MethodMatcher getMethodMatcher() { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -199,7 +203,7 @@ public int hashCode() { @Override public String toString() { - return "ComposablePointcut: " + this.classFilter + ", " +this.methodMatcher; + return getClass().getName() + ": " + this.classFilter + ", " + this.methodMatcher; } } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/ControlFlowPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/ControlFlowPointcut.java index 832eaafcfed2..56b9f034eefe 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/ControlFlowPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/ControlFlowPointcut.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,6 +18,7 @@ import java.io.Serializable; import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicInteger; import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; @@ -34,16 +35,17 @@ * @author Rod Johnson * @author Rob Harrop * @author Juergen Hoeller + * @author Sam Brannen */ @SuppressWarnings("serial") public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher, Serializable { - private Class clazz; + private final Class clazz; @Nullable - private String methodName; + private final String methodName; - private volatile int evaluations; + private final AtomicInteger evaluations = new AtomicInteger(0); /** @@ -77,8 +79,7 @@ public boolean matches(Class clazz) { } /** - * Subclasses can override this if it's possible to filter out - * some candidate classes. + * Subclasses can override this if it's possible to filter out some candidate classes. */ @Override public boolean matches(Method method, Class targetClass) { @@ -92,7 +93,7 @@ public boolean isRuntime() { @Override public boolean matches(Method method, Class targetClass, Object... args) { - this.evaluations++; + this.evaluations.incrementAndGet(); for (StackTraceElement element : new Throwable().getStackTrace()) { if (element.getClassName().equals(this.clazz.getName()) && @@ -107,7 +108,7 @@ public boolean matches(Method method, Class targetClass, Object... args) { * It's useful to know how many times we've fired, for optimization. */ public int getEvaluations() { - return this.evaluations; + return this.evaluations.get(); } @@ -123,7 +124,7 @@ public MethodMatcher getMethodMatcher() { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -143,4 +144,9 @@ public int hashCode() { return code; } + @Override + public String toString() { + return getClass().getName() + ": class = " + this.clazz.getName() + "; methodName = " + methodName; + } + } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DefaultBeanFactoryPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/DefaultBeanFactoryPointcutAdvisor.java index 49c980831828..ce68704856c6 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DefaultBeanFactoryPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DefaultBeanFactoryPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java index d675d9dc430f..ff2370a32233 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -39,7 +39,7 @@ * @author Juergen Hoeller * @since 11.11.2003 */ -@SuppressWarnings({"serial" }) +@SuppressWarnings("serial") public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFilter, Ordered, Serializable { private final Advice advice; @@ -71,7 +71,8 @@ public DefaultIntroductionAdvisor(Advice advice, @Nullable IntroductionInfo intr if (introductionInfo != null) { Class[] introducedInterfaces = introductionInfo.getInterfaces(); if (introducedInterfaces.length == 0) { - throw new IllegalArgumentException("IntroductionAdviceSupport implements no interfaces"); + throw new IllegalArgumentException( + "IntroductionInfo defines no interfaces to introduce: " + introductionInfo); } for (Class ifc : introducedInterfaces) { addInterface(ifc); @@ -82,25 +83,25 @@ public DefaultIntroductionAdvisor(Advice advice, @Nullable IntroductionInfo intr /** * Create a DefaultIntroductionAdvisor for the given advice. * @param advice the Advice to apply - * @param intf the interface to introduce + * @param ifc the interface to introduce */ - public DefaultIntroductionAdvisor(DynamicIntroductionAdvice advice, Class intf) { + public DefaultIntroductionAdvisor(DynamicIntroductionAdvice advice, Class ifc) { Assert.notNull(advice, "Advice must not be null"); this.advice = advice; - addInterface(intf); + addInterface(ifc); } /** * Add the specified interface to the list of interfaces to introduce. - * @param intf the interface to introduce + * @param ifc the interface to introduce */ - public void addInterface(Class intf) { - Assert.notNull(intf, "Interface must not be null"); - if (!intf.isInterface()) { - throw new IllegalArgumentException("Specified class [" + intf.getName() + "] must be an interface"); + public void addInterface(Class ifc) { + Assert.notNull(ifc, "Interface must not be null"); + if (!ifc.isInterface()) { + throw new IllegalArgumentException("Specified class [" + ifc.getName() + "] must be an interface"); } - this.interfaces.add(intf); + this.interfaces.add(ifc); } @Override @@ -113,8 +114,8 @@ public void validateInterfaces() throws IllegalArgumentException { for (Class ifc : this.interfaces) { if (this.advice instanceof DynamicIntroductionAdvice && !((DynamicIntroductionAdvice) this.advice).implementsInterface(ifc)) { - throw new IllegalArgumentException("DynamicIntroductionAdvice [" + this.advice + "] " + - "does not implement interface [" + ifc.getName() + "] specified for introduction"); + throw new IllegalArgumentException("DynamicIntroductionAdvice [" + this.advice + "] " + + "does not implement interface [" + ifc.getName() + "] specified for introduction"); } } } @@ -150,7 +151,7 @@ public boolean matches(Class clazz) { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -168,7 +169,7 @@ public int hashCode() { @Override public String toString() { - return ClassUtils.getShortName(getClass()) + ": advice [" + this.advice + "]; interfaces " + + return getClass().getName() + ": advice [" + this.advice + "]; interfaces " + ClassUtils.classNamesToString(this.interfaces); } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DefaultPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/DefaultPointcutAdvisor.java index 10bf83719d70..0f3ded0e6b89 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DefaultPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DefaultPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DelegatePerTargetObjectIntroductionInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/support/DelegatePerTargetObjectIntroductionInterceptor.java index 9ad7781f9967..dd820c57fb7b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DelegatePerTargetObjectIntroductionInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DelegatePerTargetObjectIntroductionInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DelegatingIntroductionInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/support/DelegatingIntroductionInterceptor.java index fb6cd155154d..3e97f8a83780 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DelegatingIntroductionInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DelegatingIntroductionInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcher.java index 1f7dc9748415..d45e1d992add 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcher.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcherPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcherPointcut.java index 56e29cb0bd94..d25972434f6f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcherPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DynamicMethodMatcherPointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/ExpressionPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/ExpressionPointcut.java index 35dd9ada9748..99b76e135d32 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/ExpressionPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/ExpressionPointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java b/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java index 53dc0eb294cd..1033375bb7bf 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/JdkRegexpMethodPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/JdkRegexpMethodPointcut.java index 071da29247a3..162b5cb31063 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/JdkRegexpMethodPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/JdkRegexpMethodPointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java b/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java index 2d3d0b678ddb..23b4d9b78de5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/MethodMatchers.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,6 +22,7 @@ import org.springframework.aop.ClassFilter; import org.springframework.aop.IntroductionAwareMethodMatcher; import org.springframework.aop.MethodMatcher; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** @@ -34,6 +35,7 @@ * @author Rod Johnson * @author Rob Harrop * @author Juergen Hoeller + * @author Sam Brannen * @since 11.11.2003 * @see ClassFilters * @see Pointcuts @@ -140,7 +142,7 @@ public boolean matches(Method method, Class targetClass, Object... args) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -155,6 +157,11 @@ public boolean equals(Object other) { public int hashCode() { return 37 * this.mm1.hashCode() + this.mm2.hashCode(); } + + @Override + public String toString() { + return getClass().getName() + ": " + this.mm1 + ", " + this.mm2; + } } @@ -207,7 +214,7 @@ protected boolean matchesClass2(Class targetClass) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -229,6 +236,11 @@ public int hashCode() { // Allow for matching with regular UnionMethodMatcher by providing same hash... return super.hashCode(); } + + @Override + public String toString() { + return getClass().getName() + ": " + this.cf1 + ", " + this.mm1 + ", " + this.cf2 + ", " + this.mm2; + } } @@ -296,7 +308,7 @@ public boolean matches(Method method, Class targetClass, Object... args) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -311,6 +323,11 @@ public boolean equals(Object other) { public int hashCode() { return 37 * this.mm1.hashCode() + this.mm2.hashCode(); } + + @Override + public String toString() { + return getClass().getName() + ": " + this.mm1 + ", " + this.mm2; + } } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcut.java index 19009b457927..d3d762465d51 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcut.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,11 +22,13 @@ import java.util.Arrays; import java.util.List; +import org.springframework.lang.Nullable; import org.springframework.util.PatternMatchUtils; /** - * Pointcut bean for simple method name matches, as alternative to regexp patterns. - * Does not handle overloaded methods: all methods with a given name will be eligible. + * Pointcut bean for simple method name matches, as an alternative to regexp patterns. + * + *

Does not handle overloaded methods: all methods with a given name will be eligible. * * @author Juergen Hoeller * @author Rod Johnson @@ -64,7 +66,7 @@ public void setMappedNames(String... mappedNames) { * before a proxy is used. *

NB: This method does not work after the proxy is in * use, as advice chains will be cached. - * @param name name of the additional method that will match + * @param name the name of the additional method that will match * @return this pointcut to allow for multiple additions in one line */ public NameMatchMethodPointcut addMethodName(String name) { @@ -98,7 +100,7 @@ protected boolean isMatch(String methodName, String mappedName) { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { return (this == other || (other instanceof NameMatchMethodPointcut && this.mappedNames.equals(((NameMatchMethodPointcut) other).mappedNames))); } @@ -108,4 +110,9 @@ public int hashCode() { return this.mappedNames.hashCode(); } + @Override + public String toString() { + return getClass().getName() + ": " + this.mappedNames; + } + } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcutAdvisor.java index dec2cbce2712..a7efd815dc5f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/NameMatchMethodPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -76,7 +76,7 @@ public void setMappedNames(String... mappedNames) { * Add another eligible method name, in addition to those already named. * Like the set methods, this method is for use when configuring proxies, * before a proxy is used. - * @param name name of the additional method that will match + * @param name the name of the additional method that will match * @return this pointcut to allow for multiple additions in one line * @see NameMatchMethodPointcut#addMethodName */ diff --git a/spring-aop/src/main/java/org/springframework/aop/support/Pointcuts.java b/spring-aop/src/main/java/org/springframework/aop/support/Pointcuts.java index d362b6fe8891..7e2ac454dfd5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/Pointcuts.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/Pointcuts.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -26,7 +26,8 @@ /** * Pointcut constants for matching getters and setters, * and static methods useful for manipulating and evaluating pointcuts. - * These methods are particularly useful for composing pointcuts + * + *

These methods are particularly useful for composing pointcuts * using the union and intersection methods. * * @author Rod Johnson @@ -106,6 +107,11 @@ public boolean matches(Method method, Class targetClass) { private Object readResolve() { return INSTANCE; } + + @Override + public String toString() { + return "Pointcuts.SETTERS"; + } } @@ -126,6 +132,11 @@ public boolean matches(Method method, Class targetClass) { private Object readResolve() { return INSTANCE; } + + @Override + public String toString() { + return "Pointcuts.GETTERS"; + } } } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/RegexpMethodPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/RegexpMethodPointcutAdvisor.java index 538d04786624..bc41a9fbdd8d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/RegexpMethodPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/RegexpMethodPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/RootClassFilter.java b/spring-aop/src/main/java/org/springframework/aop/support/RootClassFilter.java index 6b8073d69a2a..3809a303797b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/RootClassFilter.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/RootClassFilter.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,19 +19,22 @@ import java.io.Serializable; import org.springframework.aop.ClassFilter; +import org.springframework.util.Assert; /** * Simple ClassFilter implementation that passes classes (and optionally subclasses). * * @author Rod Johnson + * @author Sam Brannen */ @SuppressWarnings("serial") public class RootClassFilter implements ClassFilter, Serializable { - private Class clazz; + private final Class clazz; public RootClassFilter(Class clazz) { + Assert.notNull(clazz, "Class must not be null"); this.clazz = clazz; } @@ -41,4 +44,20 @@ public boolean matches(Class candidate) { return this.clazz.isAssignableFrom(candidate); } + @Override + public boolean equals(Object other) { + return (this == other || (other instanceof RootClassFilter && + this.clazz.equals(((RootClassFilter) other).clazz))); + } + + @Override + public int hashCode() { + return this.clazz.hashCode(); + } + + @Override + public String toString() { + return getClass().getName() + ": " + this.clazz.getName(); + } + } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcher.java index 923daaf94d06..482ecfd1c26a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcher.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcut.java index 1c2f1106cb5c..1bae02698161 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java index 70970388acfe..38dceb30086b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java index 9c53d5b80cf5..847f1bb86220 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -20,6 +20,7 @@ import org.springframework.aop.ClassFilter; import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** @@ -67,7 +68,7 @@ public boolean matches(Class clazz) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java index 2eb8b15bd5a5..d734fbc0f147 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,6 +21,7 @@ import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -30,6 +31,7 @@ * {@link #forMethodAnnotation method}. * * @author Juergen Hoeller + * @author Sam Brannen * @since 2.0 * @see AnnotationClassFilter * @see AnnotationMethodMatcher @@ -62,7 +64,7 @@ public AnnotationMatchingPointcut(Class classAnnotationTyp } /** - * Create a new AnnotationMatchingPointcut for the given annotation type. + * Create a new AnnotationMatchingPointcut for the given annotation types. * @param classAnnotationType the annotation type to look for at the class level * (can be {@code null}) * @param methodAnnotationType the annotation type to look for at the method level @@ -75,7 +77,7 @@ public AnnotationMatchingPointcut(@Nullable Class classAnn } /** - * Create a new AnnotationMatchingPointcut for the given annotation type. + * Create a new AnnotationMatchingPointcut for the given annotation types. * @param classAnnotationType the annotation type to look for at the class level * (can be {@code null}) * @param methodAnnotationType the annotation type to look for at the method level @@ -96,7 +98,7 @@ public AnnotationMatchingPointcut(@Nullable Class classAnn this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited); } else { - this.classFilter = ClassFilter.TRUE; + this.classFilter = new AnnotationCandidateClassFilter(methodAnnotationType); } if (methodAnnotationType != null) { @@ -119,7 +121,7 @@ public MethodMatcher getMethodMatcher() { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -138,10 +140,9 @@ public int hashCode() { @Override public String toString() { - return "AnnotationMatchingPointcut: " + this.classFilter + ", " +this.methodMatcher; + return "AnnotationMatchingPointcut: " + this.classFilter + ", " + this.methodMatcher; } - /** * Factory method for an AnnotationMatchingPointcut that matches * for the specified annotation at the class level. @@ -164,4 +165,47 @@ public static AnnotationMatchingPointcut forMethodAnnotation(Class annotationType; + + AnnotationCandidateClassFilter(Class annotationType) { + this.annotationType = annotationType; + } + + @Override + public boolean matches(Class clazz) { + return AnnotationUtils.isCandidateClass(clazz, this.annotationType); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof AnnotationCandidateClassFilter)) { + return false; + } + AnnotationCandidateClassFilter that = (AnnotationCandidateClassFilter) obj; + return this.annotationType.equals(that.annotationType); + } + + @Override + public int hashCode() { + return this.annotationType.hashCode(); + } + + @Override + public String toString() { + return getClass().getName() + ": " + this.annotationType; + } + + } + } diff --git a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java index 263f1391aa6c..720c7b889bbb 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,6 +23,7 @@ import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.StaticMethodMatcher; import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** @@ -31,6 +32,7 @@ * interface, if any, and the corresponding method on the target class). * * @author Juergen Hoeller + * @author Sam Brannen * @since 2.0 * @see AnnotationMatchingPointcut */ @@ -86,7 +88,7 @@ private boolean matchesMethod(Method method) { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -94,7 +96,7 @@ public boolean equals(Object other) { return false; } AnnotationMethodMatcher otherMm = (AnnotationMethodMatcher) other; - return this.annotationType.equals(otherMm.annotationType); + return (this.annotationType.equals(otherMm.annotationType) && this.checkInherited == otherMm.checkInherited); } @Override diff --git a/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java index 554222274065..cf32c396d847 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java index 81dbad3e3d40..f742a106bab8 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/AbstractPoolingTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/AbstractPoolingTargetSource.java index ccdb5c7bb266..59017bfabeba 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/AbstractPoolingTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/AbstractPoolingTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/AbstractPrototypeBasedTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/AbstractPrototypeBasedTargetSource.java index fe3d0614a50c..b3c564743b8d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/AbstractPrototypeBasedTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/AbstractPrototypeBasedTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/CommonsPool2TargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/CommonsPool2TargetSource.java index 8a6fcb8aae23..4733d02696fd 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/CommonsPool2TargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/CommonsPool2TargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java index 4cb20236b12a..2ebcc216dce7 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java index f56c71d2c356..5d85f5043863 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/LazyInitTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/LazyInitTargetSource.java index e31a83c1922b..ff0b1d1d9ae1 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/LazyInitTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/LazyInitTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/PoolingConfig.java b/spring-aop/src/main/java/org/springframework/aop/target/PoolingConfig.java index c671f024f5a0..bd439d95d728 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/PoolingConfig.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/PoolingConfig.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/PrototypeTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/PrototypeTargetSource.java index 4dac18b73df9..e43fb6a12ee9 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/PrototypeTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/PrototypeTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/SimpleBeanTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/SimpleBeanTargetSource.java index 805f1a0562d7..6446df2d65fc 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/SimpleBeanTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/SimpleBeanTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java index 6544ab3daff8..24ae9865a6db 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSource.java index 9b0231dab202..5c4b03c78495 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSourceStats.java b/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSourceStats.java index f3801a79dbec..99405f629280 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSourceStats.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSourceStats.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java index 8cba76b0f7f4..32c6ce1c928f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/BeanFactoryRefreshableTargetSource.java b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/BeanFactoryRefreshableTargetSource.java index f2f894dbec05..66f84a1c9864 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/BeanFactoryRefreshableTargetSource.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/BeanFactoryRefreshableTargetSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/Refreshable.java b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/Refreshable.java index 187544e6d849..b03bb5544a6d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/target/dynamic/Refreshable.java +++ b/spring-aop/src/main/java/org/springframework/aop/target/dynamic/Refreshable.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/main/java/overview.html b/spring-aop/src/main/java/overview.html deleted file mode 100644 index 3c1e198fbd7e..000000000000 --- a/spring-aop/src/main/java/overview.html +++ /dev/null @@ -1,7 +0,0 @@ - - -

-Spring's proxy-based AOP framework. -

- - \ No newline at end of file diff --git a/spring-aop/src/main/resources/META-INF/spring.schemas b/spring-aop/src/main/resources/META-INF/spring.schemas index c380e0e178b9..d2bfb7a1d481 100644 --- a/spring-aop/src/main/resources/META-INF/spring.schemas +++ b/spring-aop/src/main/resources/META-INF/spring.schemas @@ -8,3 +8,13 @@ http\://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframewor http\://www.springframework.org/schema/aop/spring-aop-4.2.xsd=org/springframework/aop/config/spring-aop.xsd http\://www.springframework.org/schema/aop/spring-aop-4.3.xsd=org/springframework/aop/config/spring-aop.xsd http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-4.2.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop-4.3.xsd=org/springframework/aop/config/spring-aop.xsd +https\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop.xsd diff --git a/spring-aop/src/main/resources/org/springframework/aop/config/spring-aop.xsd b/spring-aop/src/main/resources/org/springframework/aop/config/spring-aop.xsd index 49cda9de8cd2..0b669c9018e7 100644 --- a/spring-aop/src/main/resources/org/springframework/aop/config/spring-aop.xsd +++ b/spring-aop/src/main/resources/org/springframework/aop/config/spring-aop.xsd @@ -7,8 +7,8 @@ elementFormDefault="qualified" attributeFormDefault="unqualified"> - - + + exceptionType, String message) { + protected void assertException(Method method, String pointcut, Class exceptionType, String message) { assertException(method, pointcut, null, null, exceptionType, message); } - protected void assertException( - Method method, String pointcut, String returning, String throwing, Class exceptionType, String message) { + protected void assertException(Method method, String pointcut, String returning, + String throwing, Class exceptionType, String message) { AspectJAdviceParameterNameDiscoverer discoverer = new AspectJAdviceParameterNameDiscoverer(pointcut); discoverer.setRaiseExceptions(true); discoverer.setReturningName(returning); discoverer.setThrowingName(throwing); - - try { - discoverer.getParameterNames(method); - fail("Expecting " + exceptionType.getName() + " with message '" + message + "'"); - } - catch (RuntimeException expected) { - assertEquals("Expecting exception of type " + exceptionType.getName(), - exceptionType, expected.getClass()); - assertEquals("Exception message does not match expected", message, expected.getMessage()); - } + assertThatExceptionOfType(exceptionType).isThrownBy(() -> + discoverer.getParameterNames(method)) + .withMessageContaining(message); } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutTests.java index 36785d59edfc..008df78e64d9 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,20 +23,23 @@ import org.aspectj.weaver.tools.PointcutExpression; import org.aspectj.weaver.tools.PointcutPrimitive; import org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.DefaultPointcutAdvisor; -import org.springframework.tests.sample.beans.IOther; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.tests.sample.beans.subpkg.DeepBean; +import org.springframework.beans.testfixture.beans.IOther; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.beans.testfixture.beans.subpkg.DeepBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Rob Harrop @@ -54,7 +57,7 @@ public class AspectJExpressionPointcutTests { private Method setSomeNumber; - @Before + @BeforeEach public void setUp() throws NoSuchMethodException { getAge = TestBean.class.getMethod("getAge"); setAge = TestBean.class.getMethod("setAge", int.class); @@ -64,7 +67,7 @@ public void setUp() throws NoSuchMethodException { @Test public void testMatchExplicit() { - String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())"; + String expression = "execution(int org.springframework.beans.testfixture.beans.TestBean.getAge())"; Pointcut pointcut = getPointcut(expression); ClassFilter classFilter = pointcut.getClassFilter(); @@ -75,9 +78,9 @@ public void testMatchExplicit() { // not currently testable in a reliable fashion //assertDoesNotMatchStringClass(classFilter); - assertFalse("Should not be a runtime match", methodMatcher.isRuntime()); + assertThat(methodMatcher.isRuntime()).as("Should not be a runtime match").isFalse(); assertMatchesGetAge(methodMatcher); - assertFalse("Expression should match setAge() method", methodMatcher.matches(setAge, TestBean.class)); + assertThat(methodMatcher.matches(setAge, TestBean.class)).as("Expression should match setAge() method").isFalse(); } @Test @@ -93,9 +96,9 @@ public void testMatchWithTypePattern() throws Exception { // not currently testable in a reliable fashion //assertDoesNotMatchStringClass(classFilter); - assertFalse("Should not be a runtime match", methodMatcher.isRuntime()); + assertThat(methodMatcher.isRuntime()).as("Should not be a runtime match").isFalse(); assertMatchesGetAge(methodMatcher); - assertTrue("Expression should match setAge(int) method", methodMatcher.matches(setAge, TestBean.class)); + assertThat(methodMatcher.matches(setAge, TestBean.class)).as("Expression should match setAge(int) method").isTrue(); } @@ -114,18 +117,18 @@ public void testTarget() throws SecurityException, NoSuchMethodException { * @param which this or target */ private void testThisOrTarget(String which) throws SecurityException, NoSuchMethodException { - String matchesTestBean = which + "(org.springframework.tests.sample.beans.TestBean)"; - String matchesIOther = which + "(org.springframework.tests.sample.beans.IOther)"; + String matchesTestBean = which + "(org.springframework.beans.testfixture.beans.TestBean)"; + String matchesIOther = which + "(org.springframework.beans.testfixture.beans.IOther)"; AspectJExpressionPointcut testBeanPc = new AspectJExpressionPointcut(); testBeanPc.setExpression(matchesTestBean); AspectJExpressionPointcut iOtherPc = new AspectJExpressionPointcut(); iOtherPc.setExpression(matchesIOther); - assertTrue(testBeanPc.matches(TestBean.class)); - assertTrue(testBeanPc.matches(getAge, TestBean.class)); - assertTrue(iOtherPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)); - assertFalse(testBeanPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)); + assertThat(testBeanPc.matches(TestBean.class)).isTrue(); + assertThat(testBeanPc.matches(getAge, TestBean.class)).isTrue(); + assertThat(iOtherPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)).isTrue(); + assertThat(testBeanPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)).isFalse(); } @Test @@ -139,7 +142,7 @@ public void testWithinRootAndSubpackages() throws SecurityException, NoSuchMetho } private void testWithinPackage(boolean matchSubpackages) throws SecurityException, NoSuchMethodException { - String withinBeansPackage = "within(org.springframework.tests.sample.beans."; + String withinBeansPackage = "within(org.springframework.beans.testfixture.beans."; // Subpackages are matched by ** if (matchSubpackages) { withinBeansPackage += "."; @@ -148,55 +151,43 @@ private void testWithinPackage(boolean matchSubpackages) throws SecurityExceptio AspectJExpressionPointcut withinBeansPc = new AspectJExpressionPointcut(); withinBeansPc.setExpression(withinBeansPackage); - assertTrue(withinBeansPc.matches(TestBean.class)); - assertTrue(withinBeansPc.matches(getAge, TestBean.class)); - assertEquals(matchSubpackages, withinBeansPc.matches(DeepBean.class)); - assertEquals(matchSubpackages, withinBeansPc.matches( - DeepBean.class.getMethod("aMethod", String.class), DeepBean.class)); - assertFalse(withinBeansPc.matches(String.class)); - assertFalse(withinBeansPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)); + assertThat(withinBeansPc.matches(TestBean.class)).isTrue(); + assertThat(withinBeansPc.matches(getAge, TestBean.class)).isTrue(); + assertThat(withinBeansPc.matches(DeepBean.class)).isEqualTo(matchSubpackages); + assertThat(withinBeansPc.matches( + DeepBean.class.getMethod("aMethod", String.class), DeepBean.class)).isEqualTo(matchSubpackages); + assertThat(withinBeansPc.matches(String.class)).isFalse(); + assertThat(withinBeansPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)).isFalse(); } @Test public void testFriendlyErrorOnNoLocationClassMatching() { AspectJExpressionPointcut pc = new AspectJExpressionPointcut(); - try { - pc.matches(ITestBean.class); - fail(); - } - catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("expression")); - } + assertThatIllegalStateException().isThrownBy(() -> + pc.matches(ITestBean.class)) + .withMessageContaining("expression"); } @Test public void testFriendlyErrorOnNoLocation2ArgMatching() { AspectJExpressionPointcut pc = new AspectJExpressionPointcut(); - try { - pc.matches(getAge, ITestBean.class); - fail(); - } - catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("expression")); - } + assertThatIllegalStateException().isThrownBy(() -> + pc.matches(getAge, ITestBean.class)) + .withMessageContaining("expression"); } @Test public void testFriendlyErrorOnNoLocation3ArgMatching() { AspectJExpressionPointcut pc = new AspectJExpressionPointcut(); - try { - pc.matches(getAge, ITestBean.class, (Object[]) null); - fail(); - } - catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("expression")); - } + assertThatIllegalStateException().isThrownBy(() -> + pc.matches(getAge, ITestBean.class, (Object[]) null)) + .withMessageContaining("expression"); } @Test public void testMatchWithArgs() throws Exception { - String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number)) && args(Double)"; + String expression = "execution(void org.springframework.beans.testfixture.beans.TestBean.setSomeNumber(Number)) && args(Double)"; Pointcut pointcut = getPointcut(expression); ClassFilter classFilter = pointcut.getClassFilter(); @@ -207,52 +198,44 @@ public void testMatchWithArgs() throws Exception { // not currently testable in a reliable fashion //assertDoesNotMatchStringClass(classFilter); - assertTrue("Should match with setSomeNumber with Double input", - methodMatcher.matches(setSomeNumber, TestBean.class, new Double(12))); - assertFalse("Should not match setSomeNumber with Integer input", - methodMatcher.matches(setSomeNumber, TestBean.class, new Integer(11))); - assertFalse("Should not match getAge", methodMatcher.matches(getAge, TestBean.class)); - assertTrue("Should be a runtime match", methodMatcher.isRuntime()); + assertThat(methodMatcher.matches(setSomeNumber, TestBean.class, new Double(12))).as("Should match with setSomeNumber with Double input").isTrue(); + assertThat(methodMatcher.matches(setSomeNumber, TestBean.class, new Integer(11))).as("Should not match setSomeNumber with Integer input").isFalse(); + assertThat(methodMatcher.matches(getAge, TestBean.class)).as("Should not match getAge").isFalse(); + assertThat(methodMatcher.isRuntime()).as("Should be a runtime match").isTrue(); } @Test public void testSimpleAdvice() { - String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())"; + String expression = "execution(int org.springframework.beans.testfixture.beans.TestBean.getAge())"; CallCountingInterceptor interceptor = new CallCountingInterceptor(); TestBean testBean = getAdvisedProxy(expression, interceptor); - assertEquals("Calls should be 0", 0, interceptor.getCount()); + assertThat(interceptor.getCount()).as("Calls should be 0").isEqualTo(0); testBean.getAge(); - assertEquals("Calls should be 1", 1, interceptor.getCount()); + assertThat(interceptor.getCount()).as("Calls should be 1").isEqualTo(1); testBean.setAge(90); - assertEquals("Calls should still be 1", 1, interceptor.getCount()); + assertThat(interceptor.getCount()).as("Calls should still be 1").isEqualTo(1); } @Test public void testDynamicMatchingProxy() { - String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number)) && args(Double)"; + String expression = "execution(void org.springframework.beans.testfixture.beans.TestBean.setSomeNumber(Number)) && args(Double)"; CallCountingInterceptor interceptor = new CallCountingInterceptor(); TestBean testBean = getAdvisedProxy(expression, interceptor); - assertEquals("Calls should be 0", 0, interceptor.getCount()); + assertThat(interceptor.getCount()).as("Calls should be 0").isEqualTo(0); testBean.setSomeNumber(new Double(30)); - assertEquals("Calls should be 1", 1, interceptor.getCount()); + assertThat(interceptor.getCount()).as("Calls should be 1").isEqualTo(1); testBean.setSomeNumber(new Integer(90)); - assertEquals("Calls should be 1", 1, interceptor.getCount()); + assertThat(interceptor.getCount()).as("Calls should be 1").isEqualTo(1); } @Test public void testInvalidExpression() { - String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number) && args(Double)"; - - try { - getPointcut(expression).getClassFilter(); // call to getClassFilter forces resolution - fail("Invalid expression should throw IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - assertTrue(true); - } + String expression = "execution(void org.springframework.beans.testfixture.beans.TestBean.setSomeNumber(Number) && args(Double)"; + assertThatIllegalArgumentException().isThrownBy( + getPointcut(expression)::getClassFilter); // call to getClassFilter forces resolution } private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) { @@ -272,38 +255,33 @@ private TestBean getAdvisedProxy(String pointcutExpression, CallCountingIntercep } private void assertMatchesGetAge(MethodMatcher methodMatcher) { - assertTrue("Expression should match getAge() method", methodMatcher.matches(getAge, TestBean.class)); + assertThat(methodMatcher.matches(getAge, TestBean.class)).as("Expression should match getAge() method").isTrue(); } private void assertMatchesTestBeanClass(ClassFilter classFilter) { - assertTrue("Expression should match TestBean class", classFilter.matches(TestBean.class)); + assertThat(classFilter.matches(TestBean.class)).as("Expression should match TestBean class").isTrue(); } @Test public void testWithUnsupportedPointcutPrimitive() { - String expression = "call(int org.springframework.tests.sample.beans.TestBean.getAge())"; - - try { - getPointcut(expression).getClassFilter(); // call to getClassFilter forces resolution... - fail("Should not support call pointcuts"); - } - catch (UnsupportedPointcutPrimitiveException ex) { - assertEquals("Should not support call pointcut", PointcutPrimitive.CALL, ex.getUnsupportedPrimitive()); - } + String expression = "call(int org.springframework.beans.testfixture.beans.TestBean.getAge())"; + assertThatExceptionOfType(UnsupportedPointcutPrimitiveException.class).isThrownBy(() -> + getPointcut(expression).getClassFilter()) // call to getClassFilter forces resolution... + .satisfies(ex -> assertThat(ex.getUnsupportedPrimitive()).isEqualTo(PointcutPrimitive.CALL)); } @Test public void testAndSubstitution() { Pointcut pc = getPointcut("execution(* *(..)) and args(String)"); PointcutExpression expr = ((AspectJExpressionPointcut) pc).getPointcutExpression(); - assertEquals("execution(* *(..)) && args(String)",expr.getPointcutExpression()); + assertThat(expr.getPointcutExpression()).isEqualTo("execution(* *(..)) && args(String)"); } @Test public void testMultipleAndSubstitutions() { Pointcut pc = getPointcut("execution(* *(..)) and args(String) and this(Object)"); PointcutExpression expr = ((AspectJExpressionPointcut) pc).getPointcutExpression(); - assertEquals("execution(* *(..)) && args(String) && this(Object)",expr.getPointcutExpression()); + assertThat(expr.getPointcutExpression()).isEqualTo("execution(* *(..)) && args(String) && this(Object)"); } private Pointcut getPointcut(String expression) { diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutMatchingTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutMatchingTests.java index c391fd9fa091..3fd1b1e0c844 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutMatchingTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutMatchingTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,11 +16,11 @@ package org.springframework.aop.aspectj; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for matching of bean() pointcut designator. @@ -79,13 +79,11 @@ public void testNonMatchingPointcuts() { private void assertMatch(String beanName, String pcExpression) { - assertTrue("Unexpected mismatch for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"", - matches(beanName, pcExpression)); + assertThat(matches(beanName, pcExpression)).as("Unexpected mismatch for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"").isTrue(); } private void assertMisMatch(String beanName, String pcExpression) { - assertFalse("Unexpected match for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"", - matches(beanName, pcExpression)); + assertThat(matches(beanName, pcExpression)).as("Unexpected match for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"").isFalse(); } private static boolean matches(final String beanName, String pcExpression) { diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPointTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPointTests.java index 60d0d4627af9..4591b74624aa 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPointTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPointTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,8 +17,8 @@ package org.springframework.aop.aspectj; import java.io.IOException; -import java.lang.reflect.Method; import java.util.Arrays; +import java.util.concurrent.atomic.AtomicInteger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint.StaticPart; @@ -26,18 +26,19 @@ import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.SourceLocation; import org.aspectj.runtime.reflect.Factory; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.AopContext; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.aop.support.AopUtils; -import org.springframework.lang.Nullable; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Rod Johnson @@ -49,24 +50,12 @@ public class MethodInvocationProceedingJoinPointTests { @Test public void testingBindingWithJoinPoint() { - try { - AbstractAspectJAdvice.currentJoinPoint(); - fail("Needs to be bound by interceptor action"); - } - catch (IllegalStateException ex) { - // expected - } + assertThatIllegalStateException().isThrownBy(AbstractAspectJAdvice::currentJoinPoint); } @Test public void testingBindingWithProceedingJoinPoint() { - try { - AbstractAspectJAdvice.currentJoinPoint(); - fail("Needs to be bound by interceptor action"); - } - catch (IllegalStateException ex) { - // expected - } + assertThatIllegalStateException().isThrownBy(AbstractAspectJAdvice::currentJoinPoint); } @Test @@ -78,58 +67,54 @@ public void testCanGetMethodSignatureFromJoinPoint() { ProxyFactory pf = new ProxyFactory(raw); pf.setExposeProxy(true); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); - pf.addAdvice(new MethodBeforeAdvice() { - private int depth; - - @Override - public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { - JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); - assertTrue("Method named in toString", jp.toString().contains(method.getName())); - // Ensure that these don't cause problems - jp.toShortString(); - jp.toLongString(); - - assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget()); - assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())); - - ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis(); - assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())); - - assertNotSame(target, thisProxy); - - // Check getting again doesn't cause a problem - assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis()); - - // Try reentrant call--will go through this advice. - // Be sure to increment depth to avoid infinite recursion - if (depth++ == 0) { - // Check that toString doesn't cause a problem - thisProxy.toString(); - // Change age, so this will be returned by invocation - thisProxy.setAge(newAge); - assertEquals(newAge, thisProxy.getAge()); - } - - assertSame(AopContext.currentProxy(), thisProxy); - assertSame(target, raw); - - assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()); - assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()); - - MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature(); - assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature()); - assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint()); - assertEquals(method.getDeclaringClass(), msig.getDeclaringType()); - assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes())); - assertEquals(method.getReturnType(), msig.getReturnType()); - assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())); - msig.toLongString(); - msig.toShortString(); + AtomicInteger depth = new AtomicInteger(); + pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> { + JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); + assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue(); + // Ensure that these don't cause problems + jp.toShortString(); + jp.toLongString(); + + assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(target); + assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse(); + + ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis(); + assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue(); + + assertThat(thisProxy).isNotSameAs(target); + + // Check getting again doesn't cause a problem + assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy); + + // Try reentrant call--will go through this advice. + // Be sure to increment depth to avoid infinite recursion + if (depth.getAndIncrement() == 0) { + // Check that toString doesn't cause a problem + thisProxy.toString(); + // Change age, so this will be returned by invocation + thisProxy.setAge(newAge); + assertThat(thisProxy.getAge()).isEqualTo(newAge); } + + assertThat(thisProxy).isSameAs(AopContext.currentProxy()); + assertThat(raw).isSameAs(target); + + assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getName()); + assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getModifiers()); + + MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature(); + assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig); + assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint()); + assertThat(msig.getDeclaringType()).isEqualTo(method.getDeclaringClass()); + assertThat(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes())).isTrue(); + assertThat(msig.getReturnType()).isEqualTo(method.getReturnType()); + assertThat(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())).isTrue(); + msig.toLongString(); + msig.toShortString(); }); ITestBean itb = (ITestBean) pf.getProxy(); // Any call will do - assertEquals("Advice reentrantly set age", newAge, itb.getAge()); + assertThat(itb.getAge()).as("Advice reentrantly set age").isEqualTo(newAge); } @Test @@ -137,28 +122,12 @@ public void testCanGetSourceLocationFromJoinPoint() { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); - pf.addAdvice(new MethodBeforeAdvice() { - @Override - public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { - SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(); - assertEquals("Same source location must be returned on subsequent requests", sloc, AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()); - assertEquals(TestBean.class, sloc.getWithinType()); - try { - sloc.getLine(); - fail("Can't get line number"); - } - catch (UnsupportedOperationException ex) { - // Expected - } - - try { - sloc.getFileName(); - fail("Can't get file name"); - } - catch (UnsupportedOperationException ex) { - // Expected - } - } + pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> { + SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(); + assertThat(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()).as("Same source location must be returned on subsequent requests").isEqualTo(sloc); + assertThat(sloc.getWithinType()).isEqualTo(TestBean.class); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getLine); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getFileName); }); ITestBean itb = (ITestBean) pf.getProxy(); // Any call will do @@ -170,15 +139,12 @@ public void testCanGetStaticPartFromJoinPoint() { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); - pf.addAdvice(new MethodBeforeAdvice() { - @Override - public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { - StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart(); - assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart()); - assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind()); - assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature()); - assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation()); - } + pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> { + StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart(); + assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart); + assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION); + assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature()); + assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()); }); ITestBean itb = (ITestBean) pf.getProxy(); // Any call will do @@ -190,22 +156,19 @@ public void toShortAndLongStringFormedCorrectly() throws Exception { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); - pf.addAdvice(new MethodBeforeAdvice() { - @Override - public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { - // makeEncSJP, although meant for computing the enclosing join point, - // it serves our purpose here - JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); - JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); - - assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); - assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); - assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); - - assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); - assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); - assertEquals(aspectJVersionJp.toString(), jp.toString()); - } + pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> { + // makeEncSJP, although meant for computing the enclosing join point, + // it serves our purpose here + StaticPart aspectJVersionJp = Factory.makeEncSJP(method); + JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); + + assertThat(jp.getSignature().toLongString()).isEqualTo(aspectJVersionJp.getSignature().toLongString()); + assertThat(jp.getSignature().toShortString()).isEqualTo(aspectJVersionJp.getSignature().toShortString()); + assertThat(jp.getSignature().toString()).isEqualTo(aspectJVersionJp.getSignature().toString()); + + assertThat(jp.toLongString()).isEqualTo(aspectJVersionJp.toLongString()); + assertThat(jp.toShortString()).isEqualTo(aspectJVersionJp.toShortString()); + assertThat(jp.toString()).isEqualTo(aspectJVersionJp.toString()); }); ITestBean itb = (ITestBean) pf.getProxy(); itb.getAge(); diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJAdviceParameterNameDiscovererTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJAdviceParameterNameDiscovererTests.java index e3c52db8f3d1..0cc3947260a5 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJAdviceParameterNameDiscovererTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJAdviceParameterNameDiscovererTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -16,7 +16,7 @@ package org.springframework.aop.aspectj; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer.AmbiguousBindingException; diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJExpressionPointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJExpressionPointcutTests.java index ab2fc39b9774..23d63fb1bea0 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJExpressionPointcutTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/TigerAspectJExpressionPointcutTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,15 +21,15 @@ import java.util.List; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import test.annotation.EmptySpringAnnotation; import test.annotation.transaction.Tx; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Java 5 specific {@link AspectJExpressionPointcutTests}. @@ -44,7 +44,7 @@ public class TigerAspectJExpressionPointcutTests { private final Map methodsOnHasGeneric = new HashMap<>(); - @Before + @BeforeEach public void setup() throws NoSuchMethodException { getAge = TestBean.class.getMethod("getAge"); // Assumes no overloading @@ -56,7 +56,7 @@ public void setup() throws NoSuchMethodException { @Test public void testMatchGenericArgument() { - String expression = "execution(* set*(java.util.List) )"; + String expression = "execution(* set*(java.util.List) )"; AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); @@ -65,12 +65,12 @@ public void testMatchGenericArgument() { //assertFalse(ajexp.matches(TestBean.class)); Method takesGenericList = methodsOnHasGeneric.get("setFriends"); - assertTrue(ajexp.matches(takesGenericList, HasGeneric.class)); - assertTrue(ajexp.matches(methodsOnHasGeneric.get("setEnemies"), HasGeneric.class)); - assertFalse(ajexp.matches(methodsOnHasGeneric.get("setPartners"), HasGeneric.class)); - assertFalse(ajexp.matches(methodsOnHasGeneric.get("setPhoneNumbers"), HasGeneric.class)); + assertThat(ajexp.matches(takesGenericList, HasGeneric.class)).isTrue(); + assertThat(ajexp.matches(methodsOnHasGeneric.get("setEnemies"), HasGeneric.class)).isTrue(); + assertThat(ajexp.matches(methodsOnHasGeneric.get("setPartners"), HasGeneric.class)).isFalse(); + assertThat(ajexp.matches(methodsOnHasGeneric.get("setPhoneNumbers"), HasGeneric.class)).isFalse(); - assertFalse(ajexp.matches(getAge, TestBean.class)); + assertThat(ajexp.matches(getAge, TestBean.class)).isFalse(); } @Test @@ -87,16 +87,16 @@ public int queryForInt(String sql, Object... params) { AspectJExpressionPointcut jdbcVarArgs = new AspectJExpressionPointcut(); jdbcVarArgs.setExpression(expression); - assertTrue(jdbcVarArgs.matches( + assertThat(jdbcVarArgs.matches( MyTemplate.class.getMethod("queryForInt", String.class, Object[].class), - MyTemplate.class)); + MyTemplate.class)).isTrue(); Method takesGenericList = methodsOnHasGeneric.get("setFriends"); - assertFalse(jdbcVarArgs.matches(takesGenericList, HasGeneric.class)); - assertFalse(jdbcVarArgs.matches(methodsOnHasGeneric.get("setEnemies"), HasGeneric.class)); - assertFalse(jdbcVarArgs.matches(methodsOnHasGeneric.get("setPartners"), HasGeneric.class)); - assertFalse(jdbcVarArgs.matches(methodsOnHasGeneric.get("setPhoneNumbers"), HasGeneric.class)); - assertFalse(jdbcVarArgs.matches(getAge, TestBean.class)); + assertThat(jdbcVarArgs.matches(takesGenericList, HasGeneric.class)).isFalse(); + assertThat(jdbcVarArgs.matches(methodsOnHasGeneric.get("setEnemies"), HasGeneric.class)).isFalse(); + assertThat(jdbcVarArgs.matches(methodsOnHasGeneric.get("setPartners"), HasGeneric.class)).isFalse(); + assertThat(jdbcVarArgs.matches(methodsOnHasGeneric.get("setPhoneNumbers"), HasGeneric.class)).isFalse(); + assertThat(jdbcVarArgs.matches(getAge, TestBean.class)).isFalse(); } @Test @@ -115,12 +115,12 @@ public void testMatchAnnotationOnClassWithoutBinding() throws Exception { public void testMatchAnnotationOnClassWithSubpackageWildcard() throws Exception { String expression = "within(@(test.annotation..*) *)"; AspectJExpressionPointcut springAnnotatedPc = testMatchAnnotationOnClass(expression); - assertFalse(springAnnotatedPc.matches(TestBean.class.getMethod("setName", String.class), TestBean.class)); - assertTrue(springAnnotatedPc.matches(SpringAnnotated.class.getMethod("foo"), SpringAnnotated.class)); + assertThat(springAnnotatedPc.matches(TestBean.class.getMethod("setName", String.class), TestBean.class)).isFalse(); + assertThat(springAnnotatedPc.matches(SpringAnnotated.class.getMethod("foo"), SpringAnnotated.class)).isTrue(); expression = "within(@(test.annotation.transaction..*) *)"; AspectJExpressionPointcut springTxAnnotatedPc = testMatchAnnotationOnClass(expression); - assertFalse(springTxAnnotatedPc.matches(SpringAnnotated.class.getMethod("foo"), SpringAnnotated.class)); + assertThat(springTxAnnotatedPc.matches(SpringAnnotated.class.getMethod("foo"), SpringAnnotated.class)).isFalse(); } @Test @@ -133,11 +133,11 @@ private AspectJExpressionPointcut testMatchAnnotationOnClass(String expression) AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); - assertFalse(ajexp.matches(getAge, TestBean.class)); - assertTrue(ajexp.matches(HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); - assertTrue(ajexp.matches(HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); - assertTrue(ajexp.matches(BeanB.class.getMethod("setName", String.class), BeanB.class)); - assertFalse(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); + assertThat(ajexp.matches(getAge, TestBean.class)).isFalse(); + assertThat(ajexp.matches(HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)).isTrue(); + assertThat(ajexp.matches(HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)).isTrue(); + assertThat(ajexp.matches(BeanB.class.getMethod("setName", String.class), BeanB.class)).isTrue(); + assertThat(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); return ajexp; } @@ -147,12 +147,12 @@ public void testAnnotationOnMethodWithFQN() throws Exception { AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); - assertFalse(ajexp.matches(getAge, TestBean.class)); - assertFalse(ajexp.matches(HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); - assertFalse(ajexp.matches(HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); - assertFalse(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); - assertTrue(ajexp.matches(BeanA.class.getMethod("getAge"), BeanA.class)); - assertFalse(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); + assertThat(ajexp.matches(getAge, TestBean.class)).isFalse(); + assertThat(ajexp.matches(HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)).isFalse(); + assertThat(ajexp.matches(HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)).isFalse(); + assertThat(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); + assertThat(ajexp.matches(BeanA.class.getMethod("getAge"), BeanA.class)).isTrue(); + assertThat(ajexp.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); } @Test @@ -164,7 +164,7 @@ public void testAnnotationOnCglibProxyMethod() throws Exception { ProxyFactory factory = new ProxyFactory(new BeanA()); factory.setProxyTargetClass(true); BeanA proxy = (BeanA) factory.getProxy(); - assertTrue(ajexp.matches(BeanA.class.getMethod("getAge"), proxy.getClass())); + assertThat(ajexp.matches(BeanA.class.getMethod("getAge"), proxy.getClass())).isTrue(); } @Test @@ -176,7 +176,7 @@ public void testAnnotationOnDynamicProxyMethod() throws Exception { ProxyFactory factory = new ProxyFactory(new BeanA()); factory.setProxyTargetClass(false); IBeanA proxy = (IBeanA) factory.getProxy(); - assertTrue(ajexp.matches(IBeanA.class.getMethod("getAge"), proxy.getClass())); + assertThat(ajexp.matches(IBeanA.class.getMethod("getAge"), proxy.getClass())).isTrue(); } @Test @@ -185,14 +185,14 @@ public void testAnnotationOnMethodWithWildcard() throws Exception { AspectJExpressionPointcut anySpringMethodAnnotation = new AspectJExpressionPointcut(); anySpringMethodAnnotation.setExpression(expression); - assertFalse(anySpringMethodAnnotation.matches(getAge, TestBean.class)); - assertFalse(anySpringMethodAnnotation.matches( - HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); - assertFalse(anySpringMethodAnnotation.matches( - HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); - assertFalse(anySpringMethodAnnotation.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); - assertTrue(anySpringMethodAnnotation.matches(BeanA.class.getMethod("getAge"), BeanA.class)); - assertFalse(anySpringMethodAnnotation.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); + assertThat(anySpringMethodAnnotation.matches(getAge, TestBean.class)).isFalse(); + assertThat(anySpringMethodAnnotation.matches( + HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)).isFalse(); + assertThat(anySpringMethodAnnotation.matches( + HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)).isFalse(); + assertThat(anySpringMethodAnnotation.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); + assertThat(anySpringMethodAnnotation.matches(BeanA.class.getMethod("getAge"), BeanA.class)).isTrue(); + assertThat(anySpringMethodAnnotation.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); } @Test @@ -201,28 +201,27 @@ public void testAnnotationOnMethodArgumentsWithFQN() throws Exception { AspectJExpressionPointcut takesSpringAnnotatedArgument2 = new AspectJExpressionPointcut(); takesSpringAnnotatedArgument2.setExpression(expression); - assertFalse(takesSpringAnnotatedArgument2.matches(getAge, TestBean.class)); - assertFalse(takesSpringAnnotatedArgument2.matches( - HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); - assertFalse(takesSpringAnnotatedArgument2.matches( - HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); - assertFalse(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); - assertFalse(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("getAge"), BeanA.class)); - assertFalse(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); + assertThat(takesSpringAnnotatedArgument2.matches(getAge, TestBean.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches( + HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches( + HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("getAge"), BeanA.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); - assertTrue(takesSpringAnnotatedArgument2.matches( + assertThat(takesSpringAnnotatedArgument2.matches( ProcessesSpringAnnotatedParameters.class.getMethod("takesAnnotatedParameters", TestBean.class, SpringAnnotated.class), - ProcessesSpringAnnotatedParameters.class)); + ProcessesSpringAnnotatedParameters.class)).isTrue(); // True because it maybeMatches with potential argument subtypes - assertTrue(takesSpringAnnotatedArgument2.matches( + assertThat(takesSpringAnnotatedArgument2.matches( ProcessesSpringAnnotatedParameters.class.getMethod("takesNoAnnotatedParameters", TestBean.class, BeanA.class), - ProcessesSpringAnnotatedParameters.class)); + ProcessesSpringAnnotatedParameters.class)).isTrue(); - assertFalse(takesSpringAnnotatedArgument2.matches( + assertThat(takesSpringAnnotatedArgument2.matches( ProcessesSpringAnnotatedParameters.class.getMethod("takesNoAnnotatedParameters", TestBean.class, BeanA.class), - ProcessesSpringAnnotatedParameters.class, new TestBean(), new BeanA()) - ); + ProcessesSpringAnnotatedParameters.class, new TestBean(), new BeanA())).isFalse(); } @Test @@ -231,21 +230,21 @@ public void testAnnotationOnMethodArgumentsWithWildcards() throws Exception { AspectJExpressionPointcut takesSpringAnnotatedArgument2 = new AspectJExpressionPointcut(); takesSpringAnnotatedArgument2.setExpression(expression); - assertFalse(takesSpringAnnotatedArgument2.matches(getAge, TestBean.class)); - assertFalse(takesSpringAnnotatedArgument2.matches( - HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)); - assertFalse(takesSpringAnnotatedArgument2.matches( - HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)); - assertFalse(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); - assertFalse(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("getAge"), BeanA.class)); - assertFalse(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)); + assertThat(takesSpringAnnotatedArgument2.matches(getAge, TestBean.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches( + HasTransactionalAnnotation.class.getMethod("foo"), HasTransactionalAnnotation.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches( + HasTransactionalAnnotation.class.getMethod("bar", String.class), HasTransactionalAnnotation.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("getAge"), BeanA.class)).isFalse(); + assertThat(takesSpringAnnotatedArgument2.matches(BeanA.class.getMethod("setName", String.class), BeanA.class)).isFalse(); - assertTrue(takesSpringAnnotatedArgument2.matches( + assertThat(takesSpringAnnotatedArgument2.matches( ProcessesSpringAnnotatedParameters.class.getMethod("takesAnnotatedParameters", TestBean.class, SpringAnnotated.class), - ProcessesSpringAnnotatedParameters.class)); - assertFalse(takesSpringAnnotatedArgument2.matches( + ProcessesSpringAnnotatedParameters.class)).isTrue(); + assertThat(takesSpringAnnotatedArgument2.matches( ProcessesSpringAnnotatedParameters.class.getMethod("takesNoAnnotatedParameters", TestBean.class, BeanA.class), - ProcessesSpringAnnotatedParameters.class)); + ProcessesSpringAnnotatedParameters.class)).isFalse(); } @@ -300,6 +299,7 @@ interface IBeanA { static class BeanA implements IBeanA { + @SuppressWarnings("unused") private String name; private int age; @@ -319,6 +319,7 @@ public int getAge() { @Tx static class BeanB { + @SuppressWarnings("unused") private String name; public void setName(String name) { diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/TrickyAspectJPointcutExpressionTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/TrickyAspectJPointcutExpressionTests.java index dd57aef3b2f0..bac21dadf9fc 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/TrickyAspectJPointcutExpressionTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/TrickyAspectJPointcutExpressionTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -24,7 +24,7 @@ import java.lang.annotation.Target; import java.lang.reflect.Method; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.Advisor; import org.springframework.aop.MethodBeforeAdvice; @@ -34,7 +34,8 @@ import org.springframework.core.OverridingClassLoader; import org.springframework.lang.Nullable; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Dave Syer @@ -86,7 +87,7 @@ public void testManualProxyJavaWithStaticPointcutAndTwoClassLoaders() throws Exc testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, new TestServiceImpl(), "TestServiceImpl"); // Then try again with a different class loader on the target... - SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(new TestServiceImpl().getClass().getClassLoader()); + SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(TestServiceImpl.class.getClassLoader()); // Make sure the interface is loaded from the parent class loader loader.excludeClass(TestService.class.getName()); loader.excludeClass(TestException.class.getName()); @@ -110,15 +111,10 @@ private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService ta factory.addAdvisor(advisor); TestService bean = (TestService) factory.getProxy(); - assertEquals(0, logAdvice.getCountThrows()); - try { - bean.sayHello(); - fail("Expected exception"); - } - catch (TestException ex) { - assertEquals(message, ex.getMessage()); - } - assertEquals(1, logAdvice.getCountThrows()); + assertThat(logAdvice.getCountThrows()).isEqualTo(0); + assertThatExceptionOfType(TestException.class).isThrownBy( + bean::sayHello).withMessageContaining(message); + assertThat(logAdvice.getCountThrows()).isEqualTo(1); } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/TypePatternClassFilterTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/TypePatternClassFilterTests.java index 3362ad29568c..43373063622c 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/TypePatternClassFilterTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/TypePatternClassFilterTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,17 +16,19 @@ package org.springframework.aop.aspectj; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.tests.sample.beans.CountingTestBean; -import org.springframework.tests.sample.beans.IOther; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.tests.sample.beans.subpkg.DeepBean; +import org.springframework.beans.testfixture.beans.CountingTestBean; +import org.springframework.beans.testfixture.beans.IOther; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.beans.testfixture.beans.subpkg.DeepBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * Unit tests for the {@link TypePatternClassFilter} class. @@ -34,58 +36,94 @@ * @author Rod Johnson * @author Rick Evans * @author Chris Beams + * @author Sam Brannen */ -public class TypePatternClassFilterTests { +class TypePatternClassFilterTests { - @Test(expected = IllegalArgumentException.class) - public void testInvalidPattern() { - // should throw - pattern must be recognized as invalid - new TypePatternClassFilter("-"); + @Test + void nullPattern() { + assertThatIllegalArgumentException().isThrownBy(() -> new TypePatternClassFilter(null)); + } + + @Test + void invalidPattern() { + assertThatIllegalArgumentException().isThrownBy(() -> new TypePatternClassFilter("-")); } @Test - public void testValidPatternMatching() { - TypePatternClassFilter tpcf = new TypePatternClassFilter("org.springframework.tests.sample.beans.*"); - assertTrue("Must match: in package", tpcf.matches(TestBean.class)); - assertTrue("Must match: in package", tpcf.matches(ITestBean.class)); - assertTrue("Must match: in package", tpcf.matches(IOther.class)); - assertFalse("Must be excluded: in wrong package", tpcf.matches(DeepBean.class)); - assertFalse("Must be excluded: in wrong package", tpcf.matches(BeanFactory.class)); - assertFalse("Must be excluded: in wrong package", tpcf.matches(DefaultListableBeanFactory.class)); + void invocationOfMatchesMethodBlowsUpWhenNoTypePatternHasBeenSet() throws Exception { + assertThatIllegalStateException().isThrownBy(() -> new TypePatternClassFilter().matches(String.class)); } @Test - public void testSubclassMatching() { - TypePatternClassFilter tpcf = new TypePatternClassFilter("org.springframework.tests.sample.beans.ITestBean+"); - assertTrue("Must match: in package", tpcf.matches(TestBean.class)); - assertTrue("Must match: in package", tpcf.matches(ITestBean.class)); - assertTrue("Must match: in package", tpcf.matches(CountingTestBean.class)); - assertFalse("Must be excluded: not subclass", tpcf.matches(IOther.class)); - assertFalse("Must be excluded: not subclass", tpcf.matches(DefaultListableBeanFactory.class)); + void validPatternMatching() { + TypePatternClassFilter tpcf = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.*"); + + assertThat(tpcf.matches(TestBean.class)).as("Must match: in package").isTrue(); + assertThat(tpcf.matches(ITestBean.class)).as("Must match: in package").isTrue(); + assertThat(tpcf.matches(IOther.class)).as("Must match: in package").isTrue(); + + assertThat(tpcf.matches(DeepBean.class)).as("Must be excluded: in wrong package").isFalse(); + assertThat(tpcf.matches(BeanFactory.class)).as("Must be excluded: in wrong package").isFalse(); + assertThat(tpcf.matches(DefaultListableBeanFactory.class)).as("Must be excluded: in wrong package").isFalse(); + } + + @Test + void subclassMatching() { + TypePatternClassFilter tpcf = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.ITestBean+"); + + assertThat(tpcf.matches(TestBean.class)).as("Must match: in package").isTrue(); + assertThat(tpcf.matches(ITestBean.class)).as("Must match: in package").isTrue(); + assertThat(tpcf.matches(CountingTestBean.class)).as("Must match: in package").isTrue(); + + assertThat(tpcf.matches(IOther.class)).as("Must be excluded: not subclass").isFalse(); + assertThat(tpcf.matches(DefaultListableBeanFactory.class)).as("Must be excluded: not subclass").isFalse(); } @Test - public void testAndOrNotReplacement() { + void andOrNotReplacement() { TypePatternClassFilter tpcf = new TypePatternClassFilter("java.lang.Object or java.lang.String"); - assertFalse("matches Number",tpcf.matches(Number.class)); - assertTrue("matches Object",tpcf.matches(Object.class)); - assertTrue("matchesString",tpcf.matches(String.class)); + assertThat(tpcf.matches(Number.class)).as("matches Number").isFalse(); + assertThat(tpcf.matches(Object.class)).as("matches Object").isTrue(); + assertThat(tpcf.matches(String.class)).as("matchesString").isTrue(); + tpcf = new TypePatternClassFilter("java.lang.Number+ and java.lang.Float"); - assertTrue("matches Float",tpcf.matches(Float.class)); - assertFalse("matches Double",tpcf.matches(Double.class)); + assertThat(tpcf.matches(Float.class)).as("matches Float").isTrue(); + assertThat(tpcf.matches(Double.class)).as("matches Double").isFalse(); + tpcf = new TypePatternClassFilter("java.lang.Number+ and not java.lang.Float"); - assertFalse("matches Float",tpcf.matches(Float.class)); - assertTrue("matches Double",tpcf.matches(Double.class)); + assertThat(tpcf.matches(Float.class)).as("matches Float").isFalse(); + assertThat(tpcf.matches(Double.class)).as("matches Double").isTrue(); + } + + @Test + void testEquals() { + TypePatternClassFilter filter1 = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.*"); + TypePatternClassFilter filter2 = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.*"); + TypePatternClassFilter filter3 = new TypePatternClassFilter("org.springframework.tests.*"); + + assertThat(filter1).isEqualTo(filter2); + assertThat(filter1).isNotEqualTo(filter3); } - @Test(expected = IllegalArgumentException.class) - public void testSetTypePatternWithNullArgument() throws Exception { - new TypePatternClassFilter(null); + @Test + void testHashCode() { + TypePatternClassFilter filter1 = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.*"); + TypePatternClassFilter filter2 = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.*"); + TypePatternClassFilter filter3 = new TypePatternClassFilter("org.springframework.tests.*"); + + assertThat(filter1.hashCode()).isEqualTo(filter2.hashCode()); + assertThat(filter1.hashCode()).isNotEqualTo(filter3.hashCode()); } - @Test(expected = IllegalStateException.class) - public void testInvocationOfMatchesMethodBlowsUpWhenNoTypePatternHasBeenSet() throws Exception { - new TypePatternClassFilter().matches(String.class); + @Test + void testToString() { + TypePatternClassFilter filter1 = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.*"); + TypePatternClassFilter filter2 = new TypePatternClassFilter("org.springframework.beans.testfixture.beans.*"); + + assertThat(filter1.toString()) + .isEqualTo("org.springframework.aop.aspectj.TypePatternClassFilter: org.springframework.beans.testfixture.beans.*"); + assertThat(filter1.toString()).isEqualTo(filter2.toString()); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java index 46f27fdde05e..c7bad575019e 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -22,7 +22,7 @@ import java.lang.reflect.Method; import java.lang.reflect.UndeclaredThrowableException; import java.rmi.RemoteException; -import java.util.Collections; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -38,8 +38,8 @@ import org.aspectj.lang.annotation.DeclarePrecedence; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import test.aop.DefaultLockable; import test.aop.Lockable; import test.aop.PerTargetAspect; @@ -51,25 +51,29 @@ import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.aop.support.AopUtils; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.OrderComparator; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; import org.springframework.util.ObjectUtils; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** - * Abstract tests for AspectJAdvisorFactory. - * See subclasses for tests of concrete factories. + * Abstract tests for {@link AspectJAdvisorFactory} implementations. + * + *

See subclasses for tests of concrete factories. * * @author Rod Johnson * @author Chris Beams * @author Phillip Webb + * @author Sam Brannen */ -public abstract class AbstractAspectJAdvisorFactoryTests { +abstract class AbstractAspectJAdvisorFactoryTests { /** * To be overridden by concrete test subclasses. @@ -79,63 +83,55 @@ public abstract class AbstractAspectJAdvisorFactoryTests { @Test - public void testRejectsPerCflowAspect() { - try { - getFixture().getAdvisors( - new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean")); - fail("Cannot accept cflow"); - } - catch (AopConfigException ex) { - assertTrue(ex.getMessage().contains("PERCFLOW")); - } + void rejectsPerCflowAspect() { + assertThatExceptionOfType(AopConfigException.class).isThrownBy(() -> + getFixture().getAdvisors( + new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean"))) + .withMessageContaining("PERCFLOW"); } @Test - public void testRejectsPerCflowBelowAspect() { - try { - getFixture().getAdvisors( - new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean")); - fail("Cannot accept cflowbelow"); - } - catch (AopConfigException ex) { - assertTrue(ex.getMessage().contains("PERCFLOWBELOW")); - } + void rejectsPerCflowBelowAspect() { + assertThatExceptionOfType(AopConfigException.class).isThrownBy(() -> + getFixture().getAdvisors( + new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean"))) + .withMessageContaining("PERCFLOWBELOW"); } @Test - public void testPerTargetAspect() throws SecurityException, NoSuchMethodException { + void perTargetAspect() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")), TestBean.class); - assertEquals("Around advice must NOT apply", realAge, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge); Advised advised = (Advised) itb; ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1]; - assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); + assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue(); InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3]; LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory(); - assertFalse(maaif.isMaterialized()); + assertThat(maaif.isMaterialized()).isFalse(); // Check that the perclause pointcut is valid - assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); - assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut()); + assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue(); + assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut()); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); - assertTrue(maaif.isMaterialized()); + assertThat(maaif.isMaterialized()).isTrue(); - assertEquals("Around advice must apply", 0, itb.getAge()); - assertEquals("Around advice must apply", 1, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1); } @Test - public void testMultiplePerTargetAspects() throws SecurityException, NoSuchMethodException { + void multiplePerTargetAspects() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); @@ -150,20 +146,20 @@ public void testMultiplePerTargetAspects() throws SecurityException, NoSuchMetho aspect2.setOrder(5); advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2"))); - Collections.sort(advisors, new OrderComparator()); + OrderComparator.sort(advisors); TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class); - assertEquals("Around advice must NOT apply", realAge, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); - assertEquals("Around advice must apply", 0, itb.getAge()); - assertEquals("Around advice must apply", 1, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1); } @Test - public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException { + void multiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); @@ -176,147 +172,147 @@ public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityExc PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5(); advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2"))); - Collections.sort(advisors, new OrderComparator()); + OrderComparator.sort(advisors); TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class); - assertEquals("Around advice must NOT apply", realAge, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); - assertEquals("Around advice must apply", 0, itb.getAge()); - assertEquals("Around advice must apply", 1, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1); } @Test - public void testPerThisAspect() throws SecurityException, NoSuchMethodException { + void perThisAspect() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")), TestBean.class); - assertEquals("Around advice must NOT apply", realAge, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge); Advised advised = (Advised) itb; // Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors - assertEquals(4, advised.getAdvisors().length); + assertThat(advised.getAdvisors().length).isEqualTo(4); ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1]; - assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); + assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue(); InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2]; LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory(); - assertFalse(maaif.isMaterialized()); + assertThat(maaif.isMaterialized()).isFalse(); // Check that the perclause pointcut is valid - assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); - assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut()); + assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue(); + assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut()); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); - assertTrue(maaif.isMaterialized()); + assertThat(maaif.isMaterialized()).isTrue(); - assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null)); + assertThat(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null)).isTrue(); - assertEquals("Around advice must apply", 0, itb.getAge()); - assertEquals("Around advice must apply", 1, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1); } @Test - public void testPerTypeWithinAspect() throws SecurityException, NoSuchMethodException { + void perTypeWithinAspect() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory(); TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class); - assertEquals("No method calls", 0, aif.getInstantiationCount()); - assertEquals("Around advice must now apply", 0, itb.getAge()); + assertThat(aif.getInstantiationCount()).as("No method calls").isEqualTo(0); + assertThat(itb.getAge()).as("Around advice must now apply").isEqualTo(0); Advised advised = (Advised) itb; // Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors - assertEquals(4, advised.getAdvisors().length); + assertThat(advised.getAdvisors().length).isEqualTo(4); ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1]; - assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); + assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue(); InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2]; LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory(); - assertTrue(maaif.isMaterialized()); + assertThat(maaif.isMaterialized()).isTrue(); // Check that the perclause pointcut is valid - assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); - assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut()); + assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue(); + assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut()); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); - assertTrue(maaif.isMaterialized()); + assertThat(maaif.isMaterialized()).isTrue(); - assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null)); + assertThat(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null)).isTrue(); - assertEquals("Around advice must still apply", 1, itb.getAge()); - assertEquals("Around advice must still apply", 2, itb.getAge()); + assertThat(itb.getAge()).as("Around advice must still apply").isEqualTo(1); + assertThat(itb.getAge()).as("Around advice must still apply").isEqualTo(2); TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class); - assertEquals(1, aif.getInstantiationCount()); - assertEquals("Around advice be independent for second instance", 0, itb2.getAge()); - assertEquals(2, aif.getInstantiationCount()); + assertThat(aif.getInstantiationCount()).isEqualTo(1); + assertThat(itb2.getAge()).as("Around advice be independent for second instance").isEqualTo(0); + assertThat(aif.getInstantiationCount()).isEqualTo(2); } @Test - public void testNamedPointcutAspectWithFQN() { - testNamedPointcuts(new NamedPointcutAspectWithFQN()); + void namedPointcutAspectWithFQN() { + namedPointcuts(new NamedPointcutAspectWithFQN()); } @Test - public void testNamedPointcutAspectWithoutFQN() { - testNamedPointcuts(new NamedPointcutAspectWithoutFQN()); + void namedPointcutAspectWithoutFQN() { + namedPointcuts(new NamedPointcutAspectWithoutFQN()); } @Test - public void testNamedPointcutFromAspectLibrary() { - testNamedPointcuts(new NamedPointcutAspectFromLibrary()); + void namedPointcutFromAspectLibrary() { + namedPointcuts(new NamedPointcutAspectFromLibrary()); } @Test - public void testNamedPointcutFromAspectLibraryWithBinding() { + void namedPointcutFromAspectLibraryWithBinding() { TestBean target = new TestBean(); ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory( new NamedPointcutAspectFromLibraryWithBinding(), "someBean")), ITestBean.class); itb.setAge(10); - assertEquals("Around advice must apply", 20, itb.getAge()); - assertEquals(20,target.getAge()); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(20); + assertThat(target.getAge()).isEqualTo(20); } - private void testNamedPointcuts(Object aspectInstance) { + private void namedPointcuts(Object aspectInstance) { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspectInstance, "someBean")), ITestBean.class); - assertEquals("Around advice must apply", -1, itb.getAge()); - assertEquals(realAge, target.getAge()); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(-1); + assertThat(target.getAge()).isEqualTo(realAge); } @Test - public void testBindingWithSingleArg() { + void bindingWithSingleArg() { TestBean target = new TestBean(); ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new BindingAspectWithSingleArg(), "someBean")), ITestBean.class); itb.setAge(10); - assertEquals("Around advice must apply", 20, itb.getAge()); - assertEquals(20,target.getAge()); + assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(20); + assertThat(target.getAge()).isEqualTo(20); } @Test - public void testBindingWithMultipleArgsDifferentlyOrdered() { + void bindingWithMultipleArgsDifferentlyOrdered() { ManyValuedArgs target = new ManyValuedArgs(); ManyValuedArgs mva = (ManyValuedArgs) createProxy(target, getFixture().getAdvisors( @@ -329,57 +325,53 @@ public void testBindingWithMultipleArgsDifferentlyOrdered() { String d = "d"; StringBuffer e = new StringBuffer("stringbuf"); String expectedResult = a + b+ c + d + e; - assertEquals(expectedResult, mva.mungeArgs(a, b, c, d, e)); + assertThat(mva.mungeArgs(a, b, c, d, e)).isEqualTo(expectedResult); } /** * In this case the introduction will be made. */ @Test - public void testIntroductionOnTargetNotImplementingInterface() { + void introductionOnTargetNotImplementingInterface() { NotLockable notLockableTarget = new NotLockable(); - assertFalse(notLockableTarget instanceof Lockable); + assertThat(notLockableTarget instanceof Lockable).isFalse(); NotLockable notLockable1 = (NotLockable) createProxy(notLockableTarget, getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), NotLockable.class); - assertTrue(notLockable1 instanceof Lockable); + assertThat(notLockable1 instanceof Lockable).isTrue(); Lockable lockable = (Lockable) notLockable1; - assertFalse(lockable.locked()); + assertThat(lockable.locked()).isFalse(); lockable.lock(); - assertTrue(lockable.locked()); + assertThat(lockable.locked()).isTrue(); NotLockable notLockable2Target = new NotLockable(); NotLockable notLockable2 = (NotLockable) createProxy(notLockable2Target, getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), NotLockable.class); - assertTrue(notLockable2 instanceof Lockable); + assertThat(notLockable2 instanceof Lockable).isTrue(); Lockable lockable2 = (Lockable) notLockable2; - assertFalse(lockable2.locked()); + assertThat(lockable2.locked()).isFalse(); notLockable2.setIntValue(1); lockable2.lock(); - try { - notLockable2.setIntValue(32); - fail(); - } - catch (IllegalStateException ex) { - } - assertTrue(lockable2.locked()); + assertThatIllegalStateException().isThrownBy(() -> + notLockable2.setIntValue(32)); + assertThat(lockable2.locked()).isTrue(); } @Test - public void testIntroductionAdvisorExcludedFromTargetImplementingInterface() { - assertTrue(AopUtils.findAdvisorsThatCanApply( - getFixture().getAdvisors( - new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), - CannotBeUnlocked.class).isEmpty()); - assertEquals(2, AopUtils.findAdvisorsThatCanApply(getFixture().getAdvisors( - new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(),"someBean")), NotLockable.class).size()); + void introductionAdvisorExcludedFromTargetImplementingInterface() { + assertThat(AopUtils.findAdvisorsThatCanApply( + getFixture().getAdvisors( + new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), + CannotBeUnlocked.class).isEmpty()).isTrue(); + assertThat(AopUtils.findAdvisorsThatCanApply(getFixture().getAdvisors( + new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(),"someBean")), NotLockable.class).size()).isEqualTo(2); } @Test - public void testIntroductionOnTargetImplementingInterface() { + void introductionOnTargetImplementingInterface() { CannotBeUnlocked target = new CannotBeUnlocked(); Lockable proxy = (Lockable) createProxy(target, // Ensure that we exclude @@ -389,22 +381,17 @@ public void testIntroductionOnTargetImplementingInterface() { CannotBeUnlocked.class ), CannotBeUnlocked.class); - assertThat(proxy, instanceOf(Lockable.class)); + assertThat(proxy).isInstanceOf(Lockable.class); Lockable lockable = proxy; - assertTrue("Already locked", lockable.locked()); + assertThat(lockable.locked()).as("Already locked").isTrue(); lockable.lock(); - assertTrue("Real target ignores locking", lockable.locked()); - try { - lockable.unlock(); - fail(); - } - catch (UnsupportedOperationException ex) { - // Ok - } + assertThat(lockable.locked()).as("Real target ignores locking").isTrue(); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + lockable.unlock()); } @Test - public void testIntroductionOnTargetExcludedByTypePattern() { + void introductionOnTargetExcludedByTypePattern() { LinkedList target = new LinkedList<>(); List proxy = (List) createProxy(target, AopUtils.findAdvisorsThatCanApply( @@ -412,25 +399,25 @@ public void testIntroductionOnTargetExcludedByTypePattern() { List.class ), List.class); - assertFalse("Type pattern must have excluded mixin", proxy instanceof Lockable); + assertThat(proxy instanceof Lockable).as("Type pattern must have excluded mixin").isFalse(); } @Test - public void testIntroductionBasedOnAnnotationMatch_SPR5307() { + void introductionBasedOnAnnotationMatch_SPR5307() { AnnotatedTarget target = new AnnotatedTargetImpl(); List advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeAnnotatedTypeModifiable(), "someBean")); Object proxy = createProxy(target, advisors, AnnotatedTarget.class); System.out.println(advisors.get(1)); - assertTrue(proxy instanceof Lockable); + assertThat(proxy instanceof Lockable).isTrue(); Lockable lockable = (Lockable)proxy; lockable.locked(); } // TODO: Why does this test fail? It hasn't been run before, so it maybe never actually passed... @Test - @Ignore - public void testIntroductionWithArgumentBinding() { + @Disabled + void introductionWithArgumentBinding() { TestBean target = new TestBean(); List advisors = getFixture().getAdvisors( @@ -439,71 +426,54 @@ public void testIntroductionWithArgumentBinding() { new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean"))); Modifiable modifiable = (Modifiable) createProxy(target, advisors, ITestBean.class); - assertThat(modifiable, instanceOf(Modifiable.class)); + assertThat(modifiable).isInstanceOf(Modifiable.class); Lockable lockable = (Lockable) modifiable; - assertFalse(lockable.locked()); + assertThat(lockable.locked()).isFalse(); ITestBean itb = (ITestBean) modifiable; - assertFalse(modifiable.isModified()); + assertThat(modifiable.isModified()).isFalse(); int oldAge = itb.getAge(); itb.setAge(oldAge + 1); - assertTrue(modifiable.isModified()); + assertThat(modifiable.isModified()).isTrue(); modifiable.acceptChanges(); - assertFalse(modifiable.isModified()); + assertThat(modifiable.isModified()).isFalse(); itb.setAge(itb.getAge()); - assertFalse("Setting same value does not modify", modifiable.isModified()); + assertThat(modifiable.isModified()).as("Setting same value does not modify").isFalse(); itb.setName("And now for something completely different"); - assertTrue(modifiable.isModified()); + assertThat(modifiable.isModified()).isTrue(); lockable.lock(); - assertTrue(lockable.locked()); - try { - itb.setName("Else"); - fail("Should be locked"); - } - catch (IllegalStateException ex) { - // Ok - } + assertThat(lockable.locked()).isTrue(); + assertThatIllegalStateException().as("Should be locked").isThrownBy(() -> + itb.setName("Else")); lockable.unlock(); itb.setName("Tony"); } @Test - public void testAspectMethodThrowsExceptionLegalOnSignature() { + void aspectMethodThrowsExceptionLegalOnSignature() { TestBean target = new TestBean(); UnsupportedOperationException expectedException = new UnsupportedOperationException(); List advisors = getFixture().getAdvisors( - new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean")); - assertEquals("One advice method was found", 1, advisors.size()); + new SingletonMetadataAwareAspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean")); + assertThat(advisors.size()).as("One advice method was found").isEqualTo(1); ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class); - - try { - itb.getAge(); - fail(); - } - catch (UnsupportedOperationException ex) { - assertSame(expectedException, ex); - } + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy( + itb::getAge); } // TODO document this behaviour. // Is it different AspectJ behaviour, at least for checked exceptions? @Test - public void testAspectMethodThrowsExceptionIllegalOnSignature() { + void aspectMethodThrowsExceptionIllegalOnSignature() { TestBean target = new TestBean(); RemoteException expectedException = new RemoteException(); List advisors = getFixture().getAdvisors( - new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean")); - assertEquals("One advice method was found", 1, advisors.size()); + new SingletonMetadataAwareAspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean")); + assertThat(advisors.size()).as("One advice method was found").isEqualTo(1); ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class); - - try { - itb.getAge(); - fail(); - } - catch (UndeclaredThrowableException ex) { - assertSame(expectedException, ex.getCause()); - } + assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy( + itb::getAge).withCause(expectedException); } protected Object createProxy(Object target, List advisors, Class... interfaces) { @@ -524,49 +494,38 @@ protected Object createProxy(Object target, List advisors, Class... } @Test - public void testTwoAdvicesOnOneAspect() { + void twoAdvicesOnOneAspect() { TestBean target = new TestBean(); TwoAdviceAspect twoAdviceAspect = new TwoAdviceAspect(); List advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(twoAdviceAspect, "someBean")); - assertEquals("Two advice methods found", 2, advisors.size()); + assertThat(advisors.size()).as("Two advice methods found").isEqualTo(2); ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class); itb.setName(""); - assertEquals(0, itb.getAge()); + assertThat(itb.getAge()).isEqualTo(0); int newAge = 32; itb.setAge(newAge); - assertEquals(1, itb.getAge()); + assertThat(itb.getAge()).isEqualTo(1); } @Test - public void testAfterAdviceTypes() throws Exception { - Echo target = new Echo(); - ExceptionHandling afterReturningAspect = new ExceptionHandling(); + void afterAdviceTypes() throws Exception { + InvocationTrackingAspect aspect = new InvocationTrackingAspect(); List advisors = getFixture().getAdvisors( - new SingletonMetadataAwareAspectInstanceFactory(afterReturningAspect, "someBean")); - Echo echo = (Echo) createProxy(target, advisors, Echo.class); - assertEquals(0, afterReturningAspect.successCount); - assertEquals("", echo.echo("")); - assertEquals(1, afterReturningAspect.successCount); - assertEquals(0, afterReturningAspect.failureCount); + new SingletonMetadataAwareAspectInstanceFactory(aspect, "exceptionHandlingAspect")); + Echo echo = (Echo) createProxy(new Echo(), advisors, Echo.class); - try { - echo.echo(new FileNotFoundException()); - fail(); - } - catch (FileNotFoundException ex) { - // Ok - } - catch (Exception ex) { - fail(); - } - assertEquals(1, afterReturningAspect.successCount); - assertEquals(1, afterReturningAspect.failureCount); - assertEquals(afterReturningAspect.failureCount + afterReturningAspect.successCount, afterReturningAspect.afterCount); + assertThat(aspect.invocations).isEmpty(); + assertThat(echo.echo(42)).isEqualTo(42); + assertThat(aspect.invocations).containsExactly("around - start", "before", "after returning", "after", "around - end"); + + aspect.invocations.clear(); + assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> echo.echo(new FileNotFoundException())); + assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end"); } @Test - public void testFailureWithoutExplicitDeclarePrecedence() { + void failureWithoutExplicitDeclarePrecedence() { TestBean target = new TestBean(); MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory( new NoDeclarePrecedenceShouldFail(), "someBean"); @@ -575,38 +534,40 @@ public void testFailureWithoutExplicitDeclarePrecedence() { itb.getAge(); } - @Test(expected = IllegalArgumentException.class) - public void testDeclarePrecedenceNotSupported() { + @Test + void declarePrecedenceNotSupported() { TestBean target = new TestBean(); - MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory( - new DeclarePrecedenceShouldSucceed(), "someBean"); - createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class); + assertThatIllegalArgumentException().isThrownBy(() -> { + MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory( + new DeclarePrecedenceShouldSucceed(), "someBean"); + createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class); + }); } @Aspect("percflow(execution(* *(..)))") - public static class PerCflowAspect { + static class PerCflowAspect { } @Aspect("percflowbelow(execution(* *(..)))") - public static class PerCflowBelowAspect { + static class PerCflowBelowAspect { } @Aspect("pertarget(execution(* *.getSpouse()))") @Order(10) - public static class PerTargetAspectWithOrderAnnotation10 { + static class PerTargetAspectWithOrderAnnotation10 { - public int count; + int count; @Around("execution(int *.getAge())") - public int returnCountAsAge() { + int returnCountAsAge() { return count++; } @Before("execution(void *.set*(int))") - public void countSetter() { + void countSetter() { ++count; } } @@ -614,34 +575,34 @@ public void countSetter() { @Aspect("pertarget(execution(* *.getSpouse()))") @Order(5) - public static class PerTargetAspectWithOrderAnnotation5 { + static class PerTargetAspectWithOrderAnnotation5 { - public int count; + int count; @Around("execution(int *.getAge())") - public int returnCountAsAge() { + int returnCountAsAge() { return count++; } @Before("execution(void *.set*(int))") - public void countSetter() { + void countSetter() { ++count; } } - @Aspect("pertypewithin(org.springframework.tests.sample.beans.IOther+)") - public static class PerTypeWithinAspect { + @Aspect("pertypewithin(org.springframework.beans.testfixture.beans.IOther+)") + static class PerTypeWithinAspect { - public int count; + int count; @Around("execution(int *.getAge())") - public int returnCountAsAge() { + int returnCountAsAge() { return count++; } @Before("execution(void *.*(..))") - public void countAnythingVoid() { + void countAnythingVoid() { ++count; } } @@ -651,7 +612,7 @@ private class PerTypeWithinAspectInstanceFactory implements MetadataAwareAspectI private int count; - public int getInstantiationCount() { + int getInstantiationCount() { return this.count; } @@ -684,121 +645,121 @@ public int getOrder() { @Aspect - public static class NamedPointcutAspectWithFQN { + static class NamedPointcutAspectWithFQN { + @SuppressWarnings("unused") private ITestBean fieldThatShouldBeIgnoredBySpringAtAspectJProcessing = new TestBean(); @Pointcut("execution(* getAge())") - public void getAge() { + void getAge() { } @Around("org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.NamedPointcutAspectWithFQN.getAge()") - public int changeReturnValue(ProceedingJoinPoint pjp) { + int changeReturnValue(ProceedingJoinPoint pjp) { return -1; } } @Aspect - public static class NamedPointcutAspectWithoutFQN { + static class NamedPointcutAspectWithoutFQN { @Pointcut("execution(* getAge())") - public void getAge() { + void getAge() { } @Around("getAge()") - public int changeReturnValue(ProceedingJoinPoint pjp) { + int changeReturnValue(ProceedingJoinPoint pjp) { return -1; } } @Aspect - public static class NamedPointcutAspectFromLibrary { + static class NamedPointcutAspectFromLibrary { @Around("org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.Library.propertyAccess()") - public int changeReturnType(ProceedingJoinPoint pjp) { + int changeReturnType(ProceedingJoinPoint pjp) { return -1; } @Around(value="org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.Library.integerArgOperation(x)", argNames="x") - public void doubleArg(ProceedingJoinPoint pjp, int x) throws Throwable { + void doubleArg(ProceedingJoinPoint pjp, int x) throws Throwable { pjp.proceed(new Object[] {x*2}); } } @Aspect - public static class Library { + static class Library { @Pointcut("execution(!void get*())") - public void propertyAccess() {} + void propertyAccess() {} @Pointcut("execution(* *(..)) && args(i)") - public void integerArgOperation(int i) {} - + void integerArgOperation(int i) {} } @Aspect - public static class NamedPointcutAspectFromLibraryWithBinding { + static class NamedPointcutAspectFromLibraryWithBinding { @Around(value="org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.Library.integerArgOperation(x)", argNames="x") - public void doubleArg(ProceedingJoinPoint pjp, int x) throws Throwable { + void doubleArg(ProceedingJoinPoint pjp, int x) throws Throwable { pjp.proceed(new Object[] {x*2}); } } @Aspect - public static class BindingAspectWithSingleArg { + static class BindingAspectWithSingleArg { @Pointcut(value="args(a)", argNames="a") - public void setAge(int a) {} + void setAge(int a) {} @Around(value="setAge(age)",argNames="age") // @ArgNames({"age"}) // AMC needs more work here? ignoring pjp arg... ok?? - // argNames should be suported in Around as it is in Pointcut - public void changeReturnType(ProceedingJoinPoint pjp, int age) throws Throwable { + // argNames should be supported in Around as it is in Pointcut + void changeReturnType(ProceedingJoinPoint pjp, int age) throws Throwable { pjp.proceed(new Object[] {age*2}); } } @Aspect - public static class ManyValuedArgs { + static class ManyValuedArgs { - public String mungeArgs(String a, int b, int c, String d, StringBuffer e) { + String mungeArgs(String a, int b, int c, String d, StringBuffer e) { return a + b + c + d + e; } @Around(value="execution(String mungeArgs(..)) && args(a, b, c, d, e)", argNames="b,c,d,e,a") - public String reverseAdvice(ProceedingJoinPoint pjp, int b, int c, String d, StringBuffer e, String a) throws Throwable { - assertEquals(a + b+ c+ d+ e, pjp.proceed()); + String reverseAdvice(ProceedingJoinPoint pjp, int b, int c, String d, StringBuffer e, String a) throws Throwable { + assertThat(pjp.proceed()).isEqualTo(a + b+ c+ d+ e); return a + b + c + d + e; } } @Aspect - public static class ExceptionAspect { + static class ExceptionThrowingAspect { private final Exception ex; - public ExceptionAspect(Exception ex) { + ExceptionThrowingAspect(Exception ex) { this.ex = ex; } @Before("execution(* getAge())") - public void throwException() throws Exception { + void throwException() throws Exception { throw ex; } } - public static class Echo { + static class Echo { - public Object echo(Object o) throws Exception { + Object echo(Object o) throws Exception { if (o instanceof Exception) { throw (Exception) o; } @@ -808,45 +769,62 @@ public Object echo(Object o) throws Exception { @Aspect - public static class ExceptionHandling { + private static class InvocationTrackingAspect { + + List invocations = new ArrayList<>(); - public int successCount; - public int failureCount; + @Pointcut("execution(* echo(*))") + void echo() { + } + + @Around("echo()") + Object around(ProceedingJoinPoint joinPoint) throws Throwable { + invocations.add("around - start"); + try { + return joinPoint.proceed(); + } + finally { + invocations.add("around - end"); + } + } - public int afterCount; + @Before("echo()") + void before() { + invocations.add("before"); + } - @AfterReturning("execution(* echo(*))") - public void succeeded() { - ++successCount; + @AfterReturning("echo()") + void afterReturning() { + invocations.add("after returning"); } - @AfterThrowing("execution(* echo(*))") - public void failed() { - ++failureCount; + @AfterThrowing("echo()") + void afterThrowing() { + invocations.add("after throwing"); } - @After("execution(* echo(*))") - public void invoked() { - ++afterCount; + @After("echo()") + void after() { + invocations.add("after"); } } @Aspect - public static class NoDeclarePrecedenceShouldFail { + static class NoDeclarePrecedenceShouldFail { @Pointcut("execution(int *.getAge())") - public void getAge() { + void getAge() { } @Before("getAge()") - public void blowUpButDoesntMatterBecauseAroundAdviceWontLetThisBeInvoked() { + void blowUpButDoesntMatterBecauseAroundAdviceWontLetThisBeInvoked() { throw new IllegalStateException(); } @Around("getAge()") - public int preventExecution(ProceedingJoinPoint pjp) { + int preventExecution(ProceedingJoinPoint pjp) { return 666; } } @@ -854,19 +832,19 @@ public int preventExecution(ProceedingJoinPoint pjp) { @Aspect @DeclarePrecedence("test..*") - public static class DeclarePrecedenceShouldSucceed { + static class DeclarePrecedenceShouldSucceed { @Pointcut("execution(int *.getAge())") - public void getAge() { + void getAge() { } @Before("getAge()") - public void blowUpButDoesntMatterBecauseAroundAdviceWontLetThisBeInvoked() { + void blowUpButDoesntMatterBecauseAroundAdviceWontLetThisBeInvoked() { throw new IllegalStateException(); } @Around("getAge()") - public int preventExecution(ProceedingJoinPoint pjp) { + int preventExecution(ProceedingJoinPoint pjp) { return 666; } } @@ -884,12 +862,12 @@ public int preventExecution(ProceedingJoinPoint pjp) { @Aspect abstract class AbstractMakeModifiable { - public interface MutableModifable extends Modifiable { + interface MutableModifiable extends Modifiable { void markDirty(); } - public static class ModifiableImpl implements MutableModifable { + static class ModifiableImpl implements MutableModifiable { private boolean modified; @@ -910,8 +888,8 @@ public void markDirty() { } @Before(value="execution(void set*(*)) && this(modifiable) && args(newValue)", argNames="modifiable,newValue") - public void recordModificationIfSetterArgumentDiffersFromOldValue( - JoinPoint jp, MutableModifable mixin, Object newValue) { + void recordModificationIfSetterArgumentDiffersFromOldValue( + JoinPoint jp, MutableModifiable mixin, Object newValue) { /* * We use the mixin to check and, if necessary, change, @@ -970,9 +948,9 @@ private Method getGetterFromSetter(Method setter) { @Aspect class MakeITestBeanModifiable extends AbstractMakeModifiable { - @DeclareParents(value = "org.springframework.tests.sample.beans.ITestBean+", + @DeclareParents(value = "org.springframework.beans.testfixture.beans.ITestBean+", defaultImpl=ModifiableImpl.class) - public static MutableModifable mixin; + static MutableModifiable mixin; } @@ -987,7 +965,7 @@ class MakeAnnotatedTypeModifiable extends AbstractMakeModifiable { @DeclareParents(value = "(@org.springframework.aop.aspectj.annotation.Measured *)", defaultImpl = DefaultLockable.class) - public static Lockable mixin; + static Lockable mixin; } @@ -999,10 +977,10 @@ class MakeAnnotatedTypeModifiable extends AbstractMakeModifiable { class MakeLockable { @DeclareParents(value = "org.springframework..*", defaultImpl = DefaultLockable.class) - public static Lockable mixin; + static Lockable mixin; @Before(value="execution(void set*(*)) && this(mixin)", argNames="mixin") - public void checkNotLocked( Lockable mixin) { + void checkNotLocked( Lockable mixin) { // Can also obtain the mixin (this) this way //Lockable mixin = (Lockable) jp.getThis(); if (mixin.locked()) { @@ -1071,11 +1049,11 @@ class NotLockable { private int intValue; - public int getIntValue() { + int getIntValue() { return intValue; } - public void setIntValue(int intValue) { + void setIntValue(int intValue) { this.intValue = intValue; } @@ -1085,18 +1063,19 @@ public void setIntValue(int intValue) { @Aspect("perthis(execution(* *.getSpouse()))") class PerThisAspect { - public int count; + int count; // Just to check that this doesn't cause problems with introduction processing + @SuppressWarnings("unused") private ITestBean fieldThatShouldBeIgnoredBySpringAtAspectJProcessing = new TestBean(); @Around("execution(int *.getAge())") - public int returnCountAsAge() { + int returnCountAsAge() { return count++; } @Before("execution(void *.set*(int))") - public void countSetter() { + void countSetter() { ++count; } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ArgumentBindingTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ArgumentBindingTests.java index 08740b5ade62..835f4a4f95c3 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ArgumentBindingTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ArgumentBindingTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -24,13 +24,15 @@ import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Adrian Colyer @@ -39,24 +41,26 @@ */ public class ArgumentBindingTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testBindingInPointcutUsedByAdvice() { TestBean tb = new TestBean(); AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb); proxyFactory.addAspect(NamedPointcutWithArgs.class); ITestBean proxiedTestBean = proxyFactory.getProxy(); - proxiedTestBean.setName("Supercalifragalisticexpialidocious"); + assertThatIllegalArgumentException().isThrownBy(() -> + proxiedTestBean.setName("Supercalifragalisticexpialidocious")); } - @Test(expected = IllegalStateException.class) + @Test public void testAnnotationArgumentNameBinding() { TransactionalBean tb = new TransactionalBean(); AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb); proxyFactory.addAspect(PointcutWithAnnotationArgument.class); ITransactionalBean proxiedTestBean = proxyFactory.getProxy(); - proxiedTestBean.doInTransaction(); + assertThatIllegalStateException().isThrownBy( + proxiedTestBean::doInTransaction); } @Test @@ -67,8 +71,8 @@ public void testParameterNameDiscoverWithReferencePointcut() throws Exception { Method methodUsedForParameterTypeDiscovery = getClass().getMethod("methodWithOneParam", String.class); String[] pnames = discoverer.getParameterNames(methodUsedForParameterTypeDiscovery); - assertEquals("one parameter name", 1, pnames.length); - assertEquals("formal", pnames[0]); + assertThat(pnames.length).as("one parameter name").isEqualTo(1); + assertThat(pnames[0]).isEqualTo("formal"); } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectJPointcutAdvisorTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectJPointcutAdvisorTests.java index 2f84af9c415a..806f30587bbe 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectJPointcutAdvisorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectJPointcutAdvisorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -16,16 +16,18 @@ package org.springframework.aop.aspectj.annotation; -import org.junit.Test; +import org.junit.jupiter.api.Test; import test.aop.PerTargetAspect; import org.springframework.aop.Pointcut; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.aspectj.AspectJExpressionPointcutTests; +import org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.ExceptionThrowingAspect; import org.springframework.aop.framework.AopConfigException; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rod Johnson @@ -43,11 +45,11 @@ public void testSingleton() throws SecurityException, NoSuchMethodException { InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl( ajexp, TestBean.class.getMethod("getAge"), af, - new SingletonMetadataAwareAspectInstanceFactory(new AbstractAspectJAdvisorFactoryTests.ExceptionAspect(null), "someBean"), + new SingletonMetadataAwareAspectInstanceFactory(new ExceptionThrowingAspect(null), "someBean"), 1, "someBean"); - assertSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut()); - assertFalse(ajpa.isPerInstance()); + assertThat(ajpa.getAspectMetadata().getPerClausePointcut()).isSameAs(Pointcut.TRUE); + assertThat(ajpa.isPerInstance()).isFalse(); } @Test @@ -60,26 +62,29 @@ public void testPerTarget() throws SecurityException, NoSuchMethodException { new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean"), 1, "someBean"); - assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut()); - assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut); - assertTrue(ajpa.isPerInstance()); + assertThat(ajpa.getAspectMetadata().getPerClausePointcut()).isNotSameAs(Pointcut.TRUE); + boolean condition = ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut; + assertThat(condition).isTrue(); + assertThat(ajpa.isPerInstance()).isTrue(); - assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class)); - assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( - TestBean.class.getMethod("getAge"), TestBean.class)); + assertThat(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class)).isTrue(); + assertThat(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( + TestBean.class.getMethod("getAge"), TestBean.class)).isFalse(); - assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( - TestBean.class.getMethod("getSpouse"), TestBean.class)); + assertThat(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( + TestBean.class.getMethod("getSpouse"), TestBean.class)).isTrue(); } - @Test(expected = AopConfigException.class) + @Test public void testPerCflowTarget() { - testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowAspect.class); + assertThatExceptionOfType(AopConfigException.class).isThrownBy(() -> + testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowAspect.class)); } - @Test(expected = AopConfigException.class) + @Test public void testPerCflowBelowTarget() { - testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowBelowAspect.class); + assertThatExceptionOfType(AopConfigException.class).isThrownBy(() -> + testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowBelowAspect.class)); } private void testIllegalInstantiationModel(Class c) throws AopConfigException { diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectMetadataTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectMetadataTests.java index d6fa549d80d8..637baa2450a8 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectMetadataTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectMetadataTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -17,47 +17,57 @@ package org.springframework.aop.aspectj.annotation; import org.aspectj.lang.reflect.PerClauseKind; -import org.junit.Test; +import org.junit.jupiter.api.Test; import test.aop.PerTargetAspect; import org.springframework.aop.Pointcut; -import org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.ExceptionAspect; +import org.springframework.aop.aspectj.AspectJExpressionPointcut; +import org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.ExceptionThrowingAspect; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @since 2.0 * @author Rod Johnson * @author Chris Beams + * @author Sam Brannen */ -public class AspectMetadataTests { +class AspectMetadataTests { - @Test(expected = IllegalArgumentException.class) - public void testNotAnAspect() { - new AspectMetadata(String.class,"someBean"); + @Test + void notAnAspect() { + assertThatIllegalArgumentException().isThrownBy(() -> new AspectMetadata(String.class, "someBean")); } @Test - public void testSingletonAspect() { - AspectMetadata am = new AspectMetadata(ExceptionAspect.class,"someBean"); - assertFalse(am.isPerThisOrPerTarget()); - assertSame(Pointcut.TRUE, am.getPerClausePointcut()); - assertEquals(PerClauseKind.SINGLETON, am.getAjType().getPerClause().getKind()); + void singletonAspect() { + AspectMetadata am = new AspectMetadata(ExceptionThrowingAspect.class, "someBean"); + assertThat(am.isPerThisOrPerTarget()).isFalse(); + assertThat(am.getPerClausePointcut()).isSameAs(Pointcut.TRUE); + assertThat(am.getAjType().getPerClause().getKind()).isEqualTo(PerClauseKind.SINGLETON); } @Test - public void testPerTargetAspect() { - AspectMetadata am = new AspectMetadata(PerTargetAspect.class,"someBean"); - assertTrue(am.isPerThisOrPerTarget()); - assertNotSame(Pointcut.TRUE, am.getPerClausePointcut()); - assertEquals(PerClauseKind.PERTARGET, am.getAjType().getPerClause().getKind()); + void perTargetAspect() { + AspectMetadata am = new AspectMetadata(PerTargetAspect.class, "someBean"); + assertThat(am.isPerThisOrPerTarget()).isTrue(); + assertThat(am.getPerClausePointcut()).isNotSameAs(Pointcut.TRUE); + assertThat(am.getAjType().getPerClause().getKind()).isEqualTo(PerClauseKind.PERTARGET); + assertThat(am.getPerClausePointcut()).isInstanceOf(AspectJExpressionPointcut.class); + assertThat(((AspectJExpressionPointcut) am.getPerClausePointcut()).getExpression()) + .isEqualTo("execution(* *.getSpouse())"); } @Test - public void testPerThisAspect() { - AspectMetadata am = new AspectMetadata(PerThisAspect.class,"someBean"); - assertTrue(am.isPerThisOrPerTarget()); - assertNotSame(Pointcut.TRUE, am.getPerClausePointcut()); - assertEquals(PerClauseKind.PERTHIS, am.getAjType().getPerClause().getKind()); + void perThisAspect() { + AspectMetadata am = new AspectMetadata(PerThisAspect.class, "someBean"); + assertThat(am.isPerThisOrPerTarget()).isTrue(); + assertThat(am.getPerClausePointcut()).isNotSameAs(Pointcut.TRUE); + assertThat(am.getAjType().getPerClause().getKind()).isEqualTo(PerClauseKind.PERTHIS); + assertThat(am.getPerClausePointcut()).isInstanceOf(AspectJExpressionPointcut.class); + assertThat(((AspectJExpressionPointcut) am.getPerClausePointcut()).getExpression()) + .isEqualTo("execution(* *.getSpouse())"); } + } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectProxyFactoryTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectProxyFactoryTests.java index 0c4304c95ddb..d048e6e0d18c 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectProxyFactoryTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectProxyFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,12 +23,13 @@ import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.junit.Test; +import org.junit.jupiter.api.Test; import test.aop.PerThisAspect; -import org.springframework.util.SerializationTestUtils; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Rob Harrop @@ -37,10 +38,11 @@ */ public class AspectProxyFactoryTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testWithNonAspect() { AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean()); - proxyFactory.addAspect(TestBean.class); + assertThatIllegalArgumentException().isThrownBy(() -> + proxyFactory.addAspect(TestBean.class)); } @Test @@ -50,7 +52,7 @@ public void testWithSimpleAspect() throws Exception { AspectJProxyFactory proxyFactory = new AspectJProxyFactory(bean); proxyFactory.addAspect(MultiplyReturnValue.class); ITestBean proxy = proxyFactory.getProxy(); - assertEquals("Multiplication did not occur", bean.getAge() * 2, proxy.getAge()); + assertThat(proxy.getAge()).as("Multiplication did not occur").isEqualTo((bean.getAge() * 2)); } @Test @@ -67,16 +69,17 @@ public void testWithPerThisAspect() throws Exception { ITestBean proxy1 = pf1.getProxy(); ITestBean proxy2 = pf2.getProxy(); - assertEquals(0, proxy1.getAge()); - assertEquals(1, proxy1.getAge()); - assertEquals(0, proxy2.getAge()); - assertEquals(2, proxy1.getAge()); + assertThat(proxy1.getAge()).isEqualTo(0); + assertThat(proxy1.getAge()).isEqualTo(1); + assertThat(proxy2.getAge()).isEqualTo(0); + assertThat(proxy1.getAge()).isEqualTo(2); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWithInstanceWithNonAspect() throws Exception { AspectJProxyFactory pf = new AspectJProxyFactory(); - pf.addAspect(new TestBean()); + assertThatIllegalArgumentException().isThrownBy(() -> + pf.addAspect(new TestBean())); } @Test @@ -85,13 +88,12 @@ public void testSerializable() throws Exception { AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean()); proxyFactory.addAspect(LoggingAspectOnVarargs.class); ITestBean proxy = proxyFactory.getProxy(); - assertTrue(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)); + assertThat(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue(); ITestBean tb = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy); - assertTrue(tb.doWithVarargs(MyEnum.A, MyOtherEnum.C)); + assertThat(tb.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue(); } @Test - @SuppressWarnings("unchecked") public void testWithInstance() throws Exception { MultiplyReturnValue aspect = new MultiplyReturnValue(); int multiple = 3; @@ -104,16 +106,17 @@ public void testWithInstance() throws Exception { proxyFactory.addAspect(aspect); ITestBean proxy = proxyFactory.getProxy(); - assertEquals(target.getAge() * multiple, proxy.getAge()); + assertThat(proxy.getAge()).isEqualTo((target.getAge() * multiple)); ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy); - assertEquals(target.getAge() * multiple, serializedProxy.getAge()); + assertThat(serializedProxy.getAge()).isEqualTo((target.getAge() * multiple)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWithNonSingletonAspectInstance() throws Exception { AspectJProxyFactory pf = new AspectJProxyFactory(); - pf.addAspect(new PerThisAspect()); + assertThatIllegalArgumentException().isThrownBy(() -> + pf.addAspect(new PerThisAspect())); } @Test // SPR-13328 @@ -122,7 +125,7 @@ public void testProxiedVarargsWithEnumArray() throws Exception { AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean()); proxyFactory.addAspect(LoggingAspectOnVarargs.class); ITestBean proxy = proxyFactory.getProxy(); - assertTrue(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)); + assertThat(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue(); } @Test // SPR-13328 @@ -131,7 +134,7 @@ public void testUnproxiedVarargsWithEnumArray() throws Exception { AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean()); proxyFactory.addAspect(LoggingAspectOnSetter.class); ITestBean proxy = proxyFactory.getProxy(); - assertTrue(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)); + assertThat(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue(); } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactoryTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactoryTests.java index d89e34bea435..d649ea019642 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactoryTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -17,13 +17,14 @@ package org.springframework.aop.aspectj.annotation; /** - * Tests for ReflectiveAtAspectJAdvisorFactory. - * Tests are inherited: we only set the test fixture here. + * Tests for {@link ReflectiveAspectJAdvisorFactory}. + * + *

Tests are inherited: we only set the test fixture here. * * @author Rod Johnson * @since 2.0 */ -public class ReflectiveAspectJAdvisorFactoryTests extends AbstractAspectJAdvisorFactoryTests { +class ReflectiveAspectJAdvisorFactoryTests extends AbstractAspectJAdvisorFactoryTests { @Override protected AspectJAdvisorFactory getFixture() { diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJNamespaceHandlerTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJNamespaceHandlerTests.java index f334aec7a140..a6ecf37304de 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJNamespaceHandlerTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJNamespaceHandlerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,8 +16,8 @@ package org.springframework.aop.aspectj.autoproxy; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.config.AopConfigUtils; import org.springframework.aop.config.AopNamespaceUtils; @@ -29,9 +29,9 @@ import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlReaderContext; -import org.springframework.tests.beans.CollectingReaderEventListener; +import org.springframework.beans.testfixture.beans.CollectingReaderEventListener; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -46,7 +46,7 @@ public class AspectJNamespaceHandlerTests { private BeanDefinitionRegistry registry = new DefaultListableBeanFactory(); - @Before + @BeforeEach public void setUp() throws Exception { SourceExtractor sourceExtractor = new PassThroughSourceExtractor(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.registry); @@ -58,49 +58,46 @@ public void setUp() throws Exception { @Test public void testRegisterAutoProxyCreator() throws Exception { AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals("Incorrect number of definitions registered", 1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).as("Incorrect number of definitions registered").isEqualTo(1); AopNamespaceUtils.registerAspectJAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals("Incorrect number of definitions registered", 1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).as("Incorrect number of definitions registered").isEqualTo(1); } @Test public void testRegisterAspectJAutoProxyCreator() throws Exception { AopNamespaceUtils.registerAspectJAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals("Incorrect number of definitions registered", 1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).as("Incorrect number of definitions registered").isEqualTo(1); AopNamespaceUtils.registerAspectJAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals("Incorrect number of definitions registered", 1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).as("Incorrect number of definitions registered").isEqualTo(1); BeanDefinition definition = registry.getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME); - assertEquals("Incorrect APC class", - AspectJAwareAdvisorAutoProxyCreator.class.getName(), definition.getBeanClassName()); + assertThat(definition.getBeanClassName()).as("Incorrect APC class").isEqualTo(AspectJAwareAdvisorAutoProxyCreator.class.getName()); } @Test public void testRegisterAspectJAutoProxyCreatorWithExistingAutoProxyCreator() throws Exception { AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals(1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).isEqualTo(1); AopNamespaceUtils.registerAspectJAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals("Incorrect definition count", 1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).as("Incorrect definition count").isEqualTo(1); BeanDefinition definition = registry.getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME); - assertEquals("APC class not switched", - AspectJAwareAdvisorAutoProxyCreator.class.getName(), definition.getBeanClassName()); + assertThat(definition.getBeanClassName()).as("APC class not switched").isEqualTo(AspectJAwareAdvisorAutoProxyCreator.class.getName()); } @Test public void testRegisterAutoProxyCreatorWhenAspectJAutoProxyCreatorAlreadyExists() throws Exception { AopNamespaceUtils.registerAspectJAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals(1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).isEqualTo(1); AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(this.parserContext, null); - assertEquals("Incorrect definition count", 1, registry.getBeanDefinitionCount()); + assertThat(registry.getBeanDefinitionCount()).as("Incorrect definition count").isEqualTo(1); BeanDefinition definition = registry.getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME); - assertEquals("Incorrect APC class", - AspectJAwareAdvisorAutoProxyCreator.class.getName(), definition.getBeanClassName()); + assertThat(definition.getBeanClassName()).as("Incorrect APC class").isEqualTo(AspectJAwareAdvisorAutoProxyCreator.class.getName()); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparatorTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparatorTests.java index 752ae467772e..d391d45f39bd 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparatorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparatorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,8 +18,8 @@ import java.lang.reflect.Method; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.Advisor; import org.springframework.aop.AfterReturningAdvice; @@ -33,9 +33,8 @@ import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice; import org.springframework.aop.aspectj.AspectJPointcutAdvisor; import org.springframework.aop.support.DefaultPointcutAdvisor; -import org.springframework.lang.Nullable; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Adrian Colyer @@ -56,7 +55,7 @@ public class AspectJPrecedenceComparatorTests { private AspectJExpressionPointcut anyOldPointcut; - @Before + @BeforeEach public void setUp() throws Exception { this.comparator = new AspectJPrecedenceComparator(); this.anyOldMethod = getClass().getMethods()[0]; @@ -69,95 +68,95 @@ public void setUp() throws Exception { public void testSameAspectNoAfterAdvice() { Advisor advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); - assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted before advisor2").isEqualTo(-1); advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); - assertEquals("advisor2 sorted before advisor1", 1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor2 sorted before advisor1").isEqualTo(1); } @Test public void testSameAspectAfterAdvice() { Advisor advisor1 = createAspectJAfterAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); - assertEquals("advisor2 sorted before advisor1", 1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor2 sorted before advisor1").isEqualTo(1); advisor1 = createAspectJAfterReturningAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); advisor2 = createAspectJAfterThrowingAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); - assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted before advisor2").isEqualTo(-1); } @Test public void testSameAspectOneOfEach() { Advisor advisor1 = createAspectJAfterAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); - assertEquals("advisor1 and advisor2 not comparable", 1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 and advisor2 not comparable").isEqualTo(1); } @Test public void testSameAdvisorPrecedenceDifferentAspectNoAfterAdvice() { Advisor advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("nothing to say about order here", 0, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("nothing to say about order here").isEqualTo(0); advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("nothing to say about order here", 0, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("nothing to say about order here").isEqualTo(0); } @Test public void testSameAdvisorPrecedenceDifferentAspectAfterAdvice() { Advisor advisor1 = createAspectJAfterAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("nothing to say about order here", 0, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("nothing to say about order here").isEqualTo(0); advisor1 = createAspectJAfterReturningAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); advisor2 = createAspectJAfterThrowingAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("nothing to say about order here", 0, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("nothing to say about order here").isEqualTo(0); } @Test public void testHigherAdvisorPrecedenceNoAfterAdvice() { Advisor advisor1 = createSpringAOPBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER); Advisor advisor2 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted before advisor2").isEqualTo(-1); advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); advisor2 = createAspectJAroundAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted before advisor2").isEqualTo(-1); } @Test public void testHigherAdvisorPrecedenceAfterAdvice() { Advisor advisor1 = createAspectJAfterAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJAroundAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted before advisor2").isEqualTo(-1); advisor1 = createAspectJAfterReturningAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); advisor2 = createAspectJAfterThrowingAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor2 sorted after advisor1", -1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor2 sorted after advisor1").isEqualTo(-1); } @Test public void testLowerAdvisorPrecedenceNoAfterAdvice() { Advisor advisor1 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor1 sorted after advisor2", 1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1); advisor1 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect"); advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor1 sorted after advisor2", 1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1); } @Test public void testLowerAdvisorPrecedenceAfterAdvice() { Advisor advisor1 = createAspectJAfterAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect"); Advisor advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor1 sorted after advisor2", 1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1); advisor1 = createSpringAOPAfterAdvice(LOW_PRECEDENCE_ADVISOR_ORDER); advisor2 = createAspectJAfterThrowingAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect"); - assertEquals("advisor1 sorted after advisor2", 1, this.comparator.compare(advisor1, advisor2)); + assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1); } @@ -195,10 +194,7 @@ private Advisor createAspectJAdvice(int advisorOrder, int adviceDeclarationOrder } private Advisor createSpringAOPAfterAdvice(int order) { - AfterReturningAdvice advice = new AfterReturningAdvice() { - @Override - public void afterReturning(@Nullable Object returnValue, Method method, Object[] args, @Nullable Object target) throws Throwable { - } + AfterReturningAdvice advice = (returnValue, method, args, target) -> { }; DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(this.anyOldPointcut, advice); advisor.setOrder(order); diff --git a/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerEventTests.java b/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerEventTests.java index fc3dd377f4f6..d4c774424d04 100644 --- a/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerEventTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerEventTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,8 +19,8 @@ import java.util.HashSet; import java.util.Set; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanReference; @@ -29,11 +29,11 @@ import org.springframework.beans.factory.parsing.CompositeComponentDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.testfixture.beans.CollectingReaderEventListener; import org.springframework.core.io.Resource; -import org.springframework.tests.beans.CollectingReaderEventListener; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rob Harrop @@ -56,7 +56,7 @@ public class AopNamespaceHandlerEventTests { private XmlBeanDefinitionReader reader; - @Before + @BeforeEach public void setup() { this.reader = new XmlBeanDefinitionReader(this.beanFactory); this.reader.setEventListener(this.eventListener); @@ -67,14 +67,15 @@ public void setup() { public void testPointcutEvents() { this.reader.loadBeanDefinitions(POINTCUT_EVENTS_CONTEXT); ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions(); - assertEquals("Incorrect number of events fired", 1, componentDefinitions.length); - assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition); + assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(1); + boolean condition = componentDefinitions[0] instanceof CompositeComponentDefinition; + assertThat(condition).as("No holder with nested components").isTrue(); CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0]; - assertEquals("aop:config", compositeDef.getName()); + assertThat(compositeDef.getName()).isEqualTo("aop:config"); ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents(); - assertEquals("Incorrect number of inner components", 2, nestedComponentDefs.length); + assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(2); PointcutComponentDefinition pcd = null; for (ComponentDefinition componentDefinition : nestedComponentDefs) { if (componentDefinition instanceof PointcutComponentDefinition) { @@ -82,22 +83,23 @@ public void testPointcutEvents() { break; } } - assertNotNull("PointcutComponentDefinition not found", pcd); - assertEquals("Incorrect number of BeanDefinitions", 1, pcd.getBeanDefinitions().length); + assertThat(pcd).as("PointcutComponentDefinition not found").isNotNull(); + assertThat(pcd.getBeanDefinitions().length).as("Incorrect number of BeanDefinitions").isEqualTo(1); } @Test public void testAdvisorEventsWithPointcutRef() { this.reader.loadBeanDefinitions(POINTCUT_REF_CONTEXT); ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions(); - assertEquals("Incorrect number of events fired", 2, componentDefinitions.length); + assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(2); - assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition); + boolean condition1 = componentDefinitions[0] instanceof CompositeComponentDefinition; + assertThat(condition1).as("No holder with nested components").isTrue(); CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0]; - assertEquals("aop:config", compositeDef.getName()); + assertThat(compositeDef.getName()).isEqualTo("aop:config"); ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents(); - assertEquals("Incorrect number of inner components", 3, nestedComponentDefs.length); + assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(3); AdvisorComponentDefinition acd = null; for (int i = 0; i < nestedComponentDefs.length; i++) { ComponentDefinition componentDefinition = nestedComponentDefs[i]; @@ -106,27 +108,29 @@ public void testAdvisorEventsWithPointcutRef() { break; } } - assertNotNull("AdvisorComponentDefinition not found", acd); - assertEquals(1, acd.getBeanDefinitions().length); - assertEquals(2, acd.getBeanReferences().length); + assertThat(acd).as("AdvisorComponentDefinition not found").isNotNull(); + assertThat(acd.getBeanDefinitions().length).isEqualTo(1); + assertThat(acd.getBeanReferences().length).isEqualTo(2); - assertTrue("No advice bean found", componentDefinitions[1] instanceof BeanComponentDefinition); + boolean condition = componentDefinitions[1] instanceof BeanComponentDefinition; + assertThat(condition).as("No advice bean found").isTrue(); BeanComponentDefinition adviceDef = (BeanComponentDefinition) componentDefinitions[1]; - assertEquals("countingAdvice", adviceDef.getBeanName()); + assertThat(adviceDef.getBeanName()).isEqualTo("countingAdvice"); } @Test public void testAdvisorEventsWithDirectPointcut() { this.reader.loadBeanDefinitions(DIRECT_POINTCUT_EVENTS_CONTEXT); ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions(); - assertEquals("Incorrect number of events fired", 2, componentDefinitions.length); + assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(2); - assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition); + boolean condition1 = componentDefinitions[0] instanceof CompositeComponentDefinition; + assertThat(condition1).as("No holder with nested components").isTrue(); CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0]; - assertEquals("aop:config", compositeDef.getName()); + assertThat(compositeDef.getName()).isEqualTo("aop:config"); ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents(); - assertEquals("Incorrect number of inner components", 2, nestedComponentDefs.length); + assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(2); AdvisorComponentDefinition acd = null; for (int i = 0; i < nestedComponentDefs.length; i++) { ComponentDefinition componentDefinition = nestedComponentDefs[i]; @@ -135,27 +139,29 @@ public void testAdvisorEventsWithDirectPointcut() { break; } } - assertNotNull("AdvisorComponentDefinition not found", acd); - assertEquals(2, acd.getBeanDefinitions().length); - assertEquals(1, acd.getBeanReferences().length); + assertThat(acd).as("AdvisorComponentDefinition not found").isNotNull(); + assertThat(acd.getBeanDefinitions().length).isEqualTo(2); + assertThat(acd.getBeanReferences().length).isEqualTo(1); - assertTrue("No advice bean found", componentDefinitions[1] instanceof BeanComponentDefinition); + boolean condition = componentDefinitions[1] instanceof BeanComponentDefinition; + assertThat(condition).as("No advice bean found").isTrue(); BeanComponentDefinition adviceDef = (BeanComponentDefinition) componentDefinitions[1]; - assertEquals("countingAdvice", adviceDef.getBeanName()); + assertThat(adviceDef.getBeanName()).isEqualTo("countingAdvice"); } @Test public void testAspectEvent() { this.reader.loadBeanDefinitions(CONTEXT); ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions(); - assertEquals("Incorrect number of events fired", 5, componentDefinitions.length); + assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(5); - assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition); + boolean condition = componentDefinitions[0] instanceof CompositeComponentDefinition; + assertThat(condition).as("No holder with nested components").isTrue(); CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0]; - assertEquals("aop:config", compositeDef.getName()); + assertThat(compositeDef.getName()).isEqualTo("aop:config"); ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents(); - assertEquals("Incorrect number of inner components", 2, nestedComponentDefs.length); + assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(2); AspectComponentDefinition acd = null; for (ComponentDefinition componentDefinition : nestedComponentDefs) { if (componentDefinition instanceof AspectComponentDefinition) { @@ -164,11 +170,11 @@ public void testAspectEvent() { } } - assertNotNull("AspectComponentDefinition not found", acd); + assertThat(acd).as("AspectComponentDefinition not found").isNotNull(); BeanDefinition[] beanDefinitions = acd.getBeanDefinitions(); - assertEquals(5, beanDefinitions.length); + assertThat(beanDefinitions.length).isEqualTo(5); BeanReference[] beanReferences = acd.getBeanReferences(); - assertEquals(6, beanReferences.length); + assertThat(beanReferences.length).isEqualTo(6); Set expectedReferences = new HashSet<>(); expectedReferences.add("pc"); @@ -176,17 +182,19 @@ public void testAspectEvent() { for (BeanReference beanReference : beanReferences) { expectedReferences.remove(beanReference.getBeanName()); } - assertEquals("Incorrect references found", 0, expectedReferences.size()); + assertThat(expectedReferences.size()).as("Incorrect references found").isEqualTo(0); for (int i = 1; i < componentDefinitions.length; i++) { - assertTrue(componentDefinitions[i] instanceof BeanComponentDefinition); + boolean condition1 = componentDefinitions[i] instanceof BeanComponentDefinition; + assertThat(condition1).isTrue(); } ComponentDefinition[] nestedComponentDefs2 = acd.getNestedComponents(); - assertEquals("Inner PointcutComponentDefinition not found", 1, nestedComponentDefs2.length); - assertTrue(nestedComponentDefs2[0] instanceof PointcutComponentDefinition); + assertThat(nestedComponentDefs2.length).as("Inner PointcutComponentDefinition not found").isEqualTo(1); + boolean condition1 = nestedComponentDefs2[0] instanceof PointcutComponentDefinition; + assertThat(condition1).isTrue(); PointcutComponentDefinition pcd = (PointcutComponentDefinition) nestedComponentDefs2[0]; - assertEquals("Incorrect number of BeanDefinitions", 1, pcd.getBeanDefinitions().length); + assertThat(pcd.getBeanDefinitions().length).as("Incorrect number of BeanDefinitions").isEqualTo(1); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests.java b/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests.java index 91c1607d0f38..6a01b03ad7b2 100644 --- a/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,15 +16,15 @@ package org.springframework.aop.config; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Mark Fisher @@ -34,28 +34,20 @@ public class AopNamespaceHandlerPointcutErrorTests { @Test public void testDuplicatePointcutConfig() { - try { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(bf).loadBeanDefinitions( - qualifiedResource(getClass(), "pointcutDuplication.xml")); - fail("parsing should have caused a BeanDefinitionStoreException"); - } - catch (BeanDefinitionStoreException ex) { - assertTrue(ex.contains(BeanDefinitionParsingException.class)); - } + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + qualifiedResource(getClass(), "pointcutDuplication.xml"))) + .satisfies(ex -> ex.contains(BeanDefinitionParsingException.class)); } @Test public void testMissingPointcutConfig() { - try { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(bf).loadBeanDefinitions( - qualifiedResource(getClass(), "pointcutMissing.xml")); - fail("parsing should have caused a BeanDefinitionStoreException"); - } - catch (BeanDefinitionStoreException ex) { - assertTrue(ex.contains(BeanDefinitionParsingException.class)); - } + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + qualifiedResource(getClass(), "pointcutMissing.xml"))) + .satisfies(ex -> ex.contains(BeanDefinitionParsingException.class)); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/config/TopLevelAopTagTests.java b/spring-aop/src/test/java/org/springframework/aop/config/TopLevelAopTagTests.java index a0c220d079f9..f1ab7f273db9 100644 --- a/spring-aop/src/test/java/org/springframework/aop/config/TopLevelAopTagTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/config/TopLevelAopTagTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,14 +16,13 @@ package org.springframework.aop.config; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Tests that the <aop:config/> element can be used as a top level element. @@ -33,15 +32,13 @@ */ public class TopLevelAopTagTests { - private static final Resource CONTEXT = qualifiedResource(TopLevelAopTagTests.class, "context.xml"); - @Test - public void testParse() throws Exception { + public void testParse() { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); - reader.loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions( + qualifiedResource(TopLevelAopTagTests.class, "context.xml")); - assertTrue(beanFactory.containsBeanDefinition("testPointcut")); + assertThat(beanFactory.containsBeanDefinition("testPointcut")).isTrue(); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/AopProxyUtilsTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/AopProxyUtilsTests.java index 42b12f6649d2..c8475794f337 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/AopProxyUtilsTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/AopProxyUtilsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,13 +20,14 @@ import java.util.Arrays; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.SpringProxy; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Rod Johnson @@ -38,10 +39,10 @@ public class AopProxyUtilsTests { public void testCompleteProxiedInterfacesWorksWithNull() { AdvisedSupport as = new AdvisedSupport(); Class[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); - assertEquals(2, completedInterfaces.length); + assertThat(completedInterfaces.length).isEqualTo(2); List ifaces = Arrays.asList(completedInterfaces); - assertTrue(ifaces.contains(Advised.class)); - assertTrue(ifaces.contains(SpringProxy.class)); + assertThat(ifaces.contains(Advised.class)).isTrue(); + assertThat(ifaces.contains(SpringProxy.class)).isTrue(); } @Test @@ -49,7 +50,7 @@ public void testCompleteProxiedInterfacesWorksWithNullOpaque() { AdvisedSupport as = new AdvisedSupport(); as.setOpaque(true); Class[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); - assertEquals(1, completedInterfaces.length); + assertThat(completedInterfaces.length).isEqualTo(1); } @Test @@ -58,13 +59,13 @@ public void testCompleteProxiedInterfacesAdvisedNotIncluded() { as.addInterface(ITestBean.class); as.addInterface(Comparable.class); Class[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); - assertEquals(4, completedInterfaces.length); + assertThat(completedInterfaces.length).isEqualTo(4); // Can't assume ordering for others, so use a list List l = Arrays.asList(completedInterfaces); - assertTrue(l.contains(Advised.class)); - assertTrue(l.contains(ITestBean.class)); - assertTrue(l.contains(Comparable.class)); + assertThat(l.contains(Advised.class)).isTrue(); + assertThat(l.contains(ITestBean.class)).isTrue(); + assertThat(l.contains(Comparable.class)).isTrue(); } @Test @@ -74,13 +75,13 @@ public void testCompleteProxiedInterfacesAdvisedIncluded() { as.addInterface(Comparable.class); as.addInterface(Advised.class); Class[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); - assertEquals(4, completedInterfaces.length); + assertThat(completedInterfaces.length).isEqualTo(4); // Can't assume ordering for others, so use a list List l = Arrays.asList(completedInterfaces); - assertTrue(l.contains(Advised.class)); - assertTrue(l.contains(ITestBean.class)); - assertTrue(l.contains(Comparable.class)); + assertThat(l.contains(Advised.class)).isTrue(); + assertThat(l.contains(ITestBean.class)).isTrue(); + assertThat(l.contains(Comparable.class)).isTrue(); } @Test @@ -90,13 +91,13 @@ public void testCompleteProxiedInterfacesAdvisedNotIncludedOpaque() { as.addInterface(ITestBean.class); as.addInterface(Comparable.class); Class[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); - assertEquals(3, completedInterfaces.length); + assertThat(completedInterfaces.length).isEqualTo(3); // Can't assume ordering for others, so use a list List l = Arrays.asList(completedInterfaces); - assertFalse(l.contains(Advised.class)); - assertTrue(l.contains(ITestBean.class)); - assertTrue(l.contains(Comparable.class)); + assertThat(l.contains(Advised.class)).isFalse(); + assertThat(l.contains(ITestBean.class)).isTrue(); + assertThat(l.contains(Comparable.class)).isTrue(); } @Test @@ -106,8 +107,8 @@ public void testProxiedUserInterfacesWithSingleInterface() { pf.addInterface(ITestBean.class); Object proxy = pf.getProxy(); Class[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); - assertEquals(1, userInterfaces.length); - assertEquals(ITestBean.class, userInterfaces[0]); + assertThat(userInterfaces.length).isEqualTo(1); + assertThat(userInterfaces[0]).isEqualTo(ITestBean.class); } @Test @@ -118,16 +119,17 @@ public void testProxiedUserInterfacesWithMultipleInterfaces() { pf.addInterface(Comparable.class); Object proxy = pf.getProxy(); Class[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); - assertEquals(2, userInterfaces.length); - assertEquals(ITestBean.class, userInterfaces[0]); - assertEquals(Comparable.class, userInterfaces[1]); + assertThat(userInterfaces.length).isEqualTo(2); + assertThat(userInterfaces[0]).isEqualTo(ITestBean.class); + assertThat(userInterfaces[1]).isEqualTo(Comparable.class); } - @Test(expected = IllegalArgumentException.class) + @Test public void testProxiedUserInterfacesWithNoInterface() { Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[0], (proxy1, method, args) -> null); - AopProxyUtils.proxiedUserInterfaces(proxy); + assertThatIllegalArgumentException().isThrownBy(() -> + AopProxyUtils.proxiedUserInterfaces(proxy)); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/ClassWithConstructor.java b/spring-aop/src/test/java/org/springframework/aop/framework/ClassWithConstructor.java index aa52feefa882..dfbd090e8da6 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/ClassWithConstructor.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/ClassWithConstructor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/IntroductionBenchmarkTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/IntroductionBenchmarkTests.java index 46ff6077c427..d3defccfb429 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/IntroductionBenchmarkTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/IntroductionBenchmarkTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -16,11 +16,11 @@ package org.springframework.aop.framework; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.support.DelegatingIntroductionInterceptor; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.util.StopWatch; /** diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/MethodInvocationTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/MethodInvocationTests.java index a85cc95ab43f..91091035aac7 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/MethodInvocationTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/MethodInvocationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,60 +17,50 @@ package org.springframework.aop.framework; import java.lang.reflect.Method; -import java.util.LinkedList; +import java.util.Collections; import java.util.List; import org.aopalliance.intercept.MethodInterceptor; -import org.aopalliance.intercept.MethodInvocation; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson * @author Chris Beams * @since 14.03.2003 */ -public class MethodInvocationTests { +class MethodInvocationTests { @Test - public void testValidInvocation() throws Throwable { - Method m = Object.class.getMethod("hashCode"); + void testValidInvocation() throws Throwable { + Method method = Object.class.getMethod("hashCode"); Object proxy = new Object(); - final Object returnValue = new Object(); - List is = new LinkedList<>(); - is.add(new MethodInterceptor() { - @Override - public Object invoke(MethodInvocation invocation) throws Throwable { - return returnValue; - } - }); - ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, null, //? - m, null, null, is // list - ); + Object returnValue = new Object(); + List interceptors = Collections.singletonList((MethodInterceptor) invocation -> returnValue); + ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, null, method, null, null, interceptors); Object rv = invocation.proceed(); - assertTrue("correct response", rv == returnValue); + assertThat(rv).as("correct response").isSameAs(returnValue); } /** * toString on target can cause failure. */ @Test - public void testToStringDoesntHitTarget() throws Throwable { + void testToStringDoesntHitTarget() throws Throwable { Object target = new TestBean() { @Override public String toString() { throw new UnsupportedOperationException("toString"); } }; - List is = new LinkedList<>(); + List interceptors = Collections.emptyList(); Method m = Object.class.getMethod("hashCode"); Object proxy = new Object(); - ReflectiveMethodInvocation invocation = - new ReflectiveMethodInvocation(proxy, target, m, null, null, is); + ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, m, null, null, interceptors); // If it hits target, the test will fail with the UnsupportedOpException // in the inner class above. diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/NullPrimitiveTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/NullPrimitiveTests.java index e138781702ad..047e84b3d72f 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/NullPrimitiveTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/NullPrimitiveTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,14 +17,11 @@ package org.springframework.aop.framework; import org.aopalliance.intercept.MethodInterceptor; -import org.aopalliance.intercept.MethodInvocation; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import org.springframework.aop.AopInvocationException; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Test for SPR-4675. A null value returned from around advice is very hard to debug if @@ -34,10 +31,7 @@ */ public class NullPrimitiveTests { - @Rule - public ExpectedException thrown = ExpectedException.none(); - - static interface Foo { + interface Foo { int getValue(); } @@ -53,18 +47,13 @@ public int getValue() { SimpleFoo target = new SimpleFoo(); ProxyFactory factory = new ProxyFactory(target); - factory.addAdvice(new MethodInterceptor() { - @Override - public Object invoke(MethodInvocation invocation) throws Throwable { - return null; - } - }); + factory.addAdvice((MethodInterceptor) invocation -> null); Foo foo = (Foo) factory.getProxy(); - thrown.expect(AopInvocationException.class); - thrown.expectMessage("Foo.getValue()"); - assertEquals(0, foo.getValue()); + assertThatExceptionOfType(AopInvocationException.class).isThrownBy(() -> + foo.getValue()) + .withMessageContaining("Foo.getValue()"); } public static class Bar { @@ -78,18 +67,13 @@ public void testNullPrimitiveWithCglibProxy() { Bar target = new Bar(); ProxyFactory factory = new ProxyFactory(target); - factory.addAdvice(new MethodInterceptor() { - @Override - public Object invoke(MethodInvocation invocation) throws Throwable { - return null; - } - }); + factory.addAdvice((MethodInterceptor) invocation -> null); Bar bar = (Bar) factory.getProxy(); - thrown.expect(AopInvocationException.class); - thrown.expectMessage("Bar.getValue()"); - assertEquals(0, bar.getValue()); + assertThatExceptionOfType(AopInvocationException.class).isThrownBy(() -> + bar.getValue()) + .withMessageContaining("Bar.getValue()"); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/PrototypeTargetTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/PrototypeTargetTests.java index 455778c83e82..d58a9b594a16 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/PrototypeTargetTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/PrototypeTargetTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,14 +18,14 @@ import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.Resource; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Juergen Hoeller @@ -36,6 +36,7 @@ public class PrototypeTargetTests { private static final Resource CONTEXT = qualifiedResource(PrototypeTargetTests.class, "context.xml"); + @Test public void testPrototypeProxyWithPrototypeTarget() { TestBeanImpl.constructionCount = 0; @@ -46,8 +47,8 @@ public void testPrototypeProxyWithPrototypeTarget() { tb.doSomething(); } TestInterceptor interceptor = (TestInterceptor) bf.getBean("testInterceptor"); - assertEquals(10, TestBeanImpl.constructionCount); - assertEquals(10, interceptor.invocationCount); + assertThat(TestBeanImpl.constructionCount).isEqualTo(10); + assertThat(interceptor.invocationCount).isEqualTo(10); } @Test @@ -60,16 +61,19 @@ public void testSingletonProxyWithPrototypeTarget() { tb.doSomething(); } TestInterceptor interceptor = (TestInterceptor) bf.getBean("testInterceptor"); - assertEquals(1, TestBeanImpl.constructionCount); - assertEquals(10, interceptor.invocationCount); + assertThat(TestBeanImpl.constructionCount).isEqualTo(1); + assertThat(interceptor.invocationCount).isEqualTo(10); } - public static interface TestBean { - public void doSomething(); + + public interface TestBean { + + void doSomething(); } public static class TestBeanImpl implements TestBean { + private static int constructionCount = 0; public TestBeanImpl() { @@ -83,6 +87,7 @@ public void doSomething() { public static class TestInterceptor implements MethodInterceptor { + private int invocationCount = 0; @Override diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java index 06f54994b537..78cfdcde54a1 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -18,32 +18,33 @@ import java.util.ArrayList; import java.util.List; + import javax.accessibility.Accessible; import javax.swing.JFrame; import javax.swing.RootPaneContainer; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.aop.Advisor; import org.springframework.aop.interceptor.DebugInterceptor; import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.DefaultIntroductionAdvisor; import org.springframework.aop.support.DefaultPointcutAdvisor; -import org.springframework.aop.support.DelegatingIntroductionInterceptor; +import org.springframework.aop.testfixture.advice.CountingBeforeAdvice; +import org.springframework.aop.testfixture.interceptor.NopInterceptor; +import org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor; +import org.springframework.beans.testfixture.beans.IOther; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.Order; -import org.springframework.tests.TimeStamped; -import org.springframework.tests.aop.advice.CountingBeforeAdvice; -import org.springframework.tests.aop.interceptor.NopInterceptor; -import org.springframework.tests.sample.beans.IOther; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.core.testfixture.TimeStamped; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Also tests AdvisedSupport and ProxyCreatorSupport superclasses. @@ -65,10 +66,10 @@ public void testIndexOfMethods() { // Can use advised and ProxyFactory interchangeably advised.addAdvice(nop); pf.addAdvisor(advisor); - assertEquals(-1, pf.indexOf(new NopInterceptor())); - assertEquals(0, pf.indexOf(nop)); - assertEquals(1, pf.indexOf(advisor)); - assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null))); + assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1); + assertThat(pf.indexOf(nop)).isEqualTo(0); + assertThat(pf.indexOf(advisor)).isEqualTo(1); + assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1); } @Test @@ -82,13 +83,13 @@ public void testRemoveAdvisorByReference() { pf.addAdvisor(advisor); ITestBean proxied = (ITestBean) pf.getProxy(); proxied.setAge(5); - assertEquals(1, cba.getCalls()); - assertEquals(1, nop.getCount()); - assertTrue(pf.removeAdvisor(advisor)); - assertEquals(5, proxied.getAge()); - assertEquals(1, cba.getCalls()); - assertEquals(2, nop.getCount()); - assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null))); + assertThat(cba.getCalls()).isEqualTo(1); + assertThat(nop.getCount()).isEqualTo(1); + assertThat(pf.removeAdvisor(advisor)).isTrue(); + assertThat(proxied.getAge()).isEqualTo(5); + assertThat(cba.getCalls()).isEqualTo(1); + assertThat(nop.getCount()).isEqualTo(2); + assertThat(pf.removeAdvisor(new DefaultPointcutAdvisor(null))).isFalse(); } @Test @@ -104,21 +105,21 @@ public void testRemoveAdvisorByIndex() { pf.addAdvice(nop2); ITestBean proxied = (ITestBean) pf.getProxy(); proxied.setAge(5); - assertEquals(1, cba.getCalls()); - assertEquals(1, nop.getCount()); - assertEquals(1, nop2.getCount()); + assertThat(cba.getCalls()).isEqualTo(1); + assertThat(nop.getCount()).isEqualTo(1); + assertThat(nop2.getCount()).isEqualTo(1); // Removes counting before advisor pf.removeAdvisor(1); - assertEquals(5, proxied.getAge()); - assertEquals(1, cba.getCalls()); - assertEquals(2, nop.getCount()); - assertEquals(2, nop2.getCount()); + assertThat(proxied.getAge()).isEqualTo(5); + assertThat(cba.getCalls()).isEqualTo(1); + assertThat(nop.getCount()).isEqualTo(2); + assertThat(nop2.getCount()).isEqualTo(2); // Removes Nop1 pf.removeAdvisor(0); - assertEquals(5, proxied.getAge()); - assertEquals(1, cba.getCalls()); - assertEquals(2, nop.getCount()); - assertEquals(3, nop2.getCount()); + assertThat(proxied.getAge()).isEqualTo(5); + assertThat(cba.getCalls()).isEqualTo(1); + assertThat(nop.getCount()).isEqualTo(2); + assertThat(nop2.getCount()).isEqualTo(3); // Check out of bounds try { @@ -135,8 +136,8 @@ public void testRemoveAdvisorByIndex() { // Ok } - assertEquals(5, proxied.getAge()); - assertEquals(4, nop2.getCount()); + assertThat(proxied.getAge()).isEqualTo(5); + assertThat(nop2.getCount()).isEqualTo(4); } @Test @@ -155,37 +156,34 @@ public void testReplaceAdvisor() { // Replace etc methods on advised should be same as on ProxyFactory Advised advised = (Advised) proxied; proxied.setAge(5); - assertEquals(1, cba1.getCalls()); - assertEquals(0, cba2.getCalls()); - assertEquals(1, nop.getCount()); - assertFalse(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2)); - assertTrue(advised.replaceAdvisor(advisor1, advisor2)); - assertEquals(advisor2, pf.getAdvisors()[0]); - assertEquals(5, proxied.getAge()); - assertEquals(1, cba1.getCalls()); - assertEquals(2, nop.getCount()); - assertEquals(1, cba2.getCalls()); - assertFalse(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)); + assertThat(cba1.getCalls()).isEqualTo(1); + assertThat(cba2.getCalls()).isEqualTo(0); + assertThat(nop.getCount()).isEqualTo(1); + assertThat(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2)).isFalse(); + assertThat(advised.replaceAdvisor(advisor1, advisor2)).isTrue(); + assertThat(pf.getAdvisors()[0]).isEqualTo(advisor2); + assertThat(proxied.getAge()).isEqualTo(5); + assertThat(cba1.getCalls()).isEqualTo(1); + assertThat(nop.getCount()).isEqualTo(2); + assertThat(cba2.getCalls()).isEqualTo(1); + assertThat(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)).isFalse(); } @Test public void testAddRepeatedInterface() { - TimeStamped tst = new TimeStamped() { - @Override - public long getTimeStamp() { - throw new UnsupportedOperationException("getTimeStamp"); - } + TimeStamped tst = () -> { + throw new UnsupportedOperationException("getTimeStamp"); }; ProxyFactory pf = new ProxyFactory(tst); // We've already implicitly added this interface. // This call should be ignored without error pf.addInterface(TimeStamped.class); // All cool - assertThat(pf.getProxy(), instanceOf(TimeStamped.class)); + assertThat(pf.getProxy()).isInstanceOf(TimeStamped.class); } @Test - public void testGetsAllInterfaces() throws Exception { + public void testGetsAllInterfaces() { // Extend to get new interface class TestBeanSubclass extends TestBean implements Comparable { @Override @@ -196,12 +194,11 @@ public int compareTo(Object arg0) { TestBeanSubclass raw = new TestBeanSubclass(); ProxyFactory factory = new ProxyFactory(raw); //System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ",")); - assertEquals("Found correct number of interfaces", 5, factory.getProxiedInterfaces().length); + assertThat(factory.getProxiedInterfaces().length).as("Found correct number of interfaces").isEqualTo(5); ITestBean tb = (ITestBean) factory.getProxy(); - assertThat("Picked up secondary interface", tb, instanceOf(IOther.class)); - + assertThat(tb).as("Picked up secondary interface").isInstanceOf(IOther.class); raw.setAge(25); - assertTrue(tb.getAge() == raw.getAge()); + assertThat(tb.getAge() == raw.getAge()).isTrue(); long t = 555555L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t); @@ -211,10 +208,10 @@ public int compareTo(Object arg0) { factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); Class[] newProxiedInterfaces = factory.getProxiedInterfaces(); - assertEquals("Advisor proxies one more interface after introduction", oldProxiedInterfaces.length + 1, newProxiedInterfaces.length); + assertThat(newProxiedInterfaces.length).as("Advisor proxies one more interface after introduction").isEqualTo(oldProxiedInterfaces.length + 1); TimeStamped ts = (TimeStamped) factory.getProxy(); - assertTrue(ts.getTimeStamp() == t); + assertThat(ts.getTimeStamp() == t).isTrue(); // Shouldn't fail; ((IOther) ts).absquatulate(); } @@ -232,15 +229,15 @@ public Object invoke(MethodInvocation invocation) throws Throwable { NopInterceptor diUnused = new NopInterceptor(); ProxyFactory factory = new ProxyFactory(new TestBean()); factory.addAdvice(0, di); - assertThat(factory.getProxy(), instanceOf(ITestBean.class)); - assertTrue(factory.adviceIncluded(di)); - assertTrue(!factory.adviceIncluded(diUnused)); - assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 1); - assertTrue(factory.countAdvicesOfType(MyInterceptor.class) == 0); + assertThat(factory.getProxy()).isInstanceOf(ITestBean.class); + assertThat(factory.adviceIncluded(di)).isTrue(); + assertThat(!factory.adviceIncluded(diUnused)).isTrue(); + assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 1).isTrue(); + assertThat(factory.countAdvicesOfType(MyInterceptor.class) == 0).isTrue(); factory.addAdvice(0, diUnused); - assertTrue(factory.adviceIncluded(diUnused)); - assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 2); + assertThat(factory.adviceIncluded(diUnused)).isTrue(); + assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 2).isTrue(); } /** @@ -250,8 +247,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable { public void testCanAddAndRemoveAspectInterfacesOnSingleton() { ProxyFactory config = new ProxyFactory(new TestBean()); - assertFalse("Shouldn't implement TimeStamped before manipulation", - config.getProxy() instanceof TimeStamped); + assertThat(config.getProxy() instanceof TimeStamped).as("Shouldn't implement TimeStamped before manipulation").isFalse(); long time = 666L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); @@ -261,41 +257,36 @@ public void testCanAddAndRemoveAspectInterfacesOnSingleton() { int oldCount = config.getAdvisors().length; config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); - assertTrue(config.getAdvisors().length == oldCount + 1); + assertThat(config.getAdvisors().length == oldCount + 1).isTrue(); TimeStamped ts = (TimeStamped) config.getProxy(); - assertTrue(ts.getTimeStamp() == time); + assertThat(ts.getTimeStamp() == time).isTrue(); // Can remove config.removeAdvice(ti); - assertTrue(config.getAdvisors().length == oldCount); + assertThat(config.getAdvisors().length == oldCount).isTrue(); - try { - // Existing reference will fail - ts.getTimeStamp(); - fail("Existing object won't implement this interface any more"); - } - catch (RuntimeException ex) { - } + assertThatExceptionOfType(RuntimeException.class) + .as("Existing object won't implement this interface any more") + .isThrownBy(ts::getTimeStamp); // Existing reference will fail - assertFalse("Should no longer implement TimeStamped", - config.getProxy() instanceof TimeStamped); + assertThat(config.getProxy() instanceof TimeStamped).as("Should no longer implement TimeStamped").isFalse(); // Now check non-effect of removing interceptor that isn't there config.removeAdvice(new DebugInterceptor()); - assertTrue(config.getAdvisors().length == oldCount); + assertThat(config.getAdvisors().length == oldCount).isTrue(); ITestBean it = (ITestBean) ts; DebugInterceptor debugInterceptor = new DebugInterceptor(); config.addAdvice(0, debugInterceptor); it.getSpouse(); - assertEquals(1, debugInterceptor.getCount()); + assertThat(debugInterceptor.getCount()).isEqualTo(1); config.removeAdvice(debugInterceptor); it.getSpouse(); // not invoked again - assertTrue(debugInterceptor.getCount() == 1); + assertThat(debugInterceptor.getCount() == 1).isTrue(); } @Test @@ -303,15 +294,15 @@ public void testProxyTargetClassWithInterfaceAsTarget() { ProxyFactory pf = new ProxyFactory(); pf.setTargetClass(ITestBean.class); Object proxy = pf.getProxy(); - assertTrue("Proxy is a JDK proxy", AopUtils.isJdkDynamicProxy(proxy)); - assertTrue(proxy instanceof ITestBean); - assertEquals(ITestBean.class, AopProxyUtils.ultimateTargetClass(proxy)); + assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue(); + assertThat(proxy instanceof ITestBean).isTrue(); + assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(ITestBean.class); ProxyFactory pf2 = new ProxyFactory(proxy); Object proxy2 = pf2.getProxy(); - assertTrue("Proxy is a JDK proxy", AopUtils.isJdkDynamicProxy(proxy2)); - assertTrue(proxy2 instanceof ITestBean); - assertEquals(ITestBean.class, AopProxyUtils.ultimateTargetClass(proxy2)); + assertThat(AopUtils.isJdkDynamicProxy(proxy2)).as("Proxy is a JDK proxy").isTrue(); + assertThat(proxy2 instanceof ITestBean).isTrue(); + assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(ITestBean.class); } @Test @@ -319,26 +310,26 @@ public void testProxyTargetClassWithConcreteClassAsTarget() { ProxyFactory pf = new ProxyFactory(); pf.setTargetClass(TestBean.class); Object proxy = pf.getProxy(); - assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy)); - assertTrue(proxy instanceof TestBean); - assertEquals(TestBean.class, AopProxyUtils.ultimateTargetClass(proxy)); + assertThat(AopUtils.isCglibProxy(proxy)).as("Proxy is a CGLIB proxy").isTrue(); + assertThat(proxy instanceof TestBean).isTrue(); + assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(TestBean.class); ProxyFactory pf2 = new ProxyFactory(proxy); pf2.setProxyTargetClass(true); Object proxy2 = pf2.getProxy(); - assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy2)); - assertTrue(proxy2 instanceof TestBean); - assertEquals(TestBean.class, AopProxyUtils.ultimateTargetClass(proxy2)); + assertThat(AopUtils.isCglibProxy(proxy2)).as("Proxy is a CGLIB proxy").isTrue(); + assertThat(proxy2 instanceof TestBean).isTrue(); + assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(TestBean.class); } @Test - @Ignore("Not implemented yet, see http://jira.springframework.org/browse/SPR-5708") + @Disabled("Not implemented yet, see https://jira.springframework.org/browse/SPR-5708") public void testExclusionOfNonPublicInterfaces() { JFrame frame = new JFrame(); ProxyFactory proxyFactory = new ProxyFactory(frame); Object proxy = proxyFactory.getProxy(); - assertTrue(proxy instanceof RootPaneContainer); - assertTrue(proxy instanceof Accessible); + assertThat(proxy instanceof RootPaneContainer).isTrue(); + assertThat(proxy instanceof Accessible).isTrue(); } @Test @@ -349,8 +340,8 @@ public void testInterfaceProxiesCanBeOrderedThroughAnnotations() { list.add(proxy1); list.add(proxy2); AnnotationAwareOrderComparator.sort(list); - assertSame(proxy2, list.get(0)); - assertSame(proxy1, list.get(1)); + assertThat(list.get(0)).isSameAs(proxy2); + assertThat(list.get(1)).isSameAs(proxy1); } @Test @@ -365,42 +356,18 @@ public void testTargetClassProxiesCanBeOrderedThroughAnnotations() { list.add(proxy1); list.add(proxy2); AnnotationAwareOrderComparator.sort(list); - assertSame(proxy2, list.get(0)); - assertSame(proxy1, list.get(1)); + assertThat(list.get(0)).isSameAs(proxy2); + assertThat(list.get(1)).isSameAs(proxy1); } @Test public void testInterceptorWithoutJoinpoint() { final TestBean target = new TestBean("tb"); ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, (MethodInterceptor) invocation -> { - assertNull(invocation.getThis()); + assertThat(invocation.getThis()).isNull(); return invocation.getMethod().invoke(target, invocation.getArguments()); }); - assertEquals("tb", proxy.getName()); - } - - - @SuppressWarnings("serial") - private static class TimestampIntroductionInterceptor extends DelegatingIntroductionInterceptor - implements TimeStamped { - - private long ts; - - public TimestampIntroductionInterceptor() { - } - - public TimestampIntroductionInterceptor(long ts) { - this.ts = ts; - } - - public void setTime(long ts) { - this.ts = ts; - } - - @Override - public long getTimeStamp() { - return ts; - } + assertThat(proxy.getName()).isEqualTo("tb"); } diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptorTests.java index 50768689dd58..2c1060274e74 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,19 +17,19 @@ package org.springframework.aop.framework.adapter; import java.io.FileNotFoundException; -import java.io.IOException; -import java.lang.reflect.Method; import java.rmi.ConnectException; import java.rmi.RemoteException; import org.aopalliance.intercept.MethodInvocation; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.aop.ThrowsAdvice; -import org.springframework.tests.aop.advice.MethodCounter; +import org.springframework.aop.testfixture.advice.MyThrowsHandler; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * @author Rod Johnson @@ -37,10 +37,11 @@ */ public class ThrowsAdviceInterceptorTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testNoHandlerMethods() { // should require one handler method at least - new ThrowsAdviceInterceptor(new Object()); + assertThatIllegalArgumentException().isThrownBy(() -> + new ThrowsAdviceInterceptor(new Object())); } @Test @@ -50,26 +51,22 @@ public void testNotInvoked() throws Throwable { Object ret = new Object(); MethodInvocation mi = mock(MethodInvocation.class); given(mi.proceed()).willReturn(ret); - assertEquals(ret, ti.invoke(mi)); - assertEquals(0, th.getCalls()); + assertThat(ti.invoke(mi)).isEqualTo(ret); + assertThat(th.getCalls()).isEqualTo(0); } @Test public void testNoHandlerMethodForThrowable() throws Throwable { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); - assertEquals(2, ti.getHandlerMethodCount()); + assertThat(ti.getHandlerMethodCount()).isEqualTo(2); Exception ex = new Exception(); MethodInvocation mi = mock(MethodInvocation.class); given(mi.proceed()).willThrow(ex); - try { - ti.invoke(mi); - fail(); - } - catch (Exception caught) { - assertEquals(ex, caught); - } - assertEquals(0, th.getCalls()); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> + ti.invoke(mi)) + .isSameAs(ex); + assertThat(th.getCalls()).isEqualTo(0); } @Test @@ -81,15 +78,11 @@ public void testCorrectHandlerUsed() throws Throwable { given(mi.getMethod()).willReturn(Object.class.getMethod("hashCode")); given(mi.getThis()).willReturn(new Object()); given(mi.proceed()).willThrow(ex); - try { - ti.invoke(mi); - fail(); - } - catch (Exception caught) { - assertEquals(ex, caught); - } - assertEquals(1, th.getCalls()); - assertEquals(1, th.getCalls("ioException")); + assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> + ti.invoke(mi)) + .isSameAs(ex); + assertThat(th.getCalls()).isEqualTo(1); + assertThat(th.getCalls("ioException")).isEqualTo(1); } @Test @@ -100,15 +93,11 @@ public void testCorrectHandlerUsedForSubclass() throws Throwable { ConnectException ex = new ConnectException(""); MethodInvocation mi = mock(MethodInvocation.class); given(mi.proceed()).willThrow(ex); - try { - ti.invoke(mi); - fail(); - } - catch (Exception caught) { - assertEquals(ex, caught); - } - assertEquals(1, th.getCalls()); - assertEquals(1, th.getCalls("remoteException")); + assertThatExceptionOfType(ConnectException.class).isThrownBy(() -> + ti.invoke(mi)) + .isSameAs(ex); + assertThat(th.getCalls()).isEqualTo(1); + assertThat(th.getCalls("remoteException")).isEqualTo(1); } @Test @@ -129,34 +118,11 @@ public void afterThrowing(RemoteException ex) throws Throwable { ConnectException ex = new ConnectException(""); MethodInvocation mi = mock(MethodInvocation.class); given(mi.proceed()).willThrow(ex); - try { - ti.invoke(mi); - fail(); - } - catch (Throwable caught) { - assertEquals(t, caught); - } - assertEquals(1, th.getCalls()); - assertEquals(1, th.getCalls("remoteException")); - } - - - @SuppressWarnings("serial") - static class MyThrowsHandler extends MethodCounter implements ThrowsAdvice { - - // Full method signature - public void afterThrowing(Method m, Object[] args, Object target, IOException ex) { - count("ioException"); - } - - public void afterThrowing(RemoteException ex) throws Throwable { - count("remoteException"); - } - - /** Not valid, wrong number of arguments */ - public void afterThrowing(Method m, Exception ex) throws Throwable { - throw new UnsupportedOperationException("Shouldn't be called"); - } + assertThatExceptionOfType(Throwable.class).isThrownBy(() -> + ti.invoke(mi)) + .isSameAs(t); + assertThat(th.getCalls()).isEqualTo(1); + assertThat(th.getCalls("remoteException")).isEqualTo(1); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptorTests.java index 0aa8ae54e59b..43e4a93ded88 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/ConcurrencyThrottleInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,16 +18,16 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.tests.sample.beans.DerivedTestBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; +import org.springframework.beans.testfixture.beans.DerivedTestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -58,7 +58,7 @@ public void testSerializable() throws Exception { Advised advised = (Advised) serializedProxy; ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice(); - assertEquals(cti.getConcurrencyLimit(), serializedCti.getConcurrencyLimit()); + assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit()); serializedProxy.getAge(); } diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java index 56574025faf5..d5fcae4bd3c7 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,15 @@ import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; /** * @author Rob Harrop @@ -31,52 +36,60 @@ */ public class CustomizableTraceInterceptorTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testSetEmptyEnterMessage() { // Must not be able to set empty enter message - new CustomizableTraceInterceptor().setEnterMessage(""); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setEnterMessage("")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetEnterMessageWithReturnValuePlaceholder() { // Must not be able to set enter message with return value placeholder - new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetEnterMessageWithExceptionPlaceholder() { // Must not be able to set enter message with exception placeholder - new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetEnterMessageWithInvocationTimePlaceholder() { // Must not be able to set enter message with invocation time placeholder - new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetEmptyExitMessage() { // Must not be able to set empty exit message - new CustomizableTraceInterceptor().setExitMessage(""); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setExitMessage("")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetExitMessageWithExceptionPlaceholder() { // Must not be able to set exit message with exception placeholder - new CustomizableTraceInterceptor().setExitMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setExitMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetEmptyExceptionMessage() { // Must not be able to set empty exception message - new CustomizableTraceInterceptor().setExceptionMessage(""); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setExceptionMessage("")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetExceptionMethodWithReturnValuePlaceholder() { // Must not be able to set exception message with return value placeholder - new CustomizableTraceInterceptor().setExceptionMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomizableTraceInterceptor().setExceptionMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE)); } @Test @@ -109,12 +122,8 @@ public void testExceptionPathLogsCorrectly() throws Throwable { given(log.isTraceEnabled()).willReturn(true); CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log); - try { - interceptor.invoke(methodInvocation); - fail("Must have propagated the IllegalArgumentException."); - } - catch (IllegalArgumentException expected) { - } + assertThatIllegalArgumentException().isThrownBy(() -> + interceptor.invoke(methodInvocation)); verify(log).trace(anyString()); verify(log).trace(anyString(), eq(exception)); diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/DebugInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/DebugInterceptorTests.java index 4d09708cdbf3..427b9cbf845f 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/DebugInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/DebugInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,16 @@ import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; /** * Unit tests for the {@link DebugInterceptor} class. @@ -58,12 +64,8 @@ public void testExceptionPathStillLogsCorrectly() throws Throwable { given(log.isTraceEnabled()).willReturn(true); DebugInterceptor interceptor = new StubDebugInterceptor(log); - try { - interceptor.invoke(methodInvocation); - fail("Must have propagated the IllegalArgumentException."); - } - catch (IllegalArgumentException expected) { - } + assertThatIllegalArgumentException().isThrownBy(() -> + interceptor.invoke(methodInvocation)); checkCallCountTotal(interceptor); verify(log).trace(anyString()); @@ -71,7 +73,7 @@ public void testExceptionPathStillLogsCorrectly() throws Throwable { } private void checkCallCountTotal(DebugInterceptor interceptor) { - assertEquals("Intercepted call count not being incremented correctly", 1, interceptor.getCount()); + assertThat(interceptor.getCount()).as("Intercepted call count not being incremented correctly").isEqualTo(1); } diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisorsTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisorsTests.java index 26ddf28ce6d1..9bd43d1aef3f 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisorsTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisorsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,14 +16,14 @@ package org.springframework.aop.interceptor; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.NamedBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -40,7 +40,7 @@ public RequiresBeanNameBoundTestBean(String beanName) { @Override public int getAge() { - assertEquals(beanName, ExposeBeanNameAdvisors.getBeanName()); + assertThat(ExposeBeanNameAdvisors.getBeanName()).isEqualTo(beanName); return super.getAge(); } } @@ -54,7 +54,8 @@ public void testNoIntroduction() { pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(beanName)); ITestBean proxy = (ITestBean) pf.getProxy(); - assertFalse("No introduction", proxy instanceof NamedBean); + boolean condition = proxy instanceof NamedBean; + assertThat(condition).as("No introduction").isFalse(); // Requires binding proxy.getAge(); } @@ -68,12 +69,13 @@ public void testWithIntroduction() { pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorIntroducingNamedBean(beanName)); ITestBean proxy = (ITestBean) pf.getProxy(); - assertTrue("Introduction was made", proxy instanceof NamedBean); + boolean condition = proxy instanceof NamedBean; + assertThat(condition).as("Introduction was made").isTrue(); // Requires binding proxy.getAge(); NamedBean nb = (NamedBean) proxy; - assertEquals("Name returned correctly", beanName, nb.getBeanName()); + assertThat(nb.getBeanName()).as("Name returned correctly").isEqualTo(beanName); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests.java index 38f0bd9501f2..40f266f64710 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,16 +17,15 @@ package org.springframework.aop.interceptor; import org.aopalliance.intercept.MethodInvocation; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Non-XML tests are in AbstractAopProxyTests @@ -36,18 +35,16 @@ */ public class ExposeInvocationInterceptorTests { - private static final Resource CONTEXT = - qualifiedResource(ExposeInvocationInterceptorTests.class, "context.xml"); - @Test public void testXmlConfig() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + qualifiedResource(ExposeInvocationInterceptorTests.class, "context.xml")); ITestBean tb = (ITestBean) bf.getBean("proxy"); String name = "tony"; tb.setName(name); // Fires context checks - assertEquals(name, tb.getName()); + assertThat(tb.getName()).isEqualTo(name); } } @@ -74,10 +71,10 @@ public void absquatulate() { class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean { + @Override protected void assertions(MethodInvocation invocation) { - assertTrue(invocation.getThis() == this); - assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(), - ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())); + assertThat(invocation.getThis() == this).isTrue(); + assertThat(ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())).as("Invocation should be on ITestBean: " + invocation.getMethod()).isTrue(); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptorTests.java index 262390d3ec5e..18556d6df509 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,12 +19,14 @@ import com.jamonapi.MonitorFactory; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * @author Steve Souza @@ -39,12 +41,12 @@ public class JamonPerformanceMonitorInterceptorTests { private final Log log = mock(Log.class); - @Before + @BeforeEach public void setUp() { MonitorFactory.reset(); } - @After + @AfterEach public void tearDown() { MonitorFactory.reset(); } @@ -56,9 +58,8 @@ public void testInvokeUnderTraceWithNormalProcessing() throws Throwable { interceptor.invokeUnderTrace(mi, log); - assertTrue("jamon must track the method being invoked", MonitorFactory.getNumRows() > 0); - assertTrue("The jamon report must contain the toString method that was invoked", - MonitorFactory.getReport().contains("toString")); + assertThat(MonitorFactory.getNumRows() > 0).as("jamon must track the method being invoked").isTrue(); + assertThat(MonitorFactory.getReport().contains("toString")).as("The jamon report must contain the toString method that was invoked").isTrue(); } @Test @@ -66,21 +67,13 @@ public void testInvokeUnderTraceWithExceptionTracking() throws Throwable { given(mi.getMethod()).willReturn(String.class.getMethod("toString")); given(mi.proceed()).willThrow(new IllegalArgumentException()); - try { - interceptor.invokeUnderTrace(mi, log); - fail("Must have propagated the IllegalArgumentException"); - } - catch (IllegalArgumentException expected) { - } - - assertEquals("Monitors must exist for the method invocation and 2 exceptions", - 3, MonitorFactory.getNumRows()); - assertTrue("The jamon report must contain the toString method that was invoked", - MonitorFactory.getReport().contains("toString")); - assertTrue("The jamon report must contain the generic exception: " + MonitorFactory.EXCEPTIONS_LABEL, - MonitorFactory.getReport().contains(MonitorFactory.EXCEPTIONS_LABEL)); - assertTrue("The jamon report must contain the specific exception: IllegalArgumentException'", - MonitorFactory.getReport().contains("IllegalArgumentException")); + assertThatIllegalArgumentException().isThrownBy(() -> + interceptor.invokeUnderTrace(mi, log)); + + assertThat(MonitorFactory.getNumRows()).as("Monitors must exist for the method invocation and 2 exceptions").isEqualTo(3); + assertThat(MonitorFactory.getReport().contains("toString")).as("The jamon report must contain the toString method that was invoked").isTrue(); + assertThat(MonitorFactory.getReport().contains(MonitorFactory.EXCEPTIONS_LABEL)).as("The jamon report must contain the generic exception: " + MonitorFactory.EXCEPTIONS_LABEL).isTrue(); + assertThat(MonitorFactory.getReport().contains("IllegalArgumentException")).as("The jamon report must contain the specific exception: IllegalArgumentException'").isTrue(); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptorTests.java index cb14a4db7069..c4c4f94737d8 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,14 @@ import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; /** * @author Rob Harrop @@ -34,14 +38,14 @@ public class PerformanceMonitorInterceptorTests { public void testSuffixAndPrefixAssignment() { PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor(); - assertNotNull(interceptor.getPrefix()); - assertNotNull(interceptor.getSuffix()); + assertThat(interceptor.getPrefix()).isNotNull(); + assertThat(interceptor.getSuffix()).isNotNull(); interceptor.setPrefix(null); interceptor.setSuffix(null); - assertNotNull(interceptor.getPrefix()); - assertNotNull(interceptor.getSuffix()); + assertThat(interceptor.getPrefix()).isNotNull(); + assertThat(interceptor.getSuffix()).isNotNull(); } @Test @@ -66,12 +70,8 @@ public void testExceptionPathStillLogsPerformanceMetricsCorrectly() throws Throw Log log = mock(Log.class); PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor(true); - try { - interceptor.invokeUnderTrace(mi, log); - fail("Must have propagated the IllegalArgumentException."); - } - catch (IllegalArgumentException expected) { - } + assertThatIllegalArgumentException().isThrownBy(() -> + interceptor.invokeUnderTrace(mi, log)); verify(log).trace(anyString()); } diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/SimpleTraceInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/SimpleTraceInterceptorTests.java index 3f01638a3234..f076e3136ac0 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/SimpleTraceInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/SimpleTraceInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,15 @@ import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; /** * Unit tests for the {@link SimpleTraceInterceptor} class. @@ -56,13 +61,8 @@ public void testExceptionPathStillLogsCorrectly() throws Throwable { Log log = mock(Log.class); final SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true); - - try { - interceptor.invokeUnderTrace(mi, log); - fail("Must have propagated the IllegalArgumentException."); - } - catch (IllegalArgumentException expected) { - } + assertThatIllegalArgumentException().isThrownBy(() -> + interceptor.invokeUnderTrace(mi, log)); verify(log).trace(anyString()); verify(log).trace(anyString(), eq(exception)); diff --git a/spring-aop/src/test/java/org/springframework/aop/scope/DefaultScopedObjectTests.java b/spring-aop/src/test/java/org/springframework/aop/scope/DefaultScopedObjectTests.java index 88eee11983e3..296da8b65943 100644 --- a/spring-aop/src/test/java/org/springframework/aop/scope/DefaultScopedObjectTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/scope/DefaultScopedObjectTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,11 +16,12 @@ package org.springframework.aop.scope; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.Mockito.mock; /** * Unit tests for the {@link DefaultScopedObject} class. @@ -33,24 +34,28 @@ public class DefaultScopedObjectTests { private static final String GOOD_BEAN_NAME = "foo"; - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithNullBeanFactory() throws Exception { - new DefaultScopedObject(null, GOOD_BEAN_NAME); + assertThatIllegalArgumentException().isThrownBy(() -> + new DefaultScopedObject(null, GOOD_BEAN_NAME)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithNullTargetBeanName() throws Exception { - testBadTargetBeanName(null); + assertThatIllegalArgumentException().isThrownBy(() -> + testBadTargetBeanName(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithEmptyTargetBeanName() throws Exception { - testBadTargetBeanName(""); + assertThatIllegalArgumentException().isThrownBy(() -> + testBadTargetBeanName("")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithJustWhitespacedTargetBeanName() throws Exception { - testBadTargetBeanName(" "); + assertThatIllegalArgumentException().isThrownBy(() -> + testBadTargetBeanName(" ")); } private static void testBadTargetBeanName(final String badTargetBeanName) { diff --git a/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java index 4b5634f2b0aa..a746532495b0 100644 --- a/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,14 +18,13 @@ import java.util.Arrays; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Mark Fisher @@ -34,34 +33,32 @@ */ public class ScopedProxyAutowireTests { - private static final Resource SCOPED_AUTOWIRE_FALSE_CONTEXT = - qualifiedResource(ScopedProxyAutowireTests.class, "scopedAutowireFalse.xml"); - private static final Resource SCOPED_AUTOWIRE_TRUE_CONTEXT = - qualifiedResource(ScopedProxyAutowireTests.class, "scopedAutowireTrue.xml"); - - @Test public void testScopedProxyInheritsAutowireCandidateFalse() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_FALSE_CONTEXT); - assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped")); - assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, true, false)).contains("scoped")); - assertFalse(bf.containsSingleton("scoped")); + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + qualifiedResource(ScopedProxyAutowireTests.class, "scopedAutowireFalse.xml")); + + assertThat(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped")).isTrue(); + assertThat(Arrays.asList(bf.getBeanNamesForType(TestBean.class, true, false)).contains("scoped")).isTrue(); + assertThat(bf.containsSingleton("scoped")).isFalse(); TestBean autowired = (TestBean) bf.getBean("autowired"); TestBean unscoped = (TestBean) bf.getBean("unscoped"); - assertSame(unscoped, autowired.getChild()); + assertThat(autowired.getChild()).isSameAs(unscoped); } @Test public void testScopedProxyReplacesAutowireCandidateTrue() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_TRUE_CONTEXT); - assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, true, false)).contains("scoped")); - assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped")); - assertFalse(bf.containsSingleton("scoped")); + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + qualifiedResource(ScopedProxyAutowireTests.class, "scopedAutowireTrue.xml")); + + assertThat(Arrays.asList(bf.getBeanNamesForType(TestBean.class, true, false)).contains("scoped")).isTrue(); + assertThat(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped")).isTrue(); + assertThat(bf.containsSingleton("scoped")).isFalse(); TestBean autowired = (TestBean) bf.getBean("autowired"); TestBean scoped = (TestBean) bf.getBean("scoped"); - assertSame(scoped, autowired.getChild()); + assertThat(autowired.getChild()).isSameAs(scoped); } diff --git a/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyUtilsTests.java b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyUtilsTests.java new file mode 100644 index 000000000000..b69616f49757 --- /dev/null +++ b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyUtilsTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.aop.scope; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +/** + * Unit tests for {@link ScopedProxyUtils}. + * + * @author Sam Brannen + * @since 5.1.10 + */ +class ScopedProxyUtilsTests { + + @Test + void getTargetBeanNameAndIsScopedTarget() { + String originalBeanName = "myBean"; + String targetBeanName = ScopedProxyUtils.getTargetBeanName(originalBeanName); + + assertThat(targetBeanName).isNotEqualTo(originalBeanName).endsWith(originalBeanName); + assertThat(ScopedProxyUtils.isScopedTarget(targetBeanName)).isTrue(); + assertThat(ScopedProxyUtils.isScopedTarget(originalBeanName)).isFalse(); + } + + @Test + void getOriginalBeanNameAndIsScopedTarget() { + String originalBeanName = "myBean"; + String targetBeanName = ScopedProxyUtils.getTargetBeanName(originalBeanName); + String parsedOriginalBeanName = ScopedProxyUtils.getOriginalBeanName(targetBeanName); + + assertThat(parsedOriginalBeanName).isNotEqualTo(targetBeanName).isEqualTo(originalBeanName); + assertThat(ScopedProxyUtils.isScopedTarget(targetBeanName)).isTrue(); + assertThat(ScopedProxyUtils.isScopedTarget(parsedOriginalBeanName)).isFalse(); + } + + @Test + void getOriginalBeanNameForNullTargetBean() { + assertThatIllegalArgumentException() + .isThrownBy(() -> ScopedProxyUtils.getOriginalBeanName(null)) + .withMessage("bean name 'null' does not refer to the target of a scoped proxy"); + } + + @Test + void getOriginalBeanNameForNonScopedTarget() { + assertThatIllegalArgumentException() + .isThrownBy(() -> ScopedProxyUtils.getOriginalBeanName("myBean")) + .withMessage("bean name 'myBean' does not refer to the target of a scoped proxy"); + } + +} diff --git a/spring-aop/src/test/java/org/springframework/aop/support/AbstractRegexpMethodPointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/support/AbstractRegexpMethodPointcutTests.java index 39cbed336eaf..aa8cd95dc9aa 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/AbstractRegexpMethodPointcutTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/AbstractRegexpMethodPointcutTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,13 +18,13 @@ import java.io.IOException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -35,7 +35,7 @@ public abstract class AbstractRegexpMethodPointcutTests { private AbstractRegexpMethodPointcut rpc; - @Before + @BeforeEach public void setUp() { rpc = getRegexpMethodPointcut(); } @@ -54,9 +54,9 @@ public void testSerializationWithNoPatternSupplied() throws Exception { } protected void noPatternSuppliedTests(AbstractRegexpMethodPointcut rpc) throws Exception { - assertFalse(rpc.matches(Object.class.getMethod("hashCode"), String.class)); - assertFalse(rpc.matches(Object.class.getMethod("wait"), Object.class)); - assertEquals(0, rpc.getPatterns().length); + assertThat(rpc.matches(Object.class.getMethod("hashCode"), String.class)).isFalse(); + assertThat(rpc.matches(Object.class.getMethod("wait"), Object.class)).isFalse(); + assertThat(rpc.getPatterns().length).isEqualTo(0); } @Test @@ -69,46 +69,46 @@ public void testExactMatch() throws Exception { protected void exactMatchTests(AbstractRegexpMethodPointcut rpc) throws Exception { // assumes rpc.setPattern("java.lang.Object.hashCode"); - assertTrue(rpc.matches(Object.class.getMethod("hashCode"), String.class)); - assertTrue(rpc.matches(Object.class.getMethod("hashCode"), Object.class)); - assertFalse(rpc.matches(Object.class.getMethod("wait"), Object.class)); + assertThat(rpc.matches(Object.class.getMethod("hashCode"), String.class)).isTrue(); + assertThat(rpc.matches(Object.class.getMethod("hashCode"), Object.class)).isTrue(); + assertThat(rpc.matches(Object.class.getMethod("wait"), Object.class)).isFalse(); } @Test public void testSpecificMatch() throws Exception { rpc.setPattern("java.lang.String.hashCode"); - assertTrue(rpc.matches(Object.class.getMethod("hashCode"), String.class)); - assertFalse(rpc.matches(Object.class.getMethod("hashCode"), Object.class)); + assertThat(rpc.matches(Object.class.getMethod("hashCode"), String.class)).isTrue(); + assertThat(rpc.matches(Object.class.getMethod("hashCode"), Object.class)).isFalse(); } @Test public void testWildcard() throws Exception { rpc.setPattern(".*Object.hashCode"); - assertTrue(rpc.matches(Object.class.getMethod("hashCode"), Object.class)); - assertFalse(rpc.matches(Object.class.getMethod("wait"), Object.class)); + assertThat(rpc.matches(Object.class.getMethod("hashCode"), Object.class)).isTrue(); + assertThat(rpc.matches(Object.class.getMethod("wait"), Object.class)).isFalse(); } @Test public void testWildcardForOneClass() throws Exception { rpc.setPattern("java.lang.Object.*"); - assertTrue(rpc.matches(Object.class.getMethod("hashCode"), String.class)); - assertTrue(rpc.matches(Object.class.getMethod("wait"), String.class)); + assertThat(rpc.matches(Object.class.getMethod("hashCode"), String.class)).isTrue(); + assertThat(rpc.matches(Object.class.getMethod("wait"), String.class)).isTrue(); } @Test public void testMatchesObjectClass() throws Exception { rpc.setPattern("java.lang.Object.*"); - assertTrue(rpc.matches(Exception.class.getMethod("hashCode"), IOException.class)); + assertThat(rpc.matches(Exception.class.getMethod("hashCode"), IOException.class)).isTrue(); // Doesn't match a method from Throwable - assertFalse(rpc.matches(Exception.class.getMethod("getMessage"), Exception.class)); + assertThat(rpc.matches(Exception.class.getMethod("getMessage"), Exception.class)).isFalse(); } @Test public void testWithExclusion() throws Exception { this.rpc.setPattern(".*get.*"); this.rpc.setExcludedPattern(".*Age.*"); - assertTrue(this.rpc.matches(TestBean.class.getMethod("getName"), TestBean.class)); - assertFalse(this.rpc.matches(TestBean.class.getMethod("getAge"), TestBean.class)); + assertThat(this.rpc.matches(TestBean.class.getMethod("getName"), TestBean.class)).isTrue(); + assertThat(this.rpc.matches(TestBean.class.getMethod("getAge"), TestBean.class)).isFalse(); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/AopUtilsTests.java b/spring-aop/src/test/java/org/springframework/aop/support/AopUtilsTests.java index 2290c25dd242..dc5437e6cd67 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/AopUtilsTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/AopUtilsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,19 +18,19 @@ import java.lang.reflect.Method; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.aop.target.EmptyTargetSource; +import org.springframework.aop.testfixture.interceptor.NopInterceptor; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; import org.springframework.lang.Nullable; -import org.springframework.tests.aop.interceptor.NopInterceptor; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -48,13 +48,13 @@ public boolean matches(Method method, @Nullable Class clazzy) { } Pointcut no = new TestPointcut(); - assertFalse(AopUtils.canApply(no, Object.class)); + assertThat(AopUtils.canApply(no, Object.class)).isFalse(); } @Test public void testPointcutAlwaysApplies() { - assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), Object.class)); - assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), TestBean.class)); + assertThat(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), Object.class)).isTrue(); + assertThat(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), TestBean.class)).isTrue(); } @Test @@ -69,7 +69,7 @@ public boolean matches(Method method, @Nullable Class clazz) { Pointcut pc = new TestPointcut(); // will return true if we're not proxying interfaces - assertTrue(AopUtils.canApply(pc, Object.class)); + assertThat(AopUtils.canApply(pc, Object.class)).isTrue(); } /** @@ -79,14 +79,13 @@ public boolean matches(Method method, @Nullable Class clazz) { */ @Test public void testCanonicalFrameworkClassesStillCanonicalOnDeserialization() throws Exception { - assertSame(MethodMatcher.TRUE, SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE)); - assertSame(ClassFilter.TRUE, SerializationTestUtils.serializeAndDeserialize(ClassFilter.TRUE)); - assertSame(Pointcut.TRUE, SerializationTestUtils.serializeAndDeserialize(Pointcut.TRUE)); - assertSame(EmptyTargetSource.INSTANCE, SerializationTestUtils.serializeAndDeserialize(EmptyTargetSource.INSTANCE)); - assertSame(Pointcuts.SETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.SETTERS)); - assertSame(Pointcuts.GETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.GETTERS)); - assertSame(ExposeInvocationInterceptor.INSTANCE, - SerializationTestUtils.serializeAndDeserialize(ExposeInvocationInterceptor.INSTANCE)); + assertThat(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE)).isSameAs(MethodMatcher.TRUE); + assertThat(SerializationTestUtils.serializeAndDeserialize(ClassFilter.TRUE)).isSameAs(ClassFilter.TRUE); + assertThat(SerializationTestUtils.serializeAndDeserialize(Pointcut.TRUE)).isSameAs(Pointcut.TRUE); + assertThat(SerializationTestUtils.serializeAndDeserialize(EmptyTargetSource.INSTANCE)).isSameAs(EmptyTargetSource.INSTANCE); + assertThat(SerializationTestUtils.serializeAndDeserialize(Pointcuts.SETTERS)).isSameAs(Pointcuts.SETTERS); + assertThat(SerializationTestUtils.serializeAndDeserialize(Pointcuts.GETTERS)).isSameAs(Pointcuts.GETTERS); + assertThat(SerializationTestUtils.serializeAndDeserialize(ExposeInvocationInterceptor.INSTANCE)).isSameAs(ExposeInvocationInterceptor.INSTANCE); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/ClassFiltersTests.java b/spring-aop/src/test/java/org/springframework/aop/support/ClassFiltersTests.java index 964136c7e916..db83a9424df1 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/ClassFiltersTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/ClassFiltersTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,46 +16,54 @@ package org.springframework.aop.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.ClassFilter; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.NestedRuntimeException; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** + * Unit tests for {@link ClassFilters}. + * * @author Rod Johnson * @author Chris Beams + * @author Sam Brannen */ -public class ClassFiltersTests { +class ClassFiltersTests { + + private final ClassFilter exceptionFilter = new RootClassFilter(Exception.class); - private ClassFilter exceptionFilter = new RootClassFilter(Exception.class); + private final ClassFilter interfaceFilter = new RootClassFilter(ITestBean.class); - private ClassFilter itbFilter = new RootClassFilter(ITestBean.class); + private final ClassFilter hasRootCauseFilter = new RootClassFilter(NestedRuntimeException.class); - private ClassFilter hasRootCauseFilter = new RootClassFilter(NestedRuntimeException.class); @Test - public void testUnion() { - assertTrue(exceptionFilter.matches(RuntimeException.class)); - assertFalse(exceptionFilter.matches(TestBean.class)); - assertFalse(itbFilter.matches(Exception.class)); - assertTrue(itbFilter.matches(TestBean.class)); - ClassFilter union = ClassFilters.union(exceptionFilter, itbFilter); - assertTrue(union.matches(RuntimeException.class)); - assertTrue(union.matches(TestBean.class)); + void union() { + assertThat(exceptionFilter.matches(RuntimeException.class)).isTrue(); + assertThat(exceptionFilter.matches(TestBean.class)).isFalse(); + assertThat(interfaceFilter.matches(Exception.class)).isFalse(); + assertThat(interfaceFilter.matches(TestBean.class)).isTrue(); + ClassFilter union = ClassFilters.union(exceptionFilter, interfaceFilter); + assertThat(union.matches(RuntimeException.class)).isTrue(); + assertThat(union.matches(TestBean.class)).isTrue(); + assertThat(union.toString()) + .matches("^.+UnionClassFilter: \\[.+RootClassFilter: .+Exception, .+RootClassFilter: .+TestBean\\]$"); } @Test - public void testIntersection() { - assertTrue(exceptionFilter.matches(RuntimeException.class)); - assertTrue(hasRootCauseFilter.matches(NestedRuntimeException.class)); + void intersection() { + assertThat(exceptionFilter.matches(RuntimeException.class)).isTrue(); + assertThat(hasRootCauseFilter.matches(NestedRuntimeException.class)).isTrue(); ClassFilter intersection = ClassFilters.intersection(exceptionFilter, hasRootCauseFilter); - assertFalse(intersection.matches(RuntimeException.class)); - assertFalse(intersection.matches(TestBean.class)); - assertTrue(intersection.matches(NestedRuntimeException.class)); + assertThat(intersection.matches(RuntimeException.class)).isFalse(); + assertThat(intersection.matches(TestBean.class)).isFalse(); + assertThat(intersection.matches(NestedRuntimeException.class)).isTrue(); + assertThat(intersection.toString()) + .matches("^.+IntersectionClassFilter: \\[.+RootClassFilter: .+Exception, .+RootClassFilter: .+NestedRuntimeException\\]$"); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/ClassUtilsTests.java b/spring-aop/src/test/java/org/springframework/aop/support/ClassUtilsTests.java index ee9179241a45..6eac64eb77b8 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/ClassUtilsTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/ClassUtilsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -15,13 +15,13 @@ */ package org.springframework.aop.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Colin Sampaleanu @@ -39,6 +39,6 @@ public void getShortNameForCglibClass() { pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); String className = ClassUtils.getShortName(proxy.getClass()); - assertEquals("Class name did not match", "TestBean", className); + assertThat(className).as("Class name did not match").isEqualTo("TestBean"); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/ComposablePointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/support/ComposablePointcutTests.java index 78deeefce9a7..87b408222c2b 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/ComposablePointcutTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/ComposablePointcutTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,16 +18,16 @@ import java.lang.reflect.Method; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.NestedRuntimeException; import org.springframework.lang.Nullable; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -67,68 +67,68 @@ public boolean matches(Method m, @Nullable Class targetClass) { @Test public void testMatchAll() throws NoSuchMethodException { Pointcut pc = new ComposablePointcut(); - assertTrue(pc.getClassFilter().matches(Object.class)); - assertTrue(pc.getMethodMatcher().matches(Object.class.getMethod("hashCode"), Exception.class)); + assertThat(pc.getClassFilter().matches(Object.class)).isTrue(); + assertThat(pc.getMethodMatcher().matches(Object.class.getMethod("hashCode"), Exception.class)).isTrue(); } @Test public void testFilterByClass() throws NoSuchMethodException { ComposablePointcut pc = new ComposablePointcut(); - assertTrue(pc.getClassFilter().matches(Object.class)); + assertThat(pc.getClassFilter().matches(Object.class)).isTrue(); ClassFilter cf = new RootClassFilter(Exception.class); pc.intersection(cf); - assertFalse(pc.getClassFilter().matches(Object.class)); - assertTrue(pc.getClassFilter().matches(Exception.class)); + assertThat(pc.getClassFilter().matches(Object.class)).isFalse(); + assertThat(pc.getClassFilter().matches(Exception.class)).isTrue(); pc.intersection(new RootClassFilter(NestedRuntimeException.class)); - assertFalse(pc.getClassFilter().matches(Exception.class)); - assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class)); - assertFalse(pc.getClassFilter().matches(String.class)); + assertThat(pc.getClassFilter().matches(Exception.class)).isFalse(); + assertThat(pc.getClassFilter().matches(NestedRuntimeException.class)).isTrue(); + assertThat(pc.getClassFilter().matches(String.class)).isFalse(); pc.union(new RootClassFilter(String.class)); - assertFalse(pc.getClassFilter().matches(Exception.class)); - assertTrue(pc.getClassFilter().matches(String.class)); - assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class)); + assertThat(pc.getClassFilter().matches(Exception.class)).isFalse(); + assertThat(pc.getClassFilter().matches(String.class)).isTrue(); + assertThat(pc.getClassFilter().matches(NestedRuntimeException.class)).isTrue(); } @Test public void testUnionMethodMatcher() { // Matches the getAge() method in any class ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, GET_AGE_METHOD_MATCHER); - assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)).isFalse(); pc.union(GETTER_METHOD_MATCHER); // Should now match all getter methods - assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)).isTrue(); pc.union(ABSQUATULATE_METHOD_MATCHER); // Should now match absquatulate() as well - assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)).isTrue(); // But it doesn't match everything - assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_SET_AGE, TestBean.class)); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_SET_AGE, TestBean.class)).isFalse(); } @Test public void testIntersectionMethodMatcher() { ComposablePointcut pc = new ComposablePointcut(); - assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)); + assertThat(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)).isTrue(); + assertThat(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)).isTrue(); pc.intersection(GETTER_METHOD_MATCHER); - assertFalse(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)); + assertThat(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); + assertThat(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)).isTrue(); pc.intersection(GET_AGE_METHOD_MATCHER); // Use the Pointcuts matches method - assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class)).isFalse(); } @Test @@ -136,24 +136,24 @@ public void testEqualsAndHashCode() throws Exception { ComposablePointcut pc1 = new ComposablePointcut(); ComposablePointcut pc2 = new ComposablePointcut(); - assertEquals(pc1, pc2); - assertEquals(pc1.hashCode(), pc2.hashCode()); + assertThat(pc2).isEqualTo(pc1); + assertThat(pc2.hashCode()).isEqualTo(pc1.hashCode()); pc1.intersection(GETTER_METHOD_MATCHER); - assertFalse(pc1.equals(pc2)); - assertFalse(pc1.hashCode() == pc2.hashCode()); + assertThat(pc1.equals(pc2)).isFalse(); + assertThat(pc1.hashCode() == pc2.hashCode()).isFalse(); pc2.intersection(GETTER_METHOD_MATCHER); - assertEquals(pc1, pc2); - assertEquals(pc1.hashCode(), pc2.hashCode()); + assertThat(pc2).isEqualTo(pc1); + assertThat(pc2.hashCode()).isEqualTo(pc1.hashCode()); pc1.union(GET_AGE_METHOD_MATCHER); pc2.union(GET_AGE_METHOD_MATCHER); - assertEquals(pc1, pc2); - assertEquals(pc1.hashCode(), pc2.hashCode()); + assertThat(pc2).isEqualTo(pc1); + assertThat(pc2.hashCode()).isEqualTo(pc1.hashCode()); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/ControlFlowPointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/support/ControlFlowPointcutTests.java index 73677e346562..f86c6e7ca9f1 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/ControlFlowPointcutTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/ControlFlowPointcutTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,15 +16,15 @@ package org.springframework.aop.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.Pointcut; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.tests.aop.interceptor.NopInterceptor; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.aop.testfixture.interceptor.NopInterceptor; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -43,17 +43,17 @@ public void testMatches() { pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop)); // Not advised, not under One - assertEquals(target.getAge(), proxied.getAge()); - assertEquals(0, nop.getCount()); + assertThat(proxied.getAge()).isEqualTo(target.getAge()); + assertThat(nop.getCount()).isEqualTo(0); // Will be advised - assertEquals(target.getAge(), new One().getAge(proxied)); - assertEquals(1, nop.getCount()); + assertThat(new One().getAge(proxied)).isEqualTo(target.getAge()); + assertThat(nop.getCount()).isEqualTo(1); // Won't be advised - assertEquals(target.getAge(), new One().nomatch(proxied)); - assertEquals(1, nop.getCount()); - assertEquals(3, cflow.getEvaluations()); + assertThat(new One().nomatch(proxied)).isEqualTo(target.getAge()); + assertThat(nop.getCount()).isEqualTo(1); + assertThat(cflow.getEvaluations()).isEqualTo(3); } /** @@ -76,28 +76,36 @@ public void testSelectiveApplication() { // Not advised, not under One target.setAge(16); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); // Not advised; under One but not a setter - assertEquals(16, new One().getAge(proxied)); - assertEquals(0, nop.getCount()); + assertThat(new One().getAge(proxied)).isEqualTo(16); + assertThat(nop.getCount()).isEqualTo(0); // Won't be advised new One().set(proxied); - assertEquals(1, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(1); // We saved most evaluations - assertEquals(1, cflow.getEvaluations()); + assertThat(cflow.getEvaluations()).isEqualTo(1); } @Test public void testEqualsAndHashCode() throws Exception { - assertEquals(new ControlFlowPointcut(One.class), new ControlFlowPointcut(One.class)); - assertEquals(new ControlFlowPointcut(One.class, "getAge"), new ControlFlowPointcut(One.class, "getAge")); - assertFalse(new ControlFlowPointcut(One.class, "getAge").equals(new ControlFlowPointcut(One.class))); - assertEquals(new ControlFlowPointcut(One.class).hashCode(), new ControlFlowPointcut(One.class).hashCode()); - assertEquals(new ControlFlowPointcut(One.class, "getAge").hashCode(), new ControlFlowPointcut(One.class, "getAge").hashCode()); - assertFalse(new ControlFlowPointcut(One.class, "getAge").hashCode() == new ControlFlowPointcut(One.class).hashCode()); + assertThat(new ControlFlowPointcut(One.class)).isEqualTo(new ControlFlowPointcut(One.class)); + assertThat(new ControlFlowPointcut(One.class, "getAge")).isEqualTo(new ControlFlowPointcut(One.class, "getAge")); + assertThat(new ControlFlowPointcut(One.class, "getAge").equals(new ControlFlowPointcut(One.class))).isFalse(); + assertThat(new ControlFlowPointcut(One.class).hashCode()).isEqualTo(new ControlFlowPointcut(One.class).hashCode()); + assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode()).isEqualTo(new ControlFlowPointcut(One.class, "getAge").hashCode()); + assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode() == new ControlFlowPointcut(One.class).hashCode()).isFalse(); + } + + @Test + public void testToString() { + assertThat(new ControlFlowPointcut(One.class).toString()) + .isEqualTo(ControlFlowPointcut.class.getName() + ": class = " + One.class.getName() + "; methodName = null"); + assertThat(new ControlFlowPointcut(One.class, "getAge").toString()) + .isEqualTo(ControlFlowPointcut.class.getName() + ": class = " + One.class.getName() + "; methodName = getAge"); } public class One { diff --git a/spring-aop/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java index fa90846d9b5a..4325e582704f 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,24 +19,25 @@ import java.io.Serializable; import org.aopalliance.intercept.MethodInterceptor; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.IntroductionAdvisor; import org.springframework.aop.IntroductionInterceptor; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.tests.TimeStamped; -import org.springframework.tests.aop.interceptor.SerializableNopInterceptor; -import org.springframework.tests.sample.beans.INestedTestBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.NestedTestBean; -import org.springframework.tests.sample.beans.Person; -import org.springframework.tests.sample.beans.SerializablePerson; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor; +import org.springframework.beans.testfixture.beans.INestedTestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.NestedTestBean; +import org.springframework.beans.testfixture.beans.Person; +import org.springframework.beans.testfixture.beans.SerializablePerson; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.TimeStamped; +import org.springframework.core.testfixture.io.SerializationTestUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * @author Rod Johnson @@ -45,16 +46,17 @@ */ public class DelegatingIntroductionInterceptorTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testNullTarget() throws Exception { // Shouldn't accept null target - new DelegatingIntroductionInterceptor(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new DelegatingIntroductionInterceptor(null)); } @Test public void testIntroductionInterceptorWithDelegation() throws Exception { TestBean raw = new TestBean(); - assertTrue(! (raw instanceof TimeStamped)); + assertThat(! (raw instanceof TimeStamped)).isTrue(); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(TimeStamped.class); @@ -64,13 +66,13 @@ public void testIntroductionInterceptorWithDelegation() throws Exception { factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts))); TimeStamped tsp = (TimeStamped) factory.getProxy(); - assertTrue(tsp.getTimeStamp() == timestamp); + assertThat(tsp.getTimeStamp() == timestamp).isTrue(); } @Test public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception { TestBean raw = new TestBean(); - assertTrue(! (raw instanceof SubTimeStamped)); + assertThat(! (raw instanceof SubTimeStamped)).isTrue(); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(SubTimeStamped.class); @@ -80,13 +82,13 @@ public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class)); SubTimeStamped tsp = (SubTimeStamped) factory.getProxy(); - assertTrue(tsp.getTimeStamp() == timestamp); + assertThat(tsp.getTimeStamp() == timestamp).isTrue(); } @Test public void testIntroductionInterceptorWithSuperInterface() throws Exception { TestBean raw = new TestBean(); - assertTrue(! (raw instanceof TimeStamped)); + assertThat(! (raw instanceof TimeStamped)).isTrue(); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(SubTimeStamped.class); @@ -96,8 +98,8 @@ public void testIntroductionInterceptorWithSuperInterface() throws Exception { factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class)); TimeStamped tsp = (TimeStamped) factory.getProxy(); - assertTrue(!(tsp instanceof SubTimeStamped)); - assertTrue(tsp.getTimeStamp() == timestamp); + assertThat(!(tsp instanceof SubTimeStamped)).isTrue(); + assertThat(tsp.getTimeStamp() == timestamp).isTrue(); } @Test @@ -123,7 +125,7 @@ public long getTimeStamp() { //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1); TimeStamped ts = (TimeStamped) pf.getProxy(); - assertTrue(ts.getTimeStamp() == t); + assertThat(ts.getTimeStamp() == t).isTrue(); ((ITester) ts).foo(); ((ITestBean) ts).getAge(); @@ -150,18 +152,18 @@ public long getTimeStamp() { ProxyFactory pf = new ProxyFactory(target); IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii); - assertTrue(ia.isPerInstance()); + assertThat(ia.isPerInstance()).isTrue(); pf.addAdvisor(0, ia); //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1); TimeStamped ts = (TimeStamped) pf.getProxy(); - assertThat(ts, instanceOf(TimeStamped.class)); - // Shoulnd't proxy framework interfaces - assertTrue(!(ts instanceof MethodInterceptor)); - assertTrue(!(ts instanceof IntroductionInterceptor)); + assertThat(ts).isInstanceOf(TimeStamped.class); + // Shouldn't proxy framework interfaces + assertThat(!(ts instanceof MethodInterceptor)).isTrue(); + assertThat(!(ts instanceof IntroductionInterceptor)).isTrue(); - assertTrue(ts.getTimeStamp() == t); + assertThat(ts.getTimeStamp() == t).isTrue(); ((ITester) ts).foo(); ((ITestBean) ts).getAge(); @@ -172,14 +174,14 @@ public long getTimeStamp() { pf = new ProxyFactory(target); pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii)); Object o = pf.getProxy(); - assertTrue(!(o instanceof TimeStamped)); + assertThat(!(o instanceof TimeStamped)).isTrue(); } @SuppressWarnings("serial") @Test public void testIntroductionInterceptorDoesntReplaceToString() throws Exception { TestBean raw = new TestBean(); - assertTrue(! (raw instanceof TimeStamped)); + assertThat(! (raw instanceof TimeStamped)).isTrue(); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = new SerializableTimeStamped(0); @@ -192,9 +194,9 @@ public String toString() { })); TimeStamped tsp = (TimeStamped) factory.getProxy(); - assertEquals(0, tsp.getTimeStamp()); + assertThat(tsp.getTimeStamp()).isEqualTo(0); - assertEquals(raw.toString(), tsp.toString()); + assertThat(tsp.toString()).isEqualTo(raw.toString()); } @Test @@ -212,10 +214,10 @@ public ITestBean getSpouse() { pf.addAdvice(new DelegatingIntroductionInterceptor(delegate)); INestedTestBean proxy = (INestedTestBean) pf.getProxy(); - assertEquals(company, proxy.getCompany()); + assertThat(proxy.getCompany()).isEqualTo(company); ITestBean introduction = (ITestBean) proxy; - assertSame("Introduced method returning delegate returns proxy", introduction, introduction.getSpouse()); - assertTrue("Introduced method returning delegate returns proxy", AopUtils.isAopProxy(introduction.getSpouse())); + assertThat(introduction.getSpouse()).as("Introduced method returning delegate returns proxy").isSameAs(introduction); + assertThat(AopUtils.isAopProxy(introduction.getSpouse())).as("Introduced method returning delegate returns proxy").isTrue(); } @Test @@ -234,12 +236,12 @@ public void testSerializableDelegatingIntroductionInterceptorSerializable() thro Person p = (Person) factory.getProxy(); - assertEquals(name, p.getName()); - assertEquals(time, ((TimeStamped) p).getTimeStamp()); + assertThat(p.getName()).isEqualTo(name); + assertThat(((TimeStamped) p).getTimeStamp()).isEqualTo(time); Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p); - assertEquals(name, p1.getName()); - assertEquals(time, ((TimeStamped) p1).getTimeStamp()); + assertThat(p1.getName()).isEqualTo(name); + assertThat(((TimeStamped) p1).getTimeStamp()).isEqualTo(time); } // Test when target implements the interface: should get interceptor by preference. @@ -264,7 +266,7 @@ public long getTimeStamp() { TimeStamped ts = (TimeStamped) pf.getProxy(); // From introduction interceptor, not target - assertTrue(ts.getTimeStamp() == t); + assertThat(ts.getTimeStamp() == t).isTrue(); } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/JdkRegexpMethodPointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/support/JdkRegexpMethodPointcutTests.java index 643f55e21fcc..1e4c139274c3 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/JdkRegexpMethodPointcutTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/JdkRegexpMethodPointcutTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/org/springframework/aop/support/MethodMatchersTests.java b/spring-aop/src/test/java/org/springframework/aop/support/MethodMatchersTests.java index f7364f99c648..55a2d7cabb57 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/MethodMatchersTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/MethodMatchersTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -18,16 +18,16 @@ import java.lang.reflect.Method; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.MethodMatcher; +import org.springframework.beans.testfixture.beans.IOther; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; import org.springframework.lang.Nullable; -import org.springframework.tests.sample.beans.IOther; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -55,24 +55,24 @@ public MethodMatchersTests() throws Exception { @Test public void testDefaultMatchesAll() throws Exception { MethodMatcher defaultMm = MethodMatcher.TRUE; - assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)); - assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)); + assertThat(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)).isTrue(); + assertThat(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)).isTrue(); } @Test public void testMethodMatcherTrueSerializable() throws Exception { - assertSame(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE), MethodMatcher.TRUE); + assertThat(MethodMatcher.TRUE).isSameAs(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE)); } @Test public void testSingle() throws Exception { MethodMatcher defaultMm = MethodMatcher.TRUE; - assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)); - assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)); + assertThat(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)).isTrue(); + assertThat(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)).isTrue(); defaultMm = MethodMatchers.intersection(defaultMm, new StartsWithMatcher("get")); - assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)); - assertFalse(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)); + assertThat(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)).isTrue(); + assertThat(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)).isFalse(); } @@ -81,14 +81,14 @@ public void testDynamicAndStaticMethodMatcherIntersection() throws Exception { MethodMatcher mm1 = MethodMatcher.TRUE; MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches(); MethodMatcher intersection = MethodMatchers.intersection(mm1, mm2); - assertTrue("Intersection is a dynamic matcher", intersection.isRuntime()); - assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class)); - assertTrue("3Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Integer(5))); + assertThat(intersection.isRuntime()).as("Intersection is a dynamic matcher").isTrue(); + assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class)).as("2Matched setAge method").isTrue(); + assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class, 5)).as("3Matched setAge method").isTrue(); // Knock out dynamic part intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch()); - assertTrue("Intersection is a dynamic matcher", intersection.isRuntime()); - assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class)); - assertFalse("3 - not Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Integer(5))); + assertThat(intersection.isRuntime()).as("Intersection is a dynamic matcher").isTrue(); + assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class)).as("2Matched setAge method").isTrue(); + assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class, 5)).as("3 - not Matched setAge method").isFalse(); } @Test @@ -97,18 +97,18 @@ public void testStaticMethodMatcherUnion() throws Exception { MethodMatcher setterMatcher = new StartsWithMatcher("set"); MethodMatcher union = MethodMatchers.union(getterMatcher, setterMatcher); - assertFalse("Union is a static matcher", union.isRuntime()); - assertTrue("Matched setAge method", union.matches(ITESTBEAN_SETAGE, TestBean.class)); - assertTrue("Matched getAge method", union.matches(ITESTBEAN_GETAGE, TestBean.class)); - assertFalse("Didn't matched absquatulate method", union.matches(IOTHER_ABSQUATULATE, TestBean.class)); + assertThat(union.isRuntime()).as("Union is a static matcher").isFalse(); + assertThat(union.matches(ITESTBEAN_SETAGE, TestBean.class)).as("Matched setAge method").isTrue(); + assertThat(union.matches(ITESTBEAN_GETAGE, TestBean.class)).as("Matched getAge method").isTrue(); + assertThat(union.matches(IOTHER_ABSQUATULATE, TestBean.class)).as("Didn't matched absquatulate method").isFalse(); } @Test public void testUnionEquals() { MethodMatcher first = MethodMatchers.union(MethodMatcher.TRUE, MethodMatcher.TRUE); MethodMatcher second = new ComposablePointcut(MethodMatcher.TRUE).union(new ComposablePointcut(MethodMatcher.TRUE)).getMethodMatcher(); - assertTrue(first.equals(second)); - assertTrue(second.equals(first)); + assertThat(first.equals(second)).isTrue(); + assertThat(second.equals(first)).isTrue(); } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java index e7728a2d307d..98f7ca29a97e 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,18 +16,18 @@ package org.springframework.aop.support; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.tests.aop.interceptor.NopInterceptor; -import org.springframework.tests.aop.interceptor.SerializableNopInterceptor; -import org.springframework.tests.sample.beans.Person; -import org.springframework.tests.sample.beans.SerializablePerson; -import org.springframework.util.SerializationTestUtils; +import org.springframework.aop.testfixture.interceptor.NopInterceptor; +import org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor; +import org.springframework.beans.testfixture.beans.Person; +import org.springframework.beans.testfixture.beans.SerializablePerson; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -45,7 +45,7 @@ public class NameMatchMethodPointcutTests { /** * Create an empty pointcut, populating instance variables. */ - @Before + @BeforeEach public void setup() { ProxyFactory pf = new ProxyFactory(new SerializablePerson()); nop = new SerializableNopInterceptor(); @@ -58,21 +58,21 @@ public void setup() { @Test public void testMatchingOnly() { // Can't do exact matching through isMatch - assertTrue(pc.isMatch("echo", "ech*")); - assertTrue(pc.isMatch("setName", "setN*")); - assertTrue(pc.isMatch("setName", "set*")); - assertFalse(pc.isMatch("getName", "set*")); - assertFalse(pc.isMatch("setName", "set")); - assertTrue(pc.isMatch("testing", "*ing")); + assertThat(pc.isMatch("echo", "ech*")).isTrue(); + assertThat(pc.isMatch("setName", "setN*")).isTrue(); + assertThat(pc.isMatch("setName", "set*")).isTrue(); + assertThat(pc.isMatch("getName", "set*")).isFalse(); + assertThat(pc.isMatch("setName", "set")).isFalse(); + assertThat(pc.isMatch("testing", "*ing")).isTrue(); } @Test public void testEmpty() throws Throwable { - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); proxied.getName(); proxied.setName(""); proxied.echo(null); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); } @@ -80,29 +80,29 @@ public void testEmpty() throws Throwable { public void testMatchOneMethod() throws Throwable { pc.addMethodName("echo"); pc.addMethodName("set*"); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); proxied.getName(); proxied.getName(); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); proxied.echo(null); - assertEquals(1, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(1); proxied.setName(""); - assertEquals(2, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(2); proxied.setAge(25); - assertEquals(25, proxied.getAge()); - assertEquals(3, nop.getCount()); + assertThat(proxied.getAge()).isEqualTo(25); + assertThat(nop.getCount()).isEqualTo(3); } @Test public void testSets() throws Throwable { pc.setMappedNames("set*", "echo"); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); proxied.getName(); proxied.setName(""); - assertEquals(1, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(1); proxied.echo(null); - assertEquals(2, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(2); } @Test @@ -112,9 +112,9 @@ public void testSerializable() throws Throwable { Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied); NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice(); p2.getName(); - assertEquals(2, nop2.getCount()); + assertThat(nop2.getCount()).isEqualTo(2); p2.echo(null); - assertEquals(3, nop2.getCount()); + assertThat(nop2.getCount()).isEqualTo(3); } @Test @@ -124,16 +124,16 @@ public void testEqualsAndHashCode() { String foo = "foo"; - assertEquals(pc1, pc2); - assertEquals(pc1.hashCode(), pc2.hashCode()); + assertThat(pc2).isEqualTo(pc1); + assertThat(pc2.hashCode()).isEqualTo(pc1.hashCode()); pc1.setMappedName(foo); - assertFalse(pc1.equals(pc2)); - assertTrue(pc1.hashCode() != pc2.hashCode()); + assertThat(pc1.equals(pc2)).isFalse(); + assertThat(pc1.hashCode() != pc2.hashCode()).isTrue(); pc2.setMappedName(foo); - assertEquals(pc1, pc2); - assertEquals(pc1.hashCode(), pc2.hashCode()); + assertThat(pc2).isEqualTo(pc1); + assertThat(pc2.hashCode()).isEqualTo(pc1.hashCode()); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/PointcutsTests.java b/spring-aop/src/test/java/org/springframework/aop/support/PointcutsTests.java index 1d2943729a78..9a6f05e10171 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/PointcutsTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/PointcutsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -18,14 +18,14 @@ import java.lang.reflect.Method; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.ClassFilter; import org.springframework.aop.Pointcut; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.lang.Nullable; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -56,12 +56,7 @@ public class PointcutsTests { public static Pointcut allTestBeanMethodsPointcut = new StaticMethodMatcherPointcut() { @Override public ClassFilter getClassFilter() { - return new ClassFilter() { - @Override - public boolean matches(Class clazz) { - return clazz.equals(TestBean.class); - } - }; + return type -> type.equals(TestBean.class); } @Override @@ -126,22 +121,22 @@ public boolean matches(Method m, @Nullable Class targetClass) { @Test public void testTrue() { - assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class)); + assertThat(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, 6)).isTrue(); + assertThat(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, 6)).isTrue(); + assertThat(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class)).isTrue(); } @Test public void testMatches() { - assertTrue(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertFalse(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertFalse(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertTrue(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class)); + assertThat(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, 6)).isTrue(); + assertThat(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, 6)).isFalse(); + assertThat(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); } /** @@ -150,29 +145,29 @@ public void testMatches() { @Test public void testUnionOfSettersAndGetters() { Pointcut union = Pointcuts.union(allClassGetterPointcut, allClassSetterPointcut); - assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); + assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, 6)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); } @Test public void testUnionOfSpecificGetters() { Pointcut union = Pointcuts.union(allClassGetAgePointcut, allClassGetNamePointcut); - assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class)); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)); - assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); + assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, 6)).isFalse(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); // Union with all setters union = Pointcuts.union(union, allClassSetterPointcut); - assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class)); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)); - assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); + assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, 6)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); - assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); + assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, 6)).isTrue(); } /** @@ -181,16 +176,16 @@ public void testUnionOfSpecificGetters() { */ @Test public void testUnionOfAllSettersAndSubclassSetters() { - assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertTrue(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, new Integer(6))); - assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)); + assertThat(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, 6)).isFalse(); + assertThat(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, 6)).isTrue(); + assertThat(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)).isFalse(); Pointcut union = Pointcuts.union(myTestBeanSetterPointcut, allClassGetterPointcut); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class)); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class)).isTrue(); // Still doesn't match superclass setter - assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, MyTestBean.class, new Integer(6))); - assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); + assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, MyTestBean.class, 6)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, 6)).isFalse(); } /** @@ -199,44 +194,44 @@ public void testUnionOfAllSettersAndSubclassSetters() { */ @Test public void testIntersectionOfSpecificGettersAndSubclassGetters() { - assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, MyTestBean.class)); - assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, TestBean.class)); - assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)); - assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, MyTestBean.class)); - assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, MyTestBean.class)); + assertThat(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, MyTestBean.class)).isTrue(); + assertThat(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, MyTestBean.class)).isTrue(); + assertThat(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, MyTestBean.class)).isTrue(); Pointcut intersection = Pointcuts.intersection(allClassGetAgePointcut, myTestBeanGetterPointcut); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class)); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class)); - assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class)); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class)).isTrue(); // Matches subclass of MyTestBean - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class)); - assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class)); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class)).isTrue(); // Now intersection with MyTestBeanSubclass getters should eliminate MyTestBean target intersection = Pointcuts.intersection(intersection, myTestBeanSubclassGetterPointcut); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class)); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class)); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class)); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class)).isFalse(); // Still matches subclass of MyTestBean - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class)); - assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class)); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class)).isTrue(); // Now union with all TestBean methods Pointcut union = Pointcuts.union(intersection, allTestBeanMethodsPointcut); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBean.class)); - assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class)); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBean.class)).isFalse(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class)).isFalse(); // Still matches subclass of MyTestBean - assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class)); - assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class)); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class)).isFalse(); + assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class)).isTrue(); - assertTrue(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)); - assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, MyTestBean.class)); + assertThat(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)).isTrue(); + assertThat(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, MyTestBean.class)).isFalse(); } @@ -246,9 +241,9 @@ public void testIntersectionOfSpecificGettersAndSubclassGetters() { @Test public void testSimpleIntersection() { Pointcut intersection = Pointcuts.intersection(allClassGetterPointcut, allClassSetterPointcut); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6))); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class)); - assertFalse(Pointcuts.matches(intersection, TEST_BEAN_ABSQUATULATE, TestBean.class)); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_SET_AGE, TestBean.class, 6)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class)).isFalse(); + assertThat(Pointcuts.matches(intersection, TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse(); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java b/spring-aop/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java index 68b9d26b9cec..de3886ef85ee 100644 --- a/spring-aop/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,21 +16,21 @@ package org.springframework.aop.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.Advised; +import org.springframework.aop.testfixture.interceptor.NopInterceptor; +import org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.Person; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.Resource; -import org.springframework.tests.aop.interceptor.NopInterceptor; -import org.springframework.tests.aop.interceptor.SerializableNopInterceptor; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.Person; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rod Johnson @@ -39,7 +39,8 @@ public class RegexpMethodPointcutAdvisorIntegrationTests { private static final Resource CONTEXT = - qualifiedResource(RegexpMethodPointcutAdvisorIntegrationTests.class, "context.xml"); + qualifiedResource(RegexpMethodPointcutAdvisorIntegrationTests.class, "context.xml"); + @Test public void testSinglePattern() throws Throwable { @@ -48,16 +49,16 @@ public void testSinglePattern() throws Throwable { ITestBean advised = (ITestBean) bf.getBean("settersAdvised"); // Interceptor behind regexp advisor NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor"); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); int newAge = 12; // Not advised advised.exceptional(null); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); advised.setAge(newAge); - assertEquals(newAge, advised.getAge()); + assertThat(advised.getAge()).isEqualTo(newAge); // Only setter fired - assertEquals(1, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(1); } @Test @@ -68,20 +69,20 @@ public void testMultiplePatterns() throws Throwable { TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised"); // Interceptor behind regexp advisor NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor"); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); int newAge = 12; // Not advised advised.exceptional(null); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); // This is proxied advised.absquatulate(); - assertEquals(1, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(1); advised.setAge(newAge); - assertEquals(newAge, advised.getAge()); + assertThat(advised.getAge()).isEqualTo(newAge); // Only setter fired - assertEquals(2, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(2); } @Test @@ -92,31 +93,31 @@ public void testSerialization() throws Throwable { Person p = (Person) bf.getBean("serializableSettersAdvised"); // Interceptor behind regexp advisor NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor"); - assertEquals(0, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(0); int newAge = 12; // Not advised - assertEquals(0, p.getAge()); - assertEquals(0, nop.getCount()); + assertThat(p.getAge()).isEqualTo(0); + assertThat(nop.getCount()).isEqualTo(0); // This is proxied p.setAge(newAge); - assertEquals(1, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(1); p.setAge(newAge); - assertEquals(newAge, p.getAge()); + assertThat(p.getAge()).isEqualTo(newAge); // Only setter fired - assertEquals(2, nop.getCount()); + assertThat(nop.getCount()).isEqualTo(2); // Serialize and continue... p = (Person) SerializationTestUtils.serializeAndDeserialize(p); - assertEquals(newAge, p.getAge()); + assertThat(p.getAge()).isEqualTo(newAge); // Remembers count, but we need to get a new reference to nop... nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice(); - assertEquals(2, nop.getCount()); - assertEquals("serializableSettersAdvised", p.getName()); + assertThat(nop.getCount()).isEqualTo(2); + assertThat(p.getName()).isEqualTo("serializableSettersAdvised"); p.setAge(newAge + 1); - assertEquals(3, nop.getCount()); - assertEquals(newAge + 1, p.getAge()); + assertThat(nop.getCount()).isEqualTo(3); + assertThat(p.getAge()).isEqualTo((newAge + 1)); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/support/RootClassFilterTests.java b/spring-aop/src/test/java/org/springframework/aop/support/RootClassFilterTests.java new file mode 100644 index 000000000000..e09344b35bc8 --- /dev/null +++ b/spring-aop/src/test/java/org/springframework/aop/support/RootClassFilterTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.aop.support; + +import org.junit.jupiter.api.Test; + +import org.springframework.aop.ClassFilter; +import org.springframework.beans.testfixture.beans.ITestBean; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link RootClassFilter}. + * + * @author Sam Brannen + * @since 5.1.10 + */ +class RootClassFilterTests { + + private final ClassFilter filter1 = new RootClassFilter(Exception.class); + private final ClassFilter filter2 = new RootClassFilter(Exception.class); + private final ClassFilter filter3 = new RootClassFilter(ITestBean.class); + + + @Test + void matches() { + assertThat(filter1.matches(Exception.class)).isTrue(); + assertThat(filter1.matches(RuntimeException.class)).isTrue(); + assertThat(filter1.matches(Error.class)).isFalse(); + } + + @Test + void testEquals() { + assertThat(filter1).isEqualTo(filter2); + assertThat(filter1).isNotEqualTo(filter3); + } + + @Test + void testHashCode() { + assertThat(filter1.hashCode()).isEqualTo(filter2.hashCode()); + assertThat(filter1.hashCode()).isNotEqualTo(filter3.hashCode()); + } + + @Test + void testToString() { + assertThat(filter1.toString()).isEqualTo("org.springframework.aop.support.RootClassFilter: java.lang.Exception"); + assertThat(filter1.toString()).isEqualTo(filter2.toString()); + } + +} diff --git a/spring-aop/src/test/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcutTests.java b/spring-aop/src/test/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcutTests.java new file mode 100644 index 000000000000..4ccbc02290b8 --- /dev/null +++ b/spring-aop/src/test/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcutTests.java @@ -0,0 +1,105 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.aop.support.annotation; + +import org.junit.jupiter.api.Test; + +import org.springframework.aop.MethodMatcher; +import org.springframework.aop.Pointcut; +import org.springframework.beans.factory.annotation.Qualifier; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link AnnotationMatchingPointcut}. + * + * @author Sam Brannen + * @since 5.1.10 + */ +class AnnotationMatchingPointcutTests { + + @Test + void classLevelPointcuts() { + Pointcut pointcut1 = new AnnotationMatchingPointcut(Qualifier.class, true); + Pointcut pointcut2 = new AnnotationMatchingPointcut(Qualifier.class, true); + Pointcut pointcut3 = new AnnotationMatchingPointcut(Qualifier.class); + + assertThat(pointcut1.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class); + assertThat(pointcut2.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class); + assertThat(pointcut3.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class); + assertThat(pointcut1.getClassFilter().toString()).contains(Qualifier.class.getName()); + + assertThat(pointcut1.getMethodMatcher()).isEqualTo(MethodMatcher.TRUE); + assertThat(pointcut2.getMethodMatcher()).isEqualTo(MethodMatcher.TRUE); + assertThat(pointcut3.getMethodMatcher()).isEqualTo(MethodMatcher.TRUE); + + assertThat(pointcut1).isEqualTo(pointcut2); + assertThat(pointcut1).isNotEqualTo(pointcut3); + assertThat(pointcut1.hashCode()).isEqualTo(pointcut2.hashCode()); + // #1 and #3 have equivalent hash codes even though equals() returns false. + assertThat(pointcut1.hashCode()).isEqualTo(pointcut3.hashCode()); + assertThat(pointcut1.toString()).isEqualTo(pointcut2.toString()); + } + + @Test + void methodLevelPointcuts() { + Pointcut pointcut1 = new AnnotationMatchingPointcut(null, Qualifier.class, true); + Pointcut pointcut2 = new AnnotationMatchingPointcut(null, Qualifier.class, true); + Pointcut pointcut3 = new AnnotationMatchingPointcut(null, Qualifier.class); + + assertThat(pointcut1.getClassFilter().getClass().getSimpleName()).isEqualTo("AnnotationCandidateClassFilter"); + assertThat(pointcut2.getClassFilter().getClass().getSimpleName()).isEqualTo("AnnotationCandidateClassFilter"); + assertThat(pointcut3.getClassFilter().getClass().getSimpleName()).isEqualTo("AnnotationCandidateClassFilter"); + assertThat(pointcut1.getClassFilter().toString()).contains(Qualifier.class.getName()); + + assertThat(pointcut1.getMethodMatcher().getClass()).isEqualTo(AnnotationMethodMatcher.class); + assertThat(pointcut2.getMethodMatcher().getClass()).isEqualTo(AnnotationMethodMatcher.class); + assertThat(pointcut3.getMethodMatcher().getClass()).isEqualTo(AnnotationMethodMatcher.class); + + assertThat(pointcut1).isEqualTo(pointcut2); + assertThat(pointcut1).isNotEqualTo(pointcut3); + assertThat(pointcut1.hashCode()).isEqualTo(pointcut2.hashCode()); + // #1 and #3 have equivalent hash codes even though equals() returns false. + assertThat(pointcut1.hashCode()).isEqualTo(pointcut3.hashCode()); + assertThat(pointcut1.toString()).isEqualTo(pointcut2.toString()); + } + + @Test + void classLevelAndMethodLevelPointcuts() { + Pointcut pointcut1 = new AnnotationMatchingPointcut(Qualifier.class, Qualifier.class, true); + Pointcut pointcut2 = new AnnotationMatchingPointcut(Qualifier.class, Qualifier.class, true); + Pointcut pointcut3 = new AnnotationMatchingPointcut(Qualifier.class, Qualifier.class); + + assertThat(pointcut1.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class); + assertThat(pointcut2.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class); + assertThat(pointcut3.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class); + assertThat(pointcut1.getClassFilter().toString()).contains(Qualifier.class.getName()); + + assertThat(pointcut1.getMethodMatcher().getClass()).isEqualTo(AnnotationMethodMatcher.class); + assertThat(pointcut2.getMethodMatcher().getClass()).isEqualTo(AnnotationMethodMatcher.class); + assertThat(pointcut3.getMethodMatcher().getClass()).isEqualTo(AnnotationMethodMatcher.class); + assertThat(pointcut1.getMethodMatcher().toString()).contains(Qualifier.class.getName()); + + assertThat(pointcut1).isEqualTo(pointcut2); + assertThat(pointcut1).isNotEqualTo(pointcut3); + assertThat(pointcut1.hashCode()).isEqualTo(pointcut2.hashCode()); + // #1 and #3 have equivalent hash codes even though equals() returns false. + assertThat(pointcut1.hashCode()).isEqualTo(pointcut3.hashCode()); + assertThat(pointcut1.toString()).isEqualTo(pointcut2.toString()); + } + +} diff --git a/spring-aop/src/test/java/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests.java b/spring-aop/src/test/java/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests.java index f88dc8811290..6637815d058e 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,16 +16,16 @@ package org.springframework.aop.target; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.testfixture.beans.ITestBean; import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.ITestBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Stephane Nicoll @@ -42,6 +42,6 @@ public void testProxy() throws Exception { reader.loadBeanDefinitions(CONTEXT); beanFactory.preInstantiateSingletons(); ITestBean bean = (ITestBean)beanFactory.getBean("testBean"); - assertTrue(AopUtils.isAopProxy(bean)); + assertThat(AopUtils.isAopProxy(bean)).isTrue(); } } diff --git a/spring-aop/src/test/java/org/springframework/aop/target/HotSwappableTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/HotSwappableTargetSourceTests.java index d79ae01e8c9b..a3354a5587f7 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/HotSwappableTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/HotSwappableTargetSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,24 +16,24 @@ package org.springframework.aop.target; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.DefaultPointcutAdvisor; +import org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.tests.aop.interceptor.SerializableNopInterceptor; -import org.springframework.tests.sample.beans.Person; -import org.springframework.tests.sample.beans.SerializablePerson; -import org.springframework.tests.sample.beans.SideEffectBean; -import org.springframework.util.SerializationTestUtils; +import org.springframework.beans.testfixture.beans.Person; +import org.springframework.beans.testfixture.beans.SerializablePerson; +import org.springframework.beans.testfixture.beans.SideEffectBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rod Johnson @@ -41,42 +41,42 @@ */ public class HotSwappableTargetSourceTests { - private static final Resource CONTEXT = qualifiedResource(HotSwappableTargetSourceTests.class, "context.xml"); - /** Initial count value set in bean factory XML */ private static final int INITIAL_COUNT = 10; private DefaultListableBeanFactory beanFactory; - @Before - public void setUp() throws Exception { + + @BeforeEach + public void setup() { this.beanFactory = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( + qualifiedResource(HotSwappableTargetSourceTests.class, "context.xml")); } /** * We must simulate container shutdown, which should clear threads. */ - @After - public void tearDown() { + @AfterEach + public void close() { // Will call pool.close() this.beanFactory.destroySingletons(); } + /** * Check it works like a normal invoker - * */ @Test public void testBasicFunctionality() { SideEffectBean proxied = (SideEffectBean) beanFactory.getBean("swappable"); - assertEquals(INITIAL_COUNT, proxied.getCount()); + assertThat(proxied.getCount()).isEqualTo(INITIAL_COUNT); proxied.doWork(); - assertEquals(INITIAL_COUNT + 1, proxied.getCount()); + assertThat(proxied.getCount()).isEqualTo((INITIAL_COUNT + 1)); proxied = (SideEffectBean) beanFactory.getBean("swappable"); proxied.doWork(); - assertEquals(INITIAL_COUNT + 2, proxied.getCount()); + assertThat(proxied.getCount()).isEqualTo((INITIAL_COUNT + 2)); } @Test @@ -85,60 +85,37 @@ public void testValidSwaps() { SideEffectBean target2 = (SideEffectBean) beanFactory.getBean("target2"); SideEffectBean proxied = (SideEffectBean) beanFactory.getBean("swappable"); - assertEquals(target1.getCount(), proxied.getCount()); + assertThat(proxied.getCount()).isEqualTo(target1.getCount()); proxied.doWork(); - assertEquals(INITIAL_COUNT + 1, proxied.getCount()); + assertThat(proxied.getCount()).isEqualTo((INITIAL_COUNT + 1)); HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper"); Object old = swapper.swap(target2); - assertEquals("Correct old target was returned", target1, old); + assertThat(old).as("Correct old target was returned").isEqualTo(target1); // TODO should be able to make this assertion: need to fix target handling // in AdvisedSupport //assertEquals(target2, ((Advised) proxied).getTarget()); - assertEquals(20, proxied.getCount()); + assertThat(proxied.getCount()).isEqualTo(20); proxied.doWork(); - assertEquals(21, target2.getCount()); + assertThat(target2.getCount()).isEqualTo(21); // Swap it back swapper.swap(target1); - assertEquals(target1.getCount(), proxied.getCount()); + assertThat(proxied.getCount()).isEqualTo(target1.getCount()); } - - /** - * - * @param invalid - * @return the message - */ - private IllegalArgumentException testRejectsSwapToInvalidValue(Object invalid) { + @Test + public void testRejectsSwapToNull() { HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper"); - IllegalArgumentException aopex = null; - try { - swapper.swap(invalid); - fail("Shouldn't be able to swap to invalid value [" + invalid + "]"); - } - catch (IllegalArgumentException ex) { - // Ok - aopex = ex; - } - + assertThatIllegalArgumentException().as("Shouldn't be able to swap to invalid value").isThrownBy(() -> + swapper.swap(null)) + .withMessageContaining("null"); // It shouldn't be corrupted, it should still work testBasicFunctionality(); - return aopex; - } - - @Test - public void testRejectsSwapToNull() { - IllegalArgumentException ex = testRejectsSwapToInvalidValue(null); - assertTrue(ex.getMessage().indexOf("null") != -1); } - // TODO test reject swap to wrong interface or class? - // how to decide what's valid? - - @Test public void testSerialization() throws Exception { SerializablePerson sp1 = new SerializablePerson(); @@ -153,16 +130,17 @@ public void testSerialization() throws Exception { pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor())); Person p = (Person) pf.getProxy(); - assertEquals(sp1.getName(), p.getName()); + assertThat(p.getName()).isEqualTo(sp1.getName()); hts.swap(sp2); - assertEquals(sp2.getName(), p.getName()); + assertThat(p.getName()).isEqualTo(sp2.getName()); p = (Person) SerializationTestUtils.serializeAndDeserialize(p); // We need to get a reference to the client-side targetsource hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource(); - assertEquals(sp2.getName(), p.getName()); + assertThat(p.getName()).isEqualTo(sp2.getName()); hts.swap(sp1); - assertEquals(sp1.getName(), p.getName()); + assertThat(p.getName()).isEqualTo(sp1.getName()); } + } diff --git a/spring-aop/src/test/java/org/springframework/aop/target/LazyCreationTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/LazyCreationTargetSourceTests.java index ddda8ce91855..4551245335ef 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/LazyCreationTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/LazyCreationTargetSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,12 +16,12 @@ package org.springframework.aop.target; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.TargetSource; import org.springframework.aop.framework.ProxyFactory; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -44,15 +44,15 @@ public Class getTargetClass() { }; InitCountingBean proxy = (InitCountingBean) ProxyFactory.getProxy(targetSource); - assertEquals("Init count should be 0", 0, InitCountingBean.initCount); - assertEquals("Target class incorrect", InitCountingBean.class, targetSource.getTargetClass()); - assertEquals("Init count should still be 0 after getTargetClass()", 0, InitCountingBean.initCount); + assertThat(InitCountingBean.initCount).as("Init count should be 0").isEqualTo(0); + assertThat(targetSource.getTargetClass()).as("Target class incorrect").isEqualTo(InitCountingBean.class); + assertThat(InitCountingBean.initCount).as("Init count should still be 0 after getTargetClass()").isEqualTo(0); proxy.doSomething(); - assertEquals("Init count should now be 1", 1, InitCountingBean.initCount); + assertThat(InitCountingBean.initCount).as("Init count should now be 1").isEqualTo(1); proxy.doSomething(); - assertEquals("Init count should still be 1", 1, InitCountingBean.initCount); + assertThat(InitCountingBean.initCount).as("Init count should still be 1").isEqualTo(1); } diff --git a/spring-aop/src/test/java/org/springframework/aop/target/LazyInitTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/LazyInitTargetSourceTests.java index 127206fc6dfd..6c38a0f04fc2 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/LazyInitTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/LazyInitTargetSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,15 +18,15 @@ import java.util.Set; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.testfixture.beans.ITestBean; import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.ITestBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Juergen Hoeller @@ -49,9 +49,9 @@ public void testLazyInitSingletonTargetSource() { bf.preInstantiateSingletons(); ITestBean tb = (ITestBean) bf.getBean("proxy"); - assertFalse(bf.containsSingleton("target")); - assertEquals(10, tb.getAge()); - assertTrue(bf.containsSingleton("target")); + assertThat(bf.containsSingleton("target")).isFalse(); + assertThat(tb.getAge()).isEqualTo(10); + assertThat(bf.containsSingleton("target")).isTrue(); } @Test @@ -61,9 +61,9 @@ public void testCustomLazyInitSingletonTargetSource() { bf.preInstantiateSingletons(); ITestBean tb = (ITestBean) bf.getBean("proxy"); - assertFalse(bf.containsSingleton("target")); - assertEquals("Rob Harrop", tb.getName()); - assertTrue(bf.containsSingleton("target")); + assertThat(bf.containsSingleton("target")).isFalse(); + assertThat(tb.getName()).isEqualTo("Rob Harrop"); + assertThat(bf.containsSingleton("target")).isTrue(); } @Test @@ -73,14 +73,14 @@ public void testLazyInitFactoryBeanTargetSource() { bf.preInstantiateSingletons(); Set set1 = (Set) bf.getBean("proxy1"); - assertFalse(bf.containsSingleton("target1")); - assertTrue(set1.contains("10")); - assertTrue(bf.containsSingleton("target1")); + assertThat(bf.containsSingleton("target1")).isFalse(); + assertThat(set1.contains("10")).isTrue(); + assertThat(bf.containsSingleton("target1")).isTrue(); Set set2 = (Set) bf.getBean("proxy2"); - assertFalse(bf.containsSingleton("target2")); - assertTrue(set2.contains("20")); - assertTrue(bf.containsSingleton("target2")); + assertThat(bf.containsSingleton("target2")).isFalse(); + assertThat(set2.contains("20")).isTrue(); + assertThat(bf.containsSingleton("target2")).isTrue(); } diff --git a/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java index 026029b4ccac..3a84193d50f1 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,17 +16,18 @@ package org.springframework.aop.target; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.TargetSource; import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.tests.sample.beans.SerializablePerson; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; +import org.springframework.beans.testfixture.beans.SerializablePerson; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests relating to the abstract {@link AbstractPrototypeBasedTargetSource} @@ -47,7 +48,7 @@ public void testSerializability() throws Exception { MutablePropertyValues pvs = new MutablePropertyValues(); RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class); bd.setPropertyValues(pvs); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("ts", tsBd); @@ -55,10 +56,10 @@ public void testSerializability() throws Exception { TestTargetSource cpts = (TestTargetSource) bf.getBean("ts"); TargetSource serialized = (TargetSource) SerializationTestUtils.serializeAndDeserialize(cpts); - assertTrue("Changed to SingletonTargetSource on deserialization", - serialized instanceof SingletonTargetSource); + boolean condition = serialized instanceof SingletonTargetSource; + assertThat(condition).as("Changed to SingletonTargetSource on deserialization").isTrue(); SingletonTargetSource sts = (SingletonTargetSource) serialized; - assertNotNull(sts.getTarget()); + assertThat(sts.getTarget()).isNotNull(); } @@ -70,6 +71,7 @@ private static class TestTargetSource extends AbstractPrototypeBasedTargetSource * Nonserializable test field to check that subclass * state can't prevent serialization from working */ + @SuppressWarnings("unused") private TestBean thisFieldIsNotSerializable = new TestBean(); @Override diff --git a/spring-aop/src/test/java/org/springframework/aop/target/PrototypeTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/PrototypeTargetSourceTests.java index 995ee9f89ac6..7871e1072d18 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/PrototypeTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/PrototypeTargetSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,18 +16,15 @@ package org.springframework.aop.target; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.SideEffectBean; +import org.springframework.beans.testfixture.beans.SideEffectBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rod Johnson @@ -35,19 +32,20 @@ */ public class PrototypeTargetSourceTests { - private static final Resource CONTEXT = qualifiedResource(PrototypeTargetSourceTests.class, "context.xml"); - /** Initial count value set in bean factory XML */ private static final int INITIAL_COUNT = 10; - private BeanFactory beanFactory; + private DefaultListableBeanFactory beanFactory; + - @Before - public void setUp() throws Exception { + @BeforeEach + public void setup() { this.beanFactory = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.beanFactory).loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( + qualifiedResource(PrototypeTargetSourceTests.class, "context.xml")); } + /** * Test that multiple invocations of the prototype bean will result * in no change to visible state, as a new instance is used. @@ -56,15 +54,14 @@ public void setUp() throws Exception { @Test public void testPrototypeAndSingletonBehaveDifferently() { SideEffectBean singleton = (SideEffectBean) beanFactory.getBean("singleton"); - assertEquals(INITIAL_COUNT, singleton.getCount()); + assertThat(singleton.getCount()).isEqualTo(INITIAL_COUNT); singleton.doWork(); - assertEquals(INITIAL_COUNT + 1, singleton.getCount()); + assertThat(singleton.getCount()).isEqualTo((INITIAL_COUNT + 1)); SideEffectBean prototype = (SideEffectBean) beanFactory.getBean("prototype"); - assertEquals(INITIAL_COUNT, prototype.getCount()); + assertThat(prototype.getCount()).isEqualTo(INITIAL_COUNT); prototype.doWork(); - assertEquals(INITIAL_COUNT, prototype.getCount()); + assertThat(prototype.getCount()).isEqualTo(INITIAL_COUNT); } - } diff --git a/spring-aop/src/test/java/org/springframework/aop/target/ThreadLocalTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/ThreadLocalTargetSourceTests.java index 0ff97aa8a981..7cb788ed1ac8 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/ThreadLocalTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/ThreadLocalTargetSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,17 +16,16 @@ package org.springframework.aop.target; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.SideEffectBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.SideEffectBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rod Johnson @@ -34,26 +33,27 @@ */ public class ThreadLocalTargetSourceTests { - private static final Resource CONTEXT = qualifiedResource(ThreadLocalTargetSourceTests.class, "context.xml"); - /** Initial count value set in bean factory XML */ private static final int INITIAL_COUNT = 10; private DefaultListableBeanFactory beanFactory; - @Before - public void setUp() throws Exception { + + @BeforeEach + public void setup() { this.beanFactory = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( + qualifiedResource(ThreadLocalTargetSourceTests.class, "context.xml")); } /** * We must simulate container shutdown, which should clear threads. */ - protected void tearDown() { + protected void close() { this.beanFactory.destroySingletons(); } + /** * Check we can use two different ThreadLocalTargetSources * managing objects of different types without them interfering @@ -62,24 +62,24 @@ protected void tearDown() { @Test public void testUseDifferentManagedInstancesInSameThread() { SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); - assertEquals(INITIAL_COUNT, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo(INITIAL_COUNT); apartment.doWork(); - assertEquals(INITIAL_COUNT + 1, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo((INITIAL_COUNT + 1)); ITestBean test = (ITestBean) beanFactory.getBean("threadLocal2"); - assertEquals("Rod", test.getName()); - assertEquals("Kerry", test.getSpouse().getName()); + assertThat(test.getName()).isEqualTo("Rod"); + assertThat(test.getSpouse().getName()).isEqualTo("Kerry"); } @Test public void testReuseInSameThread() { SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); - assertEquals(INITIAL_COUNT, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo(INITIAL_COUNT); apartment.doWork(); - assertEquals(INITIAL_COUNT + 1, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo((INITIAL_COUNT + 1)); apartment = (SideEffectBean) beanFactory.getBean("apartment"); - assertEquals(INITIAL_COUNT + 1, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo((INITIAL_COUNT + 1)); } /** @@ -89,37 +89,37 @@ public void testReuseInSameThread() { public void testCanGetStatsViaMixin() { ThreadLocalTargetSourceStats stats = (ThreadLocalTargetSourceStats) beanFactory.getBean("apartment"); // +1 because creating target for stats call counts - assertEquals(1, stats.getInvocationCount()); + assertThat(stats.getInvocationCount()).isEqualTo(1); SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); apartment.doWork(); // +1 again - assertEquals(3, stats.getInvocationCount()); + assertThat(stats.getInvocationCount()).isEqualTo(3); // + 1 for states call! - assertEquals(3, stats.getHitCount()); + assertThat(stats.getHitCount()).isEqualTo(3); apartment.doWork(); - assertEquals(6, stats.getInvocationCount()); - assertEquals(6, stats.getHitCount()); + assertThat(stats.getInvocationCount()).isEqualTo(6); + assertThat(stats.getHitCount()).isEqualTo(6); // Only one thread so only one object can have been bound - assertEquals(1, stats.getObjectCount()); + assertThat(stats.getObjectCount()).isEqualTo(1); } @Test public void testNewThreadHasOwnInstance() throws InterruptedException { SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); - assertEquals(INITIAL_COUNT, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo(INITIAL_COUNT); apartment.doWork(); apartment.doWork(); apartment.doWork(); - assertEquals(INITIAL_COUNT + 3, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo((INITIAL_COUNT + 3)); class Runner implements Runnable { public SideEffectBean mine; @Override public void run() { this.mine = (SideEffectBean) beanFactory.getBean("apartment"); - assertEquals(INITIAL_COUNT, mine.getCount()); + assertThat(mine.getCount()).isEqualTo(INITIAL_COUNT); mine.doWork(); - assertEquals(INITIAL_COUNT + 1, mine.getCount()); + assertThat(mine.getCount()).isEqualTo((INITIAL_COUNT + 1)); } } Runner r = new Runner(); @@ -127,21 +127,21 @@ public void run() { t.start(); t.join(); - assertNotNull(r); + assertThat(r).isNotNull(); // Check it didn't affect the other thread's copy - assertEquals(INITIAL_COUNT + 3, apartment.getCount()); + assertThat(apartment.getCount()).isEqualTo((INITIAL_COUNT + 3)); // When we use other thread's copy in this thread // it should behave like ours - assertEquals(INITIAL_COUNT + 3, r.mine.getCount()); + assertThat(r.mine.getCount()).isEqualTo((INITIAL_COUNT + 3)); // Bound to two threads - assertEquals(2, ((ThreadLocalTargetSourceStats) apartment).getObjectCount()); + assertThat(((ThreadLocalTargetSourceStats) apartment).getObjectCount()).isEqualTo(2); } /** - * Test for SPR-1442. Destroyed target should re-associated with thread and not throw NPE + * Test for SPR-1442. Destroyed target should re-associated with thread and not throw NPE. */ @Test public void testReuseDestroyedTarget() { @@ -152,12 +152,7 @@ public void testReuseDestroyedTarget() { source.destroy(); // try second time - try { - source.getTarget(); - } - catch (NullPointerException ex) { - fail("Should not throw NPE"); - } + source.getTarget(); // Should not throw NPE } } diff --git a/spring-aop/src/test/java/org/springframework/aop/target/dynamic/RefreshableTargetSourceTests.java b/spring-aop/src/test/java/org/springframework/aop/target/dynamic/RefreshableTargetSourceTests.java index 7dce8a44f284..ac33fd950adb 100644 --- a/spring-aop/src/test/java/org/springframework/aop/target/dynamic/RefreshableTargetSourceTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/target/dynamic/RefreshableTargetSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,12 +16,12 @@ package org.springframework.aop.target.dynamic; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; +import org.springframework.core.testfixture.EnabledForTestGroups; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.TestGroup.PERFORMANCE; /** * @author Rob Harrop @@ -41,8 +41,8 @@ public void testRefreshCheckWithNonRefresh() throws Exception { Thread.sleep(1); Object b = ts.getTarget(); - assertEquals("Should be one call to freshTarget to get initial target", 1, ts.getCallCount()); - assertSame("Returned objects should be the same - no refresh should occur", a, b); + assertThat(ts.getCallCount()).as("Should be one call to freshTarget to get initial target").isEqualTo(1); + assertThat(b).as("Returned objects should be the same - no refresh should occur").isSameAs(a); } /** @@ -57,8 +57,8 @@ public void testRefreshCheckWithRefresh() throws Exception { Thread.sleep(100); Object b = ts.getTarget(); - assertEquals("Should have called freshTarget twice", 2, ts.getCallCount()); - assertNotSame("Should be different objects", a, b); + assertThat(ts.getCallCount()).as("Should have called freshTarget twice").isEqualTo(2); + assertThat(b).as("Should be different objects").isNotSameAs(a); } /** @@ -72,39 +72,38 @@ public void testWithNoRefreshCheck() throws Exception { Object a = ts.getTarget(); Object b = ts.getTarget(); - assertEquals("Refresh target should only be called once", 1, ts.getCallCount()); - assertSame("Objects should be the same - refresh check delay not elapsed", a, b); + assertThat(ts.getCallCount()).as("Refresh target should only be called once").isEqualTo(1); + assertThat(b).as("Objects should be the same - refresh check delay not elapsed").isSameAs(a); } @Test + @EnabledForTestGroups(PERFORMANCE) public void testRefreshOverTime() throws Exception { - Assume.group(TestGroup.PERFORMANCE); - CountingRefreshableTargetSource ts = new CountingRefreshableTargetSource(true); ts.setRefreshCheckDelay(100); Object a = ts.getTarget(); Object b = ts.getTarget(); - assertEquals("Objects should be same", a, b); + assertThat(b).as("Objects should be same").isEqualTo(a); Thread.sleep(50); Object c = ts.getTarget(); - assertEquals("A and C should be same", a, c); + assertThat(c).as("A and C should be same").isEqualTo(a); Thread.sleep(60); Object d = ts.getTarget(); - assertNotNull("D should not be null", d); - assertFalse("A and D should not be equal", a.equals(d)); + assertThat(d).as("D should not be null").isNotNull(); + assertThat(a.equals(d)).as("A and D should not be equal").isFalse(); Object e = ts.getTarget(); - assertEquals("D and E should be equal", d, e); + assertThat(e).as("D and E should be equal").isEqualTo(d); Thread.sleep(110); Object f = ts.getTarget(); - assertFalse("E and F should be different", e.equals(f)); + assertThat(e.equals(f)).as("E and F should be different").isFalse(); } diff --git a/spring-aop/src/test/java/test/annotation/EmptySpringAnnotation.java b/spring-aop/src/test/java/test/annotation/EmptySpringAnnotation.java index 140f0fee3d4d..fcb55a8330ab 100644 --- a/spring-aop/src/test/java/test/annotation/EmptySpringAnnotation.java +++ b/spring-aop/src/test/java/test/annotation/EmptySpringAnnotation.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/test/annotation/transaction/Tx.java b/spring-aop/src/test/java/test/annotation/transaction/Tx.java index 138d2410903e..bf7c9daa5c02 100644 --- a/spring-aop/src/test/java/test/annotation/transaction/Tx.java +++ b/spring-aop/src/test/java/test/annotation/transaction/Tx.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/test/aop/DefaultLockable.java b/spring-aop/src/test/java/test/aop/DefaultLockable.java index 1fddaca9f19c..1e9499df8071 100644 --- a/spring-aop/src/test/java/test/aop/DefaultLockable.java +++ b/spring-aop/src/test/java/test/aop/DefaultLockable.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/test/aop/Lockable.java b/spring-aop/src/test/java/test/aop/Lockable.java index e62a4e2ff324..7e9058d1a063 100644 --- a/spring-aop/src/test/java/test/aop/Lockable.java +++ b/spring-aop/src/test/java/test/aop/Lockable.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/test/aop/PerTargetAspect.java b/spring-aop/src/test/java/test/aop/PerTargetAspect.java index fb1026481a16..4dd5c29dbb16 100644 --- a/spring-aop/src/test/java/test/aop/PerTargetAspect.java +++ b/spring-aop/src/test/java/test/aop/PerTargetAspect.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/test/aop/PerThisAspect.java b/spring-aop/src/test/java/test/aop/PerThisAspect.java index ec55a51ecf67..f6b9a3d4a955 100644 --- a/spring-aop/src/test/java/test/aop/PerThisAspect.java +++ b/spring-aop/src/test/java/test/aop/PerThisAspect.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/java/test/aop/TwoAdviceAspect.java b/spring-aop/src/test/java/test/aop/TwoAdviceAspect.java index 6745457a9d23..f77ad9a5170c 100644 --- a/spring-aop/src/test/java/test/aop/TwoAdviceAspect.java +++ b/spring-aop/src/test/java/test/aop/TwoAdviceAspect.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aop/src/test/resources/log4j2-test.xml b/spring-aop/src/test/resources/log4j2-test.xml index 78b880bbcd83..521cc44e65a1 100644 --- a/spring-aop/src/test/resources/log4j2-test.xml +++ b/spring-aop/src/test/resources/log4j2-test.xml @@ -2,7 +2,7 @@ - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-context.xml index 984f5ada7b8c..64f222cdd372 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-context.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> @@ -20,7 +20,7 @@ - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-directPointcutEvents.xml b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-directPointcutEvents.xml index 42291802335d..852f7479377e 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-directPointcutEvents.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-directPointcutEvents.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> diff --git a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutEvents.xml b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutEvents.xml index 8350030c1711..ed04cb45e090 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutEvents.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutEvents.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> diff --git a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutRefEvents.xml b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutRefEvents.xml index 23e4a88b3fc3..8300b280512d 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutRefEvents.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerEventTests-pointcutRefEvents.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> diff --git a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutDuplication.xml b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutDuplication.xml index cd01ffd53260..33693b7c1c40 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutDuplication.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutDuplication.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> @@ -14,7 +14,7 @@ - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutMissing.xml b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutMissing.xml index 850fbc15d1b4..3d2037805e41 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutMissing.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/config/AopNamespaceHandlerPointcutErrorTests-pointcutMissing.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> @@ -14,7 +14,7 @@ - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/config/TopLevelAopTagTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/config/TopLevelAopTagTests-context.xml index f1fff08fa4a7..4a1cd95aa131 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/config/TopLevelAopTagTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/config/TopLevelAopTagTests-context.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> diff --git a/spring-aop/src/test/resources/org/springframework/aop/framework/PrototypeTargetTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/framework/PrototypeTargetTests-context.xml index 21d1eedb4089..89b8d261d4fe 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/framework/PrototypeTargetTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/framework/PrototypeTargetTests-context.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests-context.xml index 5f00163fb555..a5bf0f37d9c1 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/interceptor/ExposeInvocationInterceptorTests-context.xml @@ -1,12 +1,12 @@ - + - + @@ -15,7 +15,7 @@ INSTANCE - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireFalse.xml b/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireFalse.xml index 5cb859c8c09f..57101c4de152 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireFalse.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireFalse.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd + http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> diff --git a/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireTrue.xml b/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireTrue.xml index 22c62644dd86..b8993d5da1bd 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireTrue.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/scope/ScopedProxyAutowireTests-scopedAutowireTrue.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd + http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> diff --git a/spring-aop/src/test/resources/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests-context.xml index d219df0a731b..967e42b6868a 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests-context.xml @@ -1,15 +1,15 @@ - + - + custom 666 - + @@ -21,15 +21,15 @@ - org.springframework.tests.sample.beans.ITestBean + org.springframework.beans.testfixture.beans.ITestBean settersAdvisor - org.springframework.tests.sample.beans.Person + org.springframework.beans.testfixture.beans.Person - + serializableSettersAdvised @@ -48,7 +48,7 @@ - org.springframework.tests.sample.beans.ITestBean + org.springframework.beans.testfixture.beans.ITestBean true diff --git a/spring-aop/src/test/resources/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests-context.xml index 30951315297c..c0bbd2fe9097 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/target/CommonsPool2TargetSourceProxyTests-context.xml @@ -1,9 +1,9 @@ - + - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/target/HotSwappableTargetSourceTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/target/HotSwappableTargetSourceTests-context.xml index c8d4a287f555..25f39f9f71da 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/target/HotSwappableTargetSourceTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/target/HotSwappableTargetSourceTests-context.xml @@ -1,14 +1,14 @@ - + - + 10 - + 20 diff --git a/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-customTarget.xml b/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-customTarget.xml index 57c3fb182a2c..70f7406195da 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-customTarget.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-customTarget.xml @@ -1,9 +1,9 @@ - + - + 10 diff --git a/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-factoryBean.xml b/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-factoryBean.xml index 4b408a3410f0..1853c4cd567a 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-factoryBean.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-factoryBean.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-singleton.xml b/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-singleton.xml index 2abdc9f260a7..e10984760269 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-singleton.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/target/LazyInitTargetSourceTests-singleton.xml @@ -1,9 +1,9 @@ - + - + 10 diff --git a/spring-aop/src/test/resources/org/springframework/aop/target/PrototypeTargetSourceTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/target/PrototypeTargetSourceTests-context.xml index 6f45a79a72e8..53513ecf1df6 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/target/PrototypeTargetSourceTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/target/PrototypeTargetSourceTests-context.xml @@ -1,14 +1,14 @@ - + - + 10 - + 10 @@ -16,7 +16,7 @@ prototypeTest - + debugInterceptor,test diff --git a/spring-aop/src/test/resources/org/springframework/aop/target/ThreadLocalTargetSourceTests-context.xml b/spring-aop/src/test/resources/org/springframework/aop/target/ThreadLocalTargetSourceTests-context.xml index 57e3ce659b0a..778c8c1d7f0e 100644 --- a/spring-aop/src/test/resources/org/springframework/aop/target/ThreadLocalTargetSourceTests-context.xml +++ b/spring-aop/src/test/resources/org/springframework/aop/target/ThreadLocalTargetSourceTests-context.xml @@ -1,9 +1,9 @@ - + - + 10 @@ -11,7 +11,7 @@ prototypeTest - + - + Rod - + Kerry diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/advice/CountingAfterReturningAdvice.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/CountingAfterReturningAdvice.java similarity index 85% rename from spring-aop/src/test/java/org/springframework/tests/aop/advice/CountingAfterReturningAdvice.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/CountingAfterReturningAdvice.java index 2b37761fabcb..8cfab3ca0127 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/advice/CountingAfterReturningAdvice.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/CountingAfterReturningAdvice.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.aop.advice; +package org.springframework.aop.testfixture.advice; import java.lang.reflect.Method; diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/advice/CountingBeforeAdvice.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/CountingBeforeAdvice.java similarity index 85% rename from spring-aop/src/test/java/org/springframework/tests/aop/advice/CountingBeforeAdvice.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/CountingBeforeAdvice.java index 2e34d50262ce..bf931f6bdfaa 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/advice/CountingBeforeAdvice.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/CountingBeforeAdvice.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.aop.advice; +package org.springframework.aop.testfixture.advice; import java.lang.reflect.Method; diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/advice/MethodCounter.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/MethodCounter.java similarity index 80% rename from spring-aop/src/test/java/org/springframework/tests/aop/advice/MethodCounter.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/MethodCounter.java index 5b31b6bfadc2..384973bd9aa7 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/advice/MethodCounter.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/MethodCounter.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.aop.advice; +package org.springframework.aop.testfixture.advice; import java.io.Serializable; import java.lang.reflect.Method; @@ -25,6 +25,7 @@ * * @author Rod Johnson * @author Chris Beams + * @author Sam Brannen */ @SuppressWarnings("serial") public class MethodCounter implements Serializable { @@ -39,15 +40,12 @@ protected void count(Method m) { } protected void count(String methodName) { - Integer i = map.get(methodName); - i = (i != null) ? new Integer(i.intValue() + 1) : new Integer(1); - map.put(methodName, i); + map.merge(methodName, 1, (n, m) -> n + 1); ++allCount; } public int getCalls(String methodName) { - Integer i = map.get(methodName); - return (i != null ? i.intValue() : 0); + return map.getOrDefault(methodName, 0); } public int getCalls() { diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/advice/MyThrowsHandler.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/MyThrowsHandler.java similarity index 50% rename from spring-aop/src/test/java/org/springframework/tests/aop/advice/MyThrowsHandler.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/MyThrowsHandler.java index 4e5f8ecc3fe5..606101af9c62 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/advice/MyThrowsHandler.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/MyThrowsHandler.java @@ -1,7 +1,20 @@ -/** +/* + * Copyright 2002-2019 the original author or authors. * + * Licensed 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 + * + * https://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.springframework.tests.aop.advice; + +package org.springframework.aop.testfixture.advice; import java.io.IOException; import java.lang.reflect.Method; @@ -11,10 +24,12 @@ @SuppressWarnings("serial") public class MyThrowsHandler extends MethodCounter implements ThrowsAdvice { + // Full method signature public void afterThrowing(Method m, Object[] args, Object target, IOException ex) { count("ioException"); } + public void afterThrowing(RemoteException ex) throws Throwable { count("remoteException"); } @@ -23,4 +38,5 @@ public void afterThrowing(RemoteException ex) throws Throwable { public void afterThrowing(Method m, Exception ex) throws Throwable { throw new UnsupportedOperationException("Shouldn't be called"); } + } diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/advice/TimestampIntroductionAdvisor.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/TimestampIntroductionAdvisor.java similarity index 78% rename from spring-aop/src/test/java/org/springframework/tests/aop/advice/TimestampIntroductionAdvisor.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/TimestampIntroductionAdvisor.java index 5186dfce8ca7..5321e5d38110 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/advice/TimestampIntroductionAdvisor.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/advice/TimestampIntroductionAdvisor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,22 +14,18 @@ * limitations under the License. */ -package org.springframework.tests.aop.advice; +package org.springframework.aop.testfixture.advice; import org.springframework.aop.support.DefaultIntroductionAdvisor; import org.springframework.aop.support.DelegatingIntroductionInterceptor; -import org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor; +import org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor; /** - * * @author Rod Johnson */ @SuppressWarnings("serial") public class TimestampIntroductionAdvisor extends DefaultIntroductionAdvisor { - /** - * @param dii - */ public TimestampIntroductionAdvisor() { super(new DelegatingIntroductionInterceptor(new TimestampIntroductionInterceptor())); } diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/NopInterceptor.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/NopInterceptor.java similarity index 92% rename from spring-aop/src/test/java/org/springframework/tests/aop/interceptor/NopInterceptor.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/NopInterceptor.java index de49c8af7fce..dd4432467620 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/NopInterceptor.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/NopInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.aop.interceptor; +package org.springframework.aop.testfixture.interceptor; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/SerializableNopInterceptor.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/SerializableNopInterceptor.java similarity index 91% rename from spring-aop/src/test/java/org/springframework/tests/aop/interceptor/SerializableNopInterceptor.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/SerializableNopInterceptor.java index f6f42fe00b44..25d9546c53a0 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/SerializableNopInterceptor.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/SerializableNopInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.aop.interceptor; +package org.springframework.aop.testfixture.interceptor; import java.io.Serializable; diff --git a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/TimestampIntroductionInterceptor.java b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/TimestampIntroductionInterceptor.java similarity index 86% rename from spring-aop/src/test/java/org/springframework/tests/aop/interceptor/TimestampIntroductionInterceptor.java rename to spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/TimestampIntroductionInterceptor.java index de4dfecff6b7..24761350132a 100644 --- a/spring-aop/src/test/java/org/springframework/tests/aop/interceptor/TimestampIntroductionInterceptor.java +++ b/spring-aop/src/testFixtures/java/org/springframework/aop/testfixture/interceptor/TimestampIntroductionInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,10 +14,10 @@ * limitations under the License. */ -package org.springframework.tests.aop.interceptor; +package org.springframework.aop.testfixture.interceptor; import org.springframework.aop.support.DelegatingIntroductionInterceptor; -import org.springframework.tests.TimeStamped; +import org.springframework.core.testfixture.TimeStamped; @SuppressWarnings("serial") public class TimestampIntroductionInterceptor extends DelegatingIntroductionInterceptor diff --git a/spring-aspects/spring-aspects.gradle b/spring-aspects/spring-aspects.gradle index 51bab8a81983..12adbfb7e5f4 100644 --- a/spring-aspects/spring-aspects.gradle +++ b/spring-aspects/spring-aspects.gradle @@ -1,99 +1,34 @@ description = "Spring Aspects" -// Redefine the compileJava and compileTestJava tasks in order to compile sources with ajc instead of javac +apply plugin: "io.freefair.aspectj" -configurations { - rt - ajc - aspects - ajInpath -} - -compileJava { - actions = [] - dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava") - - def outputDir = project.sourceSets.main.java.outputDir - inputs.files(project.sourceSets.main.allSource + project.sourceSets.main.compileClasspath) - outputs.dir outputDir - - sourceCompatibility = 1.8 // fixed even when general compatibility level set to e.g. 10 - targetCompatibility = 1.8 - - doLast{ - // Assemble runtime classpath from folders and JARs that actually exist - def runtimeClasspath = project.files(sourceSets.main.runtimeClasspath.files.findAll({ it.exists() })) - - ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", - classpath: configurations.ajc.asPath) - - ant.iajc(source: sourceCompatibility, target: targetCompatibility, - maxmem: "1024m", fork: "true", Xlint: "ignore", - destDir: outputDir.absolutePath, - aspectPath: configurations.aspects.asPath, - inpath: configurations.ajInpath.asPath, - sourceRootCopyFilter: "**/*.java,**/*.aj", - classpath: (runtimeClasspath + configurations.rt).asPath) { - sourceroots { - sourceSets.main.java.srcDirs.each { - pathelement(location:it.absolutePath) - } - } - } - } -} - -compileTestJava { - actions = [] - dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileTestJava") - dependsOn jar - - def outputDir = project.sourceSets.test.java.outputDir - inputs.files(project.sourceSets.test.allSource + project.sourceSets.test.compileClasspath) - outputs.dir outputDir - - sourceCompatibility = 1.8 // fixed even when general compatibility level set to e.g. 10 - targetCompatibility = 1.8 +sourceSets.main.aspectj.srcDir "src/main/java" +sourceSets.main.java.srcDirs = files() - doLast{ - // Assemble runtime classpath from folders and JARs that actually exist - def runtimeClasspath = project.files(sourceSets.test.runtimeClasspath.files.findAll({ it.exists() })) - - ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", - classpath: configurations.ajc.asPath) - - ant.iajc(source: sourceCompatibility, target: targetCompatibility, - maxmem: "1024m", fork: "true", Xlint: "ignore", - destDir: outputDir.absolutePath, - aspectPath: jar.archivePath, - inpath: configurations.ajInpath.asPath, - classpath: (runtimeClasspath + project.files(jar.archivePath) + configurations.rt).asPath) { - sourceroots { - sourceSets.test.java.srcDirs.each { - pathelement(location:it.absolutePath) - } - } - } - } -} +sourceSets.test.aspectj.srcDir "src/test/java" +sourceSets.test.java.srcDirs = files() +aspectj.version = dependencyManagement.managedVersions['org.aspectj:aspectjweaver'] dependencies { - aspects(project(":spring-orm")) - ajc("org.aspectj:aspectjtools:${aspectjVersion}") - rt("org.aspectj:aspectjrt:${aspectjVersion}") - compile("org.aspectj:aspectjweaver:${aspectjVersion}") + compile("org.aspectj:aspectjweaver") + compileOnly("org.aspectj:aspectjrt") optional(project(":spring-aop")) // for @Async support optional(project(":spring-beans")) // for @Configurable support optional(project(":spring-context")) // for @Enable* support optional(project(":spring-context-support")) // for JavaMail and JSR-107 support optional(project(":spring-orm")) // for JPA exception translation support optional(project(":spring-tx")) // for JPA, @Transactional support - optional("javax.cache:cache-api:1.1.0") // for JCache aspect - optional("javax.transaction:javax.transaction-api:1.3") // for @javax.transaction.Transactional support + optional("javax.cache:cache-api") // for JCache aspect + optional("javax.transaction:javax.transaction-api") // for @javax.transaction.Transactional support testCompile(project(":spring-core")) // for CodeStyleAspect testCompile(project(":spring-test")) - testCompile("javax.mail:javax.mail-api:1.6.2") + testCompile(testFixtures(project(":spring-context"))) + testCompile(testFixtures(project(":spring-context-support"))) + testCompile(testFixtures(project(":spring-core"))) + testCompile(testFixtures(project(":spring-tx"))) + testCompile("javax.mail:javax.mail-api") + testCompileOnly("org.aspectj:aspectjrt") } eclipse.project { diff --git a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractDependencyInjectionAspect.aj b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractDependencyInjectionAspect.aj index 73030d03d7ff..94f9f8c6ded5 100644 --- a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractDependencyInjectionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractDependencyInjectionAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractInterfaceDrivenDependencyInjectionAspect.aj b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractInterfaceDrivenDependencyInjectionAspect.aj index a65be5f5bb0d..6e049a58a045 100644 --- a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractInterfaceDrivenDependencyInjectionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AbstractInterfaceDrivenDependencyInjectionAspect.aj @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -67,6 +67,7 @@ import java.io.Serializable; * @since 2.5.2 */ public abstract aspect AbstractInterfaceDrivenDependencyInjectionAspect extends AbstractDependencyInjectionAspect { + /** * Select initialization join point as object construction */ diff --git a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerAspect.aj b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerAspect.aj index e1c09275bd32..923e459b4438 100644 --- a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerAspect.aj @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -47,22 +47,26 @@ import org.springframework.beans.factory.wiring.BeanConfigurerSupport; public aspect AnnotationBeanConfigurerAspect extends AbstractInterfaceDrivenDependencyInjectionAspect implements BeanFactoryAware, InitializingBean, DisposableBean { - private BeanConfigurerSupport beanConfigurerSupport = new BeanConfigurerSupport(); + private final BeanConfigurerSupport beanConfigurerSupport = new BeanConfigurerSupport(); + @Override public void setBeanFactory(BeanFactory beanFactory) { this.beanConfigurerSupport.setBeanWiringInfoResolver(new AnnotationBeanWiringInfoResolver()); this.beanConfigurerSupport.setBeanFactory(beanFactory); } + @Override public void afterPropertiesSet() { this.beanConfigurerSupport.afterPropertiesSet(); } + @Override public void configureBean(Object bean) { this.beanConfigurerSupport.configureBean(bean); } + @Override public void destroy() { this.beanConfigurerSupport.destroy(); } diff --git a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/ConfigurableObject.java b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/ConfigurableObject.java index 869b7a72d8ad..c670dfc06927 100644 --- a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/ConfigurableObject.java +++ b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/ConfigurableObject.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/GenericInterfaceDrivenDependencyInjectionAspect.aj b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/GenericInterfaceDrivenDependencyInjectionAspect.aj index 2151561cb90f..867ecffb4fba 100644 --- a/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/GenericInterfaceDrivenDependencyInjectionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/beans/factory/aspectj/GenericInterfaceDrivenDependencyInjectionAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AbstractCacheAspect.aj b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AbstractCacheAspect.aj index 3b4b382031e9..ae6d6a34895b 100644 --- a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AbstractCacheAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AbstractCacheAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnnotationCacheAspect.aj b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnnotationCacheAspect.aj index 5418488b5329..671b3a66696e 100644 --- a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnnotationCacheAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnnotationCacheAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnyThrow.java b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnyThrow.java index 167cff4a437b..c59bce7b7544 100644 --- a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnyThrow.java +++ b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AnyThrow.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java index 7a47d2e1c4c7..4f0a383d55a6 100644 --- a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJJCacheConfiguration.java b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJJCacheConfiguration.java index 73bf36f068ce..524be193e2df 100644 --- a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJJCacheConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJJCacheConfiguration.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/cache/aspectj/JCacheCacheAspect.aj b/spring-aspects/src/main/java/org/springframework/cache/aspectj/JCacheCacheAspect.aj index 4587fc00c09e..ebda3f292cc9 100644 --- a/spring-aspects/src/main/java/org/springframework/cache/aspectj/JCacheCacheAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/cache/aspectj/JCacheCacheAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/EnableSpringConfigured.java b/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/EnableSpringConfigured.java index 41bb13d2e1ff..e26a94f71b55 100644 --- a/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/EnableSpringConfigured.java +++ b/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/EnableSpringConfigured.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java b/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java index 05cd5e4caf36..eb369ca49c58 100644 --- a/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AbstractAsyncExecutionAspect.aj b/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AbstractAsyncExecutionAspect.aj index 9efd0a4b93ba..eed22f42743c 100644 --- a/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AbstractAsyncExecutionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AbstractAsyncExecutionAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspect.aj b/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspect.aj index 2b22ec906181..46c1b4530aec 100644 --- a/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AspectJAsyncConfiguration.java b/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AspectJAsyncConfiguration.java index 539fcb9f2582..e13a595b42d4 100644 --- a/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AspectJAsyncConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AspectJAsyncConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -37,6 +37,7 @@ * @see org.springframework.scheduling.annotation.ProxyAsyncConfiguration */ @Configuration +@Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class AspectJAsyncConfiguration extends AbstractAsyncConfiguration { @Bean(name = TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME) diff --git a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AbstractTransactionAspect.aj b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AbstractTransactionAspect.aj index 17204f9310c2..782ca35e0777 100644 --- a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AbstractTransactionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AbstractTransactionAspect.aj @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -59,7 +59,8 @@ public abstract aspect AbstractTransactionAspect extends TransactionAspectSuppor @Override public void destroy() { - clearTransactionManagerCache(); // An aspect is basically a singleton + // An aspect is basically a singleton -> cleanup on destruction + clearTransactionManagerCache(); } @SuppressAjWarnings("adviceDidNotMatch") diff --git a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AnnotationTransactionAspect.aj b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AnnotationTransactionAspect.aj index f98093d41162..bdaae703b0dc 100644 --- a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AnnotationTransactionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AnnotationTransactionAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJJtaTransactionManagementConfiguration.java b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJJtaTransactionManagementConfiguration.java index fb644851f1af..ec733d3cf2b9 100644 --- a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJJtaTransactionManagementConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJJtaTransactionManagementConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -36,6 +36,7 @@ * @see TransactionManagementConfigurationSelector */ @Configuration +@Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class AspectJJtaTransactionManagementConfiguration extends AspectJTransactionManagementConfiguration { @Bean(name = TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_BEAN_NAME) diff --git a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJTransactionManagementConfiguration.java b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJTransactionManagementConfiguration.java index e0662bd6a8da..2c99c3050744 100644 --- a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJTransactionManagementConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AspectJTransactionManagementConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -38,6 +38,7 @@ * @see AspectJJtaTransactionManagementConfiguration */ @Configuration +@Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class AspectJTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration { @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ASPECT_BEAN_NAME) diff --git a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj index 22a3bb134b55..1644ce50651f 100644 --- a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/main/java/overview.html b/spring-aspects/src/main/java/overview.html deleted file mode 100644 index 2dd5b0922405..000000000000 --- a/spring-aspects/src/main/java/overview.html +++ /dev/null @@ -1,7 +0,0 @@ - - -

-Spring's AspectJ-based aspects. -

- - diff --git a/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/AutoProxyWithCodeStyleAspectsTests.java b/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/AutoProxyWithCodeStyleAspectsTests.java index c0c1e4b1d471..43947fa29c29 100644 --- a/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/AutoProxyWithCodeStyleAspectsTests.java +++ b/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/AutoProxyWithCodeStyleAspectsTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -16,7 +16,7 @@ package org.springframework.aop.aspectj.autoproxy; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; diff --git a/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/CodeStyleAspect.aj b/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/CodeStyleAspect.aj index cd3742f6e88f..0ba9e0dd5a6a 100644 --- a/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/CodeStyleAspect.aj +++ b/spring-aspects/src/test/java/org/springframework/aop/aspectj/autoproxy/CodeStyleAspect.aj @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/ShouldBeConfiguredBySpring.java b/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/ShouldBeConfiguredBySpring.java index facb622cb083..586d9364b5b8 100644 --- a/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/ShouldBeConfiguredBySpring.java +++ b/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/ShouldBeConfiguredBySpring.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/SpringConfiguredWithAutoProxyingTests.java b/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/SpringConfiguredWithAutoProxyingTests.java index 4ee0f22820c7..a02bcad6793b 100644 --- a/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/SpringConfiguredWithAutoProxyingTests.java +++ b/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/SpringConfiguredWithAutoProxyingTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -16,7 +16,7 @@ package org.springframework.beans.factory.aspectj; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; diff --git a/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/XmlBeanConfigurerTests.java b/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/XmlBeanConfigurerTests.java index 501d4cd50dd6..71e98f68295d 100644 --- a/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/XmlBeanConfigurerTests.java +++ b/spring-aspects/src/test/java/org/springframework/beans/factory/aspectj/XmlBeanConfigurerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,11 +16,11 @@ package org.springframework.beans.factory.aspectj; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Chris Beams @@ -33,7 +33,7 @@ public void injection() { "org/springframework/beans/factory/aspectj/beanConfigurerTests.xml")) { ShouldBeConfiguredBySpring myObject = new ShouldBeConfiguredBySpring(); - assertEquals("Rod", myObject.getName()); + assertThat(myObject.getName()).isEqualTo("Rod"); } } diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AbstractCacheAnnotationTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AbstractCacheAnnotationTests.java new file mode 100644 index 000000000000..eed37f21ec04 --- /dev/null +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AbstractCacheAnnotationTests.java @@ -0,0 +1,882 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cache.aspectj; + +import java.util.Collection; +import java.util.UUID; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.springframework.aop.framework.AopProxyUtils; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.config.AnnotatedClassCacheableService; +import org.springframework.cache.config.CacheableService; +import org.springframework.cache.config.TestEntity; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.testfixture.cache.SomeCustomKeyGenerator; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIOException; + +/** + * Copy of the shared {@code AbstractCacheAnnotationTests}: necessary + * due to issues with Gradle test fixtures and AspectJ configuration + * in the Gradle build. + * + *

Abstract cache annotation tests (containing several reusable methods). + * + * @author Costin Leau + * @author Chris Beams + * @author Phillip Webb + * @author Stephane Nicoll + */ +public abstract class AbstractCacheAnnotationTests { + + protected ConfigurableApplicationContext ctx; + + protected CacheableService cs; + + protected CacheableService ccs; + + protected CacheManager cm; + + + /** + * @return a refreshed application context + */ + protected abstract ConfigurableApplicationContext getApplicationContext(); + + + @BeforeEach + public void setup() { + this.ctx = getApplicationContext(); + this.cs = ctx.getBean("service", CacheableService.class); + this.ccs = ctx.getBean("classService", CacheableService.class); + this.cm = ctx.getBean("cacheManager", CacheManager.class); + + Collection cn = this.cm.getCacheNames(); + assertThat(cn).containsOnly("testCache", "secondary", "primary"); + } + + @AfterEach + public void close() { + if (this.ctx != null) { + this.ctx.close(); + } + } + + + protected void testCacheable(CacheableService service) { + Object o1 = new Object(); + + Object r1 = service.cache(o1); + Object r2 = service.cache(o1); + Object r3 = service.cache(o1); + + assertThat(r2).isSameAs(r1); + assertThat(r3).isSameAs(r1); + } + + protected void testCacheableNull(CacheableService service) { + Object o1 = new Object(); + assertThat(this.cm.getCache("testCache").get(o1)).isNull(); + + Object r1 = service.cacheNull(o1); + Object r2 = service.cacheNull(o1); + Object r3 = service.cacheNull(o1); + + assertThat(r2).isSameAs(r1); + assertThat(r3).isSameAs(r1); + + assertThat(this.cm.getCache("testCache")).as("testCache").isNotNull(); + assertThat(this.cm.getCache("testCache").get(o1)).as("cached object").isNotNull(); + assertThat(this.cm.getCache("testCache").get(o1).get()).isEqualTo(r3); + assertThat(r3).as("Cached value should be null").isNull(); + } + + protected void testCacheableSync(CacheableService service) { + Object o1 = new Object(); + + Object r1 = service.cacheSync(o1); + Object r2 = service.cacheSync(o1); + Object r3 = service.cacheSync(o1); + + assertThat(r2).isSameAs(r1); + assertThat(r3).isSameAs(r1); + } + + protected void testCacheableSyncNull(CacheableService service) { + Object o1 = new Object(); + assertThat(this.cm.getCache("testCache").get(o1)).isNull(); + + Object r1 = service.cacheSyncNull(o1); + Object r2 = service.cacheSyncNull(o1); + Object r3 = service.cacheSyncNull(o1); + + assertThat(r2).isSameAs(r1); + assertThat(r3).isSameAs(r1); + + assertThat(this.cm.getCache("testCache").get(o1).get()).isEqualTo(r3); + assertThat(r3).as("Cached value should be null").isNull(); + } + + protected void testEvict(CacheableService service, boolean successExpected) { + Cache cache = this.cm.getCache("testCache"); + + Object o1 = new Object(); + cache.putIfAbsent(o1, -1L); + Object r1 = service.cache(o1); + + service.evict(o1, null); + if (successExpected) { + assertThat(cache.get(o1)).isNull(); + } + else { + assertThat(cache.get(o1)).isNotNull(); + } + + Object r2 = service.cache(o1); + if (successExpected) { + assertThat(r2).isNotSameAs(r1); + } + else { + assertThat(r2).isSameAs(r1); + } + } + + protected void testEvictEarly(CacheableService service) { + Cache cache = this.cm.getCache("testCache"); + + Object o1 = new Object(); + cache.putIfAbsent(o1, -1L); + Object r1 = service.cache(o1); + + try { + service.evictEarly(o1); + } + catch (RuntimeException ex) { + // expected + } + assertThat(cache.get(o1)).isNull(); + + Object r2 = service.cache(o1); + assertThat(r2).isNotSameAs(r1); + } + + protected void testEvictException(CacheableService service) { + Object o1 = new Object(); + Object r1 = service.cache(o1); + + try { + service.evictWithException(o1); + } + catch (RuntimeException ex) { + // expected + } + // exception occurred, eviction skipped, data should still be in the cache + Object r2 = service.cache(o1); + assertThat(r2).isSameAs(r1); + } + + protected void testEvictWithKey(CacheableService service) { + Object o1 = new Object(); + Object r1 = service.cache(o1); + + service.evict(o1, null); + Object r2 = service.cache(o1); + assertThat(r2).isNotSameAs(r1); + } + + protected void testEvictWithKeyEarly(CacheableService service) { + Object o1 = new Object(); + Object r1 = service.cache(o1); + + try { + service.evictEarly(o1); + } + catch (Exception ex) { + // expected + } + Object r2 = service.cache(o1); + assertThat(r2).isNotSameAs(r1); + } + + protected void testEvictAll(CacheableService service, boolean successExpected) { + Cache cache = this.cm.getCache("testCache"); + + Object o1 = new Object(); + Object o2 = new Object(); + cache.putIfAbsent(o1, -1L); + cache.putIfAbsent(o2, -2L); + + Object r1 = service.cache(o1); + Object r2 = service.cache(o2); + assertThat(r2).isNotSameAs(r1); + + service.evictAll(new Object()); + if (successExpected) { + assertThat(cache.get(o1)).isNull(); + assertThat(cache.get(o2)).isNull(); + } + else { + assertThat(cache.get(o1)).isNotNull(); + assertThat(cache.get(o2)).isNotNull(); + } + + Object r3 = service.cache(o1); + Object r4 = service.cache(o2); + if (successExpected) { + assertThat(r3).isNotSameAs(r1); + assertThat(r4).isNotSameAs(r2); + } + else { + assertThat(r3).isSameAs(r1); + assertThat(r4).isSameAs(r2); + } + } + + protected void testEvictAllEarly(CacheableService service) { + Cache cache = this.cm.getCache("testCache"); + + Object o1 = new Object(); + Object o2 = new Object(); + cache.putIfAbsent(o1, -1L); + cache.putIfAbsent(o2, -2L); + + Object r1 = service.cache(o1); + Object r2 = service.cache(o2); + assertThat(r2).isNotSameAs(r1); + + try { + service.evictAllEarly(new Object()); + } + catch (Exception ex) { + // expected + } + assertThat(cache.get(o1)).isNull(); + assertThat(cache.get(o2)).isNull(); + + Object r3 = service.cache(o1); + Object r4 = service.cache(o2); + assertThat(r3).isNotSameAs(r1); + assertThat(r4).isNotSameAs(r2); + } + + protected void testConditionalExpression(CacheableService service) { + Object r1 = service.conditional(4); + Object r2 = service.conditional(4); + + assertThat(r2).isNotSameAs(r1); + + Object r3 = service.conditional(3); + Object r4 = service.conditional(3); + + assertThat(r4).isSameAs(r3); + } + + protected void testConditionalExpressionSync(CacheableService service) { + Object r1 = service.conditionalSync(4); + Object r2 = service.conditionalSync(4); + + assertThat(r2).isNotSameAs(r1); + + Object r3 = service.conditionalSync(3); + Object r4 = service.conditionalSync(3); + + assertThat(r4).isSameAs(r3); + } + + protected void testUnlessExpression(CacheableService service) { + Cache cache = this.cm.getCache("testCache"); + cache.clear(); + service.unless(10); + service.unless(11); + assertThat(cache.get(10).get()).isEqualTo(10L); + assertThat(cache.get(11)).isNull(); + } + + protected void testKeyExpression(CacheableService service) { + Object r1 = service.key(5, 1); + Object r2 = service.key(5, 2); + + assertThat(r2).isSameAs(r1); + + Object r3 = service.key(1, 5); + Object r4 = service.key(2, 5); + + assertThat(r4).isNotSameAs(r3); + } + + protected void testVarArgsKey(CacheableService service) { + Object r1 = service.varArgsKey(1, 2, 3); + Object r2 = service.varArgsKey(1, 2, 3); + + assertThat(r2).isSameAs(r1); + + Object r3 = service.varArgsKey(1, 2, 3); + Object r4 = service.varArgsKey(1, 2); + + assertThat(r4).isNotSameAs(r3); + } + + protected void testNullValue(CacheableService service) { + Object key = new Object(); + assertThat(service.nullValue(key)).isNull(); + int nr = service.nullInvocations().intValue(); + assertThat(service.nullValue(key)).isNull(); + assertThat(service.nullInvocations().intValue()).isEqualTo(nr); + assertThat(service.nullValue(new Object())).isNull(); + assertThat(service.nullInvocations().intValue()).isEqualTo(nr + 1); + } + + protected void testMethodName(CacheableService service, String keyName) { + Object key = new Object(); + Object r1 = service.name(key); + assertThat(service.name(key)).isSameAs(r1); + Cache cache = this.cm.getCache("testCache"); + // assert the method name is used + assertThat(cache.get(keyName)).isNotNull(); + } + + protected void testRootVars(CacheableService service) { + Object key = new Object(); + Object r1 = service.rootVars(key); + assertThat(service.rootVars(key)).isSameAs(r1); + Cache cache = this.cm.getCache("testCache"); + // assert the method name is used + String expectedKey = "rootVarsrootVars" + AopProxyUtils.ultimateTargetClass(service) + service; + assertThat(cache.get(expectedKey)).isNotNull(); + } + + protected void testCheckedThrowable(CacheableService service) { + String arg = UUID.randomUUID().toString(); + assertThatIOException().isThrownBy(() -> + service.throwChecked(arg)) + .withMessage(arg); + } + + protected void testUncheckedThrowable(CacheableService service) { + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.throwUnchecked(1L)) + .withMessage("1"); + } + + protected void testCheckedThrowableSync(CacheableService service) { + String arg = UUID.randomUUID().toString(); + assertThatIOException().isThrownBy(() -> + service.throwCheckedSync(arg)) + .withMessage(arg); + } + + protected void testUncheckedThrowableSync(CacheableService service) { + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.throwUncheckedSync(1L)) + .withMessage("1"); + } + + protected void testNullArg(CacheableService service) { + Object r1 = service.cache(null); + assertThat(service.cache(null)).isSameAs(r1); + } + + protected void testCacheUpdate(CacheableService service) { + Object o = new Object(); + Cache cache = this.cm.getCache("testCache"); + assertThat(cache.get(o)).isNull(); + Object r1 = service.update(o); + assertThat(cache.get(o).get()).isSameAs(r1); + + o = new Object(); + assertThat(cache.get(o)).isNull(); + Object r2 = service.update(o); + assertThat(cache.get(o).get()).isSameAs(r2); + } + + protected void testConditionalCacheUpdate(CacheableService service) { + Integer one = 1; + Integer three = 3; + + Cache cache = this.cm.getCache("testCache"); + assertThat((int) Integer.valueOf(service.conditionalUpdate(one).toString())).isEqualTo((int) one); + assertThat(cache.get(one)).isNull(); + + assertThat((int) Integer.valueOf(service.conditionalUpdate(three).toString())).isEqualTo((int) three); + assertThat((int) Integer.valueOf(cache.get(three).get().toString())).isEqualTo((int) three); + } + + protected void testMultiCache(CacheableService service) { + Object o1 = new Object(); + Object o2 = new Object(); + + Cache primary = this.cm.getCache("primary"); + Cache secondary = this.cm.getCache("secondary"); + + assertThat(primary.get(o1)).isNull(); + assertThat(secondary.get(o1)).isNull(); + Object r1 = service.multiCache(o1); + assertThat(primary.get(o1).get()).isSameAs(r1); + assertThat(secondary.get(o1).get()).isSameAs(r1); + + Object r2 = service.multiCache(o1); + Object r3 = service.multiCache(o1); + + assertThat(r2).isSameAs(r1); + assertThat(r3).isSameAs(r1); + + assertThat(primary.get(o2)).isNull(); + assertThat(secondary.get(o2)).isNull(); + Object r4 = service.multiCache(o2); + assertThat(primary.get(o2).get()).isSameAs(r4); + assertThat(secondary.get(o2).get()).isSameAs(r4); + } + + protected void testMultiEvict(CacheableService service) { + Object o1 = new Object(); + Object o2 = o1.toString() + "A"; + + + Object r1 = service.multiCache(o1); + Object r2 = service.multiCache(o1); + + Cache primary = this.cm.getCache("primary"); + Cache secondary = this.cm.getCache("secondary"); + + primary.put(o2, o2); + assertThat(r2).isSameAs(r1); + assertThat(primary.get(o1).get()).isSameAs(r1); + assertThat(secondary.get(o1).get()).isSameAs(r1); + + service.multiEvict(o1); + assertThat(primary.get(o1)).isNull(); + assertThat(secondary.get(o1)).isNull(); + assertThat(primary.get(o2)).isNull(); + + Object r3 = service.multiCache(o1); + Object r4 = service.multiCache(o1); + assertThat(r3).isNotSameAs(r1); + assertThat(r4).isSameAs(r3); + + assertThat(primary.get(o1).get()).isSameAs(r3); + assertThat(secondary.get(o1).get()).isSameAs(r4); + } + + protected void testMultiPut(CacheableService service) { + Object o = 1; + + Cache primary = this.cm.getCache("primary"); + Cache secondary = this.cm.getCache("secondary"); + + assertThat(primary.get(o)).isNull(); + assertThat(secondary.get(o)).isNull(); + Object r1 = service.multiUpdate(o); + assertThat(primary.get(o).get()).isSameAs(r1); + assertThat(secondary.get(o).get()).isSameAs(r1); + + o = 2; + assertThat(primary.get(o)).isNull(); + assertThat(secondary.get(o)).isNull(); + Object r2 = service.multiUpdate(o); + assertThat(primary.get(o).get()).isSameAs(r2); + assertThat(secondary.get(o).get()).isSameAs(r2); + } + + protected void testPutRefersToResult(CacheableService service) { + Long id = Long.MIN_VALUE; + TestEntity entity = new TestEntity(); + Cache primary = this.cm.getCache("primary"); + assertThat(primary.get(id)).isNull(); + assertThat(entity.getId()).isNull(); + service.putRefersToResult(entity); + assertThat(primary.get(id).get()).isSameAs(entity); + } + + protected void testMultiCacheAndEvict(CacheableService service) { + String methodName = "multiCacheAndEvict"; + + Cache primary = this.cm.getCache("primary"); + Cache secondary = this.cm.getCache("secondary"); + Object key = 1; + + secondary.put(key, key); + + assertThat(secondary.get(methodName)).isNull(); + assertThat(secondary.get(key).get()).isSameAs(key); + + Object r1 = service.multiCacheAndEvict(key); + assertThat(service.multiCacheAndEvict(key)).isSameAs(r1); + + // assert the method name is used + assertThat(primary.get(methodName).get()).isSameAs(r1); + assertThat(secondary.get(methodName)).isNull(); + assertThat(secondary.get(key)).isNull(); + } + + protected void testMultiConditionalCacheAndEvict(CacheableService service) { + Cache primary = this.cm.getCache("primary"); + Cache secondary = this.cm.getCache("secondary"); + Object key = 1; + + secondary.put(key, key); + + assertThat(primary.get(key)).isNull(); + assertThat(secondary.get(key).get()).isSameAs(key); + + Object r1 = service.multiConditionalCacheAndEvict(key); + Object r3 = service.multiConditionalCacheAndEvict(key); + + assertThat(!r1.equals(r3)).isTrue(); + assertThat(primary.get(key)).isNull(); + + Object key2 = 3; + Object r2 = service.multiConditionalCacheAndEvict(key2); + assertThat(service.multiConditionalCacheAndEvict(key2)).isSameAs(r2); + + // assert the method name is used + assertThat(primary.get(key2).get()).isSameAs(r2); + assertThat(secondary.get(key2)).isNull(); + } + + @Test + public void testCacheable() { + testCacheable(this.cs); + } + + @Test + public void testCacheableNull() { + testCacheableNull(this.cs); + } + + @Test + public void testCacheableSync() { + testCacheableSync(this.cs); + } + + @Test + public void testCacheableSyncNull() { + testCacheableSyncNull(this.cs); + } + + @Test + public void testEvict() { + testEvict(this.cs, true); + } + + @Test + public void testEvictEarly() { + testEvictEarly(this.cs); + } + + @Test + public void testEvictWithException() { + testEvictException(this.cs); + } + + @Test + public void testEvictAll() { + testEvictAll(this.cs, true); + } + + @Test + public void testEvictAllEarly() { + testEvictAllEarly(this.cs); + } + + @Test + public void testEvictWithKey() { + testEvictWithKey(this.cs); + } + + @Test + public void testEvictWithKeyEarly() { + testEvictWithKeyEarly(this.cs); + } + + @Test + public void testConditionalExpression() { + testConditionalExpression(this.cs); + } + + @Test + public void testConditionalExpressionSync() { + testConditionalExpressionSync(this.cs); + } + + @Test + public void testUnlessExpression() { + testUnlessExpression(this.cs); + } + + @Test + public void testClassCacheUnlessExpression() { + testUnlessExpression(this.cs); + } + + @Test + public void testKeyExpression() { + testKeyExpression(this.cs); + } + + @Test + public void testVarArgsKey() { + testVarArgsKey(this.cs); + } + + @Test + public void testClassCacheCacheable() { + testCacheable(this.ccs); + } + + @Test + public void testClassCacheEvict() { + testEvict(this.ccs, true); + } + + @Test + public void testClassEvictEarly() { + testEvictEarly(this.ccs); + } + + @Test + public void testClassEvictAll() { + testEvictAll(this.ccs, true); + } + + @Test + public void testClassEvictWithException() { + testEvictException(this.ccs); + } + + @Test + public void testClassCacheEvictWithWKey() { + testEvictWithKey(this.ccs); + } + + @Test + public void testClassEvictWithKeyEarly() { + testEvictWithKeyEarly(this.ccs); + } + + @Test + public void testNullValue() { + testNullValue(this.cs); + } + + @Test + public void testClassNullValue() { + Object key = new Object(); + assertThat(this.ccs.nullValue(key)).isNull(); + int nr = this.ccs.nullInvocations().intValue(); + assertThat(this.ccs.nullValue(key)).isNull(); + assertThat(this.ccs.nullInvocations().intValue()).isEqualTo(nr); + assertThat(this.ccs.nullValue(new Object())).isNull(); + // the check method is also cached + assertThat(this.ccs.nullInvocations().intValue()).isEqualTo(nr); + assertThat(AnnotatedClassCacheableService.nullInvocations.intValue()).isEqualTo(nr + 1); + } + + @Test + public void testMethodName() { + testMethodName(this.cs, "name"); + } + + @Test + public void testClassMethodName() { + testMethodName(this.ccs, "nametestCache"); + } + + @Test + public void testRootVars() { + testRootVars(this.cs); + } + + @Test + public void testClassRootVars() { + testRootVars(this.ccs); + } + + @Test + public void testCustomKeyGenerator() { + Object param = new Object(); + Object r1 = this.cs.customKeyGenerator(param); + assertThat(this.cs.customKeyGenerator(param)).isSameAs(r1); + Cache cache = this.cm.getCache("testCache"); + // Checks that the custom keyGenerator was used + Object expectedKey = SomeCustomKeyGenerator.generateKey("customKeyGenerator", param); + assertThat(cache.get(expectedKey)).isNotNull(); + } + + @Test + public void testUnknownCustomKeyGenerator() { + Object param = new Object(); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + this.cs.unknownCustomKeyGenerator(param)); + } + + @Test + public void testCustomCacheManager() { + CacheManager customCm = this.ctx.getBean("customCacheManager", CacheManager.class); + Object key = new Object(); + Object r1 = this.cs.customCacheManager(key); + assertThat(this.cs.customCacheManager(key)).isSameAs(r1); + + Cache cache = customCm.getCache("testCache"); + assertThat(cache.get(key)).isNotNull(); + } + + @Test + public void testUnknownCustomCacheManager() { + Object param = new Object(); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + this.cs.unknownCustomCacheManager(param)); + } + + @Test + public void testNullArg() { + testNullArg(this.cs); + } + + @Test + public void testClassNullArg() { + testNullArg(this.ccs); + } + + @Test + public void testCheckedException() { + testCheckedThrowable(this.cs); + } + + @Test + public void testClassCheckedException() { + testCheckedThrowable(this.ccs); + } + + @Test + public void testCheckedExceptionSync() { + testCheckedThrowableSync(this.cs); + } + + @Test + public void testClassCheckedExceptionSync() { + testCheckedThrowableSync(this.ccs); + } + + @Test + public void testUncheckedException() { + testUncheckedThrowable(this.cs); + } + + @Test + public void testClassUncheckedException() { + testUncheckedThrowable(this.ccs); + } + + @Test + public void testUncheckedExceptionSync() { + testUncheckedThrowableSync(this.cs); + } + + @Test + public void testClassUncheckedExceptionSync() { + testUncheckedThrowableSync(this.ccs); + } + + @Test + public void testUpdate() { + testCacheUpdate(this.cs); + } + + @Test + public void testClassUpdate() { + testCacheUpdate(this.ccs); + } + + @Test + public void testConditionalUpdate() { + testConditionalCacheUpdate(this.cs); + } + + @Test + public void testClassConditionalUpdate() { + testConditionalCacheUpdate(this.ccs); + } + + @Test + public void testMultiCache() { + testMultiCache(this.cs); + } + + @Test + public void testClassMultiCache() { + testMultiCache(this.ccs); + } + + @Test + public void testMultiEvict() { + testMultiEvict(this.cs); + } + + @Test + public void testClassMultiEvict() { + testMultiEvict(this.ccs); + } + + @Test + public void testMultiPut() { + testMultiPut(this.cs); + } + + @Test + public void testClassMultiPut() { + testMultiPut(this.ccs); + } + + @Test + public void testPutRefersToResult() { + testPutRefersToResult(this.cs); + } + + @Test + public void testClassPutRefersToResult() { + testPutRefersToResult(this.ccs); + } + + @Test + public void testMultiCacheAndEvict() { + testMultiCacheAndEvict(this.cs); + } + + @Test + public void testClassMultiCacheAndEvict() { + testMultiCacheAndEvict(this.ccs); + } + + @Test + public void testMultiConditionalCacheAndEvict() { + testMultiConditionalCacheAndEvict(this.cs); + } + + @Test + public void testClassMultiConditionalCacheAndEvict() { + testMultiConditionalCacheAndEvict(this.ccs); + } + +} diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJCacheAnnotationTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJCacheAnnotationTests.java index df81ab512673..4601c0ea814c 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJCacheAnnotationTests.java +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJCacheAnnotationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,16 +16,14 @@ package org.springframework.cache.aspectj; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; -import org.springframework.cache.config.AbstractCacheAnnotationTests; import org.springframework.cache.config.CacheableService; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Costin Leau @@ -39,14 +37,14 @@ protected ConfigurableApplicationContext getApplicationContext() { } @Test - public void testKeyStrategy() throws Exception { + public void testKeyStrategy() { AnnotationCacheAspect aspect = ctx.getBean( "org.springframework.cache.config.internalCacheAspect", AnnotationCacheAspect.class); - Assert.assertSame(ctx.getBean("keyGenerator"), aspect.getKeyGenerator()); + assertThat(aspect.getKeyGenerator()).isSameAs(ctx.getBean("keyGenerator")); } @Override - public void testMultiEvict(CacheableService service) { + protected void testMultiEvict(CacheableService service) { Object o1 = new Object(); Object r1 = service.multiCache(o1); @@ -55,21 +53,21 @@ public void testMultiEvict(CacheableService service) { Cache primary = cm.getCache("primary"); Cache secondary = cm.getCache("secondary"); - assertSame(r1, r2); - assertSame(r1, primary.get(o1).get()); - assertSame(r1, secondary.get(o1).get()); + assertThat(r2).isSameAs(r1); + assertThat(primary.get(o1).get()).isSameAs(r1); + assertThat(secondary.get(o1).get()).isSameAs(r1); service.multiEvict(o1); - assertNull(primary.get(o1)); - assertNull(secondary.get(o1)); + assertThat(primary.get(o1)).isNull(); + assertThat(secondary.get(o1)).isNull(); Object r3 = service.multiCache(o1); Object r4 = service.multiCache(o1); - assertNotSame(r1, r3); - assertSame(r3, r4); + assertThat(r3).isNotSameAs(r1); + assertThat(r4).isSameAs(r3); - assertSame(r3, primary.get(o1).get()); - assertSame(r4, secondary.get(o1).get()); + assertThat(primary.get(o1).get()).isSameAs(r3); + assertThat(secondary.get(o1).get()).isSameAs(r4); } } diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java index 269f607dbb11..7ca1037efbc4 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,20 +16,14 @@ package org.springframework.cache.aspectj; -import org.junit.After; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.cache.CacheManager; -import org.springframework.cache.CacheTestUtils; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.config.AnnotatedClassCacheableService; -import org.springframework.cache.config.CacheableService; -import org.springframework.cache.config.DefaultCacheableService; -import org.springframework.cache.config.SomeCustomKeyGenerator; -import org.springframework.cache.config.SomeKeyGenerator; import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; @@ -42,8 +36,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.testfixture.cache.CacheTestUtils; +import org.springframework.context.testfixture.cache.SomeCustomKeyGenerator; +import org.springframework.context.testfixture.cache.SomeKeyGenerator; +import org.springframework.context.testfixture.cache.beans.AnnotatedClassCacheableService; +import org.springframework.context.testfixture.cache.beans.CacheableService; +import org.springframework.context.testfixture.cache.beans.DefaultCacheableService; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -57,7 +57,7 @@ private void load(Class... config) { this.ctx = new AnnotationConfigApplicationContext(config); } - @After + @AfterEach public void closeContext() { if (this.ctx != null) { this.ctx.close(); @@ -69,14 +69,14 @@ public void closeContext() { public void testKeyStrategy() { load(EnableCachingConfig.class); AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class); - assertSame(this.ctx.getBean("keyGenerator", KeyGenerator.class), aspect.getKeyGenerator()); + assertThat(aspect.getKeyGenerator()).isSameAs(this.ctx.getBean("keyGenerator", KeyGenerator.class)); } @Test public void testCacheErrorHandler() { load(EnableCachingConfig.class); AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class); - assertSame(this.ctx.getBean("errorHandler", CacheErrorHandler.class), aspect.getErrorHandler()); + assertThat(aspect.getErrorHandler()).isSameAs(this.ctx.getBean("errorHandler", CacheErrorHandler.class)); } @@ -93,7 +93,7 @@ public void multipleCacheManagerBeans() { load(MultiCacheManagerConfig.class); } catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("bean of type CacheManager")); + assertThat(ex.getMessage().contains("bean of type CacheManager")).isTrue(); } } @@ -109,8 +109,9 @@ public void multipleCachingConfigurers() { } catch (BeanCreationException ex) { Throwable root = ex.getRootCause(); - assertTrue(root instanceof IllegalStateException); - assertTrue(ex.getMessage().contains("implementations of CachingConfigurer")); + boolean condition = root instanceof IllegalStateException; + assertThat(condition).isTrue(); + assertThat(ex.getMessage().contains("implementations of CachingConfigurer")).isTrue(); } } @@ -120,19 +121,18 @@ public void noCacheManagerBeans() { load(EmptyConfig.class); } catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("no bean of type CacheManager")); + assertThat(ex.getMessage().contains("no bean of type CacheManager")).isTrue(); } } @Test - @Ignore("AspectJ has some sort of caching that makes this one fail") + @Disabled("AspectJ has some sort of caching that makes this one fail") public void emptyConfigSupport() { load(EmptyConfigSupportConfig.class); AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class); - assertNotNull(aspect.getCacheResolver()); - assertEquals(SimpleCacheResolver.class, aspect.getCacheResolver().getClass()); - assertSame(this.ctx.getBean(CacheManager.class), - ((SimpleCacheResolver) aspect.getCacheResolver()).getCacheManager()); + assertThat(aspect.getCacheResolver()).isNotNull(); + assertThat(aspect.getCacheResolver().getClass()).isEqualTo(SimpleCacheResolver.class); + assertThat(((SimpleCacheResolver) aspect.getCacheResolver()).getCacheManager()).isSameAs(this.ctx.getBean(CacheManager.class)); } @Test @@ -140,8 +140,8 @@ public void bothSetOnlyResolverIsUsed() { load(FullCachingConfig.class); AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class); - assertSame(this.ctx.getBean("cacheResolver"), aspect.getCacheResolver()); - assertSame(this.ctx.getBean("keyGenerator"), aspect.getKeyGenerator()); + assertThat(aspect.getCacheResolver()).isSameAs(this.ctx.getBean("cacheResolver")); + assertThat(aspect.getKeyGenerator()).isSameAs(this.ctx.getBean("keyGenerator")); } diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java index a8ae57d20a65..7e693d6ea13a 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,15 +17,11 @@ package org.springframework.cache.aspectj; import org.springframework.cache.CacheManager; -import org.springframework.cache.CacheTestUtils; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.config.AbstractCacheAnnotationTests; import org.springframework.cache.config.AnnotatedClassCacheableService; import org.springframework.cache.config.CacheableService; import org.springframework.cache.config.DefaultCacheableService; -import org.springframework.cache.config.SomeCustomKeyGenerator; -import org.springframework.cache.config.SomeKeyGenerator; import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.SimpleCacheErrorHandler; @@ -34,6 +30,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.testfixture.cache.CacheTestUtils; +import org.springframework.context.testfixture.cache.SomeCustomKeyGenerator; +import org.springframework.context.testfixture.cache.SomeKeyGenerator; /** * @author Stephane Nicoll diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJJavaConfigTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJJavaConfigTests.java index 3dd06ee0fec5..dc278365d727 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJJavaConfigTests.java +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJJavaConfigTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -23,13 +23,13 @@ import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.config.AnnotatedJCacheableService; -import org.springframework.cache.jcache.config.AbstractJCacheAnnotationTests; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AdviceMode; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.contextsupport.testfixture.jcache.AbstractJCacheAnnotationTests; /** * @author Stephane Nicoll diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJNamespaceConfigTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJNamespaceConfigTests.java index 8f423ce340fa..c755d8c3f4aa 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJNamespaceConfigTests.java +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/JCacheAspectJNamespaceConfigTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -16,19 +16,24 @@ package org.springframework.cache.aspectj; -import org.springframework.cache.jcache.config.AbstractJCacheAnnotationTests; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; +import org.springframework.contextsupport.testfixture.jcache.AbstractJCacheAnnotationTests; /** * @author Stephane Nicoll + * @author Sam Brannen */ public class JCacheAspectJNamespaceConfigTests extends AbstractJCacheAnnotationTests { @Override protected ApplicationContext getApplicationContext() { - return new GenericXmlApplicationContext( - "/org/springframework/cache/config/annotation-jcache-aspectj.xml"); + GenericXmlApplicationContext context = new GenericXmlApplicationContext(); + // Disallow bean definition overriding to test https://github.com/spring-projects/spring-framework/pull/27499 + context.setAllowBeanDefinitionOverriding(false); + context.load("/org/springframework/cache/config/annotation-jcache-aspectj.xml"); + context.refresh(); + return context; } } diff --git a/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java b/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java index a640b15c2a48..fa494c921280 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java +++ b/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -25,6 +25,10 @@ import org.springframework.cache.annotation.Caching; /** + * Copy of the shared {@code AbstractCacheAnnotationTests}: necessary + * due to issues with Gradle test fixtures and AspectJ configuration + * in the Gradle build. + * * @author Costin Leau * @author Phillip Webb * @author Stephane Nicoll @@ -33,11 +37,13 @@ public class AnnotatedClassCacheableService implements CacheableService { private final AtomicLong counter = new AtomicLong(); + public static final AtomicLong nullInvocations = new AtomicLong(); + @Override public Object cache(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @@ -48,7 +54,7 @@ public Object cacheNull(Object arg1) { @Override @Cacheable(cacheNames = "testCache", sync = true) public Object cacheSync(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @@ -68,13 +74,14 @@ public Object conditionalSync(int field) { } @Override + @Cacheable(cacheNames = "testCache", unless = "#result > 10") public Object unless(int arg) { return arg; } @Override - @CacheEvict("testCache") - public void invalidate(Object arg1) { + @CacheEvict(cacheNames = "testCache", key = "#p0") + public void evict(Object arg1, Object arg2) { } @Override @@ -83,11 +90,6 @@ public void evictWithException(Object arg1) { throw new RuntimeException("exception thrown - evict should NOT occur"); } - @Override - @CacheEvict(cacheNames = "testCache", allEntries = true) - public void evictAll(Object arg1) { - } - @Override @CacheEvict(cacheNames = "testCache", beforeInvocation = true) public void evictEarly(Object arg1) { @@ -95,68 +97,68 @@ public void evictEarly(Object arg1) { } @Override - @CacheEvict(cacheNames = "testCache", key = "#p0") - public void evict(Object arg1, Object arg2) { + @CacheEvict(cacheNames = "testCache", allEntries = true) + public void evictAll(Object arg1) { } @Override - @CacheEvict(cacheNames = "testCache", key = "#p0", beforeInvocation = true) - public void invalidateEarly(Object arg1, Object arg2) { + @CacheEvict(cacheNames = "testCache", allEntries = true, beforeInvocation = true) + public void evictAllEarly(Object arg1) { throw new RuntimeException("exception thrown - evict should still occur"); } @Override @Cacheable(cacheNames = "testCache", key = "#p0") public Object key(Object arg1, Object arg2) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable("testCache") public Object varArgsKey(Object... args) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.caches[0].name") public Object name(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target") public Object rootVars(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", keyGenerator = "customKyeGenerator") public Object customKeyGenerator(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", keyGenerator = "unknownBeanName") public Object unknownCustomKeyGenerator(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", cacheManager = "customCacheManager") public Object customCacheManager(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", cacheManager = "unknownBeanName") public Object unknownCustomCacheManager(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @CachePut("testCache") public Object update(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @@ -203,25 +205,25 @@ public Object throwUncheckedSync(Object arg1) { @Override @Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") }) public Object multiCache(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Caching(evict = { @CacheEvict("primary"), @CacheEvict(cacheNames = "secondary", key = "#a0"), @CacheEvict(cacheNames = "primary", key = "#p0 + 'A'") }) public Object multiEvict(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Caching(cacheable = { @Cacheable(cacheNames = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") }) public Object multiCacheAndEvict(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Caching(cacheable = { @Cacheable(cacheNames = "primary", condition = "#a0 == 3") }, evict = { @CacheEvict("secondary") }) public Object multiConditionalCacheAndEvict(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override diff --git a/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedJCacheableService.java b/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedJCacheableService.java index 30e17ee8433b..0b004f46b910 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedJCacheableService.java +++ b/spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedJCacheableService.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; + import javax.cache.annotation.CacheDefaults; import javax.cache.annotation.CacheKey; import javax.cache.annotation.CachePut; @@ -29,9 +30,9 @@ import org.springframework.cache.Cache; import org.springframework.cache.interceptor.SimpleKeyGenerator; -import org.springframework.cache.jcache.config.JCacheableService; -import org.springframework.cache.jcache.support.TestableCacheKeyGenerator; -import org.springframework.cache.jcache.support.TestableCacheResolverFactory; +import org.springframework.contextsupport.testfixture.cache.TestableCacheKeyGenerator; +import org.springframework.contextsupport.testfixture.cache.TestableCacheResolverFactory; +import org.springframework.contextsupport.testfixture.jcache.JCacheableService; /** * Repository sample with a @CacheDefaults annotation diff --git a/spring-aspects/src/test/java/org/springframework/cache/config/CacheableService.java b/spring-aspects/src/test/java/org/springframework/cache/config/CacheableService.java index a862f2acbf7f..3ec6212bac60 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/config/CacheableService.java +++ b/spring-aspects/src/test/java/org/springframework/cache/config/CacheableService.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,7 +17,11 @@ package org.springframework.cache.config; /** - * Basic service interface. + * Copy of the shared {@code CacheableService}: necessary + * due to issues with Gradle test fixtures and AspectJ configuration + * in the Gradle build. + * + *

Basic service interface for caching tests. * * @author Costin Leau * @author Phillip Webb @@ -33,17 +37,15 @@ public interface CacheableService { T cacheSyncNull(Object arg1); - void invalidate(Object arg1); + void evict(Object arg1, Object arg2); + + void evictWithException(Object arg1); void evictEarly(Object arg1); void evictAll(Object arg1); - void evictWithException(Object arg1); - - void evict(Object arg1, Object arg2); - - void invalidateEarly(Object arg1, Object arg2); + void evictAllEarly(Object arg1); T conditional(int field); @@ -83,7 +85,6 @@ public interface CacheableService { T throwUncheckedSync(Object arg1); - // multi annotations T multiCache(Object arg1); T multiEvict(Object arg1); diff --git a/spring-aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java b/spring-aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java index a76d899b46bd..47a3a83a34a1 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java +++ b/spring-aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -25,7 +25,11 @@ import org.springframework.cache.annotation.Caching; /** - * Simple cacheable service + * Copy of the shared {@code DefaultCacheableService}: necessary + * due to issues with Gradle test fixtures and AspectJ configuration + * in the Gradle build. + * + *

Simple cacheable service. * * @author Costin Leau * @author Phillip Webb @@ -34,12 +38,14 @@ public class DefaultCacheableService implements CacheableService { private final AtomicLong counter = new AtomicLong(); + private final AtomicLong nullInvocations = new AtomicLong(); + @Override @Cacheable("testCache") public Long cache(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @@ -51,7 +57,7 @@ public Long cacheNull(Object arg1) { @Override @Cacheable(cacheNames = "testCache", sync = true) public Long cacheSync(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @@ -61,8 +67,8 @@ public Long cacheSyncNull(Object arg1) { } @Override - @CacheEvict("testCache") - public void invalidate(Object arg1) { + @CacheEvict(cacheNames = "testCache", key = "#p0") + public void evict(Object arg1, Object arg2) { } @Override @@ -71,11 +77,6 @@ public void evictWithException(Object arg1) { throw new RuntimeException("exception thrown - evict should NOT occur"); } - @Override - @CacheEvict(cacheNames = "testCache", allEntries = true) - public void evictAll(Object arg1) { - } - @Override @CacheEvict(cacheNames = "testCache", beforeInvocation = true) public void evictEarly(Object arg1) { @@ -83,26 +84,26 @@ public void evictEarly(Object arg1) { } @Override - @CacheEvict(cacheNames = "testCache", key = "#p0") - public void evict(Object arg1, Object arg2) { + @CacheEvict(cacheNames = "testCache", allEntries = true) + public void evictAll(Object arg1) { } @Override - @CacheEvict(cacheNames = "testCache", key = "#p0", beforeInvocation = true) - public void invalidateEarly(Object arg1, Object arg2) { + @CacheEvict(cacheNames = "testCache", allEntries = true, beforeInvocation = true) + public void evictAllEarly(Object arg1) { throw new RuntimeException("exception thrown - evict should still occur"); } @Override @Cacheable(cacheNames = "testCache", condition = "#p0 == 3") public Long conditional(int classField) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", sync = true, condition = "#p0 == 3") - public Long conditionalSync(int field) { - return counter.getAndIncrement(); + public Long conditionalSync(int classField) { + return this.counter.getAndIncrement(); } @Override @@ -114,55 +115,55 @@ public Long unless(int arg) { @Override @Cacheable(cacheNames = "testCache", key = "#p0") public Long key(Object arg1, Object arg2) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache") public Long varArgsKey(Object... args) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", key = "#root.methodName") public Long name(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target") public Long rootVars(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", keyGenerator = "customKeyGenerator") public Long customKeyGenerator(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", keyGenerator = "unknownBeanName") public Long unknownCustomKeyGenerator(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", cacheManager = "customCacheManager") public Long customCacheManager(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Cacheable(cacheNames = "testCache", cacheManager = "unknownBeanName") public Long unknownCustomCacheManager(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @CachePut("testCache") public Long update(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @@ -174,13 +175,13 @@ public Long conditionalUpdate(Object arg) { @Override @Cacheable("testCache") public Long nullValue(Object arg1) { - nullInvocations.incrementAndGet(); + this.nullInvocations.incrementAndGet(); return null; } @Override public Number nullInvocations() { - return nullInvocations.get(); + return this.nullInvocations.get(); } @Override @@ -212,25 +213,25 @@ public Long throwUncheckedSync(Object arg1) { @Override @Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") }) public Long multiCache(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Caching(evict = { @CacheEvict("primary"), @CacheEvict(cacheNames = "secondary", key = "#p0"), @CacheEvict(cacheNames = "primary", key = "#p0 + 'A'") }) public Long multiEvict(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Caching(cacheable = { @Cacheable(cacheNames = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") }) public Long multiCacheAndEvict(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override @Caching(cacheable = { @Cacheable(cacheNames = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") }) public Long multiConditionalCacheAndEvict(Object arg1) { - return counter.getAndIncrement(); + return this.counter.getAndIncrement(); } @Override diff --git a/spring-aspects/src/test/java/org/springframework/cache/config/TestEntity.java b/spring-aspects/src/test/java/org/springframework/cache/config/TestEntity.java index a2e4c61387a8..b308741ee147 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/config/TestEntity.java +++ b/spring-aspects/src/test/java/org/springframework/cache/config/TestEntity.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,7 +19,11 @@ import org.springframework.util.ObjectUtils; /** - * Simple test entity for use with caching tests. + * Copy of the shared {@code TestEntity}: necessary + * due to issues with Gradle test fixtures and AspectJ configuration + * in the Gradle build. + * + *

Simple test entity for use with caching tests. * * @author Michael Plod */ @@ -53,5 +57,4 @@ public boolean equals(Object obj) { } return false; } - } diff --git a/spring-aspects/src/test/java/org/springframework/context/annotation/aspectj/AnnotationBeanConfigurerTests.java b/spring-aspects/src/test/java/org/springframework/context/annotation/aspectj/AnnotationBeanConfigurerTests.java index 0f66aadcda85..ae781c8fa697 100644 --- a/spring-aspects/src/test/java/org/springframework/context/annotation/aspectj/AnnotationBeanConfigurerTests.java +++ b/spring-aspects/src/test/java/org/springframework/context/annotation/aspectj/AnnotationBeanConfigurerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,14 +16,14 @@ package org.springframework.context.annotation.aspectj; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.aspectj.ShouldBeConfiguredBySpring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests that @EnableSpringConfigured properly registers an @@ -39,7 +39,7 @@ public class AnnotationBeanConfigurerTests { public void injection() { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class)) { ShouldBeConfiguredBySpring myObject = new ShouldBeConfiguredBySpring(); - assertEquals("Rod", myObject.getName()); + assertThat(myObject.getName()).isEqualTo("Rod"); } } diff --git a/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspectTests.java b/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspectTests.java index e119458cd66d..2f7ad0f5b6f0 100644 --- a/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspectTests.java +++ b/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationAsyncExecutionAspectTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,25 +22,23 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.core.testfixture.EnabledForTestGroups; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; import org.springframework.util.ReflectionUtils; import org.springframework.util.concurrent.ListenableFuture; -import static org.hamcrest.CoreMatchers.startsWith; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.TestGroup.PERFORMANCE; /** * Unit tests for {@link AnnotationAsyncExecutionAspect}. @@ -48,6 +46,7 @@ * @author Ramnivas Laddad * @author Stephane Nicoll */ +@EnabledForTestGroups(PERFORMANCE) public class AnnotationAsyncExecutionAspectTests { private static final long WAIT_TIME = 1000; //milliseconds @@ -57,10 +56,8 @@ public class AnnotationAsyncExecutionAspectTests { private CountingExecutor executor; - @Before + @BeforeEach public void setUp() { - Assume.group(TestGroup.PERFORMANCE); - executor = new CountingExecutor(); AnnotationAsyncExecutionAspect.aspectOf().setExecutor(executor); } @@ -71,9 +68,9 @@ public void asyncMethodGetsRoutedAsynchronously() { ClassWithoutAsyncAnnotation obj = new ClassWithoutAsyncAnnotation(); obj.incrementAsync(); executor.waitForCompletion(); - assertEquals(1, obj.counter); - assertEquals(1, executor.submitStartCounter); - assertEquals(1, executor.submitCompleteCounter); + assertThat(obj.counter).isEqualTo(1); + assertThat(executor.submitStartCounter).isEqualTo(1); + assertThat(executor.submitCompleteCounter).isEqualTo(1); } @Test @@ -81,41 +78,39 @@ public void asyncMethodReturningFutureGetsRoutedAsynchronouslyAndReturnsAFuture( ClassWithoutAsyncAnnotation obj = new ClassWithoutAsyncAnnotation(); Future future = obj.incrementReturningAFuture(); // No need to executor.waitForCompletion() as future.get() will have the same effect - assertEquals(5, future.get().intValue()); - assertEquals(1, obj.counter); - assertEquals(1, executor.submitStartCounter); - assertEquals(1, executor.submitCompleteCounter); + assertThat(future.get().intValue()).isEqualTo(5); + assertThat(obj.counter).isEqualTo(1); + assertThat(executor.submitStartCounter).isEqualTo(1); + assertThat(executor.submitCompleteCounter).isEqualTo(1); } @Test public void syncMethodGetsRoutedSynchronously() { ClassWithoutAsyncAnnotation obj = new ClassWithoutAsyncAnnotation(); obj.increment(); - assertEquals(1, obj.counter); - assertEquals(0, executor.submitStartCounter); - assertEquals(0, executor.submitCompleteCounter); + assertThat(obj.counter).isEqualTo(1); + assertThat(executor.submitStartCounter).isEqualTo(0); + assertThat(executor.submitCompleteCounter).isEqualTo(0); } @Test public void voidMethodInAsyncClassGetsRoutedAsynchronously() { - Assume.group(TestGroup.PERFORMANCE); - ClassWithAsyncAnnotation obj = new ClassWithAsyncAnnotation(); obj.increment(); executor.waitForCompletion(); - assertEquals(1, obj.counter); - assertEquals(1, executor.submitStartCounter); - assertEquals(1, executor.submitCompleteCounter); + assertThat(obj.counter).isEqualTo(1); + assertThat(executor.submitStartCounter).isEqualTo(1); + assertThat(executor.submitCompleteCounter).isEqualTo(1); } @Test public void methodReturningFutureInAsyncClassGetsRoutedAsynchronouslyAndReturnsAFuture() throws InterruptedException, ExecutionException { ClassWithAsyncAnnotation obj = new ClassWithAsyncAnnotation(); Future future = obj.incrementReturningAFuture(); - assertEquals(5, future.get().intValue()); - assertEquals(1, obj.counter); - assertEquals(1, executor.submitStartCounter); - assertEquals(1, executor.submitCompleteCounter); + assertThat(future.get().intValue()).isEqualTo(5); + assertThat(obj.counter).isEqualTo(1); + assertThat(executor.submitStartCounter).isEqualTo(1); + assertThat(executor.submitCompleteCounter).isEqualTo(1); } /* @@ -138,14 +133,14 @@ public void qualifiedAsyncMethodsAreRoutedToCorrectExecutor() throws Interrupted ClassWithQualifiedAsyncMethods obj = new ClassWithQualifiedAsyncMethods(); Future defaultThread = obj.defaultWork(); - assertThat(defaultThread.get(), not(Thread.currentThread())); - assertThat(defaultThread.get().getName(), not(startsWith("e1-"))); + assertThat(defaultThread.get()).isNotEqualTo(Thread.currentThread()); + assertThat(defaultThread.get().getName()).doesNotStartWith("e1-"); ListenableFuture e1Thread = obj.e1Work(); - assertThat(e1Thread.get().getName(), startsWith("e1-")); + assertThat(e1Thread.get().getName()).startsWith("e1-"); CompletableFuture e1OtherThread = obj.e1OtherWork(); - assertThat(e1OtherThread.get().getName(), startsWith("e1-")); + assertThat(e1OtherThread.get().getName()).startsWith("e1-"); } @Test @@ -154,7 +149,7 @@ public void exceptionHandlerCalled() { TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(); AnnotationAsyncExecutionAspect.aspectOf().setExceptionHandler(exceptionHandler); try { - assertFalse("Handler should not have been called", exceptionHandler.isCalled()); + assertThat(exceptionHandler.isCalled()).as("Handler should not have been called").isFalse(); ClassWithException obj = new ClassWithException(); obj.failWithVoid(); exceptionHandler.await(3000); @@ -171,20 +166,14 @@ public void exceptionHandlerNeverThrowsUnexpectedException() { TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(true); AnnotationAsyncExecutionAspect.aspectOf().setExceptionHandler(exceptionHandler); try { - assertFalse("Handler should not have been called", exceptionHandler.isCalled()); + assertThat(exceptionHandler.isCalled()).as("Handler should not have been called").isFalse(); ClassWithException obj = new ClassWithException(); - try { - obj.failWithVoid(); - exceptionHandler.await(3000); - exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); - } - catch (Exception ex) { - fail("No unexpected exception should have been received but got " + ex.getMessage()); - } + obj.failWithVoid(); + exceptionHandler.await(3000); + exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); } finally { AnnotationAsyncExecutionAspect.aspectOf().setExceptionHandler(defaultExceptionHandler); - } } @@ -212,7 +201,7 @@ public synchronized void waitForCompletion() { wait(WAIT_TIME); } catch (InterruptedException ex) { - fail("Didn't finish the async job in " + WAIT_TIME + " milliseconds"); + throw new AssertionError("Didn't finish the async job in " + WAIT_TIME + " milliseconds"); } } } diff --git a/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationDrivenBeanDefinitionParserTests.java b/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationDrivenBeanDefinitionParserTests.java index a40a766dee08..20c22f4dbb75 100644 --- a/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationDrivenBeanDefinitionParserTests.java +++ b/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/AnnotationDrivenBeanDefinitionParserTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,16 +18,16 @@ import java.util.function.Supplier; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.DirectFieldAccessor; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.scheduling.config.TaskManagementConfigUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -36,13 +36,13 @@ public class AnnotationDrivenBeanDefinitionParserTests { private ConfigurableApplicationContext context; - @Before + @BeforeEach public void setup() { this.context = new ClassPathXmlApplicationContext( "annotationDrivenContext.xml", AnnotationDrivenBeanDefinitionParserTests.class); } - @After + @AfterEach public void after() { if (this.context != null) { this.context.close(); @@ -51,7 +51,7 @@ public void after() { @Test public void asyncAspectRegistered() { - assertTrue(context.containsBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME)); + assertThat(context.containsBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME)).isTrue(); } @Test @@ -59,7 +59,7 @@ public void asyncAspectRegistered() { public void asyncPostProcessorExecutorReference() { Object executor = context.getBean("testExecutor"); Object aspect = context.getBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME); - assertSame(executor, ((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("defaultExecutor")).get()); + assertThat(((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("defaultExecutor")).get()).isSameAs(executor); } @Test @@ -67,7 +67,7 @@ public void asyncPostProcessorExecutorReference() { public void asyncPostProcessorExceptionHandlerReference() { Object exceptionHandler = context.getBean("testExceptionHandler"); Object aspect = context.getBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME); - assertSame(exceptionHandler, ((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("exceptionHandler")).get()); + assertThat(((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("exceptionHandler")).get()).isSameAs(exceptionHandler); } } diff --git a/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/TestableAsyncUncaughtExceptionHandler.java b/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/TestableAsyncUncaughtExceptionHandler.java index ecd1bc62fbab..616e42996080 100644 --- a/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/TestableAsyncUncaughtExceptionHandler.java +++ b/spring-aspects/src/test/java/org/springframework/scheduling/aspectj/TestableAsyncUncaughtExceptionHandler.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,7 +22,7 @@ import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * A {@link AsyncUncaughtExceptionHandler} implementation used for testing purposes. @@ -60,21 +60,21 @@ public boolean isCalled() { } public void assertCalledWith(Method expectedMethod, Class expectedExceptionType) { - assertNotNull("Handler not called", descriptor); - assertEquals("Wrong exception type", expectedExceptionType, descriptor.ex.getClass()); - assertEquals("Wrong method", expectedMethod, descriptor.method); + assertThat(descriptor).as("Handler not called").isNotNull(); + assertThat(descriptor.ex.getClass()).as("Wrong exception type").isEqualTo(expectedExceptionType); + assertThat(descriptor.method).as("Wrong method").isEqualTo(expectedMethod); } public void await(long timeout) { try { this.latch.await(timeout, TimeUnit.MILLISECONDS); } - catch (Exception e) { + catch (Exception ex) { Thread.currentThread().interrupt(); } } - private static class UncaughtExceptionDescriptor { + private static final class UncaughtExceptionDescriptor { private final Throwable ex; private final Method method; diff --git a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithPrivateAnnotatedMember.java b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithPrivateAnnotatedMember.java index 938265b97c47..7d5b2e6d8376 100644 --- a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithPrivateAnnotatedMember.java +++ b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithPrivateAnnotatedMember.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithProtectedAnnotatedMember.java b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithProtectedAnnotatedMember.java index 07abf06c64d0..359eab233cb0 100644 --- a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithProtectedAnnotatedMember.java +++ b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ClassWithProtectedAnnotatedMember.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ITransactional.java b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ITransactional.java index 44c3ea36b389..e553c94e59fb 100644 --- a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ITransactional.java +++ b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/ITransactional.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/JtaTransactionAspectsTests.java b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/JtaTransactionAspectsTests.java index 4c7b5fcfa7fb..e0bd918a72ea 100644 --- a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/JtaTransactionAspectsTests.java +++ b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/JtaTransactionAspectsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,106 +17,98 @@ package org.springframework.transaction.aspectj; import java.io.IOException; + import javax.transaction.Transactional; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.tests.transaction.CallCountingTransactionManager; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.transaction.testfixture.CallCountingTransactionManager; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIOException; /** * @author Stephane Nicoll */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = JtaTransactionAspectsTests.Config.class) +@SpringJUnitConfig(JtaTransactionAspectsTests.Config.class) public class JtaTransactionAspectsTests { @Autowired private CallCountingTransactionManager txManager; - @Before + @BeforeEach public void setUp() { this.txManager.clear(); } @Test public void commitOnAnnotatedPublicMethod() throws Throwable { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); new JtaAnnotationPublicAnnotatedMember().echo(null); - assertEquals(1, this.txManager.commits); + assertThat(this.txManager.commits).isEqualTo(1); } @Test public void matchingRollbackOnApplied() throws Throwable { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); InterruptedException test = new InterruptedException(); - try { - new JtaAnnotationPublicAnnotatedMember().echo(test); - fail("Should have thrown an exception"); - } - catch (Throwable throwable) { - assertEquals("wrong exception", test, throwable); - } - assertEquals(1, this.txManager.rollbacks); - assertEquals(0, this.txManager.commits); + assertThatExceptionOfType(InterruptedException.class).isThrownBy(() -> + new JtaAnnotationPublicAnnotatedMember().echo(test)) + .isSameAs(test); + assertThat(this.txManager.rollbacks).isEqualTo(1); + assertThat(this.txManager.commits).isEqualTo(0); } @Test public void nonMatchingRollbackOnApplied() throws Throwable { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); IOException test = new IOException(); - try { - new JtaAnnotationPublicAnnotatedMember().echo(test); - fail("Should have thrown an exception"); - } - catch (Throwable throwable) { - assertEquals("wrong exception", test, throwable); - } - assertEquals(1, this.txManager.commits); - assertEquals(0, this.txManager.rollbacks); + assertThatIOException().isThrownBy(() -> + new JtaAnnotationPublicAnnotatedMember().echo(test)) + .isSameAs(test); + assertThat(this.txManager.commits).isEqualTo(1); + assertThat(this.txManager.rollbacks).isEqualTo(0); } @Test public void commitOnAnnotatedProtectedMethod() { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); new JtaAnnotationProtectedAnnotatedMember().doInTransaction(); - assertEquals(1, this.txManager.commits); + assertThat(this.txManager.commits).isEqualTo(1); } @Test public void nonAnnotatedMethodCallingProtectedMethod() { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); new JtaAnnotationProtectedAnnotatedMember().doSomething(); - assertEquals(1, this.txManager.commits); + assertThat(this.txManager.commits).isEqualTo(1); } @Test public void commitOnAnnotatedPrivateMethod() { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); new JtaAnnotationPrivateAnnotatedMember().doInTransaction(); - assertEquals(1, this.txManager.commits); + assertThat(this.txManager.commits).isEqualTo(1); } @Test public void nonAnnotatedMethodCallingPrivateMethod() { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); new JtaAnnotationPrivateAnnotatedMember().doSomething(); - assertEquals(1, this.txManager.commits); + assertThat(this.txManager.commits).isEqualTo(1); } @Test public void notTransactional() { - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); new TransactionAspectTests.NotTransactional().noop(); - assertEquals(0, this.txManager.begun); + assertThat(this.txManager.begun).isEqualTo(0); } diff --git a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/MethodAnnotationOnClassWithNoInterface.java b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/MethodAnnotationOnClassWithNoInterface.java index 3631b05550aa..35067b4ca536 100644 --- a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/MethodAnnotationOnClassWithNoInterface.java +++ b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/MethodAnnotationOnClassWithNoInterface.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionAspectTests.java b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionAspectTests.java index 19678b12f97c..722c357d20e2 100644 --- a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionAspectTests.java +++ b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionAspectTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,12 +16,13 @@ package org.springframework.transaction.aspectj; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.springframework.tests.transaction.CallCountingTransactionManager; +import org.springframework.transaction.testfixture.CallCountingTransactionManager; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rod Johnson @@ -46,7 +47,7 @@ public class TransactionAspectTests { new MethodAnnotationOnClassWithNoInterface(); - @Before + @BeforeEach public void initContext() { AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); } @@ -55,97 +56,81 @@ public void initContext() { @Test public void testCommitOnAnnotatedClass() throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); annotationOnlyOnClassWithNoInterface.echo(null); - assertEquals(1, txManager.commits); + assertThat(txManager.commits).isEqualTo(1); } @Test public void commitOnAnnotatedProtectedMethod() throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); beanWithAnnotatedProtectedMethod.doInTransaction(); - assertEquals(1, txManager.commits); + assertThat(txManager.commits).isEqualTo(1); } @Test public void commitOnAnnotatedPrivateMethod() throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); beanWithAnnotatedPrivateMethod.doSomething(); - assertEquals(1, txManager.commits); + assertThat(txManager.commits).isEqualTo(1); } @Test public void commitOnNonAnnotatedNonPublicMethodInTransactionalType() throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); annotationOnlyOnClassWithNoInterface.nonTransactionalMethod(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); } @Test public void commitOnAnnotatedMethod() throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); methodAnnotationOnly.echo(null); - assertEquals(1, txManager.commits); + assertThat(txManager.commits).isEqualTo(1); } @Test public void notTransactional() throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); new NotTransactional().noop(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); } @Test public void defaultCommitOnAnnotatedClass() throws Throwable { - final Exception ex = new Exception(); - try { - testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), false); - fail("Should have thrown Exception"); - } - catch (Exception ex2) { - assertSame(ex, ex2); - } + Exception ex = new Exception(); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> + testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), false)) + .isSameAs(ex); } @Test public void defaultRollbackOnAnnotatedClass() throws Throwable { - final RuntimeException ex = new RuntimeException(); - try { - testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), true); - fail("Should have thrown RuntimeException"); - } - catch (RuntimeException ex2) { - assertSame(ex, ex2); - } + RuntimeException ex = new RuntimeException(); + assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> + testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), true)) + .isSameAs(ex); } @Test public void defaultCommitOnSubclassOfAnnotatedClass() throws Throwable { - final Exception ex = new Exception(); - try { - testRollback(() -> new SubclassOfClassWithTransactionalAnnotation().echo(ex), false); - fail("Should have thrown Exception"); - } - catch (Exception ex2) { - assertSame(ex, ex2); - } + Exception ex = new Exception(); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> + testRollback(() -> new SubclassOfClassWithTransactionalAnnotation().echo(ex), false)) + .isSameAs(ex); } @Test public void defaultCommitOnSubclassOfClassWithTransactionalMethodAnnotated() throws Throwable { - final Exception ex = new Exception(); - try { - testRollback(() -> new SubclassOfClassWithTransactionalMethodAnnotation().echo(ex), false); - fail("Should have thrown Exception"); - } - catch (Exception ex2) { - assertSame(ex, ex2); - } + Exception ex = new Exception(); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> + testRollback(() -> new SubclassOfClassWithTransactionalMethodAnnotation().echo(ex), false)) + .isSameAs(ex); } @Test @@ -164,32 +149,25 @@ public void noRollbackOnImplementationOfAnnotatedInterface() throws Throwable { protected void testRollback(TransactionOperationCallback toc, boolean rollback) throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); + assertThat(txManager.begun).isEqualTo(0); try { toc.performTransactionalOperation(); } finally { - assertEquals(1, txManager.begun); - assertEquals(rollback ? 0 : 1, txManager.commits); - assertEquals(rollback ? 1 : 0, txManager.rollbacks); + assertThat(txManager.begun).isEqualTo(1); + long expected1 = rollback ? 0 : 1; + assertThat(txManager.commits).isEqualTo(expected1); + long expected = rollback ? 1 : 0; + assertThat(txManager.rollbacks).isEqualTo(expected); } } protected void testNotTransactional(TransactionOperationCallback toc, Throwable expected) throws Throwable { txManager.clear(); - assertEquals(0, txManager.begun); - try { - toc.performTransactionalOperation(); - } - catch (Throwable t) { - if (expected == null) { - fail("Expected " + expected); - } - assertSame(expected, t); - } - finally { - assertEquals(0, txManager.begun); - } + assertThat(txManager.begun).isEqualTo(0); + assertThatExceptionOfType(Throwable.class).isThrownBy( + toc::performTransactionalOperation).isSameAs(expected); + assertThat(txManager.begun).isEqualTo(0); } diff --git a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionalAnnotationOnlyOnClassWithNoInterface.java b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionalAnnotationOnlyOnClassWithNoInterface.java index bca935ce3f59..d9eeb7bbcb74 100644 --- a/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionalAnnotationOnlyOnClassWithNoInterface.java +++ b/spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionalAnnotationOnlyOnClassWithNoInterface.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-aspects/src/test/resources/log4j2-test.xml b/spring-aspects/src/test/resources/log4j2-test.xml index f5e85a2eac86..fb1c94a4807d 100644 --- a/spring-aspects/src/test/resources/log4j2-test.xml +++ b/spring-aspects/src/test/resources/log4j2-test.xml @@ -2,7 +2,7 @@ - + diff --git a/spring-aspects/src/test/resources/org/springframework/aop/aspectj/autoproxy/ajcAutoproxyTests.xml b/spring-aspects/src/test/resources/org/springframework/aop/aspectj/autoproxy/ajcAutoproxyTests.xml index e5f94b89923f..6be707bf51dd 100644 --- a/spring-aspects/src/test/resources/org/springframework/aop/aspectj/autoproxy/ajcAutoproxyTests.xml +++ b/spring-aspects/src/test/resources/org/springframework/aop/aspectj/autoproxy/ajcAutoproxyTests.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> diff --git a/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests-beans.xml b/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests-beans.xml index a850a42bda16..c996fa8b0847 100644 --- a/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests-beans.xml +++ b/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests-beans.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> diff --git a/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests.xml b/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests.xml index b43254c066f2..8d9b96b78d1c 100644 --- a/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests.xml +++ b/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/beanConfigurerTests.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-2.5.xsd"> diff --git a/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/springConfigured.xml b/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/springConfigured.xml index 324f7c3b131e..6e786b230e77 100644 --- a/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/springConfigured.xml +++ b/spring-aspects/src/test/resources/org/springframework/beans/factory/aspectj/springConfigured.xml @@ -3,9 +3,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.5.xsd + http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-2.5.xsd"> diff --git a/spring-aspects/src/test/resources/org/springframework/cache/config/annotation-cache-aspectj.xml b/spring-aspects/src/test/resources/org/springframework/cache/config/annotation-cache-aspectj.xml index 78874860896c..1a283f6f6d83 100644 --- a/spring-aspects/src/test/resources/org/springframework/cache/config/annotation-cache-aspectj.xml +++ b/spring-aspects/src/test/resources/org/springframework/cache/config/annotation-cache-aspectj.xml @@ -3,8 +3,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> + http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsd"> diff --git a/spring-beans/src/main/resources/org/springframework/beans/factory/xml/spring-util.xsd b/spring-beans/src/main/resources/org/springframework/beans/factory/xml/spring-util.xsd index ec64e9e4c96f..533a0c3a8dea 100644 --- a/spring-beans/src/main/resources/org/springframework/beans/factory/xml/spring-util.xsd +++ b/spring-beans/src/main/resources/org/springframework/beans/factory/xml/spring-util.xsd @@ -8,8 +8,8 @@ elementFormDefault="qualified" attributeFormDefault="unqualified"> - - + + diff --git a/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyAccessorTests.java b/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyAccessorTests.java index da802f68c9d8..c30a9139e656 100644 --- a/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyAccessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyAccessorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -36,35 +36,33 @@ import java.util.TreeSet; import org.apache.commons.logging.LogFactory; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowire; import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.StringArrayPropertyEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.support.DerivedFromProtectedBaseBean; +import org.springframework.beans.testfixture.beans.BooleanTestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.IndexedTestBean; +import org.springframework.beans.testfixture.beans.NumberTestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.GenericConversionService; +import org.springframework.core.testfixture.Assume; +import org.springframework.core.testfixture.EnabledForTestGroups; import org.springframework.lang.Nullable; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; -import org.springframework.tests.sample.beans.BooleanTestBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.IndexedTestBean; -import org.springframework.tests.sample.beans.NumberTestBean; -import org.springframework.tests.sample.beans.TestBean; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.within; +import static org.springframework.core.testfixture.TestGroup.PERFORMANCE; /** * Shared tests for property accessors. @@ -79,36 +77,26 @@ */ public abstract class AbstractPropertyAccessorTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - - protected abstract AbstractPropertyAccessor createAccessor(Object target); @Test public void createWithNullTarget() { - try { - createAccessor(null); - fail("Must throw an exception when constructed with null object"); - } - catch (IllegalArgumentException ex) { - // expected - } + assertThatIllegalArgumentException().isThrownBy(() -> createAccessor(null)); } @Test public void isReadableProperty() { AbstractPropertyAccessor accessor = createAccessor(new Simple("John", 2)); - assertThat(accessor.isReadableProperty("name"), is(true)); + assertThat(accessor.isReadableProperty("name")).isTrue(); } @Test public void isReadablePropertyNotReadable() { AbstractPropertyAccessor accessor = createAccessor(new NoRead()); - assertFalse(accessor.isReadableProperty("age")); + assertThat(accessor.isReadableProperty("age")).isFalse(); } /** @@ -118,37 +106,35 @@ public void isReadablePropertyNotReadable() { public void isReadablePropertyNoSuchProperty() { AbstractPropertyAccessor accessor = createAccessor(new NoRead()); - assertFalse(accessor.isReadableProperty("xxxxx")); + assertThat(accessor.isReadableProperty("xxxxx")).isFalse(); } @Test public void isReadablePropertyNull() { AbstractPropertyAccessor accessor = createAccessor(new NoRead()); - thrown.expect(IllegalArgumentException.class); - accessor.isReadableProperty(null); + assertThatIllegalArgumentException().isThrownBy(() -> accessor.isReadableProperty(null)); } @Test public void isWritableProperty() { AbstractPropertyAccessor accessor = createAccessor(new Simple("John", 2)); - assertThat(accessor.isWritableProperty("name"), is(true)); + assertThat(accessor.isWritableProperty("name")).isTrue(); } @Test public void isWritablePropertyNull() { AbstractPropertyAccessor accessor = createAccessor(new NoRead()); - thrown.expect(IllegalArgumentException.class); - accessor.isWritableProperty(null); + assertThatIllegalArgumentException().isThrownBy(() -> accessor.isWritableProperty(null)); } @Test public void isWritablePropertyNoSuchProperty() { AbstractPropertyAccessor accessor = createAccessor(new NoRead()); - assertFalse(accessor.isWritableProperty("xxxxx")); + assertThat(accessor.isWritableProperty("xxxxx")).isFalse(); } @Test @@ -156,66 +142,66 @@ public void isReadableWritableForIndexedProperties() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); - assertTrue(accessor.isReadableProperty("array")); - assertTrue(accessor.isReadableProperty("list")); - assertTrue(accessor.isReadableProperty("set")); - assertTrue(accessor.isReadableProperty("map")); - assertFalse(accessor.isReadableProperty("xxx")); - - assertTrue(accessor.isWritableProperty("array")); - assertTrue(accessor.isWritableProperty("list")); - assertTrue(accessor.isWritableProperty("set")); - assertTrue(accessor.isWritableProperty("map")); - assertFalse(accessor.isWritableProperty("xxx")); - - assertTrue(accessor.isReadableProperty("array[0]")); - assertTrue(accessor.isReadableProperty("array[0].name")); - assertTrue(accessor.isReadableProperty("list[0]")); - assertTrue(accessor.isReadableProperty("list[0].name")); - assertTrue(accessor.isReadableProperty("set[0]")); - assertTrue(accessor.isReadableProperty("set[0].name")); - assertTrue(accessor.isReadableProperty("map[key1]")); - assertTrue(accessor.isReadableProperty("map[key1].name")); - assertTrue(accessor.isReadableProperty("map[key4][0]")); - assertTrue(accessor.isReadableProperty("map[key4][0].name")); - assertTrue(accessor.isReadableProperty("map[key4][1]")); - assertTrue(accessor.isReadableProperty("map[key4][1].name")); - assertFalse(accessor.isReadableProperty("array[key1]")); - - assertTrue(accessor.isWritableProperty("array[0]")); - assertTrue(accessor.isWritableProperty("array[0].name")); - assertTrue(accessor.isWritableProperty("list[0]")); - assertTrue(accessor.isWritableProperty("list[0].name")); - assertTrue(accessor.isWritableProperty("set[0]")); - assertTrue(accessor.isWritableProperty("set[0].name")); - assertTrue(accessor.isWritableProperty("map[key1]")); - assertTrue(accessor.isWritableProperty("map[key1].name")); - assertTrue(accessor.isWritableProperty("map[key4][0]")); - assertTrue(accessor.isWritableProperty("map[key4][0].name")); - assertTrue(accessor.isWritableProperty("map[key4][1]")); - assertTrue(accessor.isWritableProperty("map[key4][1].name")); - assertFalse(accessor.isWritableProperty("array[key1]")); + assertThat(accessor.isReadableProperty("array")).isTrue(); + assertThat(accessor.isReadableProperty("list")).isTrue(); + assertThat(accessor.isReadableProperty("set")).isTrue(); + assertThat(accessor.isReadableProperty("map")).isTrue(); + assertThat(accessor.isReadableProperty("xxx")).isFalse(); + + assertThat(accessor.isWritableProperty("array")).isTrue(); + assertThat(accessor.isWritableProperty("list")).isTrue(); + assertThat(accessor.isWritableProperty("set")).isTrue(); + assertThat(accessor.isWritableProperty("map")).isTrue(); + assertThat(accessor.isWritableProperty("xxx")).isFalse(); + + assertThat(accessor.isReadableProperty("array[0]")).isTrue(); + assertThat(accessor.isReadableProperty("array[0].name")).isTrue(); + assertThat(accessor.isReadableProperty("list[0]")).isTrue(); + assertThat(accessor.isReadableProperty("list[0].name")).isTrue(); + assertThat(accessor.isReadableProperty("set[0]")).isTrue(); + assertThat(accessor.isReadableProperty("set[0].name")).isTrue(); + assertThat(accessor.isReadableProperty("map[key1]")).isTrue(); + assertThat(accessor.isReadableProperty("map[key1].name")).isTrue(); + assertThat(accessor.isReadableProperty("map[key4][0]")).isTrue(); + assertThat(accessor.isReadableProperty("map[key4][0].name")).isTrue(); + assertThat(accessor.isReadableProperty("map[key4][1]")).isTrue(); + assertThat(accessor.isReadableProperty("map[key4][1].name")).isTrue(); + assertThat(accessor.isReadableProperty("array[key1]")).isFalse(); + + assertThat(accessor.isWritableProperty("array[0]")).isTrue(); + assertThat(accessor.isWritableProperty("array[0].name")).isTrue(); + assertThat(accessor.isWritableProperty("list[0]")).isTrue(); + assertThat(accessor.isWritableProperty("list[0].name")).isTrue(); + assertThat(accessor.isWritableProperty("set[0]")).isTrue(); + assertThat(accessor.isWritableProperty("set[0].name")).isTrue(); + assertThat(accessor.isWritableProperty("map[key1]")).isTrue(); + assertThat(accessor.isWritableProperty("map[key1].name")).isTrue(); + assertThat(accessor.isWritableProperty("map[key4][0]")).isTrue(); + assertThat(accessor.isWritableProperty("map[key4][0].name")).isTrue(); + assertThat(accessor.isWritableProperty("map[key4][1]")).isTrue(); + assertThat(accessor.isWritableProperty("map[key4][1].name")).isTrue(); + assertThat(accessor.isWritableProperty("array[key1]")).isFalse(); } @Test public void getSimpleProperty() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); - assertThat(accessor.getPropertyValue("name"), is("John")); + assertThat(accessor.getPropertyValue("name")).isEqualTo("John"); } @Test public void getNestedProperty() { Person target = createPerson("John", "London", "UK"); AbstractPropertyAccessor accessor = createAccessor(target); - assertThat(accessor.getPropertyValue("address.city"), is("London")); + assertThat(accessor.getPropertyValue("address.city")).isEqualTo("London"); } @Test public void getNestedDeepProperty() { Person target = createPerson("John", "London", "UK"); AbstractPropertyAccessor accessor = createAccessor(target); - assertThat(accessor.getPropertyValue("address.country.name"), is("UK")); + assertThat(accessor.getPropertyValue("address.country.name")).isEqualTo("UK"); } @Test @@ -226,11 +212,11 @@ public void getAnotherNestedDeepProperty() { kerry.setSpouse(target); AbstractPropertyAccessor accessor = createAccessor(target); Integer KA = (Integer) accessor.getPropertyValue("spouse.age"); - assertTrue("kerry is 35", KA == 35); + assertThat(KA == 35).as("kerry is 35").isTrue(); Integer RA = (Integer) accessor.getPropertyValue("spouse.spouse.age"); - assertTrue("rod is 31, not" + RA, RA == 31); + assertThat(RA == 31).as("rod is 31, not" + RA).isTrue(); ITestBean spousesSpouse = (ITestBean) accessor.getPropertyValue("spouse.spouse"); - assertTrue("spousesSpouse = initial point", target == spousesSpouse); + assertThat(target == spousesSpouse).as("spousesSpouse = initial point").isTrue(); } @Test @@ -238,15 +224,12 @@ public void getPropertyIntermediatePropertyIsNull() { Person target = createPerson("John", "London", "UK"); target.address = null; AbstractPropertyAccessor accessor = createAccessor(target); - - try { - accessor.getPropertyValue("address.country.name"); - fail("Should have failed to get value with null intermediate path"); - } - catch (NullValueInNestedPathException e) { - assertEquals("address", e.getPropertyName()); - assertEquals(Person.class, e.getBeanClass()); - } + assertThatExceptionOfType(NullValueInNestedPathException.class).isThrownBy(() -> + accessor.getPropertyValue("address.country.name")) + .satisfies(ex -> { + assertThat(ex.getPropertyName()).isEqualTo("address"); + assertThat(ex.getBeanClass()).isEqualTo(Person.class); + }); } @Test @@ -256,7 +239,7 @@ public void getPropertyIntermediatePropertyIsNullWithAutoGrow() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setAutoGrowNestedPaths(true); - assertEquals("DefaultCountry", accessor.getPropertyValue("address.country.name")); + assertThat(accessor.getPropertyValue("address.country.name")).isEqualTo("DefaultCountry"); } @Test @@ -266,22 +249,19 @@ public void getPropertyIntermediateMapEntryIsNullWithAutoGrow() { accessor.setConversionService(new DefaultConversionService()); accessor.setAutoGrowNestedPaths(true); accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9"); - assertEquals("9", target.listOfMaps.get(0).get("luckyNumber")); + assertThat(target.listOfMaps.get(0).get("luckyNumber")).isEqualTo("9"); } @Test public void getUnknownProperty() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); - - try { - accessor.getPropertyValue("foo"); - fail("Should have failed to get an unknown property."); - } - catch (NotReadablePropertyException e) { - assertEquals(Simple.class, e.getBeanClass()); - assertEquals("foo", e.getPropertyName()); - } + assertThatExceptionOfType(NotReadablePropertyException.class).isThrownBy(() -> + accessor.getPropertyValue("foo")) + .satisfies(ex -> { + assertThat(ex.getBeanClass()).isEqualTo(Simple.class); + assertThat(ex.getPropertyName()).isEqualTo("foo"); + }); } @Test @@ -289,8 +269,8 @@ public void getUnknownNestedProperty() { Person target = createPerson("John", "London", "UK"); AbstractPropertyAccessor accessor = createAccessor(target); - thrown.expect(NotReadablePropertyException.class); - accessor.getPropertyValue("address.bar"); + assertThatExceptionOfType(NotReadablePropertyException.class).isThrownBy(() -> + accessor.getPropertyValue("address.bar")); } @Test @@ -300,8 +280,8 @@ public void setSimpleProperty() { accessor.setPropertyValue("name", "SomeValue"); - assertThat(target.name, is("SomeValue")); - assertThat(target.getName(), is("SomeValue")); + assertThat(target.name).isEqualTo("SomeValue"); + assertThat(target.getName()).isEqualTo("SomeValue"); } @Test @@ -310,7 +290,7 @@ public void setNestedProperty() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("address.city", "London"); - assertThat(target.address.city, is("London")); + assertThat(target.address.city).isEqualTo("London"); } @Test @@ -320,19 +300,18 @@ public void setNestedPropertyPolymorphic() throws Exception { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("spouse", kerry); - accessor.setPropertyValue("spouse.age", new Integer(35)); + accessor.setPropertyValue("spouse.age", 35); accessor.setPropertyValue("spouse.name", "Kerry"); accessor.setPropertyValue("spouse.company", "Lewisham"); - assertTrue("kerry name is Kerry", kerry.getName().equals("Kerry")); + assertThat(kerry.getName().equals("Kerry")).as("kerry name is Kerry").isTrue(); - assertTrue("nested set worked", target.getSpouse() == kerry); - assertTrue("no back relation", kerry.getSpouse() == null); + assertThat(target.getSpouse() == kerry).as("nested set worked").isTrue(); + assertThat(kerry.getSpouse() == null).as("no back relation").isTrue(); accessor.setPropertyValue(new PropertyValue("spouse.spouse", target)); - assertTrue("nested set worked", kerry.getSpouse() == target); + assertThat(kerry.getSpouse() == target).as("nested set worked").isTrue(); AbstractPropertyAccessor kerryAccessor = createAccessor(kerry); - assertTrue("spouse.spouse.spouse.spouse.company=Lewisham", - "Lewisham".equals(kerryAccessor.getPropertyValue("spouse.spouse.spouse.spouse.company"))); + assertThat("Lewisham".equals(kerryAccessor.getPropertyValue("spouse.spouse.spouse.spouse.company"))).as("spouse.spouse.spouse.spouse.company=Lewisham").isTrue(); } @Test @@ -343,16 +322,16 @@ public void setAnotherNestedProperty() throws Exception { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("spouse", kerry); - assertTrue("nested set worked", target.getSpouse() == kerry); - assertTrue("no back relation", kerry.getSpouse() == null); + assertThat(target.getSpouse() == kerry).as("nested set worked").isTrue(); + assertThat(kerry.getSpouse() == null).as("no back relation").isTrue(); accessor.setPropertyValue(new PropertyValue("spouse.spouse", target)); - assertTrue("nested set worked", kerry.getSpouse() == target); - assertTrue("kerry age not set", kerry.getAge() == 0); + assertThat(kerry.getSpouse() == target).as("nested set worked").isTrue(); + assertThat(kerry.getAge() == 0).as("kerry age not set").isTrue(); accessor.setPropertyValue(new PropertyValue("spouse.age", 35)); - assertTrue("Set primitive on spouse", kerry.getAge() == 35); + assertThat(kerry.getAge() == 35).as("Set primitive on spouse").isTrue(); - assertEquals(kerry, accessor.getPropertyValue("spouse")); - assertEquals(target, accessor.getPropertyValue("spouse.spouse")); + assertThat(accessor.getPropertyValue("spouse")).isEqualTo(kerry); + assertThat(accessor.getPropertyValue("spouse.spouse")).isEqualTo(target); } @Test @@ -363,8 +342,8 @@ public void setYetAnotherNestedProperties() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("doctor.company", doctorCompany); accessor.setPropertyValue("lawyer.company", lawyerCompany); - assertEquals(doctorCompany, target.getDoctor().getCompany()); - assertEquals(lawyerCompany, target.getLawyer().getCompany()); + assertThat(target.getDoctor().getCompany()).isEqualTo(doctorCompany); + assertThat(target.getLawyer().getCompany()).isEqualTo(lawyerCompany); } @Test @@ -373,7 +352,7 @@ public void setNestedDeepProperty() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("address.country.name", "UK"); - assertThat(target.address.country.name, is("UK")); + assertThat(target.address.country.name).isEqualTo("UK"); } @Test @@ -387,7 +366,7 @@ public void testErrorMessageOfNestedProperty() { accessor.getPropertyValue("spouse.bla"); } catch (NotReadablePropertyException ex) { - assertTrue(ex.getMessage().contains(TestBean.class.getName())); + assertThat(ex.getMessage().contains(TestBean.class.getName())).isTrue(); } } @@ -396,31 +375,22 @@ public void setPropertyIntermediatePropertyIsNull() { Person target = createPerson("John", "Paris", "FR"); target.address.country = null; AbstractPropertyAccessor accessor = createAccessor(target); - - try { - accessor.setPropertyValue("address.country.name", "UK"); - fail("Should have failed to set value with intermediate null value"); - } - catch (NullValueInNestedPathException e) { - assertEquals("address.country", e.getPropertyName()); - assertEquals(Person.class, e.getBeanClass()); - } - assertThat(target.address.country, is(nullValue())); // Not touched + assertThatExceptionOfType(NullValueInNestedPathException.class).isThrownBy(() -> + accessor.setPropertyValue("address.country.name", "UK")) + .satisfies(ex -> { + assertThat(ex.getPropertyName()).isEqualTo("address.country"); + assertThat(ex.getBeanClass()).isEqualTo(Person.class); + }); + assertThat(target.address.country).isNull(); // Not touched } @Test public void setAnotherPropertyIntermediatePropertyIsNull() throws Exception { ITestBean target = new TestBean("rod", 31); AbstractPropertyAccessor accessor = createAccessor(target); - try { - accessor.setPropertyValue("spouse.age", new Integer(31)); - fail("Shouldn't have succeeded with null path"); - } - catch (NullValueInNestedPathException ex) { - // expected - assertTrue("it was the spouse property that was null, not " + ex.getPropertyName(), - ex.getPropertyName().equals("spouse")); - } + assertThatExceptionOfType(NullValueInNestedPathException.class).isThrownBy(() -> + accessor.setPropertyValue("spouse.age", 31)) + .satisfies(ex -> assertThat(ex.getPropertyName()).isEqualTo("spouse")); } @Test @@ -431,10 +401,9 @@ public void setPropertyIntermediatePropertyIsNullWithAutoGrow() { accessor.setAutoGrowNestedPaths(true); accessor.setPropertyValue("address.country.name", "UK"); - assertThat(target.address.country.name, is("UK")); + assertThat(target.address.country.name).isEqualTo("UK"); } - @SuppressWarnings("AssertEqualsBetweenInconvertibleTypes") @Test public void setPropertyIntermediateListIsNullWithAutoGrow() { Foo target = new Foo(); @@ -444,7 +413,7 @@ public void setPropertyIntermediateListIsNullWithAutoGrow() { Map map = new HashMap<>(); map.put("favoriteNumber", "9"); accessor.setPropertyValue("list[0]", map); - assertEquals(map, target.list.get(0)); + assertThat(target.list.get(0)).isEqualTo(map); } @Test @@ -453,7 +422,7 @@ public void setPropertyIntermediateListIsNullWithNoConversionService() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setAutoGrowNestedPaths(true); accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9"); - assertEquals("9", target.listOfMaps.get(0).get("luckyNumber")); + assertThat(target.listOfMaps.get(0).get("luckyNumber")).isEqualTo("9"); } @Test @@ -468,7 +437,7 @@ public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceTy }); accessor.setAutoGrowNestedPaths(true); accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9"); - assertEquals("9", target.listOfMaps.get(0).get("luckyNumber")); + assertThat(target.listOfMaps.get(0).get("luckyNumber")).isEqualTo("9"); } @@ -479,18 +448,13 @@ public void setEmptyPropertyValues() { String name = "Tony"; target.setAge(age); target.setName(name); - try { - AbstractPropertyAccessor accessor = createAccessor(target); - assertTrue("age is OK", target.getAge() == age); - assertTrue("name is OK", name.equals(target.getName())); - accessor.setPropertyValues(new MutablePropertyValues()); - // Check its unchanged - assertTrue("age is OK", target.getAge() == age); - assertTrue("name is OK", name.equals(target.getName())); - } - catch (BeansException ex) { - fail("Shouldn't throw exception when everything is valid"); - } + AbstractPropertyAccessor accessor = createAccessor(target); + assertThat(target.getAge() == age).as("age is OK").isTrue(); + assertThat(name.equals(target.getName())).as("name is OK").isTrue(); + accessor.setPropertyValues(new MutablePropertyValues()); + // Check its unchanged + assertThat(target.getAge() == age).as("age is OK").isTrue(); + assertThat(name.equals(target.getName())).as("name is OK").isTrue(); } @@ -500,20 +464,15 @@ public void setValidPropertyValues() { String newName = "tony"; int newAge = 65; String newTouchy = "valid"; - try { - AbstractPropertyAccessor accessor = createAccessor(target); - MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.addPropertyValue(new PropertyValue("age", newAge)); - pvs.addPropertyValue(new PropertyValue("name", newName)); - pvs.addPropertyValue(new PropertyValue("touchy", newTouchy)); - accessor.setPropertyValues(pvs); - assertTrue("Name property should have changed", target.getName().equals(newName)); - assertTrue("Touchy property should have changed", target.getTouchy().equals(newTouchy)); - assertTrue("Age property should have changed", target.getAge() == newAge); - } - catch (BeansException ex) { - fail("Shouldn't throw exception when everything is valid"); - } + AbstractPropertyAccessor accessor = createAccessor(target); + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.addPropertyValue(new PropertyValue("age", newAge)); + pvs.addPropertyValue(new PropertyValue("name", newName)); + pvs.addPropertyValue(new PropertyValue("touchy", newTouchy)); + accessor.setPropertyValues(pvs); + assertThat(target.getName().equals(newName)).as("Name property should have changed").isTrue(); + assertThat(target.getTouchy().equals(newTouchy)).as("Touchy property should have changed").isTrue(); + assertThat(target.getAge() == newAge).as("Age property should have changed").isTrue(); } @Test @@ -522,34 +481,24 @@ public void setIndividualValidPropertyValues() { String newName = "tony"; int newAge = 65; String newTouchy = "valid"; - try { - AbstractPropertyAccessor accessor = createAccessor(target); - accessor.setPropertyValue("age", new Integer(newAge)); - accessor.setPropertyValue(new PropertyValue("name", newName)); - accessor.setPropertyValue(new PropertyValue("touchy", newTouchy)); - assertTrue("Name property should have changed", target.getName().equals(newName)); - assertTrue("Touchy property should have changed", target.getTouchy().equals(newTouchy)); - assertTrue("Age property should have changed", target.getAge() == newAge); - } - catch (BeansException ex) { - fail("Shouldn't throw exception when everything is valid"); - } + AbstractPropertyAccessor accessor = createAccessor(target); + accessor.setPropertyValue("age", newAge); + accessor.setPropertyValue(new PropertyValue("name", newName)); + accessor.setPropertyValue(new PropertyValue("touchy", newTouchy)); + assertThat(target.getName().equals(newName)).as("Name property should have changed").isTrue(); + assertThat(target.getTouchy().equals(newTouchy)).as("Touchy property should have changed").isTrue(); + assertThat(target.getAge() == newAge).as("Age property should have changed").isTrue(); } @Test public void setPropertyIsReflectedImmediately() { TestBean target = new TestBean(); int newAge = 33; - try { - AbstractPropertyAccessor accessor = createAccessor(target); - target.setAge(newAge); - Object bwAge = accessor.getPropertyValue("age"); - assertTrue("Age is an integer", bwAge instanceof Integer); - assertTrue("Bean wrapper must pick up changes", (int) bwAge == newAge); - } - catch (Exception ex) { - fail("Shouldn't throw exception when everything is valid"); - } + AbstractPropertyAccessor accessor = createAccessor(target); + target.setAge(newAge); + Object bwAge = accessor.getPropertyValue("age"); + assertThat(bwAge instanceof Integer).as("Age is an integer").isTrue(); + assertThat(bwAge).as("Bean wrapper must pick up changes").isEqualTo(newAge); } @Test @@ -558,16 +507,15 @@ public void setPropertyToNull() { target.setName("Frank"); // we need to change it back target.setSpouse(target); AbstractPropertyAccessor accessor = createAccessor(target); - assertTrue("name is not null to start off", target.getName() != null); + assertThat(target.getName() != null).as("name is not null to start off").isTrue(); accessor.setPropertyValue("name", null); - assertTrue("name is now null", target.getName() == null); + assertThat(target.getName() == null).as("name is now null").isTrue(); // now test with non-string - assertTrue("spouse is not null to start off", target.getSpouse() != null); + assertThat(target.getSpouse() != null).as("spouse is not null to start off").isTrue(); accessor.setPropertyValue("spouse", null); - assertTrue("spouse is now null", target.getSpouse() == null); + assertThat(target.getSpouse() == null).as("spouse is now null").isTrue(); } - @Test public void setIndexedPropertyIgnored() { MutablePropertyValues values = new MutablePropertyValues(); @@ -583,7 +531,7 @@ public void setPropertyWithPrimitiveConversion() { TestBean target = new TestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValues(values); - assertEquals("42", target.getName()); + assertThat(target.getName()).isEqualTo("42"); } @Test @@ -599,7 +547,7 @@ public void setValue(Object value) { } }); accessor.setPropertyValues(values); - assertEquals(Integer.class.toString(), target.getName()); + assertThat(target.getName()).isEqualTo(Integer.class.toString()); } @Test @@ -618,11 +566,11 @@ public void setValue(Object value) { } }); accessor.setPropertyValue("name", new String[] {}); - assertEquals("", target.getName()); + assertThat(target.getName()).isEqualTo(""); accessor.setPropertyValue("name", new String[] {"a1", "b2"}); - assertEquals("a1-b2", target.getName()); + assertThat(target.getName()).isEqualTo("a1-b2"); accessor.setPropertyValue("name", null); - assertEquals("", target.getName()); + assertThat(target.getName()).isEqualTo(""); } @Test @@ -631,80 +579,66 @@ public void setBooleanProperty() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("bool2", "true"); - assertTrue("Correct bool2 value", Boolean.TRUE.equals(accessor.getPropertyValue("bool2"))); - assertTrue("Correct bool2 value", target.getBool2()); + assertThat(Boolean.TRUE.equals(accessor.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue(); + assertThat(target.getBool2()).as("Correct bool2 value").isTrue(); accessor.setPropertyValue("bool2", "false"); - assertTrue("Correct bool2 value", Boolean.FALSE.equals(accessor.getPropertyValue("bool2"))); - assertTrue("Correct bool2 value", !target.getBool2()); + assertThat(Boolean.FALSE.equals(accessor.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue(); + assertThat(!target.getBool2()).as("Correct bool2 value").isTrue(); } @Test public void setNumberProperties() { NumberTestBean target = new NumberTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); - - try { - accessor.setPropertyValue("short2", "2"); - accessor.setPropertyValue("int2", "8"); - accessor.setPropertyValue("long2", "6"); - accessor.setPropertyValue("bigInteger", "3"); - accessor.setPropertyValue("float2", "8.1"); - accessor.setPropertyValue("double2", "6.1"); - accessor.setPropertyValue("bigDecimal", "4.0"); - } - catch (BeansException ex) { - fail("Should not throw BeansException: " + ex.getMessage()); - } - - assertTrue("Correct short2 value", new Short("2").equals(accessor.getPropertyValue("short2"))); - assertTrue("Correct short2 value", new Short("2").equals(target.getShort2())); - assertTrue("Correct int2 value", new Integer("8").equals(accessor.getPropertyValue("int2"))); - assertTrue("Correct int2 value", new Integer("8").equals(target.getInt2())); - assertTrue("Correct long2 value", new Long("6").equals(accessor.getPropertyValue("long2"))); - assertTrue("Correct long2 value", new Long("6").equals(target.getLong2())); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(accessor.getPropertyValue("bigInteger"))); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(target.getBigInteger())); - assertTrue("Correct float2 value", new Float("8.1").equals(accessor.getPropertyValue("float2"))); - assertTrue("Correct float2 value", new Float("8.1").equals(target.getFloat2())); - assertTrue("Correct double2 value", new Double("6.1").equals(accessor.getPropertyValue("double2"))); - assertTrue("Correct double2 value", new Double("6.1").equals(target.getDouble2())); - assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(accessor.getPropertyValue("bigDecimal"))); - assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(target.getBigDecimal())); + accessor.setPropertyValue("short2", "2"); + accessor.setPropertyValue("int2", "8"); + accessor.setPropertyValue("long2", "6"); + accessor.setPropertyValue("bigInteger", "3"); + accessor.setPropertyValue("float2", "8.1"); + accessor.setPropertyValue("double2", "6.1"); + accessor.setPropertyValue("bigDecimal", "4.0"); + assertThat(new Short("2").equals(accessor.getPropertyValue("short2"))).as("Correct short2 value").isTrue(); + assertThat(new Short("2").equals(target.getShort2())).as("Correct short2 value").isTrue(); + assertThat(new Integer("8").equals(accessor.getPropertyValue("int2"))).as("Correct int2 value").isTrue(); + assertThat(new Integer("8").equals(target.getInt2())).as("Correct int2 value").isTrue(); + assertThat(new Long("6").equals(accessor.getPropertyValue("long2"))).as("Correct long2 value").isTrue(); + assertThat(new Long("6").equals(target.getLong2())).as("Correct long2 value").isTrue(); + assertThat(new BigInteger("3").equals(accessor.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue(); + assertThat(new BigInteger("3").equals(target.getBigInteger())).as("Correct bigInteger value").isTrue(); + assertThat(new Float("8.1").equals(accessor.getPropertyValue("float2"))).as("Correct float2 value").isTrue(); + assertThat(new Float("8.1").equals(target.getFloat2())).as("Correct float2 value").isTrue(); + assertThat(new Double("6.1").equals(accessor.getPropertyValue("double2"))).as("Correct double2 value").isTrue(); + assertThat(new Double("6.1").equals(target.getDouble2())).as("Correct double2 value").isTrue(); + assertThat(new BigDecimal("4.0").equals(accessor.getPropertyValue("bigDecimal"))).as("Correct bigDecimal value").isTrue(); + assertThat(new BigDecimal("4.0").equals(target.getBigDecimal())).as("Correct bigDecimal value").isTrue(); } @Test public void setNumberPropertiesWithCoercion() { NumberTestBean target = new NumberTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); - - try { - accessor.setPropertyValue("short2", new Integer(2)); - accessor.setPropertyValue("int2", new Long(8)); - accessor.setPropertyValue("long2", new BigInteger("6")); - accessor.setPropertyValue("bigInteger", new Integer(3)); - accessor.setPropertyValue("float2", new Double(8.1)); - accessor.setPropertyValue("double2", new BigDecimal(6.1)); - accessor.setPropertyValue("bigDecimal", new Float(4.0)); - } - catch (BeansException ex) { - fail("Should not throw BeansException: " + ex.getMessage()); - } - - assertTrue("Correct short2 value", new Short("2").equals(accessor.getPropertyValue("short2"))); - assertTrue("Correct short2 value", new Short("2").equals(target.getShort2())); - assertTrue("Correct int2 value", new Integer("8").equals(accessor.getPropertyValue("int2"))); - assertTrue("Correct int2 value", new Integer("8").equals(target.getInt2())); - assertTrue("Correct long2 value", new Long("6").equals(accessor.getPropertyValue("long2"))); - assertTrue("Correct long2 value", new Long("6").equals(target.getLong2())); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(accessor.getPropertyValue("bigInteger"))); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(target.getBigInteger())); - assertTrue("Correct float2 value", new Float("8.1").equals(accessor.getPropertyValue("float2"))); - assertTrue("Correct float2 value", new Float("8.1").equals(target.getFloat2())); - assertTrue("Correct double2 value", new Double("6.1").equals(accessor.getPropertyValue("double2"))); - assertTrue("Correct double2 value", new Double("6.1").equals(target.getDouble2())); - assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(accessor.getPropertyValue("bigDecimal"))); - assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(target.getBigDecimal())); + accessor.setPropertyValue("short2", 2); + accessor.setPropertyValue("int2", 8L); + accessor.setPropertyValue("long2", new BigInteger("6")); + accessor.setPropertyValue("bigInteger", 3L); + accessor.setPropertyValue("float2", 8.1D); + accessor.setPropertyValue("double2", new BigDecimal(6.1)); + accessor.setPropertyValue("bigDecimal", 4.0F); + assertThat(new Short("2").equals(accessor.getPropertyValue("short2"))).as("Correct short2 value").isTrue(); + assertThat(new Short("2").equals(target.getShort2())).as("Correct short2 value").isTrue(); + assertThat(new Integer("8").equals(accessor.getPropertyValue("int2"))).as("Correct int2 value").isTrue(); + assertThat(new Integer("8").equals(target.getInt2())).as("Correct int2 value").isTrue(); + assertThat(new Long("6").equals(accessor.getPropertyValue("long2"))).as("Correct long2 value").isTrue(); + assertThat(new Long("6").equals(target.getLong2())).as("Correct long2 value").isTrue(); + assertThat(new BigInteger("3").equals(accessor.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue(); + assertThat(new BigInteger("3").equals(target.getBigInteger())).as("Correct bigInteger value").isTrue(); + assertThat(new Float("8.1").equals(accessor.getPropertyValue("float2"))).as("Correct float2 value").isTrue(); + assertThat(new Float("8.1").equals(target.getFloat2())).as("Correct float2 value").isTrue(); + assertThat(new Double("6.1").equals(accessor.getPropertyValue("double2"))).as("Correct double2 value").isTrue(); + assertThat(new Double("6.1").equals(target.getDouble2())).as("Correct double2 value").isTrue(); + assertThat(new BigDecimal("4.0").equals(accessor.getPropertyValue("bigDecimal"))).as("Correct bigDecimal value").isTrue(); + assertThat(new BigDecimal("4.0").equals(target.getBigDecimal())).as("Correct bigDecimal value").isTrue(); } @Test @@ -737,24 +671,23 @@ public void setPrimitiveProperties() { accessor.setPropertyValue("myPrimitiveDouble", doubleValue); accessor.setPropertyValue("myDouble", doubleValue); - assertEquals(Byte.MAX_VALUE, target.getMyPrimitiveByte()); - assertEquals(Byte.MAX_VALUE, target.getMyByte().byteValue()); - - assertEquals(Short.MAX_VALUE, target.getMyPrimitiveShort()); - assertEquals(Short.MAX_VALUE, target.getMyShort().shortValue()); + assertThat(target.getMyPrimitiveByte()).isEqualTo(Byte.MAX_VALUE); + assertThat(target.getMyByte().byteValue()).isEqualTo(Byte.MAX_VALUE); - assertEquals(Integer.MAX_VALUE, target.getMyPrimitiveInt()); - assertEquals(Integer.MAX_VALUE, target.getMyInteger().intValue()); + assertThat(target.getMyPrimitiveShort()).isEqualTo(Short.MAX_VALUE); + assertThat(target.getMyShort().shortValue()).isEqualTo(Short.MAX_VALUE); - assertEquals(Long.MAX_VALUE, target.getMyPrimitiveLong()); - assertEquals(Long.MAX_VALUE, target.getMyLong().longValue()); + assertThat(target.getMyPrimitiveInt()).isEqualTo(Integer.MAX_VALUE); + assertThat(target.getMyInteger().intValue()).isEqualTo(Integer.MAX_VALUE); - assertEquals(Float.MAX_VALUE, target.getMyPrimitiveFloat(), 0.001); - assertEquals(Float.MAX_VALUE, target.getMyFloat().floatValue(), 0.001); + assertThat(target.getMyPrimitiveLong()).isEqualTo(Long.MAX_VALUE); + assertThat(target.getMyLong().longValue()).isEqualTo(Long.MAX_VALUE); - assertEquals(Double.MAX_VALUE, target.getMyPrimitiveDouble(), 0.001); - assertEquals(Double.MAX_VALUE, target.getMyDouble().doubleValue(), 0.001); + assertThat((double) target.getMyPrimitiveFloat()).isCloseTo(Float.MAX_VALUE, within(0.001)); + assertThat((double) target.getMyFloat().floatValue()).isCloseTo(Float.MAX_VALUE, within(0.001)); + assertThat(target.getMyPrimitiveDouble()).isCloseTo(Double.MAX_VALUE, within(0.001)); + assertThat(target.getMyDouble().doubleValue()).isCloseTo(Double.MAX_VALUE, within(0.001)); } @Test @@ -763,18 +696,13 @@ public void setEnumProperty() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("autowire", "BY_NAME"); - assertEquals(Autowire.BY_NAME, target.getAutowire()); + assertThat(target.getAutowire()).isEqualTo(Autowire.BY_NAME); accessor.setPropertyValue("autowire", " BY_TYPE "); - assertEquals(Autowire.BY_TYPE, target.getAutowire()); + assertThat(target.getAutowire()).isEqualTo(Autowire.BY_TYPE); - try { - accessor.setPropertyValue("autowire", "NHERITED"); - fail("Should have thrown TypeMismatchException"); - } - catch (TypeMismatchException ex) { - // expected - } + assertThatExceptionOfType(TypeMismatchException.class).isThrownBy(() -> + accessor.setPropertyValue("autowire", "NHERITED")); } @Test @@ -782,7 +710,7 @@ public void setGenericEnumProperty() { EnumConsumer target = new EnumConsumer(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("enumValue", TestEnum.class.getName() + ".TEST_VALUE"); - assertEquals(TestEnum.TEST_VALUE, target.getEnumValue()); + assertThat(target.getEnumValue()).isEqualTo(TestEnum.TEST_VALUE); } @Test @@ -790,7 +718,7 @@ public void setWildcardEnumProperty() { WildcardEnumConsumer target = new WildcardEnumConsumer(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("enumValue", TestEnum.class.getName() + ".TEST_VALUE"); - assertEquals(TestEnum.TEST_VALUE, target.getEnumValue()); + assertThat(target.getEnumValue()).isEqualTo(TestEnum.TEST_VALUE); } @Test @@ -803,12 +731,12 @@ public void setPropertiesProperty() throws Exception { String ps = "peace=war\nfreedom=slavery"; accessor.setPropertyValue("properties", ps); - assertTrue("name was set", target.name.equals("ptest")); - assertTrue("properties non null", target.properties != null); + assertThat(target.name.equals("ptest")).as("name was set").isTrue(); + assertThat(target.properties != null).as("properties non null").isTrue(); String freedomVal = target.properties.getProperty("freedom"); String peaceVal = target.properties.getProperty("peace"); - assertTrue("peace==war", peaceVal.equals("war")); - assertTrue("Freedom==slavery", freedomVal.equals("slavery")); + assertThat(peaceVal.equals("war")).as("peace==war").isTrue(); + assertThat(freedomVal.equals("slavery")).as("Freedom==slavery").isTrue(); } @Test @@ -817,9 +745,9 @@ public void setStringArrayProperty() throws Exception { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("stringArray", new String[] {"foo", "fi", "fi", "fum"}); - assertTrue("stringArray length = 4", target.stringArray.length == 4); - assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && - target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")); + assertThat(target.stringArray.length == 4).as("stringArray length = 4").isTrue(); + assertThat(target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && + target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")).as("correct values").isTrue(); List list = new ArrayList<>(); list.add("foo"); @@ -827,25 +755,25 @@ public void setStringArrayProperty() throws Exception { list.add("fi"); list.add("fum"); accessor.setPropertyValue("stringArray", list); - assertTrue("stringArray length = 4", target.stringArray.length == 4); - assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && - target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")); + assertThat(target.stringArray.length == 4).as("stringArray length = 4").isTrue(); + assertThat(target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && + target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")).as("correct values").isTrue(); Set set = new HashSet<>(); set.add("foo"); set.add("fi"); set.add("fum"); accessor.setPropertyValue("stringArray", set); - assertTrue("stringArray length = 3", target.stringArray.length == 3); + assertThat(target.stringArray.length == 3).as("stringArray length = 3").isTrue(); List result = Arrays.asList(target.stringArray); - assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum")); + assertThat(result.contains("foo") && result.contains("fi") && result.contains("fum")).as("correct values").isTrue(); accessor.setPropertyValue("stringArray", "one"); - assertTrue("stringArray length = 1", target.stringArray.length == 1); - assertTrue("stringArray elt is ok", target.stringArray[0].equals("one")); + assertThat(target.stringArray.length == 1).as("stringArray length = 1").isTrue(); + assertThat(target.stringArray[0].equals("one")).as("stringArray elt is ok").isTrue(); accessor.setPropertyValue("stringArray", null); - assertTrue("stringArray is null", target.stringArray == null); + assertThat(target.stringArray == null).as("stringArray is null").isTrue(); } @Test @@ -860,9 +788,9 @@ public void setAsText(String text) { }); accessor.setPropertyValue("stringArray", new String[] {"4foo", "7fi", "6fi", "5fum"}); - assertTrue("stringArray length = 4", target.stringArray.length == 4); - assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && - target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")); + assertThat(target.stringArray.length == 4).as("stringArray length = 4").isTrue(); + assertThat(target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && + target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")).as("correct values").isTrue(); List list = new ArrayList<>(); list.add("4foo"); @@ -870,22 +798,22 @@ public void setAsText(String text) { list.add("6fi"); list.add("5fum"); accessor.setPropertyValue("stringArray", list); - assertTrue("stringArray length = 4", target.stringArray.length == 4); - assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && - target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")); + assertThat(target.stringArray.length == 4).as("stringArray length = 4").isTrue(); + assertThat(target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && + target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum")).as("correct values").isTrue(); Set set = new HashSet<>(); set.add("4foo"); set.add("7fi"); set.add("6fum"); accessor.setPropertyValue("stringArray", set); - assertTrue("stringArray length = 3", target.stringArray.length == 3); + assertThat(target.stringArray.length == 3).as("stringArray length = 3").isTrue(); List result = Arrays.asList(target.stringArray); - assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum")); + assertThat(result.contains("foo") && result.contains("fi") && result.contains("fum")).as("correct values").isTrue(); accessor.setPropertyValue("stringArray", "8one"); - assertTrue("stringArray length = 1", target.stringArray.length == 1); - assertTrue("correct values", target.stringArray[0].equals("one")); + assertThat(target.stringArray.length == 1).as("stringArray length = 1").isTrue(); + assertThat(target.stringArray[0].equals("one")).as("correct values").isTrue(); } @Test @@ -894,8 +822,8 @@ public void setStringArrayPropertyWithStringSplitting() throws Exception { AbstractPropertyAccessor accessor = createAccessor(target); accessor.useConfigValueEditors(); accessor.setPropertyValue("stringArray", "a1,b2"); - assertTrue("stringArray length = 2", target.stringArray.length == 2); - assertTrue("correct values", target.stringArray[0].equals("a1") && target.stringArray[1].equals("b2")); + assertThat(target.stringArray.length == 2).as("stringArray length = 2").isTrue(); + assertThat(target.stringArray[0].equals("a1") && target.stringArray[1].equals("b2")).as("correct values").isTrue(); } @Test @@ -904,8 +832,8 @@ public void setStringArrayPropertyWithCustomStringDelimiter() throws Exception { AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String[].class, "stringArray", new StringArrayPropertyEditor("-")); accessor.setPropertyValue("stringArray", "a1-b2"); - assertTrue("stringArray length = 2", target.stringArray.length == 2); - assertTrue("correct values", target.stringArray[0].equals("a1") && target.stringArray[1].equals("b2")); + assertThat(target.stringArray.length == 2).as("stringArray length = 2").isTrue(); + assertThat(target.stringArray[0].equals("a1") && target.stringArray[1].equals("b2")).as("correct values").isTrue(); } @Test @@ -915,12 +843,12 @@ public void setStringArrayWithAutoGrow() throws Exception { accessor.setAutoGrowNestedPaths(true); accessor.setPropertyValue("array[0]", "Test0"); - assertEquals(1, target.getArray().length); + assertThat(target.getArray().length).isEqualTo(1); accessor.setPropertyValue("array[2]", "Test2"); - assertEquals(3, target.getArray().length); - assertTrue("correct values", target.getArray()[0].equals("Test0") && target.getArray()[1] == null && - target.getArray()[2].equals("Test2")); + assertThat(target.getArray().length).isEqualTo(3); + assertThat(target.getArray()[0].equals("Test0") && target.getArray()[1] == null && + target.getArray()[2].equals("Test2")).as("correct values").isTrue(); } @Test @@ -929,14 +857,14 @@ public void setIntArrayProperty() { AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("intArray", new int[] {4, 5, 2, 3}); - assertTrue("intArray length = 4", target.intArray.length == 4); - assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && - target.intArray[2] == 2 && target.intArray[3] == 3); + assertThat(target.intArray.length == 4).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 4 && target.intArray[1] == 5 && + target.intArray[2] == 2 && target.intArray[3] == 3).as("correct values").isTrue(); accessor.setPropertyValue("intArray", new String[] {"4", "5", "2", "3"}); - assertTrue("intArray length = 4", target.intArray.length == 4); - assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && - target.intArray[2] == 2 && target.intArray[3] == 3); + assertThat(target.intArray.length == 4).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 4 && target.intArray[1] == 5 && + target.intArray[2] == 2 && target.intArray[3] == 3).as("correct values").isTrue(); List list = new ArrayList<>(); list.add(4); @@ -944,38 +872,38 @@ public void setIntArrayProperty() { list.add(2); list.add("3"); accessor.setPropertyValue("intArray", list); - assertTrue("intArray length = 4", target.intArray.length == 4); - assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && - target.intArray[2] == 2 && target.intArray[3] == 3); + assertThat(target.intArray.length == 4).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 4 && target.intArray[1] == 5 && + target.intArray[2] == 2 && target.intArray[3] == 3).as("correct values").isTrue(); Set set = new HashSet<>(); set.add("4"); set.add(5); set.add("3"); accessor.setPropertyValue("intArray", set); - assertTrue("intArray length = 3", target.intArray.length == 3); + assertThat(target.intArray.length == 3).as("intArray length = 3").isTrue(); List result = new ArrayList<>(); result.add(target.intArray[0]); result.add(target.intArray[1]); result.add(target.intArray[2]); - assertTrue("correct values", result.contains(new Integer(4)) && result.contains(new Integer(5)) && - result.contains(new Integer(3))); + assertThat(result.contains(4) && result.contains(5) && + result.contains(3)).as("correct values").isTrue(); accessor.setPropertyValue("intArray", new Integer[] {1}); - assertTrue("intArray length = 4", target.intArray.length == 1); - assertTrue("correct values", target.intArray[0] == 1); + assertThat(target.intArray.length == 1).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 1).as("correct values").isTrue(); - accessor.setPropertyValue("intArray", new Integer(1)); - assertTrue("intArray length = 4", target.intArray.length == 1); - assertTrue("correct values", target.intArray[0] == 1); + accessor.setPropertyValue("intArray", 1); + assertThat(target.intArray.length == 1).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 1).as("correct values").isTrue(); accessor.setPropertyValue("intArray", new String[] {"1"}); - assertTrue("intArray length = 4", target.intArray.length == 1); - assertTrue("correct values", target.intArray[0] == 1); + assertThat(target.intArray.length == 1).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 1).as("correct values").isTrue(); accessor.setPropertyValue("intArray", "1"); - assertTrue("intArray length = 4", target.intArray.length == 1); - assertTrue("correct values", target.intArray[0] == 1); + assertThat(target.intArray.length == 1).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 1).as("correct values").isTrue(); } @Test @@ -985,31 +913,31 @@ public void setIntArrayPropertyWithCustomEditor() { accessor.registerCustomEditor(int.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { - setValue(new Integer(Integer.parseInt(text) + 1)); + setValue(Integer.parseInt(text) + 1); } }); accessor.setPropertyValue("intArray", new int[] {4, 5, 2, 3}); - assertTrue("intArray length = 4", target.intArray.length == 4); - assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && - target.intArray[2] == 2 && target.intArray[3] == 3); + assertThat(target.intArray.length == 4).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 4 && target.intArray[1] == 5 && + target.intArray[2] == 2 && target.intArray[3] == 3).as("correct values").isTrue(); accessor.setPropertyValue("intArray", new String[] {"3", "4", "1", "2"}); - assertTrue("intArray length = 4", target.intArray.length == 4); - assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && - target.intArray[2] == 2 && target.intArray[3] == 3); + assertThat(target.intArray.length == 4).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 4 && target.intArray[1] == 5 && + target.intArray[2] == 2 && target.intArray[3] == 3).as("correct values").isTrue(); - accessor.setPropertyValue("intArray", new Integer(1)); - assertTrue("intArray length = 4", target.intArray.length == 1); - assertTrue("correct values", target.intArray[0] == 1); + accessor.setPropertyValue("intArray", 1); + assertThat(target.intArray.length == 1).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 1).as("correct values").isTrue(); accessor.setPropertyValue("intArray", new String[] {"0"}); - assertTrue("intArray length = 4", target.intArray.length == 1); - assertTrue("correct values", target.intArray[0] == 1); + assertThat(target.intArray.length == 1).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 1).as("correct values").isTrue(); accessor.setPropertyValue("intArray", "0"); - assertTrue("intArray length = 4", target.intArray.length == 1); - assertTrue("correct values", target.intArray[0] == 1); + assertThat(target.intArray.length == 1).as("intArray length = 4").isTrue(); + assertThat(target.intArray[0] == 1).as("correct values").isTrue(); } @Test @@ -1018,8 +946,8 @@ public void setIntArrayPropertyWithStringSplitting() throws Exception { AbstractPropertyAccessor accessor = createAccessor(target); accessor.useConfigValueEditors(); accessor.setPropertyValue("intArray", "4,5"); - assertTrue("intArray length = 2", target.intArray.length == 2); - assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5); + assertThat(target.intArray.length == 2).as("intArray length = 2").isTrue(); + assertThat(target.intArray[0] == 4 && target.intArray[1] == 5).as("correct values").isTrue(); } @Test @@ -1027,14 +955,14 @@ public void setPrimitiveArrayProperty() { PrimitiveArrayBean target = new PrimitiveArrayBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("array", new String[] {"1", "2"}); - assertEquals(2, target.getArray().length); - assertEquals(1, target.getArray()[0]); - assertEquals(2, target.getArray()[1]); + assertThat(target.getArray().length).isEqualTo(2); + assertThat(target.getArray()[0]).isEqualTo(1); + assertThat(target.getArray()[1]).isEqualTo(2); } @Test + @EnabledForTestGroups(PERFORMANCE) public void setPrimitiveArrayPropertyLargeMatching() { - Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(LogFactory.getLog(AbstractPropertyAccessorTests.class)); PrimitiveArrayBean target = new PrimitiveArrayBean(); @@ -1046,10 +974,10 @@ public void setPrimitiveArrayPropertyLargeMatching() { accessor.setPropertyValue("array", input); } sw.stop(); - assertEquals(1024, target.getArray().length); - assertEquals(0, target.getArray()[0]); + assertThat(target.getArray().length).isEqualTo(1024); + assertThat(target.getArray()[0]).isEqualTo(0); long time1 = sw.getLastTaskTimeMillis(); - assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); + assertThat(sw.getLastTaskTimeMillis() < 100).as("Took too long").isTrue(); accessor.registerCustomEditor(String.class, new StringTrimmerEditor(false)); sw.start("array2"); @@ -1057,7 +985,7 @@ public void setPrimitiveArrayPropertyLargeMatching() { accessor.setPropertyValue("array", input); } sw.stop(); - assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125); + assertThat(sw.getLastTaskTimeMillis() < 125).as("Took too long").isTrue(); accessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false)); sw.start("array3"); @@ -1065,7 +993,7 @@ public void setPrimitiveArrayPropertyLargeMatching() { accessor.setPropertyValue("array", input); } sw.stop(); - assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); + assertThat(sw.getLastTaskTimeMillis() < 100).as("Took too long").isTrue(); accessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false)); sw.start("array3"); @@ -1073,7 +1001,7 @@ public void setPrimitiveArrayPropertyLargeMatching() { accessor.setPropertyValue("array", input); } sw.stop(); - assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); + assertThat(sw.getLastTaskTimeMillis() < 100).as("Took too long").isTrue(); accessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false)); sw.start("array4"); @@ -1081,9 +1009,9 @@ public void setPrimitiveArrayPropertyLargeMatching() { accessor.setPropertyValue("array", input); } sw.stop(); - assertEquals(1024, target.getArray().length); - assertEquals(0, target.getArray()[0]); - assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1); + assertThat(target.getArray().length).isEqualTo(1024); + assertThat(target.getArray()[0]).isEqualTo(0); + assertThat(sw.getLastTaskTimeMillis() > time1).as("Took too long").isTrue(); } @Test @@ -1094,15 +1022,15 @@ public void setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor() { @Override public void setValue(Object value) { if (value instanceof Integer) { - super.setValue(new Integer((Integer) value + 1)); + super.setValue((Integer) value + 1); } } }); int[] input = new int[1024]; accessor.setPropertyValue("array", input); - assertEquals(1024, target.getArray().length); - assertEquals(1, target.getArray()[0]); - assertEquals(1, target.getArray()[1]); + assertThat(target.getArray().length).isEqualTo(1024); + assertThat(target.getArray()[0]).isEqualTo(1); + assertThat(target.getArray()[1]).isEqualTo(1); } @Test @@ -1113,15 +1041,15 @@ public void setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor() { @Override public void setValue(Object value) { if (value instanceof Integer) { - super.setValue(new Integer((Integer) value + 1)); + super.setValue((Integer) value + 1); } } }); int[] input = new int[1024]; accessor.setPropertyValue("array", input); - assertEquals(1024, target.getArray().length); - assertEquals(0, target.getArray()[0]); - assertEquals(1, target.getArray()[1]); + assertThat(target.getArray().length).isEqualTo(1024); + assertThat(target.getArray()[0]).isEqualTo(0); + assertThat(target.getArray()[1]).isEqualTo(1); } @Test @@ -1131,15 +1059,16 @@ public void setPrimitiveArrayPropertyWithAutoGrow() throws Exception { accessor.setAutoGrowNestedPaths(true); accessor.setPropertyValue("array[0]", 1); - assertEquals(1, target.getArray().length); + assertThat(target.getArray().length).isEqualTo(1); accessor.setPropertyValue("array[2]", 3); - assertEquals(3, target.getArray().length); - assertTrue("correct values", target.getArray()[0] == 1 && target.getArray()[1] == 0 && - target.getArray()[2] == 3); + assertThat(target.getArray().length).isEqualTo(3); + assertThat(target.getArray()[0] == 1 && target.getArray()[1] == 0 && + target.getArray()[2] == 3).as("correct values").isTrue(); } @Test + @SuppressWarnings("rawtypes") public void setGenericArrayProperty() { SkipReaderStub target = new SkipReaderStub(); AbstractPropertyAccessor accessor = createAccessor(target); @@ -1150,11 +1079,11 @@ public void setGenericArrayProperty() { values.add("4"); accessor.setPropertyValue("items", values); Object[] result = target.items; - assertEquals(4, result.length); - assertEquals("1", result[0]); - assertEquals("2", result[1]); - assertEquals("3", result[2]); - assertEquals("4", result[3]); + assertThat(result.length).isEqualTo(4); + assertThat(result[0]).isEqualTo("1"); + assertThat(result[1]).isEqualTo("2"); + assertThat(result[2]).isEqualTo("3"); + assertThat(result[3]).isEqualTo("4"); } @Test @@ -1164,14 +1093,13 @@ public void setArrayPropertyToObject() { Object[] array = new Object[] {"1", "2"}; accessor.setPropertyValue("object", array); - assertThat(target.getObject(), equalTo((Object) array)); + assertThat(target.getObject()).isEqualTo(array); array = new Object[] {"1"}; accessor.setPropertyValue("object", array); - assertThat(target.getObject(), equalTo((Object) array)); + assertThat(target.getObject()).isEqualTo(array); } - @Test public void setCollectionProperty() { IndexedTestBean target = new IndexedTestBean(); @@ -1188,10 +1116,10 @@ public void setCollectionProperty() { List list = new LinkedList<>(); list.add("list1"); accessor.setPropertyValue("list", list); - assertSame(coll, target.getCollection()); - assertSame(set, target.getSet()); - assertSame(sortedSet, target.getSortedSet()); - assertSame(list, target.getList()); + assertThat(target.getCollection()).isSameAs(coll); + assertThat(target.getSet()).isSameAs(set); + assertThat(target.getSortedSet()).isSameAs(sortedSet); + assertThat((List) target.getList()).isSameAs(list); } @SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests @@ -1211,14 +1139,14 @@ public void setCollectionPropertyNonMatchingType() { Set list = new HashSet<>(); list.add("list1"); accessor.setPropertyValue("list", list); - assertEquals(1, target.getCollection().size()); - assertTrue(target.getCollection().containsAll(coll)); - assertEquals(1, target.getSet().size()); - assertTrue(target.getSet().containsAll(set)); - assertEquals(1, target.getSortedSet().size()); - assertTrue(target.getSortedSet().containsAll(sortedSet)); - assertEquals(1, target.getList().size()); - assertTrue(target.getList().containsAll(list)); + assertThat(target.getCollection().size()).isEqualTo(1); + assertThat(target.getCollection().containsAll(coll)).isTrue(); + assertThat(target.getSet().size()).isEqualTo(1); + assertThat(target.getSet().containsAll(set)).isTrue(); + assertThat(target.getSortedSet().size()).isEqualTo(1); + assertThat(target.getSortedSet().containsAll(sortedSet)).isTrue(); + assertThat(target.getList().size()).isEqualTo(1); + assertThat(target.getList().containsAll(list)).isTrue(); } @SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests @@ -1238,14 +1166,14 @@ public void setCollectionPropertyWithArrayValue() { Set list = new HashSet<>(); list.add("list1"); accessor.setPropertyValue("list", list.toArray()); - assertEquals(1, target.getCollection().size()); - assertTrue(target.getCollection().containsAll(coll)); - assertEquals(1, target.getSet().size()); - assertTrue(target.getSet().containsAll(set)); - assertEquals(1, target.getSortedSet().size()); - assertTrue(target.getSortedSet().containsAll(sortedSet)); - assertEquals(1, target.getList().size()); - assertTrue(target.getList().containsAll(list)); + assertThat(target.getCollection().size()).isEqualTo(1); + assertThat(target.getCollection().containsAll(coll)).isTrue(); + assertThat(target.getSet().size()).isEqualTo(1); + assertThat(target.getSet().containsAll(set)).isTrue(); + assertThat(target.getSortedSet().size()).isEqualTo(1); + assertThat(target.getSortedSet().containsAll(sortedSet)).isTrue(); + assertThat(target.getList().size()).isEqualTo(1); + assertThat(target.getList().containsAll(list)).isTrue(); } @SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests @@ -1265,14 +1193,14 @@ public void setCollectionPropertyWithIntArrayValue() { Set list = new HashSet<>(); list.add(3); accessor.setPropertyValue("list", new int[] {3}); - assertEquals(1, target.getCollection().size()); - assertTrue(target.getCollection().containsAll(coll)); - assertEquals(1, target.getSet().size()); - assertTrue(target.getSet().containsAll(set)); - assertEquals(1, target.getSortedSet().size()); - assertTrue(target.getSortedSet().containsAll(sortedSet)); - assertEquals(1, target.getList().size()); - assertTrue(target.getList().containsAll(list)); + assertThat(target.getCollection().size()).isEqualTo(1); + assertThat(target.getCollection().containsAll(coll)).isTrue(); + assertThat(target.getSet().size()).isEqualTo(1); + assertThat(target.getSet().containsAll(set)).isTrue(); + assertThat(target.getSortedSet().size()).isEqualTo(1); + assertThat(target.getSortedSet().containsAll(sortedSet)).isTrue(); + assertThat(target.getList().size()).isEqualTo(1); + assertThat(target.getList().containsAll(list)).isTrue(); } @SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests @@ -1282,24 +1210,24 @@ public void setCollectionPropertyWithIntegerValue() { AbstractPropertyAccessor accessor = createAccessor(target); Collection coll = new HashSet<>(); coll.add(0); - accessor.setPropertyValue("collection", new Integer(0)); + accessor.setPropertyValue("collection", 0); List set = new LinkedList<>(); set.add(1); - accessor.setPropertyValue("set", new Integer(1)); + accessor.setPropertyValue("set", 1); List sortedSet = new ArrayList<>(); sortedSet.add(2); - accessor.setPropertyValue("sortedSet", new Integer(2)); + accessor.setPropertyValue("sortedSet", 2); Set list = new HashSet<>(); list.add(3); - accessor.setPropertyValue("list", new Integer(3)); - assertEquals(1, target.getCollection().size()); - assertTrue(target.getCollection().containsAll(coll)); - assertEquals(1, target.getSet().size()); - assertTrue(target.getSet().containsAll(set)); - assertEquals(1, target.getSortedSet().size()); - assertTrue(target.getSortedSet().containsAll(sortedSet)); - assertEquals(1, target.getList().size()); - assertTrue(target.getList().containsAll(list)); + accessor.setPropertyValue("list", 3); + assertThat(target.getCollection().size()).isEqualTo(1); + assertThat(target.getCollection().containsAll(coll)).isTrue(); + assertThat(target.getSet().size()).isEqualTo(1); + assertThat(target.getSet().containsAll(set)).isTrue(); + assertThat(target.getSortedSet().size()).isEqualTo(1); + assertThat(target.getSortedSet().containsAll(sortedSet)).isTrue(); + assertThat(target.getList().size()).isEqualTo(1); + assertThat(target.getList().containsAll(list)).isTrue(); } @SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests @@ -1316,12 +1244,12 @@ public void setCollectionPropertyWithStringValue() { Set list = new HashSet<>(); list.add("list1"); accessor.setPropertyValue("list", "list1"); - assertEquals(1, target.getSet().size()); - assertTrue(target.getSet().containsAll(set)); - assertEquals(1, target.getSortedSet().size()); - assertTrue(target.getSortedSet().containsAll(sortedSet)); - assertEquals(1, target.getList().size()); - assertTrue(target.getList().containsAll(list)); + assertThat(target.getSet().size()).isEqualTo(1); + assertThat(target.getSet().containsAll(set)).isTrue(); + assertThat(target.getSortedSet().size()).isEqualTo(1); + assertThat(target.getSortedSet().containsAll(sortedSet)).isTrue(); + assertThat(target.getList().size()).isEqualTo(1); + assertThat(target.getList().containsAll(list)).isTrue(); } @Test @@ -1334,15 +1262,15 @@ public void setCollectionPropertyWithStringValueAndCustomEditor() { accessor.setPropertyValue("set", "set1 "); accessor.setPropertyValue("sortedSet", "sortedSet1"); accessor.setPropertyValue("list", "list1 "); - assertEquals(1, target.getSet().size()); - assertTrue(target.getSet().contains("set1")); - assertEquals(1, target.getSortedSet().size()); - assertTrue(target.getSortedSet().contains("sortedSet1")); - assertEquals(1, target.getList().size()); - assertTrue(target.getList().contains("list1")); + assertThat(target.getSet().size()).isEqualTo(1); + assertThat(target.getSet().contains("set1")).isTrue(); + assertThat(target.getSortedSet().size()).isEqualTo(1); + assertThat(target.getSortedSet().contains("sortedSet1")).isTrue(); + assertThat(target.getList().size()).isEqualTo(1); + assertThat(target.getList().contains("list1")).isTrue(); accessor.setPropertyValue("list", Collections.singletonList("list1 ")); - assertTrue(target.getList().contains("list1")); + assertThat(target.getList().contains("list1")).isTrue(); } @Test @@ -1355,8 +1283,8 @@ public void setMapProperty() { SortedMap sortedMap = new TreeMap<>(); map.put("sortedKey", "sortedValue"); accessor.setPropertyValue("sortedMap", sortedMap); - assertSame(map, target.getMap()); - assertSame(sortedMap, target.getSortedMap()); + assertThat((Map) target.getMap()).isSameAs(map); + assertThat((Map) target.getSortedMap()).isSameAs(sortedMap); } @Test @@ -1369,10 +1297,10 @@ public void setMapPropertyNonMatchingType() { Map sortedMap = new TreeMap<>(); sortedMap.put("sortedKey", "sortedValue"); accessor.setPropertyValue("sortedMap", sortedMap); - assertEquals(1, target.getMap().size()); - assertEquals("value", target.getMap().get("key")); - assertEquals(1, target.getSortedMap().size()); - assertEquals("sortedValue", target.getSortedMap().get("sortedKey")); + assertThat(target.getMap().size()).isEqualTo(1); + assertThat(target.getMap().get("key")).isEqualTo("value"); + assertThat(target.getSortedMap().size()).isEqualTo(1); + assertThat(target.getSortedMap().get("sortedKey")).isEqualTo("sortedValue"); } @Test @@ -1389,24 +1317,19 @@ public void setAsText(String text) throws IllegalArgumentException { } }); - MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.add("map[key1]", "rod"); - pvs.add("map[key2]", "rob"); - accessor.setPropertyValues(pvs); - assertEquals("rod", ((TestBean) target.getMap().get("key1")).getName()); - assertEquals("rob", ((TestBean) target.getMap().get("key2")).getName()); + MutablePropertyValues goodValues = new MutablePropertyValues(); + goodValues.add("map[key1]", "rod"); + goodValues.add("map[key2]", "rob"); + accessor.setPropertyValues(goodValues); + assertThat(((TestBean) target.getMap().get("key1")).getName()).isEqualTo("rod"); + assertThat(((TestBean) target.getMap().get("key2")).getName()).isEqualTo("rob"); - pvs = new MutablePropertyValues(); - pvs.add("map[key1]", "rod"); - pvs.add("map[key2]", ""); - try { - accessor.setPropertyValues(pvs); - fail("Should have thrown TypeMismatchException"); - } - catch (PropertyBatchUpdateException ex) { - PropertyAccessException pae = ex.getPropertyAccessException("map[key2]"); - assertTrue(pae instanceof TypeMismatchException); - } + MutablePropertyValues badValues = new MutablePropertyValues(); + badValues.add("map[key1]", "rod"); + badValues.add("map[key2]", ""); + assertThatExceptionOfType(PropertyBatchUpdateException.class).isThrownBy(() -> + accessor.setPropertyValues(badValues)) + .satisfies(ex -> assertThat(ex.getPropertyAccessException("map[key2]")).isInstanceOf(TypeMismatchException.class)); } @Test @@ -1429,8 +1352,8 @@ public void setAsText(String text) throws IllegalArgumentException { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map", Collections.unmodifiableMap(inputMap)); accessor.setPropertyValues(pvs); - assertEquals("rod", ((TestBean) target.getMap().get(1)).getName()); - assertEquals("rob", ((TestBean) target.getMap().get(2)).getName()); + assertThat(((TestBean) target.getMap().get(1)).getName()).isEqualTo("rod"); + assertThat(((TestBean) target.getMap().get(2)).getName()).isEqualTo("rob"); } @Test @@ -1453,11 +1376,11 @@ public void setAsText(String text) throws IllegalArgumentException { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map", new ReadOnlyMap<>(inputMap)); accessor.setPropertyValues(pvs); - assertEquals("rod", ((TestBean) target.getMap().get(1)).getName()); - assertEquals("rob", ((TestBean) target.getMap().get(2)).getName()); + assertThat(((TestBean) target.getMap().get(1)).getName()).isEqualTo("rod"); + assertThat(((TestBean) target.getMap().get(2)).getName()).isEqualTo("rob"); } - @SuppressWarnings("unchecked") // must work with raw map in this test + @SuppressWarnings({ "unchecked", "rawtypes" }) // must work with raw map in this test @Test public void setRawMapPropertyWithNoEditorRegistered() { IndexedTestBean target = new IndexedTestBean(); @@ -1469,55 +1392,42 @@ public void setRawMapPropertyWithNoEditorRegistered() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map", readOnlyMap); accessor.setPropertyValues(pvs); - assertSame(readOnlyMap, target.getMap()); - assertFalse(readOnlyMap.isAccessed()); + assertThat(target.getMap()).isSameAs(readOnlyMap); + assertThat(readOnlyMap.isAccessed()).isFalse(); } @Test public void setUnknownProperty() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); - - try { - accessor.setPropertyValue("name1", "value"); - fail("Should have failed to set an unknown property."); - } - catch (NotWritablePropertyException e) { - assertEquals(Simple.class, e.getBeanClass()); - assertEquals("name1", e.getPropertyName()); - assertEquals("Invalid number of possible matches", 1, e.getPossibleMatches().length); - assertEquals("name", e.getPossibleMatches()[0]); - } + assertThatExceptionOfType(NotWritablePropertyException.class).isThrownBy(() -> + accessor.setPropertyValue("name1", "value")) + .satisfies(ex -> { + assertThat(ex.getBeanClass()).isEqualTo(Simple.class); + assertThat(ex.getPropertyName()).isEqualTo("name1"); + assertThat(ex.getPossibleMatches()).containsExactly("name"); + }); } @Test public void setUnknownPropertyWithPossibleMatches() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); - - try { - accessor.setPropertyValue("foo", "value"); - fail("Should have failed to set an unknown property."); - } - catch (NotWritablePropertyException e) { - assertEquals(Simple.class, e.getBeanClass()); - assertEquals("foo", e.getPropertyName()); - } + assertThatExceptionOfType(NotWritablePropertyException.class).isThrownBy(() -> + accessor.setPropertyValue("foo", "value")) + .satisfies(ex -> { + assertThat(ex.getBeanClass()).isEqualTo(Simple.class); + assertThat(ex.getPropertyName()).isEqualTo("foo"); + }); } @Test public void setUnknownOptionalProperty() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); - - try { - PropertyValue value = new PropertyValue("foo", "value"); - value.setOptional(true); - accessor.setPropertyValue(value); - } - catch (NotWritablePropertyException e) { - fail("Should not have failed to set an unknown optional property."); - } + PropertyValue value = new PropertyValue("foo", "value"); + value.setOptional(true); + accessor.setPropertyValue(value); } @Test @@ -1525,37 +1435,24 @@ public void setPropertyInProtectedBaseBean() { DerivedFromProtectedBaseBean target = new DerivedFromProtectedBaseBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("someProperty", "someValue"); - assertEquals("someValue", accessor.getPropertyValue("someProperty")); - assertEquals("someValue", target.getSomeProperty()); + assertThat(accessor.getPropertyValue("someProperty")).isEqualTo("someValue"); + assertThat(target.getSomeProperty()).isEqualTo("someValue"); } @Test public void setPropertyTypeMismatch() { TestBean target = new TestBean(); - try { - AbstractPropertyAccessor accessor = createAccessor(target); - accessor.setPropertyValue("age", "foobar"); - fail("Should throw exception on type mismatch"); - } - catch (TypeMismatchException ex) { - // expected - } + AbstractPropertyAccessor accessor = createAccessor(target); + assertThatExceptionOfType(TypeMismatchException.class).isThrownBy(() -> + accessor.setPropertyValue("age", "foobar")); } @Test public void setEmptyValueForPrimitiveProperty() { TestBean target = new TestBean(); - try { - AbstractPropertyAccessor accessor = createAccessor(target); - accessor.setPropertyValue("age", ""); - fail("Should throw exception on type mismatch"); - } - catch (TypeMismatchException ex) { - // expected - } - catch (Exception ex) { - fail("Shouldn't throw exception other than Type mismatch"); - } + AbstractPropertyAccessor accessor = createAccessor(target); + assertThatExceptionOfType(TypeMismatchException.class).isThrownBy(() -> + accessor.setPropertyValue("age", "")); } @Test @@ -1563,8 +1460,8 @@ public void setUnknownNestedProperty() { Person target = createPerson("John", "Paris", "FR"); AbstractPropertyAccessor accessor = createAccessor(target); - thrown.expect(NotWritablePropertyException.class); - accessor.setPropertyValue("address.bar", "value"); + assertThatExceptionOfType(NotWritablePropertyException.class).isThrownBy(() -> + accessor.setPropertyValue("address.bar", "value")); } @Test @@ -1576,15 +1473,9 @@ public void setPropertyValuesIgnoresInvalidNestedOnRequest() { pvs.addPropertyValue(new PropertyValue("more.garbage", new Object())); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValues(pvs, true); - assertTrue("Set valid and ignored invalid", target.getName().equals("rod")); - try { - // Don't ignore: should fail - accessor.setPropertyValues(pvs, false); - fail("Shouldn't have ignored invalid updates"); - } - catch (NotWritablePropertyException ex) { - // OK: but which exception?? - } + assertThat(target.getName().equals("rod")).as("Set valid and ignored invalid").isTrue(); + assertThatExceptionOfType(NotWritablePropertyException.class).isThrownBy(() -> + accessor.setPropertyValues(pvs, false)); // Don't ignore: should fail } @Test @@ -1599,26 +1490,31 @@ public void getAndSetIndexedProperties() { TestBean tb7 = ((TestBean) target.getSet().toArray()[1]); TestBean tb4 = ((TestBean) target.getMap().get("key1")); TestBean tb5 = ((TestBean) target.getMap().get("key.3")); - assertEquals("name0", tb0.getName()); - assertEquals("name1", tb1.getName()); - assertEquals("name2", tb2.getName()); - assertEquals("name3", tb3.getName()); - assertEquals("name6", tb6.getName()); - assertEquals("name7", tb7.getName()); - assertEquals("name4", tb4.getName()); - assertEquals("name5", tb5.getName()); - assertEquals("name0", accessor.getPropertyValue("array[0].name")); - assertEquals("name1", accessor.getPropertyValue("array[1].name")); - assertEquals("name2", accessor.getPropertyValue("list[0].name")); - assertEquals("name3", accessor.getPropertyValue("list[1].name")); - assertEquals("name6", accessor.getPropertyValue("set[0].name")); - assertEquals("name7", accessor.getPropertyValue("set[1].name")); - assertEquals("name4", accessor.getPropertyValue("map[key1].name")); - assertEquals("name5", accessor.getPropertyValue("map[key.3].name")); - assertEquals("name4", accessor.getPropertyValue("map['key1'].name")); - assertEquals("name5", accessor.getPropertyValue("map[\"key.3\"].name")); - assertEquals("nameX", accessor.getPropertyValue("map[key4][0].name")); - assertEquals("nameY", accessor.getPropertyValue("map[key4][1].name")); + TestBean tb8 = ((TestBean) target.getMap().get("key5[foo]")); + assertThat(tb0.getName()).isEqualTo("name0"); + assertThat(tb1.getName()).isEqualTo("name1"); + assertThat(tb2.getName()).isEqualTo("name2"); + assertThat(tb3.getName()).isEqualTo("name3"); + assertThat(tb6.getName()).isEqualTo("name6"); + assertThat(tb7.getName()).isEqualTo("name7"); + assertThat(tb4.getName()).isEqualTo("name4"); + assertThat(tb5.getName()).isEqualTo("name5"); + assertThat(tb8.getName()).isEqualTo("name8"); + assertThat(accessor.getPropertyValue("array[0].name")).isEqualTo("name0"); + assertThat(accessor.getPropertyValue("array[1].name")).isEqualTo("name1"); + assertThat(accessor.getPropertyValue("list[0].name")).isEqualTo("name2"); + assertThat(accessor.getPropertyValue("list[1].name")).isEqualTo("name3"); + assertThat(accessor.getPropertyValue("set[0].name")).isEqualTo("name6"); + assertThat(accessor.getPropertyValue("set[1].name")).isEqualTo("name7"); + assertThat(accessor.getPropertyValue("map[key1].name")).isEqualTo("name4"); + assertThat(accessor.getPropertyValue("map[key.3].name")).isEqualTo("name5"); + assertThat(accessor.getPropertyValue("map['key1'].name")).isEqualTo("name4"); + assertThat(accessor.getPropertyValue("map[\"key.3\"].name")).isEqualTo("name5"); + assertThat(accessor.getPropertyValue("map[key4][0].name")).isEqualTo("nameX"); + assertThat(accessor.getPropertyValue("map[key4][1].name")).isEqualTo("nameY"); + assertThat(accessor.getPropertyValue("map[key5[foo]].name")).isEqualTo("name8"); + assertThat(accessor.getPropertyValue("map['key5[foo]'].name")).isEqualTo("name8"); + assertThat(accessor.getPropertyValue("map[\"key5[foo]\"].name")).isEqualTo("name8"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].name", "name5"); @@ -1631,23 +1527,25 @@ public void getAndSetIndexedProperties() { pvs.add("map['key.3'].name", "name0"); pvs.add("map[key4][0].name", "nameA"); pvs.add("map[key4][1].name", "nameB"); + pvs.add("map[key5[foo]].name", "name10"); accessor.setPropertyValues(pvs); - assertEquals("name5", tb0.getName()); - assertEquals("name4", tb1.getName()); - assertEquals("name3", tb2.getName()); - assertEquals("name2", tb3.getName()); - assertEquals("name1", tb4.getName()); - assertEquals("name0", tb5.getName()); - assertEquals("name5", accessor.getPropertyValue("array[0].name")); - assertEquals("name4", accessor.getPropertyValue("array[1].name")); - assertEquals("name3", accessor.getPropertyValue("list[0].name")); - assertEquals("name2", accessor.getPropertyValue("list[1].name")); - assertEquals("name8", accessor.getPropertyValue("set[0].name")); - assertEquals("name9", accessor.getPropertyValue("set[1].name")); - assertEquals("name1", accessor.getPropertyValue("map[\"key1\"].name")); - assertEquals("name0", accessor.getPropertyValue("map['key.3'].name")); - assertEquals("nameA", accessor.getPropertyValue("map[key4][0].name")); - assertEquals("nameB", accessor.getPropertyValue("map[key4][1].name")); + assertThat(tb0.getName()).isEqualTo("name5"); + assertThat(tb1.getName()).isEqualTo("name4"); + assertThat(tb2.getName()).isEqualTo("name3"); + assertThat(tb3.getName()).isEqualTo("name2"); + assertThat(tb4.getName()).isEqualTo("name1"); + assertThat(tb5.getName()).isEqualTo("name0"); + assertThat(accessor.getPropertyValue("array[0].name")).isEqualTo("name5"); + assertThat(accessor.getPropertyValue("array[1].name")).isEqualTo("name4"); + assertThat(accessor.getPropertyValue("list[0].name")).isEqualTo("name3"); + assertThat(accessor.getPropertyValue("list[1].name")).isEqualTo("name2"); + assertThat(accessor.getPropertyValue("set[0].name")).isEqualTo("name8"); + assertThat(accessor.getPropertyValue("set[1].name")).isEqualTo("name9"); + assertThat(accessor.getPropertyValue("map[\"key1\"].name")).isEqualTo("name1"); + assertThat(accessor.getPropertyValue("map['key.3'].name")).isEqualTo("name0"); + assertThat(accessor.getPropertyValue("map[key4][0].name")).isEqualTo("nameA"); + assertThat(accessor.getPropertyValue("map[key4][1].name")).isEqualTo("nameB"); + assertThat(accessor.getPropertyValue("map[key5[foo]].name")).isEqualTo("name10"); } @Test @@ -1662,16 +1560,16 @@ public void getAndSetIndexedPropertiesWithDirectAccess() { TestBean tb7 = ((TestBean) target.getSet().toArray()[1]); TestBean tb4 = ((TestBean) target.getMap().get("key1")); TestBean tb5 = ((TestBean) target.getMap().get("key2")); - assertEquals(tb0, accessor.getPropertyValue("array[0]")); - assertEquals(tb1, accessor.getPropertyValue("array[1]")); - assertEquals(tb2, accessor.getPropertyValue("list[0]")); - assertEquals(tb3, accessor.getPropertyValue("list[1]")); - assertEquals(tb6, accessor.getPropertyValue("set[0]")); - assertEquals(tb7, accessor.getPropertyValue("set[1]")); - assertEquals(tb4, accessor.getPropertyValue("map[key1]")); - assertEquals(tb5, accessor.getPropertyValue("map[key2]")); - assertEquals(tb4, accessor.getPropertyValue("map['key1']")); - assertEquals(tb5, accessor.getPropertyValue("map[\"key2\"]")); + assertThat(accessor.getPropertyValue("array[0]")).isEqualTo(tb0); + assertThat(accessor.getPropertyValue("array[1]")).isEqualTo(tb1); + assertThat(accessor.getPropertyValue("list[0]")).isEqualTo(tb2); + assertThat(accessor.getPropertyValue("list[1]")).isEqualTo(tb3); + assertThat(accessor.getPropertyValue("set[0]")).isEqualTo(tb6); + assertThat(accessor.getPropertyValue("set[1]")).isEqualTo(tb7); + assertThat(accessor.getPropertyValue("map[key1]")).isEqualTo(tb4); + assertThat(accessor.getPropertyValue("map[key2]")).isEqualTo(tb5); + assertThat(accessor.getPropertyValue("map['key1']")).isEqualTo(tb4); + assertThat(accessor.getPropertyValue("map[\"key2\"]")).isEqualTo(tb5); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0]", tb5); @@ -1685,28 +1583,28 @@ public void getAndSetIndexedPropertiesWithDirectAccess() { pvs.add("map[key5]", tb4); pvs.add("map['key9']", tb5); accessor.setPropertyValues(pvs); - assertEquals(tb5, target.getArray()[0]); - assertEquals(tb4, target.getArray()[1]); - assertEquals(tb3, (target.getList().get(0))); - assertEquals(tb2, (target.getList().get(1))); - assertEquals(tb0, (target.getList().get(2))); - assertEquals(null, (target.getList().get(3))); - assertEquals(tb1, (target.getList().get(4))); - assertEquals(tb1, (target.getMap().get("key1"))); - assertEquals(tb0, (target.getMap().get("key2"))); - assertEquals(tb4, (target.getMap().get("key5"))); - assertEquals(tb5, (target.getMap().get("key9"))); - assertEquals(tb5, accessor.getPropertyValue("array[0]")); - assertEquals(tb4, accessor.getPropertyValue("array[1]")); - assertEquals(tb3, accessor.getPropertyValue("list[0]")); - assertEquals(tb2, accessor.getPropertyValue("list[1]")); - assertEquals(tb0, accessor.getPropertyValue("list[2]")); - assertEquals(null, accessor.getPropertyValue("list[3]")); - assertEquals(tb1, accessor.getPropertyValue("list[4]")); - assertEquals(tb1, accessor.getPropertyValue("map[\"key1\"]")); - assertEquals(tb0, accessor.getPropertyValue("map['key2']")); - assertEquals(tb4, accessor.getPropertyValue("map[\"key5\"]")); - assertEquals(tb5, accessor.getPropertyValue("map['key9']")); + assertThat(target.getArray()[0]).isEqualTo(tb5); + assertThat(target.getArray()[1]).isEqualTo(tb4); + assertThat((target.getList().get(0))).isEqualTo(tb3); + assertThat((target.getList().get(1))).isEqualTo(tb2); + assertThat((target.getList().get(2))).isEqualTo(tb0); + assertThat((target.getList().get(3))).isEqualTo(null); + assertThat((target.getList().get(4))).isEqualTo(tb1); + assertThat((target.getMap().get("key1"))).isEqualTo(tb1); + assertThat((target.getMap().get("key2"))).isEqualTo(tb0); + assertThat((target.getMap().get("key5"))).isEqualTo(tb4); + assertThat((target.getMap().get("key9"))).isEqualTo(tb5); + assertThat(accessor.getPropertyValue("array[0]")).isEqualTo(tb5); + assertThat(accessor.getPropertyValue("array[1]")).isEqualTo(tb4); + assertThat(accessor.getPropertyValue("list[0]")).isEqualTo(tb3); + assertThat(accessor.getPropertyValue("list[1]")).isEqualTo(tb2); + assertThat(accessor.getPropertyValue("list[2]")).isEqualTo(tb0); + assertThat(accessor.getPropertyValue("list[3]")).isEqualTo(null); + assertThat(accessor.getPropertyValue("list[4]")).isEqualTo(tb1); + assertThat(accessor.getPropertyValue("map[\"key1\"]")).isEqualTo(tb1); + assertThat(accessor.getPropertyValue("map['key2']")).isEqualTo(tb0); + assertThat(accessor.getPropertyValue("map[\"key5\"]")).isEqualTo(tb4); + assertThat(accessor.getPropertyValue("map['key9']")).isEqualTo(tb5); } @Test @@ -1714,7 +1612,7 @@ public void propertyType() { Person target = createPerson("John", "Paris", "FR"); AbstractPropertyAccessor accessor = createAccessor(target); - assertEquals(String.class, accessor.getPropertyType("address.city")); + assertThat(accessor.getPropertyType("address.city")).isEqualTo(String.class); } @Test @@ -1722,7 +1620,7 @@ public void propertyTypeUnknownProperty() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); - assertThat(accessor.getPropertyType("foo"), is(nullValue())); + assertThat(accessor.getPropertyType("foo")).isNull(); } @Test @@ -1730,7 +1628,7 @@ public void propertyTypeDescriptor() { Person target = createPerson("John", "Paris", "FR"); AbstractPropertyAccessor accessor = createAccessor(target); - assertThat(accessor.getPropertyTypeDescriptor("address.city"), is(notNullValue())); + assertThat(accessor.getPropertyTypeDescriptor("address.city")).isNotNull(); } @Test @@ -1738,22 +1636,22 @@ public void propertyTypeDescriptorUnknownProperty() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); - assertThat(accessor.getPropertyTypeDescriptor("foo"), is(nullValue())); + assertThat(accessor.getPropertyTypeDescriptor("foo")).isNull(); } @Test public void propertyTypeIndexedProperty() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); - assertEquals(null, accessor.getPropertyType("map[key0]")); + assertThat(accessor.getPropertyType("map[key0]")).isEqualTo(null); accessor = createAccessor(target); accessor.setPropertyValue("map[key0]", "my String"); - assertEquals(String.class, accessor.getPropertyType("map[key0]")); + assertThat(accessor.getPropertyType("map[key0]")).isEqualTo(String.class); accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false)); - assertEquals(String.class, accessor.getPropertyType("map[key0]")); + assertThat(accessor.getPropertyType("map[key0]")).isEqualTo(String.class); } @Test @@ -1761,7 +1659,7 @@ public void cornerSpr10115() { Spr10115Bean target = new Spr10115Bean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("prop1", "val1"); - assertEquals("val1", Spr10115Bean.prop1); + assertThat(Spr10115Bean.prop1).isEqualTo("val1"); } @Test @@ -1769,7 +1667,7 @@ public void cornerSpr13837() { Spr13837Bean target = new Spr13837Bean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("something", 42); - assertEquals(Integer.valueOf(42), target.something); + assertThat(target.something).isEqualTo(Integer.valueOf(42)); } @@ -1778,6 +1676,7 @@ private Person createPerson(String name, String city, String country) { } + @SuppressWarnings("unused") private static class Simple { private String name; @@ -1806,6 +1705,7 @@ public void setInteger(Integer integer) { } } + @SuppressWarnings("unused") private static class Person { private String name; @@ -1836,6 +1736,7 @@ public void setAddress(Address address) { } } + @SuppressWarnings("unused") private static class Address { private String city; @@ -1867,6 +1768,7 @@ public void setCountry(Country country) { } } + @SuppressWarnings("unused") private static class Country { private String name; @@ -1895,7 +1797,7 @@ public void setAge(int age) { } } - @SuppressWarnings("unused") + @SuppressWarnings({ "unused", "rawtypes" }) private static class Foo { private List list; @@ -2007,10 +1909,9 @@ public void setCompany(String co) { @SuppressWarnings("unused") private static class DifferentTestBean extends TestBean { - // class to test naming of beans in a error message + // class to test naming of beans in an error message } - @SuppressWarnings("unused") private static class NumberPropertyBean { @@ -2191,10 +2092,12 @@ public static class SkipReaderStub { public SkipReaderStub() { } + @SuppressWarnings("unchecked") public SkipReaderStub(T... items) { this.items = items; } + @SuppressWarnings("unchecked") public void setItems(T... items) { this.items = items; } @@ -2228,6 +2131,7 @@ public Integer getSomething() { } @Override + @SuppressWarnings("unchecked") public Spr13837Bean setSomething(final Integer something) { this.something = something; return this; diff --git a/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java b/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java index 5416624420ed..9f3bd08ec9f3 100644 --- a/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,7 +19,7 @@ import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -31,11 +31,12 @@ public abstract class AbstractPropertyValuesTests { * Must contain: forname=Tony surname=Blair age=50 */ protected void doTestTony(PropertyValues pvs) { - assertTrue("Contains 3", pvs.getPropertyValues().length == 3); - assertTrue("Contains forname", pvs.contains("forname")); - assertTrue("Contains surname", pvs.contains("surname")); - assertTrue("Contains age", pvs.contains("age")); - assertTrue("Doesn't contain tory", !pvs.contains("tory")); + assertThat(pvs.getPropertyValues().length == 3).as("Contains 3").isTrue(); + assertThat(pvs.contains("forname")).as("Contains forname").isTrue(); + assertThat(pvs.contains("surname")).as("Contains surname").isTrue(); + assertThat(pvs.contains("age")).as("Contains age").isTrue(); + boolean condition1 = !pvs.contains("tory"); + assertThat(condition1).as("Doesn't contain tory").isTrue(); PropertyValue[] ps = pvs.getPropertyValues(); Map m = new HashMap<>(); @@ -44,12 +45,13 @@ protected void doTestTony(PropertyValues pvs) { m.put("age", "50"); for (int i = 0; i < ps.length; i++) { Object val = m.get(ps[i].getName()); - assertTrue("Can't have unexpected value", val != null); - assertTrue("Val i string", val instanceof String); - assertTrue("val matches expected", val.equals(ps[i].getValue())); + assertThat(val != null).as("Can't have unexpected value").isTrue(); + boolean condition = val instanceof String; + assertThat(condition).as("Val i string").isTrue(); + assertThat(val.equals(ps[i].getValue())).as("val matches expected").isTrue(); m.remove(ps[i].getName()); } - assertTrue("Map size is 0", m.size() == 0); + assertThat(m.size() == 0).as("Map size is 0").isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java index 21c49293af70..78c6f8b4b211 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,21 +18,33 @@ import java.beans.Introspector; import java.beans.PropertyDescriptor; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; -import java.util.ArrayList; +import java.net.URI; +import java.net.URL; +import java.time.DayOfWeek; +import java.time.LocalDateTime; +import java.util.Date; import java.util.List; +import java.util.Locale; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.propertyeditors.CustomDateEditor; +import org.springframework.beans.testfixture.beans.DerivedTestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceEditor; -import org.springframework.tests.sample.beans.DerivedTestBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.lang.Nullable; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.SoftAssertions.assertSoftly; /** * Unit tests for {@link BeanUtils}. @@ -40,205 +52,244 @@ * @author Juergen Hoeller * @author Rob Harrop * @author Chris Beams + * @author Sebastien Deleuze + * @author Sam Brannen * @since 19.05.2003 */ -public class BeanUtilsTests { +class BeanUtilsTests { @Test - public void testInstantiateClass() { - // give proper class - BeanUtils.instantiateClass(ArrayList.class); + void testInstantiateClassGivenInterface() { + assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> + BeanUtils.instantiateClass(List.class)); + } - try { - // give interface - BeanUtils.instantiateClass(List.class); - fail("Should have thrown FatalBeanException"); - } - catch (FatalBeanException ex) { - // expected - } + @Test + void testInstantiateClassGivenClassWithoutDefaultConstructor() { + assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> + BeanUtils.instantiateClass(CustomDateEditor.class)); + } - try { - // give class without default constructor - BeanUtils.instantiateClass(CustomDateEditor.class); - fail("Should have thrown FatalBeanException"); - } - catch (FatalBeanException ex) { - // expected - } + @Test // gh-22531 + void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodException { + Constructor ctor = BeanWithNullableTypes.class.getDeclaredConstructor( + Integer.class, Boolean.class, String.class); + BeanWithNullableTypes bean = BeanUtils.instantiateClass(ctor, null, null, "foo"); + assertThat(bean.getCounter()).isNull(); + assertThat(bean.isFlag()).isNull(); + assertThat(bean.getValue()).isEqualTo("foo"); + } + + @Test // gh-22531 + void instantiateClassWithFewerArgsThanParameters() throws NoSuchMethodException { + Constructor constructor = getBeanWithPrimitiveTypesConstructor(); + + assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> + BeanUtils.instantiateClass(constructor, null, null, "foo")); + } + + @Test // gh-22531 + void instantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException { + Constructor constructor = getBeanWithPrimitiveTypesConstructor(); + + assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> + BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, null, "foo", null)); + } + + @Test // gh-22531, gh-27390 + void instantiateClassWithOptionalPrimitiveTypes() throws NoSuchMethodException { + Constructor constructor = getBeanWithPrimitiveTypesConstructor(); + + BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, null, "foo"); + + assertSoftly(softly -> { + softly.assertThat(bean.isFlag()).isFalse(); + softly.assertThat(bean.getByteCount()).isEqualTo((byte) 0); + softly.assertThat(bean.getShortCount()).isEqualTo((short) 0); + softly.assertThat(bean.getIntCount()).isEqualTo(0); + softly.assertThat(bean.getLongCount()).isEqualTo(0L); + softly.assertThat(bean.getFloatCount()).isEqualTo(0F); + softly.assertThat(bean.getDoubleCount()).isEqualTo(0D); + softly.assertThat(bean.getCharacter()).isEqualTo('\0'); + softly.assertThat(bean.getText()).isEqualTo("foo"); + }); + } + + private Constructor getBeanWithPrimitiveTypesConstructor() throws NoSuchMethodException { + return BeanWithPrimitiveTypes.class.getConstructor(boolean.class, byte.class, short.class, int.class, + long.class, float.class, double.class, char.class, String.class); + } + + @Test + void testInstantiatePrivateClassWithPrivateConstructor() throws NoSuchMethodException { + Constructor ctor = PrivateBeanWithPrivateConstructor.class.getDeclaredConstructor(); + BeanUtils.instantiateClass(ctor); } @Test - public void testGetPropertyDescriptors() throws Exception { + void testGetPropertyDescriptors() throws Exception { PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors(); PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class); - assertNotNull("Descriptors should not be null", descriptors); - assertEquals("Invalid number of descriptors returned", actual.length, descriptors.length); + assertThat(descriptors).as("Descriptors should not be null").isNotNull(); + assertThat(descriptors.length).as("Invalid number of descriptors returned").isEqualTo(actual.length); } @Test - public void testBeanPropertyIsArray() { + void testBeanPropertyIsArray() { PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class); for (PropertyDescriptor descriptor : descriptors) { if ("containedBeans".equals(descriptor.getName())) { - assertTrue("Property should be an array", descriptor.getPropertyType().isArray()); - assertEquals(descriptor.getPropertyType().getComponentType(), ContainedBean.class); + assertThat(descriptor.getPropertyType().isArray()).as("Property should be an array").isTrue(); + assertThat(ContainedBean.class).isEqualTo(descriptor.getPropertyType().getComponentType()); } } } @Test - public void testFindEditorByConvention() { - assertEquals(ResourceEditor.class, BeanUtils.findEditorByConvention(Resource.class).getClass()); + void testFindEditorByConvention() { + assertThat(BeanUtils.findEditorByConvention(Resource.class).getClass()).isEqualTo(ResourceEditor.class); } @Test - public void testCopyProperties() throws Exception { + void testCopyProperties() throws Exception { TestBean tb = new TestBean(); tb.setName("rod"); tb.setAge(32); tb.setTouchy("touchy"); TestBean tb2 = new TestBean(); - assertTrue("Name empty", tb2.getName() == null); - assertTrue("Age empty", tb2.getAge() == 0); - assertTrue("Touchy empty", tb2.getTouchy() == null); + assertThat(tb2.getName() == null).as("Name empty").isTrue(); + assertThat(tb2.getAge() == 0).as("Age empty").isTrue(); + assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue(); BeanUtils.copyProperties(tb, tb2); - assertTrue("Name copied", tb2.getName().equals(tb.getName())); - assertTrue("Age copied", tb2.getAge() == tb.getAge()); - assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy())); + assertThat(tb2.getName().equals(tb.getName())).as("Name copied").isTrue(); + assertThat(tb2.getAge() == tb.getAge()).as("Age copied").isTrue(); + assertThat(tb2.getTouchy().equals(tb.getTouchy())).as("Touchy copied").isTrue(); } @Test - public void testCopyPropertiesWithDifferentTypes1() throws Exception { + void testCopyPropertiesWithDifferentTypes1() throws Exception { DerivedTestBean tb = new DerivedTestBean(); tb.setName("rod"); tb.setAge(32); tb.setTouchy("touchy"); TestBean tb2 = new TestBean(); - assertTrue("Name empty", tb2.getName() == null); - assertTrue("Age empty", tb2.getAge() == 0); - assertTrue("Touchy empty", tb2.getTouchy() == null); + assertThat(tb2.getName() == null).as("Name empty").isTrue(); + assertThat(tb2.getAge() == 0).as("Age empty").isTrue(); + assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue(); BeanUtils.copyProperties(tb, tb2); - assertTrue("Name copied", tb2.getName().equals(tb.getName())); - assertTrue("Age copied", tb2.getAge() == tb.getAge()); - assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy())); + assertThat(tb2.getName().equals(tb.getName())).as("Name copied").isTrue(); + assertThat(tb2.getAge() == tb.getAge()).as("Age copied").isTrue(); + assertThat(tb2.getTouchy().equals(tb.getTouchy())).as("Touchy copied").isTrue(); } @Test - public void testCopyPropertiesWithDifferentTypes2() throws Exception { + void testCopyPropertiesWithDifferentTypes2() throws Exception { TestBean tb = new TestBean(); tb.setName("rod"); tb.setAge(32); tb.setTouchy("touchy"); DerivedTestBean tb2 = new DerivedTestBean(); - assertTrue("Name empty", tb2.getName() == null); - assertTrue("Age empty", tb2.getAge() == 0); - assertTrue("Touchy empty", tb2.getTouchy() == null); + assertThat(tb2.getName() == null).as("Name empty").isTrue(); + assertThat(tb2.getAge() == 0).as("Age empty").isTrue(); + assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue(); BeanUtils.copyProperties(tb, tb2); - assertTrue("Name copied", tb2.getName().equals(tb.getName())); - assertTrue("Age copied", tb2.getAge() == tb.getAge()); - assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy())); + assertThat(tb2.getName().equals(tb.getName())).as("Name copied").isTrue(); + assertThat(tb2.getAge() == tb.getAge()).as("Age copied").isTrue(); + assertThat(tb2.getTouchy().equals(tb.getTouchy())).as("Touchy copied").isTrue(); } @Test - public void testCopyPropertiesWithEditable() throws Exception { + void testCopyPropertiesWithEditable() throws Exception { TestBean tb = new TestBean(); - assertTrue("Name empty", tb.getName() == null); + assertThat(tb.getName() == null).as("Name empty").isTrue(); tb.setAge(32); tb.setTouchy("bla"); TestBean tb2 = new TestBean(); tb2.setName("rod"); - assertTrue("Age empty", tb2.getAge() == 0); - assertTrue("Touchy empty", tb2.getTouchy() == null); + assertThat(tb2.getAge() == 0).as("Age empty").isTrue(); + assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue(); // "touchy" should not be copied: it's not defined in ITestBean BeanUtils.copyProperties(tb, tb2, ITestBean.class); - assertTrue("Name copied", tb2.getName() == null); - assertTrue("Age copied", tb2.getAge() == 32); - assertTrue("Touchy still empty", tb2.getTouchy() == null); + assertThat(tb2.getName() == null).as("Name copied").isTrue(); + assertThat(tb2.getAge() == 32).as("Age copied").isTrue(); + assertThat(tb2.getTouchy() == null).as("Touchy still empty").isTrue(); } @Test - public void testCopyPropertiesWithIgnore() throws Exception { + void testCopyPropertiesWithIgnore() throws Exception { TestBean tb = new TestBean(); - assertTrue("Name empty", tb.getName() == null); + assertThat(tb.getName() == null).as("Name empty").isTrue(); tb.setAge(32); tb.setTouchy("bla"); TestBean tb2 = new TestBean(); tb2.setName("rod"); - assertTrue("Age empty", tb2.getAge() == 0); - assertTrue("Touchy empty", tb2.getTouchy() == null); + assertThat(tb2.getAge() == 0).as("Age empty").isTrue(); + assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue(); // "spouse", "touchy", "age" should not be copied BeanUtils.copyProperties(tb, tb2, "spouse", "touchy", "age"); - assertTrue("Name copied", tb2.getName() == null); - assertTrue("Age still empty", tb2.getAge() == 0); - assertTrue("Touchy still empty", tb2.getTouchy() == null); + assertThat(tb2.getName() == null).as("Name copied").isTrue(); + assertThat(tb2.getAge() == 0).as("Age still empty").isTrue(); + assertThat(tb2.getTouchy() == null).as("Touchy still empty").isTrue(); } @Test - public void testCopyPropertiesWithIgnoredNonExistingProperty() { + void testCopyPropertiesWithIgnoredNonExistingProperty() { NameAndSpecialProperty source = new NameAndSpecialProperty(); source.setName("name"); TestBean target = new TestBean(); BeanUtils.copyProperties(source, target, "specialProperty"); - assertEquals(target.getName(), "name"); + assertThat("name").isEqualTo(target.getName()); } @Test - public void testCopyPropertiesWithInvalidProperty() { + void testCopyPropertiesWithInvalidProperty() { InvalidProperty source = new InvalidProperty(); source.setName("name"); source.setFlag1(true); source.setFlag2(true); InvalidProperty target = new InvalidProperty(); BeanUtils.copyProperties(source, target); - assertEquals("name", target.getName()); - assertTrue(target.getFlag1()); - assertTrue(target.getFlag2()); + assertThat(target.getName()).isEqualTo("name"); + assertThat((boolean) target.getFlag1()).isTrue(); + assertThat(target.getFlag2()).isTrue(); } @Test - public void testResolveSimpleSignature() throws Exception { + void testResolveSimpleSignature() throws Exception { Method desiredMethod = MethodSignatureBean.class.getMethod("doSomething"); assertSignatureEquals(desiredMethod, "doSomething"); assertSignatureEquals(desiredMethod, "doSomething()"); } @Test - public void testResolveInvalidSignature() throws Exception { - try { - BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class); - fail("Should not be able to parse with opening but no closing paren."); - } - catch (IllegalArgumentException ex) { - // success - } + void testResolveInvalidSignatureEndParen() { + assertThatIllegalArgumentException().isThrownBy(() -> + BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class)); + } - try { - BeanUtils.resolveSignature("doSomething)", MethodSignatureBean.class); - fail("Should not be able to parse with closing but no opening paren."); - } - catch (IllegalArgumentException ex) { - // success - } + @Test + void testResolveInvalidSignatureStartParen() { + assertThatIllegalArgumentException().isThrownBy(() -> + BeanUtils.resolveSignature("doSomething)", MethodSignatureBean.class)); } @Test - public void testResolveWithAndWithoutArgList() throws Exception { + void testResolveWithAndWithoutArgList() throws Exception { Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", String.class, int.class); assertSignatureEquals(desiredMethod, "doSomethingElse"); - assertNull(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class)); + assertThat(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class)).isNull(); } @Test - public void testResolveTypedSignature() throws Exception { + void testResolveTypedSignature() throws Exception { Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", String.class, int.class); assertSignatureEquals(desiredMethod, "doSomethingElse(java.lang.String, int)"); } @Test - public void testResolveOverloadedSignature() throws Exception { + void testResolveOverloadedSignature() throws Exception { // test resolve with no args Method desiredMethod = MethodSignatureBean.class.getMethod("overloaded"); assertSignatureEquals(desiredMethod, "overloaded()"); @@ -253,7 +304,7 @@ public void testResolveOverloadedSignature() throws Exception { } @Test - public void testResolveSignatureWithArray() throws Exception { + void testResolveSignatureWithArray() throws Exception { Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAnArray", String[].class); assertSignatureEquals(desiredMethod, "doSomethingWithAnArray(java.lang.String[])"); @@ -262,21 +313,53 @@ public void testResolveSignatureWithArray() throws Exception { } @Test - public void testSPR6063() { + void testSPR6063() { PropertyDescriptor[] descrs = BeanUtils.getPropertyDescriptors(Bean.class); PropertyDescriptor keyDescr = BeanUtils.getPropertyDescriptor(Bean.class, "value"); - assertEquals(String.class, keyDescr.getPropertyType()); + assertThat(keyDescr.getPropertyType()).isEqualTo(String.class); for (PropertyDescriptor propertyDescriptor : descrs) { if (propertyDescriptor.getName().equals(keyDescr.getName())) { - assertEquals(propertyDescriptor.getName() + " has unexpected type", - keyDescr.getPropertyType(), propertyDescriptor.getPropertyType()); + assertThat(propertyDescriptor.getPropertyType()).as(propertyDescriptor.getName() + " has unexpected type").isEqualTo(keyDescr.getPropertyType()); } } } + @ParameterizedTest + @ValueSource(classes = { + boolean.class, char.class, byte.class, short.class, int.class, long.class, float.class, double.class, + Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, + DayOfWeek.class, String.class, LocalDateTime.class, Date.class, URI.class, URL.class, Locale.class, Class.class + }) + void isSimpleValueType(Class type) { + assertThat(BeanUtils.isSimpleValueType(type)).as("Type [" + type.getName() + "] should be a simple value type").isTrue(); + } + + @ParameterizedTest + @ValueSource(classes = { int[].class, Object.class, List.class, void.class, Void.class }) + void isNotSimpleValueType(Class type) { + assertThat(BeanUtils.isSimpleValueType(type)).as("Type [" + type.getName() + "] should not be a simple value type").isFalse(); + } + + @ParameterizedTest + @ValueSource(classes = { + boolean.class, char.class, byte.class, short.class, int.class, long.class, float.class, double.class, + Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, + DayOfWeek.class, String.class, LocalDateTime.class, Date.class, URI.class, URL.class, Locale.class, Class.class, + boolean[].class, Boolean[].class, LocalDateTime[].class, Date[].class + }) + void isSimpleProperty(Class type) { + assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should be a simple property").isTrue(); + } + + @ParameterizedTest + @ValueSource(classes = { Object.class, List.class, void.class, Void.class }) + void isNotSimpleProperty(Class type) { + assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should not be a simple property").isFalse(); + } + private void assertSignatureEquals(Method desiredMethod, String signature) { - assertEquals(desiredMethod, BeanUtils.resolveSignature(signature, MethodSignatureBean.class)); + assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod); } @@ -445,16 +528,105 @@ public void setValue(String aValue) { } } - private static class BeanWithSingleNonDefaultConstructor { + private static class BeanWithNullableTypes { - private final String name; + private Integer counter; - public BeanWithSingleNonDefaultConstructor(String name) { - this.name = name; + private Boolean flag; + + private String value; + + @SuppressWarnings("unused") + public BeanWithNullableTypes(@Nullable Integer counter, @Nullable Boolean flag, String value) { + this.counter = counter; + this.flag = flag; + this.value = value; } - public String getName() { - return name; + @Nullable + public Integer getCounter() { + return counter; + } + + @Nullable + public Boolean isFlag() { + return flag; + } + + public String getValue() { + return value; + } + } + + private static class BeanWithPrimitiveTypes { + + private boolean flag; + private byte byteCount; + private short shortCount; + private int intCount; + private long longCount; + private float floatCount; + private double doubleCount; + private char character; + private String text; + + + @SuppressWarnings("unused") + public BeanWithPrimitiveTypes(boolean flag, byte byteCount, short shortCount, int intCount, long longCount, + float floatCount, double doubleCount, char character, String text) { + + this.flag = flag; + this.byteCount = byteCount; + this.shortCount = shortCount; + this.intCount = intCount; + this.longCount = longCount; + this.floatCount = floatCount; + this.doubleCount = doubleCount; + this.character = character; + this.text = text; + } + + public boolean isFlag() { + return flag; + } + + public byte getByteCount() { + return byteCount; + } + + public short getShortCount() { + return shortCount; + } + + public int getIntCount() { + return intCount; + } + + public long getLongCount() { + return longCount; + } + + public float getFloatCount() { + return floatCount; + } + + public double getDoubleCount() { + return doubleCount; + } + + public char getCharacter() { + return character; + } + + public String getText() { + return text; + } + + } + + private static class PrivateBeanWithPrivateConstructor { + + private PrivateBeanWithPrivateConstructor() { } } diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java index ee24536ca5d8..78524e632b0c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -19,15 +19,16 @@ import java.util.List; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Keith Donald * @author Juergen Hoeller + * @author Sam Brannen */ public class BeanWrapperAutoGrowingTests { @@ -36,50 +37,51 @@ public class BeanWrapperAutoGrowingTests { private final BeanWrapperImpl wrapper = new BeanWrapperImpl(bean); - @Before - public void setUp() { + @BeforeEach + public void setup() { wrapper.setAutoGrowNestedPaths(true); } @Test public void getPropertyValueNullValueInNestedPath() { - assertNull(wrapper.getPropertyValue("nested.prop")); + assertThat(wrapper.getPropertyValue("nested.prop")).isNull(); } @Test public void setPropertyValueNullValueInNestedPath() { wrapper.setPropertyValue("nested.prop", "test"); - assertEquals("test", bean.getNested().getProp()); + assertThat(bean.getNested().getProp()).isEqualTo("test"); } - @Test(expected = NullValueInNestedPathException.class) + @Test public void getPropertyValueNullValueInNestedPathNoDefaultConstructor() { - wrapper.getPropertyValue("nestedNoConstructor.prop"); + assertThatExceptionOfType(NullValueInNestedPathException.class).isThrownBy(() -> + wrapper.getPropertyValue("nestedNoConstructor.prop")); } @Test public void getPropertyValueAutoGrowArray() { assertNotNull(wrapper.getPropertyValue("array[0]")); - assertEquals(1, bean.getArray().length); - assertThat(bean.getArray()[0], instanceOf(Bean.class)); + assertThat(bean.getArray().length).isEqualTo(1); + assertThat(bean.getArray()[0]).isInstanceOf(Bean.class); } @Test public void setPropertyValueAutoGrowArray() { wrapper.setPropertyValue("array[0].prop", "test"); - assertEquals("test", bean.getArray()[0].getProp()); + assertThat(bean.getArray()[0].getProp()).isEqualTo("test"); } @Test public void getPropertyValueAutoGrowArrayBySeveralElements() { assertNotNull(wrapper.getPropertyValue("array[4]")); - assertEquals(5, bean.getArray().length); - assertThat(bean.getArray()[0], instanceOf(Bean.class)); - assertThat(bean.getArray()[1], instanceOf(Bean.class)); - assertThat(bean.getArray()[2], instanceOf(Bean.class)); - assertThat(bean.getArray()[3], instanceOf(Bean.class)); - assertThat(bean.getArray()[4], instanceOf(Bean.class)); + assertThat(bean.getArray().length).isEqualTo(5); + assertThat(bean.getArray()[0]).isInstanceOf(Bean.class); + assertThat(bean.getArray()[1]).isInstanceOf(Bean.class); + assertThat(bean.getArray()[2]).isInstanceOf(Bean.class); + assertThat(bean.getArray()[3]).isInstanceOf(Bean.class); + assertThat(bean.getArray()[4]).isInstanceOf(Bean.class); assertNotNull(wrapper.getPropertyValue("array[0]")); assertNotNull(wrapper.getPropertyValue("array[1]")); assertNotNull(wrapper.getPropertyValue("array[2]")); @@ -87,34 +89,61 @@ public void getPropertyValueAutoGrowArrayBySeveralElements() { } @Test - public void getPropertyValueAutoGrowMultiDimensionalArray() { + public void getPropertyValueAutoGrow2dArray() { assertNotNull(wrapper.getPropertyValue("multiArray[0][0]")); - assertEquals(1, bean.getMultiArray()[0].length); - assertThat(bean.getMultiArray()[0][0], instanceOf(Bean.class)); + assertThat(bean.getMultiArray()[0].length).isEqualTo(1); + assertThat(bean.getMultiArray()[0][0]).isInstanceOf(Bean.class); + } + + @Test + public void getPropertyValueAutoGrow3dArray() { + assertNotNull(wrapper.getPropertyValue("threeDimensionalArray[1][2][3]")); + assertThat(bean.getThreeDimensionalArray()[1].length).isEqualTo(3); + assertThat(bean.getThreeDimensionalArray()[1][2][3]).isInstanceOf(Bean.class); + } + + @Test + public void setPropertyValueAutoGrow2dArray() { + Bean newBean = new Bean(); + newBean.setProp("enigma"); + wrapper.setPropertyValue("multiArray[2][3]", newBean); + assertThat(bean.getMultiArray()[2][3]) + .isInstanceOf(Bean.class) + .extracting(Bean::getProp).isEqualTo("enigma"); + } + + @Test + public void setPropertyValueAutoGrow3dArray() { + Bean newBean = new Bean(); + newBean.setProp("enigma"); + wrapper.setPropertyValue("threeDimensionalArray[2][3][4]", newBean); + assertThat(bean.getThreeDimensionalArray()[2][3][4]) + .isInstanceOf(Bean.class) + .extracting(Bean::getProp).isEqualTo("enigma"); } @Test public void getPropertyValueAutoGrowList() { assertNotNull(wrapper.getPropertyValue("list[0]")); - assertEquals(1, bean.getList().size()); - assertThat(bean.getList().get(0), instanceOf(Bean.class)); + assertThat(bean.getList().size()).isEqualTo(1); + assertThat(bean.getList().get(0)).isInstanceOf(Bean.class); } @Test public void setPropertyValueAutoGrowList() { wrapper.setPropertyValue("list[0].prop", "test"); - assertEquals("test", bean.getList().get(0).getProp()); + assertThat(bean.getList().get(0).getProp()).isEqualTo("test"); } @Test public void getPropertyValueAutoGrowListBySeveralElements() { assertNotNull(wrapper.getPropertyValue("list[4]")); - assertEquals(5, bean.getList().size()); - assertThat(bean.getList().get(0), instanceOf(Bean.class)); - assertThat(bean.getList().get(1), instanceOf(Bean.class)); - assertThat(bean.getList().get(2), instanceOf(Bean.class)); - assertThat(bean.getList().get(3), instanceOf(Bean.class)); - assertThat(bean.getList().get(4), instanceOf(Bean.class)); + assertThat(bean.getList().size()).isEqualTo(5); + assertThat(bean.getList().get(0)).isInstanceOf(Bean.class); + assertThat(bean.getList().get(1)).isInstanceOf(Bean.class); + assertThat(bean.getList().get(2)).isInstanceOf(Bean.class); + assertThat(bean.getList().get(3)).isInstanceOf(Bean.class); + assertThat(bean.getList().get(4)).isInstanceOf(Bean.class); assertNotNull(wrapper.getPropertyValue("list[0]")); assertNotNull(wrapper.getPropertyValue("list[1]")); assertNotNull(wrapper.getPropertyValue("list[2]")); @@ -124,41 +153,43 @@ public void getPropertyValueAutoGrowListBySeveralElements() { @Test public void getPropertyValueAutoGrowListFailsAgainstLimit() { wrapper.setAutoGrowCollectionLimit(2); - try { - assertNotNull(wrapper.getPropertyValue("list[4]")); - fail("Should have thrown InvalidPropertyException"); - } - catch (InvalidPropertyException ex) { - // expected - assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException); - } + assertThatExceptionOfType(InvalidPropertyException.class).isThrownBy(() -> + wrapper.getPropertyValue("list[4]")) + .withRootCauseInstanceOf(IndexOutOfBoundsException.class); } @Test public void getPropertyValueAutoGrowMultiDimensionalList() { assertNotNull(wrapper.getPropertyValue("multiList[0][0]")); - assertEquals(1, bean.getMultiList().get(0).size()); - assertThat(bean.getMultiList().get(0).get(0), instanceOf(Bean.class)); + assertThat(bean.getMultiList().get(0).size()).isEqualTo(1); + assertThat(bean.getMultiList().get(0).get(0)).isInstanceOf(Bean.class); } - @Test(expected = InvalidPropertyException.class) + @Test public void getPropertyValueAutoGrowListNotParameterized() { - wrapper.getPropertyValue("listNotParameterized[0]"); + assertThatExceptionOfType(InvalidPropertyException.class).isThrownBy(() -> + wrapper.getPropertyValue("listNotParameterized[0]")); } @Test public void setPropertyValueAutoGrowMap() { wrapper.setPropertyValue("map[A]", new Bean()); - assertThat(bean.getMap().get("A"), instanceOf(Bean.class)); + assertThat(bean.getMap().get("A")).isInstanceOf(Bean.class); } @Test public void setNestedPropertyValueAutoGrowMap() { wrapper.setPropertyValue("map[A].nested", new Bean()); - assertThat(bean.getMap().get("A").getNested(), instanceOf(Bean.class)); + assertThat(bean.getMap().get("A").getNested()).isInstanceOf(Bean.class); + } + + + private static void assertNotNull(Object propertyValue) { + assertThat(propertyValue).isNotNull(); } + @SuppressWarnings("rawtypes") public static class Bean { private String prop; @@ -171,6 +202,8 @@ public static class Bean { private Bean[][] multiArray; + private Bean[][][] threeDimensionalArray; + private List list; private List> multiList; @@ -211,6 +244,14 @@ public void setMultiArray(Bean[][] multiArray) { this.multiArray = multiArray; } + public Bean[][][] getThreeDimensionalArray() { + return threeDimensionalArray; + } + + public void setThreeDimensionalArray(Bean[][][] threeDimensionalArray) { + this.threeDimensionalArray = threeDimensionalArray; + } + public List getList() { return list; } diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java index f5c87cdf9db7..3a5ac3717127 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -19,13 +19,13 @@ import java.util.LinkedHashMap; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.testfixture.beans.CustomEnum; +import org.springframework.beans.testfixture.beans.GenericBean; import org.springframework.core.convert.support.DefaultConversionService; -import org.springframework.tests.sample.beans.CustomEnum; -import org.springframework.tests.sample.beans.GenericBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -38,7 +38,7 @@ public void testCustomEnum() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnum", "VALUE_1"); - assertEquals(CustomEnum.VALUE_1, gb.getCustomEnum()); + assertThat(gb.getCustomEnum()).isEqualTo(CustomEnum.VALUE_1); } @Test @@ -46,7 +46,7 @@ public void testCustomEnumWithNull() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnum", null); - assertEquals(null, gb.getCustomEnum()); + assertThat(gb.getCustomEnum()).isEqualTo(null); } @Test @@ -54,7 +54,7 @@ public void testCustomEnumWithEmptyString() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnum", ""); - assertEquals(null, gb.getCustomEnum()); + assertThat(gb.getCustomEnum()).isEqualTo(null); } @Test @@ -62,8 +62,8 @@ public void testCustomEnumArrayWithSingleValue() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnumArray", "VALUE_1"); - assertEquals(1, gb.getCustomEnumArray().length); - assertEquals(CustomEnum.VALUE_1, gb.getCustomEnumArray()[0]); + assertThat(gb.getCustomEnumArray().length).isEqualTo(1); + assertThat(gb.getCustomEnumArray()[0]).isEqualTo(CustomEnum.VALUE_1); } @Test @@ -71,9 +71,9 @@ public void testCustomEnumArrayWithMultipleValues() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnumArray", new String[] {"VALUE_1", "VALUE_2"}); - assertEquals(2, gb.getCustomEnumArray().length); - assertEquals(CustomEnum.VALUE_1, gb.getCustomEnumArray()[0]); - assertEquals(CustomEnum.VALUE_2, gb.getCustomEnumArray()[1]); + assertThat(gb.getCustomEnumArray().length).isEqualTo(2); + assertThat(gb.getCustomEnumArray()[0]).isEqualTo(CustomEnum.VALUE_1); + assertThat(gb.getCustomEnumArray()[1]).isEqualTo(CustomEnum.VALUE_2); } @Test @@ -81,9 +81,9 @@ public void testCustomEnumArrayWithMultipleValuesAsCsv() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnumArray", "VALUE_1,VALUE_2"); - assertEquals(2, gb.getCustomEnumArray().length); - assertEquals(CustomEnum.VALUE_1, gb.getCustomEnumArray()[0]); - assertEquals(CustomEnum.VALUE_2, gb.getCustomEnumArray()[1]); + assertThat(gb.getCustomEnumArray().length).isEqualTo(2); + assertThat(gb.getCustomEnumArray()[0]).isEqualTo(CustomEnum.VALUE_1); + assertThat(gb.getCustomEnumArray()[1]).isEqualTo(CustomEnum.VALUE_2); } @Test @@ -91,8 +91,8 @@ public void testCustomEnumSetWithSingleValue() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnumSet", "VALUE_1"); - assertEquals(1, gb.getCustomEnumSet().size()); - assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)); + assertThat(gb.getCustomEnumSet().size()).isEqualTo(1); + assertThat(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)).isTrue(); } @Test @@ -100,9 +100,9 @@ public void testCustomEnumSetWithMultipleValues() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnumSet", new String[] {"VALUE_1", "VALUE_2"}); - assertEquals(2, gb.getCustomEnumSet().size()); - assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)); - assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)); + assertThat(gb.getCustomEnumSet().size()).isEqualTo(2); + assertThat(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)).isTrue(); + assertThat(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)).isTrue(); } @Test @@ -110,9 +110,9 @@ public void testCustomEnumSetWithMultipleValuesAsCsv() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnumSet", "VALUE_1,VALUE_2"); - assertEquals(2, gb.getCustomEnumSet().size()); - assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)); - assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)); + assertThat(gb.getCustomEnumSet().size()).isEqualTo(2); + assertThat(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)).isTrue(); + assertThat(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)).isTrue(); } @Test @@ -120,9 +120,9 @@ public void testCustomEnumSetWithGetterSetterMismatch() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("customEnumSetMismatch", new String[] {"VALUE_1", "VALUE_2"}); - assertEquals(2, gb.getCustomEnumSet().size()); - assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)); - assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)); + assertThat(gb.getCustomEnumSet().size()).isEqualTo(2); + assertThat(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)).isTrue(); + assertThat(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)).isTrue(); } @Test @@ -130,11 +130,11 @@ public void testStandardEnumSetWithMultipleValues() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setConversionService(new DefaultConversionService()); - assertNull(gb.getStandardEnumSet()); + assertThat(gb.getStandardEnumSet()).isNull(); bw.setPropertyValue("standardEnumSet", new String[] {"VALUE_1", "VALUE_2"}); - assertEquals(2, gb.getStandardEnumSet().size()); - assertTrue(gb.getStandardEnumSet().contains(CustomEnum.VALUE_1)); - assertTrue(gb.getStandardEnumSet().contains(CustomEnum.VALUE_2)); + assertThat(gb.getStandardEnumSet().size()).isEqualTo(2); + assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_1)).isTrue(); + assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_2)).isTrue(); } @Test @@ -142,9 +142,9 @@ public void testStandardEnumSetWithAutoGrowing() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setAutoGrowNestedPaths(true); - assertNull(gb.getStandardEnumSet()); + assertThat(gb.getStandardEnumSet()).isNull(); bw.getPropertyValue("standardEnumSet.class"); - assertEquals(0, gb.getStandardEnumSet().size()); + assertThat(gb.getStandardEnumSet().size()).isEqualTo(0); } @Test @@ -152,14 +152,14 @@ public void testStandardEnumMapWithMultipleValues() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setConversionService(new DefaultConversionService()); - assertNull(gb.getStandardEnumMap()); + assertThat(gb.getStandardEnumMap()).isNull(); Map map = new LinkedHashMap<>(); map.put("VALUE_1", 1); map.put("VALUE_2", 2); bw.setPropertyValue("standardEnumMap", map); - assertEquals(2, gb.getStandardEnumMap().size()); - assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1)); - assertEquals(new Integer(2), gb.getStandardEnumMap().get(CustomEnum.VALUE_2)); + assertThat(gb.getStandardEnumMap().size()).isEqualTo(2); + assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_1)).isEqualTo(1); + assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_2)).isEqualTo(2); } @Test @@ -167,10 +167,10 @@ public void testStandardEnumMapWithAutoGrowing() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setAutoGrowNestedPaths(true); - assertNull(gb.getStandardEnumMap()); + assertThat(gb.getStandardEnumMap()).isNull(); bw.setPropertyValue("standardEnumMap[VALUE_1]", 1); - assertEquals(1, gb.getStandardEnumMap().size()); - assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1)); + assertThat(gb.getStandardEnumMap().size()).isEqualTo(1); + assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_1)).isEqualTo(1); } @Test @@ -178,7 +178,7 @@ public void testNonPublicEnum() { NonPublicEnumHolder holder = new NonPublicEnumHolder(); BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("nonPublicEnum", "VALUE_1"); - assertEquals(NonPublicEnum.VALUE_1, holder.getNonPublicEnum()); + assertThat(holder.getNonPublicEnum()).isEqualTo(NonPublicEnum.VALUE_1); } diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java index ae56737a86dd..01dfc0674a56 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -16,7 +16,6 @@ package org.springframework.beans; -import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -29,17 +28,18 @@ import java.util.Properties; import java.util.Set; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor; +import org.springframework.beans.testfixture.beans.GenericBean; +import org.springframework.beans.testfixture.beans.GenericIntegerBean; +import org.springframework.beans.testfixture.beans.GenericSetOfIntegerBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.UrlResource; -import org.springframework.tests.sample.beans.GenericBean; -import org.springframework.tests.sample.beans.GenericIntegerBean; -import org.springframework.tests.sample.beans.GenericSetOfIntegerBean; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Juergen Hoeller @@ -56,8 +56,8 @@ public void testGenericSet() { input.add("4"); input.add("5"); bw.setPropertyValue("integerSet", input); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); } @Test @@ -69,8 +69,8 @@ public void testGenericLowerBoundedSet() { input.add("4"); input.add("5"); bw.setPropertyValue("numberSet", input); - assertTrue(gb.getNumberSet().contains(new Integer(4))); - assertTrue(gb.getNumberSet().contains(new Integer(5))); + assertThat(gb.getNumberSet().contains(4)).isTrue(); + assertThat(gb.getNumberSet().contains(5)).isTrue(); } @Test @@ -79,34 +79,30 @@ public void testGenericSetWithConversionFailure() { BeanWrapper bw = new BeanWrapperImpl(gb); Set input = new HashSet<>(); input.add(new TestBean()); - try { - bw.setPropertyValue("integerSet", input); - fail("Should have thrown TypeMismatchException"); - } - catch (TypeMismatchException ex) { - assertTrue(ex.getMessage().indexOf("java.lang.Integer") != -1); - } + assertThatExceptionOfType(TypeMismatchException.class).isThrownBy(() -> + bw.setPropertyValue("integerSet", input)) + .withMessageContaining("java.lang.Integer"); } @Test - public void testGenericList() throws MalformedURLException { + public void testGenericList() throws Exception { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); List input = new ArrayList<>(); input.add("http://localhost:8080"); input.add("http://localhost:9090"); bw.setPropertyValue("resourceList", input); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); - assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(gb.getResourceList().get(1)).isEqualTo(new UrlResource("http://localhost:9090")); } @Test - public void testGenericListElement() throws MalformedURLException { + public void testGenericListElement() throws Exception { GenericBean gb = new GenericBean<>(); gb.setResourceList(new ArrayList<>()); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("resourceList[0]", "http://localhost:8080"); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); } @Test @@ -117,8 +113,8 @@ public void testGenericMap() { input.put("4", "5"); input.put("6", "7"); bw.setPropertyValue("shortMap", input); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); } @Test @@ -127,8 +123,8 @@ public void testGenericMapElement() { gb.setShortMap(new HashMap<>()); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("shortMap[4]", "5"); - assertEquals(new Integer(5), bw.getPropertyValue("shortMap[4]")); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); + assertThat(bw.getPropertyValue("shortMap[4]")).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); } @Test @@ -139,8 +135,8 @@ public void testGenericMapWithKeyType() { input.put("4", "5"); input.put("6", "7"); bw.setPropertyValue("longMap", input); - assertEquals("5", gb.getLongMap().get(new Long("4"))); - assertEquals("7", gb.getLongMap().get(new Long("6"))); + assertThat(gb.getLongMap().get(4L)).isEqualTo("5"); + assertThat(gb.getLongMap().get(6L)).isEqualTo("7"); } @Test @@ -149,8 +145,8 @@ public void testGenericMapElementWithKeyType() { gb.setLongMap(new HashMap()); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("longMap[4]", "5"); - assertEquals("5", gb.getLongMap().get(new Long("4"))); - assertEquals("5", bw.getPropertyValue("longMap[4]")); + assertThat(gb.getLongMap().get(new Long("4"))).isEqualTo("5"); + assertThat(bw.getPropertyValue("longMap[4]")).isEqualTo("5"); } @Test @@ -158,16 +154,16 @@ public void testGenericMapWithCollectionValue() { GenericBean gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); - Map input = new HashMap<>(); + Map> input = new HashMap<>(); HashSet value1 = new HashSet<>(); - value1.add(new Integer(1)); + value1.add(1); input.put("1", value1); ArrayList value2 = new ArrayList<>(); value2.add(Boolean.TRUE); input.put("2", value2); bw.setPropertyValue("collectionMap", input); - assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet); - assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList); + assertThat(gb.getCollectionMap().get(1) instanceof HashSet).isTrue(); + assertThat(gb.getCollectionMap().get(2) instanceof ArrayList).isTrue(); } @Test @@ -177,9 +173,9 @@ public void testGenericMapElementWithCollectionValue() { BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); HashSet value1 = new HashSet<>(); - value1.add(new Integer(1)); + value1.add(1); bw.setPropertyValue("collectionMap[1]", value1); - assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet); + assertThat(gb.getCollectionMap().get(1) instanceof HashSet).isTrue(); } @Test @@ -190,48 +186,48 @@ public void testGenericMapFromProperties() { input.setProperty("4", "5"); input.setProperty("6", "7"); bw.setPropertyValue("shortMap", input); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); } @Test - public void testGenericListOfLists() throws MalformedURLException { + public void testGenericListOfLists() { GenericBean gb = new GenericBean<>(); List> list = new LinkedList<>(); list.add(new LinkedList<>()); gb.setListOfLists(list); BeanWrapper bw = new BeanWrapperImpl(gb); - bw.setPropertyValue("listOfLists[0][0]", new Integer(5)); - assertEquals(new Integer(5), bw.getPropertyValue("listOfLists[0][0]")); - assertEquals(new Integer(5), gb.getListOfLists().get(0).get(0)); + bw.setPropertyValue("listOfLists[0][0]", 5); + assertThat(bw.getPropertyValue("listOfLists[0][0]")).isEqualTo(5); + assertThat(gb.getListOfLists().get(0).get(0)).isEqualTo(5); } @Test - public void testGenericListOfListsWithElementConversion() throws MalformedURLException { + public void testGenericListOfListsWithElementConversion() { GenericBean gb = new GenericBean<>(); List> list = new LinkedList<>(); list.add(new LinkedList<>()); gb.setListOfLists(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("listOfLists[0][0]", "5"); - assertEquals(new Integer(5), bw.getPropertyValue("listOfLists[0][0]")); - assertEquals(new Integer(5), gb.getListOfLists().get(0).get(0)); + assertThat(bw.getPropertyValue("listOfLists[0][0]")).isEqualTo(5); + assertThat(gb.getListOfLists().get(0).get(0)).isEqualTo(5); } @Test - public void testGenericListOfArrays() throws MalformedURLException { + public void testGenericListOfArrays() { GenericBean gb = new GenericBean<>(); ArrayList list = new ArrayList<>(); list.add(new String[] {"str1", "str2"}); gb.setListOfArrays(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("listOfArrays[0][1]", "str3 "); - assertEquals("str3 ", bw.getPropertyValue("listOfArrays[0][1]")); - assertEquals("str3 ", gb.getListOfArrays().get(0)[1]); + assertThat(bw.getPropertyValue("listOfArrays[0][1]")).isEqualTo("str3 "); + assertThat(gb.getListOfArrays().get(0)[1]).isEqualTo("str3 "); } @Test - public void testGenericListOfArraysWithElementConversion() throws MalformedURLException { + public void testGenericListOfArraysWithElementConversion() { GenericBean gb = new GenericBean<>(); ArrayList list = new ArrayList<>(); list.add(new String[] {"str1", "str2"}); @@ -239,84 +235,84 @@ public void testGenericListOfArraysWithElementConversion() throws MalformedURLEx BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(String.class, new StringTrimmerEditor(false)); bw.setPropertyValue("listOfArrays[0][1]", "str3 "); - assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]")); - assertEquals("str3", gb.getListOfArrays().get(0)[1]); + assertThat(bw.getPropertyValue("listOfArrays[0][1]")).isEqualTo("str3"); + assertThat(gb.getListOfArrays().get(0)[1]).isEqualTo("str3"); } @Test - public void testGenericListOfMaps() throws MalformedURLException { + public void testGenericListOfMaps() { GenericBean gb = new GenericBean<>(); List> list = new LinkedList<>(); list.add(new HashMap<>()); gb.setListOfMaps(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("listOfMaps[0][10]", new Long(5)); - assertEquals(new Long(5), bw.getPropertyValue("listOfMaps[0][10]")); - assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10)); + assertThat(bw.getPropertyValue("listOfMaps[0][10]")).isEqualTo(new Long(5)); + assertThat(gb.getListOfMaps().get(0).get(10)).isEqualTo(new Long(5)); } @Test - public void testGenericListOfMapsWithElementConversion() throws MalformedURLException { + public void testGenericListOfMapsWithElementConversion() { GenericBean gb = new GenericBean<>(); List> list = new LinkedList<>(); list.add(new HashMap<>()); gb.setListOfMaps(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("listOfMaps[0][10]", "5"); - assertEquals(new Long(5), bw.getPropertyValue("listOfMaps[0][10]")); - assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10)); + assertThat(bw.getPropertyValue("listOfMaps[0][10]")).isEqualTo(new Long(5)); + assertThat(gb.getListOfMaps().get(0).get(10)).isEqualTo(new Long(5)); } @Test - public void testGenericMapOfMaps() throws MalformedURLException { + public void testGenericMapOfMaps() { GenericBean gb = new GenericBean<>(); Map> map = new HashMap<>(); map.put("mykey", new HashMap<>()); gb.setMapOfMaps(map); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("mapOfMaps[mykey][10]", new Long(5)); - assertEquals(new Long(5), bw.getPropertyValue("mapOfMaps[mykey][10]")); - assertEquals(new Long(5), gb.getMapOfMaps().get("mykey").get(10)); + assertThat(bw.getPropertyValue("mapOfMaps[mykey][10]")).isEqualTo(new Long(5)); + assertThat(gb.getMapOfMaps().get("mykey").get(10)).isEqualTo(new Long(5)); } @Test - public void testGenericMapOfMapsWithElementConversion() throws MalformedURLException { + public void testGenericMapOfMapsWithElementConversion() { GenericBean gb = new GenericBean<>(); Map> map = new HashMap<>(); map.put("mykey", new HashMap<>()); gb.setMapOfMaps(map); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("mapOfMaps[mykey][10]", "5"); - assertEquals(new Long(5), bw.getPropertyValue("mapOfMaps[mykey][10]")); - assertEquals(new Long(5), gb.getMapOfMaps().get("mykey").get(10)); + assertThat(bw.getPropertyValue("mapOfMaps[mykey][10]")).isEqualTo(new Long(5)); + assertThat(gb.getMapOfMaps().get("mykey").get(10)).isEqualTo(new Long(5)); } @Test - public void testGenericMapOfLists() throws MalformedURLException { + public void testGenericMapOfLists() { GenericBean gb = new GenericBean<>(); Map> map = new HashMap<>(); - map.put(new Integer(1), new LinkedList<>()); + map.put(1, new LinkedList<>()); gb.setMapOfLists(map); BeanWrapper bw = new BeanWrapperImpl(gb); - bw.setPropertyValue("mapOfLists[1][0]", new Integer(5)); - assertEquals(new Integer(5), bw.getPropertyValue("mapOfLists[1][0]")); - assertEquals(new Integer(5), gb.getMapOfLists().get(new Integer(1)).get(0)); + bw.setPropertyValue("mapOfLists[1][0]", 5); + assertThat(bw.getPropertyValue("mapOfLists[1][0]")).isEqualTo(5); + assertThat(gb.getMapOfLists().get(1).get(0)).isEqualTo(5); } @Test - public void testGenericMapOfListsWithElementConversion() throws MalformedURLException { + public void testGenericMapOfListsWithElementConversion() { GenericBean gb = new GenericBean<>(); Map> map = new HashMap<>(); - map.put(new Integer(1), new LinkedList<>()); + map.put(1, new LinkedList<>()); gb.setMapOfLists(map); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("mapOfLists[1][0]", "5"); - assertEquals(new Integer(5), bw.getPropertyValue("mapOfLists[1][0]")); - assertEquals(new Integer(5), gb.getMapOfLists().get(new Integer(1)).get(0)); + assertThat(bw.getPropertyValue("mapOfLists[1][0]")).isEqualTo(5); + assertThat(gb.getMapOfLists().get(1).get(0)).isEqualTo(5); } @Test - public void testGenericTypeNestingMapOfInteger() throws Exception { + public void testGenericTypeNestingMapOfInteger() { Map map = new HashMap<>(); map.put("testKey", "100"); @@ -325,13 +321,13 @@ public void testGenericTypeNestingMapOfInteger() throws Exception { bw.setPropertyValue("mapOfInteger", map); Object obj = gb.getMapOfInteger().get("testKey"); - assertTrue(obj instanceof Integer); + assertThat(obj instanceof Integer).isTrue(); } @Test - public void testGenericTypeNestingMapOfListOfInteger() throws Exception { + public void testGenericTypeNestingMapOfListOfInteger() { Map> map = new HashMap<>(); - List list = Arrays.asList(new String[] {"1", "2", "3"}); + List list = Arrays.asList("1", "2", "3"); map.put("testKey", list); NestedGenericCollectionBean gb = new NestedGenericCollectionBean(); @@ -339,12 +335,12 @@ public void testGenericTypeNestingMapOfListOfInteger() throws Exception { bw.setPropertyValue("mapOfListOfInteger", map); Object obj = gb.getMapOfListOfInteger().get("testKey").get(0); - assertTrue(obj instanceof Integer); - assertEquals(1, ((Integer) obj).intValue()); + assertThat(obj instanceof Integer).isTrue(); + assertThat(((Integer) obj).intValue()).isEqualTo(1); } @Test - public void testGenericTypeNestingListOfMapOfInteger() throws Exception { + public void testGenericTypeNestingListOfMapOfInteger() { List> list = new LinkedList<>(); Map map = new HashMap<>(); map.put("testKey", "5"); @@ -355,14 +351,14 @@ public void testGenericTypeNestingListOfMapOfInteger() throws Exception { bw.setPropertyValue("listOfMapOfInteger", list); Object obj = gb.getListOfMapOfInteger().get(0).get("testKey"); - assertTrue(obj instanceof Integer); - assertEquals(5, ((Integer) obj).intValue()); + assertThat(obj instanceof Integer).isTrue(); + assertThat(((Integer) obj).intValue()).isEqualTo(5); } @Test - public void testGenericTypeNestingMapOfListOfListOfInteger() throws Exception { + public void testGenericTypeNestingMapOfListOfListOfInteger() { Map>> map = new HashMap<>(); - List list = Arrays.asList(new String[] {"1", "2", "3"}); + List list = Arrays.asList("1", "2", "3"); map.put("testKey", Collections.singletonList(list)); NestedGenericCollectionBean gb = new NestedGenericCollectionBean(); @@ -370,8 +366,8 @@ public void testGenericTypeNestingMapOfListOfListOfInteger() throws Exception { bw.setPropertyValue("mapOfListOfListOfInteger", map); Object obj = gb.getMapOfListOfListOfInteger().get("testKey").get(0).get(0); - assertTrue(obj instanceof Integer); - assertEquals(1, ((Integer) obj).intValue()); + assertThat(obj instanceof Integer).isTrue(); + assertThat(((Integer) obj).intValue()).isEqualTo(1); } @Test @@ -387,8 +383,8 @@ public void testComplexGenericMap() { BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("genericMap", inputMap); - assertEquals(new Integer(1), holder.getGenericMap().keySet().iterator().next().get(0)); - assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0)); + assertThat(holder.getGenericMap().keySet().iterator().next().get(0)).isEqualTo(1); + assertThat(holder.getGenericMap().values().iterator().next().get(0)).isEqualTo(new Long(10)); } @Test @@ -404,8 +400,8 @@ public void testComplexGenericMapWithCollectionConversion() { BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("genericMap", inputMap); - assertEquals(new Integer(1), holder.getGenericMap().keySet().iterator().next().get(0)); - assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0)); + assertThat(holder.getGenericMap().keySet().iterator().next().get(0)).isEqualTo(1); + assertThat(holder.getGenericMap().values().iterator().next().get(0)).isEqualTo(new Long(10)); } @Test @@ -417,8 +413,8 @@ public void testComplexGenericIndexedMapEntry() { BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("genericIndexedMap[1]", inputValue); - assertEquals(new Integer(1), holder.getGenericIndexedMap().keySet().iterator().next()); - assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0)); + assertThat(holder.getGenericIndexedMap().keySet().iterator().next()).isEqualTo(1); + assertThat(holder.getGenericIndexedMap().values().iterator().next().get(0)).isEqualTo(new Long(10)); } @Test @@ -430,8 +426,8 @@ public void testComplexGenericIndexedMapEntryWithCollectionConversion() { BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("genericIndexedMap[1]", inputValue); - assertEquals(new Integer(1), holder.getGenericIndexedMap().keySet().iterator().next()); - assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0)); + assertThat(holder.getGenericIndexedMap().keySet().iterator().next()).isEqualTo(1); + assertThat(holder.getGenericIndexedMap().values().iterator().next().get(0)).isEqualTo(new Long(10)); } @Test @@ -443,8 +439,8 @@ public void testComplexDerivedIndexedMapEntry() { BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("derivedIndexedMap[1]", inputValue); - assertEquals(new Integer(1), holder.getDerivedIndexedMap().keySet().iterator().next()); - assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0)); + assertThat(holder.getDerivedIndexedMap().keySet().iterator().next()).isEqualTo(1); + assertThat(holder.getDerivedIndexedMap().values().iterator().next().get(0)).isEqualTo(new Long(10)); } @Test @@ -456,30 +452,30 @@ public void testComplexDerivedIndexedMapEntryWithCollectionConversion() { BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("derivedIndexedMap[1]", inputValue); - assertEquals(new Integer(1), holder.getDerivedIndexedMap().keySet().iterator().next()); - assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0)); + assertThat(holder.getDerivedIndexedMap().keySet().iterator().next()).isEqualTo(1); + assertThat(holder.getDerivedIndexedMap().values().iterator().next().get(0)).isEqualTo(new Long(10)); } @Test - public void testGenericallyTypedIntegerBean() throws Exception { + public void testGenericallyTypedIntegerBean() { GenericIntegerBean gb = new GenericIntegerBean(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("genericProperty", "10"); bw.setPropertyValue("genericListProperty", new String[] {"20", "30"}); - assertEquals(new Integer(10), gb.getGenericProperty()); - assertEquals(new Integer(20), gb.getGenericListProperty().get(0)); - assertEquals(new Integer(30), gb.getGenericListProperty().get(1)); + assertThat(gb.getGenericProperty()).isEqualTo(10); + assertThat(gb.getGenericListProperty().get(0)).isEqualTo(20); + assertThat(gb.getGenericListProperty().get(1)).isEqualTo(30); } @Test - public void testGenericallyTypedSetOfIntegerBean() throws Exception { + public void testGenericallyTypedSetOfIntegerBean() { GenericSetOfIntegerBean gb = new GenericSetOfIntegerBean(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("genericProperty", "10"); bw.setPropertyValue("genericListProperty", new String[] {"20", "30"}); - assertEquals(new Integer(10), gb.getGenericProperty().iterator().next()); - assertEquals(new Integer(20), gb.getGenericListProperty().get(0).iterator().next()); - assertEquals(new Integer(30), gb.getGenericListProperty().get(1).iterator().next()); + assertThat(gb.getGenericProperty().iterator().next()).isEqualTo(10); + assertThat(gb.getGenericListProperty().get(0).iterator().next()).isEqualTo(20); + assertThat(gb.getGenericListProperty().get(1).iterator().next()).isEqualTo(30); } @Test @@ -487,7 +483,7 @@ public void testSettingGenericPropertyWithReadOnlyInterface() { Bar bar = new Bar(); BeanWrapper bw = new BeanWrapperImpl(bar); bw.setPropertyValue("version", "10"); - assertEquals(new Double(10.0), bar.getVersion()); + assertThat(bar.getVersion()).isEqualTo(new Double(10.0)); } @Test @@ -495,7 +491,7 @@ public void testSettingLongPropertyWithGenericInterface() { Promotion bean = new Promotion(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.setPropertyValue("id", "10"); - assertEquals(new Long(10), bean.getId()); + assertThat(bean.getId()).isEqualTo(new Long(10)); } @Test @@ -505,6 +501,7 @@ class Holder { public Holder(D data) { this.data = data; } + @SuppressWarnings("unused") public D getData() { return this.data; } @@ -515,10 +512,10 @@ public D getData() { Holder> context = new Holder<>(data); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(context); - assertEquals("y", bw.getPropertyValue("data['x']")); + assertThat(bw.getPropertyValue("data['x']")).isEqualTo("y"); bw.setPropertyValue("data['message']", "it works!"); - assertEquals("it works!", data.get("message")); + assertThat(data.get("message")).isEqualTo("it works!"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java index c4251bbc7885..0711189b5f59 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,11 +20,12 @@ import java.util.Map; import java.util.Optional; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Specific {@link BeanWrapperImpl} tests. @@ -49,8 +50,8 @@ public void setterDoesNotCallGetter() { GetterBean target = new GetterBean(); BeanWrapper accessor = createAccessor(target); accessor.setPropertyValue("name", "tom"); - assertEquals("tom", target.getAliasedName()); - assertEquals("tom", accessor.getPropertyValue("aliasedName")); + assertThat(target.getAliasedName()).isEqualTo("tom"); + assertThat(accessor.getPropertyValue("aliasedName")).isEqualTo("tom"); } @Test @@ -59,8 +60,8 @@ public void getterSilentlyFailWithOldValueExtraction() { BeanWrapper accessor = createAccessor(target); accessor.setExtractOldValueForEditor(true); // This will call the getter accessor.setPropertyValue("name", "tom"); - assertEquals("tom", target.getAliasedName()); - assertEquals("tom", accessor.getPropertyValue("aliasedName")); + assertThat(target.getAliasedName()).isEqualTo("tom"); + assertThat(accessor.getPropertyValue("aliasedName")).isEqualTo("tom"); } @Test @@ -68,8 +69,8 @@ public void aliasedSetterThroughDefaultMethod() { GetterBean target = new GetterBean(); BeanWrapper accessor = createAccessor(target); accessor.setPropertyValue("aliasedName", "tom"); - assertEquals("tom", target.getAliasedName()); - assertEquals("tom", accessor.getPropertyValue("aliasedName")); + assertThat(target.getAliasedName()).isEqualTo("tom"); + assertThat(accessor.getPropertyValue("aliasedName")).isEqualTo("tom"); } @Test @@ -77,38 +78,30 @@ public void setValidAndInvalidPropertyValuesShouldContainExceptionDetails() { TestBean target = new TestBean(); String newName = "tony"; String invalidTouchy = ".valid"; - try { - BeanWrapper accessor = createAccessor(target); - MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.addPropertyValue(new PropertyValue("age", "foobar")); - pvs.addPropertyValue(new PropertyValue("name", newName)); - pvs.addPropertyValue(new PropertyValue("touchy", invalidTouchy)); - accessor.setPropertyValues(pvs); - fail("Should throw exception when everything is valid"); - } - catch (PropertyBatchUpdateException ex) { - assertTrue("Must contain 2 exceptions", ex.getExceptionCount() == 2); - // Test validly set property matches - assertTrue("Vaid set property must stick", target.getName().equals(newName)); - assertTrue("Invalid set property must retain old value", target.getAge() == 0); - assertTrue("New value of dodgy setter must be available through exception", - ex.getPropertyAccessException("touchy").getPropertyChangeEvent().getNewValue().equals(invalidTouchy)); - } + BeanWrapper accessor = createAccessor(target); + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.addPropertyValue(new PropertyValue("age", "foobar")); + pvs.addPropertyValue(new PropertyValue("name", newName)); + pvs.addPropertyValue(new PropertyValue("touchy", invalidTouchy)); + assertThatExceptionOfType(PropertyBatchUpdateException.class).isThrownBy(() -> + accessor.setPropertyValues(pvs)) + .satisfies(ex -> { + assertThat(ex.getExceptionCount()).isEqualTo(2); + assertThat(ex.getPropertyAccessException("touchy").getPropertyChangeEvent() + .getNewValue()).isEqualTo(invalidTouchy); + }); + // Test validly set property matches + assertThat(target.getName().equals(newName)).as("Valid set property must stick").isTrue(); + assertThat(target.getAge() == 0).as("Invalid set property must retain old value").isTrue(); } @Test public void checkNotWritablePropertyHoldPossibleMatches() { TestBean target = new TestBean(); - try { - BeanWrapper accessor = createAccessor(target); - accessor.setPropertyValue("ag", "foobar"); - fail("Should throw exception on invalid property"); - } - catch (NotWritablePropertyException ex) { - // expected - assertEquals(1, ex.getPossibleMatches().length); - assertEquals("age", ex.getPossibleMatches()[0]); - } + BeanWrapper accessor = createAccessor(target); + assertThatExceptionOfType(NotWritablePropertyException.class).isThrownBy(() -> + accessor.setPropertyValue("ag", "foobar")) + .satisfies(ex -> assertThat(ex.getPossibleMatches()).containsExactly("age")); } @Test // Can't be shared; there is no such thing as a read-only field @@ -127,8 +120,8 @@ public void notWritablePropertyExceptionContainsAlternativeMatch() { bw.setPropertyValue("names", "Alef"); } catch (NotWritablePropertyException ex) { - assertNotNull("Possible matches not determined", ex.getPossibleMatches()); - assertEquals("Invalid amount of alternatives", 1, ex.getPossibleMatches().length); + assertThat(ex.getPossibleMatches()).as("Possible matches not determined").isNotNull(); + assertThat(ex.getPossibleMatches().length).as("Invalid amount of alternatives").isEqualTo(1); } } @@ -140,19 +133,20 @@ public void notWritablePropertyExceptionContainsAlternativeMatches() { bw.setPropertyValue("mystring", "Arjen"); } catch (NotWritablePropertyException ex) { - assertNotNull("Possible matches not determined", ex.getPossibleMatches()); - assertEquals("Invalid amount of alternatives", 3, ex.getPossibleMatches().length); + assertThat(ex.getPossibleMatches()).as("Possible matches not determined").isNotNull(); + assertThat(ex.getPossibleMatches().length).as("Invalid amount of alternatives").isEqualTo(3); } } + @Override @Test // Can't be shared: no type mismatch with a field public void setPropertyTypeMismatch() { PropertyTypeMismatch target = new PropertyTypeMismatch(); BeanWrapper accessor = createAccessor(target); accessor.setPropertyValue("object", "a String"); - assertEquals("a String", target.value); - assertTrue(target.getObject() == 8); - assertEquals(8, accessor.getPropertyValue("object")); + assertThat(target.value).isEqualTo("a String"); + assertThat(target.getObject() == 8).isTrue(); + assertThat(accessor.getPropertyValue("object")).isEqualTo(8); } @Test @@ -162,35 +156,36 @@ public void propertyDescriptors() { BeanWrapper accessor = createAccessor(target); accessor.setPropertyValue("name", "a"); accessor.setPropertyValue("spouse.name", "b"); - assertEquals("a", target.getName()); - assertEquals("b", target.getSpouse().getName()); - assertEquals("a", accessor.getPropertyValue("name")); - assertEquals("b", accessor.getPropertyValue("spouse.name")); - assertEquals(String.class, accessor.getPropertyDescriptor("name").getPropertyType()); - assertEquals(String.class, accessor.getPropertyDescriptor("spouse.name").getPropertyType()); + assertThat(target.getName()).isEqualTo("a"); + assertThat(target.getSpouse().getName()).isEqualTo("b"); + assertThat(accessor.getPropertyValue("name")).isEqualTo("a"); + assertThat(accessor.getPropertyValue("spouse.name")).isEqualTo("b"); + assertThat(accessor.getPropertyDescriptor("name").getPropertyType()).isEqualTo(String.class); + assertThat(accessor.getPropertyDescriptor("spouse.name").getPropertyType()).isEqualTo(String.class); } @Test + @SuppressWarnings("unchecked") public void getPropertyWithOptional() { GetterWithOptional target = new GetterWithOptional(); TestBean tb = new TestBean("x"); BeanWrapper accessor = createAccessor(target); accessor.setPropertyValue("object", tb); - assertSame(tb, target.value); - assertSame(tb, target.getObject().get()); - assertSame(tb, ((Optional) accessor.getPropertyValue("object")).get()); - assertEquals("x", target.value.getName()); - assertEquals("x", target.getObject().get().getName()); - assertEquals("x", accessor.getPropertyValue("object.name")); + assertThat(target.value).isSameAs(tb); + assertThat(target.getObject().get()).isSameAs(tb); + assertThat(((Optional) accessor.getPropertyValue("object")).get()).isSameAs(tb); + assertThat(target.value.getName()).isEqualTo("x"); + assertThat(target.getObject().get().getName()).isEqualTo("x"); + assertThat(accessor.getPropertyValue("object.name")).isEqualTo("x"); accessor.setPropertyValue("object.name", "y"); - assertSame(tb, target.value); - assertSame(tb, target.getObject().get()); - assertSame(tb, ((Optional) accessor.getPropertyValue("object")).get()); - assertEquals("y", target.value.getName()); - assertEquals("y", target.getObject().get().getName()); - assertEquals("y", accessor.getPropertyValue("object.name")); + assertThat(target.value).isSameAs(tb); + assertThat(target.getObject().get()).isSameAs(tb); + assertThat(((Optional) accessor.getPropertyValue("object")).get()).isSameAs(tb); + assertThat(target.value.getName()).isEqualTo("y"); + assertThat(target.getObject().get().getName()).isEqualTo("y"); + assertThat(accessor.getPropertyValue("object.name")).isEqualTo("y"); } @Test @@ -200,22 +195,18 @@ public void getPropertyWithOptionalAndAutoGrow() { accessor.setAutoGrowNestedPaths(true); accessor.setPropertyValue("object.name", "x"); - assertEquals("x", target.value.getName()); - assertEquals("x", target.getObject().get().getName()); - assertEquals("x", accessor.getPropertyValue("object.name")); + assertThat(target.value.getName()).isEqualTo("x"); + assertThat(target.getObject().get().getName()).isEqualTo("x"); + assertThat(accessor.getPropertyValue("object.name")).isEqualTo("x"); } @Test public void incompletelyQuotedKeyLeadsToPropertyException() { TestBean target = new TestBean(); - try { - BeanWrapper accessor = createAccessor(target); - accessor.setPropertyValue("[']", "foobar"); - fail("Should throw exception on invalid property"); - } - catch (NotWritablePropertyException ex) { - assertNull(ex.getPossibleMatches()); - } + BeanWrapper accessor = createAccessor(target); + assertThatExceptionOfType(NotWritablePropertyException.class).isThrownBy(() -> + accessor.setPropertyValue("[']", "foobar")) + .satisfies(ex -> assertThat(ex.getPossibleMatches()).isNull()); } @@ -245,10 +236,12 @@ private static class GetterBean implements AliasedProperty { private String name; + @Override public void setName(String name) { this.name = name; } + @Override public String getName() { if (this.name == null) { throw new RuntimeException("name property must be set"); diff --git a/spring-beans/src/test/java/org/springframework/beans/CachedIntrospectionResultsTests.java b/spring-beans/src/test/java/org/springframework/beans/CachedIntrospectionResultsTests.java index e0901542d079..cba7e4964a98 100644 --- a/spring-beans/src/test/java/org/springframework/beans/CachedIntrospectionResultsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/CachedIntrospectionResultsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,13 +20,12 @@ import java.beans.PropertyDescriptor; import java.util.ArrayList; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.OverridingClassLoader; -import org.springframework.tests.sample.beans.TestBean; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -38,30 +37,30 @@ public class CachedIntrospectionResultsTests { @Test public void acceptAndClearClassLoader() throws Exception { BeanWrapper bw = new BeanWrapperImpl(TestBean.class); - assertTrue(bw.isWritableProperty("name")); - assertTrue(bw.isWritableProperty("age")); - assertTrue(CachedIntrospectionResults.strongClassCache.containsKey(TestBean.class)); + assertThat(bw.isWritableProperty("name")).isTrue(); + assertThat(bw.isWritableProperty("age")).isTrue(); + assertThat(CachedIntrospectionResults.strongClassCache.containsKey(TestBean.class)).isTrue(); ClassLoader child = new OverridingClassLoader(getClass().getClassLoader()); - Class tbClass = child.loadClass("org.springframework.tests.sample.beans.TestBean"); - assertFalse(CachedIntrospectionResults.strongClassCache.containsKey(tbClass)); + Class tbClass = child.loadClass("org.springframework.beans.testfixture.beans.TestBean"); + assertThat(CachedIntrospectionResults.strongClassCache.containsKey(tbClass)).isFalse(); CachedIntrospectionResults.acceptClassLoader(child); bw = new BeanWrapperImpl(tbClass); - assertTrue(bw.isWritableProperty("name")); - assertTrue(bw.isWritableProperty("age")); - assertTrue(CachedIntrospectionResults.strongClassCache.containsKey(tbClass)); + assertThat(bw.isWritableProperty("name")).isTrue(); + assertThat(bw.isWritableProperty("age")).isTrue(); + assertThat(CachedIntrospectionResults.strongClassCache.containsKey(tbClass)).isTrue(); CachedIntrospectionResults.clearClassLoader(child); - assertFalse(CachedIntrospectionResults.strongClassCache.containsKey(tbClass)); + assertThat(CachedIntrospectionResults.strongClassCache.containsKey(tbClass)).isFalse(); - assertTrue(CachedIntrospectionResults.strongClassCache.containsKey(TestBean.class)); + assertThat(CachedIntrospectionResults.strongClassCache.containsKey(TestBean.class)).isTrue(); } @Test public void clearClassLoaderForSystemClassLoader() throws Exception { BeanUtils.getPropertyDescriptors(ArrayList.class); - assertTrue(CachedIntrospectionResults.strongClassCache.containsKey(ArrayList.class)); + assertThat(CachedIntrospectionResults.strongClassCache.containsKey(ArrayList.class)).isTrue(); CachedIntrospectionResults.clearClassLoader(ArrayList.class.getClassLoader()); - assertFalse(CachedIntrospectionResults.strongClassCache.containsKey(ArrayList.class)); + assertThat(CachedIntrospectionResults.strongClassCache.containsKey(ArrayList.class)).isFalse(); } @Test @@ -84,13 +83,11 @@ class C { } // resulting in a property descriptor including the non-standard setFoo method - assertThat(pd, notNullValue()); - assertThat(pd.getReadMethod(), equalTo(C.class.getMethod("getFoo"))); - assertThat( - "No write method found for non-void returning 'setFoo' method. " + - "Check to see if CachedIntrospectionResults is delegating to " + - "ExtendedBeanInfo as expected", - pd.getWriteMethod(), equalTo(C.class.getMethod("setFoo", String.class))); + assertThat(pd).isNotNull(); + assertThat(pd.getReadMethod()).isEqualTo(C.class.getMethod("getFoo")); + // No write method found for non-void returning 'setFoo' method. + // Check to see if CachedIntrospectionResults is delegating to ExtendedBeanInfo as expected + assertThat(pd.getWriteMethod()).isEqualTo(C.class.getMethod("setFoo", String.class)); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/ConcurrentBeanWrapperTests.java b/spring-beans/src/test/java/org/springframework/beans/ConcurrentBeanWrapperTests.java index fcace7d25a5a..f1d4912b16e9 100644 --- a/spring-beans/src/test/java/org/springframework/beans/ConcurrentBeanWrapperTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/ConcurrentBeanWrapperTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -26,9 +26,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Guillaume Poirier @@ -73,7 +73,7 @@ public void testConcurrent() { } } if (ex != null) { - fail(ex.getMessage()); + throw new AssertionError("Unexpected exception", ex); } } @@ -82,7 +82,7 @@ private static void performSet() { Properties p = (Properties) System.getProperties().clone(); - assertTrue("The System properties must not be empty", p.size() != 0); + assertThat(p.size() != 0).as("The System properties must not be empty").isTrue(); for (Iterator i = p.entrySet().iterator(); i.hasNext();) { i.next(); @@ -103,7 +103,7 @@ private static void performSet() { BeanWrapperImpl wrapper = new BeanWrapperImpl(bean); wrapper.setPropertyValue("properties", value); - assertEquals(p, bean.getProperties()); + assertThat(bean.getProperties()).isEqualTo(p); } diff --git a/spring-beans/src/test/java/org/springframework/beans/DirectFieldAccessorTests.java b/spring-beans/src/test/java/org/springframework/beans/DirectFieldAccessorTests.java index e67bae0c2631..1ca8ddc60298 100644 --- a/spring-beans/src/test/java/org/springframework/beans/DirectFieldAccessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/DirectFieldAccessorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,11 +16,11 @@ package org.springframework.beans; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Specific {@link DirectFieldAccessor} tests. @@ -38,18 +38,17 @@ protected DirectFieldAccessor createAccessor(Object target) { @Test - public void withShadowedField() throws Exception { + public void withShadowedField() { final StringBuilder sb = new StringBuilder(); - @SuppressWarnings("serial") TestBean target = new TestBean() { @SuppressWarnings("unused") StringBuilder name = sb; }; DirectFieldAccessor dfa = createAccessor(target); - assertEquals(StringBuilder.class, dfa.getPropertyType("name")); - assertEquals(sb, dfa.getPropertyValue("name")); + assertThat(dfa.getPropertyType("name")).isEqualTo(StringBuilder.class); + assertThat(dfa.getPropertyValue("name")).isEqualTo(sb); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoFactoryTests.java index 7813a8fa31b8..c4449bcb5b95 100644 --- a/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,10 @@ import java.beans.IntrospectionException; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; /** * Unit tests for {@link ExtendedBeanInfoTests}. @@ -38,7 +38,7 @@ public void shouldNotSupportClassHavingOnlyVoidReturningSetter() throws Introspe class C { public void setFoo(String s) { } } - assertThat(factory.getBeanInfo(C.class), nullValue()); + assertThat(factory.getBeanInfo(C.class)).isNull(); } @Test @@ -47,7 +47,7 @@ public void shouldSupportClassHavingNonVoidReturningSetter() throws Introspectio class C { public C setFoo(String s) { return this; } } - assertThat(factory.getBeanInfo(C.class), notNullValue()); + assertThat(factory.getBeanInfo(C.class)).isNotNull(); } @Test @@ -56,7 +56,7 @@ public void shouldSupportClassHavingNonVoidReturningIndexedSetter() throws Intro class C { public C setFoo(int i, String s) { return this; } } - assertThat(factory.getBeanInfo(C.class), notNullValue()); + assertThat(factory.getBeanInfo(C.class)).isNotNull(); } @Test @@ -65,7 +65,7 @@ public void shouldNotSupportClassHavingNonPublicNonVoidReturningIndexedSetter() class C { void setBar(String s) { } } - assertThat(factory.getBeanInfo(C.class), nullValue()); + assertThat(factory.getBeanInfo(C.class)).isNull(); } @Test @@ -74,7 +74,7 @@ public void shouldNotSupportClassHavingNonVoidReturningParameterlessSetter() thr class C { C setBar() { return this; } } - assertThat(factory.getBeanInfo(C.class), nullValue()); + assertThat(factory.getBeanInfo(C.class)).isNull(); } @Test @@ -83,7 +83,7 @@ public void shouldNotSupportClassHavingNonVoidReturningMethodNamedSet() throws I class C { C set(String s) { return this; } } - assertThat(factory.getBeanInfo(C.class), nullValue()); + assertThat(factory.getBeanInfo(C.class)).isNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java b/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java index 03f671da996e..d9876729023f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/ExtendedBeanInfoTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,14 +23,11 @@ import java.beans.PropertyDescriptor; import java.math.BigDecimal; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Chris Beams @@ -49,11 +46,11 @@ public void standardReadMethodOnly() throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(C.class); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isFalse(); } @Test @@ -65,11 +62,11 @@ public void setFoo(String f) { } BeanInfo bi = Introspector.getBeanInfo(C.class); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isTrue(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); } @Test @@ -82,11 +79,11 @@ public void setFoo(String f) { } BeanInfo bi = Introspector.getBeanInfo(C.class); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isTrue(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); } @Test @@ -98,11 +95,11 @@ public void nonStandardWriteMethodOnly() throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(C.class); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); } @Test @@ -114,16 +111,16 @@ public void standardReadAndNonStandardWriteMethods() throws IntrospectionExcepti BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); } @Test @@ -135,15 +132,15 @@ public void standardReadAndNonStandardIndexedWriteMethod() throws IntrospectionE BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foo")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foo")).isTrue(); } @Test @@ -156,24 +153,24 @@ public void standardReadMethodsAndOverloadedNonStandardWriteMethods() throws Exc BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); for (PropertyDescriptor pd : ebi.getPropertyDescriptors()) { if (pd.getName().equals("foo")) { - assertThat(pd.getWriteMethod(), is(C.class.getMethod("setFoo", String.class))); + assertThat(pd.getWriteMethod()).isEqualTo(C.class.getMethod("setFoo", String.class)); return; } } - fail("never matched write method"); + throw new AssertionError("never matched write method"); } @Test @@ -191,11 +188,11 @@ public Integer getProperty1() { } { // always passes ExtendedBeanInfo bi = new ExtendedBeanInfo(Introspector.getBeanInfo(Parent.class)); - assertThat(hasReadMethodForProperty(bi, "property1"), is(true)); + assertThat(hasReadMethodForProperty(bi, "property1")).isTrue(); } { // failed prior to fix for SPR-9414 ExtendedBeanInfo bi = new ExtendedBeanInfo(Introspector.getBeanInfo(Child.class)); - assertThat(hasReadMethodForProperty(bi, "property1"), is(true)); + assertThat(hasReadMethodForProperty(bi, "property1")).isTrue(); } } @@ -209,11 +206,11 @@ public Class getProp() { } { // always passes BeanInfo info = Introspector.getBeanInfo(Bean.class); - assertThat(info.getPropertyDescriptors().length, equalTo(2)); + assertThat(info.getPropertyDescriptors().length).isEqualTo(2); } { // failed prior to fix for SPR-9453 BeanInfo info = new ExtendedBeanInfo(Introspector.getBeanInfo(Bean.class)); - assertThat(info.getPropertyDescriptors().length, equalTo(2)); + assertThat(info.getPropertyDescriptors().length).isEqualTo(2); } } @@ -228,16 +225,16 @@ public void standardReadMethodInSuperclassAndNonStandardWriteMethodInSubclass() BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); } @Test @@ -266,30 +263,30 @@ public C setBar(int bar) { .setFoo("blue") .setBar(42); - assertThat(c.getFoo(), is("blue")); - assertThat(c.getBar(), is(42)); + assertThat(c.getFoo()).isEqualTo("blue"); + assertThat(c.getBar()).isEqualTo(42); BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(bi, "bar"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "bar"), is(false)); + assertThat(hasReadMethodForProperty(bi, "bar")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "bar")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(bi, "bar"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "bar"), is(false)); + assertThat(hasReadMethodForProperty(bi, "bar")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "bar")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); - assertThat(hasReadMethodForProperty(ebi, "bar"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "bar"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "bar")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "bar")).isTrue(); } @Test @@ -302,11 +299,11 @@ public void nonPublicStandardReadAndWriteMethods() throws Exception { BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isFalse(); } /** @@ -323,9 +320,9 @@ public void setFoo(Integer foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertEquals(hasWriteMethodForProperty(bi, "foo"), hasWriteMethodForProperty(ebi, "foo")); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isEqualTo(hasWriteMethodForProperty(bi, "foo")); } @Test @@ -338,9 +335,9 @@ public void setFoos(int index, Integer foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertEquals(hasIndexedWriteMethodForProperty(bi, "foos"), hasIndexedWriteMethodForProperty(ebi, "foos")); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isEqualTo(hasIndexedWriteMethodForProperty(bi, "foos")); } /** @@ -357,11 +354,11 @@ public void setFoo(Number foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isFalse(); } @Test @@ -374,11 +371,11 @@ public void setFoo(int index, Number foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(false)); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isFalse(); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(false)); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isFalse(); } @Test @@ -392,11 +389,11 @@ class C { BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(Introspector.getBeanInfo(C.class)); - assertThat(hasReadMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); + assertThat(hasReadMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); - assertThat(hasReadMethodForProperty(ebi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); } @Test @@ -410,11 +407,11 @@ public void setFoos(int i, String foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(Introspector.getBeanInfo(C.class)); - assertThat(hasWriteMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(true)); + assertThat(hasWriteMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isTrue(); - assertThat(hasWriteMethodForProperty(ebi, "foos"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(true)); + assertThat(hasWriteMethodForProperty(ebi, "foos")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isTrue(); } @Test @@ -430,15 +427,15 @@ public void setFoos(int i, String foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(Introspector.getBeanInfo(C.class)); - assertThat(hasReadMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(true)); + assertThat(hasReadMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isTrue(); - assertThat(hasReadMethodForProperty(ebi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foos"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foos")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isTrue(); } @Test @@ -458,15 +455,15 @@ public void setFoos(int i, String foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(Introspector.getBeanInfo(C.class)); - assertThat(hasReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foos"), is(true)); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(true)); + assertThat(hasReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isTrue(); - assertThat(hasReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isTrue(); } @Test @@ -481,14 +478,14 @@ class C { BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); // interesting! standard Inspector picks up non-void return types on indexed write methods by default - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(false)); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(Introspector.getBeanInfo(C.class)); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(true)); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isTrue(); } @Test @@ -505,20 +502,20 @@ class C { BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foos"), is(false)); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foos")).isFalse(); // again as above, standard Inspector picks up non-void return types on indexed write methods by default - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(false)); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(Introspector.getBeanInfo(C.class)); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(false)); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isFalse(); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(true)); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isTrue(); } @Test @@ -533,16 +530,16 @@ public void setFoos(String[] foos) { } } BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isFalse(); BeanInfo ebi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(ebi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(false)); + assertThat(hasReadMethodForProperty(ebi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isFalse(); } { // variant with non-standard write method @SuppressWarnings("unused") @@ -554,16 +551,16 @@ class C { } BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(bi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foos"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "foos"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(bi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foos")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "foos")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(Introspector.getBeanInfo(C.class)); - assertThat(hasReadMethodForProperty(ebi, "foos"), is(false)); - assertThat(hasIndexedReadMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foos"), is(true)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "foos"), is(false)); + assertThat(hasReadMethodForProperty(ebi, "foos")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foos")).isTrue(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "foos")).isFalse(); } } @@ -572,7 +569,7 @@ class C { * IntrospectionException regarding a "type mismatch between indexed and non-indexed * methods" intermittently (approximately one out of every four times) under JDK 7 * due to non-deterministic results from {@link Class#getDeclaredMethods()}. - * See http://bugs.sun.com/view_bug.do?bug_id=7023180 + * See https://bugs.java.com/view_bug.do?bug_id=7023180 * @see #cornerSpr9702() */ @Test @@ -595,18 +592,18 @@ class C extends B { BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); - assertThat(ebi.getPropertyDescriptors().length, equalTo(bi.getPropertyDescriptors().length)); + assertThat(ebi.getPropertyDescriptors().length).isEqualTo(bi.getPropertyDescriptors().length); } @Test @@ -619,11 +616,11 @@ public void setFoo(String foo) { } BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isTrue(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); } /** @@ -640,7 +637,7 @@ public void emptyPropertiesIgnored() throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(C.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(ebi.getPropertyDescriptors(), equalTo(bi.getPropertyDescriptors())); + assertThat(ebi.getPropertyDescriptors()).isEqualTo(bi.getPropertyDescriptors()); } @Test @@ -651,24 +648,24 @@ public void overloadedNonStandardWriteMethodsOnly_orderA() throws IntrospectionE } BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); for (PropertyDescriptor pd : ebi.getPropertyDescriptors()) { if (pd.getName().equals("foo")) { - assertThat(pd.getWriteMethod(), is(C.class.getMethod("setFoo", String.class))); + assertThat(pd.getWriteMethod()).isEqualTo(C.class.getMethod("setFoo", String.class)); return; } } - fail("never matched write method"); + throw new AssertionError("never matched write method"); } @Test @@ -679,24 +676,24 @@ public void overloadedNonStandardWriteMethodsOnly_orderB() throws IntrospectionE } BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); + assertThat(hasReadMethodForProperty(bi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "foo")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "foo"), is(false)); - assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); + assertThat(hasReadMethodForProperty(ebi, "foo")).isFalse(); + assertThat(hasWriteMethodForProperty(ebi, "foo")).isTrue(); for (PropertyDescriptor pd : ebi.getPropertyDescriptors()) { if (pd.getName().equals("foo")) { - assertThat(pd.getWriteMethod(), is(C.class.getMethod("setFoo", String.class))); + assertThat(pd.getWriteMethod()).isEqualTo(C.class.getMethod("setFoo", String.class)); return; } } - fail("never matched write method"); + throw new AssertionError("never matched write method"); } /** @@ -715,22 +712,22 @@ public void reproSpr8522() throws IntrospectionException { } BeanInfo bi = Introspector.getBeanInfo(C.class); - assertThat(hasReadMethodForProperty(bi, "dateFormat"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "dateFormat"), is(false)); - assertThat(hasIndexedReadMethodForProperty(bi, "dateFormat"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "dateFormat"), is(false)); + assertThat(hasReadMethodForProperty(bi, "dateFormat")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "dateFormat")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(bi, "dateFormat")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "dateFormat")).isFalse(); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "dateFormat"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "dateFormat"), is(false)); - assertThat(hasIndexedReadMethodForProperty(bi, "dateFormat"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "dateFormat"), is(false)); + assertThat(hasReadMethodForProperty(bi, "dateFormat")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "dateFormat")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(bi, "dateFormat")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "dateFormat")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "dateFormat"), is(false)); - assertThat(hasWriteMethodForProperty(ebi, "dateFormat"), is(true)); - assertThat(hasIndexedReadMethodForProperty(ebi, "dateFormat"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(ebi, "dateFormat"), is(false)); + assertThat(hasReadMethodForProperty(ebi, "dateFormat")).isFalse(); + assertThat(hasWriteMethodForProperty(ebi, "dateFormat")).isTrue(); + assertThat(hasIndexedReadMethodForProperty(ebi, "dateFormat")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(ebi, "dateFormat")).isFalse(); } @Test @@ -738,7 +735,7 @@ public void propertyCountsMatch() throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(TestBean.class); BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(ebi.getPropertyDescriptors().length, equalTo(bi.getPropertyDescriptors().length)); + assertThat(ebi.getPropertyDescriptors().length).isEqualTo(bi.getPropertyDescriptors().length); } @Test @@ -754,10 +751,11 @@ class ExtendedTestBean extends TestBean { for (PropertyDescriptor pd : ebi.getPropertyDescriptors()) { if (pd.getName().equals("foo")) { found = true; + break; } } - assertThat(found, is(true)); - assertThat(ebi.getPropertyDescriptors().length, equalTo(bi.getPropertyDescriptors().length+1)); + assertThat(found).isTrue(); + assertThat(ebi.getPropertyDescriptors().length).isEqualTo(bi.getPropertyDescriptors().length+1); } /** @@ -770,8 +768,7 @@ public void propertyDescriptorOrderIsEqual() throws IntrospectionException { BeanInfo ebi = new ExtendedBeanInfo(bi); for (int i = 0; i < bi.getPropertyDescriptors().length; i++) { - assertThat("element " + i + " in BeanInfo and ExtendedBeanInfo propertyDescriptor arrays do not match", - ebi.getPropertyDescriptors()[i].getName(), equalTo(bi.getPropertyDescriptors()[i].getName())); + assertThat(ebi.getPropertyDescriptors()[i].getName()).isEqualTo(bi.getPropertyDescriptors()[i].getName()); } } @@ -779,19 +776,19 @@ public void propertyDescriptorOrderIsEqual() throws IntrospectionException { public void propertyDescriptorComparator() throws IntrospectionException { ExtendedBeanInfo.PropertyDescriptorComparator c = new ExtendedBeanInfo.PropertyDescriptorComparator(); - assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("a", null, null)), equalTo(0)); - assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("abc", null, null)), equalTo(0)); - assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("b", null, null)), lessThan(0)); - assertThat(c.compare(new PropertyDescriptor("b", null, null), new PropertyDescriptor("a", null, null)), greaterThan(0)); - assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("abd", null, null)), lessThan(0)); - assertThat(c.compare(new PropertyDescriptor("xyz", null, null), new PropertyDescriptor("123", null, null)), greaterThan(0)); - assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("abc", null, null)), lessThan(0)); - assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("a", null, null)), greaterThan(0)); - assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("b", null, null)), lessThan(0)); + assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("a", null, null))).isEqualTo(0); + assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("abc", null, null))).isEqualTo(0); + assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("b", null, null))).isLessThan(0); + assertThat(c.compare(new PropertyDescriptor("b", null, null), new PropertyDescriptor("a", null, null))).isGreaterThan(0); + assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("abd", null, null))).isLessThan(0); + assertThat(c.compare(new PropertyDescriptor("xyz", null, null), new PropertyDescriptor("123", null, null))).isGreaterThan(0); + assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("abc", null, null))).isLessThan(0); + assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("a", null, null))).isGreaterThan(0); + assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("b", null, null))).isLessThan(0); - assertThat(c.compare(new PropertyDescriptor(" ", null, null), new PropertyDescriptor("a", null, null)), lessThan(0)); - assertThat(c.compare(new PropertyDescriptor("1", null, null), new PropertyDescriptor("a", null, null)), lessThan(0)); - assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("A", null, null)), greaterThan(0)); + assertThat(c.compare(new PropertyDescriptor(" ", null, null), new PropertyDescriptor("a", null, null))).isLessThan(0); + assertThat(c.compare(new PropertyDescriptor("1", null, null), new PropertyDescriptor("a", null, null))).isLessThan(0); + assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("A", null, null))).isGreaterThan(0); } @Test @@ -827,11 +824,11 @@ public boolean isTargetMethod() { // helps out here, and is now put into use in ExtendedBeanInfo as well. BeanInfo ebi = new ExtendedBeanInfo(bi); - assertThat(hasReadMethodForProperty(bi, "targetMethod"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "targetMethod"), is(false)); + assertThat(hasReadMethodForProperty(bi, "targetMethod")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "targetMethod")).isFalse(); - assertThat(hasReadMethodForProperty(ebi, "targetMethod"), is(true)); - assertThat(hasWriteMethodForProperty(ebi, "targetMethod"), is(false)); + assertThat(hasReadMethodForProperty(ebi, "targetMethod")).isTrue(); + assertThat(hasWriteMethodForProperty(ebi, "targetMethod")).isFalse(); } @Test @@ -851,37 +848,37 @@ public void setAddress(int index, String addr) { } // ExtendedBeanInfo needs to behave exactly like BeanInfo... BeanInfo ebi = new ExtendedBeanInfo(bi); - assertEquals(hasReadMethod, hasReadMethodForProperty(ebi, "address")); - assertEquals(hasWriteMethod, hasWriteMethodForProperty(ebi, "address")); - assertEquals(hasIndexedReadMethod, hasIndexedReadMethodForProperty(ebi, "address")); - assertEquals(hasIndexedWriteMethod, hasIndexedWriteMethodForProperty(ebi, "address")); + assertThat(hasReadMethodForProperty(ebi, "address")).isEqualTo(hasReadMethod); + assertThat(hasWriteMethodForProperty(ebi, "address")).isEqualTo(hasWriteMethod); + assertThat(hasIndexedReadMethodForProperty(ebi, "address")).isEqualTo(hasIndexedReadMethod); + assertThat(hasIndexedWriteMethodForProperty(ebi, "address")).isEqualTo(hasIndexedWriteMethod); } @Test public void shouldSupportStaticWriteMethod() throws IntrospectionException { { BeanInfo bi = Introspector.getBeanInfo(WithStaticWriteMethod.class); - assertThat(hasReadMethodForProperty(bi, "prop1"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "prop1"), is(false)); - assertThat(hasIndexedReadMethodForProperty(bi, "prop1"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "prop1"), is(false)); + assertThat(hasReadMethodForProperty(bi, "prop1")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "prop1")).isFalse(); + assertThat(hasIndexedReadMethodForProperty(bi, "prop1")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "prop1")).isFalse(); } { BeanInfo bi = new ExtendedBeanInfo(Introspector.getBeanInfo(WithStaticWriteMethod.class)); - assertThat(hasReadMethodForProperty(bi, "prop1"), is(false)); - assertThat(hasWriteMethodForProperty(bi, "prop1"), is(true)); - assertThat(hasIndexedReadMethodForProperty(bi, "prop1"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "prop1"), is(false)); + assertThat(hasReadMethodForProperty(bi, "prop1")).isFalse(); + assertThat(hasWriteMethodForProperty(bi, "prop1")).isTrue(); + assertThat(hasIndexedReadMethodForProperty(bi, "prop1")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "prop1")).isFalse(); } } @Test // SPR-12434 public void shouldDetectValidPropertiesAndIgnoreInvalidProperties() throws IntrospectionException { BeanInfo bi = new ExtendedBeanInfo(Introspector.getBeanInfo(java.awt.Window.class)); - assertThat(hasReadMethodForProperty(bi, "locationByPlatform"), is(true)); - assertThat(hasWriteMethodForProperty(bi, "locationByPlatform"), is(true)); - assertThat(hasIndexedReadMethodForProperty(bi, "locationByPlatform"), is(false)); - assertThat(hasIndexedWriteMethodForProperty(bi, "locationByPlatform"), is(false)); + assertThat(hasReadMethodForProperty(bi, "locationByPlatform")).isTrue(); + assertThat(hasWriteMethodForProperty(bi, "locationByPlatform")).isTrue(); + assertThat(hasIndexedReadMethodForProperty(bi, "locationByPlatform")).isFalse(); + assertThat(hasIndexedWriteMethodForProperty(bi, "locationByPlatform")).isFalse(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/MutablePropertyValuesTests.java b/spring-beans/src/test/java/org/springframework/beans/MutablePropertyValuesTests.java index b01d7829dcde..027811ed6a48 100644 --- a/spring-beans/src/test/java/org/springframework/beans/MutablePropertyValuesTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/MutablePropertyValuesTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,10 @@ import java.util.Iterator; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link MutablePropertyValues}. @@ -44,7 +44,7 @@ public void testValid() { doTestTony(deepCopy); deepCopy.setPropertyValueAt(new PropertyValue("name", "Gordon"), 0); doTestTony(pvs); - assertEquals("Gordon", deepCopy.getPropertyValue("name").getValue()); + assertThat(deepCopy.getPropertyValue("name").getValue()).isEqualTo("Gordon"); } @Test @@ -56,10 +56,10 @@ public void testAddOrOverride() { doTestTony(pvs); PropertyValue addedPv = new PropertyValue("rod", "Rod"); pvs.addPropertyValue(addedPv); - assertTrue(pvs.getPropertyValue("rod").equals(addedPv)); + assertThat(pvs.getPropertyValue("rod").equals(addedPv)).isTrue(); PropertyValue changedPv = new PropertyValue("forname", "Greg"); pvs.addPropertyValue(changedPv); - assertTrue(pvs.getPropertyValue("forname").equals(changedPv)); + assertThat(pvs.getPropertyValue("forname").equals(changedPv)).isTrue(); } @Test @@ -70,7 +70,7 @@ public void testChangesOnEquals() { pvs.addPropertyValue(new PropertyValue("age", "50")); MutablePropertyValues pvs2 = pvs; PropertyValues changes = pvs2.changesSince(pvs); - assertTrue("changes are empty", changes.getPropertyValues().length == 0); + assertThat(changes.getPropertyValues().length == 0).as("changes are empty").isTrue(); } @Test @@ -82,29 +82,27 @@ public void testChangeOfOneField() { MutablePropertyValues pvs2 = new MutablePropertyValues(pvs); PropertyValues changes = pvs2.changesSince(pvs); - assertTrue("changes are empty, not of length " + changes.getPropertyValues().length, - changes.getPropertyValues().length == 0); + assertThat(changes.getPropertyValues().length == 0).as("changes are empty, not of length " + changes.getPropertyValues().length).isTrue(); pvs2.addPropertyValue(new PropertyValue("forname", "Gordon")); changes = pvs2.changesSince(pvs); - assertEquals("1 change", 1, changes.getPropertyValues().length); + assertThat(changes.getPropertyValues().length).as("1 change").isEqualTo(1); PropertyValue fn = changes.getPropertyValue("forname"); - assertTrue("change is forname", fn != null); - assertTrue("new value is gordon", fn.getValue().equals("Gordon")); + assertThat(fn != null).as("change is forname").isTrue(); + assertThat(fn.getValue().equals("Gordon")).as("new value is gordon").isTrue(); MutablePropertyValues pvs3 = new MutablePropertyValues(pvs); changes = pvs3.changesSince(pvs); - assertTrue("changes are empty, not of length " + changes.getPropertyValues().length, - changes.getPropertyValues().length == 0); + assertThat(changes.getPropertyValues().length == 0).as("changes are empty, not of length " + changes.getPropertyValues().length).isTrue(); // add new pvs3.addPropertyValue(new PropertyValue("foo", "bar")); pvs3.addPropertyValue(new PropertyValue("fi", "fum")); changes = pvs3.changesSince(pvs); - assertTrue("2 change", changes.getPropertyValues().length == 2); + assertThat(changes.getPropertyValues().length == 2).as("2 change").isTrue(); fn = changes.getPropertyValue("foo"); - assertTrue("change in foo", fn != null); - assertTrue("new value is bar", fn.getValue().equals("bar")); + assertThat(fn != null).as("change in foo").isTrue(); + assertThat(fn.getValue().equals("bar")).as("new value is bar").isTrue(); } @Test @@ -113,26 +111,19 @@ public void iteratorContainsPropertyValue() { pvs.add("foo", "bar"); Iterator it = pvs.iterator(); - assertTrue(it.hasNext()); + assertThat(it.hasNext()).isTrue(); PropertyValue pv = it.next(); - assertEquals("foo", pv.getName()); - assertEquals("bar", pv.getValue()); - - try { - it.remove(); - fail("Should have thrown UnsupportedOperationException"); - } - catch (UnsupportedOperationException ex) { - // expected - } - assertFalse(it.hasNext()); + assertThat(pv.getName()).isEqualTo("foo"); + assertThat(pv.getValue()).isEqualTo("bar"); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(it::remove); + assertThat(it.hasNext()).isFalse(); } @Test public void iteratorIsEmptyForEmptyValues() { MutablePropertyValues pvs = new MutablePropertyValues(); Iterator it = pvs.iterator(); - assertFalse(it.hasNext()); + assertThat(it.hasNext()).isFalse(); } @Test @@ -140,17 +131,17 @@ public void streamContainsPropertyValue() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("foo", "bar"); - assertThat(pvs.stream(), notNullValue()); - assertThat(pvs.stream().count(), is(1L)); - assertThat(pvs.stream().anyMatch(pv -> "foo".equals(pv.getName()) && "bar".equals(pv.getValue())), is(true)); - assertThat(pvs.stream().anyMatch(pv -> "bar".equals(pv.getName()) && "foo".equals(pv.getValue())), is(false)); + assertThat(pvs.stream()).isNotNull(); + assertThat(pvs.stream().count()).isEqualTo(1L); + assertThat(pvs.stream().anyMatch(pv -> "foo".equals(pv.getName()) && "bar".equals(pv.getValue()))).isTrue(); + assertThat(pvs.stream().anyMatch(pv -> "bar".equals(pv.getName()) && "foo".equals(pv.getValue()))).isFalse(); } @Test public void streamIsEmptyForEmptyValues() { MutablePropertyValues pvs = new MutablePropertyValues(); - assertThat(pvs.stream(), notNullValue()); - assertThat(pvs.stream().count(), is(0L)); + assertThat(pvs.stream()).isNotNull(); + assertThat(pvs.stream().count()).isEqualTo(0L); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/PropertyAccessorUtilsTests.java b/spring-beans/src/test/java/org/springframework/beans/PropertyAccessorUtilsTests.java index 1b15a43d9e3d..4613f472624c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/PropertyAccessorUtilsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/PropertyAccessorUtilsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,33 +16,75 @@ package org.springframework.beans; -import java.util.Arrays; +import org.junit.jupiter.api.Test; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** + * Unit tests for {@link PropertyAccessorUtils}. + * * @author Juergen Hoeller * @author Chris Beams */ public class PropertyAccessorUtilsTests { @Test - public void testCanonicalPropertyName() { - assertEquals("map", PropertyAccessorUtils.canonicalPropertyName("map")); - assertEquals("map[key1]", PropertyAccessorUtils.canonicalPropertyName("map[key1]")); - assertEquals("map[key1]", PropertyAccessorUtils.canonicalPropertyName("map['key1']")); - assertEquals("map[key1]", PropertyAccessorUtils.canonicalPropertyName("map[\"key1\"]")); - assertEquals("map[key1][key2]", PropertyAccessorUtils.canonicalPropertyName("map[key1][key2]")); - assertEquals("map[key1][key2]", PropertyAccessorUtils.canonicalPropertyName("map['key1'][\"key2\"]")); - assertEquals("map[key1].name", PropertyAccessorUtils.canonicalPropertyName("map[key1].name")); - assertEquals("map[key1].name", PropertyAccessorUtils.canonicalPropertyName("map['key1'].name")); - assertEquals("map[key1].name", PropertyAccessorUtils.canonicalPropertyName("map[\"key1\"].name")); + public void getPropertyName() { + assertThat(PropertyAccessorUtils.getPropertyName("")).isEqualTo(""); + assertThat(PropertyAccessorUtils.getPropertyName("[user]")).isEqualTo(""); + assertThat(PropertyAccessorUtils.getPropertyName("user")).isEqualTo("user"); + } + + @Test + public void isNestedOrIndexedProperty() { + assertThat(PropertyAccessorUtils.isNestedOrIndexedProperty(null)).isFalse(); + assertThat(PropertyAccessorUtils.isNestedOrIndexedProperty("")).isFalse(); + assertThat(PropertyAccessorUtils.isNestedOrIndexedProperty("user")).isFalse(); + + assertThat(PropertyAccessorUtils.isNestedOrIndexedProperty("[user]")).isTrue(); + assertThat(PropertyAccessorUtils.isNestedOrIndexedProperty("user.name")).isTrue(); + } + + @Test + public void getFirstNestedPropertySeparatorIndex() { + assertThat(PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex("[user]")).isEqualTo(-1); + assertThat(PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex("user.name")).isEqualTo(4); + } + + @Test + public void getLastNestedPropertySeparatorIndex() { + assertThat(PropertyAccessorUtils.getLastNestedPropertySeparatorIndex("[user]")).isEqualTo(-1); + assertThat(PropertyAccessorUtils.getLastNestedPropertySeparatorIndex("user.address.street")).isEqualTo(12); + } + + @Test + public void matchesProperty() { + assertThat(PropertyAccessorUtils.matchesProperty("user", "email")).isFalse(); + assertThat(PropertyAccessorUtils.matchesProperty("username", "user")).isFalse(); + assertThat(PropertyAccessorUtils.matchesProperty("admin[user]", "user")).isFalse(); + + assertThat(PropertyAccessorUtils.matchesProperty("user", "user")).isTrue(); + assertThat(PropertyAccessorUtils.matchesProperty("user[name]", "user")).isTrue(); } @Test - public void testCanonicalPropertyNames() { + public void canonicalPropertyName() { + assertThat(PropertyAccessorUtils.canonicalPropertyName(null)).isEqualTo(""); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map")).isEqualTo("map"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map[key1]")).isEqualTo("map[key1]"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map['key1']")).isEqualTo("map[key1]"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map[\"key1\"]")).isEqualTo("map[key1]"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map[key1][key2]")).isEqualTo("map[key1][key2]"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map['key1'][\"key2\"]")).isEqualTo("map[key1][key2]"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map[key1].name")).isEqualTo("map[key1].name"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map['key1'].name")).isEqualTo("map[key1].name"); + assertThat(PropertyAccessorUtils.canonicalPropertyName("map[\"key1\"].name")).isEqualTo("map[key1].name"); + } + + @Test + public void canonicalPropertyNames() { + assertThat(PropertyAccessorUtils.canonicalPropertyNames(null)).isNull(); + String[] original = new String[] {"map", "map[key1]", "map['key1']", "map[\"key1\"]", "map[key1][key2]", "map['key1'][\"key2\"]", "map[key1].name", "map['key1'].name", "map[\"key1\"].name"}; @@ -50,7 +92,7 @@ public void testCanonicalPropertyNames() { new String[] {"map", "map[key1]", "map[key1]", "map[key1]", "map[key1][key2]", "map[key1][key2]", "map[key1].name", "map[key1].name", "map[key1].name"}; - assertTrue(Arrays.equals(canonical, PropertyAccessorUtils.canonicalPropertyNames(original))); + assertThat(PropertyAccessorUtils.canonicalPropertyNames(original)).isEqualTo(canonical); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/PropertyMatchesTests.java b/spring-beans/src/test/java/org/springframework/beans/PropertyMatchesTests.java index c37810ac3d0d..09e72571e00f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/PropertyMatchesTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/PropertyMatchesTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,10 +16,14 @@ package org.springframework.beans; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + + + + -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; /** * Tests for {@link PropertyMatches}. @@ -31,96 +35,97 @@ public class PropertyMatchesTests { @Test public void simpleBeanPropertyTypo() { PropertyMatches matches = PropertyMatches.forProperty("naem", SampleBeanProperties.class); - assertThat(matches.getPossibleMatches(), hasItemInArray("name")); + assertThat(matches.getPossibleMatches()).contains("name"); } @Test public void complexBeanPropertyTypo() { PropertyMatches matches = PropertyMatches.forProperty("desriptn", SampleBeanProperties.class); - assertThat(matches.getPossibleMatches(), emptyArray()); + assertThat(matches.getPossibleMatches()).isEmpty(); } @Test public void unknownBeanProperty() { PropertyMatches matches = PropertyMatches.forProperty("unknown", SampleBeanProperties.class); - assertThat(matches.getPossibleMatches(), emptyArray()); + assertThat(matches.getPossibleMatches()).isEmpty(); } @Test public void severalMatchesBeanProperty() { PropertyMatches matches = PropertyMatches.forProperty("counter", SampleBeanProperties.class); - assertThat(matches.getPossibleMatches(), hasItemInArray("counter1")); - assertThat(matches.getPossibleMatches(), hasItemInArray("counter2")); - assertThat(matches.getPossibleMatches(), hasItemInArray("counter3")); + assertThat(matches.getPossibleMatches()).contains("counter1"); + assertThat(matches.getPossibleMatches()).contains("counter2"); + assertThat(matches.getPossibleMatches()).contains("counter3"); } @Test public void simpleBeanPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forProperty("naem", SampleBeanProperties.class); String msg = matches.buildErrorMessage(); - assertThat(msg, containsString("naem")); - assertThat(msg, containsString("name")); - assertThat(msg, containsString("setter")); - assertThat(msg, not(containsString("field"))); + assertThat(msg).contains("naem"); + assertThat(msg).contains("name"); + assertThat(msg).contains("setter"); + assertThat(msg).doesNotContain("field"); } @Test public void complexBeanPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forProperty("counter", SampleBeanProperties.class); String msg = matches.buildErrorMessage(); - assertThat(msg, containsString("counter")); - assertThat(msg, containsString("counter1")); - assertThat(msg, containsString("counter2")); - assertThat(msg, containsString("counter3")); + assertThat(msg).contains("counter"); + assertThat(msg).contains("counter1"); + assertThat(msg).contains("counter2"); + assertThat(msg).contains("counter3"); } @Test public void simpleFieldPropertyTypo() { PropertyMatches matches = PropertyMatches.forField("naem", SampleFieldProperties.class); - assertThat(matches.getPossibleMatches(), hasItemInArray("name")); + assertThat(matches.getPossibleMatches()).contains("name"); } @Test public void complexFieldPropertyTypo() { PropertyMatches matches = PropertyMatches.forField("desriptn", SampleFieldProperties.class); - assertThat(matches.getPossibleMatches(), emptyArray()); + assertThat(matches.getPossibleMatches()).isEmpty(); } @Test public void unknownFieldProperty() { PropertyMatches matches = PropertyMatches.forField("unknown", SampleFieldProperties.class); - assertThat(matches.getPossibleMatches(), emptyArray()); + assertThat(matches.getPossibleMatches()).isEmpty(); } @Test public void severalMatchesFieldProperty() { PropertyMatches matches = PropertyMatches.forField("counter", SampleFieldProperties.class); - assertThat(matches.getPossibleMatches(), hasItemInArray("counter1")); - assertThat(matches.getPossibleMatches(), hasItemInArray("counter2")); - assertThat(matches.getPossibleMatches(), hasItemInArray("counter3")); + assertThat(matches.getPossibleMatches()).contains("counter1"); + assertThat(matches.getPossibleMatches()).contains("counter2"); + assertThat(matches.getPossibleMatches()).contains("counter3"); } @Test public void simpleFieldPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forField("naem", SampleFieldProperties.class); String msg = matches.buildErrorMessage(); - assertThat(msg, containsString("naem")); - assertThat(msg, containsString("name")); - assertThat(msg, containsString("field")); - assertThat(msg, not(containsString("setter"))); + assertThat(msg).contains("naem"); + assertThat(msg).contains("name"); + assertThat(msg).contains("field"); + assertThat(msg).doesNotContain("setter"); } @Test public void complexFieldPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forField("counter", SampleFieldProperties.class); String msg = matches.buildErrorMessage(); - assertThat(msg, containsString("counter")); - assertThat(msg, containsString("counter1")); - assertThat(msg, containsString("counter2")); - assertThat(msg, containsString("counter3")); + assertThat(msg).contains("counter"); + assertThat(msg).contains("counter1"); + assertThat(msg).contains("counter2"); + assertThat(msg).contains("counter3"); } + @SuppressWarnings("unused") private static class SampleBeanProperties { private String name; @@ -175,6 +180,7 @@ public void setCounter3(int counter3) { } + @SuppressWarnings("unused") private static class SampleFieldProperties { private String name; diff --git a/spring-beans/src/test/java/org/springframework/beans/SimplePropertyDescriptorTests.java b/spring-beans/src/test/java/org/springframework/beans/SimplePropertyDescriptorTests.java index 72cf0cf8960f..4a154ef10a47 100644 --- a/spring-beans/src/test/java/org/springframework/beans/SimplePropertyDescriptorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/SimplePropertyDescriptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,10 +21,10 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Method; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; /** * @author Chris Beams @@ -36,8 +36,8 @@ public class SimplePropertyDescriptorTests { public void toStringOutput() throws IntrospectionException, SecurityException, NoSuchMethodException { { Object pd = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null); - assertThat(pd.toString(), containsString( - "PropertyDescriptor[name=foo, propertyType=null, readMethod=null")); + assertThat(pd.toString()).contains( + "PropertyDescriptor[name=foo, propertyType=null, readMethod=null"); } { class C { @@ -46,15 +46,15 @@ class C { } Method m = C.class.getMethod("setFoo", String.class); Object pd = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, m); - assertThat(pd.toString(), allOf( - containsString("PropertyDescriptor[name=foo"), - containsString("propertyType=class java.lang.String"), - containsString("readMethod=null, writeMethod=public java.lang.Object"))); + assertThat(pd.toString()).contains( + "PropertyDescriptor[name=foo", + "propertyType=class java.lang.String", + "readMethod=null, writeMethod=public java.lang.Object"); } { Object pd = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null); - assertThat(pd.toString(), containsString( - "PropertyDescriptor[name=foo, propertyType=null, indexedPropertyType=null")); + assertThat(pd.toString()).contains( + "PropertyDescriptor[name=foo, propertyType=null, indexedPropertyType=null"); } { class C { @@ -63,21 +63,21 @@ class C { } Method m = C.class.getMethod("setFoo", int.class, String.class); Object pd = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, m); - assertThat(pd.toString(), allOf( - containsString("PropertyDescriptor[name=foo, propertyType=null"), - containsString("indexedPropertyType=class java.lang.String"), - containsString("indexedWriteMethod=public java.lang.Object"))); + assertThat(pd.toString()).contains( + "PropertyDescriptor[name=foo, propertyType=null", + "indexedPropertyType=class java.lang.String", + "indexedWriteMethod=public java.lang.Object"); } } @Test public void nonIndexedEquality() throws IntrospectionException, SecurityException, NoSuchMethodException { Object pd1 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null); - assertThat(pd1, equalTo(pd1)); + assertThat(pd1).isEqualTo(pd1); Object pd2 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null); - assertThat(pd1, equalTo(pd2)); - assertThat(pd2, equalTo(pd1)); + assertThat(pd1).isEqualTo(pd2); + assertThat(pd2).isEqualTo(pd1); @SuppressWarnings("unused") class C { @@ -86,35 +86,35 @@ class C { } Method wm1 = C.class.getMethod("setFoo", String.class); Object pd3 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, wm1); - assertThat(pd1, not(equalTo(pd3))); - assertThat(pd3, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd3); + assertThat(pd3).isNotEqualTo(pd1); Method rm1 = C.class.getMethod("getFoo"); Object pd4 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", rm1, null); - assertThat(pd1, not(equalTo(pd4))); - assertThat(pd4, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd4); + assertThat(pd4).isNotEqualTo(pd1); Object pd5 = new PropertyDescriptor("foo", null, null); - assertThat(pd1, equalTo(pd5)); - assertThat(pd5, equalTo(pd1)); + assertThat(pd1).isEqualTo(pd5); + assertThat(pd5).isEqualTo(pd1); Object pd6 = "not a PD"; - assertThat(pd1, not(equalTo(pd6))); - assertThat(pd6, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd6); + assertThat(pd6).isNotEqualTo(pd1); Object pd7 = null; - assertThat(pd1, not(equalTo(pd7))); - assertThat(pd7, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd7); + assertThat(pd7).isNotEqualTo(pd1); } @Test public void indexedEquality() throws IntrospectionException, SecurityException, NoSuchMethodException { Object pd1 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null); - assertThat(pd1, equalTo(pd1)); + assertThat(pd1).isEqualTo(pd1); Object pd2 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null); - assertThat(pd1, equalTo(pd2)); - assertThat(pd2, equalTo(pd1)); + assertThat(pd1).isEqualTo(pd2); + assertThat(pd2).isEqualTo(pd1); @SuppressWarnings("unused") class C { @@ -123,25 +123,25 @@ class C { } Method wm1 = C.class.getMethod("setFoo", int.class, String.class); Object pd3 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, wm1); - assertThat(pd1, not(equalTo(pd3))); - assertThat(pd3, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd3); + assertThat(pd3).isNotEqualTo(pd1); Method rm1 = C.class.getMethod("getFoo", int.class); Object pd4 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, rm1, null); - assertThat(pd1, not(equalTo(pd4))); - assertThat(pd4, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd4); + assertThat(pd4).isNotEqualTo(pd1); Object pd5 = new IndexedPropertyDescriptor("foo", null, null, null, null); - assertThat(pd1, equalTo(pd5)); - assertThat(pd5, equalTo(pd1)); + assertThat(pd1).isEqualTo(pd5); + assertThat(pd5).isEqualTo(pd1); Object pd6 = "not a PD"; - assertThat(pd1, not(equalTo(pd6))); - assertThat(pd6, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd6); + assertThat(pd6).isNotEqualTo(pd1); Object pd7 = null; - assertThat(pd1, not(equalTo(pd7))); - assertThat(pd7, not(equalTo(pd1))); + assertThat(pd1).isNotEqualTo(pd7); + assertThat(pd7).isNotEqualTo(pd1); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java index 587f30261ee5..e869c9c6c906 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -16,33 +16,37 @@ package org.springframework.beans.factory; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.Arrays; import java.util.List; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.StaticListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.testfixture.beans.AnnotatedBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.IndexedTestBean; +import org.springframework.beans.testfixture.beans.TestAnnotation; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.beans.testfixture.beans.factory.DummyFactory; import org.springframework.cglib.proxy.NoOp; +import org.springframework.core.annotation.AliasFor; import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.AnnotatedBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.IndexedTestBean; -import org.springframework.tests.sample.beans.TestAnnotation; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.tests.sample.beans.factory.DummyFactory; import org.springframework.util.ObjectUtils; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rod Johnson * @author Juergen Hoeller * @author Chris Beams + * @author Sam Brannen * @since 04.07.2003 */ public class BeanFactoryUtilsTests { @@ -58,10 +62,9 @@ public class BeanFactoryUtilsTests { private DefaultListableBeanFactory dependentBeansFactory; - @Before - public void setUp() { + @BeforeEach + public void setup() { // Interesting hierarchical factory to test counts. - // Slow to read so we cache it. DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT); @@ -82,48 +85,47 @@ public void testHierarchicalCountBeansWithNonHierarchicalFactory() { StaticListableBeanFactory lbf = new StaticListableBeanFactory(); lbf.addBean("t1", new TestBean()); lbf.addBean("t2", new TestBean()); - assertTrue(BeanFactoryUtils.countBeansIncludingAncestors(lbf) == 2); + assertThat(BeanFactoryUtils.countBeansIncludingAncestors(lbf) == 2).isTrue(); } /** * Check that override doesn't count as two separate beans. */ @Test - public void testHierarchicalCountBeansWithOverride() throws Exception { + public void testHierarchicalCountBeansWithOverride() { // Leaf count - assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1); + assertThat(this.listableBeanFactory.getBeanDefinitionCount() == 1).isTrue(); // Count minus duplicate - assertTrue("Should count 8 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory), - BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 8); + assertThat(BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 8).as("Should count 8 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory)).isTrue(); } @Test - public void testHierarchicalNamesWithNoMatch() throws Exception { + public void testHierarchicalNamesWithNoMatch() { List names = Arrays.asList( BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, NoOp.class)); - assertEquals(0, names.size()); + assertThat(names.size()).isEqualTo(0); } @Test - public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception { + public void testHierarchicalNamesWithMatchOnlyInRoot() { List names = Arrays.asList( BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class)); - assertEquals(1, names.size()); - assertTrue(names.contains("indexedBean")); + assertThat(names.size()).isEqualTo(1); + assertThat(names.contains("indexedBean")).isTrue(); // Distinguish from default ListableBeanFactory behavior - assertTrue(listableBeanFactory.getBeanNamesForType(IndexedTestBean.class).length == 0); + assertThat(listableBeanFactory.getBeanNamesForType(IndexedTestBean.class).length == 0).isTrue(); } @Test - public void testGetBeanNamesForTypeWithOverride() throws Exception { + public void testGetBeanNamesForTypeWithOverride() { List names = Arrays.asList( BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class)); // includes 2 TestBeans from FactoryBeans (DummyFactory definitions) - assertEquals(4, names.size()); - assertTrue(names.contains("test")); - assertTrue(names.contains("test3")); - assertTrue(names.contains("testFactory1")); - assertTrue(names.contains("testFactory2")); + assertThat(names.size()).isEqualTo(4); + assertThat(names.contains("test")).isTrue(); + assertThat(names.contains("test3")).isTrue(); + assertThat(names.contains("testFactory1")).isTrue(); + assertThat(names.contains("testFactory2")).isTrue(); } @Test @@ -131,7 +133,7 @@ public void testNoBeansOfType() { StaticListableBeanFactory lbf = new StaticListableBeanFactory(); lbf.addBean("foo", new Object()); Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false); - assertTrue(beans.isEmpty()); + assertThat(beans.isEmpty()).isTrue(); } @Test @@ -148,21 +150,22 @@ public void testFindsBeansOfTypeWithStaticFactory() { lbf.addBean("t4", t4); Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true); - assertEquals(4, beans.size()); - assertEquals(t1, beans.get("t1")); - assertEquals(t2, beans.get("t2")); - assertEquals(t3.getObject(), beans.get("t3")); - assertTrue(beans.get("t4") instanceof TestBean); + assertThat(beans.size()).isEqualTo(4); + assertThat(beans.get("t1")).isEqualTo(t1); + assertThat(beans.get("t2")).isEqualTo(t2); + assertThat(beans.get("t3")).isEqualTo(t3.getObject()); + boolean condition = beans.get("t4") instanceof TestBean; + assertThat(condition).isTrue(); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true); - assertEquals(2, beans.size()); - assertEquals(t3, beans.get("&t3")); - assertEquals(t4, beans.get("&t4")); + assertThat(beans.size()).isEqualTo(2); + assertThat(beans.get("&t3")).isEqualTo(t3); + assertThat(beans.get("&t4")).isEqualTo(t4); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true); - assertEquals(2, beans.size()); - assertEquals(t3, beans.get("&t3")); - assertEquals(t4, beans.get("&t4")); + assertThat(beans.size()).isEqualTo(2); + assertThat(beans.get("&t3")).isEqualTo(t3); + assertThat(beans.get("&t4")).isEqualTo(t4); } @Test @@ -182,141 +185,309 @@ public void testFindsBeansOfTypeWithDefaultFactory() { Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false); - assertEquals(6, beans.size()); - assertEquals(test3, beans.get("test3")); - assertEquals(test, beans.get("test")); - assertEquals(t1, beans.get("t1")); - assertEquals(t2, beans.get("t2")); - assertEquals(t3.getObject(), beans.get("t3")); - assertTrue(beans.get("t4") instanceof TestBean); + assertThat(beans.size()).isEqualTo(6); + assertThat(beans.get("test3")).isEqualTo(test3); + assertThat(beans.get("test")).isEqualTo(test); + assertThat(beans.get("t1")).isEqualTo(t1); + assertThat(beans.get("t2")).isEqualTo(t2); + assertThat(beans.get("t3")).isEqualTo(t3.getObject()); + boolean condition2 = beans.get("t4") instanceof TestBean; + assertThat(condition2).isTrue(); // t3 and t4 are found here as of Spring 2.0, since they are pre-registered // singleton instances, while testFactory1 and testFactory are *not* found // because they are FactoryBean definitions that haven't been initialized yet. beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true); Object testFactory1 = this.listableBeanFactory.getBean("testFactory1"); - assertEquals(5, beans.size()); - assertEquals(test, beans.get("test")); - assertEquals(testFactory1, beans.get("testFactory1")); - assertEquals(t1, beans.get("t1")); - assertEquals(t2, beans.get("t2")); - assertEquals(t3.getObject(), beans.get("t3")); + assertThat(beans.size()).isEqualTo(5); + assertThat(beans.get("test")).isEqualTo(test); + assertThat(beans.get("testFactory1")).isEqualTo(testFactory1); + assertThat(beans.get("t1")).isEqualTo(t1); + assertThat(beans.get("t2")).isEqualTo(t2); + assertThat(beans.get("t3")).isEqualTo(t3.getObject()); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true); - assertEquals(8, beans.size()); - assertEquals(test3, beans.get("test3")); - assertEquals(test, beans.get("test")); - assertEquals(testFactory1, beans.get("testFactory1")); - assertTrue(beans.get("testFactory2") instanceof TestBean); - assertEquals(t1, beans.get("t1")); - assertEquals(t2, beans.get("t2")); - assertEquals(t3.getObject(), beans.get("t3")); - assertTrue(beans.get("t4") instanceof TestBean); + assertThat(beans.size()).isEqualTo(8); + assertThat(beans.get("test3")).isEqualTo(test3); + assertThat(beans.get("test")).isEqualTo(test); + assertThat(beans.get("testFactory1")).isEqualTo(testFactory1); + boolean condition1 = beans.get("testFactory2") instanceof TestBean; + assertThat(condition1).isTrue(); + assertThat(beans.get("t1")).isEqualTo(t1); + assertThat(beans.get("t2")).isEqualTo(t2); + assertThat(beans.get("t3")).isEqualTo(t3.getObject()); + boolean condition = beans.get("t4") instanceof TestBean; + assertThat(condition).isTrue(); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true); - assertEquals(4, beans.size()); - assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1")); - assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2")); - assertEquals(t3, beans.get("&t3")); - assertEquals(t4, beans.get("&t4")); + assertThat(beans.size()).isEqualTo(4); + assertThat(beans.get("&testFactory1")).isEqualTo(this.listableBeanFactory.getBean("&testFactory1")); + assertThat(beans.get("&testFactory2")).isEqualTo(this.listableBeanFactory.getBean("&testFactory2")); + assertThat(beans.get("&t3")).isEqualTo(t3); + assertThat(beans.get("&t4")).isEqualTo(t4); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true); - assertEquals(4, beans.size()); - assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1")); - assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2")); - assertEquals(t3, beans.get("&t3")); - assertEquals(t4, beans.get("&t4")); + assertThat(beans.size()).isEqualTo(4); + assertThat(beans.get("&testFactory1")).isEqualTo(this.listableBeanFactory.getBean("&testFactory1")); + assertThat(beans.get("&testFactory2")).isEqualTo(this.listableBeanFactory.getBean("&testFactory2")); + assertThat(beans.get("&t3")).isEqualTo(t3); + assertThat(beans.get("&t4")).isEqualTo(t4); } @Test - public void testHierarchicalResolutionWithOverride() throws Exception { + public void testHierarchicalResolutionWithOverride() { Object test3 = this.listableBeanFactory.getBean("test3"); Object test = this.listableBeanFactory.getBean("test"); Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false); - assertEquals(2, beans.size()); - assertEquals(test3, beans.get("test3")); - assertEquals(test, beans.get("test")); + assertThat(beans.size()).isEqualTo(2); + assertThat(beans.get("test3")).isEqualTo(test3); + assertThat(beans.get("test")).isEqualTo(test); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, false); - assertEquals(1, beans.size()); - assertEquals(test, beans.get("test")); + assertThat(beans.size()).isEqualTo(1); + assertThat(beans.get("test")).isEqualTo(test); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true); Object testFactory1 = this.listableBeanFactory.getBean("testFactory1"); - assertEquals(2, beans.size()); - assertEquals(test, beans.get("test")); - assertEquals(testFactory1, beans.get("testFactory1")); + assertThat(beans.size()).isEqualTo(2); + assertThat(beans.get("test")).isEqualTo(test); + assertThat(beans.get("testFactory1")).isEqualTo(testFactory1); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true); - assertEquals(4, beans.size()); - assertEquals(test3, beans.get("test3")); - assertEquals(test, beans.get("test")); - assertEquals(testFactory1, beans.get("testFactory1")); - assertTrue(beans.get("testFactory2") instanceof TestBean); + assertThat(beans.size()).isEqualTo(4); + assertThat(beans.get("test3")).isEqualTo(test3); + assertThat(beans.get("test")).isEqualTo(test); + assertThat(beans.get("testFactory1")).isEqualTo(testFactory1); + boolean condition = beans.get("testFactory2") instanceof TestBean; + assertThat(condition).isTrue(); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true); - assertEquals(2, beans.size()); - assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1")); - assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2")); + assertThat(beans.size()).isEqualTo(2); + assertThat(beans.get("&testFactory1")).isEqualTo(this.listableBeanFactory.getBean("&testFactory1")); + assertThat(beans.get("&testFactory2")).isEqualTo(this.listableBeanFactory.getBean("&testFactory2")); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true); - assertEquals(2, beans.size()); - assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1")); - assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2")); + assertThat(beans.size()).isEqualTo(2); + assertThat(beans.get("&testFactory1")).isEqualTo(this.listableBeanFactory.getBean("&testFactory1")); + assertThat(beans.get("&testFactory2")).isEqualTo(this.listableBeanFactory.getBean("&testFactory2")); } @Test - public void testHierarchicalNamesForAnnotationWithNoMatch() throws Exception { + public void testHierarchicalNamesForAnnotationWithNoMatch() { List names = Arrays.asList( BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(this.listableBeanFactory, Override.class)); - assertEquals(0, names.size()); + assertThat(names.size()).isEqualTo(0); } @Test - public void testHierarchicalNamesForAnnotationWithMatchOnlyInRoot() throws Exception { + public void testHierarchicalNamesForAnnotationWithMatchOnlyInRoot() { List names = Arrays.asList( BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(this.listableBeanFactory, TestAnnotation.class)); - assertEquals(1, names.size()); - assertTrue(names.contains("annotatedBean")); + assertThat(names.size()).isEqualTo(1); + assertThat(names.contains("annotatedBean")).isTrue(); // Distinguish from default ListableBeanFactory behavior - assertTrue(listableBeanFactory.getBeanNamesForAnnotation(TestAnnotation.class).length == 0); + assertThat(listableBeanFactory.getBeanNamesForAnnotation(TestAnnotation.class).length == 0).isTrue(); } @Test - public void testGetBeanNamesForAnnotationWithOverride() throws Exception { + public void testGetBeanNamesForAnnotationWithOverride() { AnnotatedBean annotatedBean = new AnnotatedBean(); this.listableBeanFactory.registerSingleton("anotherAnnotatedBean", annotatedBean); List names = Arrays.asList( BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(this.listableBeanFactory, TestAnnotation.class)); - assertEquals(2, names.size()); - assertTrue(names.contains("annotatedBean")); - assertTrue(names.contains("anotherAnnotatedBean")); + assertThat(names.size()).isEqualTo(2); + assertThat(names.contains("annotatedBean")).isTrue(); + assertThat(names.contains("anotherAnnotatedBean")).isTrue(); } @Test public void testADependencies() { String[] deps = this.dependentBeansFactory.getDependentBeans("a"); - assertTrue(ObjectUtils.isEmpty(deps)); + assertThat(ObjectUtils.isEmpty(deps)).isTrue(); } @Test public void testBDependencies() { String[] deps = this.dependentBeansFactory.getDependentBeans("b"); - assertTrue(Arrays.equals(new String[] { "c" }, deps)); + assertThat(Arrays.equals(new String[] { "c" }, deps)).isTrue(); } @Test public void testCDependencies() { String[] deps = this.dependentBeansFactory.getDependentBeans("c"); - assertTrue(Arrays.equals(new String[] { "int", "long" }, deps)); + assertThat(Arrays.equals(new String[] { "int", "long" }, deps)).isTrue(); } @Test public void testIntDependencies() { String[] deps = this.dependentBeansFactory.getDependentBeans("int"); - assertTrue(Arrays.equals(new String[] { "buffer" }, deps)); + assertThat(Arrays.equals(new String[] { "buffer" }, deps)).isTrue(); + } + + @Test + public void findAnnotationOnBean() { + this.listableBeanFactory.registerSingleton("controllerAdvice", new ControllerAdviceClass()); + this.listableBeanFactory.registerSingleton("restControllerAdvice", new RestControllerAdviceClass()); + testFindAnnotationOnBean(this.listableBeanFactory); + } + + @Test // gh-25520 + public void findAnnotationOnBeanWithStaticFactory() { + StaticListableBeanFactory lbf = new StaticListableBeanFactory(); + lbf.addBean("controllerAdvice", new ControllerAdviceClass()); + lbf.addBean("restControllerAdvice", new RestControllerAdviceClass()); + testFindAnnotationOnBean(lbf); + } + + private void testFindAnnotationOnBean(ListableBeanFactory lbf) { + assertControllerAdvice(lbf, "controllerAdvice"); + assertControllerAdvice(lbf, "restControllerAdvice"); + } + + private void assertControllerAdvice(ListableBeanFactory lbf, String beanName) { + ControllerAdvice controllerAdvice = lbf.findAnnotationOnBean(beanName, ControllerAdvice.class); + assertThat(controllerAdvice).isNotNull(); + assertThat(controllerAdvice.value()).isEqualTo("com.example"); + assertThat(controllerAdvice.basePackage()).isEqualTo("com.example"); + } + + @Test + public void isSingletonAndIsPrototypeWithStaticFactory() { + StaticListableBeanFactory lbf = new StaticListableBeanFactory(); + TestBean bean = new TestBean(); + DummyFactory fb1 = new DummyFactory(); + DummyFactory fb2 = new DummyFactory(); + fb2.setSingleton(false); + TestBeanSmartFactoryBean sfb1 = new TestBeanSmartFactoryBean(true, true); + TestBeanSmartFactoryBean sfb2 = new TestBeanSmartFactoryBean(true, false); + TestBeanSmartFactoryBean sfb3 = new TestBeanSmartFactoryBean(false, true); + TestBeanSmartFactoryBean sfb4 = new TestBeanSmartFactoryBean(false, false); + lbf.addBean("bean", bean); + lbf.addBean("fb1", fb1); + lbf.addBean("fb2", fb2); + lbf.addBean("sfb1", sfb1); + lbf.addBean("sfb2", sfb2); + lbf.addBean("sfb3", sfb3); + lbf.addBean("sfb4", sfb4); + + Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true); + assertThat(beans.get("bean")).isSameAs(bean); + assertThat(beans.get("fb1")).isSameAs(fb1.getObject()); + assertThat(beans.get("fb2")).isInstanceOf(TestBean.class); + assertThat(beans.get("sfb1")).isInstanceOf(TestBean.class); + assertThat(beans.get("sfb2")).isInstanceOf(TestBean.class); + assertThat(beans.get("sfb3")).isInstanceOf(TestBean.class); + assertThat(beans.get("sfb4")).isInstanceOf(TestBean.class); + + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(7); + assertThat(lbf.getBean("bean")).isInstanceOf(TestBean.class); + assertThat(lbf.getBean("&fb1")).isInstanceOf(FactoryBean.class); + assertThat(lbf.getBean("&fb2")).isInstanceOf(FactoryBean.class); + assertThat(lbf.getBean("&sfb1")).isInstanceOf(SmartFactoryBean.class); + assertThat(lbf.getBean("&sfb2")).isInstanceOf(SmartFactoryBean.class); + assertThat(lbf.getBean("&sfb3")).isInstanceOf(SmartFactoryBean.class); + assertThat(lbf.getBean("&sfb4")).isInstanceOf(SmartFactoryBean.class); + + assertThat(lbf.isSingleton("bean")).isTrue(); + assertThat(lbf.isSingleton("fb1")).isTrue(); + assertThat(lbf.isSingleton("fb2")).isTrue(); + assertThat(lbf.isSingleton("sfb1")).isTrue(); + assertThat(lbf.isSingleton("sfb2")).isTrue(); + assertThat(lbf.isSingleton("sfb3")).isTrue(); + assertThat(lbf.isSingleton("sfb4")).isTrue(); + + assertThat(lbf.isSingleton("&fb1")).isTrue(); + assertThat(lbf.isSingleton("&fb2")).isFalse(); + assertThat(lbf.isSingleton("&sfb1")).isTrue(); + assertThat(lbf.isSingleton("&sfb2")).isTrue(); + assertThat(lbf.isSingleton("&sfb3")).isFalse(); + assertThat(lbf.isSingleton("&sfb4")).isFalse(); + + assertThat(lbf.isPrototype("bean")).isFalse(); + assertThat(lbf.isPrototype("fb1")).isFalse(); + assertThat(lbf.isPrototype("fb2")).isFalse(); + assertThat(lbf.isPrototype("sfb1")).isFalse(); + assertThat(lbf.isPrototype("sfb2")).isFalse(); + assertThat(lbf.isPrototype("sfb3")).isFalse(); + assertThat(lbf.isPrototype("sfb4")).isFalse(); + + assertThat(lbf.isPrototype("&fb1")).isFalse(); + assertThat(lbf.isPrototype("&fb2")).isTrue(); + assertThat(lbf.isPrototype("&sfb1")).isTrue(); + assertThat(lbf.isPrototype("&sfb2")).isFalse(); + assertThat(lbf.isPrototype("&sfb3")).isTrue(); + assertThat(lbf.isPrototype("&sfb4")).isTrue(); + } + + + @Retention(RetentionPolicy.RUNTIME) + @interface ControllerAdvice { + + @AliasFor("basePackage") + String value() default ""; + + @AliasFor("value") + String basePackage() default ""; + } + + + @Retention(RetentionPolicy.RUNTIME) + @ControllerAdvice + @interface RestControllerAdvice { + + @AliasFor(annotation = ControllerAdvice.class) + String value() default ""; + + @AliasFor(annotation = ControllerAdvice.class) + String basePackage() default ""; + } + + + @ControllerAdvice("com.example") + static class ControllerAdviceClass { + } + + + @RestControllerAdvice("com.example") + static class RestControllerAdviceClass { + } + + + static class TestBeanSmartFactoryBean implements SmartFactoryBean { + + private final TestBean testBean = new TestBean("enigma", 42); + + private final boolean singleton; + + private final boolean prototype; + + TestBeanSmartFactoryBean(boolean singleton, boolean prototype) { + this.singleton = singleton; + this.prototype = prototype; + } + + @Override + public boolean isSingleton() { + return this.singleton; + } + + @Override + public boolean isPrototype() { + return this.prototype; + } + + @Override + public Class getObjectType() { + return TestBean.class; + } + + public TestBean getObject() { + // We don't really care if the actual instance is a singleton or prototype + // for the tests that use this factory. + return this.testBean; + } } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/ConcurrentBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/ConcurrentBeanFactoryTests.java index c21ab35c9f5c..2e4857b4ccd1 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/ConcurrentBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/ConcurrentBeanFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -27,20 +27,17 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.springframework.beans.PropertyEditorRegistrar; -import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.propertyeditors.CustomDateEditor; -import org.springframework.core.io.Resource; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; +import org.springframework.core.testfixture.EnabledForTestGroups; +import org.springframework.core.testfixture.TestGroup; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Guillaume Poirier @@ -48,12 +45,11 @@ * @author Chris Beams * @since 10.03.2004 */ +@EnabledForTestGroups(TestGroup.PERFORMANCE) public class ConcurrentBeanFactoryTests { - private static final Log logger = LogFactory.getLog(ConcurrentBeanFactoryTests.class); - private static final Resource CONTEXT = qualifiedResource(ConcurrentBeanFactoryTests.class, "context.xml"); - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd"); + private static final Date DATE_1, DATE_2; static { @@ -66,27 +62,30 @@ public class ConcurrentBeanFactoryTests { } } + + private static final Log logger = LogFactory.getLog(ConcurrentBeanFactoryTests.class); + private BeanFactory factory; - private final Set set = Collections.synchronizedSet(new HashSet()); + private final Set set = Collections.synchronizedSet(new HashSet<>()); - private Throwable ex = null; + private Throwable ex; - @Before - public void setUp() throws Exception { - Assume.group(TestGroup.PERFORMANCE); + @BeforeEach + public void setup() throws Exception { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CONTEXT); - factory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() { - @Override - public void registerCustomEditors(PropertyEditorRegistry registry) { - registry.registerCustomEditor(Date.class, new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false)); - } - }); + new XmlBeanDefinitionReader(factory).loadBeanDefinitions( + qualifiedResource(ConcurrentBeanFactoryTests.class, "context.xml")); + + factory.addPropertyEditorRegistrar( + registry -> registry.registerCustomEditor(Date.class, + new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false))); + this.factory = factory; } + @Test public void testSingleThread() { for (int i = 0; i < 100; i++) { @@ -118,7 +117,7 @@ public void testConcurrent() { } } if (ex != null) { - fail(ex.getMessage()); + throw new AssertionError("Unexpected exception", ex); } } @@ -126,8 +125,8 @@ private void performTest() { ConcurrentBean b1 = (ConcurrentBean) factory.getBean("bean1"); ConcurrentBean b2 = (ConcurrentBean) factory.getBean("bean2"); - assertEquals(DATE_1, b1.getDate()); - assertEquals(DATE_2, b2.getDate()); + assertThat(b1.getDate()).isEqualTo(DATE_1); + assertThat(b2.getDate()).isEqualTo(DATE_2); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index 1b27b8e4b078..b4aac030245d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -38,16 +38,15 @@ import java.util.Set; import java.util.concurrent.Callable; import java.util.stream.Collectors; +import java.util.stream.IntStream; + import javax.annotation.Priority; import javax.security.auth.Subject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.ArgumentMatchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; @@ -58,11 +57,11 @@ import org.springframework.beans.TypeConverter; import org.springframework.beans.TypeMismatchException; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.beans.factory.config.AutowiredPropertyMarker; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanExpressionContext; import org.springframework.beans.factory.config.BeanExpressionResolver; import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; import org.springframework.beans.factory.config.PropertiesFactoryBean; @@ -79,6 +78,14 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.ConstructorDependenciesBean; import org.springframework.beans.propertyeditors.CustomNumberEditor; +import org.springframework.beans.testfixture.beans.DependenciesBean; +import org.springframework.beans.testfixture.beans.DerivedTestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.LifecycleBean; +import org.springframework.beans.testfixture.beans.NestedTestBean; +import org.springframework.beans.testfixture.beans.SideEffectBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.beans.testfixture.beans.factory.DummyFactory; import org.springframework.core.MethodParameter; import org.springframework.core.ResolvableType; import org.springframework.core.annotation.AnnotationAwareOrderComparator; @@ -87,24 +94,26 @@ import org.springframework.core.convert.support.GenericConversionService; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import org.springframework.core.testfixture.Assume; +import org.springframework.core.testfixture.EnabledForTestGroups; +import org.springframework.core.testfixture.TestGroup; +import org.springframework.core.testfixture.io.SerializationTestUtils; +import org.springframework.core.testfixture.security.TestPrincipal; import org.springframework.lang.Nullable; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; -import org.springframework.tests.sample.beans.DependenciesBean; -import org.springframework.tests.sample.beans.DerivedTestBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.LifecycleBean; -import org.springframework.tests.sample.beans.NestedTestBean; -import org.springframework.tests.sample.beans.SideEffectBean; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.tests.sample.beans.factory.DummyFactory; -import org.springframework.util.SerializationTestUtils; import org.springframework.util.StopWatch; import org.springframework.util.StringValueResolver; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; /** * Tests properties population and autowire behavior. @@ -117,69 +126,63 @@ * @author Phillip Webb * @author Stephane Nicoll */ -public class DefaultListableBeanFactoryTests { +class DefaultListableBeanFactoryTests { private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class); - - @Rule - public ExpectedException thrown = ExpectedException.none(); + private DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); @Test - public void testUnreferencedSingletonWasInstantiated() { + void unreferencedSingletonWasInstantiated() { KnowsIfInstantiated.clearInstantiationRecord(); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName()); - assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); + assertThat(!KnowsIfInstantiated.wasInstantiated()).as("singleton not instantiated").isTrue(); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); lbf.preInstantiateSingletons(); - assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated()); + assertThat(KnowsIfInstantiated.wasInstantiated()).as("singleton was instantiated").isTrue(); } @Test - public void testLazyInitialization() { + void lazyInitialization() { KnowsIfInstantiated.clearInstantiationRecord(); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName()); p.setProperty("x1.(lazy-init)", "true"); - assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); + assertThat(!KnowsIfInstantiated.wasInstantiated()).as("singleton not instantiated").isTrue(); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); + assertThat(!KnowsIfInstantiated.wasInstantiated()).as("singleton not instantiated").isTrue(); lbf.preInstantiateSingletons(); - assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); + assertThat(!KnowsIfInstantiated.wasInstantiated()).as("singleton not instantiated").isTrue(); lbf.getBean("x1"); - assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated()); + assertThat(KnowsIfInstantiated.wasInstantiated()).as("singleton was instantiated").isTrue(); } @Test - public void testFactoryBeanDidNotCreatePrototype() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void factoryBeanDidNotCreatePrototype() { Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.singleton", "false"); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); - assertEquals(TestBean.class, lbf.getType("x1")); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); lbf.preInstantiateSingletons(); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); lbf.getBean("x1"); - assertEquals(TestBean.class, lbf.getType("x1")); - assertTrue(lbf.containsBean("x1")); - assertTrue(lbf.containsBean("&x1")); - assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated()); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isTrue(); + assertThat(DummyFactory.wasPrototypeCreated()).as("prototype was instantiated").isTrue(); } @Test - public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeFactoryBeanIgnoredByNonEagerTypeMatching() { Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state @@ -188,33 +191,32 @@ public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() { p.setProperty("x1.singleton", "false"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(0, beanNames.length); + assertThat(beanNames.length).isEqualTo(0); beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); - assertEquals(0, beanNames.length); - - assertFalse(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertTrue(lbf.containsBean("&x1")); - assertFalse(lbf.isSingleton("x1")); - assertFalse(lbf.isSingleton("&x1")); - assertTrue(lbf.isPrototype("x1")); - assertTrue(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); - assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))); - assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))); - assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(DummyFactory.class, lbf.getType("&x1")); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); - } - - @Test - public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThat(beanNames.length).isEqualTo(0); + + assertThat(lbf.containsSingleton("x1")).isFalse(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isTrue(); + assertThat(lbf.isSingleton("x1")).isFalse(); + assertThat(lbf.isSingleton("&x1")).isFalse(); + assertThat(lbf.isPrototype("x1")).isTrue(); + assertThat(lbf.isPrototype("&x1")).isTrue(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.isTypeMatch("&x1", DummyFactory.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))).isFalse(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(DummyFactory.class); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); + } + + @Test + void singletonFactoryBeanIgnoredByNonEagerTypeMatching() { Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state @@ -223,33 +225,32 @@ public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() { p.setProperty("x1.singleton", "true"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(0, beanNames.length); + assertThat(beanNames.length).isEqualTo(0); beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); - assertEquals(0, beanNames.length); - - assertFalse(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertTrue(lbf.containsBean("&x1")); - assertFalse(lbf.isSingleton("x1")); - assertFalse(lbf.isSingleton("&x1")); - assertTrue(lbf.isPrototype("x1")); - assertTrue(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); - assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))); - assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))); - assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(DummyFactory.class, lbf.getType("&x1")); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); - } - - @Test - public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThat(beanNames.length).isEqualTo(0); + + assertThat(lbf.containsSingleton("x1")).isFalse(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isTrue(); + assertThat(lbf.isSingleton("x1")).isFalse(); + assertThat(lbf.isSingleton("&x1")).isFalse(); + assertThat(lbf.isPrototype("x1")).isTrue(); + assertThat(lbf.isPrototype("&x1")).isTrue(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.isTypeMatch("&x1", DummyFactory.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))).isFalse(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(DummyFactory.class); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); + } + + @Test + void nonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() { Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state @@ -257,33 +258,32 @@ public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() { p.setProperty("x1.singleton", "false"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(0, beanNames.length); + assertThat(beanNames.length).isEqualTo(0); beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); - assertEquals(0, beanNames.length); - - assertFalse(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertTrue(lbf.containsBean("&x1")); - assertFalse(lbf.isSingleton("x1")); - assertTrue(lbf.isSingleton("&x1")); - assertTrue(lbf.isPrototype("x1")); - assertFalse(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); - assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))); - assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))); - assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(DummyFactory.class, lbf.getType("&x1")); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); - } - - @Test - public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThat(beanNames.length).isEqualTo(0); + + assertThat(lbf.containsSingleton("x1")).isFalse(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isTrue(); + assertThat(lbf.isSingleton("x1")).isFalse(); + assertThat(lbf.isSingleton("&x1")).isTrue(); + assertThat(lbf.isPrototype("x1")).isTrue(); + assertThat(lbf.isPrototype("&x1")).isFalse(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.isTypeMatch("&x1", DummyFactory.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))).isTrue(); + assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))).isFalse(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(DummyFactory.class); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); + } + + @Test + void initializedFactoryBeanFoundByNonEagerTypeMatching() { Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state @@ -292,108 +292,105 @@ public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); lbf.preInstantiateSingletons(); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(1, beanNames.length); - assertEquals("x1", beanNames[0]); - assertTrue(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertTrue(lbf.containsBean("&x1")); - assertTrue(lbf.containsLocalBean("x1")); - assertTrue(lbf.containsLocalBean("&x1")); - assertFalse(lbf.isSingleton("x1")); - assertTrue(lbf.isSingleton("&x1")); - assertTrue(lbf.isPrototype("x1")); - assertFalse(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); - assertTrue(lbf.isTypeMatch("x1", Object.class)); - assertTrue(lbf.isTypeMatch("&x1", Object.class)); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(DummyFactory.class, lbf.getType("&x1")); - assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("x1"); + assertThat(lbf.containsSingleton("x1")).isTrue(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isTrue(); + assertThat(lbf.containsLocalBean("x1")).isTrue(); + assertThat(lbf.containsLocalBean("&x1")).isTrue(); + assertThat(lbf.isSingleton("x1")).isFalse(); + assertThat(lbf.isSingleton("&x1")).isTrue(); + assertThat(lbf.isPrototype("x1")).isTrue(); + assertThat(lbf.isPrototype("&x1")).isFalse(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.isTypeMatch("&x1", DummyFactory.class)).isTrue(); + assertThat(lbf.isTypeMatch("x1", Object.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", Object.class)).isTrue(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(DummyFactory.class); + assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); lbf.registerAlias("x1", "x2"); - assertTrue(lbf.containsBean("x2")); - assertTrue(lbf.containsBean("&x2")); - assertTrue(lbf.containsLocalBean("x2")); - assertTrue(lbf.containsLocalBean("&x2")); - assertFalse(lbf.isSingleton("x2")); - assertTrue(lbf.isSingleton("&x2")); - assertTrue(lbf.isPrototype("x2")); - assertFalse(lbf.isPrototype("&x2")); - assertTrue(lbf.isTypeMatch("x2", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x2", TestBean.class)); - assertTrue(lbf.isTypeMatch("&x2", DummyFactory.class)); - assertTrue(lbf.isTypeMatch("x2", Object.class)); - assertTrue(lbf.isTypeMatch("&x2", Object.class)); - assertEquals(TestBean.class, lbf.getType("x2")); - assertEquals(DummyFactory.class, lbf.getType("&x2")); - assertEquals(1, lbf.getAliases("x1").length); - assertEquals("x2", lbf.getAliases("x1")[0]); - assertEquals(1, lbf.getAliases("&x1").length); - assertEquals("&x2", lbf.getAliases("&x1")[0]); - assertEquals(1, lbf.getAliases("x2").length); - assertEquals("x1", lbf.getAliases("x2")[0]); - assertEquals(1, lbf.getAliases("&x2").length); - assertEquals("&x1", lbf.getAliases("&x2")[0]); - } - - @Test - public void testStaticFactoryMethodFoundByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThat(lbf.containsBean("x2")).isTrue(); + assertThat(lbf.containsBean("&x2")).isTrue(); + assertThat(lbf.containsLocalBean("x2")).isTrue(); + assertThat(lbf.containsLocalBean("&x2")).isTrue(); + assertThat(lbf.isSingleton("x2")).isFalse(); + assertThat(lbf.isSingleton("&x2")).isTrue(); + assertThat(lbf.isPrototype("x2")).isTrue(); + assertThat(lbf.isPrototype("&x2")).isFalse(); + assertThat(lbf.isTypeMatch("x2", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x2", TestBean.class)).isFalse(); + assertThat(lbf.isTypeMatch("&x2", DummyFactory.class)).isTrue(); + assertThat(lbf.isTypeMatch("x2", Object.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x2", Object.class)).isTrue(); + assertThat(lbf.getType("x2")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x2")).isEqualTo(DummyFactory.class); + assertThat(lbf.getAliases("x1").length).isEqualTo(1); + assertThat(lbf.getAliases("x1")[0]).isEqualTo("x2"); + assertThat(lbf.getAliases("&x1").length).isEqualTo(1); + assertThat(lbf.getAliases("&x1")[0]).isEqualTo("&x2"); + assertThat(lbf.getAliases("x2").length).isEqualTo(1); + assertThat(lbf.getAliases("x2")[0]).isEqualTo("x1"); + assertThat(lbf.getAliases("&x2").length).isEqualTo(1); + assertThat(lbf.getAliases("&x2")[0]).isEqualTo("&x1"); + } + + @Test + void staticFactoryMethodFoundByNonEagerTypeMatching() { RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class); rbd.setFactoryMethodName("createTestBean"); lbf.registerBeanDefinition("x1", rbd); TestBeanFactory.initialized = false; String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(1, beanNames.length); - assertEquals("x1", beanNames[0]); - assertFalse(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertFalse(lbf.containsBean("&x1")); - assertTrue(lbf.isSingleton("x1")); - assertFalse(lbf.isSingleton("&x1")); - assertFalse(lbf.isPrototype("x1")); - assertFalse(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(null, lbf.getType("&x1")); - assertFalse(TestBeanFactory.initialized); - } - - @Test - public void testStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("x1"); + assertThat(lbf.containsSingleton("x1")).isFalse(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isFalse(); + assertThat(lbf.isSingleton("x1")).isTrue(); + assertThat(lbf.isSingleton("&x1")).isFalse(); + assertThat(lbf.isPrototype("x1")).isFalse(); + assertThat(lbf.isPrototype("&x1")).isFalse(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(null); + assertThat(TestBeanFactory.initialized).isFalse(); + } + + @Test + void staticPrototypeFactoryMethodFoundByNonEagerTypeMatching() { RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); rbd.setFactoryMethodName("createTestBean"); lbf.registerBeanDefinition("x1", rbd); TestBeanFactory.initialized = false; String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(1, beanNames.length); - assertEquals("x1", beanNames[0]); - assertFalse(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertFalse(lbf.containsBean("&x1")); - assertFalse(lbf.isSingleton("x1")); - assertFalse(lbf.isSingleton("&x1")); - assertTrue(lbf.isPrototype("x1")); - assertFalse(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(null, lbf.getType("&x1")); - assertFalse(TestBeanFactory.initialized); - } - - @Test - public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("x1"); + assertThat(lbf.containsSingleton("x1")).isFalse(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isFalse(); + assertThat(lbf.isSingleton("x1")).isFalse(); + assertThat(lbf.isSingleton("&x1")).isFalse(); + assertThat(lbf.isPrototype("x1")).isTrue(); + assertThat(lbf.isPrototype("&x1")).isFalse(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(null); + assertThat(TestBeanFactory.initialized).isFalse(); + } + + @Test + void nonStaticFactoryMethodFoundByNonEagerTypeMatching() { RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class); lbf.registerBeanDefinition("factory", factoryBd); RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class); @@ -403,134 +400,128 @@ public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() { TestBeanFactory.initialized = false; String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(1, beanNames.length); - assertEquals("x1", beanNames[0]); - assertFalse(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertFalse(lbf.containsBean("&x1")); - assertTrue(lbf.isSingleton("x1")); - assertFalse(lbf.isSingleton("&x1")); - assertFalse(lbf.isPrototype("x1")); - assertFalse(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(null, lbf.getType("&x1")); - assertFalse(TestBeanFactory.initialized); - } - - @Test - public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("x1"); + assertThat(lbf.containsSingleton("x1")).isFalse(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isFalse(); + assertThat(lbf.isSingleton("x1")).isTrue(); + assertThat(lbf.isSingleton("&x1")).isFalse(); + assertThat(lbf.isPrototype("x1")).isFalse(); + assertThat(lbf.isPrototype("&x1")).isFalse(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(null); + assertThat(TestBeanFactory.initialized).isFalse(); + } + + @Test + void nonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() { RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class); lbf.registerBeanDefinition("factory", factoryBd); RootBeanDefinition rbd = new RootBeanDefinition(); rbd.setFactoryBeanName("factory"); rbd.setFactoryMethodName("createTestBeanNonStatic"); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("x1", rbd); TestBeanFactory.initialized = false; String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); - assertEquals(1, beanNames.length); - assertEquals("x1", beanNames[0]); - assertFalse(lbf.containsSingleton("x1")); - assertTrue(lbf.containsBean("x1")); - assertFalse(lbf.containsBean("&x1")); - assertTrue(lbf.containsLocalBean("x1")); - assertFalse(lbf.containsLocalBean("&x1")); - assertFalse(lbf.isSingleton("x1")); - assertFalse(lbf.isSingleton("&x1")); - assertTrue(lbf.isPrototype("x1")); - assertFalse(lbf.isPrototype("&x1")); - assertTrue(lbf.isTypeMatch("x1", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); - assertTrue(lbf.isTypeMatch("x1", Object.class)); - assertFalse(lbf.isTypeMatch("&x1", Object.class)); - assertEquals(TestBean.class, lbf.getType("x1")); - assertEquals(null, lbf.getType("&x1")); - assertFalse(TestBeanFactory.initialized); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("x1"); + assertThat(lbf.containsSingleton("x1")).isFalse(); + assertThat(lbf.containsBean("x1")).isTrue(); + assertThat(lbf.containsBean("&x1")).isFalse(); + assertThat(lbf.containsLocalBean("x1")).isTrue(); + assertThat(lbf.containsLocalBean("&x1")).isFalse(); + assertThat(lbf.isSingleton("x1")).isFalse(); + assertThat(lbf.isSingleton("&x1")).isFalse(); + assertThat(lbf.isPrototype("x1")).isTrue(); + assertThat(lbf.isPrototype("&x1")).isFalse(); + assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse(); + assertThat(lbf.isTypeMatch("x1", Object.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x1", Object.class)).isFalse(); + assertThat(lbf.getType("x1")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x1")).isEqualTo(null); + assertThat(TestBeanFactory.initialized).isFalse(); lbf.registerAlias("x1", "x2"); - assertTrue(lbf.containsBean("x2")); - assertFalse(lbf.containsBean("&x2")); - assertTrue(lbf.containsLocalBean("x2")); - assertFalse(lbf.containsLocalBean("&x2")); - assertFalse(lbf.isSingleton("x2")); - assertFalse(lbf.isSingleton("&x2")); - assertTrue(lbf.isPrototype("x2")); - assertFalse(lbf.isPrototype("&x2")); - assertTrue(lbf.isTypeMatch("x2", TestBean.class)); - assertFalse(lbf.isTypeMatch("&x2", TestBean.class)); - assertTrue(lbf.isTypeMatch("x2", Object.class)); - assertFalse(lbf.isTypeMatch("&x2", Object.class)); - assertEquals(TestBean.class, lbf.getType("x2")); - assertEquals(null, lbf.getType("&x2")); - assertEquals(1, lbf.getAliases("x1").length); - assertEquals("x2", lbf.getAliases("x1")[0]); - assertEquals(1, lbf.getAliases("&x1").length); - assertEquals("&x2", lbf.getAliases("&x1")[0]); - assertEquals(1, lbf.getAliases("x2").length); - assertEquals("x1", lbf.getAliases("x2")[0]); - assertEquals(1, lbf.getAliases("&x2").length); - assertEquals("&x1", lbf.getAliases("&x2")[0]); - } - - @Test - public void testEmpty() { + assertThat(lbf.containsBean("x2")).isTrue(); + assertThat(lbf.containsBean("&x2")).isFalse(); + assertThat(lbf.containsLocalBean("x2")).isTrue(); + assertThat(lbf.containsLocalBean("&x2")).isFalse(); + assertThat(lbf.isSingleton("x2")).isFalse(); + assertThat(lbf.isSingleton("&x2")).isFalse(); + assertThat(lbf.isPrototype("x2")).isTrue(); + assertThat(lbf.isPrototype("&x2")).isFalse(); + assertThat(lbf.isTypeMatch("x2", TestBean.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x2", TestBean.class)).isFalse(); + assertThat(lbf.isTypeMatch("x2", Object.class)).isTrue(); + assertThat(lbf.isTypeMatch("&x2", Object.class)).isFalse(); + assertThat(lbf.getType("x2")).isEqualTo(TestBean.class); + assertThat(lbf.getType("&x2")).isEqualTo(null); + assertThat(lbf.getAliases("x1").length).isEqualTo(1); + assertThat(lbf.getAliases("x1")[0]).isEqualTo("x2"); + assertThat(lbf.getAliases("&x1").length).isEqualTo(1); + assertThat(lbf.getAliases("&x1")[0]).isEqualTo("&x2"); + assertThat(lbf.getAliases("x2").length).isEqualTo(1); + assertThat(lbf.getAliases("x2")[0]).isEqualTo("x1"); + assertThat(lbf.getAliases("&x2").length).isEqualTo(1); + assertThat(lbf.getAliases("&x2")[0]).isEqualTo("&x1"); + } + + @Test + void empty() { ListableBeanFactory lbf = new DefaultListableBeanFactory(); - assertTrue("No beans defined --> array != null", lbf.getBeanDefinitionNames() != null); - assertTrue("No beans defined after no arg constructor", lbf.getBeanDefinitionNames().length == 0); - assertTrue("No beans defined after no arg constructor", lbf.getBeanDefinitionCount() == 0); + assertThat(lbf.getBeanDefinitionNames() != null).as("No beans defined --> array != null").isTrue(); + assertThat(lbf.getBeanDefinitionNames().length == 0).as("No beans defined after no arg constructor").isTrue(); + assertThat(lbf.getBeanDefinitionCount() == 0).as("No beans defined after no arg constructor").isTrue(); } @Test - public void testEmptyPropertiesPopulation() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void emptyPropertiesPopulation() { Properties p = new Properties(); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("No beans defined after ignorable invalid", lbf.getBeanDefinitionCount() == 0); + assertThat(lbf.getBeanDefinitionCount() == 0).as("No beans defined after ignorable invalid").isTrue(); } @Test - public void testHarmlessIgnorableRubbish() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void harmlessIgnorableRubbish() { Properties p = new Properties(); p.setProperty("foo", "bar"); p.setProperty("qwert", "er"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, "test"); - assertTrue("No beans defined after harmless ignorable rubbish", lbf.getBeanDefinitionCount() == 0); + assertThat(lbf.getBeanDefinitionCount() == 0).as("No beans defined after harmless ignorable rubbish").isTrue(); } @Test - public void testPropertiesPopulationWithNullPrefix() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void propertiesPopulationWithNullPrefix() { Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); p.setProperty("test.age", "48"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("1 beans registered, not " + count, count == 1); - testSingleTestBean(lbf); + assertThat(count == 1).as("1 beans registered, not " + count).isTrue(); + singleTestBean(lbf); } @Test - public void testPropertiesPopulationWithPrefix() { + void propertiesPopulationWithPrefix() { String PREFIX = "beans."; - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty(PREFIX + "test.(class)", TestBean.class.getName()); p.setProperty(PREFIX + "test.name", "Tony"); p.setProperty(PREFIX + "test.age", "0x30"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX); - assertTrue("1 beans registered, not " + count, count == 1); - testSingleTestBean(lbf); + assertThat(count == 1).as("1 beans registered, not " + count).isTrue(); + singleTestBean(lbf); } @Test - public void testSimpleReference() { + void simpleReference() { String PREFIX = "beans."; - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName()); @@ -542,97 +533,168 @@ public void testSimpleReference() { p.setProperty(PREFIX + "kerry.spouse(ref)", "rod"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX); - assertTrue("2 beans registered, not " + count, count == 2); + assertThat(count == 2).as("2 beans registered, not " + count).isTrue(); TestBean kerry = lbf.getBean("kerry", TestBean.class); - assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName())); + assertThat("Kerry".equals(kerry.getName())).as("Kerry name is Kerry").isTrue(); ITestBean spouse = kerry.getSpouse(); - assertTrue("Kerry spouse is non null", spouse != null); - assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName())); + assertThat(spouse != null).as("Kerry spouse is non null").isTrue(); + assertThat("Rod".equals(spouse.getName())).as("Kerry spouse name is Rod").isTrue(); } @Test - public void testPropertiesWithDotsInKey() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void propertiesWithDotsInKey() { Properties p = new Properties(); p.setProperty("tb.(class)", TestBean.class.getName()); p.setProperty("tb.someMap[my.key]", "my.value"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertTrue("1 beans registered, not " + count, count == 1); - assertEquals(1, lbf.getBeanDefinitionCount()); + assertThat(count == 1).as("1 beans registered, not " + count).isTrue(); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); TestBean tb = lbf.getBean("tb", TestBean.class); - assertEquals("my.value", tb.getSomeMap().get("my.key")); + assertThat(tb.getSomeMap().get("my.key")).isEqualTo("my.value"); } @Test - public void testUnresolvedReference() { + void unresolvedReference() { String PREFIX = "beans."; - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); - try { - p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName()); - p.setProperty(PREFIX + "kerry.name", "Kerry"); - p.setProperty(PREFIX + "kerry.age", "35"); - p.setProperty(PREFIX + "kerry.spouse(ref)", "rod"); - - (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX); + p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName()); + p.setProperty(PREFIX + "kerry.name", "Kerry"); + p.setProperty(PREFIX + "kerry.age", "35"); + p.setProperty(PREFIX + "kerry.spouse(ref)", "rod"); - lbf.getBean("kerry"); - fail("Unresolved reference should have been detected"); - } - catch (BeansException ex) { - // cool - } + new PropertiesBeanDefinitionReader(lbf).registerBeanDefinitions(p, PREFIX); + assertThatExceptionOfType(BeansException.class).as("unresolved reference").isThrownBy(() -> + lbf.getBean("kerry")); } @Test - public void testSelfReference() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void selfReference() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("spouse", new RuntimeBeanReference("self")); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("self", bd); + + TestBean self = (TestBean) lbf.getBean("self"); + assertThat(self.getSpouse()).isEqualTo(self); + } + + @Test + void referenceByName() { + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("doctor", new RuntimeBeanReference("doc")); + RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); + bd.setPropertyValues(pvs); + lbf.registerBeanDefinition("self", bd); + lbf.registerBeanDefinition("doc", new RootBeanDefinition(NestedTestBean.class)); + TestBean self = (TestBean) lbf.getBean("self"); - assertEquals(self, self.getSpouse()); + assertThat(self.getDoctor()).isEqualTo(lbf.getBean("doc")); } @Test - public void testPossibleMatches() { - try { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); - MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.add("ag", "foobar"); - RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); - bd.setPropertyValues(pvs); - lbf.registerBeanDefinition("tb", bd); - lbf.getBean("tb"); - fail("Should throw exception on invalid property"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getCause() instanceof NotWritablePropertyException); - NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); - // expected - assertEquals(1, cause.getPossibleMatches().length); - assertEquals("age", cause.getPossibleMatches()[0]); - } + void referenceByType() { + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("doctor", new RuntimeBeanReference(NestedTestBean.class)); + RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); + bd.setPropertyValues(pvs); + lbf.registerBeanDefinition("self", bd); + lbf.registerBeanDefinition("doc", new RootBeanDefinition(NestedTestBean.class)); + + TestBean self = (TestBean) lbf.getBean("self"); + assertThat(self.getDoctor()).isEqualTo(lbf.getBean("doc")); } @Test - public void testPrototype() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void referenceByAutowire() { + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("doctor", AutowiredPropertyMarker.INSTANCE); + RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); + bd.setPropertyValues(pvs); + lbf.registerBeanDefinition("self", bd); + lbf.registerBeanDefinition("doc", new RootBeanDefinition(NestedTestBean.class)); + + TestBean self = (TestBean) lbf.getBean("self"); + assertThat(self.getDoctor()).isEqualTo(lbf.getBean("doc")); + } + + @Test + void arrayReferenceByName() { + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("stringArray", new RuntimeBeanReference("string")); + RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); + bd.setPropertyValues(pvs); + lbf.registerBeanDefinition("self", bd); + lbf.registerSingleton("string", "A"); + + TestBean self = (TestBean) lbf.getBean("self"); + assertThat(self.getStringArray()).hasSize(1); + assertThat(self.getStringArray()).contains("A"); + } + + @Test + void arrayReferenceByType() { + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("stringArray", new RuntimeBeanReference(String.class)); + RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); + bd.setPropertyValues(pvs); + lbf.registerBeanDefinition("self", bd); + lbf.registerSingleton("string", "A"); + + TestBean self = (TestBean) lbf.getBean("self"); + assertThat(self.getStringArray()).hasSize(1); + assertThat(self.getStringArray()).contains("A"); + } + + @Test + void arrayReferenceByAutowire() { + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("stringArray", AutowiredPropertyMarker.INSTANCE); + RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); + bd.setPropertyValues(pvs); + lbf.registerBeanDefinition("self", bd); + lbf.registerSingleton("string1", "A"); + lbf.registerSingleton("string2", "B"); + + TestBean self = (TestBean) lbf.getBean("self"); + assertThat(self.getStringArray()).hasSize(2); + assertThat(self.getStringArray()).contains("A"); + assertThat(self.getStringArray()).contains("B"); + } + + @Test + void possibleMatches() { + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("ag", "foobar"); + RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); + bd.setPropertyValues(pvs); + lbf.registerBeanDefinition("tb", bd); + + assertThatExceptionOfType(BeanCreationException.class).as("invalid property").isThrownBy(() -> + lbf.getBean("tb")) + .withCauseInstanceOf(NotWritablePropertyException.class) + .satisfies(ex -> { + NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); + assertThat(cause.getPossibleMatches()).hasSize(1); + assertThat(cause.getPossibleMatches()[0]).isEqualTo("age"); + }); + } + + @Test + void prototype() { Properties p = new Properties(); p.setProperty("kerry.(class)", TestBean.class.getName()); p.setProperty("kerry.age", "35"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); TestBean kerry1 = (TestBean) lbf.getBean("kerry"); TestBean kerry2 = (TestBean) lbf.getBean("kerry"); - assertTrue("Non null", kerry1 != null); - assertTrue("Singletons equal", kerry1 == kerry2); + assertThat(kerry1 != null).as("Non null").isTrue(); + assertThat(kerry1 == kerry2).as("Singletons equal").isTrue(); lbf = new DefaultListableBeanFactory(); p = new Properties(); @@ -642,8 +704,8 @@ public void testPrototype() { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); kerry1 = (TestBean) lbf.getBean("kerry"); kerry2 = (TestBean) lbf.getBean("kerry"); - assertTrue("Non null", kerry1 != null); - assertTrue("Prototypes NOT equal", kerry1 != kerry2); + assertThat(kerry1 != null).as("Non null").isTrue(); + assertThat(kerry1 != kerry2).as("Prototypes NOT equal").isTrue(); lbf = new DefaultListableBeanFactory(); p = new Properties(); @@ -653,13 +715,12 @@ public void testPrototype() { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); kerry1 = (TestBean) lbf.getBean("kerry"); kerry2 = (TestBean) lbf.getBean("kerry"); - assertTrue("Non null", kerry1 != null); - assertTrue("Specified singletons equal", kerry1 == kerry2); + assertThat(kerry1 != null).as("Non null").isTrue(); + assertThat(kerry1 == kerry2).as("Specified singletons equal").isTrue(); } @Test - public void testPrototypeCircleLeadsToException() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeCircleLeadsToException() { Properties p = new Properties(); p.setProperty("kerry.(class)", TestBean.class.getName()); p.setProperty("kerry.(singleton)", "false"); @@ -670,20 +731,14 @@ public void testPrototypeCircleLeadsToException() { p.setProperty("rod.age", "34"); p.setProperty("rod.spouse", "*kerry"); - (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - try { - lbf.getBean("kerry"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - // expected - assertTrue(ex.contains(BeanCurrentlyInCreationException.class)); - } + new PropertiesBeanDefinitionReader(lbf).registerBeanDefinitions(p); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + lbf.getBean("kerry")) + .satisfies(ex -> assertThat(ex.contains(BeanCurrentlyInCreationException.class)).isTrue()); } @Test - public void testPrototypeExtendsPrototype() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeExtendsPrototype() { Properties p = new Properties(); p.setProperty("wife.(class)", TestBean.class.getName()); p.setProperty("wife.name", "kerry"); @@ -693,9 +748,9 @@ public void testPrototypeExtendsPrototype() { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); TestBean kerry1 = (TestBean) lbf.getBean("kerry"); TestBean kerry2 = (TestBean) lbf.getBean("kerry"); - assertEquals("kerry", kerry1.getName()); - assertNotNull("Non null", kerry1); - assertTrue("Singletons equal", kerry1 == kerry2); + assertThat(kerry1.getName()).isEqualTo("kerry"); + assertThat(kerry1).as("Non null").isNotNull(); + assertThat(kerry1 == kerry2).as("Singletons equal").isTrue(); lbf = new DefaultListableBeanFactory(); p = new Properties(); @@ -706,11 +761,11 @@ public void testPrototypeExtendsPrototype() { p.setProperty("kerry.(singleton)", "false"); p.setProperty("kerry.age", "35"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); - assertFalse(lbf.isSingleton("kerry")); + assertThat(lbf.isSingleton("kerry")).isFalse(); kerry1 = (TestBean) lbf.getBean("kerry"); kerry2 = (TestBean) lbf.getBean("kerry"); - assertTrue("Non null", kerry1 != null); - assertTrue("Prototypes NOT equal", kerry1 != kerry2); + assertThat(kerry1 != null).as("Non null").isTrue(); + assertThat(kerry1 != kerry2).as("Prototypes NOT equal").isTrue(); lbf = new DefaultListableBeanFactory(); p = new Properties(); @@ -720,12 +775,12 @@ public void testPrototypeExtendsPrototype() { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); kerry1 = (TestBean) lbf.getBean("kerry"); kerry2 = (TestBean) lbf.getBean("kerry"); - assertTrue("Non null", kerry1 != null); - assertTrue("Specified singletons equal", kerry1 == kerry2); + assertThat(kerry1 != null).as("Non null").isTrue(); + assertThat(kerry1 == kerry2).as("Specified singletons equal").isTrue(); } @Test - public void testCanReferenceParentBeanFromChildViaAlias() { + void canReferenceParentBeanFromChildViaAlias() { final String EXPECTED_NAME = "Juergen"; final int EXPECTED_AGE = 41; @@ -741,16 +796,17 @@ public void testCanReferenceParentBeanFromChildViaAlias() { factory.registerBeanDefinition("child", childDefinition); factory.registerAlias("parent", "alias"); - TestBean child = (TestBean) factory.getBean("child"); - assertEquals(EXPECTED_NAME, child.getName()); - assertEquals(EXPECTED_AGE, child.getAge()); + TestBean child = factory.getBean("child", TestBean.class); + assertThat(child.getName()).isEqualTo(EXPECTED_NAME); + assertThat(child.getAge()).isEqualTo(EXPECTED_AGE); + BeanDefinition mergedBeanDefinition1 = factory.getMergedBeanDefinition("child"); + BeanDefinition mergedBeanDefinition2 = factory.getMergedBeanDefinition("child"); - assertEquals("Use cached merged bean definition", - factory.getMergedBeanDefinition("child"), factory.getMergedBeanDefinition("child")); + assertThat(mergedBeanDefinition1).as("Use cached merged bean definition").isSameAs(mergedBeanDefinition2); } @Test - public void testGetTypeWorksAfterParentChildMerging() { + void getTypeWorksAfterParentChildMerging() { RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class); ChildBeanDefinition childDefinition = new ChildBeanDefinition("parent", DerivedTestBean.class, null, null); @@ -759,13 +815,12 @@ public void testGetTypeWorksAfterParentChildMerging() { factory.registerBeanDefinition("child", childDefinition); factory.freezeConfiguration(); - assertEquals(TestBean.class, factory.getType("parent")); - assertEquals(DerivedTestBean.class, factory.getType("child")); + assertThat(factory.getType("parent")).isEqualTo(TestBean.class); + assertThat(factory.getType("child")).isEqualTo(DerivedTestBean.class); } @Test - public void testNameAlreadyBound() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void nameAlreadyBound() { Properties p = new Properties(); p.setProperty("kerry.(class)", TestBean.class.getName()); p.setProperty("kerry.age", "35"); @@ -774,119 +829,137 @@ public void testNameAlreadyBound() { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); } catch (BeanDefinitionStoreException ex) { - assertEquals("kerry", ex.getBeanName()); + assertThat(ex.getBeanName()).isEqualTo("kerry"); // expected } } - private void testSingleTestBean(ListableBeanFactory lbf) { - assertTrue("1 beans defined", lbf.getBeanDefinitionCount() == 1); + private void singleTestBean(ListableBeanFactory lbf) { + assertThat(lbf.getBeanDefinitionCount() == 1).as("1 beans defined").isTrue(); String[] names = lbf.getBeanDefinitionNames(); - assertTrue(names != lbf.getBeanDefinitionNames()); - assertTrue("Array length == 1", names.length == 1); - assertTrue("0th element == test", names[0].equals("test")); + assertThat(names != lbf.getBeanDefinitionNames()).isTrue(); + assertThat(names.length == 1).as("Array length == 1").isTrue(); + assertThat(names[0].equals("test")).as("0th element == test").isTrue(); TestBean tb = (TestBean) lbf.getBean("test"); - assertTrue("Test is non null", tb != null); - assertTrue("Test bean name is Tony", "Tony".equals(tb.getName())); - assertTrue("Test bean age is 48", tb.getAge() == 48); + assertThat(tb != null).as("Test is non null").isTrue(); + assertThat("Tony".equals(tb.getName())).as("Test bean name is Tony").isTrue(); + assertThat(tb.getAge() == 48).as("Test bean age is 48").isTrue(); } @Test - public void testAliasCircle() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void aliasCircle() { lbf.registerAlias("test", "test2"); lbf.registerAlias("test2", "test3"); - - try { - lbf.registerAlias("test3", "test2"); - fail("Should have thrown IllegalStateException"); - } - catch (IllegalStateException ex) { - // expected - } - - try { - lbf.registerAlias("test3", "test"); - fail("Should have thrown IllegalStateException"); - } - catch (IllegalStateException ex) { - // expected - } - + assertThatIllegalStateException().isThrownBy(() -> + lbf.registerAlias("test3", "test2")); + assertThatIllegalStateException().isThrownBy(() -> + lbf.registerAlias("test3", "test")); lbf.registerAlias("test", "test3"); } @Test - public void testBeanDefinitionOverriding() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); - lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); + void aliasChaining() { lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class)); - lbf.registerAlias("otherTest", "test2"); - lbf.registerAlias("test", "test2"); - assertTrue(lbf.getBean("test") instanceof NestedTestBean); - assertTrue(lbf.getBean("test2") instanceof NestedTestBean); + lbf.registerAlias("test", "testAlias"); + lbf.registerAlias("testAlias", "testAlias2"); + lbf.registerAlias("testAlias2", "testAlias3"); + Object bean = lbf.getBean("test"); + assertThat(lbf.getBean("testAlias")).isSameAs(bean); + assertThat(lbf.getBean("testAlias2")).isSameAs(bean); + assertThat(lbf.getBean("testAlias3")).isSameAs(bean); } @Test - public void testBeanDefinitionRemoval() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); - lbf.setAllowBeanDefinitionOverriding(false); + void beanDefinitionOverriding() { lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); - lbf.registerAlias("test", "test2"); - lbf.preInstantiateSingletons(); - lbf.removeBeanDefinition("test"); - lbf.removeAlias("test2"); lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class)); + lbf.registerAlias("otherTest", "test2"); lbf.registerAlias("test", "test2"); - assertTrue(lbf.getBean("test") instanceof NestedTestBean); - assertTrue(lbf.getBean("test2") instanceof NestedTestBean); + assertThat(lbf.getBean("test")).isInstanceOf(NestedTestBean.class); + assertThat(lbf.getBean("test2")).isInstanceOf(NestedTestBean.class); } @Test - public void testBeanDefinitionOverridingNotAllowed() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanDefinitionOverridingNotAllowed() { lbf.setAllowBeanDefinitionOverriding(false); BeanDefinition oldDef = new RootBeanDefinition(TestBean.class); BeanDefinition newDef = new RootBeanDefinition(NestedTestBean.class); lbf.registerBeanDefinition("test", oldDef); - try { - lbf.registerBeanDefinition("test", newDef); - fail("Should have thrown BeanDefinitionOverrideException"); - } - catch (BeanDefinitionOverrideException ex) { - assertEquals("test", ex.getBeanName()); - assertSame(newDef, ex.getBeanDefinition()); - assertSame(oldDef, ex.getExistingDefinition()); - } + assertThatExceptionOfType(BeanDefinitionOverrideException.class).isThrownBy(() -> + lbf.registerBeanDefinition("test", newDef)) + .satisfies(ex -> { + assertThat(ex.getBeanName()).isEqualTo("test"); + assertThat(ex.getBeanDefinition()).isEqualTo(newDef); + assertThat(ex.getExistingDefinition()).isEqualTo(oldDef); + }); } @Test - public void testBeanDefinitionOverridingWithAlias() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanDefinitionOverridingWithAlias() { lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); lbf.registerAlias("test", "testAlias"); lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class)); lbf.registerAlias("test", "testAlias"); - assertTrue(lbf.getBean("test") instanceof NestedTestBean); - assertTrue(lbf.getBean("testAlias") instanceof NestedTestBean); + assertThat(lbf.getBean("test")).isInstanceOf(NestedTestBean.class); + assertThat(lbf.getBean("testAlias")).isInstanceOf(NestedTestBean.class); } @Test - public void testAliasChaining() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanDefinitionOverridingWithConstructorArgumentMismatch() { + RootBeanDefinition bd1 = new RootBeanDefinition(NestedTestBean.class); + bd1.getConstructorArgumentValues().addIndexedArgumentValue(1, "value1"); + lbf.registerBeanDefinition("test", bd1); + RootBeanDefinition bd2 = new RootBeanDefinition(NestedTestBean.class); + bd2.getConstructorArgumentValues().addIndexedArgumentValue(0, "value0"); + lbf.registerBeanDefinition("test", bd2); + assertThat(lbf.getBean("test")).isInstanceOf(NestedTestBean.class); + assertThat(lbf.getBean("test", NestedTestBean.class).getCompany()).isEqualTo("value0"); + } + + @Test + void beanDefinitionRemoval() { + lbf.setAllowBeanDefinitionOverriding(false); + lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); + lbf.registerAlias("test", "test2"); + lbf.preInstantiateSingletons(); + assertThat(lbf.getBean("test")).isInstanceOf(TestBean.class); + assertThat(lbf.getBean("test2")).isInstanceOf(TestBean.class); + lbf.removeBeanDefinition("test"); + lbf.removeAlias("test2"); lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class)); - lbf.registerAlias("test", "testAlias"); - lbf.registerAlias("testAlias", "testAlias2"); - lbf.registerAlias("testAlias2", "testAlias3"); - Object bean = lbf.getBean("test"); - assertSame(bean, lbf.getBean("testAlias")); - assertSame(bean, lbf.getBean("testAlias2")); - assertSame(bean, lbf.getBean("testAlias3")); + lbf.registerAlias("test", "test2"); + assertThat(lbf.getBean("test")).isInstanceOf(NestedTestBean.class); + assertThat(lbf.getBean("test2")).isInstanceOf(NestedTestBean.class); + } + + @Test // gh-23542 + void concurrentBeanDefinitionRemoval() { + final int MAX = 200; + lbf.setAllowBeanDefinitionOverriding(false); + + // Register the bean definitions before invoking preInstantiateSingletons() + // to simulate realistic usage of an ApplicationContext; otherwise, the bean + // factory thinks it's an "empty" factory which causes this test to fail in + // an unrealistic manner. + IntStream.range(0, MAX).forEach(this::registerTestBean); + lbf.preInstantiateSingletons(); + + // This test is considered successful if the following does not result in an exception. + IntStream.range(0, MAX).parallel().forEach(this::removeTestBean); + } + + private void registerTestBean(int i) { + String name = "test" + i; + lbf.registerBeanDefinition(name, new RootBeanDefinition(TestBean.class)); + } + + private void removeTestBean(int i) { + String name = "test" + i; + lbf.removeBeanDefinition(name); } @Test - public void testBeanReferenceWithNewSyntax() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanReferenceWithNewSyntax() { Properties p = new Properties(); p.setProperty("r.(class)", TestBean.class.getName()); p.setProperty("r.name", "rod"); @@ -896,30 +969,25 @@ public void testBeanReferenceWithNewSyntax() { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); TestBean k = (TestBean) lbf.getBean("k"); TestBean r = (TestBean) lbf.getBean("r"); - assertTrue(k.getSpouse() == r); + assertThat(k.getSpouse() == r).isTrue(); } @Test - public void testCanEscapeBeanReferenceSyntax() { + void canEscapeBeanReferenceSyntax() { String name = "*name"; - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("r.(class)", TestBean.class.getName()); p.setProperty("r.name", "*" + name); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); TestBean r = (TestBean) lbf.getBean("r"); - assertTrue(r.getName().equals(name)); + assertThat(r.getName().equals(name)).isTrue(); } @Test - public void testCustomEditor() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); - lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() { - @Override - public void registerCustomEditors(PropertyEditorRegistry registry) { + void customEditor() { + lbf.addPropertyEditorRegistrar(registry -> { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); - } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1,1"); @@ -927,12 +995,11 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { bd.setPropertyValues(pvs); lbf.registerBeanDefinition("testBean", bd); TestBean testBean = (TestBean) lbf.getBean("testBean"); - assertTrue(testBean.getMyFloat().floatValue() == 1.1f); + assertThat(testBean.getMyFloat().floatValue() == 1.1f).isTrue(); } @Test - public void testCustomConverter() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void customConverter() { GenericConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new Converter() { @Override @@ -953,12 +1020,11 @@ public Float convert(String source) { bd.setPropertyValues(pvs); lbf.registerBeanDefinition("testBean", bd); TestBean testBean = (TestBean) lbf.getBean("testBean"); - assertTrue(testBean.getMyFloat().floatValue() == 1.1f); + assertThat(testBean.getMyFloat().floatValue() == 1.1f).isTrue(); } @Test - public void testCustomEditorWithBeanReference() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void customEditorWithBeanReference() { lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { @@ -973,12 +1039,11 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { lbf.registerBeanDefinition("testBean", bd); lbf.registerSingleton("myFloat", "1,1"); TestBean testBean = (TestBean) lbf.getBean("testBean"); - assertTrue(testBean.getMyFloat().floatValue() == 1.1f); + assertThat(testBean.getMyFloat().floatValue() == 1.1f).isTrue(); } @Test - public void testCustomTypeConverter() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void customTypeConverter() { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); lbf.setTypeConverter(new CustomTypeConverter(nf)); MutablePropertyValues pvs = new MutablePropertyValues(); @@ -988,14 +1053,13 @@ public void testCustomTypeConverter() { cav.addIndexedArgumentValue(1, "myAge"); lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, cav, pvs)); TestBean testBean = (TestBean) lbf.getBean("testBean"); - assertEquals("myName", testBean.getName()); - assertEquals(5, testBean.getAge()); - assertTrue(testBean.getMyFloat().floatValue() == 1.1f); + assertThat(testBean.getName()).isEqualTo("myName"); + assertThat(testBean.getAge()).isEqualTo(5); + assertThat(testBean.getMyFloat().floatValue() == 1.1f).isTrue(); } @Test - public void testCustomTypeConverterWithBeanReference() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void customTypeConverterWithBeanReference() { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); lbf.setTypeConverter(new CustomTypeConverter(nf)); MutablePropertyValues pvs = new MutablePropertyValues(); @@ -1006,14 +1070,13 @@ public void testCustomTypeConverterWithBeanReference() { lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, cav, pvs)); lbf.registerSingleton("myFloat", "1,1"); TestBean testBean = (TestBean) lbf.getBean("testBean"); - assertEquals("myName", testBean.getName()); - assertEquals(5, testBean.getAge()); - assertTrue(testBean.getMyFloat().floatValue() == 1.1f); + assertThat(testBean.getName()).isEqualTo("myName"); + assertThat(testBean.getAge()).isEqualTo(5); + assertThat(testBean.getMyFloat().floatValue() == 1.1f).isTrue(); } @Test - public void testRegisterExistingSingletonWithReference() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void registerExistingSingletonWithReference() { Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); @@ -1023,34 +1086,33 @@ public void testRegisterExistingSingletonWithReference() { Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); - assertTrue(lbf.isSingleton("singletonObject")); - assertEquals(TestBean.class, lbf.getType("singletonObject")); + assertThat(lbf.isSingleton("singletonObject")).isTrue(); + assertThat(lbf.getType("singletonObject")).isEqualTo(TestBean.class); TestBean test = (TestBean) lbf.getBean("test"); - assertEquals(singletonObject, lbf.getBean("singletonObject")); - assertEquals(singletonObject, test.getSpouse()); + assertThat(lbf.getBean("singletonObject")).isEqualTo(singletonObject); + assertThat(test.getSpouse()).isEqualTo(singletonObject); Map beansOfType = lbf.getBeansOfType(TestBean.class, false, true); - assertEquals(2, beansOfType.size()); - assertTrue(beansOfType.containsValue(test)); - assertTrue(beansOfType.containsValue(singletonObject)); + assertThat(beansOfType.size()).isEqualTo(2); + assertThat(beansOfType.containsValue(test)).isTrue(); + assertThat(beansOfType.containsValue(singletonObject)).isTrue(); beansOfType = lbf.getBeansOfType(null, false, true); - assertEquals(2, beansOfType.size()); + assertThat(beansOfType.size()).isEqualTo(2); Iterator beanNames = lbf.getBeanNamesIterator(); - assertEquals("test", beanNames.next()); - assertEquals("singletonObject", beanNames.next()); - assertFalse(beanNames.hasNext()); + assertThat(beanNames.next()).isEqualTo("test"); + assertThat(beanNames.next()).isEqualTo("singletonObject"); + assertThat(beanNames.hasNext()).isFalse(); - assertTrue(lbf.containsSingleton("test")); - assertTrue(lbf.containsSingleton("singletonObject")); - assertTrue(lbf.containsBeanDefinition("test")); - assertFalse(lbf.containsBeanDefinition("singletonObject")); + assertThat(lbf.containsSingleton("test")).isTrue(); + assertThat(lbf.containsSingleton("singletonObject")).isTrue(); + assertThat(lbf.containsBeanDefinition("test")).isTrue(); + assertThat(lbf.containsBeanDefinition("singletonObject")).isFalse(); } @Test - public void testRegisterExistingSingletonWithNameOverriding() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void registerExistingSingletonWithNameOverriding() { Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); @@ -1062,34 +1124,33 @@ public void testRegisterExistingSingletonWithNameOverriding() { lbf.registerSingleton("singletonObject", singletonObject); lbf.preInstantiateSingletons(); - assertTrue(lbf.isSingleton("singletonObject")); - assertEquals(TestBean.class, lbf.getType("singletonObject")); + assertThat(lbf.isSingleton("singletonObject")).isTrue(); + assertThat(lbf.getType("singletonObject")).isEqualTo(TestBean.class); TestBean test = (TestBean) lbf.getBean("test"); - assertEquals(singletonObject, lbf.getBean("singletonObject")); - assertEquals(singletonObject, test.getSpouse()); + assertThat(lbf.getBean("singletonObject")).isEqualTo(singletonObject); + assertThat(test.getSpouse()).isEqualTo(singletonObject); Map beansOfType = lbf.getBeansOfType(TestBean.class, false, true); - assertEquals(2, beansOfType.size()); - assertTrue(beansOfType.containsValue(test)); - assertTrue(beansOfType.containsValue(singletonObject)); + assertThat(beansOfType.size()).isEqualTo(2); + assertThat(beansOfType.containsValue(test)).isTrue(); + assertThat(beansOfType.containsValue(singletonObject)).isTrue(); beansOfType = lbf.getBeansOfType(null, false, true); Iterator beanNames = lbf.getBeanNamesIterator(); - assertEquals("test", beanNames.next()); - assertEquals("singletonObject", beanNames.next()); - assertFalse(beanNames.hasNext()); - assertEquals(2, beansOfType.size()); + assertThat(beanNames.next()).isEqualTo("test"); + assertThat(beanNames.next()).isEqualTo("singletonObject"); + assertThat(beanNames.hasNext()).isFalse(); + assertThat(beansOfType.size()).isEqualTo(2); - assertTrue(lbf.containsSingleton("test")); - assertTrue(lbf.containsSingleton("singletonObject")); - assertTrue(lbf.containsBeanDefinition("test")); - assertTrue(lbf.containsBeanDefinition("singletonObject")); + assertThat(lbf.containsSingleton("test")).isTrue(); + assertThat(lbf.containsSingleton("singletonObject")).isTrue(); + assertThat(lbf.containsBeanDefinition("test")).isTrue(); + assertThat(lbf.containsBeanDefinition("singletonObject")).isTrue(); } @Test - public void testRegisterExistingSingletonWithAutowire() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void registerExistingSingletonWithAutowire() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Tony"); pvs.add("age", "48"); @@ -1101,299 +1162,252 @@ public void testRegisterExistingSingletonWithAutowire() { Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); - assertTrue(lbf.containsBean("singletonObject")); - assertTrue(lbf.isSingleton("singletonObject")); - assertEquals(TestBean.class, lbf.getType("singletonObject")); - assertEquals(0, lbf.getAliases("singletonObject").length); + assertThat(lbf.containsBean("singletonObject")).isTrue(); + assertThat(lbf.isSingleton("singletonObject")).isTrue(); + assertThat(lbf.getType("singletonObject")).isEqualTo(TestBean.class); + assertThat(lbf.getAliases("singletonObject").length).isEqualTo(0); DependenciesBean test = (DependenciesBean) lbf.getBean("test"); - assertEquals(singletonObject, lbf.getBean("singletonObject")); - assertEquals(singletonObject, test.getSpouse()); + assertThat(lbf.getBean("singletonObject")).isEqualTo(singletonObject); + assertThat(test.getSpouse()).isEqualTo(singletonObject); } @Test - public void testRegisterExistingSingletonWithAlreadyBound() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void registerExistingSingletonWithAlreadyBound() { Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); - try { - lbf.registerSingleton("singletonObject", singletonObject); - fail("Should have thrown IllegalStateException"); - } - catch (IllegalStateException ex) { - // expected - } + assertThatIllegalStateException().isThrownBy(() -> + lbf.registerSingleton("singletonObject", singletonObject)); } @Test - public void testReregisterBeanDefinition() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void reregisterBeanDefinition() { RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); - bd1.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd1.setScope(BeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("testBean", bd1); - assertTrue(lbf.getBean("testBean") instanceof TestBean); + assertThat(lbf.getBean("testBean")).isInstanceOf(TestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(NestedTestBean.class); - bd2.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd2.setScope(BeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("testBean", bd2); - assertTrue(lbf.getBean("testBean") instanceof NestedTestBean); + assertThat(lbf.getBean("testBean")).isInstanceOf(NestedTestBean.class); } @Test - public void testArrayPropertyWithAutowiring() throws MalformedURLException { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); - bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); + void arrayPropertyWithAutowiring() throws MalformedURLException { + lbf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); + lbf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); - bf.registerBeanDefinition("arrayBean", rbd); - ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); + lbf.registerBeanDefinition("arrayBean", rbd); + ArrayBean ab = (ArrayBean) lbf.getBean("arrayBean"); - assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); - assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); + assertThat(ab.getResourceArray()[0]).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(ab.getResourceArray()[1]).isEqualTo(new UrlResource("http://localhost:9090")); } @Test - public void testArrayPropertyWithOptionalAutowiring() throws MalformedURLException { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - + void arrayPropertyWithOptionalAutowiring() throws MalformedURLException { RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); - bf.registerBeanDefinition("arrayBean", rbd); - ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); + lbf.registerBeanDefinition("arrayBean", rbd); + ArrayBean ab = (ArrayBean) lbf.getBean("arrayBean"); - assertNull(ab.getResourceArray()); + assertThat(ab.getResourceArray()).isNull(); } @Test - public void testArrayConstructorWithAutowiring() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerSingleton("integer1", new Integer(4)); - bf.registerSingleton("integer2", new Integer(5)); + void arrayConstructorWithAutowiring() { + lbf.registerSingleton("integer1",4); + lbf.registerSingleton("integer2", 5); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); - bf.registerBeanDefinition("arrayBean", rbd); - ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); + lbf.registerBeanDefinition("arrayBean", rbd); + ArrayBean ab = (ArrayBean) lbf.getBean("arrayBean"); - assertEquals(new Integer(4), ab.getIntegerArray()[0]); - assertEquals(new Integer(5), ab.getIntegerArray()[1]); + assertThat(ab.getIntegerArray()[0]).isEqualTo(4); + assertThat(ab.getIntegerArray()[1]).isEqualTo(5); } @Test - public void testArrayConstructorWithOptionalAutowiring() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - + void arrayConstructorWithOptionalAutowiring() { RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); - bf.registerBeanDefinition("arrayBean", rbd); - ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); + lbf.registerBeanDefinition("arrayBean", rbd); + ArrayBean ab = (ArrayBean) lbf.getBean("arrayBean"); - assertNull(ab.getIntegerArray()); + assertThat(ab.getIntegerArray()).isNull(); } @Test - public void testDoubleArrayConstructorWithAutowiring() throws MalformedURLException { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerSingleton("integer1", new Integer(4)); - bf.registerSingleton("integer2", new Integer(5)); - bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); - bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); + void doubleArrayConstructorWithAutowiring() throws MalformedURLException { + lbf.registerSingleton("integer1", 4); + lbf.registerSingleton("integer2", 5); + lbf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); + lbf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); - bf.registerBeanDefinition("arrayBean", rbd); - ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); + lbf.registerBeanDefinition("arrayBean", rbd); + ArrayBean ab = (ArrayBean) lbf.getBean("arrayBean"); - assertEquals(new Integer(4), ab.getIntegerArray()[0]); - assertEquals(new Integer(5), ab.getIntegerArray()[1]); - assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); - assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); + assertThat(ab.getIntegerArray()[0]).isEqualTo(4); + assertThat(ab.getIntegerArray()[1]).isEqualTo(5); + assertThat(ab.getResourceArray()[0]).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(ab.getResourceArray()[1]).isEqualTo(new UrlResource("http://localhost:9090")); } @Test - public void testDoubleArrayConstructorWithOptionalAutowiring() throws MalformedURLException { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); - bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); + void doubleArrayConstructorWithOptionalAutowiring() throws MalformedURLException { + lbf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); + lbf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); - bf.registerBeanDefinition("arrayBean", rbd); - ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); + lbf.registerBeanDefinition("arrayBean", rbd); + ArrayBean ab = (ArrayBean) lbf.getBean("arrayBean"); - assertNull(ab.getIntegerArray()); - assertNull(ab.getResourceArray()); + assertThat(ab.getIntegerArray()).isNull(); + assertThat(ab.getResourceArray()).isNull(); } @Test - public void testExpressionInStringArray() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + void expressionInStringArray() { BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class); - when(beanExpressionResolver.evaluate(eq("#{foo}"), ArgumentMatchers.any(BeanExpressionContext.class))) - .thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties"); - bf.setBeanExpressionResolver(beanExpressionResolver); + given(beanExpressionResolver.evaluate(eq("#{foo}"), any(BeanExpressionContext.class))) + .willReturn("classpath:/org/springframework/beans/factory/xml/util.properties"); + lbf.setBeanExpressionResolver(beanExpressionResolver); RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("locations", new String[]{"#{foo}"}); rbd.setPropertyValues(pvs); - bf.registerBeanDefinition("myProperties", rbd); - Properties properties = (Properties) bf.getBean("myProperties"); - assertEquals("bar", properties.getProperty("foo")); + lbf.registerBeanDefinition("myProperties", rbd); + Properties properties = (Properties) lbf.getBean("myProperties"); + assertThat(properties.getProperty("foo")).isEqualTo("bar"); } @Test - public void testAutowireWithNoDependencies() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireWithNoDependencies() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("rod", bd); - assertEquals(1, lbf.getBeanDefinitionCount()); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); Object registered = lbf.autowire(NoDependencies.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); - assertEquals(1, lbf.getBeanDefinitionCount()); - assertTrue(registered instanceof NoDependencies); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); + assertThat(registered instanceof NoDependencies).isTrue(); } @Test - public void testAutowireWithSatisfiedJavaBeanDependency() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireWithSatisfiedJavaBeanDependency() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("rod", bd); - assertEquals(1, lbf.getBeanDefinitionCount()); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); // Depends on age, name and spouse (TestBean) Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - assertEquals(1, lbf.getBeanDefinitionCount()); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); DependenciesBean kerry = (DependenciesBean) registered; TestBean rod = (TestBean) lbf.getBean("rod"); - assertSame(rod, kerry.getSpouse()); + assertThat(kerry.getSpouse()).isSameAs(rod); } @Test - public void testAutowireWithSatisfiedConstructorDependency() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireWithSatisfiedConstructorDependency() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("rod", bd); - assertEquals(1, lbf.getBeanDefinitionCount()); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); Object registered = lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); - assertEquals(1, lbf.getBeanDefinitionCount()); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); ConstructorDependency kerry = (ConstructorDependency) registered; TestBean rod = (TestBean) lbf.getBean("rod"); - assertSame(rod, kerry.spouse); + assertThat(kerry.spouse).isSameAs(rod); } @Test - public void testAutowireWithTwoMatchesForConstructorDependency() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireWithTwoMatchesForConstructorDependency() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("rod", bd); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("rod2", bd2); - try { - lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertTrue(ex.getMessage().contains("rod")); - assertTrue(ex.getMessage().contains("rod2")); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false)) + .withMessageContaining("rod") + .withMessageContaining("rod2"); } @Test - public void testAutowireWithUnsatisfiedConstructorDependency() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireWithUnsatisfiedConstructorDependency() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("rod", bd); - assertEquals(1, lbf.getBeanDefinitionCount()); - try { - lbf.autowire(UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true); - fail("Should have unsatisfied constructor dependency on SideEffectBean"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - } + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1); + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowire(UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true)); } @Test - public void testAutowireConstructor() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireConstructor() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); ConstructorDependenciesBean bean = (ConstructorDependenciesBean) lbf.autowire(ConstructorDependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true); Object spouse = lbf.getBean("spouse"); - assertTrue(bean.getSpouse1() == spouse); - assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse); + assertThat(bean.getSpouse1() == spouse).isTrue(); + assertThat(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse).isTrue(); } @Test - public void testAutowireBeanByName() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByName() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); TestBean spouse = (TestBean) lbf.getBean("spouse"); - assertEquals(spouse, bean.getSpouse()); - assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse); + assertThat(bean.getSpouse()).isEqualTo(spouse); + assertThat(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse).isTrue(); } @Test - public void testAutowireBeanByNameWithDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByNameWithDependencyCheck() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spous", bd); - try { - lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true)); } @Test - public void testAutowireBeanByNameWithNoDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByNameWithNoDependencyCheck() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spous", bd); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); - assertNull(bean.getSpouse()); + assertThat(bean.getSpouse()).isNull(); } @Test - public void testDependsOnCycle() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void dependsOnCycle() { RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setDependsOn("tb2"); lbf.registerBeanDefinition("tb1", bd1); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setDependsOn("tb1"); lbf.registerBeanDefinition("tb2", bd2); - try { - lbf.preInstantiateSingletons(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - // expected - assertTrue(ex.getMessage().contains("Circular")); - assertTrue(ex.getMessage().contains("'tb2'")); - assertTrue(ex.getMessage().contains("'tb1'")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + lbf.preInstantiateSingletons()) + .withMessageContaining("Circular") + .withMessageContaining("'tb2'") + .withMessageContaining("'tb1'"); } @Test - public void testImplicitDependsOnCycle() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void implicitDependsOnCycle() { RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setDependsOn("tb2"); lbf.registerBeanDefinition("tb1", bd1); @@ -1403,47 +1417,65 @@ public void testImplicitDependsOnCycle() { RootBeanDefinition bd3 = new RootBeanDefinition(TestBean.class); bd3.setDependsOn("tb1"); lbf.registerBeanDefinition("tb3", bd3); - try { - lbf.preInstantiateSingletons(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - // expected - assertTrue(ex.getMessage().contains("Circular")); - assertTrue(ex.getMessage().contains("'tb3'")); - assertTrue(ex.getMessage().contains("'tb1'")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + lbf::preInstantiateSingletons) + .withMessageContaining("Circular") + .withMessageContaining("'tb3'") + .withMessageContaining("'tb1'"); } - @Test(expected = NoSuchBeanDefinitionException.class) - public void testGetBeanByTypeWithNoneFound() { + @Test + void getBeanByTypeWithNoneFound() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); - lbf.getBean(TestBean.class); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class)); } @Test - public void testGetBeanByTypeDefinedInParent() { + void getBeanByTypeWithLateRegistration() { + DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class)); + RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); + lbf.registerBeanDefinition("bd1", bd1); + TestBean bean = lbf.getBean(TestBean.class); + assertThat(bean.getBeanName()).isEqualTo("bd1"); + } + + @Test + void getBeanByTypeWithLateRegistrationAgainstFrozen() { + DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + lbf.freezeConfiguration(); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class)); + RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); + lbf.registerBeanDefinition("bd1", bd1); + TestBean bean = lbf.getBean(TestBean.class); + assertThat(bean.getBeanName()).isEqualTo("bd1"); + } + + @Test + void getBeanByTypeDefinedInParent() { DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); parent.registerBeanDefinition("bd1", bd1); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(parent); TestBean bean = lbf.getBean(TestBean.class); - assertThat(bean.getBeanName(), equalTo("bd1")); + assertThat(bean.getBeanName()).isEqualTo("bd1"); } - @Test(expected = NoUniqueBeanDefinitionException.class) - public void testGetBeanByTypeWithAmbiguity() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + @Test + void getBeanByTypeWithAmbiguity() { RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); - lbf.getBean(TestBean.class); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class)); } @Test - public void testGetBeanByTypeWithPrimary() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeWithPrimary() { RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setLazyInit(true); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); @@ -1451,27 +1483,44 @@ public void testGetBeanByTypeWithPrimary() { lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); - assertThat(bean.getBeanName(), equalTo("bd2")); - assertFalse(lbf.containsSingleton("bd1")); + assertThat(bean.getBeanName()).isEqualTo("bd2"); + assertThat(lbf.containsSingleton("bd1")).isFalse(); } @Test - public void testGetBeanByTypeWithMultiplePrimary() { + @SuppressWarnings("rawtypes") + void getFactoryBeanByTypeWithPrimary() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + RootBeanDefinition bd1 = new RootBeanDefinition(NullTestBeanFactoryBean.class); + RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class); + bd2.setPrimary(true); + lbf.registerBeanDefinition("bd1", bd1); + lbf.registerBeanDefinition("bd2", bd2); + NullTestBeanFactoryBean factoryBeanByType = lbf.getBean(NullTestBeanFactoryBean.class); + NullTestBeanFactoryBean bd1FactoryBean = (NullTestBeanFactoryBean)lbf.getBean("&bd1"); + NullTestBeanFactoryBean bd2FactoryBean = (NullTestBeanFactoryBean)lbf.getBean("&bd2"); + assertThat(factoryBeanByType).isNotNull(); + assertThat(bd1FactoryBean).isNotNull(); + assertThat(bd2FactoryBean).isNotNull(); + assertThat(bd1FactoryBean).isNotEqualTo(factoryBeanByType); + assertThat(bd2FactoryBean).isEqualTo(factoryBeanByType); + } + + @Test + void getBeanByTypeWithMultiplePrimary() { RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setPrimary(true); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setPrimary(true); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); - thrown.expect(NoUniqueBeanDefinitionException.class); - thrown.expectMessage(containsString("more than one 'primary'")); - lbf.getBean(TestBean.class); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class)) + .withMessageContaining("more than one 'primary'"); } @Test - public void testGetBeanByTypeWithPriority() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeWithPriority() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class); @@ -1481,12 +1530,11 @@ public void testGetBeanByTypeWithPriority() { lbf.registerBeanDefinition("bd3", bd3); lbf.preInstantiateSingletons(); TestBean bean = lbf.getBean(TestBean.class); - assertThat(bean.getBeanName(), equalTo("bd1")); + assertThat(bean.getBeanName()).isEqualTo("bd1"); } @Test - public void testMapInjectionWithPriority() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void mapInjectionWithPriority() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class); @@ -1498,38 +1546,35 @@ public void testMapInjectionWithPriority() { lbf.registerBeanDefinition("bd4", bd4); lbf.preInstantiateSingletons(); TestBean bean = lbf.getBean(TestBeanRecipient.class).testBean; - assertThat(bean.getBeanName(), equalTo("bd1")); + assertThat(bean.getBeanName()).isEqualTo("bd1"); } @Test - public void testGetBeanByTypeWithMultiplePriority() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeWithMultiplePriority() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); - thrown.expect(NoUniqueBeanDefinitionException.class); - thrown.expectMessage(containsString("Multiple beans found with the same priority")); - thrown.expectMessage(containsString("5")); // conflicting priority - lbf.getBean(TestBean.class); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class)) + .withMessageContaining("Multiple beans found with the same priority") + .withMessageContaining("5"); // conflicting priority } @Test - public void testGetBeanByTypeWithPriorityAndNullInstance() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeWithPriorityAndNullInstance() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); - assertThat(bean.getBeanName(), equalTo("bd1")); + assertThat(bean.getBeanName()).isEqualTo("bd1"); } @Test - public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypePrimaryHasPrecedenceOverPriority() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); @@ -1537,12 +1582,11 @@ public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() { lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); - assertThat(bean.getBeanName(), equalTo("bd2")); + assertThat(bean.getBeanName()).isEqualTo("bd2"); } @Test - public void testGetBeanByTypeFiltersOutNonAutowireCandidates() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeFiltersOutNonAutowireCandidates() { RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); RootBeanDefinition na1 = new RootBeanDefinition(TestBean.class); @@ -1551,157 +1595,116 @@ public void testGetBeanByTypeFiltersOutNonAutowireCandidates() { lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("na1", na1); TestBean actual = lbf.getBean(TestBean.class); // na1 was filtered - assertSame(lbf.getBean("bd1", TestBean.class), actual); + assertThat(actual).isSameAs(lbf.getBean("bd1", TestBean.class)); lbf.registerBeanDefinition("bd2", bd2); - try { - lbf.getBean(TestBean.class); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class)); } @Test - public void testGetBeanByTypeInstanceWithNoneFound() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeWithNullRequiredType() { + assertThatIllegalArgumentException().isThrownBy(() -> lbf.getBean((Class) null)); + } - try { - lbf.getBean(ConstructorDependency.class); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } - try { - lbf.getBean(ConstructorDependency.class, 42); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } + @Test + void getBeanProviderByTypeWithNullRequiredType() { + assertThatIllegalArgumentException().isThrownBy(() -> lbf.getBeanProvider((Class) null)); + } + + @Test + void resolveNamedBeanByTypeWithNullRequiredType() { + assertThatIllegalArgumentException().isThrownBy(() -> lbf.resolveNamedBean((Class) null)); + } + + @Test + void getBeanByTypeInstanceWithNoneFound() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(ConstructorDependency.class)); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(ConstructorDependency.class, 42)); ObjectProvider provider = lbf.getBeanProvider(ConstructorDependency.class); - try { - provider.getObject(); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } - try { - provider.getObject(42); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } - assertNull(provider.getIfAvailable()); - assertNull(provider.getIfUnique()); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy( + provider::getObject); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + provider.getObject(42)); + assertThat(provider.getIfAvailable()).isNull(); + assertThat(provider.getIfUnique()).isNull(); } @Test - public void testGetBeanByTypeInstanceDefinedInParent() { + void getBeanByTypeInstanceDefinedInParent() { DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99); parent.registerBeanDefinition("bd1", bd1); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(parent); ConstructorDependency bean = lbf.getBean(ConstructorDependency.class); - assertThat(bean.beanName, equalTo("bd1")); - assertThat(bean.spouseAge, equalTo(99)); + assertThat(bean.beanName).isEqualTo("bd1"); + assertThat(bean.spouseAge).isEqualTo(99); bean = lbf.getBean(ConstructorDependency.class, 42); - assertThat(bean.beanName, equalTo("bd1")); - assertThat(bean.spouseAge, equalTo(42)); + assertThat(bean.beanName).isEqualTo("bd1"); + assertThat(bean.spouseAge).isEqualTo(42); ObjectProvider provider = lbf.getBeanProvider(ConstructorDependency.class); bean = provider.getObject(); - assertThat(bean.beanName, equalTo("bd1")); - assertThat(bean.spouseAge, equalTo(99)); + assertThat(bean.beanName).isEqualTo("bd1"); + assertThat(bean.spouseAge).isEqualTo(99); bean = provider.getObject(42); - assertThat(bean.beanName, equalTo("bd1")); - assertThat(bean.spouseAge, equalTo(42)); + assertThat(bean.beanName).isEqualTo("bd1"); + assertThat(bean.spouseAge).isEqualTo(42); bean = provider.getIfAvailable(); - assertThat(bean.beanName, equalTo("bd1")); - assertThat(bean.spouseAge, equalTo(99)); + assertThat(bean.beanName).isEqualTo("bd1"); + assertThat(bean.spouseAge).isEqualTo(99); bean = provider.getIfUnique(); - assertThat(bean.beanName, equalTo("bd1")); - assertThat(bean.spouseAge, equalTo(99)); + assertThat(bean.beanName).isEqualTo("bd1"); + assertThat(bean.spouseAge).isEqualTo(99); } @Test - public void testGetBeanByTypeInstanceWithAmbiguity() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeInstanceWithAmbiguity() { RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99); RootBeanDefinition bd2 = new RootBeanDefinition(ConstructorDependency.class); - bd2.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd2.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd2.getConstructorArgumentValues().addGenericArgumentValue("43"); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); - - try { - lbf.getBean(ConstructorDependency.class); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - try { - lbf.getBean(ConstructorDependency.class, 42); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(ConstructorDependency.class)); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(ConstructorDependency.class, 42)); ObjectProvider provider = lbf.getBeanProvider(ConstructorDependency.class); - try { - provider.getObject(); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - try { - provider.getObject(42); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - try { - provider.getIfAvailable(); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - assertNull(provider.getIfUnique()); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy( + provider::getObject); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> + provider.getObject(42)); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy( + provider::getIfAvailable); + assertThat(provider.getIfUnique()).isNull(); Set resolved = new HashSet<>(); for (ConstructorDependency instance : provider) { resolved.add(instance); } - assertEquals(2, resolved.size()); - assertTrue(resolved.contains(lbf.getBean("bd1"))); - assertTrue(resolved.contains(lbf.getBean("bd2"))); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.contains(lbf.getBean("bd1"))).isTrue(); + assertThat(resolved.contains(lbf.getBean("bd2"))).isTrue(); resolved = new HashSet<>(); provider.forEach(resolved::add); - assertEquals(2, resolved.size()); - assertTrue(resolved.contains(lbf.getBean("bd1"))); - assertTrue(resolved.contains(lbf.getBean("bd2"))); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.contains(lbf.getBean("bd1"))).isTrue(); + assertThat(resolved.contains(lbf.getBean("bd2"))).isTrue(); resolved = provider.stream().collect(Collectors.toSet()); - assertEquals(2, resolved.size()); - assertTrue(resolved.contains(lbf.getBean("bd1"))); - assertTrue(resolved.contains(lbf.getBean("bd2"))); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.contains(lbf.getBean("bd1"))).isTrue(); + assertThat(resolved.contains(lbf.getBean("bd2"))).isTrue(); } @Test - public void testGetBeanByTypeInstanceWithPrimary() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeInstanceWithPrimary() { RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99); RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43); bd2.setPrimary(true); @@ -1709,49 +1712,48 @@ public void testGetBeanByTypeInstanceWithPrimary() { lbf.registerBeanDefinition("bd2", bd2); ConstructorDependency bean = lbf.getBean(ConstructorDependency.class); - assertThat(bean.beanName, equalTo("bd2")); - assertThat(bean.spouseAge, equalTo(43)); + assertThat(bean.beanName).isEqualTo("bd2"); + assertThat(bean.spouseAge).isEqualTo(43); bean = lbf.getBean(ConstructorDependency.class, 42); - assertThat(bean.beanName, equalTo("bd2")); - assertThat(bean.spouseAge, equalTo(42)); + assertThat(bean.beanName).isEqualTo("bd2"); + assertThat(bean.spouseAge).isEqualTo(42); ObjectProvider provider = lbf.getBeanProvider(ConstructorDependency.class); bean = provider.getObject(); - assertThat(bean.beanName, equalTo("bd2")); - assertThat(bean.spouseAge, equalTo(43)); + assertThat(bean.beanName).isEqualTo("bd2"); + assertThat(bean.spouseAge).isEqualTo(43); bean = provider.getObject(42); - assertThat(bean.beanName, equalTo("bd2")); - assertThat(bean.spouseAge, equalTo(42)); + assertThat(bean.beanName).isEqualTo("bd2"); + assertThat(bean.spouseAge).isEqualTo(42); bean = provider.getIfAvailable(); - assertThat(bean.beanName, equalTo("bd2")); - assertThat(bean.spouseAge, equalTo(43)); + assertThat(bean.beanName).isEqualTo("bd2"); + assertThat(bean.spouseAge).isEqualTo(43); bean = provider.getIfUnique(); - assertThat(bean.beanName, equalTo("bd2")); - assertThat(bean.spouseAge, equalTo(43)); + assertThat(bean.beanName).isEqualTo("bd2"); + assertThat(bean.spouseAge).isEqualTo(43); Set resolved = new HashSet<>(); for (ConstructorDependency instance : provider) { resolved.add(instance); } - assertEquals(2, resolved.size()); - assertTrue(resolved.contains(lbf.getBean("bd1"))); - assertTrue(resolved.contains(lbf.getBean("bd2"))); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.contains(lbf.getBean("bd1"))).isTrue(); + assertThat(resolved.contains(lbf.getBean("bd2"))).isTrue(); resolved = new HashSet<>(); provider.forEach(resolved::add); - assertEquals(2, resolved.size()); - assertTrue(resolved.contains(lbf.getBean("bd1"))); - assertTrue(resolved.contains(lbf.getBean("bd2"))); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.contains(lbf.getBean("bd1"))).isTrue(); + assertThat(resolved.contains(lbf.getBean("bd2"))).isTrue(); resolved = provider.stream().collect(Collectors.toSet()); - assertEquals(2, resolved.size()); - assertTrue(resolved.contains(lbf.getBean("bd1"))); - assertTrue(resolved.contains(lbf.getBean("bd2"))); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.contains(lbf.getBean("bd1"))).isTrue(); + assertThat(resolved.contains(lbf.getBean("bd2"))).isTrue(); } @Test - public void testGetBeanByTypeInstanceWithMultiplePrimary() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeInstanceWithMultiplePrimary() { RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99); RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43); bd1.setPrimary(true); @@ -1759,14 +1761,13 @@ public void testGetBeanByTypeInstanceWithMultiplePrimary() { lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); - thrown.expect(NoUniqueBeanDefinitionException.class); - thrown.expectMessage(containsString("more than one 'primary'")); - lbf.getBean(ConstructorDependency.class, 42); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(ConstructorDependency.class, 42)) + .withMessageContaining("more than one 'primary'"); } @Test - public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanByTypeInstanceFiltersOutNonAutowireCandidates() { RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99); RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43); RootBeanDefinition na1 = createConstructorDependencyBeanDefinition(21); @@ -1775,153 +1776,151 @@ public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates() { lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("na1", na1); ConstructorDependency actual = lbf.getBean(ConstructorDependency.class, 42); // na1 was filtered - assertThat(actual.beanName, equalTo("bd1")); + assertThat(actual.beanName).isEqualTo("bd1"); lbf.registerBeanDefinition("bd2", bd2); - try { - lbf.getBean(TestBean.class, 67); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + lbf.getBean(TestBean.class, 67)); } @Test - public void testBeanProviderSerialization() throws Exception { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + @SuppressWarnings("rawtypes") + void beanProviderSerialization() throws Exception { lbf.setSerializationId("test"); ObjectProvider provider = lbf.getBeanProvider(ConstructorDependency.class); ObjectProvider deserialized = (ObjectProvider) SerializationTestUtils.serializeAndDeserialize(provider); - try { - deserialized.getObject(); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } - try { - deserialized.getObject(42); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } - assertNull(deserialized.getIfAvailable()); - assertNull(deserialized.getIfUnique()); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy( + deserialized::getObject); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + deserialized.getObject(42)); + assertThat(deserialized.getIfAvailable()).isNull(); + assertThat(deserialized.getIfUnique()).isNull(); } @Test - public void testGetBeanWithArgsNotCreatedForFactoryBeanChecking() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanWithArgsNotCreatedForFactoryBeanChecking() { RootBeanDefinition bd1 = new RootBeanDefinition(ConstructorDependency.class); - bd1.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd1.setScope(BeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("bd1", bd1); RootBeanDefinition bd2 = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); - bd2.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd2.setScope(BeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("bd2", bd2); ConstructorDependency bean = lbf.getBean(ConstructorDependency.class, 42); - assertThat(bean.beanName, equalTo("bd1")); - assertThat(bean.spouseAge, equalTo(42)); + assertThat(bean.beanName).isEqualTo("bd1"); + assertThat(bean.spouseAge).isEqualTo(42); - assertEquals(1, lbf.getBeanNamesForType(ConstructorDependency.class).length); - assertEquals(1, lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length); - assertEquals(1, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)).length); - assertEquals(0, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)).length); + assertThat(lbf.getBeanNamesForType(ConstructorDependency.class).length).isEqualTo(1); + assertThat(lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length).isEqualTo(1); + assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)).length).isEqualTo(1); + assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)).length).isEqualTo(0); + assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class), true, true).length).isEqualTo(1); + assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class), true, true).length).isEqualTo(0); } private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) { RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependency.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.getConstructorArgumentValues().addGenericArgumentValue(age); return bd; } @Test - public void testAutowireBeanByType() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByType() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("test", bd); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); TestBean test = (TestBean) lbf.getBean("test"); - assertEquals(test, bean.getSpouse()); + assertThat(bean.getSpouse()).isEqualTo(test); } /** * Verifies that a dependency on a {@link FactoryBean} can be autowired * by type, specifically addressing the JIRA issue raised in SPR-4040. */ @Test - public void testAutowireBeanWithFactoryBeanByType() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanWithFactoryBeanByType() { RootBeanDefinition bd = new RootBeanDefinition(LazyInitFactory.class); lbf.registerBeanDefinition("factoryBean", bd); LazyInitFactory factoryBean = (LazyInitFactory) lbf.getBean("&factoryBean"); - assertNotNull("The FactoryBean should have been registered.", factoryBean); + assertThat(factoryBean).as("The FactoryBean should have been registered.").isNotNull(); FactoryBeanDependentBean bean = (FactoryBeanDependentBean) lbf.autowire(FactoryBeanDependentBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the LazyInitFactory.", - factoryBean, bean.getFactoryBean()); + assertThat(bean.getFactoryBean()).as("The FactoryBeanDependentBean should have been autowired 'by type' with the LazyInitFactory.").isEqualTo(factoryBean); } @Test - public void testGetTypeForAbstractFactoryBean() { + void autowireBeanWithFactoryBeanByTypeWithPrimary() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + RootBeanDefinition bd1 = new RootBeanDefinition(LazyInitFactory.class); + RootBeanDefinition bd2 = new RootBeanDefinition(LazyInitFactory.class); + bd2.setPrimary(true); + lbf.registerBeanDefinition("bd1", bd1); + lbf.registerBeanDefinition("bd2", bd2); + LazyInitFactory bd1FactoryBean = (LazyInitFactory) lbf.getBean("&bd1"); + LazyInitFactory bd2FactoryBean = (LazyInitFactory) lbf.getBean("&bd2"); + assertThat(bd1FactoryBean).isNotNull(); + assertThat(bd2FactoryBean).isNotNull(); + FactoryBeanDependentBean bean = (FactoryBeanDependentBean) lbf.autowire(FactoryBeanDependentBean.class, + AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); + assertThat(bean.getFactoryBean()).isNotEqualTo(bd1FactoryBean); + assertThat(bean.getFactoryBean()).isEqualTo(bd2FactoryBean); + } + + @Test + void getTypeForAbstractFactoryBean() { RootBeanDefinition bd = new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class); bd.setAbstract(true); lbf.registerBeanDefinition("factoryBean", bd); - assertNull(lbf.getType("factoryBean")); + assertThat(lbf.getType("factoryBean")).isNull(); } @Test - public void testGetBeanNamesForTypeBeforeFactoryBeanCreation() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanNamesForTypeBeforeFactoryBeanCreation() { lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); - assertFalse(lbf.containsSingleton("factoryBean")); + assertThat(lbf.containsSingleton("factoryBean")).isFalse(); String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); beanNames = lbf.getBeanNamesForType(Callable.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); } @Test - public void testGetBeanNamesForTypeAfterFactoryBeanCreation() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void getBeanNamesForTypeAfterFactoryBeanCreation() { lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); lbf.getBean("&factoryBean"); String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); beanNames = lbf.getBeanNamesForType(Callable.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false); - assertEquals(1, beanNames.length); - assertEquals("&factoryBean", beanNames[0]); + assertThat(beanNames.length).isEqualTo(1); + assertThat(beanNames[0]).isEqualTo("&factoryBean"); } /** @@ -1930,57 +1929,43 @@ public void testGetBeanNamesForTypeAfterFactoryBeanCreation() { * Java method names. In other words, you can't name a method * {@code set&FactoryBean(...)}. */ - @Test(expected = TypeMismatchException.class) - public void testAutowireBeanWithFactoryBeanByName() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + @Test + void autowireBeanWithFactoryBeanByName() { RootBeanDefinition bd = new RootBeanDefinition(LazyInitFactory.class); lbf.registerBeanDefinition("factoryBean", bd); LazyInitFactory factoryBean = (LazyInitFactory) lbf.getBean("&factoryBean"); - assertNotNull("The FactoryBean should have been registered.", factoryBean); - lbf.autowire(FactoryBeanDependentBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); + assertThat(factoryBean).as("The FactoryBean should have been registered.").isNotNull(); + assertThatExceptionOfType(TypeMismatchException.class).isThrownBy(() -> + lbf.autowire(FactoryBeanDependentBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true)); } @Test - public void testAutowireBeanByTypeWithTwoMatches() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByTypeWithTwoMatches() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); - try { - lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertTrue(ex.getMessage().contains("test")); - assertTrue(ex.getMessage().contains("spouse")); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) + .withMessageContaining("test") + .withMessageContaining("spouse"); } @Test - public void testAutowireBeanByTypeWithDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); - try { - lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - } + void autowireBeanByTypeWithDependencyCheck() { + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)); } @Test - public void testAutowireBeanByTypeWithNoDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByTypeWithNoDependencyCheck() { DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); - assertNull(bean.getSpouse()); + assertThat(bean.getSpouse()).isNull(); } @Test - public void testAutowireBeanByTypeWithTwoMatchesAndOnePrimary() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByTypeWithTwoMatchesAndOnePrimary() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPrimary(true); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); @@ -1989,33 +1974,24 @@ public void testAutowireBeanByTypeWithTwoMatchesAndOnePrimary() { DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - assertThat(bean.getSpouse(), equalTo(lbf.getBean("test"))); + assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("test")); } @Test - public void testAutowireBeanByTypeWithTwoPrimaryCandidates() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByTypeWithTwoPrimaryCandidates() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPrimary(true); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setPrimary(true); lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); - - try { - lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertNotNull("Exception should have cause", ex.getCause()); - assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) + .withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class); } @Test - public void testAutowireBeanByTypeWithTwoMatchesAndPriority() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByTypeWithTwoMatchesAndPriority() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class); @@ -2024,33 +2000,24 @@ public void testAutowireBeanByTypeWithTwoMatchesAndPriority() { DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - assertThat(bean.getSpouse(), equalTo(lbf.getBean("test"))); + assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("test")); } @Test - public void testAutowireBeanByTypeWithIdenticalPriorityCandidates() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByTypeWithIdenticalPriorityCandidates() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class); lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); - - try { - lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertNotNull("Exception should have cause", ex.getCause()); - assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass()); - assertTrue(ex.getMessage().contains("5")); // conflicting priority - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) + .withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class) + .withMessageContaining("5"); } @Test - public void testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireBeanByTypePrimaryTakesPrecedenceOverPriority() { lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); @@ -2060,154 +2027,128 @@ public void testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority() { DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - assertThat(bean.getSpouse(), equalTo(lbf.getBean("spouse"))); + assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("spouse")); } @Test - public void testAutowireExistingBeanByName() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireExistingBeanByName() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); DependenciesBean existingBean = new DependenciesBean(); lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); TestBean spouse = (TestBean) lbf.getBean("spouse"); - assertEquals(existingBean.getSpouse(), spouse); - assertSame(spouse, BeanFactoryUtils.beanOfType(lbf, TestBean.class)); + assertThat(spouse).isEqualTo(existingBean.getSpouse()); + assertThat(BeanFactoryUtils.beanOfType(lbf, TestBean.class)).isSameAs(spouse); } @Test - public void testAutowireExistingBeanByNameWithDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireExistingBeanByNameWithDependencyCheck() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spous", bd); DependenciesBean existingBean = new DependenciesBean(); - try { - lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true)); } @Test - public void testAutowireExistingBeanByNameWithNoDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireExistingBeanByNameWithNoDependencyCheck() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spous", bd); DependenciesBean existingBean = new DependenciesBean(); lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); - assertNull(existingBean.getSpouse()); + assertThat(existingBean.getSpouse()).isNull(); } @Test - public void testAutowireExistingBeanByType() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireExistingBeanByType() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("test", bd); DependenciesBean existingBean = new DependenciesBean(); lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); TestBean test = (TestBean) lbf.getBean("test"); - assertEquals(existingBean.getSpouse(), test); + assertThat(test).isEqualTo(existingBean.getSpouse()); } @Test - public void testAutowireExistingBeanByTypeWithDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireExistingBeanByTypeWithDependencyCheck() { DependenciesBean existingBean = new DependenciesBean(); - try { - lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException expected) { - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)); } @Test - public void testAutowireExistingBeanByTypeWithNoDependencyCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void autowireExistingBeanByTypeWithNoDependencyCheck() { DependenciesBean existingBean = new DependenciesBean(); lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); - assertNull(existingBean.getSpouse()); + assertThat(existingBean.getSpouse()).isNull(); } @Test - public void testInvalidAutowireMode() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); - try { - lbf.autowireBeanProperties(new TestBean(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException expected) { - } + void invalidAutowireMode() { + assertThatIllegalArgumentException().isThrownBy(() -> + lbf.autowireBeanProperties(new TestBean(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false)); } @Test - public void testApplyBeanPropertyValues() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void applyBeanPropertyValues() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "99"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("test", bd); TestBean tb = new TestBean(); - assertEquals(0, tb.getAge()); + assertThat(tb.getAge()).isEqualTo(0); lbf.applyBeanPropertyValues(tb, "test"); - assertEquals(99, tb.getAge()); + assertThat(tb.getAge()).isEqualTo(99); } @Test - public void testApplyBeanPropertyValuesWithIncompleteDefinition() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void applyBeanPropertyValuesWithIncompleteDefinition() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "99"); RootBeanDefinition bd = new RootBeanDefinition(); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("test", bd); TestBean tb = new TestBean(); - assertEquals(0, tb.getAge()); + assertThat(tb.getAge()).isEqualTo(0); lbf.applyBeanPropertyValues(tb, "test"); - assertEquals(99, tb.getAge()); - assertNull(tb.getBeanFactory()); - assertNull(tb.getSpouse()); + assertThat(tb.getAge()).isEqualTo(99); + assertThat(tb.getBeanFactory()).isNull(); + assertThat(tb.getSpouse()).isNull(); } @Test - public void testCreateBean() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void createBean() { TestBean tb = lbf.createBean(TestBean.class); - assertSame(lbf, tb.getBeanFactory()); + assertThat(tb.getBeanFactory()).isSameAs(lbf); lbf.destroyBean(tb); } @Test - public void testCreateBeanWithDisposableBean() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void createBeanWithDisposableBean() { DerivedTestBean tb = lbf.createBean(DerivedTestBean.class); - assertSame(lbf, tb.getBeanFactory()); + assertThat(tb.getBeanFactory()).isSameAs(lbf); lbf.destroyBean(tb); - assertTrue(tb.wasDestroyed()); + assertThat(tb.wasDestroyed()).isTrue(); } @Test - public void testConfigureBean() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void configureBean() { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "99"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("test", bd); TestBean tb = new TestBean(); - assertEquals(0, tb.getAge()); + assertThat(tb.getAge()).isEqualTo(0); lbf.configureBean(tb, "test"); - assertEquals(99, tb.getAge()); - assertSame(lbf, tb.getBeanFactory()); - assertNull(tb.getSpouse()); + assertThat(tb.getAge()).isEqualTo(99); + assertThat(tb.getBeanFactory()).isSameAs(lbf); + assertThat(tb.getSpouse()).isNull(); } @Test - public void testConfigureBeanWithAutowiring() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void configureBeanWithAutowiring() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); MutablePropertyValues pvs = new MutablePropertyValues(); @@ -2217,14 +2158,13 @@ public void testConfigureBeanWithAutowiring() { lbf.registerBeanDefinition("test", tbd); TestBean tb = new TestBean(); lbf.configureBean(tb, "test"); - assertSame(lbf, tb.getBeanFactory()); + assertThat(tb.getBeanFactory()).isSameAs(lbf); TestBean spouse = (TestBean) lbf.getBean("spouse"); - assertEquals(spouse, tb.getSpouse()); + assertThat(tb.getSpouse()).isEqualTo(spouse); } @Test - public void testExtensiveCircularReference() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void extensiveCircularReference() { for (int i = 0; i < 1000; i++) { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0)))); @@ -2236,55 +2176,39 @@ public void testExtensiveCircularReference() { for (int i = 0; i < 1000; i++) { TestBean bean = (TestBean) lbf.getBean("bean" + i); TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0)); - assertTrue(bean.getSpouse() == otherBean); + assertThat(bean.getSpouse() == otherBean).isTrue(); } } @Test - public void testCircularReferenceThroughAutowiring() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void circularReferenceThroughAutowiring() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); - try { - lbf.preInstantiateSingletons(); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException expected) { - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy( + lbf::preInstantiateSingletons); } @Test - public void testCircularReferenceThroughFactoryBeanAutowiring() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void circularReferenceThroughFactoryBeanAutowiring() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); - try { - lbf.preInstantiateSingletons(); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException expected) { - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy( + lbf::preInstantiateSingletons); } @Test - public void testCircularReferenceThroughFactoryBeanTypeCheck() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void circularReferenceThroughFactoryBeanTypeCheck() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); - try { - lbf.getBeansOfType(String.class); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException expected) { - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + lbf.getBeansOfType(String.class)); } @Test - public void testAvoidCircularReferenceThroughAutowiring() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void avoidCircularReferenceThroughAutowiring() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); @@ -2295,8 +2219,7 @@ public void testAvoidCircularReferenceThroughAutowiring() { } @Test - public void testConstructorDependencyWithClassResolution() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void constructorDependencyWithClassResolution() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class); bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.String"); lbf.registerBeanDefinition("test", bd); @@ -2304,141 +2227,138 @@ public void testConstructorDependencyWithClassResolution() { } @Test - public void testConstructorDependencyWithUnresolvableClass() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void constructorDependencyWithUnresolvableClass() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class); bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.Strin"); lbf.registerBeanDefinition("test", bd); - try { - lbf.preInstantiateSingletons(); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException expected) { - assertTrue(expected.toString().contains("java.lang.Strin")); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy( + lbf::preInstantiateSingletons); } @Test - public void testBeanDefinitionWithInterface() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanDefinitionWithInterface() { lbf.registerBeanDefinition("test", new RootBeanDefinition(ITestBean.class)); - try { - lbf.getBean("test"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertEquals("test", ex.getBeanName()); - assertTrue(ex.getMessage().toLowerCase().contains("interface")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + lbf.getBean("test")) + .withMessageContaining("interface") + .satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test")); } @Test - public void testBeanDefinitionWithAbstractClass() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanDefinitionWithAbstractClass() { lbf.registerBeanDefinition("test", new RootBeanDefinition(AbstractBeanFactory.class)); - try { - lbf.getBean("test"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertEquals("test", ex.getBeanName()); - assertTrue(ex.getMessage().toLowerCase().contains("abstract")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + lbf.getBean("test")) + .withMessageContaining("abstract") + .satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test")); } @Test - public void testPrototypeFactoryBeanNotEagerlyCalled() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeFactoryBeanNotEagerlyCalled() { lbf.registerBeanDefinition("test", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); lbf.preInstantiateSingletons(); } @Test - public void testLazyInitFactory() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void lazyInitFlag() { + DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); + RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); + bd1.setLazyInit(true); + factory.registerBeanDefinition("tb1", bd1); + RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); + bd2.setLazyInit(false); + factory.registerBeanDefinition("tb2", bd2); + factory.registerBeanDefinition("tb3", new RootBeanDefinition(TestBean.class)); + + assertThat(((AbstractBeanDefinition) factory.getMergedBeanDefinition("tb1")).getLazyInit()).isEqualTo(Boolean.TRUE); + assertThat(((AbstractBeanDefinition) factory.getMergedBeanDefinition("tb2")).getLazyInit()).isEqualTo(Boolean.FALSE); + assertThat(((AbstractBeanDefinition) factory.getMergedBeanDefinition("tb3")).getLazyInit()).isNull(); + + factory.preInstantiateSingletons(); + assertThat(factory.containsSingleton("tb1")).isFalse(); + assertThat(factory.containsSingleton("tb2")).isTrue(); + assertThat(factory.containsSingleton("tb3")).isTrue(); + } + + @Test + void lazyInitFactory() { lbf.registerBeanDefinition("test", new RootBeanDefinition(LazyInitFactory.class)); lbf.preInstantiateSingletons(); LazyInitFactory factory = (LazyInitFactory) lbf.getBean("&test"); - assertFalse(factory.initialized); + assertThat(factory.initialized).isFalse(); } @Test - public void testSmartInitFactory() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void smartInitFactory() { lbf.registerBeanDefinition("test", new RootBeanDefinition(EagerInitFactory.class)); lbf.preInstantiateSingletons(); EagerInitFactory factory = (EagerInitFactory) lbf.getBean("&test"); - assertTrue(factory.initialized); + assertThat(factory.initialized).isTrue(); } @Test - public void testPrototypeFactoryBeanNotEagerlyCalledInCaseOfBeanClassName() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeFactoryBeanNotEagerlyCalledInCaseOfBeanClassName() { lbf.registerBeanDefinition("test", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName(), null, null)); lbf.preInstantiateSingletons(); } @Test - public void testPrototypeStringCreatedRepeatedly() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeStringCreatedRepeatedly() { RootBeanDefinition stringDef = new RootBeanDefinition(String.class); - stringDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + stringDef.setScope(BeanDefinition.SCOPE_PROTOTYPE); stringDef.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue("value")); lbf.registerBeanDefinition("string", stringDef); String val1 = lbf.getBean("string", String.class); String val2 = lbf.getBean("string", String.class); - assertEquals("value", val1); - assertEquals("value", val2); - assertNotSame(val1, val2); + assertThat(val1).isEqualTo("value"); + assertThat(val2).isEqualTo("value"); + assertThat(val2).isNotSameAs(val1); } @Test - public void testPrototypeWithArrayConversionForConstructor() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeWithArrayConversionForConstructor() { List list = new ManagedList<>(); list.add("myName"); list.add("myBeanName"); RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.getConstructorArgumentValues().addGenericArgumentValue(list); lbf.registerBeanDefinition("test", bd); DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test"); - assertEquals("myName", tb.getName()); - assertEquals("myBeanName", tb.getBeanName()); + assertThat(tb.getName()).isEqualTo("myName"); + assertThat(tb.getBeanName()).isEqualTo("myBeanName"); DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test"); - assertTrue(tb != tb2); - assertEquals("myName", tb2.getName()); - assertEquals("myBeanName", tb2.getBeanName()); + assertThat(tb != tb2).isTrue(); + assertThat(tb2.getName()).isEqualTo("myName"); + assertThat(tb2.getBeanName()).isEqualTo("myBeanName"); } @Test - public void testPrototypeWithArrayConversionForFactoryMethod() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void prototypeWithArrayConversionForFactoryMethod() { List list = new ManagedList<>(); list.add("myName"); list.add("myBeanName"); RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setFactoryMethodName("create"); bd.getConstructorArgumentValues().addGenericArgumentValue(list); lbf.registerBeanDefinition("test", bd); DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test"); - assertEquals("myName", tb.getName()); - assertEquals("myBeanName", tb.getBeanName()); + assertThat(tb.getName()).isEqualTo("myName"); + assertThat(tb.getBeanName()).isEqualTo("myBeanName"); DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test"); - assertTrue(tb != tb2); - assertEquals("myName", tb2.getName()); - assertEquals("myBeanName", tb2.getBeanName()); + assertThat(tb != tb2).isTrue(); + assertThat(tb2.getName()).isEqualTo("myName"); + assertThat(tb2.getBeanName()).isEqualTo("myBeanName"); } @Test - public void testPrototypeCreationIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void prototypeCreationIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("test", rbd); lbf.freezeConfiguration(); StopWatch sw = new StopWatch(); @@ -2448,16 +2368,15 @@ public void testPrototypeCreationIsFastEnough() { } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000); + assertThat(sw.getTotalTimeMillis() < 3000).as("Prototype creation took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - public void testPrototypeCreationWithDependencyCheckIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void prototypeCreationWithDependencyCheckIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(LifecycleBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); rbd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS); lbf.registerBeanDefinition("test", rbd); lbf.addBeanPostProcessor(new LifecycleBean.PostProcessor()); @@ -2469,17 +2388,15 @@ public void testPrototypeCreationWithDependencyCheckIsFastEnough() { } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000); + assertThat(sw.getTotalTimeMillis() < 3000).as("Prototype creation took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - @Ignore // TODO re-enable when ConstructorResolver TODO sorted out - public void testPrototypeCreationWithConstructorArgumentsIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void prototypeCreationWithConstructorArgumentsIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); rbd.getConstructorArgumentValues().addGenericArgumentValue("juergen"); rbd.getConstructorArgumentValues().addGenericArgumentValue("99"); lbf.registerBeanDefinition("test", rbd); @@ -2488,21 +2405,19 @@ public void testPrototypeCreationWithConstructorArgumentsIsFastEnough() { sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); - assertEquals("juergen", tb.getName()); - assertEquals(99, tb.getAge()); + assertThat(tb.getName()).isEqualTo("juergen"); + assertThat(tb.getAge()).isEqualTo(99); } sw.stop(); - // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000); + assertThat(sw.getTotalTimeMillis() < 3000).as("Prototype creation took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - public void testPrototypeCreationWithResolvedConstructorArgumentsIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void prototypeCreationWithResolvedConstructorArgumentsIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); rbd.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("spouse")); lbf.registerBeanDefinition("test", rbd); lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); @@ -2512,20 +2427,19 @@ public void testPrototypeCreationWithResolvedConstructorArgumentsIsFastEnough() sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); - assertSame(spouse, tb.getSpouse()); + assertThat(tb.getSpouse()).isSameAs(spouse); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); + assertThat(sw.getTotalTimeMillis() < 4000).as("Prototype creation took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - public void testPrototypeCreationWithPropertiesIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void prototypeCreationWithPropertiesIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("name", "juergen"); rbd.getPropertyValues().add("age", "99"); lbf.registerBeanDefinition("test", rbd); @@ -2534,21 +2448,20 @@ public void testPrototypeCreationWithPropertiesIsFastEnough() { sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); - assertEquals("juergen", tb.getName()); - assertEquals(99, tb.getAge()); + assertThat(tb.getName()).isEqualTo("juergen"); + assertThat(tb.getAge()).isEqualTo(99); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); + assertThat(sw.getTotalTimeMillis() < 4000).as("Prototype creation took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - public void testPrototypeCreationWithResolvedPropertiesIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void prototypeCreationWithResolvedPropertiesIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse")); lbf.registerBeanDefinition("test", rbd); lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); @@ -2558,18 +2471,17 @@ public void testPrototypeCreationWithResolvedPropertiesIsFastEnough() { sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); - assertSame(spouse, tb.getSpouse()); + assertThat(tb.getSpouse()).isSameAs(spouse); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); + assertThat(sw.getTotalTimeMillis() < 4000).as("Prototype creation took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - public void testSingletonLookupByNameIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void singletonLookupByNameIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); lbf.freezeConfiguration(); StopWatch sw = new StopWatch(); @@ -2579,14 +2491,13 @@ public void testSingletonLookupByNameIsFastEnough() { } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Singleton lookup took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 1000); + assertThat(sw.getTotalTimeMillis() < 1000).as("Singleton lookup took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - public void testSingletonLookupByTypeIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void singletonLookupByTypeIsFastEnough() { Assume.notLogging(factoryLog); - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); lbf.freezeConfiguration(); StopWatch sw = new StopWatch(); @@ -2596,12 +2507,11 @@ public void testSingletonLookupByTypeIsFastEnough() { } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); - assertTrue("Singleton lookup took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 1000); + assertThat(sw.getTotalTimeMillis() < 1000).as("Singleton lookup took too long: " + sw.getTotalTimeMillis()).isTrue(); } @Test - public void testBeanPostProcessorWithWrappedObjectAndDisposableBean() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanPostProcessorWithWrappedObjectAndDisposableBean() { RootBeanDefinition bd = new RootBeanDefinition(BeanWithDisposableBean.class); lbf.registerBeanDefinition("test", bd); lbf.addBeanPostProcessor(new BeanPostProcessor() { @@ -2609,20 +2519,15 @@ public void testBeanPostProcessorWithWrappedObjectAndDisposableBean() { public Object postProcessBeforeInitialization(Object bean, String beanName) { return new TestBean(); } - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) { - return bean; - } }); BeanWithDisposableBean.closed = false; lbf.preInstantiateSingletons(); lbf.destroySingletons(); - assertTrue("Destroy method invoked", BeanWithDisposableBean.closed); + assertThat(BeanWithDisposableBean.closed).as("Destroy method invoked").isTrue(); } @Test - public void testBeanPostProcessorWithWrappedObjectAndCloseable() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanPostProcessorWithWrappedObjectAndCloseable() { RootBeanDefinition bd = new RootBeanDefinition(BeanWithCloseable.class); lbf.registerBeanDefinition("test", bd); lbf.addBeanPostProcessor(new BeanPostProcessor() { @@ -2630,20 +2535,15 @@ public void testBeanPostProcessorWithWrappedObjectAndCloseable() { public Object postProcessBeforeInitialization(Object bean, String beanName) { return new TestBean(); } - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) { - return bean; - } }); BeanWithDisposableBean.closed = false; lbf.preInstantiateSingletons(); lbf.destroySingletons(); - assertTrue("Destroy method invoked", BeanWithCloseable.closed); + assertThat(BeanWithCloseable.closed).as("Destroy method invoked").isTrue(); } @Test - public void testBeanPostProcessorWithWrappedObjectAndDestroyMethod() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void beanPostProcessorWithWrappedObjectAndDestroyMethod() { RootBeanDefinition bd = new RootBeanDefinition(BeanWithDestroyMethod.class); bd.setDestroyMethodName("close"); lbf.registerBeanDefinition("test", bd); @@ -2652,20 +2552,15 @@ public void testBeanPostProcessorWithWrappedObjectAndDestroyMethod() { public Object postProcessBeforeInitialization(Object bean, String beanName) { return new TestBean(); } - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) { - return bean; - } }); BeanWithDestroyMethod.closeCount = 0; lbf.preInstantiateSingletons(); lbf.destroySingletons(); - assertEquals("Destroy methods invoked", 1, BeanWithDestroyMethod.closeCount); + assertThat(BeanWithDestroyMethod.closeCount).as("Destroy methods invoked").isEqualTo(1); } @Test - public void testDestroyMethodOnInnerBean() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void destroyMethodOnInnerBean() { RootBeanDefinition innerBd = new RootBeanDefinition(BeanWithDestroyMethod.class); innerBd.setDestroyMethodName("close"); RootBeanDefinition bd = new RootBeanDefinition(BeanWithDestroyMethod.class); @@ -2675,14 +2570,13 @@ public void testDestroyMethodOnInnerBean() { BeanWithDestroyMethod.closeCount = 0; lbf.preInstantiateSingletons(); lbf.destroySingletons(); - assertEquals("Destroy methods invoked", 2, BeanWithDestroyMethod.closeCount); + assertThat(BeanWithDestroyMethod.closeCount).as("Destroy methods invoked").isEqualTo(2); } @Test - public void testDestroyMethodOnInnerBeanAsPrototype() { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + void destroyMethodOnInnerBeanAsPrototype() { RootBeanDefinition innerBd = new RootBeanDefinition(BeanWithDestroyMethod.class); - innerBd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + innerBd.setScope(BeanDefinition.SCOPE_PROTOTYPE); innerBd.setDestroyMethodName("close"); RootBeanDefinition bd = new RootBeanDefinition(BeanWithDestroyMethod.class); bd.setDestroyMethodName("close"); @@ -2691,16 +2585,16 @@ public void testDestroyMethodOnInnerBeanAsPrototype() { BeanWithDestroyMethod.closeCount = 0; lbf.preInstantiateSingletons(); lbf.destroySingletons(); - assertEquals("Destroy methods invoked", 1, BeanWithDestroyMethod.closeCount); + assertThat(BeanWithDestroyMethod.closeCount).as("Destroy methods invoked").isEqualTo(1); } @Test - public void testFindTypeOfSingletonFactoryMethodOnBeanInstance() { + void findTypeOfSingletonFactoryMethodOnBeanInstance() { findTypeOfPrototypeFactoryMethodOnBeanInstance(true); } @Test - public void testFindTypeOfPrototypeFactoryMethodOnBeanInstance() { + void findTypeOfPrototypeFactoryMethodOnBeanInstance() { findTypeOfPrototypeFactoryMethodOnBeanInstance(false); } @@ -2715,7 +2609,6 @@ private void findTypeOfPrototypeFactoryMethodOnBeanInstance(boolean singleton) { String expectedNameFromProperties = "tony"; String expectedNameFromArgs = "gordon"; - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition instanceFactoryDefinition = new RootBeanDefinition(BeanWithFactoryMethod.class); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", expectedNameFromProperties); @@ -2726,7 +2619,7 @@ private void findTypeOfPrototypeFactoryMethodOnBeanInstance(boolean singleton) { factoryMethodDefinitionWithProperties.setFactoryBeanName("factoryBeanInstance"); factoryMethodDefinitionWithProperties.setFactoryMethodName("create"); if (!singleton) { - factoryMethodDefinitionWithProperties.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + factoryMethodDefinitionWithProperties.setScope(BeanDefinition.SCOPE_PROTOTYPE); } lbf.registerBeanDefinition("fmWithProperties", factoryMethodDefinitionWithProperties); @@ -2734,7 +2627,7 @@ private void findTypeOfPrototypeFactoryMethodOnBeanInstance(boolean singleton) { factoryMethodDefinitionGeneric.setFactoryBeanName("factoryBeanInstance"); factoryMethodDefinitionGeneric.setFactoryMethodName("createGeneric"); if (!singleton) { - factoryMethodDefinitionGeneric.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + factoryMethodDefinitionGeneric.setScope(BeanDefinition.SCOPE_PROTOTYPE); } lbf.registerBeanDefinition("fmGeneric", factoryMethodDefinitionGeneric); @@ -2745,64 +2638,65 @@ private void findTypeOfPrototypeFactoryMethodOnBeanInstance(boolean singleton) { cvals.addGenericArgumentValue(expectedNameFromArgs); factoryMethodDefinitionWithArgs.setConstructorArgumentValues(cvals); if (!singleton) { - factoryMethodDefinitionWithArgs.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + factoryMethodDefinitionWithArgs.setScope(BeanDefinition.SCOPE_PROTOTYPE); } lbf.registerBeanDefinition("fmWithArgs", factoryMethodDefinitionWithArgs); - assertEquals(4, lbf.getBeanDefinitionCount()); + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(4); List tbNames = Arrays.asList(lbf.getBeanNamesForType(TestBean.class)); - assertTrue(tbNames.contains("fmWithProperties")); - assertTrue(tbNames.contains("fmWithArgs")); - assertEquals(2, tbNames.size()); + assertThat(tbNames.contains("fmWithProperties")).isTrue(); + assertThat(tbNames.contains("fmWithArgs")).isTrue(); + assertThat(tbNames.size()).isEqualTo(2); TestBean tb = (TestBean) lbf.getBean("fmWithProperties"); TestBean second = (TestBean) lbf.getBean("fmWithProperties"); if (singleton) { - assertSame(tb, second); + assertThat(second).isSameAs(tb); } else { - assertNotSame(tb, second); + assertThat(second).isNotSameAs(tb); } - assertEquals(expectedNameFromProperties, tb.getName()); + assertThat(tb.getName()).isEqualTo(expectedNameFromProperties); tb = (TestBean) lbf.getBean("fmGeneric"); second = (TestBean) lbf.getBean("fmGeneric"); if (singleton) { - assertSame(tb, second); + assertThat(second).isSameAs(tb); } else { - assertNotSame(tb, second); + assertThat(second).isNotSameAs(tb); } - assertEquals(expectedNameFromProperties, tb.getName()); + assertThat(tb.getName()).isEqualTo(expectedNameFromProperties); TestBean tb2 = (TestBean) lbf.getBean("fmWithArgs"); second = (TestBean) lbf.getBean("fmWithArgs"); if (singleton) { - assertSame(tb2, second); + assertThat(second).isSameAs(tb2); } else { - assertNotSame(tb2, second); + assertThat(second).isNotSameAs(tb2); } - assertEquals(expectedNameFromArgs, tb2.getName()); + assertThat(tb2.getName()).isEqualTo(expectedNameFromArgs); } - @Test(expected = IllegalStateException.class) - public void testScopingBeanToUnregisteredScopeResultsInAnException() { + @Test + void scopingBeanToUnregisteredScopeResultsInAnException() { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class); AbstractBeanDefinition beanDefinition = builder.getBeanDefinition(); beanDefinition.setScope("he put himself so low could hardly look me in the face"); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition("testBean", beanDefinition); - factory.getBean("testBean"); + assertThatIllegalStateException().isThrownBy(() -> + factory.getBean("testBean")); } @Test - public void testExplicitScopeInheritanceForChildBeanDefinitions() { + void explicitScopeInheritanceForChildBeanDefinitions() { String theChildScope = "bonanza!"; RootBeanDefinition parent = new RootBeanDefinition(); - parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + parent.setScope(BeanDefinition.SCOPE_PROTOTYPE); AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition(); child.setBeanClass(TestBean.class); @@ -2813,13 +2707,15 @@ public void testExplicitScopeInheritanceForChildBeanDefinitions() { factory.registerBeanDefinition("child", child); AbstractBeanDefinition def = (AbstractBeanDefinition) factory.getBeanDefinition("child"); - assertEquals("Child 'scope' not overriding parent scope (it must).", theChildScope, def.getScope()); + assertThat(def.getScope()).as("Child 'scope' not overriding parent scope (it must).").isEqualTo(theChildScope); } @Test - public void testScopeInheritanceForChildBeanDefinitions() { + void scopeInheritanceForChildBeanDefinitions() { + String theParentScope = "bonanza!"; + RootBeanDefinition parent = new RootBeanDefinition(); - parent.setScope("bonanza!"); + parent.setScope(theParentScope); AbstractBeanDefinition child = new ChildBeanDefinition("parent"); child.setBeanClass(TestBean.class); @@ -2829,24 +2725,23 @@ public void testScopeInheritanceForChildBeanDefinitions() { factory.registerBeanDefinition("child", child); BeanDefinition def = factory.getMergedBeanDefinition("child"); - assertEquals("Child 'scope' not inherited", "bonanza!", def.getScope()); + assertThat(def.getScope()).as("Child 'scope' not inherited").isEqualTo(theParentScope); } @Test - public void testFieldSettingWithInstantiationAwarePostProcessorNoShortCircuit() { + void fieldSettingWithInstantiationAwarePostProcessorNoShortCircuit() { doTestFieldSettingWithInstantiationAwarePostProcessor(false); } @Test - public void testFieldSettingWithInstantiationAwarePostProcessorWithShortCircuit() { + void fieldSettingWithInstantiationAwarePostProcessorWithShortCircuit() { doTestFieldSettingWithInstantiationAwarePostProcessor(true); } private void doTestFieldSettingWithInstantiationAwarePostProcessor(final boolean skipPropertyPopulation) { - DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); int ageSetByPropertyValue = 27; - bd.getPropertyValues().addPropertyValue(new PropertyValue("age", new Integer(ageSetByPropertyValue))); + bd.getPropertyValues().addPropertyValue(new PropertyValue("age", ageSetByPropertyValue)); lbf.registerBeanDefinition("test", bd); final String nameSetOnField = "nameSetOnField"; lbf.addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() { @@ -2860,68 +2755,58 @@ public boolean postProcessAfterInstantiation(Object bean, String beanName) throw return !skipPropertyPopulation; } catch (Exception ex) { - fail("Unexpected exception: " + ex); - // Keep compiler happy about return - throw new IllegalStateException(); + throw new AssertionError("Unexpected exception", ex); } } }); lbf.preInstantiateSingletons(); TestBean tb = (TestBean) lbf.getBean("test"); - assertEquals("Name was set on field by IAPP", nameSetOnField, tb.getName()); + assertThat(tb.getName()).as("Name was set on field by IAPP").isEqualTo(nameSetOnField); if (!skipPropertyPopulation) { - assertEquals("Property value still set", ageSetByPropertyValue, tb.getAge()); + assertThat(tb.getAge()).as("Property value still set").isEqualTo(ageSetByPropertyValue); } else { - assertEquals("Property value was NOT set and still has default value", 0, tb.getAge()); + assertThat(tb.getAge()).as("Property value was NOT set and still has default value").isEqualTo(0); } } - @SuppressWarnings("unchecked") @Test - public void testInitSecurityAwarePrototypeBean() { - final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + @SuppressWarnings({ "unchecked", "rawtypes" }) + void initSecurityAwarePrototypeBean() { RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class); - bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setInitMethodName("init"); lbf.registerBeanDefinition("test", bd); final Subject subject = new Subject(); subject.getPrincipals().add(new TestPrincipal("user1")); TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject, - new PrivilegedAction() { - @Override - public Object run() { - return lbf.getBean("test"); - } - }, null); - assertNotNull(bean); - assertEquals("user1", bean.getUserName()); + (PrivilegedAction) () -> lbf.getBean("test"), null); + assertThat(bean).isNotNull(); + assertThat(bean.getUserName()).isEqualTo("user1"); } @Test - public void testContainsBeanReturnsTrueEvenForAbstractBeanDefinition() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerBeanDefinition("abs", BeanDefinitionBuilder + void containsBeanReturnsTrueEvenForAbstractBeanDefinition() { + lbf.registerBeanDefinition("abs", BeanDefinitionBuilder .rootBeanDefinition(TestBean.class).setAbstract(true).getBeanDefinition()); - assertThat(bf.containsBean("abs"), equalTo(true)); - assertThat(bf.containsBean("bogus"), equalTo(false)); + assertThat(lbf.containsBean("abs")).isEqualTo(true); + assertThat(lbf.containsBean("bogus")).isEqualTo(false); } @Test - public void resolveEmbeddedValue() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + void resolveEmbeddedValue() { StringValueResolver r1 = mock(StringValueResolver.class); StringValueResolver r2 = mock(StringValueResolver.class); StringValueResolver r3 = mock(StringValueResolver.class); - bf.addEmbeddedValueResolver(r1); - bf.addEmbeddedValueResolver(r2); - bf.addEmbeddedValueResolver(r3); + lbf.addEmbeddedValueResolver(r1); + lbf.addEmbeddedValueResolver(r2); + lbf.addEmbeddedValueResolver(r3); given(r1.resolveStringValue("A")).willReturn("B"); given(r2.resolveStringValue("B")).willReturn(null); given(r3.resolveStringValue(isNull())).willThrow(new IllegalArgumentException()); - bf.resolveEmbeddedValue("A"); + lbf.resolveEmbeddedValue("A"); verify(r1).resolveStringValue("A"); verify(r2).resolveStringValue("B"); @@ -2929,32 +2814,29 @@ public void resolveEmbeddedValue() { } @Test - public void populatedJavaUtilOptionalBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + void populatedJavaUtilOptionalBean() { RootBeanDefinition bd = new RootBeanDefinition(Optional.class); bd.setFactoryMethodName("of"); bd.getConstructorArgumentValues().addGenericArgumentValue("CONTENT"); - bf.registerBeanDefinition("optionalBean", bd); - assertEquals(Optional.of("CONTENT"), bf.getBean(Optional.class)); + lbf.registerBeanDefinition("optionalBean", bd); + assertThat((Optional) lbf.getBean(Optional.class)).isEqualTo(Optional.of("CONTENT")); } @Test - public void emptyJavaUtilOptionalBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + void emptyJavaUtilOptionalBean() { RootBeanDefinition bd = new RootBeanDefinition(Optional.class); bd.setFactoryMethodName("empty"); - bf.registerBeanDefinition("optionalBean", bd); - assertSame(Optional.empty(), bf.getBean(Optional.class)); + lbf.registerBeanDefinition("optionalBean", bd); + assertThat((Optional) lbf.getBean(Optional.class)).isSameAs(Optional.empty()); } @Test - public void testNonPublicEnum() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + void nonPublicEnum() { RootBeanDefinition bd = new RootBeanDefinition(NonPublicEnumHolder.class); bd.getConstructorArgumentValues().addGenericArgumentValue("VALUE_1"); - bf.registerBeanDefinition("holderBean", bd); - NonPublicEnumHolder holder = (NonPublicEnumHolder) bf.getBean("holderBean"); - assertEquals(NonPublicEnum.VALUE_1, holder.getNonPublicEnum()); + lbf.registerBeanDefinition("holderBean", bd); + NonPublicEnumHolder holder = (NonPublicEnumHolder) lbf.getBean("holderBean"); + assertThat(holder.getNonPublicEnum()).isEqualTo(NonPublicEnum.VALUE_1); } /** @@ -2967,44 +2849,43 @@ public void testNonPublicEnum() { * under the 1000 ms timeout, usually ~= 300ms. With caching removed and on the same * hardware the method will take ~13000 ms. See SPR-6870. */ - @Test(timeout = 1000) - public void testByTypeLookupIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - + @Test + @Timeout(1) + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void byTypeLookupIsFastEnough() { for (int i = 0; i < 1000; i++) { - bf.registerBeanDefinition("a" + i, new RootBeanDefinition(A.class)); + lbf.registerBeanDefinition("a" + i, new RootBeanDefinition(A.class)); } - bf.registerBeanDefinition("b", new RootBeanDefinition(B.class)); + lbf.registerBeanDefinition("b", new RootBeanDefinition(B.class)); - bf.freezeConfiguration(); + lbf.freezeConfiguration(); for (int i = 0; i < 10000; i++) { - bf.getBean(B.class); + lbf.getBean(B.class); } } - @Test(timeout = 1000) - public void testRegistrationOfManyBeanDefinitionsIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerBeanDefinition("b", new RootBeanDefinition(B.class)); - // bf.getBean("b"); + @Test + @Timeout(1) + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void registrationOfManyBeanDefinitionsIsFastEnough() { + lbf.registerBeanDefinition("b", new RootBeanDefinition(B.class)); + // lbf.getBean("b"); for (int i = 0; i < 100000; i++) { - bf.registerBeanDefinition("a" + i, new RootBeanDefinition(A.class)); + lbf.registerBeanDefinition("a" + i, new RootBeanDefinition(A.class)); } } - @Test(timeout = 1000) - public void testRegistrationOfManySingletonsIsFastEnough() { - Assume.group(TestGroup.PERFORMANCE); - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerBeanDefinition("b", new RootBeanDefinition(B.class)); - // bf.getBean("b"); + @Test + @Timeout(1) + @EnabledForTestGroups(TestGroup.PERFORMANCE) + void registrationOfManySingletonsIsFastEnough() { + lbf.registerBeanDefinition("b", new RootBeanDefinition(B.class)); + // lbf.getBean("b"); for (int i = 0; i < 100000; i++) { - bf.registerSingleton("a" + i, new A()); + lbf.registerSingleton("a" + i, new A()); } } @@ -3142,6 +3023,7 @@ public static class BeanWithDestroyMethod extends BaseClassWithDestroyMethod { private static int closeCount = 0; + @SuppressWarnings("unused") private BeanWithDestroyMethod inner; public void setInner(BeanWithDestroyMethod inner) { @@ -3356,7 +3238,7 @@ public CustomTypeConverter(NumberFormat numberFormat) { } @Override - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public Object convertIfNecessary(Object value, @Nullable Class requiredType) { if (value instanceof String && Float.class.isAssignableFrom(requiredType)) { try { @@ -3367,7 +3249,7 @@ public Object convertIfNecessary(Object value, @Nullable Class requiredType) { } } else if (value instanceof String && int.class.isAssignableFrom(requiredType)) { - return new Integer(5); + return 5; } else { return value; @@ -3375,57 +3257,25 @@ else if (value instanceof String && int.class.isAssignableFrom(requiredType)) { } @Override - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public Object convertIfNecessary(Object value, @Nullable Class requiredType, @Nullable MethodParameter methodParam) { return convertIfNecessary(value, requiredType); } @Override - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public Object convertIfNecessary(Object value, @Nullable Class requiredType, @Nullable Field field) { return convertIfNecessary(value, requiredType); } } - private static class TestPrincipal implements Principal { - - private String name; - - public TestPrincipal(String name) { - this.name = name; - } - - @Override - public String getName() { - return this.name; - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof TestPrincipal)) { - return false; - } - TestPrincipal p = (TestPrincipal) obj; - return this.name.equals(p.name); - } - - @Override - public int hashCode() { - return this.name.hashCode(); - } - } - - @SuppressWarnings("unused") private static class TestSecuredBean { private String userName; - public void init() { + void init() { AccessControlContext acc = AccessController.getContext(); Subject subject = Subject.getSubject(acc); if (subject == null) { @@ -3504,6 +3354,7 @@ private static class TestBeanRecipient { public TestBean testBean; + @SuppressWarnings("unused") public TestBeanRecipient(TestBean testBean) { this.testBean = testBean; } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanLookupTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanLookupTests.java index e6c9d10d0340..9075bb9b37c4 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanLookupTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanLookupTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,8 +16,8 @@ package org.springframework.beans.factory; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.AbstractFactoryBean; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -25,8 +25,7 @@ import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.ClassPathResource; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Written with the intention of reproducing SPR-7318. @@ -36,7 +35,7 @@ public class FactoryBeanLookupTests { private BeanFactory beanFactory; - @Before + @BeforeEach public void setUp() { beanFactory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory).loadBeanDefinitions( @@ -46,31 +45,31 @@ public void setUp() { @Test public void factoryBeanLookupByNameDereferencing() { Object fooFactory = beanFactory.getBean("&fooFactory"); - assertThat(fooFactory, instanceOf(FooFactoryBean.class)); + assertThat(fooFactory).isInstanceOf(FooFactoryBean.class); } @Test public void factoryBeanLookupByType() { FooFactoryBean fooFactory = beanFactory.getBean(FooFactoryBean.class); - assertNotNull(fooFactory); + assertThat(fooFactory).isNotNull(); } @Test public void factoryBeanLookupByTypeAndNameDereference() { FooFactoryBean fooFactory = beanFactory.getBean("&fooFactory", FooFactoryBean.class); - assertNotNull(fooFactory); + assertThat(fooFactory).isNotNull(); } @Test public void factoryBeanObjectLookupByName() { Object fooFactory = beanFactory.getBean("fooFactory"); - assertThat(fooFactory, instanceOf(Foo.class)); + assertThat(fooFactory).isInstanceOf(Foo.class); } @Test public void factoryBeanObjectLookupByNameAndType() { Foo foo = beanFactory.getBean("fooFactory", Foo.class); - assertNotNull(foo); + assertThat(foo).isNotNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanTests.java index 2c2754a91100..1e466c6da352 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,18 +20,18 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.Resource; -import org.springframework.stereotype.Component; +import org.springframework.core.testfixture.stereotype.Component; import org.springframework.util.Assert; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rob Harrop @@ -52,7 +52,7 @@ public void testFactoryBeanReturnsNull() throws Exception { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(factory).loadBeanDefinitions(RETURNS_NULL_CONTEXT); - assertEquals("null", factory.getBean("factoryBean").toString()); + assertThat(factory.getBean("factoryBean").toString()).isEqualTo("null"); } @Test @@ -63,17 +63,17 @@ public void testFactoryBeansWithAutowiring() throws Exception { BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer"); ppc.postProcessBeanFactory(factory); - assertNull(factory.getType("betaFactory")); + assertThat(factory.getType("betaFactory")).isNull(); Alpha alpha = (Alpha) factory.getBean("alpha"); Beta beta = (Beta) factory.getBean("beta"); Gamma gamma = (Gamma) factory.getBean("gamma"); Gamma gamma2 = (Gamma) factory.getBean("gammaFactory"); - assertSame(beta, alpha.getBeta()); - assertSame(gamma, beta.getGamma()); - assertSame(gamma2, beta.getGamma()); - assertEquals("yourName", beta.getName()); + assertThat(alpha.getBeta()).isSameAs(beta); + assertThat(beta.getGamma()).isSameAs(gamma); + assertThat(beta.getGamma()).isSameAs(gamma2); + assertThat(beta.getName()).isEqualTo("yourName"); } @Test @@ -87,8 +87,8 @@ public void testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure() throw Beta beta = (Beta) factory.getBean("beta"); Alpha alpha = (Alpha) factory.getBean("alpha"); Gamma gamma = (Gamma) factory.getBean("gamma"); - assertSame(beta, alpha.getBeta()); - assertSame(gamma, beta.getGamma()); + assertThat(alpha.getBeta()).isSameAs(beta); + assertThat(beta.getGamma()).isSameAs(gamma); } @Test @@ -114,12 +114,12 @@ public void testCircularReferenceWithPostProcessor() { factory.addBeanPostProcessor(counter); BeanImpl1 impl1 = factory.getBean(BeanImpl1.class); - assertNotNull(impl1); - assertNotNull(impl1.getImpl2()); - assertNotNull(impl1.getImpl2()); - assertSame(impl1, impl1.getImpl2().getImpl1()); - assertEquals(1, counter.getCount("bean1")); - assertEquals(1, counter.getCount("bean2")); + assertThat(impl1).isNotNull(); + assertThat(impl1.getImpl2()).isNotNull(); + assertThat(impl1.getImpl2()).isNotNull(); + assertThat(impl1.getImpl2().getImpl1()).isSameAs(impl1); + assertThat(counter.getCount("bean1")).isEqualTo(1); + assertThat(counter.getCount("bean2")).isEqualTo(1); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/Spr5475Tests.java b/spring-beans/src/test/java/org/springframework/beans/factory/Spr5475Tests.java index 32fa68476017..846f6f6696c7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/Spr5475Tests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/Spr5475Tests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -16,22 +16,22 @@ package org.springframework.beans.factory; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; /** * SPR-5475 exposed the fact that the error message displayed when incorrectly * invoking a factory method is not instructive to the user and rather misleading. * * @author Chris Beams + * @author Juergen Hoeller */ public class Spr5475Tests { @@ -41,7 +41,8 @@ public void noArgFactoryMethodInvokedWithOneArg() { rootBeanDefinition(Foo.class) .setFactoryMethod("noArgFactory") .addConstructorArgValue("bogusArg").getBeanDefinition(), - "Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(String)'. " + + "Error creating bean with name 'foo': No matching factory method found on class " + + "[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(String)'. " + "Check that a method with the specified name and arguments exists and that it is static."); } @@ -52,7 +53,8 @@ public void noArgFactoryMethodInvokedWithTwoArgs() { .setFactoryMethod("noArgFactory") .addConstructorArgValue("bogusArg1") .addConstructorArgValue("bogusArg2".getBytes()).getBeanDefinition(), - "Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(String,byte[])'. " + + "Error creating bean with name 'foo': No matching factory method found on class " + + "[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(String,byte[])'. " + "Check that a method with the specified name and arguments exists and that it is static."); } @@ -66,21 +68,17 @@ public void noArgFactoryMethodInvokedWithTwoArgsAndTypesSpecified() { def.setConstructorArgumentValues(cav); assertExceptionMessageForMisconfiguredFactoryMethod(def, - "Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(CharSequence,byte[])'. " + + "Error creating bean with name 'foo': No matching factory method found on class " + + "[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(CharSequence,byte[])'. " + "Check that a method with the specified name and arguments exists and that it is static."); } private void assertExceptionMessageForMisconfiguredFactoryMethod(BeanDefinition bd, String expectedMessage) { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition("foo", bd); - - try { - factory.preInstantiateSingletons(); - fail("should have failed with BeanCreationException due to incorrectly invoked factory method"); - } - catch (BeanCreationException ex) { - assertThat(ex.getMessage(), equalTo(expectedMessage)); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + factory::preInstantiateSingletons) + .withMessageContaining(expectedMessage); } @Test diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AnnotationBeanWiringInfoResolverTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AnnotationBeanWiringInfoResolverTests.java index 7b0d3cbcbba7..4851c1387fd7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AnnotationBeanWiringInfoResolverTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AnnotationBeanWiringInfoResolverTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,11 +16,12 @@ package org.springframework.beans.factory.annotation; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.wiring.BeanWiringInfo; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Rick Evans @@ -30,44 +31,40 @@ public class AnnotationBeanWiringInfoResolverTests { @Test public void testResolveWiringInfo() throws Exception { - try { - new AnnotationBeanWiringInfoResolver().resolveWiringInfo(null); - fail("Must have thrown an IllegalArgumentException by this point (null argument)"); - } - catch (IllegalArgumentException expected) { - } + assertThatIllegalArgumentException().isThrownBy(() -> + new AnnotationBeanWiringInfoResolver().resolveWiringInfo(null)); } @Test public void testResolveWiringInfoWithAnInstanceOfANonAnnotatedClass() { AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver(); BeanWiringInfo info = resolver.resolveWiringInfo("java.lang.String is not @Configurable"); - assertNull("Must be returning null for a non-@Configurable class instance", info); + assertThat(info).as("Must be returning null for a non-@Configurable class instance").isNull(); } @Test public void testResolveWiringInfoWithAnInstanceOfAnAnnotatedClass() { AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver(); BeanWiringInfo info = resolver.resolveWiringInfo(new Soap()); - assertNotNull("Must *not* be returning null for a non-@Configurable class instance", info); + assertThat(info).as("Must *not* be returning null for a non-@Configurable class instance").isNotNull(); } @Test public void testResolveWiringInfoWithAnInstanceOfAnAnnotatedClassWithAutowiringTurnedOffExplicitly() { AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver(); BeanWiringInfo info = resolver.resolveWiringInfo(new WirelessSoap()); - assertNotNull("Must *not* be returning null for an @Configurable class instance even when autowiring is NO", info); - assertFalse(info.indicatesAutowiring()); - assertEquals(WirelessSoap.class.getName(), info.getBeanName()); + assertThat(info).as("Must *not* be returning null for an @Configurable class instance even when autowiring is NO").isNotNull(); + assertThat(info.indicatesAutowiring()).isFalse(); + assertThat(info.getBeanName()).isEqualTo(WirelessSoap.class.getName()); } @Test public void testResolveWiringInfoWithAnInstanceOfAnAnnotatedClassWithAutowiringTurnedOffExplicitlyAndCustomBeanName() { AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver(); BeanWiringInfo info = resolver.resolveWiringInfo(new NamedWirelessSoap()); - assertNotNull("Must *not* be returning null for an @Configurable class instance even when autowiring is NO", info); - assertFalse(info.indicatesAutowiring()); - assertEquals("DerBigStick", info.getBeanName()); + assertThat(info).as("Must *not* be returning null for an @Configurable class instance even when autowiring is NO").isNotNull(); + assertThat(info.indicatesAutowiring()).isFalse(); + assertThat(info.getBeanName()).isEqualTo("DerBigStick"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java index 6c87289928f1..1e6b7cdd5ece 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -36,12 +36,14 @@ import java.util.Properties; import java.util.Set; import java.util.concurrent.Callable; +import java.util.function.Consumer; +import java.util.function.Function; import java.util.stream.Collectors; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.BeanCreationException; @@ -49,6 +51,7 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InjectionPoint; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.ObjectFactory; @@ -61,18 +64,19 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.IndexedTestBean; +import org.springframework.beans.testfixture.beans.NestedTestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.Ordered; import org.springframework.core.ResolvableType; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.Order; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.IndexedTestBean; -import org.springframework.tests.sample.beans.NestedTestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; import org.springframework.util.ReflectionUtils; -import org.springframework.util.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Juergen Hoeller @@ -88,7 +92,7 @@ public class AutowiredAnnotationBeanPostProcessorTests { private AutowiredAnnotationBeanPostProcessor bpp; - @Before + @BeforeEach public void setup() { bf = new DefaultListableBeanFactory(); bf.registerResolvableDependency(BeanFactory.class, bf); @@ -99,7 +103,7 @@ public void setup() { bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); } - @After + @AfterEach public void close() { bf.destroySingletons(); } @@ -108,36 +112,32 @@ public void close() { @Test public void testIncompleteBeanDefinition() { bf.registerBeanDefinition("testBean", new GenericBeanDefinition()); - try { - bf.getBean("testBean"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getRootCause() instanceof IllegalStateException); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + bf.getBean("testBean")) + .withRootCauseInstanceOf(IllegalStateException.class); } @Test public void testResourceInjection() { RootBeanDefinition bd = new RootBeanDefinition(ResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); } @Test public void testExtendedResourceInjection() { RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -145,25 +145,25 @@ public void testExtendedResourceInjection() { bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); String[] depBeans = bf.getDependenciesForBean("annotatedBean"); - assertEquals(2, depBeans.length); - assertEquals("testBean", depBeans[0]); - assertEquals("nestedTestBean", depBeans[1]); + assertThat(depBeans.length).isEqualTo(2); + assertThat(depBeans[0]).isEqualTo("testBean"); + assertThat(depBeans[1]).isEqualTo("nestedTestBean"); } @Test @@ -175,19 +175,19 @@ public void testExtendedResourceInjectionWithDestruction() { TestBean tb = bf.getBean("testBean", TestBean.class); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); - - assertArrayEquals(new String[] {"testBean", "nestedTestBean"}, bf.getDependenciesForBean("annotatedBean")); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); + + assertThat(bf.getDependenciesForBean("annotatedBean")).isEqualTo(new String[] {"testBean", "nestedTestBean"}); bf.destroySingleton("testBean"); - assertFalse(bf.containsSingleton("testBean")); - assertFalse(bf.containsSingleton("annotatedBean")); - assertTrue(bean.destroyed); - assertSame(0, bf.getDependenciesForBean("annotatedBean").length); + assertThat(bf.containsSingleton("testBean")).isFalse(); + assertThat(bf.containsSingleton("annotatedBean")).isFalse(); + assertThat(bean.destroyed).isTrue(); + assertThat(bf.getDependenciesForBean("annotatedBean").length).isSameAs(0); } @Test @@ -202,12 +202,12 @@ public void testExtendedResourceInjectionWithOverriding() { bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb2, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb2); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test @@ -220,14 +220,14 @@ public void testExtendedResourceInjectionWithSkippedOverriddenMethods() { bf.registerSingleton("nestedTestBean", ntb); OverriddenExtendedResourceInjectionBean bean = (OverriddenExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertNull(bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertNull(bean.getBeanFactory()); - assertTrue(bean.baseInjected); - assertTrue(bean.subInjected); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isNull(); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isNull(); + assertThat(bean.baseInjected).isTrue(); + assertThat(bean.subInjected).isTrue(); } @Test @@ -240,21 +240,22 @@ public void testExtendedResourceInjectionWithDefaultMethod() { bf.registerSingleton("nestedTestBean", ntb); DefaultMethodResourceInjectionBean bean = (DefaultMethodResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertNull(bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertNull(bean.getBeanFactory()); - assertTrue(bean.baseInjected); - assertTrue(bean.subInjected); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isNull(); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isNull(); + assertThat(bean.baseInjected).isTrue(); + assertThat(bean.subInjected).isTrue(); } @Test + @SuppressWarnings("deprecation") public void testExtendedResourceInjectionWithAtRequired() { bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -262,12 +263,12 @@ public void testExtendedResourceInjectionWithAtRequired() { bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test @@ -283,23 +284,138 @@ public void testOptionalResourceInjection() { bf.registerSingleton("nestedTestBean2", ntb2); OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(itb, bean.getIndexedTestBean()); - assertEquals(2, bean.getNestedTestBeans().length); - assertSame(ntb1, bean.getNestedTestBeans()[0]); - assertSame(ntb2, bean.getNestedTestBeans()[1]); - assertEquals(2, bean.nestedTestBeansField.length); - assertSame(ntb1, bean.nestedTestBeansField[0]); - assertSame(ntb2, bean.nestedTestBeansField[1]); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2); + } + + @Test + public void testOptionalResourceInjectionWithSingletonRemoval() { + RootBeanDefinition rbd = new RootBeanDefinition(OptionalResourceInjectionBean.class); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); + bf.registerBeanDefinition("annotatedBean", rbd); + TestBean tb = new TestBean(); + bf.registerSingleton("testBean", tb); + IndexedTestBean itb = new IndexedTestBean(); + bf.registerSingleton("indexedTestBean", itb); + NestedTestBean ntb1 = new NestedTestBean(); + bf.registerSingleton("nestedTestBean1", ntb1); + NestedTestBean ntb2 = new NestedTestBean(); + bf.registerSingleton("nestedTestBean2", ntb2); + + OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2); + + bf.destroySingleton("testBean"); + + bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isNull(); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2); + + bf.registerSingleton("testBean", tb); + + bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2); + } + + @Test + public void testOptionalResourceInjectionWithBeanDefinitionRemoval() { + RootBeanDefinition rbd = new RootBeanDefinition(OptionalResourceInjectionBean.class); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); + bf.registerBeanDefinition("annotatedBean", rbd); + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); + IndexedTestBean itb = new IndexedTestBean(); + bf.registerSingleton("indexedTestBean", itb); + NestedTestBean ntb1 = new NestedTestBean(); + bf.registerSingleton("nestedTestBean1", ntb1); + NestedTestBean ntb2 = new NestedTestBean(); + bf.registerSingleton("nestedTestBean2", ntb2); + + OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getTestBean2()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getTestBean3()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getTestBean4()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2); + + bf.removeBeanDefinition("testBean"); + + bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isNull(); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2); + + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); + + bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getTestBean2()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getTestBean3()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getTestBean4()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2); } @Test public void testOptionalCollectionResourceInjection() { RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -313,26 +429,26 @@ public void testOptionalCollectionResourceInjection() { // Two calls to verify that caching doesn't break re-creation. OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(itb, bean.getIndexedTestBean()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb1, bean.getNestedTestBeans().get(0)); - assertSame(ntb2, bean.getNestedTestBeans().get(1)); - assertEquals(2, bean.nestedTestBeansSetter.size()); - assertSame(ntb1, bean.nestedTestBeansSetter.get(0)); - assertSame(ntb2, bean.nestedTestBeansSetter.get(1)); - assertEquals(2, bean.nestedTestBeansField.size()); - assertSame(ntb1, bean.nestedTestBeansField.get(0)); - assertSame(ntb2, bean.nestedTestBeansField.get(1)); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb2); + assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(2); + assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansSetter.get(1)).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.size()).isEqualTo(2); + assertThat(bean.nestedTestBeansField.get(0)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField.get(1)).isSameAs(ntb2); } @Test public void testOptionalCollectionResourceInjectionWithSingleElement() { RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -344,17 +460,17 @@ public void testOptionalCollectionResourceInjectionWithSingleElement() { // Two calls to verify that caching doesn't break re-creation. OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(itb, bean.getIndexedTestBean()); - assertEquals(1, bean.getNestedTestBeans().size()); - assertSame(ntb1, bean.getNestedTestBeans().get(0)); - assertEquals(1, bean.nestedTestBeansSetter.size()); - assertSame(ntb1, bean.nestedTestBeansSetter.get(0)); - assertEquals(1, bean.nestedTestBeansField.size()); - assertSame(ntb1, bean.nestedTestBeansField.get(0)); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(1); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(1); + assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField.size()).isEqualTo(1); + assertThat(bean.nestedTestBeansField.get(0)).isSameAs(ntb1); } @Test @@ -364,11 +480,11 @@ public void testOptionalResourceInjectionWithIncompleteDependencies() { bf.registerSingleton("testBean", tb); OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertNull(bean.getTestBean4()); - assertNull(bean.getNestedTestBeans()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isNull(); + assertThat(bean.getNestedTestBeans()).isNull(); } @Test @@ -376,11 +492,11 @@ public void testOptionalResourceInjectionWithNoDependencies() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class)); OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean()); - assertNull(bean.getTestBean2()); - assertNull(bean.getTestBean3()); - assertNull(bean.getTestBean4()); - assertNull(bean.getNestedTestBeans()); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isNull(); + assertThat(bean.getNestedTestBeans()).isNull(); } @Test @@ -398,17 +514,17 @@ public void testOrderedResourceInjection() { bf.registerSingleton("nestedTestBean2", ntb2); OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(itb, bean.getIndexedTestBean()); - assertEquals(2, bean.getNestedTestBeans().length); - assertSame(ntb2, bean.getNestedTestBeans()[0]); - assertSame(ntb1, bean.getNestedTestBeans()[1]); - assertEquals(2, bean.nestedTestBeansField.length); - assertSame(ntb2, bean.nestedTestBeansField[0]); - assertSame(ntb1, bean.nestedTestBeansField[1]); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb1); } @Test @@ -424,23 +540,23 @@ public void testAnnotationOrderedResourceInjection() { bf.registerSingleton("nestedTestBean2", ntb2); OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(itb, bean.getIndexedTestBean()); - assertEquals(2, bean.getNestedTestBeans().length); - assertSame(ntb2, bean.getNestedTestBeans()[0]); - assertSame(ntb1, bean.getNestedTestBeans()[1]); - assertEquals(2, bean.nestedTestBeansField.length); - assertSame(ntb2, bean.nestedTestBeansField[0]); - assertSame(ntb1, bean.nestedTestBeansField[1]); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField.length).isEqualTo(2); + assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb1); } @Test public void testOrderedCollectionResourceInjection() { RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -456,26 +572,26 @@ public void testOrderedCollectionResourceInjection() { // Two calls to verify that caching doesn't break re-creation. OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(itb, bean.getIndexedTestBean()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb2, bean.getNestedTestBeans().get(0)); - assertSame(ntb1, bean.getNestedTestBeans().get(1)); - assertEquals(2, bean.nestedTestBeansSetter.size()); - assertSame(ntb2, bean.nestedTestBeansSetter.get(0)); - assertSame(ntb1, bean.nestedTestBeansSetter.get(1)); - assertEquals(2, bean.nestedTestBeansField.size()); - assertSame(ntb2, bean.nestedTestBeansField.get(0)); - assertSame(ntb1, bean.nestedTestBeansField.get(1)); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(2); + assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb2); + assertThat(bean.nestedTestBeansSetter.get(1)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField.size()).isEqualTo(2); + assertThat(bean.nestedTestBeansField.get(0)).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.get(1)).isSameAs(ntb1); } @Test public void testAnnotationOrderedCollectionResourceInjection() { RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); - rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -489,26 +605,26 @@ public void testAnnotationOrderedCollectionResourceInjection() { // Two calls to verify that caching doesn't break re-creation. OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(itb, bean.getIndexedTestBean()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb2, bean.getNestedTestBeans().get(0)); - assertSame(ntb1, bean.getNestedTestBeans().get(1)); - assertEquals(2, bean.nestedTestBeansSetter.size()); - assertSame(ntb2, bean.nestedTestBeansSetter.get(0)); - assertSame(ntb1, bean.nestedTestBeansSetter.get(1)); - assertEquals(2, bean.nestedTestBeansField.size()); - assertSame(ntb2, bean.nestedTestBeansField.get(0)); - assertSame(ntb1, bean.nestedTestBeansField.get(1)); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getIndexedTestBean()).isSameAs(itb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(2); + assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb2); + assertThat(bean.nestedTestBeansSetter.get(1)).isSameAs(ntb1); + assertThat(bean.nestedTestBeansField.size()).isEqualTo(2); + assertThat(bean.nestedTestBeansField.get(0)).isSameAs(ntb2); + assertThat(bean.nestedTestBeansField.get(1)).isSameAs(ntb1); } @Test public void testConstructorResourceInjection() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -516,26 +632,103 @@ public void testConstructorResourceInjection() { bf.registerSingleton("nestedTestBean", ntb); ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); + + bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); + } + + @Test + public void testConstructorResourceInjectionWithSingletonRemoval() { + RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); + bf.registerBeanDefinition("annotatedBean", bd); + TestBean tb = new TestBean(); + bf.registerSingleton("testBean", tb); + NestedTestBean ntb = new NestedTestBean(); + bf.registerSingleton("nestedTestBean", ntb); + + ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); + + bf.destroySingleton("nestedTestBean"); + + bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isNull(); + assertThat(bean.getBeanFactory()).isSameAs(bf); + + bf.registerSingleton("nestedTestBean", ntb); + + bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); + } + + @Test + public void testConstructorResourceInjectionWithBeanDefinitionRemoval() { + RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); + bf.registerBeanDefinition("annotatedBean", bd); + TestBean tb = new TestBean(); + bf.registerSingleton("testBean", tb); + bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NestedTestBean.class)); + + ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(bf.getBean("nestedTestBean")); + assertThat(bean.getBeanFactory()).isSameAs(bf); + + bf.removeBeanDefinition("nestedTestBean"); bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isNull(); + assertThat(bean.getBeanFactory()).isSameAs(bf); + + bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NestedTestBean.class)); + + bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(bf.getBean("nestedTestBean")); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test public void testConstructorResourceInjectionWithNullFromFactoryBean() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -543,26 +736,26 @@ public void testConstructorResourceInjectionWithNullFromFactoryBean() { bf.registerSingleton("nestedTestBean2", new NestedTestBean()); ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertNull(bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isNull(); + assertThat(bean.getBeanFactory()).isSameAs(bf); bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertNull(bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isNull(); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test public void testConstructorResourceInjectionWithNullFromFactoryMethod() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition tb = new RootBeanDefinition(NullFactoryMethods.class); tb.setFactoryMethodName("createTestBean"); @@ -573,20 +766,20 @@ public void testConstructorResourceInjectionWithNullFromFactoryMethod() { bf.registerSingleton("nestedTestBean2", new NestedTestBean()); ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean()); - assertNull(bean.getTestBean2()); - assertNull(bean.getTestBean3()); - assertNull(bean.getTestBean4()); - assertNull(bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isNull(); + assertThat(bean.getNestedTestBean()).isNull(); + assertThat(bean.getBeanFactory()).isSameAs(bf); bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean()); - assertNull(bean.getTestBean2()); - assertNull(bean.getTestBean3()); - assertNull(bean.getTestBean4()); - assertNull(bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isNull(); + assertThat(bean.getNestedTestBean()).isNull(); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test @@ -600,25 +793,19 @@ public void testConstructorResourceInjectionWithMultipleCandidates() { bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertEquals(2, bean.getNestedTestBeans().length); - assertSame(ntb1, bean.getNestedTestBeans()[0]); - assertSame(ntb2, bean.getNestedTestBeans()[1]); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2); } @Test public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class)); - - try { - bf.getBean("annotatedBean"); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(ConstructorWithoutFallbackBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("annotatedBean")) + .satisfies(methodParameterDeclaredOn(ConstructorWithoutFallbackBean.class)); } @Test @@ -632,14 +819,14 @@ public void testConstructorResourceInjectionWithCollectionAndNullFromFactoryBean bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertEquals(1, bean.getNestedTestBeans().size()); - assertSame(ntb2, bean.getNestedTestBeans().get(0)); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(1); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); Map map = bf.getBeansOfType(NestedTestBean.class); - assertNull(map.get("nestedTestBean1")); - assertSame(ntb2, map.get("nestedTestBean2")); + assertThat(map.get("nestedTestBean1")).isNull(); + assertThat(map.get("nestedTestBean2")).isSameAs(ntb2); } @Test @@ -654,11 +841,11 @@ public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb1, bean.getNestedTestBeans().get(0)); - assertSame(ntb2, bean.getNestedTestBeans().get(1)); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb2); } @Test @@ -672,11 +859,11 @@ public void testConstructorResourceInjectionWithMultipleOrderedCandidates() { bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertEquals(2, bean.getNestedTestBeans().length); - assertSame(ntb2, bean.getNestedTestBeans()[0]); - assertSame(ntb1, bean.getNestedTestBeans()[1]); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().length).isEqualTo(2); + assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb1); } @Test @@ -690,11 +877,11 @@ public void testConstructorResourceInjectionWithMultipleCandidatesAsOrderedColle bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb2, bean.getNestedTestBeans().get(0)); - assertSame(ntb1, bean.getNestedTestBeans().get(1)); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1); } @Test @@ -708,10 +895,10 @@ public void testSingleConstructorInjectionWithMultipleCandidatesAsRequiredVararg bf.registerSingleton("nestedTestBean2", ntb2); SingleConstructorVarargBean bean = (SingleConstructorVarargBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb2, bean.getNestedTestBeans().get(0)); - assertSame(ntb1, bean.getNestedTestBeans().get(1)); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1); } @Test @@ -721,9 +908,9 @@ public void testSingleConstructorInjectionWithEmptyVararg() { bf.registerSingleton("testBean", tb); SingleConstructorVarargBean bean = (SingleConstructorVarargBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertNotNull(bean.getNestedTestBeans()); - assertTrue(bean.getNestedTestBeans().isEmpty()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getNestedTestBeans()).isNotNull(); + assertThat(bean.getNestedTestBeans().isEmpty()).isTrue(); } @Test @@ -737,10 +924,10 @@ public void testSingleConstructorInjectionWithMultipleCandidatesAsRequiredCollec bf.registerSingleton("nestedTestBean2", ntb2); SingleConstructorRequiredCollectionBean bean = (SingleConstructorRequiredCollectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb2, bean.getNestedTestBeans().get(0)); - assertSame(ntb1, bean.getNestedTestBeans().get(1)); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1); } @Test @@ -750,9 +937,9 @@ public void testSingleConstructorInjectionWithEmptyCollection() { bf.registerSingleton("testBean", tb); SingleConstructorRequiredCollectionBean bean = (SingleConstructorRequiredCollectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertNotNull(bean.getNestedTestBeans()); - assertTrue(bean.getNestedTestBeans().isEmpty()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getNestedTestBeans()).isNotNull(); + assertThat(bean.getNestedTestBeans().isEmpty()).isTrue(); } @Test @@ -766,10 +953,10 @@ public void testSingleConstructorInjectionWithMultipleCandidatesAsOrderedCollect bf.registerSingleton("nestedTestBean2", ntb2); SingleConstructorOptionalCollectionBean bean = (SingleConstructorOptionalCollectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb2, bean.getNestedTestBeans().get(0)); - assertSame(ntb1, bean.getNestedTestBeans().get(1)); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1); } @Test @@ -779,24 +966,25 @@ public void testSingleConstructorInjectionWithEmptyCollectionAsNull() { bf.registerSingleton("testBean", tb); SingleConstructorOptionalCollectionBean bean = (SingleConstructorOptionalCollectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertNull(bean.getNestedTestBeans()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getNestedTestBeans()).isNull(); } - @Test(expected = UnsatisfiedDependencyException.class) + @Test public void testSingleConstructorInjectionWithMissingDependency() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorOptionalCollectionBean.class)); - bf.getBean("annotatedBean"); + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("annotatedBean")); } - @Test(expected = UnsatisfiedDependencyException.class) + @Test public void testSingleConstructorInjectionWithNullDependency() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorOptionalCollectionBean.class)); RootBeanDefinition tb = new RootBeanDefinition(NullFactoryMethods.class); tb.setFactoryMethodName("createTestBean"); bf.registerBeanDefinition("testBean", tb); - - bf.getBean("annotatedBean"); + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("annotatedBean")); } @Test @@ -806,8 +994,8 @@ public void testConstructorResourceInjectionWithMultipleCandidatesAndFallback() bf.registerSingleton("testBean", tb); ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean3()); - assertNull(bean.getTestBean4()); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isNull(); } @Test @@ -815,14 +1003,14 @@ public void testConstructorResourceInjectionWithMultipleCandidatesAndDefaultFall bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class)); ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean3()); - assertNull(bean.getTestBean4()); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isNull(); } @Test public void testConstructorInjectionWithMap() { RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb1 = new TestBean("tb1"); bf.registerSingleton("testBean1", tb1); @@ -831,20 +1019,20 @@ public void testConstructorInjectionWithMap() { bf.registerBeanDefinition("testBean2", tb2); MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertEquals(1, bean.getTestBeanMap().size()); - assertSame(tb1, bean.getTestBeanMap().get("testBean1")); - assertNull(bean.getTestBeanMap().get("testBean2")); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().get("testBean1")).isSameAs(tb1); + assertThat(bean.getTestBeanMap().get("testBean2")).isNull(); bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertEquals(1, bean.getTestBeanMap().size()); - assertSame(tb1, bean.getTestBeanMap().get("testBean1")); - assertNull(bean.getTestBeanMap().get("testBean2")); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().get("testBean1")).isSameAs(tb1); + assertThat(bean.getTestBeanMap().get("testBean2")).isNull(); } @Test public void testFieldInjectionWithMap() { RootBeanDefinition bd = new RootBeanDefinition(MapFieldInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb1 = new TestBean("tb1"); TestBean tb2 = new TestBean("tb2"); @@ -852,39 +1040,39 @@ public void testFieldInjectionWithMap() { bf.registerSingleton("testBean2", tb2); MapFieldInjectionBean bean = (MapFieldInjectionBean) bf.getBean("annotatedBean"); - assertEquals(2, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); - assertTrue(bean.getTestBeanMap().values().contains(tb1)); - assertTrue(bean.getTestBeanMap().values().contains(tb2)); + assertThat(bean.getTestBeanMap().size()).isEqualTo(2); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb1)).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb2)).isTrue(); bean = (MapFieldInjectionBean) bf.getBean("annotatedBean"); - assertEquals(2, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); - assertTrue(bean.getTestBeanMap().values().contains(tb1)); - assertTrue(bean.getTestBeanMap().values().contains(tb2)); + assertThat(bean.getTestBeanMap().size()).isEqualTo(2); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb1)).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb2)).isTrue(); } @Test public void testMethodInjectionWithMap() { RootBeanDefinition bd = new RootBeanDefinition(MapMethodInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); - assertEquals(1, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean")); - assertTrue(bean.getTestBeanMap().values().contains(tb)); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().keySet().contains("testBean")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); + assertThat(bean.getTestBean()).isSameAs(tb); bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); - assertEquals(1, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean")); - assertTrue(bean.getTestBeanMap().values().contains(tb)); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().keySet().contains("testBean")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); + assertThat(bean.getTestBean()).isSameAs(tb); } @Test @@ -892,15 +1080,9 @@ public void testMethodInjectionWithMapAndMultipleMatches() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class)); bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); - - try { - bf.getBean("annotatedBean"); - fail("should have failed, more than one bean of type"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(MapMethodInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).as("should have failed, more than one bean of type").isThrownBy(() -> + bf.getBean("annotatedBean")) + .satisfies(methodParameterDeclaredOn(MapMethodInjectionBean.class)); } @Test @@ -913,10 +1095,10 @@ public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandid MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); TestBean tb = (TestBean) bf.getBean("testBean1"); - assertEquals(1, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().values().contains(tb)); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); + assertThat(bean.getTestBean()).isSameAs(tb); } @Test @@ -924,14 +1106,14 @@ public void testMethodInjectionWithMapAndNoMatches() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class)); MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBeanMap()); - assertNull(bean.getTestBean()); + assertThat(bean.getTestBeanMap()).isNull(); + assertThat(bean.getTestBean()).isNull(); } @Test public void testConstructorInjectionWithTypedMapAsBean() { RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); MyTestBeanMap tbm = new MyTestBeanMap(); tbm.put("testBean1", new TestBean("tb1")); @@ -940,15 +1122,15 @@ public void testConstructorInjectionWithTypedMapAsBean() { bf.registerSingleton("otherMap", new Properties()); MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(tbm, bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(tbm); bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(tbm, bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(tbm); } @Test public void testConstructorInjectionWithPlainMapAsBean() { RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition tbm = new RootBeanDefinition(CollectionFactoryMethods.class); tbm.setUniqueFactoryMethodName("testBeanMap"); @@ -956,15 +1138,15 @@ public void testConstructorInjectionWithPlainMapAsBean() { bf.registerSingleton("otherMap", new HashMap<>()); MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); } @Test public void testConstructorInjectionWithCustomMapAsBean() { RootBeanDefinition bd = new RootBeanDefinition(CustomMapConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition tbm = new RootBeanDefinition(CustomCollectionFactoryMethods.class); tbm.setUniqueFactoryMethodName("testBeanMap"); @@ -973,28 +1155,28 @@ public void testConstructorInjectionWithCustomMapAsBean() { bf.registerSingleton("testBean2", new TestBean()); CustomMapConstructorInjectionBean bean = (CustomMapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); bean = (CustomMapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); } @Test public void testConstructorInjectionWithPlainHashMapAsBean() { RootBeanDefinition bd = new RootBeanDefinition(QualifiedMapConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); bf.registerBeanDefinition("myTestBeanMap", new RootBeanDefinition(HashMap.class)); QualifiedMapConstructorInjectionBean bean = (QualifiedMapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); bean = (QualifiedMapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap()); + assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); } @Test public void testConstructorInjectionWithTypedSetAsBean() { RootBeanDefinition bd = new RootBeanDefinition(SetConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); MyTestBeanSet tbs = new MyTestBeanSet(); tbs.add(new TestBean("tb1")); @@ -1003,15 +1185,15 @@ public void testConstructorInjectionWithTypedSetAsBean() { bf.registerSingleton("otherSet", new HashSet<>()); SetConstructorInjectionBean bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(tbs, bean.getTestBeanSet()); + assertThat(bean.getTestBeanSet()).isSameAs(tbs); bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(tbs, bean.getTestBeanSet()); + assertThat(bean.getTestBeanSet()).isSameAs(tbs); } @Test public void testConstructorInjectionWithPlainSetAsBean() { RootBeanDefinition bd = new RootBeanDefinition(SetConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition tbs = new RootBeanDefinition(CollectionFactoryMethods.class); tbs.setUniqueFactoryMethodName("testBeanSet"); @@ -1019,24 +1201,24 @@ public void testConstructorInjectionWithPlainSetAsBean() { bf.registerSingleton("otherSet", new HashSet<>()); SetConstructorInjectionBean bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet()); + assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet()); + assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); } @Test public void testConstructorInjectionWithCustomSetAsBean() { RootBeanDefinition bd = new RootBeanDefinition(CustomSetConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition tbs = new RootBeanDefinition(CustomCollectionFactoryMethods.class); tbs.setUniqueFactoryMethodName("testBeanSet"); bf.registerBeanDefinition("myTestBeanSet", tbs); CustomSetConstructorInjectionBean bean = (CustomSetConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet()); + assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); bean = (CustomSetConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet()); + assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); } @Test @@ -1044,8 +1226,8 @@ public void testSelfReference() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionBean.class)); SelfInjectionBean bean = (SelfInjectionBean) bf.getBean("annotatedBean"); - assertSame(bean, bean.reference); - assertNull(bean.referenceCollection); + assertThat(bean.reference).isSameAs(bean); + assertThat(bean.referenceCollection).isNull(); } @Test @@ -1055,9 +1237,9 @@ public void testSelfReferenceWithOther() { SelfInjectionBean bean = (SelfInjectionBean) bf.getBean("annotatedBean"); SelfInjectionBean bean2 = (SelfInjectionBean) bf.getBean("annotatedBean2"); - assertSame(bean2, bean.reference); - assertEquals(1, bean.referenceCollection.size()); - assertSame(bean2, bean.referenceCollection.get(0)); + assertThat(bean.reference).isSameAs(bean2); + assertThat(bean.referenceCollection.size()).isEqualTo(1); + assertThat(bean.referenceCollection.get(0)).isSameAs(bean2); } @Test @@ -1065,8 +1247,8 @@ public void testSelfReferenceCollection() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionCollectionBean.class)); SelfInjectionCollectionBean bean = (SelfInjectionCollectionBean) bf.getBean("annotatedBean"); - assertSame(bean, bean.reference); - assertNull(bean.referenceCollection); + assertThat(bean.reference).isSameAs(bean); + assertThat(bean.referenceCollection).isNull(); } @Test @@ -1076,9 +1258,9 @@ public void testSelfReferenceCollectionWithOther() { SelfInjectionCollectionBean bean = (SelfInjectionCollectionBean) bf.getBean("annotatedBean"); SelfInjectionCollectionBean bean2 = (SelfInjectionCollectionBean) bf.getBean("annotatedBean2"); - assertSame(bean2, bean.reference); - assertSame(1, bean2.referenceCollection.size()); - assertSame(bean2, bean.referenceCollection.get(0)); + assertThat(bean.reference).isSameAs(bean2); + assertThat(bean2.referenceCollection.size()).isSameAs(1); + assertThat(bean.referenceCollection.get(0)).isSameAs(bean2); } @Test @@ -1087,7 +1269,7 @@ public void testObjectFactoryFieldInjection() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -1096,7 +1278,7 @@ public void testObjectFactoryConstructorInjection() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ObjectFactoryConstructorInjectionBean bean = (ObjectFactoryConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -1107,10 +1289,10 @@ public void testObjectFactoryInjectionIntoPrototypeBean() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); ObjectFactoryFieldInjectionBean anotherBean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); - assertNotSame(anotherBean, bean); - assertSame(bf.getBean("testBean"), anotherBean.getTestBean()); + assertThat(bean).isNotSameAs(anotherBean); + assertThat(anotherBean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -1122,7 +1304,7 @@ public void testObjectFactoryQualifierInjection() { bf.registerBeanDefinition("dependencyBean2", new RootBeanDefinition(TestBean.class)); ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("dependencyBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("dependencyBean")); } @Test @@ -1134,7 +1316,7 @@ public void testObjectFactoryQualifierProviderInjection() { bf.registerBeanDefinition("dependencyBean2", new RootBeanDefinition(TestBean.class)); ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("dependencyBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("dependencyBean")); } @Test @@ -1144,40 +1326,40 @@ public void testObjectFactorySerialization() throws Exception { bf.setSerializationId("test"); ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = (ObjectFactoryFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test public void testObjectProviderInjectionWithPrototype() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); - tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("testBean", tbd); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); - assertEquals(bf.getBean("testBean"), bean.getTestBean()); - assertEquals(bf.getBean("testBean", "myName"), bean.getTestBean("myName")); - assertEquals(bf.getBean("testBean"), bean.getOptionalTestBean()); - assertEquals(bf.getBean("testBean"), bean.getOptionalTestBeanWithDefault()); - assertEquals(bf.getBean("testBean"), bean.consumeOptionalTestBean()); - assertEquals(bf.getBean("testBean"), bean.getUniqueTestBean()); - assertEquals(bf.getBean("testBean"), bean.getUniqueTestBeanWithDefault()); - assertEquals(bf.getBean("testBean"), bean.consumeUniqueTestBean()); + assertThat(bean.getTestBean()).isEqualTo(bf.getBean("testBean")); + assertThat(bean.getTestBean("myName")).isEqualTo(bf.getBean("testBean", "myName")); + assertThat(bean.getOptionalTestBean()).isEqualTo(bf.getBean("testBean")); + assertThat(bean.getOptionalTestBeanWithDefault()).isEqualTo(bf.getBean("testBean")); + assertThat(bean.consumeOptionalTestBean()).isEqualTo(bf.getBean("testBean")); + assertThat(bean.getUniqueTestBean()).isEqualTo(bf.getBean("testBean")); + assertThat(bean.getUniqueTestBeanWithDefault()).isEqualTo(bf.getBean("testBean")); + assertThat(bean.consumeUniqueTestBean()).isEqualTo(bf.getBean("testBean")); List testBeans = bean.iterateTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); testBeans = bean.forEachTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); testBeans = bean.streamTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); testBeans = bean.sortedTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); } @Test @@ -1186,26 +1368,26 @@ public void testObjectProviderInjectionWithSingletonTarget() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); - assertSame(bf.getBean("testBean"), bean.getOptionalTestBean()); - assertSame(bf.getBean("testBean"), bean.getOptionalTestBeanWithDefault()); - assertEquals(bf.getBean("testBean"), bean.consumeOptionalTestBean()); - assertSame(bf.getBean("testBean"), bean.getUniqueTestBean()); - assertSame(bf.getBean("testBean"), bean.getUniqueTestBeanWithDefault()); - assertEquals(bf.getBean("testBean"), bean.consumeUniqueTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getOptionalTestBean()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getOptionalTestBeanWithDefault()).isSameAs(bf.getBean("testBean")); + assertThat(bean.consumeOptionalTestBean()).isEqualTo(bf.getBean("testBean")); + assertThat(bean.getUniqueTestBean()).isSameAs(bf.getBean("testBean")); + assertThat(bean.getUniqueTestBeanWithDefault()).isSameAs(bf.getBean("testBean")); + assertThat(bean.consumeUniqueTestBean()).isEqualTo(bf.getBean("testBean")); List testBeans = bean.iterateTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); testBeans = bean.forEachTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); testBeans = bean.streamTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); testBeans = bean.sortedTestBeans(); - assertEquals(1, testBeans.size()); - assertTrue(testBeans.contains(bf.getBean("testBean"))); + assertThat(testBeans.size()).isEqualTo(1); + assertThat(testBeans.contains(bf.getBean("testBean"))).isTrue(); } @Test @@ -1213,28 +1395,23 @@ public void testObjectProviderInjectionWithTargetNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); - try { - bean.getTestBean(); - fail("Should have thrown NoSuchBeanDefinitionException"); - } - catch (NoSuchBeanDefinitionException ex) { - // expected - } - assertNull(bean.getOptionalTestBean()); - assertNull(bean.consumeOptionalTestBean()); - assertEquals(new TestBean("default"), bean.getOptionalTestBeanWithDefault()); - assertEquals(new TestBean("default"), bean.getUniqueTestBeanWithDefault()); - assertNull(bean.getUniqueTestBean()); - assertNull(bean.consumeUniqueTestBean()); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy( + bean::getTestBean); + assertThat(bean.getOptionalTestBean()).isNull(); + assertThat(bean.consumeOptionalTestBean()).isNull(); + assertThat(bean.getOptionalTestBeanWithDefault()).isEqualTo(new TestBean("default")); + assertThat(bean.getUniqueTestBeanWithDefault()).isEqualTo(new TestBean("default")); + assertThat(bean.getUniqueTestBean()).isNull(); + assertThat(bean.consumeUniqueTestBean()).isNull(); List testBeans = bean.iterateTestBeans(); - assertTrue(testBeans.isEmpty()); + assertThat(testBeans.isEmpty()).isTrue(); testBeans = bean.forEachTestBeans(); - assertTrue(testBeans.isEmpty()); + assertThat(testBeans.isEmpty()).isTrue(); testBeans = bean.streamTestBeans(); - assertTrue(testBeans.isEmpty()); + assertThat(testBeans.isEmpty()).isTrue(); testBeans = bean.sortedTestBeans(); - assertTrue(testBeans.isEmpty()); + assertThat(testBeans.isEmpty()).isTrue(); } @Test @@ -1244,46 +1421,28 @@ public void testObjectProviderInjectionWithTargetNotUnique() { bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); - try { - bean.getTestBean(); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - try { - bean.getOptionalTestBean(); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - try { - bean.consumeOptionalTestBean(); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - assertNull(bean.getUniqueTestBean()); - assertNull(bean.consumeUniqueTestBean()); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(bean::getTestBean); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(bean::getOptionalTestBean); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(bean::consumeOptionalTestBean); + assertThat(bean.getUniqueTestBean()).isNull(); + assertThat(bean.consumeUniqueTestBean()).isNull(); List testBeans = bean.iterateTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean1"), testBeans.get(0)); - assertSame(bf.getBean("testBean2"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean1")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean2")); testBeans = bean.forEachTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean1"), testBeans.get(0)); - assertSame(bf.getBean("testBean2"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean1")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean2")); testBeans = bean.streamTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean1"), testBeans.get(0)); - assertSame(bf.getBean("testBean2"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean1")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean2")); testBeans = bean.sortedTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean1"), testBeans.get(0)); - assertSame(bf.getBean("testBean2"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean1")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean2")); } @Test @@ -1299,29 +1458,29 @@ public void testObjectProviderInjectionWithTargetPrimary() { bf.registerBeanDefinition("testBean2", tb2); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean1"), bean.getTestBean()); - assertSame(bf.getBean("testBean1"), bean.getOptionalTestBean()); - assertSame(bf.getBean("testBean1"), bean.consumeOptionalTestBean()); - assertSame(bf.getBean("testBean1"), bean.getUniqueTestBean()); - assertSame(bf.getBean("testBean1"), bean.consumeUniqueTestBean()); - assertFalse(bf.containsSingleton("testBean2")); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean1")); + assertThat(bean.getOptionalTestBean()).isSameAs(bf.getBean("testBean1")); + assertThat(bean.consumeOptionalTestBean()).isSameAs(bf.getBean("testBean1")); + assertThat(bean.getUniqueTestBean()).isSameAs(bf.getBean("testBean1")); + assertThat(bean.consumeUniqueTestBean()).isSameAs(bf.getBean("testBean1")); + assertThat(bf.containsSingleton("testBean2")).isFalse(); List testBeans = bean.iterateTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean1"), testBeans.get(0)); - assertSame(bf.getBean("testBean2"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean1")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean2")); testBeans = bean.forEachTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean1"), testBeans.get(0)); - assertSame(bf.getBean("testBean2"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean1")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean2")); testBeans = bean.streamTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean1"), testBeans.get(0)); - assertSame(bf.getBean("testBean2"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean1")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean2")); testBeans = bean.sortedTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean2"), testBeans.get(0)); - assertSame(bf.getBean("testBean1"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean2")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean1")); } @Test @@ -1338,9 +1497,9 @@ public void testObjectProviderInjectionWithUnresolvedOrderedStream() { ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); List testBeans = bean.sortedTestBeans(); - assertEquals(2, testBeans.size()); - assertSame(bf.getBean("testBean2"), testBeans.get(0)); - assertSame(bf.getBean("testBean1"), testBeans.get(1)); + assertThat(testBeans.size()).isEqualTo(2); + assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean2")); + assertThat(testBeans.get(1)).isSameAs(bf.getBean("testBean1")); } @Test @@ -1355,7 +1514,7 @@ public void testCustomAnnotationRequiredFieldResourceInjection() { CustomAnnotationRequiredFieldResourceInjectionBean bean = (CustomAnnotationRequiredFieldResourceInjectionBean) bf.getBean("customBean"); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(tb); } @Test @@ -1365,16 +1524,9 @@ public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDepende bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredFieldResourceInjectionBean.class)); - - try { - bf.getBean("customBean"); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, - ex.getInjectionPoint().getField().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("customBean")) + .satisfies(fieldDeclaredOn(CustomAnnotationRequiredFieldResourceInjectionBean.class)); } @Test @@ -1388,16 +1540,9 @@ public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenMultipleD bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - - try { - bf.getBean("customBean"); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, - ex.getInjectionPoint().getField().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("customBean")) + .satisfies(fieldDeclaredOn(CustomAnnotationRequiredFieldResourceInjectionBean.class)); } @Test @@ -1412,7 +1557,7 @@ public void testCustomAnnotationRequiredMethodResourceInjection() { CustomAnnotationRequiredMethodResourceInjectionBean bean = (CustomAnnotationRequiredMethodResourceInjectionBean) bf.getBean("customBean"); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(tb); } @Test @@ -1422,16 +1567,9 @@ public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenNoDepend bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredMethodResourceInjectionBean.class)); - - try { - bf.getBean("customBean"); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class, - ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("customBean")) + .satisfies(methodParameterDeclaredOn(CustomAnnotationRequiredMethodResourceInjectionBean.class)); } @Test @@ -1445,16 +1583,9 @@ public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenMultiple bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - - try { - bf.getBean("customBean"); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class, - ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("customBean")) + .satisfies(methodParameterDeclaredOn(CustomAnnotationRequiredMethodResourceInjectionBean.class)); } @Test @@ -1469,9 +1600,9 @@ public void testCustomAnnotationOptionalFieldResourceInjection() { CustomAnnotationOptionalFieldResourceInjectionBean bean = (CustomAnnotationOptionalFieldResourceInjectionBean) bf.getBean("customBean"); - assertSame(tb, bean.getTestBean3()); - assertNull(bean.getTestBean()); - assertNull(bean.getTestBean2()); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); } @Test @@ -1484,9 +1615,9 @@ public void testCustomAnnotationOptionalFieldResourceInjectionWhenNoDependencyFo CustomAnnotationOptionalFieldResourceInjectionBean bean = (CustomAnnotationOptionalFieldResourceInjectionBean) bf.getBean("customBean"); - assertNull(bean.getTestBean3()); - assertNull(bean.getTestBean()); - assertNull(bean.getTestBean2()); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); } @Test @@ -1500,16 +1631,9 @@ public void testCustomAnnotationOptionalFieldResourceInjectionWhenMultipleDepend bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - - try { - bf.getBean("customBean"); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(CustomAnnotationOptionalFieldResourceInjectionBean.class, - ex.getInjectionPoint().getField().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("customBean")) + .satisfies(fieldDeclaredOn(CustomAnnotationOptionalFieldResourceInjectionBean.class)); } @Test @@ -1524,9 +1648,9 @@ public void testCustomAnnotationOptionalMethodResourceInjection() { CustomAnnotationOptionalMethodResourceInjectionBean bean = (CustomAnnotationOptionalMethodResourceInjectionBean) bf.getBean("customBean"); - assertSame(tb, bean.getTestBean3()); - assertNull(bean.getTestBean()); - assertNull(bean.getTestBean2()); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); } @Test @@ -1539,9 +1663,9 @@ public void testCustomAnnotationOptionalMethodResourceInjectionWhenNoDependencyF CustomAnnotationOptionalMethodResourceInjectionBean bean = (CustomAnnotationOptionalMethodResourceInjectionBean) bf.getBean("customBean"); - assertNull(bean.getTestBean3()); - assertNull(bean.getTestBean()); - assertNull(bean.getTestBean2()); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean()).isNull(); + assertThat(bean.getTestBean2()).isNull(); } @Test @@ -1555,23 +1679,16 @@ public void testCustomAnnotationOptionalMethodResourceInjectionWhenMultipleDepen bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - - try { - bf.getBean("customBean"); - fail("Should have thrown UnsatisfiedDependencyException"); - } - catch (UnsatisfiedDependencyException ex) { - // expected - assertSame(CustomAnnotationOptionalMethodResourceInjectionBean.class, - ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); - } + assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> + bf.getBean("customBean")) + .satisfies(methodParameterDeclaredOn(CustomAnnotationOptionalMethodResourceInjectionBean.class)); } /** * Verifies that a dependency on a {@link FactoryBean} can be autowired via * {@link Autowired @Autowired}, specifically addressing the JIRA issue * raised in SPR-4040. */ @Test @@ -1582,16 +1699,15 @@ public void testBeanAutowiredWithFactoryBean() { final StringFactoryBean factoryBean = (StringFactoryBean) bf.getBean("&stringFactoryBean"); final FactoryBeanDependentBean bean = (FactoryBeanDependentBean) bf.getBean("factoryBeanDependentBean"); - assertNotNull("The singleton StringFactoryBean should have been registered.", factoryBean); - assertNotNull("The factoryBeanDependentBean should have been registered.", bean); - assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.", - factoryBean, bean.getFactoryBean()); + assertThat(factoryBean).as("The singleton StringFactoryBean should have been registered.").isNotNull(); + assertThat(bean).as("The factoryBeanDependentBean should have been registered.").isNotNull(); + assertThat(bean.getFactoryBean()).as("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.").isEqualTo(factoryBean); } @Test public void testGenericsBasedFieldInjection() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); String sv = "X"; bf.registerSingleton("stringValue", sv); @@ -1603,40 +1719,40 @@ public void testGenericsBasedFieldInjection() { bf.registerSingleton("integerRepo", ir); RepositoryFieldInjectionBean bean = (RepositoryFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(sv, bean.string); - assertSame(iv, bean.integer); - assertSame(1, bean.stringArray.length); - assertSame(1, bean.integerArray.length); - assertSame(sv, bean.stringArray[0]); - assertSame(iv, bean.integerArray[0]); - assertSame(1, bean.stringList.size()); - assertSame(1, bean.integerList.size()); - assertSame(sv, bean.stringList.get(0)); - assertSame(iv, bean.integerList.get(0)); - assertSame(1, bean.stringMap.size()); - assertSame(1, bean.integerMap.size()); - assertSame(sv, bean.stringMap.get("stringValue")); - assertSame(iv, bean.integerMap.get("integerValue")); - assertSame(sr, bean.stringRepository); - assertSame(ir, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ir, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ir, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ir, bean.integerRepositoryMap.get("integerRepo")); + assertThat(bean.string).isSameAs(sv); + assertThat(bean.integer).isSameAs(iv); + assertThat(bean.stringArray.length).isSameAs(1); + assertThat(bean.integerArray.length).isSameAs(1); + assertThat(bean.stringArray[0]).isSameAs(sv); + assertThat(bean.integerArray[0]).isSameAs(iv); + assertThat(bean.stringList.size()).isSameAs(1); + assertThat(bean.integerList.size()).isSameAs(1); + assertThat(bean.stringList.get(0)).isSameAs(sv); + assertThat(bean.integerList.get(0)).isSameAs(iv); + assertThat(bean.stringMap.size()).isSameAs(1); + assertThat(bean.integerMap.size()).isSameAs(1); + assertThat(bean.stringMap.get("stringValue")).isSameAs(sv); + assertThat(bean.integerMap.get("integerValue")).isSameAs(iv); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ir); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ir); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ir); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("integerRepo")).isSameAs(ir); } @Test public void testGenericsBasedFieldInjectionWithSubstitutedVariables() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSubstitutedVariables.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); String sv = "X"; bf.registerSingleton("stringValue", sv); @@ -1648,40 +1764,40 @@ public void testGenericsBasedFieldInjectionWithSubstitutedVariables() { bf.registerSingleton("integerRepo", ir); RepositoryFieldInjectionBeanWithSubstitutedVariables bean = (RepositoryFieldInjectionBeanWithSubstitutedVariables) bf.getBean("annotatedBean"); - assertSame(sv, bean.string); - assertSame(iv, bean.integer); - assertSame(1, bean.stringArray.length); - assertSame(1, bean.integerArray.length); - assertSame(sv, bean.stringArray[0]); - assertSame(iv, bean.integerArray[0]); - assertSame(1, bean.stringList.size()); - assertSame(1, bean.integerList.size()); - assertSame(sv, bean.stringList.get(0)); - assertSame(iv, bean.integerList.get(0)); - assertSame(1, bean.stringMap.size()); - assertSame(1, bean.integerMap.size()); - assertSame(sv, bean.stringMap.get("stringValue")); - assertSame(iv, bean.integerMap.get("integerValue")); - assertSame(sr, bean.stringRepository); - assertSame(ir, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ir, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ir, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ir, bean.integerRepositoryMap.get("integerRepo")); + assertThat(bean.string).isSameAs(sv); + assertThat(bean.integer).isSameAs(iv); + assertThat(bean.stringArray.length).isSameAs(1); + assertThat(bean.integerArray.length).isSameAs(1); + assertThat(bean.stringArray[0]).isSameAs(sv); + assertThat(bean.integerArray[0]).isSameAs(iv); + assertThat(bean.stringList.size()).isSameAs(1); + assertThat(bean.integerList.size()).isSameAs(1); + assertThat(bean.stringList.get(0)).isSameAs(sv); + assertThat(bean.integerList.get(0)).isSameAs(iv); + assertThat(bean.stringMap.size()).isSameAs(1); + assertThat(bean.integerMap.size()).isSameAs(1); + assertThat(bean.stringMap.get("stringValue")).isSameAs(sv); + assertThat(bean.integerMap.get("integerValue")).isSameAs(iv); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ir); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ir); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ir); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("integerRepo")).isSameAs(ir); } @Test public void testGenericsBasedFieldInjectionWithQualifiers() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithQualifiers.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); StringRepository sr = new StringRepository(); bf.registerSingleton("stringRepo", sr); @@ -1689,26 +1805,26 @@ public void testGenericsBasedFieldInjectionWithQualifiers() { bf.registerSingleton("integerRepo", ir); RepositoryFieldInjectionBeanWithQualifiers bean = (RepositoryFieldInjectionBeanWithQualifiers) bf.getBean("annotatedBean"); - assertSame(sr, bean.stringRepository); - assertSame(ir, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ir, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ir, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ir, bean.integerRepositoryMap.get("integerRepo")); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ir); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ir); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ir); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("integerRepo")).isSameAs(ir); } @Test public void testGenericsBasedFieldInjectionWithMocks() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithQualifiers.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition rbd = new RootBeanDefinition(MocksControl.class); @@ -1728,78 +1844,78 @@ public void testGenericsBasedFieldInjectionWithMocks() { RepositoryFieldInjectionBeanWithQualifiers bean = (RepositoryFieldInjectionBeanWithQualifiers) bf.getBean("annotatedBean"); Repository sr = bf.getBean("stringRepo", Repository.class); Repository ir = bf.getBean("integerRepository", Repository.class); - assertSame(sr, bean.stringRepository); - assertSame(ir, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ir, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ir, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ir, bean.integerRepositoryMap.get("integerRepository")); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ir); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ir); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ir); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("integerRepository")).isSameAs(ir); } @Test public void testGenericsBasedFieldInjectionWithSimpleMatch() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); bf.registerSingleton("repo", new StringRepository()); RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean"); Repository repo = bf.getBean("repo", Repository.class); - assertSame(repo, bean.repository); - assertSame(repo, bean.stringRepository); - assertSame(1, bean.repositoryArray.length); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(repo, bean.repositoryArray[0]); - assertSame(repo, bean.stringRepositoryArray[0]); - assertSame(1, bean.repositoryList.size()); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(repo, bean.repositoryList.get(0)); - assertSame(repo, bean.stringRepositoryList.get(0)); - assertSame(1, bean.repositoryMap.size()); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(repo, bean.repositoryMap.get("repo")); - assertSame(repo, bean.stringRepositoryMap.get("repo")); + assertThat(bean.repository).isSameAs(repo); + assertThat(bean.stringRepository).isSameAs(repo); + assertThat(bean.repositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.repositoryArray[0]).isSameAs(repo); + assertThat(bean.stringRepositoryArray[0]).isSameAs(repo); + assertThat(bean.repositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.repositoryList.get(0)).isSameAs(repo); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(repo); + assertThat(bean.repositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.repositoryMap.get("repo")).isSameAs(repo); + assertThat(bean.stringRepositoryMap.get("repo")).isSameAs(repo); - assertArrayEquals(new String[] {"repo"}, bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class))); + assertThat(bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class))).isEqualTo(new String[] {"repo"}); } @Test public void testGenericsBasedFactoryBeanInjectionWithBeanDefinition() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); bf.registerBeanDefinition("repoFactoryBean", new RootBeanDefinition(RepositoryFactoryBean.class)); RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) bf.getBean("annotatedBean"); RepositoryFactoryBean repoFactoryBean = bf.getBean("&repoFactoryBean", RepositoryFactoryBean.class); - assertSame(repoFactoryBean, bean.repositoryFactoryBean); + assertThat(bean.repositoryFactoryBean).isSameAs(repoFactoryBean); } @Test public void testGenericsBasedFactoryBeanInjectionWithSingletonBean() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); bf.registerSingleton("repoFactoryBean", new RepositoryFactoryBean<>()); RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) bf.getBean("annotatedBean"); RepositoryFactoryBean repoFactoryBean = bf.getBean("&repoFactoryBean", RepositoryFactoryBean.class); - assertSame(repoFactoryBean, bean.repositoryFactoryBean); + assertThat(bean.repositoryFactoryBean).isSameAs(repoFactoryBean); } @Test public void testGenericsBasedFieldInjectionWithSimpleMatchAndMock() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition rbd = new RootBeanDefinition(MocksControl.class); @@ -1812,26 +1928,26 @@ public void testGenericsBasedFieldInjectionWithSimpleMatchAndMock() { RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean"); Repository repo = bf.getBean("repo", Repository.class); - assertSame(repo, bean.repository); - assertSame(repo, bean.stringRepository); - assertSame(1, bean.repositoryArray.length); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(repo, bean.repositoryArray[0]); - assertSame(repo, bean.stringRepositoryArray[0]); - assertSame(1, bean.repositoryList.size()); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(repo, bean.repositoryList.get(0)); - assertSame(repo, bean.stringRepositoryList.get(0)); - assertSame(1, bean.repositoryMap.size()); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(repo, bean.repositoryMap.get("repo")); - assertSame(repo, bean.stringRepositoryMap.get("repo")); + assertThat(bean.repository).isSameAs(repo); + assertThat(bean.stringRepository).isSameAs(repo); + assertThat(bean.repositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.repositoryArray[0]).isSameAs(repo); + assertThat(bean.stringRepositoryArray[0]).isSameAs(repo); + assertThat(bean.repositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.repositoryList.get(0)).isSameAs(repo); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(repo); + assertThat(bean.repositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.repositoryMap.get("repo")).isSameAs(repo); + assertThat(bean.stringRepositoryMap.get("repo")).isSameAs(repo); } @Test public void testGenericsBasedFieldInjectionWithSimpleMatchAndMockito() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); RootBeanDefinition rbd = new RootBeanDefinition(); @@ -1843,26 +1959,26 @@ public void testGenericsBasedFieldInjectionWithSimpleMatchAndMockito() { RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean"); Repository repo = bf.getBean("repo", Repository.class); - assertSame(repo, bean.repository); - assertSame(repo, bean.stringRepository); - assertSame(1, bean.repositoryArray.length); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(repo, bean.repositoryArray[0]); - assertSame(repo, bean.stringRepositoryArray[0]); - assertSame(1, bean.repositoryList.size()); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(repo, bean.repositoryList.get(0)); - assertSame(repo, bean.stringRepositoryList.get(0)); - assertSame(1, bean.repositoryMap.size()); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(repo, bean.repositoryMap.get("repo")); - assertSame(repo, bean.stringRepositoryMap.get("repo")); + assertThat(bean.repository).isSameAs(repo); + assertThat(bean.stringRepository).isSameAs(repo); + assertThat(bean.repositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.repositoryArray[0]).isSameAs(repo); + assertThat(bean.stringRepositoryArray[0]).isSameAs(repo); + assertThat(bean.repositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.repositoryList.get(0)).isSameAs(repo); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(repo); + assertThat(bean.repositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.repositoryMap.get("repo")).isSameAs(repo); + assertThat(bean.stringRepositoryMap.get("repo")).isSameAs(repo); } @Test public void testGenericsBasedMethodInjection() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryMethodInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); String sv = "X"; bf.registerSingleton("stringValue", sv); @@ -1874,40 +1990,40 @@ public void testGenericsBasedMethodInjection() { bf.registerSingleton("integerRepo", ir); RepositoryMethodInjectionBean bean = (RepositoryMethodInjectionBean) bf.getBean("annotatedBean"); - assertSame(sv, bean.string); - assertSame(iv, bean.integer); - assertSame(1, bean.stringArray.length); - assertSame(1, bean.integerArray.length); - assertSame(sv, bean.stringArray[0]); - assertSame(iv, bean.integerArray[0]); - assertSame(1, bean.stringList.size()); - assertSame(1, bean.integerList.size()); - assertSame(sv, bean.stringList.get(0)); - assertSame(iv, bean.integerList.get(0)); - assertSame(1, bean.stringMap.size()); - assertSame(1, bean.integerMap.size()); - assertSame(sv, bean.stringMap.get("stringValue")); - assertSame(iv, bean.integerMap.get("integerValue")); - assertSame(sr, bean.stringRepository); - assertSame(ir, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ir, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ir, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ir, bean.integerRepositoryMap.get("integerRepo")); + assertThat(bean.string).isSameAs(sv); + assertThat(bean.integer).isSameAs(iv); + assertThat(bean.stringArray.length).isSameAs(1); + assertThat(bean.integerArray.length).isSameAs(1); + assertThat(bean.stringArray[0]).isSameAs(sv); + assertThat(bean.integerArray[0]).isSameAs(iv); + assertThat(bean.stringList.size()).isSameAs(1); + assertThat(bean.integerList.size()).isSameAs(1); + assertThat(bean.stringList.get(0)).isSameAs(sv); + assertThat(bean.integerList.get(0)).isSameAs(iv); + assertThat(bean.stringMap.size()).isSameAs(1); + assertThat(bean.integerMap.size()).isSameAs(1); + assertThat(bean.stringMap.get("stringValue")).isSameAs(sv); + assertThat(bean.integerMap.get("integerValue")).isSameAs(iv); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ir); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ir); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ir); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("integerRepo")).isSameAs(ir); } @Test public void testGenericsBasedMethodInjectionWithSubstitutedVariables() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryMethodInjectionBeanWithSubstitutedVariables.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); String sv = "X"; bf.registerSingleton("stringValue", sv); @@ -1919,40 +2035,40 @@ public void testGenericsBasedMethodInjectionWithSubstitutedVariables() { bf.registerSingleton("integerRepo", ir); RepositoryMethodInjectionBeanWithSubstitutedVariables bean = (RepositoryMethodInjectionBeanWithSubstitutedVariables) bf.getBean("annotatedBean"); - assertSame(sv, bean.string); - assertSame(iv, bean.integer); - assertSame(1, bean.stringArray.length); - assertSame(1, bean.integerArray.length); - assertSame(sv, bean.stringArray[0]); - assertSame(iv, bean.integerArray[0]); - assertSame(1, bean.stringList.size()); - assertSame(1, bean.integerList.size()); - assertSame(sv, bean.stringList.get(0)); - assertSame(iv, bean.integerList.get(0)); - assertSame(1, bean.stringMap.size()); - assertSame(1, bean.integerMap.size()); - assertSame(sv, bean.stringMap.get("stringValue")); - assertSame(iv, bean.integerMap.get("integerValue")); - assertSame(sr, bean.stringRepository); - assertSame(ir, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ir, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ir, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ir, bean.integerRepositoryMap.get("integerRepo")); + assertThat(bean.string).isSameAs(sv); + assertThat(bean.integer).isSameAs(iv); + assertThat(bean.stringArray.length).isSameAs(1); + assertThat(bean.integerArray.length).isSameAs(1); + assertThat(bean.stringArray[0]).isSameAs(sv); + assertThat(bean.integerArray[0]).isSameAs(iv); + assertThat(bean.stringList.size()).isSameAs(1); + assertThat(bean.integerList.size()).isSameAs(1); + assertThat(bean.stringList.get(0)).isSameAs(sv); + assertThat(bean.integerList.get(0)).isSameAs(iv); + assertThat(bean.stringMap.size()).isSameAs(1); + assertThat(bean.integerMap.size()).isSameAs(1); + assertThat(bean.stringMap.get("stringValue")).isSameAs(sv); + assertThat(bean.integerMap.get("integerValue")).isSameAs(iv); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ir); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ir); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ir); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("integerRepo")).isSameAs(ir); } @Test public void testGenericsBasedConstructorInjection() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); StringRepository sr = new StringRepository(); bf.registerSingleton("stringRepo", sr); @@ -1960,78 +2076,78 @@ public void testGenericsBasedConstructorInjection() { bf.registerSingleton("integerRepo", ir); RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(sr, bean.stringRepository); - assertSame(ir, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ir, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ir, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ir, bean.integerRepositoryMap.get("integerRepo")); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ir); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ir); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ir); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("integerRepo")).isSameAs(ir); } @Test @SuppressWarnings("rawtypes") public void testGenericsBasedConstructorInjectionWithNonTypedTarget() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); GenericRepository gr = new GenericRepository(); bf.registerSingleton("genericRepo", gr); RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(gr, bean.stringRepository); - assertSame(gr, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(gr, bean.stringRepositoryArray[0]); - assertSame(gr, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(gr, bean.stringRepositoryList.get(0)); - assertSame(gr, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(gr, bean.stringRepositoryMap.get("genericRepo")); - assertSame(gr, bean.integerRepositoryMap.get("genericRepo")); + assertThat(bean.stringRepository).isSameAs(gr); + assertThat(bean.integerRepository).isSameAs(gr); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(gr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(gr); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(gr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(gr); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("genericRepo")).isSameAs(gr); + assertThat(bean.integerRepositoryMap.get("genericRepo")).isSameAs(gr); } @Test public void testGenericsBasedConstructorInjectionWithNonGenericTarget() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); SimpleRepository ngr = new SimpleRepository(); bf.registerSingleton("simpleRepo", ngr); RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(ngr, bean.stringRepository); - assertSame(ngr, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(ngr, bean.stringRepositoryArray[0]); - assertSame(ngr, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(ngr, bean.stringRepositoryList.get(0)); - assertSame(ngr, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(ngr, bean.stringRepositoryMap.get("simpleRepo")); - assertSame(ngr, bean.integerRepositoryMap.get("simpleRepo")); + assertThat(bean.stringRepository).isSameAs(ngr); + assertThat(bean.integerRepository).isSameAs(ngr); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(ngr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ngr); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(ngr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ngr); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("simpleRepo")).isSameAs(ngr); + assertThat(bean.integerRepositoryMap.get("simpleRepo")).isSameAs(ngr); } @Test @SuppressWarnings("rawtypes") public void testGenericsBasedConstructorInjectionWithMixedTargets() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); StringRepository sr = new StringRepository(); bf.registerSingleton("stringRepo", sr); @@ -2039,26 +2155,26 @@ public void testGenericsBasedConstructorInjectionWithMixedTargets() { bf.registerSingleton("genericRepo", gr); RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(sr, bean.stringRepository); - assertSame(gr, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(gr, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(gr, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(gr, bean.integerRepositoryMap.get("genericRepo")); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(gr); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(gr); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(gr); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("genericRepo")).isSameAs(gr); } @Test public void testGenericsBasedConstructorInjectionWithMixedTargetsIncludingNonGeneric() { RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); StringRepository sr = new StringRepository(); bf.registerSingleton("stringRepo", sr); @@ -2066,20 +2182,20 @@ public void testGenericsBasedConstructorInjectionWithMixedTargetsIncludingNonGen bf.registerSingleton("simpleRepo", ngr); RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); - assertSame(sr, bean.stringRepository); - assertSame(ngr, bean.integerRepository); - assertSame(1, bean.stringRepositoryArray.length); - assertSame(1, bean.integerRepositoryArray.length); - assertSame(sr, bean.stringRepositoryArray[0]); - assertSame(ngr, bean.integerRepositoryArray[0]); - assertSame(1, bean.stringRepositoryList.size()); - assertSame(1, bean.integerRepositoryList.size()); - assertSame(sr, bean.stringRepositoryList.get(0)); - assertSame(ngr, bean.integerRepositoryList.get(0)); - assertSame(1, bean.stringRepositoryMap.size()); - assertSame(1, bean.integerRepositoryMap.size()); - assertSame(sr, bean.stringRepositoryMap.get("stringRepo")); - assertSame(ngr, bean.integerRepositoryMap.get("simpleRepo")); + assertThat(bean.stringRepository).isSameAs(sr); + assertThat(bean.integerRepository).isSameAs(ngr); + assertThat(bean.stringRepositoryArray.length).isSameAs(1); + assertThat(bean.integerRepositoryArray.length).isSameAs(1); + assertThat(bean.stringRepositoryArray[0]).isSameAs(sr); + assertThat(bean.integerRepositoryArray[0]).isSameAs(ngr); + assertThat(bean.stringRepositoryList.size()).isSameAs(1); + assertThat(bean.integerRepositoryList.size()).isSameAs(1); + assertThat(bean.stringRepositoryList.get(0)).isSameAs(sr); + assertThat(bean.integerRepositoryList.get(0)).isSameAs(ngr); + assertThat(bean.stringRepositoryMap.size()).isSameAs(1); + assertThat(bean.integerRepositoryMap.size()).isSameAs(1); + assertThat(bean.stringRepositoryMap.get("stringRepo")).isSameAs(sr); + assertThat(bean.integerRepositoryMap.get("simpleRepo")).isSameAs(ngr); } @Test @@ -2092,8 +2208,8 @@ public void testGenericsBasedInjectionIntoMatchingTypeVariable() { GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1"); GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2"); - assertSame(bean2, bean1.gi2); - assertEquals(ResolvableType.forClass(GenericInterface1Impl.class), bd.getResolvableType()); + assertThat(bean1.gi2).isSameAs(bean2); + assertThat(bd.getResolvableType()).isEqualTo(ResolvableType.forClass(GenericInterface1Impl.class)); } @Test @@ -2106,8 +2222,8 @@ public void testGenericsBasedInjectionIntoUnresolvedTypeVariable() { GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1"); GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2"); - assertSame(bean2, bean1.gi2); - assertEquals(ResolvableType.forClass(GenericInterface1Impl.class), bd.getResolvableType()); + assertThat(bean1.gi2).isSameAs(bean2); + assertThat(bd.getResolvableType()).isEqualTo(ResolvableType.forClass(GenericInterface1Impl.class)); } @Test @@ -2122,13 +2238,13 @@ public void testGenericsBasedInjectionIntoTypeVariableSelectingBestMatch() { GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1"); GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2"); - assertSame(bean2, bean1.gi2); - assertArrayEquals(new String[] {"bean1"}, bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(GenericInterface1.class, String.class))); - assertArrayEquals(new String[] {"bean2"}, bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(GenericInterface2.class, String.class))); + assertThat(bean1.gi2).isSameAs(bean2); + assertThat(bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(GenericInterface1.class, String.class))).isEqualTo(new String[] {"bean1"}); + assertThat(bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(GenericInterface2.class, String.class))).isEqualTo(new String[] {"bean2"}); } @Test - @Ignore // SPR-11521 + @Disabled // SPR-11521 @SuppressWarnings("rawtypes") public void testGenericsBasedInjectionIntoTypeVariableSelectingBestMatchAgainstFactoryMethodSignature() { RootBeanDefinition bd = new RootBeanDefinition(GenericInterface1Impl.class); @@ -2140,7 +2256,7 @@ public void testGenericsBasedInjectionIntoTypeVariableSelectingBestMatchAgainstF GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1"); GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2"); - assertSame(bean2, bean1.gi2); + assertThat(bean1.gi2).isSameAs(bean2); } @Test @@ -2153,9 +2269,9 @@ public void testGenericsBasedInjectionWithBeanDefinitionTargetResolvableType() { bf.registerBeanDefinition("bean2", bd2); bf.registerBeanDefinition("bean3", new RootBeanDefinition(MultiGenericFieldInjection.class)); - assertEquals("bean1 a bean2 123", bf.getBean("bean3").toString()); - assertEquals(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, String.class), bd1.getResolvableType()); - assertEquals(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, Integer.class), bd2.getResolvableType()); + assertThat(bf.getBean("bean3").toString()).isEqualTo("bean1 a bean2 123"); + assertThat(bd1.getResolvableType()).isEqualTo(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, String.class)); + assertThat(bd2.getResolvableType()).isEqualTo(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, Integer.class)); } @Test @@ -2166,7 +2282,7 @@ public void testCircularTypeReference() { bf.registerBeanDefinition("bean4", new RootBeanDefinition(StockMovementInstructionImpl.class)); StockServiceImpl service = bf.getBean(StockServiceImpl.class); - assertSame(bf.getBean(StockMovementDaoImpl.class), service.stockMovementDao); + assertThat(service.stockMovementDao).isSameAs(bf.getBean(StockMovementDaoImpl.class)); } @Test @@ -2174,7 +2290,7 @@ public void testBridgeMethodHandling() { bf.registerBeanDefinition("bean1", new RootBeanDefinition(MyCallable.class)); bf.registerBeanDefinition("bean2", new RootBeanDefinition(SecondCallable.class)); bf.registerBeanDefinition("bean3", new RootBeanDefinition(FooBar.class)); - assertNotNull(bf.getBean(FooBar.class)); + assertThat(bf.getBean(FooBar.class)).isNotNull(); } @Test @@ -2182,14 +2298,14 @@ public void testSingleConstructorWithProvidedArgument() { RootBeanDefinition bd = new RootBeanDefinition(ProvidedArgumentBean.class); bd.getConstructorArgumentValues().addGenericArgumentValue(Collections.singletonList("value")); bf.registerBeanDefinition("beanWithArgs", bd); - assertNotNull(bf.getBean(ProvidedArgumentBean.class)); + assertThat(bf.getBean(ProvidedArgumentBean.class)).isNotNull(); } @Test public void testAnnotatedDefaultConstructor() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class)); - assertNotNull(bf.getBean("annotatedBean")); + assertThat(bf.getBean("annotatedBean")).isNotNull(); } @Test // SPR-15125 @@ -2197,7 +2313,7 @@ public void testFactoryBeanSelfInjection() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectingFactoryBean.class)); SelfInjectingFactoryBean bean = bf.getBean(SelfInjectingFactoryBean.class); - assertSame(bf.getBean("annotatedBean"), bean.testBean); + assertThat(bean.testBean).isSameAs(bf.getBean("annotatedBean")); } @Test // SPR-15125 @@ -2207,7 +2323,31 @@ public void testFactoryBeanSelfInjectionViaFactoryMethod() { bf.registerBeanDefinition("annotatedBean", bd); SelfInjectingFactoryBean bean = bf.getBean(SelfInjectingFactoryBean.class); - assertSame(bf.getBean("annotatedBean"), bean.testBean); + assertThat(bean.testBean).isSameAs(bf.getBean("annotatedBean")); + } + + private Consumer methodParameterDeclaredOn( + Class expected) { + return declaredOn( + injectionPoint -> injectionPoint.getMethodParameter().getDeclaringClass(), + expected); + } + + private Consumer fieldDeclaredOn( + Class expected) { + return declaredOn( + injectionPoint -> injectionPoint.getField().getDeclaringClass(), + expected); + } + + private Consumer declaredOn( + Function> declaringClassExtractor, + Class expected) { + return ex -> { + InjectionPoint injectionPoint = ex.getInjectionPoint(); + Class declaringClass = declaringClassExtractor.apply(injectionPoint); + assertThat(declaringClass).isSameAs(expected); + }; } @@ -2260,7 +2400,9 @@ public NonPublicResourceInjectionBean() { } @Override - @Autowired @Required + @Autowired + @Required + @SuppressWarnings("deprecation") public void setTestBean2(TestBean testBean2) { super.setTestBean2(testBean2); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests.java index 83dd5af5c5f2..02a87f6c0d4b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,18 +16,16 @@ package org.springframework.beans.factory.annotation; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.support.AutowireCandidateResolver; -import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Unit tests for {@link CustomAutowireConfigurer}. @@ -38,19 +36,18 @@ */ public class CustomAutowireConfigurerTests { - private static final Resource CONTEXT = qualifiedResource(CustomAutowireConfigurerTests.class, "context.xml"); - @Test public void testCustomResolver() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - BeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); - reader.loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + qualifiedResource(CustomAutowireConfigurerTests.class, "context.xml")); + CustomAutowireConfigurer cac = new CustomAutowireConfigurer(); CustomResolver customResolver = new CustomResolver(); bf.setAutowireCandidateResolver(customResolver); cac.postProcessBeanFactory(bf); TestBean testBean = (TestBean) bf.getBean("testBean"); - assertEquals("#1!", testBean.getName()); + assertThat(testBean.getName()).isEqualTo("#1!"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java index 8c2fa870a0e3..43e57e7d26a8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,13 +22,14 @@ import java.util.List; import java.util.Map; import java.util.Optional; + import javax.inject.Inject; import javax.inject.Named; import javax.inject.Provider; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanFactory; @@ -39,13 +40,14 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.IndexedTestBean; -import org.springframework.tests.sample.beans.NestedTestBean; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.util.SerializationTestUtils; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.IndexedTestBean; +import org.springframework.beans.testfixture.beans.NestedTestBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Unit tests for {@link org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor} @@ -61,7 +63,7 @@ public class InjectAnnotationBeanPostProcessorTests { private AutowiredAnnotationBeanPostProcessor bpp; - @Before + @BeforeEach public void setup() { bf = new DefaultListableBeanFactory(); bf.registerResolvableDependency(BeanFactory.class, bf); @@ -71,7 +73,7 @@ public void setup() { bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); } - @After + @AfterEach public void close() { bf.destroySingletons(); } @@ -84,31 +86,32 @@ public void testIncompleteBeanDefinition() { bf.getBean("testBean"); } catch (BeanCreationException ex) { - assertTrue(ex.getRootCause() instanceof IllegalStateException); + boolean condition = ex.getRootCause() instanceof IllegalStateException; + assertThat(condition).isTrue(); } } @Test public void testResourceInjection() { RootBeanDefinition bd = new RootBeanDefinition(ResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); } @Test public void testExtendedResourceInjection() { RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -116,20 +119,20 @@ public void testExtendedResourceInjection() { bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test @@ -144,19 +147,20 @@ public void testExtendedResourceInjectionWithOverriding() { bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb2, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb2); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test + @SuppressWarnings("deprecation") public void testExtendedResourceInjectionWithAtRequired() { bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -164,18 +168,18 @@ public void testExtendedResourceInjectionWithAtRequired() { bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test public void testConstructorResourceInjection() { RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -183,20 +187,20 @@ public void testConstructorResourceInjection() { bf.registerSingleton("nestedTestBean", ntb); ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean()); - assertSame(tb, bean.getTestBean2()); - assertSame(tb, bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertSame(ntb, bean.getNestedTestBean()); - assertSame(bf, bean.getBeanFactory()); + assertThat(bean.getTestBean()).isSameAs(tb); + assertThat(bean.getTestBean2()).isSameAs(tb); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBean()).isSameAs(ntb); + assertThat(bean.getBeanFactory()).isSameAs(bf); } @Test @@ -211,11 +215,11 @@ public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean3()); - assertSame(tb, bean.getTestBean4()); - assertEquals(2, bean.getNestedTestBeans().size()); - assertSame(ntb1, bean.getNestedTestBeans().get(0)); - assertSame(ntb2, bean.getNestedTestBeans().get(1)); + assertThat(bean.getTestBean3()).isNull(); + assertThat(bean.getTestBean4()).isSameAs(tb); + assertThat(bean.getNestedTestBeans().size()).isEqualTo(2); + assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb1); + assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb2); } @Test @@ -225,14 +229,14 @@ public void testConstructorResourceInjectionWithMultipleCandidatesAndFallback() bf.registerSingleton("testBean", tb); ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); - assertSame(tb, bean.getTestBean3()); - assertNull(bean.getTestBean4()); + assertThat(bean.getTestBean3()).isSameAs(tb); + assertThat(bean.getTestBean4()).isNull(); } @Test public void testConstructorInjectionWithMap() { RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb1 = new TestBean(); TestBean tb2 = new TestBean(); @@ -240,24 +244,24 @@ public void testConstructorInjectionWithMap() { bf.registerSingleton("testBean2", tb1); MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertEquals(2, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); - assertTrue(bean.getTestBeanMap().values().contains(tb1)); - assertTrue(bean.getTestBeanMap().values().contains(tb2)); + assertThat(bean.getTestBeanMap().size()).isEqualTo(2); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb1)).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb2)).isTrue(); bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); - assertEquals(2, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); - assertTrue(bean.getTestBeanMap().values().contains(tb1)); - assertTrue(bean.getTestBeanMap().values().contains(tb2)); + assertThat(bean.getTestBeanMap().size()).isEqualTo(2); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb1)).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb2)).isTrue(); } @Test public void testFieldInjectionWithMap() { RootBeanDefinition bd = new RootBeanDefinition(MapFieldInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb1 = new TestBean(); TestBean tb2 = new TestBean(); @@ -265,39 +269,39 @@ public void testFieldInjectionWithMap() { bf.registerSingleton("testBean2", tb1); MapFieldInjectionBean bean = (MapFieldInjectionBean) bf.getBean("annotatedBean"); - assertEquals(2, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); - assertTrue(bean.getTestBeanMap().values().contains(tb1)); - assertTrue(bean.getTestBeanMap().values().contains(tb2)); + assertThat(bean.getTestBeanMap().size()).isEqualTo(2); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb1)).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb2)).isTrue(); bean = (MapFieldInjectionBean) bf.getBean("annotatedBean"); - assertEquals(2, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); - assertTrue(bean.getTestBeanMap().values().contains(tb1)); - assertTrue(bean.getTestBeanMap().values().contains(tb2)); + assertThat(bean.getTestBeanMap().size()).isEqualTo(2); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb1)).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb2)).isTrue(); } @Test public void testMethodInjectionWithMap() { RootBeanDefinition bd = new RootBeanDefinition(MapMethodInjectionBean.class); - bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); - assertEquals(1, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean")); - assertTrue(bean.getTestBeanMap().values().contains(tb)); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().keySet().contains("testBean")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); + assertThat(bean.getTestBean()).isSameAs(tb); bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); - assertEquals(1, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean")); - assertTrue(bean.getTestBeanMap().values().contains(tb)); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().keySet().contains("testBean")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); + assertThat(bean.getTestBean()).isSameAs(tb); } @Test @@ -305,14 +309,8 @@ public void testMethodInjectionWithMapAndMultipleMatches() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class)); bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); - - try { - bf.getBean("annotatedBean"); - fail("should have failed, more than one bean of type"); - } - catch (BeanCreationException e) { - // expected - } + assertThatExceptionOfType(BeanCreationException.class).as("should have failed, more than one bean of type").isThrownBy(() -> + bf.getBean("annotatedBean")); } @Test @@ -325,10 +323,10 @@ public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandid MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); TestBean tb = (TestBean) bf.getBean("testBean1"); - assertEquals(1, bean.getTestBeanMap().size()); - assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); - assertTrue(bean.getTestBeanMap().values().contains(tb)); - assertSame(tb, bean.getTestBean()); + assertThat(bean.getTestBeanMap().size()).isEqualTo(1); + assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); + assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); + assertThat(bean.getTestBean()).isSameAs(tb); } @Test @@ -340,7 +338,7 @@ public void testObjectFactoryInjection() { bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -351,7 +349,7 @@ public void testObjectFactoryQualifierInjection() { bf.registerBeanDefinition("testBean", bd); ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -365,10 +363,10 @@ public void testObjectFactoryFieldInjectionIntoPrototypeBean() { bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); ObjectFactoryQualifierFieldInjectionBean anotherBean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean"); - assertNotSame(anotherBean, bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean).isNotSameAs(anotherBean); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -382,10 +380,10 @@ public void testObjectFactoryMethodInjectionIntoPrototypeBean() { bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); ObjectFactoryQualifierMethodInjectionBean bean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); ObjectFactoryQualifierMethodInjectionBean anotherBean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean"); - assertNotSame(anotherBean, bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean).isNotSameAs(anotherBean); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -395,9 +393,9 @@ public void testObjectFactoryWithBeanField() throws Exception { bf.setSerializationId("test"); ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = (ObjectFactoryFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -407,9 +405,9 @@ public void testObjectFactoryWithBeanMethod() throws Exception { bf.setSerializationId("test"); ObjectFactoryMethodInjectionBean bean = (ObjectFactoryMethodInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = (ObjectFactoryMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -419,9 +417,9 @@ public void testObjectFactoryWithTypedListField() throws Exception { bf.setSerializationId("test"); ObjectFactoryListFieldInjectionBean bean = (ObjectFactoryListFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = (ObjectFactoryListFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -431,9 +429,9 @@ public void testObjectFactoryWithTypedListMethod() throws Exception { bf.setSerializationId("test"); ObjectFactoryListMethodInjectionBean bean = (ObjectFactoryListMethodInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = (ObjectFactoryListMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -443,9 +441,9 @@ public void testObjectFactoryWithTypedMapField() throws Exception { bf.setSerializationId("test"); ObjectFactoryMapFieldInjectionBean bean = (ObjectFactoryMapFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = (ObjectFactoryMapFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -455,9 +453,9 @@ public void testObjectFactoryWithTypedMapMethod() throws Exception { bf.setSerializationId("test"); ObjectFactoryMapMethodInjectionBean bean = (ObjectFactoryMapMethodInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = (ObjectFactoryMapMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } /** @@ -473,10 +471,9 @@ public void testBeanAutowiredWithFactoryBean() { final StringFactoryBean factoryBean = (StringFactoryBean) bf.getBean("&stringFactoryBean"); final FactoryBeanDependentBean bean = (FactoryBeanDependentBean) bf.getBean("factoryBeanDependentBean"); - assertNotNull("The singleton StringFactoryBean should have been registered.", factoryBean); - assertNotNull("The factoryBeanDependentBean should have been registered.", bean); - assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.", - factoryBean, bean.getFactoryBean()); + assertThat(factoryBean).as("The singleton StringFactoryBean should have been registered.").isNotNull(); + assertThat(bean).as("The factoryBeanDependentBean should have been registered.").isNotNull(); + assertThat(bean.getFactoryBean()).as("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.").isEqualTo(factoryBean); } @Test @@ -485,7 +482,7 @@ public void testNullableFieldInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); NullableFieldInjectionBean bean = (NullableFieldInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -493,7 +490,7 @@ public void testNullableFieldInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(NullableFieldInjectionBean.class)); NullableFieldInjectionBean bean = (NullableFieldInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean()); + assertThat(bean.getTestBean()).isNull(); } @Test @@ -502,7 +499,7 @@ public void testNullableMethodInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); NullableMethodInjectionBean bean = (NullableMethodInjectionBean) bf.getBean("annotatedBean"); - assertSame(bf.getBean("testBean"), bean.getTestBean()); + assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @Test @@ -510,7 +507,7 @@ public void testNullableMethodInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(NullableMethodInjectionBean.class)); NullableMethodInjectionBean bean = (NullableMethodInjectionBean) bf.getBean("annotatedBean"); - assertNull(bean.getTestBean()); + assertThat(bean.getTestBean()).isNull(); } @Test @@ -519,8 +516,8 @@ public void testOptionalFieldInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean"); - assertTrue(bean.getTestBean().isPresent()); - assertSame(bf.getBean("testBean"), bean.getTestBean().get()); + assertThat(bean.getTestBean().isPresent()).isTrue(); + assertThat(bean.getTestBean().get()).isSameAs(bf.getBean("testBean")); } @Test @@ -528,7 +525,7 @@ public void testOptionalFieldInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalFieldInjectionBean.class)); OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean"); - assertFalse(bean.getTestBean().isPresent()); + assertThat(bean.getTestBean().isPresent()).isFalse(); } @Test @@ -537,8 +534,8 @@ public void testOptionalMethodInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalMethodInjectionBean bean = (OptionalMethodInjectionBean) bf.getBean("annotatedBean"); - assertTrue(bean.getTestBean().isPresent()); - assertSame(bf.getBean("testBean"), bean.getTestBean().get()); + assertThat(bean.getTestBean().isPresent()).isTrue(); + assertThat(bean.getTestBean().get()).isSameAs(bf.getBean("testBean")); } @Test @@ -546,7 +543,7 @@ public void testOptionalMethodInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalMethodInjectionBean.class)); OptionalMethodInjectionBean bean = (OptionalMethodInjectionBean) bf.getBean("annotatedBean"); - assertFalse(bean.getTestBean().isPresent()); + assertThat(bean.getTestBean().isPresent()).isFalse(); } @Test @@ -555,8 +552,8 @@ public void testOptionalListFieldInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalListFieldInjectionBean bean = (OptionalListFieldInjectionBean) bf.getBean("annotatedBean"); - assertTrue(bean.getTestBean().isPresent()); - assertSame(bf.getBean("testBean"), bean.getTestBean().get().get(0)); + assertThat(bean.getTestBean().isPresent()).isTrue(); + assertThat(bean.getTestBean().get().get(0)).isSameAs(bf.getBean("testBean")); } @Test @@ -564,7 +561,7 @@ public void testOptionalListFieldInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListFieldInjectionBean.class)); OptionalListFieldInjectionBean bean = (OptionalListFieldInjectionBean) bf.getBean("annotatedBean"); - assertFalse(bean.getTestBean().isPresent()); + assertThat(bean.getTestBean().isPresent()).isFalse(); } @Test @@ -573,8 +570,8 @@ public void testOptionalListMethodInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalListMethodInjectionBean bean = (OptionalListMethodInjectionBean) bf.getBean("annotatedBean"); - assertTrue(bean.getTestBean().isPresent()); - assertSame(bf.getBean("testBean"), bean.getTestBean().get().get(0)); + assertThat(bean.getTestBean().isPresent()).isTrue(); + assertThat(bean.getTestBean().get().get(0)).isSameAs(bf.getBean("testBean")); } @Test @@ -582,7 +579,7 @@ public void testOptionalListMethodInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListMethodInjectionBean.class)); OptionalListMethodInjectionBean bean = (OptionalListMethodInjectionBean) bf.getBean("annotatedBean"); - assertFalse(bean.getTestBean().isPresent()); + assertThat(bean.getTestBean().isPresent()).isFalse(); } @Test @@ -591,8 +588,8 @@ public void testProviderOfOptionalFieldInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ProviderOfOptionalFieldInjectionBean bean = (ProviderOfOptionalFieldInjectionBean) bf.getBean("annotatedBean"); - assertTrue(bean.getTestBean().isPresent()); - assertSame(bf.getBean("testBean"), bean.getTestBean().get()); + assertThat(bean.getTestBean().isPresent()).isTrue(); + assertThat(bean.getTestBean().get()).isSameAs(bf.getBean("testBean")); } @Test @@ -600,7 +597,7 @@ public void testProviderOfOptionalFieldInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ProviderOfOptionalFieldInjectionBean.class)); ProviderOfOptionalFieldInjectionBean bean = (ProviderOfOptionalFieldInjectionBean) bf.getBean("annotatedBean"); - assertFalse(bean.getTestBean().isPresent()); + assertThat(bean.getTestBean().isPresent()).isFalse(); } @Test @@ -609,8 +606,8 @@ public void testProviderOfOptionalMethodInjectionWithBeanAvailable() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ProviderOfOptionalMethodInjectionBean bean = (ProviderOfOptionalMethodInjectionBean) bf.getBean("annotatedBean"); - assertTrue(bean.getTestBean().isPresent()); - assertSame(bf.getBean("testBean"), bean.getTestBean().get()); + assertThat(bean.getTestBean().isPresent()).isTrue(); + assertThat(bean.getTestBean().get()).isSameAs(bf.getBean("testBean")); } @Test @@ -618,14 +615,14 @@ public void testProviderOfOptionalMethodInjectionWithBeanNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ProviderOfOptionalMethodInjectionBean.class)); ProviderOfOptionalMethodInjectionBean bean = (ProviderOfOptionalMethodInjectionBean) bf.getBean("annotatedBean"); - assertFalse(bean.getTestBean().isPresent()); + assertThat(bean.getTestBean().isPresent()).isFalse(); } @Test public void testAnnotatedDefaultConstructor() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class)); - assertNotNull(bf.getBean("annotatedBean")); + assertThat(bf.getBean("annotatedBean")).isNotNull(); } @@ -669,7 +666,9 @@ public ExtendedResourceInjectionBean() { } @Override - @Inject @Required + @Inject + @Required + @SuppressWarnings("deprecation") public void setTestBean2(TestBean testBean2) { super.setTestBean2(testBean2); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java index 0f5b5ef2e412..bd30b5b44c24 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -16,14 +16,16 @@ package org.springframework.beans.factory.annotation; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Karl Pietrzak @@ -34,8 +36,8 @@ public class LookupAnnotationTests { private DefaultListableBeanFactory beanFactory; - @Before - public void setUp() { + @BeforeEach + public void setup() { beanFactory = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor aabpp = new AutowiredAnnotationBeanPostProcessor(); aabpp.setBeanFactory(beanFactory); @@ -43,7 +45,7 @@ public void setUp() { beanFactory.registerBeanDefinition("abstractBean", new RootBeanDefinition(AbstractBean.class)); beanFactory.registerBeanDefinition("beanConsumer", new RootBeanDefinition(BeanConsumer.class)); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); - tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); + tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); beanFactory.registerBeanDefinition("testBean", tbd); } @@ -51,69 +53,78 @@ public void setUp() { @Test public void testWithoutConstructorArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); Object expected = bean.get(); - assertEquals(TestBean.class, expected.getClass()); - assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean); } @Test public void testWithOverloadedArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); TestBean expected = bean.get("haha"); - assertEquals(TestBean.class, expected.getClass()); - assertEquals("haha", expected.getName()); - assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(expected.getName()).isEqualTo("haha"); + assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean); } @Test public void testWithOneConstructorArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); TestBean expected = bean.getOneArgument("haha"); - assertEquals(TestBean.class, expected.getClass()); - assertEquals("haha", expected.getName()); - assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(expected.getName()).isEqualTo("haha"); + assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean); } @Test public void testWithTwoConstructorArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); TestBean expected = bean.getTwoArguments("haha", 72); - assertEquals(TestBean.class, expected.getClass()); - assertEquals("haha", expected.getName()); - assertEquals(72, expected.getAge()); - assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(expected.getName()).isEqualTo("haha"); + assertThat(expected.getAge()).isEqualTo(72); + assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean); } @Test public void testWithThreeArgsShouldFail() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); - try { - bean.getThreeArguments("name", 1, 2); - fail("TestBean does not have a three arg constructor so this should not have worked"); - } - catch (AbstractMethodError ex) { - } - assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean); + assertThat(bean).isNotNull(); + assertThatExceptionOfType(AbstractMethodError.class).as("TestBean has no three arg constructor").isThrownBy(() -> + bean.getThreeArguments("name", 1, 2)); + assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean); } @Test public void testWithEarlyInjection() { AbstractBean bean = beanFactory.getBean("beanConsumer", BeanConsumer.class).abstractBean; - assertNotNull(bean); + assertThat(bean).isNotNull(); Object expected = bean.get(); - assertEquals(TestBean.class, expected.getClass()); - assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean); + } + + @Test // gh-25806 + public void testWithNullBean() { + RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class, () -> null); + tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); + beanFactory.registerBeanDefinition("testBean", tbd); + + AbstractBean bean = beanFactory.getBean("beanConsumer", BeanConsumer.class).abstractBean; + assertThat(bean).isNotNull(); + Object expected = bean.get(); + assertThat(expected).isNull(); + assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean); } public static abstract class AbstractBean { - @Lookup + @Lookup("testBean") public abstract TestBean get(); @Lookup @@ -125,6 +136,7 @@ public static abstract class AbstractBean { @Lookup public abstract TestBean getTwoArguments(String name, int age); + // no @Lookup annotation public abstract TestBean getThreeArguments(String name, int age, int anotherArg); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/ParameterResolutionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/ParameterResolutionTests.java new file mode 100644 index 000000000000..9710d29c38a1 --- /dev/null +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/ParameterResolutionTests.java @@ -0,0 +1,167 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.beans.factory.annotation; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Executable; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.beans.factory.config.DependencyDescriptor; +import org.springframework.util.ClassUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +/** + * Unit tests for {@link ParameterResolutionDelegate}. + * + * @author Sam Brannen + * @author Juergen Hoeller + * @author Loïc Ledoyen + */ +public class ParameterResolutionTests { + + @Test + public void isAutowirablePreconditions() { + assertThatIllegalArgumentException().isThrownBy(() -> + ParameterResolutionDelegate.isAutowirable(null, 0)) + .withMessageContaining("Parameter must not be null"); + } + + @Test + public void annotatedParametersInMethodAreCandidatesForAutowiring() throws Exception { + Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class); + assertAutowirableParameters(method); + } + + @Test + public void annotatedParametersInTopLevelClassConstructorAreCandidatesForAutowiring() throws Exception { + Constructor constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class); + assertAutowirableParameters(constructor); + } + + @Test + public void annotatedParametersInInnerClassConstructorAreCandidatesForAutowiring() throws Exception { + Class innerClass = AutowirableClass.InnerAutowirableClass.class; + assertThat(ClassUtils.isInnerClass(innerClass)).isTrue(); + Constructor constructor = innerClass.getConstructor(AutowirableClass.class, String.class, String.class); + assertAutowirableParameters(constructor); + } + + private void assertAutowirableParameters(Executable executable) { + int startIndex = (executable instanceof Constructor) + && ClassUtils.isInnerClass(executable.getDeclaringClass()) ? 1 : 0; + Parameter[] parameters = executable.getParameters(); + for (int parameterIndex = startIndex; parameterIndex < parameters.length; parameterIndex++) { + Parameter parameter = parameters[parameterIndex]; + assertThat(ParameterResolutionDelegate.isAutowirable(parameter, parameterIndex)).as("Parameter " + parameter + " must be autowirable").isTrue(); + } + } + + @Test + public void nonAnnotatedParametersInTopLevelClassConstructorAreNotCandidatesForAutowiring() throws Exception { + Constructor notAutowirableConstructor = AutowirableClass.class.getConstructor(String.class); + + Parameter[] parameters = notAutowirableConstructor.getParameters(); + for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) { + Parameter parameter = parameters[parameterIndex]; + assertThat(ParameterResolutionDelegate.isAutowirable(parameter, parameterIndex)).as("Parameter " + parameter + " must not be autowirable").isFalse(); + } + } + + @Test + public void resolveDependencyPreconditionsForParameter() { + assertThatIllegalArgumentException().isThrownBy(() -> + ParameterResolutionDelegate.resolveDependency(null, 0, null, mock(AutowireCapableBeanFactory.class))) + .withMessageContaining("Parameter must not be null"); + } + + @Test + public void resolveDependencyPreconditionsForContainingClass() throws Exception { + assertThatIllegalArgumentException().isThrownBy(() -> + ParameterResolutionDelegate.resolveDependency(getParameter(), 0, null, null)) + .withMessageContaining("Containing class must not be null"); + } + + @Test + public void resolveDependencyPreconditionsForBeanFactory() throws Exception { + assertThatIllegalArgumentException().isThrownBy(() -> + ParameterResolutionDelegate.resolveDependency(getParameter(), 0, getClass(), null)) + .withMessageContaining("AutowireCapableBeanFactory must not be null"); + } + + private Parameter getParameter() throws NoSuchMethodException { + Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class); + return method.getParameters()[0]; + } + + @Test + public void resolveDependencyForAnnotatedParametersInTopLevelClassConstructor() throws Exception { + Constructor constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class); + + AutowireCapableBeanFactory beanFactory = mock(AutowireCapableBeanFactory.class); + // Configure the mocked BeanFactory to return the DependencyDescriptor for convenience and + // to avoid using an ArgumentCaptor. + given(beanFactory.resolveDependency(any(), isNull())).willAnswer(invocation -> invocation.getArgument(0)); + + Parameter[] parameters = constructor.getParameters(); + for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) { + Parameter parameter = parameters[parameterIndex]; + DependencyDescriptor intermediateDependencyDescriptor = (DependencyDescriptor) ParameterResolutionDelegate.resolveDependency( + parameter, parameterIndex, AutowirableClass.class, beanFactory); + assertThat(intermediateDependencyDescriptor.getAnnotatedElement()).isEqualTo(constructor); + assertThat(intermediateDependencyDescriptor.getMethodParameter().getParameter()).isEqualTo(parameter); + } + } + + + void autowirableMethod( + @Autowired String firstParameter, + @Qualifier("someQualifier") String secondParameter, + @Value("${someValue}") String thirdParameter, + @Autowired(required = false) String fourthParameter) { + } + + + public static class AutowirableClass { + + public AutowirableClass(@Autowired String firstParameter, + @Qualifier("someQualifier") String secondParameter, + @Value("${someValue}") String thirdParameter, + @Autowired(required = false) String fourthParameter) { + } + + public AutowirableClass(String notAutowirableParameter) { + } + + public class InnerAutowirableClass { + + public InnerAutowirableClass(@Autowired String firstParameter, + @Qualifier("someQualifier") String secondParameter) { + } + } + } + +} diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java index 9851381cc6ac..a88a756029b8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,7 +21,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanFactory; @@ -32,7 +32,8 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rob Harrop @@ -44,48 +45,38 @@ public class RequiredAnnotationBeanPostProcessorTests { @Test public void testWithRequiredPropertyOmitted() { - try { - DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); - BeanDefinition beanDef = BeanDefinitionBuilder - .genericBeanDefinition(RequiredTestBean.class) - .addPropertyValue("name", "Rob Harrop") - .addPropertyValue("favouriteColour", "Blue") - .addPropertyValue("jobTitle", "Grand Poobah") - .getBeanDefinition(); - factory.registerBeanDefinition("testBean", beanDef); - factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); - factory.preInstantiateSingletons(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - String message = ex.getCause().getMessage(); - assertTrue(message.contains("Property")); - assertTrue(message.contains("age")); - assertTrue(message.contains("testBean")); - } + DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); + BeanDefinition beanDef = BeanDefinitionBuilder + .genericBeanDefinition(RequiredTestBean.class) + .addPropertyValue("name", "Rob Harrop") + .addPropertyValue("favouriteColour", "Blue") + .addPropertyValue("jobTitle", "Grand Poobah") + .getBeanDefinition(); + factory.registerBeanDefinition("testBean", beanDef); + factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + factory::preInstantiateSingletons) + .withMessageContaining("Property") + .withMessageContaining("age") + .withMessageContaining("testBean"); } @Test public void testWithThreeRequiredPropertiesOmitted() { - try { - DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); - BeanDefinition beanDef = BeanDefinitionBuilder - .genericBeanDefinition(RequiredTestBean.class) - .addPropertyValue("name", "Rob Harrop") - .getBeanDefinition(); - factory.registerBeanDefinition("testBean", beanDef); - factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); - factory.preInstantiateSingletons(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - String message = ex.getCause().getMessage(); - assertTrue(message.contains("Properties")); - assertTrue(message.contains("age")); - assertTrue(message.contains("favouriteColour")); - assertTrue(message.contains("jobTitle")); - assertTrue(message.contains("testBean")); - } + DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); + BeanDefinition beanDef = BeanDefinitionBuilder + .genericBeanDefinition(RequiredTestBean.class) + .addPropertyValue("name", "Rob Harrop") + .getBeanDefinition(); + factory.registerBeanDefinition("testBean", beanDef); + factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + factory::preInstantiateSingletons) + .withMessageContaining("Properties") + .withMessageContaining("age") + .withMessageContaining("favouriteColour") + .withMessageContaining("jobTitle") + .withMessageContaining("testBean"); } @Test @@ -101,54 +92,44 @@ public void testWithAllRequiredPropertiesSpecified() { factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean"); - assertEquals(24, bean.getAge()); - assertEquals("Blue", bean.getFavouriteColour()); + assertThat(bean.getAge()).isEqualTo(24); + assertThat(bean.getFavouriteColour()).isEqualTo("Blue"); } @Test public void testWithCustomAnnotation() { - try { - DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); - BeanDefinition beanDef = BeanDefinitionBuilder - .genericBeanDefinition(RequiredTestBean.class) - .getBeanDefinition(); - factory.registerBeanDefinition("testBean", beanDef); - RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor(); - rabpp.setRequiredAnnotationType(MyRequired.class); - factory.addBeanPostProcessor(rabpp); - factory.preInstantiateSingletons(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - String message = ex.getCause().getMessage(); - assertTrue(message.contains("Property")); - assertTrue(message.contains("name")); - assertTrue(message.contains("testBean")); - } + DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); + BeanDefinition beanDef = BeanDefinitionBuilder + .genericBeanDefinition(RequiredTestBean.class) + .getBeanDefinition(); + factory.registerBeanDefinition("testBean", beanDef); + RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor(); + rabpp.setRequiredAnnotationType(MyRequired.class); + factory.addBeanPostProcessor(rabpp); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + factory::preInstantiateSingletons) + .withMessageContaining("Property") + .withMessageContaining("name") + .withMessageContaining("testBean"); } @Test public void testWithStaticFactoryMethod() { - try { - DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); - BeanDefinition beanDef = BeanDefinitionBuilder - .genericBeanDefinition(RequiredTestBean.class) - .setFactoryMethod("create") - .addPropertyValue("name", "Rob Harrop") - .addPropertyValue("favouriteColour", "Blue") - .addPropertyValue("jobTitle", "Grand Poobah") - .getBeanDefinition(); - factory.registerBeanDefinition("testBean", beanDef); - factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); - factory.preInstantiateSingletons(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - String message = ex.getCause().getMessage(); - assertTrue(message.contains("Property")); - assertTrue(message.contains("age")); - assertTrue(message.contains("testBean")); - } + DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); + BeanDefinition beanDef = BeanDefinitionBuilder + .genericBeanDefinition(RequiredTestBean.class) + .setFactoryMethod("create") + .addPropertyValue("name", "Rob Harrop") + .addPropertyValue("favouriteColour", "Blue") + .addPropertyValue("jobTitle", "Grand Poobah") + .getBeanDefinition(); + factory.registerBeanDefinition("testBean", beanDef); + factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + factory::preInstantiateSingletons) + .withMessageContaining("Property") + .withMessageContaining("age") + .withMessageContaining("testBean"); } @Test @@ -165,8 +146,8 @@ public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() { factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean"); - assertEquals(24, bean.getAge()); - assertEquals("Blue", bean.getFavouriteColour()); + assertThat(bean.getAge()).isEqualTo(24); + assertThat(bean.getFavouriteColour()).isEqualTo("Blue"); } @Test diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomEditorConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomEditorConfigurerTests.java index 46e497e8a6c9..a94425ccf1aa 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomEditorConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomEditorConfigurerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -25,7 +25,7 @@ import java.util.Locale; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyEditorRegistrar; @@ -33,9 +33,9 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.propertyeditors.CustomDateEditor; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -70,9 +70,9 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { bf.registerBeanDefinition("tb2", bd2); TestBean tb1 = (TestBean) bf.getBean("tb1"); - assertEquals(df.parse("2.12.1975"), tb1.getDate()); + assertThat(tb1.getDate()).isEqualTo(df.parse("2.12.1975")); TestBean tb2 = (TestBean) bf.getBean("tb2"); - assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey")); + assertThat(tb2.getSomeMap().get("myKey")).isEqualTo(df.parse("2.12.1975")); } @Test @@ -92,7 +92,7 @@ public void testCustomEditorConfigurerWithEditorAsClass() throws ParseException TestBean tb = (TestBean) bf.getBean("tb"); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN); - assertEquals(df.parse("2.12.1975"), tb.getDate()); + assertThat(tb.getDate()).isEqualTo(df.parse("2.12.1975")); } @Test @@ -111,8 +111,8 @@ public void testCustomEditorConfigurerWithRequiredTypeArray() throws ParseExcept bf.registerBeanDefinition("tb", bd); TestBean tb = (TestBean) bf.getBean("tb"); - assertTrue(tb.getStringArray() != null && tb.getStringArray().length == 1); - assertEquals("test", tb.getStringArray()[0]); + assertThat(tb.getStringArray() != null && tb.getStringArray().length == 1).isTrue(); + assertThat(tb.getStringArray()[0]).isEqualTo("test"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java index 4c45809067b5..c30265249631 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,12 +19,14 @@ import java.util.HashMap; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.Mockito.mock; /** * Unit tests for {@link CustomScopeConfigurer}. @@ -64,7 +66,8 @@ public void testSunnyDayWithBonaFideScopeClass() { CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); figurer.postProcessBeanFactory(factory); - assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope); + boolean condition = factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope; + assertThat(condition).isTrue(); } @Test @@ -74,35 +77,39 @@ public void testSunnyDayWithBonaFideScopeClassName() { CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); figurer.postProcessBeanFactory(factory); - assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope); + boolean condition = factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope; + assertThat(condition).isTrue(); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWhereScopeMapHasNullScopeValueInEntrySet() { Map scopes = new HashMap<>(); scopes.put(FOO_SCOPE, null); CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); - figurer.postProcessBeanFactory(factory); + assertThatIllegalArgumentException().isThrownBy(() -> + figurer.postProcessBeanFactory(factory)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWhereScopeMapHasNonScopeInstanceInEntrySet() { Map scopes = new HashMap<>(); scopes.put(FOO_SCOPE, this); // <-- not a valid value... CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); - figurer.postProcessBeanFactory(factory); + assertThatIllegalArgumentException().isThrownBy(() -> + figurer.postProcessBeanFactory(factory)); } - @SuppressWarnings("unchecked") - @Test(expected = ClassCastException.class) + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() { Map scopes = new HashMap(); scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)... CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); - figurer.postProcessBeanFactory(factory); + assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> + figurer.postProcessBeanFactory(factory)); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java index 07752bf048ea..2be93f0d400e 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,12 +16,12 @@ package org.springframework.beans.factory.config; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Arjen Poutsma @@ -43,8 +43,8 @@ public void postProcess() { DeprecatedBeanWarner warner = new MyDeprecatedBeanWarner(); warner.postProcessBeanFactory(beanFactory); - assertEquals(beanName, this.beanName); - assertEquals(def, this.beanDefinition); + assertThat(this.beanName).isEqualTo(beanName); + assertThat(this.beanDefinition).isEqualTo(def); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests.java index 2f432e33be5d..b697500fb343 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -18,15 +18,14 @@ import java.sql.Connection; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Unit tests for {@link FieldRetrievingFactoryBean}. @@ -37,15 +36,12 @@ */ public class FieldRetrievingFactoryBeanTests { - private static final Resource CONTEXT = - qualifiedResource(FieldRetrievingFactoryBeanTests.class, "context.xml"); - @Test public void testStaticField() throws Exception { FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); fr.setStaticField("java.sql.Connection.TRANSACTION_SERIALIZABLE"); fr.afterPropertiesSet(); - assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); + assertThat(fr.getObject()).isEqualTo(Connection.TRANSACTION_SERIALIZABLE); } @Test @@ -53,7 +49,7 @@ public void testStaticFieldWithWhitespace() throws Exception { FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); fr.setStaticField(" java.sql.Connection.TRANSACTION_SERIALIZABLE "); fr.afterPropertiesSet(); - assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); + assertThat(fr.getObject()).isEqualTo(Connection.TRANSACTION_SERIALIZABLE); } @Test @@ -62,7 +58,7 @@ public void testStaticFieldViaClassAndFieldName() throws Exception { fr.setTargetClass(Connection.class); fr.setTargetField("TRANSACTION_SERIALIZABLE"); fr.afterPropertiesSet(); - assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); + assertThat(fr.getObject()).isEqualTo(Connection.TRANSACTION_SERIALIZABLE); } @Test @@ -72,7 +68,7 @@ public void testNonStaticField() throws Exception { fr.setTargetObject(target); fr.setTargetField("publicField"); fr.afterPropertiesSet(); - assertEquals(target.publicField, fr.getObject()); + assertThat(fr.getObject()).isEqualTo(target.publicField); } @Test @@ -80,7 +76,7 @@ public void testNothingButBeanName() throws Exception { FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); fr.setBeanName("java.sql.Connection.TRANSACTION_SERIALIZABLE"); fr.afterPropertiesSet(); - assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); + assertThat(fr.getObject()).isEqualTo(Connection.TRANSACTION_SERIALIZABLE); } @Test @@ -119,18 +115,20 @@ public void testJustTargetObject() throws Exception { @Test public void testWithConstantOnClassWithPackageLevelVisibility() throws Exception { FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); - fr.setBeanName("org.springframework.tests.sample.beans.PackageLevelVisibleBean.CONSTANT"); + fr.setBeanName("org.springframework.beans.testfixture.beans.PackageLevelVisibleBean.CONSTANT"); fr.afterPropertiesSet(); - assertEquals("Wuby", fr.getObject()); + assertThat(fr.getObject()).isEqualTo("Wuby"); } @Test public void testBeanNameSyntaxWithBeanFactory() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + qualifiedResource(FieldRetrievingFactoryBeanTests.class, "context.xml")); + TestBean testBean = (TestBean) bf.getBean("testBean"); - assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[0]); - assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[1]); + assertThat(testBean.getSomeIntegerArray()[0]).isEqualTo(Connection.TRANSACTION_SERIALIZABLE); + assertThat(testBean.getSomeIntegerArray()[1]).isEqualTo(Connection.TRANSACTION_SERIALIZABLE); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java index 8d0786bada52..fae712817bd5 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,13 +20,15 @@ import java.util.Collection; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.support.ArgumentConvertingMethodInvoker; import org.springframework.util.MethodInvoker; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for {@link MethodInvokingFactoryBean} and {@link MethodInvokingBean}. @@ -40,75 +42,38 @@ public class MethodInvokingFactoryBeanTests { @Test public void testParameterValidation() throws Exception { - String validationError = "improper validation of input properties"; // assert that only static OR non static are set, but not both or none MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); - try { - mcfb.afterPropertiesSet(); - fail(validationError); - } - catch (IllegalArgumentException ex) { - // expected - } + assertThatIllegalArgumentException().isThrownBy(mcfb::afterPropertiesSet); mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetObject(this); mcfb.setTargetMethod("whatever"); - try { - mcfb.afterPropertiesSet(); - fail(validationError); - } - catch (NoSuchMethodException ex) { - // expected - } + assertThatExceptionOfType(NoSuchMethodException.class).isThrownBy(mcfb::afterPropertiesSet); // bogus static method mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("some.bogus.Method.name"); - try { - mcfb.afterPropertiesSet(); - fail(validationError); - } - catch (NoSuchMethodException ex) { - // expected - } + assertThatExceptionOfType(NoSuchMethodException.class).isThrownBy(mcfb::afterPropertiesSet); // bogus static method mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("method1"); - try { - mcfb.afterPropertiesSet(); - fail(validationError); - } - catch (IllegalArgumentException ex) { - // expected - } + assertThatIllegalArgumentException().isThrownBy(mcfb::afterPropertiesSet); // missing method mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetObject(this); - try { - mcfb.afterPropertiesSet(); - fail(validationError); - } - catch (IllegalArgumentException ex) { - // expected - } + assertThatIllegalArgumentException().isThrownBy(mcfb::afterPropertiesSet); // bogus method mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetObject(this); mcfb.setTargetMethod("bogus"); - try { - mcfb.afterPropertiesSet(); - fail(validationError); - } - catch (NoSuchMethodException ex) { - // expected - } + assertThatExceptionOfType(NoSuchMethodException.class).isThrownBy(mcfb::afterPropertiesSet); // static method TestClass1._staticField1 = 0; @@ -133,14 +98,14 @@ public void testGetObjectType() throws Exception { mcfb.setTargetObject(tc1); mcfb.setTargetMethod("method1"); mcfb.afterPropertiesSet(); - assertTrue(int.class.equals(mcfb.getObjectType())); + assertThat(int.class.equals(mcfb.getObjectType())).isTrue(); mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("voidRetvalMethod"); mcfb.afterPropertiesSet(); Class objType = mcfb.getObjectType(); - assertSame(objType, void.class); + assertThat(void.class).isSameAs(objType); // verify that we can call a method with args that are subtypes of the // target method arg types @@ -158,13 +123,7 @@ public void testGetObjectType() throws Exception { mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes"); mcfb.setArguments("1", new Object()); - try { - mcfb.afterPropertiesSet(); - fail("Should have thrown NoSuchMethodException"); - } - catch (NoSuchMethodException ex) { - // expected - } + assertThatExceptionOfType(NoSuchMethodException.class).isThrownBy(mcfb::afterPropertiesSet); } @Test @@ -176,9 +135,9 @@ public void testGetObject() throws Exception { mcfb.setTargetMethod("method1"); mcfb.afterPropertiesSet(); Integer i = (Integer) mcfb.getObject(); - assertEquals(1, i.intValue()); + assertThat(i.intValue()).isEqualTo(1); i = (Integer) mcfb.getObject(); - assertEquals(1, i.intValue()); + assertThat(i.intValue()).isEqualTo(1); // non-singleton, non-static tc1 = new TestClass1(); @@ -188,9 +147,9 @@ public void testGetObject() throws Exception { mcfb.setSingleton(false); mcfb.afterPropertiesSet(); i = (Integer) mcfb.getObject(); - assertEquals(1, i.intValue()); + assertThat(i.intValue()).isEqualTo(1); i = (Integer) mcfb.getObject(); - assertEquals(2, i.intValue()); + assertThat(i.intValue()).isEqualTo(2); // singleton, static TestClass1._staticField1 = 0; @@ -199,9 +158,9 @@ public void testGetObject() throws Exception { mcfb.setTargetMethod("staticMethod1"); mcfb.afterPropertiesSet(); i = (Integer) mcfb.getObject(); - assertEquals(1, i.intValue()); + assertThat(i.intValue()).isEqualTo(1); i = (Integer) mcfb.getObject(); - assertEquals(1, i.intValue()); + assertThat(i.intValue()).isEqualTo(1); // non-singleton, static TestClass1._staticField1 = 0; @@ -210,16 +169,16 @@ public void testGetObject() throws Exception { mcfb.setSingleton(false); mcfb.afterPropertiesSet(); i = (Integer) mcfb.getObject(); - assertEquals(1, i.intValue()); + assertThat(i.intValue()).isEqualTo(1); i = (Integer) mcfb.getObject(); - assertEquals(2, i.intValue()); + assertThat(i.intValue()).isEqualTo(2); // void return value mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("voidRetvalMethod"); mcfb.afterPropertiesSet(); - assertNull(mcfb.getObject()); + assertThat(mcfb.getObject()).isNull(); // now see if we can match methods with arguments that have supertype arguments mcfb = new MethodInvokingFactoryBean(); @@ -236,45 +195,32 @@ public void testArgumentConversion() throws Exception { mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes"); mcfb.setArguments(new ArrayList<>(), new ArrayList(), "hello", "bogus"); - try { - mcfb.afterPropertiesSet(); - fail("Matched method with wrong number of args"); - } - catch (NoSuchMethodException ex) { - // expected - } + assertThatExceptionOfType(NoSuchMethodException.class).as( + "Matched method with wrong number of args").isThrownBy( + mcfb::afterPropertiesSet); mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes"); mcfb.setArguments(1, new Object()); - try { - mcfb.afterPropertiesSet(); - mcfb.getObject(); - fail("Should have failed on getObject with mismatched argument types"); - } - catch (NoSuchMethodException ex) { - // expected - } + assertThatExceptionOfType(NoSuchMethodException.class).as( + "Should have failed on getObject with mismatched argument types").isThrownBy( + mcfb::afterPropertiesSet); mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes2"); mcfb.setArguments(new ArrayList<>(), new ArrayList(), "hello", "bogus"); mcfb.afterPropertiesSet(); - assertEquals("hello", mcfb.getObject()); + assertThat(mcfb.getObject()).isEqualTo("hello"); mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes2"); mcfb.setArguments(new ArrayList<>(), new ArrayList(), new Object()); - try { - mcfb.afterPropertiesSet(); - fail("Matched method when shouldn't have matched"); - } - catch (NoSuchMethodException ex) { - // expected - } + assertThatExceptionOfType(NoSuchMethodException.class).as( + "Matched method when shouldn't have matched").isThrownBy( + mcfb::afterPropertiesSet); } @Test diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/MyDeprecatedBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/MyDeprecatedBean.java index ef3071088ba3..69a9db66d5de 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/MyDeprecatedBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/MyDeprecatedBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests.java index 5cc613d8ccd3..97805143da86 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,22 +17,24 @@ package org.springframework.beans.factory.config; import java.util.Date; + import javax.inject.Provider; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.util.SerializationTestUtils; +import org.springframework.core.testfixture.io.SerializationTestUtils; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Colin Sampaleanu @@ -42,34 +44,35 @@ */ public class ObjectFactoryCreatingFactoryBeanTests { - private static final Resource CONTEXT = - qualifiedResource(ObjectFactoryCreatingFactoryBeanTests.class, "context.xml"); - private DefaultListableBeanFactory beanFactory; - @Before - public void setUp() { + + @BeforeEach + public void setup() { this.beanFactory = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( + qualifiedResource(ObjectFactoryCreatingFactoryBeanTests.class, "context.xml")); this.beanFactory.setSerializationId("test"); } - @After - public void tearDown() { + @AfterEach + public void close() { this.beanFactory.setSerializationId(null); } + @Test - public void testFactoryOperation() throws Exception { + public void testFactoryOperation() { FactoryTestBean testBean = beanFactory.getBean("factoryTestBean", FactoryTestBean.class); ObjectFactory objectFactory = testBean.getObjectFactory(); Date date1 = (Date) objectFactory.getObject(); Date date2 = (Date) objectFactory.getObject(); - assertTrue(date1 != date2); + assertThat(date1 != date2).isTrue(); } @Test + @SuppressWarnings("rawtypes") public void testFactorySerialization() throws Exception { FactoryTestBean testBean = beanFactory.getBean("factoryTestBean", FactoryTestBean.class); ObjectFactory objectFactory = testBean.getObjectFactory(); @@ -78,20 +81,21 @@ public void testFactorySerialization() throws Exception { Date date1 = (Date) objectFactory.getObject(); Date date2 = (Date) objectFactory.getObject(); - assertTrue(date1 != date2); + assertThat(date1 != date2).isTrue(); } @Test - public void testProviderOperation() throws Exception { + public void testProviderOperation() { ProviderTestBean testBean = beanFactory.getBean("providerTestBean", ProviderTestBean.class); Provider provider = testBean.getProvider(); Date date1 = (Date) provider.get(); Date date2 = (Date) provider.get(); - assertTrue(date1 != date2); + assertThat(date1 != date2).isTrue(); } @Test + @SuppressWarnings("rawtypes") public void testProviderSerialization() throws Exception { ProviderTestBean testBean = beanFactory.getBean("providerTestBean", ProviderTestBean.class); Provider provider = testBean.getProvider(); @@ -100,7 +104,7 @@ public void testProviderSerialization() throws Exception { Date date1 = (Date) provider.get(); Date date2 = (Date) provider.get(); - assertTrue(date1 != date2); + assertThat(date1 != date2).isTrue(); } @Test @@ -117,44 +121,37 @@ public void testDoesNotComplainWhenTargetBeanNameRefersToSingleton() throws Exce factory.afterPropertiesSet(); ObjectFactory objectFactory = factory.getObject(); Object actualSingleton = objectFactory.getObject(); - assertSame(expectedSingleton, actualSingleton); + assertThat(actualSingleton).isSameAs(expectedSingleton); } @Test public void testWhenTargetBeanNameIsNull() throws Exception { - try { - new ObjectFactoryCreatingFactoryBean().afterPropertiesSet(); - fail("Must have thrown an IllegalArgumentException; 'targetBeanName' property not set."); - } - catch (IllegalArgumentException expected) {} + assertThatIllegalArgumentException().as( + "'targetBeanName' property not set").isThrownBy( + new ObjectFactoryCreatingFactoryBean()::afterPropertiesSet); } @Test public void testWhenTargetBeanNameIsEmptyString() throws Exception { - try { - ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); - factory.setTargetBeanName(""); - factory.afterPropertiesSet(); - fail("Must have thrown an IllegalArgumentException; 'targetBeanName' property set to (invalid) empty string."); - } - catch (IllegalArgumentException expected) {} + ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); + factory.setTargetBeanName(""); + assertThatIllegalArgumentException().as( + "'targetBeanName' property set to (invalid) empty string").isThrownBy( + factory::afterPropertiesSet); } @Test public void testWhenTargetBeanNameIsWhitespacedString() throws Exception { - try { - ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); - factory.setTargetBeanName(" \t"); - factory.afterPropertiesSet(); - fail("Must have thrown an IllegalArgumentException; 'targetBeanName' property set to (invalid) only-whitespace string."); - } - catch (IllegalArgumentException expected) {} + ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); + factory.setTargetBeanName(" \t"); + assertThatIllegalArgumentException().as( + "'targetBeanName' property set to (invalid) only-whitespace string").isThrownBy( + factory::afterPropertiesSet); } @Test - public void testEnsureOFBFBReportsThatItActuallyCreatesObjectFactoryInstances() throws Exception { - assertEquals("Must be reporting that it creates ObjectFactory instances (as per class contract).", - ObjectFactory.class, new ObjectFactoryCreatingFactoryBean().getObjectType()); + public void testEnsureOFBFBReportsThatItActuallyCreatesObjectFactoryInstances() { + assertThat(new ObjectFactoryCreatingFactoryBean().getObjectType()).as("Must be reporting that it creates ObjectFactory instances (as per class contract).").isEqualTo(ObjectFactory.class); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertiesFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertiesFactoryBeanTests.java index 4e78d1b3471a..27a00cb5c47c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertiesFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertiesFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,12 +18,12 @@ import java.util.Properties; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.core.io.Resource; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Unit tests for {@link PropertiesFactoryBean}. @@ -44,7 +44,7 @@ public void testWithPropertiesFile() throws Exception { pfb.setLocation(TEST_PROPS); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); - assertEquals("99", props.getProperty("tb.array[0].age")); + assertThat(props.getProperty("tb.array[0].age")).isEqualTo("99"); } @Test @@ -53,7 +53,7 @@ public void testWithPropertiesXmlFile() throws Exception { pfb.setLocation(TEST_PROPS_XML); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); - assertEquals("99", props.getProperty("tb.array[0].age")); + assertThat(props.getProperty("tb.array[0].age")).isEqualTo("99"); } @Test @@ -64,7 +64,7 @@ public void testWithLocalProperties() throws Exception { pfb.setProperties(localProps); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); - assertEquals("value2", props.getProperty("key2")); + assertThat(props.getProperty("key2")).isEqualTo("value2"); } @Test @@ -77,8 +77,8 @@ public void testWithPropertiesFileAndLocalProperties() throws Exception { pfb.setProperties(localProps); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); - assertEquals("99", props.getProperty("tb.array[0].age")); - assertEquals("value2", props.getProperty("key2")); + assertThat(props.getProperty("tb.array[0].age")).isEqualTo("99"); + assertThat(props.getProperty("key2")).isEqualTo("value2"); } @Test @@ -102,12 +102,12 @@ public void testWithPropertiesFileAndMultipleLocalProperties() throws Exception pfb.afterPropertiesSet(); Properties props = pfb.getObject(); - assertEquals("99", props.getProperty("tb.array[0].age")); - assertEquals("value2", props.getProperty("key2")); - assertEquals("framework", props.getProperty("spring")); - assertEquals("Mattingly", props.getProperty("Don")); - assertEquals("man", props.getProperty("spider")); - assertEquals("man", props.getProperty("bat")); + assertThat(props.getProperty("tb.array[0].age")).isEqualTo("99"); + assertThat(props.getProperty("key2")).isEqualTo("value2"); + assertThat(props.getProperty("spring")).isEqualTo("framework"); + assertThat(props.getProperty("Don")).isEqualTo("Mattingly"); + assertThat(props.getProperty("spider")).isEqualTo("man"); + assertThat(props.getProperty("bat")).isEqualTo("man"); } @Test @@ -121,8 +121,8 @@ public void testWithPropertiesFileAndLocalPropertiesAndLocalOverride() throws Ex pfb.setLocalOverride(true); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); - assertEquals("0", props.getProperty("tb.array[0].age")); - assertEquals("value2", props.getProperty("key2")); + assertThat(props.getProperty("tb.array[0].age")).isEqualTo("0"); + assertThat(props.getProperty("key2")).isEqualTo("value2"); } @Test @@ -135,12 +135,12 @@ public void testWithPrototype() throws Exception { pfb.setProperties(localProps); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); - assertEquals("99", props.getProperty("tb.array[0].age")); - assertEquals("value2", props.getProperty("key2")); + assertThat(props.getProperty("tb.array[0].age")).isEqualTo("99"); + assertThat(props.getProperty("key2")).isEqualTo("value2"); Properties newProps = pfb.getObject(); - assertTrue(props != newProps); - assertEquals("99", newProps.getProperty("tb.array[0].age")); - assertEquals("value2", newProps.getProperty("key2")); + assertThat(props != newProps).isTrue(); + assertThat(newProps.getProperty("tb.array[0].age")).isEqualTo("99"); + assertThat(newProps.getProperty("key2")).isEqualTo("value2"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests.java index 5aaf05bdb839..bc0c7cce84f7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -16,16 +16,16 @@ package org.springframework.beans.factory.config; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Unit tests for {@link PropertyPathFactoryBean}. @@ -43,43 +43,47 @@ public class PropertyPathFactoryBeanTests { public void testPropertyPathFactoryBeanWithSingletonResult() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT); - assertEquals(new Integer(12), xbf.getBean("propertyPath1")); - assertEquals(new Integer(11), xbf.getBean("propertyPath2")); - assertEquals(new Integer(10), xbf.getBean("tb.age")); - assertEquals(ITestBean.class, xbf.getType("otb.spouse")); + assertThat(xbf.getBean("propertyPath1")).isEqualTo(12); + assertThat(xbf.getBean("propertyPath2")).isEqualTo(11); + assertThat(xbf.getBean("tb.age")).isEqualTo(10); + assertThat(xbf.getType("otb.spouse")).isEqualTo(ITestBean.class); Object result1 = xbf.getBean("otb.spouse"); Object result2 = xbf.getBean("otb.spouse"); - assertTrue(result1 instanceof TestBean); - assertTrue(result1 == result2); - assertEquals(99, ((TestBean) result1).getAge()); + boolean condition = result1 instanceof TestBean; + assertThat(condition).isTrue(); + assertThat(result1 == result2).isTrue(); + assertThat(((TestBean) result1).getAge()).isEqualTo(99); } @Test public void testPropertyPathFactoryBeanWithPrototypeResult() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT); - assertNull(xbf.getType("tb.spouse")); - assertEquals(TestBean.class, xbf.getType("propertyPath3")); + assertThat(xbf.getType("tb.spouse")).isNull(); + assertThat(xbf.getType("propertyPath3")).isEqualTo(TestBean.class); Object result1 = xbf.getBean("tb.spouse"); Object result2 = xbf.getBean("propertyPath3"); Object result3 = xbf.getBean("propertyPath3"); - assertTrue(result1 instanceof TestBean); - assertTrue(result2 instanceof TestBean); - assertTrue(result3 instanceof TestBean); - assertEquals(11, ((TestBean) result1).getAge()); - assertEquals(11, ((TestBean) result2).getAge()); - assertEquals(11, ((TestBean) result3).getAge()); - assertTrue(result1 != result2); - assertTrue(result1 != result3); - assertTrue(result2 != result3); + boolean condition2 = result1 instanceof TestBean; + assertThat(condition2).isTrue(); + boolean condition1 = result2 instanceof TestBean; + assertThat(condition1).isTrue(); + boolean condition = result3 instanceof TestBean; + assertThat(condition).isTrue(); + assertThat(((TestBean) result1).getAge()).isEqualTo(11); + assertThat(((TestBean) result2).getAge()).isEqualTo(11); + assertThat(((TestBean) result3).getAge()).isEqualTo(11); + assertThat(result1 != result2).isTrue(); + assertThat(result1 != result3).isTrue(); + assertThat(result2 != result3).isTrue(); } @Test public void testPropertyPathFactoryBeanWithNullResult() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT); - assertNull(xbf.getType("tb.spouse.spouse")); - assertEquals("null", xbf.getBean("tb.spouse.spouse").toString()); + assertThat(xbf.getType("tb.spouse.spouse")).isNull(); + assertThat(xbf.getBean("tb.spouse.spouse").toString()).isEqualTo("null"); } @Test @@ -88,23 +92,24 @@ public void testPropertyPathFactoryBeanAsInnerBean() { new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT); TestBean spouse = (TestBean) xbf.getBean("otb.spouse"); TestBean tbWithInner = (TestBean) xbf.getBean("tbWithInner"); - assertSame(spouse, tbWithInner.getSpouse()); - assertTrue(!tbWithInner.getFriends().isEmpty()); - assertSame(spouse, tbWithInner.getFriends().iterator().next()); + assertThat(tbWithInner.getSpouse()).isSameAs(spouse); + boolean condition = !tbWithInner.getFriends().isEmpty(); + assertThat(condition).isTrue(); + assertThat(tbWithInner.getFriends().iterator().next()).isSameAs(spouse); } @Test public void testPropertyPathFactoryBeanAsNullReference() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT); - assertNull(xbf.getBean("tbWithNullReference", TestBean.class).getSpouse()); + assertThat(xbf.getBean("tbWithNullReference", TestBean.class).getSpouse()).isNull(); } @Test public void testPropertyPathFactoryBeanAsInnerNull() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT); - assertNull(xbf.getBean("tbWithInnerNull", TestBean.class).getSpouse()); + assertThat(xbf.getBean("tbWithInnerNull", TestBean.class).getSpouse()).isNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java index e5cc670775a9..13d8b138ff43 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,32 +18,32 @@ import java.util.Properties; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.TestBean; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; -import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; +import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerWithGeneratedName; /** * Unit tests for {@link PropertyPlaceholderConfigurer}. * * @author Chris Beams */ +@SuppressWarnings("deprecation") public class PropertyPlaceholderConfigurerTests { private static final String P1 = "p1"; private static final String P1_LOCAL_PROPS_VAL = "p1LocalPropsVal"; private static final String P1_SYSTEM_PROPS_VAL = "p1SystemPropsVal"; - private static final String P1_SYSTEM_ENV_VAL = "p1SystemEnvVal"; private DefaultListableBeanFactory bf; private PropertyPlaceholderConfigurer ppc; @@ -52,7 +52,7 @@ public class PropertyPlaceholderConfigurerTests { private AbstractBeanDefinition p1BeanDef; - @Before + @BeforeEach public void setup() { p1BeanDef = rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${" + P1 + "}") @@ -68,7 +68,7 @@ public void setup() { } - @After + @AfterEach public void cleanup() { System.clearProperty(P1); } @@ -98,8 +98,8 @@ public void resolveFromSystemProperties() { registerWithGeneratedName(p1BeanDef, bf); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); - assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); - assertThat(bean.getSex(), equalTo("systemValue")); + assertThat(bean.getName()).isEqualTo(P1_LOCAL_PROPS_VAL); + assertThat(bean.getSex()).isEqualTo("systemValue"); System.clearProperty("otherKey"); } @@ -109,7 +109,7 @@ public void resolveFromLocalProperties() { registerWithGeneratedName(p1BeanDef, bf); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); - assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); + assertThat(bean.getName()).isEqualTo(P1_LOCAL_PROPS_VAL); } @Test @@ -117,7 +117,7 @@ public void setSystemPropertiesMode_defaultIsFallback() { registerWithGeneratedName(p1BeanDef, bf); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); - assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); + assertThat(bean.getName()).isEqualTo(P1_LOCAL_PROPS_VAL); } @Test @@ -126,7 +126,7 @@ public void setSystemSystemPropertiesMode_toOverride_andResolveFromSystemPropert ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); - assertThat(bean.getName(), equalTo(P1_SYSTEM_PROPS_VAL)); + assertThat(bean.getName()).isEqualTo(P1_SYSTEM_PROPS_VAL); } @Test @@ -137,7 +137,7 @@ public void setSystemSystemPropertiesMode_toOverride_andSetSearchSystemEnvironme ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); - assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); // has to resort to local props + assertThat(bean.getName()).isEqualTo(P1_LOCAL_PROPS_VAL); // has to resort to local props } /** @@ -149,7 +149,6 @@ public void twoPlaceholderConfigurers_withConflictingSettings() { String P2 = "p2"; String P2_LOCAL_PROPS_VAL = "p2LocalPropsVal"; String P2_SYSTEM_PROPS_VAL = "p2SystemPropsVal"; - String P2_SYSTEM_ENV_VAL = "p2SystemEnvVal"; AbstractBeanDefinition p2BeanDef = rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${" + P1 + "}") @@ -175,11 +174,11 @@ public void twoPlaceholderConfigurers_withConflictingSettings() { ppc2.postProcessBeanFactory(bf); TestBean p1Bean = bf.getBean("p1Bean", TestBean.class); - assertThat(p1Bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); + assertThat(p1Bean.getName()).isEqualTo(P1_LOCAL_PROPS_VAL); TestBean p2Bean = bf.getBean("p2Bean", TestBean.class); - assertThat(p2Bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); - assertThat(p2Bean.getCountry(), equalTo(P2_SYSTEM_PROPS_VAL)); + assertThat(p2Bean.getName()).isEqualTo(P1_LOCAL_PROPS_VAL); + assertThat(p2Bean.getCountry()).isEqualTo(P2_SYSTEM_PROPS_VAL); System.clearProperty(P2); } @@ -203,8 +202,8 @@ public void customPlaceholderPrefixAndSuffix() { System.clearProperty("key1"); System.clearProperty("key2"); - assertThat(bf.getBean(TestBean.class).getName(), is("systemKey1Value")); - assertThat(bf.getBean(TestBean.class).getSex(), is("${key2}")); + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("systemKey1Value"); + assertThat(bf.getBean(TestBean.class).getSex()).isEqualTo("${key2}"); } @Test @@ -217,7 +216,7 @@ public void nullValueIsPreserved() { .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); - assertThat(bf.getBean(TestBean.class).getName(), nullValue()); + assertThat(bf.getBean(TestBean.class).getName()).isNull(); System.clearProperty("my.name"); } @@ -230,7 +229,7 @@ public void trimValuesIsOffByDefault() { .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); - assertThat(bf.getBean(TestBean.class).getName(), equalTo(" myValue ")); + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo(" myValue "); System.clearProperty("my.name"); } @@ -244,7 +243,7 @@ public void trimValuesIsApplied() { .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); - assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue")); + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("myValue"); System.clearProperty("my.name"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java index 23206d7c4b87..5dd76865de42 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -27,7 +27,7 @@ import java.util.prefs.Preferences; import java.util.prefs.PreferencesFactory; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.factory.BeanDefinitionStoreException; @@ -39,14 +39,15 @@ import org.springframework.beans.factory.support.ManagedMap; import org.springframework.beans.factory.support.ManagedSet; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.testfixture.beans.IndexedTestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.IndexedTestBean; -import org.springframework.tests.sample.beans.TestBean; import org.springframework.util.StringUtils; -import static org.junit.Assert.*; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Unit tests for various {@link PropertyResourceConfigurer} implementations including: @@ -59,6 +60,7 @@ * @since 02.10.2003 * @see PropertyPlaceholderConfigurerTests */ +@SuppressWarnings("deprecation") public class PropertyResourceConfigurerTests { static { @@ -107,10 +109,10 @@ public void testPropertyOverrideConfigurer() { TestBean tb1 = (TestBean) factory.getBean("tb1"); TestBean tb2 = (TestBean) factory.getBean("tb2"); - assertEquals(99, tb1.getAge()); - assertEquals(99, tb2.getAge()); - assertEquals(null, tb1.getName()); - assertEquals("test", tb2.getName()); + assertThat(tb1.getAge()).isEqualTo(99); + assertThat(tb2.getAge()).isEqualTo(99); + assertThat(tb1.getName()).isEqualTo(null); + assertThat(tb2.getName()).isEqualTo("test"); } @Test @@ -127,8 +129,8 @@ public void testPropertyOverrideConfigurerWithNestedProperty() { poc.postProcessBeanFactory(factory); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); - assertEquals(99, tb.getArray()[0].getAge()); - assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); + assertThat(tb.getArray()[0].getAge()).isEqualTo(99); + assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("test"); } @Test @@ -146,8 +148,8 @@ public void testPropertyOverrideConfigurerWithNestedPropertyAndDotInBeanName() { poc.postProcessBeanFactory(factory); IndexedTestBean tb = (IndexedTestBean) factory.getBean("my.tb"); - assertEquals(99, tb.getArray()[0].getAge()); - assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); + assertThat(tb.getArray()[0].getAge()).isEqualTo(99); + assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("test"); } @Test @@ -164,8 +166,8 @@ public void testPropertyOverrideConfigurerWithNestedMapPropertyAndDotInMapKey() poc.postProcessBeanFactory(factory); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); - assertEquals("99", tb.getMap().get("key1")); - assertEquals("test", tb.getMap().get("key2.ext")); + assertThat(tb.getMap().get("key1")).isEqualTo("99"); + assertThat(tb.getMap().get("key2.ext")).isEqualTo("test"); } @Test @@ -181,7 +183,7 @@ public void testPropertyOverrideConfigurerWithHeldProperties() { poc.postProcessBeanFactory(factory); PropertiesHolder tb = (PropertiesHolder) factory.getBean("tb"); - assertEquals("true", tb.getHeldProperties().getProperty("mail.smtp.auth")); + assertThat(tb.getHeldProperties().getProperty("mail.smtp.auth")).isEqualTo("true"); } @Test @@ -194,8 +196,8 @@ public void testPropertyOverrideConfigurerWithPropertiesFile() { poc.postProcessBeanFactory(factory); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); - assertEquals(99, tb.getArray()[0].getAge()); - assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); + assertThat(tb.getArray()[0].getAge()).isEqualTo(99); + assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("test"); } @Test @@ -209,8 +211,8 @@ public void testPropertyOverrideConfigurerWithInvalidPropertiesFile() { poc.postProcessBeanFactory(factory); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); - assertEquals(99, tb.getArray()[0].getAge()); - assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); + assertThat(tb.getArray()[0].getAge()).isEqualTo(99); + assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("test"); } @Test @@ -223,8 +225,8 @@ public void testPropertyOverrideConfigurerWithPropertiesXmlFile() { poc.postProcessBeanFactory(factory); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); - assertEquals(99, tb.getArray()[0].getAge()); - assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); + assertThat(tb.getArray()[0].getAge()).isEqualTo(99); + assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("test"); } @Test @@ -240,8 +242,8 @@ public void testPropertyOverrideConfigurerWithConvertProperties() { bfpp.postProcessBeanFactory(factory); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); - assertEquals("X99", tb.getArray()[0].getName()); - assertEquals("Xtest", ((TestBean) tb.getList().get(1)).getName()); + assertThat(tb.getArray()[0].getName()).isEqualTo("X99"); + assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("Xtest"); } @Test @@ -259,7 +261,7 @@ public void testPropertyOverrideConfigurerWithInvalidKey() { props.setProperty("tb3.name", "test"); poc.setProperties(props); poc.postProcessBeanFactory(factory); - assertEquals("test", factory.getBean("tb2", TestBean.class).getName()); + assertThat(factory.getBean("tb2", TestBean.class).getName()).isEqualTo("test"); } { PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer(); @@ -274,7 +276,7 @@ public void testPropertyOverrideConfigurerWithInvalidKey() { } catch (BeanInitializationException ex) { // prove that the processor chokes on the invalid key - assertTrue(ex.getMessage().toLowerCase().contains("argh")); + assertThat(ex.getMessage().toLowerCase().contains("argh")).isTrue(); } } } @@ -306,10 +308,10 @@ public void testPropertyOverrideConfigurerWithIgnoreInvalidKeys() { TestBean tb1 = (TestBean) factory.getBean("tb1"); TestBean tb2 = (TestBean) factory.getBean("tb2"); - assertEquals(99, tb1.getAge()); - assertEquals(99, tb2.getAge()); - assertEquals(null, tb1.getName()); - assertEquals("test", tb2.getName()); + assertThat(tb1.getAge()).isEqualTo(99); + assertThat(tb2.getAge()).isEqualTo(99); + assertThat(tb1.getName()).isEqualTo(null); + assertThat(tb2.getName()).isEqualTo("test"); } @Test @@ -323,7 +325,7 @@ public void testPropertyPlaceholderConfigurerWithParentChildSeparation() { } private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) { - Map singletonMap = Collections.singletonMap("myKey", "myValue"); + Map singletonMap = Collections.singletonMap("myKey", "myValue"); if (parentChildSeparation) { MutablePropertyValues pvs1 = new MutablePropertyValues(); pvs1.add("age", "${age}"); @@ -396,36 +398,36 @@ private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) TestBean tb1 = (TestBean) factory.getBean("tb1"); TestBean tb2 = (TestBean) factory.getBean("tb2"); - assertEquals(98, tb1.getAge()); - assertEquals(98, tb2.getAge()); - assertEquals("namemyvarmyvar${", tb1.getName()); - assertEquals("myvarname98", tb2.getName()); - assertEquals(tb2, tb1.getSpouse()); - assertEquals(1, tb1.getSomeMap().size()); - assertEquals("myValue", tb1.getSomeMap().get("myKey")); - assertEquals(2, tb2.getStringArray().length); - assertEquals(System.getProperty("os.name"), tb2.getStringArray()[0]); - assertEquals("98", tb2.getStringArray()[1]); - assertEquals(2, tb2.getFriends().size()); - assertEquals("na98me", tb2.getFriends().iterator().next()); - assertEquals(tb2, tb2.getFriends().toArray()[1]); - assertEquals(3, tb2.getSomeSet().size()); - assertTrue(tb2.getSomeSet().contains("na98me")); - assertTrue(tb2.getSomeSet().contains(tb2)); - assertTrue(tb2.getSomeSet().contains(new Integer(98))); - assertEquals(6, tb2.getSomeMap().size()); - assertEquals("98", tb2.getSomeMap().get("key98")); - assertEquals(tb2, tb2.getSomeMap().get("key98ref")); - assertEquals(tb2, tb2.getSomeMap().get("key1")); - assertEquals("98name", tb2.getSomeMap().get("key2")); + assertThat(tb1.getAge()).isEqualTo(98); + assertThat(tb2.getAge()).isEqualTo(98); + assertThat(tb1.getName()).isEqualTo("namemyvarmyvar${"); + assertThat(tb2.getName()).isEqualTo("myvarname98"); + assertThat(tb1.getSpouse()).isEqualTo(tb2); + assertThat(tb1.getSomeMap().size()).isEqualTo(1); + assertThat(tb1.getSomeMap().get("myKey")).isEqualTo("myValue"); + assertThat(tb2.getStringArray().length).isEqualTo(2); + assertThat(tb2.getStringArray()[0]).isEqualTo(System.getProperty("os.name")); + assertThat(tb2.getStringArray()[1]).isEqualTo("98"); + assertThat(tb2.getFriends().size()).isEqualTo(2); + assertThat(tb2.getFriends().iterator().next()).isEqualTo("na98me"); + assertThat(tb2.getFriends().toArray()[1]).isEqualTo(tb2); + assertThat(tb2.getSomeSet().size()).isEqualTo(3); + assertThat(tb2.getSomeSet().contains("na98me")).isTrue(); + assertThat(tb2.getSomeSet().contains(tb2)).isTrue(); + assertThat(tb2.getSomeSet().contains(98)).isTrue(); + assertThat(tb2.getSomeMap().size()).isEqualTo(6); + assertThat(tb2.getSomeMap().get("key98")).isEqualTo("98"); + assertThat(tb2.getSomeMap().get("key98ref")).isEqualTo(tb2); + assertThat(tb2.getSomeMap().get("key1")).isEqualTo(tb2); + assertThat(tb2.getSomeMap().get("key2")).isEqualTo("98name"); TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3"); TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4"); - assertEquals(0, inner1.getAge()); - assertEquals(null, inner1.getName()); - assertEquals(System.getProperty("os.name"), inner1.getCountry()); - assertEquals(98, inner2.getAge()); - assertEquals("namemyvarmyvar${", inner2.getName()); - assertEquals(System.getProperty("os.name"), inner2.getCountry()); + assertThat(inner1.getAge()).isEqualTo(0); + assertThat(inner1.getName()).isEqualTo(null); + assertThat(inner1.getCountry()).isEqualTo(System.getProperty("os.name")); + assertThat(inner2.getAge()).isEqualTo(98); + assertThat(inner2.getName()).isEqualTo("namemyvarmyvar${"); + assertThat(inner2.getCountry()).isEqualTo(System.getProperty("os.name")); } @Test @@ -437,7 +439,7 @@ public void testPropertyPlaceholderConfigurerWithSystemPropertyFallback() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals(System.getProperty("os.name"), tb.getCountry()); + assertThat(tb.getCountry()).isEqualTo(System.getProperty("os.name")); } @Test @@ -452,7 +454,7 @@ public void testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("myos", tb.getCountry()); + assertThat(tb.getCountry()).isEqualTo("myos"); } @Test @@ -468,42 +470,28 @@ public void testPropertyPlaceholderConfigurerWithOverridingSystemProperty() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals(System.getProperty("os.name"), tb.getCountry()); + assertThat(tb.getCountry()).isEqualTo(System.getProperty("os.name")); } @Test public void testPropertyPlaceholderConfigurerWithUnresolvableSystemProperty() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("touchy", "${user.dir}").getBeanDefinition()); - PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_NEVER); - - try { - ppc.postProcessBeanFactory(factory); - fail("Should have thrown BeanDefinitionStoreException"); - } - catch (BeanDefinitionStoreException ex) { - // expected - assertTrue(ex.getMessage().contains("user.dir")); - } + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + ppc.postProcessBeanFactory(factory)) + .withMessageContaining("user.dir"); } @Test public void testPropertyPlaceholderConfigurerWithUnresolvablePlaceholder() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${ref}").getBeanDefinition()); - PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); - - try { - ppc.postProcessBeanFactory(factory); - fail("Should have thrown BeanDefinitionStoreException"); - } - catch (BeanDefinitionStoreException ex) { - // expected - assertTrue(ex.getMessage().contains("ref")); - } + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + ppc.postProcessBeanFactory(factory)) + .withMessageContaining("ref"); } @Test @@ -516,7 +504,7 @@ public void testPropertyPlaceholderConfigurerWithIgnoreUnresolvablePlaceholder() ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("${ref}", tb.getName()); + assertThat(tb.getName()).isEqualTo("${ref}"); } @Test @@ -529,7 +517,7 @@ public void testPropertyPlaceholderConfigurerWithEmptyStringAsNull() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertNull(tb.getName()); + assertThat(tb.getName()).isNull(); } @Test @@ -545,7 +533,7 @@ public void testPropertyPlaceholderConfigurerWithEmptyStringInPlaceholderAsNull( ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertNull(tb.getName()); + assertThat(tb.getName()).isNull(); } @Test @@ -561,7 +549,7 @@ public void testPropertyPlaceholderConfigurerWithNestedPlaceholderInKey() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("myname", tb.getName()); + assertThat(tb.getName()).isEqualTo("myname"); } @Test @@ -577,7 +565,7 @@ public void testPropertyPlaceholderConfigurerWithPlaceholderInAlias() { TestBean tb = (TestBean) factory.getBean("tb"); TestBean tb2 = (TestBean) factory.getBean("tb2"); - assertSame(tb, tb2); + assertThat(tb2).isSameAs(tb); } @Test @@ -592,8 +580,8 @@ public void testPropertyPlaceholderConfigurerWithSelfReferencingPlaceholderInAli ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertNotNull(tb); - assertEquals(0, factory.getAliases("tb").length); + assertThat(tb).isNotNull(); + assertThat(factory.getAliases("tb").length).isEqualTo(0); } @Test @@ -609,14 +597,8 @@ public void testPropertyPlaceholderConfigurerWithCircularReference() { props.setProperty("var", "${m}"); props.setProperty("m", "${var}"); ppc.setProperties(props); - - try { - ppc.postProcessBeanFactory(factory); - fail("Should have thrown BeanDefinitionStoreException"); - } - catch (BeanDefinitionStoreException ex) { - // expected - } + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + ppc.postProcessBeanFactory(factory)); } @Test @@ -631,7 +613,7 @@ public void testPropertyPlaceholderConfigurerWithDefaultProperties() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("mytest", tb.getTouchy()); + assertThat(tb.getTouchy()).isEqualTo("mytest"); } @Test @@ -643,7 +625,7 @@ public void testPropertyPlaceholderConfigurerWithInlineDefault() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("mytest", tb.getTouchy()); + assertThat(tb.getTouchy()).isEqualTo("mytest"); } @Test @@ -663,11 +645,11 @@ public void testPropertyPlaceholderConfigurerWithAliases() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("mytest", tb.getTouchy()); + assertThat(tb.getTouchy()).isEqualTo("mytest"); tb = (TestBean) factory.getBean("alias"); - assertEquals("mytest", tb.getTouchy()); + assertThat(tb.getTouchy()).isEqualTo("mytest"); tb = (TestBean) factory.getBean("alias2"); - assertEquals("mytest", tb.getTouchy()); + assertThat(tb.getTouchy()).isEqualTo("mytest"); } @Test @@ -689,9 +671,9 @@ public void testPreferencesPlaceholderConfigurer() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("myNameValue", tb.getName()); - assertEquals(99, tb.getAge()); - assertEquals("myOtherTouchyValue", tb.getTouchy()); + assertThat(tb.getName()).isEqualTo("myNameValue"); + assertThat(tb.getAge()).isEqualTo(99); + assertThat(tb.getTouchy()).isEqualTo("myOtherTouchyValue"); Preferences.userRoot().remove("myTouchy"); Preferences.systemRoot().remove("myTouchy"); Preferences.systemRoot().remove("myName"); @@ -718,9 +700,9 @@ public void testPreferencesPlaceholderConfigurerWithCustomTreePaths() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("myNameValue", tb.getName()); - assertEquals(99, tb.getAge()); - assertEquals("myOtherTouchyValue", tb.getTouchy()); + assertThat(tb.getName()).isEqualTo("myNameValue"); + assertThat(tb.getAge()).isEqualTo(99); + assertThat(tb.getTouchy()).isEqualTo("myOtherTouchyValue"); Preferences.userRoot().node("myUserPath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath").remove("myName"); @@ -747,9 +729,9 @@ public void testPreferencesPlaceholderConfigurerWithPathInPlaceholder() { ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); - assertEquals("myNameValue", tb.getName()); - assertEquals(99, tb.getAge()); - assertEquals("myOtherTouchyValue", tb.getTouchy()); + assertThat(tb.getName()).isEqualTo("myNameValue"); + assertThat(tb.getAge()).isEqualTo(99); + assertThat(tb.getTouchy()).isEqualTo("myOtherTouchyValue"); Preferences.userRoot().node("myUserPath/myotherpath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath/myotherpath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath/mypath").remove("myName"); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBeanTests.java index 0b704b8df6f5..87139e363e3e 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,9 @@ package org.springframework.beans.factory.config; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.beans.FatalBeanException; import org.springframework.beans.factory.BeanFactory; @@ -27,9 +27,11 @@ import org.springframework.core.NestedCheckedException; import org.springframework.core.NestedRuntimeException; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.Mockito.mock; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition; /** * Unit tests for {@link ServiceLocatorFactoryBean}. @@ -42,7 +44,7 @@ public class ServiceLocatorFactoryBeanTests { private DefaultListableBeanFactory bf; - @Before + @BeforeEach public void setUp() { bf = new DefaultListableBeanFactory(); } @@ -57,7 +59,7 @@ public void testNoArgGetter() { TestServiceLocator factory = (TestServiceLocator) bf.getBean("factory"); TestService testService = factory.getTestService(); - assertNotNull(testService); + assertThat(testService).isNotNull(); } @Test @@ -76,27 +78,12 @@ public void testErrorOnTooManyOrTooFew() throws Exception { genericBeanDefinition(ServiceLocatorFactoryBean.class) .addPropertyValue("serviceLocatorInterface", TestService2Locator.class) .getBeanDefinition()); - - try { - TestServiceLocator factory = (TestServiceLocator) bf.getBean("factory"); - factory.getTestService(); - fail("Must fail on more than one matching type"); - } - catch (NoSuchBeanDefinitionException ex) { /* expected */ } - - try { - TestServiceLocator2 factory = (TestServiceLocator2) bf.getBean("factory2"); - factory.getTestService(null); - fail("Must fail on more than one matching type"); - } - catch (NoSuchBeanDefinitionException ex) { /* expected */ } - - try { - TestService2Locator factory = (TestService2Locator) bf.getBean("factory3"); - factory.getTestService(); - fail("Must fail on no matching types"); - } - catch (NoSuchBeanDefinitionException ex) { /* expected */ } + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).as("more than one matching type").isThrownBy(() -> + ((TestServiceLocator) bf.getBean("factory")).getTestService()); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).as("more than one matching type").isThrownBy(() -> + ((TestServiceLocator2) bf.getBean("factory2")).getTestService(null)); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).as("no matching types").isThrownBy(() -> + ((TestService2Locator) bf.getBean("factory3")).getTestService()); } @Test @@ -118,31 +105,14 @@ public void testErrorOnTooManyOrTooFewWithCustomServiceLocatorException() { .addPropertyValue("serviceLocatorInterface", TestService2Locator.class) .addPropertyValue("serviceLocatorExceptionClass", CustomServiceLocatorException3.class) .getBeanDefinition()); - - try { - TestServiceLocator factory = (TestServiceLocator) bf.getBean("factory"); - factory.getTestService(); - fail("Must fail on more than one matching type"); - } - catch (CustomServiceLocatorException1 expected) { - assertTrue(expected.getCause() instanceof NoSuchBeanDefinitionException); - } - - try { - TestServiceLocator2 factory2 = (TestServiceLocator2) bf.getBean("factory2"); - factory2.getTestService(null); - fail("Must fail on more than one matching type"); - } - catch (CustomServiceLocatorException2 expected) { - assertTrue(expected.getCause() instanceof NoSuchBeanDefinitionException); - } - - try { - TestService2Locator factory3 = (TestService2Locator) bf.getBean("factory3"); - factory3.getTestService(); - fail("Must fail on no matching type"); - } - catch (CustomServiceLocatorException3 ex) { /* expected */ } + assertThatExceptionOfType(CustomServiceLocatorException1.class).as("more than one matching type").isThrownBy(() -> + ((TestServiceLocator) bf.getBean("factory")).getTestService()) + .withCauseInstanceOf(NoSuchBeanDefinitionException.class); + assertThatExceptionOfType(CustomServiceLocatorException2.class).as("more than one matching type").isThrownBy(() -> + ((TestServiceLocator2) bf.getBean("factory2")).getTestService(null)) + .withCauseInstanceOf(NoSuchBeanDefinitionException.class); + assertThatExceptionOfType(CustomServiceLocatorException3.class).as("no matching type").isThrownBy(() -> + ((TestService2Locator) bf.getBean("factory3")).getTestService()); } @Test @@ -161,14 +131,11 @@ public void testStringArgGetter() throws Exception { // now test with explicit id testBean = factory.getTestService("testService"); // now verify failure on bad id - try { - factory.getTestService("bogusTestService"); - fail("Illegal operation allowed"); - } - catch (NoSuchBeanDefinitionException ex) { /* expected */ } + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + factory.getTestService("bogusTestService")); } - @Ignore @Test // worked when using an ApplicationContext (see commented), fails when using BeanFactory + @Disabled @Test // worked when using an ApplicationContext (see commented), fails when using BeanFactory public void testCombinedLocatorInterface() { bf.registerBeanDefinition("testService", genericBeanDefinition(TestService.class).getBeanDefinition()); bf.registerAlias("testService", "1"); @@ -191,17 +158,17 @@ public void testCombinedLocatorInterface() { TestService testBean2 = factory.getTestService("testService"); TestService testBean3 = factory.getTestService(1); TestService testBean4 = factory.someFactoryMethod(); - assertNotSame(testBean1, testBean2); - assertNotSame(testBean1, testBean3); - assertNotSame(testBean1, testBean4); - assertNotSame(testBean2, testBean3); - assertNotSame(testBean2, testBean4); - assertNotSame(testBean3, testBean4); - - assertTrue(factory.toString().indexOf("TestServiceLocator3") != -1); + assertThat(testBean2).isNotSameAs(testBean1); + assertThat(testBean3).isNotSameAs(testBean1); + assertThat(testBean4).isNotSameAs(testBean1); + assertThat(testBean3).isNotSameAs(testBean2); + assertThat(testBean4).isNotSameAs(testBean2); + assertThat(testBean4).isNotSameAs(testBean3); + + assertThat(factory.toString().contains("TestServiceLocator3")).isTrue(); } - @Ignore @Test // worked when using an ApplicationContext (see commented), fails when using BeanFactory + @Disabled @Test // worked when using an ApplicationContext (see commented), fails when using BeanFactory public void testServiceMappings() { bf.registerBeanDefinition("testService1", genericBeanDefinition(TestService.class).getBeanDefinition()); bf.registerBeanDefinition("testService2", genericBeanDefinition(ExtendedTestService.class).getBeanDefinition()); @@ -225,53 +192,62 @@ public void testServiceMappings() { TestService testBean2 = factory.getTestService("testService1"); TestService testBean3 = factory.getTestService(1); TestService testBean4 = factory.getTestService(2); - assertNotSame(testBean1, testBean2); - assertNotSame(testBean1, testBean3); - assertNotSame(testBean1, testBean4); - assertNotSame(testBean2, testBean3); - assertNotSame(testBean2, testBean4); - assertNotSame(testBean3, testBean4); - assertFalse(testBean1 instanceof ExtendedTestService); - assertFalse(testBean2 instanceof ExtendedTestService); - assertFalse(testBean3 instanceof ExtendedTestService); - assertTrue(testBean4 instanceof ExtendedTestService); + assertThat(testBean2).isNotSameAs(testBean1); + assertThat(testBean3).isNotSameAs(testBean1); + assertThat(testBean4).isNotSameAs(testBean1); + assertThat(testBean3).isNotSameAs(testBean2); + assertThat(testBean4).isNotSameAs(testBean2); + assertThat(testBean4).isNotSameAs(testBean3); + boolean condition3 = testBean1 instanceof ExtendedTestService; + assertThat(condition3).isFalse(); + boolean condition2 = testBean2 instanceof ExtendedTestService; + assertThat(condition2).isFalse(); + boolean condition1 = testBean3 instanceof ExtendedTestService; + assertThat(condition1).isFalse(); + boolean condition = testBean4 instanceof ExtendedTestService; + assertThat(condition).isTrue(); } - @Test(expected = IllegalArgumentException.class) + @Test public void testNoServiceLocatorInterfaceSupplied() throws Exception { - new ServiceLocatorFactoryBean().afterPropertiesSet(); + assertThatIllegalArgumentException().isThrownBy( + new ServiceLocatorFactoryBean()::afterPropertiesSet); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWhenServiceLocatorInterfaceIsNotAnInterfaceType() throws Exception { ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean(); factory.setServiceLocatorInterface(getClass()); - factory.afterPropertiesSet(); + assertThatIllegalArgumentException().isThrownBy( + factory::afterPropertiesSet); // should throw, bad (non-interface-type) serviceLocator interface supplied } - @Test(expected = IllegalArgumentException.class) + @Test public void testWhenServiceLocatorExceptionClassToExceptionTypeWithOnlyNoArgCtor() throws Exception { ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean(); - factory.setServiceLocatorExceptionClass(ExceptionClassWithOnlyZeroArgCtor.class); + assertThatIllegalArgumentException().isThrownBy(() -> + factory.setServiceLocatorExceptionClass(ExceptionClassWithOnlyZeroArgCtor.class)); // should throw, bad (invalid-Exception-type) serviceLocatorException class supplied } - @Test(expected = IllegalArgumentException.class) - @SuppressWarnings("unchecked") + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testWhenServiceLocatorExceptionClassIsNotAnExceptionSubclass() throws Exception { ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean(); - factory.setServiceLocatorExceptionClass((Class) getClass()); + assertThatIllegalArgumentException().isThrownBy(() -> + factory.setServiceLocatorExceptionClass((Class) getClass())); // should throw, bad (non-Exception-type) serviceLocatorException class supplied } - @Test(expected = UnsupportedOperationException.class) + @Test public void testWhenServiceLocatorMethodCalledWithTooManyParameters() throws Exception { ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean(); factory.setServiceLocatorInterface(ServiceLocatorInterfaceWithExtraNonCompliantMethod.class); factory.afterPropertiesSet(); ServiceLocatorInterfaceWithExtraNonCompliantMethod locator = (ServiceLocatorInterfaceWithExtraNonCompliantMethod) factory.getObject(); - locator.getTestService("not", "allowed"); //bad method (too many args, doesn't obey class contract) + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + locator.getTestService("not", "allowed")); //bad method (too many args, doesn't obey class contract) } @Test diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/SimpleScopeTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/SimpleScopeTests.java index 2a81acb06fa2..38b10b562f5c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/SimpleScopeTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/SimpleScopeTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,17 +19,16 @@ import java.util.LinkedList; import java.util.List; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * Simple test to illustrate and verify scope usage. @@ -40,12 +39,11 @@ */ public class SimpleScopeTests { - private static final Resource CONTEXT = qualifiedResource(SimpleScopeTests.class, "context.xml"); - private DefaultListableBeanFactory beanFactory; - @Before - public void setUp() { + + @BeforeEach + public void setup() { beanFactory = new DefaultListableBeanFactory(); Scope scope = new NoOpScope() { private int index; @@ -65,21 +63,22 @@ public Object get(String name, ObjectFactory objectFactory) { beanFactory.registerScope("myScope", scope); String[] scopeNames = beanFactory.getRegisteredScopeNames(); - assertEquals(1, scopeNames.length); - assertEquals("myScope", scopeNames[0]); - assertSame(scope, beanFactory.getRegisteredScope("myScope")); + assertThat(scopeNames.length).isEqualTo(1); + assertThat(scopeNames[0]).isEqualTo("myScope"); + assertThat(beanFactory.getRegisteredScope("myScope")).isSameAs(scope); - XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory); - xbdr.loadBeanDefinitions(CONTEXT); + new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions( + qualifiedResource(SimpleScopeTests.class, "context.xml")); } + @Test public void testCanGetScopedObject() { TestBean tb1 = (TestBean) beanFactory.getBean("usesScope"); TestBean tb2 = (TestBean) beanFactory.getBean("usesScope"); - assertNotSame(tb1, tb2); + assertThat(tb2).isNotSameAs(tb1); TestBean tb3 = (TestBean) beanFactory.getBean("usesScope"); - assertSame(tb3, tb1); + assertThat(tb1).isSameAs(tb3); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/TestTypes.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/TestTypes.java index e9ff4c6a3155..164718deb727 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/TestTypes.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/TestTypes.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java index fa07de82e4e3..b7e05ed64747 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,14 +21,16 @@ import java.util.LinkedHashMap; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.yaml.snakeyaml.constructor.DuplicateKeyException; import org.springframework.core.io.AbstractResource; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.FileSystemResource; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * Tests for {@link YamlMapFactoryBean}. @@ -45,19 +47,21 @@ public class YamlMapFactoryBeanTests { public void testSetIgnoreResourceNotFound() { this.factory.setResolutionMethod(YamlMapFactoryBean.ResolutionMethod.OVERRIDE_AND_IGNORE); this.factory.setResources(new FileSystemResource("non-exsitent-file.yml")); - assertEquals(0, this.factory.getObject().size()); + assertThat(this.factory.getObject().size()).isEqualTo(0); } - @Test(expected = IllegalStateException.class) + @Test public void testSetBarfOnResourceNotFound() { - this.factory.setResources(new FileSystemResource("non-exsitent-file.yml")); - assertEquals(0, this.factory.getObject().size()); + assertThatIllegalStateException().isThrownBy(() -> { + this.factory.setResources(new FileSystemResource("non-exsitent-file.yml")); + this.factory.getObject().size(); + }); } @Test public void testGetObject() { this.factory.setResources(new ByteArrayResource("foo: bar".getBytes())); - assertEquals(1, this.factory.getObject().size()); + assertThat(this.factory.getObject().size()).isEqualTo(1); } @SuppressWarnings("unchecked") @@ -66,8 +70,8 @@ public void testOverrideAndRemoveDefaults() { this.factory.setResources(new ByteArrayResource("foo:\n bar: spam".getBytes()), new ByteArrayResource("foo:\n spam: bar".getBytes())); - assertEquals(1, this.factory.getObject().size()); - assertEquals(2, ((Map) this.factory.getObject().get("foo")).size()); + assertThat(this.factory.getObject().size()).isEqualTo(1); + assertThat(((Map) this.factory.getObject().get("foo")).size()).isEqualTo(2); } @Test @@ -84,7 +88,7 @@ public InputStream getInputStream() throws IOException { } }, new ByteArrayResource("foo:\n spam: bar".getBytes())); - assertEquals(1, this.factory.getObject().size()); + assertThat(this.factory.getObject().size()).isEqualTo(1); } @Test @@ -92,14 +96,15 @@ public void testMapWithPeriodsInKey() { this.factory.setResources(new ByteArrayResource("foo:\n ? key1.key2\n : value".getBytes())); Map map = this.factory.getObject(); - assertEquals(1, map.size()); - assertTrue(map.containsKey("foo")); + assertThat(map.size()).isEqualTo(1); + assertThat(map.containsKey("foo")).isTrue(); Object object = map.get("foo"); - assertTrue(object instanceof LinkedHashMap); + boolean condition = object instanceof LinkedHashMap; + assertThat(condition).isTrue(); @SuppressWarnings("unchecked") Map sub = (Map) object; - assertTrue(sub.containsKey("key1.key2")); - assertEquals("value", sub.get("key1.key2")); + assertThat(sub.containsKey("key1.key2")).isTrue(); + assertThat(sub.get("key1.key2")).isEqualTo("value"); } @Test @@ -107,20 +112,22 @@ public void testMapWithIntegerValue() { this.factory.setResources(new ByteArrayResource("foo:\n ? key1.key2\n : 3".getBytes())); Map map = this.factory.getObject(); - assertEquals(1, map.size()); - assertTrue(map.containsKey("foo")); + assertThat(map.size()).isEqualTo(1); + assertThat(map.containsKey("foo")).isTrue(); Object object = map.get("foo"); - assertTrue(object instanceof LinkedHashMap); + boolean condition = object instanceof LinkedHashMap; + assertThat(condition).isTrue(); @SuppressWarnings("unchecked") Map sub = (Map) object; - assertEquals(1, sub.size()); - assertEquals(Integer.valueOf(3), sub.get("key1.key2")); + assertThat(sub.size()).isEqualTo(1); + assertThat(sub.get("key1.key2")).isEqualTo(Integer.valueOf(3)); } - @Test(expected = DuplicateKeyException.class) + @Test public void testDuplicateKey() { this.factory.setResources(new ByteArrayResource("mymap:\n foo: bar\nmymap:\n bar: foo".getBytes())); - this.factory.getObject().get("mymap"); + assertThatExceptionOfType(DuplicateKeyException.class).isThrownBy(() -> + this.factory.getObject().get("mymap")); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlProcessorTests.java index 1673e2caa056..60fbd272ce2c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlProcessorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -16,112 +16,164 @@ package org.springframework.beans.factory.config; +import java.net.URL; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.constructor.ConstructorException; import org.yaml.snakeyaml.parser.ParserException; import org.yaml.snakeyaml.scanner.ScannerException; import org.springframework.core.io.ByteArrayResource; -import static org.junit.Assert.*; +import static java.util.stream.Collectors.toList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.entry; /** * Tests for {@link YamlProcessor}. * * @author Dave Syer * @author Juergen Hoeller + * @author Sam Brannen */ -public class YamlProcessorTests { +class YamlProcessorTests { private final YamlProcessor processor = new YamlProcessor() {}; - @Rule - public ExpectedException exception = ExpectedException.none(); - @Test - public void arrayConvertedToIndexedBeanReference() { - this.processor.setResources(new ByteArrayResource("foo: bar\nbar: [1,2,3]".getBytes())); + void arrayConvertedToIndexedBeanReference() { + setYaml("foo: bar\nbar: [1,2,3]"); this.processor.process((properties, map) -> { - assertEquals(4, properties.size()); - assertEquals("bar", properties.get("foo")); - assertEquals("bar", properties.getProperty("foo")); - assertEquals(1, properties.get("bar[0]")); - assertEquals("1", properties.getProperty("bar[0]")); - assertEquals(2, properties.get("bar[1]")); - assertEquals("2", properties.getProperty("bar[1]")); - assertEquals(3, properties.get("bar[2]")); - assertEquals("3", properties.getProperty("bar[2]")); + assertThat(properties.size()).isEqualTo(4); + assertThat(properties.get("foo")).isEqualTo("bar"); + assertThat(properties.getProperty("foo")).isEqualTo("bar"); + assertThat(properties.get("bar[0]")).isEqualTo(1); + assertThat(properties.getProperty("bar[0]")).isEqualTo("1"); + assertThat(properties.get("bar[1]")).isEqualTo(2); + assertThat(properties.getProperty("bar[1]")).isEqualTo("2"); + assertThat(properties.get("bar[2]")).isEqualTo(3); + assertThat(properties.getProperty("bar[2]")).isEqualTo("3"); }); } @Test - public void testStringResource() { - this.processor.setResources(new ByteArrayResource("foo # a document that is a literal".getBytes())); - this.processor.process((properties, map) -> assertEquals("foo", map.get("document"))); + void stringResource() { + setYaml("foo # a document that is a literal"); + this.processor.process((properties, map) -> assertThat(map.get("document")).isEqualTo("foo")); } @Test - public void testBadDocumentStart() { - this.processor.setResources(new ByteArrayResource("foo # a document\nbar: baz".getBytes())); - this.exception.expect(ParserException.class); - this.exception.expectMessage("line 2, column 1"); - this.processor.process((properties, map) -> {}); + void badDocumentStart() { + setYaml("foo # a document\nbar: baz"); + assertThatExceptionOfType(ParserException.class) + .isThrownBy(() -> this.processor.process((properties, map) -> {})) + .withMessageContaining("line 2, column 1"); } @Test - public void testBadResource() { - this.processor.setResources(new ByteArrayResource("foo: bar\ncd\nspam:\n foo: baz".getBytes())); - this.exception.expect(ScannerException.class); - this.exception.expectMessage("line 3, column 1"); - this.processor.process((properties, map) -> {}); + void badResource() { + setYaml("foo: bar\ncd\nspam:\n foo: baz"); + assertThatExceptionOfType(ScannerException.class) + .isThrownBy(() -> this.processor.process((properties, map) -> {})) + .withMessageContaining("line 3, column 1"); } @Test - public void mapConvertedToIndexedBeanReference() { - this.processor.setResources(new ByteArrayResource("foo: bar\nbar:\n spam: bucket".getBytes())); + void mapConvertedToIndexedBeanReference() { + setYaml("foo: bar\nbar:\n spam: bucket"); this.processor.process((properties, map) -> { - assertEquals("bucket", properties.get("bar.spam")); - assertEquals(2, properties.size()); + assertThat(properties.get("bar.spam")).isEqualTo("bucket"); + assertThat(properties).hasSize(2); }); } @Test - public void integerKeyBehaves() { - this.processor.setResources(new ByteArrayResource("foo: bar\n1: bar".getBytes())); + void integerKeyBehaves() { + setYaml("foo: bar\n1: bar"); this.processor.process((properties, map) -> { - assertEquals("bar", properties.get("[1]")); - assertEquals(2, properties.size()); + assertThat(properties.get("[1]")).isEqualTo("bar"); + assertThat(properties).hasSize(2); }); } @Test - public void integerDeepKeyBehaves() { - this.processor.setResources(new ByteArrayResource("foo:\n 1: bar".getBytes())); + void integerDeepKeyBehaves() { + setYaml("foo:\n 1: bar"); this.processor.process((properties, map) -> { - assertEquals("bar", properties.get("foo[1]")); - assertEquals(1, properties.size()); + assertThat(properties.get("foo[1]")).isEqualTo("bar"); + assertThat(properties).hasSize(1); }); } @Test @SuppressWarnings("unchecked") - public void flattenedMapIsSameAsPropertiesButOrdered() { - this.processor.setResources(new ByteArrayResource("foo: bar\nbar:\n spam: bucket".getBytes())); + void flattenedMapIsSameAsPropertiesButOrdered() { + setYaml("cat: dog\nfoo: bar\nbar:\n spam: bucket"); this.processor.process((properties, map) -> { - assertEquals("bucket", properties.get("bar.spam")); - assertEquals(2, properties.size()); Map flattenedMap = processor.getFlattenedMap(map); - assertEquals("bucket", flattenedMap.get("bar.spam")); - assertEquals(2, flattenedMap.size()); - assertTrue(flattenedMap instanceof LinkedHashMap); + assertThat(flattenedMap).isInstanceOf(LinkedHashMap.class); + + assertThat(properties).hasSize(3); + assertThat(flattenedMap).hasSize(3); + + assertThat(properties.get("bar.spam")).isEqualTo("bucket"); + assertThat(flattenedMap.get("bar.spam")).isEqualTo("bucket"); + Map bar = (Map) map.get("bar"); - assertEquals("bucket", bar.get("spam")); + assertThat(bar.get("spam")).isEqualTo("bucket"); + + List keysFromProperties = properties.keySet().stream().collect(toList()); + List keysFromFlattenedMap = flattenedMap.keySet().stream().collect(toList()); + assertThat(keysFromProperties).containsExactlyInAnyOrderElementsOf(keysFromFlattenedMap); + // Keys in the Properties object are sorted. + assertThat(keysFromProperties).containsExactly("bar.spam", "cat", "foo"); + // But the flattened map retains the order from the input. + assertThat(keysFromFlattenedMap).containsExactly("cat", "foo", "bar.spam"); + }); + } + + @Test + void customTypeSupportedByDefault() throws Exception { + URL url = new URL("https://localhost:9000/"); + setYaml("value: !!java.net.URL [\"" + url + "\"]"); + + this.processor.process((properties, map) -> { + assertThat(properties).containsExactly(entry("value", url)); + assertThat(map).containsExactly(entry("value", url)); + }); + } + + @Test + void customTypesSupportedDueToExplicitConfiguration() throws Exception { + this.processor.setSupportedTypes(URL.class, String.class); + + URL url = new URL("https://localhost:9000/"); + setYaml("value: !!java.net.URL [!!java.lang.String [\"" + url + "\"]]"); + + this.processor.process((properties, map) -> { + assertThat(properties).containsExactly(entry("value", url)); + assertThat(map).containsExactly(entry("value", url)); }); } + @Test + void customTypeNotSupportedDueToExplicitConfiguration() { + this.processor.setSupportedTypes(List.class); + + setYaml("value: !!java.net.URL [\"https://localhost:9000/\"]"); + + assertThatExceptionOfType(ConstructorException.class) + .isThrownBy(() -> this.processor.process((properties, map) -> {})) + .withMessageContaining("Unsupported type encountered in YAML document: java.net.URL"); + } + + private void setYaml(String yaml) { + this.processor.setResources(new ByteArrayResource(yaml.getBytes())); + } + } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java index aa32bc603523..828ac0808184 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,19 +19,19 @@ import java.util.Map; import java.util.Properties; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.DuplicateKeyException; import org.yaml.snakeyaml.scanner.ScannerException; +import org.springframework.beans.factory.config.YamlProcessor.DocumentMatcher; +import org.springframework.beans.factory.config.YamlProcessor.MatchStatus; +import org.springframework.beans.factory.config.YamlProcessor.ResolutionMethod; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.springframework.beans.factory.config.YamlProcessor.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link YamlPropertiesFactoryBean}. @@ -41,81 +41,76 @@ */ public class YamlPropertiesFactoryBeanTests { - @Rule - public ExpectedException exception = ExpectedException.none(); - - @Test - public void testLoadResource() { + public void loadResource() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bar")); - assertThat(properties.getProperty("spam.foo"), equalTo("baz")); + assertThat(properties.getProperty("foo")).isEqualTo("bar"); + assertThat(properties.getProperty("spam.foo")).isEqualTo("baz"); } @Test - public void testBadResource() { + public void badResource() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); - factory.setResources(new ByteArrayResource( - "foo: bar\ncd\nspam:\n foo: baz".getBytes())); - this.exception.expect(ScannerException.class); - this.exception.expectMessage("line 3, column 1"); - factory.getObject(); + factory.setResources(new ByteArrayResource("foo: bar\ncd\nspam:\n foo: baz".getBytes())); + assertThatExceptionOfType(ScannerException.class) + .isThrownBy(factory::getObject) + .withMessageContaining("line 3, column 1"); } @Test - public void testLoadResourcesWithOverride() { + public void loadResourcesWithOverride() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources( new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes()), new ByteArrayResource("foo:\n bar: spam".getBytes())); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bar")); - assertThat(properties.getProperty("spam.foo"), equalTo("baz")); - assertThat(properties.getProperty("foo.bar"), equalTo("spam")); + assertThat(properties.getProperty("foo")).isEqualTo("bar"); + assertThat(properties.getProperty("spam.foo")).isEqualTo("baz"); + assertThat(properties.getProperty("foo.bar")).isEqualTo("spam"); } - @Test(expected = DuplicateKeyException.class) - public void testLoadResourcesWithInternalOverride() { + @Test + public void loadResourcesWithInternalOverride() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam:\n foo: baz\nfoo: bucket".getBytes())); - factory.getObject(); + assertThatExceptionOfType(DuplicateKeyException.class).isThrownBy(factory::getObject); } - @Test(expected = DuplicateKeyException.class) - public void testLoadResourcesWithNestedInternalOverride() { + @Test + public void loadResourcesWithNestedInternalOverride() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo:\n bar: spam\n foo: baz\nbreak: it\nfoo: bucket".getBytes())); - factory.getObject(); + assertThatExceptionOfType(DuplicateKeyException.class).isThrownBy(factory::getObject); } @Test - public void testLoadResourceWithMultipleDocuments() { + public void loadResourceWithMultipleDocuments() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam: baz\n---\nfoo: bag".getBytes())); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bag")); - assertThat(properties.getProperty("spam"), equalTo("baz")); + assertThat(properties.getProperty("foo")).isEqualTo("bag"); + assertThat(properties.getProperty("spam")).isEqualTo("baz"); } @Test - public void testLoadResourceWithSelectedDocuments() { + public void loadResourceWithSelectedDocuments() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes())); factory.setDocumentMatchers(properties -> ("bag".equals(properties.getProperty("foo")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND)); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bag")); - assertThat(properties.getProperty("spam"), equalTo("bad")); + assertThat(properties.getProperty("foo")).isEqualTo("bag"); + assertThat(properties.getProperty("spam")).isEqualTo("bad"); } @Test - public void testLoadResourceWithDefaultMatch() { + public void loadResourceWithDefaultMatch() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setMatchDefault(true); factory.setResources(new ByteArrayResource( @@ -128,13 +123,13 @@ public void testLoadResourceWithDefaultMatch() { MatchStatus.FOUND : MatchStatus.NOT_FOUND); }); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bag")); - assertThat(properties.getProperty("spam"), equalTo("bad")); - assertThat(properties.getProperty("one"), equalTo("two")); + assertThat(properties.getProperty("foo")).isEqualTo("bag"); + assertThat(properties.getProperty("spam")).isEqualTo("bad"); + assertThat(properties.getProperty("one")).isEqualTo("two"); } @Test - public void testLoadResourceWithoutDefaultMatch() { + public void loadResourceWithoutDefaultMatch() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setMatchDefault(false); factory.setResources(new ByteArrayResource( @@ -150,13 +145,13 @@ public MatchStatus matches(Properties properties) { } }); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bag")); - assertThat(properties.getProperty("spam"), equalTo("bad")); - assertThat(properties.getProperty("one"), nullValue()); + assertThat(properties.getProperty("foo")).isEqualTo("bag"); + assertThat(properties.getProperty("spam")).isEqualTo("bad"); + assertThat(properties.getProperty("one")).isNull(); } @Test - public void testLoadResourceWithDefaultMatchSkippingMissedMatch() { + public void loadResourceWithDefaultMatchSkippingMissedMatch() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setMatchDefault(true); factory.setResources(new ByteArrayResource( @@ -169,79 +164,79 @@ public void testLoadResourceWithDefaultMatchSkippingMissedMatch() { MatchStatus.FOUND : MatchStatus.NOT_FOUND); }); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bag")); - assertThat(properties.getProperty("spam"), equalTo("bad")); - assertThat(properties.getProperty("one"), equalTo("two")); + assertThat(properties.getProperty("foo")).isEqualTo("bag"); + assertThat(properties.getProperty("spam")).isEqualTo("bad"); + assertThat(properties.getProperty("one")).isEqualTo("two"); } @Test - public void testLoadNonExistentResource() { + public void loadNonExistentResource() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE); factory.setResources(new ClassPathResource("no-such-file.yml")); Properties properties = factory.getObject(); - assertThat(properties.size(), equalTo(0)); + assertThat(properties).isEmpty(); } @Test - public void testLoadNull() { + public void loadNull() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo: bar\nspam:".getBytes())); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo"), equalTo("bar")); - assertThat(properties.getProperty("spam"), equalTo("")); + assertThat(properties.getProperty("foo")).isEqualTo("bar"); + assertThat(properties.getProperty("spam")).isEqualTo(""); } @Test - public void testLoadEmptyArrayValue() { + public void loadEmptyArrayValue() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("a: alpha\ntest: []".getBytes())); Properties properties = factory.getObject(); - assertThat(properties.getProperty("a"), equalTo("alpha")); - assertThat(properties.getProperty("test"), equalTo("")); + assertThat(properties.getProperty("a")).isEqualTo("alpha"); + assertThat(properties.getProperty("test")).isEqualTo(""); } @Test - public void testLoadArrayOfString() { + public void loadArrayOfString() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo:\n- bar\n- baz".getBytes())); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo[0]"), equalTo("bar")); - assertThat(properties.getProperty("foo[1]"), equalTo("baz")); - assertThat(properties.get("foo"), is(nullValue())); + assertThat(properties.getProperty("foo[0]")).isEqualTo("bar"); + assertThat(properties.getProperty("foo[1]")).isEqualTo("baz"); + assertThat(properties.get("foo")).isNull(); } @Test - public void testLoadArrayOfInteger() { + public void loadArrayOfInteger() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo:\n- 1\n- 2".getBytes())); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo[0]"), equalTo("1")); - assertThat(properties.getProperty("foo[1]"), equalTo("2")); - assertThat(properties.get("foo"), is(nullValue())); + assertThat(properties.getProperty("foo[0]")).isEqualTo("1"); + assertThat(properties.getProperty("foo[1]")).isEqualTo("2"); + assertThat(properties.get("foo")).isNull(); } @Test - public void testLoadArrayOfObject() { + public void loadArrayOfObject() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo:\n- bar:\n spam: crap\n- baz\n- one: two\n three: four".getBytes() )); Properties properties = factory.getObject(); - assertThat(properties.getProperty("foo[0].bar.spam"), equalTo("crap")); - assertThat(properties.getProperty("foo[1]"), equalTo("baz")); - assertThat(properties.getProperty("foo[2].one"), equalTo("two")); - assertThat(properties.getProperty("foo[2].three"), equalTo("four")); - assertThat(properties.get("foo"), is(nullValue())); + assertThat(properties.getProperty("foo[0].bar.spam")).isEqualTo("crap"); + assertThat(properties.getProperty("foo[1]")).isEqualTo("baz"); + assertThat(properties.getProperty("foo[2].one")).isEqualTo("two"); + assertThat(properties.getProperty("foo[2].three")).isEqualTo("four"); + assertThat(properties.get("foo")).isNull(); } - @SuppressWarnings("unchecked") @Test - public void testYaml() { + @SuppressWarnings("unchecked") + public void yaml() { Yaml yaml = new Yaml(); Map map = yaml.loadAs("foo: bar\nspam:\n foo: baz", Map.class); - assertThat(map.get("foo"), equalTo("bar")); - assertThat(((Map) map.get("spam")).get("foo"), equalTo("baz")); + assertThat(map.get("foo")).isEqualTo("bar"); + assertThat(((Map) map.get("spam")).get("foo")).isEqualTo("baz"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ConstructorArgumentEntryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ConstructorArgumentEntryTests.java index 430e6ef07472..9e3155dc4591 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ConstructorArgumentEntryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ConstructorArgumentEntryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,7 +16,9 @@ package org.springframework.beans.factory.parsing; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for {@link ConstructorArgumentEntry}. @@ -26,9 +28,10 @@ */ public class ConstructorArgumentEntryTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorBailsOnNegativeCtorIndexArgument() { - new ConstructorArgumentEntry(-1); + assertThatIllegalArgumentException().isThrownBy(() -> + new ConstructorArgumentEntry(-1)); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java index adfe6176cb12..34abf223af41 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,16 +19,15 @@ import java.util.ArrayList; import java.util.List; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; -import static org.springframework.tests.TestResourceUtils.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** * @author Rob Harrop @@ -37,8 +36,6 @@ */ public class CustomProblemReporterTests { - private static final Resource CONTEXT = qualifiedResource(CustomProblemReporterTests.class, "context.xml"); - private CollatingProblemReporter problemReporter; private DefaultListableBeanFactory beanFactory; @@ -46,21 +43,22 @@ public class CustomProblemReporterTests { private XmlBeanDefinitionReader reader; - @Before - public void setUp() { + @BeforeEach + public void setup() { this.problemReporter = new CollatingProblemReporter(); this.beanFactory = new DefaultListableBeanFactory(); this.reader = new XmlBeanDefinitionReader(this.beanFactory); this.reader.setProblemReporter(this.problemReporter); } + @Test public void testErrorsAreCollated() { - this.reader.loadBeanDefinitions(CONTEXT); - assertEquals("Incorrect number of errors collated", 4, this.problemReporter.getErrors().length); + this.reader.loadBeanDefinitions(qualifiedResource(CustomProblemReporterTests.class, "context.xml")); + assertThat(this.problemReporter.getErrors().length).as("Incorrect number of errors collated").isEqualTo(4); TestBean bean = (TestBean) this.beanFactory.getBean("validBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/FailFastProblemReporterTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/FailFastProblemReporterTests.java index 19180d4156cc..fdd7faed0ea3 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/FailFastProblemReporterTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/FailFastProblemReporterTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,11 +17,15 @@ package org.springframework.beans.factory.parsing; import org.apache.commons.logging.Log; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.core.io.DescriptiveResource; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.isA; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; /** * @author Rick Evans @@ -30,11 +34,12 @@ */ public class FailFastProblemReporterTests { - @Test(expected = BeanDefinitionParsingException.class) + @Test public void testError() throws Exception { FailFastProblemReporter reporter = new FailFastProblemReporter(); - reporter.error(new Problem("VGER", new Location(new DescriptiveResource("here")), - null, new IllegalArgumentException())); + assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() -> + reporter.error(new Problem("VGER", new Location(new DescriptiveResource("here")), + null, new IllegalArgumentException()))); } @Test diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/NullSourceExtractorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/NullSourceExtractorTests.java index 24b11fd95566..48dd45999e0f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/NullSourceExtractorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/NullSourceExtractorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,9 @@ package org.springframework.beans.factory.parsing; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rick Evans @@ -30,13 +30,13 @@ public class NullSourceExtractorTests { public void testPassThroughContract() throws Exception { Object source = new Object(); Object extractedSource = new NullSourceExtractor().extractSource(source, null); - assertNull("The contract of NullSourceExtractor states that the extraction *always* return null", extractedSource); + assertThat(extractedSource).as("The contract of NullSourceExtractor states that the extraction *always* return null").isNull(); } @Test public void testPassThroughContractEvenWithNull() throws Exception { Object extractedSource = new NullSourceExtractor().extractSource(null, null); - assertNull("The contract of NullSourceExtractor states that the extraction *always* return null", extractedSource); + assertThat(extractedSource).as("The contract of NullSourceExtractor states that the extraction *always* return null").isNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ParseStateTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ParseStateTests.java index 523ba431df73..ce6a3aaa7df7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ParseStateTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/ParseStateTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,9 @@ package org.springframework.beans.factory.parsing; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -33,9 +33,9 @@ public void testSimple() throws Exception { ParseState parseState = new ParseState(); parseState.push(entry); - assertEquals("Incorrect peek value.", entry, parseState.peek()); + assertThat(parseState.peek()).as("Incorrect peek value.").isEqualTo(entry); parseState.pop(); - assertNull("Should get null on peek()", parseState.peek()); + assertThat(parseState.peek()).as("Should get null on peek()").isNull(); } @Test @@ -46,16 +46,16 @@ public void testNesting() throws Exception { ParseState parseState = new ParseState(); parseState.push(one); - assertEquals(one, parseState.peek()); + assertThat(parseState.peek()).isEqualTo(one); parseState.push(two); - assertEquals(two, parseState.peek()); + assertThat(parseState.peek()).isEqualTo(two); parseState.push(three); - assertEquals(three, parseState.peek()); + assertThat(parseState.peek()).isEqualTo(three); parseState.pop(); - assertEquals(two, parseState.peek()); + assertThat(parseState.peek()).isEqualTo(two); parseState.pop(); - assertEquals(one, parseState.peek()); + assertThat(parseState.peek()).isEqualTo(one); } @Test @@ -67,7 +67,7 @@ public void testSnapshot() throws Exception { ParseState snapshot = original.snapshot(); original.push(new MockEntry()); - assertEquals("Snapshot should not have been modified.", entry, snapshot.peek()); + assertThat(snapshot.peek()).as("Snapshot should not have been modified.").isEqualTo(entry); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PassThroughSourceExtractorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PassThroughSourceExtractorTests.java index 392879715830..2f6fe191d233 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PassThroughSourceExtractorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PassThroughSourceExtractorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,9 @@ package org.springframework.beans.factory.parsing; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests for {@link PassThroughSourceExtractor}. @@ -32,15 +32,15 @@ public class PassThroughSourceExtractorTests { public void testPassThroughContract() throws Exception { Object source = new Object(); Object extractedSource = new PassThroughSourceExtractor().extractSource(source, null); - assertSame("The contract of PassThroughSourceExtractor states that the supplied " + - "source object *must* be returned as-is", source, extractedSource); + assertThat(extractedSource).as("The contract of PassThroughSourceExtractor states that the supplied " + + "source object *must* be returned as-is").isSameAs(source); } @Test public void testPassThroughContractEvenWithNull() throws Exception { Object extractedSource = new PassThroughSourceExtractor().extractSource(null, null); - assertNull("The contract of PassThroughSourceExtractor states that the supplied " + - "source object *must* be returned as-is (even if null)", extractedSource); + assertThat(extractedSource).as("The contract of PassThroughSourceExtractor states that the supplied " + + "source object *must* be returned as-is (even if null)").isNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PropertyEntryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PropertyEntryTests.java index 11e1db26fa86..084fb1f5a749 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PropertyEntryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/PropertyEntryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,7 +16,9 @@ package org.springframework.beans.factory.parsing; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for {@link PropertyEntry}. @@ -26,19 +28,22 @@ */ public class PropertyEntryTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorBailsOnNullPropertyNameArgument() throws Exception { - new PropertyEntry(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new PropertyEntry(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorBailsOnEmptyPropertyNameArgument() throws Exception { - new PropertyEntry(""); + assertThatIllegalArgumentException().isThrownBy(() -> + new PropertyEntry("")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorBailsOnWhitespacedPropertyNameArgument() throws Exception { - new PropertyEntry("\t "); + assertThatIllegalArgumentException().isThrownBy(() -> + new PropertyEntry("\t ")); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java index b471059aebbc..bdf544c6c550 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,55 +18,59 @@ import java.util.List; import java.util.ServiceLoader; + import javax.xml.parsers.DocumentBuilderFactory; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; -import static org.junit.Assert.*; -import static org.junit.Assume.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** * @author Juergen Hoeller * @author Chris Beams */ -public class ServiceLoaderTests { +class ServiceLoaderTests { - @Test - public void testServiceLoaderFactoryBean() { + @BeforeAll + static void assumeDocumentBuilderFactoryCanBeLoaded() { assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); + } + @Test + void testServiceLoaderFactoryBean() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceLoaderFactoryBean.class); bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName()); bf.registerBeanDefinition("service", bd); ServiceLoader serviceLoader = (ServiceLoader) bf.getBean("service"); - assertTrue(serviceLoader.iterator().next() instanceof DocumentBuilderFactory); + boolean condition = serviceLoader.iterator().next() instanceof DocumentBuilderFactory; + assertThat(condition).isTrue(); } @Test - public void testServiceFactoryBean() { - assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); - + void testServiceFactoryBean() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceFactoryBean.class); bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName()); bf.registerBeanDefinition("service", bd); - assertTrue(bf.getBean("service") instanceof DocumentBuilderFactory); + boolean condition = bf.getBean("service") instanceof DocumentBuilderFactory; + assertThat(condition).isTrue(); } @Test - public void testServiceListFactoryBean() { - assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); - + void testServiceListFactoryBean() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceListFactoryBean.class); bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName()); bf.registerBeanDefinition("service", bd); List serviceList = (List) bf.getBean("service"); - assertTrue(serviceList.get(0) instanceof DocumentBuilderFactory); + boolean condition = serviceList.get(0) instanceof DocumentBuilderFactory; + assertThat(condition).isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/AutowireUtilsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/AutowireUtilsTests.java index c1de073dc27f..e7e04d7390d8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/AutowireUtilsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/AutowireUtilsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,60 +20,54 @@ import java.util.HashMap; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.ReflectionUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** + * Unit tests for {@link AutowireUtils}. + * * @author Juergen Hoeller * @author Sam Brannen + * @author Loïc Ledoyen */ public class AutowireUtilsTests { @Test public void genericMethodReturnTypes() { Method notParameterized = ReflectionUtils.findMethod(MyTypeWithMethods.class, "notParameterized"); - assertEquals(String.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(notParameterized, new Object[]{}, getClass().getClassLoader())); + Object actual = AutowireUtils.resolveReturnTypeForFactoryMethod(notParameterized, new Object[0], getClass().getClassLoader()); + assertThat(actual).isEqualTo(String.class); Method notParameterizedWithArguments = ReflectionUtils.findMethod(MyTypeWithMethods.class, "notParameterizedWithArguments", Integer.class, Boolean.class); - assertEquals(String.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(notParameterizedWithArguments, new Object[] {99, true}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(notParameterizedWithArguments, new Object[]{99, true}, getClass().getClassLoader())).isEqualTo(String.class); Method createProxy = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createProxy", Object.class); - assertEquals(String.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(createProxy, new Object[] {"foo"}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(createProxy, new Object[]{"foo"}, getClass().getClassLoader())).isEqualTo(String.class); Method createNamedProxyWithDifferentTypes = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createNamedProxy", String.class, Object.class); - assertEquals(Long.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedProxyWithDifferentTypes, new Object[] {"enigma", 99L}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedProxyWithDifferentTypes, new Object[]{"enigma", 99L}, getClass().getClassLoader())).isEqualTo(Long.class); Method createNamedProxyWithDuplicateTypes = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createNamedProxy", String.class, Object.class); - assertEquals(String.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedProxyWithDuplicateTypes, new Object[] {"enigma", "foo"}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedProxyWithDuplicateTypes, new Object[]{"enigma", "foo"}, getClass().getClassLoader())).isEqualTo(String.class); Method createMock = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createMock", Class.class); - assertEquals(Runnable.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(createMock, new Object[] {Runnable.class}, getClass().getClassLoader())); - assertEquals(Runnable.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(createMock, new Object[] {Runnable.class.getName()}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(createMock, new Object[]{Runnable.class}, getClass().getClassLoader())).isEqualTo(Runnable.class); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(createMock, new Object[]{Runnable.class.getName()}, getClass().getClassLoader())).isEqualTo(Runnable.class); Method createNamedMock = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createNamedMock", String.class, Class.class); - assertEquals(Runnable.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedMock, new Object[] {"foo", Runnable.class}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedMock, new Object[]{"foo", Runnable.class}, getClass().getClassLoader())).isEqualTo(Runnable.class); Method createVMock = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createVMock", Object.class, Class.class); - assertEquals(Runnable.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(createVMock, new Object[] {"foo", Runnable.class}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(createVMock, new Object[]{"foo", Runnable.class}, getClass().getClassLoader())).isEqualTo(Runnable.class); // Ideally we would expect String.class instead of Object.class, but // resolveReturnTypeForFactoryMethod() does not currently support this form of // look-up. Method extractValueFrom = ReflectionUtils.findMethod(MyTypeWithMethods.class, "extractValueFrom", MyInterfaceType.class); - assertEquals(Object.class, - AutowireUtils.resolveReturnTypeForFactoryMethod(extractValueFrom, new Object[] {new MySimpleInterfaceType()}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(extractValueFrom, new Object[]{new MySimpleInterfaceType()}, getClass().getClassLoader())).isEqualTo(Object.class); // Ideally we would expect Boolean.class instead of Object.class, but this // information is not available at run-time due to type erasure. @@ -81,7 +75,7 @@ public void genericMethodReturnTypes() { map.put(0, false); map.put(1, true); Method extractMagicValue = ReflectionUtils.findMethod(MyTypeWithMethods.class, "extractMagicValue", Map.class); - assertEquals(Object.class, AutowireUtils.resolveReturnTypeForFactoryMethod(extractMagicValue, new Object[] {map}, getClass().getClassLoader())); + assertThat(AutowireUtils.resolveReturnTypeForFactoryMethod(extractMagicValue, new Object[]{map}, getClass().getClassLoader())).isEqualTo(Object.class); } @@ -93,31 +87,6 @@ public class MySimpleInterfaceType implements MyInterfaceType { public static class MyTypeWithMethods { - public MyInterfaceType integer() { - return null; - } - - public MySimpleInterfaceType string() { - return null; - } - - public Object object() { - return null; - } - - @SuppressWarnings("rawtypes") - public MyInterfaceType raw() { - return null; - } - - public String notParameterized() { - return null; - } - - public String notParameterizedWithArguments(Integer x, Boolean b) { - return null; - } - /** * Simulates a factory method that wraps the supplied object in a proxy of the * same type. @@ -173,6 +142,31 @@ public static V extractMagicValue(Map map) { return null; } + public MyInterfaceType integer() { + return null; + } + + public MySimpleInterfaceType string() { + return null; + } + + public Object object() { + return null; + } + + @SuppressWarnings("rawtypes") + public MyInterfaceType raw() { + return null; + } + + public String notParameterized() { + return null; + } + + public String notParameterizedWithArguments(Integer x, Boolean b) { + return null; + } + public void readIntegerInputMessage(MyInterfaceType message) { } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionBuilderTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionBuilderTests.java index fd3036b6047f..ec157df512c2 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionBuilderTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionBuilderTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,12 +18,12 @@ import java.util.Arrays; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -36,42 +36,42 @@ public void beanClassWithSimpleProperty() { String[] dependsOn = new String[] { "A", "B", "C" }; BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class); bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE); - bdb.addPropertyReference("age", "15"); - for (int i = 0; i < dependsOn.length; i++) { - bdb.addDependsOn(dependsOn[i]); + bdb.addPropertyValue("age", "15"); + for (String dependsOnEntry : dependsOn) { + bdb.addDependsOn(dependsOnEntry); } RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition(); - assertFalse(rbd.isSingleton()); - assertEquals(TestBean.class, rbd.getBeanClass()); - assertTrue("Depends on was added", Arrays.equals(dependsOn, rbd.getDependsOn())); - assertTrue(rbd.getPropertyValues().contains("age")); + assertThat(rbd.isSingleton()).isFalse(); + assertThat(rbd.getBeanClass()).isEqualTo(TestBean.class); + assertThat(Arrays.equals(dependsOn, rbd.getDependsOn())).as("Depends on was added").isTrue(); + assertThat(rbd.getPropertyValues().contains("age")).isTrue(); } @Test public void beanClassWithFactoryMethod() { BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class, "create"); RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition(); - assertTrue(rbd.hasBeanClass()); - assertEquals(TestBean.class, rbd.getBeanClass()); - assertEquals("create", rbd.getFactoryMethodName()); + assertThat(rbd.hasBeanClass()).isTrue(); + assertThat(rbd.getBeanClass()).isEqualTo(TestBean.class); + assertThat(rbd.getFactoryMethodName()).isEqualTo("create"); } @Test public void beanClassName() { BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class.getName()); RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition(); - assertFalse(rbd.hasBeanClass()); - assertEquals(TestBean.class.getName(), rbd.getBeanClassName()); + assertThat(rbd.hasBeanClass()).isFalse(); + assertThat(rbd.getBeanClassName()).isEqualTo(TestBean.class.getName()); } @Test public void beanClassNameWithFactoryMethod() { BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class.getName(), "create"); RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition(); - assertFalse(rbd.hasBeanClass()); - assertEquals(TestBean.class.getName(), rbd.getBeanClassName()); - assertEquals("create", rbd.getFactoryMethodName()); + assertThat(rbd.hasBeanClass()).isFalse(); + assertThat(rbd.getBeanClassName()).isEqualTo(TestBean.class.getName()); + assertThat(rbd.getFactoryMethodName()).isEqualTo("create"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionTests.java index 9e32012468aa..de770309e058 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -16,12 +16,12 @@ package org.springframework.beans.factory.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinitionHolder; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -35,14 +35,16 @@ public void beanDefinitionEquality() { bd.setLazyInit(true); bd.setScope("request"); RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); + boolean condition1 = !bd.equals(otherBd); + assertThat(condition1).isTrue(); + boolean condition = !otherBd.equals(bd); + assertThat(condition).isTrue(); otherBd.setAbstract(true); otherBd.setLazyInit(true); otherBd.setScope("request"); - assertTrue(bd.equals(otherBd)); - assertTrue(otherBd.equals(bd)); - assertTrue(bd.hashCode() == otherBd.hashCode()); + assertThat(bd.equals(otherBd)).isTrue(); + assertThat(otherBd.equals(bd)).isTrue(); + assertThat(bd.hashCode() == otherBd.hashCode()).isTrue(); } @Test @@ -52,52 +54,95 @@ public void beanDefinitionEqualityWithPropertyValues() { bd.getPropertyValues().add("age", "99"); RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class); otherBd.getPropertyValues().add("name", "myName"); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); + boolean condition3 = !bd.equals(otherBd); + assertThat(condition3).isTrue(); + boolean condition2 = !otherBd.equals(bd); + assertThat(condition2).isTrue(); otherBd.getPropertyValues().add("age", "11"); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); + boolean condition1 = !bd.equals(otherBd); + assertThat(condition1).isTrue(); + boolean condition = !otherBd.equals(bd); + assertThat(condition).isTrue(); otherBd.getPropertyValues().add("age", "99"); - assertTrue(bd.equals(otherBd)); - assertTrue(otherBd.equals(bd)); - assertTrue(bd.hashCode() == otherBd.hashCode()); + assertThat(bd.equals(otherBd)).isTrue(); + assertThat(otherBd.equals(bd)).isTrue(); + assertThat(bd.hashCode() == otherBd.hashCode()).isTrue(); } @Test public void beanDefinitionEqualityWithConstructorArguments() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.getConstructorArgumentValues().addGenericArgumentValue("test"); - bd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5)); + bd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5); RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class); otherBd.getConstructorArgumentValues().addGenericArgumentValue("test"); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); - otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(9)); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); - otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5)); - assertTrue(bd.equals(otherBd)); - assertTrue(otherBd.equals(bd)); - assertTrue(bd.hashCode() == otherBd.hashCode()); + boolean condition3 = !bd.equals(otherBd); + assertThat(condition3).isTrue(); + boolean condition2 = !otherBd.equals(bd); + assertThat(condition2).isTrue(); + otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 9); + boolean condition1 = !bd.equals(otherBd); + assertThat(condition1).isTrue(); + boolean condition = !otherBd.equals(bd); + assertThat(condition).isTrue(); + otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5); + assertThat(bd.equals(otherBd)).isTrue(); + assertThat(otherBd.equals(bd)).isTrue(); + assertThat(bd.hashCode() == otherBd.hashCode()).isTrue(); } @Test public void beanDefinitionEqualityWithTypedConstructorArguments() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.getConstructorArgumentValues().addGenericArgumentValue("test", "int"); - bd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5), "long"); + bd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5, "long"); RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class); otherBd.getConstructorArgumentValues().addGenericArgumentValue("test", "int"); - otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5)); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); - otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5), "int"); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); - otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5), "long"); - assertTrue(bd.equals(otherBd)); - assertTrue(otherBd.equals(bd)); - assertTrue(bd.hashCode() == otherBd.hashCode()); + otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5); + boolean condition3 = !bd.equals(otherBd); + assertThat(condition3).isTrue(); + boolean condition2 = !otherBd.equals(bd); + assertThat(condition2).isTrue(); + otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5, "int"); + boolean condition1 = !bd.equals(otherBd); + assertThat(condition1).isTrue(); + boolean condition = !otherBd.equals(bd); + assertThat(condition).isTrue(); + otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5, "long"); + assertThat(bd.equals(otherBd)).isTrue(); + assertThat(otherBd.equals(bd)).isTrue(); + assertThat(bd.hashCode() == otherBd.hashCode()).isTrue(); + } + + @Test + public void genericBeanDefinitionEquality() { + GenericBeanDefinition bd = new GenericBeanDefinition(); + bd.setParentName("parent"); + bd.setScope("request"); + bd.setAbstract(true); + bd.setLazyInit(true); + GenericBeanDefinition otherBd = new GenericBeanDefinition(); + otherBd.setScope("request"); + otherBd.setAbstract(true); + otherBd.setLazyInit(true); + boolean condition1 = !bd.equals(otherBd); + assertThat(condition1).isTrue(); + boolean condition = !otherBd.equals(bd); + assertThat(condition).isTrue(); + otherBd.setParentName("parent"); + assertThat(bd.equals(otherBd)).isTrue(); + assertThat(otherBd.equals(bd)).isTrue(); + assertThat(bd.hashCode() == otherBd.hashCode()).isTrue(); + + bd.getPropertyValues(); + assertThat(bd.equals(otherBd)).isTrue(); + assertThat(otherBd.equals(bd)).isTrue(); + assertThat(bd.hashCode() == otherBd.hashCode()).isTrue(); + + bd.getConstructorArgumentValues(); + assertThat(bd.equals(otherBd)).isTrue(); + assertThat(otherBd.equals(bd)).isTrue(); + assertThat(bd.hashCode() == otherBd.hashCode()).isTrue(); } @Test @@ -108,22 +153,24 @@ public void beanDefinitionHolderEquality() { bd.setScope("request"); BeanDefinitionHolder holder = new BeanDefinitionHolder(bd, "bd"); RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class); - assertTrue(!bd.equals(otherBd)); - assertTrue(!otherBd.equals(bd)); + boolean condition1 = !bd.equals(otherBd); + assertThat(condition1).isTrue(); + boolean condition = !otherBd.equals(bd); + assertThat(condition).isTrue(); otherBd.setAbstract(true); otherBd.setLazyInit(true); otherBd.setScope("request"); BeanDefinitionHolder otherHolder = new BeanDefinitionHolder(bd, "bd"); - assertTrue(holder.equals(otherHolder)); - assertTrue(otherHolder.equals(holder)); - assertTrue(holder.hashCode() == otherHolder.hashCode()); + assertThat(holder.equals(otherHolder)).isTrue(); + assertThat(otherHolder.equals(holder)).isTrue(); + assertThat(holder.hashCode() == otherHolder.hashCode()).isTrue(); } @Test public void beanDefinitionMerging() { RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.getConstructorArgumentValues().addGenericArgumentValue("test"); - bd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5)); + bd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5); bd.getPropertyValues().add("name", "myName"); bd.getPropertyValues().add("age", "99"); bd.setQualifiedElement(getClass()); @@ -133,13 +180,13 @@ public void beanDefinitionMerging() { RootBeanDefinition mergedBd = new RootBeanDefinition(bd); mergedBd.overrideFrom(childBd); - assertEquals(2, mergedBd.getConstructorArgumentValues().getArgumentCount()); - assertEquals(2, mergedBd.getPropertyValues().size()); - assertEquals(bd, mergedBd); + assertThat(mergedBd.getConstructorArgumentValues().getArgumentCount()).isEqualTo(2); + assertThat(mergedBd.getPropertyValues().size()).isEqualTo(2); + assertThat(mergedBd).isEqualTo(bd); - mergedBd.getConstructorArgumentValues().getArgumentValue(1, null).setValue(new Integer(9)); - assertEquals(new Integer(5), bd.getConstructorArgumentValues().getArgumentValue(1, null).getValue()); - assertEquals(getClass(), bd.getQualifiedElement()); + mergedBd.getConstructorArgumentValues().getArgumentValue(1, null).setValue(9); + assertThat(bd.getConstructorArgumentValues().getArgumentValue(1, null).getValue()).isEqualTo(5); + assertThat(bd.getQualifiedElement()).isEqualTo(getClass()); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanFactoryGenericsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanFactoryGenericsTests.java index 3ed1b9bec66c..6a69d4974e62 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanFactoryGenericsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/BeanFactoryGenericsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -32,7 +32,7 @@ import java.util.Set; import java.util.stream.Collectors; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.PropertyEditorRegistrar; @@ -43,20 +43,21 @@ import org.springframework.beans.factory.config.TypedStringValue; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.propertyeditors.CustomNumberEditor; +import org.springframework.beans.testfixture.beans.GenericBean; +import org.springframework.beans.testfixture.beans.GenericIntegerBean; +import org.springframework.beans.testfixture.beans.GenericSetOfIntegerBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.OverridingClassLoader; import org.springframework.core.ResolvableType; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.Order; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.UrlResource; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; -import org.springframework.tests.sample.beans.GenericBean; -import org.springframework.tests.sample.beans.GenericIntegerBean; -import org.springframework.tests.sample.beans.GenericSetOfIntegerBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.core.testfixture.EnabledForTestGroups; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.springframework.core.testfixture.TestGroup.LONG_RUNNING; /** * @author Juergen Hoeller @@ -79,8 +80,8 @@ public void testGenericSetProperty() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); } @Test @@ -96,8 +97,8 @@ public void testGenericListProperty() throws Exception { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); - assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(gb.getResourceList().get(1)).isEqualTo(new UrlResource("http://localhost:9090")); } @Test @@ -111,8 +112,8 @@ public void testGenericListPropertyWithAutowiring() throws Exception { bf.registerBeanDefinition("genericBean", rbd); GenericIntegerBean gb = (GenericIntegerBean) bf.getBean("genericBean"); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); - assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(gb.getResourceList().get(1)).isEqualTo(new UrlResource("http://localhost:9090")); } @Test @@ -125,14 +126,12 @@ public void testGenericListPropertyWithInvalidElementType() { rbd.getPropertyValues().add("testBeanList", input); bf.registerBeanDefinition("genericBean", rbd); - try { - bf.getBean("genericBean"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getMessage().contains("genericBean") && ex.getMessage().contains("testBeanList[0]")); - assertTrue(ex.getMessage().contains(TestBean.class.getName()) && ex.getMessage().contains("Integer")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + bf.getBean("genericBean")) + .withMessageContaining("genericBean") + .withMessageContaining("testBeanList[0]") + .withMessageContaining(TestBean.class.getName()) + .withMessageContaining("Integer"); } @Test @@ -144,7 +143,7 @@ public void testGenericListPropertyWithOptionalAutowiring() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertNull(gb.getResourceList()); + assertThat(gb.getResourceList()).isNull(); } @Test @@ -160,8 +159,8 @@ public void testGenericMapProperty() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); } @Test @@ -171,11 +170,11 @@ public void testGenericListOfArraysProperty() { new ClassPathResource("genericBeanTests.xml", getClass())); GenericBean gb = (GenericBean) bf.getBean("listOfArrays"); - assertEquals(1, gb.getListOfArrays().size()); + assertThat(gb.getListOfArrays().size()).isEqualTo(1); String[] array = gb.getListOfArrays().get(0); - assertEquals(2, array.length); - assertEquals("value1", array[0]); - assertEquals("value2", array[1]); + assertThat(array.length).isEqualTo(2); + assertThat(array[0]).isEqualTo("value1"); + assertThat(array[1]).isEqualTo("value2"); } @@ -192,23 +191,23 @@ public void testGenericSetConstructor() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); } @Test public void testGenericSetConstructorWithAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerSingleton("integer1", new Integer(4)); - bf.registerSingleton("integer2", new Integer(5)); + bf.registerSingleton("integer1", 4); + bf.registerSingleton("integer2", 5); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); } @Test @@ -220,7 +219,7 @@ public void testGenericSetConstructorWithOptionalAutowiring() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertNull(gb.getIntegerSet()); + assertThat(gb.getIntegerSet()).isNull(); } @Test @@ -240,17 +239,17 @@ public void testGenericSetListConstructor() throws Exception { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); - assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(gb.getResourceList().get(1)).isEqualTo(new UrlResource("http://localhost:9090")); } @Test public void testGenericSetListConstructorWithAutowiring() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerSingleton("integer1", new Integer(4)); - bf.registerSingleton("integer2", new Integer(5)); + bf.registerSingleton("integer1", 4); + bf.registerSingleton("integer2", 5); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); @@ -259,10 +258,10 @@ public void testGenericSetListConstructorWithAutowiring() throws Exception { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); - assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(gb.getResourceList().get(1)).isEqualTo(new UrlResource("http://localhost:9090")); } @Test @@ -276,8 +275,8 @@ public void testGenericSetListConstructorWithOptionalAutowiring() throws Excepti bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertNull(gb.getIntegerSet()); - assertNull(gb.getResourceList()); + assertThat(gb.getIntegerSet()).isNull(); + assertThat(gb.getResourceList()).isNull(); } @Test @@ -297,10 +296,10 @@ public void testGenericSetMapConstructor() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); } @Test @@ -317,9 +316,9 @@ public void testGenericMapResourceConstructor() throws Exception { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); } @Test @@ -339,13 +338,13 @@ public void testGenericMapMapConstructor() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertNotSame(gb.getPlainMap(), gb.getShortMap()); - assertEquals(2, gb.getPlainMap().size()); - assertEquals("0", gb.getPlainMap().get("1")); - assertEquals("3", gb.getPlainMap().get("2")); - assertEquals(2, gb.getShortMap().size()); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); + assertThat(gb.getShortMap()).isNotSameAs(gb.getPlainMap()); + assertThat(gb.getPlainMap().size()).isEqualTo(2); + assertThat(gb.getPlainMap().get("1")).isEqualTo("0"); + assertThat(gb.getPlainMap().get("2")).isEqualTo("3"); + assertThat(gb.getShortMap().size()).isEqualTo(2); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); } @Test @@ -362,13 +361,13 @@ public void testGenericMapMapConstructorWithSameRefAndConversion() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertNotSame(gb.getPlainMap(), gb.getShortMap()); - assertEquals(2, gb.getPlainMap().size()); - assertEquals("0", gb.getPlainMap().get("1")); - assertEquals("3", gb.getPlainMap().get("2")); - assertEquals(2, gb.getShortMap().size()); - assertEquals(new Integer(0), gb.getShortMap().get(new Short("1"))); - assertEquals(new Integer(3), gb.getShortMap().get(new Short("2"))); + assertThat(gb.getShortMap()).isNotSameAs(gb.getPlainMap()); + assertThat(gb.getPlainMap().size()).isEqualTo(2); + assertThat(gb.getPlainMap().get("1")).isEqualTo("0"); + assertThat(gb.getPlainMap().get("2")).isEqualTo("3"); + assertThat(gb.getShortMap().size()).isEqualTo(2); + assertThat(gb.getShortMap().get(new Short("1"))).isEqualTo(0); + assertThat(gb.getShortMap().get(new Short("2"))).isEqualTo(3); } @Test @@ -377,18 +376,18 @@ public void testGenericMapMapConstructorWithSameRefAndNoConversion() { RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); Map input = new HashMap<>(); - input.put(new Short((short) 1), new Integer(0)); - input.put(new Short((short) 2), new Integer(3)); + input.put(new Short((short) 1), 0); + input.put(new Short((short) 2), 3); rbd.getConstructorArgumentValues().addGenericArgumentValue(input); rbd.getConstructorArgumentValues().addGenericArgumentValue(input); bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertSame(gb.getPlainMap(), gb.getShortMap()); - assertEquals(2, gb.getShortMap().size()); - assertEquals(new Integer(0), gb.getShortMap().get(new Short("1"))); - assertEquals(new Integer(3), gb.getShortMap().get(new Short("2"))); + assertThat(gb.getShortMap()).isSameAs(gb.getPlainMap()); + assertThat(gb.getShortMap().size()).isEqualTo(2); + assertThat(gb.getShortMap().get(new Short("1"))).isEqualTo(0); + assertThat(gb.getShortMap().get(new Short("2"))).isEqualTo(3); } @Test @@ -404,8 +403,8 @@ public void testGenericMapWithKeyTypeConstructor() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertEquals("5", gb.getLongMap().get(new Long("4"))); - assertEquals("7", gb.getLongMap().get(new Long("6"))); + assertThat(gb.getLongMap().get(4L)).isEqualTo("5"); + assertThat(gb.getLongMap().get(6L)).isEqualTo("7"); } @Test @@ -421,7 +420,7 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { Map> input = new HashMap<>(); HashSet value1 = new HashSet<>(); - value1.add(new Integer(1)); + value1.add(1); input.put("1", value1); ArrayList value2 = new ArrayList<>(); value2.add(Boolean.TRUE); @@ -432,8 +431,10 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet); - assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList); + boolean condition1 = gb.getCollectionMap().get(1) instanceof HashSet; + assertThat(condition1).isTrue(); + boolean condition = gb.getCollectionMap().get(2) instanceof ArrayList; + assertThat(condition).isTrue(); } @@ -451,8 +452,8 @@ public void testGenericSetFactoryMethod() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); } @Test @@ -473,10 +474,10 @@ public void testGenericSetListFactoryMethod() throws Exception { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); - assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); + assertThat(gb.getResourceList().get(1)).isEqualTo(new UrlResource("http://localhost:9090")); } @Test @@ -497,10 +498,10 @@ public void testGenericSetMapFactoryMethod() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getIntegerSet().contains(new Integer(4))); - assertTrue(gb.getIntegerSet().contains(new Integer(5))); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); + assertThat(gb.getIntegerSet().contains(4)).isTrue(); + assertThat(gb.getIntegerSet().contains(5)).isTrue(); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); } @Test @@ -518,9 +519,9 @@ public void testGenericMapResourceFactoryMethod() throws Exception { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); - assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); + assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080")); } @Test @@ -541,10 +542,10 @@ public void testGenericMapMapFactoryMethod() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertEquals("0", gb.getPlainMap().get("1")); - assertEquals("3", gb.getPlainMap().get("2")); - assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); - assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); + assertThat(gb.getPlainMap().get("1")).isEqualTo("0"); + assertThat(gb.getPlainMap().get("2")).isEqualTo("3"); + assertThat(gb.getShortMap().get(new Short("4"))).isEqualTo(5); + assertThat(gb.getShortMap().get(new Short("6"))).isEqualTo(7); } @Test @@ -561,8 +562,8 @@ public void testGenericMapWithKeyTypeFactoryMethod() { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertEquals("5", gb.getLongMap().get(new Long("4"))); - assertEquals("7", gb.getLongMap().get(new Long("6"))); + assertThat(gb.getLongMap().get(new Long("4"))).isEqualTo("5"); + assertThat(gb.getLongMap().get(new Long("6"))).isEqualTo("7"); } @Test @@ -579,7 +580,7 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { Map> input = new HashMap<>(); HashSet value1 = new HashSet<>(); - value1.add(new Integer(1)); + value1.add(1); input.put("1", value1); ArrayList value2 = new ArrayList<>(); value2.add(Boolean.TRUE); @@ -590,8 +591,10 @@ public void registerCustomEditors(PropertyEditorRegistry registry) { bf.registerBeanDefinition("genericBean", rbd); GenericBean gb = (GenericBean) bf.getBean("genericBean"); - assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet); - assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList); + boolean condition1 = gb.getCollectionMap().get(1) instanceof HashSet; + assertThat(condition1).isTrue(); + boolean condition = gb.getCollectionMap().get(2) instanceof ArrayList; + assertThat(condition).isTrue(); } @Test @@ -600,8 +603,8 @@ public void testGenericListBean() throws Exception { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("genericBeanTests.xml", getClass())); List list = (List) bf.getBean("list"); - assertEquals(1, list.size()); - assertEquals(new URL("http://localhost:8080"), list.get(0)); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo(new URL("http://localhost:8080")); } @Test @@ -610,8 +613,8 @@ public void testGenericSetBean() throws Exception { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("genericBeanTests.xml", getClass())); Set set = (Set) bf.getBean("set"); - assertEquals(1, set.size()); - assertEquals(new URL("http://localhost:8080"), set.iterator().next()); + assertThat(set.size()).isEqualTo(1); + assertThat(set.iterator().next()).isEqualTo(new URL("http://localhost:8080")); } @Test @@ -620,9 +623,9 @@ public void testGenericMapBean() throws Exception { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("genericBeanTests.xml", getClass())); Map map = (Map) bf.getBean("map"); - assertEquals(1, map.size()); - assertEquals(new Integer(10), map.keySet().iterator().next()); - assertEquals(new URL("http://localhost:8080"), map.values().iterator().next()); + assertThat(map.size()).isEqualTo(1); + assertThat(map.keySet().iterator().next()).isEqualTo(10); + assertThat(map.values().iterator().next()).isEqualTo(new URL("http://localhost:8080")); } @Test @@ -631,9 +634,9 @@ public void testGenericallyTypedIntegerBean() { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("genericBeanTests.xml", getClass())); GenericIntegerBean gb = (GenericIntegerBean) bf.getBean("integerBean"); - assertEquals(new Integer(10), gb.getGenericProperty()); - assertEquals(new Integer(20), gb.getGenericListProperty().get(0)); - assertEquals(new Integer(30), gb.getGenericListProperty().get(1)); + assertThat(gb.getGenericProperty()).isEqualTo(10); + assertThat(gb.getGenericListProperty().get(0)).isEqualTo(20); + assertThat(gb.getGenericListProperty().get(1)).isEqualTo(30); } @Test @@ -642,21 +645,20 @@ public void testGenericallyTypedSetOfIntegerBean() { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("genericBeanTests.xml", getClass())); GenericSetOfIntegerBean gb = (GenericSetOfIntegerBean) bf.getBean("setOfIntegerBean"); - assertEquals(new Integer(10), gb.getGenericProperty().iterator().next()); - assertEquals(new Integer(20), gb.getGenericListProperty().get(0).iterator().next()); - assertEquals(new Integer(30), gb.getGenericListProperty().get(1).iterator().next()); + assertThat(gb.getGenericProperty().iterator().next()).isEqualTo(10); + assertThat(gb.getGenericListProperty().get(0).iterator().next()).isEqualTo(20); + assertThat(gb.getGenericListProperty().get(1).iterator().next()).isEqualTo(30); } @Test + @EnabledForTestGroups(LONG_RUNNING) public void testSetBean() throws Exception { - Assume.group(TestGroup.LONG_RUNNING); - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("genericBeanTests.xml", getClass())); UrlSet us = (UrlSet) bf.getBean("setBean"); - assertEquals(1, us.size()); - assertEquals(new URL("http://www.springframework.org"), us.iterator().next()); + assertThat(us.size()).isEqualTo(1); + assertThat(us.iterator().next()).isEqualTo(new URL("https://www.springframework.org")); } /** @@ -678,10 +680,10 @@ public void parameterizedStaticFactoryMethod() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("mock", rbd); - assertEquals(Runnable.class, bf.getType("mock")); - assertEquals(Runnable.class, bf.getType("mock")); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); Map beans = bf.getBeansOfType(Runnable.class); - assertEquals(1, beans.size()); + assertThat(beans.size()).isEqualTo(1); } /** @@ -708,12 +710,12 @@ public void parameterizedInstanceFactoryMethod() { rbd.getConstructorArgumentValues().addGenericArgumentValue(Runnable.class); bf.registerBeanDefinition("mock", rbd); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertEquals(Runnable.class, bf.getType("mock")); - assertEquals(Runnable.class, bf.getType("mock")); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); Map beans = bf.getBeansOfType(Runnable.class); - assertEquals(1, beans.size()); + assertThat(beans.size()).isEqualTo(1); } @Test @@ -729,12 +731,12 @@ public void parameterizedInstanceFactoryMethodWithNonResolvedClassName() { rbd.getConstructorArgumentValues().addGenericArgumentValue(Runnable.class.getName()); bf.registerBeanDefinition("mock", rbd); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertEquals(Runnable.class, bf.getType("mock")); - assertEquals(Runnable.class, bf.getType("mock")); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); Map beans = bf.getBeansOfType(Runnable.class); - assertEquals(1, beans.size()); + assertThat(beans.size()).isEqualTo(1); } @Test @@ -748,12 +750,12 @@ public void parameterizedInstanceFactoryMethodWithWrappedClassName() { rbd.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue(Runnable.class.getName())); bf.registerBeanDefinition("mock", rbd); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertEquals(Runnable.class, bf.getType("mock")); - assertEquals(Runnable.class, bf.getType("mock")); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); Map beans = bf.getBeansOfType(Runnable.class); - assertEquals(1, beans.size()); + assertThat(beans.size()).isEqualTo(1); } @Test @@ -769,12 +771,12 @@ public void parameterizedInstanceFactoryMethodWithInvalidClassName() { rbd.getConstructorArgumentValues().addGenericArgumentValue("x"); bf.registerBeanDefinition("mock", rbd); - assertFalse(bf.isTypeMatch("mock", Runnable.class)); - assertFalse(bf.isTypeMatch("mock", Runnable.class)); - assertNull(bf.getType("mock")); - assertNull(bf.getType("mock")); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isFalse(); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isFalse(); + assertThat(bf.getType("mock")).isNull(); + assertThat(bf.getType("mock")).isNull(); Map beans = bf.getBeansOfType(Runnable.class); - assertEquals(0, beans.size()); + assertThat(beans.size()).isEqualTo(0); } @Test @@ -790,12 +792,12 @@ public void parameterizedInstanceFactoryMethodWithIndexedArgument() { rbd.getConstructorArgumentValues().addIndexedArgumentValue(0, Runnable.class); bf.registerBeanDefinition("mock", rbd); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertEquals(Runnable.class, bf.getType("mock")); - assertEquals(Runnable.class, bf.getType("mock")); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); Map beans = bf.getBeansOfType(Runnable.class); - assertEquals(1, beans.size()); + assertThat(beans.size()).isEqualTo(1); } @Test // SPR-16720 @@ -812,12 +814,12 @@ public void parameterizedInstanceFactoryMethodWithTempClassLoader() { rbd.getConstructorArgumentValues().addGenericArgumentValue(Runnable.class); bf.registerBeanDefinition("mock", rbd); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertTrue(bf.isTypeMatch("mock", Runnable.class)); - assertEquals(Runnable.class, bf.getType("mock")); - assertEquals(Runnable.class, bf.getType("mock")); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue(); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); + assertThat(bf.getType("mock")).isEqualTo(Runnable.class); Map beans = bf.getBeansOfType(Runnable.class); - assertEquals(1, beans.size()); + assertThat(beans.size()).isEqualTo(1); } @Test @@ -831,17 +833,17 @@ public void testGenericMatchingWithBeanNameDifferentiation() { new RootBeanDefinition(NumberBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false)); NumberBean nb = bf.getBean(NumberBean.class); - assertSame(bf.getBean("doubleStore"), nb.getDoubleStore()); - assertSame(bf.getBean("floatStore"), nb.getFloatStore()); + assertThat(nb.getDoubleStore()).isSameAs(bf.getBean("doubleStore")); + assertThat(nb.getFloatStore()).isSameAs(bf.getBean("floatStore")); String[] numberStoreNames = bf.getBeanNamesForType(ResolvableType.forClass(NumberStore.class)); String[] doubleStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Double.class)); String[] floatStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Float.class)); - assertEquals(2, numberStoreNames.length); - assertEquals("doubleStore", numberStoreNames[0]); - assertEquals("floatStore", numberStoreNames[1]); - assertEquals(0, doubleStoreNames.length); - assertEquals(0, floatStoreNames.length); + assertThat(numberStoreNames.length).isEqualTo(2); + assertThat(numberStoreNames[0]).isEqualTo("doubleStore"); + assertThat(numberStoreNames[1]).isEqualTo("floatStore"); + assertThat(doubleStoreNames.length).isEqualTo(0); + assertThat(floatStoreNames.length).isEqualTo(0); } @Test @@ -860,92 +862,80 @@ public void testGenericMatchingWithFullTypeDifferentiation() { new RootBeanDefinition(NumberBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false)); NumberBean nb = bf.getBean(NumberBean.class); - assertSame(bf.getBean("store1"), nb.getDoubleStore()); - assertSame(bf.getBean("store2"), nb.getFloatStore()); + assertThat(nb.getDoubleStore()).isSameAs(bf.getBean("store1")); + assertThat(nb.getFloatStore()).isSameAs(bf.getBean("store2")); String[] numberStoreNames = bf.getBeanNamesForType(ResolvableType.forClass(NumberStore.class)); String[] doubleStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Double.class)); String[] floatStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Float.class)); - assertEquals(2, numberStoreNames.length); - assertEquals("store1", numberStoreNames[0]); - assertEquals("store2", numberStoreNames[1]); - assertEquals(1, doubleStoreNames.length); - assertEquals("store1", doubleStoreNames[0]); - assertEquals(1, floatStoreNames.length); - assertEquals("store2", floatStoreNames[0]); + assertThat(numberStoreNames.length).isEqualTo(2); + assertThat(numberStoreNames[0]).isEqualTo("store1"); + assertThat(numberStoreNames[1]).isEqualTo("store2"); + assertThat(doubleStoreNames.length).isEqualTo(1); + assertThat(doubleStoreNames[0]).isEqualTo("store1"); + assertThat(floatStoreNames.length).isEqualTo(1); + assertThat(floatStoreNames[0]).isEqualTo("store2"); ObjectProvider> numberStoreProvider = bf.getBeanProvider(ResolvableType.forClass(NumberStore.class)); ObjectProvider> doubleStoreProvider = bf.getBeanProvider(ResolvableType.forClassWithGenerics(NumberStore.class, Double.class)); ObjectProvider> floatStoreProvider = bf.getBeanProvider(ResolvableType.forClassWithGenerics(NumberStore.class, Float.class)); - try { - numberStoreProvider.getObject(); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - try { - numberStoreProvider.getIfAvailable(); - fail("Should have thrown NoUniqueBeanDefinitionException"); - } - catch (NoUniqueBeanDefinitionException ex) { - // expected - } - assertNull(numberStoreProvider.getIfUnique()); - assertSame(bf.getBean("store1"), doubleStoreProvider.getObject()); - assertSame(bf.getBean("store1"), doubleStoreProvider.getIfAvailable()); - assertSame(bf.getBean("store1"), doubleStoreProvider.getIfUnique()); - assertSame(bf.getBean("store2"), floatStoreProvider.getObject()); - assertSame(bf.getBean("store2"), floatStoreProvider.getIfAvailable()); - assertSame(bf.getBean("store2"), floatStoreProvider.getIfUnique()); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(numberStoreProvider::getObject); + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(numberStoreProvider::getIfAvailable); + assertThat(numberStoreProvider.getIfUnique()).isNull(); + assertThat(doubleStoreProvider.getObject()).isSameAs(bf.getBean("store1")); + assertThat(doubleStoreProvider.getIfAvailable()).isSameAs(bf.getBean("store1")); + assertThat(doubleStoreProvider.getIfUnique()).isSameAs(bf.getBean("store1")); + assertThat(floatStoreProvider.getObject()).isSameAs(bf.getBean("store2")); + assertThat(floatStoreProvider.getIfAvailable()).isSameAs(bf.getBean("store2")); + assertThat(floatStoreProvider.getIfUnique()).isSameAs(bf.getBean("store2")); List> resolved = new ArrayList<>(); for (NumberStore instance : numberStoreProvider) { resolved.add(instance); } - assertEquals(2, resolved.size()); - assertSame(bf.getBean("store1"), resolved.get(0)); - assertSame(bf.getBean("store2"), resolved.get(1)); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.get(0)).isSameAs(bf.getBean("store1")); + assertThat(resolved.get(1)).isSameAs(bf.getBean("store2")); resolved = numberStoreProvider.stream().collect(Collectors.toList()); - assertEquals(2, resolved.size()); - assertSame(bf.getBean("store1"), resolved.get(0)); - assertSame(bf.getBean("store2"), resolved.get(1)); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.get(0)).isSameAs(bf.getBean("store1")); + assertThat(resolved.get(1)).isSameAs(bf.getBean("store2")); resolved = numberStoreProvider.orderedStream().collect(Collectors.toList()); - assertEquals(2, resolved.size()); - assertSame(bf.getBean("store2"), resolved.get(0)); - assertSame(bf.getBean("store1"), resolved.get(1)); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.get(0)).isSameAs(bf.getBean("store2")); + assertThat(resolved.get(1)).isSameAs(bf.getBean("store1")); resolved = new ArrayList<>(); for (NumberStore instance : doubleStoreProvider) { resolved.add(instance); } - assertEquals(1, resolved.size()); - assertTrue(resolved.contains(bf.getBean("store1"))); + assertThat(resolved.size()).isEqualTo(1); + assertThat(resolved.contains(bf.getBean("store1"))).isTrue(); resolved = doubleStoreProvider.stream().collect(Collectors.toList()); - assertEquals(1, resolved.size()); - assertTrue(resolved.contains(bf.getBean("store1"))); + assertThat(resolved.size()).isEqualTo(1); + assertThat(resolved.contains(bf.getBean("store1"))).isTrue(); resolved = doubleStoreProvider.orderedStream().collect(Collectors.toList()); - assertEquals(1, resolved.size()); - assertTrue(resolved.contains(bf.getBean("store1"))); + assertThat(resolved.size()).isEqualTo(1); + assertThat(resolved.contains(bf.getBean("store1"))).isTrue(); resolved = new ArrayList<>(); for (NumberStore instance : floatStoreProvider) { resolved.add(instance); } - assertEquals(1, resolved.size()); - assertTrue(resolved.contains(bf.getBean("store2"))); + assertThat(resolved.size()).isEqualTo(1); + assertThat(resolved.contains(bf.getBean("store2"))).isTrue(); resolved = floatStoreProvider.stream().collect(Collectors.toList()); - assertEquals(1, resolved.size()); - assertTrue(resolved.contains(bf.getBean("store2"))); + assertThat(resolved.size()).isEqualTo(1); + assertThat(resolved.contains(bf.getBean("store2"))).isTrue(); resolved = floatStoreProvider.orderedStream().collect(Collectors.toList()); - assertEquals(1, resolved.size()); - assertTrue(resolved.contains(bf.getBean("store2"))); + assertThat(resolved.size()).isEqualTo(1); + assertThat(resolved.contains(bf.getBean("store2"))).isTrue(); } @Test @@ -963,9 +953,9 @@ public void testGenericMatchingWithUnresolvedOrderedStream() { ObjectProvider> numberStoreProvider = bf.getBeanProvider(ResolvableType.forClass(NumberStore.class)); List> resolved = numberStoreProvider.orderedStream().collect(Collectors.toList()); - assertEquals(2, resolved.size()); - assertSame(bf.getBean("store2"), resolved.get(0)); - assertSame(bf.getBean("store1"), resolved.get(1)); + assertThat(resolved.size()).isEqualTo(2); + assertThat(resolved.get(0)).isSameAs(bf.getBean("store2")); + assertThat(resolved.get(1)).isSameAs(bf.getBean("store1")); } @@ -987,9 +977,9 @@ public static class NamedUrlMap extends HashMap { public static class CollectionDependentBean { public CollectionDependentBean(NamedUrlList list, NamedUrlSet set, NamedUrlMap map) { - assertEquals(1, list.size()); - assertEquals(1, set.size()); - assertEquals(1, map.size()); + assertThat(list.size()).isEqualTo(1); + assertThat(set.size()).isEqualTo(1); + assertThat(map.size()).isEqualTo(1); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistryTests.java index 1b49c287abc3..6e3f8465e9e8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,14 +16,14 @@ package org.springframework.beans.factory.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeansException; import org.springframework.beans.factory.ObjectFactory; -import org.springframework.tests.sample.beans.DerivedTestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.DerivedTestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -38,7 +38,7 @@ public void testSingletons() { TestBean tb = new TestBean(); beanRegistry.registerSingleton("tb", tb); - assertSame(tb, beanRegistry.getSingleton("tb")); + assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb); TestBean tb2 = (TestBean) beanRegistry.getSingleton("tb2", new ObjectFactory() { @Override @@ -46,19 +46,19 @@ public Object getObject() throws BeansException { return new TestBean(); } }); - assertSame(tb2, beanRegistry.getSingleton("tb2")); + assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2); - assertSame(tb, beanRegistry.getSingleton("tb")); - assertSame(tb2, beanRegistry.getSingleton("tb2")); - assertEquals(2, beanRegistry.getSingletonCount()); + assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb); + assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2); + assertThat(beanRegistry.getSingletonCount()).isEqualTo(2); String[] names = beanRegistry.getSingletonNames(); - assertEquals(2, names.length); - assertEquals("tb", names[0]); - assertEquals("tb2", names[1]); + assertThat(names.length).isEqualTo(2); + assertThat(names[0]).isEqualTo("tb"); + assertThat(names[1]).isEqualTo("tb2"); beanRegistry.destroySingletons(); - assertEquals(0, beanRegistry.getSingletonCount()); - assertEquals(0, beanRegistry.getSingletonNames().length); + assertThat(beanRegistry.getSingletonCount()).isEqualTo(0); + assertThat(beanRegistry.getSingletonNames().length).isEqualTo(0); } @Test @@ -68,19 +68,19 @@ public void testDisposableBean() { DerivedTestBean tb = new DerivedTestBean(); beanRegistry.registerSingleton("tb", tb); beanRegistry.registerDisposableBean("tb", tb); - assertSame(tb, beanRegistry.getSingleton("tb")); + assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb); - assertSame(tb, beanRegistry.getSingleton("tb")); - assertEquals(1, beanRegistry.getSingletonCount()); + assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb); + assertThat(beanRegistry.getSingletonCount()).isEqualTo(1); String[] names = beanRegistry.getSingletonNames(); - assertEquals(1, names.length); - assertEquals("tb", names[0]); - assertFalse(tb.wasDestroyed()); + assertThat(names.length).isEqualTo(1); + assertThat(names[0]).isEqualTo("tb"); + assertThat(tb.wasDestroyed()).isFalse(); beanRegistry.destroySingletons(); - assertEquals(0, beanRegistry.getSingletonCount()); - assertEquals(0, beanRegistry.getSingletonNames().length); - assertTrue(tb.wasDestroyed()); + assertThat(beanRegistry.getSingletonCount()).isEqualTo(0); + assertThat(beanRegistry.getSingletonNames().length).isEqualTo(0); + assertThat(tb.wasDestroyed()).isTrue(); } @Test @@ -90,15 +90,15 @@ public void testDependentRegistration() { beanRegistry.registerDependentBean("a", "b"); beanRegistry.registerDependentBean("b", "c"); beanRegistry.registerDependentBean("c", "b"); - assertTrue(beanRegistry.isDependent("a", "b")); - assertTrue(beanRegistry.isDependent("b", "c")); - assertTrue(beanRegistry.isDependent("c", "b")); - assertTrue(beanRegistry.isDependent("a", "c")); - assertFalse(beanRegistry.isDependent("c", "a")); - assertFalse(beanRegistry.isDependent("b", "a")); - assertFalse(beanRegistry.isDependent("a", "a")); - assertTrue(beanRegistry.isDependent("b", "b")); - assertTrue(beanRegistry.isDependent("c", "c")); + assertThat(beanRegistry.isDependent("a", "b")).isTrue(); + assertThat(beanRegistry.isDependent("b", "c")).isTrue(); + assertThat(beanRegistry.isDependent("c", "b")).isTrue(); + assertThat(beanRegistry.isDependent("a", "c")).isTrue(); + assertThat(beanRegistry.isDependent("c", "a")).isFalse(); + assertThat(beanRegistry.isDependent("b", "a")).isFalse(); + assertThat(beanRegistry.isDependent("a", "a")).isFalse(); + assertThat(beanRegistry.isDependent("b", "b")).isTrue(); + assertThat(beanRegistry.isDependent("c", "c")).isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/DefinitionMetadataEqualsHashCodeTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/DefinitionMetadataEqualsHashCodeTests.java index 67d05e16a390..36c0c4be39cc 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/DefinitionMetadataEqualsHashCodeTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/DefinitionMetadataEqualsHashCodeTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -16,13 +16,13 @@ package org.springframework.beans.factory.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.RuntimeBeanReference; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests for {@code equals()} and {@code hashCode()} in bean definitions. @@ -68,8 +68,8 @@ public void rootBeanDefinitionAndMethodOverridesWithDifferentOverloadedValues() // override in 'master' will not. But... the bean definitions should still be // considered equal. - assertEquals("Should be equal", master, equal); - assertEquals("Hash code for equal instances must match", master.hashCode(), equal.hashCode()); + assertThat(equal).as("Should be equal").isEqualTo(master); + assertThat(equal.hashCode()).as("Hash code for equal instances must match").isEqualTo(master.hashCode()); } @Test @@ -122,14 +122,14 @@ private void setBaseProperties(AbstractBeanDefinition definition) { } private void assertEqualsAndHashCodeContracts(Object master, Object equal, Object notEqual, Object subclass) { - assertEquals("Should be equal", master, equal); - assertEquals("Hash code for equal instances should match", master.hashCode(), equal.hashCode()); + assertThat(equal).as("Should be equal").isEqualTo(master); + assertThat(equal.hashCode()).as("Hash code for equal instances should match").isEqualTo(master.hashCode()); - assertNotEquals("Should not be equal", master, notEqual); - assertNotEquals("Hash code for non-equal instances should not match", master.hashCode(), notEqual.hashCode()); + assertThat(notEqual).as("Should not be equal").isNotEqualTo(master); + assertThat(notEqual.hashCode()).as("Hash code for non-equal instances should not match").isNotEqualTo(master.hashCode()); - assertEquals("Subclass should be equal", master, subclass); - assertEquals("Hash code for subclass should match", master.hashCode(), subclass.hashCode()); + assertThat(subclass).as("Subclass should be equal").isEqualTo(master); + assertThat(subclass.hashCode()).as("Hash code for subclass should match").isEqualTo(master.hashCode()); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/LookupMethodTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/LookupMethodTests.java index dac823f36894..eeb34b6f8f1e 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/LookupMethodTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/LookupMethodTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,14 +16,15 @@ package org.springframework.beans.factory.support; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Karl Pietrzak @@ -34,7 +35,7 @@ public class LookupMethodTests { private DefaultListableBeanFactory beanFactory; - @Before + @BeforeEach public void setUp() { beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); @@ -45,59 +46,55 @@ public void setUp() { @Test public void testWithoutConstructorArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); Object expected = bean.get(); - assertEquals(TestBean.class, expected.getClass()); + assertThat(expected.getClass()).isEqualTo(TestBean.class); } @Test public void testWithOverloadedArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); TestBean expected = bean.get("haha"); - assertEquals(TestBean.class, expected.getClass()); - assertEquals("haha", expected.getName()); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(expected.getName()).isEqualTo("haha"); } @Test public void testWithOneConstructorArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); TestBean expected = bean.getOneArgument("haha"); - assertEquals(TestBean.class, expected.getClass()); - assertEquals("haha", expected.getName()); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(expected.getName()).isEqualTo("haha"); } @Test public void testWithTwoConstructorArg() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); TestBean expected = bean.getTwoArguments("haha", 72); - assertEquals(TestBean.class, expected.getClass()); - assertEquals("haha", expected.getName()); - assertEquals(72, expected.getAge()); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(expected.getName()).isEqualTo("haha"); + assertThat(expected.getAge()).isEqualTo(72); } @Test public void testWithThreeArgsShouldFail() { AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean"); - assertNotNull(bean); - try { - bean.getThreeArguments("name", 1, 2); - fail("TestBean does not have a three arg constructor so this should not have worked"); - } - catch (AbstractMethodError ex) { - } + assertThat(bean).isNotNull(); + assertThatExceptionOfType(AbstractMethodError.class).as("does not have a three arg constructor").isThrownBy(() -> + bean.getThreeArguments("name", 1, 2)); } @Test public void testWithOverriddenLookupMethod() { AbstractBean bean = (AbstractBean) beanFactory.getBean("extendedBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); TestBean expected = bean.getOneArgument("haha"); - assertEquals(TestBean.class, expected.getClass()); - assertEquals("haha", expected.getName()); - assertTrue(expected.isJedi()); + assertThat(expected.getClass()).isEqualTo(TestBean.class); + assertThat(expected.getName()).isEqualTo("haha"); + assertThat(expected.isJedi()).isTrue(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java index 75e40faad9a2..9bf6576d518c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,11 @@ import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Rick Evans @@ -39,7 +41,7 @@ public void mergeSunnyDay() { child.add("three"); child.setMergeEnabled(true); List mergedList = child.merge(parent); - assertEquals("merge() obviously did not work.", 3, mergedList.size()); + assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3); } @Test @@ -47,21 +49,23 @@ public void mergeWithNullParent() { ManagedList child = new ManagedList(); child.add("one"); child.setMergeEnabled(true); - assertSame(child, child.merge(null)); + assertThat(child.merge(null)).isSameAs(child); } - @Test(expected = IllegalStateException.class) + @Test public void mergeNotAllowedWhenMergeNotEnabled() { ManagedList child = new ManagedList(); - child.merge(null); + assertThatIllegalStateException().isThrownBy(() -> + child.merge(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void mergeWithNonCompatibleParentType() { ManagedList child = new ManagedList(); child.add("one"); child.setMergeEnabled(true); - child.merge("hello"); + assertThatIllegalArgumentException().isThrownBy(() -> + child.merge("hello")); } @Test @@ -72,7 +76,7 @@ public void mergeEmptyChild() { ManagedList child = new ManagedList(); child.setMergeEnabled(true); List mergedList = child.merge(parent); - assertEquals("merge() obviously did not work.", 2, mergedList.size()); + assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(2); } @Test @@ -85,7 +89,7 @@ public void mergeChildValuesOverrideTheParents() { child.add("one"); child.setMergeEnabled(true); List mergedList = child.merge(parent); - assertEquals("merge() obviously did not work.", 3, mergedList.size()); + assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java index 0b537fd116da..1f6dc5e416a4 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,11 @@ import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Rick Evans @@ -39,26 +41,28 @@ public void mergeSunnyDay() { child.put("three", "three"); child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); - assertEquals("merge() obviously did not work.", 3, mergedMap.size()); + assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(3); } @Test public void mergeWithNullParent() { ManagedMap child = new ManagedMap(); child.setMergeEnabled(true); - assertSame(child, child.merge(null)); + assertThat(child.merge(null)).isSameAs(child); } - @Test(expected = IllegalArgumentException.class) + @Test public void mergeWithNonCompatibleParentType() { ManagedMap map = new ManagedMap(); map.setMergeEnabled(true); - map.merge("hello"); + assertThatIllegalArgumentException().isThrownBy(() -> + map.merge("hello")); } - @Test(expected = IllegalStateException.class) + @Test public void mergeNotAllowedWhenMergeNotEnabled() { - new ManagedMap().merge(null); + assertThatIllegalStateException().isThrownBy(() -> + new ManagedMap().merge(null)); } @Test @@ -69,7 +73,7 @@ public void mergeEmptyChild() { ManagedMap child = new ManagedMap(); child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); - assertEquals("merge() obviously did not work.", 2, mergedMap.size()); + assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(2); } @Test @@ -82,8 +86,8 @@ public void mergeChildValuesOverrideTheParents() { child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); // child value for 'one' must override parent value... - assertEquals("merge() obviously did not work.", 2, mergedMap.size()); - assertEquals("Parent value not being overridden during merge().", "fork", mergedMap.get("one")); + assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(2); + assertThat(mergedMap.get("one")).as("Parent value not being overridden during merge().").isEqualTo("fork"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedPropertiesTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedPropertiesTests.java index 80cc3a5cd184..473679f3d107 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedPropertiesTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedPropertiesTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,11 @@ import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Rick Evans @@ -39,27 +41,29 @@ public void mergeSunnyDay() { child.setProperty("three", "three"); child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); - assertEquals("merge() obviously did not work.", 3, mergedMap.size()); + assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(3); } @Test public void mergeWithNullParent() { ManagedProperties child = new ManagedProperties(); child.setMergeEnabled(true); - assertSame(child, child.merge(null)); + assertThat(child.merge(null)).isSameAs(child); } - @Test(expected = IllegalArgumentException.class) + @Test public void mergeWithNonCompatibleParentType() { ManagedProperties map = new ManagedProperties(); map.setMergeEnabled(true); - map.merge("hello"); + assertThatIllegalArgumentException().isThrownBy(() -> + map.merge("hello")); } - @Test(expected = IllegalStateException.class) + @Test public void mergeNotAllowedWhenMergeNotEnabled() { ManagedProperties map = new ManagedProperties(); - map.merge(null); + assertThatIllegalStateException().isThrownBy(() -> + map.merge(null)); } @Test @@ -70,7 +74,7 @@ public void mergeEmptyChild() { ManagedProperties child = new ManagedProperties(); child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); - assertEquals("merge() obviously did not work.", 2, mergedMap.size()); + assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(2); } @Test @@ -83,8 +87,8 @@ public void mergeChildValuesOverrideTheParents() { child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); // child value for 'one' must override parent value... - assertEquals("merge() obviously did not work.", 2, mergedMap.size()); - assertEquals("Parent value not being overridden during merge().", "fork", mergedMap.get("one")); + assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(2); + assertThat(mergedMap.get("one")).as("Parent value not being overridden during merge().").isEqualTo("fork"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java index 260a00d99af1..39c08997814c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,11 @@ import java.util.Set; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Rick Evans @@ -39,7 +41,7 @@ public void mergeSunnyDay() { child.add("three"); child.setMergeEnabled(true); Set mergedSet = child.merge(parent); - assertEquals("merge() obviously did not work.", 3, mergedSet.size()); + assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(3); } @Test @@ -47,20 +49,22 @@ public void mergeWithNullParent() { ManagedSet child = new ManagedSet(); child.add("one"); child.setMergeEnabled(true); - assertSame(child, child.merge(null)); + assertThat(child.merge(null)).isSameAs(child); } - @Test(expected = IllegalStateException.class) + @Test public void mergeNotAllowedWhenMergeNotEnabled() { - new ManagedSet().merge(null); + assertThatIllegalStateException().isThrownBy(() -> + new ManagedSet().merge(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void mergeWithNonCompatibleParentType() { ManagedSet child = new ManagedSet(); child.add("one"); child.setMergeEnabled(true); - child.merge("hello"); + assertThatIllegalArgumentException().isThrownBy(() -> + child.merge("hello")); } @Test @@ -71,7 +75,7 @@ public void mergeEmptyChild() { ManagedSet child = new ManagedSet(); child.setMergeEnabled(true); Set mergedSet = child.merge(parent); - assertEquals("merge() obviously did not work.", 2, mergedSet.size()); + assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2); } @Test @@ -84,7 +88,7 @@ public void mergeChildValuesOverrideTheParents() { child.add("one"); child.setMergeEnabled(true); Set mergedSet = child.merge(parent); - assertEquals("merge() obviously did not work.", 2, mergedSet.size()); + assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReaderTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReaderTests.java index 2479b0177d7f..dbb70e54b9b0 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReaderTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReaderTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,12 +16,12 @@ package org.springframework.beans.factory.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -37,7 +37,7 @@ public class PropertiesBeanDefinitionReaderTests { public void withSimpleConstructorArg() { this.reader.loadBeanDefinitions(new ClassPathResource("simpleConstructorArg.properties", getClass())); TestBean bean = (TestBean) this.beanFactory.getBean("testBean"); - assertEquals("Rob Harrop", bean.getName()); + assertThat(bean.getName()).isEqualTo("Rob Harrop"); } @Test @@ -45,15 +45,15 @@ public void withConstructorArgRef() { this.reader.loadBeanDefinitions(new ClassPathResource("refConstructorArg.properties", getClass())); TestBean rob = (TestBean) this.beanFactory.getBean("rob"); TestBean sally = (TestBean) this.beanFactory.getBean("sally"); - assertEquals(sally, rob.getSpouse()); + assertThat(rob.getSpouse()).isEqualTo(sally); } @Test public void withMultipleConstructorsArgs() { this.reader.loadBeanDefinitions(new ClassPathResource("multiConstructorArgs.properties", getClass())); TestBean bean = (TestBean) this.beanFactory.getBean("testBean"); - assertEquals("Rob Harrop", bean.getName()); - assertEquals(23, bean.getAge()); + assertThat(bean.getName()).isEqualTo("Rob Harrop"); + assertThat(bean.getAge()).isEqualTo(23); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/QualifierAnnotationAutowireBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/QualifierAnnotationAutowireBeanFactoryTests.java index e47dacb02186..f660a8af020d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/QualifierAnnotationAutowireBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/QualifierAnnotationAutowireBeanFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -21,8 +21,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.config.ConstructorArgumentValues; @@ -31,7 +31,7 @@ import org.springframework.core.MethodParameter; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Mark Fisher @@ -51,11 +51,11 @@ public void testAutowireCandidateDefaultWithIrrelevantDescriptor() throws Except cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition rbd = new RootBeanDefinition(Person.class, cavs, null); lbf.registerBeanDefinition(JUERGEN, rbd); - assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, - new DependencyDescriptor(Person.class.getDeclaredField("name"), false))); - assertTrue(lbf.isAutowireCandidate(JUERGEN, - new DependencyDescriptor(Person.class.getDeclaredField("name"), true))); + assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, + new DependencyDescriptor(Person.class.getDeclaredField("name"), false))).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, + new DependencyDescriptor(Person.class.getDeclaredField("name"), true))).isTrue(); } @Test @@ -66,14 +66,14 @@ public void testAutowireCandidateExplicitlyFalseWithIrrelevantDescriptor() throw RootBeanDefinition rbd = new RootBeanDefinition(Person.class, cavs, null); rbd.setAutowireCandidate(false); lbf.registerBeanDefinition(JUERGEN, rbd); - assertFalse(lbf.isAutowireCandidate(JUERGEN, null)); - assertFalse(lbf.isAutowireCandidate(JUERGEN, - new DependencyDescriptor(Person.class.getDeclaredField("name"), false))); - assertFalse(lbf.isAutowireCandidate(JUERGEN, - new DependencyDescriptor(Person.class.getDeclaredField("name"), true))); + assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isFalse(); + assertThat(lbf.isAutowireCandidate(JUERGEN, + new DependencyDescriptor(Person.class.getDeclaredField("name"), false))).isFalse(); + assertThat(lbf.isAutowireCandidate(JUERGEN, + new DependencyDescriptor(Person.class.getDeclaredField("name"), true))).isFalse(); } - @Ignore + @Disabled @Test public void testAutowireCandidateWithFieldDescriptor() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); @@ -90,12 +90,12 @@ public void testAutowireCandidateWithFieldDescriptor() throws Exception { QualifiedTestBean.class.getDeclaredField("qualified"), false); DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("nonqualified"), false); - assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); - assertTrue(lbf.isAutowireCandidate(MARK, null)); - assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor)); - assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)); + assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, null)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isFalse(); } @Test @@ -111,9 +111,9 @@ public void testAutowireCandidateExplicitlyFalseWithFieldDescriptor() throws Exc QualifiedTestBean.class.getDeclaredField("qualified"), false); DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("nonqualified"), false); - assertFalse(lbf.isAutowireCandidate(JUERGEN, null)); - assertFalse(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)); - assertFalse(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); + assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isFalse(); + assertThat(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)).isFalse(); + assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isFalse(); } @Test @@ -128,12 +128,12 @@ public void testAutowireCandidateWithShortClassName() throws Exception { QualifiedTestBean.class.getDeclaredField("qualified"), false); DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("nonqualified"), false); - assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); + assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue(); } - @Ignore + @Disabled @Test public void testAutowireCandidateWithConstructorDescriptor() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); @@ -149,13 +149,13 @@ public void testAutowireCandidateWithConstructorDescriptor() throws Exception { MethodParameter param = new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0); DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(param, false); param.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); - assertEquals("tpb", param.getParameterName()); - assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); - assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)); + assertThat(param.getParameterName()).isEqualTo("tpb"); + assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isFalse(); } - @Ignore + @Disabled @Test public void testAutowireCandidateWithMethodDescriptor() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); @@ -175,15 +175,15 @@ public void testAutowireCandidateWithMethodDescriptor() throws Exception { DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(qualifiedParam, false); DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(nonqualifiedParam, false); qualifiedParam.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); - assertEquals("tpb", qualifiedParam.getParameterName()); + assertThat(qualifiedParam.getParameterName()).isEqualTo("tpb"); nonqualifiedParam.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); - assertEquals("tpb", nonqualifiedParam.getParameterName()); - assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)); - assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); - assertTrue(lbf.isAutowireCandidate(MARK, null)); - assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor)); - assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)); + assertThat(nonqualifiedParam.getParameterName()).isEqualTo("tpb"); + assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, null)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isFalse(); } @Test @@ -202,8 +202,8 @@ public void testAutowireCandidateWithMultipleCandidatesDescriptor() throws Excep DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor( new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0), false); - assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); - assertTrue(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)); + assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue(); + assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isTrue(); } @@ -244,7 +244,7 @@ public String getName() { @Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Qualifier - private static @interface TestQualifier { + private @interface TestQualifier { } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/Spr8954Tests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/Spr8954Tests.java index 719381c6b4d5..aa463b93e2d2 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/Spr8954Tests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/Spr8954Tests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,15 +19,15 @@ import java.util.Arrays; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; + /** * Unit tests for SPR-8954, in which a custom {@link InstantiationAwareBeanPostProcessor} @@ -44,7 +44,7 @@ public class Spr8954Tests { private DefaultListableBeanFactory bf; - @Before + @BeforeEach public void setUp() { bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("foo", new RootBeanDefinition(FooFactoryBean.class)); @@ -53,32 +53,32 @@ public void setUp() { @Test public void repro() { - assertThat(bf.getBean("foo"), instanceOf(Foo.class)); - assertThat(bf.getBean("&foo"), instanceOf(FooFactoryBean.class)); - assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true)); + assertThat(bf.getBean("foo")).isInstanceOf(Foo.class); + assertThat(bf.getBean("&foo")).isInstanceOf(FooFactoryBean.class); + assertThat(bf.isTypeMatch("&foo", FactoryBean.class)).isTrue(); @SuppressWarnings("rawtypes") Map fbBeans = bf.getBeansOfType(FactoryBean.class); - assertThat(fbBeans.size(), is(1)); - assertThat(fbBeans.keySet(), hasItem("&foo")); + assertThat(fbBeans).hasSize(1); + assertThat(fbBeans.keySet()).contains("&foo"); Map aiBeans = bf.getBeansOfType(AnInterface.class); - assertThat(aiBeans.size(), is(1)); - assertThat(aiBeans.keySet(), hasItem("&foo")); + assertThat(aiBeans).hasSize(1); + assertThat(aiBeans.keySet()).contains("&foo"); } @Test public void findsBeansByTypeIfNotInstantiated() { - assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true)); + assertThat(bf.isTypeMatch("&foo", FactoryBean.class)).isTrue(); @SuppressWarnings("rawtypes") Map fbBeans = bf.getBeansOfType(FactoryBean.class); - assertThat(1, equalTo(fbBeans.size())); - assertThat("&foo", equalTo(fbBeans.keySet().iterator().next())); + assertThat(1).isEqualTo(fbBeans.size()); + assertThat("&foo").isEqualTo(fbBeans.keySet().iterator().next()); Map aiBeans = bf.getBeansOfType(AnInterface.class); - assertThat(aiBeans.size(), is(1)); - assertThat(aiBeans.keySet(), hasItem("&foo")); + assertThat(aiBeans).hasSize(1); + assertThat(aiBeans.keySet()).contains("&foo"); } /** @@ -87,11 +87,11 @@ public void findsBeansByTypeIfNotInstantiated() { @Test public void findsFactoryBeanNameByTypeWithoutInstantiation() { String[] names = bf.getBeanNamesForType(AnInterface.class, false, false); - assertThat(Arrays.asList(names), hasItem("&foo")); + assertThat(Arrays.asList(names)).contains("&foo"); Map beans = bf.getBeansOfType(AnInterface.class, false, false); - assertThat(beans.size(), is(1)); - assertThat(beans.keySet(), hasItem("&foo")); + assertThat(beans).hasSize(1); + assertThat(beans.keySet()).contains("&foo"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java index 73cf1ffc7898..84cb04da5994 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -28,11 +28,13 @@ import java.security.ProtectionDomain; import java.util.PropertyPermission; import java.util.Set; +import java.util.function.Consumer; + import javax.security.auth.AuthPermission; import javax.security.auth.Subject; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanClassLoaderAware; @@ -43,23 +45,26 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.SmartFactoryBean; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.SecurityContextProvider; import org.springframework.beans.factory.support.security.support.ConstructorBean; import org.springframework.beans.factory.support.security.support.CustomCallbackBean; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.core.NestedRuntimeException; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; +import org.springframework.core.testfixture.security.TestPrincipal; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Security test case. Checks whether the container uses its privileges for its * internal work but does not leak them when touching/calling user code. * - *t The first half of the test case checks that permissions are downgraded when + *

The first half of the test case checks that permissions are downgraded when * calling user code while the second half that the caller code permission get * through and Spring doesn't override the permission stack. * @@ -109,7 +114,7 @@ public Object getListProperty() { } private void checkCurrentContext() { - assertEquals(expectedName, getCurrentSubjectName()); + assertThat(getCurrentSubjectName()).isEqualTo(expectedName); } } @@ -154,11 +159,11 @@ public void setBeanFactory(BeanFactory beanFactory) } private void checkCurrentContext() { - assertEquals(expectedName, getCurrentSubjectName()); + assertThat(getCurrentSubjectName()).isEqualTo(expectedName); } } - @SuppressWarnings("unused") + @SuppressWarnings({ "unused", "rawtypes" }) private static class NonPrivilegedFactoryBean implements SmartFactoryBean { private String expectedName; @@ -198,7 +203,7 @@ public boolean isSingleton() { } private void checkCurrentContext() { - assertEquals(expectedName, getCurrentSubjectName()); + assertThat(getCurrentSubjectName()).isEqualTo(expectedName); } } @@ -209,16 +214,16 @@ private static class NonPrivilegedFactory { public NonPrivilegedFactory(String expected) { this.expectedName = expected; - assertEquals(expectedName, getCurrentSubjectName()); + assertThat(getCurrentSubjectName()).isEqualTo(expectedName); } public static Object makeStaticInstance(String expectedName) { - assertEquals(expectedName, getCurrentSubjectName()); + assertThat(getCurrentSubjectName()).isEqualTo(expectedName); return new Object(); } public Object makeInstance() { - assertEquals(expectedName, getCurrentSubjectName()); + assertThat(getCurrentSubjectName()).isEqualTo(expectedName); return new Object(); } } @@ -248,37 +253,6 @@ public String run() { }); } - private static class TestPrincipal implements Principal { - - private String name; - - public TestPrincipal(String name) { - this.name = name; - } - - @Override - public String getName() { - return this.name; - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof TestPrincipal)) { - return false; - } - TestPrincipal p = (TestPrincipal) obj; - return this.name.equals(p.name); - } - - @Override - public int hashCode() { - return this.name.hashCode(); - } - } - public CallbacksSecurityTests() { // setup security if (System.getSecurityManager() == null) { @@ -294,7 +268,7 @@ public CallbacksSecurityTests() { } } - @Before + @BeforeEach public void setUp() throws Exception { final ProtectionDomain empty = new ProtectionDomain(null, @@ -321,145 +295,93 @@ public AccessControlContext getAccessControlContext() { @Test public void testSecuritySanity() throws Exception { AccessControlContext acc = provider.getAccessControlContext(); - try { - acc.checkPermission(new PropertyPermission("*", "read")); - fail("Acc should not have any permissions"); - } - catch (SecurityException se) { - // expected - } + assertThatExceptionOfType(SecurityException.class).as( + "Acc should not have any permissions").isThrownBy(() -> + acc.checkPermission(new PropertyPermission("*", "read"))); - final CustomCallbackBean bean = new CustomCallbackBean(); - final Method method = bean.getClass().getMethod("destroy"); + CustomCallbackBean bean = new CustomCallbackBean(); + Method method = bean.getClass().getMethod("destroy"); method.setAccessible(true); - try { - AccessController.doPrivileged(new PrivilegedExceptionAction() { - - @Override - public Object run() throws Exception { - method.invoke(bean); - return null; - } - }, acc); - fail("expected security exception"); - } - catch (Exception ex) { - } + assertThatExceptionOfType(Exception.class).isThrownBy(() -> + AccessController.doPrivileged((PrivilegedExceptionAction) () -> { + method.invoke(bean); + return null; + }, acc)); - final Class cl = ConstructorBean.class; - try { - AccessController.doPrivileged( - new PrivilegedExceptionAction() { - - @Override - public Object run() throws Exception { - return cl.newInstance(); - } - }, acc); - fail("expected security exception"); - } - catch (Exception ex) { - } + Class cl = ConstructorBean.class; + assertThatExceptionOfType(Exception.class).isThrownBy(() -> + AccessController.doPrivileged((PrivilegedExceptionAction) () -> + cl.newInstance(), acc)); } @Test public void testSpringInitBean() throws Exception { - try { - beanFactory.getBean("spring-init"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getCause() instanceof SecurityException); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("spring-init")) + .withCauseInstanceOf(SecurityException.class); } @Test public void testCustomInitBean() throws Exception { - try { - beanFactory.getBean("custom-init"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getCause() instanceof SecurityException); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("custom-init")) + .withCauseInstanceOf(SecurityException.class); } @Test public void testSpringDestroyBean() throws Exception { beanFactory.getBean("spring-destroy"); beanFactory.destroySingletons(); - assertNull(System.getProperty("security.destroy")); + assertThat(System.getProperty("security.destroy")).isNull(); } @Test public void testCustomDestroyBean() throws Exception { beanFactory.getBean("custom-destroy"); beanFactory.destroySingletons(); - assertNull(System.getProperty("security.destroy")); + assertThat(System.getProperty("security.destroy")).isNull(); } @Test public void testCustomFactoryObject() throws Exception { - try { - beanFactory.getBean("spring-factory"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getCause() instanceof SecurityException); - } - + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("spring-factory")) + .withCauseInstanceOf(SecurityException.class); } @Test public void testCustomFactoryType() throws Exception { - assertNull(beanFactory.getType("spring-factory")); - assertNull(System.getProperty("factory.object.type")); + assertThat(beanFactory.getType("spring-factory")).isNull(); + assertThat(System.getProperty("factory.object.type")).isNull(); } @Test public void testCustomStaticFactoryMethod() throws Exception { - try { - beanFactory.getBean("custom-static-factory-method"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getMostSpecificCause() instanceof SecurityException); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("custom-static-factory-method")) + .satisfies(ex -> assertThat(ex.getMostSpecificCause()).isInstanceOf(SecurityException.class)); } @Test public void testCustomInstanceFactoryMethod() throws Exception { - try { - beanFactory.getBean("custom-factory-method"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getMostSpecificCause() instanceof SecurityException); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("custom-factory-method")) + .satisfies(ex -> assertThat(ex.getMostSpecificCause()).isInstanceOf(SecurityException.class)); } @Test public void testTrustedFactoryMethod() throws Exception { - try { - beanFactory.getBean("privileged-static-factory-method"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getMostSpecificCause() instanceof SecurityException); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("privileged-static-factory-method")) + .satisfies(mostSpecificCauseOf(SecurityException.class)); } @Test public void testConstructor() throws Exception { - try { - beanFactory.getBean("constructor"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - // expected - assertTrue(ex.getMostSpecificCause() instanceof SecurityException); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("constructor")) + .satisfies(mostSpecificCauseOf(SecurityException.class)); } @Test @@ -479,14 +401,9 @@ public Object run() throws Exception { @Test public void testPropertyInjection() throws Exception { - try { - beanFactory.getBean("property-injection"); - fail("expected security exception"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getMessage().contains("security")); - } - + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + beanFactory.getBean("property-injection")) + .withMessageContaining("security"); beanFactory.getBean("working-property-injection"); } @@ -495,7 +412,7 @@ public void testInitSecurityAwarePrototypeBean() { final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); BeanDefinitionBuilder bdb = BeanDefinitionBuilder .genericBeanDefinition(NonPrivilegedBean.class).setScope( - ConfigurableBeanFactory.SCOPE_PROTOTYPE) + BeanDefinition.SCOPE_PROTOTYPE) .setInitMethodName("init").setDestroyMethodName("destroy") .addConstructorArgValue("user1"); lbf.registerBeanDefinition("test", bdb.getBeanDefinition()); @@ -509,7 +426,7 @@ public NonPrivilegedBean run() { return lbf.getBean("test", NonPrivilegedBean.class); } }, null); - assertNotNull(bean); + assertThat(bean).isNotNull(); } @Test @@ -531,8 +448,8 @@ public void testTrustedExecution() throws Exception { @Override public Object run() { // sanity check - assertEquals("user1", getCurrentSubjectName()); - assertEquals(false, NonPrivilegedBean.destroyed); + assertThat(getCurrentSubjectName()).isEqualTo("user1"); + assertThat(NonPrivilegedBean.destroyed).isEqualTo(false); beanFactory.getBean("trusted-spring-callbacks"); beanFactory.getBean("trusted-custom-init-destroy"); @@ -548,9 +465,15 @@ public Object run() { beanFactory.getBean("trusted-working-property-injection"); beanFactory.destroySingletons(); - assertEquals(true, NonPrivilegedBean.destroyed); + assertThat(NonPrivilegedBean.destroyed).isEqualTo(true); return null; } }, provider.getAccessControlContext()); } + + private Consumer mostSpecificCauseOf(Class type) { + return ex -> assertThat(ex.getMostSpecificCause()).isInstanceOf(type); + + } + } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/ConstructorBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/ConstructorBean.java index a68028c5921d..fc60fc3db145 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/ConstructorBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/ConstructorBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomCallbackBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomCallbackBean.java index cf72ae3a95ba..4874306e6e12 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomCallbackBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomCallbackBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomFactoryBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomFactoryBean.java index 4df91dc13600..4ec3d7131bf9 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomFactoryBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/CustomFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.beans.factory.support.security.support; import java.util.Properties; @@ -22,15 +23,15 @@ /** * @author Costin Leau */ -public class CustomFactoryBean implements FactoryBean { +public class CustomFactoryBean implements FactoryBean { @Override - public Object getObject() throws Exception { + public Properties getObject() throws Exception { return System.getProperties(); } @Override - public Class getObjectType() { + public Class getObjectType() { System.setProperty("factory.object.type", "true"); return Properties.class; } @@ -39,4 +40,5 @@ public Class getObjectType() { public boolean isSingleton() { return true; } + } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/DestroyBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/DestroyBean.java index 41bc80b8ab8b..67005abf7836 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/DestroyBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/DestroyBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/FactoryBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/FactoryBean.java index 87a4ec27fcb9..4f7fb62e5be2 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/FactoryBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/FactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/InitBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/InitBean.java index 2c371011e36d..3693bb9d749e 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/InitBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/InitBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/PropertyBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/PropertyBean.java index 22131ab9adcf..51933137f0da 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/PropertyBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/security/support/PropertyBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanConfigurerSupportTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanConfigurerSupportTests.java index d6df28bef27f..8ff2952fb113 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanConfigurerSupportTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanConfigurerSupportTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,15 +16,18 @@ package org.springframework.beans.factory.wiring; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; /** * @author Rick Evans @@ -33,9 +36,10 @@ */ public class BeanConfigurerSupportTests { - @Test(expected = IllegalArgumentException.class) + @Test public void supplyIncompatibleBeanFactoryImplementation() throws Exception { - new StubBeanConfigurerSupport().setBeanFactory(mock(BeanFactory.class)); + assertThatIllegalArgumentException().isThrownBy(() -> + new StubBeanConfigurerSupport().setBeanFactory(mock(BeanFactory.class))); } @Test @@ -49,7 +53,7 @@ public void configureBeanDoesNothingIfBeanWiringInfoResolverResolvesToNull() thr configurer.setBeanFactory(new DefaultListableBeanFactory()); configurer.configureBean(beanInstance); verify(resolver).resolveWiringInfo(beanInstance); - assertNull(beanInstance.getName()); + assertThat(beanInstance.getName()).isNull(); } @Test @@ -57,7 +61,7 @@ public void configureBeanDoesNothingIfNoBeanFactoryHasBeenSet() throws Exception TestBean beanInstance = new TestBean(); BeanConfigurerSupport configurer = new StubBeanConfigurerSupport(); configurer.configureBean(beanInstance); - assertNull(beanInstance.getName()); + assertThat(beanInstance.getName()).isNull(); } @Test @@ -73,7 +77,7 @@ public void configureBeanReallyDoesDefaultToUsingTheFullyQualifiedClassNameOfThe configurer.setBeanFactory(factory); configurer.afterPropertiesSet(); configurer.configureBean(beanInstance); - assertEquals("Bean is evidently not being configured (for some reason)", "Harriet Wheeler", beanInstance.getName()); + assertThat(beanInstance.getName()).as("Bean is evidently not being configured (for some reason)").isEqualTo("Harriet Wheeler"); } @Test @@ -93,7 +97,7 @@ public void configureBeanPerformsAutowiringByNameIfAppropriateBeanWiringInfoReso configurer.setBeanFactory(factory); configurer.setBeanWiringInfoResolver(resolver); configurer.configureBean(beanInstance); - assertEquals("Bean is evidently not being configured (for some reason)", "David Gavurin", beanInstance.getSpouse().getName()); + assertThat(beanInstance.getSpouse().getName()).as("Bean is evidently not being configured (for some reason)").isEqualTo("David Gavurin"); } @Test @@ -113,7 +117,7 @@ public void configureBeanPerformsAutowiringByTypeIfAppropriateBeanWiringInfoReso configurer.setBeanFactory(factory); configurer.setBeanWiringInfoResolver(resolver); configurer.configureBean(beanInstance); - assertEquals("Bean is evidently not being configured (for some reason)", "David Gavurin", beanInstance.getSpouse().getName()); + assertThat(beanInstance.getSpouse().getName()).as("Bean is evidently not being configured (for some reason)").isEqualTo("David Gavurin"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanWiringInfoTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanWiringInfoTests.java index 639b868b5957..7919ba0a0b45 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanWiringInfoTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/wiring/BeanWiringInfoTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,10 @@ package org.springframework.beans.factory.wiring; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for the BeanWiringInfo class. @@ -28,53 +29,58 @@ */ public class BeanWiringInfoTests { - @Test(expected = IllegalArgumentException.class) + @Test public void ctorWithNullBeanName() throws Exception { - new BeanWiringInfo(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new BeanWiringInfo(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void ctorWithWhitespacedBeanName() throws Exception { - new BeanWiringInfo(" \t"); + assertThatIllegalArgumentException().isThrownBy(() -> + new BeanWiringInfo(" \t")); } - @Test(expected = IllegalArgumentException.class) + @Test public void ctorWithEmptyBeanName() throws Exception { - new BeanWiringInfo(""); + assertThatIllegalArgumentException().isThrownBy(() -> + new BeanWiringInfo("")); } - @Test(expected = IllegalArgumentException.class) + @Test public void ctorWithNegativeIllegalAutowiringValue() throws Exception { - new BeanWiringInfo(-1, true); + assertThatIllegalArgumentException().isThrownBy(() -> + new BeanWiringInfo(-1, true)); } - @Test(expected = IllegalArgumentException.class) + @Test public void ctorWithPositiveOutOfRangeAutowiringValue() throws Exception { - new BeanWiringInfo(123871, true); + assertThatIllegalArgumentException().isThrownBy(() -> + new BeanWiringInfo(123871, true)); } @Test public void usingAutowireCtorIndicatesAutowiring() throws Exception { BeanWiringInfo info = new BeanWiringInfo(BeanWiringInfo.AUTOWIRE_BY_NAME, true); - assertTrue(info.indicatesAutowiring()); + assertThat(info.indicatesAutowiring()).isTrue(); } @Test public void usingBeanNameCtorDoesNotIndicateAutowiring() throws Exception { BeanWiringInfo info = new BeanWiringInfo("fooService"); - assertFalse(info.indicatesAutowiring()); + assertThat(info.indicatesAutowiring()).isFalse(); } @Test public void noDependencyCheckValueIsPreserved() throws Exception { BeanWiringInfo info = new BeanWiringInfo(BeanWiringInfo.AUTOWIRE_BY_NAME, true); - assertTrue(info.getDependencyCheck()); + assertThat(info.getDependencyCheck()).isTrue(); } @Test public void dependencyCheckValueIsPreserved() throws Exception { BeanWiringInfo info = new BeanWiringInfo(BeanWiringInfo.AUTOWIRE_BY_TYPE, false); - assertFalse(info.getDependencyCheck()); + assertThat(info.getDependencyCheck()).isFalse(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/wiring/ClassNameBeanWiringInfoResolverTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/wiring/ClassNameBeanWiringInfoResolverTests.java index 60d4645e91c7..67cef6f11665 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/wiring/ClassNameBeanWiringInfoResolverTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/wiring/ClassNameBeanWiringInfoResolverTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,10 @@ package org.springframework.beans.factory.wiring; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for the ClassNameBeanWiringInfoResolver class. @@ -27,9 +28,10 @@ */ public class ClassNameBeanWiringInfoResolverTests { - @Test(expected = IllegalArgumentException.class) + @Test public void resolveWiringInfoWithNullBeanInstance() throws Exception { - new ClassNameBeanWiringInfoResolver().resolveWiringInfo(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new ClassNameBeanWiringInfoResolver().resolveWiringInfo(null)); } @Test @@ -37,9 +39,8 @@ public void resolveWiringInfo() { ClassNameBeanWiringInfoResolver resolver = new ClassNameBeanWiringInfoResolver(); Long beanInstance = new Long(1); BeanWiringInfo info = resolver.resolveWiringInfo(beanInstance); - assertNotNull(info); - assertEquals("Not resolving bean name to the class name of the supplied bean instance as per class contract.", - beanInstance.getClass().getName(), info.getBeanName()); + assertThat(info).isNotNull(); + assertThat(info.getBeanName()).as("Not resolving bean name to the class name of the supplied bean instance as per class contract.").isEqualTo(beanInstance.getClass().getName()); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/AutowireWithExclusionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/AutowireWithExclusionTests.java index 0bd5cc414af4..8ea0719a82be 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/AutowireWithExclusionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/AutowireWithExclusionTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,16 +16,16 @@ package org.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -40,8 +40,8 @@ public void byTypeAutowireWithAutoSelfExclusion() throws Exception { beanFactory.preInstantiateSingletons(); TestBean rob = (TestBean) beanFactory.getBean("rob"); TestBean sally = (TestBean) beanFactory.getBean("sally"); - assertEquals(sally, rob.getSpouse()); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSpouse()).isEqualTo(sally); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -50,8 +50,8 @@ public void byTypeAutowireWithExclusion() throws Exception { DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-exclusion.xml"); beanFactory.preInstantiateSingletons(); TestBean rob = (TestBean) beanFactory.getBean("rob"); - assertEquals("props1", rob.getSomeProperties().getProperty("name")); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props1"); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -65,8 +65,8 @@ public void byTypeAutowireWithExclusionInParentFactory() throws Exception { robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally")); child.registerBeanDefinition("rob2", robDef); TestBean rob = (TestBean) child.getBean("rob2"); - assertEquals("props1", rob.getSomeProperties().getProperty("name")); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props1"); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -84,8 +84,8 @@ public void byTypeAutowireWithPrimaryInParentFactory() throws Exception { propsDef.getPropertyValues().add("properties", "name=props3"); child.registerBeanDefinition("props3", propsDef); TestBean rob = (TestBean) child.getBean("rob2"); - assertEquals("props1", rob.getSomeProperties().getProperty("name")); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props1"); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -103,8 +103,8 @@ public void byTypeAutowireWithPrimaryOverridingParentFactory() throws Exception propsDef.setPrimary(true); child.registerBeanDefinition("props3", propsDef); TestBean rob = (TestBean) child.getBean("rob2"); - assertEquals("props3", rob.getSomeProperties().getProperty("name")); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props3"); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -123,8 +123,8 @@ public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception { propsDef.setPrimary(true); child.registerBeanDefinition("props3", propsDef); TestBean rob = (TestBean) child.getBean("rob2"); - assertEquals("props3", rob.getSomeProperties().getProperty("name")); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props3"); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -133,8 +133,8 @@ public void byTypeAutowireWithInclusion() throws Exception { DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-inclusion.xml"); beanFactory.preInstantiateSingletons(); TestBean rob = (TestBean) beanFactory.getBean("rob"); - assertEquals("props1", rob.getSomeProperties().getProperty("name")); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props1"); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -143,8 +143,8 @@ public void byTypeAutowireWithSelectiveInclusion() throws Exception { DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-selective-inclusion.xml"); beanFactory.preInstantiateSingletons(); TestBean rob = (TestBean) beanFactory.getBean("rob"); - assertEquals("props1", rob.getSomeProperties().getProperty("name")); - assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props1"); + assertThat(CountingFactory.getFactoryBeanInstanceCount()).isEqualTo(1); } @Test @@ -152,19 +152,19 @@ public void constructorAutowireWithAutoSelfExclusion() throws Exception { DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-constructor-with-exclusion.xml"); TestBean rob = (TestBean) beanFactory.getBean("rob"); TestBean sally = (TestBean) beanFactory.getBean("sally"); - assertEquals(sally, rob.getSpouse()); + assertThat(rob.getSpouse()).isEqualTo(sally); TestBean rob2 = (TestBean) beanFactory.getBean("rob"); - assertEquals(rob, rob2); - assertNotSame(rob, rob2); - assertEquals(rob.getSpouse(), rob2.getSpouse()); - assertNotSame(rob.getSpouse(), rob2.getSpouse()); + assertThat(rob2).isEqualTo(rob); + assertThat(rob2).isNotSameAs(rob); + assertThat(rob2.getSpouse()).isEqualTo(rob.getSpouse()); + assertThat(rob2.getSpouse()).isNotSameAs(rob.getSpouse()); } @Test public void constructorAutowireWithExclusion() throws Exception { DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-constructor-with-exclusion.xml"); TestBean rob = (TestBean) beanFactory.getBean("rob"); - assertEquals("props1", rob.getSomeProperties().getProperty("name")); + assertThat(rob.getSomeProperties().getProperty("name")).isEqualTo("props1"); } private DefaultListableBeanFactory getBeanFactory(String configPath) { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/BeanNameGenerationTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/BeanNameGenerationTests.java index c8367982196c..55fef5cb34b5 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/BeanNameGenerationTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/BeanNameGenerationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,14 +16,14 @@ package org.springframework.beans.factory.xml; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.core.io.ClassPathResource; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -34,7 +34,7 @@ public class BeanNameGenerationTests { private DefaultListableBeanFactory beanFactory; - @Before + @BeforeEach public void setUp() { this.beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); @@ -48,21 +48,21 @@ public void naming() { String targetName = className + BeanDefinitionReaderUtils.GENERATED_BEAN_NAME_SEPARATOR + "0"; GeneratedNameBean topLevel1 = (GeneratedNameBean) beanFactory.getBean(targetName); - assertNotNull(topLevel1); + assertThat(topLevel1).isNotNull(); targetName = className + BeanDefinitionReaderUtils.GENERATED_BEAN_NAME_SEPARATOR + "1"; GeneratedNameBean topLevel2 = (GeneratedNameBean) beanFactory.getBean(targetName); - assertNotNull(topLevel2); + assertThat(topLevel2).isNotNull(); GeneratedNameBean child1 = topLevel1.getChild(); - assertNotNull(child1.getBeanName()); - assertTrue(child1.getBeanName().startsWith(className)); + assertThat(child1.getBeanName()).isNotNull(); + assertThat(child1.getBeanName().startsWith(className)).isTrue(); GeneratedNameBean child2 = topLevel2.getChild(); - assertNotNull(child2.getBeanName()); - assertTrue(child2.getBeanName().startsWith(className)); + assertThat(child2.getBeanName()).isNotNull(); + assertThat(child2.getBeanName().startsWith(className)).isTrue(); - assertFalse(child1.getBeanName().equals(child2.getBeanName())); + assertThat(child1.getBeanName().equals(child2.getBeanName())).isFalse(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionMergingTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionMergingTests.java index b0cdb5f023ae..04b82ca200a8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionMergingTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionMergingTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,15 +22,15 @@ import java.util.Properties; import java.util.Set; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit and integration tests for the collection merging support. @@ -44,7 +44,7 @@ public class CollectionMergingTests { private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - @Before + @BeforeEach public void setUp() throws Exception { BeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); reader.loadBeanDefinitions(new ClassPathResource("collectionMerging.xml", getClass())); @@ -54,148 +54,154 @@ public void setUp() throws Exception { public void mergeList() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithList"); List list = bean.getSomeList(); - assertEquals("Incorrect size", 3, list.size()); - assertEquals("Rob Harrop", list.get(0)); - assertEquals("Rod Johnson", list.get(1)); - assertEquals("Juergen Hoeller", list.get(2)); + assertThat(list.size()).as("Incorrect size").isEqualTo(3); + assertThat(list.get(0)).isEqualTo("Rob Harrop"); + assertThat(list.get(1)).isEqualTo("Rod Johnson"); + assertThat(list.get(2)).isEqualTo("Juergen Hoeller"); } @Test public void mergeListWithInnerBeanAsListElement() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithListOfRefs"); - List list = bean.getSomeList(); - assertNotNull(list); - assertEquals(3, list.size()); - assertNotNull(list.get(2)); - assertTrue(list.get(2) instanceof TestBean); + List list = bean.getSomeList(); + assertThat(list).isNotNull(); + assertThat(list.size()).isEqualTo(3); + assertThat(list.get(2)).isNotNull(); + boolean condition = list.get(2) instanceof TestBean; + assertThat(condition).isTrue(); } @Test public void mergeSet() { TestBean bean = (TestBean) this.beanFactory.getBean("childWithSet"); Set set = bean.getSomeSet(); - assertEquals("Incorrect size", 2, set.size()); - assertTrue(set.contains("Rob Harrop")); - assertTrue(set.contains("Sally Greenwood")); + assertThat(set.size()).as("Incorrect size").isEqualTo(2); + assertThat(set.contains("Rob Harrop")).isTrue(); + assertThat(set.contains("Sally Greenwood")).isTrue(); } @Test public void mergeSetWithInnerBeanAsSetElement() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithSetOfRefs"); - Set set = bean.getSomeSet(); - assertNotNull(set); - assertEquals(2, set.size()); + Set set = bean.getSomeSet(); + assertThat(set).isNotNull(); + assertThat(set.size()).isEqualTo(2); Iterator it = set.iterator(); it.next(); Object o = it.next(); - assertNotNull(o); - assertTrue(o instanceof TestBean); - assertEquals("Sally", ((TestBean) o).getName()); + assertThat(o).isNotNull(); + boolean condition = o instanceof TestBean; + assertThat(condition).isTrue(); + assertThat(((TestBean) o).getName()).isEqualTo("Sally"); } @Test public void mergeMap() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithMap"); Map map = bean.getSomeMap(); - assertEquals("Incorrect size", 3, map.size()); - assertEquals("Sally", map.get("Rob")); - assertEquals("Kerry", map.get("Rod")); - assertEquals("Eva", map.get("Juergen")); + assertThat(map.size()).as("Incorrect size").isEqualTo(3); + assertThat(map.get("Rob")).isEqualTo("Sally"); + assertThat(map.get("Rod")).isEqualTo("Kerry"); + assertThat(map.get("Juergen")).isEqualTo("Eva"); } @Test public void mergeMapWithInnerBeanAsMapEntryValue() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithMapOfRefs"); - Map map = bean.getSomeMap(); - assertNotNull(map); - assertEquals(2, map.size()); - assertNotNull(map.get("Rob")); - assertTrue(map.get("Rob") instanceof TestBean); - assertEquals("Sally", ((TestBean) map.get("Rob")).getName()); + Map map = bean.getSomeMap(); + assertThat(map).isNotNull(); + assertThat(map.size()).isEqualTo(2); + assertThat(map.get("Rob")).isNotNull(); + boolean condition = map.get("Rob") instanceof TestBean; + assertThat(condition).isTrue(); + assertThat(((TestBean) map.get("Rob")).getName()).isEqualTo("Sally"); } @Test public void mergeProperties() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithProps"); Properties props = bean.getSomeProperties(); - assertEquals("Incorrect size", 3, props.size()); - assertEquals("Sally", props.getProperty("Rob")); - assertEquals("Kerry",props.getProperty("Rod")); - assertEquals("Eva", props.getProperty("Juergen")); + assertThat(props.size()).as("Incorrect size").isEqualTo(3); + assertThat(props.getProperty("Rob")).isEqualTo("Sally"); + assertThat(props.getProperty("Rod")).isEqualTo("Kerry"); + assertThat(props.getProperty("Juergen")).isEqualTo("Eva"); } @Test public void mergeListInConstructor() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithListInConstructor"); List list = bean.getSomeList(); - assertEquals("Incorrect size", 3, list.size()); - assertEquals("Rob Harrop", list.get(0)); - assertEquals("Rod Johnson", list.get(1)); - assertEquals("Juergen Hoeller", list.get(2)); + assertThat(list.size()).as("Incorrect size").isEqualTo(3); + assertThat(list.get(0)).isEqualTo("Rob Harrop"); + assertThat(list.get(1)).isEqualTo("Rod Johnson"); + assertThat(list.get(2)).isEqualTo("Juergen Hoeller"); } @Test public void mergeListWithInnerBeanAsListElementInConstructor() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithListOfRefsInConstructor"); - List list = bean.getSomeList(); - assertNotNull(list); - assertEquals(3, list.size()); - assertNotNull(list.get(2)); - assertTrue(list.get(2) instanceof TestBean); + List list = bean.getSomeList(); + assertThat(list).isNotNull(); + assertThat(list.size()).isEqualTo(3); + assertThat(list.get(2)).isNotNull(); + boolean condition = list.get(2) instanceof TestBean; + assertThat(condition).isTrue(); } @Test public void mergeSetInConstructor() { TestBean bean = (TestBean) this.beanFactory.getBean("childWithSetInConstructor"); Set set = bean.getSomeSet(); - assertEquals("Incorrect size", 2, set.size()); - assertTrue(set.contains("Rob Harrop")); - assertTrue(set.contains("Sally Greenwood")); + assertThat(set.size()).as("Incorrect size").isEqualTo(2); + assertThat(set.contains("Rob Harrop")).isTrue(); + assertThat(set.contains("Sally Greenwood")).isTrue(); } @Test public void mergeSetWithInnerBeanAsSetElementInConstructor() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithSetOfRefsInConstructor"); - Set set = bean.getSomeSet(); - assertNotNull(set); - assertEquals(2, set.size()); + Set set = bean.getSomeSet(); + assertThat(set).isNotNull(); + assertThat(set.size()).isEqualTo(2); Iterator it = set.iterator(); it.next(); Object o = it.next(); - assertNotNull(o); - assertTrue(o instanceof TestBean); - assertEquals("Sally", ((TestBean) o).getName()); + assertThat(o).isNotNull(); + boolean condition = o instanceof TestBean; + assertThat(condition).isTrue(); + assertThat(((TestBean) o).getName()).isEqualTo("Sally"); } @Test public void mergeMapInConstructor() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithMapInConstructor"); Map map = bean.getSomeMap(); - assertEquals("Incorrect size", 3, map.size()); - assertEquals("Sally", map.get("Rob")); - assertEquals("Kerry", map.get("Rod")); - assertEquals("Eva", map.get("Juergen")); + assertThat(map.size()).as("Incorrect size").isEqualTo(3); + assertThat(map.get("Rob")).isEqualTo("Sally"); + assertThat(map.get("Rod")).isEqualTo("Kerry"); + assertThat(map.get("Juergen")).isEqualTo("Eva"); } @Test public void mergeMapWithInnerBeanAsMapEntryValueInConstructor() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithMapOfRefsInConstructor"); - Map map = bean.getSomeMap(); - assertNotNull(map); - assertEquals(2, map.size()); - assertNotNull(map.get("Rob")); - assertTrue(map.get("Rob") instanceof TestBean); - assertEquals("Sally", ((TestBean) map.get("Rob")).getName()); + Map map = bean.getSomeMap(); + assertThat(map).isNotNull(); + assertThat(map.size()).isEqualTo(2); + assertThat(map.get("Rob")).isNotNull(); + boolean condition = map.get("Rob") instanceof TestBean; + assertThat(condition).isTrue(); + assertThat(((TestBean) map.get("Rob")).getName()).isEqualTo("Sally"); } @Test public void mergePropertiesInConstructor() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("childWithPropsInConstructor"); Properties props = bean.getSomeProperties(); - assertEquals("Incorrect size", 3, props.size()); - assertEquals("Sally", props.getProperty("Rob")); - assertEquals("Kerry", props.getProperty("Rod")); - assertEquals("Eva", props.getProperty("Juergen")); + assertThat(props.size()).as("Incorrect size").isEqualTo(3); + assertThat(props.getProperty("Rob")).isEqualTo("Sally"); + assertThat(props.getProperty("Rod")).isEqualTo("Kerry"); + assertThat(props.getProperty("Juergen")).isEqualTo("Eva"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionsWithDefaultTypesTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionsWithDefaultTypesTests.java index cbbc2f760f91..8a67fea2c1ba 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionsWithDefaultTypesTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/CollectionsWithDefaultTypesTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -19,13 +19,13 @@ import java.util.List; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -45,7 +45,7 @@ public CollectionsWithDefaultTypesTests() { public void testListHasDefaultType() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("testBean"); for (Object o : bean.getSomeList()) { - assertEquals("Value type is incorrect", Integer.class, o.getClass()); + assertThat(o.getClass()).as("Value type is incorrect").isEqualTo(Integer.class); } } @@ -53,7 +53,7 @@ public void testListHasDefaultType() throws Exception { public void testSetHasDefaultType() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("testBean"); for (Object o : bean.getSomeSet()) { - assertEquals("Value type is incorrect", Integer.class, o.getClass()); + assertThat(o.getClass()).as("Value type is incorrect").isEqualTo(Integer.class); } } @@ -69,26 +69,27 @@ public void testMapWithNestedElementsHasDefaultKeyAndValueType() throws Exceptio assertMap(bean.getSomeMap()); } + @SuppressWarnings("rawtypes") private void assertMap(Map map) { for (Map.Entry entry : map.entrySet()) { - assertEquals("Key type is incorrect", Integer.class, entry.getKey().getClass()); - assertEquals("Value type is incorrect", Boolean.class, entry.getValue().getClass()); + assertThat(entry.getKey().getClass()).as("Key type is incorrect").isEqualTo(Integer.class); + assertThat(entry.getValue().getClass()).as("Value type is incorrect").isEqualTo(Boolean.class); } } @Test + @SuppressWarnings("rawtypes") public void testBuildCollectionFromMixtureOfReferencesAndValues() throws Exception { MixedCollectionBean jumble = (MixedCollectionBean) this.beanFactory.getBean("jumble"); - assertTrue("Expected 3 elements, not " + jumble.getJumble().size(), - jumble.getJumble().size() == 3); + assertThat(jumble.getJumble().size() == 3).as("Expected 3 elements, not " + jumble.getJumble().size()).isTrue(); List l = (List) jumble.getJumble(); - assertTrue(l.get(0).equals("literal")); + assertThat(l.get(0).equals("literal")).isTrue(); Integer[] array1 = (Integer[]) l.get(1); - assertTrue(array1[0].equals(new Integer(2))); - assertTrue(array1[1].equals(new Integer(4))); + assertThat(array1[0].equals(2)).isTrue(); + assertThat(array1[1].equals(4)).isTrue(); int[] array2 = (int[]) l.get(2); - assertTrue(array2[0] == 3); - assertTrue(array2[1] == 5); + assertThat(array2[0] == 3).isTrue(); + assertThat(array2[1] == 5).isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/ConstructorDependenciesBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/ConstructorDependenciesBean.java index a98e99ef1021..1d3c6286d312 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/ConstructorDependenciesBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/ConstructorDependenciesBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,8 +18,8 @@ import java.io.Serializable; -import org.springframework.tests.sample.beans.IndexedTestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.IndexedTestBean; +import org.springframework.beans.testfixture.beans.TestBean; /** * Simple bean used to check constructor dependency checking. diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/CountingFactory.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/CountingFactory.java index f5becb6cc2a1..21ccf619b38b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/CountingFactory.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/CountingFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,12 +17,12 @@ package org.springframework.beans.factory.xml; import org.springframework.beans.factory.FactoryBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; /** * @author Juergen Hoeller */ -public class CountingFactory implements FactoryBean { +public class CountingFactory implements FactoryBean { private static int factoryBeanInstanceCount = 0; @@ -51,12 +51,12 @@ public void setTestBean(TestBean tb) { @Override - public Object getObject() { + public String getObject() { return "myString"; } @Override - public Class getObjectType() { + public Class getObjectType() { return String.class; } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java index 8271c61bfb54..0c6f7f80a20f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,13 +16,13 @@ package org.springframework.beans.factory.xml; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.core.io.ClassPathResource; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -33,7 +33,7 @@ public class DefaultLifecycleMethodsTests { private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - @Before + @BeforeEach public void setup() throws Exception { new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( new ClassPathResource("defaultLifecycleMethods.xml", getClass())); @@ -43,22 +43,22 @@ public void setup() throws Exception { @Test public void lifecycleMethodsInvoked() { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleAware"); - assertTrue("Bean not initialized", bean.isInitCalled()); - assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled()); - assertFalse("Bean destroyed too early", bean.isDestroyCalled()); + assertThat(bean.isInitCalled()).as("Bean not initialized").isTrue(); + assertThat(bean.isCustomInitCalled()).as("Custom init method called incorrectly").isFalse(); + assertThat(bean.isDestroyCalled()).as("Bean destroyed too early").isFalse(); this.beanFactory.destroySingletons(); - assertTrue("Bean not destroyed", bean.isDestroyCalled()); - assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled()); + assertThat(bean.isDestroyCalled()).as("Bean not destroyed").isTrue(); + assertThat(bean.isCustomDestroyCalled()).as("Custom destroy method called incorrectly").isFalse(); } @Test public void lifecycleMethodsDisabled() throws Exception { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleMethodsDisabled"); - assertFalse("Bean init method called incorrectly", bean.isInitCalled()); - assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled()); + assertThat(bean.isInitCalled()).as("Bean init method called incorrectly").isFalse(); + assertThat(bean.isCustomInitCalled()).as("Custom init method called incorrectly").isFalse(); this.beanFactory.destroySingletons(); - assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled()); - assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled()); + assertThat(bean.isDestroyCalled()).as("Bean destroy method called incorrectly").isFalse(); + assertThat(bean.isCustomDestroyCalled()).as("Custom destroy method called incorrectly").isFalse(); } @Test @@ -73,32 +73,32 @@ public void ignoreDefaultLifecycleMethods() throws Exception { @Test public void overrideDefaultLifecycleMethods() throws Exception { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("overrideLifecycleMethods"); - assertFalse("Default init method called incorrectly", bean.isInitCalled()); - assertTrue("Custom init method not called", bean.isCustomInitCalled()); + assertThat(bean.isInitCalled()).as("Default init method called incorrectly").isFalse(); + assertThat(bean.isCustomInitCalled()).as("Custom init method not called").isTrue(); this.beanFactory.destroySingletons(); - assertFalse("Default destroy method called incorrectly", bean.isDestroyCalled()); - assertTrue("Custom destroy method not called", bean.isCustomDestroyCalled()); + assertThat(bean.isDestroyCalled()).as("Default destroy method called incorrectly").isFalse(); + assertThat(bean.isCustomDestroyCalled()).as("Custom destroy method not called").isTrue(); } @Test public void childWithDefaultLifecycleMethods() throws Exception { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("childWithDefaultLifecycleMethods"); - assertTrue("Bean not initialized", bean.isInitCalled()); - assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled()); - assertFalse("Bean destroyed too early", bean.isDestroyCalled()); + assertThat(bean.isInitCalled()).as("Bean not initialized").isTrue(); + assertThat(bean.isCustomInitCalled()).as("Custom init method called incorrectly").isFalse(); + assertThat(bean.isDestroyCalled()).as("Bean destroyed too early").isFalse(); this.beanFactory.destroySingletons(); - assertTrue("Bean not destroyed", bean.isDestroyCalled()); - assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled()); + assertThat(bean.isDestroyCalled()).as("Bean not destroyed").isTrue(); + assertThat(bean.isCustomDestroyCalled()).as("Custom destroy method called incorrectly").isFalse(); } @Test public void childWithLifecycleMethodsDisabled() throws Exception { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("childWithLifecycleMethodsDisabled"); - assertFalse("Bean init method called incorrectly", bean.isInitCalled()); - assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled()); + assertThat(bean.isInitCalled()).as("Bean init method called incorrectly").isFalse(); + assertThat(bean.isCustomInitCalled()).as("Custom init method called incorrectly").isFalse(); this.beanFactory.destroySingletons(); - assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled()); - assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled()); + assertThat(bean.isDestroyCalled()).as("Bean destroy method called incorrectly").isFalse(); + assertThat(bean.isCustomDestroyCalled()).as("Custom destroy method called incorrectly").isFalse(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DelegatingEntityResolverTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DelegatingEntityResolverTests.java index 68a917b13c63..6e05b66c7c6b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DelegatingEntityResolverTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DelegatingEntityResolverTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,10 +16,12 @@ package org.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + /** * Unit tests for the {@link DelegatingEntityResolver} class. * @@ -28,19 +30,22 @@ */ public class DelegatingEntityResolverTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWhereDtdEntityResolverIsNull() throws Exception { - new DelegatingEntityResolver(null, new NoOpEntityResolver()); + assertThatIllegalArgumentException().isThrownBy(() -> + new DelegatingEntityResolver(null, new NoOpEntityResolver())); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWhereSchemaEntityResolverIsNull() throws Exception { - new DelegatingEntityResolver(new NoOpEntityResolver(), null); + assertThatIllegalArgumentException().isThrownBy(() -> + new DelegatingEntityResolver(new NoOpEntityResolver(), null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWhereEntityResolversAreBothNull() throws Exception { - new DelegatingEntityResolver(null, null); + assertThatIllegalArgumentException().isThrownBy(() -> + new DelegatingEntityResolver(null, null)); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DummyReferencer.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DummyReferencer.java index 83a54e4bf824..10bf54f5e07c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DummyReferencer.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DummyReferencer.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,8 +17,8 @@ package org.springframework.beans.factory.xml; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.tests.sample.beans.factory.DummyFactory; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.beans.testfixture.beans.factory.DummyFactory; /** * @author Juergen Hoeller diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DuplicateBeanIdTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DuplicateBeanIdTests.java index a42359b98b83..2520004ea3c6 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DuplicateBeanIdTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DuplicateBeanIdTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,19 +16,20 @@ package org.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + /** * With Spring 3.1, bean id attributes (and all other id attributes across the * core schemas) are no longer typed as xsd:id, but as xsd:string. This allows - * for using the same bean id within nested elements. + * for using the same bean id within nested <beans> elements. * * Duplicate ids *within the same level of nesting* will still be treated as an * error through the ProblemReporter, as this could never be an intended/valid @@ -45,13 +46,8 @@ public class DuplicateBeanIdTests { public void duplicateBeanIdsWithinSameNestingLevelRaisesError() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); - try { - reader.loadBeanDefinitions(new ClassPathResource("DuplicateBeanIdTests-sameLevel-context.xml", this.getClass())); - fail("expected parsing exception due to duplicate ids in same nesting level"); - } - catch (Exception ex) { - // expected - } + assertThatExceptionOfType(Exception.class).as("duplicate ids in same nesting level").isThrownBy(() -> + reader.loadBeanDefinitions(new ClassPathResource("DuplicateBeanIdTests-sameLevel-context.xml", this.getClass()))); } @Test @@ -60,6 +56,6 @@ public void duplicateBeanIdsAcrossNestingLevels() { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); reader.loadBeanDefinitions(new ClassPathResource("DuplicateBeanIdTests-multiLevel-context.xml", this.getClass())); TestBean testBean = bf.getBean(TestBean.class); // there should be only one - assertThat(testBean.getName(), equalTo("nested")); + assertThat(testBean.getName()).isEqualTo("nested"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/EventPublicationTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/EventPublicationTests.java index fa9cec9db1e3..29e2561fc756 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/EventPublicationTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/EventPublicationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,8 +18,8 @@ import java.util.List; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.w3c.dom.Element; import org.springframework.beans.factory.config.BeanDefinition; @@ -30,10 +30,10 @@ import org.springframework.beans.factory.parsing.ImportDefinition; import org.springframework.beans.factory.parsing.PassThroughSourceExtractor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.CollectingReaderEventListener; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.beans.CollectingReaderEventListener; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -48,7 +48,7 @@ public class EventPublicationTests { - @Before + @BeforeEach public void setUp() throws Exception { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); reader.setEventListener(this.eventListener); @@ -59,66 +59,72 @@ public void setUp() throws Exception { @Test public void defaultsEventReceived() throws Exception { List defaultsList = this.eventListener.getDefaults(); - assertTrue(!defaultsList.isEmpty()); - assertTrue(defaultsList.get(0) instanceof DocumentDefaultsDefinition); + boolean condition2 = !defaultsList.isEmpty(); + assertThat(condition2).isTrue(); + boolean condition1 = defaultsList.get(0) instanceof DocumentDefaultsDefinition; + assertThat(condition1).isTrue(); DocumentDefaultsDefinition defaults = (DocumentDefaultsDefinition) defaultsList.get(0); - assertEquals("true", defaults.getLazyInit()); - assertEquals("constructor", defaults.getAutowire()); - assertEquals("myInit", defaults.getInitMethod()); - assertEquals("myDestroy", defaults.getDestroyMethod()); - assertEquals("true", defaults.getMerge()); - assertTrue(defaults.getSource() instanceof Element); + assertThat(defaults.getLazyInit()).isEqualTo("true"); + assertThat(defaults.getAutowire()).isEqualTo("constructor"); + assertThat(defaults.getInitMethod()).isEqualTo("myInit"); + assertThat(defaults.getDestroyMethod()).isEqualTo("myDestroy"); + assertThat(defaults.getMerge()).isEqualTo("true"); + boolean condition = defaults.getSource() instanceof Element; + assertThat(condition).isTrue(); } @Test public void beanEventReceived() throws Exception { ComponentDefinition componentDefinition1 = this.eventListener.getComponentDefinition("testBean"); - assertTrue(componentDefinition1 instanceof BeanComponentDefinition); - assertEquals(1, componentDefinition1.getBeanDefinitions().length); + boolean condition3 = componentDefinition1 instanceof BeanComponentDefinition; + assertThat(condition3).isTrue(); + assertThat(componentDefinition1.getBeanDefinitions().length).isEqualTo(1); BeanDefinition beanDefinition1 = componentDefinition1.getBeanDefinitions()[0]; - assertEquals(new TypedStringValue("Rob Harrop"), - beanDefinition1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()); - assertEquals(1, componentDefinition1.getBeanReferences().length); - assertEquals("testBean2", componentDefinition1.getBeanReferences()[0].getBeanName()); - assertEquals(1, componentDefinition1.getInnerBeanDefinitions().length); + assertThat(beanDefinition1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()).isEqualTo(new TypedStringValue("Rob Harrop")); + assertThat(componentDefinition1.getBeanReferences().length).isEqualTo(1); + assertThat(componentDefinition1.getBeanReferences()[0].getBeanName()).isEqualTo("testBean2"); + assertThat(componentDefinition1.getInnerBeanDefinitions().length).isEqualTo(1); BeanDefinition innerBd1 = componentDefinition1.getInnerBeanDefinitions()[0]; - assertEquals(new TypedStringValue("ACME"), - innerBd1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()); - assertTrue(componentDefinition1.getSource() instanceof Element); + assertThat(innerBd1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()).isEqualTo(new TypedStringValue("ACME")); + boolean condition2 = componentDefinition1.getSource() instanceof Element; + assertThat(condition2).isTrue(); ComponentDefinition componentDefinition2 = this.eventListener.getComponentDefinition("testBean2"); - assertTrue(componentDefinition2 instanceof BeanComponentDefinition); - assertEquals(1, componentDefinition1.getBeanDefinitions().length); + boolean condition1 = componentDefinition2 instanceof BeanComponentDefinition; + assertThat(condition1).isTrue(); + assertThat(componentDefinition1.getBeanDefinitions().length).isEqualTo(1); BeanDefinition beanDefinition2 = componentDefinition2.getBeanDefinitions()[0]; - assertEquals(new TypedStringValue("Juergen Hoeller"), - beanDefinition2.getPropertyValues().getPropertyValue("name").getValue()); - assertEquals(0, componentDefinition2.getBeanReferences().length); - assertEquals(1, componentDefinition2.getInnerBeanDefinitions().length); + assertThat(beanDefinition2.getPropertyValues().getPropertyValue("name").getValue()).isEqualTo(new TypedStringValue("Juergen Hoeller")); + assertThat(componentDefinition2.getBeanReferences().length).isEqualTo(0); + assertThat(componentDefinition2.getInnerBeanDefinitions().length).isEqualTo(1); BeanDefinition innerBd2 = componentDefinition2.getInnerBeanDefinitions()[0]; - assertEquals(new TypedStringValue("Eva Schallmeiner"), - innerBd2.getPropertyValues().getPropertyValue("name").getValue()); - assertTrue(componentDefinition2.getSource() instanceof Element); + assertThat(innerBd2.getPropertyValues().getPropertyValue("name").getValue()).isEqualTo(new TypedStringValue("Eva Schallmeiner")); + boolean condition = componentDefinition2.getSource() instanceof Element; + assertThat(condition).isTrue(); } @Test public void aliasEventReceived() throws Exception { List aliases = this.eventListener.getAliases("testBean"); - assertEquals(2, aliases.size()); + assertThat(aliases.size()).isEqualTo(2); AliasDefinition aliasDefinition1 = (AliasDefinition) aliases.get(0); - assertEquals("testBeanAlias1", aliasDefinition1.getAlias()); - assertTrue(aliasDefinition1.getSource() instanceof Element); + assertThat(aliasDefinition1.getAlias()).isEqualTo("testBeanAlias1"); + boolean condition1 = aliasDefinition1.getSource() instanceof Element; + assertThat(condition1).isTrue(); AliasDefinition aliasDefinition2 = (AliasDefinition) aliases.get(1); - assertEquals("testBeanAlias2", aliasDefinition2.getAlias()); - assertTrue(aliasDefinition2.getSource() instanceof Element); + assertThat(aliasDefinition2.getAlias()).isEqualTo("testBeanAlias2"); + boolean condition = aliasDefinition2.getSource() instanceof Element; + assertThat(condition).isTrue(); } @Test public void importEventReceived() throws Exception { List imports = this.eventListener.getImports(); - assertEquals(1, imports.size()); + assertThat(imports.size()).isEqualTo(1); ImportDefinition importDefinition = (ImportDefinition) imports.get(0); - assertEquals("beanEventsImported.xml", importDefinition.getImportedResource()); - assertTrue(importDefinition.getSource() instanceof Element); + assertThat(importDefinition.getImportedResource()).isEqualTo("beanEventsImported.xml"); + boolean condition = importDefinition.getSource() instanceof Element; + assertThat(condition).isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethodTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethodTests.java index 42279ded04b3..d2f26ee0fe63 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethodTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethodTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -21,14 +21,16 @@ import java.util.List; import java.util.Properties; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.FactoryMethods; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Juergen Hoeller @@ -43,31 +45,31 @@ public void testFactoryMethodsSingletonOnTargetClass() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); TestBean tb = (TestBean) xbf.getBean("defaultTestBean"); - assertEquals("defaultInstance", tb.getName()); - assertEquals(1, tb.getAge()); + assertThat(tb.getName()).isEqualTo("defaultInstance"); + assertThat(tb.getAge()).isEqualTo(1); FactoryMethods fm = (FactoryMethods) xbf.getBean("default"); - assertEquals(0, fm.getNum()); - assertEquals("default", fm.getName()); - assertEquals("defaultInstance", fm.getTestBean().getName()); - assertEquals("setterString", fm.getStringValue()); + assertThat(fm.getNum()).isEqualTo(0); + assertThat(fm.getName()).isEqualTo("default"); + assertThat(fm.getTestBean().getName()).isEqualTo("defaultInstance"); + assertThat(fm.getStringValue()).isEqualTo("setterString"); fm = (FactoryMethods) xbf.getBean("testBeanOnly"); - assertEquals(0, fm.getNum()); - assertEquals("default", fm.getName()); + assertThat(fm.getNum()).isEqualTo(0); + assertThat(fm.getName()).isEqualTo("default"); // This comes from the test bean - assertEquals("Juergen", fm.getTestBean().getName()); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); fm = (FactoryMethods) xbf.getBean("full"); - assertEquals(27, fm.getNum()); - assertEquals("gotcha", fm.getName()); - assertEquals("Juergen", fm.getTestBean().getName()); + assertThat(fm.getNum()).isEqualTo(27); + assertThat(fm.getName()).isEqualTo("gotcha"); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); FactoryMethods fm2 = (FactoryMethods) xbf.getBean("full"); - assertSame(fm, fm2); + assertThat(fm2).isSameAs(fm); xbf.destroySingletons(); - assertTrue(tb.wasDestroyed()); + assertThat(tb.wasDestroyed()).isTrue(); } @Test @@ -75,14 +77,8 @@ public void testFactoryMethodsWithInvalidDestroyMethod() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); - - try { - xbf.getBean("defaultTestBeanWithInvalidDestroyMethod"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - // expected - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + xbf.getBean("defaultTestBeanWithInvalidDestroyMethod")); } @Test @@ -91,15 +87,9 @@ public void testFactoryMethodsWithNullInstance() { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); - assertEquals("null", xbf.getBean("null").toString()); - - try { - xbf.getBean("nullWithProperty"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - // expected - } + assertThat(xbf.getBean("null").toString()).isEqualTo("null"); + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + xbf.getBean("nullWithProperty")); } @Test @@ -109,19 +99,19 @@ public void testFactoryMethodsWithNullValue() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); FactoryMethods fm = (FactoryMethods) xbf.getBean("fullWithNull"); - assertEquals(27, fm.getNum()); - assertEquals(null, fm.getName()); - assertEquals("Juergen", fm.getTestBean().getName()); + assertThat(fm.getNum()).isEqualTo(27); + assertThat(fm.getName()).isEqualTo(null); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); fm = (FactoryMethods) xbf.getBean("fullWithGenericNull"); - assertEquals(27, fm.getNum()); - assertEquals(null, fm.getName()); - assertEquals("Juergen", fm.getTestBean().getName()); + assertThat(fm.getNum()).isEqualTo(27); + assertThat(fm.getName()).isEqualTo(null); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); fm = (FactoryMethods) xbf.getBean("fullWithNamedNull"); - assertEquals(27, fm.getNum()); - assertEquals(null, fm.getName()); - assertEquals("Juergen", fm.getTestBean().getName()); + assertThat(fm.getNum()).isEqualTo(27); + assertThat(fm.getName()).isEqualTo(null); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); } @Test @@ -131,9 +121,9 @@ public void testFactoryMethodsWithAutowire() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); FactoryMethods fm = (FactoryMethods) xbf.getBean("fullWithAutowire"); - assertEquals(27, fm.getNum()); - assertEquals("gotchaAutowired", fm.getName()); - assertEquals("Juergen", fm.getTestBean().getName()); + assertThat(fm.getNum()).isEqualTo(27); + assertThat(fm.getName()).isEqualTo("gotchaAutowired"); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); } @Test @@ -143,7 +133,7 @@ public void testProtectedFactoryMethod() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); TestBean tb = (TestBean) xbf.getBean("defaultTestBean.protected"); - assertEquals(1, tb.getAge()); + assertThat(tb.getAge()).isEqualTo(1); } @Test @@ -153,7 +143,7 @@ public void testPrivateFactoryMethod() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); TestBean tb = (TestBean) xbf.getBean("defaultTestBean.private"); - assertEquals(1, tb.getAge()); + assertThat(tb.getAge()).isEqualTo(1); } @Test @@ -163,38 +153,38 @@ public void testFactoryMethodsPrototypeOnTargetClass() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); FactoryMethods fm = (FactoryMethods) xbf.getBean("defaultPrototype"); FactoryMethods fm2 = (FactoryMethods) xbf.getBean("defaultPrototype"); - assertEquals(0, fm.getNum()); - assertEquals("default", fm.getName()); - assertEquals("defaultInstance", fm.getTestBean().getName()); - assertEquals("setterString", fm.getStringValue()); - assertEquals(fm.getNum(), fm2.getNum()); - assertEquals(fm.getStringValue(), fm2.getStringValue()); + assertThat(fm.getNum()).isEqualTo(0); + assertThat(fm.getName()).isEqualTo("default"); + assertThat(fm.getTestBean().getName()).isEqualTo("defaultInstance"); + assertThat(fm.getStringValue()).isEqualTo("setterString"); + assertThat(fm2.getNum()).isEqualTo(fm.getNum()); + assertThat(fm2.getStringValue()).isEqualTo(fm.getStringValue()); // The TestBean is created separately for each bean - assertNotSame(fm.getTestBean(), fm2.getTestBean()); - assertNotSame(fm, fm2); + assertThat(fm2.getTestBean()).isNotSameAs(fm.getTestBean()); + assertThat(fm2).isNotSameAs(fm); fm = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype"); fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype"); - assertEquals(0, fm.getNum()); - assertEquals("default", fm.getName()); + assertThat(fm.getNum()).isEqualTo(0); + assertThat(fm.getName()).isEqualTo("default"); // This comes from the test bean - assertEquals("Juergen", fm.getTestBean().getName()); - assertEquals(fm.getNum(), fm2.getNum()); - assertEquals(fm.getStringValue(), fm2.getStringValue()); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); + assertThat(fm2.getNum()).isEqualTo(fm.getNum()); + assertThat(fm2.getStringValue()).isEqualTo(fm.getStringValue()); // The TestBean reference is resolved to a prototype in the factory - assertSame(fm.getTestBean(), fm2.getTestBean()); - assertNotSame(fm, fm2); + assertThat(fm2.getTestBean()).isSameAs(fm.getTestBean()); + assertThat(fm2).isNotSameAs(fm); fm = (FactoryMethods) xbf.getBean("fullPrototype"); fm2 = (FactoryMethods) xbf.getBean("fullPrototype"); - assertEquals(27, fm.getNum()); - assertEquals("gotcha", fm.getName()); - assertEquals("Juergen", fm.getTestBean().getName()); - assertEquals(fm.getNum(), fm2.getNum()); - assertEquals(fm.getStringValue(), fm2.getStringValue()); + assertThat(fm.getNum()).isEqualTo(27); + assertThat(fm.getName()).isEqualTo("gotcha"); + assertThat(fm.getTestBean().getName()).isEqualTo("Juergen"); + assertThat(fm2.getNum()).isEqualTo(fm.getNum()); + assertThat(fm2.getStringValue()).isEqualTo(fm.getStringValue()); // The TestBean reference is resolved to a prototype in the factory - assertSame(fm.getTestBean(), fm2.getTestBean()); - assertNotSame(fm, fm2); + assertThat(fm2.getTestBean()).isSameAs(fm.getTestBean()); + assertThat(fm2).isNotSameAs(fm); } /** @@ -206,24 +196,24 @@ public void testFactoryMethodsOnExternalClass() { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); - assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs")); - assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs")); + assertThat(xbf.getType("externalFactoryMethodWithoutArgs")).isEqualTo(TestBean.class); + assertThat(xbf.getType("externalFactoryMethodWithArgs")).isEqualTo(TestBean.class); String[] names = xbf.getBeanNamesForType(TestBean.class); - assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs")); - assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs")); + assertThat(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs")).isTrue(); + assertThat(Arrays.asList(names).contains("externalFactoryMethodWithArgs")).isTrue(); TestBean tb = (TestBean) xbf.getBean("externalFactoryMethodWithoutArgs"); - assertEquals(2, tb.getAge()); - assertEquals("Tristan", tb.getName()); + assertThat(tb.getAge()).isEqualTo(2); + assertThat(tb.getName()).isEqualTo("Tristan"); tb = (TestBean) xbf.getBean("externalFactoryMethodWithArgs"); - assertEquals(33, tb.getAge()); - assertEquals("Rod", tb.getName()); + assertThat(tb.getAge()).isEqualTo(33); + assertThat(tb.getName()).isEqualTo("Rod"); - assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs")); - assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs")); + assertThat(xbf.getType("externalFactoryMethodWithoutArgs")).isEqualTo(TestBean.class); + assertThat(xbf.getType("externalFactoryMethodWithArgs")).isEqualTo(TestBean.class); names = xbf.getBeanNamesForType(TestBean.class); - assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs")); - assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs")); + assertThat(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs")).isTrue(); + assertThat(Arrays.asList(names).contains("externalFactoryMethodWithArgs")).isTrue(); } @Test @@ -234,14 +224,14 @@ public void testInstanceFactoryMethodWithoutArgs() { InstanceFactory.count = 0; xbf.preInstantiateSingletons(); - assertEquals(1, InstanceFactory.count); + assertThat(InstanceFactory.count).isEqualTo(1); FactoryMethods fm = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs"); - assertEquals("instanceFactory", fm.getTestBean().getName()); - assertEquals(1, InstanceFactory.count); + assertThat(fm.getTestBean().getName()).isEqualTo("instanceFactory"); + assertThat(InstanceFactory.count).isEqualTo(1); FactoryMethods fm2 = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs"); - assertEquals("instanceFactory", fm2.getTestBean().getName()); - assertSame(fm2, fm); - assertEquals(1, InstanceFactory.count); + assertThat(fm2.getTestBean().getName()).isEqualTo("instanceFactory"); + assertThat(fm).isSameAs(fm2); + assertThat(InstanceFactory.count).isEqualTo(1); } @Test @@ -249,13 +239,8 @@ public void testFactoryMethodNoMatchingStaticMethod() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); - try { - xbf.getBean("noMatchPrototype"); - fail("No static method matched"); - } - catch (BeanCreationException ex) { - // Ok - } + assertThatExceptionOfType(BeanCreationException.class).as("No static method matched").isThrownBy(() -> + xbf.getBean("noMatchPrototype")); } @Test @@ -263,13 +248,9 @@ public void testNonExistingFactoryMethod() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); - try { - xbf.getBean("invalidPrototype"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getMessage().contains("nonExisting(TestBean)")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + xbf.getBean("invalidPrototype")) + .withMessageContaining("nonExisting(TestBean)"); } @Test @@ -277,13 +258,9 @@ public void testFactoryMethodArgumentsForNonExistingMethod() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); - try { - xbf.getBean("invalidPrototype", new TestBean()); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getMessage().contains("nonExisting(TestBean)")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + xbf.getBean("invalidPrototype", new TestBean())) + .withMessageContaining("nonExisting(TestBean)"); } @Test @@ -297,29 +274,29 @@ public void testCanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype() { tbArg2.setName("arg2"); FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg); - assertEquals(0, fm1.getNum()); - assertEquals("default", fm1.getName()); + assertThat(fm1.getNum()).isEqualTo(0); + assertThat(fm1.getName()).isEqualTo("default"); // This comes from the test bean - assertEquals("arg1", fm1.getTestBean().getName()); + assertThat(fm1.getTestBean().getName()).isEqualTo("arg1"); FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg2); - assertEquals("arg2", fm2.getTestBean().getName()); - assertEquals(fm1.getNum(), fm2.getNum()); - assertEquals(fm2.getStringValue(), "testBeanOnlyPrototypeDISetterString"); - assertEquals(fm2.getStringValue(), fm2.getStringValue()); + assertThat(fm2.getTestBean().getName()).isEqualTo("arg2"); + assertThat(fm2.getNum()).isEqualTo(fm1.getNum()); + assertThat("testBeanOnlyPrototypeDISetterString").isEqualTo(fm2.getStringValue()); + assertThat(fm2.getStringValue()).isEqualTo(fm2.getStringValue()); // The TestBean reference is resolved to a prototype in the factory - assertSame(fm2.getTestBean(), fm2.getTestBean()); - assertNotSame(fm1, fm2); + assertThat(fm2.getTestBean()).isSameAs(fm2.getTestBean()); + assertThat(fm2).isNotSameAs(fm1); - FactoryMethods fm3 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg2, new Integer(1), "myName"); - assertEquals(1, fm3.getNum()); - assertEquals("myName", fm3.getName()); - assertEquals("arg2", fm3.getTestBean().getName()); + FactoryMethods fm3 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg2, 1, "myName"); + assertThat(fm3.getNum()).isEqualTo(1); + assertThat(fm3.getName()).isEqualTo("myName"); + assertThat(fm3.getTestBean().getName()).isEqualTo("arg2"); FactoryMethods fm4 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg); - assertEquals(0, fm4.getNum()); - assertEquals("default", fm4.getName()); - assertEquals("arg1", fm4.getTestBean().getName()); + assertThat(fm4.getNum()).isEqualTo(0); + assertThat(fm4.getName()).isEqualTo("default"); + assertThat(fm4.getTestBean().getName()).isEqualTo("arg1"); } @Test @@ -331,10 +308,10 @@ public void testCanSpecifyFactoryMethodArgumentsOnSingleton() { // First getBean call triggers actual creation of the singleton bean TestBean tb = new TestBean(); FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly", tb); - assertSame(tb, fm1.getTestBean()); + assertThat(fm1.getTestBean()).isSameAs(tb); FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean()); - assertSame(fm1, fm2); - assertSame(tb, fm2.getTestBean()); + assertThat(fm2).isSameAs(fm1); + assertThat(fm2.getTestBean()).isSameAs(tb); } @Test @@ -347,8 +324,8 @@ public void testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation() { FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly"); TestBean tb = fm1.getTestBean(); FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean()); - assertSame(fm1, fm2); - assertSame(tb, fm2.getTestBean()); + assertThat(fm2).isSameAs(fm1); + assertThat(fm2.getTestBean()).isSameAs(tb); } @Test @@ -358,20 +335,22 @@ public void testFactoryMethodWithDifferentReturnType() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); // Check that listInstance is not considered a bean of type FactoryMethods. - assertTrue(List.class.isAssignableFrom(xbf.getType("listInstance"))); + assertThat(List.class.isAssignableFrom(xbf.getType("listInstance"))).isTrue(); String[] names = xbf.getBeanNamesForType(FactoryMethods.class); - assertTrue(!Arrays.asList(names).contains("listInstance")); + boolean condition1 = !Arrays.asList(names).contains("listInstance"); + assertThat(condition1).isTrue(); names = xbf.getBeanNamesForType(List.class); - assertTrue(Arrays.asList(names).contains("listInstance")); + assertThat(Arrays.asList(names).contains("listInstance")).isTrue(); xbf.preInstantiateSingletons(); - assertTrue(List.class.isAssignableFrom(xbf.getType("listInstance"))); + assertThat(List.class.isAssignableFrom(xbf.getType("listInstance"))).isTrue(); names = xbf.getBeanNamesForType(FactoryMethods.class); - assertTrue(!Arrays.asList(names).contains("listInstance")); + boolean condition = !Arrays.asList(names).contains("listInstance"); + assertThat(condition).isTrue(); names = xbf.getBeanNamesForType(List.class); - assertTrue(Arrays.asList(names).contains("listInstance")); + assertThat(Arrays.asList(names).contains("listInstance")).isTrue(); List list = (List) xbf.getBean("listInstance"); - assertEquals(Collections.EMPTY_LIST, list); + assertThat(list).isEqualTo(Collections.EMPTY_LIST); } @Test @@ -381,8 +360,8 @@ public void testFactoryMethodForJavaMailSession() { reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); MailSession session = (MailSession) xbf.getBean("javaMailSession"); - assertEquals("someuser", session.getProperty("mail.smtp.user")); - assertEquals("somepw", session.getProperty("mail.smtp.password")); + assertThat(session.getProperty("mail.smtp.user")).isEqualTo("someuser"); + assertThat(session.getProperty("mail.smtp.password")).isEqualTo("somepw"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/GeneratedNameBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/GeneratedNameBean.java index fdf65f88d71d..51cb92c4a8a3 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/GeneratedNameBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/GeneratedNameBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/InstanceFactory.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/InstanceFactory.java index 6c8872054e10..af7f2f718b19 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/InstanceFactory.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/InstanceFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -16,7 +16,8 @@ package org.springframework.beans.factory.xml; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.FactoryMethods; +import org.springframework.beans.testfixture.beans.TestBean; /** * Test class for Spring's ability to create objects using diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/MetadataAttachmentTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/MetadataAttachmentTests.java index ff66dec9777d..1c796266d1f5 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/MetadataAttachmentTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/MetadataAttachmentTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,15 +16,15 @@ package org.springframework.beans.factory.xml; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.core.io.ClassPathResource; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -34,7 +34,7 @@ public class MetadataAttachmentTests { private DefaultListableBeanFactory beanFactory; - @Before + @BeforeEach public void setUp() throws Exception { this.beanFactory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( @@ -44,21 +44,21 @@ public void setUp() throws Exception { @Test public void metadataAttachment() throws Exception { BeanDefinition beanDefinition1 = this.beanFactory.getMergedBeanDefinition("testBean1"); - assertEquals("bar", beanDefinition1.getAttribute("foo")); + assertThat(beanDefinition1.getAttribute("foo")).isEqualTo("bar"); } @Test public void metadataIsInherited() throws Exception { BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("testBean2"); - assertEquals("Metadata not inherited", "bar", beanDefinition.getAttribute("foo")); - assertEquals("Child metdata not attached", "123", beanDefinition.getAttribute("abc")); + assertThat(beanDefinition.getAttribute("foo")).as("Metadata not inherited").isEqualTo("bar"); + assertThat(beanDefinition.getAttribute("abc")).as("Child metdata not attached").isEqualTo("123"); } @Test public void propertyMetadata() throws Exception { BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("testBean3"); PropertyValue pv = beanDefinition.getPropertyValues().getPropertyValue("name"); - assertEquals("Harrop", pv.getAttribute("surname")); + assertThat(pv.getAttribute("surname")).isEqualTo("Harrop"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/MixedCollectionBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/MixedCollectionBean.java index 8ed5f2517ec8..a3419e46f113 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/MixedCollectionBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/MixedCollectionBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -27,14 +27,14 @@ */ public class MixedCollectionBean { - private Collection jumble; + private Collection jumble; - public void setJumble(Collection jumble) { + public void setJumble(Collection jumble) { this.jumble = jumble; } - public Collection getJumble() { + public Collection getJumble() { return jumble; } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests.java index 106e1d79b6bf..debeb353cafa 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,17 +16,16 @@ package org.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.Matchers.hasItems; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; + + /** * Tests for propagating enclosing beans element defaults to nested beans elements. @@ -41,17 +40,32 @@ public void defaultLazyInit() { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("NestedBeansElementAttributeRecursionTests-lazy-context.xml", this.getClass())); + assertLazyInits(bf); + } + + @Test + public void defaultLazyInitWithNonValidatingParser() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(bf); + xmlBeanDefinitionReader.setValidating(false); + xmlBeanDefinitionReader.loadBeanDefinitions( + new ClassPathResource("NestedBeansElementAttributeRecursionTests-lazy-context.xml", this.getClass())); + + assertLazyInits(bf); + } + + private void assertLazyInits(DefaultListableBeanFactory bf) { BeanDefinition foo = bf.getBeanDefinition("foo"); BeanDefinition bar = bf.getBeanDefinition("bar"); BeanDefinition baz = bf.getBeanDefinition("baz"); BeanDefinition biz = bf.getBeanDefinition("biz"); BeanDefinition buz = bf.getBeanDefinition("buz"); - assertThat(foo.isLazyInit(), is(false)); - assertThat(bar.isLazyInit(), is(true)); - assertThat(baz.isLazyInit(), is(false)); - assertThat(biz.isLazyInit(), is(true)); - assertThat(buz.isLazyInit(), is(true)); + assertThat(foo.isLazyInit()).isFalse(); + assertThat(bar.isLazyInit()).isTrue(); + assertThat(baz.isLazyInit()).isFalse(); + assertThat(biz.isLazyInit()).isTrue(); + assertThat(buz.isLazyInit()).isTrue(); } @Test @@ -61,21 +75,38 @@ public void defaultMerge() { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("NestedBeansElementAttributeRecursionTests-merge-context.xml", this.getClass())); + assertMerge(bf); + } + + @Test + @SuppressWarnings("unchecked") + public void defaultMergeWithNonValidatingParser() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(bf); + xmlBeanDefinitionReader.setValidating(false); + xmlBeanDefinitionReader.loadBeanDefinitions( + new ClassPathResource("NestedBeansElementAttributeRecursionTests-merge-context.xml", this.getClass())); + + assertMerge(bf); + } + + @SuppressWarnings("unchecked") + private void assertMerge(DefaultListableBeanFactory bf) { TestBean topLevel = bf.getBean("topLevelConcreteTestBean", TestBean.class); // has the concrete child bean values - assertThat((Iterable) topLevel.getSomeList(), hasItems("charlie", "delta")); + assertThat((Iterable) topLevel.getSomeList()).contains("charlie", "delta"); // but does not merge the parent values - assertThat((Iterable) topLevel.getSomeList(), not(hasItems("alpha", "bravo"))); + assertThat((Iterable) topLevel.getSomeList()).doesNotContain("alpha", "bravo"); TestBean firstLevel = bf.getBean("firstLevelNestedTestBean", TestBean.class); // merges all values - assertThat((Iterable) firstLevel.getSomeList(), - hasItems("charlie", "delta", "echo", "foxtrot")); + assertThat((Iterable) firstLevel.getSomeList()).contains( + "charlie", "delta", "echo", "foxtrot"); TestBean secondLevel = bf.getBean("secondLevelNestedTestBean", TestBean.class); // merges all values - assertThat((Iterable)secondLevel.getSomeList(), - hasItems("charlie", "delta", "echo", "foxtrot", "golf", "hotel")); + assertThat((Iterable)secondLevel.getSomeList()).contains( + "charlie", "delta", "echo", "foxtrot", "golf", "hotel"); } @Test @@ -84,23 +115,38 @@ public void defaultAutowireCandidates() { new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml", this.getClass())); - assertThat(bf.getBeanDefinition("fooService").isAutowireCandidate(), is(true)); - assertThat(bf.getBeanDefinition("fooRepository").isAutowireCandidate(), is(true)); - assertThat(bf.getBeanDefinition("other").isAutowireCandidate(), is(false)); + assertAutowireCandidates(bf); + } + + @Test + public void defaultAutowireCandidatesWithNonValidatingParser() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(bf); + xmlBeanDefinitionReader.setValidating(false); + xmlBeanDefinitionReader.loadBeanDefinitions( + new ClassPathResource("NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml", this.getClass())); + + assertAutowireCandidates(bf); + } + + private void assertAutowireCandidates(DefaultListableBeanFactory bf) { + assertThat(bf.getBeanDefinition("fooService").isAutowireCandidate()).isTrue(); + assertThat(bf.getBeanDefinition("fooRepository").isAutowireCandidate()).isTrue(); + assertThat(bf.getBeanDefinition("other").isAutowireCandidate()).isFalse(); - assertThat(bf.getBeanDefinition("barService").isAutowireCandidate(), is(true)); - assertThat(bf.getBeanDefinition("fooController").isAutowireCandidate(), is(false)); + assertThat(bf.getBeanDefinition("barService").isAutowireCandidate()).isTrue(); + assertThat(bf.getBeanDefinition("fooController").isAutowireCandidate()).isFalse(); - assertThat(bf.getBeanDefinition("bizRepository").isAutowireCandidate(), is(true)); - assertThat(bf.getBeanDefinition("bizService").isAutowireCandidate(), is(false)); + assertThat(bf.getBeanDefinition("bizRepository").isAutowireCandidate()).isTrue(); + assertThat(bf.getBeanDefinition("bizService").isAutowireCandidate()).isFalse(); - assertThat(bf.getBeanDefinition("bazService").isAutowireCandidate(), is(true)); - assertThat(bf.getBeanDefinition("random").isAutowireCandidate(), is(false)); - assertThat(bf.getBeanDefinition("fooComponent").isAutowireCandidate(), is(false)); - assertThat(bf.getBeanDefinition("fRepository").isAutowireCandidate(), is(false)); + assertThat(bf.getBeanDefinition("bazService").isAutowireCandidate()).isTrue(); + assertThat(bf.getBeanDefinition("random").isAutowireCandidate()).isFalse(); + assertThat(bf.getBeanDefinition("fooComponent").isAutowireCandidate()).isFalse(); + assertThat(bf.getBeanDefinition("fRepository").isAutowireCandidate()).isFalse(); - assertThat(bf.getBeanDefinition("aComponent").isAutowireCandidate(), is(true)); - assertThat(bf.getBeanDefinition("someService").isAutowireCandidate(), is(false)); + assertThat(bf.getBeanDefinition("aComponent").isAutowireCandidate()).isTrue(); + assertThat(bf.getBeanDefinition("someService").isAutowireCandidate()).isFalse(); } @Test @@ -114,17 +160,17 @@ public void initMethod() { InitDestroyBean beanC = bf.getBean("beanC", InitDestroyBean.class); InitDestroyBean beanD = bf.getBean("beanD", InitDestroyBean.class); - assertThat(beanA.initMethod1Called, is(true)); - assertThat(beanB.initMethod2Called, is(true)); - assertThat(beanC.initMethod3Called, is(true)); - assertThat(beanD.initMethod2Called, is(true)); + assertThat(beanA.initMethod1Called).isTrue(); + assertThat(beanB.initMethod2Called).isTrue(); + assertThat(beanC.initMethod3Called).isTrue(); + assertThat(beanD.initMethod2Called).isTrue(); bf.destroySingletons(); - assertThat(beanA.destroyMethod1Called, is(true)); - assertThat(beanB.destroyMethod2Called, is(true)); - assertThat(beanC.destroyMethod3Called, is(true)); - assertThat(beanD.destroyMethod2Called, is(true)); + assertThat(beanA.destroyMethod1Called).isTrue(); + assertThat(beanB.destroyMethod2Called).isTrue(); + assertThat(beanC.destroyMethod3Called).isTrue(); + assertThat(beanD.destroyMethod2Called).isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementTests.java index 3ac37c704a93..4117c82c4329 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/NestedBeansElementTests.java @@ -1,6 +1,22 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.core.env.ConfigurableEnvironment; @@ -8,8 +24,8 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests for new nested beans element support in Spring XML @@ -26,7 +42,7 @@ public void getBean_withoutActiveProfile() { new XmlBeanDefinitionReader(bf).loadBeanDefinitions(XML); Object foo = bf.getBean("foo"); - assertThat(foo, instanceOf(String.class)); + assertThat(foo).isInstanceOf(String.class); } @Test @@ -42,7 +58,7 @@ public void getBean_withActiveProfile() { bf.getBean("devOnlyBean"); // should not throw NSBDE Object foo = bf.getBean("foo"); - assertThat(foo, instanceOf(Integer.class)); + assertThat(foo).isInstanceOf(Integer.class); bf.getBean("devOnlyBean"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java index faa9ca0bbdd3..56795fe75735 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,10 +16,8 @@ package org.springframework.beans.factory.xml; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeMatcher; -import org.junit.Test; +import org.assertj.core.api.Condition; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -27,8 +25,8 @@ import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.ClassPathResource; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Tests various combinations of profile declarations against various profile @@ -62,58 +60,59 @@ public class ProfileXmlBeanDefinitionTests { private static final String TARGET_BEAN = "foo"; - @Test(expected = IllegalArgumentException.class) + @Test public void testProfileValidation() { - beanFactoryFor(PROD_ELIGIBLE_XML, NULL_ACTIVE); + assertThatIllegalArgumentException().isThrownBy(() -> + beanFactoryFor(PROD_ELIGIBLE_XML, NULL_ACTIVE)); } @Test public void testProfilePermutations() { - assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, NONE_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, DEV_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean()); - - assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, NONE_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, DEV_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, PROD_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean()); - - assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, DEV_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, MULTI_ACTIVE), not(containsTargetBean())); - - assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, DEV_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean()); - - assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, NONE_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, UNKNOWN_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, DEV_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean()); - - assertThat(beanFactoryFor(MULTI_NEGATED_XML, NONE_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_NEGATED_XML, UNKNOWN_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_NEGATED_XML, DEV_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_NEGATED_XML, PROD_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_NEGATED_XML, MULTI_ACTIVE), not(containsTargetBean())); - - assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, UNKNOWN_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, DEV_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean()); - - assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, NONE_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, UNKNOWN_ACTIVE), not(containsTargetBean())); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, DEV_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, PROD_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, MULTI_ACTIVE), containsTargetBean()); - - assertThat(beanFactoryFor(UNKNOWN_ELIGIBLE_XML, MULTI_ACTIVE), not(containsTargetBean())); + assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, NONE_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, DEV_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, PROD_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, MULTI_ACTIVE)).is(containingTarget()); + + assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, NONE_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, DEV_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, PROD_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, MULTI_ACTIVE)).is(containingTarget()); + + assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, NONE_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, DEV_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, PROD_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(NOT_DEV_ELIGIBLE_XML, MULTI_ACTIVE)).isNot(containingTarget()); + + assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, NONE_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, DEV_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, PROD_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, MULTI_ACTIVE)).is(containingTarget()); + + assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, NONE_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, UNKNOWN_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, DEV_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, PROD_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, MULTI_ACTIVE)).is(containingTarget()); + + assertThat(beanFactoryFor(MULTI_NEGATED_XML, NONE_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_NEGATED_XML, UNKNOWN_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_NEGATED_XML, DEV_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_NEGATED_XML, PROD_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_NEGATED_XML, MULTI_ACTIVE)).isNot(containingTarget()); + + assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, NONE_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, UNKNOWN_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, DEV_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, PROD_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_NOT_DEV_ELIGIBLE_XML, MULTI_ACTIVE)).is(containingTarget()); + + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, NONE_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, UNKNOWN_ACTIVE)).isNot(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, DEV_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, PROD_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, MULTI_ACTIVE)).is(containingTarget()); + + assertThat(beanFactoryFor(UNKNOWN_ELIGIBLE_XML, MULTI_ACTIVE)).isNot(containingTarget()); } @Test @@ -126,7 +125,7 @@ public void testDefaultProfile() { reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_ELIGIBLE_XML, getClass())); - assertThat(beanFactory, not(containsTargetBean())); + assertThat(beanFactory).isNot(containingTarget()); } { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); @@ -136,14 +135,14 @@ public void testDefaultProfile() { reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(CUSTOM_DEFAULT_ELIGIBLE_XML, getClass())); - assertThat(beanFactory, containsTargetBean()); + assertThat(beanFactory).is(containingTarget()); } } @Test public void testDefaultAndNonDefaultProfile() { - assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean()); - assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, "other"), not(containsTargetBean())); + assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, NONE_ACTIVE)).is(containingTarget()); + assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, "other")).isNot(containingTarget()); { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); @@ -153,7 +152,7 @@ public void testDefaultAndNonDefaultProfile() { env.setDefaultProfiles("default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass())); - assertThat(beanFactory, containsTargetBean()); + assertThat(beanFactory).is(containingTarget()); } { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); @@ -163,7 +162,7 @@ public void testDefaultAndNonDefaultProfile() { env.setDefaultProfiles("default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass())); - assertThat(beanFactory, containsTargetBean()); + assertThat(beanFactory).is(containingTarget()); } { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); @@ -173,7 +172,7 @@ public void testDefaultAndNonDefaultProfile() { //env.setDefaultProfiles("default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass())); - assertThat(beanFactory, containsTargetBean()); + assertThat(beanFactory).is(containingTarget()); } } @@ -188,25 +187,8 @@ private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activePr return beanFactory; } - - private static Matcher containsBeanDefinition(final String beanName) { - return new TypeSafeMatcher() { - - @Override - public void describeTo(Description desc) { - desc.appendText("a BeanDefinitionRegistry containing bean named ") - .appendValue(beanName); - } - - @Override - public boolean matchesSafely(BeanDefinitionRegistry beanFactory) { - return beanFactory.containsBeanDefinition(beanName); - } - - }; + private Condition containingTarget() { + return new Condition<>(registry -> registry.containsBeanDefinition(TARGET_BEAN), "contains target"); } - private static Matcher containsTargetBean() { - return containsBeanDefinition(TARGET_BEAN); - } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProtectedLifecycleBean.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProtectedLifecycleBean.java index 1ff3456cf4e1..95aca60954d6 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProtectedLifecycleBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/ProtectedLifecycleBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/SchemaValidationTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/SchemaValidationTests.java index 81d464c015b4..70199450995b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/SchemaValidationTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/SchemaValidationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,16 +16,16 @@ package org.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.xml.sax.SAXParseException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.TestBean; - -import org.xml.sax.SAXParseException; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rob Harrop @@ -36,13 +36,9 @@ public class SchemaValidationTests { public void withAutodetection() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); - try { - reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass())); - fail("Should not be able to parse a file with errors"); - } - catch (BeansException ex) { - assertTrue(ex.getCause() instanceof SAXParseException); - } + assertThatExceptionOfType(BeansException.class).isThrownBy(() -> + reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass()))) + .withCauseInstanceOf(SAXParseException.class); } @Test @@ -50,13 +46,9 @@ public void withExplicitValidationMode() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD); - try { - reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass())); - fail("Should not be able to parse a file with errors"); - } - catch (BeansException ex) { - assertTrue(ex.getCause() instanceof SAXParseException); - } + assertThatExceptionOfType(BeansException.class).isThrownBy(() -> + reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass()))) + .withCauseInstanceOf(SAXParseException.class); } @Test @@ -67,8 +59,8 @@ public void loadDefinitions() throws Exception { reader.loadBeanDefinitions(new ClassPathResource("schemaValidated.xml", getClass())); TestBean foo = (TestBean) bf.getBean("fooBean"); - assertNotNull("Spouse is null", foo.getSpouse()); - assertEquals("Incorrect number of friends", 2, foo.getFriends().size()); + assertThat(foo.getSpouse()).as("Spouse is null").isNotNull(); + assertThat(foo.getFriends().size()).as("Incorrect number of friends").isEqualTo(2); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandlerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandlerTests.java index 2c3b3cec4856..24a9d43e89be 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandlerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandlerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,15 +16,16 @@ package org.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.DummyBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.DummyBean; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Costin Leau @@ -37,7 +38,7 @@ public void simpleValue() throws Exception { String name = "simple"; // beanFactory.getBean("simple1", DummyBean.class); DummyBean nameValue = beanFactory.getBean(name, DummyBean.class); - assertEquals("simple", nameValue.getValue()); + assertThat(nameValue.getValue()).isEqualTo("simple"); } @Test @@ -46,7 +47,7 @@ public void simpleRef() throws Exception { String name = "simple-ref"; // beanFactory.getBean("name-value1", TestBean.class); DummyBean nameValue = beanFactory.getBean(name, DummyBean.class); - assertEquals(beanFactory.getBean("name"), nameValue.getValue()); + assertThat(nameValue.getValue()).isEqualTo(beanFactory.getBean("name")); } @Test @@ -55,8 +56,8 @@ public void nameValue() throws Exception { String name = "name-value"; // beanFactory.getBean("name-value1", TestBean.class); TestBean nameValue = beanFactory.getBean(name, TestBean.class); - assertEquals(name, nameValue.getName()); - assertEquals(10, nameValue.getAge()); + assertThat(nameValue.getName()).isEqualTo(name); + assertThat(nameValue.getAge()).isEqualTo(10); } @Test @@ -65,8 +66,8 @@ public void nameRef() throws Exception { TestBean nameValue = beanFactory.getBean("name-value", TestBean.class); DummyBean nameRef = beanFactory.getBean("name-ref", DummyBean.class); - assertEquals("some-name", nameRef.getName()); - assertEquals(nameValue, nameRef.getSpouse()); + assertThat(nameRef.getName()).isEqualTo("some-name"); + assertThat(nameRef.getSpouse()).isEqualTo(nameValue); } @Test @@ -74,9 +75,9 @@ public void typeIndexedValue() throws Exception { DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml"); DummyBean typeRef = beanFactory.getBean("indexed-value", DummyBean.class); - assertEquals("at", typeRef.getName()); - assertEquals("austria", typeRef.getValue()); - assertEquals(10, typeRef.getAge()); + assertThat(typeRef.getName()).isEqualTo("at"); + assertThat(typeRef.getValue()).isEqualTo("austria"); + assertThat(typeRef.getAge()).isEqualTo(10); } @Test @@ -84,23 +85,24 @@ public void typeIndexedRef() throws Exception { DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml"); DummyBean typeRef = beanFactory.getBean("indexed-ref", DummyBean.class); - assertEquals("some-name", typeRef.getName()); - assertEquals(beanFactory.getBean("name-value"), typeRef.getSpouse()); + assertThat(typeRef.getName()).isEqualTo("some-name"); + assertThat(typeRef.getSpouse()).isEqualTo(beanFactory.getBean("name-value")); } - @Test(expected = BeanDefinitionStoreException.class) + @Test public void ambiguousConstructor() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(bf).loadBeanDefinitions( - new ClassPathResource("simpleConstructorNamespaceHandlerTestsWithErrors.xml", getClass())); + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + new XmlBeanDefinitionReader(bf).loadBeanDefinitions( + new ClassPathResource("simpleConstructorNamespaceHandlerTestsWithErrors.xml", getClass()))); } @Test public void constructorWithNameEndingInRef() throws Exception { DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml"); DummyBean derivedBean = beanFactory.getBean("beanWithRefConstructorArg", DummyBean.class); - assertEquals(10, derivedBean.getAge()); - assertEquals("silly name", derivedBean.getName()); + assertThat(derivedBean.getAge()).isEqualTo(10); + assertThat(derivedBean.getName()).isEqualTo("silly name"); } private DefaultListableBeanFactory createFactory(String resourceName) { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandlerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandlerTests.java index f16977695bd8..68ef31d9a82f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandlerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandlerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,15 +16,16 @@ package org.springframework.beans.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rob Harrop @@ -40,9 +41,9 @@ public void simpleBeanConfigured() throws Exception { new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass())); ITestBean rob = (TestBean) beanFactory.getBean("rob"); ITestBean sally = (TestBean) beanFactory.getBean("sally"); - assertEquals("Rob Harrop", rob.getName()); - assertEquals(24, rob.getAge()); - assertEquals(rob.getSpouse(), sally); + assertThat(rob.getName()).isEqualTo("Rob Harrop"); + assertThat(rob.getAge()).isEqualTo(24); + assertThat(sally).isEqualTo(rob.getSpouse()); } @Test @@ -52,16 +53,17 @@ public void innerBeanConfigured() throws Exception { new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass())); TestBean sally = (TestBean) beanFactory.getBean("sally2"); ITestBean rob = sally.getSpouse(); - assertEquals("Rob Harrop", rob.getName()); - assertEquals(24, rob.getAge()); - assertEquals(rob.getSpouse(), sally); + assertThat(rob.getName()).isEqualTo("Rob Harrop"); + assertThat(rob.getAge()).isEqualTo(24); + assertThat(sally).isEqualTo(rob.getSpouse()); } - @Test(expected = BeanDefinitionStoreException.class) + @Test public void withPropertyDefinedTwice() throws Exception { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions( - new ClassPathResource("simplePropertyNamespaceHandlerTestsWithErrors.xml", getClass())); + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions( + new ClassPathResource("simplePropertyNamespaceHandlerTestsWithErrors.xml", getClass()))); } @Test @@ -70,7 +72,7 @@ public void propertyWithNameEndingInRef() throws Exception { new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions( new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass())); ITestBean sally = (TestBean) beanFactory.getBean("derivedSally"); - assertEquals("r", sally.getSpouse().getName()); + assertThat(sally.getSpouse().getName()).isEqualTo("r"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/TestBeanCreator.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/TestBeanCreator.java index 849da031cc36..091a20290e7c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/TestBeanCreator.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/TestBeanCreator.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -16,7 +16,7 @@ package org.springframework.beans.factory.xml; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; /** * Test class for Spring's ability to create diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java index 14efce4d5c6a..2260a86c52e9 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -24,27 +24,28 @@ import java.util.Set; import java.util.TreeMap; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.FieldRetrievingFactoryBean; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.beans.factory.parsing.ComponentDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.CollectingReaderEventListener; +import org.springframework.beans.testfixture.beans.CustomEnum; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.beans.CollectingReaderEventListener; -import org.springframework.tests.sample.beans.CustomEnum; -import org.springframework.tests.sample.beans.TestBean; import org.springframework.util.LinkedCaseInsensitiveMap; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop * @author Juergen Hoeller * @author Mark Fisher */ +@SuppressWarnings("rawtypes") public class UtilNamespaceHandlerTests { private DefaultListableBeanFactory beanFactory; @@ -52,7 +53,7 @@ public class UtilNamespaceHandlerTests { private CollectingReaderEventListener listener = new CollectingReaderEventListener(); - @Before + @BeforeEach public void setUp() { this.beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); @@ -64,110 +65,112 @@ public void setUp() { @Test public void testConstant() { Integer min = (Integer) this.beanFactory.getBean("min"); - assertEquals(Integer.MIN_VALUE, min.intValue()); + assertThat(min.intValue()).isEqualTo(Integer.MIN_VALUE); } @Test public void testConstantWithDefaultName() { Integer max = (Integer) this.beanFactory.getBean("java.lang.Integer.MAX_VALUE"); - assertEquals(Integer.MAX_VALUE, max.intValue()); + assertThat(max.intValue()).isEqualTo(Integer.MAX_VALUE); } @Test public void testEvents() { ComponentDefinition propertiesComponent = this.listener.getComponentDefinition("myProperties"); - assertNotNull("Event for 'myProperties' not sent", propertiesComponent); + assertThat(propertiesComponent).as("Event for 'myProperties' not sent").isNotNull(); AbstractBeanDefinition propertiesBean = (AbstractBeanDefinition) propertiesComponent.getBeanDefinitions()[0]; - assertEquals("Incorrect BeanDefinition", PropertiesFactoryBean.class, propertiesBean.getBeanClass()); + assertThat(propertiesBean.getBeanClass()).as("Incorrect BeanDefinition").isEqualTo(PropertiesFactoryBean.class); ComponentDefinition constantComponent = this.listener.getComponentDefinition("min"); - assertNotNull("Event for 'min' not sent", propertiesComponent); + assertThat(propertiesComponent).as("Event for 'min' not sent").isNotNull(); AbstractBeanDefinition constantBean = (AbstractBeanDefinition) constantComponent.getBeanDefinitions()[0]; - assertEquals("Incorrect BeanDefinition", FieldRetrievingFactoryBean.class, constantBean.getBeanClass()); + assertThat(constantBean.getBeanClass()).as("Incorrect BeanDefinition").isEqualTo(FieldRetrievingFactoryBean.class); } @Test public void testNestedProperties() { TestBean bean = (TestBean) this.beanFactory.getBean("testBean"); Properties props = bean.getSomeProperties(); - assertEquals("Incorrect property value", "bar", props.get("foo")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("bar"); } @Test public void testPropertyPath() { String name = (String) this.beanFactory.getBean("name"); - assertEquals("Rob Harrop", name); + assertThat(name).isEqualTo("Rob Harrop"); } @Test public void testNestedPropertyPath() { TestBean bean = (TestBean) this.beanFactory.getBean("testBean"); - assertEquals("Rob Harrop", bean.getName()); + assertThat(bean.getName()).isEqualTo("Rob Harrop"); } @Test public void testSimpleMap() { Map map = (Map) this.beanFactory.getBean("simpleMap"); - assertEquals("bar", map.get("foo")); + assertThat(map.get("foo")).isEqualTo("bar"); Map map2 = (Map) this.beanFactory.getBean("simpleMap"); - assertTrue(map == map2); + assertThat(map == map2).isTrue(); } @Test public void testScopedMap() { Map map = (Map) this.beanFactory.getBean("scopedMap"); - assertEquals("bar", map.get("foo")); + assertThat(map.get("foo")).isEqualTo("bar"); Map map2 = (Map) this.beanFactory.getBean("scopedMap"); - assertEquals("bar", map2.get("foo")); - assertTrue(map != map2); + assertThat(map2.get("foo")).isEqualTo("bar"); + assertThat(map != map2).isTrue(); } @Test public void testSimpleList() { List list = (List) this.beanFactory.getBean("simpleList"); - assertEquals("Rob Harrop", list.get(0)); + assertThat(list.get(0)).isEqualTo("Rob Harrop"); List list2 = (List) this.beanFactory.getBean("simpleList"); - assertTrue(list == list2); + assertThat(list == list2).isTrue(); } @Test public void testScopedList() { List list = (List) this.beanFactory.getBean("scopedList"); - assertEquals("Rob Harrop", list.get(0)); + assertThat(list.get(0)).isEqualTo("Rob Harrop"); List list2 = (List) this.beanFactory.getBean("scopedList"); - assertEquals("Rob Harrop", list2.get(0)); - assertTrue(list != list2); + assertThat(list2.get(0)).isEqualTo("Rob Harrop"); + assertThat(list != list2).isTrue(); } @Test public void testSimpleSet() { Set set = (Set) this.beanFactory.getBean("simpleSet"); - assertTrue(set.contains("Rob Harrop")); + assertThat(set.contains("Rob Harrop")).isTrue(); Set set2 = (Set) this.beanFactory.getBean("simpleSet"); - assertTrue(set == set2); + assertThat(set == set2).isTrue(); } @Test public void testScopedSet() { Set set = (Set) this.beanFactory.getBean("scopedSet"); - assertTrue(set.contains("Rob Harrop")); + assertThat(set.contains("Rob Harrop")).isTrue(); Set set2 = (Set) this.beanFactory.getBean("scopedSet"); - assertTrue(set2.contains("Rob Harrop")); - assertTrue(set != set2); + assertThat(set2.contains("Rob Harrop")).isTrue(); + assertThat(set != set2).isTrue(); } @Test public void testMapWithRef() { Map map = (Map) this.beanFactory.getBean("mapWithRef"); - assertTrue(map instanceof TreeMap); - assertEquals(this.beanFactory.getBean("testBean"), map.get("bean")); + boolean condition = map instanceof TreeMap; + assertThat(condition).isTrue(); + assertThat(map.get("bean")).isEqualTo(this.beanFactory.getBean("testBean")); } @Test public void testMapWithTypes() { Map map = (Map) this.beanFactory.getBean("mapWithTypes"); - assertTrue(map instanceof LinkedCaseInsensitiveMap); - assertEquals(this.beanFactory.getBean("testBean"), map.get("bean")); + boolean condition = map instanceof LinkedCaseInsensitiveMap; + assertThat(condition).isTrue(); + assertThat(map.get("bean")).isEqualTo(this.beanFactory.getBean("testBean")); } @Test @@ -175,51 +178,52 @@ public void testNestedCollections() { TestBean bean = (TestBean) this.beanFactory.getBean("nestedCollectionsBean"); List list = bean.getSomeList(); - assertEquals(1, list.size()); - assertEquals("foo", list.get(0)); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo("foo"); Set set = bean.getSomeSet(); - assertEquals(1, set.size()); - assertTrue(set.contains("bar")); + assertThat(set.size()).isEqualTo(1); + assertThat(set.contains("bar")).isTrue(); Map map = bean.getSomeMap(); - assertEquals(1, map.size()); - assertTrue(map.get("foo") instanceof Set); + assertThat(map.size()).isEqualTo(1); + boolean condition = map.get("foo") instanceof Set; + assertThat(condition).isTrue(); Set innerSet = (Set) map.get("foo"); - assertEquals(1, innerSet.size()); - assertTrue(innerSet.contains("bar")); + assertThat(innerSet.size()).isEqualTo(1); + assertThat(innerSet.contains("bar")).isTrue(); TestBean bean2 = (TestBean) this.beanFactory.getBean("nestedCollectionsBean"); - assertEquals(list, bean2.getSomeList()); - assertEquals(set, bean2.getSomeSet()); - assertEquals(map, bean2.getSomeMap()); - assertFalse(list == bean2.getSomeList()); - assertFalse(set == bean2.getSomeSet()); - assertFalse(map == bean2.getSomeMap()); + assertThat(bean2.getSomeList()).isEqualTo(list); + assertThat(bean2.getSomeSet()).isEqualTo(set); + assertThat(bean2.getSomeMap()).isEqualTo(map); + assertThat(list == bean2.getSomeList()).isFalse(); + assertThat(set == bean2.getSomeSet()).isFalse(); + assertThat(map == bean2.getSomeMap()).isFalse(); } @Test public void testNestedShortcutCollections() { TestBean bean = (TestBean) this.beanFactory.getBean("nestedShortcutCollections"); - assertEquals(1, bean.getStringArray().length); - assertEquals("fooStr", bean.getStringArray()[0]); + assertThat(bean.getStringArray().length).isEqualTo(1); + assertThat(bean.getStringArray()[0]).isEqualTo("fooStr"); List list = bean.getSomeList(); - assertEquals(1, list.size()); - assertEquals("foo", list.get(0)); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo("foo"); Set set = bean.getSomeSet(); - assertEquals(1, set.size()); - assertTrue(set.contains("bar")); + assertThat(set.size()).isEqualTo(1); + assertThat(set.contains("bar")).isTrue(); TestBean bean2 = (TestBean) this.beanFactory.getBean("nestedShortcutCollections"); - assertTrue(Arrays.equals(bean.getStringArray(), bean2.getStringArray())); - assertFalse(bean.getStringArray() == bean2.getStringArray()); - assertEquals(list, bean2.getSomeList()); - assertEquals(set, bean2.getSomeSet()); - assertFalse(list == bean2.getSomeList()); - assertFalse(set == bean2.getSomeSet()); + assertThat(Arrays.equals(bean.getStringArray(), bean2.getStringArray())).isTrue(); + assertThat(bean.getStringArray() == bean2.getStringArray()).isFalse(); + assertThat(bean2.getSomeList()).isEqualTo(list); + assertThat(bean2.getSomeSet()).isEqualTo(set); + assertThat(list == bean2.getSomeList()).isFalse(); + assertThat(set == bean2.getSomeSet()).isFalse(); } @Test @@ -227,25 +231,25 @@ public void testNestedInCollections() { TestBean bean = (TestBean) this.beanFactory.getBean("nestedCustomTagBean"); List list = bean.getSomeList(); - assertEquals(1, list.size()); - assertEquals(Integer.MIN_VALUE, list.get(0)); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo(Integer.MIN_VALUE); Set set = bean.getSomeSet(); - assertEquals(2, set.size()); - assertTrue(set.contains(Thread.State.NEW)); - assertTrue(set.contains(Thread.State.RUNNABLE)); + assertThat(set.size()).isEqualTo(2); + assertThat(set.contains(Thread.State.NEW)).isTrue(); + assertThat(set.contains(Thread.State.RUNNABLE)).isTrue(); Map map = bean.getSomeMap(); - assertEquals(1, map.size()); - assertEquals(CustomEnum.VALUE_1, map.get("min")); + assertThat(map.size()).isEqualTo(1); + assertThat(map.get("min")).isEqualTo(CustomEnum.VALUE_1); TestBean bean2 = (TestBean) this.beanFactory.getBean("nestedCustomTagBean"); - assertEquals(list, bean2.getSomeList()); - assertEquals(set, bean2.getSomeSet()); - assertEquals(map, bean2.getSomeMap()); - assertFalse(list == bean2.getSomeList()); - assertFalse(set == bean2.getSomeSet()); - assertFalse(map == bean2.getSomeMap()); + assertThat(bean2.getSomeList()).isEqualTo(list); + assertThat(bean2.getSomeSet()).isEqualTo(set); + assertThat(bean2.getSomeMap()).isEqualTo(map); + assertThat(list == bean2.getSomeList()).isFalse(); + assertThat(set == bean2.getSomeSet()).isFalse(); + assertThat(map == bean2.getSomeMap()).isFalse(); } @Test @@ -253,16 +257,16 @@ public void testCircularCollections() { TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionsBean"); List list = bean.getSomeList(); - assertEquals(1, list.size()); - assertEquals(bean, list.get(0)); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo(bean); Set set = bean.getSomeSet(); - assertEquals(1, set.size()); - assertTrue(set.contains(bean)); + assertThat(set.size()).isEqualTo(1); + assertThat(set.contains(bean)).isTrue(); Map map = bean.getSomeMap(); - assertEquals(1, map.size()); - assertEquals(bean, map.get("foo")); + assertThat(map.size()).isEqualTo(1); + assertThat(map.get("foo")).isEqualTo(bean); } @Test @@ -271,19 +275,19 @@ public void testCircularCollectionBeansStartingWithList() { TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean"); List list = bean.getSomeList(); - assertTrue(Proxy.isProxyClass(list.getClass())); - assertEquals(1, list.size()); - assertEquals(bean, list.get(0)); + assertThat(Proxy.isProxyClass(list.getClass())).isTrue(); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo(bean); Set set = bean.getSomeSet(); - assertFalse(Proxy.isProxyClass(set.getClass())); - assertEquals(1, set.size()); - assertTrue(set.contains(bean)); + assertThat(Proxy.isProxyClass(set.getClass())).isFalse(); + assertThat(set.size()).isEqualTo(1); + assertThat(set.contains(bean)).isTrue(); Map map = bean.getSomeMap(); - assertFalse(Proxy.isProxyClass(map.getClass())); - assertEquals(1, map.size()); - assertEquals(bean, map.get("foo")); + assertThat(Proxy.isProxyClass(map.getClass())).isFalse(); + assertThat(map.size()).isEqualTo(1); + assertThat(map.get("foo")).isEqualTo(bean); } @Test @@ -292,19 +296,19 @@ public void testCircularCollectionBeansStartingWithSet() { TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean"); List list = bean.getSomeList(); - assertFalse(Proxy.isProxyClass(list.getClass())); - assertEquals(1, list.size()); - assertEquals(bean, list.get(0)); + assertThat(Proxy.isProxyClass(list.getClass())).isFalse(); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo(bean); Set set = bean.getSomeSet(); - assertTrue(Proxy.isProxyClass(set.getClass())); - assertEquals(1, set.size()); - assertTrue(set.contains(bean)); + assertThat(Proxy.isProxyClass(set.getClass())).isTrue(); + assertThat(set.size()).isEqualTo(1); + assertThat(set.contains(bean)).isTrue(); Map map = bean.getSomeMap(); - assertFalse(Proxy.isProxyClass(map.getClass())); - assertEquals(1, map.size()); - assertEquals(bean, map.get("foo")); + assertThat(Proxy.isProxyClass(map.getClass())).isFalse(); + assertThat(map.size()).isEqualTo(1); + assertThat(map.get("foo")).isEqualTo(bean); } @Test @@ -313,80 +317,80 @@ public void testCircularCollectionBeansStartingWithMap() { TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean"); List list = bean.getSomeList(); - assertFalse(Proxy.isProxyClass(list.getClass())); - assertEquals(1, list.size()); - assertEquals(bean, list.get(0)); + assertThat(Proxy.isProxyClass(list.getClass())).isFalse(); + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0)).isEqualTo(bean); Set set = bean.getSomeSet(); - assertFalse(Proxy.isProxyClass(set.getClass())); - assertEquals(1, set.size()); - assertTrue(set.contains(bean)); + assertThat(Proxy.isProxyClass(set.getClass())).isFalse(); + assertThat(set.size()).isEqualTo(1); + assertThat(set.contains(bean)).isTrue(); Map map = bean.getSomeMap(); - assertTrue(Proxy.isProxyClass(map.getClass())); - assertEquals(1, map.size()); - assertEquals(bean, map.get("foo")); + assertThat(Proxy.isProxyClass(map.getClass())).isTrue(); + assertThat(map.size()).isEqualTo(1); + assertThat(map.get("foo")).isEqualTo(bean); } @Test public void testNestedInConstructor() { TestBean bean = (TestBean) this.beanFactory.getBean("constructedTestBean"); - assertEquals("Rob Harrop", bean.getName()); + assertThat(bean.getName()).isEqualTo("Rob Harrop"); } @Test public void testLoadProperties() { Properties props = (Properties) this.beanFactory.getBean("myProperties"); - assertEquals("Incorrect property value", "bar", props.get("foo")); - assertEquals("Incorrect property value", null, props.get("foo2")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("bar"); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo(null); Properties props2 = (Properties) this.beanFactory.getBean("myProperties"); - assertTrue(props == props2); + assertThat(props == props2).isTrue(); } @Test public void testScopedProperties() { Properties props = (Properties) this.beanFactory.getBean("myScopedProperties"); - assertEquals("Incorrect property value", "bar", props.get("foo")); - assertEquals("Incorrect property value", null, props.get("foo2")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("bar"); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo(null); Properties props2 = (Properties) this.beanFactory.getBean("myScopedProperties"); - assertEquals("Incorrect property value", "bar", props.get("foo")); - assertEquals("Incorrect property value", null, props.get("foo2")); - assertTrue(props != props2); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("bar"); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo(null); + assertThat(props != props2).isTrue(); } @Test public void testLocalProperties() { Properties props = (Properties) this.beanFactory.getBean("myLocalProperties"); - assertEquals("Incorrect property value", null, props.get("foo")); - assertEquals("Incorrect property value", "bar2", props.get("foo2")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo(null); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo("bar2"); } @Test public void testMergedProperties() { Properties props = (Properties) this.beanFactory.getBean("myMergedProperties"); - assertEquals("Incorrect property value", "bar", props.get("foo")); - assertEquals("Incorrect property value", "bar2", props.get("foo2")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("bar"); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo("bar2"); } @Test public void testLocalOverrideDefault() { Properties props = (Properties) this.beanFactory.getBean("defaultLocalOverrideProperties"); - assertEquals("Incorrect property value", "bar", props.get("foo")); - assertEquals("Incorrect property value", "local2", props.get("foo2")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("bar"); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo("local2"); } @Test public void testLocalOverrideFalse() { Properties props = (Properties) this.beanFactory.getBean("falseLocalOverrideProperties"); - assertEquals("Incorrect property value", "bar", props.get("foo")); - assertEquals("Incorrect property value", "local2", props.get("foo2")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("bar"); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo("local2"); } @Test public void testLocalOverrideTrue() { Properties props = (Properties) this.beanFactory.getBean("trueLocalOverrideProperties"); - assertEquals("Incorrect property value", "local", props.get("foo")); - assertEquals("Incorrect property value", "local2", props.get("foo2")); + assertThat(props.get("foo")).as("Incorrect property value").isEqualTo("local"); + assertThat(props.get("foo2")).as("Incorrect property value").isEqualTo("local2"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java index c7ac691035a9..e44f0d9df7a0 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -29,8 +29,8 @@ import java.util.TreeMap; import java.util.TreeSet; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanDefinitionStoreException; @@ -38,11 +38,12 @@ import org.springframework.beans.factory.config.MapFactoryBean; import org.springframework.beans.factory.config.SetFactoryBean; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.testfixture.beans.HasMap; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.HasMap; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for collections in XML bean definitions. @@ -51,12 +52,13 @@ * @author Chris Beams * @since 19.12.2004 */ +@SuppressWarnings({ "rawtypes", "unchecked" }) public class XmlBeanCollectionTests { private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - @Before + @BeforeEach public void loadBeans() { new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( new ClassPathResource("collections.xml", getClass())); @@ -68,17 +70,20 @@ public void testCollectionFactoryDefaults() throws Exception { ListFactoryBean listFactory = new ListFactoryBean(); listFactory.setSourceList(new LinkedList()); listFactory.afterPropertiesSet(); - assertTrue(listFactory.getObject() instanceof ArrayList); + boolean condition2 = listFactory.getObject() instanceof ArrayList; + assertThat(condition2).isTrue(); SetFactoryBean setFactory = new SetFactoryBean(); setFactory.setSourceSet(new TreeSet()); setFactory.afterPropertiesSet(); - assertTrue(setFactory.getObject() instanceof LinkedHashSet); + boolean condition1 = setFactory.getObject() instanceof LinkedHashSet; + assertThat(condition1).isTrue(); MapFactoryBean mapFactory = new MapFactoryBean(); mapFactory.setSourceMap(new TreeMap()); mapFactory.afterPropertiesSet(); - assertTrue(mapFactory.getObject() instanceof LinkedHashMap); + boolean condition = mapFactory.getObject() instanceof LinkedHashMap; + assertThat(condition).isTrue(); } @Test @@ -86,25 +91,25 @@ public void testRefSubelement() throws Exception { //assertTrue("5 beans in reftypes, not " + this.beanFactory.getBeanDefinitionCount(), this.beanFactory.getBeanDefinitionCount() == 5); TestBean jen = (TestBean) this.beanFactory.getBean("jenny"); TestBean dave = (TestBean) this.beanFactory.getBean("david"); - assertTrue(jen.getSpouse() == dave); + assertThat(jen.getSpouse() == dave).isTrue(); } @Test public void testPropertyWithLiteralValueSubelement() throws Exception { TestBean verbose = (TestBean) this.beanFactory.getBean("verbose"); - assertTrue(verbose.getName().equals("verbose")); + assertThat(verbose.getName().equals("verbose")).isTrue(); } @Test public void testPropertyWithIdRefLocalAttrSubelement() throws Exception { TestBean verbose = (TestBean) this.beanFactory.getBean("verbose2"); - assertTrue(verbose.getName().equals("verbose")); + assertThat(verbose.getName().equals("verbose")).isTrue(); } @Test public void testPropertyWithIdRefBeanAttrSubelement() throws Exception { TestBean verbose = (TestBean) this.beanFactory.getBean("verbose3"); - assertTrue(verbose.getName().equals("verbose")); + assertThat(verbose.getName().equals("verbose")).isTrue(); } @Test @@ -117,10 +122,10 @@ public void testRefSubelementsBuildCollection() throws Exception { // Our bean doesn't modify the collection: // of course it could be a different copy in a real object. Object[] friends = rod.getFriends().toArray(); - assertTrue(friends.length == 2); + assertThat(friends.length == 2).isTrue(); - assertTrue("First friend must be jen, not " + friends[0], friends[0] == jen); - assertTrue(friends[1] == dave); + assertThat(friends[0] == jen).as("First friend must be jen, not " + friends[0]).isTrue(); + assertThat(friends[1] == dave).isTrue(); // Should be ordered } @@ -131,314 +136,314 @@ public void testRefSubelementsBuildCollectionWithPrototypes() throws Exception { TestBean rod = (TestBean) this.beanFactory.getBean("pRod"); Object[] friends = rod.getFriends().toArray(); - assertTrue(friends.length == 2); - assertTrue("First friend must be jen, not " + friends[0], - friends[0].toString().equals(jen.toString())); - assertTrue("Jen not same instance", friends[0] != jen); - assertTrue(friends[1].toString().equals(dave.toString())); - assertTrue("Dave not same instance", friends[1] != dave); - assertEquals("Jen", dave.getSpouse().getName()); + assertThat(friends.length == 2).isTrue(); + assertThat(friends[0].toString().equals(jen.toString())).as("First friend must be jen, not " + friends[0]).isTrue(); + assertThat(friends[0] != jen).as("Jen not same instance").isTrue(); + assertThat(friends[1].toString().equals(dave.toString())).isTrue(); + assertThat(friends[1] != dave).as("Dave not same instance").isTrue(); + assertThat(dave.getSpouse().getName()).isEqualTo("Jen"); TestBean rod2 = (TestBean) this.beanFactory.getBean("pRod"); Object[] friends2 = rod2.getFriends().toArray(); - assertTrue(friends2.length == 2); - assertTrue("First friend must be jen, not " + friends2[0], - friends2[0].toString().equals(jen.toString())); - assertTrue("Jen not same instance", friends2[0] != friends[0]); - assertTrue(friends2[1].toString().equals(dave.toString())); - assertTrue("Dave not same instance", friends2[1] != friends[1]); + assertThat(friends2.length == 2).isTrue(); + assertThat(friends2[0].toString().equals(jen.toString())).as("First friend must be jen, not " + friends2[0]).isTrue(); + assertThat(friends2[0] != friends[0]).as("Jen not same instance").isTrue(); + assertThat(friends2[1].toString().equals(dave.toString())).isTrue(); + assertThat(friends2[1] != friends[1]).as("Dave not same instance").isTrue(); } @Test public void testRefSubelementsBuildCollectionFromSingleElement() throws Exception { TestBean loner = (TestBean) this.beanFactory.getBean("loner"); TestBean dave = (TestBean) this.beanFactory.getBean("david"); - assertTrue(loner.getFriends().size() == 1); - assertTrue(loner.getFriends().contains(dave)); + assertThat(loner.getFriends().size() == 1).isTrue(); + assertThat(loner.getFriends().contains(dave)).isTrue(); } @Test public void testBuildCollectionFromMixtureOfReferencesAndValues() throws Exception { MixedCollectionBean jumble = (MixedCollectionBean) this.beanFactory.getBean("jumble"); - assertTrue("Expected 5 elements, not " + jumble.getJumble().size(), - jumble.getJumble().size() == 5); + assertThat(jumble.getJumble().size() == 5).as("Expected 5 elements, not " + jumble.getJumble().size()).isTrue(); List l = (List) jumble.getJumble(); - assertTrue(l.get(0).equals(this.beanFactory.getBean("david"))); - assertTrue(l.get(1).equals("literal")); - assertTrue(l.get(2).equals(this.beanFactory.getBean("jenny"))); - assertTrue(l.get(3).equals("rod")); + assertThat(l.get(0).equals(this.beanFactory.getBean("david"))).isTrue(); + assertThat(l.get(1).equals("literal")).isTrue(); + assertThat(l.get(2).equals(this.beanFactory.getBean("jenny"))).isTrue(); + assertThat(l.get(3).equals("rod")).isTrue(); Object[] array = (Object[]) l.get(4); - assertTrue(array[0].equals(this.beanFactory.getBean("david"))); - assertTrue(array[1].equals("literal2")); + assertThat(array[0].equals(this.beanFactory.getBean("david"))).isTrue(); + assertThat(array[1].equals("literal2")).isTrue(); } @Test public void testInvalidBeanNameReference() throws Exception { - try { - this.beanFactory.getBean("jumble2"); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getCause() instanceof BeanDefinitionStoreException); - assertTrue(ex.getCause().getMessage().contains("rod2")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> + this.beanFactory.getBean("jumble2")) + .withCauseInstanceOf(BeanDefinitionStoreException.class) + .withMessageContaining("rod2"); } @Test public void testEmptyMap() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("emptyMap"); - assertTrue(hasMap.getMap().size() == 0); + assertThat(hasMap.getMap().size() == 0).isTrue(); } @Test public void testMapWithLiteralsOnly() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("literalMap"); - assertTrue(hasMap.getMap().size() == 3); - assertTrue(hasMap.getMap().get("foo").equals("bar")); - assertTrue(hasMap.getMap().get("fi").equals("fum")); - assertTrue(hasMap.getMap().get("fa") == null); + assertThat(hasMap.getMap().size() == 3).isTrue(); + assertThat(hasMap.getMap().get("foo").equals("bar")).isTrue(); + assertThat(hasMap.getMap().get("fi").equals("fum")).isTrue(); + assertThat(hasMap.getMap().get("fa") == null).isTrue(); } @Test public void testMapWithLiteralsAndReferences() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("mixedMap"); - assertTrue(hasMap.getMap().size() == 5); - assertTrue(hasMap.getMap().get("foo").equals(new Integer(10))); + assertThat(hasMap.getMap().size() == 5).isTrue(); + assertThat(hasMap.getMap().get("foo").equals(10)).isTrue(); TestBean jenny = (TestBean) this.beanFactory.getBean("jenny"); - assertTrue(hasMap.getMap().get("jenny") == jenny); - assertTrue(hasMap.getMap().get(new Integer(5)).equals("david")); - assertTrue(hasMap.getMap().get("bar") instanceof Long); - assertTrue(hasMap.getMap().get("bar").equals(new Long(100))); - assertTrue(hasMap.getMap().get("baz") instanceof Integer); - assertTrue(hasMap.getMap().get("baz").equals(new Integer(200))); + assertThat(hasMap.getMap().get("jenny") == jenny).isTrue(); + assertThat(hasMap.getMap().get(5).equals("david")).isTrue(); + boolean condition1 = hasMap.getMap().get("bar") instanceof Long; + assertThat(condition1).isTrue(); + assertThat(hasMap.getMap().get("bar").equals(100L)).isTrue(); + boolean condition = hasMap.getMap().get("baz") instanceof Integer; + assertThat(condition).isTrue(); + assertThat(hasMap.getMap().get("baz").equals(200)).isTrue(); } @Test public void testMapWithLiteralsAndPrototypeReferences() throws Exception { TestBean jenny = (TestBean) this.beanFactory.getBean("pJenny"); HasMap hasMap = (HasMap) this.beanFactory.getBean("pMixedMap"); - assertTrue(hasMap.getMap().size() == 2); - assertTrue(hasMap.getMap().get("foo").equals("bar")); - assertTrue(hasMap.getMap().get("jenny").toString().equals(jenny.toString())); - assertTrue("Not same instance", hasMap.getMap().get("jenny") != jenny); + assertThat(hasMap.getMap().size() == 2).isTrue(); + assertThat(hasMap.getMap().get("foo").equals("bar")).isTrue(); + assertThat(hasMap.getMap().get("jenny").toString().equals(jenny.toString())).isTrue(); + assertThat(hasMap.getMap().get("jenny") != jenny).as("Not same instance").isTrue(); HasMap hasMap2 = (HasMap) this.beanFactory.getBean("pMixedMap"); - assertTrue(hasMap2.getMap().size() == 2); - assertTrue(hasMap2.getMap().get("foo").equals("bar")); - assertTrue(hasMap2.getMap().get("jenny").toString().equals(jenny.toString())); - assertTrue("Not same instance", hasMap2.getMap().get("jenny") != hasMap.getMap().get("jenny")); + assertThat(hasMap2.getMap().size() == 2).isTrue(); + assertThat(hasMap2.getMap().get("foo").equals("bar")).isTrue(); + assertThat(hasMap2.getMap().get("jenny").toString().equals(jenny.toString())).isTrue(); + assertThat(hasMap2.getMap().get("jenny") != hasMap.getMap().get("jenny")).as("Not same instance").isTrue(); } @Test public void testMapWithLiteralsReferencesAndList() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("mixedMapWithList"); - assertTrue(hasMap.getMap().size() == 4); - assertTrue(hasMap.getMap().get(null).equals("bar")); + assertThat(hasMap.getMap().size() == 4).isTrue(); + assertThat(hasMap.getMap().get(null).equals("bar")).isTrue(); TestBean jenny = (TestBean) this.beanFactory.getBean("jenny"); - assertTrue(hasMap.getMap().get("jenny").equals(jenny)); + assertThat(hasMap.getMap().get("jenny").equals(jenny)).isTrue(); // Check list List l = (List) hasMap.getMap().get("list"); - assertNotNull(l); - assertTrue(l.size() == 4); - assertTrue(l.get(0).equals("zero")); - assertTrue(l.get(3) == null); + assertThat(l).isNotNull(); + assertThat(l.size() == 4).isTrue(); + assertThat(l.get(0).equals("zero")).isTrue(); + assertThat(l.get(3) == null).isTrue(); // Check nested map in list Map m = (Map) l.get(1); - assertNotNull(m); - assertTrue(m.size() == 2); - assertTrue(m.get("fo").equals("bar")); - assertTrue("Map element 'jenny' should be equal to jenny bean, not " + m.get("jen"), - m.get("jen").equals(jenny)); + assertThat(m).isNotNull(); + assertThat(m.size() == 2).isTrue(); + assertThat(m.get("fo").equals("bar")).isTrue(); + assertThat(m.get("jen").equals(jenny)).as("Map element 'jenny' should be equal to jenny bean, not " + m.get("jen")).isTrue(); // Check nested list in list l = (List) l.get(2); - assertNotNull(l); - assertTrue(l.size() == 2); - assertTrue(l.get(0).equals(jenny)); - assertTrue(l.get(1).equals("ba")); + assertThat(l).isNotNull(); + assertThat(l.size() == 2).isTrue(); + assertThat(l.get(0).equals(jenny)).isTrue(); + assertThat(l.get(1).equals("ba")).isTrue(); // Check nested map m = (Map) hasMap.getMap().get("map"); - assertNotNull(m); - assertTrue(m.size() == 2); - assertTrue(m.get("foo").equals("bar")); - assertTrue("Map element 'jenny' should be equal to jenny bean, not " + m.get("jenny"), - m.get("jenny").equals(jenny)); + assertThat(m).isNotNull(); + assertThat(m.size() == 2).isTrue(); + assertThat(m.get("foo").equals("bar")).isTrue(); + assertThat(m.get("jenny").equals(jenny)).as("Map element 'jenny' should be equal to jenny bean, not " + m.get("jenny")).isTrue(); } @Test public void testEmptySet() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("emptySet"); - assertTrue(hasMap.getSet().size() == 0); + assertThat(hasMap.getSet().size() == 0).isTrue(); } @Test public void testPopulatedSet() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("set"); - assertTrue(hasMap.getSet().size() == 3); - assertTrue(hasMap.getSet().contains("bar")); + assertThat(hasMap.getSet().size() == 3).isTrue(); + assertThat(hasMap.getSet().contains("bar")).isTrue(); TestBean jenny = (TestBean) this.beanFactory.getBean("jenny"); - assertTrue(hasMap.getSet().contains(jenny)); - assertTrue(hasMap.getSet().contains(null)); + assertThat(hasMap.getSet().contains(jenny)).isTrue(); + assertThat(hasMap.getSet().contains(null)).isTrue(); Iterator it = hasMap.getSet().iterator(); - assertEquals("bar", it.next()); - assertEquals(jenny, it.next()); - assertEquals(null, it.next()); + assertThat(it.next()).isEqualTo("bar"); + assertThat(it.next()).isEqualTo(jenny); + assertThat(it.next()).isEqualTo(null); } @Test public void testPopulatedConcurrentSet() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("concurrentSet"); - assertTrue(hasMap.getConcurrentSet().size() == 3); - assertTrue(hasMap.getConcurrentSet().contains("bar")); + assertThat(hasMap.getConcurrentSet().size() == 3).isTrue(); + assertThat(hasMap.getConcurrentSet().contains("bar")).isTrue(); TestBean jenny = (TestBean) this.beanFactory.getBean("jenny"); - assertTrue(hasMap.getConcurrentSet().contains(jenny)); - assertTrue(hasMap.getConcurrentSet().contains(null)); + assertThat(hasMap.getConcurrentSet().contains(jenny)).isTrue(); + assertThat(hasMap.getConcurrentSet().contains(null)).isTrue(); } @Test public void testPopulatedIdentityMap() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("identityMap"); - assertTrue(hasMap.getIdentityMap().size() == 2); + assertThat(hasMap.getIdentityMap().size() == 2).isTrue(); HashSet set = new HashSet(hasMap.getIdentityMap().keySet()); - assertTrue(set.contains("foo")); - assertTrue(set.contains("jenny")); + assertThat(set.contains("foo")).isTrue(); + assertThat(set.contains("jenny")).isTrue(); } @Test public void testEmptyProps() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("emptyProps"); - assertTrue(hasMap.getProps().size() == 0); - assertEquals(hasMap.getProps().getClass(), Properties.class); + assertThat(hasMap.getProps().size() == 0).isTrue(); + assertThat(Properties.class).isEqualTo(hasMap.getProps().getClass()); } @Test public void testPopulatedProps() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("props"); - assertTrue(hasMap.getProps().size() == 2); - assertTrue(hasMap.getProps().get("foo").equals("bar")); - assertTrue(hasMap.getProps().get("2").equals("TWO")); + assertThat(hasMap.getProps().size() == 2).isTrue(); + assertThat(hasMap.getProps().get("foo").equals("bar")).isTrue(); + assertThat(hasMap.getProps().get("2").equals("TWO")).isTrue(); } @Test public void testObjectArray() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("objectArray"); - assertTrue(hasMap.getObjectArray().length == 2); - assertTrue(hasMap.getObjectArray()[0].equals("one")); - assertTrue(hasMap.getObjectArray()[1].equals(this.beanFactory.getBean("jenny"))); + assertThat(hasMap.getObjectArray().length == 2).isTrue(); + assertThat(hasMap.getObjectArray()[0].equals("one")).isTrue(); + assertThat(hasMap.getObjectArray()[1].equals(this.beanFactory.getBean("jenny"))).isTrue(); } @Test public void testIntegerArray() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("integerArray"); - assertTrue(hasMap.getIntegerArray().length == 3); - assertTrue(hasMap.getIntegerArray()[0].intValue() == 0); - assertTrue(hasMap.getIntegerArray()[1].intValue() == 1); - assertTrue(hasMap.getIntegerArray()[2].intValue() == 2); + assertThat(hasMap.getIntegerArray().length == 3).isTrue(); + assertThat(hasMap.getIntegerArray()[0].intValue() == 0).isTrue(); + assertThat(hasMap.getIntegerArray()[1].intValue() == 1).isTrue(); + assertThat(hasMap.getIntegerArray()[2].intValue() == 2).isTrue(); } @Test public void testClassArray() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("classArray"); - assertTrue(hasMap.getClassArray().length == 2); - assertTrue(hasMap.getClassArray()[0].equals(String.class)); - assertTrue(hasMap.getClassArray()[1].equals(Exception.class)); + assertThat(hasMap.getClassArray().length == 2).isTrue(); + assertThat(hasMap.getClassArray()[0].equals(String.class)).isTrue(); + assertThat(hasMap.getClassArray()[1].equals(Exception.class)).isTrue(); } @Test public void testClassList() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("classList"); - assertTrue(hasMap.getClassList().size()== 2); - assertTrue(hasMap.getClassList().get(0).equals(String.class)); - assertTrue(hasMap.getClassList().get(1).equals(Exception.class)); + assertThat(hasMap.getClassList().size()== 2).isTrue(); + assertThat(hasMap.getClassList().get(0).equals(String.class)).isTrue(); + assertThat(hasMap.getClassList().get(1).equals(Exception.class)).isTrue(); } @Test public void testProps() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("props"); - assertEquals(2, hasMap.getProps().size()); - assertEquals("bar", hasMap.getProps().getProperty("foo")); - assertEquals("TWO", hasMap.getProps().getProperty("2")); + assertThat(hasMap.getProps().size()).isEqualTo(2); + assertThat(hasMap.getProps().getProperty("foo")).isEqualTo("bar"); + assertThat(hasMap.getProps().getProperty("2")).isEqualTo("TWO"); HasMap hasMap2 = (HasMap) this.beanFactory.getBean("propsViaMap"); - assertEquals(2, hasMap2.getProps().size()); - assertEquals("bar", hasMap2.getProps().getProperty("foo")); - assertEquals("TWO", hasMap2.getProps().getProperty("2")); + assertThat(hasMap2.getProps().size()).isEqualTo(2); + assertThat(hasMap2.getProps().getProperty("foo")).isEqualTo("bar"); + assertThat(hasMap2.getProps().getProperty("2")).isEqualTo("TWO"); } @Test public void testListFactory() throws Exception { List list = (List) this.beanFactory.getBean("listFactory"); - assertTrue(list instanceof LinkedList); - assertTrue(list.size() == 2); - assertEquals("bar", list.get(0)); - assertEquals("jenny", list.get(1)); + boolean condition = list instanceof LinkedList; + assertThat(condition).isTrue(); + assertThat(list.size() == 2).isTrue(); + assertThat(list.get(0)).isEqualTo("bar"); + assertThat(list.get(1)).isEqualTo("jenny"); } @Test public void testPrototypeListFactory() throws Exception { List list = (List) this.beanFactory.getBean("pListFactory"); - assertTrue(list instanceof LinkedList); - assertTrue(list.size() == 2); - assertEquals("bar", list.get(0)); - assertEquals("jenny", list.get(1)); + boolean condition = list instanceof LinkedList; + assertThat(condition).isTrue(); + assertThat(list.size() == 2).isTrue(); + assertThat(list.get(0)).isEqualTo("bar"); + assertThat(list.get(1)).isEqualTo("jenny"); } @Test public void testSetFactory() throws Exception { Set set = (Set) this.beanFactory.getBean("setFactory"); - assertTrue(set instanceof TreeSet); - assertTrue(set.size() == 2); - assertTrue(set.contains("bar")); - assertTrue(set.contains("jenny")); + boolean condition = set instanceof TreeSet; + assertThat(condition).isTrue(); + assertThat(set.size() == 2).isTrue(); + assertThat(set.contains("bar")).isTrue(); + assertThat(set.contains("jenny")).isTrue(); } @Test public void testPrototypeSetFactory() throws Exception { Set set = (Set) this.beanFactory.getBean("pSetFactory"); - assertTrue(set instanceof TreeSet); - assertTrue(set.size() == 2); - assertTrue(set.contains("bar")); - assertTrue(set.contains("jenny")); + boolean condition = set instanceof TreeSet; + assertThat(condition).isTrue(); + assertThat(set.size() == 2).isTrue(); + assertThat(set.contains("bar")).isTrue(); + assertThat(set.contains("jenny")).isTrue(); } @Test public void testMapFactory() throws Exception { Map map = (Map) this.beanFactory.getBean("mapFactory"); - assertTrue(map instanceof TreeMap); - assertTrue(map.size() == 2); - assertEquals("bar", map.get("foo")); - assertEquals("jenny", map.get("jen")); + boolean condition = map instanceof TreeMap; + assertThat(condition).isTrue(); + assertThat(map.size() == 2).isTrue(); + assertThat(map.get("foo")).isEqualTo("bar"); + assertThat(map.get("jen")).isEqualTo("jenny"); } @Test public void testPrototypeMapFactory() throws Exception { Map map = (Map) this.beanFactory.getBean("pMapFactory"); - assertTrue(map instanceof TreeMap); - assertTrue(map.size() == 2); - assertEquals("bar", map.get("foo")); - assertEquals("jenny", map.get("jen")); + boolean condition = map instanceof TreeMap; + assertThat(condition).isTrue(); + assertThat(map.size() == 2).isTrue(); + assertThat(map.get("foo")).isEqualTo("bar"); + assertThat(map.get("jen")).isEqualTo("jenny"); } @Test public void testChoiceBetweenSetAndMap() { MapAndSet sam = (MapAndSet) this.beanFactory.getBean("setAndMap"); - assertTrue("Didn't choose constructor with Map argument", sam.getObject() instanceof Map); + boolean condition = sam.getObject() instanceof Map; + assertThat(condition).as("Didn't choose constructor with Map argument").isTrue(); Map map = (Map) sam.getObject(); - assertEquals(3, map.size()); - assertEquals("val1", map.get("key1")); - assertEquals("val2", map.get("key2")); - assertEquals("val3", map.get("key3")); + assertThat(map.size()).isEqualTo(3); + assertThat(map.get("key1")).isEqualTo("val1"); + assertThat(map.get("key2")).isEqualTo("val2"); + assertThat(map.get("key3")).isEqualTo("val3"); } @Test public void testEnumSetFactory() throws Exception { Set set = (Set) this.beanFactory.getBean("enumSetFactory"); - assertTrue(set.size() == 2); - assertTrue(set.contains("ONE")); - assertTrue(set.contains("TWO")); + assertThat(set.size() == 2).isTrue(); + assertThat(set.contains("ONE")).isTrue(); + assertThat(set.contains("TWO")).isTrue(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java index 213b566290eb..69768da8278f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,20 +18,21 @@ import java.util.Arrays; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.InputSource; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; -import org.springframework.tests.sample.beans.TestBean; import org.springframework.util.ObjectUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rick Evans @@ -46,11 +47,12 @@ public void setParserClassSunnyDay() { new XmlBeanDefinitionReader(registry).setDocumentReaderClass(DefaultBeanDefinitionDocumentReader.class); } - @Test(expected = BeanDefinitionStoreException.class) + @Test public void withOpenInputStream() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); - new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource)); } @Test @@ -79,11 +81,12 @@ public void withWildcardImport() { testBeanDefinitions(registry); } - @Test(expected = BeanDefinitionStoreException.class) + @Test public void withInputSource() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml")); - new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); + assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> + new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource)); } @Test @@ -105,19 +108,19 @@ public void withFreshInputStream() { } private void testBeanDefinitions(BeanDefinitionRegistry registry) { - assertEquals(24, registry.getBeanDefinitionCount()); - assertEquals(24, registry.getBeanDefinitionNames().length); - assertTrue(Arrays.asList(registry.getBeanDefinitionNames()).contains("rod")); - assertTrue(Arrays.asList(registry.getBeanDefinitionNames()).contains("aliased")); - assertTrue(registry.containsBeanDefinition("rod")); - assertTrue(registry.containsBeanDefinition("aliased")); - assertEquals(TestBean.class.getName(), registry.getBeanDefinition("rod").getBeanClassName()); - assertEquals(TestBean.class.getName(), registry.getBeanDefinition("aliased").getBeanClassName()); - assertTrue(registry.isAlias("youralias")); + assertThat(registry.getBeanDefinitionCount()).isEqualTo(24); + assertThat(registry.getBeanDefinitionNames().length).isEqualTo(24); + assertThat(Arrays.asList(registry.getBeanDefinitionNames()).contains("rod")).isTrue(); + assertThat(Arrays.asList(registry.getBeanDefinitionNames()).contains("aliased")).isTrue(); + assertThat(registry.containsBeanDefinition("rod")).isTrue(); + assertThat(registry.containsBeanDefinition("aliased")).isTrue(); + assertThat(registry.getBeanDefinition("rod").getBeanClassName()).isEqualTo(TestBean.class.getName()); + assertThat(registry.getBeanDefinition("aliased").getBeanClassName()).isEqualTo(TestBean.class.getName()); + assertThat(registry.isAlias("youralias")).isTrue(); String[] aliases = registry.getAliases("aliased"); - assertEquals(2, aliases.length); - assertTrue(ObjectUtils.containsElement(aliases, "myalias")); - assertTrue(ObjectUtils.containsElement(aliases, "youralias")); + assertThat(aliases.length).isEqualTo(2); + assertThat(ObjectUtils.containsElement(aliases, "myalias")).isTrue(); + assertThat(ObjectUtils.containsElement(aliases, "youralias")).isTrue(); } @Test @@ -135,7 +138,7 @@ private void doTestValidation(String resourceName) { Resource resource = new ClassPathResource(resourceName, getClass()); new XmlBeanDefinitionReader(factory).loadBeanDefinitions(resource); TestBean bean = (TestBean) factory.getBean("testBean"); - assertNotNull(bean); + assertThat(bean).isNotNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java index 15b8ad914a65..d48b55f2cb8d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,8 +21,8 @@ import java.util.List; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; @@ -30,13 +30,14 @@ import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.LifecycleBean; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.beans.testfixture.beans.factory.DummyFactory; +import org.springframework.beans.testfixture.factory.xml.AbstractListableBeanFactoryTests; import org.springframework.core.io.ClassPathResource; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.LifecycleBean; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.tests.sample.beans.factory.DummyFactory; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -50,7 +51,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest private DefaultListableBeanFactory factory; - @Before + @BeforeEach public void setup() { parent = new DefaultListableBeanFactory(); @@ -123,7 +124,7 @@ public void protectedLifecycleMethods() { @Test public void descriptionButNoProperties() { TestBean validEmpty = (TestBean) getBeanFactory().getBean("validEmptyWithDescription"); - assertEquals(0, validEmpty.getAge()); + assertThat(validEmpty.getAge()).isEqualTo(0); } /** @@ -135,71 +136,71 @@ public void autoAliasing() { TestBean tb1 = (TestBean) getBeanFactory().getBean("aliased"); TestBean alias1 = (TestBean) getBeanFactory().getBean("myalias"); - assertTrue(tb1 == alias1); + assertThat(tb1 == alias1).isTrue(); List tb1Aliases = Arrays.asList(getBeanFactory().getAliases("aliased")); - assertEquals(2, tb1Aliases.size()); - assertTrue(tb1Aliases.contains("myalias")); - assertTrue(tb1Aliases.contains("youralias")); - assertTrue(beanNames.contains("aliased")); - assertFalse(beanNames.contains("myalias")); - assertFalse(beanNames.contains("youralias")); + assertThat(tb1Aliases.size()).isEqualTo(2); + assertThat(tb1Aliases.contains("myalias")).isTrue(); + assertThat(tb1Aliases.contains("youralias")).isTrue(); + assertThat(beanNames.contains("aliased")).isTrue(); + assertThat(beanNames.contains("myalias")).isFalse(); + assertThat(beanNames.contains("youralias")).isFalse(); TestBean tb2 = (TestBean) getBeanFactory().getBean("multiAliased"); TestBean alias2 = (TestBean) getBeanFactory().getBean("alias1"); TestBean alias3 = (TestBean) getBeanFactory().getBean("alias2"); TestBean alias3a = (TestBean) getBeanFactory().getBean("alias3"); TestBean alias3b = (TestBean) getBeanFactory().getBean("alias4"); - assertTrue(tb2 == alias2); - assertTrue(tb2 == alias3); - assertTrue(tb2 == alias3a); - assertTrue(tb2 == alias3b); + assertThat(tb2 == alias2).isTrue(); + assertThat(tb2 == alias3).isTrue(); + assertThat(tb2 == alias3a).isTrue(); + assertThat(tb2 == alias3b).isTrue(); List tb2Aliases = Arrays.asList(getBeanFactory().getAliases("multiAliased")); - assertEquals(4, tb2Aliases.size()); - assertTrue(tb2Aliases.contains("alias1")); - assertTrue(tb2Aliases.contains("alias2")); - assertTrue(tb2Aliases.contains("alias3")); - assertTrue(tb2Aliases.contains("alias4")); - assertTrue(beanNames.contains("multiAliased")); - assertFalse(beanNames.contains("alias1")); - assertFalse(beanNames.contains("alias2")); - assertFalse(beanNames.contains("alias3")); - assertFalse(beanNames.contains("alias4")); + assertThat(tb2Aliases.size()).isEqualTo(4); + assertThat(tb2Aliases.contains("alias1")).isTrue(); + assertThat(tb2Aliases.contains("alias2")).isTrue(); + assertThat(tb2Aliases.contains("alias3")).isTrue(); + assertThat(tb2Aliases.contains("alias4")).isTrue(); + assertThat(beanNames.contains("multiAliased")).isTrue(); + assertThat(beanNames.contains("alias1")).isFalse(); + assertThat(beanNames.contains("alias2")).isFalse(); + assertThat(beanNames.contains("alias3")).isFalse(); + assertThat(beanNames.contains("alias4")).isFalse(); TestBean tb3 = (TestBean) getBeanFactory().getBean("aliasWithoutId1"); TestBean alias4 = (TestBean) getBeanFactory().getBean("aliasWithoutId2"); TestBean alias5 = (TestBean) getBeanFactory().getBean("aliasWithoutId3"); - assertTrue(tb3 == alias4); - assertTrue(tb3 == alias5); + assertThat(tb3 == alias4).isTrue(); + assertThat(tb3 == alias5).isTrue(); List tb3Aliases = Arrays.asList(getBeanFactory().getAliases("aliasWithoutId1")); - assertEquals(2, tb3Aliases.size()); - assertTrue(tb3Aliases.contains("aliasWithoutId2")); - assertTrue(tb3Aliases.contains("aliasWithoutId3")); - assertTrue(beanNames.contains("aliasWithoutId1")); - assertFalse(beanNames.contains("aliasWithoutId2")); - assertFalse(beanNames.contains("aliasWithoutId3")); + assertThat(tb3Aliases.size()).isEqualTo(2); + assertThat(tb3Aliases.contains("aliasWithoutId2")).isTrue(); + assertThat(tb3Aliases.contains("aliasWithoutId3")).isTrue(); + assertThat(beanNames.contains("aliasWithoutId1")).isTrue(); + assertThat(beanNames.contains("aliasWithoutId2")).isFalse(); + assertThat(beanNames.contains("aliasWithoutId3")).isFalse(); TestBean tb4 = (TestBean) getBeanFactory().getBean(TestBean.class.getName() + "#0"); - assertEquals(null, tb4.getName()); + assertThat(tb4.getName()).isEqualTo(null); Map drs = getListableBeanFactory().getBeansOfType(DummyReferencer.class, false, false); - assertEquals(5, drs.size()); - assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#0")); - assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#1")); - assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#2")); + assertThat(drs.size()).isEqualTo(5); + assertThat(drs.containsKey(DummyReferencer.class.getName() + "#0")).isTrue(); + assertThat(drs.containsKey(DummyReferencer.class.getName() + "#1")).isTrue(); + assertThat(drs.containsKey(DummyReferencer.class.getName() + "#2")).isTrue(); } @Test public void factoryNesting() { ITestBean father = (ITestBean) getBeanFactory().getBean("father"); - assertTrue("Bean from root context", father != null); + assertThat(father != null).as("Bean from root context").isTrue(); TestBean rod = (TestBean) getBeanFactory().getBean("rod"); - assertTrue("Bean from child context", "Rod".equals(rod.getName())); - assertTrue("Bean has external reference", rod.getSpouse() == father); + assertThat("Rod".equals(rod.getName())).as("Bean from child context").isTrue(); + assertThat(rod.getSpouse() == father).as("Bean has external reference").isTrue(); rod = (TestBean) parent.getBean("rod"); - assertTrue("Bean from root context", "Roderick".equals(rod.getName())); + assertThat("Roderick".equals(rod.getName())).as("Bean from root context").isTrue(); } @Test @@ -207,25 +208,25 @@ public void factoryReferences() { DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory"); DummyReferencer ref = (DummyReferencer) getBeanFactory().getBean("factoryReferencer"); - assertTrue(ref.getTestBean1() == ref.getTestBean2()); - assertTrue(ref.getDummyFactory() == factory); + assertThat(ref.getTestBean1() == ref.getTestBean2()).isTrue(); + assertThat(ref.getDummyFactory() == factory).isTrue(); DummyReferencer ref2 = (DummyReferencer) getBeanFactory().getBean("factoryReferencerWithConstructor"); - assertTrue(ref2.getTestBean1() == ref2.getTestBean2()); - assertTrue(ref2.getDummyFactory() == factory); + assertThat(ref2.getTestBean1() == ref2.getTestBean2()).isTrue(); + assertThat(ref2.getDummyFactory() == factory).isTrue(); } @Test public void prototypeReferences() { // check that not broken by circular reference resolution mechanism DummyReferencer ref1 = (DummyReferencer) getBeanFactory().getBean("prototypeReferencer"); - assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref1.getTestBean2()); + assertThat(ref1.getTestBean1() != ref1.getTestBean2()).as("Not referencing same bean twice").isTrue(); DummyReferencer ref2 = (DummyReferencer) getBeanFactory().getBean("prototypeReferencer"); - assertTrue("Not the same referencer", ref1 != ref2); - assertTrue("Not referencing same bean twice", ref2.getTestBean1() != ref2.getTestBean2()); - assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean1()); - assertTrue("Not referencing same bean twice", ref1.getTestBean2() != ref2.getTestBean2()); - assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean2()); + assertThat(ref1 != ref2).as("Not the same referencer").isTrue(); + assertThat(ref2.getTestBean1() != ref2.getTestBean2()).as("Not referencing same bean twice").isTrue(); + assertThat(ref1.getTestBean1() != ref2.getTestBean1()).as("Not referencing same bean twice").isTrue(); + assertThat(ref1.getTestBean2() != ref2.getTestBean2()).as("Not referencing same bean twice").isTrue(); + assertThat(ref1.getTestBean1() != ref2.getTestBean2()).as("Not referencing same bean twice").isTrue(); } @Test @@ -234,24 +235,24 @@ public void beanPostProcessor() { TestBean kathy = (TestBean) getBeanFactory().getBean("kathy"); DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory"); TestBean factoryCreated = (TestBean) getBeanFactory().getBean("singletonFactory"); - assertTrue(kerry.isPostProcessed()); - assertTrue(kathy.isPostProcessed()); - assertTrue(factory.isPostProcessed()); - assertTrue(factoryCreated.isPostProcessed()); + assertThat(kerry.isPostProcessed()).isTrue(); + assertThat(kathy.isPostProcessed()).isTrue(); + assertThat(factory.isPostProcessed()).isTrue(); + assertThat(factoryCreated.isPostProcessed()).isTrue(); } @Test public void emptyValues() { TestBean rod = (TestBean) getBeanFactory().getBean("rod"); TestBean kerry = (TestBean) getBeanFactory().getBean("kerry"); - assertTrue("Touchy is empty", "".equals(rod.getTouchy())); - assertTrue("Touchy is empty", "".equals(kerry.getTouchy())); + assertThat("".equals(rod.getTouchy())).as("Touchy is empty").isTrue(); + assertThat("".equals(kerry.getTouchy())).as("Touchy is empty").isTrue(); } @Test public void commentsAndCdataInValue() { TestBean bean = (TestBean) getBeanFactory().getBean("commentsInValue"); - assertEquals("Failed to handle comments and CDATA properly", "this is a ", bean.getName()); + assertThat(bean.getName()).as("Failed to handle comments and CDATA properly").isEqualTo("this is a "); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/support/DefaultNamespaceHandlerResolverTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/support/DefaultNamespaceHandlerResolverTests.java index b2e9a9c4722f..faa41995e6d5 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/support/DefaultNamespaceHandlerResolverTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/support/DefaultNamespaceHandlerResolverTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,13 +16,14 @@ package org.springframework.beans.factory.xml.support; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver; import org.springframework.beans.factory.xml.NamespaceHandler; import org.springframework.beans.factory.xml.UtilNamespaceHandler; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit and integration tests for the {@link DefaultNamespaceHandlerResolver} class. @@ -36,54 +37,38 @@ public class DefaultNamespaceHandlerResolverTests { public void testResolvedMappedHandler() { DefaultNamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(getClass().getClassLoader()); NamespaceHandler handler = resolver.resolve("http://www.springframework.org/schema/util"); - assertNotNull("Handler should not be null.", handler); - assertEquals("Incorrect handler loaded", UtilNamespaceHandler.class, handler.getClass()); + assertThat(handler).as("Handler should not be null.").isNotNull(); + assertThat(handler.getClass()).as("Incorrect handler loaded").isEqualTo(UtilNamespaceHandler.class); } @Test public void testResolvedMappedHandlerWithNoArgCtor() { DefaultNamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(); NamespaceHandler handler = resolver.resolve("http://www.springframework.org/schema/util"); - assertNotNull("Handler should not be null.", handler); - assertEquals("Incorrect handler loaded", UtilNamespaceHandler.class, handler.getClass()); + assertThat(handler).as("Handler should not be null.").isNotNull(); + assertThat(handler.getClass()).as("Incorrect handler loaded").isEqualTo(UtilNamespaceHandler.class); } @Test - public void testNonExistentHandlerClass() throws Exception { + public void testNonExistentHandlerClass() { String mappingPath = "org/springframework/beans/factory/xml/support/nonExistent.properties"; - try { - new DefaultNamespaceHandlerResolver(getClass().getClassLoader(), mappingPath); - // pass - } - catch (Throwable ex) { - fail("Non-existent handler classes must be ignored: " + ex); - } + new DefaultNamespaceHandlerResolver(getClass().getClassLoader(), mappingPath); } @Test - public void testResolveInvalidHandler() throws Exception { - String mappingPath = "org/springframework/beans/factory/xml/support/invalid.properties"; - try { - new DefaultNamespaceHandlerResolver(getClass().getClassLoader(), mappingPath); - fail("Should not be able to map a class that doesn't implement NamespaceHandler"); - } - catch (Throwable expected) { - } - } - - @Test - public void testCtorWithNullClassLoaderArgument() throws Exception { + public void testCtorWithNullClassLoaderArgument() { // simply must not bail... new DefaultNamespaceHandlerResolver(null); } - @Test(expected = IllegalArgumentException.class) - public void testCtorWithNullClassLoaderArgumentAndNullMappingLocationArgument() throws Exception { - new DefaultNamespaceHandlerResolver(null, null); + @Test + public void testCtorWithNullClassLoaderArgumentAndNullMappingLocationArgument() { + assertThatIllegalArgumentException().isThrownBy(() -> + new DefaultNamespaceHandlerResolver(null, null)); } @Test - public void testCtorWithNonExistentMappingLocationArgument() throws Exception { + public void testCtorWithNonExistentMappingLocationArgument() { // simply must not bail; we don't want non-existent resources to result in an Exception new DefaultNamespaceHandlerResolver(null, "738trbc bobabloobop871"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/BeanInfoTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/BeanInfoTests.java index e96901ad9b45..7b91fc60e64a 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/BeanInfoTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/BeanInfoTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -20,14 +20,14 @@ import java.beans.PropertyDescriptor; import java.beans.SimpleBeanInfo; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.FatalBeanException; import org.springframework.util.Assert; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -39,20 +39,20 @@ public class BeanInfoTests { public void testComplexObject() { ValueBean bean = new ValueBean(); BeanWrapper bw = new BeanWrapperImpl(bean); - Integer value = new Integer(1); + Integer value = 1; bw.setPropertyValue("value", value); - assertEquals("value not set correctly", bean.getValue(), value); + assertThat(value).as("value not set correctly").isEqualTo(bean.getValue()); - value = new Integer(2); + value = 2; bw.setPropertyValue("value", value.toString()); - assertEquals("value not converted", bean.getValue(), value); + assertThat(value).as("value not converted").isEqualTo(bean.getValue()); bw.setPropertyValue("value", null); - assertNull("value not null", bean.getValue()); + assertThat(bean.getValue()).as("value not null").isNull(); bw.setPropertyValue("value", ""); - assertNull("value not converted to null", bean.getValue()); + assertThat(bean.getValue()).as("value not converted to null").isNull(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ByteArrayPropertyEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ByteArrayPropertyEditorTests.java index 29db9143b2d6..0ea1102cdaec 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ByteArrayPropertyEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ByteArrayPropertyEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,9 @@ import java.beans.PropertyEditor; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests for the {@link ByteArrayPropertyEditor} class. @@ -37,21 +37,20 @@ public void sunnyDaySetAsText() throws Exception { byteEditor.setAsText(text); Object value = byteEditor.getValue(); - assertNotNull(value); - assertTrue(value instanceof byte[]); + assertThat(value).isNotNull().isInstanceOf(byte[].class); byte[] bytes = (byte[]) value; for (int i = 0; i < text.length(); ++i) { - assertEquals("cyte[] differs at index '" + i + "'", text.charAt(i), bytes[i]); + assertThat(bytes[i]).as("cyte[] differs at index '" + i + "'").isEqualTo((byte) text.charAt(i)); } - assertEquals(text, byteEditor.getAsText()); + assertThat(byteEditor.getAsText()).isEqualTo(text); } @Test public void getAsTextReturnsEmptyStringIfValueIsNull() throws Exception { - assertEquals("", byteEditor.getAsText()); + assertThat(byteEditor.getAsText()).isEqualTo(""); byteEditor.setAsText(null); - assertEquals("", byteEditor.getAsText()); + assertThat(byteEditor.getAsText()).isEqualTo(""); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CharArrayPropertyEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CharArrayPropertyEditorTests.java index 42f6789ca5da..d01f59100e17 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CharArrayPropertyEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CharArrayPropertyEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,9 @@ import java.beans.PropertyEditor; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests for the {@link CharArrayPropertyEditor} class. @@ -37,21 +37,20 @@ public void sunnyDaySetAsText() throws Exception { charEditor.setAsText(text); Object value = charEditor.getValue(); - assertNotNull(value); - assertTrue(value instanceof char[]); + assertThat(value).isNotNull().isInstanceOf(char[].class); char[] chars = (char[]) value; for (int i = 0; i < text.length(); ++i) { - assertEquals("char[] differs at index '" + i + "'", text.charAt(i), chars[i]); + assertThat(chars[i]).as("char[] differs at index '" + i + "'").isEqualTo(text.charAt(i)); } - assertEquals(text, charEditor.getAsText()); + assertThat(charEditor.getAsText()).isEqualTo(text); } @Test public void getAsTextReturnsEmptyStringIfValueIsNull() throws Exception { - assertEquals("", charEditor.getAsText()); + assertThat(charEditor.getAsText()).isEqualTo(""); charEditor.setAsText(null); - assertEquals("", charEditor.getAsText()); + assertThat(charEditor.getAsText()).isEqualTo(""); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomCollectionEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomCollectionEditorTests.java index 55579c96cf57..ad8d4c761de2 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomCollectionEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomCollectionEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -20,9 +20,10 @@ import java.util.Collection; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for the {@link CustomCollectionEditor} class. @@ -32,21 +33,24 @@ */ public class CustomCollectionEditorTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithNullCollectionType() throws Exception { - new CustomCollectionEditor(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomCollectionEditor(null)); } - @Test(expected = IllegalArgumentException.class) - @SuppressWarnings("unchecked") + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testCtorWithNonCollectionType() throws Exception { - new CustomCollectionEditor((Class) String.class); + assertThatIllegalArgumentException().isThrownBy(() -> + new CustomCollectionEditor((Class) String.class)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWithCollectionTypeThatDoesNotExposeAPublicNoArgCtor() throws Exception { CustomCollectionEditor editor = new CustomCollectionEditor(CollectionTypeWithNoNoArgCtor.class); - editor.setValue("1"); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setValue("1")); } @Test @@ -54,13 +58,14 @@ public void testSunnyDaySetValue() throws Exception { CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class); editor.setValue(new int[] {0, 1, 2}); Object value = editor.getValue(); - assertNotNull(value); - assertTrue(value instanceof ArrayList); + assertThat(value).isNotNull(); + boolean condition = value instanceof ArrayList; + assertThat(condition).isTrue(); List list = (List) value; - assertEquals("There must be 3 elements in the converted collection", 3, list.size()); - assertEquals(new Integer(0), list.get(0)); - assertEquals(new Integer(1), list.get(1)); - assertEquals(new Integer(2), list.get(2)); + assertThat(list.size()).as("There must be 3 elements in the converted collection").isEqualTo(3); + assertThat(list.get(0)).isEqualTo(0); + assertThat(list.get(1)).isEqualTo(1); + assertThat(list.get(2)).isEqualTo(2); } @Test @@ -68,9 +73,9 @@ public void testWhenTargetTypeIsExactlyTheCollectionInterfaceUsesFallbackCollect CustomCollectionEditor editor = new CustomCollectionEditor(Collection.class); editor.setValue("0, 1, 2"); Collection value = (Collection) editor.getValue(); - assertNotNull(value); - assertEquals("There must be 1 element in the converted collection", 1, value.size()); - assertEquals("0, 1, 2", value.iterator().next()); + assertThat(value).isNotNull(); + assertThat(value.size()).as("There must be 1 element in the converted collection").isEqualTo(1); + assertThat(value.iterator().next()).isEqualTo("0, 1, 2"); } @Test @@ -78,11 +83,12 @@ public void testSunnyDaySetAsTextYieldsSingleValue() throws Exception { CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class); editor.setValue("0, 1, 2"); Object value = editor.getValue(); - assertNotNull(value); - assertTrue(value instanceof ArrayList); + assertThat(value).isNotNull(); + boolean condition = value instanceof ArrayList; + assertThat(condition).isTrue(); List list = (List) value; - assertEquals("There must be 1 element in the converted collection", 1, list.size()); - assertEquals("0, 1, 2", list.get(0)); + assertThat(list.size()).as("There must be 1 element in the converted collection").isEqualTo(1); + assertThat(list.get(0)).isEqualTo("0, 1, 2"); } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomEditorTests.java index b49b2e0b8a03..0de98a10f002 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -36,20 +36,23 @@ import java.util.Vector; import java.util.regex.Pattern; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyValue; -import org.springframework.tests.sample.beans.BooleanTestBean; -import org.springframework.tests.sample.beans.ITestBean; -import org.springframework.tests.sample.beans.IndexedTestBean; -import org.springframework.tests.sample.beans.NumberTestBean; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.BooleanTestBean; +import org.springframework.beans.testfixture.beans.ITestBean; +import org.springframework.beans.testfixture.beans.IndexedTestBean; +import org.springframework.beans.testfixture.beans.NumberTestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.within; /** * Unit tests for the various PropertyEditors in Spring. @@ -72,14 +75,13 @@ public void testComplexObject() { BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(ITestBean.class, new TestBeanEditor()); MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.addPropertyValue(new PropertyValue("age", new Integer(55))); + pvs.addPropertyValue(new PropertyValue("age", 55)); pvs.addPropertyValue(new PropertyValue("name", newName)); pvs.addPropertyValue(new PropertyValue("touchy", "valid")); pvs.addPropertyValue(new PropertyValue("spouse", tbString)); bw.setPropertyValues(pvs); - assertTrue("spouse is non-null", tb.getSpouse() != null); - assertTrue("spouse name is Kerry and age is 34", - tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34); + assertThat(tb.getSpouse() != null).as("spouse is non-null").isTrue(); + assertThat(tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34).as("spouse name is Kerry and age is 34").isTrue(); } @Test @@ -92,19 +94,18 @@ public void testComplexObjectWithOldValueAccess() { bw.setExtractOldValueForEditor(true); bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor()); MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.addPropertyValue(new PropertyValue("age", new Integer(55))); + pvs.addPropertyValue(new PropertyValue("age", 55)); pvs.addPropertyValue(new PropertyValue("name", newName)); pvs.addPropertyValue(new PropertyValue("touchy", "valid")); pvs.addPropertyValue(new PropertyValue("spouse", tbString)); bw.setPropertyValues(pvs); - assertTrue("spouse is non-null", tb.getSpouse() != null); - assertTrue("spouse name is Kerry and age is 34", - tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34); + assertThat(tb.getSpouse() != null).as("spouse is non-null").isTrue(); + assertThat(tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34).as("spouse name is Kerry and age is 34").isTrue(); ITestBean spouse = tb.getSpouse(); bw.setPropertyValues(pvs); - assertSame("Should have remained same object", spouse, tb.getSpouse()); + assertThat(tb.getSpouse()).as("Should have remained same object").isSameAs(spouse); } @Test @@ -119,10 +120,10 @@ public void setAsText(String text) throws IllegalArgumentException { }); bw.setPropertyValue("name", "value"); bw.setPropertyValue("touchy", "value"); - assertEquals("prefixvalue", bw.getPropertyValue("name")); - assertEquals("prefixvalue", tb.getName()); - assertEquals("value", bw.getPropertyValue("touchy")); - assertEquals("value", tb.getTouchy()); + assertThat(bw.getPropertyValue("name")).isEqualTo("prefixvalue"); + assertThat(tb.getName()).isEqualTo("prefixvalue"); + assertThat(bw.getPropertyValue("touchy")).isEqualTo("value"); + assertThat(tb.getTouchy()).isEqualTo("value"); } @Test @@ -137,10 +138,10 @@ public void setAsText(String text) throws IllegalArgumentException { }); bw.setPropertyValue("name", "value"); bw.setPropertyValue("touchy", "value"); - assertEquals("prefixvalue", bw.getPropertyValue("name")); - assertEquals("prefixvalue", tb.getName()); - assertEquals("prefixvalue", bw.getPropertyValue("touchy")); - assertEquals("prefixvalue", tb.getTouchy()); + assertThat(bw.getPropertyValue("name")).isEqualTo("prefixvalue"); + assertThat(tb.getName()).isEqualTo("prefixvalue"); + assertThat(bw.getPropertyValue("touchy")).isEqualTo("prefixvalue"); + assertThat(tb.getTouchy()).isEqualTo("prefixvalue"); } @Test @@ -156,10 +157,10 @@ public void setAsText(String text) throws IllegalArgumentException { }); bw.setPropertyValue("spouse.name", "value"); bw.setPropertyValue("touchy", "value"); - assertEquals("prefixvalue", bw.getPropertyValue("spouse.name")); - assertEquals("prefixvalue", tb.getSpouse().getName()); - assertEquals("value", bw.getPropertyValue("touchy")); - assertEquals("value", tb.getTouchy()); + assertThat(bw.getPropertyValue("spouse.name")).isEqualTo("prefixvalue"); + assertThat(tb.getSpouse().getName()).isEqualTo("prefixvalue"); + assertThat(bw.getPropertyValue("touchy")).isEqualTo("value"); + assertThat(tb.getTouchy()).isEqualTo("value"); } @Test @@ -175,10 +176,10 @@ public void setAsText(String text) throws IllegalArgumentException { }); bw.setPropertyValue("spouse.name", "value"); bw.setPropertyValue("touchy", "value"); - assertEquals("prefixvalue", bw.getPropertyValue("spouse.name")); - assertEquals("prefixvalue", tb.getSpouse().getName()); - assertEquals("prefixvalue", bw.getPropertyValue("touchy")); - assertEquals("prefixvalue", tb.getTouchy()); + assertThat(bw.getPropertyValue("spouse.name")).isEqualTo("prefixvalue"); + assertThat(tb.getSpouse().getName()).isEqualTo("prefixvalue"); + assertThat(bw.getPropertyValue("touchy")).isEqualTo("prefixvalue"); + assertThat(tb.getTouchy()).isEqualTo("prefixvalue"); } @Test @@ -187,44 +188,44 @@ public void testDefaultBooleanEditorForPrimitiveType() { BeanWrapper bw = new BeanWrapperImpl(tb); bw.setPropertyValue("bool1", "true"); - assertTrue("Correct bool1 value", Boolean.TRUE.equals(bw.getPropertyValue("bool1"))); - assertTrue("Correct bool1 value", tb.isBool1()); + assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool1"))).as("Correct bool1 value").isTrue(); + assertThat(tb.isBool1()).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", "false"); - assertTrue("Correct bool1 value", Boolean.FALSE.equals(bw.getPropertyValue("bool1"))); - assertTrue("Correct bool1 value", !tb.isBool1()); + assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool1"))).as("Correct bool1 value").isTrue(); + boolean condition4 = !tb.isBool1(); + assertThat(condition4).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", " true "); - assertTrue("Correct bool1 value", tb.isBool1()); + assertThat(tb.isBool1()).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", " false "); - assertTrue("Correct bool1 value", !tb.isBool1()); + boolean condition3 = !tb.isBool1(); + assertThat(condition3).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", "on"); - assertTrue("Correct bool1 value", tb.isBool1()); + assertThat(tb.isBool1()).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", "off"); - assertTrue("Correct bool1 value", !tb.isBool1()); + boolean condition2 = !tb.isBool1(); + assertThat(condition2).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", "yes"); - assertTrue("Correct bool1 value", tb.isBool1()); + assertThat(tb.isBool1()).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", "no"); - assertTrue("Correct bool1 value", !tb.isBool1()); + boolean condition1 = !tb.isBool1(); + assertThat(condition1).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", "1"); - assertTrue("Correct bool1 value", tb.isBool1()); + assertThat(tb.isBool1()).as("Correct bool1 value").isTrue(); bw.setPropertyValue("bool1", "0"); - assertTrue("Correct bool1 value", !tb.isBool1()); + boolean condition = !tb.isBool1(); + assertThat(condition).as("Correct bool1 value").isTrue(); - try { - bw.setPropertyValue("bool1", "argh"); - fail("Should have thrown BeansException"); - } - catch (BeansException ex) { - // expected - } + assertThatExceptionOfType(BeansException.class).isThrownBy(() -> + bw.setPropertyValue("bool1", "argh")); } @Test @@ -233,33 +234,37 @@ public void testDefaultBooleanEditorForWrapperType() { BeanWrapper bw = new BeanWrapperImpl(tb); bw.setPropertyValue("bool2", "true"); - assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2"))); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue(); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "false"); - assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2"))); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue(); + boolean condition3 = !tb.getBool2().booleanValue(); + assertThat(condition3).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "on"); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "off"); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + boolean condition2 = !tb.getBool2().booleanValue(); + assertThat(condition2).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "yes"); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "no"); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + boolean condition1 = !tb.getBool2().booleanValue(); + assertThat(condition1).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "1"); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "0"); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + boolean condition = !tb.getBool2().booleanValue(); + assertThat(condition).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", ""); - assertNull("Correct bool2 value", tb.getBool2()); + assertThat(tb.getBool2()).as("Correct bool2 value").isNull(); } @Test @@ -269,34 +274,38 @@ public void testCustomBooleanEditorWithAllowEmpty() { bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true)); bw.setPropertyValue("bool2", "true"); - assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2"))); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue(); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "false"); - assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2"))); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue(); + boolean condition3 = !tb.getBool2().booleanValue(); + assertThat(condition3).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "on"); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "off"); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + boolean condition2 = !tb.getBool2().booleanValue(); + assertThat(condition2).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "yes"); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "no"); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + boolean condition1 = !tb.getBool2().booleanValue(); + assertThat(condition1).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "1"); - assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); + assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", "0"); - assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); + boolean condition = !tb.getBool2().booleanValue(); + assertThat(condition).as("Correct bool2 value").isTrue(); bw.setPropertyValue("bool2", ""); - assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null); - assertTrue("Correct bool2 value", tb.getBool2() == null); + assertThat(bw.getPropertyValue("bool2") == null).as("Correct bool2 value").isTrue(); + assertThat(tb.getBool2() == null).as("Correct bool2 value").isTrue(); } @Test @@ -307,26 +316,20 @@ public void testCustomBooleanEditorWithSpecialTrueAndFalseStrings() throws Excep CustomBooleanEditor editor = new CustomBooleanEditor(trueString, falseString, false); editor.setAsText(trueString); - assertTrue(((Boolean) editor.getValue()).booleanValue()); - assertEquals(trueString, editor.getAsText()); + assertThat(((Boolean) editor.getValue()).booleanValue()).isTrue(); + assertThat(editor.getAsText()).isEqualTo(trueString); editor.setAsText(falseString); - assertFalse(((Boolean) editor.getValue()).booleanValue()); - assertEquals(falseString, editor.getAsText()); + assertThat(((Boolean) editor.getValue()).booleanValue()).isFalse(); + assertThat(editor.getAsText()).isEqualTo(falseString); editor.setAsText(trueString.toUpperCase()); - assertTrue(((Boolean) editor.getValue()).booleanValue()); - assertEquals(trueString, editor.getAsText()); + assertThat(((Boolean) editor.getValue()).booleanValue()).isTrue(); + assertThat(editor.getAsText()).isEqualTo(trueString); editor.setAsText(falseString.toUpperCase()); - assertFalse(((Boolean) editor.getValue()).booleanValue()); - assertEquals(falseString, editor.getAsText()); - - try { - editor.setAsText(null); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - // expected - } + assertThat(((Boolean) editor.getValue()).booleanValue()).isFalse(); + assertThat(editor.getAsText()).isEqualTo(falseString); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText(null)); } @Test @@ -347,30 +350,30 @@ public void testDefaultNumberEditor() { bw.setPropertyValue("double2", "6.1"); bw.setPropertyValue("bigDecimal", "4.5"); - assertTrue("Correct short1 value", new Short("1").equals(bw.getPropertyValue("short1"))); - assertTrue("Correct short1 value", tb.getShort1() == 1); - assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2"))); - assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2())); - assertTrue("Correct int1 value", new Integer("7").equals(bw.getPropertyValue("int1"))); - assertTrue("Correct int1 value", tb.getInt1() == 7); - assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2"))); - assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2())); - assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1"))); - assertTrue("Correct long1 value", tb.getLong1() == 5); - assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2"))); - assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2())); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger"))); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger())); - assertTrue("Correct float1 value", new Float("7.1").equals(bw.getPropertyValue("float1"))); - assertTrue("Correct float1 value", new Float("7.1").equals(new Float(tb.getFloat1()))); - assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2"))); - assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2())); - assertTrue("Correct double1 value", new Double("5.1").equals(bw.getPropertyValue("double1"))); - assertTrue("Correct double1 value", tb.getDouble1() == 5.1); - assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2"))); - assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2())); - assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal"))); - assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal())); + assertThat(new Short("1").equals(bw.getPropertyValue("short1"))).as("Correct short1 value").isTrue(); + assertThat(tb.getShort1() == 1).as("Correct short1 value").isTrue(); + assertThat(new Short("2").equals(bw.getPropertyValue("short2"))).as("Correct short2 value").isTrue(); + assertThat(new Short("2").equals(tb.getShort2())).as("Correct short2 value").isTrue(); + assertThat(new Integer("7").equals(bw.getPropertyValue("int1"))).as("Correct int1 value").isTrue(); + assertThat(tb.getInt1() == 7).as("Correct int1 value").isTrue(); + assertThat(new Integer("8").equals(bw.getPropertyValue("int2"))).as("Correct int2 value").isTrue(); + assertThat(new Integer("8").equals(tb.getInt2())).as("Correct int2 value").isTrue(); + assertThat(new Long("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue(); + assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue(); + assertThat(new Long("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue(); + assertThat(new Long("6").equals(tb.getLong2())).as("Correct long2 value").isTrue(); + assertThat(new BigInteger("3").equals(bw.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue(); + assertThat(new BigInteger("3").equals(tb.getBigInteger())).as("Correct bigInteger value").isTrue(); + assertThat(new Float("7.1").equals(bw.getPropertyValue("float1"))).as("Correct float1 value").isTrue(); + assertThat(new Float("7.1").equals(new Float(tb.getFloat1()))).as("Correct float1 value").isTrue(); + assertThat(new Float("8.1").equals(bw.getPropertyValue("float2"))).as("Correct float2 value").isTrue(); + assertThat(new Float("8.1").equals(tb.getFloat2())).as("Correct float2 value").isTrue(); + assertThat(new Double("5.1").equals(bw.getPropertyValue("double1"))).as("Correct double1 value").isTrue(); + assertThat(tb.getDouble1() == 5.1).as("Correct double1 value").isTrue(); + assertThat(new Double("6.1").equals(bw.getPropertyValue("double2"))).as("Correct double2 value").isTrue(); + assertThat(new Double("6.1").equals(tb.getDouble2())).as("Correct double2 value").isTrue(); + assertThat(new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal"))).as("Correct bigDecimal value").isTrue(); + assertThat(new BigDecimal("4.5").equals(tb.getBigDecimal())).as("Correct bigDecimal value").isTrue(); } @Test @@ -404,30 +407,30 @@ public void testCustomNumberEditorWithoutAllowEmpty() { bw.setPropertyValue("double2", "6,1"); bw.setPropertyValue("bigDecimal", "4,5"); - assertTrue("Correct short1 value", new Short("1").equals(bw.getPropertyValue("short1"))); - assertTrue("Correct short1 value", tb.getShort1() == 1); - assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2"))); - assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2())); - assertTrue("Correct int1 value", new Integer("7").equals(bw.getPropertyValue("int1"))); - assertTrue("Correct int1 value", tb.getInt1() == 7); - assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2"))); - assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2())); - assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1"))); - assertTrue("Correct long1 value", tb.getLong1() == 5); - assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2"))); - assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2())); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger"))); - assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger())); - assertTrue("Correct float1 value", new Float("7.1").equals(bw.getPropertyValue("float1"))); - assertTrue("Correct float1 value", new Float("7.1").equals(new Float(tb.getFloat1()))); - assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2"))); - assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2())); - assertTrue("Correct double1 value", new Double("5.1").equals(bw.getPropertyValue("double1"))); - assertTrue("Correct double1 value", tb.getDouble1() == 5.1); - assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2"))); - assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2())); - assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal"))); - assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal())); + assertThat(new Short("1").equals(bw.getPropertyValue("short1"))).as("Correct short1 value").isTrue(); + assertThat(tb.getShort1() == 1).as("Correct short1 value").isTrue(); + assertThat(new Short("2").equals(bw.getPropertyValue("short2"))).as("Correct short2 value").isTrue(); + assertThat(new Short("2").equals(tb.getShort2())).as("Correct short2 value").isTrue(); + assertThat(new Integer("7").equals(bw.getPropertyValue("int1"))).as("Correct int1 value").isTrue(); + assertThat(tb.getInt1() == 7).as("Correct int1 value").isTrue(); + assertThat(new Integer("8").equals(bw.getPropertyValue("int2"))).as("Correct int2 value").isTrue(); + assertThat(new Integer("8").equals(tb.getInt2())).as("Correct int2 value").isTrue(); + assertThat(new Long("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue(); + assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue(); + assertThat(new Long("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue(); + assertThat(new Long("6").equals(tb.getLong2())).as("Correct long2 value").isTrue(); + assertThat(new BigInteger("3").equals(bw.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue(); + assertThat(new BigInteger("3").equals(tb.getBigInteger())).as("Correct bigInteger value").isTrue(); + assertThat(new Float("7.1").equals(bw.getPropertyValue("float1"))).as("Correct float1 value").isTrue(); + assertThat(new Float("7.1").equals(new Float(tb.getFloat1()))).as("Correct float1 value").isTrue(); + assertThat(new Float("8.1").equals(bw.getPropertyValue("float2"))).as("Correct float2 value").isTrue(); + assertThat(new Float("8.1").equals(tb.getFloat2())).as("Correct float2 value").isTrue(); + assertThat(new Double("5.1").equals(bw.getPropertyValue("double1"))).as("Correct double1 value").isTrue(); + assertThat(tb.getDouble1() == 5.1).as("Correct double1 value").isTrue(); + assertThat(new Double("6.1").equals(bw.getPropertyValue("double2"))).as("Correct double2 value").isTrue(); + assertThat(new Double("6.1").equals(tb.getDouble2())).as("Correct double2 value").isTrue(); + assertThat(new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal"))).as("Correct bigDecimal value").isTrue(); + assertThat(new BigDecimal("4.5").equals(tb.getBigDecimal())).as("Correct bigDecimal value").isTrue(); } @Test @@ -440,24 +443,18 @@ public void testCustomNumberEditorWithAllowEmpty() { bw.setPropertyValue("long1", "5"); bw.setPropertyValue("long2", "6"); - assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1"))); - assertTrue("Correct long1 value", tb.getLong1() == 5); - assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2"))); - assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2())); + assertThat(new Long("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue(); + assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue(); + assertThat(new Long("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue(); + assertThat(new Long("6").equals(tb.getLong2())).as("Correct long2 value").isTrue(); bw.setPropertyValue("long2", ""); - assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null); - assertTrue("Correct long2 value", tb.getLong2() == null); - - try { - bw.setPropertyValue("long1", ""); - fail("Should have thrown BeansException"); - } - catch (BeansException ex) { - // expected - assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1"))); - assertTrue("Correct long1 value", tb.getLong1() == 5); - } + assertThat(bw.getPropertyValue("long2") == null).as("Correct long2 value").isTrue(); + assertThat(tb.getLong2() == null).as("Correct long2 value").isTrue(); + assertThatExceptionOfType(BeansException.class).isThrownBy(() -> + bw.setPropertyValue("long1", "")); + assertThat(bw.getPropertyValue("long1")).isEqualTo(5L); + assertThat(tb.getLong1()).isEqualTo(5); } @Test @@ -467,23 +464,21 @@ public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception { BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true)); bw.setPropertyValue("bigDecimal", "1000"); - assertEquals(1000.0f, tb.getBigDecimal().floatValue(), 0f); + assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.0f, within(0f)); + bw.setPropertyValue("bigDecimal", "1000,5"); - assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f); + assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.5f, within(0f)); + bw.setPropertyValue("bigDecimal", "1 000,5"); - assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f); + assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.5f, within(0f)); + } @Test public void testParseShortGreaterThanMaxValueWithoutNumberFormat() { - try { - CustomNumberEditor editor = new CustomNumberEditor(Short.class, true); - editor.setAsText(String.valueOf(Short.MAX_VALUE + 1)); - fail(Short.MAX_VALUE + 1 + " is greater than max value"); - } - catch (NumberFormatException ex) { - // expected - } + CustomNumberEditor editor = new CustomNumberEditor(Short.class, true); + assertThatExceptionOfType(NumberFormatException.class).as("greater than Short.MAX_VALUE + 1").isThrownBy(() -> + editor.setAsText(String.valueOf(Short.MAX_VALUE + 1))); } @Test @@ -491,7 +486,7 @@ public void testByteArrayPropertyEditor() { PrimitiveArrayBean bean = new PrimitiveArrayBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.setPropertyValue("byteArray", "myvalue"); - assertEquals("myvalue", new String(bean.getByteArray())); + assertThat(new String(bean.getByteArray())).isEqualTo("myvalue"); } @Test @@ -499,7 +494,7 @@ public void testCharArrayPropertyEditor() { PrimitiveArrayBean bean = new PrimitiveArrayBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.setPropertyValue("charArray", "myvalue"); - assertEquals("myvalue", new String(bean.getCharArray())); + assertThat(new String(bean.getCharArray())).isEqualTo("myvalue"); } @Test @@ -508,20 +503,20 @@ public void testCharacterEditor() { BeanWrapper bw = new BeanWrapperImpl(cb); bw.setPropertyValue("myChar", new Character('c')); - assertEquals('c', cb.getMyChar()); + assertThat(cb.getMyChar()).isEqualTo('c'); bw.setPropertyValue("myChar", "c"); - assertEquals('c', cb.getMyChar()); + assertThat(cb.getMyChar()).isEqualTo('c'); bw.setPropertyValue("myChar", "\u0041"); - assertEquals('A', cb.getMyChar()); + assertThat(cb.getMyChar()).isEqualTo('A'); bw.setPropertyValue("myChar", "\\u0022"); - assertEquals('"', cb.getMyChar()); + assertThat(cb.getMyChar()).isEqualTo('"'); CharacterEditor editor = new CharacterEditor(false); editor.setAsText("M"); - assertEquals("M", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo("M"); } @Test @@ -531,73 +526,76 @@ public void testCharacterEditorWithAllowEmpty() { bw.registerCustomEditor(Character.class, new CharacterEditor(true)); bw.setPropertyValue("myCharacter", new Character('c')); - assertEquals(new Character('c'), cb.getMyCharacter()); + assertThat(cb.getMyCharacter()).isEqualTo(new Character('c')); bw.setPropertyValue("myCharacter", "c"); - assertEquals(new Character('c'), cb.getMyCharacter()); + assertThat(cb.getMyCharacter()).isEqualTo(new Character('c')); bw.setPropertyValue("myCharacter", "\u0041"); - assertEquals(new Character('A'), cb.getMyCharacter()); + assertThat(cb.getMyCharacter()).isEqualTo(new Character('A')); bw.setPropertyValue("myCharacter", " "); - assertEquals(new Character(' '), cb.getMyCharacter()); + assertThat(cb.getMyCharacter()).isEqualTo(new Character(' ')); bw.setPropertyValue("myCharacter", ""); - assertNull(cb.getMyCharacter()); + assertThat(cb.getMyCharacter()).isNull(); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCharacterEditorSetAsTextWithStringLongerThanOneCharacter() throws Exception { PropertyEditor charEditor = new CharacterEditor(false); - charEditor.setAsText("ColdWaterCanyon"); + assertThatIllegalArgumentException().isThrownBy(() -> + charEditor.setAsText("ColdWaterCanyon")); } @Test public void testCharacterEditorGetAsTextReturnsEmptyStringIfValueIsNull() throws Exception { PropertyEditor charEditor = new CharacterEditor(false); - assertEquals("", charEditor.getAsText()); + assertThat(charEditor.getAsText()).isEqualTo(""); charEditor = new CharacterEditor(true); charEditor.setAsText(null); - assertEquals("", charEditor.getAsText()); + assertThat(charEditor.getAsText()).isEqualTo(""); charEditor.setAsText(""); - assertEquals("", charEditor.getAsText()); + assertThat(charEditor.getAsText()).isEqualTo(""); charEditor.setAsText(" "); - assertEquals(" ", charEditor.getAsText()); + assertThat(charEditor.getAsText()).isEqualTo(" "); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCharacterEditorSetAsTextWithNullNotAllowingEmptyAsNull() throws Exception { PropertyEditor charEditor = new CharacterEditor(false); - charEditor.setAsText(null); + assertThatIllegalArgumentException().isThrownBy(() -> + charEditor.setAsText(null)); } @Test public void testClassEditor() { PropertyEditor classEditor = new ClassEditor(); classEditor.setAsText(TestBean.class.getName()); - assertEquals(TestBean.class, classEditor.getValue()); - assertEquals(TestBean.class.getName(), classEditor.getAsText()); + assertThat(classEditor.getValue()).isEqualTo(TestBean.class); + assertThat(classEditor.getAsText()).isEqualTo(TestBean.class.getName()); classEditor.setAsText(null); - assertEquals("", classEditor.getAsText()); + assertThat(classEditor.getAsText()).isEqualTo(""); classEditor.setAsText(""); - assertEquals("", classEditor.getAsText()); + assertThat(classEditor.getAsText()).isEqualTo(""); classEditor.setAsText("\t "); - assertEquals("", classEditor.getAsText()); + assertThat(classEditor.getAsText()).isEqualTo(""); } - @Test(expected = IllegalArgumentException.class) + @Test public void testClassEditorWithNonExistentClass() throws Exception { PropertyEditor classEditor = new ClassEditor(); - classEditor.setAsText("hairdresser.on.Fire"); + assertThatIllegalArgumentException().isThrownBy(() -> + classEditor.setAsText("hairdresser.on.Fire")); } @Test public void testClassEditorWithArray() { PropertyEditor classEditor = new ClassEditor(); - classEditor.setAsText("org.springframework.tests.sample.beans.TestBean[]"); - assertEquals(TestBean[].class, classEditor.getValue()); - assertEquals("org.springframework.tests.sample.beans.TestBean[]", classEditor.getAsText()); + classEditor.setAsText("org.springframework.beans.testfixture.beans.TestBean[]"); + assertThat(classEditor.getValue()).isEqualTo(TestBean[].class); + assertThat(classEditor.getAsText()).isEqualTo("org.springframework.beans.testfixture.beans.TestBean[]"); } /* @@ -608,7 +606,7 @@ public void testGetAsTextWithTwoDimensionalArray() throws Exception { String[][] chessboard = new String[8][8]; ClassEditor editor = new ClassEditor(); editor.setValue(chessboard.getClass()); - assertEquals("java.lang.String[][]", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo("java.lang.String[][]"); } /* @@ -619,15 +617,15 @@ public void testGetAsTextWithRidiculousMultiDimensionalArray() throws Exception String[][][][][] ridiculousChessboard = new String[8][4][0][1][3]; ClassEditor editor = new ClassEditor(); editor.setValue(ridiculousChessboard.getClass()); - assertEquals("java.lang.String[][][][][]", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo("java.lang.String[][][][][]"); } @Test public void testFileEditor() { PropertyEditor fileEditor = new FileEditor(); fileEditor.setAsText("file:myfile.txt"); - assertEquals(new File("myfile.txt"), fileEditor.getValue()); - assertEquals((new File("myfile.txt")).getPath(), fileEditor.getAsText()); + assertThat(fileEditor.getValue()).isEqualTo(new File("myfile.txt")); + assertThat(fileEditor.getAsText()).isEqualTo((new File("myfile.txt")).getPath()); } @Test @@ -648,12 +646,12 @@ public void testFileEditorWithAbsolutePath() { // testing on Windows if (new File("C:/myfile.txt").isAbsolute()) { fileEditor.setAsText("C:/myfile.txt"); - assertEquals(new File("C:/myfile.txt"), fileEditor.getValue()); + assertThat(fileEditor.getValue()).isEqualTo(new File("C:/myfile.txt")); } // testing on Unix if (new File("/myfile.txt").isAbsolute()) { fileEditor.setAsText("/myfile.txt"); - assertEquals(new File("/myfile.txt"), fileEditor.getValue()); + assertThat(fileEditor.getValue()).isEqualTo(new File("/myfile.txt")); } } @@ -661,11 +659,11 @@ public void testFileEditorWithAbsolutePath() { public void testLocaleEditor() { PropertyEditor localeEditor = new LocaleEditor(); localeEditor.setAsText("en_CA"); - assertEquals(Locale.CANADA, localeEditor.getValue()); - assertEquals("en_CA", localeEditor.getAsText()); + assertThat(localeEditor.getValue()).isEqualTo(Locale.CANADA); + assertThat(localeEditor.getAsText()).isEqualTo("en_CA"); localeEditor = new LocaleEditor(); - assertEquals("", localeEditor.getAsText()); + assertThat(localeEditor.getAsText()).isEqualTo(""); } @Test @@ -674,15 +672,15 @@ public void testPatternEditor() { PropertyEditor patternEditor = new PatternEditor(); patternEditor.setAsText(REGEX); - assertEquals(Pattern.compile(REGEX).pattern(), ((Pattern) patternEditor.getValue()).pattern()); - assertEquals(REGEX, patternEditor.getAsText()); + assertThat(((Pattern) patternEditor.getValue()).pattern()).isEqualTo(Pattern.compile(REGEX).pattern()); + assertThat(patternEditor.getAsText()).isEqualTo(REGEX); patternEditor = new PatternEditor(); - assertEquals("", patternEditor.getAsText()); + assertThat(patternEditor.getAsText()).isEqualTo(""); patternEditor = new PatternEditor(); patternEditor.setAsText(null); - assertEquals("", patternEditor.getAsText()); + assertThat(patternEditor.getAsText()).isEqualTo(""); } @Test @@ -690,24 +688,19 @@ public void testCustomBooleanEditor() { CustomBooleanEditor editor = new CustomBooleanEditor(false); editor.setAsText("true"); - assertEquals(Boolean.TRUE, editor.getValue()); - assertEquals("true", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(Boolean.TRUE); + assertThat(editor.getAsText()).isEqualTo("true"); editor.setAsText("false"); - assertEquals(Boolean.FALSE, editor.getValue()); - assertEquals("false", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(Boolean.FALSE); + assertThat(editor.getAsText()).isEqualTo("false"); editor.setValue(null); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); - try { - editor.setAsText(null); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - // expected - } + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText(null)); } @Test @@ -715,32 +708,32 @@ public void testCustomBooleanEditorWithEmptyAsNull() { CustomBooleanEditor editor = new CustomBooleanEditor(true); editor.setAsText("true"); - assertEquals(Boolean.TRUE, editor.getValue()); - assertEquals("true", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(Boolean.TRUE); + assertThat(editor.getAsText()).isEqualTo("true"); editor.setAsText("false"); - assertEquals(Boolean.FALSE, editor.getValue()); - assertEquals("false", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(Boolean.FALSE); + assertThat(editor.getAsText()).isEqualTo("false"); editor.setValue(null); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testCustomDateEditor() { CustomDateEditor editor = new CustomDateEditor(null, false); editor.setValue(null); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testCustomDateEditorWithEmptyAsNull() { CustomDateEditor editor = new CustomDateEditor(null, true); editor.setValue(null); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); } @Test @@ -749,124 +742,112 @@ public void testCustomDateEditorWithExactDateLength() { String validDate = "01/01/2005"; String invalidDate = "01/01/05"; - assertTrue(validDate.length() == maxLength); - assertFalse(invalidDate.length() == maxLength); + assertThat(validDate.length() == maxLength).isTrue(); + assertThat(invalidDate.length() == maxLength).isFalse(); CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true, maxLength); - - try { - editor.setAsText(validDate); - } - catch (IllegalArgumentException ex) { - fail("Exception shouldn't be thrown because this is a valid date"); - } - - try { - editor.setAsText(invalidDate); - fail("Exception should be thrown because this is an invalid date"); - } - catch (IllegalArgumentException ex) { - // expected - assertTrue(ex.getMessage().contains("10")); - } + editor.setAsText(validDate); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText(invalidDate)) + .withMessageContaining("10"); } @Test public void testCustomNumberEditor() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); editor.setAsText("5"); - assertEquals(new Integer(5), editor.getValue()); - assertEquals("5", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(5); + assertThat(editor.getAsText()).isEqualTo("5"); editor.setValue(null); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testCustomNumberEditorWithHex() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); editor.setAsText("0x" + Integer.toHexString(64)); - assertEquals(new Integer(64), editor.getValue()); + assertThat(editor.getValue()).isEqualTo(64); } @Test public void testCustomNumberEditorWithEmptyAsNull() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, true); editor.setAsText("5"); - assertEquals(new Integer(5), editor.getValue()); - assertEquals("5", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(5); + assertThat(editor.getAsText()).isEqualTo("5"); editor.setAsText(""); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); editor.setValue(null); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testStringTrimmerEditor() { StringTrimmerEditor editor = new StringTrimmerEditor(false); editor.setAsText("test"); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(" test "); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(""); - assertEquals("", editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(""); + assertThat(editor.getAsText()).isEqualTo(""); editor.setValue(null); - assertEquals("", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo(""); editor.setAsText(null); - assertEquals("", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testStringTrimmerEditorWithEmptyAsNull() { StringTrimmerEditor editor = new StringTrimmerEditor(true); editor.setAsText("test"); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(" test "); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(" "); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); editor.setValue(null); - assertEquals("", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testStringTrimmerEditorWithCharsToDelete() { StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", false); editor.setAsText("te\ns\ft"); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(" test "); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(""); - assertEquals("", editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(""); + assertThat(editor.getAsText()).isEqualTo(""); editor.setValue(null); - assertEquals("", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testStringTrimmerEditorWithCharsToDeleteAndEmptyAsNull() { StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", true); editor.setAsText("te\ns\ft"); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(" test "); - assertEquals("test", editor.getValue()); - assertEquals("test", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo("test"); + assertThat(editor.getAsText()).isEqualTo("test"); editor.setAsText(" \n\f "); - assertEquals(null, editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isEqualTo(null); + assertThat(editor.getAsText()).isEqualTo(""); editor.setValue(null); - assertEquals("", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo(""); } @Test @@ -885,20 +866,20 @@ public void setAsText(String text) throws IllegalArgumentException { TestBean tb3 = ((TestBean) bean.getList().get(1)); TestBean tb4 = ((TestBean) bean.getMap().get("key1")); TestBean tb5 = ((TestBean) bean.getMap().get("key2")); - assertEquals("name0", tb0.getName()); - assertEquals("name1", tb1.getName()); - assertEquals("name2", tb2.getName()); - assertEquals("name3", tb3.getName()); - assertEquals("name4", tb4.getName()); - assertEquals("name5", tb5.getName()); - assertEquals("name0", bw.getPropertyValue("array[0].name")); - assertEquals("name1", bw.getPropertyValue("array[1].name")); - assertEquals("name2", bw.getPropertyValue("list[0].name")); - assertEquals("name3", bw.getPropertyValue("list[1].name")); - assertEquals("name4", bw.getPropertyValue("map[key1].name")); - assertEquals("name5", bw.getPropertyValue("map[key2].name")); - assertEquals("name4", bw.getPropertyValue("map['key1'].name")); - assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name")); + assertThat(tb0.getName()).isEqualTo("name0"); + assertThat(tb1.getName()).isEqualTo("name1"); + assertThat(tb2.getName()).isEqualTo("name2"); + assertThat(tb3.getName()).isEqualTo("name3"); + assertThat(tb4.getName()).isEqualTo("name4"); + assertThat(tb5.getName()).isEqualTo("name5"); + assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("name0"); + assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("name1"); + assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("name2"); + assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("name3"); + assertThat(bw.getPropertyValue("map[key1].name")).isEqualTo("name4"); + assertThat(bw.getPropertyValue("map[key2].name")).isEqualTo("name5"); + assertThat(bw.getPropertyValue("map['key1'].name")).isEqualTo("name4"); + assertThat(bw.getPropertyValue("map[\"key2\"].name")).isEqualTo("name5"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].name", "name5"); @@ -908,18 +889,18 @@ public void setAsText(String text) throws IllegalArgumentException { pvs.add("map[key1].name", "name1"); pvs.add("map['key2'].name", "name0"); bw.setPropertyValues(pvs); - assertEquals("prefixname5", tb0.getName()); - assertEquals("prefixname4", tb1.getName()); - assertEquals("prefixname3", tb2.getName()); - assertEquals("prefixname2", tb3.getName()); - assertEquals("prefixname1", tb4.getName()); - assertEquals("prefixname0", tb5.getName()); - assertEquals("prefixname5", bw.getPropertyValue("array[0].name")); - assertEquals("prefixname4", bw.getPropertyValue("array[1].name")); - assertEquals("prefixname3", bw.getPropertyValue("list[0].name")); - assertEquals("prefixname2", bw.getPropertyValue("list[1].name")); - assertEquals("prefixname1", bw.getPropertyValue("map[\"key1\"].name")); - assertEquals("prefixname0", bw.getPropertyValue("map['key2'].name")); + assertThat(tb0.getName()).isEqualTo("prefixname5"); + assertThat(tb1.getName()).isEqualTo("prefixname4"); + assertThat(tb2.getName()).isEqualTo("prefixname3"); + assertThat(tb3.getName()).isEqualTo("prefixname2"); + assertThat(tb4.getName()).isEqualTo("prefixname1"); + assertThat(tb5.getName()).isEqualTo("prefixname0"); + assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("prefixname5"); + assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("prefixname4"); + assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("prefixname3"); + assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("prefixname2"); + assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("prefixname1"); + assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("prefixname0"); } @Test @@ -952,20 +933,20 @@ public void setAsText(String text) throws IllegalArgumentException { TestBean tb3 = ((TestBean) bean.getList().get(1)); TestBean tb4 = ((TestBean) bean.getMap().get("key1")); TestBean tb5 = ((TestBean) bean.getMap().get("key2")); - assertEquals("name0", tb0.getName()); - assertEquals("name1", tb1.getName()); - assertEquals("name2", tb2.getName()); - assertEquals("name3", tb3.getName()); - assertEquals("name4", tb4.getName()); - assertEquals("name5", tb5.getName()); - assertEquals("name0", bw.getPropertyValue("array[0].name")); - assertEquals("name1", bw.getPropertyValue("array[1].name")); - assertEquals("name2", bw.getPropertyValue("list[0].name")); - assertEquals("name3", bw.getPropertyValue("list[1].name")); - assertEquals("name4", bw.getPropertyValue("map[key1].name")); - assertEquals("name5", bw.getPropertyValue("map[key2].name")); - assertEquals("name4", bw.getPropertyValue("map['key1'].name")); - assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name")); + assertThat(tb0.getName()).isEqualTo("name0"); + assertThat(tb1.getName()).isEqualTo("name1"); + assertThat(tb2.getName()).isEqualTo("name2"); + assertThat(tb3.getName()).isEqualTo("name3"); + assertThat(tb4.getName()).isEqualTo("name4"); + assertThat(tb5.getName()).isEqualTo("name5"); + assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("name0"); + assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("name1"); + assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("name2"); + assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("name3"); + assertThat(bw.getPropertyValue("map[key1].name")).isEqualTo("name4"); + assertThat(bw.getPropertyValue("map[key2].name")).isEqualTo("name5"); + assertThat(bw.getPropertyValue("map['key1'].name")).isEqualTo("name4"); + assertThat(bw.getPropertyValue("map[\"key2\"].name")).isEqualTo("name5"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].name", "name5"); @@ -975,18 +956,18 @@ public void setAsText(String text) throws IllegalArgumentException { pvs.add("map[key1].name", "name1"); pvs.add("map['key2'].name", "name0"); bw.setPropertyValues(pvs); - assertEquals("arrayname5", tb0.getName()); - assertEquals("arrayname4", tb1.getName()); - assertEquals("listname3", tb2.getName()); - assertEquals("listname2", tb3.getName()); - assertEquals("mapname1", tb4.getName()); - assertEquals("mapname0", tb5.getName()); - assertEquals("arrayname5", bw.getPropertyValue("array[0].name")); - assertEquals("arrayname4", bw.getPropertyValue("array[1].name")); - assertEquals("listname3", bw.getPropertyValue("list[0].name")); - assertEquals("listname2", bw.getPropertyValue("list[1].name")); - assertEquals("mapname1", bw.getPropertyValue("map[\"key1\"].name")); - assertEquals("mapname0", bw.getPropertyValue("map['key2'].name")); + assertThat(tb0.getName()).isEqualTo("arrayname5"); + assertThat(tb1.getName()).isEqualTo("arrayname4"); + assertThat(tb2.getName()).isEqualTo("listname3"); + assertThat(tb3.getName()).isEqualTo("listname2"); + assertThat(tb4.getName()).isEqualTo("mapname1"); + assertThat(tb5.getName()).isEqualTo("mapname0"); + assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("arrayname5"); + assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("arrayname4"); + assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("listname3"); + assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("listname2"); + assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapname1"); + assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapname0"); } @Test @@ -1037,20 +1018,20 @@ public void setAsText(String text) throws IllegalArgumentException { TestBean tb3 = ((TestBean) bean.getList().get(1)); TestBean tb4 = ((TestBean) bean.getMap().get("key1")); TestBean tb5 = ((TestBean) bean.getMap().get("key2")); - assertEquals("name0", tb0.getName()); - assertEquals("name1", tb1.getName()); - assertEquals("name2", tb2.getName()); - assertEquals("name3", tb3.getName()); - assertEquals("name4", tb4.getName()); - assertEquals("name5", tb5.getName()); - assertEquals("name0", bw.getPropertyValue("array[0].name")); - assertEquals("name1", bw.getPropertyValue("array[1].name")); - assertEquals("name2", bw.getPropertyValue("list[0].name")); - assertEquals("name3", bw.getPropertyValue("list[1].name")); - assertEquals("name4", bw.getPropertyValue("map[key1].name")); - assertEquals("name5", bw.getPropertyValue("map[key2].name")); - assertEquals("name4", bw.getPropertyValue("map['key1'].name")); - assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name")); + assertThat(tb0.getName()).isEqualTo("name0"); + assertThat(tb1.getName()).isEqualTo("name1"); + assertThat(tb2.getName()).isEqualTo("name2"); + assertThat(tb3.getName()).isEqualTo("name3"); + assertThat(tb4.getName()).isEqualTo("name4"); + assertThat(tb5.getName()).isEqualTo("name5"); + assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("name0"); + assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("name1"); + assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("name2"); + assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("name3"); + assertThat(bw.getPropertyValue("map[key1].name")).isEqualTo("name4"); + assertThat(bw.getPropertyValue("map[key2].name")).isEqualTo("name5"); + assertThat(bw.getPropertyValue("map['key1'].name")).isEqualTo("name4"); + assertThat(bw.getPropertyValue("map[\"key2\"].name")).isEqualTo("name5"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].name", "name5"); @@ -1060,18 +1041,18 @@ public void setAsText(String text) throws IllegalArgumentException { pvs.add("map[key1].name", "name1"); pvs.add("map['key2'].name", "name0"); bw.setPropertyValues(pvs); - assertEquals("array0name5", tb0.getName()); - assertEquals("array1name4", tb1.getName()); - assertEquals("list0name3", tb2.getName()); - assertEquals("list1name2", tb3.getName()); - assertEquals("mapkey1name1", tb4.getName()); - assertEquals("mapkey2name0", tb5.getName()); - assertEquals("array0name5", bw.getPropertyValue("array[0].name")); - assertEquals("array1name4", bw.getPropertyValue("array[1].name")); - assertEquals("list0name3", bw.getPropertyValue("list[0].name")); - assertEquals("list1name2", bw.getPropertyValue("list[1].name")); - assertEquals("mapkey1name1", bw.getPropertyValue("map[\"key1\"].name")); - assertEquals("mapkey2name0", bw.getPropertyValue("map['key2'].name")); + assertThat(tb0.getName()).isEqualTo("array0name5"); + assertThat(tb1.getName()).isEqualTo("array1name4"); + assertThat(tb2.getName()).isEqualTo("list0name3"); + assertThat(tb3.getName()).isEqualTo("list1name2"); + assertThat(tb4.getName()).isEqualTo("mapkey1name1"); + assertThat(tb5.getName()).isEqualTo("mapkey2name0"); + assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("array0name5"); + assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("array1name4"); + assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("list0name3"); + assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("list1name2"); + assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapkey1name1"); + assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapkey2name0"); } @Test @@ -1123,18 +1104,18 @@ public String getAsText() { return ((String) getValue()).substring(4); } }); - assertEquals("name0", tb0.getName()); - assertEquals("name1", tb1.getName()); - assertEquals("name2", tb2.getName()); - assertEquals("name3", tb3.getName()); - assertEquals("name4", tb4.getName()); - assertEquals("name5", tb5.getName()); - assertEquals("name0", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")); - assertEquals("name1", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")); - assertEquals("name2", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")); - assertEquals("name3", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")); - assertEquals("name4", bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name")); - assertEquals("name5", bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name")); + assertThat(tb0.getName()).isEqualTo("name0"); + assertThat(tb1.getName()).isEqualTo("name1"); + assertThat(tb2.getName()).isEqualTo("name2"); + assertThat(tb3.getName()).isEqualTo("name3"); + assertThat(tb4.getName()).isEqualTo("name4"); + assertThat(tb5.getName()).isEqualTo("name5"); + assertThat(bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")).isEqualTo("name0"); + assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("name1"); + assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("name2"); + assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("name3"); + assertThat(bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name")).isEqualTo("name4"); + assertThat(bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name")).isEqualTo("name5"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].nestedIndexedBean.array[0].name", "name5"); @@ -1144,18 +1125,18 @@ public String getAsText() { pvs.add("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1"); pvs.add("map['key2'].nestedIndexedBean.map[key2].name", "name0"); bw.setPropertyValues(pvs); - assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName()); - assertEquals("arrayname4", tb1.getNestedIndexedBean().getArray()[1].getName()); - assertEquals("listname3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()); - assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName()); - assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName()); - assertEquals("mapname0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()); - assertEquals("arrayname5", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")); - assertEquals("arrayname4", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")); - assertEquals("listname3", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")); - assertEquals("listname2", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")); - assertEquals("mapname1", bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name")); - assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name")); + assertThat(tb0.getNestedIndexedBean().getArray()[0].getName()).isEqualTo("arrayname5"); + assertThat(tb1.getNestedIndexedBean().getArray()[1].getName()).isEqualTo("arrayname4"); + assertThat(((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()).isEqualTo("listname3"); + assertThat(((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName()).isEqualTo("listname2"); + assertThat(((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName()).isEqualTo("mapname1"); + assertThat(((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()).isEqualTo("mapname0"); + assertThat(bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")).isEqualTo("arrayname5"); + assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("arrayname4"); + assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("listname3"); + assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("listname2"); + assertThat(bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name")).isEqualTo("mapname1"); + assertThat(bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name")).isEqualTo("mapname0"); } @Test @@ -1201,12 +1182,12 @@ public void setAsText(String text) throws IllegalArgumentException { pvs.add("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1"); pvs.add("map['key2'].nestedIndexedBean.map[key2].name", "name0"); bw.setPropertyValues(pvs); - assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName()); - assertEquals("name4", tb1.getNestedIndexedBean().getArray()[1].getName()); - assertEquals("name3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()); - assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName()); - assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName()); - assertEquals("name0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()); + assertThat(tb0.getNestedIndexedBean().getArray()[0].getName()).isEqualTo("arrayname5"); + assertThat(tb1.getNestedIndexedBean().getArray()[1].getName()).isEqualTo("name4"); + assertThat(((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()).isEqualTo("name3"); + assertThat(((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName()).isEqualTo("listname2"); + assertThat(((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName()).isEqualTo("mapname1"); + assertThat(((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()).isEqualTo("name0"); } @Test @@ -1255,12 +1236,12 @@ public String getAsText() { pvs.add("map[key1]", "e"); pvs.add("map['key2']", "f"); bw.setPropertyValues(pvs); - assertEquals("arraya", bean.getArray()[0].getName()); - assertEquals("arrayb", bean.getArray()[1].getName()); - assertEquals("listc", ((TestBean) bean.getList().get(0)).getName()); - assertEquals("listd", ((TestBean) bean.getList().get(1)).getName()); - assertEquals("mape", ((TestBean) bean.getMap().get("key1")).getName()); - assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName()); + assertThat(bean.getArray()[0].getName()).isEqualTo("arraya"); + assertThat(bean.getArray()[1].getName()).isEqualTo("arrayb"); + assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("listc"); + assertThat(((TestBean) bean.getList().get(1)).getName()).isEqualTo("listd"); + assertThat(((TestBean) bean.getMap().get("key1")).getName()).isEqualTo("mape"); + assertThat(((TestBean) bean.getMap().get("key2")).getName()).isEqualTo("mapf"); } @Test @@ -1342,12 +1323,12 @@ public String getAsText() { pvs.add("map[key1]", "e"); pvs.add("map['key2']", "f"); bw.setPropertyValues(pvs); - assertEquals("array0a", bean.getArray()[0].getName()); - assertEquals("array1b", bean.getArray()[1].getName()); - assertEquals("list0c", ((TestBean) bean.getList().get(0)).getName()); - assertEquals("list1d", ((TestBean) bean.getList().get(1)).getName()); - assertEquals("mapkey1e", ((TestBean) bean.getMap().get("key1")).getName()); - assertEquals("mapkey2f", ((TestBean) bean.getMap().get("key2")).getName()); + assertThat(bean.getArray()[0].getName()).isEqualTo("array0a"); + assertThat(bean.getArray()[1].getName()).isEqualTo("array1b"); + assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("list0c"); + assertThat(((TestBean) bean.getList().get(1)).getName()).isEqualTo("list1d"); + assertThat(((TestBean) bean.getMap().get("key1")).getName()).isEqualTo("mapkey1e"); + assertThat(((TestBean) bean.getMap().get("key2")).getName()).isEqualTo("mapkey2f"); } @Test @@ -1363,9 +1344,9 @@ public void setAsText(String text) throws IllegalArgumentException { } }); bw.setPropertyValue("list", "1"); - assertEquals("list1", ((TestBean) bean.getList().get(0)).getName()); + assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("list1"); bw.setPropertyValue("list[0]", "test"); - assertEquals("test", bean.getList().get(0)); + assertThat(bean.getList().get(0)).isEqualTo("test"); } @Test @@ -1376,13 +1357,13 @@ public void testConversionToOldCollections() throws PropertyVetoException { bw.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class)); bw.setPropertyValue("vector", new String[] {"a", "b"}); - assertEquals(2, tb.getVector().size()); - assertEquals("a", tb.getVector().get(0)); - assertEquals("b", tb.getVector().get(1)); + assertThat(tb.getVector().size()).isEqualTo(2); + assertThat(tb.getVector().get(0)).isEqualTo("a"); + assertThat(tb.getVector().get(1)).isEqualTo("b"); bw.setPropertyValue("hashtable", Collections.singletonMap("foo", "bar")); - assertEquals(1, tb.getHashtable().size()); - assertEquals("bar", tb.getHashtable().get("foo")); + assertThat(tb.getHashtable().size()).isEqualTo(1); + assertThat(tb.getHashtable().get("foo")).isEqualTo("bar"); } @Test @@ -1394,11 +1375,11 @@ public void testUninitializedArrayPropertyWithCustomEditor() { TestBean tb = new TestBean(); bw.setPropertyValue("list", new ArrayList<>()); bw.setPropertyValue("list[0]", tb); - assertEquals(tb, bean.getList().get(0)); - assertEquals(pe, bw.findCustomEditor(int.class, "list.age")); - assertEquals(pe, bw.findCustomEditor(null, "list.age")); - assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age")); - assertEquals(pe, bw.findCustomEditor(null, "list[0].age")); + assertThat(bean.getList().get(0)).isEqualTo(tb); + assertThat(bw.findCustomEditor(int.class, "list.age")).isEqualTo(pe); + assertThat(bw.findCustomEditor(null, "list.age")).isEqualTo(pe); + assertThat(bw.findCustomEditor(int.class, "list[0].age")).isEqualTo(pe); + assertThat(bw.findCustomEditor(null, "list[0].age")).isEqualTo(pe); } @Test @@ -1412,9 +1393,9 @@ public void setAsText(String text) throws IllegalArgumentException { } }); bw.setPropertyValue("array", new String[] {"a", "b"}); - assertEquals(2, tb.getArray().length); - assertEquals("a", tb.getArray()[0].getName()); - assertEquals("b", tb.getArray()[1].getName()); + assertThat(tb.getArray().length).isEqualTo(2); + assertThat(tb.getArray()[0].getName()).isEqualTo("a"); + assertThat(tb.getArray()[1].getName()).isEqualTo("b"); } @Test @@ -1428,7 +1409,7 @@ public void setAsText(String text) throws IllegalArgumentException { } }); bw.setPropertyValue("name", new String[] {"a", "b"}); - assertEquals("-a,b-", tb.getName()); + assertThat(tb.getName()).isEqualTo("-a,b-"); } @Test @@ -1436,10 +1417,10 @@ public void testClassArrayEditorSunnyDay() throws Exception { ClassArrayEditor classArrayEditor = new ClassArrayEditor(); classArrayEditor.setAsText("java.lang.String,java.util.HashMap"); Class[] classes = (Class[]) classArrayEditor.getValue(); - assertEquals(2, classes.length); - assertEquals(String.class, classes[0]); - assertEquals(HashMap.class, classes[1]); - assertEquals("java.lang.String,java.util.HashMap", classArrayEditor.getAsText()); + assertThat(classes.length).isEqualTo(2); + assertThat(classes[0]).isEqualTo(String.class); + assertThat(classes[1]).isEqualTo(HashMap.class); + assertThat(classArrayEditor.getAsText()).isEqualTo("java.lang.String,java.util.HashMap"); // ensure setAsText can consume the return value of getAsText classArrayEditor.setAsText(classArrayEditor.getAsText()); } @@ -1449,12 +1430,12 @@ public void testClassArrayEditorSunnyDayWithArrayTypes() throws Exception { ClassArrayEditor classArrayEditor = new ClassArrayEditor(); classArrayEditor.setAsText("java.lang.String[],java.util.Map[],int[],float[][][]"); Class[] classes = (Class[]) classArrayEditor.getValue(); - assertEquals(4, classes.length); - assertEquals(String[].class, classes[0]); - assertEquals(Map[].class, classes[1]); - assertEquals(int[].class, classes[2]); - assertEquals(float[][][].class, classes[3]); - assertEquals("java.lang.String[],java.util.Map[],int[],float[][][]", classArrayEditor.getAsText()); + assertThat(classes.length).isEqualTo(4); + assertThat(classes[0]).isEqualTo(String[].class); + assertThat(classes[1]).isEqualTo(Map[].class); + assertThat(classes[2]).isEqualTo(int[].class); + assertThat(classes[3]).isEqualTo(float[][][].class); + assertThat(classArrayEditor.getAsText()).isEqualTo("java.lang.String[],java.util.Map[],int[],float[][][]"); // ensure setAsText can consume the return value of getAsText classArrayEditor.setAsText(classArrayEditor.getAsText()); } @@ -1463,24 +1444,24 @@ public void testClassArrayEditorSunnyDayWithArrayTypes() throws Exception { public void testClassArrayEditorSetAsTextWithNull() throws Exception { ClassArrayEditor editor = new ClassArrayEditor(); editor.setAsText(null); - assertNull(editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isNull(); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testClassArrayEditorSetAsTextWithEmptyString() throws Exception { ClassArrayEditor editor = new ClassArrayEditor(); editor.setAsText(""); - assertNull(editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isNull(); + assertThat(editor.getAsText()).isEqualTo(""); } @Test public void testClassArrayEditorSetAsTextWithWhitespaceString() throws Exception { ClassArrayEditor editor = new ClassArrayEditor(); editor.setAsText("\n"); - assertNull(editor.getValue()); - assertEquals("", editor.getAsText()); + assertThat(editor.getValue()).isNull(); + assertThat(editor.getAsText()).isEqualTo(""); } @Test @@ -1489,9 +1470,9 @@ public void testCharsetEditor() throws Exception { String name = "UTF-8"; editor.setAsText(name); Charset charset = Charset.forName(name); - assertEquals("Invalid Charset conversion", charset, editor.getValue()); + assertThat(editor.getValue()).as("Invalid Charset conversion").isEqualTo(charset); editor.setValue(charset); - assertEquals("Invalid Charset conversion", name, editor.getAsText()); + assertThat(editor.getAsText()).as("Invalid Charset conversion").isEqualTo(name); } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/FileEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/FileEditorTests.java index ab76a6498f06..ec5cc0f469d8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/FileEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/FileEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,11 +19,12 @@ import java.beans.PropertyEditor; import java.io.File; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Thomas Risberg @@ -38,15 +39,17 @@ public void testClasspathFileName() throws Exception { fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"); Object value = fileEditor.getValue(); - assertTrue(value instanceof File); + boolean condition = value instanceof File; + assertThat(condition).isTrue(); File file = (File) value; - assertTrue(file.exists()); + assertThat(file.exists()).isTrue(); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWithNonExistentResource() throws Exception { PropertyEditor propertyEditor = new FileEditor(); - propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc"); + assertThatIllegalArgumentException().isThrownBy(() -> + propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc")); } @Test @@ -54,9 +57,11 @@ public void testWithNonExistentFile() throws Exception { PropertyEditor fileEditor = new FileEditor(); fileEditor.setAsText("file:no_way_this_file_is_found.doc"); Object value = fileEditor.getValue(); - assertTrue(value instanceof File); + boolean condition1 = value instanceof File; + assertThat(condition1).isTrue(); File file = (File) value; - assertTrue(!file.exists()); + boolean condition = !file.exists(); + assertThat(condition).isTrue(); } @Test @@ -64,9 +69,11 @@ public void testAbsoluteFileName() throws Exception { PropertyEditor fileEditor = new FileEditor(); fileEditor.setAsText("/no_way_this_file_is_found.doc"); Object value = fileEditor.getValue(); - assertTrue(value instanceof File); + boolean condition1 = value instanceof File; + assertThat(condition1).isTrue(); File file = (File) value; - assertTrue(!file.exists()); + boolean condition = !file.exists(); + assertThat(condition).isTrue(); } @Test @@ -76,11 +83,12 @@ public void testUnqualifiedFileNameFound() throws Exception { ClassUtils.getShortName(getClass()) + ".class"; fileEditor.setAsText(fileName); Object value = fileEditor.getValue(); - assertTrue(value instanceof File); + boolean condition = value instanceof File; + assertThat(condition).isTrue(); File file = (File) value; - assertTrue(file.exists()); + assertThat(file.exists()).isTrue(); String absolutePath = file.getAbsolutePath().replace('\\', '/'); - assertTrue(absolutePath.endsWith(fileName)); + assertThat(absolutePath.endsWith(fileName)).isTrue(); } @Test @@ -90,11 +98,12 @@ public void testUnqualifiedFileNameNotFound() throws Exception { ClassUtils.getShortName(getClass()) + ".clazz"; fileEditor.setAsText(fileName); Object value = fileEditor.getValue(); - assertTrue(value instanceof File); + boolean condition = value instanceof File; + assertThat(condition).isTrue(); File file = (File) value; - assertFalse(file.exists()); + assertThat(file.exists()).isFalse(); String absolutePath = file.getAbsolutePath().replace('\\', '/'); - assertTrue(absolutePath.endsWith(fileName)); + assertThat(absolutePath.endsWith(fileName)).isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/InputStreamEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/InputStreamEditorTests.java index 6e90e92a53fd..e0fb3aabce6f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/InputStreamEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/InputStreamEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,11 +18,12 @@ import java.io.InputStream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for the {@link InputStreamEditor} class. @@ -32,9 +33,10 @@ */ public class InputStreamEditorTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithNullResourceEditor() throws Exception { - new InputStreamEditor(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new InputStreamEditor(null)); } @Test @@ -46,10 +48,11 @@ public void testSunnyDay() throws Exception { InputStreamEditor editor = new InputStreamEditor(); editor.setAsText(resource); Object value = editor.getValue(); - assertNotNull(value); - assertTrue(value instanceof InputStream); + assertThat(value).isNotNull(); + boolean condition = value instanceof InputStream; + assertThat(condition).isTrue(); stream = (InputStream) value; - assertTrue(stream.available() > 0); + assertThat(stream.available() > 0).isTrue(); } finally { if (stream != null) { @@ -58,20 +61,21 @@ public void testSunnyDay() throws Exception { } } - @Test(expected = IllegalArgumentException.class) + @Test public void testWhenResourceDoesNotExist() throws Exception { InputStreamEditor editor = new InputStreamEditor(); - editor.setAsText("classpath:bingo!"); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText("classpath:bingo!")); } @Test public void testGetAsTextReturnsNullByDefault() throws Exception { - assertNull(new InputStreamEditor().getAsText()); + assertThat(new InputStreamEditor().getAsText()).isNull(); String resource = "classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"; InputStreamEditor editor = new InputStreamEditor(); editor.setAsText(resource); - assertNull(editor.getAsText()); + assertThat(editor.getAsText()).isNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java index e96bd94a8495..f0c659bcbdb7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -20,11 +20,12 @@ import java.io.File; import java.nio.file.Path; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Juergen Hoeller @@ -33,76 +34,104 @@ public class PathEditorTests { @Test - public void testClasspathPathName() throws Exception { + public void testClasspathPathName() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"); Object value = pathEditor.getValue(); - assertTrue(value instanceof Path); + assertThat(value instanceof Path).isTrue(); Path path = (Path) value; - assertTrue(path.toFile().exists()); + assertThat(path.toFile().exists()).isTrue(); } - @Test(expected = IllegalArgumentException.class) - public void testWithNonExistentResource() throws Exception { + @Test + public void testWithNonExistentResource() { PropertyEditor propertyEditor = new PathEditor(); - propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc"); + assertThatIllegalArgumentException().isThrownBy(() -> + propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc")); } @Test - public void testWithNonExistentPath() throws Exception { + public void testWithNonExistentPath() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("file:/no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); - assertTrue(value instanceof Path); + assertThat(value instanceof Path).isTrue(); Path path = (Path) value; - assertTrue(!path.toFile().exists()); + assertThat(!path.toFile().exists()).isTrue(); } @Test - public void testAbsolutePath() throws Exception { + public void testAbsolutePath() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("/no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); - assertTrue(value instanceof Path); + assertThat(value instanceof Path).isTrue(); + Path path = (Path) value; + assertThat(!path.toFile().exists()).isTrue(); + } + + @Test + public void testWindowsAbsolutePath() { + PropertyEditor pathEditor = new PathEditor(); + pathEditor.setAsText("C:\\no_way_this_file_is_found.doc"); + Object value = pathEditor.getValue(); + assertThat(value instanceof Path).isTrue(); Path path = (Path) value; - assertTrue(!path.toFile().exists()); + assertThat(!path.toFile().exists()).isTrue(); + } + + @Test + public void testWindowsAbsoluteFilePath() { + PropertyEditor pathEditor = new PathEditor(); + try { + pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc"); + Object value = pathEditor.getValue(); + assertThat(value instanceof Path).isTrue(); + Path path = (Path) value; + assertThat(!path.toFile().exists()).isTrue(); + } + catch (IllegalArgumentException ex) { + if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore + throw ex; + } + } } @Test - public void testUnqualifiedPathNameFound() throws Exception { + public void testUnqualifiedPathNameFound() { PropertyEditor pathEditor = new PathEditor(); String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"; pathEditor.setAsText(fileName); Object value = pathEditor.getValue(); - assertTrue(value instanceof Path); + assertThat(value instanceof Path).isTrue(); Path path = (Path) value; File file = path.toFile(); - assertTrue(file.exists()); + assertThat(file.exists()).isTrue(); String absolutePath = file.getAbsolutePath(); if (File.separatorChar == '\\') { absolutePath = absolutePath.replace('\\', '/'); } - assertTrue(absolutePath.endsWith(fileName)); + assertThat(absolutePath.endsWith(fileName)).isTrue(); } @Test - public void testUnqualifiedPathNameNotFound() throws Exception { + public void testUnqualifiedPathNameNotFound() { PropertyEditor pathEditor = new PathEditor(); String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".clazz"; pathEditor.setAsText(fileName); Object value = pathEditor.getValue(); - assertTrue(value instanceof Path); + assertThat(value instanceof Path).isTrue(); Path path = (Path) value; File file = path.toFile(); - assertFalse(file.exists()); + assertThat(file.exists()).isFalse(); String absolutePath = file.getAbsolutePath(); if (File.separatorChar == '\\') { absolutePath = absolutePath.replace('\\', '/'); } - assertTrue(absolutePath.endsWith(fileName)); + assertThat(absolutePath.endsWith(fileName)).isTrue(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PropertiesEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PropertiesEditorTests.java index 31e88a9cc3a4..32fb123680ba 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PropertiesEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PropertiesEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,9 +20,9 @@ import java.util.Map; import java.util.Properties; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Test the conversion of Strings to {@link java.util.Properties} objects, @@ -40,8 +40,8 @@ public void oneProperty() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(s); Properties p = (Properties) pe.getValue(); - assertTrue("contains one entry", p.entrySet().size() == 1); - assertTrue("foo=bar", p.get("foo").equals("bar")); + assertThat(p.entrySet().size() == 1).as("contains one entry").isTrue(); + assertThat(p.get("foo").equals("bar")).as("foo=bar").isTrue(); } @Test @@ -51,9 +51,9 @@ public void twoProperties() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(s); Properties p = (Properties) pe.getValue(); - assertTrue("contains two entries", p.entrySet().size() == 2); - assertTrue("foo=bar with whitespace", p.get("foo").equals("bar with whitespace")); - assertTrue("me=mi", p.get("me").equals("mi")); + assertThat(p.entrySet().size() == 2).as("contains two entries").isTrue(); + assertThat(p.get("foo").equals("bar with whitespace")).as("foo=bar with whitespace").isTrue(); + assertThat(p.get("me").equals("mi")).as("me=mi").isTrue(); } @Test @@ -64,10 +64,10 @@ public void handlesEqualsInValue() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(s); Properties p = (Properties) pe.getValue(); - assertTrue("contains two entries", p.entrySet().size() == 3); - assertTrue("foo=bar", p.get("foo").equals("bar")); - assertTrue("me=mi", p.get("me").equals("mi")); - assertTrue("x='y=z'", p.get("x").equals("y=z")); + assertThat(p.entrySet().size() == 3).as("contains two entries").isTrue(); + assertThat(p.get("foo").equals("bar")).as("foo=bar").isTrue(); + assertThat(p.get("me").equals("mi")).as("me=mi").isTrue(); + assertThat(p.get("x").equals("y=z")).as("x='y=z'").isTrue(); } @Test @@ -76,10 +76,10 @@ public void handlesEmptyProperty() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(s); Properties p = (Properties) pe.getValue(); - assertTrue("contains two entries", p.entrySet().size() == 3); - assertTrue("foo=bar", p.get("foo").equals("bar")); - assertTrue("me=mi", p.get("me").equals("mi")); - assertTrue("x='y=z'", p.get("x").equals("")); + assertThat(p.entrySet().size() == 3).as("contains two entries").isTrue(); + assertThat(p.get("foo").equals("bar")).as("foo=bar").isTrue(); + assertThat(p.get("me").equals("mi")).as("me=mi").isTrue(); + assertThat(p.get("x").equals("")).as("x='y=z'").isTrue(); } @Test @@ -88,9 +88,9 @@ public void handlesEmptyPropertyWithoutEquals() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(s); Properties p = (Properties) pe.getValue(); - assertTrue("contains three entries", p.entrySet().size() == 3); - assertTrue("foo is empty", p.get("foo").equals("")); - assertTrue("me=mi", p.get("me").equals("mi")); + assertThat(p.entrySet().size() == 3).as("contains three entries").isTrue(); + assertThat(p.get("foo").equals("")).as("foo is empty").isTrue(); + assertThat(p.get("me").equals("mi")).as("me=mi").isTrue(); } /** @@ -107,9 +107,9 @@ public void ignoresCommentLinesAndEmptyLines() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(s); Properties p = (Properties) pe.getValue(); - assertTrue("contains three entries", p.entrySet().size() == 3); - assertTrue("foo is bar", p.get("foo").equals("bar")); - assertTrue("me=mi", p.get("me").equals("mi")); + assertThat(p.entrySet().size() == 3).as("contains three entries").isTrue(); + assertThat(p.get("foo").equals("bar")).as("foo is bar").isTrue(); + assertThat(p.get("me").equals("mi")).as("me=mi").isTrue(); } /** @@ -129,9 +129,9 @@ public void ignoresLeadingSpacesAndTabs() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(s); Properties p = (Properties) pe.getValue(); - assertTrue("contains 3 entries, not " + p.size(), p.size() == 3); - assertTrue("foo is bar", p.get("foo").equals("bar")); - assertTrue("me=mi", p.get("me").equals("mi")); + assertThat(p.size() == 3).as("contains 3 entries, not " + p.size()).isTrue(); + assertThat(p.get("foo").equals("bar")).as("foo is bar").isTrue(); + assertThat(p.get("me").equals("mi")).as("me=mi").isTrue(); } @Test @@ -139,7 +139,7 @@ public void nullValue() { PropertiesEditor pe= new PropertiesEditor(); pe.setAsText(null); Properties p = (Properties) pe.getValue(); - assertEquals(0, p.size()); + assertThat(p.size()).isEqualTo(0); } @Test @@ -147,7 +147,7 @@ public void emptyString() { PropertiesEditor pe = new PropertiesEditor(); pe.setAsText(""); Properties p = (Properties) pe.getValue(); - assertTrue("empty string means empty properties", p.isEmpty()); + assertThat(p.isEmpty()).as("empty string means empty properties").isTrue(); } @Test @@ -159,13 +159,14 @@ public void usingMapAsValueSource() throws Exception { PropertiesEditor pe = new PropertiesEditor(); pe.setValue(map); Object value = pe.getValue(); - assertNotNull(value); - assertTrue(value instanceof Properties); + assertThat(value).isNotNull(); + boolean condition = value instanceof Properties; + assertThat(condition).isTrue(); Properties props = (Properties) value; - assertEquals(3, props.size()); - assertEquals("1", props.getProperty("one")); - assertEquals("2", props.getProperty("two")); - assertEquals("3", props.getProperty("three")); + assertThat(props.size()).isEqualTo(3); + assertThat(props.getProperty("one")).isEqualTo("1"); + assertThat(props.getProperty("two")).isEqualTo("2"); + assertThat(props.getProperty("three")).isEqualTo("3"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ReaderEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ReaderEditorTests.java index d4e19b8bae86..55734526206d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ReaderEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ReaderEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,11 +18,12 @@ import java.io.Reader; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for the {@link ReaderEditor} class. @@ -32,9 +33,10 @@ */ public class ReaderEditorTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithNullResourceEditor() throws Exception { - new ReaderEditor(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new ReaderEditor(null)); } @Test @@ -46,10 +48,11 @@ public void testSunnyDay() throws Exception { ReaderEditor editor = new ReaderEditor(); editor.setAsText(resource); Object value = editor.getValue(); - assertNotNull(value); - assertTrue(value instanceof Reader); + assertThat(value).isNotNull(); + boolean condition = value instanceof Reader; + assertThat(condition).isTrue(); reader = (Reader) value; - assertTrue(reader.ready()); + assertThat(reader.ready()).isTrue(); } finally { if (reader != null) { @@ -58,21 +61,22 @@ public void testSunnyDay() throws Exception { } } - @Test(expected = IllegalArgumentException.class) + @Test public void testWhenResourceDoesNotExist() throws Exception { String resource = "classpath:bingo!"; ReaderEditor editor = new ReaderEditor(); - editor.setAsText(resource); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText(resource)); } @Test public void testGetAsTextReturnsNullByDefault() throws Exception { - assertNull(new ReaderEditor().getAsText()); + assertThat(new ReaderEditor().getAsText()).isNull(); String resource = "classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"; ReaderEditor editor = new ReaderEditor(); editor.setAsText(resource); - assertNull(editor.getAsText()); + assertThat(editor.getAsText()).isNull(); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ResourceBundleEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ResourceBundleEditorTests.java index 613959dc3c8c..3785304164a2 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ResourceBundleEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ResourceBundleEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,10 @@ import java.util.ResourceBundle; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for the {@link ResourceBundleEditor} class. @@ -40,12 +41,12 @@ public void testSetAsTextWithJustBaseName() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME); Object value = editor.getValue(); - assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); - assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", - value instanceof ResourceBundle); + assertThat(value).as("Returned ResourceBundle was null (must not be for valid setAsText(..) call).").isNotNull(); + boolean condition = value instanceof ResourceBundle; + assertThat(condition).as("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).").isTrue(); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); - assertEquals(MESSAGE_KEY, string); + assertThat(string).isEqualTo(MESSAGE_KEY); } @Test @@ -53,12 +54,12 @@ public void testSetAsTextWithBaseNameThatEndsInDefaultSeparator() throws Excepti ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "_"); Object value = editor.getValue(); - assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); - assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", - value instanceof ResourceBundle); + assertThat(value).as("Returned ResourceBundle was null (must not be for valid setAsText(..) call).").isNotNull(); + boolean condition = value instanceof ResourceBundle; + assertThat(condition).as("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).").isTrue(); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); - assertEquals(MESSAGE_KEY, string); + assertThat(string).isEqualTo(MESSAGE_KEY); } @Test @@ -66,12 +67,12 @@ public void testSetAsTextWithBaseNameAndLanguageCode() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "Lang" + "_en"); Object value = editor.getValue(); - assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); - assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", - value instanceof ResourceBundle); + assertThat(value).as("Returned ResourceBundle was null (must not be for valid setAsText(..) call).").isNotNull(); + boolean condition = value instanceof ResourceBundle; + assertThat(condition).as("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).").isTrue(); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); - assertEquals("yob", string); + assertThat(string).isEqualTo("yob"); } @Test @@ -79,12 +80,12 @@ public void testSetAsTextWithBaseNameLanguageAndCountryCode() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "LangCountry" + "_en_GB"); Object value = editor.getValue(); - assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); - assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", - value instanceof ResourceBundle); + assertThat(value).as("Returned ResourceBundle was null (must not be for valid setAsText(..) call).").isNotNull(); + boolean condition = value instanceof ResourceBundle; + assertThat(condition).as("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).").isTrue(); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); - assertEquals("chav", string); + assertThat(string).isEqualTo("chav"); } @Test @@ -92,36 +93,40 @@ public void testSetAsTextWithTheKitchenSink() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "LangCountryDialect" + "_en_GB_GLASGOW"); Object value = editor.getValue(); - assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); - assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", - value instanceof ResourceBundle); + assertThat(value).as("Returned ResourceBundle was null (must not be for valid setAsText(..) call).").isNotNull(); + boolean condition = value instanceof ResourceBundle; + assertThat(condition).as("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).").isTrue(); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); - assertEquals("ned", string); + assertThat(string).isEqualTo("ned"); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetAsTextWithNull() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); - editor.setAsText(null); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetAsTextWithEmptyString() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); - editor.setAsText(""); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText("")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetAsTextWithWhiteSpaceString() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); - editor.setAsText(" "); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText(" ")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testSetAsTextWithJustSeparatorString() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); - editor.setAsText("_"); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText("_")); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java index a720f6ca2876..0583c78460b7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,95 +16,88 @@ package org.springframework.beans.propertyeditors; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rick Evans * @author Juergen Hoeller + * @author Sam Brannen */ -public class StringArrayPropertyEditorTests { +class StringArrayPropertyEditorTests { @Test - public void withDefaultSeparator() throws Exception { + void withDefaultSeparator() { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(); editor.setAsText("0,1,2"); Object value = editor.getValue(); - assertNotNull(value); - assertTrue(value instanceof String[]); - String[] array = (String[]) value; - for (int i = 0; i < array.length; ++i) { - assertEquals("" + i, array[i]); - } - assertEquals("0,1,2", editor.getAsText()); + assertTrimmedElements(value); + assertThat(editor.getAsText()).isEqualTo("0,1,2"); } @Test - public void trimByDefault() throws Exception { + void trimByDefault() { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(); editor.setAsText(" 0,1 , 2 "); Object value = editor.getValue(); - String[] array = (String[]) value; - for (int i = 0; i < array.length; ++i) { - assertEquals("" + i, array[i]); - } - assertEquals("0,1,2", editor.getAsText()); + assertTrimmedElements(value); + assertThat(editor.getAsText()).isEqualTo("0,1,2"); } @Test - public void noTrim() throws Exception { - StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",",false,false); + void noTrim() { + StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", false, false); editor.setAsText(" 0,1 , 2 "); Object value = editor.getValue(); String[] array = (String[]) value; for (int i = 0; i < array.length; ++i) { - assertEquals(3, array[i].length()); - assertEquals("" + i, array[i].trim()); + assertThat(array[i].length()).isEqualTo(3); + assertThat(array[i].trim()).isEqualTo(("" + i)); } - assertEquals(" 0,1 , 2 ", editor.getAsText()); + assertThat(editor.getAsText()).isEqualTo(" 0,1 , 2 "); } @Test - public void withCustomSeparator() throws Exception { + void withCustomSeparator() { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(":"); editor.setAsText("0:1:2"); Object value = editor.getValue(); - assertTrue(value instanceof String[]); - String[] array = (String[]) value; - for (int i = 0; i < array.length; ++i) { - assertEquals("" + i, array[i]); - } - assertEquals("0:1:2", editor.getAsText()); + assertTrimmedElements(value); + assertThat(editor.getAsText()).isEqualTo("0:1:2"); } @Test - public void withCharsToDelete() throws Exception { + void withCharsToDelete() { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", "\r\n", false); editor.setAsText("0\r,1,\n2"); Object value = editor.getValue(); - assertTrue(value instanceof String[]); - String[] array = (String[]) value; - for (int i = 0; i < array.length; ++i) { - assertEquals("" + i, array[i]); - } - assertEquals("0,1,2", editor.getAsText()); + assertTrimmedElements(value); + assertThat(editor.getAsText()).isEqualTo("0,1,2"); } @Test - public void withEmptyArray() throws Exception { + void withEmptyArray() { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(); editor.setAsText(""); Object value = editor.getValue(); - assertTrue(value instanceof String[]); - assertEquals(0, ((String[]) value).length); + assertThat(value).isInstanceOf(String[].class); + assertThat((String[]) value).isEmpty(); } @Test - public void withEmptyArrayAsNull() throws Exception { + void withEmptyArrayAsNull() { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", true); editor.setAsText(""); - assertNull(editor.getValue()); + assertThat(editor.getValue()).isNull(); + } + + private static void assertTrimmedElements(Object value) { + assertThat(value).isInstanceOf(String[].class); + String[] array = (String[]) value; + for (int i = 0; i < array.length; ++i) { + assertThat(array[i]).isEqualTo(("" + i)); + } } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java index 4eb6245dd4c6..e9896ed50ab6 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,11 +19,11 @@ import java.beans.PropertyEditor; import java.net.URI; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -43,22 +43,23 @@ public void withNonExistentResource() throws Exception { @Test public void standardURL() throws Exception { - doTestURI("http://www.springframework.org"); + doTestURI("https://www.springframework.org"); } @Test public void standardURLWithFragment() throws Exception { - doTestURI("http://www.springframework.org#1"); + doTestURI("https://www.springframework.org#1"); } @Test public void standardURLWithWhitespace() throws Exception { PropertyEditor uriEditor = new URIEditor(); - uriEditor.setAsText(" http://www.springframework.org "); + uriEditor.setAsText(" https://www.springframework.org "); Object value = uriEditor.getValue(); - assertTrue(value instanceof URI); + boolean condition = value instanceof URI; + assertThat(condition).isTrue(); URI uri = (URI) value; - assertEquals("http://www.springframework.org", uri.toString()); + assertThat(uri.toString()).isEqualTo("https://www.springframework.org"); } @Test @@ -67,10 +68,12 @@ public void classpathURL() throws Exception { uriEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"); Object value = uriEditor.getValue(); - assertTrue(value instanceof URI); + boolean condition1 = value instanceof URI; + assertThat(condition1).isTrue(); URI uri = (URI) value; - assertEquals(uri.toString(), uriEditor.getAsText()); - assertTrue(!uri.getScheme().startsWith("classpath")); + assertThat(uriEditor.getAsText()).isEqualTo(uri.toString()); + boolean condition = !uri.getScheme().startsWith("classpath"); + assertThat(condition).isTrue(); } @Test @@ -79,10 +82,12 @@ public void classpathURLWithWhitespace() throws Exception { uriEditor.setAsText(" classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class "); Object value = uriEditor.getValue(); - assertTrue(value instanceof URI); + boolean condition1 = value instanceof URI; + assertThat(condition1).isTrue(); URI uri = (URI) value; - assertEquals(uri.toString(), uriEditor.getAsText()); - assertTrue(!uri.getScheme().startsWith("classpath")); + assertThat(uriEditor.getAsText()).isEqualTo(uri.toString()); + boolean condition = !uri.getScheme().startsWith("classpath"); + assertThat(condition).isTrue(); } @Test @@ -90,46 +95,49 @@ public void classpathURLAsIs() throws Exception { PropertyEditor uriEditor = new URIEditor(); uriEditor.setAsText("classpath:test.txt"); Object value = uriEditor.getValue(); - assertTrue(value instanceof URI); + boolean condition = value instanceof URI; + assertThat(condition).isTrue(); URI uri = (URI) value; - assertEquals(uri.toString(), uriEditor.getAsText()); - assertTrue(uri.getScheme().startsWith("classpath")); + assertThat(uriEditor.getAsText()).isEqualTo(uri.toString()); + assertThat(uri.getScheme().startsWith("classpath")).isTrue(); } @Test public void setAsTextWithNull() throws Exception { PropertyEditor uriEditor = new URIEditor(); uriEditor.setAsText(null); - assertNull(uriEditor.getValue()); - assertEquals("", uriEditor.getAsText()); + assertThat(uriEditor.getValue()).isNull(); + assertThat(uriEditor.getAsText()).isEqualTo(""); } @Test public void getAsTextReturnsEmptyStringIfValueNotSet() throws Exception { PropertyEditor uriEditor = new URIEditor(); - assertEquals("", uriEditor.getAsText()); + assertThat(uriEditor.getAsText()).isEqualTo(""); } @Test public void encodeURI() throws Exception { PropertyEditor uriEditor = new URIEditor(); - uriEditor.setAsText("http://example.com/spaces and \u20AC"); + uriEditor.setAsText("https://example.com/spaces and \u20AC"); Object value = uriEditor.getValue(); - assertTrue(value instanceof URI); + boolean condition = value instanceof URI; + assertThat(condition).isTrue(); URI uri = (URI) value; - assertEquals(uri.toString(), uriEditor.getAsText()); - assertEquals("http://example.com/spaces%20and%20%E2%82%AC", uri.toASCIIString()); + assertThat(uriEditor.getAsText()).isEqualTo(uri.toString()); + assertThat(uri.toASCIIString()).isEqualTo("https://example.com/spaces%20and%20%E2%82%AC"); } @Test public void encodeAlreadyEncodedURI() throws Exception { PropertyEditor uriEditor = new URIEditor(false); - uriEditor.setAsText("http://example.com/spaces%20and%20%E2%82%AC"); + uriEditor.setAsText("https://example.com/spaces%20and%20%E2%82%AC"); Object value = uriEditor.getValue(); - assertTrue(value instanceof URI); + boolean condition = value instanceof URI; + assertThat(condition).isTrue(); URI uri = (URI) value; - assertEquals(uri.toString(), uriEditor.getAsText()); - assertEquals("http://example.com/spaces%20and%20%E2%82%AC", uri.toASCIIString()); + assertThat(uriEditor.getAsText()).isEqualTo(uri.toString()); + assertThat(uri.toASCIIString()).isEqualTo("https://example.com/spaces%20and%20%E2%82%AC"); } @@ -137,9 +145,10 @@ private void doTestURI(String uriSpec) { PropertyEditor uriEditor = new URIEditor(); uriEditor.setAsText(uriSpec); Object value = uriEditor.getValue(); - assertTrue(value instanceof URI); + boolean condition = value instanceof URI; + assertThat(condition).isTrue(); URI uri = (URI) value; - assertEquals(uriSpec, uri.toString()); + assertThat(uri.toString()).isEqualTo(uriSpec); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URLEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URLEditorTests.java index 3259ec47d66a..2d6c4b0d6a1a 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URLEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/URLEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,11 +19,12 @@ import java.beans.PropertyEditor; import java.net.URL; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.ClassUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Rick Evans @@ -31,9 +32,10 @@ */ public class URLEditorTests { - @Test(expected = IllegalArgumentException.class) + @Test public void testCtorWithNullResourceEditor() throws Exception { - new URLEditor(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new URLEditor(null)); } @Test @@ -41,19 +43,21 @@ public void testStandardURI() throws Exception { PropertyEditor urlEditor = new URLEditor(); urlEditor.setAsText("mailto:juergen.hoeller@interface21.com"); Object value = urlEditor.getValue(); - assertTrue(value instanceof URL); + boolean condition = value instanceof URL; + assertThat(condition).isTrue(); URL url = (URL) value; - assertEquals(url.toExternalForm(), urlEditor.getAsText()); + assertThat(urlEditor.getAsText()).isEqualTo(url.toExternalForm()); } @Test public void testStandardURL() throws Exception { PropertyEditor urlEditor = new URLEditor(); - urlEditor.setAsText("http://www.springframework.org"); + urlEditor.setAsText("https://www.springframework.org"); Object value = urlEditor.getValue(); - assertTrue(value instanceof URL); + boolean condition = value instanceof URL; + assertThat(condition).isTrue(); URL url = (URL) value; - assertEquals(url.toExternalForm(), urlEditor.getAsText()); + assertThat(urlEditor.getAsText()).isEqualTo(url.toExternalForm()); } @Test @@ -62,30 +66,33 @@ public void testClasspathURL() throws Exception { urlEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"); Object value = urlEditor.getValue(); - assertTrue(value instanceof URL); + boolean condition1 = value instanceof URL; + assertThat(condition1).isTrue(); URL url = (URL) value; - assertEquals(url.toExternalForm(), urlEditor.getAsText()); - assertTrue(!url.getProtocol().startsWith("classpath")); + assertThat(urlEditor.getAsText()).isEqualTo(url.toExternalForm()); + boolean condition = !url.getProtocol().startsWith("classpath"); + assertThat(condition).isTrue(); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWithNonExistentResource() throws Exception { PropertyEditor urlEditor = new URLEditor(); - urlEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening"); + assertThatIllegalArgumentException().isThrownBy(() -> + urlEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening")); } @Test public void testSetAsTextWithNull() throws Exception { PropertyEditor urlEditor = new URLEditor(); urlEditor.setAsText(null); - assertNull(urlEditor.getValue()); - assertEquals("", urlEditor.getAsText()); + assertThat(urlEditor.getValue()).isNull(); + assertThat(urlEditor.getAsText()).isEqualTo(""); } @Test public void testGetAsTextReturnsEmptyStringIfValueNotSet() throws Exception { PropertyEditor urlEditor = new URLEditor(); - assertEquals("", urlEditor.getAsText()); + assertThat(urlEditor.getAsText()).isEqualTo(""); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java index f234eea4b94b..7a9162314b2f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,9 @@ import java.time.ZoneId; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Nicholas Williams @@ -34,10 +34,10 @@ public void americaChicago() { editor.setAsText("America/Chicago"); ZoneId zoneId = (ZoneId) editor.getValue(); - assertNotNull("The zone ID should not be null.", zoneId); - assertEquals("The zone ID is not correct.", ZoneId.of("America/Chicago"), zoneId); + assertThat(zoneId).as("The zone ID should not be null.").isNotNull(); + assertThat(zoneId).as("The zone ID is not correct.").isEqualTo(ZoneId.of("America/Chicago")); - assertEquals("The text version is not correct.", "America/Chicago", editor.getAsText()); + assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/Chicago"); } @Test @@ -45,21 +45,21 @@ public void americaLosAngeles() { editor.setAsText("America/Los_Angeles"); ZoneId zoneId = (ZoneId) editor.getValue(); - assertNotNull("The zone ID should not be null.", zoneId); - assertEquals("The zone ID is not correct.", ZoneId.of("America/Los_Angeles"), zoneId); + assertThat(zoneId).as("The zone ID should not be null.").isNotNull(); + assertThat(zoneId).as("The zone ID is not correct.").isEqualTo(ZoneId.of("America/Los_Angeles")); - assertEquals("The text version is not correct.", "America/Los_Angeles", editor.getAsText()); + assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/Los_Angeles"); } @Test public void getNullAsText() { - assertEquals("The returned value is not correct.", "", editor.getAsText()); + assertThat(editor.getAsText()).as("The returned value is not correct.").isEqualTo(""); } @Test public void getValueAsText() { editor.setValue(ZoneId.of("America/New_York")); - assertEquals("The text version is not correct.", "America/New_York", editor.getAsText()); + assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York"); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java b/spring-beans/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java index 9e0e5af062fe..ab95a47a1239 100644 --- a/spring-beans/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/java/org/springframework/beans/support/PagedListHolderTests.java b/spring-beans/src/test/java/org/springframework/beans/support/PagedListHolderTests.java index df307be06c97..0afce6483444 100644 --- a/spring-beans/src/test/java/org/springframework/beans/support/PagedListHolderTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/support/PagedListHolderTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,13 +19,11 @@ import java.util.ArrayList; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller @@ -36,9 +34,8 @@ public class PagedListHolderTests { @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) public void testPagedListHolder() { - Assume.group(TestGroup.LONG_RUNNING); - TestBean tb1 = new TestBean(); tb1.setName("eva"); tb1.setAge(25); @@ -54,114 +51,114 @@ public void testPagedListHolder() { tbs.add(tb3); PagedListHolder holder = new PagedListHolder(tbs); - assertTrue("Correct source", holder.getSource() == tbs); - assertTrue("Correct number of elements", holder.getNrOfElements() == 3); - assertTrue("Correct number of pages", holder.getPageCount() == 1); - assertTrue("Correct page size", holder.getPageSize() == PagedListHolder.DEFAULT_PAGE_SIZE); - assertTrue("Correct page number", holder.getPage() == 0); - assertTrue("First page", holder.isFirstPage()); - assertTrue("Last page", holder.isLastPage()); - assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); - assertTrue("Correct first element", holder.getLastElementOnPage() == 2); - assertTrue("Correct page list size", holder.getPageList().size() == 3); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); - assertTrue("Correct page list contents", holder.getPageList().get(1) == tb2); - assertTrue("Correct page list contents", holder.getPageList().get(2) == tb3); + assertThat(holder.getSource() == tbs).as("Correct source").isTrue(); + assertThat(holder.getNrOfElements() == 3).as("Correct number of elements").isTrue(); + assertThat(holder.getPageCount() == 1).as("Correct number of pages").isTrue(); + assertThat(holder.getPageSize() == PagedListHolder.DEFAULT_PAGE_SIZE).as("Correct page size").isTrue(); + assertThat(holder.getPage() == 0).as("Correct page number").isTrue(); + assertThat(holder.isFirstPage()).as("First page").isTrue(); + assertThat(holder.isLastPage()).as("Last page").isTrue(); + assertThat(holder.getFirstElementOnPage() == 0).as("Correct first element").isTrue(); + assertThat(holder.getLastElementOnPage() == 2).as("Correct first element").isTrue(); + assertThat(holder.getPageList().size() == 3).as("Correct page list size").isTrue(); + assertThat(holder.getPageList().get(0) == tb1).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(1) == tb2).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(2) == tb3).as("Correct page list contents").isTrue(); holder.setPageSize(2); - assertTrue("Correct number of pages", holder.getPageCount() == 2); - assertTrue("Correct page size", holder.getPageSize() == 2); - assertTrue("Correct page number", holder.getPage() == 0); - assertTrue("First page", holder.isFirstPage()); - assertFalse("Last page", holder.isLastPage()); - assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); - assertTrue("Correct last element", holder.getLastElementOnPage() == 1); - assertTrue("Correct page list size", holder.getPageList().size() == 2); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); - assertTrue("Correct page list contents", holder.getPageList().get(1) == tb2); + assertThat(holder.getPageCount() == 2).as("Correct number of pages").isTrue(); + assertThat(holder.getPageSize() == 2).as("Correct page size").isTrue(); + assertThat(holder.getPage() == 0).as("Correct page number").isTrue(); + assertThat(holder.isFirstPage()).as("First page").isTrue(); + assertThat(holder.isLastPage()).as("Last page").isFalse(); + assertThat(holder.getFirstElementOnPage() == 0).as("Correct first element").isTrue(); + assertThat(holder.getLastElementOnPage() == 1).as("Correct last element").isTrue(); + assertThat(holder.getPageList().size() == 2).as("Correct page list size").isTrue(); + assertThat(holder.getPageList().get(0) == tb1).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(1) == tb2).as("Correct page list contents").isTrue(); holder.setPage(1); - assertTrue("Correct page number", holder.getPage() == 1); - assertFalse("First page", holder.isFirstPage()); - assertTrue("Last page", holder.isLastPage()); - assertTrue("Correct first element", holder.getFirstElementOnPage() == 2); - assertTrue("Correct last element", holder.getLastElementOnPage() == 2); - assertTrue("Correct page list size", holder.getPageList().size() == 1); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3); + assertThat(holder.getPage() == 1).as("Correct page number").isTrue(); + assertThat(holder.isFirstPage()).as("First page").isFalse(); + assertThat(holder.isLastPage()).as("Last page").isTrue(); + assertThat(holder.getFirstElementOnPage() == 2).as("Correct first element").isTrue(); + assertThat(holder.getLastElementOnPage() == 2).as("Correct last element").isTrue(); + assertThat(holder.getPageList().size() == 1).as("Correct page list size").isTrue(); + assertThat(holder.getPageList().get(0) == tb3).as("Correct page list contents").isTrue(); holder.setPageSize(3); - assertTrue("Correct number of pages", holder.getPageCount() == 1); - assertTrue("Correct page size", holder.getPageSize() == 3); - assertTrue("Correct page number", holder.getPage() == 0); - assertTrue("First page", holder.isFirstPage()); - assertTrue("Last page", holder.isLastPage()); - assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); - assertTrue("Correct last element", holder.getLastElementOnPage() == 2); + assertThat(holder.getPageCount() == 1).as("Correct number of pages").isTrue(); + assertThat(holder.getPageSize() == 3).as("Correct page size").isTrue(); + assertThat(holder.getPage() == 0).as("Correct page number").isTrue(); + assertThat(holder.isFirstPage()).as("First page").isTrue(); + assertThat(holder.isLastPage()).as("Last page").isTrue(); + assertThat(holder.getFirstElementOnPage() == 0).as("Correct first element").isTrue(); + assertThat(holder.getLastElementOnPage() == 2).as("Correct last element").isTrue(); holder.setPage(1); holder.setPageSize(2); - assertTrue("Correct number of pages", holder.getPageCount() == 2); - assertTrue("Correct page size", holder.getPageSize() == 2); - assertTrue("Correct page number", holder.getPage() == 1); - assertFalse("First page", holder.isFirstPage()); - assertTrue("Last page", holder.isLastPage()); - assertTrue("Correct first element", holder.getFirstElementOnPage() == 2); - assertTrue("Correct last element", holder.getLastElementOnPage() == 2); + assertThat(holder.getPageCount() == 2).as("Correct number of pages").isTrue(); + assertThat(holder.getPageSize() == 2).as("Correct page size").isTrue(); + assertThat(holder.getPage() == 1).as("Correct page number").isTrue(); + assertThat(holder.isFirstPage()).as("First page").isFalse(); + assertThat(holder.isLastPage()).as("Last page").isTrue(); + assertThat(holder.getFirstElementOnPage() == 2).as("Correct first element").isTrue(); + assertThat(holder.getLastElementOnPage() == 2).as("Correct last element").isTrue(); holder.setPageSize(2); holder.setPage(1); ((MutableSortDefinition) holder.getSort()).setProperty("name"); ((MutableSortDefinition) holder.getSort()).setIgnoreCase(false); holder.resort(); - assertTrue("Correct source", holder.getSource() == tbs); - assertTrue("Correct number of elements", holder.getNrOfElements() == 3); - assertTrue("Correct number of pages", holder.getPageCount() == 2); - assertTrue("Correct page size", holder.getPageSize() == 2); - assertTrue("Correct page number", holder.getPage() == 0); - assertTrue("First page", holder.isFirstPage()); - assertFalse("Last page", holder.isLastPage()); - assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); - assertTrue("Correct last element", holder.getLastElementOnPage() == 1); - assertTrue("Correct page list size", holder.getPageList().size() == 2); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3); - assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1); + assertThat(holder.getSource() == tbs).as("Correct source").isTrue(); + assertThat(holder.getNrOfElements() == 3).as("Correct number of elements").isTrue(); + assertThat(holder.getPageCount() == 2).as("Correct number of pages").isTrue(); + assertThat(holder.getPageSize() == 2).as("Correct page size").isTrue(); + assertThat(holder.getPage() == 0).as("Correct page number").isTrue(); + assertThat(holder.isFirstPage()).as("First page").isTrue(); + assertThat(holder.isLastPage()).as("Last page").isFalse(); + assertThat(holder.getFirstElementOnPage() == 0).as("Correct first element").isTrue(); + assertThat(holder.getLastElementOnPage() == 1).as("Correct last element").isTrue(); + assertThat(holder.getPageList().size() == 2).as("Correct page list size").isTrue(); + assertThat(holder.getPageList().get(0) == tb3).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(1) == tb1).as("Correct page list contents").isTrue(); ((MutableSortDefinition) holder.getSort()).setProperty("name"); holder.resort(); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb2); - assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1); + assertThat(holder.getPageList().get(0) == tb2).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(1) == tb1).as("Correct page list contents").isTrue(); ((MutableSortDefinition) holder.getSort()).setProperty("name"); holder.resort(); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3); - assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1); + assertThat(holder.getPageList().get(0) == tb3).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(1) == tb1).as("Correct page list contents").isTrue(); holder.setPage(1); - assertTrue("Correct page list size", holder.getPageList().size() == 1); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb2); + assertThat(holder.getPageList().size() == 1).as("Correct page list size").isTrue(); + assertThat(holder.getPageList().get(0) == tb2).as("Correct page list contents").isTrue(); ((MutableSortDefinition) holder.getSort()).setProperty("age"); holder.resort(); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); - assertTrue("Correct page list contents", holder.getPageList().get(1) == tb3); + assertThat(holder.getPageList().get(0) == tb1).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(1) == tb3).as("Correct page list contents").isTrue(); ((MutableSortDefinition) holder.getSort()).setIgnoreCase(true); holder.resort(); - assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); - assertTrue("Correct page list contents", holder.getPageList().get(1) == tb3); + assertThat(holder.getPageList().get(0) == tb1).as("Correct page list contents").isTrue(); + assertThat(holder.getPageList().get(1) == tb3).as("Correct page list contents").isTrue(); holder.nextPage(); - assertEquals(1, holder.getPage()); + assertThat(holder.getPage()).isEqualTo(1); holder.previousPage(); - assertEquals(0, holder.getPage()); + assertThat(holder.getPage()).isEqualTo(0); holder.nextPage(); - assertEquals(1, holder.getPage()); + assertThat(holder.getPage()).isEqualTo(1); holder.nextPage(); - assertEquals(1, holder.getPage()); + assertThat(holder.getPage()).isEqualTo(1); holder.previousPage(); - assertEquals(0, holder.getPage()); + assertThat(holder.getPage()).isEqualTo(0); holder.previousPage(); - assertEquals(0, holder.getPage()); + assertThat(holder.getPage()).isEqualTo(0); } diff --git a/spring-beans/src/test/java/org/springframework/beans/support/PropertyComparatorTests.java b/spring-beans/src/test/java/org/springframework/beans/support/PropertyComparatorTests.java index b1bffd966551..3ca5a5d85049 100644 --- a/spring-beans/src/test/java/org/springframework/beans/support/PropertyComparatorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/support/PropertyComparatorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,9 @@ import java.util.Comparator; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests for {@link PropertyComparator}. @@ -39,9 +39,9 @@ public void testPropertyComparator() { dog2.setNickName("biscy"); PropertyComparator c = new PropertyComparator<>("nickName", false, true); - assertTrue(c.compare(dog, dog2) > 0); - assertTrue(c.compare(dog, dog) == 0); - assertTrue(c.compare(dog2, dog) < 0); + assertThat(c.compare(dog, dog2) > 0).isTrue(); + assertThat(c.compare(dog, dog) == 0).isTrue(); + assertThat(c.compare(dog2, dog) < 0).isTrue(); } @Test @@ -49,7 +49,7 @@ public void testPropertyComparatorNulls() { Dog dog = new Dog(); Dog dog2 = new Dog(); PropertyComparator c = new PropertyComparator<>("nickName", false, true); - assertTrue(c.compare(dog, dog2) == 0); + assertThat(c.compare(dog, dog2) == 0).isTrue(); } @Test @@ -64,13 +64,13 @@ public void testChainedComparators() { dog2.setFirstName("biscuit"); dog2.setLastName("grayspots"); - assertTrue(c.compare(dog1, dog2) == 0); + assertThat(c.compare(dog1, dog2) == 0).isTrue(); c = c.thenComparing(new PropertyComparator<>("firstName", false, true)); - assertTrue(c.compare(dog1, dog2) > 0); + assertThat(c.compare(dog1, dog2) > 0).isTrue(); dog2.setLastName("konikk dog"); - assertTrue(c.compare(dog2, dog1) > 0); + assertThat(c.compare(dog2, dog1) > 0).isTrue(); } @Test @@ -86,12 +86,13 @@ public void testChainedComparatorsReversed() { dog2.setFirstName("biscuit"); dog2.setLastName("grayspots"); - assertTrue(c.compare(dog1, dog2) > 0); + assertThat(c.compare(dog1, dog2) > 0).isTrue(); c = c.reversed(); - assertTrue(c.compare(dog1, dog2) < 0); + assertThat(c.compare(dog1, dog2) < 0).isTrue(); } + @SuppressWarnings("unused") private static class Dog implements Comparable { private String nickName; diff --git a/spring-beans/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java b/spring-beans/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java index 7748b29d9571..4c5528c7cdaf 100644 --- a/spring-beans/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java +++ b/spring-beans/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/BeanUtilsKotlinTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/KotlinBeanUtilsTests.kt similarity index 57% rename from spring-beans/src/test/kotlin/org/springframework/beans/BeanUtilsKotlinTests.kt rename to spring-beans/src/test/kotlin/org/springframework/beans/KotlinBeanUtilsTests.kt index 01b2c4a3b0dd..647d320fad96 100644 --- a/spring-beans/src/test/kotlin/org/springframework/beans/BeanUtilsKotlinTests.kt +++ b/spring-beans/src/test/kotlin/org/springframework/beans/KotlinBeanUtilsTests.kt @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,8 +16,8 @@ package org.springframework.beans -import org.junit.Assert.* -import org.junit.Test +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test /** * Kotlin tests for {@link BeanUtils}. @@ -25,45 +25,69 @@ import org.junit.Test * @author Sebastien Deleuze */ @Suppress("unused", "UNUSED_PARAMETER") -class BeanUtilsKotlinTests { +class KotlinBeanUtilsTests { @Test fun `Instantiate immutable class`() { val constructor = BeanUtils.findPrimaryConstructor(Foo::class.java)!! val foo = BeanUtils.instantiateClass(constructor, "a", 3) - assertEquals("a", foo.param1) - assertEquals(3, foo.param2) + assertThat(foo.param1).isEqualTo("a") + assertThat(foo.param2).isEqualTo(3) } @Test fun `Instantiate immutable class with optional parameter and all parameters specified`() { val constructor = BeanUtils.findPrimaryConstructor(Bar::class.java)!! val bar = BeanUtils.instantiateClass(constructor, "a", 8) - assertEquals("a", bar.param1) - assertEquals(8, bar.param2) + assertThat(bar.param1).isEqualTo("a") + assertThat(bar.param2).isEqualTo(8) } @Test fun `Instantiate immutable class with optional parameter and only mandatory parameters specified by position`() { val constructor = BeanUtils.findPrimaryConstructor(Bar::class.java)!! val bar = BeanUtils.instantiateClass(constructor, "a") - assertEquals("a", bar.param1) - assertEquals(12, bar.param2) + assertThat(bar.param1).isEqualTo("a") + assertThat(bar.param2).isEqualTo(12) } @Test - fun `Instantiate immutable class with optional parameter specified with null value`() { + fun `Instantiate immutable class with optional parameter specified with null value`() { val constructor = BeanUtils.findPrimaryConstructor(Bar::class.java)!! val bar = BeanUtils.instantiateClass(constructor, "a", null) - assertEquals("a", bar.param1) - assertEquals(12, bar.param2) + assertThat(bar.param1).isEqualTo("a") + assertThat(bar.param2).isEqualTo(12) + } + + @Test // gh-22531 + fun `Instantiate immutable class with nullable parameter`() { + val constructor = BeanUtils.findPrimaryConstructor(Qux::class.java)!! + val bar = BeanUtils.instantiateClass(constructor, "a", null) + assertThat(bar.param1).isEqualTo("a") + assertThat(bar.param2).isNull() } @Test // SPR-15851 fun `Instantiate mutable class with declared constructor and default values for all parameters`() { val baz = BeanUtils.instantiateClass(Baz::class.java.getDeclaredConstructor()) - assertEquals("a", baz.param1) - assertEquals(12, baz.param2) + assertThat(baz.param1).isEqualTo("a") + assertThat(baz.param2).isEqualTo(12) + } + + @Test + @Suppress("UsePropertyAccessSyntax") + fun `Instantiate class with private constructor`() { + BeanUtils.instantiateClass(PrivateConstructor::class.java.getDeclaredConstructor()) + } + + @Test + fun `Instantiate class with protected constructor`() { + BeanUtils.instantiateClass(ProtectedConstructor::class.java.getDeclaredConstructor()) + } + + @Test + fun `Instantiate private class`() { + BeanUtils.instantiateClass(PrivateClass::class.java.getDeclaredConstructor()) } class Foo(val param1: String, val param2: Int) @@ -72,6 +96,8 @@ class BeanUtilsKotlinTests { class Baz(var param1: String = "a", var param2: Int = 12) + class Qux(val param1: String, val param2: Int?) + class TwoConstructorsWithDefaultOne { constructor() @@ -96,4 +122,10 @@ class BeanUtilsKotlinTests { constructor(param1: String) } + class PrivateConstructor private constructor() + + open class ProtectedConstructor protected constructor() + + private class PrivateClass + } diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt index 85d71758dcbd..6ba9e5dd50ce 100644 --- a/spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt +++ b/spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,13 +16,9 @@ package org.springframework.beans.factory -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.Answers -import org.mockito.ArgumentMatchers -import org.mockito.Mock -import org.mockito.Mockito.* -import org.mockito.junit.MockitoJUnitRunner +import io.mockk.mockk +import io.mockk.verify +import org.junit.jupiter.api.Test import org.springframework.core.ResolvableType /** @@ -30,23 +26,21 @@ import org.springframework.core.ResolvableType * * @author Sebastien Deleuze */ -@RunWith(MockitoJUnitRunner::class) class BeanFactoryExtensionsTests { - @Mock(answer = Answers.RETURNS_MOCKS) - lateinit var bf: BeanFactory + val bf = mockk(relaxed = true) @Test fun `getBean with reified type parameters`() { bf.getBean() - verify(bf, times(1)).getBean(Foo::class.java) + verify { bf.getBean(Foo::class.java) } } @Test fun `getBean with String and reified type parameters`() { val name = "foo" bf.getBean(name) - verify(bf, times(1)).getBean(name, Foo::class.java) + verify { bf.getBean(name, Foo::class.java) } } @Test @@ -54,13 +48,13 @@ class BeanFactoryExtensionsTests { val arg1 = "arg1" val arg2 = "arg2" bf.getBean(arg1, arg2) - verify(bf, times(1)).getBean(Foo::class.java, arg1, arg2) + verify { bf.getBean(Foo::class.java, arg1, arg2) } } @Test fun `getBeanProvider with reified type parameters`() { bf.getBeanProvider() - verify(bf, times(1)).getBeanProvider>(ArgumentMatchers.any()) + verify { bf.getBeanProvider>(ofType()) } } class Foo diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt index 833300b59d5a..a86a60aec9ba 100644 --- a/spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt +++ b/spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,77 +16,72 @@ package org.springframework.beans.factory -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.Answers -import org.mockito.Mock -import org.mockito.Mockito.* -import org.mockito.junit.MockitoJUnitRunner +import io.mockk.mockk +import io.mockk.verify +import org.junit.jupiter.api.Test /** * Mock object based tests for ListableBeanFactory Kotlin extensions * * @author Sebastien Deleuze */ -@RunWith(MockitoJUnitRunner::class) class ListableBeanFactoryExtensionsTests { - @Mock(answer = Answers.RETURNS_MOCKS) - lateinit var lbf: ListableBeanFactory + val lbf = mockk(relaxed = true) @Test fun `getBeanNamesForType with reified type parameters`() { lbf.getBeanNamesForType() - verify(lbf, times(1)).getBeanNamesForType(Foo::class.java, true , true) + verify { lbf.getBeanNamesForType(Foo::class.java, true , true) } } @Test fun `getBeanNamesForType with reified type parameters and Boolean`() { lbf.getBeanNamesForType(false) - verify(lbf, times(1)).getBeanNamesForType(Foo::class.java, false , true) + verify { lbf.getBeanNamesForType(Foo::class.java, false , true) } } @Test fun `getBeanNamesForType with reified type parameters, Boolean and Boolean`() { lbf.getBeanNamesForType(false, false) - verify(lbf, times(1)).getBeanNamesForType(Foo::class.java, false , false) + verify { lbf.getBeanNamesForType(Foo::class.java, false , false) } } @Test fun `getBeansOfType with reified type parameters`() { lbf.getBeansOfType() - verify(lbf, times(1)).getBeansOfType(Foo::class.java, true , true) + verify { lbf.getBeansOfType(Foo::class.java, true , true) } } @Test fun `getBeansOfType with reified type parameters and Boolean`() { lbf.getBeansOfType(false) - verify(lbf, times(1)).getBeansOfType(Foo::class.java, false , true) + verify { lbf.getBeansOfType(Foo::class.java, false , true) } } @Test fun `getBeansOfType with reified type parameters, Boolean and Boolean`() { lbf.getBeansOfType(false, false) - verify(lbf, times(1)).getBeansOfType(Foo::class.java, false , false) + verify { lbf.getBeansOfType(Foo::class.java, false , false) } } @Test fun `getBeanNamesForAnnotation with reified type parameters`() { lbf.getBeanNamesForAnnotation() - verify(lbf, times(1)).getBeanNamesForAnnotation(Bar::class.java) + verify { lbf.getBeanNamesForAnnotation(Bar::class.java) } } @Test fun `getBeansWithAnnotation with reified type parameters`() { lbf.getBeansWithAnnotation() - verify(lbf, times(1)).getBeansWithAnnotation(Bar::class.java) + verify { lbf.getBeansWithAnnotation(Bar::class.java) } } @Test fun `findAnnotationOnBean with String and reified type parameters`() { val name = "bar" lbf.findAnnotationOnBean(name) - verify(lbf, times(1)).findAnnotationOnBean(name, Bar::class.java) + verify { lbf.findAnnotationOnBean(name, Bar::class.java) } } class Foo diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt index a195bc1ca1c4..37d6793cad50 100644 --- a/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt +++ b/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,15 +16,14 @@ package org.springframework.beans.factory.annotation -import org.junit.Test - +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatExceptionOfType +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.BeanCreationException import org.springframework.beans.factory.support.DefaultListableBeanFactory import org.springframework.beans.factory.support.RootBeanDefinition -import org.springframework.tests.sample.beans.TestBean - -import org.junit.Assert.* -import org.springframework.beans.factory.BeanCreationException -import org.springframework.tests.sample.beans.Colour +import org.springframework.beans.testfixture.beans.Colour +import org.springframework.beans.testfixture.beans.TestBean /** * Tests for Kotlin support with [Autowired]. @@ -47,9 +46,9 @@ class KotlinAutowiredTests { bf.registerSingleton("testBean", tb) val kb = bf.getBean("annotatedBean", KotlinBean::class.java) - assertSame(tb, kb.injectedFromConstructor) - assertSame(tb, kb.injectedFromMethod) - assertSame(tb, kb.injectedField) + assertThat(kb.injectedFromConstructor).isSameAs(tb) + assertThat(kb.injectedFromMethod).isSameAs(tb) + assertThat(kb.injectedField).isSameAs(tb) } @Test @@ -63,9 +62,9 @@ class KotlinAutowiredTests { bf.registerBeanDefinition("annotatedBean", bd) val kb = bf.getBean("annotatedBean", KotlinBean::class.java) - assertNull(kb.injectedFromConstructor) - assertNull(kb.injectedFromMethod) - assertNull(kb.injectedField) + assertThat(kb.injectedFromConstructor).isNull() + assertThat(kb.injectedFromMethod).isNull() + assertThat(kb.injectedField).isNull() } @Test // SPR-15847 @@ -81,9 +80,9 @@ class KotlinAutowiredTests { bf.registerSingleton("testBean", tb) val kb = bf.getBean("bean", KotlinBeanWithMandatoryAndOptionalParameters::class.java) - assertSame(tb, kb.injectedFromConstructor) - assertEquals("foo", kb.optional) - assertEquals("bar", kb.initializedField) + assertThat(kb.injectedFromConstructor).isSameAs(tb) + assertThat(kb.optional).isEqualTo("foo") + assertThat(kb.initializedField).isEqualTo("bar") } @Test @@ -97,9 +96,9 @@ class KotlinAutowiredTests { bf.registerBeanDefinition("bean", bd) val kb = bf.getBean("bean", KotlinBeanWithOptionalParameters::class.java) - assertNotNull(kb.optional1) - assertEquals("foo", kb.optional2) - assertEquals("bar", kb.initializedField) + assertThat(kb.optional1).isNotNull() + assertThat(kb.optional2).isEqualTo("foo") + assertThat(kb.initializedField).isEqualTo("bar") } @Test // SPR-15847 @@ -115,8 +114,8 @@ class KotlinAutowiredTests { bf.registerSingleton("testBean", tb) val kb = bf.getBean("bean", KotlinBeanWithOptionalParameterAndExplicitConstructor::class.java) - assertSame(tb, kb.injectedFromConstructor) - assertEquals("foo", kb.optional) + assertThat(kb.injectedFromConstructor).isSameAs(tb) + assertThat(kb.optional).isEqualTo("foo") } @Test // SPR-15847 @@ -134,9 +133,9 @@ class KotlinAutowiredTests { bf.registerSingleton("colour", colour) val kb = bf.getBean("bean", KotlinBeanWithAutowiredSecondaryConstructor::class.java) - assertSame(tb, kb.injectedFromConstructor) - assertEquals("bar", kb.optional) - assertSame(colour, kb.injectedFromSecondaryConstructor) + assertThat(kb.injectedFromConstructor).isSameAs(tb) + assertThat(kb.optional).isEqualTo("bar") + assertThat(kb.injectedFromSecondaryConstructor).isSameAs(colour) } @Test // SPR-16012 @@ -150,7 +149,7 @@ class KotlinAutowiredTests { bf.registerBeanDefinition("bean", bd) val kb = bf.getBean("bean", KotlinBeanWithPrimaryAndDefaultConstructors::class.java) - assertNotNull(kb.testBean) + assertThat(kb.testBean).isNotNull() } @Test // SPR-16012 @@ -166,7 +165,7 @@ class KotlinAutowiredTests { bf.registerSingleton("testBean", tb) val kb = bf.getBean("bean", KotlinBeanWithPrimaryAndDefaultConstructors::class.java) - assertEquals(tb, kb.testBean) + assertThat(kb.testBean).isEqualTo(tb) } @Test // SPR-16289 @@ -183,7 +182,7 @@ class KotlinAutowiredTests { bf.getBean(KotlinBeanWithPrimaryAndSecondaryConstructors::class.java) } - @Test(expected = BeanCreationException::class) // SPR-16022 + @Test // SPR-16022 fun `No autowiring with primary and secondary non annotated constructors`() { val bf = DefaultListableBeanFactory() val bpp = AutowiredAnnotationBeanPostProcessor() @@ -197,7 +196,9 @@ class KotlinAutowiredTests { val colour = Colour.BLUE bf.registerSingleton("colour", colour) - bf.getBean("bean", KotlinBeanWithSecondaryConstructor::class.java) + assertThatExceptionOfType(BeanCreationException::class.java).isThrownBy { + bf.getBean("bean", KotlinBeanWithSecondaryConstructor::class.java) + } } diff --git a/spring-beans/src/test/resources/log4j2-test.xml b/spring-beans/src/test/resources/log4j2-test.xml index f5e85a2eac86..fb1c94a4807d 100644 --- a/spring-beans/src/test/resources/log4j2-test.xml +++ b/spring-beans/src/test/resources/log4j2-test.xml @@ -2,7 +2,7 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-dependentBeans.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-dependentBeans.xml index b853f8bd2678..7742f4046d3c 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-dependentBeans.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-dependentBeans.xml @@ -2,7 +2,7 @@ + http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-leaf.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-leaf.xml index 11d9f33a67fc..435dcb06cbd8 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-leaf.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-leaf.xml @@ -1,9 +1,9 @@ - + - + custom 25 diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-middle.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-middle.xml index c5fa5b285817..4804e44cf889 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-middle.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-middle.xml @@ -1,10 +1,10 @@ - + - + custom 666 @@ -13,7 +13,7 @@ Check that invoker is automatically added to wrap target. Non pointcut bean name should be wrapped in invoker. --> - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-root.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-root.xml index 7cc47b9a010f..fea62a3791b1 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-root.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/BeanFactoryUtilsTests-root.xml @@ -1,26 +1,26 @@ - + - + - + - + - + custom 25 - + - + false diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/ConcurrentBeanFactoryTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/ConcurrentBeanFactoryTests-context.xml index c88fd668a300..8dceadf07278 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/ConcurrentBeanFactoryTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/ConcurrentBeanFactoryTests-context.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanLookupTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanLookupTests-context.xml index d87aaffc639f..bbd84e286a84 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanLookupTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanLookupTests-context.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-abstract.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-abstract.xml index e82ccdc59bcd..9bd5ca7c57c9 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-abstract.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-abstract.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-circular.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-circular.xml index 1c328aa8d461..18fef0b0006e 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-circular.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-circular.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-returnsNull.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-returnsNull.xml index d9b583910ea0..a40ed35bb98a 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-returnsNull.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-returnsNull.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-withAutowiring.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-withAutowiring.xml index 0f166640440d..90ce2158a93a 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-withAutowiring.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/FactoryBeanTests-withAutowiring.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests-context.xml index 09e68d8f38ea..f3481e412f35 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/annotation/CustomAutowireConfigurerTests-context.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests-context.xml index 2e2d9fd435fc..35c854bdb0e0 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/config/FieldRetrievingFactoryBeanTests-context.xml @@ -1,9 +1,9 @@ - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests-context.xml index aafb5327e06c..960e6ed73cda 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/config/ObjectFactoryCreatingFactoryBeanTests-context.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests-context.xml index bc9234d1c30d..d9979e38339a 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/config/PropertyPathFactoryBeanTests-context.xml @@ -1,21 +1,21 @@ - + - + 10 - + 11 - + 98 - + 99 @@ -23,7 +23,7 @@ - + 12 @@ -46,10 +46,10 @@ tb spouse - org.springframework.tests.sample.beans.TestBean + org.springframework.beans.testfixture.beans.TestBean - + @@ -59,11 +59,11 @@ - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/config/SimpleScopeTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/config/SimpleScopeTests-context.xml index 69db4284cc79..9ac321b0cc45 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/config/SimpleScopeTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/config/SimpleScopeTests-context.xml @@ -1,8 +1,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/parsing/CustomProblemReporterTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/parsing/CustomProblemReporterTests-context.xml index c2cc486e9da5..1c6e22b51182 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/parsing/CustomProblemReporterTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/parsing/CustomProblemReporterTests-context.xml @@ -1,12 +1,12 @@ + "https://www.springframework.org/dtd/spring-beans-2.0.dtd"> - + @@ -14,9 +14,9 @@ - + - + @@ -26,5 +26,6 @@ - + + \ No newline at end of file diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/support/genericBeanTests.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/support/genericBeanTests.xml index 3e9a6577b556..c610f9cf1591 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/support/genericBeanTests.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/support/genericBeanTests.xml @@ -1,9 +1,9 @@ - + - + @@ -43,7 +43,7 @@ autowire="constructor"> - + @@ -53,7 +53,7 @@ - + @@ -64,7 +64,7 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/support/lookupMethodTests.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/support/lookupMethodTests.xml index 48d9bc11219e..a04f0e3beb42 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/support/lookupMethodTests.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/support/lookupMethodTests.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> @@ -13,9 +13,9 @@ - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/support/multiConstructorArgs.properties b/spring-beans/src/test/resources/org/springframework/beans/factory/support/multiConstructorArgs.properties index 8d5f74fc31a9..9cc82ac44933 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/support/multiConstructorArgs.properties +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/support/multiConstructorArgs.properties @@ -1,3 +1,3 @@ -testBean.(class)=org.springframework.tests.sample.beans.TestBean +testBean.(class)=org.springframework.beans.testfixture.beans.TestBean testBean.$0=Rob Harrop testBean.$1=23 diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/support/refConstructorArg.properties b/spring-beans/src/test/resources/org/springframework/beans/factory/support/refConstructorArg.properties index 4d3723c7de8a..b1bb01e2a507 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/support/refConstructorArg.properties +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/support/refConstructorArg.properties @@ -1,5 +1,5 @@ -sally.(class)=org.springframework.tests.sample.beans.TestBean +sally.(class)=org.springframework.beans.testfixture.beans.TestBean sally.name=Sally -rob.(class)=org.springframework.tests.sample.beans.TestBean +rob.(class)=org.springframework.beans.testfixture.beans.TestBean rob.$0(ref)=sally diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/support/security/callbacks.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/support/security/callbacks.xml index 36a8aeaebfc7..1df2d27b4850 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/support/security/callbacks.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/support/security/callbacks.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/support/simpleConstructorArg.properties b/spring-beans/src/test/resources/org/springframework/beans/factory/support/simpleConstructorArg.properties index d0f1eea32665..bc39b1b018c0 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/support/simpleConstructorArg.properties +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/support/simpleConstructorArg.properties @@ -1,2 +1,2 @@ -testBean.(class)=org.springframework.tests.sample.beans.TestBean +testBean.(class)=org.springframework.beans.testfixture.beans.TestBean testBean.$0=Rob Harrop diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-multiLevel-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-multiLevel-context.xml index 8e53cee29347..0e326790f7fd 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-multiLevel-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-multiLevel-context.xml @@ -2,14 +2,14 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-sameLevel-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-sameLevel-context.xml index 12a2fb08602d..40ecbec4ac9a 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-sameLevel-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/DuplicateBeanIdTests-sameLevel-context.xml @@ -2,14 +2,14 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml index d6be7a4426f3..2749cd23384e 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-context.xml index 49e415572c83..632d0356cef9 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-autowire-context.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-init-destroy-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-init-destroy-context.xml index 90c5495d7c64..bc030754df74 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-init-destroy-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-init-destroy-context.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-lazy-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-lazy-context.xml index 3c3df4abf083..78601ebf4bbd 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-lazy-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-lazy-context.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-merge-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-merge-context.xml index 2edadb511ebc..f3453c7bb943 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-merge-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementAttributeRecursionTests-merge-context.xml @@ -2,10 +2,10 @@ - + alpha diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementTests-context.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementTests-context.xml index 5500aeedaee0..8a44f25d797c 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementTests-context.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/NestedBeansElementTests-context.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-customDefaultProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-customDefaultProfile.xml index fbd09ae0fc87..74c2c2ff9496 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-customDefaultProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-customDefaultProfile.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultAndDevProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultAndDevProfile.xml index 5fbc741ec85b..fcb24a4fdab4 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultAndDevProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultAndDevProfile.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultProfile.xml index a7d425abf1bd..ca8cbf4c8035 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-defaultProfile.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-devProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-devProfile.xml index 8ae82bc8cf60..7f391e8b7385 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-devProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-devProfile.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfile.xml index dac5fb924498..e3a36f29f5bb 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfile.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNegated.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNegated.xml index a11d17043118..9c35ae70d86c 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNegated.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNegated.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNotDev.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNotDev.xml index b0a246c4a271..f5820c54c074 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNotDev.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-multiProfileNotDev.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-noProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-noProfile.xml index 656ea3aceb19..49a8c7abd1eb 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-noProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-noProfile.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-notDevProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-notDevProfile.xml index 06ac54a37a89..9d1852262370 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-notDevProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-notDevProfile.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-prodProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-prodProfile.xml index aba52d7066f5..879b23caee5c 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-prodProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-prodProfile.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml index 8bfb9a98138e..3d5023102756 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-unknownProfile.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-unknownProfile.xml index 922ba2904855..0b0dbcef6eed 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-unknownProfile.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-unknownProfile.xml @@ -2,7 +2,7 @@ diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-constructor-with-exclusion.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-constructor-with-exclusion.xml index 2fcb11795450..2b2515103095 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-constructor-with-exclusion.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-constructor-with-exclusion.xml @@ -1,11 +1,11 @@ - + - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-exclusion.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-exclusion.xml index a2e966aab984..692cc7743f56 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-exclusion.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-exclusion.xml @@ -1,11 +1,11 @@ - + - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-inclusion.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-inclusion.xml index 9b93a16810de..f4090d14a71e 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-inclusion.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-inclusion.xml @@ -1,12 +1,12 @@ - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-selective-inclusion.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-selective-inclusion.xml index 089517cd427b..d52ba19ec021 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-selective-inclusion.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/autowire-with-selective-inclusion.xml @@ -1,12 +1,12 @@ - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEvents.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEvents.xml index 0b9a97de5b8a..2bdea186844c 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEvents.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEvents.xml @@ -1,5 +1,5 @@ - + @@ -10,22 +10,22 @@ - + - + - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEventsImported.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEventsImported.xml index bdfc3c4c31e0..299c52abef96 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEventsImported.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanEventsImported.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanNameGeneration.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanNameGeneration.xml index 813049d1a59a..1f22caddffd8 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanNameGeneration.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/beanNameGeneration.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionMerging.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionMerging.xml index d8fc1db88ea5..d45764707947 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionMerging.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionMerging.xml @@ -1,9 +1,9 @@ - + - + Rob Harrop @@ -23,12 +23,12 @@ - + - + Rob Harrop @@ -47,14 +47,14 @@ - + - + @@ -76,7 +76,7 @@ - + @@ -84,7 +84,7 @@ - + Sall @@ -103,7 +103,7 @@ - + Rob Harrop @@ -123,12 +123,12 @@ - + - + Rob Harrop @@ -147,14 +147,14 @@ - + - + @@ -176,7 +176,7 @@ - + @@ -184,7 +184,7 @@ - + Sall diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml index de31737e4fce..5683b4fe490e 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml @@ -1,9 +1,9 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> - + Jenny 30 @@ -12,7 +12,7 @@ - + Simple bean, without any collections. @@ -23,7 +23,7 @@ 27 - + Rod 32 @@ -35,7 +35,7 @@ - + Jenny 30 @@ -43,13 +43,13 @@ - + David 27 - + Rod 32 @@ -63,7 +63,7 @@ - + loner 26 @@ -101,26 +101,26 @@ - + verbose - + - + - + - + @@ -130,7 +130,7 @@ - + @@ -157,7 +157,7 @@ - + @@ -176,7 +176,7 @@ - + @@ -185,7 +185,7 @@ - + @@ -217,14 +217,14 @@ - + - + bar @@ -234,7 +234,7 @@ - + bar @@ -244,7 +244,7 @@ - + @@ -253,7 +253,7 @@ - + @@ -261,7 +261,7 @@ - + bar @@ -270,7 +270,7 @@ - + @@ -279,7 +279,7 @@ - + one @@ -288,7 +288,7 @@ - + 0 @@ -298,11 +298,11 @@ - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionsWithDefaultTypes.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionsWithDefaultTypes.xml index 8a31ff86962e..a1a1ea378b1b 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionsWithDefaultTypes.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collectionsWithDefaultTypes.xml @@ -1,9 +1,9 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> - + 1 @@ -28,7 +28,7 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml index 7414f870b187..9bd3f136386c 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/factory-methods.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/factory-methods.xml index 9f339bfd3df4..93f338b00178 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/factory-methods.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/factory-methods.xml @@ -1,9 +1,9 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> - setterString @@ -21,48 +21,48 @@ - - - setterString - 27 gotcha - 27 - 27 - 27 - @@ -72,31 +72,31 @@ - setterString - testBeanOnlyPrototypeDISetterString - - 27 gotcha - 27 @@ -104,10 +104,10 @@ bogus - - + Juergen @@ -130,7 +130,7 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ignoreDefaultLifecycleMethods.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ignoreDefaultLifecycleMethods.xml index a10029da323a..25ffd4262bd2 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ignoreDefaultLifecycleMethods.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/ignoreDefaultLifecycleMethods.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/import.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/import.xml index 278e5dff8116..44b1c8f1e9db 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/import.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/import.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/importPattern.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/importPattern.xml index 085a29d85fd5..329d25898ead 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/importPattern.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/importPattern.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/invalidPerSchema.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/invalidPerSchema.xml index 430c8adc7e42..6715273a6384 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/invalidPerSchema.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/invalidPerSchema.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/schemaValidated.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/schemaValidated.xml index 7802a4c935b0..59b5ef71c3cc 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/schemaValidated.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/schemaValidated.xml @@ -1,18 +1,18 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> - + - + - + - - + + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTests.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTests.xml index b26742888786..c8adc6bdb3dc 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTests.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTests.xml @@ -2,46 +2,46 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> - + - + - + - + - + - + - + \ No newline at end of file diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTestsWithErrors.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTestsWithErrors.xml index 7ffa44cd2344..3dbfb5fae18f 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTestsWithErrors.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simpleConstructorNamespaceHandlerTestsWithErrors.xml @@ -1,9 +1,9 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTests.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTests.xml index 260168852ff0..83e74fad1694 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTests.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTests.xml @@ -2,22 +2,22 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> - + - + - + - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTestsWithErrors.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTestsWithErrors.xml index 4399b58b10d0..b64093d0fc8a 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTestsWithErrors.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/simplePropertyNamespaceHandlerTestsWithErrors.xml @@ -2,12 +2,12 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> - + - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/test.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/test.xml index 71faa7e27e5f..7c4c7596e79f 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/test.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/test.xml @@ -1,9 +1,9 @@ - + - + I have no properties and I'm happy without them. @@ -12,7 +12,7 @@ - + aliased @@ -20,17 +20,17 @@ - + aliased - + aliased - + @@ -40,7 +40,7 @@ - + Rod 31 @@ -52,29 +52,29 @@ - + Kerry 34 - + Kathy 28 - + typeMismatch 34x - + - true @@ -85,10 +85,10 @@ - + - + false @@ -113,14 +113,14 @@ - + listenerVeto 66 - + - + this is a ]]> diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml index 6956ac3c714e..972aabcd669a 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.1.xsd + http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util-4.1.xsd"> - + @@ -26,13 +26,13 @@ - + - + @@ -50,7 +50,7 @@ + key-type="java.lang.String" value-type="org.springframework.beans.testfixture.beans.TestBean"> @@ -72,7 +72,7 @@ Rob Harrop - + foo @@ -94,13 +94,13 @@ - + - + @@ -116,13 +116,13 @@ min - + - + @@ -152,7 +152,7 @@ - + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithDtd.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithDtd.xml index 3cca869dbffa..fbe7861e21c0 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithDtd.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithDtd.xml @@ -6,8 +6,8 @@ This is a top level block comment - + - + \ No newline at end of file diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithXsd.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithXsd.xml index 20aa0f537406..1e467348ea18 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithXsd.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/validateWithXsd.xml @@ -6,6 +6,6 @@ This is a top level block comment - - + + \ No newline at end of file diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/withMeta.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/withMeta.xml index 05c47afa53a6..5f7388177722 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/withMeta.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/withMeta.xml @@ -2,17 +2,17 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> - + - + - + diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/AgeHolder.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/AgeHolder.java similarity index 86% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/AgeHolder.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/AgeHolder.java index ebc0932df93a..722cea949646 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/AgeHolder.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/AgeHolder.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; public interface AgeHolder { diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/AnnotatedBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/AnnotatedBean.java similarity index 86% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/AnnotatedBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/AnnotatedBean.java index d08c5476d717..985cf64de8af 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/AnnotatedBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/AnnotatedBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * @author Stephane Nicoll diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/BooleanTestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/BooleanTestBean.java similarity index 90% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/BooleanTestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/BooleanTestBean.java index 0b15573b040e..5edbd304eddc 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/BooleanTestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/BooleanTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * @author Juergen Hoeller diff --git a/spring-beans/src/test/java/org/springframework/tests/beans/CollectingReaderEventListener.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CollectingReaderEventListener.java similarity index 86% rename from spring-beans/src/test/java/org/springframework/tests/beans/CollectingReaderEventListener.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CollectingReaderEventListener.java index 1445c9944e1d..a4684998a36a 100644 --- a/spring-beans/src/test/java/org/springframework/tests/beans/CollectingReaderEventListener.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CollectingReaderEventListener.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.beans; +package org.springframework.beans.testfixture.beans; import java.util.ArrayList; import java.util.Collection; @@ -65,16 +65,12 @@ public ComponentDefinition getComponentDefinition(String name) { public ComponentDefinition[] getComponentDefinitions() { Collection collection = this.componentDefinitions.values(); - return collection.toArray(new ComponentDefinition[collection.size()]); + return collection.toArray(new ComponentDefinition[0]); } @Override public void aliasRegistered(AliasDefinition aliasDefinition) { - List aliases = this.aliasMap.get(aliasDefinition.getBeanName()); - if (aliases == null) { - aliases = new ArrayList<>(); - this.aliasMap.put(aliasDefinition.getBeanName(), aliases); - } + List aliases = this.aliasMap.computeIfAbsent(aliasDefinition.getBeanName(), k -> new ArrayList<>()); aliases.add(aliasDefinition); } diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/Colour.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Colour.java similarity index 88% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/Colour.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Colour.java index 0c6903a55c05..531149005197 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/Colour.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Colour.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,13 +14,12 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * @author Rob Harrop * @author Juergen Hoeller */ -@SuppressWarnings("serial") public class Colour { public static final Colour RED = new Colour("RED"); diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/CountingTestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CountingTestBean.java similarity index 87% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/CountingTestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CountingTestBean.java index e5f6947de5a6..aa9083a88e11 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/CountingTestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CountingTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/CustomEnum.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CustomEnum.java similarity index 87% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/CustomEnum.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CustomEnum.java index 01dc3b8540a7..de0d721943d8 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/CustomEnum.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/CustomEnum.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * @author Juergen Hoeller diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DependenciesBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DependenciesBean.java similarity index 93% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/DependenciesBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DependenciesBean.java index b17d1028cc53..f990781e11df 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DependenciesBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DependenciesBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DerivedTestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DerivedTestBean.java similarity index 95% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/DerivedTestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DerivedTestBean.java index d12db5b7adf1..c0e9c2e1dca4 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DerivedTestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DerivedTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.io.Serializable; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DummyBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DummyBean.java similarity index 92% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/DummyBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DummyBean.java index cb5767eef59b..cac5cced1c8a 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DummyBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DummyBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * @author Costin Leau diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DummyFactory.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DummyFactory.java similarity index 97% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/DummyFactory.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DummyFactory.java index 42743d599958..2cf683a7c5fb 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/DummyFactory.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/DummyFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; diff --git a/spring-context/src/test/java/org/springframework/tests/sample/beans/Employee.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Employee.java similarity index 82% rename from spring-context/src/test/java/org/springframework/tests/sample/beans/Employee.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Employee.java index fc2c639fa5cc..3e9da7609e35 100644 --- a/spring-context/src/test/java/org/springframework/tests/sample/beans/Employee.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Employee.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; public class Employee extends TestBean { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethods.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/FactoryMethods.java similarity index 89% rename from spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethods.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/FactoryMethods.java index 4a9703eaa7cd..98b26798a2c8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethods.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/FactoryMethods.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,13 +14,11 @@ * limitations under the License. */ -package org.springframework.beans.factory.xml; +package org.springframework.beans.testfixture.beans; import java.util.Collections; import java.util.List; -import org.springframework.tests.sample.beans.TestBean; - /** * Test class for Spring's ability to create objects using static * factory methods, rather than constructors. @@ -47,7 +45,7 @@ public static FactoryMethods newInstance(TestBean tb) { return new FactoryMethods(tb, "default", 0); } - protected static FactoryMethods newInstance(TestBean tb, int num, String name) { + public static FactoryMethods newInstance(TestBean tb, int num, String name) { if (name == null) { throw new IllegalStateException("Should never be called with null value"); } @@ -62,7 +60,7 @@ static ExtendedFactoryMethods newInstance(TestBean tb, int num, Integer somethin } @SuppressWarnings("unused") - private static List listInstance() { + private static List listInstance() { return Collections.EMPTY_LIST; } diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBean.java similarity index 92% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBean.java index 3f63617c814d..2ad5ece17f5e 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.util.ArrayList; import java.util.Collection; @@ -51,7 +51,7 @@ public class GenericBean { private List> listOfMaps; - private Map plainMap; + private Map plainMap; private Map shortMap; @@ -100,7 +100,7 @@ public GenericBean(Map shortMap, Resource resource) { this.resourceList = Collections.singletonList(resource); } - public GenericBean(Map plainMap, Map shortMap) { + public GenericBean(Map plainMap, Map shortMap) { this.plainMap = plainMap; this.shortMap = shortMap; } @@ -178,7 +178,7 @@ public void setListOfMaps(List> listOfMaps) { this.listOfMaps = listOfMaps; } - public Map getPlainMap() { + public Map getPlainMap() { return plainMap; } @@ -289,30 +289,37 @@ public void setStandardEnumMap(EnumMap standardEnumMap) { this.standardEnumMap = standardEnumMap; } + @SuppressWarnings({ "rawtypes", "unchecked" }) public static GenericBean createInstance(Set integerSet) { return new GenericBean(integerSet); } + @SuppressWarnings({ "rawtypes", "unchecked" }) public static GenericBean createInstance(Set integerSet, List resourceList) { return new GenericBean(integerSet, resourceList); } + @SuppressWarnings({ "rawtypes", "unchecked" }) public static GenericBean createInstance(HashSet integerSet, Map shortMap) { return new GenericBean(integerSet, shortMap); } + @SuppressWarnings({ "rawtypes", "unchecked" }) public static GenericBean createInstance(Map shortMap, Resource resource) { return new GenericBean(shortMap, resource); } + @SuppressWarnings({ "rawtypes", "unchecked" }) public static GenericBean createInstance(Map map, Map shortMap) { return new GenericBean(map, shortMap); } + @SuppressWarnings({ "rawtypes", "unchecked" }) public static GenericBean createInstance(HashMap longMap) { return new GenericBean(longMap); } + @SuppressWarnings({ "rawtypes", "unchecked" }) public static GenericBean createInstance(boolean someFlag, Map> collectionMap) { return new GenericBean(someFlag, collectionMap); } diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericIntegerBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericIntegerBean.java similarity index 86% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericIntegerBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericIntegerBean.java index 305a936f1236..69fd56d17472 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericIntegerBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericIntegerBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericSetOfIntegerBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericSetOfIntegerBean.java similarity index 87% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericSetOfIntegerBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericSetOfIntegerBean.java index 13fb8971556b..c0439f9fb669 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/GenericSetOfIntegerBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericSetOfIntegerBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.util.Set; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/HasMap.java similarity index 83% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/HasMap.java index 51c5415b6740..c874077bd1b2 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/HasMap.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.util.IdentityHashMap; import java.util.List; @@ -45,9 +45,9 @@ public class HasMap { private List> classList; - private IdentityHashMap identityMap; + private IdentityHashMap identityMap; - private CopyOnWriteArraySet concurrentSet; + private CopyOnWriteArraySet concurrentSet; private HasMap() { } @@ -108,19 +108,19 @@ public void setClassList(List> classList) { this.classList = classList; } - public IdentityHashMap getIdentityMap() { + public IdentityHashMap getIdentityMap() { return identityMap; } - public void setIdentityMap(IdentityHashMap identityMap) { + public void setIdentityMap(IdentityHashMap identityMap) { this.identityMap = identityMap; } - public CopyOnWriteArraySet getConcurrentSet() { + public CopyOnWriteArraySet getConcurrentSet() { return concurrentSet; } - public void setConcurrentSet(CopyOnWriteArraySet concurrentSet) { + public void setConcurrentSet(CopyOnWriteArraySet concurrentSet) { this.concurrentSet = concurrentSet; } diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/INestedTestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/INestedTestBean.java similarity index 85% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/INestedTestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/INestedTestBean.java index 63551fe970e0..3107e6b5f21c 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/INestedTestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/INestedTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; public interface INestedTestBean { diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/IOther.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/IOther.java similarity index 78% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/IOther.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/IOther.java index aea22b6b9d73..05059bcf6873 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/IOther.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/IOther.java @@ -1,12 +1,11 @@ - /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -15,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; public interface IOther { diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/ITestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/ITestBean.java similarity index 90% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/ITestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/ITestBean.java index 796e7e234ce6..742b39c4ea7e 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/ITestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/ITestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,12 +14,12 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.io.IOException; /** - * Interface used for {@link org.springframework.tests.sample.beans.TestBean}. + * Interface used for {@link org.springframework.beans.testfixture.beans.TestBean}. * *

Two methods are the same as on Person, but if this * extends person it breaks quite a few tests.. diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/IndexedTestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/IndexedTestBean.java similarity index 92% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/IndexedTestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/IndexedTestBean.java index 2bcd01de8d87..02948f7eb854 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/IndexedTestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/IndexedTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.util.ArrayList; import java.util.Collection; @@ -30,6 +30,7 @@ * @author Juergen Hoeller * @since 11.11.2003 */ +@SuppressWarnings("rawtypes") public class IndexedTestBean { private TestBean[] array; @@ -57,6 +58,7 @@ public IndexedTestBean(boolean populate) { } } + @SuppressWarnings("unchecked") public void populate() { TestBean tb0 = new TestBean("name0", 0); TestBean tb1 = new TestBean("name1", 0); @@ -66,6 +68,7 @@ public void populate() { TestBean tb5 = new TestBean("name5", 0); TestBean tb6 = new TestBean("name6", 0); TestBean tb7 = new TestBean("name7", 0); + TestBean tb8 = new TestBean("name8", 0); TestBean tbX = new TestBean("nameX", 0); TestBean tbY = new TestBean("nameY", 0); this.array = new TestBean[] {tb0, tb1}; @@ -83,6 +86,7 @@ public void populate() { list.add(tbX); list.add(tbY); this.map.put("key4", list); + this.map.put("key5[foo]", tb8); } diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/LifecycleBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/LifecycleBean.java similarity index 97% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/LifecycleBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/LifecycleBean.java index 99863ee31a27..b2e54028270d 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/LifecycleBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/LifecycleBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/MustBeInitialized.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/MustBeInitialized.java similarity index 92% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/MustBeInitialized.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/MustBeInitialized.java index cc83d69d833e..2e53ce2f885a 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/MustBeInitialized.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/MustBeInitialized.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import org.springframework.beans.factory.InitializingBean; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/NestedTestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/NestedTestBean.java similarity index 93% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/NestedTestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/NestedTestBean.java index 92ca0347a1bb..ea26ec0072c0 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/NestedTestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/NestedTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * Simple nested test bean used for testing bean factories, AOP framework etc. diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/NumberTestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/NumberTestBean.java similarity index 95% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/NumberTestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/NumberTestBean.java index 5e14f69f0b53..224965b5c7a6 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/NumberTestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/NumberTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.math.BigDecimal; import java.math.BigInteger; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/PackageLevelVisibleBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/PackageLevelVisibleBean.java similarity index 88% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/PackageLevelVisibleBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/PackageLevelVisibleBean.java index 142a5da2d264..73a0bd3b9358 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/PackageLevelVisibleBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/PackageLevelVisibleBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * @see org.springframework.beans.factory.config.FieldRetrievingFactoryBeanTests diff --git a/spring-aop/src/test/java/org/springframework/tests/sample/beans/Person.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Person.java similarity index 89% rename from spring-aop/src/test/java/org/springframework/tests/sample/beans/Person.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Person.java index 38e0903e2e6e..d57a8ca4267f 100644 --- a/spring-aop/src/test/java/org/springframework/tests/sample/beans/Person.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Person.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/Pet.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Pet.java similarity index 91% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/Pet.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Pet.java index f280ed25bc99..661eff92feb7 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/Pet.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/Pet.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * @author Rob Harrop diff --git a/spring-aop/src/test/java/org/springframework/tests/sample/beans/SerializablePerson.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/SerializablePerson.java similarity index 93% rename from spring-aop/src/test/java/org/springframework/tests/sample/beans/SerializablePerson.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/SerializablePerson.java index bfa856144a52..6f9436906cc1 100644 --- a/spring-aop/src/test/java/org/springframework/tests/sample/beans/SerializablePerson.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/SerializablePerson.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.io.Serializable; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/SideEffectBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/SideEffectBean.java similarity index 89% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/SideEffectBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/SideEffectBean.java index 9477a5e54887..36911bdedcf1 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/SideEffectBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/SideEffectBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; /** * Bean that changes state on a business invocation, so that diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestAnnotation.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/TestAnnotation.java similarity index 87% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/TestAnnotation.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/TestAnnotation.java index 5e64245ca050..379c87419b5f 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestAnnotation.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/TestAnnotation.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/TestBean.java similarity index 95% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/TestBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/TestBean.java index f1ee1d4df5db..ca3850a220a6 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/TestBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/TestBean.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; import java.io.IOException; import java.util.ArrayList; @@ -61,8 +61,6 @@ public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOt private ITestBean spouse; - protected ITestBean[] spouses; - private String touchy; private String[] stringArray; @@ -77,7 +75,7 @@ public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOt private Date date = new Date(); - private Float myFloat = new Float(0.0); + private Float myFloat = Float.valueOf(0.0f); private Collection friends = new LinkedList<>(); @@ -423,7 +421,7 @@ public void setPets(List pets) { /** - * @see org.springframework.tests.sample.beans.ITestBean#exceptional(Throwable) + * @see org.springframework.beans.testfixture.beans.ITestBean#exceptional(Throwable) */ @Override public void exceptional(Throwable t) throws Throwable { @@ -437,7 +435,7 @@ public void unreliableFileOperation() throws IOException { throw new IOException(); } /** - * @see org.springframework.tests.sample.beans.ITestBean#returnsThis() + * @see org.springframework.beans.testfixture.beans.ITestBean#returnsThis() */ @Override public Object returnsThis() { @@ -445,7 +443,7 @@ public Object returnsThis() { } /** - * @see org.springframework.tests.sample.beans.IOther#absquatulate() + * @see org.springframework.beans.testfixture.beans.IOther#absquatulate() */ @Override public void absquatulate() { diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/factory/DummyFactory.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/DummyFactory.java similarity index 96% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/factory/DummyFactory.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/DummyFactory.java index f7bcd0ae57aa..9e3d8b6511a2 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/factory/DummyFactory.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/DummyFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans.factory; +package org.springframework.beans.testfixture.beans.factory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; @@ -24,7 +24,7 @@ import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; /** * Simple factory to allow testing of FactoryBean support in AbstractBeanFactory. diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/package-info.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/package-info.java similarity index 56% rename from spring-beans/src/test/java/org/springframework/tests/sample/beans/package-info.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/package-info.java index 575bd1933f9b..14a1870d42b7 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/package-info.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/package-info.java @@ -1,4 +1,4 @@ /** * General purpose sample beans that can be used with tests. */ -package org.springframework.tests.sample.beans; +package org.springframework.beans.testfixture.beans; diff --git a/spring-aop/src/test/java/org/springframework/tests/sample/beans/subpkg/DeepBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/subpkg/DeepBean.java similarity index 68% rename from spring-aop/src/test/java/org/springframework/tests/sample/beans/subpkg/DeepBean.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/subpkg/DeepBean.java index 45546b4cfde1..50fc891ad832 100644 --- a/spring-aop/src/test/java/org/springframework/tests/sample/beans/subpkg/DeepBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/subpkg/DeepBean.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,14 +14,12 @@ * limitations under the License. */ -package org.springframework.tests.sample.beans.subpkg; - -import org.springframework.aop.aspectj.AspectJExpressionPointcutTests; +package org.springframework.beans.testfixture.beans.subpkg; /** * Used for testing pointcut matching. * - * @see AspectJExpressionPointcutTests#testWithinRootAndSubpackages() + * @see org.springframework.aop.aspectj.AspectJExpressionPointcutTests#testWithinRootAndSubpackages() * * @author Chris Beams */ diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/AbstractBeanFactoryTests.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/factory/xml/AbstractBeanFactoryTests.java similarity index 50% rename from spring-beans/src/test/java/org/springframework/beans/factory/xml/AbstractBeanFactoryTests.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/factory/xml/AbstractBeanFactoryTests.java index 64d96f0b1688..307a9d64c12d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/AbstractBeanFactoryTests.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/factory/xml/AbstractBeanFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,27 +14,29 @@ * limitations under the License. */ -package org.springframework.beans.factory.xml; +package org.springframework.beans.testfixture.factory.xml; import java.beans.PropertyEditorSupport; import java.util.StringTokenizer; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeansException; -import org.springframework.beans.PropertyBatchUpdateException; +import org.springframework.beans.TypeMismatchException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanIsNotAFactoryException; import org.springframework.beans.factory.BeanNotOfRequiredTypeException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.tests.sample.beans.LifecycleBean; -import org.springframework.tests.sample.beans.MustBeInitialized; -import org.springframework.tests.sample.beans.TestBean; -import org.springframework.tests.sample.beans.factory.DummyFactory; +import org.springframework.beans.testfixture.beans.LifecycleBean; +import org.springframework.beans.testfixture.beans.MustBeInitialized; +import org.springframework.beans.testfixture.beans.TestBean; +import org.springframework.beans.testfixture.beans.factory.DummyFactory; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Subclasses must initialize the bean factory and any other variables they need. @@ -52,20 +54,21 @@ public abstract class AbstractBeanFactoryTests { */ @Test public void inheritance() { - assertTrue(getBeanFactory().containsBean("rod")); - assertTrue(getBeanFactory().containsBean("roderick")); + assertThat(getBeanFactory().containsBean("rod")).isTrue(); + assertThat(getBeanFactory().containsBean("roderick")).isTrue(); TestBean rod = (TestBean) getBeanFactory().getBean("rod"); TestBean roderick = (TestBean) getBeanFactory().getBean("roderick"); - assertTrue("not == ", rod != roderick); - assertTrue("rod.name is Rod", rod.getName().equals("Rod")); - assertTrue("rod.age is 31", rod.getAge() == 31); - assertTrue("roderick.name is Roderick", roderick.getName().equals("Roderick")); - assertTrue("roderick.age was inherited", roderick.getAge() == rod.getAge()); + assertThat(rod != roderick).as("not == ").isTrue(); + assertThat(rod.getName().equals("Rod")).as("rod.name is Rod").isTrue(); + assertThat(rod.getAge() == 31).as("rod.age is 31").isTrue(); + assertThat(roderick.getName().equals("Roderick")).as("roderick.name is Roderick").isTrue(); + assertThat(roderick.getAge() == rod.getAge()).as("roderick.age was inherited").isTrue(); } - @Test(expected = IllegalArgumentException.class) + @Test public void getBeanWithNullArg() { - getBeanFactory().getBean((String) null); + assertThatIllegalArgumentException().isThrownBy(() -> + getBeanFactory().getBean((String) null)); } /** @@ -86,177 +89,171 @@ public void initializingBeanCallback() { @Test public void lifecycleCallbacks() { LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle"); - assertEquals("lifecycle", lb.getBeanName()); + assertThat(lb.getBeanName()).isEqualTo("lifecycle"); // The dummy business method will throw an exception if the // necessary callbacks weren't invoked in the right order. lb.businessMethod(); - assertTrue("Not destroyed", !lb.isDestroyed()); + boolean condition = !lb.isDestroyed(); + assertThat(condition).as("Not destroyed").isTrue(); } @Test public void findsValidInstance() { Object o = getBeanFactory().getBean("rod"); - assertTrue("Rod bean is a TestBean", o instanceof TestBean); + boolean condition = o instanceof TestBean; + assertThat(condition).as("Rod bean is a TestBean").isTrue(); TestBean rod = (TestBean) o; - assertTrue("rod.name is Rod", rod.getName().equals("Rod")); - assertTrue("rod.age is 31", rod.getAge() == 31); + assertThat(rod.getName().equals("Rod")).as("rod.name is Rod").isTrue(); + assertThat(rod.getAge() == 31).as("rod.age is 31").isTrue(); } @Test public void getInstanceByMatchingClass() { Object o = getBeanFactory().getBean("rod", TestBean.class); - assertTrue("Rod bean is a TestBean", o instanceof TestBean); + boolean condition = o instanceof TestBean; + assertThat(condition).as("Rod bean is a TestBean").isTrue(); } @Test public void getInstanceByNonmatchingClass() { - try { - getBeanFactory().getBean("rod", BeanFactory.class); - fail("Rod bean is not of type BeanFactory; getBeanInstance(rod, BeanFactory.class) should throw BeanNotOfRequiredTypeException"); - } - catch (BeanNotOfRequiredTypeException ex) { - // So far, so good - assertTrue("Exception has correct bean name", ex.getBeanName().equals("rod")); - assertTrue("Exception requiredType must be BeanFactory.class", ex.getRequiredType().equals(BeanFactory.class)); - assertTrue("Exception actualType as TestBean.class", TestBean.class.isAssignableFrom(ex.getActualType())); - assertTrue("Actual type is correct", ex.getActualType() == getBeanFactory().getBean("rod").getClass()); - } + assertThatExceptionOfType(BeanNotOfRequiredTypeException.class).isThrownBy(() -> + getBeanFactory().getBean("rod", BeanFactory.class)) + .satisfies(ex -> { + assertThat(ex.getBeanName()).isEqualTo("rod"); + assertThat(ex.getRequiredType()).isEqualTo(BeanFactory.class); + assertThat(ex.getActualType()).isEqualTo(TestBean.class).isEqualTo(getBeanFactory().getBean("rod").getClass()); + }); } @Test public void getSharedInstanceByMatchingClass() { Object o = getBeanFactory().getBean("rod", TestBean.class); - assertTrue("Rod bean is a TestBean", o instanceof TestBean); + boolean condition = o instanceof TestBean; + assertThat(condition).as("Rod bean is a TestBean").isTrue(); } @Test public void getSharedInstanceByMatchingClassNoCatch() { Object o = getBeanFactory().getBean("rod", TestBean.class); - assertTrue("Rod bean is a TestBean", o instanceof TestBean); + boolean condition = o instanceof TestBean; + assertThat(condition).as("Rod bean is a TestBean").isTrue(); } @Test public void getSharedInstanceByNonmatchingClass() { - try { - getBeanFactory().getBean("rod", BeanFactory.class); - fail("Rod bean is not of type BeanFactory; getBeanInstance(rod, BeanFactory.class) should throw BeanNotOfRequiredTypeException"); - } - catch (BeanNotOfRequiredTypeException ex) { - // So far, so good - assertTrue("Exception has correct bean name", ex.getBeanName().equals("rod")); - assertTrue("Exception requiredType must be BeanFactory.class", ex.getRequiredType().equals(BeanFactory.class)); - assertTrue("Exception actualType as TestBean.class", TestBean.class.isAssignableFrom(ex.getActualType())); - } + assertThatExceptionOfType(BeanNotOfRequiredTypeException.class).isThrownBy(() -> + getBeanFactory().getBean("rod", BeanFactory.class)) + .satisfies(ex -> { + assertThat(ex.getBeanName()).isEqualTo("rod"); + assertThat(ex.getRequiredType()).isEqualTo(BeanFactory.class); + assertThat(ex.getActualType()).isEqualTo(TestBean.class); + }); } @Test public void sharedInstancesAreEqual() { Object o = getBeanFactory().getBean("rod"); - assertTrue("Rod bean1 is a TestBean", o instanceof TestBean); + boolean condition1 = o instanceof TestBean; + assertThat(condition1).as("Rod bean1 is a TestBean").isTrue(); Object o1 = getBeanFactory().getBean("rod"); - assertTrue("Rod bean2 is a TestBean", o1 instanceof TestBean); - assertTrue("Object equals applies", o == o1); + boolean condition = o1 instanceof TestBean; + assertThat(condition).as("Rod bean2 is a TestBean").isTrue(); + assertThat(o == o1).as("Object equals applies").isTrue(); } @Test public void prototypeInstancesAreIndependent() { TestBean tb1 = (TestBean) getBeanFactory().getBean("kathy"); TestBean tb2 = (TestBean) getBeanFactory().getBean("kathy"); - assertTrue("ref equal DOES NOT apply", tb1 != tb2); - assertTrue("object equal true", tb1.equals(tb2)); + assertThat(tb1 != tb2).as("ref equal DOES NOT apply").isTrue(); + assertThat(tb1.equals(tb2)).as("object equal true").isTrue(); tb1.setAge(1); tb2.setAge(2); - assertTrue("1 age independent = 1", tb1.getAge() == 1); - assertTrue("2 age independent = 2", tb2.getAge() == 2); - assertTrue("object equal now false", !tb1.equals(tb2)); + assertThat(tb1.getAge() == 1).as("1 age independent = 1").isTrue(); + assertThat(tb2.getAge() == 2).as("2 age independent = 2").isTrue(); + boolean condition = !tb1.equals(tb2); + assertThat(condition).as("object equal now false").isTrue(); } - @Test(expected = BeansException.class) + @Test public void notThere() { - assertFalse(getBeanFactory().containsBean("Mr Squiggle")); - getBeanFactory().getBean("Mr Squiggle"); + assertThat(getBeanFactory().containsBean("Mr Squiggle")).isFalse(); + assertThatExceptionOfType(BeansException.class).isThrownBy(() -> + getBeanFactory().getBean("Mr Squiggle")); } @Test public void validEmpty() { Object o = getBeanFactory().getBean("validEmpty"); - assertTrue("validEmpty bean is a TestBean", o instanceof TestBean); + boolean condition = o instanceof TestBean; + assertThat(condition).as("validEmpty bean is a TestBean").isTrue(); TestBean ve = (TestBean) o; - assertTrue("Valid empty has defaults", ve.getName() == null && ve.getAge() == 0 && ve.getSpouse() == null); + assertThat(ve.getName() == null && ve.getAge() == 0 && ve.getSpouse() == null).as("Valid empty has defaults").isTrue(); } - public void xtestTypeMismatch() { - try { - getBeanFactory().getBean("typeMismatch"); - fail("Shouldn't succeed with type mismatch"); - } - catch (BeanCreationException wex) { - assertEquals("typeMismatch", wex.getBeanName()); - assertTrue(wex.getCause() instanceof PropertyBatchUpdateException); - PropertyBatchUpdateException ex = (PropertyBatchUpdateException) wex.getCause(); - // Further tests - assertTrue("Has one error ", ex.getExceptionCount() == 1); - assertTrue("Error is for field age", ex.getPropertyAccessException("age") != null); - assertTrue("We have rejected age in exception", ex.getPropertyAccessException("age").getPropertyChangeEvent().getNewValue().equals("34x")); - } + @Test + public void typeMismatch() { + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> getBeanFactory().getBean("typeMismatch")) + .withCauseInstanceOf(TypeMismatchException.class); } @Test public void grandparentDefinitionFoundInBeanFactory() throws Exception { TestBean dad = (TestBean) getBeanFactory().getBean("father"); - assertTrue("Dad has correct name", dad.getName().equals("Albert")); + assertThat(dad.getName().equals("Albert")).as("Dad has correct name").isTrue(); } @Test public void factorySingleton() throws Exception { - assertTrue(getBeanFactory().isSingleton("&singletonFactory")); - assertTrue(getBeanFactory().isSingleton("singletonFactory")); + assertThat(getBeanFactory().isSingleton("&singletonFactory")).isTrue(); + assertThat(getBeanFactory().isSingleton("singletonFactory")).isTrue(); TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory"); - assertTrue("Singleton from factory has correct name, not " + tb.getName(), tb.getName().equals(DummyFactory.SINGLETON_NAME)); + assertThat(tb.getName().equals(DummyFactory.SINGLETON_NAME)).as("Singleton from factory has correct name, not " + tb.getName()).isTrue(); DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory"); TestBean tb2 = (TestBean) getBeanFactory().getBean("singletonFactory"); - assertTrue("Singleton references ==", tb == tb2); - assertTrue("FactoryBean is BeanFactoryAware", factory.getBeanFactory() != null); + assertThat(tb == tb2).as("Singleton references ==").isTrue(); + assertThat(factory.getBeanFactory() != null).as("FactoryBean is BeanFactoryAware").isTrue(); } @Test public void factoryPrototype() throws Exception { - assertTrue(getBeanFactory().isSingleton("&prototypeFactory")); - assertFalse(getBeanFactory().isSingleton("prototypeFactory")); + assertThat(getBeanFactory().isSingleton("&prototypeFactory")).isTrue(); + assertThat(getBeanFactory().isSingleton("prototypeFactory")).isFalse(); TestBean tb = (TestBean) getBeanFactory().getBean("prototypeFactory"); - assertTrue(!tb.getName().equals(DummyFactory.SINGLETON_NAME)); + boolean condition = !tb.getName().equals(DummyFactory.SINGLETON_NAME); + assertThat(condition).isTrue(); TestBean tb2 = (TestBean) getBeanFactory().getBean("prototypeFactory"); - assertTrue("Prototype references !=", tb != tb2); + assertThat(tb != tb2).as("Prototype references !=").isTrue(); } /** * Check that we can get the factory bean itself. * This is only possible if we're dealing with a factory - * @throws Exception */ @Test public void getFactoryItself() throws Exception { - assertNotNull(getBeanFactory().getBean("&singletonFactory")); + assertThat(getBeanFactory().getBean("&singletonFactory")).isNotNull(); } /** * Check that afterPropertiesSet gets called on factory - * @throws Exception */ @Test public void factoryIsInitialized() throws Exception { TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory"); - assertNotNull(tb); + assertThat(tb).isNotNull(); DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory"); - assertTrue("Factory was initialized because it implemented InitializingBean", factory.wasInitialized()); + assertThat(factory.wasInitialized()).as("Factory was initialized because it implemented InitializingBean").isTrue(); } /** * It should be illegal to dereference a normal bean as a factory. */ - @Test(expected = BeanIsNotAFactoryException.class) + @Test public void rejectsFactoryGetOnNormalBean() { - getBeanFactory().getBean("&rod"); + assertThatExceptionOfType(BeanIsNotAFactoryException.class).isThrownBy(() -> + getBeanFactory().getBean("&rod")); } // TODO: refactor in AbstractBeanFactory (tests for AbstractBeanFactory) @@ -270,20 +267,16 @@ public void aliasing() { ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) bf; String alias = "rods alias"; - try { - cbf.getBean(alias); - fail("Shouldn't permit factory get on normal bean"); - } - catch (NoSuchBeanDefinitionException ex) { - // Ok - assertTrue(alias.equals(ex.getBeanName())); - } + + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> + cbf.getBean(alias)) + .satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo(alias)); // Create alias cbf.registerAlias("rod", alias); Object rod = getBeanFactory().getBean("rod"); Object aliasRod = getBeanFactory().getBean(alias); - assertTrue(rod == aliasRod); + assertThat(rod == aliasRod).isTrue(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/AbstractListableBeanFactoryTests.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/factory/xml/AbstractListableBeanFactoryTests.java similarity index 61% rename from spring-beans/src/test/java/org/springframework/beans/factory/xml/AbstractListableBeanFactoryTests.java rename to spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/factory/xml/AbstractListableBeanFactoryTests.java index b0a9ea0771d0..0717f8a49366 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/AbstractListableBeanFactoryTests.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/factory/xml/AbstractListableBeanFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,16 +14,16 @@ * limitations under the License. */ -package org.springframework.beans.factory.xml; +package org.springframework.beans.testfixture.factory.xml; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.ListableBeanFactory; -import org.springframework.tests.sample.beans.TestBean; +import org.springframework.beans.testfixture.beans.TestBean; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rod Johnson @@ -50,25 +50,24 @@ public void count() { protected final void assertCount(int count) { String[] defnames = getListableBeanFactory().getBeanDefinitionNames(); - assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count); + assertThat(defnames.length == count).as("We should have " + count + " beans, not " + defnames.length).isTrue(); } protected void assertTestBeanCount(int count) { String[] defNames = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, false); - assertTrue("We should have " + count + " beans for class org.springframework.tests.sample.beans.TestBean, not " + - defNames.length, defNames.length == count); + assertThat(defNames.length == count).as("We should have " + count + " beans for class org.springframework.beans.testfixture.beans.TestBean, not " + + defNames.length).isTrue(); int countIncludingFactoryBeans = count + 2; String[] names = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, true); - assertTrue("We should have " + countIncludingFactoryBeans + - " beans for class org.springframework.tests.sample.beans.TestBean, not " + names.length, - names.length == countIncludingFactoryBeans); + assertThat(names.length == countIncludingFactoryBeans).as("We should have " + countIncludingFactoryBeans + + " beans for class org.springframework.beans.testfixture.beans.TestBean, not " + names.length).isTrue(); } @Test public void getDefinitionsForNoSuchClass() { String[] defnames = getListableBeanFactory().getBeanNamesForType(String.class); - assertTrue("No string definitions", defnames.length == 0); + assertThat(defnames.length == 0).as("No string definitions").isTrue(); } /** @@ -77,19 +76,17 @@ public void getDefinitionsForNoSuchClass() { */ @Test public void getCountForFactoryClass() { - assertTrue("Should have 2 factories, not " + - getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length, - getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2); + assertThat(getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2).as("Should have 2 factories, not " + + getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length).isTrue(); - assertTrue("Should have 2 factories, not " + - getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length, - getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2); + assertThat(getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2).as("Should have 2 factories, not " + + getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length).isTrue(); } @Test public void containsBeanDefinition() { - assertTrue(getListableBeanFactory().containsBeanDefinition("rod")); - assertTrue(getListableBeanFactory().containsBeanDefinition("roderick")); + assertThat(getListableBeanFactory().containsBeanDefinition("rod")).isTrue(); + assertThat(getListableBeanFactory().containsBeanDefinition("roderick")).isTrue(); } } diff --git a/spring-context-indexer/spring-context-indexer.gradle b/spring-context-indexer/spring-context-indexer.gradle index 2b68cd8a513a..a9769ada026a 100644 --- a/spring-context-indexer/spring-context-indexer.gradle +++ b/spring-context-indexer/spring-context-indexer.gradle @@ -2,7 +2,8 @@ description = "Spring Context Indexer" dependencies { testCompile(project(":spring-context")) - testCompile("javax.inject:javax.inject:1") - testCompile("javax.annotation:javax.annotation-api:1.3.2") - testCompile("org.eclipse.persistence:javax.persistence:2.2.0") + testCompile("javax.inject:javax.inject") + testCompile("javax.annotation:javax.annotation-api") + testCompile("javax.transaction:javax.transaction-api") + testCompile("org.eclipse.persistence:javax.persistence") } diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsIndexer.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsIndexer.java index 210fbe30d9f6..6fd4bda2bf44 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsIndexer.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsIndexer.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -23,6 +23,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; + import javax.annotation.processing.Completion; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.Processor; diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsMetadata.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsMetadata.java index 8becf0f64a33..232207d4caba 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsMetadata.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/CandidateComponentsMetadata.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/IndexedStereotypesProvider.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/IndexedStereotypesProvider.java index 92712ba07d49..c8ef2b751851 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/IndexedStereotypesProvider.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/IndexedStereotypesProvider.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -19,6 +19,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; + import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/ItemMetadata.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/ItemMetadata.java index 494e63a2a76f..2106326793af 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/ItemMetadata.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/ItemMetadata.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataCollector.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataCollector.java index a4e69af1f8f6..c1442e32677a 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataCollector.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataCollector.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -20,6 +20,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; + import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.element.Element; diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java index 872f7a276607..c00f682b77ea 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/MetadataStore.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; + import javax.annotation.processing.ProcessingEnvironment; import javax.tools.FileObject; import javax.tools.StandardLocation; diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PackageInfoStereotypesProvider.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PackageInfoStereotypesProvider.java index 62a789e3f360..d35b4370c27b 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PackageInfoStereotypesProvider.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PackageInfoStereotypesProvider.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,6 +18,7 @@ import java.util.HashSet; import java.util.Set; + import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java index 701eccc01e8c..84a8d838c921 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -28,14 +28,15 @@ * Marshaller to write {@link CandidateComponentsMetadata} as properties. * * @author Stephane Nicoll + * @author Vedran Pavic * @since 5.0 */ abstract class PropertiesMarshaller { public static void write(CandidateComponentsMetadata metadata, OutputStream out) throws IOException { - Properties props = new Properties(); + Properties props = new SortedProperties(true); metadata.getItems().forEach(m -> props.put(m.getType(), String.join(",", m.getStereotypes()))); - props.store(out, ""); + props.store(out, null); } public static CandidateComponentsMetadata read(InputStream in) throws IOException { diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/SortedProperties.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/SortedProperties.java new file mode 100644 index 000000000000..127a1a2b9614 --- /dev/null +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/SortedProperties.java @@ -0,0 +1,156 @@ +/* + * Copyright 2002-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.context.index.processor; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.StringWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.Comparator; +import java.util.Enumeration; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.Set; +import java.util.TreeSet; + +/** + * Specialization of {@link Properties} that sorts properties alphanumerically + * based on their keys. + * + *

This can be useful when storing the {@link Properties} instance in a + * properties file, since it allows such files to be generated in a repeatable + * manner with consistent ordering of properties. + * + *

Comments in generated properties files can also be optionally omitted. + * + * @author Sam Brannen + * @since 5.2 + * @see java.util.Properties + */ +@SuppressWarnings("serial") +class SortedProperties extends Properties { + + static final String EOL = System.lineSeparator(); + + private static final Comparator keyComparator = Comparator.comparing(String::valueOf); + + private static final Comparator> entryComparator = Entry.comparingByKey(keyComparator); + + + private final boolean omitComments; + + + /** + * Construct a new {@code SortedProperties} instance that honors the supplied + * {@code omitComments} flag. + * @param omitComments {@code true} if comments should be omitted when + * storing properties in a file + */ + SortedProperties(boolean omitComments) { + this.omitComments = omitComments; + } + + /** + * Construct a new {@code SortedProperties} instance with properties populated + * from the supplied {@link Properties} object and honoring the supplied + * {@code omitComments} flag. + *

Default properties from the supplied {@code Properties} object will + * not be copied. + * @param properties the {@code Properties} object from which to copy the + * initial properties + * @param omitComments {@code true} if comments should be omitted when + * storing properties in a file + */ + SortedProperties(Properties properties, boolean omitComments) { + this(omitComments); + putAll(properties); + } + + + @Override + public void store(OutputStream out, String comments) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + super.store(baos, (this.omitComments ? null : comments)); + String contents = baos.toString(StandardCharsets.ISO_8859_1.name()); + for (String line : contents.split(EOL)) { + if (!(this.omitComments && line.startsWith("#"))) { + out.write((line + EOL).getBytes(StandardCharsets.ISO_8859_1)); + } + } + } + + @Override + public void store(Writer writer, String comments) throws IOException { + StringWriter stringWriter = new StringWriter(); + super.store(stringWriter, (this.omitComments ? null : comments)); + String contents = stringWriter.toString(); + for (String line : contents.split(EOL)) { + if (!(this.omitComments && line.startsWith("#"))) { + writer.write(line + EOL); + } + } + } + + @Override + public void storeToXML(OutputStream out, String comments) throws IOException { + super.storeToXML(out, (this.omitComments ? null : comments)); + } + + @Override + public void storeToXML(OutputStream out, String comments, String encoding) throws IOException { + super.storeToXML(out, (this.omitComments ? null : comments), encoding); + } + + /** + * Return a sorted enumeration of the keys in this {@link Properties} object. + * @see #keySet() + */ + @Override + public synchronized Enumeration keys() { + return Collections.enumeration(keySet()); + } + + /** + * Return a sorted set of the keys in this {@link Properties} object. + *

The keys will be converted to strings if necessary using + * {@link String#valueOf(Object)} and sorted alphanumerically according to + * the natural order of strings. + */ + @Override + public Set keySet() { + Set sortedKeys = new TreeSet<>(keyComparator); + sortedKeys.addAll(super.keySet()); + return Collections.synchronizedSet(sortedKeys); + } + + /** + * Return a sorted set of the entries in this {@link Properties} object. + *

The entries will be sorted based on their keys, and the keys will be + * converted to strings if necessary using {@link String#valueOf(Object)} + * and compared alphanumerically according to the natural order of strings. + */ + @Override + public Set> entrySet() { + Set> sortedEntries = new TreeSet<>(entryComparator); + sortedEntries.addAll(super.entrySet()); + return Collections.synchronizedSet(sortedEntries); + } + +} diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StandardStereotypesProvider.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StandardStereotypesProvider.java index e7480cce6deb..378343c6a33c 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StandardStereotypesProvider.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StandardStereotypesProvider.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,6 +18,7 @@ import java.util.LinkedHashSet; import java.util.Set; + import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StereotypesProvider.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StereotypesProvider.java index a79568bdd39d..e0e5f7a7422c 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StereotypesProvider.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/StereotypesProvider.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.context.index.processor; import java.util.Set; + import javax.lang.model.element.Element; /** diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/TypeHelper.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/TypeHelper.java index 8026d86a07a6..7daaeece334a 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/TypeHelper.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/TypeHelper.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,7 +17,9 @@ package org.springframework.context.index.processor; import java.util.ArrayList; +import java.util.Collections; import java.util.List; + import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; @@ -110,7 +112,13 @@ public List getDirectInterfaces(Element element) { } public List getAllAnnotationMirrors(Element e) { - return this.env.getElementUtils().getAllAnnotationMirrors(e); + try { + return this.env.getElementUtils().getAllAnnotationMirrors(e); + } + catch (Exception ex) { + // This may fail if one of the annotations is not available. + return Collections.emptyList(); + } } } diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java index 2d45a60710fd..aa1a21d7367c 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,23 +19,25 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Path; + import javax.annotation.ManagedBean; import javax.inject.Named; import javax.persistence.Converter; import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.MappedSuperclass; +import javax.transaction.Transactional; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.springframework.context.index.sample.AbstractController; import org.springframework.context.index.sample.MetaControllerIndexed; import org.springframework.context.index.sample.SampleComponent; import org.springframework.context.index.sample.SampleController; +import org.springframework.context.index.sample.SampleEmbedded; import org.springframework.context.index.sample.SampleMetaController; import org.springframework.context.index.sample.SampleMetaIndexedController; import org.springframework.context.index.sample.SampleNonStaticEmbedded; @@ -44,9 +46,9 @@ import org.springframework.context.index.sample.SampleService; import org.springframework.context.index.sample.cdi.SampleManagedBean; import org.springframework.context.index.sample.cdi.SampleNamed; +import org.springframework.context.index.sample.cdi.SampleTransactional; import org.springframework.context.index.sample.jpa.SampleConverter; import org.springframework.context.index.sample.jpa.SampleEmbeddable; -import org.springframework.context.index.sample.SampleEmbedded; import org.springframework.context.index.sample.jpa.SampleEntity; import org.springframework.context.index.sample.jpa.SampleMappedSuperClass; import org.springframework.context.index.sample.type.Repo; @@ -59,208 +61,202 @@ import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.springframework.context.index.processor.Metadata.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link CandidateComponentsIndexer}. * * @author Stephane Nicoll + * @author Vedran Pavic + * @author Sam Brannen */ -public class CandidateComponentsIndexerTests { +class CandidateComponentsIndexerTests { private TestCompiler compiler; - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Before - public void createCompiler() throws IOException { - this.compiler = new TestCompiler(this.temporaryFolder); + @BeforeEach + void createCompiler(@TempDir Path tempDir) throws IOException { + this.compiler = new TestCompiler(tempDir); } - @Test - public void noCandidate() throws IOException { + void noCandidate() { CandidateComponentsMetadata metadata = compile(SampleNone.class); - assertThat(metadata.getItems(), hasSize(0)); + assertThat(metadata.getItems()).hasSize(0); } @Test - public void noAnnotation() throws IOException { + void noAnnotation() { CandidateComponentsMetadata metadata = compile(CandidateComponentsIndexerTests.class); - assertThat(metadata.getItems(), hasSize(0)); + assertThat(metadata.getItems()).hasSize(0); } @Test - public void stereotypeComponent() throws IOException { + void stereotypeComponent() { testComponent(SampleComponent.class); } @Test - public void stereotypeService() throws IOException { + void stereotypeService() { testComponent(SampleService.class); } @Test - public void stereotypeController() throws IOException { + void stereotypeController() { testComponent(SampleController.class); } @Test - public void stereotypeControllerMetaAnnotation() throws IOException { + void stereotypeControllerMetaAnnotation() { testComponent(SampleMetaController.class); } @Test - public void stereotypeRepository() throws IOException { + void stereotypeRepository() { testSingleComponent(SampleRepository.class, Component.class); } @Test - public void stereotypeControllerMetaIndex() throws IOException { - testSingleComponent(SampleMetaIndexedController.class, - Component.class, MetaControllerIndexed.class); + void stereotypeControllerMetaIndex() { + testSingleComponent(SampleMetaIndexedController.class, Component.class, MetaControllerIndexed.class); } @Test - public void stereotypeOnAbstractClass() throws IOException { + void stereotypeOnAbstractClass() { testComponent(AbstractController.class); } @Test - public void cdiManagedBean() throws IOException { + void cdiManagedBean() { testSingleComponent(SampleManagedBean.class, ManagedBean.class); } @Test - public void cdiNamed() throws IOException { + void cdiNamed() { testSingleComponent(SampleNamed.class, Named.class); } @Test - public void persistenceEntity() throws IOException { + void cdiTransactional() { + testSingleComponent(SampleTransactional.class, Transactional.class); + } + + @Test + void persistenceEntity() { testSingleComponent(SampleEntity.class, Entity.class); } @Test - public void persistenceMappedSuperClass() throws IOException { + void persistenceMappedSuperClass() { testSingleComponent(SampleMappedSuperClass.class, MappedSuperclass.class); } @Test - public void persistenceEmbeddable() throws IOException { + void persistenceEmbeddable() { testSingleComponent(SampleEmbeddable.class, Embeddable.class); } @Test - public void persistenceConverter() throws IOException { + void persistenceConverter() { testSingleComponent(SampleConverter.class, Converter.class); } @Test - public void packageInfo() throws IOException { - CandidateComponentsMetadata metadata = compile( - "org/springframework/context/index/sample/jpa/package-info"); - assertThat(metadata, hasComponent( - "org.springframework.context.index.sample.jpa", "package-info")); + void packageInfo() { + CandidateComponentsMetadata metadata = compile("org/springframework/context/index/sample/jpa/package-info"); + assertThat(metadata).has(Metadata.of("org.springframework.context.index.sample.jpa", "package-info")); } @Test - public void typeStereotypeFromMetaInterface() throws IOException { + void typeStereotypeFromMetaInterface() { testSingleComponent(SampleSpecializedRepo.class, Repo.class); } @Test - public void typeStereotypeFromInterfaceFromSuperClass() throws IOException { + void typeStereotypeFromInterfaceFromSuperClass() { testSingleComponent(SampleRepo.class, Repo.class); } @Test - public void typeStereotypeFromSeveralInterfaces() throws IOException { + void typeStereotypeFromSeveralInterfaces() { testSingleComponent(SampleSmartRepo.class, Repo.class, SmartRepo.class); } @Test - public void typeStereotypeOnInterface() throws IOException { + void typeStereotypeOnInterface() { testSingleComponent(SpecializedRepo.class, Repo.class); } @Test - public void typeStereotypeOnInterfaceFromSeveralInterfaces() throws IOException { + void typeStereotypeOnInterfaceFromSeveralInterfaces() { testSingleComponent(SmartRepo.class, Repo.class, SmartRepo.class); } @Test - public void typeStereotypeOnIndexedInterface() throws IOException { + void typeStereotypeOnIndexedInterface() { testSingleComponent(Repo.class, Repo.class); } @Test - public void embeddedCandidatesAreDetected() + void embeddedCandidatesAreDetected() throws IOException, ClassNotFoundException { // Validate nested type structure String nestedType = "org.springframework.context.index.sample.SampleEmbedded.Another$AnotherPublicCandidate"; Class type = ClassUtils.forName(nestedType, getClass().getClassLoader()); - assertThat(type, sameInstance(SampleEmbedded.Another.AnotherPublicCandidate.class)); + assertThat(type).isSameAs(SampleEmbedded.Another.AnotherPublicCandidate.class); CandidateComponentsMetadata metadata = compile(SampleEmbedded.class); - assertThat(metadata, hasComponent( - SampleEmbedded.PublicCandidate.class, Component.class)); - assertThat(metadata, hasComponent(nestedType, Component.class.getName())); - assertThat(metadata.getItems(), hasSize(2)); + assertThat(metadata).has(Metadata.of(SampleEmbedded.PublicCandidate.class, Component.class)); + assertThat(metadata).has(Metadata.of(nestedType, Component.class.getName())); + assertThat(metadata.getItems()).hasSize(2); } @Test - public void embeddedNonStaticCandidateAreIgnored() throws IOException { + void embeddedNonStaticCandidateAreIgnored() { CandidateComponentsMetadata metadata = compile(SampleNonStaticEmbedded.class); - assertThat(metadata.getItems(), hasSize(0)); + assertThat(metadata.getItems()).hasSize(0); } - private void testComponent(Class... classes) throws IOException { + private void testComponent(Class... classes) { CandidateComponentsMetadata metadata = compile(classes); for (Class c : classes) { - assertThat(metadata, hasComponent(c, Component.class)); + assertThat(metadata).has(Metadata.of(c, Component.class)); } - assertThat(metadata.getItems(), hasSize(classes.length)); + assertThat(metadata.getItems()).hasSize(classes.length); } - private void testSingleComponent(Class target, Class... stereotypes) throws IOException { + private void testSingleComponent(Class target, Class... stereotypes) { CandidateComponentsMetadata metadata = compile(target); - assertThat(metadata, hasComponent(target, stereotypes)); - assertThat(metadata.getItems(), hasSize(1)); + assertThat(metadata).has(Metadata.of(target, stereotypes)); + assertThat(metadata.getItems()).hasSize(1); } - private CandidateComponentsMetadata compile(Class... types) throws IOException { + private CandidateComponentsMetadata compile(Class... types) { CandidateComponentsIndexer processor = new CandidateComponentsIndexer(); this.compiler.getTask(types).call(processor); return readGeneratedMetadata(this.compiler.getOutputLocation()); } - private CandidateComponentsMetadata compile(String... types) throws IOException { + private CandidateComponentsMetadata compile(String... types) { CandidateComponentsIndexer processor = new CandidateComponentsIndexer(); this.compiler.getTask(types).call(processor); return readGeneratedMetadata(this.compiler.getOutputLocation()); } private CandidateComponentsMetadata readGeneratedMetadata(File outputLocation) { - try { - File metadataFile = new File(outputLocation, - MetadataStore.METADATA_PATH); - if (metadataFile.isFile()) { - return PropertiesMarshaller.read(new FileInputStream(metadataFile)); + File metadataFile = new File(outputLocation, MetadataStore.METADATA_PATH); + if (metadataFile.isFile()) { + try (FileInputStream fileInputStream = new FileInputStream(metadataFile)) { + CandidateComponentsMetadata metadata = PropertiesMarshaller.read(fileInputStream); + return metadata; } - else { - return new CandidateComponentsMetadata(); + catch (IOException ex) { + throw new IllegalStateException("Failed to read metadata from disk", ex); } } - catch (IOException ex) { - throw new IllegalStateException("Failed to read metadata from disk", ex); + else { + return new CandidateComponentsMetadata(); } } diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/Metadata.java b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/Metadata.java index 719740066cb7..30ca2caf1a26 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/Metadata.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/Metadata.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,84 +20,32 @@ import java.util.List; import java.util.stream.Collectors; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; +import org.assertj.core.api.Condition; /** - * Hamcrest {@link org.hamcrest.Matcher Matcher} to help test {@link CandidateComponentsMetadata}. + * AssertJ {@link Condition} to help test {@link CandidateComponentsMetadata}. * * @author Stephane Nicoll */ -public class Metadata { +class Metadata { - public static ItemMetadataMatcher hasComponent(Class type, Class... stereotypes) { - return new ItemMetadataMatcher(type.getName(), stereotypes); + public static Condition of(Class type, Class... stereotypes) { + return of(type.getName(), Arrays.stream(stereotypes).map(Class::getName).collect(Collectors.toList())); } - public static ItemMetadataMatcher hasComponent(String type, String... stereotypes) { - return new ItemMetadataMatcher(type, stereotypes); + public static Condition of(String type, String... stereotypes) { + return of(type, Arrays.asList(stereotypes)); } - - private static class ItemMetadataMatcher extends BaseMatcher { - - private final String type; - - private final List stereotypes; - - private ItemMetadataMatcher(String type, List stereotypes) { - this.type = type; - this.stereotypes = stereotypes; - } - - public ItemMetadataMatcher(String type, String... stereotypes) { - this(type, Arrays.asList(stereotypes)); - } - - public ItemMetadataMatcher(String type, Class... stereotypes) { - this(type, Arrays.stream(stereotypes) - .map(Class::getName).collect(Collectors.toList())); - } - - @Override - public boolean matches(Object value) { - if (!(value instanceof CandidateComponentsMetadata)) { - return false; - } - ItemMetadata itemMetadata = getFirstItemWithType((CandidateComponentsMetadata) value, this.type); - if (itemMetadata == null) { - return false; - } - if (this.type != null && !this.type.equals(itemMetadata.getType())) { - return false; - } - if (this.stereotypes != null) { - for (String stereotype : this.stereotypes) { - if (!itemMetadata.getStereotypes().contains(stereotype)) { - return false; - } - } - if (this.stereotypes.size() != itemMetadata.getStereotypes().size()) { - return false; - } - } - return true; - } - - private ItemMetadata getFirstItemWithType(CandidateComponentsMetadata metadata, String type) { - for (ItemMetadata item : metadata.getItems()) { - if (item.getType().equals(type)) { - return item; - } - } - return null; - } - - @Override - public void describeTo(Description description) { - description.appendText("Candidates with type ").appendValue(this.type); - description.appendText(" and stereotypes ").appendValue(this.stereotypes); - } + public static Condition of(String type, + List stereotypes) { + return new Condition<>(metadata -> { + ItemMetadata itemMetadata = metadata.getItems().stream() + .filter(item -> item.getType().equals(type)) + .findFirst().orElse(null); + return itemMetadata != null && itemMetadata.getStereotypes().size() == stereotypes.size() + && itemMetadata.getStereotypes().containsAll(stereotypes); + }, "Candidates with type %s and stereotypes %s", type, stereotypes); } } diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/PropertiesMarshallerTests.java b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/PropertiesMarshallerTests.java index 6fc2222644e8..e518841ef3a5 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/PropertiesMarshallerTests.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/PropertiesMarshallerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,19 +19,19 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashSet; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.springframework.context.index.processor.Metadata.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link PropertiesMarshaller}. * * @author Stephane Nicoll + * @author Vedran Pavic */ public class PropertiesMarshallerTests { @@ -45,9 +45,22 @@ public void readWrite() throws IOException { PropertiesMarshaller.write(metadata, outputStream); CandidateComponentsMetadata readMetadata = PropertiesMarshaller.read( new ByteArrayInputStream(outputStream.toByteArray())); - assertThat(readMetadata, hasComponent("com.foo", "first", "second")); - assertThat(readMetadata, hasComponent("com.bar", "first")); - assertThat(readMetadata.getItems(), hasSize(2)); + assertThat(readMetadata).has(Metadata.of("com.foo", "first", "second")); + assertThat(readMetadata).has(Metadata.of("com.bar", "first")); + assertThat(readMetadata.getItems()).hasSize(2); + } + + @Test + public void metadataIsWrittenDeterministically() throws IOException { + CandidateComponentsMetadata metadata = new CandidateComponentsMetadata(); + metadata.add(createItem("com.b", "type")); + metadata.add(createItem("com.c", "type")); + metadata.add(createItem("com.a", "type")); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PropertiesMarshaller.write(metadata, outputStream); + String contents = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1); + assertThat(contents.split(System.lineSeparator())).containsExactly("com.a=type", "com.b=type", "com.c=type"); } private static ItemMetadata createItem(String type, String... stereotypes) { diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/AbstractController.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/AbstractController.java index 156ecf140885..aea49652c5bf 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/AbstractController.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/AbstractController.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaController.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaController.java index 813228f7cad2..e37a765bccca 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaController.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaController.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaControllerIndexed.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaControllerIndexed.java index 9e66cbcc5e85..b67e9c70867d 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaControllerIndexed.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/MetaControllerIndexed.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleComponent.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleComponent.java index 621c7e0d6554..fbf08aa9c93e 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleComponent.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleComponent.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleController.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleController.java index e7593d0e7dec..eed0f8cb99ca 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleController.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleController.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleEmbedded.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleEmbedded.java index d7bd7cb5e495..5fd68a1a8c83 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleEmbedded.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleEmbedded.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaController.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaController.java index 314cb496323a..e474a9c43955 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaController.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaController.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaIndexedController.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaIndexedController.java index c620da58ca5d..1488c1f77848 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaIndexedController.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleMetaIndexedController.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNonStaticEmbedded.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNonStaticEmbedded.java index 5f74293c8301..4e12931d879a 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNonStaticEmbedded.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNonStaticEmbedded.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNone.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNone.java index 145e89f923d9..f81de9365b85 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNone.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleNone.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,7 +17,6 @@ package org.springframework.context.index.sample; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.core.type.Scope; /** * Candidate with no matching annotation. diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleRepository.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleRepository.java index b254ee7624aa..c850d94770f8 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleRepository.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleRepository.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleService.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleService.java index 2ed7e6025ce6..44a3e36f2166 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleService.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/SampleService.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/Scope.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/Scope.java new file mode 100644 index 000000000000..b96de6139374 --- /dev/null +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/Scope.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.context.index.sample; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Copy of the {@code @Scope} annotation for testing purposes. + */ +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface Scope { + + String value() default "singleton"; + +} diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleManagedBean.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleManagedBean.java index 54d09f1d7485..d3bf3dd8b785 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleManagedBean.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleManagedBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleNamed.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleNamed.java index 27364da26de4..20ca0342e68f 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleNamed.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleNamed.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleTransactional.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleTransactional.java new file mode 100644 index 000000000000..f104d5604e88 --- /dev/null +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleTransactional.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.context.index.sample.cdi; + +import javax.transaction.Transactional; + +/** + * Test candidate for {@link Transactional}. This verifies that the annotation processor + * can process an annotation that declares itself with an annotation that is not on the + * classpath. + * + * @author Vedran Pavic + * @author Stephane Nicoll + */ +@Transactional +public class SampleTransactional { +} diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleConverter.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleConverter.java index 25e01ca30618..129f090f5779 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleConverter.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleConverter.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEmbeddable.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEmbeddable.java index 53204e472b8a..79269507395a 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEmbeddable.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEmbeddable.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEntity.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEntity.java index b03bdf7619d0..101c3891d902 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEntity.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleEntity.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleMappedSuperClass.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleMappedSuperClass.java index 2e9227418312..73737f4e98be 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleMappedSuperClass.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/SampleMappedSuperClass.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/package-info.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/package-info.java index 6d2cd95012e6..17732bbdd543 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/package-info.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/jpa/package-info.java @@ -1,19 +1,3 @@ -/* - * Copyright 2002-2018 the original author or authors. - * - * Licensed 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. - */ - /** * Test candidate for {@code package-info}. * diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/AbstractRepo.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/AbstractRepo.java index da194359e621..3c9dcba1f5d4 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/AbstractRepo.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/AbstractRepo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/Repo.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/Repo.java index 341b28123def..e10518622d6d 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/Repo.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/Repo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleEntity.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleEntity.java index 83830aaeb389..a909a779e94a 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleEntity.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleEntity.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleRepo.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleRepo.java index 4d52f68de16c..61b402c64d23 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleRepo.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleRepo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSmartRepo.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSmartRepo.java index 755d2a15fe0e..02103cf19fc1 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSmartRepo.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSmartRepo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSpecializedRepo.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSpecializedRepo.java index fa932a36b5e5..7782cf164393 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSpecializedRepo.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SampleSpecializedRepo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SmartRepo.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SmartRepo.java index b7dc3b0a1b62..4848fd0e2265 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SmartRepo.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SmartRepo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SpecializedRepo.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SpecializedRepo.java index 4c2e31e825ab..b4751e1ab89d 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SpecializedRepo.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/type/SpecializedRepo.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/test/TestCompiler.java b/spring-context-indexer/src/test/java/org/springframework/context/index/test/TestCompiler.java index 08d2040435d7..2650c8a9f9ea 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/test/TestCompiler.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/test/TestCompiler.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,12 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; + import javax.annotation.processing.Processor; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; @@ -29,12 +31,11 @@ import javax.tools.StandardLocation; import javax.tools.ToolProvider; -import org.junit.rules.TemporaryFolder; - /** * Wrapper to make the {@link JavaCompiler} easier to use in tests. * * @author Stephane Nicoll + * @author Sam Brannen */ public class TestCompiler { @@ -47,14 +48,14 @@ public class TestCompiler { private final File outputLocation; - public TestCompiler(TemporaryFolder temporaryFolder) throws IOException { - this(ToolProvider.getSystemJavaCompiler(), temporaryFolder); + public TestCompiler(Path tempDir) throws IOException { + this(ToolProvider.getSystemJavaCompiler(), tempDir); } - public TestCompiler(JavaCompiler compiler, TemporaryFolder temporaryFolder) throws IOException { + public TestCompiler(JavaCompiler compiler, Path tempDir) throws IOException { this.compiler = compiler; this.fileManager = compiler.getStandardFileManager(null, null, null); - this.outputLocation = temporaryFolder.newFolder(); + this.outputLocation = tempDir.toFile(); Iterable temp = Collections.singletonList(this.outputLocation); this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, temp); this.fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, temp); diff --git a/spring-context-support/spring-context-support.gradle b/spring-context-support/spring-context-support.gradle index 53198409586a..f8a8631293ad 100644 --- a/spring-context-support/spring-context-support.gradle +++ b/spring-context-support/spring-context-support.gradle @@ -6,20 +6,27 @@ dependencies { compile(project(":spring-core")) optional(project(":spring-jdbc")) // for Quartz support optional(project(":spring-tx")) // for Quartz support - optional("javax.activation:javax.activation-api:1.2.0") - optional("javax.mail:javax.mail-api:1.6.2") - optional("javax.cache:cache-api:1.1.0") - optional("com.github.ben-manes.caffeine:caffeine:2.6.2") - optional("net.sf.ehcache:ehcache:2.10.4") - optional("org.quartz-scheduler:quartz:2.3.0") - optional("org.codehaus.fabric3.api:commonj:1.1.0") - optional("org.freemarker:freemarker:${freemarkerVersion}") + optional("javax.activation:javax.activation-api") + optional("javax.mail:javax.mail-api") + optional("javax.cache:cache-api") + optional("com.github.ben-manes.caffeine:caffeine") + optional("net.sf.ehcache:ehcache") + optional("org.quartz-scheduler:quartz") + optional("org.codehaus.fabric3.api:commonj") + optional("org.freemarker:freemarker") testCompile(project(":spring-context")) - testCompile("org.hsqldb:hsqldb:${hsqldbVersion}") - testCompile("org.hibernate:hibernate-validator:6.0.13.Final") - testCompile("javax.annotation:javax.annotation-api:1.3.2") - testRuntime("org.ehcache:jcache:1.0.1") - testRuntime("org.ehcache:ehcache:3.4.0") - testRuntime("org.glassfish:javax.el:3.0.1-b08") - testRuntime("com.sun.mail:javax.mail:1.6.2") + testCompile(testFixtures(project(":spring-beans"))) + testCompile(testFixtures(project(":spring-context"))) + testCompile(testFixtures(project(":spring-core"))) + testCompile(testFixtures(project(":spring-tx"))) + testCompile("org.hsqldb:hsqldb") + testCompile("org.hibernate:hibernate-validator") + testCompile("javax.annotation:javax.annotation-api") + testRuntime("org.ehcache:jcache") + testRuntime("org.ehcache:ehcache") + testRuntime("org.glassfish:javax.el") + testRuntime("com.sun.mail:javax.mail") + testFixturesApi("org.junit.jupiter:junit-jupiter-api") + testFixturesImplementation("org.assertj:assertj-core") + testFixturesImplementation("org.mockito:mockito-core") } diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java index 57e2d5e82055..a19884b19ce8 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -35,6 +35,7 @@ * @author Juergen Hoeller * @author Stephane Nicoll * @since 4.3 + * @see CaffeineCacheManager */ public class CaffeineCache extends AbstractValueAdaptingCache { @@ -123,11 +124,23 @@ public void evict(Object key) { this.cache.invalidate(key); } + @Override + public boolean evictIfPresent(Object key) { + return (this.cache.asMap().remove(key) != null); + } + @Override public void clear() { this.cache.invalidateAll(); } + @Override + public boolean invalidate() { + boolean notEmpty = !this.cache.asMap().isEmpty(); + this.cache.invalidateAll(); + return notEmpty; + } + private class PutIfAbsentFunction implements Function { diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java index bf0ec1ff4cce..d5b77cbc1bb9 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -21,7 +21,7 @@ import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CopyOnWriteArrayList; import com.github.benmanes.caffeine.cache.CacheLoader; import com.github.benmanes.caffeine.cache.Caffeine; @@ -50,15 +50,12 @@ * @author Ben Manes * @author Juergen Hoeller * @author Stephane Nicoll + * @author Sam Brannen * @since 4.3 * @see CaffeineCache */ public class CaffeineCacheManager implements CacheManager { - private final ConcurrentMap cacheMap = new ConcurrentHashMap<>(16); - - private boolean dynamic = true; - private Caffeine cacheBuilder = Caffeine.newBuilder(); @Nullable @@ -66,6 +63,12 @@ public class CaffeineCacheManager implements CacheManager { private boolean allowNullValues = true; + private boolean dynamic = true; + + private final Map cacheMap = new ConcurrentHashMap<>(16); + + private final Collection customCacheNames = new CopyOnWriteArrayList<>(); + /** * Construct a dynamic CaffeineCacheManager, @@ -134,6 +137,13 @@ public void setCacheSpecification(String cacheSpecification) { doSetCaffeine(Caffeine.from(cacheSpecification)); } + private void doSetCaffeine(Caffeine cacheBuilder) { + if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) { + this.cacheBuilder = cacheBuilder; + refreshCommonCaches(); + } + } + /** * Set the Caffeine CacheLoader to use for building each individual * {@link CaffeineCache} instance, turning it into a LoadingCache. @@ -144,7 +154,7 @@ public void setCacheSpecification(String cacheSpecification) { public void setCacheLoader(CacheLoader cacheLoader) { if (!ObjectUtils.nullSafeEquals(this.cacheLoader, cacheLoader)) { this.cacheLoader = cacheLoader; - refreshKnownCaches(); + refreshCommonCaches(); } } @@ -157,7 +167,7 @@ public void setCacheLoader(CacheLoader cacheLoader) { public void setAllowNullValues(boolean allowNullValues) { if (this.allowNullValues != allowNullValues) { this.allowNullValues = allowNullValues; - refreshKnownCaches(); + refreshCommonCaches(); } } @@ -178,55 +188,80 @@ public Collection getCacheNames() { @Override @Nullable public Cache getCache(String name) { - Cache cache = this.cacheMap.get(name); - if (cache == null && this.dynamic) { - synchronized (this.cacheMap) { - cache = this.cacheMap.get(name); - if (cache == null) { - cache = createCaffeineCache(name); - this.cacheMap.put(name, cache); - } - } - } - return cache; + return this.cacheMap.computeIfAbsent(name, cacheName -> + this.dynamic ? createCaffeineCache(cacheName) : null); } + /** - * Create a new CaffeineCache instance for the specified cache name. + * Register the given native Caffeine Cache instance with this cache manager, + * adapting it to Spring's cache API for exposure through {@link #getCache}. + * Any number of such custom caches may be registered side by side. + *

This allows for custom settings per cache (as opposed to all caches + * sharing the common settings in the cache manager's configuration) and + * is typically used with the Caffeine builder API: + * {@code registerCustomCache("myCache", Caffeine.newBuilder().maximumSize(10).build())} + *

Note that any other caches, whether statically specified through + * {@link #setCacheNames} or dynamically built on demand, still operate + * with the common settings in the cache manager's configuration. + * @param name the name of the cache + * @param cache the custom Caffeine Cache instance to register + * @since 5.2.8 + * @see #adaptCaffeineCache + */ + public void registerCustomCache(String name, com.github.benmanes.caffeine.cache.Cache cache) { + this.customCacheNames.add(name); + this.cacheMap.put(name, adaptCaffeineCache(name, cache)); + } + + /** + * Adapt the given new native Caffeine Cache instance to Spring's {@link Cache} + * abstraction for the specified cache name. * @param name the name of the cache + * @param cache the native Caffeine Cache instance * @return the Spring CaffeineCache adapter (or a decorator thereof) + * @since 5.2.8 + * @see CaffeineCache + * @see #isAllowNullValues() + */ + protected Cache adaptCaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache cache) { + return new CaffeineCache(name, cache, isAllowNullValues()); + } + + /** + * Build a common {@link CaffeineCache} instance for the specified cache name, + * using the common Caffeine configuration specified on this cache manager. + *

Delegates to {@link #adaptCaffeineCache} as the adaptation method to + * Spring's cache abstraction (allowing for centralized decoration etc), + * passing in a freshly built native Caffeine Cache instance. + * @param name the name of the cache + * @return the Spring CaffeineCache adapter (or a decorator thereof) + * @see #adaptCaffeineCache + * @see #createNativeCaffeineCache */ protected Cache createCaffeineCache(String name) { - return new CaffeineCache(name, createNativeCaffeineCache(name), isAllowNullValues()); + return adaptCaffeineCache(name, createNativeCaffeineCache(name)); } /** - * Create a native Caffeine Cache instance for the specified cache name. + * Build a common Caffeine Cache instance for the specified cache name, + * using the common Caffeine configuration specified on this cache manager. * @param name the name of the cache * @return the native Caffeine Cache instance + * @see #createCaffeineCache */ protected com.github.benmanes.caffeine.cache.Cache createNativeCaffeineCache(String name) { - if (this.cacheLoader != null) { - return this.cacheBuilder.build(this.cacheLoader); - } - else { - return this.cacheBuilder.build(); - } - } - - private void doSetCaffeine(Caffeine cacheBuilder) { - if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) { - this.cacheBuilder = cacheBuilder; - refreshKnownCaches(); - } + return (this.cacheLoader != null ? this.cacheBuilder.build(this.cacheLoader) : this.cacheBuilder.build()); } /** - * Create the known caches again with the current state of this manager. + * Recreate the common caches with the current state of this manager. */ - private void refreshKnownCaches() { + private void refreshCommonCaches() { for (Map.Entry entry : this.cacheMap.entrySet()) { - entry.setValue(createCaffeineCache(entry.getKey())); + if (!this.customCacheNames.contains(entry.getKey())) { + entry.setValue(createCaffeineCache(entry.getKey())); + } } } diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java index 743592643f87..4309fa73a2cb 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -34,6 +34,7 @@ * @author Juergen Hoeller * @author Stephane Nicoll * @since 3.1 + * @see EhCacheCacheManager */ public class EhCacheCache implements Cache { @@ -72,6 +73,19 @@ public ValueWrapper get(Object key) { return toValueWrapper(element); } + @SuppressWarnings("unchecked") + @Override + @Nullable + public T get(Object key, @Nullable Class type) { + Element element = this.cache.get(key); + Object value = (element != null ? element.getObjectValue() : null); + if (value != null && type != null && !type.isInstance(value)) { + throw new IllegalStateException( + "Cached value is not of required type [" + type.getName() + "]: " + value); + } + return (T) value; + } + @SuppressWarnings("unchecked") @Override @Nullable @@ -95,7 +109,6 @@ public T get(Object key, Callable valueLoader) { this.cache.releaseWriteLockOnKey(key); } } - } private T loadValue(Object key, Callable valueLoader) { @@ -110,19 +123,6 @@ private T loadValue(Object key, Callable valueLoader) { return value; } - @Override - @SuppressWarnings("unchecked") - @Nullable - public T get(Object key, @Nullable Class type) { - Element element = this.cache.get(key); - Object value = (element != null ? element.getObjectValue() : null); - if (value != null && type != null && !type.isInstance(value)) { - throw new IllegalStateException( - "Cached value is not of required type [" + type.getName() + "]: " + value); - } - return (T) value; - } - @Override public void put(Object key, @Nullable Object value) { this.cache.put(new Element(key, value)); @@ -140,11 +140,23 @@ public void evict(Object key) { this.cache.remove(key); } + @Override + public boolean evictIfPresent(Object key) { + return this.cache.remove(key); + } + @Override public void clear() { this.cache.removeAll(); } + @Override + public boolean invalidate() { + boolean notEmpty = (this.cache.getSize() > 0); + this.cache.removeAll(); + return notEmpty; + } + @Nullable private Element lookup(Object key) { diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java index e49645b87fac..f3e58a55b288 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -34,6 +34,7 @@ * @author Juergen Hoeller * @author Stephane Nicoll * @since 3.1 + * @see EhCacheCache */ public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManager { diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java index f4f73a5c18e2..3d4f839a3b93 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java index 4e41ae91a467..068341965ad1 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerUtils.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerUtils.java index 78f42785d531..7d6654a86acd 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerUtils.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerUtils.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/package-info.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/package-info.java index fe4f342105a6..d786a802512f 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/package-info.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/package-info.java @@ -1,6 +1,6 @@ /** * Support classes for the open source cache - * EhCache 2.x, + * EhCache 2.x, * allowing to set up an EhCache CacheManager and Caches * as beans in a Spring context. * diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java index e6920e97e937..84d2e3f9bcf2 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.cache.jcache; import java.util.concurrent.Callable; + import javax.cache.Cache; import javax.cache.processor.EntryProcessor; import javax.cache.processor.EntryProcessorException; @@ -35,6 +36,7 @@ * @author Juergen Hoeller * @author Stephane Nicoll * @since 3.2 + * @see JCacheCacheManager */ public class JCacheCache extends AbstractValueAdaptingCache { @@ -105,11 +107,23 @@ public void evict(Object key) { this.cache.remove(key); } + @Override + public boolean evictIfPresent(Object key) { + return this.cache.remove(key); + } + @Override public void clear() { this.cache.removeAll(); } + @Override + public boolean invalidate() { + boolean notEmpty = this.cache.iterator().hasNext(); + this.cache.removeAll(); + return notEmpty; + } + private class ValueLoaderEntryProcessor implements EntryProcessor { diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java index b72fcdc3e9dc..e4feb09554ba 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -18,6 +18,7 @@ import java.util.Collection; import java.util.LinkedHashSet; + import javax.cache.CacheManager; import javax.cache.Caching; @@ -35,6 +36,7 @@ * @author Juergen Hoeller * @author Stephane Nicoll * @since 3.2 + * @see JCacheCache */ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManager { diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java index cfadbe9e0363..da3a2e15669d 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,6 +18,7 @@ import java.net.URI; import java.util.Properties; + import javax.cache.CacheManager; import javax.cache.Caching; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/AbstractJCacheConfiguration.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/AbstractJCacheConfiguration.java index 9c484cd33320..34bb1a08d63e 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/AbstractJCacheConfiguration.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/AbstractJCacheConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -39,7 +39,7 @@ * @see JCacheConfigurer */ @Configuration -public class AbstractJCacheConfiguration extends AbstractCachingConfiguration { +public abstract class AbstractJCacheConfiguration extends AbstractCachingConfiguration { @Nullable protected Supplier exceptionCacheResolver; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurer.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurer.java index 1c3e333d193c..989e720aeb99 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurer.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurer.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurerSupport.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurerSupport.java index 9e55f2596b25..e36c4fb6df94 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurerSupport.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/JCacheConfigurerSupport.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/ProxyJCacheConfiguration.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/ProxyJCacheConfiguration.java index 0f8147d29099..c779bfc10ae4 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/config/ProxyJCacheConfiguration.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/config/ProxyJCacheConfiguration.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractCacheInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractCacheInterceptor.java index 849cccc3ddf4..06025fc26a13 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractCacheInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractCacheInterceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -59,7 +59,7 @@ protected abstract Object invoke(CacheOperationInvocationContext context, Cac /** * Resolve the cache to use. * @param context the invocation context - * @return the cache to use (never null) + * @return the cache to use (never {@code null}) */ protected Cache resolveCache(CacheOperationInvocationContext context) { Collection caches = context.getOperation().getCacheResolver().resolveCaches(context); @@ -73,7 +73,7 @@ protected Cache resolveCache(CacheOperationInvocationContext context) { /** * Convert the collection of caches in a single expected element. *

Throw an {@link IllegalStateException} if the collection holds more than one element - * @return the single element or {@code null} if the collection is empty + * @return the single element, or {@code null} if the collection is empty */ @Nullable static Cache extractFrom(Collection caches) { diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java index 5e31deae5cae..3edf2a2c153a 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java index 9f9f5bfa3c26..e3ecd6fa3b25 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -19,6 +19,7 @@ import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.List; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheMethodDetails; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java index e879259df0db..d1771b1bd6e3 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -24,6 +24,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheKey; import javax.cache.annotation.CacheMethodDetails; @@ -62,12 +63,15 @@ protected AbstractJCacheOperation(CacheMethodDetails methodDetails, CacheReso this.allParameterDetails = initializeAllParameterDetails(methodDetails.getMethod()); } - - /** - * Return the {@link ExceptionTypeFilter} to use to filter exceptions thrown while - * invoking the method. - */ - public abstract ExceptionTypeFilter getExceptionTypeFilter(); + private static List initializeAllParameterDetails(Method method) { + int parameterCount = method.getParameterCount(); + List result = new ArrayList<>(parameterCount); + for (int i = 0; i < parameterCount; i++) { + CacheParameterDetail detail = new CacheParameterDetail(method, i); + result.add(detail); + } + return result; + } @Override @@ -113,12 +117,25 @@ public CacheInvocationParameter[] getAllParameters(Object... values) { return result.toArray(new CacheInvocationParameter[0]); } + + /** + * Return the {@link ExceptionTypeFilter} to use to filter exceptions thrown while + * invoking the method. + * @see #createExceptionTypeFilter + */ + public abstract ExceptionTypeFilter getExceptionTypeFilter(); + + /** + * Convenience method for subclasses to create a specific {@code ExceptionTypeFilter}. + * @see #getExceptionTypeFilter() + */ protected ExceptionTypeFilter createExceptionTypeFilter( Class[] includes, Class[] excludes) { return new ExceptionTypeFilter(Arrays.asList(includes), Arrays.asList(excludes), true); } + @Override public String toString() { return getOperationDescription().append("]").toString(); @@ -137,16 +154,6 @@ protected StringBuilder getOperationDescription() { } - private static List initializeAllParameterDetails(Method method) { - List result = new ArrayList<>(); - for (int i = 0; i < method.getParameterCount(); i++) { - CacheParameterDetail detail = new CacheParameterDetail(method, i); - result.add(detail); - } - return result; - } - - /** * Details for a single cache parameter. */ diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractKeyCacheInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractKeyCacheInterceptor.java index b6a0b4353072..1539505b535b 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractKeyCacheInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractKeyCacheInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.cache.jcache.interceptor; import java.lang.annotation.Annotation; + import javax.cache.annotation.CacheKeyInvocationContext; import org.springframework.cache.interceptor.CacheErrorHandler; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AnnotationJCacheOperationSource.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AnnotationJCacheOperationSource.java index d574881141ef..b289b14715f4 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AnnotationJCacheOperationSource.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AnnotationJCacheOperationSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -20,6 +20,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; + import javax.cache.annotation.CacheDefaults; import javax.cache.annotation.CacheKeyGenerator; import javax.cache.annotation.CacheMethodDetails; @@ -212,10 +213,9 @@ protected String generateDefaultCacheName(Method method) { parameters.add(parameterType.getName()); } - StringBuilder sb = new StringBuilder(method.getDeclaringClass().getName()); - sb.append(".").append(method.getName()); - sb.append("(").append(StringUtils.collectionToCommaDelimitedString(parameters)).append(")"); - return sb.toString(); + return method.getDeclaringClass().getName() + + '.' + method.getName() + + '(' + StringUtils.collectionToCommaDelimitedString(parameters) + ')'; } private int countNonNull(Object... instances) { diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/BeanFactoryJCacheOperationSourceAdvisor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/BeanFactoryJCacheOperationSourceAdvisor.java index df81c605aa30..62ae44f03a68 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/BeanFactoryJCacheOperationSourceAdvisor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/BeanFactoryJCacheOperationSourceAdvisor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java index 9df61af08aba..d71af3244dba 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutOperation.java index 847a6ce8a7b7..efbfb65652e0 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutOperation.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,6 +18,7 @@ import java.lang.reflect.Method; import java.util.List; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CachePut; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllInterceptor.java index c6ad61794d22..66b583e37870 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllInterceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -42,7 +42,6 @@ protected Object invoke( CacheOperationInvocationContext context, CacheOperationInvoker invoker) { CacheRemoveAllOperation operation = context.getOperation(); - boolean earlyRemove = operation.isEarlyRemove(); if (earlyRemove) { removeAll(context); @@ -67,10 +66,10 @@ protected Object invoke( protected void removeAll(CacheOperationInvocationContext context) { Cache cache = resolveCache(context); if (logger.isTraceEnabled()) { - logger.trace("Invalidating entire cache '" + cache.getName() + "' for operation " - + context.getOperation()); + logger.trace("Invalidating entire cache '" + cache.getName() + "' for operation " + + context.getOperation()); } - doClear(cache); + doClear(cache, context.getOperation().isEarlyRemove()); } } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperation.java index 553d7f3dde0e..aa578266f3e5 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperation.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveEntryInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveEntryInterceptor.java index 681e6f598f8c..84852783166a 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveEntryInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveEntryInterceptor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -42,7 +42,6 @@ protected Object invoke( CacheOperationInvocationContext context, CacheOperationInvoker invoker) { CacheRemoveOperation operation = context.getOperation(); - boolean earlyRemove = operation.isEarlyRemove(); if (earlyRemove) { removeValue(context); @@ -68,10 +67,10 @@ private void removeValue(CacheOperationInvocationContext c Object key = generateKey(context); Cache cache = resolveCache(context); if (logger.isTraceEnabled()) { - logger.trace("Invalidating key [" + key + "] on cache '" + cache.getName() - + "' for operation " + context.getOperation()); + logger.trace("Invalidating key [" + key + "] on cache '" + cache.getName() + + "' for operation " + context.getOperation()); } - doEvict(cache, key); + doEvict(cache, key, context.getOperation().isEarlyRemove()); } } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperation.java index 93997701664a..5fb9a31dfdb6 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperation.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -50,8 +50,8 @@ public ExceptionTypeFilter getExceptionTypeFilter() { } /** - * Specify if the cache entry should be remove before invoking the method. By default, the - * cache entry is removed after the method invocation. + * Specify if the cache entry should be removed before invoking the method. + *

By default, the cache entry is removed after the method invocation. * @see javax.cache.annotation.CacheRemove#afterInvocation() */ public boolean isEarlyRemove() { diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java index db454f311c55..88724adc12c2 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,6 +18,7 @@ import java.util.Collection; import java.util.Collections; + import javax.cache.annotation.CacheInvocationContext; import org.springframework.cache.Cache; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java index 54e7804bbbe9..ea2f4a09eac5 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultOperation.java index 16cbe65a7a2d..3f4eedfe1020 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultOperation.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheInvocationContext.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheInvocationContext.java index 341d1398ed80..35523c2b46a4 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheInvocationContext.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheInvocationContext.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -20,6 +20,7 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.Set; + import javax.cache.annotation.CacheInvocationContext; import javax.cache.annotation.CacheInvocationParameter; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheKeyInvocationContext.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheKeyInvocationContext.java index 20513504a4ad..d1f396e97974 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheKeyInvocationContext.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheKeyInvocationContext.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.cache.jcache.interceptor; import java.lang.annotation.Annotation; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheKeyInvocationContext; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheMethodDetails.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheMethodDetails.java index 719825785e15..ca4cebf570de 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheMethodDetails.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultCacheMethodDetails.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; + import javax.cache.annotation.CacheMethodDetails; /** diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java index e5d0e758f2d5..c323cf449508 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheAspectSupport.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheAspectSupport.java index 121de7492fad..893e4a6b792b 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheAspectSupport.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheAspectSupport.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -88,6 +88,7 @@ public JCacheOperationSource getCacheOperationSource() { return this.cacheOperationSource; } + @Override public void afterPropertiesSet() { getCacheOperationSource(); diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheInterceptor.java index 532c9732739d..c4835a2c4206 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheInterceptor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperation.java index 330d236a29aa..00c5ef91e9cf 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperation.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.cache.jcache.interceptor; import java.lang.annotation.Annotation; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheMethodDetails; @@ -24,9 +25,10 @@ import org.springframework.cache.interceptor.CacheResolver; /** - * Model the base of JSR-107 cache operation. - *

A cache operation can be statically cached as it does not contain - * any runtime operation of a specific cache invocation. + * Model the base of JSR-107 cache operation through an interface contract. + * + *

A cache operation can be statically cached as it does not contain any + * runtime operation of a specific cache invocation. * * @author Stephane Nicoll * @since 4.1 diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSource.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSource.java index 43068e7fba7e..445a7ef82824 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSource.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSource.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSourcePointcut.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSourcePointcut.java index 3e4e12788096..4b6d7e0fb940 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSourcePointcut.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheOperationSourcePointcut.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -48,7 +48,7 @@ public boolean matches(Method method, Class targetClass) { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java index fb00ab22a20a..78a8c4e00a27 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -20,6 +20,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheKeyGenerator; import javax.cache.annotation.CacheKeyInvocationContext; @@ -107,7 +108,7 @@ private static Object doGenerate(KeyGenerator keyGenerator, CacheKeyInvocationCo Object value = param.getValue(); if (param.getParameterPosition() == context.getAllParameters().length - 1 && context.getMethod().isVarArgs()) { - parameters.addAll((List) CollectionUtils.arrayToList(value)); + parameters.addAll(CollectionUtils.arrayToList(value)); } else { parameters.add(value); diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/SimpleExceptionCacheResolver.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/SimpleExceptionCacheResolver.java index c636ff86f566..f2091ab1fb31 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/SimpleExceptionCacheResolver.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/SimpleExceptionCacheResolver.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManager.java index 3e7143cdf307..a65e0ca0c491 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManager.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheDecorator.java b/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheDecorator.java index bd4da577efb9..ca193ee0d8f3 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheDecorator.java +++ b/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheDecorator.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -25,14 +25,16 @@ import org.springframework.util.Assert; /** - * Cache decorator which synchronizes its {@link #put}, {@link #evict} and {@link #clear} - * operations with Spring-managed transactions (through Spring's {@link TransactionSynchronizationManager}, - * performing the actual cache put/evict/clear operation only in the after-commit phase of a - * successful transaction. If no transaction is active, {@link #put}, {@link #evict} and + * Cache decorator which synchronizes its {@link #put}, {@link #evict} and + * {@link #clear} operations with Spring-managed transactions (through Spring's + * {@link TransactionSynchronizationManager}, performing the actual cache + * put/evict/clear operation only in the after-commit phase of a successful + * transaction. If no transaction is active, {@link #put}, {@link #evict} and * {@link #clear} operations will be performed immediately, as usual. * - *

Use of more aggressive operations such as {@link #putIfAbsent} cannot be deferred - * to the after-commit phase of a running transaction. Use these with care. + *

Note: Use of immediate operations such as {@link #putIfAbsent} and + * {@link #evictIfPresent} cannot be deferred to the after-commit phase of a + * running transaction. Use these with care in a transactional environment. * * @author Juergen Hoeller * @author Stephane Nicoll @@ -54,6 +56,7 @@ public TransactionAwareCacheDecorator(Cache targetCache) { this.targetCache = targetCache; } + /** * Return the target Cache that this Cache should delegate to. */ @@ -124,6 +127,11 @@ public void afterCommit() { } } + @Override + public boolean evictIfPresent(Object key) { + return this.targetCache.evictIfPresent(key); + } + @Override public void clear() { if (TransactionSynchronizationManager.isSynchronizationActive()) { @@ -139,4 +147,9 @@ public void afterCommit() { } } + @Override + public boolean invalidate() { + return this.targetCache.invalidate(); + } + } diff --git a/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheManagerProxy.java b/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheManagerProxy.java index f20f894ea253..d6c51895f7c7 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheManagerProxy.java +++ b/spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheManagerProxy.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailAuthenticationException.java b/spring-context-support/src/main/java/org/springframework/mail/MailAuthenticationException.java index d4eb9cc60440..d4de3d17ba29 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailAuthenticationException.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailAuthenticationException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailException.java b/spring-context-support/src/main/java/org/springframework/mail/MailException.java index 243ca0ca289d..076136955822 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailException.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailMessage.java b/spring-context-support/src/main/java/org/springframework/mail/MailMessage.java index 6fd0b9951eea..3ec68c4e55dc 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailMessage.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailMessage.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailParseException.java b/spring-context-support/src/main/java/org/springframework/mail/MailParseException.java index b6b47f16131e..12290edc5016 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailParseException.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailParseException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailPreparationException.java b/spring-context-support/src/main/java/org/springframework/mail/MailPreparationException.java index d448f47f84aa..cc7901a7228d 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailPreparationException.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailPreparationException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailSendException.java b/spring-context-support/src/main/java/org/springframework/mail/MailSendException.java index d830aefab107..693082c840c2 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailSendException.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailSendException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailSender.java b/spring-context-support/src/main/java/org/springframework/mail/MailSender.java index bcccc6821fff..6fd8265f59c2 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailSender.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailSender.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/SimpleMailMessage.java b/spring-context-support/src/main/java/org/springframework/mail/SimpleMailMessage.java index 0d876549d302..95268a2993e0 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/SimpleMailMessage.java +++ b/spring-context-support/src/main/java/org/springframework/mail/SimpleMailMessage.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -222,7 +222,7 @@ public void copyTo(MailMessage target) { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } @@ -276,9 +276,7 @@ private static String[] copyOrNull(@Nullable String[] state) { } private static String[] copy(String[] state) { - String[] copy = new String[state.length]; - System.arraycopy(state, 0, copy, 0, state.length); - return copy; + return state.clone(); } } diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMap.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMap.java index a5787191e0dd..cc6933129d8e 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMap.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMap.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; + import javax.activation.FileTypeMap; import javax.activation.MimetypesFileTypeMap; diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/InternetAddressEditor.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/InternetAddressEditor.java index 1984fa86936b..7738328c563f 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/InternetAddressEditor.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/InternetAddressEditor.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.mail.javamail; import java.beans.PropertyEditorSupport; + import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java index a3bf54208e01..f810be606dd8 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSender.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.mail.javamail; import java.io.InputStream; + import javax.mail.internet.MimeMessage; import org.springframework.mail.MailException; diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java index 51cd48113cb6..9286d0ef665a 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -23,7 +23,9 @@ import java.util.List; import java.util.Map; import java.util.Properties; + import javax.activation.FileTypeMap; +import javax.mail.Address; import javax.mail.AuthenticationFailedException; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; @@ -459,7 +461,8 @@ protected void doSend(MimeMessage[] mimeMessages, @Nullable Object[] originalMes // Preserve explicitly specified message id... mimeMessage.setHeader(HEADER_MESSAGE_ID, messageId); } - transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); + Address[] addresses = mimeMessage.getAllRecipients(); + transport.sendMessage(mimeMessage, (addresses != null ? addresses : new Address[0])); } catch (Exception ex) { Object original = (originalMessages != null ? originalMessages[i] : mimeMessage); diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMailMessage.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMailMessage.java index b46ac264c70f..e62690a6d763 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMailMessage.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMailMessage.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -17,6 +17,7 @@ package org.springframework.mail.javamail; import java.util.Date; + import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java index 96e977fe7939..c9d21201a67f 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -22,6 +22,7 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Date; + import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; @@ -158,8 +159,6 @@ public class MimeMessageHelper { private static final String HEADER_PRIORITY = "X-Priority"; - private static final String HEADER_CONTENT_ID = "Content-ID"; - private final MimeMessage mimeMessage; @@ -174,6 +173,8 @@ public class MimeMessageHelper { private FileTypeMap fileTypeMap; + private boolean encodeFilenames = true; + private boolean validateAddresses = false; @@ -463,7 +464,7 @@ protected FileTypeMap getDefaultFileTypeMap(MimeMessage mimeMessage) { * Set the Java Activation Framework {@code FileTypeMap} to use * for determining the content type of inline content and attachments * that get added to the message. - *

Default is the {@code FileTypeMap} that the underlying + *

The default is the {@code FileTypeMap} that the underlying * MimeMessage carries, if any, or the Activation Framework's default * {@code FileTypeMap} instance else. * @see #addInline @@ -479,18 +480,40 @@ public void setFileTypeMap(@Nullable FileTypeMap fileTypeMap) { /** * Return the {@code FileTypeMap} used by this MimeMessageHelper. + * @see #setFileTypeMap */ public FileTypeMap getFileTypeMap() { return this.fileTypeMap; } + /** + * Set whether to encode attachment filenames passed to this helper's + * {@code #addAttachment} methods. + *

The default is {@code true} for compatibility with older email clients; + * turn this to {@code false} for standard MIME behavior. On a related note, + * check out JavaMail's {@code mail.mime.encodefilename} system property. + * @since 5.2.9 + * @see #addAttachment(String, DataSource) + * @see MimeBodyPart#setFileName(String) + */ + public void setEncodeFilenames(boolean encodeFilenames) { + this.encodeFilenames = encodeFilenames; + } + + /** + * Return whether to encode attachment filenames passed to this helper's + * {@code #addAttachment} methods. + * @since 5.2.9 + * @see #setEncodeFilenames + */ + public boolean isEncodeFilenames() { + return this.encodeFilenames; + } + /** * Set whether to validate all addresses which get passed to this helper. - * Default is "false". - *

Note that this is by default just available for JavaMail >= 1.3. - * You can override the default {@code validateAddress method} for - * validation on older JavaMail versions (or for custom validation). + *

The default is {@code false}. * @see #validateAddress */ public void setValidateAddresses(boolean validateAddresses) { @@ -499,6 +522,7 @@ public void setValidateAddresses(boolean validateAddresses) { /** * Return whether this helper will validate all addresses passed to it. + * @see #setValidateAddresses */ public boolean isValidateAddresses() { return this.validateAddresses; @@ -507,10 +531,8 @@ public boolean isValidateAddresses() { /** * Validate the given mail address. * Called by all of MimeMessageHelper's address setters and adders. - *

Default implementation invokes {@code InternetAddress.validate()}, + *

The default implementation invokes {@link InternetAddress#validate()}, * provided that address validation is activated for the helper instance. - *

Note that this method will just work on JavaMail >= 1.3. You can override - * it for validation on older JavaMail versions or for custom validation. * @param address the address to validate * @throws AddressException if validation failed * @see #isValidateAddresses() @@ -524,7 +546,8 @@ protected void validateAddress(InternetAddress address) throws AddressException /** * Validate all given mail addresses. - * Default implementation simply delegates to validateAddress for each address. + *

The default implementation simply delegates to {@link #validateAddress} + * for each address. * @param addresses the addresses to validate * @throws AddressException if validation failed * @see #validateAddress(InternetAddress) @@ -884,9 +907,7 @@ public void addInline(String contentId, DataSource dataSource) throws MessagingE Assert.notNull(dataSource, "DataSource must not be null"); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.INLINE); - // We're using setHeader here to remain compatible with JavaMail 1.2, - // rather than JavaMail 1.3's setContentID. - mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">"); + mimeBodyPart.setContentID("<" + contentId + ">"); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getMimeMultipart().addBodyPart(mimeBodyPart); } @@ -996,7 +1017,8 @@ public void addAttachment(String attachmentFilename, DataSource dataSource) thro try { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); - mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename)); + mimeBodyPart.setFileName(isEncodeFilenames() ? + MimeUtility.encodeText(attachmentFilename) : attachmentFilename); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getRootMimeMultipart().addBodyPart(mimeBodyPart); } diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessagePreparator.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessagePreparator.java index 4aa82246af31..5973d17104b6 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessagePreparator.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessagePreparator.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/SmartMimeMessage.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/SmartMimeMessage.java index 01d7ee5e91c5..2fa44c910af9 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/SmartMimeMessage.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/SmartMimeMessage.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingTimerListener.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingTimerListener.java index 95a37d00db92..d6d493e1cac1 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingTimerListener.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingTimerListener.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingWork.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingWork.java index fe5ad3b6873a..9f7eb9110ae8 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingWork.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/DelegatingWork.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/ScheduledTimerListener.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/ScheduledTimerListener.java index 17e9c5dc171f..52ca0afe1265 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/ScheduledTimerListener.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/ScheduledTimerListener.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerAccessor.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerAccessor.java index ac0992225f98..b5c0e15a068c 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerAccessor.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerAccessor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerFactoryBean.java index 2339a5541d30..485ad6c8af1f 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,6 +18,7 @@ import java.util.LinkedList; import java.util.List; + import javax.naming.NamingException; import commonj.timers.Timer; diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerTaskScheduler.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerTaskScheduler.java index 6bfa02f47b16..97ea5b86a81d 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerTaskScheduler.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/TimerManagerTaskScheduler.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java index b7debde2d436..a9adcc823d5a 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -20,6 +20,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; + import javax.naming.NamingException; import commonj.work.Work; @@ -57,13 +58,13 @@ *

The CommonJ WorkManager will usually be retrieved from the application * server's JNDI environment, as defined in the server's management console. * - *

Note: On the upcoming EE 7 compliant versions of WebLogic and WebSphere, a + *

Note: On EE 7/8 compliant versions of WebLogic and WebSphere, a * {@link org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor} - * should be preferred, following JSR-236 support in Java EE 7. + * should be preferred, following JSR-236 support in Java EE 7/8. * * @author Juergen Hoeller * @since 2.0 - * @deprecated as of 5.1, in favor of EE 7's + * @deprecated as of 5.1, in favor of the EE 7/8 based * {@link org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor} */ @Deprecated @@ -120,6 +121,11 @@ public void setWorkListener(WorkListener workListener) { * execution callback (which may be a wrapper around the user-supplied task). *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * is limited to plain {@code Runnable} execution via {@code execute} calls. + * In case of {@code #submit} calls, the exposed {@code Runnable} will be a + * {@code FutureTask} which does not propagate any exceptions; you might + * have to cast it and call {@code Future#get} to evaluate exceptions. * @since 4.3 */ public void setTaskDecorator(TaskDecorator taskDecorator) { diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/AdaptableJobFactory.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/AdaptableJobFactory.java index 9971ebee4734..33898c102b7a 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/AdaptableJobFactory.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/AdaptableJobFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/CronTriggerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/CronTriggerFactoryBean.java index eaa5d18e39f6..9c9885175398 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/CronTriggerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/CronTriggerFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/DelegatingJob.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/DelegatingJob.java index 7536e0aa3a9d..80cfeaa439f3 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/DelegatingJob.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/DelegatingJob.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobDetailFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobDetailFactoryBean.java index 1495c0522a1f..78210ab29e90 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobDetailFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobDetailFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -199,8 +199,8 @@ public void afterPropertiesSet() { if (this.applicationContextJobDataKey != null) { if (this.applicationContext == null) { throw new IllegalStateException( - "JobDetailBean needs to be set up in an ApplicationContext " + - "to be able to handle an 'applicationContextJobDataKey'"); + "JobDetailBean needs to be set up in an ApplicationContext " + + "to be able to handle an 'applicationContextJobDataKey'"); } getJobDataMap().put(this.applicationContextJobDataKey, this.applicationContext); } diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobMethodInvocationFailedException.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobMethodInvocationFailedException.java index d79bd3f69d98..8fd115e38473 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobMethodInvocationFailedException.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobMethodInvocationFailedException.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalDataSourceJobStore.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalDataSourceJobStore.java index fcec56275985..4543f95b0d01 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalDataSourceJobStore.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalDataSourceJobStore.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -17,7 +17,9 @@ package org.springframework.scheduling.quartz; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.SQLException; + import javax.sql.DataSource; import org.quartz.SchedulerConfigException; @@ -56,6 +58,8 @@ * @since 1.1 * @see SchedulerFactoryBean#setDataSource * @see SchedulerFactoryBean#setNonTransactionalDataSource + * @see SchedulerFactoryBean#getConfigTimeDataSource() + * @see SchedulerFactoryBean#getConfigTimeNonTransactionalDataSource() * @see org.springframework.jdbc.datasource.DataSourceUtils#doGetConnection * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ @@ -109,7 +113,7 @@ public Connection getConnection() throws SQLException { public void shutdown() { // Do nothing - a Spring-managed DataSource has its own lifecycle. } - /* Quartz 2.2 initialize method */ + @Override public void initialize() { // Do nothing - a Spring-managed DataSource has its own lifecycle. } @@ -137,7 +141,7 @@ public Connection getConnection() throws SQLException { public void shutdown() { // Do nothing - a Spring-managed DataSource has its own lifecycle. } - /* Quartz 2.2 initialize method */ + @Override public void initialize() { // Do nothing - a Spring-managed DataSource has its own lifecycle. } @@ -146,7 +150,8 @@ public void initialize() { // No, if HSQL is the platform, we really don't want to use locks... try { - String productName = JdbcUtils.extractDatabaseMetaData(this.dataSource, "getDatabaseProductName"); + String productName = JdbcUtils.extractDatabaseMetaData(this.dataSource, + DatabaseMetaData::getDatabaseProductName); productName = JdbcUtils.commonDatabaseName(productName); if (productName != null && productName.toLowerCase().contains("hsql")) { setUseDBLocks(false); diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalTaskExecutorThreadPool.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalTaskExecutorThreadPool.java index 37daaf70d403..42af660341b0 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalTaskExecutorThreadPool.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalTaskExecutorThreadPool.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.java index 33992a603216..f4658c7c92b5 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.DisallowConcurrentExecution; +import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @@ -123,7 +124,7 @@ public void setGroup(String group) { * realized through adding the {@code @PersistJobDataAfterExecution} and * {@code @DisallowConcurrentExecution} markers. * More information on stateful versus stateless jobs can be found - * here. + * here. *

The default setting is to run jobs concurrently. */ public void setConcurrent(boolean concurrent) { @@ -164,7 +165,6 @@ protected Class resolveClassName(String className) throws ClassNotFoundExcept @Override - @SuppressWarnings("unchecked") public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException { prepare(); @@ -172,13 +172,13 @@ public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodExce String name = (this.name != null ? this.name : this.beanName); // Consider the concurrent flag to choose between stateful and stateless job. - Class jobClass = (this.concurrent ? MethodInvokingJob.class : StatefulMethodInvokingJob.class); + Class jobClass = (this.concurrent ? MethodInvokingJob.class : StatefulMethodInvokingJob.class); // Build JobDetail instance. JobDetailImpl jdi = new JobDetailImpl(); jdi.setName(name != null ? name : toString()); jdi.setGroup(this.group); - jdi.setJobClass((Class) jobClass); + jdi.setJobClass(jobClass); jdi.setDurability(true); jdi.getJobDataMap().put("methodInvoker", this); this.jobDetail = jdi; diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java index 8f823d249284..c63934d0d3d3 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/ResourceLoaderClassLoadHelper.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/ResourceLoaderClassLoadHelper.java index 362dd3e9c4b7..996a598460d2 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/ResourceLoaderClassLoadHelper.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/ResourceLoaderClassLoadHelper.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -82,6 +82,7 @@ public Class loadClass(String name) throws ClassNotFoundException { } @SuppressWarnings("unchecked") + @Override public Class loadClass(String name, Class clazz) throws ClassNotFoundException { return (Class) loadClass(name); } diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java index cf30b4196816..434cd49a3b11 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -41,9 +41,9 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.lang.Nullable; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionException; import org.springframework.transaction.TransactionStatus; -import org.springframework.transaction.support.DefaultTransactionDefinition; /** * Common base class for accessing a Quartz Scheduler, i.e. for registering jobs, @@ -207,7 +207,7 @@ public void setResourceLoader(ResourceLoader resourceLoader) { protected void registerJobsAndTriggers() throws SchedulerException { TransactionStatus transactionStatus = null; if (this.transactionManager != null) { - transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition()); + transactionStatus = this.transactionManager.getTransaction(TransactionDefinition.withDefaults()); } try { diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java index 11cd3fe123f7..42a80e2a33bf 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerContextAware.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerContextAware.java index 8e43f1aa79ce..ae32172fa1d0 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerContextAware.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerContextAware.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java index 3124dc726aa8..9136111d9e90 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -21,6 +21,7 @@ import java.util.Properties; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; + import javax.sql.DataSource; import org.quartz.Scheduler; @@ -312,6 +313,7 @@ public void setTaskExecutor(Executor taskExecutor) { * If set, this will override corresponding settings in Quartz properties. *

Note: If this is set, the Quartz settings should not define * a job store "dataSource" to avoid meaningless double configuration. + * Also, do not define a "org.quartz.jobStore.class" property at all. *

A Spring-specific subclass of Quartz' JobStoreCMT will be used. * It is therefore strongly recommended to perform all operations on * the Scheduler within Spring-managed (or plain JTA) transactions. @@ -619,6 +621,9 @@ private Scheduler prepareScheduler(SchedulerFactory schedulerFactory) throws Sch this.jobFactory = new AdaptableJobFactory(); } if (this.jobFactory != null) { + if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware) { + ((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext); + } if (this.jobFactory instanceof SchedulerContextAware) { ((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext()); } diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleThreadPoolTaskExecutor.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleThreadPoolTaskExecutor.java index 0b2885980317..35534962d9f9 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleThreadPoolTaskExecutor.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleThreadPoolTaskExecutor.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBean.java index 420beb7ed325..3cd25be16d7b 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java index efcc289b2039..3c6369f9b15f 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed 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 + * https://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, @@ -22,6 +22,9 @@ import org.springframework.beans.BeanWrapper; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyAccessorFactory; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.lang.Nullable; /** @@ -41,11 +44,15 @@ * @see SchedulerFactoryBean#setJobFactory * @see QuartzJobBean */ -public class SpringBeanJobFactory extends AdaptableJobFactory implements SchedulerContextAware { +public class SpringBeanJobFactory extends AdaptableJobFactory + implements ApplicationContextAware, SchedulerContextAware { @Nullable private String[] ignoredUnknownProperties; + @Nullable + private ApplicationContext applicationContext; + @Nullable private SchedulerContext schedulerContext; @@ -62,6 +69,11 @@ public void setIgnoredUnknownProperties(String... ignoredUnknownProperties) { this.ignoredUnknownProperties = ignoredUnknownProperties; } + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + @Override public void setSchedulerContext(SchedulerContext schedulerContext) { this.schedulerContext = schedulerContext; @@ -74,7 +86,11 @@ public void setSchedulerContext(SchedulerContext schedulerContext) { */ @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { - Object job = super.createJobInstance(bundle); + Object job = (this.applicationContext != null ? + this.applicationContext.getAutowireCapableBeanFactory().createBean( + bundle.getJobDetail().getJobClass(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false) : + super.createJobInstance(bundle)); + if (isEligibleForPropertyPopulation(job)) { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job); MutablePropertyValues pvs = new MutablePropertyValues(); @@ -95,6 +111,7 @@ protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { bw.setPropertyValues(pvs, true); } } + return job; } diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/package-info.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/package-info.java index 48b6d70f95d5..6ca38a31963d 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/package-info.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/package-info.java @@ -1,6 +1,6 @@ /** * Support classes for the open source scheduler - * Quartz, + * Quartz, * allowing to set up Quartz Schedulers, JobDetails and * Triggers as beans in a Spring context. Also provides * convenience classes for implementing Quartz Jobs. diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java index a354e4a975b5..2b29d05986f9 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBean.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBean.java index 3f4841ca83a7..5639ac06a91f 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java index 751ee13dc2d4..e1ca06bf99e3 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -46,7 +46,7 @@ public abstract class FreeMarkerTemplateUtils { public static String processTemplateIntoString(Template template, Object model) throws IOException, TemplateException { - StringWriter result = new StringWriter(); + StringWriter result = new StringWriter(1024); template.process(model, result); return result.toString(); } diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java index 063274957919..d4140aa681f1 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/package-info.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/package-info.java index 4dc244e9f485..492946e8998f 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/package-info.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/package-info.java @@ -1,6 +1,6 @@ /** * Support classes for setting up - * FreeMarker + * FreeMarker * within a Spring application context. */ @NonNullApi diff --git a/spring-context-support/src/main/java/overview.html b/spring-context-support/src/main/java/overview.html deleted file mode 100644 index 09300c837acb..000000000000 --- a/spring-context-support/src/main/java/overview.html +++ /dev/null @@ -1,7 +0,0 @@ - - -

-Support classes for integrating common third-party libraries into a Spring application context. -

- - \ No newline at end of file diff --git a/spring-context-support/src/main/resources/org/springframework/mail/javamail/mime.types b/spring-context-support/src/main/resources/org/springframework/mail/javamail/mime.types index 1eabbf5d2f4c..b03625f63316 100644 --- a/spring-context-support/src/main/resources/org/springframework/mail/javamail/mime.types +++ b/spring-context-support/src/main/resources/org/springframework/mail/javamail/mime.types @@ -1,11 +1,11 @@ ################################################################################ -# Copyright 2002-2018 the original author or authors. +# Copyright 2002-2019 the original author or authors. # # Licensed 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 +# https://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, @@ -17,9 +17,11 @@ ################################################################################ # # Defaults for the Java Activation Framework (revised). +# # Modified extensions registered in this file: # text/plain java c c++ cpp pl cc h # image/png png +# image/svg+xml svg # ################################################################################ @@ -48,7 +50,7 @@ video/x-msvideo avi ################################################################################ # # Additional file types adapted from -# http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html +# http://sites.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html # kindly re-licensed to Apache Software License 2.0 by Ian Graham. # ################################################################################ @@ -67,6 +69,8 @@ image/x-xbitmap xbm image/x-xpixmap xpm # Portable Network Graphics image/png png +# Scalable Vector Graphics +image/svg+xml svg # Image Exchange Format (RFC 1314) image/ief ief # RGB diff --git a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java index 678bc6294b98..e90a3ece9363 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -19,15 +19,14 @@ import com.github.benmanes.caffeine.cache.CacheLoader; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.CaffeineSpec; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.Mockito.mock; /** * @author Ben Manes @@ -36,80 +35,84 @@ */ public class CaffeineCacheManagerTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Test public void testDynamicMode() { CacheManager cm = new CaffeineCacheManager(); Cache cache1 = cm.getCache("c1"); - assertTrue(cache1 instanceof CaffeineCache); + boolean condition2 = cache1 instanceof CaffeineCache; + assertThat(condition2).isTrue(); Cache cache1again = cm.getCache("c1"); - assertSame(cache1again, cache1); + assertThat(cache1).isSameAs(cache1again); Cache cache2 = cm.getCache("c2"); - assertTrue(cache2 instanceof CaffeineCache); + boolean condition1 = cache2 instanceof CaffeineCache; + assertThat(condition1).isTrue(); Cache cache2again = cm.getCache("c2"); - assertSame(cache2again, cache2); + assertThat(cache2).isSameAs(cache2again); Cache cache3 = cm.getCache("c3"); - assertTrue(cache3 instanceof CaffeineCache); + boolean condition = cache3 instanceof CaffeineCache; + assertThat(condition).isTrue(); Cache cache3again = cm.getCache("c3"); - assertSame(cache3again, cache3); + assertThat(cache3).isSameAs(cache3again); cache1.put("key1", "value1"); - assertEquals("value1", cache1.get("key1").get()); + assertThat(cache1.get("key1").get()).isEqualTo("value1"); cache1.put("key2", 2); - assertEquals(2, cache1.get("key2").get()); + assertThat(cache1.get("key2").get()).isEqualTo(2); cache1.put("key3", null); - assertNull(cache1.get("key3").get()); + assertThat(cache1.get("key3").get()).isNull(); cache1.evict("key3"); - assertNull(cache1.get("key3")); + assertThat(cache1.get("key3")).isNull(); } @Test public void testStaticMode() { CaffeineCacheManager cm = new CaffeineCacheManager("c1", "c2"); Cache cache1 = cm.getCache("c1"); - assertTrue(cache1 instanceof CaffeineCache); + boolean condition3 = cache1 instanceof CaffeineCache; + assertThat(condition3).isTrue(); Cache cache1again = cm.getCache("c1"); - assertSame(cache1again, cache1); + assertThat(cache1).isSameAs(cache1again); Cache cache2 = cm.getCache("c2"); - assertTrue(cache2 instanceof CaffeineCache); + boolean condition2 = cache2 instanceof CaffeineCache; + assertThat(condition2).isTrue(); Cache cache2again = cm.getCache("c2"); - assertSame(cache2again, cache2); + assertThat(cache2).isSameAs(cache2again); Cache cache3 = cm.getCache("c3"); - assertNull(cache3); + assertThat(cache3).isNull(); cache1.put("key1", "value1"); - assertEquals("value1", cache1.get("key1").get()); + assertThat(cache1.get("key1").get()).isEqualTo("value1"); cache1.put("key2", 2); - assertEquals(2, cache1.get("key2").get()); + assertThat(cache1.get("key2").get()).isEqualTo(2); cache1.put("key3", null); - assertNull(cache1.get("key3").get()); + assertThat(cache1.get("key3").get()).isNull(); cache1.evict("key3"); - assertNull(cache1.get("key3")); + assertThat(cache1.get("key3")).isNull(); cm.setAllowNullValues(false); Cache cache1x = cm.getCache("c1"); - assertTrue(cache1x instanceof CaffeineCache); - assertTrue(cache1x != cache1); + boolean condition1 = cache1x instanceof CaffeineCache; + assertThat(condition1).isTrue(); + assertThat(cache1x != cache1).isTrue(); Cache cache2x = cm.getCache("c2"); - assertTrue(cache2x instanceof CaffeineCache); - assertTrue(cache2x != cache2); + boolean condition = cache2x instanceof CaffeineCache; + assertThat(condition).isTrue(); + assertThat(cache2x != cache2).isTrue(); Cache cache3x = cm.getCache("c3"); - assertNull(cache3x); + assertThat(cache3x).isNull(); cache1x.put("key1", "value1"); - assertEquals("value1", cache1x.get("key1").get()); + assertThat(cache1x.get("key1").get()).isEqualTo("value1"); cache1x.put("key2", 2); - assertEquals(2, cache1x.get("key2").get()); + assertThat(cache1x.get("key2").get()).isEqualTo(2); cm.setAllowNullValues(true); Cache cache1y = cm.getCache("c1"); cache1y.put("key3", null); - assertNull(cache1y.get("key3").get()); + assertThat(cache1y.get("key3").get()).isNull(); cache1y.evict("key3"); - assertNull(cache1y.get("key3")); + assertThat(cache1y.get("key3")).isNull(); } @Test @@ -120,11 +123,11 @@ public void changeCaffeineRecreateCache() { Caffeine caffeine = Caffeine.newBuilder().maximumSize(10); cm.setCaffeine(caffeine); Cache cache1x = cm.getCache("c1"); - assertTrue(cache1x != cache1); + assertThat(cache1x != cache1).isTrue(); - cm.setCaffeine(caffeine); // Set same instance + cm.setCaffeine(caffeine); // Set same instance Cache cache1xx = cm.getCache("c1"); - assertSame(cache1x, cache1xx); + assertThat(cache1xx).isSameAs(cache1x); } @Test @@ -134,7 +137,7 @@ public void changeCaffeineSpecRecreateCache() { cm.setCaffeineSpec(CaffeineSpec.parse("maximumSize=10")); Cache cache1x = cm.getCache("c1"); - assertTrue(cache1x != cache1); + assertThat(cache1x != cache1).isTrue(); } @Test @@ -144,7 +147,7 @@ public void changeCacheSpecificationRecreateCache() { cm.setCacheSpecification("maximumSize=10"); Cache cache1x = cm.getCache("c1"); - assertTrue(cache1x != cache1); + assertThat(cache1x != cache1).isTrue(); } @Test @@ -152,22 +155,24 @@ public void changeCacheLoaderRecreateCache() { CaffeineCacheManager cm = new CaffeineCacheManager("c1"); Cache cache1 = cm.getCache("c1"); - CacheLoader loader = mockCacheLoader(); + @SuppressWarnings("unchecked") + CacheLoader loader = mock(CacheLoader.class); + cm.setCacheLoader(loader); Cache cache1x = cm.getCache("c1"); - assertTrue(cache1x != cache1); + assertThat(cache1x != cache1).isTrue(); - cm.setCacheLoader(loader); // Set same instance + cm.setCacheLoader(loader); // Set same instance Cache cache1xx = cm.getCache("c1"); - assertSame(cache1x, cache1xx); + assertThat(cache1xx).isSameAs(cache1x); } @Test public void setCacheNameNullRestoreDynamicMode() { CaffeineCacheManager cm = new CaffeineCacheManager("c1"); - assertNull(cm.getCache("someCache")); + assertThat(cm.getCache("someCache")).isNull(); cm.setCacheNames(null); - assertNotNull(cm.getCache("someCache")); + assertThat(cm.getCache("someCache")).isNotNull(); } @Test @@ -184,17 +189,26 @@ public Object load(Object key) throws Exception { }); Cache cache1 = cm.getCache("c1"); Cache.ValueWrapper value = cache1.get("ping"); - assertNotNull(value); - assertEquals("pong", value.get()); + assertThat(value).isNotNull(); + assertThat(value.get()).isEqualTo("pong"); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("I only know ping"); - assertNull(cache1.get("foo")); + assertThatIllegalArgumentException().isThrownBy(() -> assertThat(cache1.get("foo")).isNull()) + .withMessageContaining("I only know ping"); } - @SuppressWarnings("unchecked") - private CacheLoader mockCacheLoader() { - return mock(CacheLoader.class); + @Test + public void customCacheRegistration() { + CaffeineCacheManager cm = new CaffeineCacheManager("c1"); + com.github.benmanes.caffeine.cache.Cache nc = Caffeine.newBuilder().build(); + cm.registerCustomCache("c2", nc); + + Cache cache1 = cm.getCache("c1"); + Cache cache2 = cm.getCache("c2"); + assertThat(nc == cache2.getNativeCache()).isTrue(); + + cm.setCaffeine(Caffeine.newBuilder().maximumSize(10)); + assertThat(cm.getCache("c1") != cache1).isTrue(); + assertThat(cm.getCache("c2") == cache2).isTrue(); } } diff --git a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java index 0fbed8b14ce6..48e1fd5a9393 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,13 +17,13 @@ package org.springframework.cache.caffeine; import com.github.benmanes.caffeine.cache.Caffeine; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.springframework.cache.AbstractValueAdaptingCacheTests; import org.springframework.cache.Cache; +import org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Ben Manes @@ -37,7 +37,7 @@ public class CaffeineCacheTests extends AbstractValueAdaptingCacheTests { private EhCacheCache cache; - @Before + @BeforeEach public void setup() { cacheManager = new CacheManager(new Configuration().name("EhCacheCacheTests") .defaultCache(new CacheConfiguration("default", 100))); @@ -55,7 +55,7 @@ public void setup() { cache = new EhCacheCache(nativeCache); } - @After + @AfterEach public void shutdown() { cacheManager.shutdown(); } @@ -73,9 +73,8 @@ protected Ehcache getNativeCache() { @Test + @EnabledForTestGroups(LONG_RUNNING) public void testExpiredElements() throws Exception { - Assume.group(TestGroup.LONG_RUNNING); - String key = "brancusi"; String value = "constantin"; Element brancusi = new Element(key, value); @@ -83,10 +82,10 @@ public void testExpiredElements() throws Exception { brancusi.setTimeToLive(3); nativeCache.put(brancusi); - assertEquals(value, cache.get(key).get()); + assertThat(cache.get(key).get()).isEqualTo(value); // wait for the entry to expire Thread.sleep(5 * 1000); - assertNull(cache.get(key)); + assertThat(cache.get(key)).isNull(); } } diff --git a/spring-context-support/src/test/java/org/springframework/cache/ehcache/EhCacheSupportTests.java b/spring-context-support/src/test/java/org/springframework/cache/ehcache/EhCacheSupportTests.java index d15107c58360..c663b0fba335 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/ehcache/EhCacheSupportTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/ehcache/EhCacheSupportTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -25,11 +25,12 @@ import net.sf.ehcache.constructs.blocking.SelfPopulatingCache; import net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory; import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.core.io.ClassPathResource; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Juergen Hoeller @@ -42,14 +43,14 @@ public class EhCacheSupportTests { public void testBlankCacheManager() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setCacheManagerName("myCacheManager"); - assertEquals(CacheManager.class, cacheManagerFb.getObjectType()); - assertTrue("Singleton property", cacheManagerFb.isSingleton()); + assertThat(cacheManagerFb.getObjectType()).isEqualTo(CacheManager.class); + assertThat(cacheManagerFb.isSingleton()).as("Singleton property").isTrue(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); - assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0); + assertThat(cm.getCacheNames().length == 0).as("Loaded CacheManager with no caches").isTrue(); Cache myCache1 = cm.getCache("myCache1"); - assertTrue("No myCache1 defined", myCache1 == null); + assertThat(myCache1 == null).as("No myCache1 defined").isTrue(); } finally { cacheManagerFb.destroy(); @@ -59,23 +60,20 @@ public void testBlankCacheManager() { @Test public void testCacheManagerConflict() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); - cacheManagerFb.setCacheManagerName("myCacheManager"); - assertEquals(CacheManager.class, cacheManagerFb.getObjectType()); - assertTrue("Singleton property", cacheManagerFb.isSingleton()); - cacheManagerFb.afterPropertiesSet(); try { + cacheManagerFb.setCacheManagerName("myCacheManager"); + assertThat(cacheManagerFb.getObjectType()).isEqualTo(CacheManager.class); + assertThat(cacheManagerFb.isSingleton()).as("Singleton property").isTrue(); + cacheManagerFb.afterPropertiesSet(); CacheManager cm = cacheManagerFb.getObject(); - assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0); + assertThat(cm.getCacheNames().length == 0).as("Loaded CacheManager with no caches").isTrue(); Cache myCache1 = cm.getCache("myCache1"); - assertTrue("No myCache1 defined", myCache1 == null); + assertThat(myCache1 == null).as("No myCache1 defined").isTrue(); EhCacheManagerFactoryBean cacheManagerFb2 = new EhCacheManagerFactoryBean(); cacheManagerFb2.setCacheManagerName("myCacheManager"); - cacheManagerFb2.afterPropertiesSet(); - fail("Should have thrown CacheException because of naming conflict"); - } - catch (CacheException ex) { - // expected + assertThatExceptionOfType(CacheException.class).as("because of naming conflict").isThrownBy( + cacheManagerFb2::afterPropertiesSet); } finally { cacheManagerFb.destroy(); @@ -86,21 +84,21 @@ public void testCacheManagerConflict() { public void testAcceptExistingCacheManager() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setCacheManagerName("myCacheManager"); - assertEquals(CacheManager.class, cacheManagerFb.getObjectType()); - assertTrue("Singleton property", cacheManagerFb.isSingleton()); + assertThat(cacheManagerFb.getObjectType()).isEqualTo(CacheManager.class); + assertThat(cacheManagerFb.isSingleton()).as("Singleton property").isTrue(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); - assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0); + assertThat(cm.getCacheNames().length == 0).as("Loaded CacheManager with no caches").isTrue(); Cache myCache1 = cm.getCache("myCache1"); - assertTrue("No myCache1 defined", myCache1 == null); + assertThat(myCache1 == null).as("No myCache1 defined").isTrue(); EhCacheManagerFactoryBean cacheManagerFb2 = new EhCacheManagerFactoryBean(); cacheManagerFb2.setCacheManagerName("myCacheManager"); cacheManagerFb2.setAcceptExisting(true); cacheManagerFb2.afterPropertiesSet(); CacheManager cm2 = cacheManagerFb2.getObject(); - assertSame(cm, cm2); + assertThat(cm2).isSameAs(cm); cacheManagerFb2.destroy(); } finally { @@ -115,10 +113,10 @@ public void testCacheManagerFromConfigFile() { cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); - assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1); + assertThat(cm.getCacheNames().length == 1).as("Correct number of caches loaded").isTrue(); Cache myCache1 = cm.getCache("myCache1"); - assertFalse("myCache1 is not eternal", myCache1.getCacheConfiguration().isEternal()); - assertTrue("myCache1.maxElements == 300", myCache1.getCacheConfiguration().getMaxEntriesLocalHeap() == 300); + assertThat(myCache1.getCacheConfiguration().isEternal()).as("myCache1 is not eternal").isFalse(); + assertThat(myCache1.getCacheConfiguration().getMaxEntriesLocalHeap() == 300).as("myCache1.maxElements == 300").isTrue(); } finally { cacheManagerFb.destroy(); @@ -142,8 +140,8 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) { try { EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); Class objectType = cacheFb.getObjectType(); - assertTrue(Ehcache.class.isAssignableFrom(objectType)); - assertTrue("Singleton property", cacheFb.isSingleton()); + assertThat(Ehcache.class.isAssignableFrom(objectType)).isTrue(); + assertThat(cacheFb.isSingleton()).as("Singleton property").isTrue(); if (useCacheManagerFb) { cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass())); @@ -157,14 +155,14 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) { cacheFb.afterPropertiesSet(); cache = (Cache) cacheFb.getObject(); Class objectType2 = cacheFb.getObjectType(); - assertSame(objectType, objectType2); + assertThat(objectType2).isSameAs(objectType); CacheConfiguration config = cache.getCacheConfiguration(); - assertEquals("myCache1", cache.getName()); + assertThat(cache.getName()).isEqualTo("myCache1"); if (useCacheManagerFb){ - assertEquals("myCache1.maxElements", 300, config.getMaxEntriesLocalHeap()); + assertThat(config.getMaxEntriesLocalHeap()).as("myCache1.maxElements").isEqualTo(300); } else { - assertEquals("myCache1.maxElements", 10000, config.getMaxEntriesLocalHeap()); + assertThat(config.getMaxEntriesLocalHeap()).as("myCache1.maxElements").isEqualTo(10000); } // Cache region is not defined. Should create one with default properties. @@ -176,12 +174,12 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) { cacheFb.afterPropertiesSet(); cache = (Cache) cacheFb.getObject(); config = cache.getCacheConfiguration(); - assertEquals("undefinedCache", cache.getName()); - assertTrue("default maxElements is correct", config.getMaxEntriesLocalHeap() == 10000); - assertFalse("default eternal is correct", config.isEternal()); - assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 120); - assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 120); - assertTrue("default diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 120); + assertThat(cache.getName()).isEqualTo("undefinedCache"); + assertThat(config.getMaxEntriesLocalHeap() == 10000).as("default maxElements is correct").isTrue(); + assertThat(config.isEternal()).as("default eternal is correct").isFalse(); + assertThat(config.getTimeToLiveSeconds() == 120).as("default timeToLive is correct").isTrue(); + assertThat(config.getTimeToIdleSeconds() == 120).as("default timeToIdle is correct").isTrue(); + assertThat(config.getDiskExpiryThreadIntervalSeconds() == 120).as("default diskExpiryThreadIntervalSeconds is correct").isTrue(); // overriding the default properties cacheFb = new EhCacheFactoryBean(); @@ -197,11 +195,11 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) { cache = (Cache) cacheFb.getObject(); config = cache.getCacheConfiguration(); - assertEquals("undefinedCache2", cache.getName()); - assertTrue("overridden maxElements is correct", config.getMaxEntriesLocalHeap() == 5); - assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 8); - assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 7); - assertTrue("overridden diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 10); + assertThat(cache.getName()).isEqualTo("undefinedCache2"); + assertThat(config.getMaxEntriesLocalHeap() == 5).as("overridden maxElements is correct").isTrue(); + assertThat(config.getTimeToLiveSeconds() == 8).as("default timeToLive is correct").isTrue(); + assertThat(config.getTimeToIdleSeconds() == 7).as("default timeToIdle is correct").isTrue(); + assertThat(config.getDiskExpiryThreadIntervalSeconds() == 10).as("overridden diskExpiryThreadIntervalSeconds is correct").isTrue(); } finally { if (cacheManagerFbInitialized) { @@ -223,10 +221,11 @@ public void testEhCacheFactoryBeanWithBlockingCache() { cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setBlocking(true); - assertEquals(cacheFb.getObjectType(), BlockingCache.class); + assertThat(BlockingCache.class).isEqualTo(cacheFb.getObjectType()); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); - assertTrue(myCache1 instanceof BlockingCache); + boolean condition = myCache1 instanceof BlockingCache; + assertThat(condition).isTrue(); } finally { cacheManagerFb.destroy(); @@ -243,11 +242,12 @@ public void testEhCacheFactoryBeanWithSelfPopulatingCache() { cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(key -> key); - assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class); + assertThat(SelfPopulatingCache.class).isEqualTo(cacheFb.getObjectType()); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); - assertTrue(myCache1 instanceof SelfPopulatingCache); - assertEquals("myKey1", myCache1.get("myKey1").getObjectValue()); + boolean condition = myCache1 instanceof SelfPopulatingCache; + assertThat(condition).isTrue(); + assertThat(myCache1.get("myKey1").getObjectValue()).isEqualTo("myKey1"); } finally { cacheManagerFb.destroy(); @@ -272,11 +272,12 @@ public Object createEntry(Object key) { public void updateEntryValue(Object key, Object value) { } }); - assertEquals(cacheFb.getObjectType(), UpdatingSelfPopulatingCache.class); + assertThat(UpdatingSelfPopulatingCache.class).isEqualTo(cacheFb.getObjectType()); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); - assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache); - assertEquals("myKey1", myCache1.get("myKey1").getObjectValue()); + boolean condition = myCache1 instanceof UpdatingSelfPopulatingCache; + assertThat(condition).isTrue(); + assertThat(myCache1.get("myKey1").getObjectValue()).isEqualTo("myKey1"); } finally { cacheManagerFb.destroy(); diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/AbstractJCacheTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/AbstractJCacheTests.java index ca587a9175f3..0d7df53620da 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/AbstractJCacheTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/AbstractJCacheTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,9 +19,8 @@ import java.util.ArrayList; import java.util.List; -import org.junit.Rule; -import org.junit.rules.ExpectedException; -import org.junit.rules.TestName; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInfo; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; @@ -35,14 +34,18 @@ /** * @author Stephane Nicoll + * @author Sam Brannen */ public abstract class AbstractJCacheTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); + protected String cacheName; + + + @BeforeEach + void trackCacheName(TestInfo testInfo) { + this.cacheName = testInfo.getTestMethod().get().getName(); + } - @Rule - public final TestName name = new TestName(); protected final CacheManager cacheManager = createSimpleCacheManager("default", "simpleCache"); diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheCacheManagerTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheCacheManagerTests.java index e81fb50d682e..118494273672 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheCacheManagerTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheCacheManagerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,14 +18,16 @@ import java.util.ArrayList; import java.util.List; + import javax.cache.Cache; import javax.cache.CacheManager; -import org.junit.Before; +import org.junit.jupiter.api.BeforeEach; import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManagerTests; -import static org.mockito.BDDMockito.*; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * @author Stephane Nicoll @@ -39,7 +41,7 @@ public class JCacheCacheManagerTests extends AbstractTransactionSupportingCacheM private JCacheCacheManager transactionalCacheManager; - @Before + @BeforeEach public void setupOnce() { cacheManagerMock = new CacheManagerMock(); cacheManagerMock.addCache(CACHE_NAME); @@ -96,7 +98,7 @@ private CacheManager getCacheManager() { return cacheManager; } - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public void addCache(String name) { cacheNames.add(name); Cache cache = mock(Cache.class); diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3AnnotationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3AnnotationTests.java index 608eb7bc7cf8..105e4e620995 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3AnnotationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3AnnotationTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3ApiTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3ApiTests.java index 97e42e646970..32a2585c9f85 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3ApiTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCache3ApiTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheAnnotationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheAnnotationTests.java index 5e897570a5b8..188cc293c974 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheAnnotationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheAnnotationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -21,30 +21,35 @@ import javax.cache.configuration.MutableConfiguration; import javax.cache.spi.CachingProvider; -import org.junit.After; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.config.AbstractCacheAnnotationTests; -import org.springframework.cache.config.AnnotatedClassCacheableService; -import org.springframework.cache.config.CacheableService; -import org.springframework.cache.config.DefaultCacheableService; -import org.springframework.cache.config.SomeCustomKeyGenerator; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.SimpleKeyGenerator; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.testfixture.cache.AbstractCacheAnnotationTests; +import org.springframework.context.testfixture.cache.SomeCustomKeyGenerator; +import org.springframework.context.testfixture.cache.beans.AnnotatedClassCacheableService; +import org.springframework.context.testfixture.cache.beans.CacheableService; +import org.springframework.context.testfixture.cache.beans.DefaultCacheableService; +import org.springframework.transaction.support.TransactionTemplate; +import org.springframework.transaction.testfixture.CallCountingTransactionManager; /** * @author Stephane Nicoll + * @author Juergen Hoeller */ public class JCacheEhCacheAnnotationTests extends AbstractCacheAnnotationTests { + private final TransactionTemplate txTemplate = new TransactionTemplate(new CallCountingTransactionManager()); + private CacheManager jCacheManager; @@ -62,7 +67,7 @@ protected CachingProvider getCachingProvider() { return Caching.getCachingProvider("org.ehcache.jcache.JCacheCachingProvider"); } - @After + @AfterEach public void shutdown() { if (jCacheManager != null) { jCacheManager.close(); @@ -72,10 +77,30 @@ public void shutdown() { @Override @Test - @Ignore("Multi cache manager support to be added") + @Disabled("Multi cache manager support to be added") public void testCustomCacheManager() { } + @Test + public void testEvictWithTransaction() { + txTemplate.executeWithoutResult(s -> testEvict(this.cs, false)); + } + + @Test + public void testEvictEarlyWithTransaction() { + txTemplate.executeWithoutResult(s -> testEvictEarly(this.cs)); + } + + @Test + public void testEvictAllWithTransaction() { + txTemplate.executeWithoutResult(s -> testEvictAll(this.cs, false)); + } + + @Test + public void testEvictAllEarlyWithTransaction() { + txTemplate.executeWithoutResult(s -> testEvictAllEarly(this.cs)); + } + @Configuration @EnableCaching @@ -87,7 +112,9 @@ static class EnableCachingConfig extends CachingConfigurerSupport { @Override @Bean public org.springframework.cache.CacheManager cacheManager() { - return new JCacheCacheManager(jCacheManager()); + JCacheCacheManager cm = new JCacheCacheManager(jCacheManager()); + cm.setTransactionAware(true); + return cm; } @Bean diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheApiTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheApiTests.java index 90ab11fa20af..b826e2a3fb37 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheApiTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheApiTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -22,10 +22,10 @@ import javax.cache.configuration.MutableConfiguration; import javax.cache.spi.CachingProvider; -import org.junit.After; -import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; -import org.springframework.cache.AbstractValueAdaptingCacheTests; +import org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests; /** * @author Stephane Nicoll @@ -41,7 +41,7 @@ public class JCacheEhCacheApiTests extends AbstractValueAdaptingCacheTests()); @@ -57,7 +57,7 @@ protected CachingProvider getCachingProvider() { return Caching.getCachingProvider("org.ehcache.jcache.JCacheCachingProvider"); } - @After + @AfterEach public void shutdown() { if (this.cacheManager != null) { this.cacheManager.close(); diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/AbstractJCacheAnnotationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/AbstractJCacheAnnotationTests.java deleted file mode 100644 index 4a83100c3b90..000000000000 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/AbstractJCacheAnnotationTests.java +++ /dev/null @@ -1,575 +0,0 @@ -/* - * Copyright 2002-2015 the original author or authors. - * - * Licensed 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.springframework.cache.jcache.config; - -import java.io.IOException; -import java.util.concurrent.ConcurrentHashMap; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; - -import org.springframework.cache.Cache; -import org.springframework.cache.CacheManager; -import org.springframework.cache.interceptor.SimpleKeyGenerator; -import org.springframework.context.ApplicationContext; - -import static org.junit.Assert.*; - -/** - * @author Stephane Nicoll - */ -public abstract class AbstractJCacheAnnotationTests { - - public static final String DEFAULT_CACHE = "default"; - - public static final String EXCEPTION_CACHE = "exception"; - - @Rule - public final TestName name = new TestName(); - - protected ApplicationContext ctx; - - private JCacheableService service; - - private CacheManager cacheManager; - - protected abstract ApplicationContext getApplicationContext(); - - @Before - public void setUp() { - ctx = getApplicationContext(); - service = ctx.getBean(JCacheableService.class); - cacheManager = ctx.getBean("cacheManager", CacheManager.class); - } - - @Test - public void cache() { - String keyItem = name.getMethodName(); - - Object first = service.cache(keyItem); - Object second = service.cache(keyItem); - assertSame(first, second); - } - - @Test - public void cacheNull() { - Cache cache = getCache(DEFAULT_CACHE); - - String keyItem = name.getMethodName(); - assertNull(cache.get(keyItem)); - - Object first = service.cacheNull(keyItem); - Object second = service.cacheNull(keyItem); - assertSame(first, second); - - Cache.ValueWrapper wrapper = cache.get(keyItem); - assertNotNull(wrapper); - assertSame(first, wrapper.get()); - assertNull("Cached value should be null", wrapper.get()); - } - - @Test - public void cacheException() { - String keyItem = name.getMethodName(); - Cache cache = getCache(EXCEPTION_CACHE); - - Object key = createKey(keyItem); - assertNull(cache.get(key)); - - try { - service.cacheWithException(keyItem, true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - // This is what we expect - } - - Cache.ValueWrapper result = cache.get(key); - assertNotNull(result); - assertEquals(UnsupportedOperationException.class, result.get().getClass()); - } - - @Test - public void cacheExceptionVetoed() { - String keyItem = name.getMethodName(); - Cache cache = getCache(EXCEPTION_CACHE); - - Object key = createKey(keyItem); - assertNull(cache.get(key)); - - try { - service.cacheWithException(keyItem, false); - fail("Should have thrown an exception"); - } - catch (NullPointerException e) { - // This is what we expect - } - assertNull(cache.get(key)); - } - - @Test - public void cacheCheckedException() { - String keyItem = name.getMethodName(); - Cache cache = getCache(EXCEPTION_CACHE); - - Object key = createKey(keyItem); - assertNull(cache.get(key)); - - try { - service.cacheWithCheckedException(keyItem, true); - fail("Should have thrown an exception"); - } - catch (IOException e) { - // This is what we expect - } - - Cache.ValueWrapper result = cache.get(key); - assertNotNull(result); - assertEquals(IOException.class, result.get().getClass()); - } - - - @SuppressWarnings("ThrowableResultOfMethodCallIgnored") - @Test - public void cacheExceptionRewriteCallStack() { - final String keyItem = name.getMethodName(); - - UnsupportedOperationException first = null; - long ref = service.exceptionInvocations(); - try { - service.cacheWithException(keyItem, true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - first = e; - } - // Sanity check, this particular call has called the service - assertEquals("First call should not have been cached", ref + 1, service.exceptionInvocations()); - - UnsupportedOperationException second = methodInCallStack(keyItem); - // Sanity check, this particular call has *not* called the service - assertEquals("Second call should have been cached", ref + 1, service.exceptionInvocations()); - - assertEquals(first.getCause(), second.getCause()); - assertEquals(first.getMessage(), second.getMessage()); - assertFalse("Original stack must not contain any reference to methodInCallStack", - contain(first, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")); - assertTrue("Cached stack should have been rewritten with a reference to methodInCallStack", - contain(second, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")); - } - - @Test - public void cacheAlwaysInvoke() { - String keyItem = name.getMethodName(); - - Object first = service.cacheAlwaysInvoke(keyItem); - Object second = service.cacheAlwaysInvoke(keyItem); - assertNotSame(first, second); - } - - @Test - public void cacheWithPartialKey() { - String keyItem = name.getMethodName(); - - Object first = service.cacheWithPartialKey(keyItem, true); - Object second = service.cacheWithPartialKey(keyItem, false); - assertSame(first, second); // second argument not used, see config - } - - @Test - public void cacheWithCustomCacheResolver() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - service.cacheWithCustomCacheResolver(keyItem); - - assertNull(cache.get(key)); // Cache in mock cache - } - - @Test - public void cacheWithCustomKeyGenerator() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - service.cacheWithCustomKeyGenerator(keyItem, "ignored"); - - assertNull(cache.get(key)); - } - - @Test - public void put() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - assertNull(cache.get(key)); - - service.put(keyItem, value); - - Cache.ValueWrapper result = cache.get(key); - assertNotNull(result); - assertEquals(value, result.get()); - } - - @Test - public void putWithException() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - assertNull(cache.get(key)); - - try { - service.putWithException(keyItem, value, true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - // This is what we expect - } - - Cache.ValueWrapper result = cache.get(key); - assertNotNull(result); - assertEquals(value, result.get()); - } - - @Test - public void putWithExceptionVetoPut() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - assertNull(cache.get(key)); - - try { - service.putWithException(keyItem, value, false); - fail("Should have thrown an exception"); - } - catch (NullPointerException e) { - // This is what we expect - } - assertNull(cache.get(key)); - } - - @Test - public void earlyPut() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - assertNull(cache.get(key)); - - service.earlyPut(keyItem, value); - - Cache.ValueWrapper result = cache.get(key); - assertNotNull(result); - assertEquals(value, result.get()); - } - - @Test - public void earlyPutWithException() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - assertNull(cache.get(key)); - - try { - service.earlyPutWithException(keyItem, value, true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - // This is what we expect - } - - Cache.ValueWrapper result = cache.get(key); - assertNotNull(result); - assertEquals(value, result.get()); - } - - @Test - public void earlyPutWithExceptionVetoPut() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - assertNull(cache.get(key)); - - try { - service.earlyPutWithException(keyItem, value, false); - fail("Should have thrown an exception"); - } - catch (NullPointerException e) { - // This is what we expect - } - // This will be cached anyway as the earlyPut has updated the cache before - Cache.ValueWrapper result = cache.get(key); - assertNotNull(result); - assertEquals(value, result.get()); - } - - @Test - public void remove() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - cache.put(key, value); - - service.remove(keyItem); - - assertNull(cache.get(key)); - } - - @Test - public void removeWithException() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - cache.put(key, value); - - try { - service.removeWithException(keyItem, true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - // This is what we expect - } - - assertNull(cache.get(key)); - } - - @Test - public void removeWithExceptionVetoRemove() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - cache.put(key, value); - - try { - service.removeWithException(keyItem, false); - fail("Should have thrown an exception"); - } - catch (NullPointerException e) { - // This is what we expect - } - Cache.ValueWrapper wrapper = cache.get(key); - assertNotNull(wrapper); - assertEquals(value, wrapper.get()); - } - - @Test - public void earlyRemove() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - cache.put(key, value); - - service.earlyRemove(keyItem); - - assertNull(cache.get(key)); - } - - @Test - public void earlyRemoveWithException() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - cache.put(key, value); - - try { - service.earlyRemoveWithException(keyItem, true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - // This is what we expect - } - assertNull(cache.get(key)); - } - - @Test - public void earlyRemoveWithExceptionVetoRemove() { - String keyItem = name.getMethodName(); - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(keyItem); - Object value = new Object(); - cache.put(key, value); - - try { - service.earlyRemoveWithException(keyItem, false); - fail("Should have thrown an exception"); - } - catch (NullPointerException e) { - // This is what we expect - } - // This will be remove anyway as the earlyRemove has removed the cache before - assertNull(cache.get(key)); - } - - @Test - public void removeAll() { - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(name.getMethodName()); - cache.put(key, new Object()); - - service.removeAll(); - - assertTrue(isEmpty(cache)); - } - - @Test - public void removeAllWithException() { - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(name.getMethodName()); - cache.put(key, new Object()); - - try { - service.removeAllWithException(true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - // This is what we expect - } - - assertTrue(isEmpty(cache)); - } - - @Test - public void removeAllWithExceptionVetoRemove() { - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(name.getMethodName()); - cache.put(key, new Object()); - - try { - service.removeAllWithException(false); - fail("Should have thrown an exception"); - } - catch (NullPointerException e) { - // This is what we expect - } - assertNotNull(cache.get(key)); - } - - @Test - public void earlyRemoveAll() { - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(name.getMethodName()); - cache.put(key, new Object()); - - service.earlyRemoveAll(); - - assertTrue(isEmpty(cache)); - } - - @Test - public void earlyRemoveAllWithException() { - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(name.getMethodName()); - cache.put(key, new Object()); - - try { - service.earlyRemoveAllWithException(true); - fail("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - // This is what we expect - } - assertTrue(isEmpty(cache)); - } - - @Test - public void earlyRemoveAllWithExceptionVetoRemove() { - Cache cache = getCache(DEFAULT_CACHE); - - Object key = createKey(name.getMethodName()); - cache.put(key, new Object()); - - try { - service.earlyRemoveAllWithException(false); - fail("Should have thrown an exception"); - } - catch (NullPointerException e) { - // This is what we expect - } - // This will be remove anyway as the earlyRemove has removed the cache before - assertTrue(isEmpty(cache)); - } - - protected boolean isEmpty(Cache cache) { - ConcurrentHashMap nativeCache = (ConcurrentHashMap) cache.getNativeCache(); - return nativeCache.isEmpty(); - } - - - private Object createKey(Object... params) { - return SimpleKeyGenerator.generateKey(params); - } - - private Cache getCache(String name) { - Cache cache = cacheManager.getCache(name); - assertNotNull("required cache " + name + " does not exist", cache); - return cache; - } - - /** - * The only purpose of this method is to invoke a particular method on the - * service so that the call stack is different. - */ - private UnsupportedOperationException methodInCallStack(String keyItem) { - try { - service.cacheWithException(keyItem, true); - throw new IllegalStateException("Should have thrown an exception"); - } - catch (UnsupportedOperationException e) { - return e; - } - } - - private boolean contain(Throwable t, String className, String methodName) { - for (StackTraceElement element : t.getStackTrace()) { - if (className.equals(element.getClassName()) && methodName.equals(element.getMethodName())) { - return true; - } - } - return false; - } - -} diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheCustomInterceptorTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheCustomInterceptorTests.java index 2576dc71c21f..6d5714d09574 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheCustomInterceptorTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheCustomInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,9 +20,9 @@ import java.util.Arrays; import java.util.Map; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; @@ -37,8 +37,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.contextsupport.testfixture.jcache.JCacheableService; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Stephane Nicoll @@ -52,14 +54,14 @@ public class JCacheCustomInterceptorTests { protected Cache exceptionCache; - @Before + @BeforeEach public void setup() { ctx = new AnnotationConfigApplicationContext(EnableCachingConfig.class); cs = ctx.getBean("service", JCacheableService.class); exceptionCache = ctx.getBean("exceptionCache", Cache.class); } - @After + @AfterEach public void tearDown() { if (ctx != null) { ctx.close(); @@ -70,30 +72,23 @@ public void tearDown() { @Test public void onlyOneInterceptorIsAvailable() { Map interceptors = ctx.getBeansOfType(JCacheInterceptor.class); - assertEquals("Only one interceptor should be defined", 1, interceptors.size()); + assertThat(interceptors.size()).as("Only one interceptor should be defined").isEqualTo(1); JCacheInterceptor interceptor = interceptors.values().iterator().next(); - assertEquals("Custom interceptor not defined", TestCacheInterceptor.class, interceptor.getClass()); + assertThat(interceptor.getClass()).as("Custom interceptor not defined").isEqualTo(TestCacheInterceptor.class); } @Test public void customInterceptorAppliesWithRuntimeException() { Object o = cs.cacheWithException("id", true); - assertEquals(55L, o); // See TestCacheInterceptor + // See TestCacheInterceptor + assertThat(o).isEqualTo(55L); } @Test public void customInterceptorAppliesWithCheckedException() { - try { - cs.cacheWithCheckedException("id", true); - fail("Should have failed"); - } - catch (RuntimeException e) { - assertNotNull("missing original exception", e.getCause()); - assertEquals(IOException.class, e.getCause().getClass()); - } - catch (Exception e) { - fail("Wrong exception type " + e); - } + assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> + cs.cacheWithCheckedException("id", true)) + .withCauseExactlyInstanceOf(IOException.class); } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheJavaConfigTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheJavaConfigTests.java index 1c72a2a433a6..9288994fc6f0 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheJavaConfigTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheJavaConfigTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,16 +18,13 @@ import java.util.Arrays; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; -import org.springframework.cache.config.SomeKeyGenerator; import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; @@ -45,17 +42,18 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.testfixture.cache.SomeKeyGenerator; +import org.springframework.contextsupport.testfixture.jcache.AbstractJCacheAnnotationTests; +import org.springframework.contextsupport.testfixture.jcache.JCacheableService; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Stephane Nicoll */ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Override protected ApplicationContext getApplicationContext() { return new AnnotationConfigApplicationContext(EnableCachingConfig.class); @@ -68,13 +66,12 @@ public void fullCachingConfig() throws Exception { new AnnotationConfigApplicationContext(FullCachingConfig.class); DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); - assertSame(context.getBean(KeyGenerator.class), cos.getKeyGenerator()); - assertSame(context.getBean("cacheResolver", CacheResolver.class), - cos.getCacheResolver()); - assertSame(context.getBean("exceptionCacheResolver", CacheResolver.class), - cos.getExceptionCacheResolver()); + assertThat(cos.getKeyGenerator()).isSameAs(context.getBean(KeyGenerator.class)); + assertThat(cos.getCacheResolver()).isSameAs(context.getBean("cacheResolver", CacheResolver.class)); + assertThat(cos.getExceptionCacheResolver()).isSameAs(context.getBean("exceptionCacheResolver", CacheResolver.class)); JCacheInterceptor interceptor = context.getBean(JCacheInterceptor.class); - assertSame(context.getBean("errorHandler", CacheErrorHandler.class), interceptor.getErrorHandler()); + assertThat(interceptor.getErrorHandler()).isSameAs(context.getBean("errorHandler", CacheErrorHandler.class)); + context.close(); } @Test @@ -83,11 +80,10 @@ public void emptyConfigSupport() { new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class); DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); - assertNotNull(cos.getCacheResolver()); - assertEquals(SimpleCacheResolver.class, cos.getCacheResolver().getClass()); - assertSame(context.getBean(CacheManager.class), - ((SimpleCacheResolver) cos.getCacheResolver()).getCacheManager()); - assertNull(cos.getExceptionCacheResolver()); + assertThat(cos.getCacheResolver()).isNotNull(); + assertThat(cos.getCacheResolver().getClass()).isEqualTo(SimpleCacheResolver.class); + assertThat(((SimpleCacheResolver) cos.getCacheResolver()).getCacheManager()).isSameAs(context.getBean(CacheManager.class)); + assertThat(cos.getExceptionCacheResolver()).isNull(); context.close(); } @@ -97,9 +93,9 @@ public void bothSetOnlyResolverIsUsed() { new AnnotationConfigApplicationContext(FullCachingConfigSupport.class); DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); - assertSame(context.getBean("cacheResolver"), cos.getCacheResolver()); - assertSame(context.getBean("keyGenerator"), cos.getKeyGenerator()); - assertSame(context.getBean("exceptionCacheResolver"), cos.getExceptionCacheResolver()); + assertThat(cos.getCacheResolver()).isSameAs(context.getBean("cacheResolver")); + assertThat(cos.getKeyGenerator()).isSameAs(context.getBean("keyGenerator")); + assertThat(cos.getExceptionCacheResolver()).isSameAs(context.getBean("exceptionCacheResolver")); context.close(); } @@ -110,14 +106,14 @@ public void exceptionCacheResolverLazilyRequired() { try { DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); - assertSame(context.getBean("cacheResolver"), cos.getCacheResolver()); + assertThat(cos.getCacheResolver()).isSameAs(context.getBean("cacheResolver")); JCacheableService service = context.getBean(JCacheableService.class); service.cache("id"); // This call requires the cache manager to be set - thrown.expect(IllegalStateException.class); - service.cacheWithException("test", false); + assertThatIllegalStateException().isThrownBy(() -> + service.cacheWithException("test", false)); } finally { context.close(); diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheNamespaceDrivenTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheNamespaceDrivenTests.java index 139b8feb35d2..262689f83ad8 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheNamespaceDrivenTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheNamespaceDrivenTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,7 +16,7 @@ package org.springframework.cache.jcache.config; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource; @@ -24,8 +24,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; +import org.springframework.contextsupport.testfixture.jcache.AbstractJCacheAnnotationTests; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -44,14 +45,14 @@ public void cacheResolver() { "/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml"); DefaultJCacheOperationSource ci = context.getBean(DefaultJCacheOperationSource.class); - assertSame(context.getBean("cacheResolver"), ci.getCacheResolver()); + assertThat(ci.getCacheResolver()).isSameAs(context.getBean("cacheResolver")); context.close(); } @Test public void testCacheErrorHandler() { JCacheInterceptor ci = ctx.getBean(JCacheInterceptor.class); - assertSame(ctx.getBean("errorHandler", CacheErrorHandler.class), ci.getErrorHandler()); + assertThat(ci.getErrorHandler()).isSameAs(ctx.getBean("errorHandler", CacheErrorHandler.class)); } } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheStandaloneConfigTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheStandaloneConfigTests.java index 82a6e40e1402..f07e5214850e 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheStandaloneConfigTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheStandaloneConfigTests.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -18,6 +18,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; +import org.springframework.contextsupport.testfixture.jcache.AbstractJCacheAnnotationTests; /** * @author Stephane Nicoll diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java index d885d8182717..c7a0cfcd4776 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AbstractCacheOperationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,17 +18,18 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheMethodDetails; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.cache.jcache.AbstractJCacheTests; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -43,20 +44,18 @@ public abstract class AbstractCacheOperationTests> @Test public void simple() { O operation = createSimpleOperation(); - assertEquals("Wrong cache name", "simpleCache", operation.getCacheName()); - assertEquals("Unexpected number of annotation on " + operation.getMethod(), - 1, operation.getAnnotations().size()); - assertEquals("Wrong method annotation", operation.getCacheAnnotation(), - operation.getAnnotations().iterator().next()); + assertThat(operation.getCacheName()).as("Wrong cache name").isEqualTo("simpleCache"); + assertThat(operation.getAnnotations().size()).as("Unexpected number of annotation on " + operation.getMethod()).isEqualTo(1); + assertThat(operation.getAnnotations().iterator().next()).as("Wrong method annotation").isEqualTo(operation.getCacheAnnotation()); - assertNotNull("cache resolver should be set", operation.getCacheResolver()); + assertThat(operation.getCacheResolver()).as("cache resolver should be set").isNotNull(); } protected void assertCacheInvocationParameter(CacheInvocationParameter actual, Class targetType, Object value, int position) { - assertEquals("wrong parameter type for " + actual, targetType, actual.getRawType()); - assertEquals("wrong parameter value for " + actual, value, actual.getValue()); - assertEquals("wrong parameter position for " + actual, position, actual.getParameterPosition()); + assertThat(actual.getRawType()).as("wrong parameter type for " + actual).isEqualTo(targetType); + assertThat(actual.getValue()).as("wrong parameter value for " + actual).isEqualTo(value); + assertThat(actual.getParameterPosition()).as("wrong parameter position for " + actual).isEqualTo(position); } protected CacheMethodDetails create(Class annotationType, diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotatedJCacheableService.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotatedJCacheableService.java index 57c699e9b992..e5cfb12b64e6 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotatedJCacheableService.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotatedJCacheableService.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; + import javax.cache.annotation.CacheDefaults; import javax.cache.annotation.CacheKey; import javax.cache.annotation.CachePut; @@ -29,9 +30,9 @@ import org.springframework.cache.Cache; import org.springframework.cache.interceptor.SimpleKeyGenerator; -import org.springframework.cache.jcache.config.JCacheableService; -import org.springframework.cache.jcache.support.TestableCacheKeyGenerator; -import org.springframework.cache.jcache.support.TestableCacheResolverFactory; +import org.springframework.contextsupport.testfixture.cache.TestableCacheKeyGenerator; +import org.springframework.contextsupport.testfixture.cache.TestableCacheResolverFactory; +import org.springframework.contextsupport.testfixture.jcache.JCacheableService; /** * Repository sample with a @CacheDefaults annotation diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java index 81c4256b1767..bcc9276243bd 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,27 +18,30 @@ import java.lang.reflect.Method; import java.util.Comparator; + import javax.cache.annotation.CacheDefaults; import javax.cache.annotation.CacheKeyGenerator; import javax.cache.annotation.CacheRemove; import javax.cache.annotation.CacheRemoveAll; import javax.cache.annotation.CacheResult; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.jcache.AbstractJCacheTests; -import org.springframework.cache.jcache.support.TestableCacheKeyGenerator; -import org.springframework.cache.jcache.support.TestableCacheResolver; -import org.springframework.cache.jcache.support.TestableCacheResolverFactory; +import org.springframework.contextsupport.testfixture.cache.TestableCacheKeyGenerator; +import org.springframework.contextsupport.testfixture.cache.TestableCacheResolver; +import org.springframework.contextsupport.testfixture.cache.TestableCacheResolverFactory; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * @author Stephane Nicoll @@ -50,7 +53,7 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests { private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - @Before + @BeforeEach public void setup() { source.setCacheResolver(defaultCacheResolver); source.setExceptionCacheResolver(defaultExceptionCacheResolver); @@ -63,15 +66,15 @@ public void setup() { public void cache() { CacheResultOperation op = getDefaultCacheOperation(CacheResultOperation.class, String.class); assertDefaults(op); - assertNull("Exception caching not enabled so resolver should not be set", op.getExceptionCacheResolver()); + assertThat(op.getExceptionCacheResolver()).as("Exception caching not enabled so resolver should not be set").isNull(); } @Test public void cacheWithException() { CacheResultOperation op = getDefaultCacheOperation(CacheResultOperation.class, String.class, boolean.class); assertDefaults(op); - assertEquals(defaultExceptionCacheResolver, op.getExceptionCacheResolver()); - assertEquals("exception", op.getExceptionCacheName()); + assertThat(op.getExceptionCacheResolver()).isEqualTo(defaultExceptionCacheResolver); + assertThat(op.getExceptionCacheName()).isEqualTo("exception"); } @Test @@ -89,24 +92,23 @@ public void remove() { @Test public void removeAll() { CacheRemoveAllOperation op = getDefaultCacheOperation(CacheRemoveAllOperation.class); - assertEquals(defaultCacheResolver, op.getCacheResolver()); + assertThat(op.getCacheResolver()).isEqualTo(defaultCacheResolver); } @Test public void noAnnotation() { - assertNull(getCacheOperation(AnnotatedJCacheableService.class, name.getMethodName())); + assertThat(getCacheOperation(AnnotatedJCacheableService.class, this.cacheName)).isNull(); } @Test public void multiAnnotations() { - thrown.expect(IllegalStateException.class); - getCacheOperation(InvalidCases.class, name.getMethodName()); + assertThatIllegalStateException().isThrownBy(() -> getCacheOperation(InvalidCases.class, this.cacheName)); } @Test public void defaultCacheNameWithCandidate() { Method method = ReflectionUtils.findMethod(Object.class, "toString"); - assertEquals("foo", source.determineCacheName(method, null, "foo")); + assertThat(source.determineCacheName(method, null, "foo")).isEqualTo("foo"); } @Test @@ -114,38 +116,37 @@ public void defaultCacheNameWithDefaults() { Method method = ReflectionUtils.findMethod(Object.class, "toString"); CacheDefaults mock = mock(CacheDefaults.class); given(mock.cacheName()).willReturn(""); - assertEquals("java.lang.Object.toString()", source.determineCacheName(method, mock, "")); + assertThat(source.determineCacheName(method, mock, "")).isEqualTo("java.lang.Object.toString()"); } @Test public void defaultCacheNameNoDefaults() { Method method = ReflectionUtils.findMethod(Object.class, "toString"); - assertEquals("java.lang.Object.toString()", source.determineCacheName(method, null, "")); + assertThat(source.determineCacheName(method, null, "")).isEqualTo("java.lang.Object.toString()"); } @Test public void defaultCacheNameWithParameters() { Method method = ReflectionUtils.findMethod(Comparator.class, "compare", Object.class, Object.class); - assertEquals("java.util.Comparator.compare(java.lang.Object,java.lang.Object)", - source.determineCacheName(method, null, "")); + assertThat(source.determineCacheName(method, null, "")).isEqualTo("java.util.Comparator.compare(java.lang.Object,java.lang.Object)"); } @Test public void customCacheResolver() { CacheResultOperation operation = - getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class); + getCacheOperation(CacheResultOperation.class, CustomService.class, this.cacheName, Long.class); assertJCacheResolver(operation.getCacheResolver(), TestableCacheResolver.class); assertJCacheResolver(operation.getExceptionCacheResolver(), null); - assertEquals(KeyGeneratorAdapter.class, operation.getKeyGenerator().getClass()); - assertEquals(defaultKeyGenerator, ((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget()); + assertThat(operation.getKeyGenerator().getClass()).isEqualTo(KeyGeneratorAdapter.class); + assertThat(((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget()).isEqualTo(defaultKeyGenerator); } @Test public void customKeyGenerator() { CacheResultOperation operation = - getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class); - assertEquals(defaultCacheResolver, operation.getCacheResolver()); - assertNull(operation.getExceptionCacheResolver()); + getCacheOperation(CacheResultOperation.class, CustomService.class, this.cacheName, Long.class); + assertThat(operation.getCacheResolver()).isEqualTo(defaultCacheResolver); + assertThat(operation.getExceptionCacheResolver()).isNull(); assertCacheKeyGenerator(operation.getKeyGenerator(), TestableCacheKeyGenerator.class); } @@ -154,17 +155,18 @@ public void customKeyGeneratorSpringBean() { TestableCacheKeyGenerator bean = new TestableCacheKeyGenerator(); beanFactory.registerSingleton("fooBar", bean); CacheResultOperation operation = - getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class); - assertEquals(defaultCacheResolver, operation.getCacheResolver()); - assertNull(operation.getExceptionCacheResolver()); + getCacheOperation(CacheResultOperation.class, CustomService.class, this.cacheName, Long.class); + assertThat(operation.getCacheResolver()).isEqualTo(defaultCacheResolver); + assertThat(operation.getExceptionCacheResolver()).isNull(); KeyGeneratorAdapter adapter = (KeyGeneratorAdapter) operation.getKeyGenerator(); - assertSame(bean, adapter.getTarget()); // take bean from context + // take bean from context + assertThat(adapter.getTarget()).isSameAs(bean); } @Test public void customKeyGeneratorAndCacheResolver() { CacheResultOperation operation = getCacheOperation(CacheResultOperation.class, - CustomServiceWithDefaults.class, name.getMethodName(), Long.class); + CustomServiceWithDefaults.class, this.cacheName, Long.class); assertJCacheResolver(operation.getCacheResolver(), TestableCacheResolver.class); assertJCacheResolver(operation.getExceptionCacheResolver(), null); assertCacheKeyGenerator(operation.getKeyGenerator(), TestableCacheKeyGenerator.class); @@ -173,28 +175,28 @@ public void customKeyGeneratorAndCacheResolver() { @Test public void customKeyGeneratorAndCacheResolverWithExceptionName() { CacheResultOperation operation = getCacheOperation(CacheResultOperation.class, - CustomServiceWithDefaults.class, name.getMethodName(), Long.class); + CustomServiceWithDefaults.class, this.cacheName, Long.class); assertJCacheResolver(operation.getCacheResolver(), TestableCacheResolver.class); assertJCacheResolver(operation.getExceptionCacheResolver(), TestableCacheResolver.class); assertCacheKeyGenerator(operation.getKeyGenerator(), TestableCacheKeyGenerator.class); } private void assertDefaults(AbstractJCacheKeyOperation operation) { - assertEquals(defaultCacheResolver, operation.getCacheResolver()); - assertEquals(KeyGeneratorAdapter.class, operation.getKeyGenerator().getClass()); - assertEquals(defaultKeyGenerator, ((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget()); + assertThat(operation.getCacheResolver()).isEqualTo(defaultCacheResolver); + assertThat(operation.getKeyGenerator().getClass()).isEqualTo(KeyGeneratorAdapter.class); + assertThat(((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget()).isEqualTo(defaultKeyGenerator); } protected > T getDefaultCacheOperation(Class operationType, Class... parameterTypes) { - return getCacheOperation(operationType, AnnotatedJCacheableService.class, name.getMethodName(), parameterTypes); + return getCacheOperation(operationType, AnnotatedJCacheableService.class, this.cacheName, parameterTypes); } protected > T getCacheOperation( Class operationType, Class targetType, String methodName, Class... parameterTypes) { JCacheOperation result = getCacheOperation(targetType, methodName, parameterTypes); - assertNotNull(result); - assertEquals(operationType, result.getClass()); + assertThat(result).isNotNull(); + assertThat(result.getClass()).isEqualTo(operationType); return operationType.cast(result); } @@ -208,20 +210,20 @@ private void assertJCacheResolver(CacheResolver actual, Class expectedTargetType) { if (expectedTargetType == null) { - assertNull(actual); + assertThat(actual).isNull(); } else { - assertEquals("Wrong cache resolver implementation", CacheResolverAdapter.class, actual.getClass()); + assertThat(actual.getClass()).as("Wrong cache resolver implementation").isEqualTo(CacheResolverAdapter.class); CacheResolverAdapter adapter = (CacheResolverAdapter) actual; - assertEquals("Wrong target JCache implementation", expectedTargetType, adapter.getTarget().getClass()); + assertThat(adapter.getTarget().getClass()).as("Wrong target JCache implementation").isEqualTo(expectedTargetType); } } private void assertCacheKeyGenerator(KeyGenerator actual, Class expectedTargetType) { - assertEquals("Wrong cache resolver implementation", KeyGeneratorAdapter.class, actual.getClass()); + assertThat(actual.getClass()).as("Wrong cache resolver implementation").isEqualTo(KeyGeneratorAdapter.class); KeyGeneratorAdapter adapter = (KeyGeneratorAdapter) actual; - assertEquals("Wrong target CacheKeyGenerator implementation", expectedTargetType, adapter.getTarget().getClass()); + assertThat(adapter.getTarget().getClass()).as("Wrong target CacheKeyGenerator implementation").isEqualTo(expectedTargetType); } @@ -244,8 +246,7 @@ public Object customCacheResolver(Long id) { } - @CacheDefaults(cacheResolverFactory = TestableCacheResolverFactory.class, - cacheKeyGenerator = TestableCacheKeyGenerator.class) + @CacheDefaults(cacheResolverFactory = TestableCacheResolverFactory.class, cacheKeyGenerator = TestableCacheKeyGenerator.class) static class CustomServiceWithDefaults { @CacheResult diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CachePutOperationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CachePutOperationTests.java index 6c0e1832c4bd..534094c0c934 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CachePutOperationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CachePutOperationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,13 +17,16 @@ package org.springframework.cache.jcache.interceptor; import java.io.IOException; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CachePut; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Stephane Nicoll @@ -42,12 +45,12 @@ public void simplePut() { CachePutOperation operation = createSimpleOperation(); CacheInvocationParameter[] allParameters = operation.getAllParameters(2L, sampleInstance); - assertEquals(2, allParameters.length); + assertThat(allParameters.length).isEqualTo(2); assertCacheInvocationParameter(allParameters[0], Long.class, 2L, 0); assertCacheInvocationParameter(allParameters[1], SampleObject.class, sampleInstance, 1); CacheInvocationParameter valueParameter = operation.getValueParameter(2L, sampleInstance); - assertNotNull(valueParameter); + assertThat(valueParameter).isNotNull(); assertCacheInvocationParameter(valueParameter, SampleObject.class, sampleInstance, 1); } @@ -56,8 +59,8 @@ public void noCacheValue() { CacheMethodDetails methodDetails = create(CachePut.class, SampleObject.class, "noCacheValue", Long.class); - thrown.expect(IllegalArgumentException.class); - createDefaultOperation(methodDetails); + assertThatIllegalArgumentException().isThrownBy(() -> + createDefaultOperation(methodDetails)); } @Test @@ -65,16 +68,16 @@ public void multiCacheValues() { CacheMethodDetails methodDetails = create(CachePut.class, SampleObject.class, "multiCacheValues", Long.class, SampleObject.class, SampleObject.class); - thrown.expect(IllegalArgumentException.class); - createDefaultOperation(methodDetails); + assertThatIllegalArgumentException().isThrownBy(() -> + createDefaultOperation(methodDetails)); } @Test public void invokeWithWrongParameters() { CachePutOperation operation = createSimpleOperation(); - thrown.expect(IllegalStateException.class); - operation.getValueParameter(2L); + assertThatIllegalStateException().isThrownBy(() -> + operation.getValueParameter(2L)); } @Test @@ -82,10 +85,10 @@ public void fullPutConfig() { CacheMethodDetails methodDetails = create(CachePut.class, SampleObject.class, "fullPutConfig", Long.class, SampleObject.class); CachePutOperation operation = createDefaultOperation(methodDetails); - assertTrue(operation.isEarlyPut()); - assertNotNull(operation.getExceptionTypeFilter()); - assertTrue(operation.getExceptionTypeFilter().match(IOException.class)); - assertFalse(operation.getExceptionTypeFilter().match(NullPointerException.class)); + assertThat(operation.isEarlyPut()).isTrue(); + assertThat(operation.getExceptionTypeFilter()).isNotNull(); + assertThat(operation.getExceptionTypeFilter().match(IOException.class)).isTrue(); + assertThat(operation.getExceptionTypeFilter().match(NullPointerException.class)).isFalse(); } private CachePutOperation createDefaultOperation(CacheMethodDetails methodDetails) { diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperationTests.java index 1dc263b3d349..7327a4059c50 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllOperationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,9 +20,9 @@ import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CacheRemoveAll; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -42,7 +42,7 @@ public void simpleRemoveAll() { CacheRemoveAllOperation operation = createSimpleOperation(); CacheInvocationParameter[] allParameters = operation.getAllParameters(); - assertEquals(0, allParameters.length); + assertThat(allParameters.length).isEqualTo(0); } } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperationTests.java index 668a18d0fa24..b8ae9595a1b3 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheRemoveOperationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,9 +20,9 @@ import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CacheRemove; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -42,7 +42,7 @@ public void simpleRemove() { CacheRemoveOperation operation = createSimpleOperation(); CacheInvocationParameter[] allParameters = operation.getAllParameters(2L); - assertEquals(1, allParameters.length); + assertThat(allParameters.length).isEqualTo(1); assertCacheInvocationParameter(allParameters[0], Long.class, 2L, 0); } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java index f97225b4377e..1b1e91495b71 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,38 +19,35 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Collection; + import javax.cache.annotation.CacheInvocationContext; import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CacheResolver; import javax.cache.annotation.CacheResult; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; import org.springframework.cache.jcache.AbstractJCacheTests; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * @author Stephane Nicoll */ public class CacheResolverAdapterTests extends AbstractJCacheTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - - @Test public void resolveSimpleCache() throws Exception { DefaultCacheInvocationContext dummyContext = createDummyContext(); CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, "testCache")); Collection caches = adapter.resolveCaches(dummyContext); - assertNotNull(caches); - assertEquals(1, caches.size()); - assertEquals("testCache", caches.iterator().next().getName()); + assertThat(caches).isNotNull(); + assertThat(caches.size()).isEqualTo(1); + assertThat(caches.iterator().next().getName()).isEqualTo("testCache"); } @Test @@ -58,10 +55,11 @@ public void resolveUnknownCache() throws Exception { DefaultCacheInvocationContext dummyContext = createDummyContext(); CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, null)); - thrown.expect(IllegalStateException.class); - adapter.resolveCaches(dummyContext); + assertThatIllegalStateException().isThrownBy(() -> + adapter.resolveCaches(dummyContext)); } + @SuppressWarnings({ "rawtypes", "unchecked" }) protected CacheResolver getCacheResolver(CacheInvocationContext context, String cacheName) { CacheResolver cacheResolver = mock(CacheResolver.class); javax.cache.Cache cache; diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResultOperationTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResultOperationTests.java index 09662a4f3f8f..aff8f3ebe611 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResultOperationTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResultOperationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,16 +19,18 @@ import java.io.IOException; import java.lang.annotation.Annotation; import java.util.Set; + import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheKey; import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CacheResult; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Stephane Nicoll @@ -48,18 +50,18 @@ protected CacheResultOperation createSimpleOperation() { public void simpleGet() { CacheResultOperation operation = createSimpleOperation(); - assertNotNull(operation.getKeyGenerator()); - assertNotNull(operation.getExceptionCacheResolver()); + assertThat(operation.getKeyGenerator()).isNotNull(); + assertThat(operation.getExceptionCacheResolver()).isNotNull(); - assertNull(operation.getExceptionCacheName()); - assertEquals(defaultExceptionCacheResolver, operation.getExceptionCacheResolver()); + assertThat(operation.getExceptionCacheName()).isNull(); + assertThat(operation.getExceptionCacheResolver()).isEqualTo(defaultExceptionCacheResolver); CacheInvocationParameter[] allParameters = operation.getAllParameters(2L); - assertEquals(1, allParameters.length); + assertThat(allParameters.length).isEqualTo(1); assertCacheInvocationParameter(allParameters[0], Long.class, 2L, 0); CacheInvocationParameter[] keyParameters = operation.getKeyParameters(2L); - assertEquals(1, keyParameters.length); + assertThat(keyParameters.length).isEqualTo(1); assertCacheInvocationParameter(keyParameters[0], Long.class, 2L, 0); } @@ -70,7 +72,7 @@ public void multiParameterKey() { CacheResultOperation operation = createDefaultOperation(methodDetails); CacheInvocationParameter[] keyParameters = operation.getKeyParameters(3L, Boolean.TRUE, "Foo"); - assertEquals(2, keyParameters.length); + assertThat(keyParameters.length).isEqualTo(2); assertCacheInvocationParameter(keyParameters[0], Long.class, 3L, 0); assertCacheInvocationParameter(keyParameters[1], String.class, "Foo", 2); } @@ -81,8 +83,9 @@ public void invokeWithWrongParameters() { SampleObject.class, "anotherSimpleGet", String.class, Long.class); CacheResultOperation operation = createDefaultOperation(methodDetails); - thrown.expect(IllegalStateException.class); - operation.getAllParameters("bar"); // missing one argument + // missing one argument + assertThatIllegalStateException().isThrownBy(() -> + operation.getAllParameters("bar")); } @Test @@ -91,8 +94,9 @@ public void tooManyKeyValues() { SampleObject.class, "anotherSimpleGet", String.class, Long.class); CacheResultOperation operation = createDefaultOperation(methodDetails); - thrown.expect(IllegalStateException.class); - operation.getKeyParameters("bar"); // missing one argument + // missing one argument + assertThatIllegalStateException().isThrownBy(() -> + operation.getKeyParameters("bar")); } @Test @@ -103,12 +107,12 @@ public void annotatedGet() { CacheInvocationParameter[] parameters = operation.getAllParameters(2L, "foo"); Set firstParameterAnnotations = parameters[0].getAnnotations(); - assertEquals(1, firstParameterAnnotations.size()); - assertEquals(CacheKey.class, firstParameterAnnotations.iterator().next().annotationType()); + assertThat(firstParameterAnnotations.size()).isEqualTo(1); + assertThat(firstParameterAnnotations.iterator().next().annotationType()).isEqualTo(CacheKey.class); Set secondParameterAnnotations = parameters[1].getAnnotations(); - assertEquals(1, secondParameterAnnotations.size()); - assertEquals(Value.class, secondParameterAnnotations.iterator().next().annotationType()); + assertThat(secondParameterAnnotations.size()).isEqualTo(1); + assertThat(secondParameterAnnotations.iterator().next().annotationType()).isEqualTo(Value.class); } @Test @@ -116,10 +120,10 @@ public void fullGetConfig() { CacheMethodDetails methodDetails = create(CacheResult.class, SampleObject.class, "fullGetConfig", Long.class); CacheResultOperation operation = createDefaultOperation(methodDetails); - assertTrue(operation.isAlwaysInvoked()); - assertNotNull(operation.getExceptionTypeFilter()); - assertTrue(operation.getExceptionTypeFilter().match(IOException.class)); - assertFalse(operation.getExceptionTypeFilter().match(NullPointerException.class)); + assertThat(operation.isAlwaysInvoked()).isTrue(); + assertThat(operation.getExceptionTypeFilter()).isNotNull(); + assertThat(operation.getExceptionTypeFilter().match(IOException.class)).isTrue(); + assertThat(operation.getExceptionTypeFilter().match(NullPointerException.class)).isFalse(); } private CacheResultOperation createDefaultOperation(CacheMethodDetails methodDetails) { diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java index da4435b1d877..3b341a0c4df0 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,6 +18,7 @@ import java.util.Arrays; import java.util.concurrent.atomic.AtomicLong; + import javax.cache.annotation.CacheDefaults; import javax.cache.annotation.CachePut; import javax.cache.annotation.CacheRemove; @@ -25,10 +26,8 @@ import javax.cache.annotation.CacheResult; import javax.cache.annotation.CacheValue; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; @@ -41,8 +40,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; /** * @author Stephane Nicoll @@ -57,17 +59,15 @@ public class JCacheErrorHandlerTests { private SimpleService simpleService; - @Rule - public final ExpectedException thrown = ExpectedException.none(); - - @Before + @BeforeEach public void setup() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); this.cache = context.getBean("mockCache", Cache.class); this.errorCache = context.getBean("mockErrorCache", Cache.class); this.errorHandler = context.getBean(CacheErrorHandler.class); this.simpleService = context.getBean(SimpleService.class); + context.close(); } @@ -103,7 +103,7 @@ public void getFailPutExceptionFail() { this.simpleService.getFail(0L); } catch (IllegalStateException ex) { - assertEquals("Test exception", ex.getMessage()); + assertThat(ex.getMessage()).isEqualTo("Test exception"); } verify(this.errorHandler).handleCachePutError( exceptionOnPut, this.errorCache, key, SimpleService.TEST_EXCEPTION); diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheInterceptorTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheInterceptorTests.java index 029db9114b61..0c6671ebbb07 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheInterceptorTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheInterceptorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,7 +18,7 @@ import java.lang.reflect.Method; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.support.StaticListableBeanFactory; import org.springframework.cache.CacheManager; @@ -29,7 +29,8 @@ import org.springframework.cache.jcache.AbstractJCacheTests; import org.springframework.util.ReflectionUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Stephane Nicoll @@ -47,15 +48,9 @@ cacheManager, new NamedCacheResolver(cacheManager, "default", "simpleCache"), AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); - try { - interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}); - } - catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("JSR-107 only supports a single cache")); - } - catch (Throwable ex) { - fail("Unexpected: " + ex); - } + assertThatIllegalStateException().isThrownBy(() -> + interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"})) + .withMessageContaining("JSR-107 only supports a single cache"); } @Test @@ -66,22 +61,15 @@ cacheManager, new NamedCacheResolver(cacheManager), // Returns empty list AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); - - try { - interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}); - } - catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("Cache could not have been resolved for")); - } - catch (Throwable ex) { - fail("Unexpected: " + ex); - } + assertThatIllegalStateException().isThrownBy(() -> + interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"})) + .withMessageContaining("Cache could not have been resolved for"); } @Test public void cacheManagerMandatoryIfCacheResolverNotSet() { - thrown.expect(IllegalStateException.class); - createOperationSource(null, null, null, defaultKeyGenerator); + assertThatIllegalStateException().isThrownBy(() -> + createOperationSource(null, null, null, defaultKeyGenerator)); } @Test @@ -99,9 +87,9 @@ public void cacheResultReturnsProperType() throws Throwable { CacheOperationInvoker invoker = new DummyInvoker(0L); Object execute = interceptor.execute(invoker, service, method, new Object[] {"myId"}); - assertNotNull("result cannot be null.", execute); - assertEquals("Wrong result type", Long.class, execute.getClass()); - assertEquals("Wrong result", 0L, execute); + assertThat(execute).as("result cannot be null.").isNotNull(); + assertThat(execute.getClass()).as("Wrong result type").isEqualTo(Long.class); + assertThat(execute).as("Wrong result").isEqualTo(0L); } protected JCacheOperationSource createOperationSource(CacheManager cacheManager, diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheKeyGeneratorTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheKeyGeneratorTests.java index 0e61f73dc021..dd5497764b0c 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheKeyGeneratorTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheKeyGeneratorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -19,12 +19,13 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.concurrent.atomic.AtomicLong; + import javax.cache.annotation.CacheDefaults; import javax.cache.annotation.CacheKey; import javax.cache.annotation.CacheResult; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; @@ -38,10 +39,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** - * * @author Stephane Nicoll */ public class JCacheKeyGeneratorTests { @@ -52,12 +52,13 @@ public class JCacheKeyGeneratorTests { private Cache cache; - @Before + @BeforeEach public void setup() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); this.keyGenerator = context.getBean(TestKeyGenerator.class); this.simpleService = context.getBean(SimpleService.class); this.cache = context.getBean(CacheManager.class).getCache("test"); + context.close(); } @Test @@ -65,10 +66,10 @@ public void getSimple() { this.keyGenerator.expect(1L); Object first = this.simpleService.get(1L); Object second = this.simpleService.get(1L); - assertSame(first, second); + assertThat(second).isSameAs(first); Object key = new SimpleKey(1L); - assertEquals(first, cache.get(key).get()); + assertThat(cache.get(key).get()).isEqualTo(first); } @Test @@ -76,10 +77,10 @@ public void getFlattenVararg() { this.keyGenerator.expect(1L, "foo", "bar"); Object first = this.simpleService.get(1L, "foo", "bar"); Object second = this.simpleService.get(1L, "foo", "bar"); - assertSame(first, second); + assertThat(second).isSameAs(first); Object key = new SimpleKey(1L, "foo", "bar"); - assertEquals(first, cache.get(key).get()); + assertThat(cache.get(key).get()).isEqualTo(first); } @Test @@ -87,10 +88,10 @@ public void getFiltered() { this.keyGenerator.expect(1L); Object first = this.simpleService.getFiltered(1L, "foo", "bar"); Object second = this.simpleService.getFiltered(1L, "foo", "bar"); - assertSame(first, second); + assertThat(second).isSameAs(first); Object key = new SimpleKey(1L); - assertEquals(first, cache.get(key).get()); + assertThat(cache.get(key).get()).isEqualTo(first); } @@ -149,9 +150,8 @@ private void expect(Object... params) { @Override public Object generate(Object target, Method method, Object... params) { - assertTrue("Unexpected parameters: expected: " - + Arrays.toString(this.expectedParams) + " but got: " + Arrays.toString(params), - Arrays.equals(expectedParams, params)); + assertThat(Arrays.equals(expectedParams, params)).as("Unexpected parameters: expected: " + + Arrays.toString(this.expectedParams) + " but got: " + Arrays.toString(params)).isTrue(); return new SimpleKey(params); } } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/SampleObject.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/SampleObject.java index 60a2ee5fd57e..f0de3c094a67 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/SampleObject.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/SampleObject.java @@ -1,3 +1,19 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cache.jcache.interceptor; import javax.cache.annotation.CacheKey; diff --git a/spring-context-support/src/test/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManagerTests.java b/spring-context-support/src/test/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManagerTests.java index a86bd573cd85..cf0487eb71cb 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManagerTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/transaction/AbstractTransactionSupportingCacheManagerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,28 +16,33 @@ package org.springframework.cache.transaction; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * Shared tests for {@link CacheManager} that inherit from * {@link AbstractTransactionSupportingCacheManager}. * * @author Stephane Nicoll + * @author Sam Brannen */ public abstract class AbstractTransactionSupportingCacheManagerTests { public static final String CACHE_NAME = "testCacheManager"; - @Rule - public final TestName name = new TestName(); + + protected String cacheName; + + @BeforeEach + void trackCacheName(TestInfo testInfo) { + this.cacheName = testInfo.getTestMethod().get().getName(); + } /** @@ -67,51 +72,48 @@ public abstract class AbstractTransactionSupportingCacheManagerTests new TransactionAwareCacheDecorator(null)); } @Test public void getTargetCache() { Cache target = new ConcurrentMapCache("testCache"); TransactionAwareCacheDecorator cache = new TransactionAwareCacheDecorator(target); - assertSame(target, cache.getTargetCache()); + assertThat(cache.getTargetCache()).isSameAs(target); } @Test public void regularOperationsOnTarget() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); - assertEquals(target.getName(), cache.getName()); - assertEquals(target.getNativeCache(), cache.getNativeCache()); + assertThat(cache.getName()).isEqualTo(target.getName()); + assertThat(cache.getNativeCache()).isEqualTo(target.getNativeCache()); Object key = new Object(); target.put(key, "123"); - assertEquals("123", cache.get(key).get()); - assertEquals("123", cache.get(key, String.class)); + assertThat(cache.get(key).get()).isEqualTo("123"); + assertThat(cache.get(key, String.class)).isEqualTo("123"); cache.clear(); - assertNull(target.get(key)); + assertThat(target.get(key)).isNull(); } @Test @@ -76,35 +70,51 @@ public void putNonTransactional() { Object key = new Object(); cache.put(key, "123"); - assertEquals("123", target.get(key, String.class)); + assertThat(target.get(key, String.class)).isEqualTo("123"); } @Test public void putTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); - - TransactionStatus status = this.txManager.getTransaction( - new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); - Object key = new Object(); - cache.put(key, "123"); - assertNull(target.get(key)); - this.txManager.commit(status); - assertEquals("123", target.get(key, String.class)); + txTemplate.executeWithoutResult(s -> { + cache.put(key, "123"); + assertThat(target.get(key)).isNull(); + }); + + assertThat(target.get(key, String.class)).isEqualTo("123"); } @Test - public void putIfAbsent() { // no transactional support for putIfAbsent + public void putIfAbsentNonTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); Object key = new Object(); - assertNull(cache.putIfAbsent(key, "123")); - assertEquals("123", target.get(key, String.class)); - assertEquals("123", cache.putIfAbsent(key, "456").get()); - assertEquals("123", target.get(key, String.class)); // unchanged + assertThat(cache.putIfAbsent(key, "123")).isNull(); + assertThat(target.get(key, String.class)).isEqualTo("123"); + assertThat(cache.putIfAbsent(key, "456").get()).isEqualTo("123"); + // unchanged + assertThat(target.get(key, String.class)).isEqualTo("123"); + } + + @Test + public void putIfAbsentTransactional() { // no transactional support for putIfAbsent + Cache target = new ConcurrentMapCache("testCache"); + Cache cache = new TransactionAwareCacheDecorator(target); + Object key = new Object(); + + txTemplate.executeWithoutResult(s -> { + assertThat(cache.putIfAbsent(key, "123")).isNull(); + assertThat(target.get(key, String.class)).isEqualTo("123"); + assertThat(cache.putIfAbsent(key, "456").get()).isEqualTo("123"); + // unchanged + assertThat(target.get(key, String.class)).isEqualTo("123"); + }); + + assertThat(target.get(key, String.class)).isEqualTo("123"); } @Test @@ -115,7 +125,7 @@ public void evictNonTransactional() { cache.put(key, "123"); cache.evict(key); - assertNull(target.get(key)); + assertThat(target.get(key)).isNull(); } @Test @@ -125,14 +135,38 @@ public void evictTransactional() { Object key = new Object(); cache.put(key, "123"); + txTemplate.executeWithoutResult(s -> { + cache.evict(key); + assertThat(target.get(key, String.class)).isEqualTo("123"); + }); - TransactionStatus status = this.txManager.getTransaction( - new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); - cache.evict(key); - assertEquals("123", target.get(key, String.class)); - this.txManager.commit(status); + assertThat(target.get(key)).isNull(); + } + + @Test + public void evictIfPresentNonTransactional() { + Cache target = new ConcurrentMapCache("testCache"); + Cache cache = new TransactionAwareCacheDecorator(target); + Object key = new Object(); + cache.put(key, "123"); - assertNull(target.get(key)); + cache.evictIfPresent(key); + assertThat(target.get(key)).isNull(); + } + + @Test + public void evictIfPresentTransactional() { // no transactional support for evictIfPresent + Cache target = new ConcurrentMapCache("testCache"); + Cache cache = new TransactionAwareCacheDecorator(target); + Object key = new Object(); + cache.put(key, "123"); + + txTemplate.executeWithoutResult(s -> { + cache.evictIfPresent(key); + assertThat(target.get(key)).isNull(); + }); + + assertThat(target.get(key)).isNull(); } @Test @@ -143,7 +177,7 @@ public void clearNonTransactional() { cache.put(key, "123"); cache.clear(); - assertNull(target.get(key)); + assertThat(target.get(key)).isNull(); } @Test @@ -153,13 +187,38 @@ public void clearTransactional() { Object key = new Object(); cache.put(key, "123"); + txTemplate.executeWithoutResult(s -> { + cache.clear(); + assertThat(target.get(key, String.class)).isEqualTo("123"); + }); - TransactionStatus status = this.txManager.getTransaction( - new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); - cache.clear(); - assertEquals("123", target.get(key, String.class)); - this.txManager.commit(status); + assertThat(target.get(key)).isNull(); + } - assertNull(target.get(key)); + @Test + public void invalidateNonTransactional() { + Cache target = new ConcurrentMapCache("testCache"); + Cache cache = new TransactionAwareCacheDecorator(target); + Object key = new Object(); + cache.put(key, "123"); + + cache.invalidate(); + assertThat(target.get(key)).isNull(); } + + @Test + public void invalidateTransactional() { // no transactional support for invalidate + Cache target = new ConcurrentMapCache("testCache"); + Cache cache = new TransactionAwareCacheDecorator(target); + Object key = new Object(); + cache.put(key, "123"); + + txTemplate.executeWithoutResult(s -> { + cache.invalidate(); + assertThat(target.get(key)).isNull(); + }); + + assertThat(target.get(key)).isNull(); + } + } diff --git a/spring-context-support/src/test/java/org/springframework/mail/SimpleMailMessageTests.java b/spring-context-support/src/test/java/org/springframework/mail/SimpleMailMessageTests.java index 1172c7afe29e..5312152ce4ce 100644 --- a/spring-context-support/src/test/java/org/springframework/mail/SimpleMailMessageTests.java +++ b/spring-context-support/src/test/java/org/springframework/mail/SimpleMailMessageTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,9 +20,10 @@ import java.util.Date; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Dmitriy Kopylenko @@ -40,8 +41,8 @@ public void testSimpleMessageCopyCtor() { message.setTo("you@mail.org"); SimpleMailMessage messageCopy = new SimpleMailMessage(message); - assertEquals("me@mail.org", messageCopy.getFrom()); - assertEquals("you@mail.org", messageCopy.getTo()[0]); + assertThat(messageCopy.getFrom()).isEqualTo("me@mail.org"); + assertThat(messageCopy.getTo()[0]).isEqualTo("you@mail.org"); message.setReplyTo("reply@mail.org"); message.setCc(new String[]{"he@mail.org", "she@mail.org"}); @@ -51,32 +52,32 @@ public void testSimpleMessageCopyCtor() { message.setSubject("my subject"); message.setText("my text"); - assertEquals("me@mail.org", message.getFrom()); - assertEquals("reply@mail.org", message.getReplyTo()); - assertEquals("you@mail.org", message.getTo()[0]); + assertThat(message.getFrom()).isEqualTo("me@mail.org"); + assertThat(message.getReplyTo()).isEqualTo("reply@mail.org"); + assertThat(message.getTo()[0]).isEqualTo("you@mail.org"); List ccs = Arrays.asList(message.getCc()); - assertTrue(ccs.contains("he@mail.org")); - assertTrue(ccs.contains("she@mail.org")); + assertThat(ccs.contains("he@mail.org")).isTrue(); + assertThat(ccs.contains("she@mail.org")).isTrue(); List bccs = Arrays.asList(message.getBcc()); - assertTrue(bccs.contains("us@mail.org")); - assertTrue(bccs.contains("them@mail.org")); - assertEquals(sentDate, message.getSentDate()); - assertEquals("my subject", message.getSubject()); - assertEquals("my text", message.getText()); + assertThat(bccs.contains("us@mail.org")).isTrue(); + assertThat(bccs.contains("them@mail.org")).isTrue(); + assertThat(message.getSentDate()).isEqualTo(sentDate); + assertThat(message.getSubject()).isEqualTo("my subject"); + assertThat(message.getText()).isEqualTo("my text"); messageCopy = new SimpleMailMessage(message); - assertEquals("me@mail.org", messageCopy.getFrom()); - assertEquals("reply@mail.org", messageCopy.getReplyTo()); - assertEquals("you@mail.org", messageCopy.getTo()[0]); + assertThat(messageCopy.getFrom()).isEqualTo("me@mail.org"); + assertThat(messageCopy.getReplyTo()).isEqualTo("reply@mail.org"); + assertThat(messageCopy.getTo()[0]).isEqualTo("you@mail.org"); ccs = Arrays.asList(messageCopy.getCc()); - assertTrue(ccs.contains("he@mail.org")); - assertTrue(ccs.contains("she@mail.org")); + assertThat(ccs.contains("he@mail.org")).isTrue(); + assertThat(ccs.contains("she@mail.org")).isTrue(); bccs = Arrays.asList(message.getBcc()); - assertTrue(bccs.contains("us@mail.org")); - assertTrue(bccs.contains("them@mail.org")); - assertEquals(sentDate, messageCopy.getSentDate()); - assertEquals("my subject", messageCopy.getSubject()); - assertEquals("my text", messageCopy.getText()); + assertThat(bccs.contains("us@mail.org")).isTrue(); + assertThat(bccs.contains("them@mail.org")).isTrue(); + assertThat(messageCopy.getSentDate()).isEqualTo(sentDate); + assertThat(messageCopy.getSubject()).isEqualTo("my subject"); + assertThat(messageCopy.getText()).isEqualTo("my text"); } @Test @@ -93,9 +94,9 @@ public void testDeepCopyOfStringArrayTypedFieldsOnCopyCtor() throws Exception { original.getCc()[0] = "mmm@mmm.org"; original.getBcc()[0] = "mmm@mmm.org"; - assertEquals("fiona@mail.org", copy.getTo()[0]); - assertEquals("he@mail.org", copy.getCc()[0]); - assertEquals("us@mail.org", copy.getBcc()[0]); + assertThat(copy.getTo()[0]).isEqualTo("fiona@mail.org"); + assertThat(copy.getCc()[0]).isEqualTo("he@mail.org"); + assertThat(copy.getBcc()[0]).isEqualTo("us@mail.org"); } /** @@ -116,8 +117,8 @@ public final void testHashCode() { // Copy the message SimpleMailMessage message2 = new SimpleMailMessage(message1); - assertEquals(message1, message2); - assertEquals(message1.hashCode(), message2.hashCode()); + assertThat(message2).isEqualTo(message1); + assertThat(message2.hashCode()).isEqualTo(message1.hashCode()); } public final void testEqualsObject() { @@ -127,20 +128,22 @@ public final void testEqualsObject() { // Same object is equal message1 = new SimpleMailMessage(); message2 = message1; - assertTrue(message1.equals(message2)); + assertThat(message1.equals(message2)).isTrue(); // Null object is not equal message1 = new SimpleMailMessage(); message2 = null; - assertTrue(!(message1.equals(message2))); + boolean condition1 = !(message1.equals(message2)); + assertThat(condition1).isTrue(); // Different class is not equal - assertTrue(!(message1.equals(new Object()))); + boolean condition = !(message1.equals(new Object())); + assertThat(condition).isTrue(); // Equal values are equal message1 = new SimpleMailMessage(); message2 = new SimpleMailMessage(); - assertTrue(message1.equals(message2)); + assertThat(message1.equals(message2)).isTrue(); message1 = new SimpleMailMessage(); message1.setFrom("from@somewhere"); @@ -152,17 +155,19 @@ public final void testEqualsObject() { message1.setSubject("subject"); message1.setText("text"); message2 = new SimpleMailMessage(message1); - assertTrue(message1.equals(message2)); + assertThat(message1.equals(message2)).isTrue(); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCopyCtorChokesOnNullOriginalMessage() throws Exception { - new SimpleMailMessage(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new SimpleMailMessage(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCopyToChokesOnNullTargetMessage() throws Exception { - new SimpleMailMessage().copyTo(null); + assertThatIllegalArgumentException().isThrownBy(() -> + new SimpleMailMessage().copyTo(null)); } } diff --git a/spring-context-support/src/test/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMapTests.java b/spring-context-support/src/test/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMapTests.java index 5837c9566d3f..ebbf493cd5f0 100644 --- a/spring-context-support/src/test/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMapTests.java +++ b/spring-context-support/src/test/java/org/springframework/mail/javamail/ConfigurableMimeFileTypeMapTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,12 +18,12 @@ import java.io.File; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Rob Harrop @@ -36,18 +36,18 @@ public void againstDefaultConfiguration() throws Exception { ConfigurableMimeFileTypeMap ftm = new ConfigurableMimeFileTypeMap(); ftm.afterPropertiesSet(); - assertEquals("Invalid content type for HTM", "text/html", ftm.getContentType("foobar.HTM")); - assertEquals("Invalid content type for html", "text/html", ftm.getContentType("foobar.html")); - assertEquals("Invalid content type for c++", "text/plain", ftm.getContentType("foobar.c++")); - assertEquals("Invalid content type for svf", "image/vnd.svf", ftm.getContentType("foobar.svf")); - assertEquals("Invalid content type for dsf", "image/x-mgx-dsf", ftm.getContentType("foobar.dsf")); - assertEquals("Invalid default content type", "application/octet-stream", ftm.getContentType("foobar.foo")); + assertThat(ftm.getContentType("foobar.HTM")).as("Invalid content type for HTM").isEqualTo("text/html"); + assertThat(ftm.getContentType("foobar.html")).as("Invalid content type for html").isEqualTo("text/html"); + assertThat(ftm.getContentType("foobar.c++")).as("Invalid content type for c++").isEqualTo("text/plain"); + assertThat(ftm.getContentType("foobar.svf")).as("Invalid content type for svf").isEqualTo("image/vnd.svf"); + assertThat(ftm.getContentType("foobar.dsf")).as("Invalid content type for dsf").isEqualTo("image/x-mgx-dsf"); + assertThat(ftm.getContentType("foobar.foo")).as("Invalid default content type").isEqualTo("application/octet-stream"); } @Test public void againstDefaultConfigurationWithFilePath() throws Exception { ConfigurableMimeFileTypeMap ftm = new ConfigurableMimeFileTypeMap(); - assertEquals("Invalid content type for HTM", "text/html", ftm.getContentType(new File("/tmp/foobar.HTM"))); + assertThat(ftm.getContentType(new File("/tmp/foobar.HTM"))).as("Invalid content type for HTM").isEqualTo("text/html"); } @Test @@ -56,9 +56,9 @@ public void withAdditionalMappings() throws Exception { ftm.setMappings(new String[] {"foo/bar HTM foo", "foo/cpp c++"}); ftm.afterPropertiesSet(); - assertEquals("Invalid content type for HTM - override didn't work", "foo/bar", ftm.getContentType("foobar.HTM")); - assertEquals("Invalid content type for c++ - override didn't work", "foo/cpp", ftm.getContentType("foobar.c++")); - assertEquals("Invalid content type for foo - new mapping didn't work", "foo/bar", ftm.getContentType("bar.foo")); + assertThat(ftm.getContentType("foobar.HTM")).as("Invalid content type for HTM - override didn't work").isEqualTo("foo/bar"); + assertThat(ftm.getContentType("foobar.c++")).as("Invalid content type for c++ - override didn't work").isEqualTo("foo/cpp"); + assertThat(ftm.getContentType("bar.foo")).as("Invalid content type for foo - new mapping didn't work").isEqualTo("foo/bar"); } @Test @@ -69,10 +69,10 @@ public void withCustomMappingLocation() throws Exception { ftm.setMappingLocation(resource); ftm.afterPropertiesSet(); - assertEquals("Invalid content type for foo", "text/foo", ftm.getContentType("foobar.foo")); - assertEquals("Invalid content type for bar", "text/bar", ftm.getContentType("foobar.bar")); - assertEquals("Invalid content type for fimg", "image/foo", ftm.getContentType("foobar.fimg")); - assertEquals("Invalid content type for bimg", "image/bar", ftm.getContentType("foobar.bimg")); + assertThat(ftm.getContentType("foobar.foo")).as("Invalid content type for foo").isEqualTo("text/foo"); + assertThat(ftm.getContentType("foobar.bar")).as("Invalid content type for bar").isEqualTo("text/bar"); + assertThat(ftm.getContentType("foobar.fimg")).as("Invalid content type for fimg").isEqualTo("image/foo"); + assertThat(ftm.getContentType("foobar.bimg")).as("Invalid content type for bimg").isEqualTo("image/bar"); } } diff --git a/spring-context-support/src/test/java/org/springframework/mail/javamail/InternetAddressEditorTests.java b/spring-context-support/src/test/java/org/springframework/mail/javamail/InternetAddressEditorTests.java index fff03d58a630..15a6fd3edb6e 100644 --- a/spring-context-support/src/test/java/org/springframework/mail/javamail/InternetAddressEditorTests.java +++ b/spring-context-support/src/test/java/org/springframework/mail/javamail/InternetAddressEditorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,9 +16,10 @@ package org.springframework.mail.javamail; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Brian Hanafee @@ -36,42 +37,43 @@ public class InternetAddressEditorTests { @Test public void uninitialized() { - assertEquals("Uninitialized editor did not return empty value string", EMPTY, editor.getAsText()); + assertThat(editor.getAsText()).as("Uninitialized editor did not return empty value string").isEqualTo(EMPTY); } @Test public void setNull() { editor.setAsText(null); - assertEquals("Setting null did not result in empty value string", EMPTY, editor.getAsText()); + assertThat(editor.getAsText()).as("Setting null did not result in empty value string").isEqualTo(EMPTY); } @Test public void setEmpty() { editor.setAsText(EMPTY); - assertEquals("Setting empty string did not result in empty value string", EMPTY, editor.getAsText()); + assertThat(editor.getAsText()).as("Setting empty string did not result in empty value string").isEqualTo(EMPTY); } @Test public void allWhitespace() { editor.setAsText(" "); - assertEquals("All whitespace was not recognized", EMPTY, editor.getAsText()); + assertThat(editor.getAsText()).as("All whitespace was not recognized").isEqualTo(EMPTY); } @Test - public void simpleGoodAddess() { + public void simpleGoodAddress() { editor.setAsText(SIMPLE); - assertEquals("Simple email address failed", SIMPLE, editor.getAsText()); + assertThat(editor.getAsText()).as("Simple email address failed").isEqualTo(SIMPLE); } @Test public void excessWhitespace() { editor.setAsText(" " + SIMPLE + " "); - assertEquals("Whitespace was not stripped", SIMPLE, editor.getAsText()); + assertThat(editor.getAsText()).as("Whitespace was not stripped").isEqualTo(SIMPLE); } - @Test(expected = IllegalArgumentException.class) + @Test public void simpleBadAddress() { - editor.setAsText(BAD); + assertThatIllegalArgumentException().isThrownBy(() -> + editor.setAsText(BAD)); } } diff --git a/spring-context-support/src/test/java/org/springframework/mail/javamail/JavaMailSenderTests.java b/spring-context-support/src/test/java/org/springframework/mail/javamail/JavaMailSenderTests.java index 623831d7508b..3bd467b9106e 100644 --- a/spring-context-support/src/test/java/org/springframework/mail/javamail/JavaMailSenderTests.java +++ b/spring-context-support/src/test/java/org/springframework/mail/javamail/JavaMailSenderTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -23,6 +23,7 @@ import java.util.GregorianCalendar; import java.util.List; import java.util.Properties; + import javax.activation.FileTypeMap; import javax.mail.Address; import javax.mail.Message; @@ -35,16 +36,16 @@ import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import org.springframework.mail.MailParseException; import org.springframework.mail.MailSendException; import org.springframework.mail.SimpleMailMessage; import org.springframework.util.ObjectUtils; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.entry; /** * @author Juergen Hoeller @@ -53,9 +54,6 @@ */ public class JavaMailSenderTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Test public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException { MockJavaMailSender sender = new MockJavaMailSender(); @@ -68,44 +66,45 @@ public void javaMailSenderWithSimpleMessage() throws MessagingException, IOExcep simpleMessage.setFrom("me@mail.org"); simpleMessage.setReplyTo("reply@mail.org"); simpleMessage.setTo("you@mail.org"); - simpleMessage.setCc(new String[] {"he@mail.org", "she@mail.org"}); - simpleMessage.setBcc(new String[] {"us@mail.org", "them@mail.org"}); + simpleMessage.setCc("he@mail.org", "she@mail.org"); + simpleMessage.setBcc("us@mail.org", "them@mail.org"); Date sentDate = new GregorianCalendar(2004, 1, 1).getTime(); simpleMessage.setSentDate(sentDate); simpleMessage.setSubject("my subject"); simpleMessage.setText("my text"); sender.send(simpleMessage); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals(30, sender.transport.getConnectedPort()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedPort()).isEqualTo(30); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); - assertEquals(1, sender.transport.getSentMessages().size()); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); MimeMessage sentMessage = sender.transport.getSentMessage(0); List
froms = Arrays.asList(sentMessage.getFrom()); - assertEquals(1, froms.size()); - assertEquals("me@mail.org", ((InternetAddress) froms.get(0)).getAddress()); + assertThat(froms.size()).isEqualTo(1); + assertThat(((InternetAddress) froms.get(0)).getAddress()).isEqualTo("me@mail.org"); List
replyTos = Arrays.asList(sentMessage.getReplyTo()); - assertEquals("reply@mail.org", ((InternetAddress) replyTos.get(0)).getAddress()); + assertThat(((InternetAddress) replyTos.get(0)).getAddress()).isEqualTo("reply@mail.org"); List
tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO)); - assertEquals(1, tos.size()); - assertEquals("you@mail.org", ((InternetAddress) tos.get(0)).getAddress()); + assertThat(tos.size()).isEqualTo(1); + assertThat(((InternetAddress) tos.get(0)).getAddress()).isEqualTo("you@mail.org"); List
ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC)); - assertEquals(2, ccs.size()); - assertEquals("he@mail.org", ((InternetAddress) ccs.get(0)).getAddress()); - assertEquals("she@mail.org", ((InternetAddress) ccs.get(1)).getAddress()); + assertThat(ccs.size()).isEqualTo(2); + assertThat(((InternetAddress) ccs.get(0)).getAddress()).isEqualTo("he@mail.org"); + assertThat(((InternetAddress) ccs.get(1)).getAddress()).isEqualTo("she@mail.org"); List
bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC)); - assertEquals(2, bccs.size()); - assertEquals("us@mail.org", ((InternetAddress) bccs.get(0)).getAddress()); - assertEquals("them@mail.org", ((InternetAddress) bccs.get(1)).getAddress()); - assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime()); - assertEquals("my subject", sentMessage.getSubject()); - assertEquals("my text", sentMessage.getContent()); + assertThat(bccs.size()).isEqualTo(2); + assertThat(((InternetAddress) bccs.get(0)).getAddress()).isEqualTo("us@mail.org"); + assertThat(((InternetAddress) bccs.get(1)).getAddress()).isEqualTo("them@mail.org"); + assertThat(sentMessage.getSentDate().getTime()).isEqualTo(sentDate.getTime()); + assertThat(sentMessage.getSubject()).isEqualTo("my subject"); + assertThat(sentMessage.getContent()).isEqualTo("my text"); } - public void testJavaMailSenderWithSimpleMessages() throws MessagingException, IOException { + @Test + public void javaMailSenderWithSimpleMessages() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost("host"); sender.setUsername("username"); @@ -117,23 +116,24 @@ public void testJavaMailSenderWithSimpleMessages() throws MessagingException, IO simpleMessage2.setTo("she@mail.org"); sender.send(simpleMessage1, simpleMessage2); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); - assertEquals(2, sender.transport.getSentMessages().size()); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(2); MimeMessage sentMessage1 = sender.transport.getSentMessage(0); List
tos1 = Arrays.asList(sentMessage1.getRecipients(Message.RecipientType.TO)); - assertEquals(1, tos1.size()); - assertEquals("he@mail.org", ((InternetAddress) tos1.get(0)).getAddress()); + assertThat(tos1.size()).isEqualTo(1); + assertThat(((InternetAddress) tos1.get(0)).getAddress()).isEqualTo("he@mail.org"); MimeMessage sentMessage2 = sender.transport.getSentMessage(1); List
tos2 = Arrays.asList(sentMessage2.getRecipients(Message.RecipientType.TO)); - assertEquals(1, tos2.size()); - assertEquals("she@mail.org", ((InternetAddress) tos2.get(0)).getAddress()); + assertThat(tos2.size()).isEqualTo(1); + assertThat(((InternetAddress) tos2.get(0)).getAddress()).isEqualTo("she@mail.org"); } - public void testJavaMailSenderWithMimeMessage() throws MessagingException { + @Test + public void javaMailSenderWithMimeMessage() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost("host"); sender.setUsername("username"); @@ -143,12 +143,12 @@ public void testJavaMailSenderWithMimeMessage() throws MessagingException { mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org")); sender.send(mimeMessage); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(mimeMessage, sender.transport.getSentMessage(0)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage); } @Test @@ -164,13 +164,13 @@ public void javaMailSenderWithMimeMessages() throws MessagingException { mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org")); sender.send(mimeMessage1, mimeMessage2); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(2, sender.transport.getSentMessages().size()); - assertEquals(mimeMessage1, sender.transport.getSentMessage(0)); - assertEquals(mimeMessage2, sender.transport.getSentMessage(1)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(2); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage1); + assertThat(sender.transport.getSentMessage(1)).isEqualTo(mimeMessage2); } @Test @@ -191,12 +191,12 @@ public void prepare(MimeMessage mimeMessage) throws MessagingException { }; sender.send(preparator); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(messages.get(0), sender.transport.getSentMessage(0)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(messages.get(0)); } @Test @@ -224,13 +224,13 @@ public void prepare(MimeMessage mimeMessage) throws MessagingException { }; sender.send(preparator1, preparator2); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(2, sender.transport.getSentMessages().size()); - assertEquals(messages.get(0), sender.transport.getSentMessage(0)); - assertEquals(messages.get(1), sender.transport.getSentMessage(1)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(2); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(messages.get(0)); + assertThat(sender.transport.getSentMessage(1)).isEqualTo(messages.get(1)); } @Test @@ -241,18 +241,19 @@ public void javaMailSenderWithMimeMessageHelper() throws MessagingException { sender.setPassword("password"); MimeMessageHelper message = new MimeMessageHelper(sender.createMimeMessage()); - assertNull(message.getEncoding()); - assertTrue(message.getFileTypeMap() instanceof ConfigurableMimeFileTypeMap); + assertThat(message.getEncoding()).isNull(); + boolean condition = message.getFileTypeMap() instanceof ConfigurableMimeFileTypeMap; + assertThat(condition).isTrue(); message.setTo("you@mail.org"); sender.send(message.getMimeMessage()); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(message.getMimeMessage(), sender.transport.getSentMessage(0)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(message.getMimeMessage()); } @Test @@ -263,20 +264,20 @@ public void javaMailSenderWithMimeMessageHelperAndSpecificEncoding() throws Mess sender.setPassword("password"); MimeMessageHelper message = new MimeMessageHelper(sender.createMimeMessage(), "UTF-8"); - assertEquals("UTF-8", message.getEncoding()); + assertThat(message.getEncoding()).isEqualTo("UTF-8"); FileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap(); message.setFileTypeMap(fileTypeMap); - assertEquals(fileTypeMap, message.getFileTypeMap()); + assertThat(message.getFileTypeMap()).isEqualTo(fileTypeMap); message.setTo("you@mail.org"); sender.send(message.getMimeMessage()); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(message.getMimeMessage(), sender.transport.getSentMessage(0)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(message.getMimeMessage()); } @Test @@ -290,18 +291,18 @@ public void javaMailSenderWithMimeMessageHelperAndDefaultEncoding() throws Messa FileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap(); sender.setDefaultFileTypeMap(fileTypeMap); MimeMessageHelper message = new MimeMessageHelper(sender.createMimeMessage()); - assertEquals("UTF-8", message.getEncoding()); - assertEquals(fileTypeMap, message.getFileTypeMap()); + assertThat(message.getEncoding()).isEqualTo("UTF-8"); + assertThat(message.getFileTypeMap()).isEqualTo(fileTypeMap); message.setTo("you@mail.org"); sender.send(message.getMimeMessage()); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(message.getMimeMessage(), sender.transport.getSentMessage(0)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(message.getMimeMessage()); } @Test @@ -314,7 +315,8 @@ public void javaMailSenderWithParseExceptionOnSimpleMessage() { } catch (MailParseException ex) { // expected - assertTrue(ex.getCause() instanceof AddressException); + boolean condition = ex.getCause() instanceof AddressException; + assertThat(condition).isTrue(); } } @@ -332,7 +334,8 @@ public void prepare(MimeMessage mimeMessage) throws MessagingException { } catch (MailParseException ex) { // expected - assertTrue(ex.getCause() instanceof AddressException); + boolean condition = ex.getCause() instanceof AddressException; + assertThat(condition).isTrue(); } } @@ -342,7 +345,7 @@ public void javaMailSenderWithCustomSession() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender() { @Override protected Transport getTransport(Session sess) throws NoSuchProviderException { - assertEquals(session, sess); + assertThat(sess).isEqualTo(session); return super.getTransport(sess); } }; @@ -357,12 +360,12 @@ protected Transport getTransport(Session sess) throws NoSuchProviderException { mimeMessage.setSentDate(new GregorianCalendar(2005, 3, 1).getTime()); sender.send(mimeMessage); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(mimeMessage, sender.transport.getSentMessage(0)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage); } @Test @@ -372,7 +375,7 @@ public void javaMailProperties() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender() { @Override protected Transport getTransport(Session sess) throws NoSuchProviderException { - assertEquals("bogusValue", sess.getProperty("bogusKey")); + assertThat(sess.getProperty("bogusKey")).isEqualTo("bogusValue"); return super.getTransport(sess); } }; @@ -385,56 +388,40 @@ protected Transport getTransport(Session sess) throws NoSuchProviderException { mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org")); sender.send(mimeMessage); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(mimeMessage, sender.transport.getSentMessage(0)); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage); } @Test - public void failedMailServerConnect() throws Exception { + public void failedMailServerConnect() { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost(null); sender.setUsername("username"); sender.setPassword("password"); SimpleMailMessage simpleMessage1 = new SimpleMailMessage(); - try { - sender.send(simpleMessage1); - fail("Should have thrown MailSendException"); - } - catch (MailSendException ex) { - // expected - ex.printStackTrace(); - assertTrue(ex.getFailedMessages() != null); - assertEquals(1, ex.getFailedMessages().size()); - assertSame(simpleMessage1, ex.getFailedMessages().keySet().iterator().next()); - assertSame(ex.getCause(), ex.getFailedMessages().values().iterator().next()); - } + assertThatExceptionOfType(MailSendException.class).isThrownBy(() -> + sender.send(simpleMessage1)) + .satisfies(ex -> assertThat(ex.getFailedMessages()).containsExactly(entry(simpleMessage1, (Exception) ex.getCause()))); } @Test - public void failedMailServerClose() throws Exception { + public void failedMailServerClose() { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost(""); sender.setUsername("username"); sender.setPassword("password"); SimpleMailMessage simpleMessage1 = new SimpleMailMessage(); - try { - sender.send(simpleMessage1); - fail("Should have thrown MailSendException"); - } - catch (MailSendException ex) { - // expected - ex.printStackTrace(); - assertTrue(ex.getFailedMessages() != null); - assertEquals(0, ex.getFailedMessages().size()); - } + assertThatExceptionOfType(MailSendException.class).isThrownBy(() -> + sender.send(simpleMessage1)) + .satisfies(ex -> assertThat(ex.getFailedMessages()).isEmpty()); } @Test - public void failedSimpleMessage() throws Exception { + public void failedSimpleMessage() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost("host"); sender.setUsername("username"); @@ -451,22 +438,23 @@ public void failedSimpleMessage() throws Exception { } catch (MailSendException ex) { ex.printStackTrace(); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(new InternetAddress("she@mail.org"), sender.transport.getSentMessage(0).getAllRecipients()[0]); - assertEquals(1, ex.getFailedMessages().size()); - assertEquals(simpleMessage1, ex.getFailedMessages().keySet().iterator().next()); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0).getAllRecipients()[0]).isEqualTo(new InternetAddress("she@mail.org")); + assertThat(ex.getFailedMessages().size()).isEqualTo(1); + assertThat(ex.getFailedMessages().keySet().iterator().next()).isEqualTo(simpleMessage1); Object subEx = ex.getFailedMessages().values().iterator().next(); - assertTrue(subEx instanceof MessagingException); - assertEquals("failed", ((MessagingException) subEx).getMessage()); + boolean condition = subEx instanceof MessagingException; + assertThat(condition).isTrue(); + assertThat(((MessagingException) subEx).getMessage()).isEqualTo("failed"); } } @Test - public void fFailedMimeMessage() throws Exception { + public void failedMimeMessage() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost("host"); sender.setUsername("username"); @@ -483,34 +471,34 @@ public void fFailedMimeMessage() throws Exception { } catch (MailSendException ex) { ex.printStackTrace(); - assertEquals("host", sender.transport.getConnectedHost()); - assertEquals("username", sender.transport.getConnectedUsername()); - assertEquals("password", sender.transport.getConnectedPassword()); - assertTrue(sender.transport.isCloseCalled()); - assertEquals(1, sender.transport.getSentMessages().size()); - assertEquals(mimeMessage2, sender.transport.getSentMessage(0)); - assertEquals(1, ex.getFailedMessages().size()); - assertEquals(mimeMessage1, ex.getFailedMessages().keySet().iterator().next()); + assertThat(sender.transport.getConnectedHost()).isEqualTo("host"); + assertThat(sender.transport.getConnectedUsername()).isEqualTo("username"); + assertThat(sender.transport.getConnectedPassword()).isEqualTo("password"); + assertThat(sender.transport.isCloseCalled()).isTrue(); + assertThat(sender.transport.getSentMessages().size()).isEqualTo(1); + assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage2); + assertThat(ex.getFailedMessages().size()).isEqualTo(1); + assertThat(ex.getFailedMessages().keySet().iterator().next()).isEqualTo(mimeMessage1); Object subEx = ex.getFailedMessages().values().iterator().next(); - assertTrue(subEx instanceof MessagingException); - assertEquals("failed", ((MessagingException) subEx).getMessage()); + boolean condition = subEx instanceof MessagingException; + assertThat(condition).isTrue(); + assertThat(((MessagingException) subEx).getMessage()).isEqualTo("failed"); } } @Test - public void testConnection() throws Exception { + public void testConnection() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost("host"); sender.testConnection(); } @Test - public void testConnectionWithFailure() throws Exception { + public void testConnectionWithFailure() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost(null); - - thrown.expect(MessagingException.class); - sender.testConnection(); + assertThatExceptionOfType(MessagingException.class).isThrownBy( + sender::testConnection); } @@ -592,14 +580,15 @@ public void sendMessage(Message message, Address[] addresses) throws MessagingEx if ("fail".equals(message.getSubject())) { throw new MessagingException("failed"); } - if (!ObjectUtils.nullSafeEquals(addresses, message.getAllRecipients())) { + if (addresses == null || (message.getAllRecipients() == null ? addresses.length > 0 : + !ObjectUtils.nullSafeEquals(addresses, message.getAllRecipients()))) { throw new MessagingException("addresses not correct"); } if (message.getSentDate() == null) { throw new MessagingException("No sentDate specified"); } if (message.getSubject() != null && message.getSubject().contains("custom")) { - assertEquals(new GregorianCalendar(2005, 3, 1).getTime(), message.getSentDate()); + assertThat(message.getSentDate()).isEqualTo(new GregorianCalendar(2005, 3, 1).getTime()); } this.sentMessages.add(message); } diff --git a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/CronTriggerFactoryBeanTests.java b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/CronTriggerFactoryBeanTests.java index 5aa0142fc7d7..79dc04418b68 100644 --- a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/CronTriggerFactoryBeanTests.java +++ b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/CronTriggerFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,10 @@ import java.text.ParseException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.quartz.CronTrigger; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -35,7 +35,7 @@ public void createWithoutJobDetail() throws ParseException { factory.setCronExpression("0 15 10 ? * *"); factory.afterPropertiesSet(); CronTrigger trigger = factory.getObject(); - assertEquals("0 15 10 ? * *", trigger.getCronExpression()); + assertThat(trigger.getCronExpression()).isEqualTo("0 15 10 ? * *"); } } diff --git a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java index bd8156a6342a..c38def86e2d4 100644 --- a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java +++ b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -16,13 +16,13 @@ package org.springframework.scheduling.quartz; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StopWatch; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Mark Fisher @@ -34,26 +34,26 @@ public class QuartzSchedulerLifecycleTests { public void destroyLazyInitSchedulerWithDefaultShutdownOrderDoesNotHang() { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("quartzSchedulerLifecycleTests.xml", getClass()); - assertNotNull(context.getBean("lazyInitSchedulerWithDefaultShutdownOrder")); + assertThat(context.getBean("lazyInitSchedulerWithDefaultShutdownOrder")).isNotNull(); StopWatch sw = new StopWatch(); sw.start("lazyScheduler"); context.close(); sw.stop(); - assertTrue("Quartz Scheduler with lazy-init is hanging on destruction: " + - sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 500); + assertThat(sw.getTotalTimeMillis() < 500).as("Quartz Scheduler with lazy-init is hanging on destruction: " + + sw.getTotalTimeMillis()).isTrue(); } @Test // SPR-6354 public void destroyLazyInitSchedulerWithCustomShutdownOrderDoesNotHang() { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("quartzSchedulerLifecycleTests.xml", getClass()); - assertNotNull(context.getBean("lazyInitSchedulerWithCustomShutdownOrder")); + assertThat(context.getBean("lazyInitSchedulerWithCustomShutdownOrder")).isNotNull(); StopWatch sw = new StopWatch(); sw.start("lazyScheduler"); context.close(); sw.stop(); - assertTrue("Quartz Scheduler with lazy-init is hanging on destruction: " + - sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 500); + assertThat(sw.getTotalTimeMillis() < 500).as("Quartz Scheduler with lazy-init is hanging on destruction: " + + sw.getTotalTimeMillis()).isTrue(); } } diff --git a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java index 68a619c0d9bf..a30b1c291212 100644 --- a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java +++ b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,9 +18,10 @@ import java.util.HashMap; import java.util.Map; + import javax.sql.DataSource; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @@ -33,16 +34,19 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.task.TaskExecutor; +import org.springframework.core.testfixture.EnabledForTestGroups; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.tests.Assume; -import org.springframework.tests.TestGroup; -import org.springframework.tests.sample.beans.TestBean; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.springframework.core.testfixture.TestGroup.PERFORMANCE; /** * @author Juergen Hoeller @@ -80,8 +84,8 @@ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String sc schedulerFactoryBean.afterPropertiesSet(); schedulerFactoryBean.start(); Scheduler returnedScheduler = schedulerFactoryBean.getObject(); - assertEquals(tb, returnedScheduler.getContext().get("testBean")); - assertEquals(ac, returnedScheduler.getContext().get("appCtx")); + assertThat(returnedScheduler.getContext().get("testBean")).isEqualTo(tb); + assertThat(returnedScheduler.getContext().get("appCtx")).isEqualTo(ac); } finally { schedulerFactoryBean.destroy(); @@ -92,9 +96,8 @@ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String sc } @Test + @EnabledForTestGroups(PERFORMANCE) public void schedulerWithTaskExecutor() throws Exception { - Assume.group(TestGroup.PERFORMANCE); - CountingTaskExecutor taskExecutor = new CountingTaskExecutor(); DummyJob.count = 0; @@ -119,23 +122,23 @@ public void schedulerWithTaskExecutor() throws Exception { bean.start(); Thread.sleep(500); - assertTrue("DummyJob should have been executed at least once.", DummyJob.count > 0); - assertEquals(DummyJob.count, taskExecutor.count); + assertThat(DummyJob.count > 0).as("DummyJob should have been executed at least once.").isTrue(); + assertThat(taskExecutor.count).isEqualTo(DummyJob.count); bean.destroy(); } - @Test(expected = IllegalArgumentException.class) + @Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void jobDetailWithRunnableInsteadOfJob() { JobDetailImpl jobDetail = new JobDetailImpl(); - jobDetail.setJobClass((Class) DummyRunnable.class); + assertThatIllegalArgumentException().isThrownBy(() -> + jobDetail.setJobClass((Class) DummyRunnable.class)); } @Test + @EnabledForTestGroups(PERFORMANCE) public void schedulerWithQuartzJobBean() throws Exception { - Assume.group(TestGroup.PERFORMANCE); - DummyJob.param = 0; DummyJob.count = 0; @@ -160,16 +163,15 @@ public void schedulerWithQuartzJobBean() throws Exception { bean.start(); Thread.sleep(500); - assertEquals(10, DummyJobBean.param); - assertTrue(DummyJobBean.count > 0); + assertThat(DummyJobBean.param).isEqualTo(10); + assertThat(DummyJobBean.count > 0).isTrue(); bean.destroy(); } @Test + @EnabledForTestGroups(PERFORMANCE) public void schedulerWithSpringBeanJobFactory() throws Exception { - Assume.group(TestGroup.PERFORMANCE); - DummyJob.param = 0; DummyJob.count = 0; @@ -196,16 +198,15 @@ public void schedulerWithSpringBeanJobFactory() throws Exception { bean.start(); Thread.sleep(500); - assertEquals(10, DummyJob.param); - assertTrue("DummyJob should have been executed at least once.", DummyJob.count > 0); + assertThat(DummyJob.param).isEqualTo(10); + assertThat(DummyJob.count > 0).as("DummyJob should have been executed at least once.").isTrue(); bean.destroy(); } @Test + @EnabledForTestGroups(PERFORMANCE) public void schedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() throws Exception { - Assume.group(TestGroup.PERFORMANCE); - DummyJob.param = 0; DummyJob.count = 0; @@ -233,15 +234,15 @@ public void schedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() throws bean.afterPropertiesSet(); Thread.sleep(500); - assertEquals(0, DummyJob.param); - assertTrue(DummyJob.count == 0); + assertThat(DummyJob.param).isEqualTo(0); + assertThat(DummyJob.count == 0).isTrue(); bean.destroy(); } @Test + @EnabledForTestGroups(PERFORMANCE) public void schedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Exception { - Assume.group(TestGroup.PERFORMANCE); DummyJobBean.param = 0; DummyJobBean.count = 0; @@ -267,15 +268,15 @@ public void schedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Exception bean.start(); Thread.sleep(500); - assertEquals(10, DummyJobBean.param); - assertTrue(DummyJobBean.count > 0); + assertThat(DummyJobBean.param).isEqualTo(10); + assertThat(DummyJobBean.count > 0).isTrue(); bean.destroy(); } @Test + @EnabledForTestGroups(PERFORMANCE) public void schedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws Exception { - Assume.group(TestGroup.PERFORMANCE); DummyJob.param = 0; DummyJob.count = 0; @@ -286,8 +287,8 @@ public void schedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws Excep bean.start(); Thread.sleep(500); - assertEquals(10, DummyJob.param); - assertTrue("DummyJob should have been executed at least once.", DummyJob.count > 0); + assertThat(DummyJob.param).isEqualTo(10); + assertThat(DummyJob.count > 0).as("DummyJob should have been executed at least once.").isTrue(); bean.destroy(); } @@ -298,9 +299,9 @@ public void multipleSchedulers() throws Exception { try { Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1"); Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2"); - assertNotSame(scheduler1, scheduler2); - assertEquals("quartz1", scheduler1.getSchedulerName()); - assertEquals("quartz2", scheduler2.getSchedulerName()); + assertThat(scheduler2).isNotSameAs(scheduler1); + assertThat(scheduler1.getSchedulerName()).isEqualTo("quartz1"); + assertThat(scheduler2.getSchedulerName()).isEqualTo("quartz2"); } finally { ctx.close(); @@ -313,9 +314,9 @@ public void multipleSchedulersWithQuartzProperties() throws Exception { try { Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1"); Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2"); - assertNotSame(scheduler1, scheduler2); - assertEquals("quartz1", scheduler1.getSchedulerName()); - assertEquals("quartz2", scheduler2.getSchedulerName()); + assertThat(scheduler2).isNotSameAs(scheduler1); + assertThat(scheduler1.getSchedulerName()).isEqualTo("quartz1"); + assertThat(scheduler2.getSchedulerName()).isEqualTo("quartz2"); } finally { ctx.close(); @@ -323,18 +324,18 @@ public void multipleSchedulersWithQuartzProperties() throws Exception { } @Test + @EnabledForTestGroups(PERFORMANCE) public void twoAnonymousMethodInvokingJobDetailFactoryBeans() throws Exception { - Assume.group(TestGroup.PERFORMANCE); ClassPathXmlApplicationContext ctx = context("multipleAnonymousMethodInvokingJobDetailFB.xml"); Thread.sleep(3000); try { QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService"); QuartzTestBean importService = (QuartzTestBean) ctx.getBean("importService"); - assertEquals("doImport called exportService", 0, exportService.getImportCount()); - assertEquals("doExport not called on exportService", 2, exportService.getExportCount()); - assertEquals("doImport not called on importService", 2, importService.getImportCount()); - assertEquals("doExport called on importService", 0, importService.getExportCount()); + assertThat(exportService.getImportCount()).as("doImport called exportService").isEqualTo(0); + assertThat(exportService.getExportCount()).as("doExport not called on exportService").isEqualTo(2); + assertThat(importService.getImportCount()).as("doImport not called on importService").isEqualTo(2); + assertThat(importService.getExportCount()).as("doExport called on importService").isEqualTo(0); } finally { ctx.close(); @@ -342,18 +343,18 @@ public void twoAnonymousMethodInvokingJobDetailFactoryBeans() throws Exception { } @Test + @EnabledForTestGroups(PERFORMANCE) public void schedulerAccessorBean() throws Exception { - Assume.group(TestGroup.PERFORMANCE); ClassPathXmlApplicationContext ctx = context("schedulerAccessorBean.xml"); Thread.sleep(3000); try { QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService"); QuartzTestBean importService = (QuartzTestBean) ctx.getBean("importService"); - assertEquals("doImport called exportService", 0, exportService.getImportCount()); - assertEquals("doExport not called on exportService", 2, exportService.getExportCount()); - assertEquals("doImport not called on importService", 2, importService.getImportCount()); - assertEquals("doExport called on importService", 0, importService.getExportCount()); + assertThat(exportService.getImportCount()).as("doImport called exportService").isEqualTo(0); + assertThat(exportService.getExportCount()).as("doExport not called on exportService").isEqualTo(2); + assertThat(importService.getImportCount()).as("doImport not called on importService").isEqualTo(2); + assertThat(importService.getExportCount()).as("doExport called on importService").isEqualTo(0); } finally { ctx.close(); @@ -366,9 +367,9 @@ public void schedulerAutoStartsOnContextRefreshedEventByDefault() throws Excepti StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("scheduler", new RootBeanDefinition(SchedulerFactoryBean.class)); Scheduler bean = context.getBean("scheduler", Scheduler.class); - assertFalse(bean.isStarted()); + assertThat(bean.isStarted()).isFalse(); context.refresh(); - assertTrue(bean.isStarted()); + assertThat(bean.isStarted()).isTrue(); } @Test @@ -379,15 +380,15 @@ public void schedulerAutoStartupFalse() throws Exception { .addPropertyValue("autoStartup", false).getBeanDefinition(); context.registerBeanDefinition("scheduler", beanDefinition); Scheduler bean = context.getBean("scheduler", Scheduler.class); - assertFalse(bean.isStarted()); + assertThat(bean.isStarted()).isFalse(); context.refresh(); - assertFalse(bean.isStarted()); + assertThat(bean.isStarted()).isFalse(); } @Test public void schedulerRepositoryExposure() throws Exception { ClassPathXmlApplicationContext ctx = context("schedulerRepositoryExposure.xml"); - assertSame(SchedulerRepository.getInstance().lookup("myScheduler"), ctx.getBean("scheduler")); + assertThat(ctx.getBean("scheduler")).isSameAs(SchedulerRepository.getInstance().lookup("myScheduler")); ctx.close(); } @@ -397,14 +398,12 @@ public void schedulerRepositoryExposure() throws Exception { */ @Test public void schedulerWithHsqlDataSource() throws Exception { - // Assume.group(TestGroup.PERFORMANCE); - DummyJob.param = 0; DummyJob.count = 0; ClassPathXmlApplicationContext ctx = context("databasePersistence.xml"); JdbcTemplate jdbcTemplate = new JdbcTemplate(ctx.getBean(DataSource.class)); - assertFalse("No triggers were persisted", jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").isEmpty()); + assertThat(jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").isEmpty()).as("No triggers were persisted").isFalse(); /* Thread.sleep(3000); diff --git a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzTestBean.java b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzTestBean.java index 8890e39ed089..77539f00cc14 100644 --- a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzTestBean.java +++ b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzTestBean.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBeanTests.java b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBeanTests.java index 7021682b23b0..6db0bcc82d2d 100644 --- a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBeanTests.java +++ b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBeanTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,10 +18,10 @@ import java.text.ParseException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.quartz.SimpleTrigger; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Stephane Nicoll @@ -36,8 +36,8 @@ public void createWithoutJobDetail() throws ParseException { factory.setRepeatInterval(1000L); factory.afterPropertiesSet(); SimpleTrigger trigger = factory.getObject(); - assertEquals(5, trigger.getRepeatCount()); - assertEquals(1000L, trigger.getRepeatInterval()); + assertThat(trigger.getRepeatCount()).isEqualTo(5); + assertThat(trigger.getRepeatInterval()).isEqualTo(1000L); } } diff --git a/spring-context-support/src/test/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBeanTests.java b/spring-context-support/src/test/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBeanTests.java new file mode 100644 index 000000000000..c0a07e448a0c --- /dev/null +++ b/spring-context-support/src/test/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBeanTests.java @@ -0,0 +1,102 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.ui.freemarker; + +import java.util.HashMap; +import java.util.Properties; + +import freemarker.template.Configuration; +import freemarker.template.Template; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIOException; + +/** + * @author Juergen Hoeller + * @author Issam El-atif + * @author Sam Brannen + */ +public class FreeMarkerConfigurationFactoryBeanTests { + + private final FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean(); + + @Test + public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception { + fcfb.setConfigLocation(new FileSystemResource("myprops.properties")); + Properties props = new Properties(); + props.setProperty("myprop", "/mydir"); + fcfb.setFreemarkerSettings(props); + assertThatIOException().isThrownBy(fcfb::afterPropertiesSet); + } + + @Test + public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception { + fcfb.setTemplateLoaderPath("file:/mydir"); + fcfb.afterPropertiesSet(); + Configuration cfg = fcfb.getObject(); + assertThat(cfg.getTemplateLoader()).isInstanceOf(SpringTemplateLoader.class); + } + + @Test + @SuppressWarnings("rawtypes") + public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception { + fcfb.setTemplateLoaderPath("file:/mydir"); + Properties settings = new Properties(); + settings.setProperty("localized_lookup", "false"); + fcfb.setFreemarkerSettings(settings); + fcfb.setResourceLoader(new ResourceLoader() { + @Override + public Resource getResource(String location) { + if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) { + throw new IllegalArgumentException(location); + } + return new ByteArrayResource("test".getBytes(), "test"); + } + @Override + public ClassLoader getClassLoader() { + return getClass().getClassLoader(); + } + }); + fcfb.afterPropertiesSet(); + assertThat(fcfb.getObject()).isInstanceOf(Configuration.class); + Configuration fc = fcfb.getObject(); + Template ft = fc.getTemplate("test"); + assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test"); + } + + @Test // SPR-12448 + public void freeMarkerConfigurationAsBean() { + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + RootBeanDefinition loaderDef = new RootBeanDefinition(SpringTemplateLoader.class); + loaderDef.getConstructorArgumentValues().addGenericArgumentValue(new DefaultResourceLoader()); + loaderDef.getConstructorArgumentValues().addGenericArgumentValue("/freemarker"); + RootBeanDefinition configDef = new RootBeanDefinition(Configuration.class); + configDef.getPropertyValues().add("templateLoader", loaderDef); + beanFactory.registerBeanDefinition("freeMarkerConfig", configDef); + assertThat(beanFactory.getBean(Configuration.class)).isNotNull(); + } + +} diff --git a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/BeanValidationPostProcessorTests.java b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/BeanValidationPostProcessorTests.java index 8d0d3c1577d2..4d014707077a 100644 --- a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/BeanValidationPostProcessorTests.java +++ b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/BeanValidationPostProcessorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -20,16 +20,17 @@ import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor; import org.springframework.context.support.GenericApplicationContext; -import org.springframework.tests.sample.beans.TestBean; import org.springframework.validation.beanvalidation.BeanValidationPostProcessor; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Juergen Hoeller @@ -42,14 +43,9 @@ public void testNotNullConstraint() { ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class)); ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class)); ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class)); - try { - ac.refresh(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getRootCause().getMessage().contains("testBean")); - assertTrue(ex.getRootCause().getMessage().contains("invalid")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + ac::refresh) + .satisfies(ex -> assertThat(ex.getRootCause().getMessage()).contains("testBean", "invalid")); ac.close(); } @@ -85,14 +81,9 @@ public void testSizeConstraint() { bd.getPropertyValues().add("testBean", new TestBean()); bd.getPropertyValues().add("stringValue", "s"); ac.registerBeanDefinition("bean", bd); - try { - ac.refresh(); - fail("Should have thrown BeanCreationException"); - } - catch (BeanCreationException ex) { - assertTrue(ex.getRootCause().getMessage().contains("stringValue")); - assertTrue(ex.getRootCause().getMessage().contains("invalid")); - } + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( + ac::refresh) + .satisfies(ex -> assertThat(ex.getRootCause().getMessage()).contains("stringValue", "invalid")); ac.close(); } @@ -135,7 +126,7 @@ public void setStringValue(String stringValue) { @PostConstruct public void init() { - assertNotNull("Shouldn't be here after constraint checking", this.testBean); + assertThat(this.testBean).as("Shouldn't be here after constraint checking").isNotNull(); } } diff --git a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/MethodValidationTests.java b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/MethodValidationTests.java index 0fed965eca1d..763bc156f684 100644 --- a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/MethodValidationTests.java +++ b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/MethodValidationTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -18,15 +18,18 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; + +import javax.validation.ValidationException; import javax.validation.Validator; import javax.validation.constraints.Max; import javax.validation.constraints.NotNull; import javax.validation.groups.Default; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.factory.FactoryBean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -40,7 +43,8 @@ import org.springframework.validation.beanvalidation.MethodValidationInterceptor; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Juergen Hoeller @@ -48,15 +52,17 @@ public class MethodValidationTests { @Test + @SuppressWarnings("unchecked") public void testMethodValidationInterceptor() { MyValidBean bean = new MyValidBean(); ProxyFactory proxyFactory = new ProxyFactory(bean); proxyFactory.addAdvice(new MethodValidationInterceptor()); proxyFactory.addAdvisor(new AsyncAnnotationAdvisor()); - doTestProxyValidation((MyValidInterface) proxyFactory.getProxy()); + doTestProxyValidation((MyValidInterface) proxyFactory.getProxy()); } @Test + @SuppressWarnings("unchecked") public void testMethodValidationPostProcessor() { StaticApplicationContext ac = new StaticApplicationContext(); ac.registerSingleton("mvpp", MethodValidationPostProcessor.class); @@ -69,61 +75,40 @@ public void testMethodValidationPostProcessor() { ac.close(); } - private void doTestProxyValidation(MyValidInterface proxy) { - assertNotNull(proxy.myValidMethod("value", 5)); - try { - assertNotNull(proxy.myValidMethod("value", 15)); - fail("Should have thrown ValidationException"); - } - catch (javax.validation.ValidationException ex) { - // expected - } - try { - assertNotNull(proxy.myValidMethod(null, 5)); - fail("Should have thrown ValidationException"); - } - catch (javax.validation.ValidationException ex) { - // expected - } - try { - assertNotNull(proxy.myValidMethod("value", 0)); - fail("Should have thrown ValidationException"); - } - catch (javax.validation.ValidationException ex) { - // expected - } - + private void doTestProxyValidation(MyValidInterface proxy) { + assertThat(proxy.myValidMethod("value", 5)).isNotNull(); + assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> + proxy.myValidMethod("value", 15)); + assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> + proxy.myValidMethod(null, 5)); + assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> + proxy.myValidMethod("value", 0)); proxy.myValidAsyncMethod("value", 5); - try { - proxy.myValidAsyncMethod("value", 15); - fail("Should have thrown ValidationException"); - } - catch (javax.validation.ValidationException ex) { - // expected - } - try { - proxy.myValidAsyncMethod(null, 5); - fail("Should have thrown ValidationException"); - } - catch (javax.validation.ValidationException ex) { - // expected - } - - assertEquals("myValue", proxy.myGenericMethod("myValue")); - try { - proxy.myGenericMethod(null); - fail("Should have thrown ValidationException"); - } - catch (javax.validation.ValidationException ex) { - // expected - } + assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> + proxy.myValidAsyncMethod("value", 15)); + assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> + proxy.myValidAsyncMethod(null, 5)); + assertThat(proxy.myGenericMethod("myValue")).isEqualTo("myValue"); + assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> + proxy.myGenericMethod(null)); } @Test public void testLazyValidatorForMethodValidation() { + @SuppressWarnings("resource") AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( - LazyMethodValidationConfig.class, CustomValidatorBean.class, MyValidBean.class); - ctx.getBean(MyValidInterface.class).myValidMethod("value", 5); + LazyMethodValidationConfig.class, CustomValidatorBean.class, + MyValidBean.class, MyValidFactoryBean.class); + ctx.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5)); + } + + @Test + public void testLazyValidatorForMethodValidationWithProxyTargetClass() { + @SuppressWarnings("resource") + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + LazyMethodValidationConfigWithProxyTargetClass.class, CustomValidatorBean.class, + MyValidBean.class, MyValidFactoryBean.class); + ctx.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5)); } @@ -146,6 +131,35 @@ public String myGenericMethod(String value) { } + @MyStereotype + public static class MyValidFactoryBean implements FactoryBean, MyValidInterface { + + @Override + public String getObject() { + return null; + } + + @Override + public Class getObjectType() { + return String.class; + } + + @Override + public Object myValidMethod(String arg1, int arg2) { + return (arg2 == 0 ? null : "value"); + } + + @Override + public void myValidAsyncMethod(String arg1, int arg2) { + } + + @Override + public String myGenericMethod(String value) { + return value; + } + } + + public interface MyValidInterface { @NotNull Object myValidMethod(@NotNull(groups = MyGroup.class) String arg1, @Max(10) int arg2); @@ -188,4 +202,17 @@ public static MethodValidationPostProcessor methodValidationPostProcessor(@Lazy } } + + @Configuration + public static class LazyMethodValidationConfigWithProxyTargetClass { + + @Bean + public static MethodValidationPostProcessor methodValidationPostProcessor(@Lazy Validator validator) { + MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor(); + postProcessor.setValidator(validator); + postProcessor.setProxyTargetClass(true); + return postProcessor; + } + } + } diff --git a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/SpringValidatorAdapterTests.java b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/SpringValidatorAdapterTests.java index 8fab425d2a28..99f9d412910d 100644 --- a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/SpringValidatorAdapterTests.java +++ b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/SpringValidatorAdapterTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -17,9 +17,11 @@ package org.springframework.validation.beanvalidation2; import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; import java.util.ArrayList; @@ -31,9 +33,11 @@ import java.util.Locale; import java.util.Map; import java.util.Set; + import javax.validation.Constraint; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; +import javax.validation.ConstraintViolation; import javax.validation.Payload; import javax.validation.Valid; import javax.validation.Validation; @@ -42,20 +46,19 @@ import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; import org.springframework.context.support.StaticMessageSource; +import org.springframework.core.testfixture.io.SerializationTestUtils; import org.springframework.util.ObjectUtils; import org.springframework.validation.BeanPropertyBindingResult; +import org.springframework.validation.FieldError; import org.springframework.validation.beanvalidation.SpringValidatorAdapter; -import static java.lang.annotation.ElementType.*; -import static java.lang.annotation.RetentionPolicy.*; -import static org.hamcrest.core.Is.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Kazuki Shimizu @@ -70,10 +73,10 @@ public class SpringValidatorAdapterTests { private final StaticMessageSource messageSource = new StaticMessageSource(); - @Before + @BeforeEach public void setupSpringValidatorAdapter() { - messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}"); - messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value with {1}"); + messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} must be between {2} and {1}"); + messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value as {1}"); messageSource.addMessage("password", Locale.ENGLISH, "Password"); messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)"); } @@ -82,11 +85,11 @@ public void setupSpringValidatorAdapter() { @Test public void testUnwrap() { Validator nativeValidator = validatorAdapter.unwrap(Validator.class); - assertSame(this.nativeValidator, nativeValidator); + assertThat(nativeValidator).isSameAs(this.nativeValidator); } @Test // SPR-13406 - public void testNoStringArgumentValue() { + public void testNoStringArgumentValue() throws Exception { TestBean testBean = new TestBean(); testBean.setPassword("pass"); testBean.setConfirmPassword("pass"); @@ -94,14 +97,18 @@ public void testNoStringArgumentValue() { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); - assertThat(errors.getFieldErrorCount("password"), is(1)); - assertThat(errors.getFieldValue("password"), is("pass")); - assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), - is("Size of Password is must be between 8 and 128")); + assertThat(errors.getFieldErrorCount("password")).isEqualTo(1); + assertThat(errors.getFieldValue("password")).isEqualTo("pass"); + FieldError error = errors.getFieldError("password"); + assertThat(error).isNotNull(); + assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128"); + assertThat(error.contains(ConstraintViolation.class)).isTrue(); + assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password"); + assertThat(SerializationTestUtils.serializeAndDeserialize(error.toString())).isEqualTo(error.toString()); } @Test // SPR-13406 - public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName() { + public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName() throws Exception { TestBean testBean = new TestBean(); testBean.setPassword("password"); testBean.setConfirmPassword("PASSWORD"); @@ -109,10 +116,14 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLog BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); - assertThat(errors.getFieldErrorCount("password"), is(1)); - assertThat(errors.getFieldValue("password"), is("password")); - assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), - is("Password must be same value with Password(Confirm)")); + assertThat(errors.getFieldErrorCount("password")).isEqualTo(1); + assertThat(errors.getFieldValue("password")).isEqualTo("password"); + FieldError error = errors.getFieldError("password"); + assertThat(error).isNotNull(); + assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Password must be same value as Password(Confirm)"); + assertThat(error.contains(ConstraintViolation.class)).isTrue(); + assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password"); + assertThat(SerializationTestUtils.serializeAndDeserialize(error.toString())).isEqualTo(error.toString()); } @Test // SPR-13406 @@ -124,13 +135,19 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedL BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); - assertThat(errors.getFieldErrorCount("email"), is(1)); - assertThat(errors.getFieldValue("email"), is("test@example.com")); - assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); - assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), - is("email must be same value with confirmEmail")); - assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), - is("Email required")); + assertThat(errors.getFieldErrorCount("email")).isEqualTo(1); + assertThat(errors.getFieldValue("email")).isEqualTo("test@example.com"); + assertThat(errors.getFieldErrorCount("confirmEmail")).isEqualTo(1); + FieldError error1 = errors.getFieldError("email"); + FieldError error2 = errors.getFieldError("confirmEmail"); + assertThat(error1).isNotNull(); + assertThat(error2).isNotNull(); + assertThat(messageSource.getMessage(error1, Locale.ENGLISH)).isEqualTo("email must be same value as confirmEmail"); + assertThat(messageSource.getMessage(error2, Locale.ENGLISH)).isEqualTo("Email required"); + assertThat(error1.contains(ConstraintViolation.class)).isTrue(); + assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("email"); + assertThat(error2.contains(ConstraintViolation.class)).isTrue(); + assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("confirmEmail"); } @Test // SPR-15123 @@ -144,13 +161,37 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMe BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); - assertThat(errors.getFieldErrorCount("email"), is(1)); - assertThat(errors.getFieldValue("email"), is("test@example.com")); - assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); - assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), - is("email must be same value with confirmEmail")); - assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), - is("Email required")); + assertThat(errors.getFieldErrorCount("email")).isEqualTo(1); + assertThat(errors.getFieldValue("email")).isEqualTo("test@example.com"); + assertThat(errors.getFieldErrorCount("confirmEmail")).isEqualTo(1); + FieldError error1 = errors.getFieldError("email"); + FieldError error2 = errors.getFieldError("confirmEmail"); + assertThat(error1).isNotNull(); + assertThat(error2).isNotNull(); + assertThat(messageSource.getMessage(error1, Locale.ENGLISH)).isEqualTo("email must be same value as confirmEmail"); + assertThat(messageSource.getMessage(error2, Locale.ENGLISH)).isEqualTo("Email required"); + assertThat(error1.contains(ConstraintViolation.class)).isTrue(); + assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("email"); + assertThat(error2.contains(ConstraintViolation.class)).isTrue(); + assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("confirmEmail"); + } + + @Test + public void testPatternMessage() { + TestBean testBean = new TestBean(); + testBean.setEmail("X"); + testBean.setConfirmEmail("X"); + + BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); + validatorAdapter.validate(testBean, errors); + + assertThat(errors.getFieldErrorCount("email")).isEqualTo(1); + assertThat(errors.getFieldValue("email")).isEqualTo("X"); + FieldError error = errors.getFieldError("email"); + assertThat(error).isNotNull(); + assertThat(messageSource.getMessage(error, Locale.ENGLISH)).contains("[\\w.'-]{1,}@[\\w.'-]{1,}"); + assertThat(error.contains(ConstraintViolation.class)).isTrue(); + assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("email"); } @Test // SPR-16177 @@ -162,7 +203,7 @@ public void testWithList() { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent"); validatorAdapter.validate(parent, errors); - assertTrue(errors.getErrorCount() > 0); + assertThat(errors.getErrorCount() > 0).isTrue(); } @Test // SPR-16177 @@ -174,7 +215,7 @@ public void testWithSet() { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent"); validatorAdapter.validate(parent, errors); - assertTrue(errors.getErrorCount() > 0); + assertThat(errors.getErrorCount() > 0).isTrue(); } private List createChildren(Parent parent) { @@ -199,8 +240,8 @@ public void testListElementConstraint() { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean"); validatorAdapter.validate(bean, errors); - assertThat(errors.getFieldErrorCount("property[4]"), is(1)); - assertNull(errors.getFieldValue("property[4]")); + assertThat(errors.getFieldErrorCount("property[4]")).isEqualTo(1); + assertThat(errors.getFieldValue("property[4]")).isNull(); } @Test // SPR-15839 @@ -214,8 +255,8 @@ public void testMapValueConstraint() { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean"); validatorAdapter.validate(bean, errors); - assertThat(errors.getFieldErrorCount("property[no value can be]"), is(1)); - assertNull(errors.getFieldValue("property[no value can be]")); + assertThat(errors.getFieldErrorCount("property[no value can be]")).isEqualTo(1); + assertThat(errors.getFieldValue("property[no value can be]")).isNull(); } @Test // SPR-15839 @@ -229,8 +270,8 @@ public void testMapEntryConstraint() { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean"); validatorAdapter.validate(bean, errors); - assertTrue(errors.hasFieldErrors("property[]")); - assertNull(errors.getFieldValue("property[]")); + assertThat(errors.hasFieldErrors("property[]")).isTrue(); + assertThat(errors.getFieldValue("property[]")).isNull(); } @@ -243,6 +284,7 @@ static class TestBean { private String confirmPassword; + @Pattern(regexp = "[\\w.'-]{1,}@[\\w.'-]{1,}") private String email; @Pattern(regexp = "[\\p{L} -]*", message = "Email required") @@ -284,8 +326,8 @@ public void setConfirmEmail(String confirmEmail) { @Documented @Constraint(validatedBy = {SameValidator.class}) - @Target({TYPE, ANNOTATION_TYPE}) - @Retention(RUNTIME) + @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) + @Retention(RetentionPolicy.RUNTIME) @Repeatable(SameGroup.class) @interface Same { @@ -299,8 +341,8 @@ public void setConfirmEmail(String confirmEmail) { String comparingField(); - @Target({TYPE, ANNOTATION_TYPE}) - @Retention(RUNTIME) + @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) + @Retention(RetentionPolicy.RUNTIME) @Documented @interface List { Same[] value(); @@ -310,8 +352,8 @@ public void setConfirmEmail(String confirmEmail) { @Documented @Inherited - @Retention(RUNTIME) - @Target({TYPE, ANNOTATION_TYPE}) + @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) + @Retention(RetentionPolicy.RUNTIME) @interface SameGroup { Same[] value(); @@ -326,12 +368,14 @@ public static class SameValidator implements ConstraintValidator { private String message; + @Override public void initialize(Same constraintAnnotation) { field = constraintAnnotation.field(); comparingField = constraintAnnotation.comparingField(); message = constraintAnnotation.message(); } + @Override public boolean isValid(Object value, ConstraintValidatorContext context) { BeanWrapper beanWrapper = new BeanWrapperImpl(value); Object fieldValue = beanWrapper.getPropertyValue(field); @@ -403,13 +447,13 @@ public static class Child { private Integer id; - @javax.validation.constraints.NotNull + @NotNull private String name; - @javax.validation.constraints.NotNull + @NotNull private Integer age; - @javax.validation.constraints.NotNull + @NotNull private Parent parent; public Integer getId() { @@ -447,7 +491,7 @@ public void setParent(Parent parent) { @Constraint(validatedBy = AnythingValidator.class) - @Retention(RUNTIME) + @Retention(RetentionPolicy.RUNTIME) public @interface AnythingValid { String message() default "{AnythingValid.message}"; @@ -468,22 +512,22 @@ public void initialize(AnythingValid constraintAnnotation) { @Override public boolean isValid(Object value, ConstraintValidatorContext context) { - List fieldsErros = new ArrayList<>(); - Arrays.asList(value.getClass().getDeclaredFields()).forEach(f -> { - f.setAccessible(true); + List fieldsErrors = new ArrayList<>(); + Arrays.asList(value.getClass().getDeclaredFields()).forEach(field -> { + field.setAccessible(true); try { - if (!f.getName().equals(ID) && f.get(value) == null) { - fieldsErros.add(f); + if (!field.getName().equals(ID) && field.get(value) == null) { + fieldsErrors.add(field); context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()) - .addPropertyNode(f.getName()) + .addPropertyNode(field.getName()) .addConstraintViolation(); } - } catch (IllegalAccessException ex) { + } + catch (IllegalAccessException ex) { throw new IllegalStateException(ex); } - }); - return fieldsErros.isEmpty(); + return fieldsErrors.isEmpty(); } } diff --git a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/ValidatorFactoryTests.java b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/ValidatorFactoryTests.java index bc4aa2a2fdbe..77e473dc084e 100644 --- a/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/ValidatorFactoryTests.java +++ b/spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/ValidatorFactoryTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -27,6 +27,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; + import javax.validation.Constraint; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; @@ -39,7 +40,7 @@ import org.hibernate.validator.HibernateValidator; import org.hibernate.validator.HibernateValidatorFactory; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ConfigurableApplicationContext; @@ -52,41 +53,39 @@ import org.springframework.validation.ObjectError; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Juergen Hoeller */ +@SuppressWarnings("resource") public class ValidatorFactoryTests { @Test + @SuppressWarnings("cast") public void testSimpleValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); Set> result = validator.validate(person); - assertEquals(2, result.size()); + assertThat(result.size()).isEqualTo(2); for (ConstraintViolation cv : result) { String path = cv.getPropertyPath().toString(); - if ("name".equals(path) || "address.street".equals(path)) { - assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); - } - else { - fail("Invalid constraint violation with path '" + path + "'"); - } + assertThat(path).matches(actual -> "name".equals(actual) || "address.street".equals(actual)); + assertThat(cv.getConstraintDescriptor().getAnnotation()).isInstanceOf(NotNull.class); } Validator nativeValidator = validator.unwrap(Validator.class); - assertTrue(nativeValidator.getClass().getName().startsWith("org.hibernate")); - assertTrue(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory); - assertTrue(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory); + assertThat(nativeValidator.getClass().getName().startsWith("org.hibernate")).isTrue(); + assertThat(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory).isTrue(); + assertThat(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory).isTrue(); validator.destroy(); } @Test + @SuppressWarnings("cast") public void testSimpleValidationWithCustomProvider() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setProviderClass(HibernateValidator.class); @@ -94,21 +93,17 @@ public void testSimpleValidationWithCustomProvider() { ValidPerson person = new ValidPerson(); Set> result = validator.validate(person); - assertEquals(2, result.size()); + assertThat(result.size()).isEqualTo(2); for (ConstraintViolation cv : result) { String path = cv.getPropertyPath().toString(); - if ("name".equals(path) || "address.street".equals(path)) { - assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); - } - else { - fail("Invalid constraint violation with path '" + path + "'"); - } + assertThat(path).matches(actual -> "name".equals(actual) || "address.street".equals(actual)); + assertThat(cv.getConstraintDescriptor().getAnnotation()).isInstanceOf(NotNull.class); } Validator nativeValidator = validator.unwrap(Validator.class); - assertTrue(nativeValidator.getClass().getName().startsWith("org.hibernate")); - assertTrue(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory); - assertTrue(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory); + assertThat(nativeValidator.getClass().getName().startsWith("org.hibernate")).isTrue(); + assertThat(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory).isTrue(); + assertThat(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory).isTrue(); validator.destroy(); } @@ -122,11 +117,11 @@ public void testSimpleValidationWithClassLevel() { person.setName("Juergen"); person.getAddress().setStreet("Juergen's Street"); Set> result = validator.validate(person); - assertEquals(1, result.size()); + assertThat(result.size()).isEqualTo(1); Iterator> iterator = result.iterator(); ConstraintViolation cv = iterator.next(); - assertEquals("", cv.getPropertyPath().toString()); - assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NameAddressValid); + assertThat(cv.getPropertyPath().toString()).isEqualTo(""); + assertThat(cv.getConstraintDescriptor().getAnnotation() instanceof NameAddressValid).isTrue(); } @Test @@ -139,9 +134,8 @@ public void testSpringValidationFieldType() { person.getAddress().setStreet("Phil's Street"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person"); validator.validate(person, errors); - assertEquals(1, errors.getErrorCount()); - assertThat("Field/Value type mismatch", errors.getFieldError("address").getRejectedValue(), - instanceOf(ValidAddress.class)); + assertThat(errors.getErrorCount()).isEqualTo(1); + assertThat(errors.getFieldError("address").getRejectedValue()).isInstanceOf(ValidAddress.class); } @Test @@ -152,24 +146,24 @@ public void testSpringValidation() { ValidPerson person = new ValidPerson(); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); - assertEquals(2, result.getErrorCount()); + assertThat(result.getErrorCount()).isEqualTo(2); FieldError fieldError = result.getFieldError("name"); - assertEquals("name", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("name"); List errorCodes = Arrays.asList(fieldError.getCodes()); - assertEquals(4, errorCodes.size()); - assertTrue(errorCodes.contains("NotNull.person.name")); - assertTrue(errorCodes.contains("NotNull.name")); - assertTrue(errorCodes.contains("NotNull.java.lang.String")); - assertTrue(errorCodes.contains("NotNull")); + assertThat(errorCodes.size()).isEqualTo(4); + assertThat(errorCodes.contains("NotNull.person.name")).isTrue(); + assertThat(errorCodes.contains("NotNull.name")).isTrue(); + assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue(); + assertThat(errorCodes.contains("NotNull")).isTrue(); fieldError = result.getFieldError("address.street"); - assertEquals("address.street", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("address.street"); errorCodes = Arrays.asList(fieldError.getCodes()); - assertEquals(5, errorCodes.size()); - assertTrue(errorCodes.contains("NotNull.person.address.street")); - assertTrue(errorCodes.contains("NotNull.address.street")); - assertTrue(errorCodes.contains("NotNull.street")); - assertTrue(errorCodes.contains("NotNull.java.lang.String")); - assertTrue(errorCodes.contains("NotNull")); + assertThat(errorCodes.size()).isEqualTo(5); + assertThat(errorCodes.contains("NotNull.person.address.street")).isTrue(); + assertThat(errorCodes.contains("NotNull.address.street")).isTrue(); + assertThat(errorCodes.contains("NotNull.street")).isTrue(); + assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue(); + assertThat(errorCodes.contains("NotNull")).isTrue(); } @Test @@ -182,12 +176,12 @@ public void testSpringValidationWithClassLevel() { person.getAddress().setStreet("Juergen's Street"); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); - assertEquals(1, result.getErrorCount()); + assertThat(result.getErrorCount()).isEqualTo(1); ObjectError globalError = result.getGlobalError(); List errorCodes = Arrays.asList(globalError.getCodes()); - assertEquals(2, errorCodes.size()); - assertTrue(errorCodes.contains("NameAddressValid.person")); - assertTrue(errorCodes.contains("NameAddressValid")); + assertThat(errorCodes.size()).isEqualTo(2); + assertThat(errorCodes.contains("NameAddressValid.person")).isTrue(); + assertThat(errorCodes.contains("NameAddressValid")).isTrue(); } @Test @@ -202,12 +196,12 @@ public void testSpringValidationWithAutowiredValidator() { person.getAddress().setStreet("Juergen's Street"); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); - assertEquals(1, result.getErrorCount()); + assertThat(result.getErrorCount()).isEqualTo(1); ObjectError globalError = result.getGlobalError(); List errorCodes = Arrays.asList(globalError.getCodes()); - assertEquals(2, errorCodes.size()); - assertTrue(errorCodes.contains("NameAddressValid.person")); - assertTrue(errorCodes.contains("NameAddressValid")); + assertThat(errorCodes.size()).isEqualTo(2); + assertThat(errorCodes.contains("NameAddressValid.person")).isTrue(); + assertThat(errorCodes.contains("NameAddressValid")).isTrue(); ctx.close(); } @@ -220,13 +214,13 @@ public void testSpringValidationWithErrorInListElement() { person.getAddressList().add(new ValidAddress()); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); - assertEquals(3, result.getErrorCount()); + assertThat(result.getErrorCount()).isEqualTo(3); FieldError fieldError = result.getFieldError("name"); - assertEquals("name", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("name"); fieldError = result.getFieldError("address.street"); - assertEquals("address.street", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("address.street"); fieldError = result.getFieldError("addressList[0].street"); - assertEquals("addressList[0].street", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("addressList[0].street"); } @Test @@ -238,13 +232,13 @@ public void testSpringValidationWithErrorInSetElement() { person.getAddressSet().add(new ValidAddress()); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); - assertEquals(3, result.getErrorCount()); + assertThat(result.getErrorCount()).isEqualTo(3); FieldError fieldError = result.getFieldError("name"); - assertEquals("name", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("name"); fieldError = result.getFieldError("address.street"); - assertEquals("address.street", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("address.street"); fieldError = result.getFieldError("addressSet[].street"); - assertEquals("addressSet[].street", fieldError.getField()); + assertThat(fieldError.getField()).isEqualTo("addressSet[].street"); } @Test @@ -256,7 +250,7 @@ public void testInnerBeanValidation() { Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); - assertNull(rejected); + assertThat(rejected).isNull(); } @Test @@ -268,7 +262,7 @@ public void testValidationWithOptionalField() { Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); - assertNull(rejected); + assertThat(rejected).isNull(); } @Test @@ -284,7 +278,10 @@ public void testListValidation() { errors.initConversion(new DefaultConversionService()); validator.validate(listContainer, errors); - assertEquals("X", errors.getFieldValue("list[1]")); + FieldError fieldError = errors.getFieldError("list[1]"); + assertThat(fieldError).isNotNull(); + assertThat(fieldError.getRejectedValue()).isEqualTo("X"); + assertThat(errors.getFieldValue("list[1]")).isEqualTo("X"); } @@ -379,7 +376,7 @@ public void initialize(NameAddressValid constraintAnnotation) { @Override public boolean isValid(ValidPerson value, ConstraintValidatorContext context) { if (value.expectsAutowiredValidator) { - assertNotNull(this.environment); + assertThat(this.environment).isNotNull(); } boolean valid = (value.name == null || !value.address.street.contains(value.name)); if (!valid && "Phil".equals(value.name)) { diff --git a/spring-context-support/src/test/resources/log4j2-test.xml b/spring-context-support/src/test/resources/log4j2-test.xml index ee1cd2ad04a4..9f3f3485f5d9 100644 --- a/spring-context-support/src/test/resources/log4j2-test.xml +++ b/spring-context-support/src/test/resources/log4j2-test.xml @@ -2,7 +2,7 @@ - + diff --git a/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml b/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml index 9c3c96360da5..4f76dd6fb110 100644 --- a/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml +++ b/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml @@ -3,9 +3,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd + https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache - http://www.springframework.org/schema/cache/spring-cache.xsd"> + https://www.springframework.org/schema/cache/spring-cache.xsd"> diff --git a/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven.xml b/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven.xml index bd2a2edb898e..b487b40b2ab2 100644 --- a/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven.xml +++ b/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheNamespaceDriven.xml @@ -3,9 +3,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd + https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache - http://www.springframework.org/schema/cache/spring-cache.xsd"> + https://www.springframework.org/schema/cache/spring-cache.xsd"> diff --git a/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheStandaloneConfig.xml b/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheStandaloneConfig.xml index 467604f4ae98..428ea099ae3b 100644 --- a/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheStandaloneConfig.xml +++ b/spring-context-support/src/test/resources/org/springframework/cache/jcache/config/jCacheStandaloneConfig.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/databasePersistence.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/databasePersistence.xml index 8d72191ad624..9b7b97c07c36 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/databasePersistence.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/databasePersistence.xml @@ -1,8 +1,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd + http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/job-scheduling-data.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/job-scheduling-data.xml index 6b98c0d7d221..b437445431f3 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/job-scheduling-data.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/job-scheduling-data.xml @@ -1,7 +1,7 @@ diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleAnonymousMethodInvokingJobDetailFB.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleAnonymousMethodInvokingJobDetailFB.xml index bbe23b83ddd2..e45ccd195feb 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleAnonymousMethodInvokingJobDetailFB.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleAnonymousMethodInvokingJobDetailFB.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulers.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulers.xml index 02d8f3668b65..df9af20cd541 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulers.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulers.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml index d1b83b17ed28..e00dc29ab433 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml index fe15a1bb1dd6..7e7dfb14abb8 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml @@ -2,7 +2,7 @@ + https://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml index 17f789e2e3c8..0ba9c4a57a84 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml index b805ac5f0ec7..97952cd8a779 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml @@ -1,5 +1,5 @@ - + diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheKeyGenerator.java b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheKeyGenerator.java similarity index 57% rename from spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheKeyGenerator.java rename to spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheKeyGenerator.java index 98e3b3acfa7f..9bb225ba80f9 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheKeyGenerator.java +++ b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheKeyGenerator.java @@ -1,6 +1,23 @@ -package org.springframework.cache.jcache.support; +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.contextsupport.testfixture.cache; import java.lang.annotation.Annotation; + import javax.cache.annotation.CacheKeyGenerator; import javax.cache.annotation.CacheKeyInvocationContext; import javax.cache.annotation.GeneratedCacheKey; diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheResolver.java b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheResolver.java similarity index 78% rename from spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheResolver.java rename to spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheResolver.java index 93013e398b49..04567b1a8459 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheResolver.java +++ b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheResolver.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -14,14 +14,16 @@ * limitations under the License. */ -package org.springframework.cache.jcache.support; +package org.springframework.contextsupport.testfixture.cache; import java.lang.annotation.Annotation; + import javax.cache.Cache; import javax.cache.annotation.CacheInvocationContext; import javax.cache.annotation.CacheResolver; -import static org.mockito.BDDMockito.*; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * @author Stephane Nicoll @@ -31,6 +33,7 @@ public class TestableCacheResolver implements CacheResolver { @Override public Cache resolveCache(CacheInvocationContext cacheInvocationContext) { String cacheName = cacheInvocationContext.getCacheName(); + @SuppressWarnings("unchecked") Cache mock = mock(Cache.class); given(mock.getName()).willReturn(cacheName); return mock; diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheResolverFactory.java b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheResolverFactory.java similarity index 91% rename from spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheResolverFactory.java rename to spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheResolverFactory.java index 547497248c0a..5849090acd1b 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/support/TestableCacheResolverFactory.java +++ b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/cache/TestableCacheResolverFactory.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,9 +14,10 @@ * limitations under the License. */ -package org.springframework.cache.jcache.support; +package org.springframework.contextsupport.testfixture.cache; import java.lang.annotation.Annotation; + import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CacheResolver; import javax.cache.annotation.CacheResolverFactory; diff --git a/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/jcache/AbstractJCacheAnnotationTests.java b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/jcache/AbstractJCacheAnnotationTests.java new file mode 100644 index 000000000000..db5bb038e94f --- /dev/null +++ b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/jcache/AbstractJCacheAnnotationTests.java @@ -0,0 +1,474 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.contextsupport.testfixture.jcache; + +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; + +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.interceptor.SimpleKeyGenerator; +import org.springframework.context.ApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIOException; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; + +/** + * @author Stephane Nicoll + */ +public abstract class AbstractJCacheAnnotationTests { + + public static final String DEFAULT_CACHE = "default"; + + public static final String EXCEPTION_CACHE = "exception"; + + + protected String keyItem; + + protected ApplicationContext ctx; + + private JCacheableService service; + + private CacheManager cacheManager; + + protected abstract ApplicationContext getApplicationContext(); + + @BeforeEach + public void setUp(TestInfo testInfo) { + this.keyItem = testInfo.getTestMethod().get().getName(); + this.ctx = getApplicationContext(); + this.service = this.ctx.getBean(JCacheableService.class); + this.cacheManager = this.ctx.getBean("cacheManager", CacheManager.class); + } + + @Test + public void cache() { + Object first = service.cache(this.keyItem); + Object second = service.cache(this.keyItem); + assertThat(second).isSameAs(first); + } + + @Test + public void cacheNull() { + Cache cache = getCache(DEFAULT_CACHE); + + assertThat(cache.get(this.keyItem)).isNull(); + + Object first = service.cacheNull(this.keyItem); + Object second = service.cacheNull(this.keyItem); + assertThat(second).isSameAs(first); + + Cache.ValueWrapper wrapper = cache.get(this.keyItem); + assertThat(wrapper).isNotNull(); + assertThat(wrapper.get()).isSameAs(first); + assertThat(wrapper.get()).as("Cached value should be null").isNull(); + } + + @Test + public void cacheException() { + Cache cache = getCache(EXCEPTION_CACHE); + + Object key = createKey(this.keyItem); + assertThat(cache.get(key)).isNull(); + + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.cacheWithException(this.keyItem, true)); + + Cache.ValueWrapper result = cache.get(key); + assertThat(result).isNotNull(); + assertThat(result.get().getClass()).isEqualTo(UnsupportedOperationException.class); + } + + @Test + public void cacheExceptionVetoed() { + Cache cache = getCache(EXCEPTION_CACHE); + + Object key = createKey(this.keyItem); + assertThat(cache.get(key)).isNull(); + + assertThatNullPointerException().isThrownBy(() -> + service.cacheWithException(this.keyItem, false)); + assertThat(cache.get(key)).isNull(); + } + + @Test + public void cacheCheckedException() { + Cache cache = getCache(EXCEPTION_CACHE); + + Object key = createKey(this.keyItem); + assertThat(cache.get(key)).isNull(); + assertThatIOException().isThrownBy(() -> + service.cacheWithCheckedException(this.keyItem, true)); + + Cache.ValueWrapper result = cache.get(key); + assertThat(result).isNotNull(); + assertThat(result.get().getClass()).isEqualTo(IOException.class); + } + + + @SuppressWarnings("ThrowableResultOfMethodCallIgnored") + @Test + public void cacheExceptionRewriteCallStack() { + long ref = service.exceptionInvocations(); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.cacheWithException(this.keyItem, true)) + .satisfies(first -> { + // Sanity check, this particular call has called the service + // First call should not have been cached + assertThat(service.exceptionInvocations()).isEqualTo(ref + 1); + + UnsupportedOperationException second = methodInCallStack(this.keyItem); + // Sanity check, this particular call has *not* called the service + // Second call should have been cached + assertThat(service.exceptionInvocations()).isEqualTo(ref + 1); + + assertThat(first).hasCause(second.getCause()); + assertThat(first).hasMessage(second.getMessage()); + // Original stack must not contain any reference to methodInCallStack + assertThat(contain(first, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")).isFalse(); + assertThat(contain(second, AbstractJCacheAnnotationTests.class.getName(), "methodInCallStack")).isTrue(); + }); + } + + @Test + public void cacheAlwaysInvoke() { + Object first = service.cacheAlwaysInvoke(this.keyItem); + Object second = service.cacheAlwaysInvoke(this.keyItem); + assertThat(second).isNotSameAs(first); + } + + @Test + public void cacheWithPartialKey() { + Object first = service.cacheWithPartialKey(this.keyItem, true); + Object second = service.cacheWithPartialKey(this.keyItem, false); + // second argument not used, see config + assertThat(second).isSameAs(first); + } + + @Test + public void cacheWithCustomCacheResolver() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + service.cacheWithCustomCacheResolver(this.keyItem); + + // Cache in mock cache + assertThat(cache.get(key)).isNull(); + } + + @Test + public void cacheWithCustomKeyGenerator() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + service.cacheWithCustomKeyGenerator(this.keyItem, "ignored"); + + assertThat(cache.get(key)).isNull(); + } + + @Test + public void put() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + assertThat(cache.get(key)).isNull(); + + service.put(this.keyItem, value); + + Cache.ValueWrapper result = cache.get(key); + assertThat(result).isNotNull(); + assertThat(result.get()).isEqualTo(value); + } + + @Test + public void putWithException() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + assertThat(cache.get(key)).isNull(); + + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.putWithException(this.keyItem, value, true)); + + Cache.ValueWrapper result = cache.get(key); + assertThat(result).isNotNull(); + assertThat(result.get()).isEqualTo(value); + } + + @Test + public void putWithExceptionVetoPut() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + assertThat(cache.get(key)).isNull(); + + assertThatNullPointerException().isThrownBy(() -> + service.putWithException(this.keyItem, value, false)); + assertThat(cache.get(key)).isNull(); + } + + @Test + public void earlyPut() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + assertThat(cache.get(key)).isNull(); + + service.earlyPut(this.keyItem, value); + + Cache.ValueWrapper result = cache.get(key); + assertThat(result).isNotNull(); + assertThat(result.get()).isEqualTo(value); + } + + @Test + public void earlyPutWithException() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + assertThat(cache.get(key)).isNull(); + + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.earlyPutWithException(this.keyItem, value, true)); + + Cache.ValueWrapper result = cache.get(key); + assertThat(result).isNotNull(); + assertThat(result.get()).isEqualTo(value); + } + + @Test + public void earlyPutWithExceptionVetoPut() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + assertThat(cache.get(key)).isNull(); + assertThatNullPointerException().isThrownBy(() -> + service.earlyPutWithException(this.keyItem, value, false)); + // This will be cached anyway as the earlyPut has updated the cache before + Cache.ValueWrapper result = cache.get(key); + assertThat(result).isNotNull(); + assertThat(result.get()).isEqualTo(value); + } + + @Test + public void remove() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + cache.put(key, value); + + service.remove(this.keyItem); + + assertThat(cache.get(key)).isNull(); + } + + @Test + public void removeWithException() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + cache.put(key, value); + + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.removeWithException(this.keyItem, true)); + + assertThat(cache.get(key)).isNull(); + } + + @Test + public void removeWithExceptionVetoRemove() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + cache.put(key, value); + + assertThatNullPointerException().isThrownBy(() -> + service.removeWithException(this.keyItem, false)); + Cache.ValueWrapper wrapper = cache.get(key); + assertThat(wrapper).isNotNull(); + assertThat(wrapper.get()).isEqualTo(value); + } + + @Test + public void earlyRemove() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + cache.put(key, value); + + service.earlyRemove(this.keyItem); + + assertThat(cache.get(key)).isNull(); + } + + @Test + public void earlyRemoveWithException() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + cache.put(key, value); + + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.earlyRemoveWithException(this.keyItem, true)); + assertThat(cache.get(key)).isNull(); + } + + @Test + public void earlyRemoveWithExceptionVetoRemove() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + Object value = new Object(); + cache.put(key, value); + + assertThatNullPointerException().isThrownBy(() -> + service.earlyRemoveWithException(this.keyItem, false)); + // This will be remove anyway as the earlyRemove has removed the cache before + assertThat(cache.get(key)).isNull(); + } + + @Test + public void removeAll() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + cache.put(key, new Object()); + + service.removeAll(); + + assertThat(isEmpty(cache)).isTrue(); + } + + @Test + public void removeAllWithException() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + cache.put(key, new Object()); + + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.removeAllWithException(true)); + + assertThat(isEmpty(cache)).isTrue(); + } + + @Test + public void removeAllWithExceptionVetoRemove() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + cache.put(key, new Object()); + + assertThatNullPointerException().isThrownBy(() -> + service.removeAllWithException(false)); + assertThat(cache.get(key)).isNotNull(); + } + + @Test + public void earlyRemoveAll() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + cache.put(key, new Object()); + + service.earlyRemoveAll(); + + assertThat(isEmpty(cache)).isTrue(); + } + + @Test + public void earlyRemoveAllWithException() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + cache.put(key, new Object()); + + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> + service.earlyRemoveAllWithException(true)); + assertThat(isEmpty(cache)).isTrue(); + } + + @Test + public void earlyRemoveAllWithExceptionVetoRemove() { + Cache cache = getCache(DEFAULT_CACHE); + + Object key = createKey(this.keyItem); + cache.put(key, new Object()); + + assertThatNullPointerException().isThrownBy(() -> + service.earlyRemoveAllWithException(false)); + // This will be remove anyway as the earlyRemove has removed the cache before + assertThat(isEmpty(cache)).isTrue(); + } + + protected boolean isEmpty(Cache cache) { + ConcurrentHashMap nativeCache = (ConcurrentHashMap) cache.getNativeCache(); + return nativeCache.isEmpty(); + } + + + private Object createKey(Object... params) { + return SimpleKeyGenerator.generateKey(params); + } + + private Cache getCache(String name) { + Cache cache = cacheManager.getCache(name); + assertThat(cache).as("required cache " + name + " does not exist").isNotNull(); + return cache; + } + + /** + * The only purpose of this method is to invoke a particular method on the + * service so that the call stack is different. + */ + private UnsupportedOperationException methodInCallStack(String keyItem) { + try { + service.cacheWithException(keyItem, true); + throw new IllegalStateException("Should have thrown an exception"); + } + catch (UnsupportedOperationException e) { + return e; + } + } + + private boolean contain(Throwable t, String className, String methodName) { + for (StackTraceElement element : t.getStackTrace()) { + if (className.equals(element.getClassName()) && methodName.equals(element.getMethodName())) { + return true; + } + } + return false; + } + +} diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheableService.java b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/jcache/JCacheableService.java similarity index 93% rename from spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheableService.java rename to spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/jcache/JCacheableService.java index fe2fe4b56d1c..db36d1103207 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheableService.java +++ b/spring-context-support/src/testFixtures/java/org/springframework/contextsupport/testfixture/jcache/JCacheableService.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cache.jcache.config; +package org.springframework.contextsupport.testfixture.jcache; import java.io.IOException; diff --git a/spring-context/spring-context.gradle b/spring-context/spring-context.gradle index c252345fdc6a..30ab5d6e9a6a 100644 --- a/spring-context/spring-context.gradle +++ b/spring-context/spring-context.gradle @@ -1,6 +1,7 @@ description = "Spring Context" apply plugin: "groovy" +apply plugin: "kotlin" dependencies { compile(project(":spring-aop")) @@ -8,28 +9,41 @@ dependencies { compile(project(":spring-core")) compile(project(":spring-expression")) optional(project(":spring-instrument")) - optional("javax.annotation:javax.annotation-api:1.3.2") - optional("javax.ejb:javax.ejb-api:3.2") - optional("javax.enterprise.concurrent:javax.enterprise.concurrent-api:1.0") - optional("javax.inject:javax.inject:1") - optional("javax.interceptor:javax.interceptor-api:1.2.2") - optional("javax.money:money-api:1.0.3") + optional("javax.annotation:javax.annotation-api") + optional("javax.ejb:javax.ejb-api") + optional("javax.enterprise.concurrent:javax.enterprise.concurrent-api") + optional("javax.inject:javax.inject") + optional("javax.interceptor:javax.interceptor-api") + optional("javax.money:money-api") + // Overriding 2.0.1.Final due to Bean Validation 1.1 compatibility in LocalValidatorFactoryBean optional("javax.validation:validation-api:1.1.0.Final") - optional("javax.xml.ws:jaxws-api:2.3.0") - optional("org.aspectj:aspectjweaver:${aspectjVersion}") - optional("org.codehaus.groovy:groovy:${groovyVersion}") - optional("org.beanshell:bsh:2.0b5") - optional("joda-time:joda-time:2.10") - optional("org.hibernate:hibernate-validator:5.4.2.Final") - optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}") - optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}") - testCompile("org.codehaus.groovy:groovy-xml:${groovyVersion}") - testCompile("org.codehaus.groovy:groovy-jsr223:${groovyVersion}") - testCompile("org.codehaus.groovy:groovy-test:${groovyVersion}") - testCompile("org.apache.commons:commons-pool2:2.6.0") - testCompile("javax.inject:javax.inject-tck:1") - testCompile("org.awaitility:awaitility:3.1.2") - testRuntime("javax.xml.bind:jaxb-api:2.3.0") - testRuntime("org.glassfish:javax.el:3.0.1-b08") - testRuntime("org.javamoney:moneta:1.3") + optional("javax.xml.ws:jaxws-api") + optional("org.aspectj:aspectjweaver") + optional("org.codehaus.groovy:groovy") + optional("org.apache-extras.beanshell:bsh") + optional("joda-time:joda-time") + optional("org.hibernate:hibernate-validator:5.4.3.Final") + optional("org.jetbrains.kotlin:kotlin-reflect") + optional("org.jetbrains.kotlin:kotlin-stdlib") + optional("org.reactivestreams:reactive-streams") + testCompile(testFixtures(project(":spring-aop"))) + testCompile(testFixtures(project(":spring-beans"))) + testCompile(testFixtures(project(":spring-core"))) + testCompile("io.projectreactor:reactor-core") + testCompile("org.codehaus.groovy:groovy-jsr223") + testCompile("org.codehaus.groovy:groovy-test") + testCompile("org.codehaus.groovy:groovy-xml") + testCompile("org.apache.commons:commons-pool2") + testCompile("org.awaitility:awaitility") + testCompile("javax.inject:javax.inject-tck") + testRuntime("javax.xml.bind:jaxb-api") + testRuntime("org.glassfish:javax.el") + // Substitute for javax.management:jmxremote_optional:1.0.1_04 (not available on Maven Central) + testRuntime("org.glassfish.external:opendmk_jmxremote_optional_jar") + testRuntime("org.javamoney:moneta") + testRuntime("org.junit.vintage:junit-vintage-engine") // for @Inject TCK + testFixturesApi("org.junit.jupiter:junit-jupiter-api") + testFixturesImplementation(testFixtures(project(":spring-beans"))) + testFixturesImplementation("com.google.code.findbugs:jsr305") + testFixturesImplementation("org.assertj:assertj-core") } diff --git a/spring-context/src/main/java/org/springframework/cache/Cache.java b/spring-context/src/main/java/org/springframework/cache/Cache.java index 37f814181efb..dea4505d9ad2 100644 --- a/spring-context/src/main/java/org/springframework/cache/Cache.java +++ b/spring-context/src/main/java/org/springframework/cache/Cache.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -55,6 +55,7 @@ public interface Cache { * a cached {@code null} value. A straight {@code null} being * returned means that the cache contains no mapping for this key. * @see #get(Object, Class) + * @see #get(Object, Callable) */ @Nullable ValueWrapper get(Object key); @@ -94,6 +95,7 @@ public interface Cache { * @return the value to which this cache maps the specified key * @throws ValueRetrievalException if the {@code valueLoader} throws an exception * @since 4.3 + * @see #get(Object) */ @Nullable T get(Object key, Callable valueLoader); @@ -102,8 +104,13 @@ public interface Cache { * Associate the specified value with the specified key in this cache. *

If the cache previously contained a mapping for this key, the old * value is replaced by the specified value. + *

Actual registration may be performed in an asynchronous or deferred + * fashion, with subsequent lookups possibly not seeing the entry yet. + * This may for example be the case with transactional cache decorators. + * Use {@link #putIfAbsent} for guaranteed immediate registration. * @param key the key with which the specified value is to be associated * @param value the value to be associated with the specified key + * @see #putIfAbsent(Object, Object) */ void put(Object key, @Nullable Object value); @@ -112,19 +119,19 @@ public interface Cache { * if it is not set already. *

This is equivalent to: *


-	 * Object existingValue = cache.get(key);
+	 * ValueWrapper existingValue = cache.get(key);
 	 * if (existingValue == null) {
 	 *     cache.put(key, value);
-	 *     return null;
-	 * } else {
-	 *     return existingValue;
 	 * }
+	 * return existingValue;
 	 * 
* except that the action is performed atomically. While all out-of-the-box * {@link CacheManager} implementations are able to perform the put atomically, * the operation may also be implemented in two steps, e.g. with a check for * presence and a subsequent put, in a non-atomic way. Check the documentation * of the native cache implementation that you are using for more details. + *

The default implementation delegates to {@link #get(Object)} and + * {@link #put(Object, Object)} along the lines of the code snippet above. * @param key the key with which the specified value is to be associated * @param value the value to be associated with the specified key * @return the value to which this cache maps the specified key (which may be @@ -132,21 +139,73 @@ public interface Cache { * mapping for that key prior to this call. Returning {@code null} is therefore * an indicator that the given {@code value} has been associated with the key. * @since 4.1 + * @see #put(Object, Object) */ @Nullable - ValueWrapper putIfAbsent(Object key, @Nullable Object value); + default ValueWrapper putIfAbsent(Object key, @Nullable Object value) { + ValueWrapper existingValue = get(key); + if (existingValue == null) { + put(key, value); + } + return existingValue; + } /** * Evict the mapping for this key from this cache if it is present. + *

Actual eviction may be performed in an asynchronous or deferred + * fashion, with subsequent lookups possibly still seeing the entry. + * This may for example be the case with transactional cache decorators. + * Use {@link #evictIfPresent} for guaranteed immediate removal. * @param key the key whose mapping is to be removed from the cache + * @see #evictIfPresent(Object) */ void evict(Object key); /** - * Remove all mappings from the cache. + * Evict the mapping for this key from this cache if it is present, + * expecting the key to be immediately invisible for subsequent lookups. + *

The default implementation delegates to {@link #evict(Object)}, + * returning {@code false} for not-determined prior presence of the key. + * Cache providers and in particular cache decorators are encouraged + * to perform immediate eviction if possible (e.g. in case of generally + * deferred cache operations within a transaction) and to reliably + * determine prior presence of the given key. + * @param key the key whose mapping is to be removed from the cache + * @return {@code true} if the cache was known to have a mapping for + * this key before, {@code false} if it did not (or if prior presence + * could not be determined) + * @since 5.2 + * @see #evict(Object) + */ + default boolean evictIfPresent(Object key) { + evict(key); + return false; + } + + /** + * Clear the cache through removing all mappings. + *

Actual clearing may be performed in an asynchronous or deferred + * fashion, with subsequent lookups possibly still seeing the entries. + * This may for example be the case with transactional cache decorators. + * Use {@link #invalidate()} for guaranteed immediate removal of entries. + * @see #invalidate() */ void clear(); + /** + * Invalidate the cache through removing all mappings, expecting all + * entries to be immediately invisible for subsequent lookups. + * @return {@code true} if the cache was known to have mappings before, + * {@code false} if it did not (or if prior presence of entries could + * not be determined) + * @since 5.2 + * @see #clear() + */ + default boolean invalidate() { + clear(); + return false; + } + /** * A (wrapper) object representing a cache value. diff --git a/spring-context/src/main/java/org/springframework/cache/CacheManager.java b/spring-context/src/main/java/org/springframework/cache/CacheManager.java index fffb6bc2bd73..833715c63cb6 100644 --- a/spring-context/src/main/java/org/springframework/cache/CacheManager.java +++ b/spring-context/src/main/java/org/springframework/cache/CacheManager.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -22,23 +22,28 @@ /** * Spring's central cache manager SPI. - * Allows for retrieving named {@link Cache} regions. + * + *

Allows for retrieving named {@link Cache} regions. * * @author Costin Leau + * @author Sam Brannen * @since 3.1 */ public interface CacheManager { /** - * Return the cache associated with the given name. + * Get the cache associated with the given name. + *

Note that the cache may be lazily created at runtime if the + * native provider supports it. * @param name the cache identifier (must not be {@code null}) - * @return the associated cache, or {@code null} if none found + * @return the associated cache, or {@code null} if such a cache + * does not exist or could be not created */ @Nullable Cache getCache(String name); /** - * Return a collection of the cache names known by this manager. + * Get a collection of the cache names known by this manager. * @return the names of all caches known by the cache manager */ Collection getCacheNames(); diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java b/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java index c652ec00a0de..07d8394c38ac 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/AnnotationCacheOperationSource.java b/spring-context/src/main/java/org/springframework/cache/annotation/AnnotationCacheOperationSource.java index e2f8681e181e..5d34d0e1031f 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/AnnotationCacheOperationSource.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/AnnotationCacheOperationSource.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -103,6 +103,16 @@ public AnnotationCacheOperationSource(Set annotationParse } + @Override + public boolean isCandidateClass(Class targetClass) { + for (CacheAnnotationParser parser : this.annotationParsers) { + if (parser.isCandidateClass(targetClass)) { + return true; + } + } + return false; + } + @Override @Nullable protected Collection findCacheOperations(Class clazz) { @@ -127,8 +137,8 @@ protected Collection findCacheOperations(Method method) { @Nullable protected Collection determineCacheOperations(CacheOperationProvider provider) { Collection ops = null; - for (CacheAnnotationParser annotationParser : this.annotationParsers) { - Collection annOps = provider.getCacheOperations(annotationParser); + for (CacheAnnotationParser parser : this.annotationParsers) { + Collection annOps = provider.getCacheOperations(parser); if (annOps != null) { if (ops == null) { ops = annOps; @@ -154,7 +164,7 @@ protected boolean allowPublicMethodsOnly() { @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if (this == other) { return true; } diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/CacheAnnotationParser.java b/spring-context/src/main/java/org/springframework/cache/annotation/CacheAnnotationParser.java index e03bedee72f8..10231a157131 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/CacheAnnotationParser.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/CacheAnnotationParser.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed 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 + * https://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, @@ -30,12 +30,31 @@ * * @author Costin Leau * @author Stephane Nicoll + * @author Juergen Hoeller * @since 3.1 * @see AnnotationCacheOperationSource * @see SpringCacheAnnotationParser */ public interface CacheAnnotationParser { + /** + * Determine whether the given class is a candidate for cache operations + * in the annotation format of this {@code CacheAnnotationParser}. + *

If this method returns {@code false}, the methods on the given class + * will not get traversed for {@code #parseCacheAnnotations} introspection. + * Returning {@code false} is therefore an optimization for non-affected + * classes, whereas {@code true} simply means that the class needs to get + * fully introspected for each method on the given class individually. + * @param targetClass the class to introspect + * @return {@code false} if the class is known to have no cache operation + * annotations at class or method level; {@code true} otherwise. The default + * implementation returns {@code true}, leading to regular introspection. + * @since 5.2 + */ + default boolean isCandidateClass(Class targetClass) { + return true; + } + /** * Parse the cache definition for the given class, * based on an annotation type understood by this parser. diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/CacheConfig.java b/spring-context/src/main/java/org/springframework/cache/annotation/CacheConfig.java index de48d95dd284..234f353b142d 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/CacheConfig.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/CacheConfig.java @@ -5,7 +5,7 @@ * 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 + * https://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, diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/CacheEvict.java b/spring-context/src/main/java/org/springframework/cache/annotation/CacheEvict.java index 9364e800f250..5aa397885212 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/CacheEvict.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/CacheEvict.java @@ -5,7 +5,7 @@ * 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 + * https://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, @@ -38,7 +38,7 @@ * @since 3.1 * @see CacheConfig */ -@Target({ElementType.METHOD, ElementType.TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @@ -142,7 +142,7 @@ * occur irrespective of the method outcome (i.e., whether it threw an * exception or not). *

Defaults to {@code false}, meaning that the cache eviction operation - * will occur after the advised method is invoked successfully (i.e., + * will occur after the advised method is invoked successfully (i.e. * only if the invocation did not throw an exception). */ boolean beforeInvocation() default false; diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/CachePut.java b/spring-context/src/main/java/org/springframework/cache/annotation/CachePut.java index 72d41be08275..abf47bfd0fa1 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/CachePut.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/CachePut.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed 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 + * https://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, @@ -31,7 +31,8 @@ * *

In contrast to the {@link Cacheable @Cacheable} annotation, this annotation * does not cause the advised method to be skipped. Rather, it always causes the - * method to be invoked and its result to be stored in the associated cache. Note + * method to be invoked and its result to be stored in the associated cache if the + * {@link #condition()} and {@link #unless()} expressions match accordingly. Note * that Java8's {@code Optional} return types are automatically handled and its * content is stored in the cache if present. * @@ -45,7 +46,7 @@ * @since 3.1 * @see CacheConfig */ -@Target({ElementType.METHOD, ElementType.TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @@ -118,10 +119,15 @@ /** * Spring Expression Language (SpEL) expression used for making the cache * put operation conditional. + *

This expression is evaluated after the method has been called due to the + * nature of the put operation and can therefore refer to the {@code result}. *

Default is {@code ""}, meaning the method result is always cached. *

The SpEL expression evaluates against a dedicated context that provides the * following meta-data: *