Skip to content

Commit f87c076

Browse files
authored
Migration Tool: PutObjectRequest File/InputStream transform (#5942)
* Refactor common code to S3TransformUtils * Transforms for PutObjectRequest, putObject(), and upload() * Address comments and add comments * Update placeholder String to variable
1 parent 6c80beb commit f87c076

File tree

27 files changed

+1127
-263
lines changed

27 files changed

+1127
-263
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.v2migrationtests;
17+
18+
import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import org.apache.commons.io.FileUtils;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.condition.EnabledIf;
26+
27+
/**
28+
* The transformed code will not compile, only testing the transform itself
29+
*/
30+
public class MavenNoCompileTest extends MavenTestBase {
31+
32+
@BeforeAll
33+
static void setUp() throws IOException {
34+
mavenBefore = new File(MavenNoCompileTest.class.getResource("maven-nocompile/before").getFile()).toPath();
35+
mavenAfter = new File(MavenNoCompileTest.class.getResource("maven-nocompile/after").getFile()).toPath();
36+
target = new File(MavenNoCompileTest.class.getResource("/").getFile()).toPath().getParent();
37+
38+
mavenActual = target.resolve("maven-nocompile/actual");
39+
mavenExpected = target.resolve("maven-nocompile/expected");
40+
41+
deleteTempDirectories();
42+
43+
FileUtils.copyDirectory(mavenBefore.toFile(), mavenActual.toFile());
44+
FileUtils.copyDirectory(mavenAfter.toFile(), mavenExpected.toFile());
45+
46+
replaceVersion(mavenExpected.resolve("pom.xml"), sdkVersion);
47+
replaceVersion(mavenActual.resolve("pom.xml"), sdkVersion);
48+
}
49+
50+
@Test
51+
@EnabledIf("versionAvailable")
52+
void mavenProject_shouldConvert() throws IOException {
53+
boolean experimental = true;
54+
verifyTransformation(experimental);
55+
}
56+
}

test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenProjectTest.java

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,19 @@
1515

1616
package software.amazon.awssdk.v2migrationtests;
1717

18-
import static java.util.Collections.addAll;
19-
import static software.amazon.awssdk.v2migrationtests.TestUtils.assertTwoDirectoriesHaveSameStructure;
20-
import static software.amazon.awssdk.v2migrationtests.TestUtils.getMigrationToolVersion;
21-
import static software.amazon.awssdk.v2migrationtests.TestUtils.getVersion;
2218
import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion;
23-
import static software.amazon.awssdk.v2migrationtests.TestUtils.run;
2419

2520
import java.io.File;
2621
import java.io.IOException;
27-
import java.nio.file.Path;
28-
import java.util.ArrayList;
29-
import java.util.List;
3022
import org.apache.commons.io.FileUtils;
3123
import org.junit.jupiter.api.BeforeAll;
3224
import org.junit.jupiter.api.Test;
3325
import org.junit.jupiter.api.condition.EnabledIf;
34-
import software.amazon.awssdk.utils.Logger;
3526

36-
public class MavenProjectTest {
37-
private static final Logger log = Logger.loggerFor(MavenProjectTest.class);
38-
private static String sdkVersion;
39-
private static Path mavenBefore;
40-
private static Path mavenAfter;
41-
private static Path target;
42-
private static Path mavenActual;
43-
private static Path mavenExpected;
27+
public class MavenProjectTest extends MavenTestBase {
4428

4529
@BeforeAll
4630
static void setUp() throws IOException {
47-
sdkVersion = getVersion();
4831
mavenBefore = new File(MavenProjectTest.class.getResource("maven/before").getFile()).toPath();
4932
mavenAfter = new File(MavenProjectTest.class.getResource("maven/after").getFile()).toPath();
5033
target = new File(MavenProjectTest.class.getResource("/").getFile()).toPath().getParent();
@@ -61,38 +44,11 @@ static void setUp() throws IOException {
6144
replaceVersion(mavenActual.resolve("pom.xml"), sdkVersion);
6245
}
6346

64-
private static void deleteTempDirectories() throws IOException {
65-
FileUtils.deleteDirectory(mavenActual.toFile());
66-
FileUtils.deleteDirectory(mavenExpected.toFile());
67-
}
68-
6947
@Test
7048
@EnabledIf("versionAvailable")
7149
void mavenProject_shouldConvert() throws IOException {
72-
verifyTransformation();
50+
boolean experimental = false;
51+
verifyTransformation(experimental);
7352
verifyCompilation();
7453
}
75-
76-
private static void verifyTransformation() throws IOException {
77-
List<String> rewriteArgs = new ArrayList<>();
78-
// pin version since updates have broken tests
79-
String rewriteMavenPluginVersion = "5.46.0";
80-
addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run",
81-
"-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW",
82-
"-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2");
83-
84-
run(mavenActual, rewriteArgs.toArray(new String[0]));
85-
FileUtils.deleteDirectory(mavenActual.resolve("target").toFile());
86-
assertTwoDirectoriesHaveSameStructure(mavenActual, mavenExpected);
87-
}
88-
89-
private static void verifyCompilation() {
90-
List<String> packageArgs = new ArrayList<>();
91-
addAll(packageArgs, "mvn", "package");
92-
run(mavenActual, packageArgs.toArray(new String[0]));
93-
}
94-
95-
boolean versionAvailable() {
96-
return TestUtils.versionAvailable(sdkVersion);
97-
}
9854
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.v2migrationtests;
17+
18+
import static java.util.Collections.addAll;
19+
import static software.amazon.awssdk.v2migrationtests.TestUtils.assertTwoDirectoriesHaveSameStructure;
20+
import static software.amazon.awssdk.v2migrationtests.TestUtils.getMigrationToolVersion;
21+
import static software.amazon.awssdk.v2migrationtests.TestUtils.getVersion;
22+
import static software.amazon.awssdk.v2migrationtests.TestUtils.run;
23+
24+
import java.io.IOException;
25+
import java.nio.file.Path;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import org.apache.commons.io.FileUtils;
29+
import org.junit.jupiter.api.BeforeAll;
30+
31+
public class MavenTestBase {
32+
33+
protected static String sdkVersion;
34+
protected static Path mavenBefore;
35+
protected static Path mavenAfter;
36+
protected static Path target;
37+
protected static Path mavenActual;
38+
protected static Path mavenExpected;
39+
40+
@BeforeAll
41+
static void init() throws IOException {
42+
sdkVersion = getVersion();
43+
}
44+
45+
protected static void deleteTempDirectories() throws IOException {
46+
FileUtils.deleteDirectory(mavenActual.toFile());
47+
FileUtils.deleteDirectory(mavenExpected.toFile());
48+
}
49+
50+
protected static void verifyTransformation(boolean experimental) throws IOException {
51+
String recipeCmd = "-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2";
52+
if (experimental) {
53+
recipeCmd += "Experimental";
54+
}
55+
56+
List<String> rewriteArgs = new ArrayList<>();
57+
// pin version since updates have broken tests
58+
String rewriteMavenPluginVersion = "5.46.0";
59+
addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run",
60+
"-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW",
61+
recipeCmd);
62+
63+
run(mavenActual, rewriteArgs.toArray(new String[0]));
64+
FileUtils.deleteDirectory(mavenActual.resolve("target").toFile());
65+
assertTwoDirectoriesHaveSameStructure(mavenActual, mavenExpected);
66+
}
67+
68+
protected static void verifyCompilation() {
69+
List<String> packageArgs = new ArrayList<>();
70+
addAll(packageArgs, "mvn", "package");
71+
run(mavenActual, packageArgs.toArray(new String[0]));
72+
}
73+
74+
boolean versionAvailable() {
75+
return TestUtils.versionAvailable(sdkVersion);
76+
}
77+
}

test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTransferManagerTest.java

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,84 +15,42 @@
1515

1616
package software.amazon.awssdk.v2migrationtests;
1717

18-
import static java.util.Collections.addAll;
19-
import static software.amazon.awssdk.v2migrationtests.TestUtils.assertTwoDirectoriesHaveSameStructure;
20-
import static software.amazon.awssdk.v2migrationtests.TestUtils.getMigrationToolVersion;
2118
import static software.amazon.awssdk.v2migrationtests.TestUtils.getVersion;
2219
import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion;
23-
import static software.amazon.awssdk.v2migrationtests.TestUtils.run;
2420

2521
import java.io.File;
2622
import java.io.IOException;
27-
import java.nio.file.Path;
28-
import java.util.ArrayList;
29-
import java.util.List;
3023
import org.apache.commons.io.FileUtils;
3124
import org.junit.jupiter.api.BeforeAll;
3225
import org.junit.jupiter.api.Test;
3326
import org.junit.jupiter.api.condition.EnabledIf;
34-
import software.amazon.awssdk.utils.Logger;
3527

36-
public class MavenTransferManagerTest {
37-
private static final Logger log = Logger.loggerFor(MavenTransferManagerTest.class);
38-
private static String sdkVersion;
39-
private static Path mavenTmBefore;
40-
private static Path mavenTmAfter;
41-
private static Path target;
42-
private static Path mavenTmActual;
43-
private static Path mavenTmExpected;
28+
public class MavenTransferManagerTest extends MavenTestBase {
4429

4530
@BeforeAll
4631
static void setUp() throws IOException {
4732
sdkVersion = getVersion();
48-
mavenTmBefore = new File(MavenTransferManagerTest.class.getResource("maven-tm/before").getFile()).toPath();
49-
mavenTmAfter = new File(MavenTransferManagerTest.class.getResource("maven-tm/after").getFile()).toPath();
33+
mavenBefore = new File(MavenTransferManagerTest.class.getResource("maven-tm/before").getFile()).toPath();
34+
mavenAfter = new File(MavenTransferManagerTest.class.getResource("maven-tm/after").getFile()).toPath();
5035
target = new File(MavenTransferManagerTest.class.getResource("/").getFile()).toPath().getParent();
5136

52-
mavenTmActual = target.resolve("maven-tm/actual");
53-
mavenTmExpected = target.resolve("maven-tm/expected");
37+
mavenActual = target.resolve("maven-tm/actual");
38+
mavenExpected = target.resolve("maven-tm/expected");
5439

5540
deleteTempDirectories();
5641

57-
FileUtils.copyDirectory(mavenTmBefore.toFile(), mavenTmActual.toFile());
58-
FileUtils.copyDirectory(mavenTmAfter.toFile(), mavenTmExpected.toFile());
42+
FileUtils.copyDirectory(mavenBefore.toFile(), mavenActual.toFile());
43+
FileUtils.copyDirectory(mavenAfter.toFile(), mavenExpected.toFile());
5944

60-
replaceVersion(mavenTmExpected.resolve("pom.xml"), sdkVersion);
61-
replaceVersion(mavenTmActual.resolve("pom.xml"), sdkVersion);
62-
}
63-
64-
private static void deleteTempDirectories() throws IOException {
65-
FileUtils.deleteDirectory(mavenTmActual.toFile());
66-
FileUtils.deleteDirectory(mavenTmExpected.toFile());
45+
replaceVersion(mavenExpected.resolve("pom.xml"), sdkVersion);
46+
replaceVersion(mavenActual.resolve("pom.xml"), sdkVersion);
6747
}
6848

6949
@Test
7050
@EnabledIf("versionAvailable")
7151
void mavenProject_shouldConvert() throws IOException {
72-
verifyTransformation();
52+
boolean experimental = true;
53+
verifyTransformation(experimental);
7354
verifyCompilation();
7455
}
75-
76-
private static void verifyTransformation() throws IOException {
77-
List<String> rewriteArgs = new ArrayList<>();
78-
// pin version since updates have broken tests
79-
String rewriteMavenPluginVersion = "5.46.0";
80-
addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run",
81-
"-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW",
82-
"-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2Experimental");
83-
84-
run(mavenTmActual, rewriteArgs.toArray(new String[0]));
85-
FileUtils.deleteDirectory(mavenTmActual.resolve("target").toFile());
86-
assertTwoDirectoriesHaveSameStructure(mavenTmActual, mavenTmExpected);
87-
}
88-
89-
private static void verifyCompilation() {
90-
List<String> packageArgs = new ArrayList<>();
91-
addAll(packageArgs, "mvn", "package");
92-
run(mavenTmActual, packageArgs.toArray(new String[0]));
93-
}
94-
95-
boolean versionAvailable() {
96-
return TestUtils.versionAvailable(sdkVersion);
97-
}
9856
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<groupId>software.amazon.awssdk</groupId>
23+
<artifactId>sample-application</artifactId>
24+
<version>1.2.3</version>
25+
26+
<properties>
27+
<maven.compiler.source>1.8</maven.compiler.source>
28+
<maven.compiler.target>1.8</maven.compiler.target>
29+
</properties>
30+
31+
<dependencyManagement>
32+
<dependencies>
33+
<dependency>
34+
<groupId>software.amazon.awssdk</groupId>
35+
<artifactId>bom</artifactId>
36+
<version>V2_VERSION</version>
37+
<type>pom</type>
38+
<scope>import</scope>
39+
</dependency>
40+
</dependencies>
41+
</dependencyManagement>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>software.amazon.awssdk</groupId>
46+
<artifactId>s3</artifactId>
47+
<version>V2_VERSION</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>software.amazon.awssdk</groupId>
51+
<artifactId>s3-transfer-manager</artifactId>
52+
<version>V2_VERSION</version>
53+
</dependency>
54+
</dependencies>
55+
</project>

0 commit comments

Comments
 (0)