Skip to content

Add inline test demo #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Simple Inline Test Demo

## TL;DR

```bash
bash run.sh junit
# OR
bash run.sh assert
```

## Demo Structure

This demo is made of the following files and directories:

* `amazon-sqs-java-extended-client-lib`: The target project to conduct the demo.
* `inlinetest-1.0.jar`: Jar for ITest -- the Java inline test framework.
* `junit-platform-console-standalone-1.12.0.jar`: JUnit standalone jar to execute JUnit inline tests with.
* `run.sh`: A `bash` script to demonstrate the process of running an inline test.
* `inline-tests`: This generated directory contains generated sources and bytecode of the inline test.
* `clean.sh`: A `bash` script to clean files generated during the demo.
* `diff.txt`: Shows the changes made to the project under demonstration. Contains lines for adding inline test dependency to `pom.xml`, adding imports, and adding the inline test itself.
* `out.txt`: Generated output of running the demo.

## Output of the script

These are some notable files that will be generated as a result of running the script, represented in relative path from the `demo` directory:

* `amazon-sqs-java-extended-client-lib/deps.txt`: A colon-separated list of dependencies for the project. This file is used during parsing, compiling and executing the inline test.
* `inline-tests/src/AmazonSQSExtendedClient_0Test.java`: The test class that is obtained by parsing an inline test.
* `inline-tests/bin/com/amazon/sqs/javamessaging/AmazonSQSExtendedClient_0Test.class`: Generated bytecode from the parsed inline test class.
* `out.txt`: Generated output of running the demo.

## Target Statement

Inline tests are designed to provide lightweight oracles for various developer-chosen inputs and outputs for complex single statements. As of now, the ITest framework majorly supports inline tests for 4 kinds of target statements:

* Bit manipulation
* String manipulation
* Regular expressions
* Java Stream

In this example, on line 887-888 of the source file `amazon-sqs-java-extended-client-lib/src/main/java/com/amazon/sqs/javamessaging/AmazonSQSExtendedClient.java`, there is a string manipulation target statement:

```java
// SQSExtendedClientConstants.S3_KEY_MARKER is "-..s3Key..-"
int secondOccurence = receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER,
receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER) + 1);
```

There are more complicated examples in the wild, but this can be a good case for demonstration.

## Inline Test

An inline test can be added below this statement:

```java
itest().given(receiptHandle, "1FOc=-..s3Key..-y*[email protected]").checkEq(secondOccurence, 21);
```

It checks that if the variables on the right hand side of the assignment are given the values specified in the `given` clause, the value on the left hand side (`secondOccurence`) is expected to be `21`. The developer can specify multiple such inline tests below a target statement to test for different cases.

Before adding the inline test, the method containing the target statement looks like this:

```java
private String getOrigReceiptHandle(String receiptHandle) {
int secondOccurence = receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER,
receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER) + 1);
return receiptHandle.substring(secondOccurence + SQSExtendedClientConstants.S3_KEY_MARKER.length());
}
```

After adding the inline test, the enclosing method of the target statement looks like this:

```java
private String getOrigReceiptHandle(String receiptHandle) {
int secondOccurence = receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER,
receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER) + 1);
itest().given(receiptHandle, "1FOc=-..s3Key..-y*[email protected]").checkEq(secondOccurence, 21);
return receiptHandle.substring(secondOccurence + SQSExtendedClientConstants.S3_KEY_MARKER.length());
}
```

In this example, the developer assigns the index of the second occurrence of `"-..s3Key..-"` in `receiptHandle` to the variable `secondOccurence`. Therefore, the inline test constructed for this example can provide different values for `receiptHandle` using the `given` clause, and check their corresponding values of `secondOccurence` with the `checkEq` clause.

## Parsing Inline Test

Parsing an inline test is defined as the process of using the ITest tool to turn a (target statement, inline test) pair, in this case these two lines:

```java
int secondOccurence = receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER,
receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER) + 1);
itest().given(receiptHandle, "1FOc=-..s3Key..-y*[email protected]").checkEq(secondOccurence, 21);
```

to either:

1. a class with a main method and uses Java's default asserts
2. or a JUnit class that uses JUnit's asserts

This process is demonstrated in `run.sh`'s `parse_inline_test` function.

### Assertion Style

In the process of parsing an inline test, one can choose to parse inline into either a simple Java class, or a JUnit Java class.

### Content of the generated JUnit class

```java
package com.amazon.sqs.javamessaging;

// Other imports omitted for space
import org.inlinetest.ITest;
import static org.inlinetest.ITest.itest;
import static org.inlinetest.ITest.group;

public class AmazonSQSExtendedClient_0Test {

@Test
void testLine889() throws Exception {
String receiptHandle = "1FOc=-..s3Key..-y*[email protected]";
int secondOccurence = receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER, receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER) + 1);
assertEquals(21, secondOccurence);
}
}
```

