|
| 1 | +package org.javaee7.jpa.aggregate_function_in_select; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import javax.inject.Inject; |
| 9 | + |
| 10 | +import org.javaee7.jpa.aggregate_function_in_select.entity.AggregatedTestEntity; |
| 11 | +import org.javaee7.jpa.aggregate_function_in_select.service.TestService; |
| 12 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 13 | +import org.jboss.arquillian.junit.Arquillian; |
| 14 | +import org.jboss.shrinkwrap.api.Archive; |
| 15 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 16 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 17 | +import org.jboss.shrinkwrap.resolver.api.maven.Maven; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | + |
| 21 | +/** |
| 22 | + * This tests that an aggregate function can be used in the select clause |
| 23 | + * <p> |
| 24 | + * Currently the JPQL constructor expression is tested for this. |
| 25 | + * |
| 26 | + * @author Arjan Tijms |
| 27 | + */ |
| 28 | +@RunWith(Arquillian.class) |
| 29 | +public class AggregateFunctionInSelectTest { |
| 30 | + |
| 31 | + private static final String WEBAPP_SRC = "src/main/webapp"; |
| 32 | + |
| 33 | + @Inject |
| 34 | + private TestService testService; |
| 35 | + |
| 36 | + @Deployment |
| 37 | + public static Archive<?> deploy() { |
| 38 | + return ShrinkWrap.create(WebArchive.class) |
| 39 | + .addPackages(true, AggregateFunctionInSelectTest.class.getPackage()) |
| 40 | + .addAsResource("META-INF/persistence.xml") |
| 41 | + .addAsWebInfResource(resource("web.xml")) |
| 42 | + .addAsLibraries(Maven.resolver() |
| 43 | + .loadPomFromFile("pom.xml") |
| 44 | + .resolve("org.hsqldb:hsqldb") |
| 45 | + .withoutTransitivity() |
| 46 | + .asSingleFile()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void aggregateFunctionInCtorSelectJPQL() throws Exception { |
| 51 | + |
| 52 | + testService.saveEntities(); |
| 53 | + |
| 54 | + List<AggregatedTestEntity> testEntities = testService.getAggregation(); |
| 55 | + |
| 56 | + assertTrue( |
| 57 | + "All entities should have been aggregated into 1 result row", |
| 58 | + testEntities.size() == 1 |
| 59 | + ); |
| 60 | + |
| 61 | + String values = testEntities.get(0).getValues(); |
| 62 | + |
| 63 | + assertTrue( |
| 64 | + "Aggregation should be 1,2 or 2,1, but was: " + values, |
| 65 | + values.equals("1,2") || values.equals("2,1") // order doesn't matter here |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + private static File resource(String name) { |
| 70 | + return new File(WEBAPP_SRC + "/WEB-INF", name); |
| 71 | + } |
| 72 | +} |
0 commit comments