{"id":22043547,"url":"https://github.com/glenkpeterson/testutils","last_synced_at":"2025-09-13T08:42:53.389Z","repository":{"id":34547533,"uuid":"38492169","full_name":"GlenKPeterson/TestUtils","owner":"GlenKPeterson","description":"Utilities for testing `.equals()`, `.compareTo()` and `Comparator` Java/Kotlin contracts.  Also FakeHttp(Request|Response)","archived":false,"fork":false,"pushed_at":"2022-03-18T22:56:46.000Z","size":281,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T13:47:37.145Z","etag":null,"topics":["comparators","equality","java","kotlin","testing"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GlenKPeterson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-03T12:42:19.000Z","updated_at":"2022-01-05T21:25:17.000Z","dependencies_parsed_at":"2022-07-16T09:06:02.714Z","dependency_job_id":null,"html_url":"https://github.com/GlenKPeterson/TestUtils","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/GlenKPeterson/TestUtils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlenKPeterson%2FTestUtils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlenKPeterson%2FTestUtils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlenKPeterson%2FTestUtils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlenKPeterson%2FTestUtils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GlenKPeterson","download_url":"https://codeload.github.com/GlenKPeterson/TestUtils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlenKPeterson%2FTestUtils/sbom","scorecard":{"id":56873,"data":{"date":"2025-08-11","repo":{"name":"github.com/GlenKPeterson/TestUtils","commit":"ca8b5d794e168903a4ce15f7241adfcfea0fb67b"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-15T00:56:37.863Z","repository_id":34547533,"created_at":"2025-08-15T00:56:37.863Z","updated_at":"2025-08-15T00:56:37.863Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274941230,"owners_count":25378203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-13T02:00:10.085Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["comparators","equality","java","kotlin","testing"],"created_at":"2024-11-30T12:16:33.495Z","updated_at":"2025-09-13T08:42:53.340Z","avatar_url":"https://github.com/GlenKPeterson.png","language":"Kotlin","readme":"# TestUtils\nUtilities for testing common Java/Kotlin contracts.  Currently: equals(), hashCode(), and compareTo().\nI find a bug almost every time I apply these tests to old code.  Usage is defined in the Javadocs.\n\nThe idea of contract-based testing was from watching Bill Venners:\nhttps://www.youtube.com/watch?v=bCTZQi2dpl8\nAny bugs are my own.\n\nThis project also includes fake Http servlet requests/responses useful for end-to-end testing java servlets. \n\n## Maven Dependency\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.organicdesign/TestUtils/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.organicdesign/TestUtils)\n[![javadoc](https://javadoc.io/badge2/org.organicdesign/TestUtils/javadoc.svg)](https://javadoc.io/doc/org.organicdesign/TestUtils)\n\nNote that this project is just for testing, so add it only to the `test` scope of your project:\n```xml\n\u003cdependency\u003e\n\t\u003cgroupId\u003eorg.organicdesign\u003c/groupId\u003e\n\t\u003cartifactId\u003eTestUtils\u003c/artifactId\u003e\n\t\u003cversion\u003e2.0.3\u003c/version\u003e\n\t\u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\n## Usage: Equality\n```java\nimport static org.organicdesign.testUtils.EqualsContract.equalsDistinctHashCode;\nimport static org.organicdesign.testUtils.EqualsContract.equalsSameHashCode;\n\npublic class PaddingTest {\n    @Test public void equalHashTest() {\n        // Test padding-top different\n        equalsDistinctHashCode(Padding.of(1),\n                               Padding.of(1,1,1,1),\n                               Padding.of(1),\n                               Padding.of(2,1,1,1));\n\n        // Test transposed padding-right and padding-bottom are different (but have same hashcode)\n        equalsSameHashCode(Padding.of(3, 5, 7, 1.1f),\n                           Padding.of(3, 5, 7, 1.1f),\n                           Padding.of(3, 5, 7, 1.1f),\n                           Padding.of(3, 7, 5, 1.1f));\n\n        // Padding values that differ by less than 0.1f have the same hashcode\n        // but are not equal.  Prove it (also tests when padding-left is different):\n        equalsSameHashCode(Padding.of(1),\n                           Padding.of(1, 1, 1, 1),\n                           Padding.of(1),\n                           Padding.of(1, 1, 1, 1.0001f));\n    }\n}\n```\n\nThe above is a suitable test for the class [com.planbase.pdf.layoutmanager.Padding](https://github.com/GlenKPeterson/PdfLayoutManager/blob/master/src/main/java/com/planbase/pdf/layoutmanager/Padding.java)\n\n* All four arguments must be distinct objects (not pointers to the same object in memory)\n* The first three arguments must equal each other (and therefore must have the same hashCode), but must not equal the fourth argument.\n* When possible/practical, use a fourth object with a different hashCode\n* When practical, it's a good idea to also find and test an unequal fourth object with the same hashCode\n* Think about the most different ways you can construct objects for the first three arguments.  The above example is a little weak in that regard because there just aren't many legal ways to construct Padding (good for Padding!).\n\n## Usage: FakeHttpServletRequest/Response\n\n```kotlin\nimport com.goalqpc.memJogLib.servlet.MjlServletHandler.handle\nimport com.nhaarman.mockitokotlin2.mock\nimport org.eclipse.jetty.server.Request\nimport org.organicdesign.testUtils.http.FakeHttpServletResponse\nimport org.organicdesign.testUtils.http.ReqB\nimport javax.servlet.http.HttpServletResponse\nimport kotlin.test.Test\nimport kotlin.test.assertEquals\nimport kotlin.test.assertNull\nimport kotlin.test.assertTrue\n\nclass ServletHandlerTest {\n\n    @Test\n    fun testFavicon() {\n        // ReqB uses a fluent interface to build FakeHttpServletRequests\n        val httpReq = ReqB().uri(\"/favicon.ico\").toReq()\n        // FakeHttpServletResponse generally needs no initialization\n        val resp = FakeHttpServletResponse()\n        // Using mokito for the request we don't need to inspect later\n        val mockBaseReq: Request = mock { }\n\n        // Send fake request and response to servlet handler\n        handle(\"\", mockBaseReq, httpReq, resp)\n\n        // After handling a request, first check for proper response code\n        assertEquals(HttpServletResponse.SC_OK, resp.status)\n        // No redirect\n        assertNull(resp.redirect)\n        // Correct content type\n        assertEquals(\"image/x-icon\", resp.contentType)\n        // Query headers\n        val headers: Collection\u003cString\u003e = resp.getHeaders(\"Cache-Control\")\n        assertTrue(headers.contains(\"public\"))\n        assertTrue(headers.contains(\"max-age=28800\"))\n\n        // Great debugging printout if you want that.\n        // println(httpReq.indentedStr(0))\n        // println(resp.indentedStr(0))\n        // println((resp.outputStream as FakeServletOutputStream)\n        //         .indentedStr(0))\n    }\n}\n```\n\nIt's easy to test the response body too:\n```kotlin\nassertTrue(resp.outputStream.toString().contains(\"@media only screen\"))\n```\n\n## Contributions\nTo build locally (in an appropriate folder), you need Java 8+, gradle, and git installed.  Then:\n```bash\ngit clone https://github.com/GlenKPeterson/TestUtils.git\ngradle clean assemble publishToMavenLocal\n```\n\n## Change Log\n\n### 2.0.3 2022-03-18 \"Removed slf4j\"\n- Also removed debugging println statements.\n\n### 2.0.2 2022-03-18 \"Bumped dependencies\"\n - Upgraded Jetty-http to 11.0.8\n\n### 2.0.1 2022-01-05 \"jakarta.servlet-api:5.0.0\"\n\n**ACTION REQUIRED:** Audit anywhere you use `fileItem.getName()` (or `.name` in Kotlin)!\nThat was the uploaded file name in apache-commons.fileupload,\nbut it's the HTML Form Input Field Name in the new servlet spec.\nFind one usage, click on it, and invoke \"Find all usages\" in your editor.\nThen replace it with `.getSubmittedFileName()`\n\n- Updated from javax.servlet api 4 to jakarta 5.\n- Now includes jetty-http in build because the file-upload processing was way too complicated to duplicate here.\n  Also cut and pasted two classes and a few methods from jetty-server.\n- No longer includes Junit in build.  Throws `AssertionError`s instead of calling JUnit.assertEqual().\n- Internally uses Junit 5.\n\n### 1.0.6 2021-12-27 \"Bumped versions\"\n- Updated dependencies\n\n### 1.0.5 2021-09-20 \"Bumped versions\"\n- Updated dependencies\n\n### 1.0.4 2021-09-17 \"Fix CVE-2020-15250\"\n- Added dependency to junit 4.13.2 because the one packaged with kotlin-test is currently vulnerable to CVE-2020-15250\n\n### 1.0.3 2021-09-14 \"bump\"\n- Bumped dependency versions.\n\n### 1.0.2 2020-12-16 \"differentMiddle\"\n - Added StringDiff.differentMiddle()\n - Made FakeHttpServletResponse.getOutputStream() return a FakeServletOutputStream\n   instead of OutputStream, so you can access .stringWriter to call .toString() on it.\n - Bumped dependency versions.\n\n### 1.0.1 2020-10-16 \"Kotlin type signatures\"\n - Converted the various contract tests (Equals, Comparator, and Comparable) to Kotlin\n in order to simplify the type signatures.\n - Renamed CompareToContract to ComparableContract\n\n***Upgrade Instructions:***\nReplace all words: `CompareToContract` with `ComparableContract`\n\n#### 1.0.0 2020-10-08 \"1.0\"\n - Added more @NotNull annotations and did a tiny bit of cleanup.\n - I've been using this for years.  It's 1.0 quality, so I'm calling it 1.0.\n\n#### 0.0.20 2020-10-07 \"CompareToContract Signature\"\n - This fixes a longstanding bug (and/or maybe a new Kotlin incompatibility)\n in the generic type signature of `CompareToContract.testCompareTo()`.\n It hasn't caused a problem (yet) in Java, but Kotlin 1.4.10 likes\n the new version *much* better.\n\n#### 0.0.19 2020-10-06\n - Bumped dependency versions\n\n#### 0.0.18 2020-08-20\n - Updated to Kotlin 1.4.0 and Dokka 1.4.0-rc which gives us real javadoc now.\n - Changed repository name to remove redundant '.testUtils'\n\n**Note:** Version 0.0.17 lacked Javadoc\n\n#### 0.0.16 2020-05-18\n - Allow any RuntimeException to be thrown by `comparator.compare(item, null)`.\n   Previously we had allowed NullPointerException and IllegalArgumentException.\n   Kotlin throws IllegalStateException.  Really any kind of RuntimeException\n   is adequate.\n - Upgraded Kotlin\n\n**Note:** Version 0.0.15 was a fluke - failed strangely in the Sonatype release process, so just re-released as .16.\n\n##### 0.0.14 2020-04-02\n - Updated all dependencies.\n - Upgraded Gradle to 6.3 and switched to .kts gradle file format.\n\n##### 0.0.13 2019-05-24\nUpdated all dependencies.\n\n#### 0.0.12 2019-04-29\nImplemented FakeHttpServletResponse.bufferSize\n\n0.0.11 2019-04-05\nAdded CookiePrinter class for showing HTTP cookies.\n\n0.0.10\n - Moved Kv out to top-level class (was inside FakeHttpServletRequest)\n - Added .indentedStr() and .toString() implementations\n - Upgraded Indented dependency.\n - Reorganized FakeHttp... classes to put accessor methods next to the fields they are related to.\n This also puts unimplemented methods at the bottom.\n - Test coverage at 85% by line\n\n0.0.9\n - Implemented more methods in FakeHttpServletResponse\n\n0.0.8\n - Renamed TestHttpServletRequest/Response to FakeHttpServlet... and put them in an HTTP sub-package.\n - Added ReqB as a FakeHttpServletRequestBuilder.\n - Made FakeHttpServletRequest.getRemoteAddr() not null and used \"0:0:0:0:0:0:0:1\" (IP-V8 localhost) as the default value.\n Implemented .locale.  Might have implemented or fixed implementations of other methods.\n - Added tests for CompareToContract and Serialization\n - Updated dependencies.\n\n0.0.7 Added HttpServletRequest mock\n\n0.0.6 Added ComparatorContract test and Serialization helper function.\n\n0.0.5 Allow compareTo(null) to throw *either* a NullPointerException or an IllegalArgumentException\n\n0.0.4 Fixed variance on CompareToContract.testCompareTo()\n\n0.0.3-SNAPSHOT Added/updated JavaDocs.\n\n## License\nCopyright 2015 Glen Peterson and PlanBase Inc.\n\nThis program and the accompanying materials are made available under the\nterms of the Apache License, Version 2.0:\nhttps://www.apache.org/licenses/LICENSE-2.0\nor the Eclipse Public License v. 2.0:\nhttps://www.eclipse.org/legal/epl-2.0\n\nSPDX-License-Identifier: Apache-2.0 OR EPL-2.0\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglenkpeterson%2Ftestutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fglenkpeterson%2Ftestutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglenkpeterson%2Ftestutils/lists"}