### Content of the generated assertion class

```java
package com.amazon.sqs.javamessaging;

// Other imports omitted for space
import org.inlinetest.ITest;
import static org.inlinetest.ITest.itest;
import static org.inlinetest.ITest.group;

public class AmazonSQSExtendedClient_0Test {

static void testLine889() {
String receiptHandle = "1FOc=-..s3Key..-y*[email protected]";
int secondOccurence = receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER, receiptHandle.indexOf(SQSExtendedClientConstants.S3_KEY_MARKER) + 1);
assert Objects.equals(secondOccurence, 21);
}

public static void main(String[] args) {
testLine889();
System.out.println("inline tests passed: 1");
}
}
```

## Compiling Inline Test

This is just using Java compiler to compile the parsed class. Details in `run.sh`'s `compile_inline_test` function.

## Executing Inline Test

Depending on the assertion style parameter, one can either directly use `java` to execute the inline test class, or use JUnit standalone jar to execute the JUnit inline test class. Details in `run.sh`'s `execute_inline_test` function.

51 changes: 51 additions & 0 deletions demo/amazon-sqs-java-extended-client-lib/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Apache License
Version 2.0, January 2004

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:

1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
2. You must cause any modified files to carry prominent notices stating that You changed the files; and
3. 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
4. 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
46 changes: 46 additions & 0 deletions demo/amazon-sqs-java-extended-client-lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Amazon SQS Extended Client Library for Java
===========================================
The **Amazon SQS Extended Client Library for Java** enables you to manage Amazon SQS message payloads with Amazon S3. This is especially useful for storing and retrieving messages with a message payload size greater than the current SQS limit of 256 KB, up to a maximum of 2 GB. Specifically, you can use this library to:

* Specify whether message payloads are always stored in Amazon S3 or only when a message's size exceeds 256 KB.

* Send a message that references a single message object stored in an Amazon S3 bucket.

* Get the corresponding message object from an Amazon S3 bucket.

* Delete the corresponding message object from an Amazon S3 bucket.

You can download release builds through the [releases section of this](https://github.com/awslabs/amazon-sqs-java-extended-client-lib) project.

For more information on using the amazon-sqs-java-extended-client-lib, see our getting started guide [here](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/s3-messages.html).

## Getting Started

* **Sign up for AWS** -- Before you begin, you need an AWS account. For more information about creating an AWS account and retrieving your AWS credentials, see [AWS Account and Credentials](http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-setup.html) in the AWS SDK for Java Developer Guide.
* **Sign up for Amazon SQS** -- Go to the Amazon [SQS console](https://console.aws.amazon.com/sqs/home?region=us-east-1) to sign up for the service.
* **Minimum requirements** -- To use the sample application, you'll need Java 7 (or later) and [Maven 3](http://maven.apache.org/). For more information about the requirements, see the [Getting Started](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/s3-messages.html) section of the Amazon SQS Developer Guide.
* **Download** -- Download the [latest preview release](https://github.com/awslabs/amazon-sqs-java-extended-client-lib/releases) or pick it up from Maven:
### Version 2.x
```xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-extended-client-lib</artifactId>
<version>2.0.2</version>
<type>jar</type>
</dependency>
```

### Version 1.x
```xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-extended-client-lib</artifactId>
<version>1.2.2</version>
<type>jar</type>
</dependency>
```
* **Further information** - Read the [API documentation](http://aws.amazon.com/documentation/sqs/).

## Feedback
* Give us feedback [here](https://github.com/awslabs/amazon-sqs-java-extended-client-lib/issues).
* If you'd like to contribute a new feature or bug fix, we'd love to see Github pull requests from you.
20 changes: 20 additions & 0 deletions demo/amazon-sqs-java-extended-client-lib/VERSIONING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Versioning Policy

We use a three-part X.Y.Z (Major.Minor.Patch) versioning definition, as follows:
* X (Major) version changes are significant and expected to break backwards compatibility.
* Y (Minor) version changes are moderate changes. These include:
* Significant non-breaking feature additions.
* Any change to the version of a dependency.
* Possible backwards-incompatible changes. These changes will be noted and explained in detail in the release notes.
* Z (Patch) version changes are small changes. These changes will not break backwards compatibility.
* Z releases will also include warning of upcoming breaking changes, whenever possible.

## What this means for you

We recommend running the most recent version. Here are our suggestions for managing updates:

* X changes will require some effort to incorporate.
* Y changes will not require significant effort to incorporate.
* If you have good unit and integration tests, these changes are generally safe to pick up automatically.
* Z changes will not require any changes to your code. Z changes are intended to be picked up automatically.
* Good unit and integration tests are always recommended.
Loading