|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * Copyright 2019-2022 the original author or authors. |
| 6 | + * * * * |
| 7 | + * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * * * * you may not use this file except in compliance with the License. |
| 9 | + * * * * You may obtain a copy of the License at |
| 10 | + * * * * |
| 11 | + * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * * * * |
| 13 | + * * * * Unless required by applicable law or agreed to in writing, software |
| 14 | + * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * * * * See the License for the specific language governing permissions and |
| 17 | + * * * * limitations under the License. |
| 18 | + * * * |
| 19 | + * * |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +package org.springdoc.core; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Nested; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.condition.EnabledForJreRange; |
| 28 | +import org.junit.jupiter.api.condition.JRE; |
| 29 | +import org.junit.jupiter.api.io.TempDir; |
| 30 | + |
| 31 | +import org.springframework.core.MethodParameter; |
| 32 | + |
| 33 | +import javax.tools.JavaCompiler; |
| 34 | +import javax.tools.ToolProvider; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.io.FileWriter; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.PrintWriter; |
| 40 | +import java.lang.reflect.Method; |
| 41 | +import java.net.URL; |
| 42 | +import java.net.URLClassLoader; |
| 43 | +import java.util.stream.Stream; |
| 44 | + |
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | + |
| 47 | +/** |
| 48 | + * Tests for {@link MethodParameterPojoExtractor}. |
| 49 | + */ |
| 50 | +class MethodParameterPojoExtractorTest { |
| 51 | + @TempDir |
| 52 | + File tempDir; |
| 53 | + |
| 54 | + /** |
| 55 | + * Tests for {@link MethodParameterPojoExtractor#extractFrom(Class<?>)}. |
| 56 | + */ |
| 57 | + @Nested |
| 58 | + class extractFrom { |
| 59 | + @Test |
| 60 | + @EnabledForJreRange(min = JRE.JAVA_17) |
| 61 | + void ifRecordObjectShouldGetField() throws IOException, ClassNotFoundException { |
| 62 | + File recordObject = new File(tempDir, "RecordObject.java"); |
| 63 | + try (PrintWriter writer = new PrintWriter(new FileWriter(recordObject))) { |
| 64 | + writer.println("public record RecordObject(String email, String firstName, String lastName){"); |
| 65 | + writer.println("}"); |
| 66 | + } |
| 67 | + String[] args = { |
| 68 | + recordObject.getAbsolutePath() |
| 69 | + }; |
| 70 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 71 | + int r = compiler.run(null, null, null, args); |
| 72 | + if (r != 0) { |
| 73 | + throw new IllegalStateException("Compilation failed"); |
| 74 | + } |
| 75 | + URL[] urls = { tempDir.toURI().toURL() }; |
| 76 | + ClassLoader loader = URLClassLoader.newInstance(urls); |
| 77 | + |
| 78 | + Class<?> clazz = loader.loadClass("RecordObject"); |
| 79 | + |
| 80 | + Stream<MethodParameter> actual = MethodParameterPojoExtractor.extractFrom(clazz); |
| 81 | + assertThat(actual) |
| 82 | + .extracting(MethodParameter::getMethod) |
| 83 | + .extracting(Method::getName) |
| 84 | + .containsOnlyOnce("email", "firstName", "lastName"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void ifRecordObjectShouldGetMethod() { |
| 89 | + Stream<MethodParameter> actual = MethodParameterPojoExtractor.extractFrom(ClassObject.class); |
| 90 | + assertThat(actual) |
| 91 | + .extracting(MethodParameter::getMethod) |
| 92 | + .extracting(Method::getName) |
| 93 | + .containsOnlyOnce("getEmail", "getFirstName", "getLastName"); |
| 94 | + } |
| 95 | + |
| 96 | + public class ClassObject { |
| 97 | + private String email; |
| 98 | + |
| 99 | + private String firstName; |
| 100 | + |
| 101 | + private String lastName; |
| 102 | + |
| 103 | + public ClassObject(String email, String firstName, String lastName) { |
| 104 | + this.email = email; |
| 105 | + this.firstName = firstName; |
| 106 | + this.lastName = lastName; |
| 107 | + } |
| 108 | + |
| 109 | + public String getEmail() { |
| 110 | + return email; |
| 111 | + } |
| 112 | + |
| 113 | + public String getFirstName() { |
| 114 | + return firstName; |
| 115 | + } |
| 116 | + |
| 117 | + public String getLastName() { |
| 118 | + return lastName; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments