Add test for fragments API

See gh-249
This commit is contained in:
Christoph Strobl
2024-08-08 11:56:44 +02:00
committed by Andy Wilkinson
parent d4bd78d01a
commit ab0ff54ae7
6 changed files with 67 additions and 1 deletions

View File

@@ -116,4 +116,11 @@ class DataJpaApplicationAotTests {
});
}
@Test
void fragmentsApi(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("fragmentsApi(): RepositoryExtension says hello to Book");
});
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.acme.data.jpa.extension;
import org.springframework.data.repository.core.RepositoryMethodContext;
import org.springframework.data.repository.core.support.RepositoryMetadataAccess;
class DefaultRepositoryExtension<T> implements RepositoryExtension<T>, RepositoryMetadataAccess {
@Override
public String extensionMethod(String arg) {
return "RepositoryExtension says %s to %s".formatted(arg,
RepositoryMethodContext.getContext().getMetadata().getDomainType().getSimpleName());
}
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.acme.data.jpa.extension;
public interface RepositoryExtension<T> {
String extensionMethod(String arg);
}

View File

@@ -15,12 +15,13 @@
*/
package com.example.data.jpa;
import com.acme.data.jpa.extension.RepositoryExtension;
import com.example.data.jpa.model.Book;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.ListCrudRepository;
public interface BookRepository extends ListCrudRepository<Book, Long> {
public interface BookRepository extends ListCrudRepository<Book, Long>, RepositoryExtension<Book> {
@EntityGraph(attributePaths = "authors")
@Query("SELECT b FROM Book b WHERE b.title = :title")

View File

@@ -42,6 +42,7 @@ class TransactionalRunner implements CommandLineRunner {
deleteAll();
entityGraph();
insertPublishers(); // uses AbstractPersistable
fragmentsApi();
}
private void deleteAll() {
@@ -119,4 +120,8 @@ class TransactionalRunner implements CommandLineRunner {
return List.of(publisher1);
}
private void fragmentsApi() {
System.out.printf("fragmentsApi(): %s%n", this.bookRepository.extensionMethod("hello"));
}
}

View File

@@ -0,0 +1 @@
com.acme.data.jpa.extension.RepositoryExtension=com.acme.data.jpa.extension.DefaultRepositoryExtension