Polishing

This commit is contained in:
Sam Brannen 2022-05-06 14:12:30 +02:00
parent 16e7f1f212
commit 8627bef8d9
3 changed files with 29 additions and 36 deletions

View File

@ -73,9 +73,9 @@ public class FileSystemGeneratedFiles implements GeneratedFiles {
private static Function<Kind, Path> conventionRoots(Path root) { private static Function<Kind, Path> conventionRoots(Path root) {
Assert.notNull(root, "'root' must not be null"); Assert.notNull(root, "'root' must not be null");
return kind -> switch (kind) { return kind -> switch (kind) {
case SOURCE -> root.resolve("sources"); case SOURCE -> root.resolve("sources");
case RESOURCE -> root.resolve("resources"); case RESOURCE -> root.resolve("resources");
case CLASS -> root.resolve("classes"); case CLASS -> root.resolve("classes");
}; };
} }
@ -83,7 +83,7 @@ public class FileSystemGeneratedFiles implements GeneratedFiles {
public void addFile(Kind kind, String path, InputStreamSource content) { public void addFile(Kind kind, String path, InputStreamSource content) {
Assert.notNull(kind, "'kind' must not be null"); Assert.notNull(kind, "'kind' must not be null");
Assert.hasLength(path, "'path' must not be empty"); Assert.hasLength(path, "'path' must not be empty");
Assert.notNull(content, "'kind' must not be null"); Assert.notNull(content, "'content' must not be null");
Path root = this.roots.apply(kind).toAbsolutePath().normalize(); Path root = this.roots.apply(kind).toAbsolutePath().normalize();
Path relativePath = root.resolve(path).toAbsolutePath().normalize(); Path relativePath = root.resolve(path).toAbsolutePath().normalize();
Assert.isTrue(relativePath.startsWith(root), () -> "'path' must be relative"); Assert.isTrue(relativePath.startsWith(root), () -> "'path' must be relative");

View File

@ -250,33 +250,35 @@ public abstract class ObjectUtils {
* @return the new array (of the same component type; never {@code null}) * @return the new array (of the same component type; never {@code null})
*/ */
public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj) { public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj) {
return addObjectToArray(array, obj, (array != null) ? array.length : 0); return addObjectToArray(array, obj, (array != null ? array.length : 0));
} }
/** /**
* Append the given object to the given array, returning a new array * Add the given object to the given array at the specified position, returning
* consisting of the input array contents plus the given object. * a new array consisting of the input array contents plus the given object.
* @param array the array to append to (can be {@code null}) * @param array the array to add to (can be {@code null})
* @param obj the object to append * @param obj the object to append
* @param position the position at which to add the object
* @return the new array (of the same component type; never {@code null}) * @return the new array (of the same component type; never {@code null})
* @since 6.0
*/ */
public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj, int position) { public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj, int position) {
Class<?> compType = Object.class; Class<?> componentType = Object.class;
if (array != null) { if (array != null) {
compType = array.getClass().getComponentType(); componentType = array.getClass().getComponentType();
} }
else if (obj != null) { else if (obj != null) {
compType = obj.getClass(); componentType = obj.getClass();
} }
int newArrLength = (array != null ? array.length + 1 : 1); int newArrayLength = (array != null ? array.length + 1 : 1);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
A[] newArr = (A[]) Array.newInstance(compType, newArrLength); A[] newArray = (A[]) Array.newInstance(componentType, newArrayLength);
if (array != null) { if (array != null) {
System.arraycopy(array, 0, newArr, 0, position); System.arraycopy(array, 0, newArray, 0, position);
System.arraycopy(array, position, newArr, position + 1, array.length - position); System.arraycopy(array, position, newArray, position + 1, array.length - position);
} }
newArr[position] = obj; newArray[position] = obj;
return newArr; return newArray;
} }
/** /**

View File

@ -46,12 +46,9 @@ class FileSystemGeneratedFilesTests {
generatedFiles.addResourceFile("META-INF/test", "test"); generatedFiles.addResourceFile("META-INF/test", "test");
generatedFiles.addClassFile("com/example/TestProxy.class", generatedFiles.addClassFile("com/example/TestProxy.class",
new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8))); new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8)));
assertThat(this.root.resolve("sources/com/example/Test.java")).content() assertThat(this.root.resolve("sources/com/example/Test.java")).content().isEqualTo("{}");
.isEqualTo("{}"); assertThat(this.root.resolve("resources/META-INF/test")).content().isEqualTo("test");
assertThat(this.root.resolve("resources/META-INF/test")).content() assertThat(this.root.resolve("classes/com/example/TestProxy.class")).content().isEqualTo("!");
.isEqualTo("test");
assertThat(this.root.resolve("classes/com/example/TestProxy.class")).content()
.isEqualTo("!");
} }
@Test @Test
@ -62,12 +59,9 @@ class FileSystemGeneratedFilesTests {
generatedFiles.addResourceFile("META-INF/test", "test"); generatedFiles.addResourceFile("META-INF/test", "test");
generatedFiles.addClassFile("com/example/TestProxy.class", generatedFiles.addClassFile("com/example/TestProxy.class",
new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8))); new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8)));
assertThat(this.root.resolve("the-SOURCE/com/example/Test.java")).content() assertThat(this.root.resolve("the-SOURCE/com/example/Test.java")).content().isEqualTo("{}");
.isEqualTo("{}"); assertThat(this.root.resolve("the-RESOURCE/META-INF/test")).content().isEqualTo("test");
assertThat(this.root.resolve("the-RESOURCE/META-INF/test")).content() assertThat(this.root.resolve("the-CLASS/com/example/TestProxy.class")).content().isEqualTo("!");
.isEqualTo("test");
assertThat(this.root.resolve("the-CLASS/com/example/TestProxy.class")).content()
.isEqualTo("!");
} }
@Test @Test
@ -80,17 +74,15 @@ class FileSystemGeneratedFilesTests {
@Test @Test
void createWhenRootsIsNullThrowsException() { void createWhenRootsIsNullThrowsException() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy( .isThrownBy(() -> new FileSystemGeneratedFiles((Function<Kind, Path>) null))
() -> new FileSystemGeneratedFiles((Function<Kind, Path>) null))
.withMessage("'roots' must not be null"); .withMessage("'roots' must not be null");
} }
@Test @Test
void createWhenRootsResultsInNullThrowsException() { void createWhenRootsResultsInNullThrowsException() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy( .isThrownBy(() -> new FileSystemGeneratedFiles(kind -> (kind != Kind.CLASS) ?
() -> new FileSystemGeneratedFiles(kind -> (kind != Kind.CLASS) this.root.resolve(kind.toString()) : null))
? this.root.resolve(kind.toString()) : null))
.withMessage("'roots' must return a value for all file kinds"); .withMessage("'roots' must return a value for all file kinds");
} }
@ -102,8 +94,7 @@ class FileSystemGeneratedFilesTests {
assertPathMustBeRelative(generatedFiles, "test/../../test"); assertPathMustBeRelative(generatedFiles, "test/../../test");
} }
private void assertPathMustBeRelative(FileSystemGeneratedFiles generatedFiles, private void assertPathMustBeRelative(FileSystemGeneratedFiles generatedFiles, String path) {
String path) {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> generatedFiles.addResourceFile(path, "test")) .isThrownBy(() -> generatedFiles.addResourceFile(path, "test"))
.withMessage("'path' must be relative"); .withMessage("'path' must be relative");