From af4bace85bfc10aa0fd0786defc2b06876bb53e0 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Wed, 3 Dec 2025 17:03:05 -0500 Subject: [PATCH 1/4] Cleanup some Java TypeSpec tests --- .../eclipse-format-azure-sdk-for-java.xml | 401 ++++++++++++++++++ .../core/mapper/JavagenUnitTests.java | 17 - .../util/PartialUpdateHandlerTest.java | 327 +++++--------- .../CodeFormatterUtilTests.java | 39 ++ .../core/util/ModelTestCaseUtilTests.java | 4 +- .../generator/core/util/SchemaUtilTests.java | 14 +- packages/http-client-java/generator/pom.xml | 8 + 7 files changed, 569 insertions(+), 241 deletions(-) create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml delete mode 100644 packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/JavagenUnitTests.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml b/packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml new file mode 100644 index 00000000000..8e5fa985b2a --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml @@ -0,0 +1,401 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/JavagenUnitTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/JavagenUnitTests.java deleted file mode 100644 index 836dd0597de..00000000000 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/mapper/JavagenUnitTests.java +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.microsoft.typespec.http.client.generator.core.mapper; - -/** - * unit test entry - */ -public class JavagenUnitTests { - - /** - * Test the correctness of generated returnType javadoc. - * It consists of two dimensions: - * 1. whether description exists already on operation or schema itself - * 2. when baseType and returnType varies - */ -} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java index 36e46def2c5..519993fa1df 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java @@ -10,18 +10,21 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.github.javaparser.TokenRange; import com.github.javaparser.ast.AccessSpecifier; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.body.ConstructorDeclaration; import com.github.javaparser.ast.body.InitializerDeclaration; +import com.github.javaparser.ast.modules.ModuleDeclaration; import com.github.javaparser.ast.stmt.BlockStmt; import java.io.IOException; import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -30,13 +33,8 @@ public class PartialUpdateHandlerTest { @Test public void testClassOrInterfaceFileToTestAddMemberToExistingFile() throws IOException, URISyntaxException { - String existingFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithAddedMemberClient.java") - .toURI())), StandardCharsets.UTF_8); - String generatedFileContent = new String( - Files.readAllBytes(Paths.get( - getClass().getClassLoader().getResource("partialupdate/StringOperationGeneratedClient.java").toURI())), - StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationWithAddedMemberClient.java"); + String generatedFileContent = load("partialupdate/StringOperationGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -56,13 +54,8 @@ public void testClassOrInterfaceFileToTestAddMemberToExistingFile() throws IOExc @Test public void testClassOrInterfaceFileToTestUpdateMethodSignatureToExistingFile() throws URISyntaxException, IOException { - String existingFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithUpdateMemberClient.java") - .toURI())), StandardCharsets.UTF_8); - String generatedFileContent = new String( - Files.readAllBytes(Paths.get( - getClass().getClassLoader().getResource("partialupdate/StringOperationGeneratedClient.java").toURI())), - StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationWithUpdateMemberClient.java"); + String generatedFileContent = load("partialupdate/StringOperationGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -86,13 +79,8 @@ public void testClassOrInterfaceFileToTestUpdateMethodSignatureToExistingFile() @Test public void testClassOrInterfaceFileToTestRemoveMethodToExistingFile() throws URISyntaxException, IOException { - String existingFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithRemovedMemberGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); - String generatedFileContent = new String( - Files.readAllBytes(Paths.get( - getClass().getClassLoader().getResource("partialupdate/StringOperationGeneratedClient.java").toURI())), - StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationWithRemovedMemberGeneratedClient.java"); + String generatedFileContent = load("partialupdate/StringOperationGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -105,13 +93,8 @@ public void testClassOrInterfaceFileToTestRemoveMethodToExistingFile() throws UR @Test public void testClassOrInterfaceFileToTestSawaggerAddAPI() throws URISyntaxException, IOException { - String existingFileContent = new String( - Files.readAllBytes(Paths.get( - getClass().getClassLoader().getResource("partialupdate/StringOperationGeneratedClient.java").toURI())), - StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithAddedMemberGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationGeneratedClient.java"); + String generatedFileContent = load("partialupdate/StringOperationWithAddedMemberGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -123,13 +106,8 @@ public void testClassOrInterfaceFileToTestSawaggerAddAPI() throws URISyntaxExcep @Test public void testClassOrInterfaceFileToTestGeneratedFileRemoveAPI() throws URISyntaxException, IOException { - String existingFileContent = new String( - Files.readAllBytes(Paths.get( - getClass().getClassLoader().getResource("partialupdate/StringOperationGeneratedClient.java").toURI())), - StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithRemovedMemberGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationGeneratedClient.java"); + String generatedFileContent = load("partialupdate/StringOperationWithRemovedMemberGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -140,13 +118,8 @@ public void testClassOrInterfaceFileToTestGeneratedFileRemoveAPI() throws URISyn @Test public void testClassOrInterfaceFileToTestGeneratedFileUpdateAPI() throws URISyntaxException, IOException { - String existingFileContent = new String( - Files.readAllBytes(Paths.get( - getClass().getClassLoader().getResource("partialupdate/StringOperationGeneratedClient.java").toURI())), - StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithUpdateMemberGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationGeneratedClient.java"); + String generatedFileContent = load("partialupdate/StringOperationWithUpdateMemberGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -167,12 +140,8 @@ public void testClassOrInterfaceFileToTestGeneratedFileUpdateAPI() throws URISyn @Test public void testClassOrInterfaceFileToTestGeneratedFileUpdateAPIAndExistingFileUpdateMethod() throws URISyntaxException, IOException { - String existingFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithUpdateMemberClient.java") - .toURI())), StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithUpdateMemberGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationWithUpdateMemberClient.java"); + String generatedFileContent = load("partialupdate/StringOperationWithUpdateMemberGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -195,12 +164,8 @@ public void testClassOrInterfaceFileToTestGeneratedFileUpdateAPIAndExistingFileU @Test public void testClassOrInterfaceFileToTestGeneratedFileRemoveAPIAndExistingFileUpdateMethod() throws URISyntaxException, IOException { - String existingFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithUpdateMemberClient.java") - .toURI())), StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithRemovedMemberGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationWithUpdateMemberClient.java"); + String generatedFileContent = load("partialupdate/StringOperationWithRemovedMemberGeneratedClient.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -222,13 +187,8 @@ public void testClassOrInterfaceFileToTestGeneratedFileRemoveAPIAndExistingFileU public void testClassOrInterfaceFileToTestWhenGeneratedFileHasSameNameButDifferentSignatureWithExistingGeneratedMethodToTestThenShouldIncludeThisSameNameMethod() throws URISyntaxException, IOException { - String existingFileContent = new String( - Files.readAllBytes(Paths - .get(getClass().getClassLoader().getResource("partialupdate/PagedGeneratedAsyncClient.java").toURI())), - StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/PagedGeneratedAsyncClientWithConvenienceMethod.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/PagedGeneratedAsyncClient.java"); + String generatedFileContent = load("partialupdate/PagedGeneratedAsyncClientWithConvenienceMethod.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -245,12 +205,8 @@ public void testClassOrInterfaceFileToTestGeneratedFileRemoveAPIAndExistingFileU @Test public void testClassOrInterfaceFileToTestWhenNoChangesAreMadeOnNextGenerationToTestThenTheFileShouldStaySame() throws URISyntaxException, IOException { - String existingFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/PagedGeneratedAsyncClientWithConvenienceMethod.java") - .toURI())), StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/PagedGeneratedAsyncClientWithConvenienceMethod.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/PagedGeneratedAsyncClientWithConvenienceMethod.java"); + String generatedFileContent = load("partialupdate/PagedGeneratedAsyncClientWithConvenienceMethod.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -267,12 +223,8 @@ public void testClassOrInterfaceFileToTestWhenNoChangesAreMadeOnNextGenerationTo @Test public void testClassOrInterfaceFileToTestVerifyGeneratedFileShouldNotContainDuplicateMethods() throws URISyntaxException, IOException { - String existingFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithDuplicateMethodGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); - String generatedFileContent = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader() - .getResource("partialupdate/StringOperationWithDuplicateMethodGeneratedClient.java") - .toURI())), StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/StringOperationWithDuplicateMethodGeneratedClient.java"); + String generatedFileContent = load("partialupdate/StringOperationWithDuplicateMethodGeneratedClient.java"); Exception exception = assertThrows(RuntimeException.class, () -> { PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -287,13 +239,8 @@ public void testClassOrInterfaceFileToTestVerifyGeneratedFileShouldNotContainDup @Test public void testClassOrInterfaceFileToTestModelCustomization() throws URISyntaxException, IOException { - String existingFileContent = new String( - Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("partialupdate/Model.java").toURI())), - StandardCharsets.UTF_8); - String generatedFileContent = new String( - Files.readAllBytes( - Paths.get(getClass().getClassLoader().getResource("partialupdate/GeneratedModel.java").toURI())), - StandardCharsets.UTF_8); + String existingFileContent = load("partialupdate/Model.java"); + String generatedFileContent = load("partialupdate/GeneratedModel.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -308,10 +255,10 @@ public void testClassOrInterfaceFileToTestModelCustomization() throws URISyntaxE @Test public void testModuleInfoFileToTestWhenGeneratedFileEqualsExistingFileToTestThenUseGeneratedFile() { - String existingFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" - + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n" - + "\n" + "module com.azure.iot.deviceupdate {\n" + "\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.iot.deviceupdate;\n" + "}\n"; + String existingFileContent = String.join("\n", "// Copyright (c) Microsoft Corporation. All rights reserved.", + "// Licensed under the MIT License.", "// Code generated by Microsoft (R) AutoRest Code Generator.", "", + "module com.azure.iot.deviceupdate {", "", " requires transitive com.azure.core;", "", + " exports com.azure.iot.deviceupdate;", "}", ""); String generatedFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n" + "\n" + "module com.azure.iot.deviceupdate {\n" + "\n" + " requires transitive com.azure.core;\n" + "\n" @@ -320,7 +267,7 @@ public void testModuleInfoFileToTestWhenGeneratedFileEqualsExistingFileToTestThe String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); - assertEquals(true, compilationUnit.getModule().isPresent()); + assertTrue(compilationUnit.getModule().isPresent()); assertEquals("com.azure.iot.deviceupdate", compilationUnit.getModule().get().getName().toString()); assertEquals(2, compilationUnit.getModule().get().getDirectives().size()); } @@ -342,7 +289,7 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist CompilationUnit compilationUnit = parse(output); assertNotNull(compilationUnit.getOrphanComments()); assertEquals(3, compilationUnit.getOrphanComments().size()); - assertEquals(true, compilationUnit.getModule().isPresent()); + assertTrue(compilationUnit.getModule().isPresent()); assertEquals("com.azure.messaging.webpubsubnew", compilationUnit.getModule().get().getName().toString()); assertEquals(3, compilationUnit.getModule().get().getDirectives().size()); } @@ -362,37 +309,27 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); - assertEquals(true, compilationUnit.getModule().isPresent()); - assertEquals("com.azure.messaging.webpubsubnew", compilationUnit.getModule().get().getName().toString()); - assertEquals(3, compilationUnit.getModule().get().getDirectives().size()); + ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); + assertEquals("com.azure.messaging.webpubsubnew", module.getName().toString()); + assertEquals(3, module.getDirectives().size()); assertEquals("requires transitive com.azure.core;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(0) - .asModuleRequiresDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.messaging.webpubsubnew;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(1) - .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.messaging.webpubsubnew.models;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(2) - .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); - + .map(TokenRange::toString) + .orElseThrow()); } @Test @@ -402,8 +339,8 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" + " exports com.azure.communication.phonenumbersdemo;\n" + "}"; String generatedFileContent - = "" + "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" - + "\n" + " exports com.azure.communication.phonenumbersdemo;\n" + = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" + + " exports com.azure.communication.phonenumbersdemo;\n" + " exports com.azure.communication.phonenumbersdemo.models;\n" + "\n" + " opens com.azure.communication.phonenumbersdemo.implementation.models to\n" + " com.azure.core,\n" + " com.fasterxml.jackson.databind;\n" @@ -412,57 +349,41 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); - assertEquals(true, compilationUnit.getModule().isPresent()); - assertEquals("com.azure.communication.phonenumbersdemo", - compilationUnit.getModule().get().getName().toString()); - assertEquals(5, compilationUnit.getModule().get().getDirectives().size()); + ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); + assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); + assertEquals(5, module.getDirectives().size()); assertEquals("requires transitive com.azure.core;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(0) - .asModuleRequiresDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(1) - .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo.models;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(2) - .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals( "opens com.azure.communication.phonenumbersdemo.implementation.models to com.azure.core, com.fasterxml.jackson.databind;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(3) - .asModuleOpensDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals( "opens com.azure.communication.phonenumbersdemo.models to com.azure.core, com.fasterxml.jackson.databind;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(4) - .asModuleOpensDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); } @Test @@ -487,57 +408,45 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); - assertEquals(true, compilationUnit.getModule().isPresent()); - assertEquals("com.azure.communication.phonenumbersdemo", - compilationUnit.getModule().get().getName().toString()); - assertEquals(5, compilationUnit.getModule().get().getDirectives().size()); + ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); + assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); + assertEquals(5, module.getDirectives().size()); assertEquals("requires transitive com.azure.core;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(0) - .asModuleRequiresDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(1) .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo.models;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(2) .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals( "opens com.azure.communication.phonenumbersdemo.implementation.models to com.azure.core, com.fasterxml.jackson.databind;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(3) .asModuleOpensDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals( "opens com.azure.communication.phonenumbersdemo.models to com.azure.core, com.fasterxml.jackson.databind;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(4) .asModuleOpensDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); } @Test @@ -553,45 +462,36 @@ public void testModuleInfoFileToTestMergeExistingFileAndGeneratedFileToTestIgnor String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); - assertEquals(true, compilationUnit.getModule().isPresent()); - assertEquals("com.azure.communication.phonenumbersdemo", - compilationUnit.getModule().get().getName().toString()); - assertEquals(3, compilationUnit.getModule().get().getDirectives().size()); + ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); + assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); + assertEquals(3, module.getDirectives().size()); assertEquals("requires transitive com.azure.core;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(0) .asModuleRequiresDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(1) .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo.models;", - compilationUnit.getModule() - .get() - .getDirectives() + module.getDirectives() .get(2) .asModuleExportsDirective() .getTokenRange() - .get() - .toString()); + .map(TokenRange::toString) + .orElseThrow()); } @Test public void testStaticBlockOverride() throws Exception { - String existingFileContent = Files.readString( - Paths.get(getClass().getClassLoader().getResource("partialupdate/ModelWithStaticBlock.java").toURI())); - String generatedFileContent = Files.readString(Paths - .get(getClass().getClassLoader().getResource("partialupdate/ModelWithStaticBlockGenerated.java").toURI())); + String existingFileContent = load("partialupdate/ModelWithStaticBlock.java"); + String generatedFileContent = load("partialupdate/ModelWithStaticBlockGenerated.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -613,10 +513,8 @@ public void testStaticBlockOverride() throws Exception { @Test public void testStaticBlockAdd() throws Exception { - String existingFileContent = Files.readString( - Paths.get(getClass().getClassLoader().getResource("partialupdate/ModelWithoutStaticBlock.java").toURI())); - String generatedFileContent = Files.readString(Paths - .get(getClass().getClassLoader().getResource("partialupdate/ModelWithStaticBlockGenerated.java").toURI())); + String existingFileContent = load("partialupdate/ModelWithoutStaticBlock.java"); + String generatedFileContent = load("partialupdate/ModelWithStaticBlockGenerated.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -640,33 +538,27 @@ public void testStaticBlockAdd() throws Exception { @Test public void testStaticBlockRemove() throws Exception { - String existingFileContent = Files.readString( - Paths.get(getClass().getClassLoader().getResource("partialupdate/ModelWithStaticBlock.java").toURI())); - String generatedFileContent = Files.readString(Paths.get( - getClass().getClassLoader().getResource("partialupdate/ModelWithoutStaticBlockGenerated.java").toURI())); + String existingFileContent = load("partialupdate/ModelWithStaticBlock.java"); + String generatedFileContent = load("partialupdate/ModelWithoutStaticBlockGenerated.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); - List staticInitializerDeclaration = compilationUnit.getTypes() + // Verify no static initialization blocks exist. + assertTrue(compilationUnit.getTypes() .get(0) .getMembers() .stream() .filter(m -> m instanceof InitializerDeclaration) .map(m -> (InitializerDeclaration) m) - .filter(InitializerDeclaration::isStatic) - .collect(Collectors.toList()); - // verify 0 block - Assertions.assertEquals(0, staticInitializerDeclaration.size()); + .noneMatch(InitializerDeclaration::isStatic)); } @Test public void testCodeFormatterOff() throws Exception { - String existingFileContent = Files.readString( - Paths.get(getClass().getClassLoader().getResource("partialupdate/ModelWithCodeFormatterOff.java").toURI())); - String generatedFileContent = Files.readString( - Paths.get(getClass().getClassLoader().getResource("partialupdate/GeneratedModel.java").toURI())); + String existingFileContent = load("partialupdate/ModelWithCodeFormatterOff.java"); + String generatedFileContent = load("partialupdate/GeneratedModel.java"); String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -686,4 +578,9 @@ public void testCodeFormatterOff() throws Exception { Assertions.assertTrue(lines.contains("// @formatter:off")); Assertions.assertTrue(lines.contains("// @formatter:on")); } + + private String load(String resource) throws IOException, URISyntaxException { + URL resourceUrl = Objects.requireNonNull(getClass().getClassLoader().getResource(resource)); + return Files.readString(Paths.get(resourceUrl.toURI())); + } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java new file mode 100644 index 00000000000..eafd9c8b0b0 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.microsoft.typespec.http.client.generator.core.postprocessor.implementation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; + +public class CodeFormatterUtilTests { + @Test + public void moduleInfoFormatting() { + String initial = String.join("\n", "// Copyright (c) Microsoft Corporation. All rights reserved.", + "// Licensed under the MIT License.", "// Code generated by Microsoft (R) TypeSpec Code Generator.", "", + "module com.azure.resourcemanager.avs {", "requires transitive com.azure.core.management;", + "exports com.azure.resourcemanager.avs;", "exports com.azure.resourcemanager.avs.fluent;", + "exports com.azure.resourcemanager.avs.fluent.models;", "exports com.azure.resourcemanager.avs.models;", + "opens com.azure.resourcemanager.avs.fluent.models", "to com.azure.core;", + "opens com.azure.resourcemanager.avs.models", "to com.azure.core;", + "opens com.azure.resourcemanager.avs.implementation.models to com.azure.core;", "}"); + String expected = String.join("\n", "// Copyright (c) Microsoft Corporation. All rights reserved.", + "// Licensed under the MIT License.", "// Code generated by Microsoft (R) TypeSpec Code Generator.", "", + "module com.azure.resourcemanager.avs {", " requires transitive com.azure.core.management;", "", + " exports com.azure.resourcemanager.avs;", " exports com.azure.resourcemanager.avs.fluent;", + " exports com.azure.resourcemanager.avs.fluent.models;", + " exports com.azure.resourcemanager.avs.models;", "", + " opens com.azure.resourcemanager.avs.fluent.models to com.azure.core;", + " opens com.azure.resourcemanager.avs.models to com.azure.core;", + " opens com.azure.resourcemanager.avs.implementation.models to com.azure.core;", "}", ""); + + List formattingResult + = CodeFormatterUtil.formatCode(new HashMap<>(Map.of("module-info.java", initial))); + + assertEquals(1, formattingResult.size()); + assertEquals(expected, formattingResult.get(0)); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtilTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtilTests.java index 9ba2e415227..fe135218b64 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtilTests.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtilTests.java @@ -20,7 +20,7 @@ public void testJsonFromEnumType() { .build(); Object jsonObject = ModelTestCaseUtil.jsonFromType(0, type); - Assertions.assertTrue(jsonObject instanceof String); + Assertions.assertInstanceOf(String.class, jsonObject); Assertions.assertTrue(Objects.equals("200", jsonObject) || Objects.equals("404", jsonObject)); type = new EnumType.Builder().elementType(ClassType.INTEGER) @@ -28,7 +28,7 @@ public void testJsonFromEnumType() { .build(); jsonObject = ModelTestCaseUtil.jsonFromType(0, type); - Assertions.assertTrue(jsonObject instanceof Integer); + Assertions.assertInstanceOf(Integer.class, jsonObject); Assertions.assertTrue(Objects.equals(200, jsonObject) || Objects.equals(404, jsonObject)); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/SchemaUtilTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/SchemaUtilTests.java index e886a6c6105..f399a1e0e5d 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/SchemaUtilTests.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/util/SchemaUtilTests.java @@ -66,17 +66,17 @@ public void testObjectSchemaFindParent() { Assertions.assertEquals(PET, SchemaUtil.getLowestCommonParent(List.of(CAT, DOG, CORGI))); ObjectSchema dummy = new ObjectSchema(); dummy.set$key("dummy"); - Assertions.assertTrue(SchemaUtil.getLowestCommonParent(List.of(dummy, DOG)) instanceof AnySchema); + Assertions.assertInstanceOf(AnySchema.class, SchemaUtil.getLowestCommonParent(List.of(dummy, DOG))); } @Test public void testAllSchemaFindParent() { - Assertions.assertTrue(SchemaUtil.getLowestCommonParent(List.of(new ArraySchema(), PET)) instanceof AnySchema); - Assertions - .assertTrue(SchemaUtil.getLowestCommonParent(List.of(new DictionarySchema(), PET)) instanceof AnySchema); + Assertions.assertInstanceOf(AnySchema.class, SchemaUtil.getLowestCommonParent(List.of(new ArraySchema(), PET))); + Assertions.assertInstanceOf(AnySchema.class, + SchemaUtil.getLowestCommonParent(List.of(new DictionarySchema(), PET))); StringSchema stringSchema = new StringSchema(); - Assertions.assertTrue(SchemaUtil.getLowestCommonParent(List.of(stringSchema)) instanceof StringSchema); - Assertions - .assertTrue(SchemaUtil.getLowestCommonParent(List.of(stringSchema, stringSchema)) instanceof StringSchema); + Assertions.assertInstanceOf(StringSchema.class, SchemaUtil.getLowestCommonParent(List.of(stringSchema))); + Assertions.assertInstanceOf(StringSchema.class, + SchemaUtil.getLowestCommonParent(List.of(stringSchema, stringSchema))); } } diff --git a/packages/http-client-java/generator/pom.xml b/packages/http-client-java/generator/pom.xml index b6a21f416cf..83b81a56d05 100644 --- a/packages/http-client-java/generator/pom.xml +++ b/packages/http-client-java/generator/pom.xml @@ -77,6 +77,14 @@ 1 false + + --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED + --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED + --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED + --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED + --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED + --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED + From 0b1c94387df936ad6d3ee76a539a56324a9c4366 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Wed, 3 Dec 2025 18:02:02 -0500 Subject: [PATCH 2/4] Fix formatting --- .../util/PartialUpdateHandlerTest.java | 54 ++++--------------- 1 file changed, 9 insertions(+), 45 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java index 519993fa1df..0796db10718 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java @@ -313,23 +313,11 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist assertEquals("com.azure.messaging.webpubsubnew", module.getName().toString()); assertEquals(3, module.getDirectives().size()); assertEquals("requires transitive com.azure.core;", - module.getDirectives() - .get(0) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(0).getTokenRange().map(TokenRange::toString).orElseThrow()); assertEquals("exports com.azure.messaging.webpubsubnew;", - module.getDirectives() - .get(1) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(1).getTokenRange().map(TokenRange::toString).orElseThrow()); assertEquals("exports com.azure.messaging.webpubsubnew.models;", - module.getDirectives() - .get(2) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(2).getTokenRange().map(TokenRange::toString).orElseThrow()); } @Test @@ -353,37 +341,17 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); assertEquals(5, module.getDirectives().size()); assertEquals("requires transitive com.azure.core;", - module.getDirectives() - .get(0) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(0).getTokenRange().map(TokenRange::toString).orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo;", - module.getDirectives() - .get(1) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(1).getTokenRange().map(TokenRange::toString).orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo.models;", - module.getDirectives() - .get(2) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(2).getTokenRange().map(TokenRange::toString).orElseThrow()); assertEquals( "opens com.azure.communication.phonenumbersdemo.implementation.models to com.azure.core, com.fasterxml.jackson.databind;", - module.getDirectives() - .get(3) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(3).getTokenRange().map(TokenRange::toString).orElseThrow()); assertEquals( "opens com.azure.communication.phonenumbersdemo.models to com.azure.core, com.fasterxml.jackson.databind;", - module.getDirectives() - .get(4) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(4).getTokenRange().map(TokenRange::toString).orElseThrow()); } @Test @@ -412,11 +380,7 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); assertEquals(5, module.getDirectives().size()); assertEquals("requires transitive com.azure.core;", - module.getDirectives() - .get(0) - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + module.getDirectives().get(0).getTokenRange().map(TokenRange::toString).orElseThrow()); assertEquals("exports com.azure.communication.phonenumbersdemo;", module.getDirectives() .get(1) From 1a3d95638c6631916ad8a8b31a81695a473f09ae Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Thu, 4 Dec 2025 10:16:26 -0500 Subject: [PATCH 3/4] Add another test --- .../http-client-generator-core/pom.xml | 36 ++ .../eclipse-format-azure-sdk-for-java.xml | 401 ------------------ .../CodeFormatterUtilTests.java | 11 +- 3 files changed, 42 insertions(+), 406 deletions(-) delete mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml diff --git a/packages/http-client-java/generator/http-client-generator-core/pom.xml b/packages/http-client-java/generator/http-client-generator-core/pom.xml index 59ba660d8c0..2015b776104 100644 --- a/packages/http-client-java/generator/http-client-generator-core/pom.xml +++ b/packages/http-client-java/generator/http-client-generator-core/pom.xml @@ -20,6 +20,36 @@ ../ + + + + org.apache.maven.plugins + maven-resources-plugin + 3.3.1 + + + copy-eclipse-format-settings + generate-test-resources + + copy-resources + + + target/test-classes + + + ../ + + eclipse-format-azure-sdk-for-java.xml + + + + + + + + + + io.clientcore @@ -171,5 +201,11 @@ 5.13.4 test + + org.junit.jupiter + junit-jupiter-params + 5.13.4 + test + diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml b/packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml deleted file mode 100644 index 8e5fa985b2a..00000000000 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/resources/eclipse-format-azure-sdk-for-java.xml +++ /dev/null @@ -1,401 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java index eafd9c8b0b0..4a7fad1c5a9 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/postprocessor/implementation/CodeFormatterUtilTests.java @@ -7,11 +7,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public class CodeFormatterUtilTests { - @Test - public void moduleInfoFormatting() { + @ParameterizedTest + @ValueSource(strings = { "module-info.java", "src/main/module-info.java" }) + public void moduleInfoFormatting(String fileName) { String initial = String.join("\n", "// Copyright (c) Microsoft Corporation. All rights reserved.", "// Licensed under the MIT License.", "// Code generated by Microsoft (R) TypeSpec Code Generator.", "", "module com.azure.resourcemanager.avs {", "requires transitive com.azure.core.management;", @@ -30,8 +32,7 @@ public void moduleInfoFormatting() { " opens com.azure.resourcemanager.avs.models to com.azure.core;", " opens com.azure.resourcemanager.avs.implementation.models to com.azure.core;", "}", ""); - List formattingResult - = CodeFormatterUtil.formatCode(new HashMap<>(Map.of("module-info.java", initial))); + List formattingResult = CodeFormatterUtil.formatCode(new HashMap<>(Map.of(fileName, initial))); assertEquals(1, formattingResult.size()); assertEquals(expected, formattingResult.get(0)); From fb05de0e1c07f408b5309c71f36570e0b15ad816 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Thu, 4 Dec 2025 11:08:11 -0500 Subject: [PATCH 4/4] Additional cleanup --- .../util/PartialUpdateHandlerTest.java | 328 +++++++----------- 1 file changed, 121 insertions(+), 207 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java index 0796db10718..b8740567fe4 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/test/java/com/microsoft/typespec/http/client/generator/core/partialupdate/util/PartialUpdateHandlerTest.java @@ -13,16 +13,16 @@ import com.github.javaparser.TokenRange; import com.github.javaparser.ast.AccessSpecifier; import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.body.BodyDeclaration; import com.github.javaparser.ast.body.ConstructorDeclaration; -import com.github.javaparser.ast.body.InitializerDeclaration; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.TypeDeclaration; import com.github.javaparser.ast.modules.ModuleDeclaration; -import com.github.javaparser.ast.stmt.BlockStmt; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -61,20 +61,12 @@ public void testClassOrInterfaceFileToTestUpdateMethodSignatureToExistingFile() CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse").size()); - assertEquals(2, - compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse").get(0).getParameters().size()); - assertEquals("test", - compilationUnit.getTypes() - .get(0) - .getMethodsByName("putNullWithResponse") - .get(0) - .getParameters() - .get(1) - .getName() - .asString()); - assertEquals(AccessSpecifier.NONE, - compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse").get(0).getAccessSpecifier()); + List methods = compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse"); + assertEquals(1, methods.size()); + MethodDeclaration method = methods.get(0); + assertEquals(2, method.getParameters().size()); + assertEquals("test", method.getParameters().get(1).getNameAsString()); + assertEquals(AccessSpecifier.NONE, method.getAccessSpecifier()); } @Test @@ -126,15 +118,9 @@ public void testClassOrInterfaceFileToTestGeneratedFileUpdateAPI() throws URISyn CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); assertEquals(1, compilationUnit.getTypes().get(0).getMethods().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse").size()); - assertEquals("updateParam", - compilationUnit.getTypes() - .get(0) - .getMethodsByName("putNullWithResponse") - .get(0) - .getParameter(1) - .getName() - .asString()); + List methods = compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse"); + assertEquals(1, methods.size()); + assertEquals("updateParam", methods.get(0).getParameter(1).getNameAsString()); } @Test @@ -148,17 +134,11 @@ public void testClassOrInterfaceFileToTestGeneratedFileUpdateAPIAndExistingFileU CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); assertEquals(1, compilationUnit.getTypes().get(0).getMethods().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse").size()); - assertEquals("test", - compilationUnit.getTypes() - .get(0) - .getMethodsByName("putNullWithResponse") - .get(0) - .getParameter(1) - .getName() - .asString()); - assertEquals(AccessSpecifier.NONE, - compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse").get(0).getAccessSpecifier()); + List methods = compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse"); + assertEquals(1, methods.size()); + MethodDeclaration method = methods.get(0); + assertEquals("test", method.getParameter(1).getNameAsString()); + assertEquals(AccessSpecifier.NONE, method.getAccessSpecifier()); } @Test @@ -172,15 +152,9 @@ public void testClassOrInterfaceFileToTestGeneratedFileRemoveAPIAndExistingFileU CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); assertEquals(1, compilationUnit.getTypes().get(0).getMethods().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse").size()); - assertEquals("test", - compilationUnit.getTypes() - .get(0) - .getMethodsByName("putNullWithResponse") - .get(0) - .getParameter(1) - .getName() - .asString()); + List methods = compilationUnit.getTypes().get(0).getMethodsByName("putNullWithResponse"); + assertEquals(1, methods.size()); + assertEquals("test", methods.get(0).getParameter(1).getNameAsString()); } @Test @@ -194,12 +168,13 @@ public void testClassOrInterfaceFileToTestGeneratedFileRemoveAPIAndExistingFileU CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); - assertEquals(4, compilationUnit.getTypes().get(0).getMembers().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getConstructors().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getFields().size()); - assertEquals(2, compilationUnit.getTypes().get(0).getMethods().size()); - assertEquals(2, compilationUnit.getTypes().get(0).getMethodsByName("list").size()); + TypeDeclaration type = compilationUnit.getTypes().get(0); + assertEquals(4, type.getMembers().size()); + assertEquals(1, type.getConstructors().size()); + assertEquals(1, type.getFields().size()); + assertEquals(2, type.getMethods().size()); + assertEquals(2, type.getMethodsByName("list").size()); } @Test @@ -212,12 +187,13 @@ public void testClassOrInterfaceFileToTestWhenNoChangesAreMadeOnNextGenerationTo CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); - assertEquals(4, compilationUnit.getTypes().get(0).getMembers().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getConstructors().size()); - assertEquals(1, compilationUnit.getTypes().get(0).getFields().size()); - assertEquals(2, compilationUnit.getTypes().get(0).getMethods().size()); - assertEquals(2, compilationUnit.getTypes().get(0).getMethodsByName("list").size()); + TypeDeclaration type = compilationUnit.getTypes().get(0); + assertEquals(4, type.getMembers().size()); + assertEquals(1, type.getConstructors().size()); + assertEquals(1, type.getFields().size()); + assertEquals(2, type.getMethods().size()); + assertEquals(2, type.getMethodsByName("list").size()); } @Test @@ -226,14 +202,10 @@ public void testClassOrInterfaceFileToTestVerifyGeneratedFileShouldNotContainDup String existingFileContent = load("partialupdate/StringOperationWithDuplicateMethodGeneratedClient.java"); String generatedFileContent = load("partialupdate/StringOperationWithDuplicateMethodGeneratedClient.java"); - Exception exception = assertThrows(RuntimeException.class, () -> { - PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); - }); + Exception exception = assertThrows(RuntimeException.class, + () -> PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent)); - String expectedMessage = "Found duplicate methods in the generated file."; - String actualMessage = exception.getMessage(); - - assertTrue(actualMessage.contains(expectedMessage)); + assertTrue(exception.getMessage().contains("Found duplicate methods in the generated file.")); } @@ -255,14 +227,14 @@ public void testClassOrInterfaceFileToTestModelCustomization() throws URISyntaxE @Test public void testModuleInfoFileToTestWhenGeneratedFileEqualsExistingFileToTestThenUseGeneratedFile() { - String existingFileContent = String.join("\n", "// Copyright (c) Microsoft Corporation. All rights reserved.", - "// Licensed under the MIT License.", "// Code generated by Microsoft (R) AutoRest Code Generator.", "", - "module com.azure.iot.deviceupdate {", "", " requires transitive com.azure.core;", "", - " exports com.azure.iot.deviceupdate;", "}", ""); + String existingFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" + + "// Licensed under the MIT License.\n// Code generated by Microsoft (R) AutoRest Code Generator.\n\n" + + "module com.azure.iot.deviceupdate {\n\n requires transitive com.azure.core;\n\n" + + " exports com.azure.iot.deviceupdate;\n}\n\n"; String generatedFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" - + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n" - + "\n" + "module com.azure.iot.deviceupdate {\n" + "\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.iot.deviceupdate;\n" + "}\n"; + + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n\n" + + "module com.azure.iot.deviceupdate {\n\n requires transitive com.azure.core;\n\n" + + " exports com.azure.iot.deviceupdate;\n}\n"; String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -275,14 +247,13 @@ public void testModuleInfoFileToTestWhenGeneratedFileEqualsExistingFileToTestThe @Test public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExistingFile() { String existingFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" - + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n" - + "\n" + "\n" + "module com.azure.messaging.webpubsubnew {\n" + " requires transitive com.azure.core;\n" - + "\n" + " exports com.azure.messaging.webpubsubnew;\n" - + " exports com.azure.messaging.webpubsubnew.models;\n" + "}"; + + "// Licensed under the MIT License.\n// Code generated by Microsoft (R) AutoRest Code Generator.\n\n\n" + + "module com.azure.messaging.webpubsubnew {\n requires transitive com.azure.core;\n\n" + + " exports com.azure.messaging.webpubsubnew;\n exports com.azure.messaging.webpubsubnew.models;\n}"; String generatedFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" - + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n" - + "\n" + "\n" + "module com.azure.messaging.webpubsubnew {\n" + " requires transitive com.azure.core;\n" - + "\n" + " exports com.azure.messaging.webpubsubnew;\n" + "}"; + + "// Licensed under the MIT License.\n// Code generated by Microsoft (R) AutoRest Code Generator.\n\n\n" + + "module com.azure.messaging.webpubsubnew {\n requires transitive com.azure.core;\n\n" + + " exports com.azure.messaging.webpubsubnew;\n}"; String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); @@ -298,158 +269,105 @@ public void testModuleInfoFileToTestWhenExistingFileHasUpdatesToTestThenUseExist public void testModuleInfoFileToTestMergeExistingFileAndGeneratedFileToTestWhenExistingFileContentHasMoreDirectives() { String existingFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" - + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n" - + "module com.azure.messaging.webpubsubnew {\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.messaging.webpubsubnew;\n" - + " exports com.azure.messaging.webpubsubnew.models;\n" + "}"; + + "// Licensed under the MIT License.\n// Code generated by Microsoft (R) AutoRest Code Generator.\n" + + "module com.azure.messaging.webpubsubnew {\n requires transitive com.azure.core;\n\n" + + " exports com.azure.messaging.webpubsubnew;\n exports com.azure.messaging.webpubsubnew.models;\n}"; String generatedFileContent = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" - + "// Licensed under the MIT License.\n" + "// Code generated by Microsoft (R) AutoRest Code Generator.\n" - + "module com.azure.messaging.webpubsubnew {\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.messaging.webpubsubnew;\n" + "}"; + + "// Licensed under the MIT License.\n// Code generated by Microsoft (R) AutoRest Code Generator.\n" + + "module com.azure.messaging.webpubsubnew {\n requires transitive com.azure.core;\n\n" + + " exports com.azure.messaging.webpubsubnew;\n}"; String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); assertEquals("com.azure.messaging.webpubsubnew", module.getName().toString()); assertEquals(3, module.getDirectives().size()); - assertEquals("requires transitive com.azure.core;", - module.getDirectives().get(0).getTokenRange().map(TokenRange::toString).orElseThrow()); - assertEquals("exports com.azure.messaging.webpubsubnew;", - module.getDirectives().get(1).getTokenRange().map(TokenRange::toString).orElseThrow()); - assertEquals("exports com.azure.messaging.webpubsubnew.models;", - module.getDirectives().get(2).getTokenRange().map(TokenRange::toString).orElseThrow()); + assertEquals("requires transitive com.azure.core;", moduleDirectiveToString(module, 0)); + assertEquals("exports com.azure.messaging.webpubsubnew;", moduleDirectiveToString(module, 1)); + assertEquals("exports com.azure.messaging.webpubsubnew.models;", moduleDirectiveToString(module, 2)); } @Test public void testModuleInfoFileToTestMergeExistingFileAndGeneratedFileToTestWhenGeneratedFileContentHasMoreDirectives() { - String existingFileContent - = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.communication.phonenumbersdemo;\n" + "}"; - String generatedFileContent - = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.communication.phonenumbersdemo;\n" - + " exports com.azure.communication.phonenumbersdemo.models;\n" + "\n" - + " opens com.azure.communication.phonenumbersdemo.implementation.models to\n" - + " com.azure.core,\n" + " com.fasterxml.jackson.databind;\n" - + " opens com.azure.communication.phonenumbersdemo.models to\n" + " com.azure.core,\n" - + " com.fasterxml.jackson.databind;\n" + "}"; + String existingFileContent = "module com.azure.communication.phonenumbersdemo {\n" + + " requires transitive com.azure.core;\n\n exports com.azure.communication.phonenumbersdemo;\n}"; + String generatedFileContent = "module com.azure.communication.phonenumbersdemo {\n" + + " requires transitive com.azure.core;\n\n exports com.azure.communication.phonenumbersdemo;\n" + + " exports com.azure.communication.phonenumbersdemo.models;\n\n" + + " opens com.azure.communication.phonenumbersdemo.implementation.models to\n" + + " com.azure.core,\n com.fasterxml.jackson.databind;\n" + + " opens com.azure.communication.phonenumbersdemo.models to\n com.azure.core,\n" + + " com.fasterxml.jackson.databind;\n}"; String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); assertEquals(5, module.getDirectives().size()); - assertEquals("requires transitive com.azure.core;", - module.getDirectives().get(0).getTokenRange().map(TokenRange::toString).orElseThrow()); - assertEquals("exports com.azure.communication.phonenumbersdemo;", - module.getDirectives().get(1).getTokenRange().map(TokenRange::toString).orElseThrow()); - assertEquals("exports com.azure.communication.phonenumbersdemo.models;", - module.getDirectives().get(2).getTokenRange().map(TokenRange::toString).orElseThrow()); + assertEquals("requires transitive com.azure.core;", moduleDirectiveToString(module, 0)); + assertEquals("exports com.azure.communication.phonenumbersdemo;", moduleDirectiveToString(module, 1)); + assertEquals("exports com.azure.communication.phonenumbersdemo.models;", moduleDirectiveToString(module, 2)); assertEquals( "opens com.azure.communication.phonenumbersdemo.implementation.models to com.azure.core, com.fasterxml.jackson.databind;", - module.getDirectives().get(3).getTokenRange().map(TokenRange::toString).orElseThrow()); + moduleDirectiveToString(module, 3)); assertEquals( "opens com.azure.communication.phonenumbersdemo.models to com.azure.core, com.fasterxml.jackson.databind;", - module.getDirectives().get(4).getTokenRange().map(TokenRange::toString).orElseThrow()); + moduleDirectiveToString(module, 4)); } @Test public void testModuleInfoFileToTestMergeExistingFileAndGeneratedFileToTestWhenExistingAndGeneratedFileHasSameContent() { - String existingFileContent - = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.communication.phonenumbersdemo;\n" - + " exports com.azure.communication.phonenumbersdemo.models;\n" + "\n" - + " opens com.azure.communication.phonenumbersdemo.implementation.models to\n" - + " com.azure.core,\n" + " com.fasterxml.jackson.databind;\n" - + " opens com.azure.communication.phonenumbersdemo.models to\n" + " com.azure.core,\n" - + " com.fasterxml.jackson.databind;\n" + "}"; - String generatedFileContent - = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.communication.phonenumbersdemo;\n" - + " exports com.azure.communication.phonenumbersdemo.models;\n" + "\n" - + " opens com.azure.communication.phonenumbersdemo.implementation.models to\n" - + " com.azure.core,\n" + " com.fasterxml.jackson.databind;\n" - + " opens com.azure.communication.phonenumbersdemo.models to\n" + " com.azure.core,\n" - + " com.fasterxml.jackson.databind;\n" + "}"; + String existingFileContent = "module com.azure.communication.phonenumbersdemo {\n" + + " requires transitive com.azure.core;\n\n exports com.azure.communication.phonenumbersdemo;\n" + + " exports com.azure.communication.phonenumbersdemo.models;\n\n" + + " opens com.azure.communication.phonenumbersdemo.implementation.models to\n" + + " com.azure.core,\n com.fasterxml.jackson.databind;\n" + + " opens com.azure.communication.phonenumbersdemo.models to\n com.azure.core,\n" + + " com.fasterxml.jackson.databind;\n}"; + String generatedFileContent = "module com.azure.communication.phonenumbersdemo {\n" + + " requires transitive com.azure.core;\n\n exports com.azure.communication.phonenumbersdemo;\n" + + " exports com.azure.communication.phonenumbersdemo.models;\n\n" + + " opens com.azure.communication.phonenumbersdemo.implementation.models to\n" + + " com.azure.core,\n com.fasterxml.jackson.databind;\n" + + " opens com.azure.communication.phonenumbersdemo.models to\n com.azure.core,\n" + + " com.fasterxml.jackson.databind;\n}"; String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); assertEquals(5, module.getDirectives().size()); - assertEquals("requires transitive com.azure.core;", - module.getDirectives().get(0).getTokenRange().map(TokenRange::toString).orElseThrow()); - assertEquals("exports com.azure.communication.phonenumbersdemo;", - module.getDirectives() - .get(1) - .asModuleExportsDirective() - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); - assertEquals("exports com.azure.communication.phonenumbersdemo.models;", - module.getDirectives() - .get(2) - .asModuleExportsDirective() - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + assertEquals("requires transitive com.azure.core;", moduleDirectiveToString(module, 0)); + assertEquals("exports com.azure.communication.phonenumbersdemo;", moduleDirectiveToString(module, 1)); + assertEquals("exports com.azure.communication.phonenumbersdemo.models;", moduleDirectiveToString(module, 2)); assertEquals( "opens com.azure.communication.phonenumbersdemo.implementation.models to com.azure.core, com.fasterxml.jackson.databind;", - module.getDirectives() - .get(3) - .asModuleOpensDirective() - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + moduleDirectiveToString(module, 3)); assertEquals( "opens com.azure.communication.phonenumbersdemo.models to com.azure.core, com.fasterxml.jackson.databind;", - module.getDirectives() - .get(4) - .asModuleOpensDirective() - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + moduleDirectiveToString(module, 4)); } @Test public void testModuleInfoFileToTestMergeExistingFileAndGeneratedFileToTestIgnoreEmptyLineAndWhiteSpace() { String existingFileContent - = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" - + "\n" + " exports com.azure.communication.phonenumbersdemo; \n" - + " exports com.azure.communication.phonenumbersdemo.models;\n" + "\n" + "}"; - String generatedFileContent - = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n" + "\n" - + " exports com.azure.communication.phonenumbersdemo;\n" - + " exports com.azure.communication.phonenumbersdemo.models;\n" + "\n" + "}"; + = "module com.azure.communication.phonenumbersdemo {\n" + " requires transitive com.azure.core;\n\n\n" + + " exports com.azure.communication.phonenumbersdemo; \n" + + " exports com.azure.communication.phonenumbersdemo.models;\n\n}"; + String generatedFileContent = "module com.azure.communication.phonenumbersdemo {\n" + + " requires transitive com.azure.core;\n\n exports com.azure.communication.phonenumbersdemo;\n" + + " exports com.azure.communication.phonenumbersdemo.models;\n\n}"; String output = PartialUpdateHandler.handlePartialUpdateForFile(generatedFileContent, existingFileContent); CompilationUnit compilationUnit = parse(output); ModuleDeclaration module = compilationUnit.getModule().orElseThrow(); assertEquals("com.azure.communication.phonenumbersdemo", module.getName().toString()); assertEquals(3, module.getDirectives().size()); - assertEquals("requires transitive com.azure.core;", - module.getDirectives() - .get(0) - .asModuleRequiresDirective() - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); - assertEquals("exports com.azure.communication.phonenumbersdemo;", - module.getDirectives() - .get(1) - .asModuleExportsDirective() - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); - assertEquals("exports com.azure.communication.phonenumbersdemo.models;", - module.getDirectives() - .get(2) - .asModuleExportsDirective() - .getTokenRange() - .map(TokenRange::toString) - .orElseThrow()); + assertEquals("requires transitive com.azure.core;", moduleDirectiveToString(module, 0)); + assertEquals("exports com.azure.communication.phonenumbersdemo;", moduleDirectiveToString(module, 1)); + assertEquals("exports com.azure.communication.phonenumbersdemo.models;", moduleDirectiveToString(module, 2)); } @Test @@ -461,13 +379,11 @@ public void testStaticBlockOverride() throws Exception { CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); - List staticInitializerDeclaration = compilationUnit.getTypes() + List> staticInitializerDeclaration = compilationUnit.getTypes() .get(0) .getMembers() .stream() - .filter(m -> m instanceof InitializerDeclaration) - .map(m -> (InitializerDeclaration) m) - .filter(InitializerDeclaration::isStatic) + .filter(m -> m.isInitializerDeclaration() && m.asInitializerDeclaration().isStatic()) .collect(Collectors.toList()); // verify 1 block Assertions.assertEquals(1, staticInitializerDeclaration.size()); @@ -484,16 +400,14 @@ public void testStaticBlockAdd() throws Exception { CompilationUnit compilationUnit = parse(output); assertEquals(1, compilationUnit.getTypes().size()); - List staticInitializerDeclaration = compilationUnit.getTypes() - .get(0) - .getMembers() - .stream() - .filter(m -> m instanceof InitializerDeclaration) - .map(m -> (InitializerDeclaration) m) - .filter(InitializerDeclaration::isStatic) - .collect(Collectors.toList()); // verify 1 block - Assertions.assertEquals(1, staticInitializerDeclaration.size()); + assertEquals(1, + compilationUnit.getTypes() + .get(0) + .getMembers() + .stream() + .filter(m -> m.isInitializerDeclaration() && m.asInitializerDeclaration().isStatic()) + .count()); // also verify a few other methods is added Assertions.assertNotNull(compilationUnit.getTypes().get(0).getMethodsByName("serializeAsJsonMergePatch")); Assertions.assertNotNull(compilationUnit.getTypes().get(0).getFieldByName("jsonMergePatch")); @@ -514,9 +428,7 @@ public void testStaticBlockRemove() throws Exception { .get(0) .getMembers() .stream() - .filter(m -> m instanceof InitializerDeclaration) - .map(m -> (InitializerDeclaration) m) - .noneMatch(InitializerDeclaration::isStatic)); + .noneMatch(m -> m.isInitializerDeclaration() && m.asInitializerDeclaration().isStatic())); } @Test @@ -528,17 +440,15 @@ public void testCodeFormatterOff() throws Exception { CompilationUnit compilationUnit = parse(output); ConstructorDeclaration constructorDeclaration = compilationUnit.getTypes().get(0).getConstructors().get(1); - Assertions.assertTrue( - constructorDeclaration.getJavadoc().get().getDescription().toText().contains("")); - Assertions.assertTrue( - constructorDeclaration.getJavadoc().get().getDescription().toText().contains("")); - BlockStmt constructorCodeBlock = (BlockStmt) constructorDeclaration.getChildNodes() - .stream() - .filter(node -> node instanceof BlockStmt) - .findFirst() - .get(); + + // Check that the Javadoc retained formatter flags. + String javadoc = constructorDeclaration.getJavadoc().map(doc -> doc.getDescription().toText()).orElseThrow(); + Assertions.assertTrue(javadoc.contains("")); + Assertions.assertTrue(javadoc.contains("")); + + // Check that the constructor body retained formatter flags. List lines - = Arrays.stream(constructorCodeBlock.toString().split("\n")).map(String::trim).collect(Collectors.toList()); + = constructorDeclaration.getBody().toString().lines().map(String::trim).collect(Collectors.toList()); Assertions.assertTrue(lines.contains("// @formatter:off")); Assertions.assertTrue(lines.contains("// @formatter:on")); } @@ -547,4 +457,8 @@ private String load(String resource) throws IOException, URISyntaxException { URL resourceUrl = Objects.requireNonNull(getClass().getClassLoader().getResource(resource)); return Files.readString(Paths.get(resourceUrl.toURI())); } + + private static String moduleDirectiveToString(ModuleDeclaration module, int index) { + return module.getDirectives().get(index).getTokenRange().map(TokenRange::toString).orElseThrow(); + } }