Skip to content

Commit 9d36399

Browse files
authored
Cleanup some Java TypeSpec tests (#9132)
Adds validation test for `module-info.java` formatting and cleans up partial update tests.
1 parent 3a1ac82 commit 9d36399

File tree

7 files changed

+270
-428
lines changed

7 files changed

+270
-428
lines changed

packages/http-client-java/generator/http-client-generator-core/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,36 @@
2020
<spotless.config.path>../</spotless.config.path>
2121
</properties>
2222

23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-resources-plugin</artifactId>
28+
<version>3.3.1</version>
29+
<executions>
30+
<execution>
31+
<id>copy-eclipse-format-settings</id>
32+
<phase>generate-test-resources</phase>
33+
<goals>
34+
<goal>copy-resources</goal>
35+
</goals>
36+
<configuration>
37+
<outputDirectory>target/test-classes</outputDirectory>
38+
<resources>
39+
<resource>
40+
<directory>../</directory>
41+
<includes>
42+
<include>eclipse-format-azure-sdk-for-java.xml</include>
43+
</includes>
44+
</resource>
45+
</resources>
46+
</configuration>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
2353
<dependencies>
2454
<dependency>
2555
<groupId>io.clientcore</groupId>
@@ -171,5 +201,11 @@
171201
<version>5.13.4</version>
172202
<scope>test</scope>
173203
</dependency>
204+
<dependency>
205+
<groupId>org.junit.jupiter</groupId>
206+
<artifactId>junit-jupiter-params</artifactId>
207+
<version>5.13.4</version>
208+
<scope>test</scope>
209+
</dependency>
174210
</dependencies>
175211
</project>

packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/JavagenUnitTests.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java

Lines changed: 177 additions & 402 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.microsoft.typespec.http.client.generator.core.postprocessor.implementation;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.ValueSource;
12+
13+
public class CodeFormatterUtilTests {
14+
@ParameterizedTest
15+
@ValueSource(strings = { "module-info.java", "src/main/module-info.java" })
16+
public void moduleInfoFormatting(String fileName) {
17+
String initial = String.join("\n", "// Copyright (c) Microsoft Corporation. All rights reserved.",
18+
"// Licensed under the MIT License.", "// Code generated by Microsoft (R) TypeSpec Code Generator.", "",
19+
"module com.azure.resourcemanager.avs {", "requires transitive com.azure.core.management;",
20+
"exports com.azure.resourcemanager.avs;", "exports com.azure.resourcemanager.avs.fluent;",
21+
"exports com.azure.resourcemanager.avs.fluent.models;", "exports com.azure.resourcemanager.avs.models;",
22+
"opens com.azure.resourcemanager.avs.fluent.models", "to com.azure.core;",
23+
"opens com.azure.resourcemanager.avs.models", "to com.azure.core;",
24+
"opens com.azure.resourcemanager.avs.implementation.models to com.azure.core;", "}");
25+
String expected = String.join("\n", "// Copyright (c) Microsoft Corporation. All rights reserved.",
26+
"// Licensed under the MIT License.", "// Code generated by Microsoft (R) TypeSpec Code Generator.", "",
27+
"module com.azure.resourcemanager.avs {", " requires transitive com.azure.core.management;", "",
28+
" exports com.azure.resourcemanager.avs;", " exports com.azure.resourcemanager.avs.fluent;",
29+
" exports com.azure.resourcemanager.avs.fluent.models;",
30+
" exports com.azure.resourcemanager.avs.models;", "",
31+
" opens com.azure.resourcemanager.avs.fluent.models to com.azure.core;",
32+
" opens com.azure.resourcemanager.avs.models to com.azure.core;",
33+
" opens com.azure.resourcemanager.avs.implementation.models to com.azure.core;", "}", "");
34+
35+
List<String> formattingResult = CodeFormatterUtil.formatCode(new HashMap<>(Map.of(fileName, initial)));
36+
37+
assertEquals(1, formattingResult.size());
38+
assertEquals(expected, formattingResult.get(0));
39+
}
40+
}

packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtilTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public void testJsonFromEnumType() {
2020
.build();
2121

2222
Object jsonObject = ModelTestCaseUtil.jsonFromType(0, type);
23-
Assertions.assertTrue(jsonObject instanceof String);
23+
Assertions.assertInstanceOf(String.class, jsonObject);
2424
Assertions.assertTrue(Objects.equals("200", jsonObject) || Objects.equals("404", jsonObject));
2525

2626
type = new EnumType.Builder().elementType(ClassType.INTEGER)
2727
.values(Arrays.asList(new ClientEnumValue("200", "200"), new ClientEnumValue("404", "404")))
2828
.build();
2929

3030
jsonObject = ModelTestCaseUtil.jsonFromType(0, type);
31-
Assertions.assertTrue(jsonObject instanceof Integer);
31+
Assertions.assertInstanceOf(Integer.class, jsonObject);
3232
Assertions.assertTrue(Objects.equals(200, jsonObject) || Objects.equals(404, jsonObject));
3333
}
3434
}

packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/SchemaUtilTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ public void testObjectSchemaFindParent() {
6666
Assertions.assertEquals(PET, SchemaUtil.getLowestCommonParent(List.of(CAT, DOG, CORGI)));
6767
ObjectSchema dummy = new ObjectSchema();
6868
dummy.set$key("dummy");
69-
Assertions.assertTrue(SchemaUtil.getLowestCommonParent(List.of(dummy, DOG)) instanceof AnySchema);
69+
Assertions.assertInstanceOf(AnySchema.class, SchemaUtil.getLowestCommonParent(List.of(dummy, DOG)));
7070
}
7171

7272
@Test
7373
public void testAllSchemaFindParent() {
74-
Assertions.assertTrue(SchemaUtil.getLowestCommonParent(List.of(new ArraySchema(), PET)) instanceof AnySchema);
75-
Assertions
76-
.assertTrue(SchemaUtil.getLowestCommonParent(List.of(new DictionarySchema(), PET)) instanceof AnySchema);
74+
Assertions.assertInstanceOf(AnySchema.class, SchemaUtil.getLowestCommonParent(List.of(new ArraySchema(), PET)));
75+
Assertions.assertInstanceOf(AnySchema.class,
76+
SchemaUtil.getLowestCommonParent(List.of(new DictionarySchema(), PET)));
7777
StringSchema stringSchema = new StringSchema();
78-
Assertions.assertTrue(SchemaUtil.getLowestCommonParent(List.of(stringSchema)) instanceof StringSchema);
79-
Assertions
80-
.assertTrue(SchemaUtil.getLowestCommonParent(List.of(stringSchema, stringSchema)) instanceof StringSchema);
78+
Assertions.assertInstanceOf(StringSchema.class, SchemaUtil.getLowestCommonParent(List.of(stringSchema)));
79+
Assertions.assertInstanceOf(StringSchema.class,
80+
SchemaUtil.getLowestCommonParent(List.of(stringSchema, stringSchema)));
8181
}
8282
}

packages/http-client-java/generator/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@
7777
</systemPropertyVariables>
7878
<forkCount>1</forkCount>
7979
<testFailureIgnore>false</testFailureIgnore>
80+
<argLine>
81+
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
82+
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
83+
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
84+
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
85+
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
86+
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
87+
</argLine>
8088
</configuration>
8189
</plugin>
8290

0 commit comments

Comments
 (0)