Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create CDI extension tests #289

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tck-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<properties>
<jakarta.data.tck.version>${project.version}</jakarta.data.tck.version>
<jakarta.data.api.version>${project.version}</jakarta.data.api.version>

<maven.site.skip>true</maven.site.skip>
<asciidoctor.maven.plugin.version>2.2.4</asciidoctor.maven.plugin.version>
Expand All @@ -49,10 +50,23 @@
</properties>

<dependencies>
<!-- The TCK, used to auto generate docs -->
<dependency>
<groupId>jakarta.data</groupId>
<artifactId>jakarta-data-tck</artifactId>
<version>${jakarta.data.tck.version}</version>
</dependency>
<!-- Provided dependencies for the TCK -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${jakarta.servlet.version}</version>
</dependency>

<dependency>
<groupId>jakarta.data</groupId>
<artifactId>jakarta-data-api</artifactId>
<version>${jakarta.data.api.version}</version>
</dependency>
</dependencies>

Expand Down
7 changes: 7 additions & 0 deletions tck/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>${jakarta.enterprise.cdi.version}</version>
<scope>provided</scope>
</dependency>

<!-- Provided Entity APIs -->
<dependency>
<groupId>jakarta.nosql</groupId>
Expand Down
32 changes: 32 additions & 0 deletions tck/src/main/java/ee/jakarta/tck/data/core/cdi/Directory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.core.cdi;

import java.util.List;

import ee.jakarta.tck.data.core.cdi.provider.PersonExtension;
import jakarta.data.repository.DataRepository;
import jakarta.data.repository.Repository;

/**
* A Directory repository for testing.
*
* @see ee.jakarta.tck.data.core.cdi.provider.DirectoryRepository
*/
@Repository(provider = PersonExtension.PERSON_PROVIDER)
public interface Directory extends DataRepository<Person, Long> {
List<String> findFirstNameByIdInOrderByAgeDesc(List<Long> ids);
}
61 changes: 61 additions & 0 deletions tck/src/main/java/ee/jakarta/tck/data/core/cdi/ExtensionTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.core.cdi;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;

import ee.jakarta.tck.data.core.cdi.provider.PersonExtension;
import ee.jakarta.tck.data.framework.junit.anno.AnyEntity;
import ee.jakarta.tck.data.framework.junit.anno.Assertion;
import ee.jakarta.tck.data.framework.junit.anno.CDI;
import ee.jakarta.tck.data.framework.junit.anno.Core;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.inject.Inject;

@Core
@AnyEntity
@CDI
KyleAure marked this conversation as resolved.
Show resolved Hide resolved
public class ExtensionTests {

@Deployment
public static WebArchive createDeployment() {
JavaArchive provider = ShrinkWrap.create(JavaArchive.class)
.addPackage(PersonExtension.class.getPackage())
.addAsServiceProvider(Extension.class, PersonExtension.class);


return ShrinkWrap.create(WebArchive.class)
.addPackage(ExtensionTests.class.getPackage())
.addAsLibraries(provider);

}

@Inject
Directory directory;

@Assertion(id = "133", strategy="TODO")
public void testDataProviderWithExtension() {
assertEquals(List.of("Olivia", "Lauren", "Victor"), directory.findFirstNameByIdInOrderByAgeDesc(List.of(04L, 05L, 011L)));

}
}
44 changes: 44 additions & 0 deletions tck/src/main/java/ee/jakarta/tck/data/core/cdi/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.core.cdi;

import ee.jakarta.tck.data.core.cdi.provider.PersonEntity;

/**
* A test entity that will be persisted to a repository.
* Uses the custom {@literal @}PersonEntity annotation.
*
* @see ee.jakarta.tck.data.core.cdi.provider.PersonEntity
*/
@PersonEntity
public class Person {
public long id;
public String firstName;
public String lastName;
public int age;

public Person() {
//blank
}

public Person(long id, String firstName, String lastName, int age) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.core.cdi.provider;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import ee.jakarta.tck.data.core.cdi.Directory;
import ee.jakarta.tck.data.core.cdi.Person;

/**
* An implementation of the Directory repository interface.
* Implementation is backed by an in-memory HashMap and has one function.
*/
public class DirectoryRepository implements Directory {

private Map<Long, Person> data = new HashMap<Long, Person>();

public DirectoryRepository() {
data.put(01L, new Person(01L, "Alexander", "Grant", 72));
data.put(02L, new Person(02L, "Bella", "Glover", 48));
data.put(03L, new Person(03L, "Dorothy", "Wright", 67));
data.put(04L, new Person(04L, "Lauren", "Powell", 14));
data.put(05L, new Person(05L, "Olivia", "Skinner", 22));
data.put(06L, new Person(06L, "Robert", "Green", 108));
data.put(07L, new Person(07L, "Leonard", "Nolan", 29));
data.put(010L, new Person(010L, "Michelle", "Parr", 59));
data.put(011L, new Person(011L, "Victor", "Gibson", 12));
data.put(012L, new Person(012L, "Grace", "Clarkson", 85));
}

@Override
public List<String> findFirstNameByIdInOrderByAgeDesc(List<Long> ids) {
return data.values()
.stream()
.filter(p -> ids.contains(p.id))
.sorted(Comparator.comparing((Person p) -> p.age).reversed())
.map(p -> p.firstName)
.collect(Collectors.toList());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.core.cdi.provider;

import java.util.Collections;
import java.util.Set;
import java.util.logging.Logger;

import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.enterprise.inject.spi.Producer;
import jakarta.enterprise.inject.spi.ProducerFactory;

/**
* A CDI producer for the DictonaryRepository
KyleAure marked this conversation as resolved.
Show resolved Hide resolved
*
* @param <R> The repository producer (i.e. this)
* @param <P> The type of the bean containing the producer
*/
public class DirectoryRepositoryProducer<R, P> implements Producer<DirectoryRepository> {

private static final Logger log = Logger.getLogger(DirectoryRepositoryProducer.class.getCanonicalName());

/**
* Factory class for this repository producer.
*/
static class Factory<P> implements ProducerFactory<P> {
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public <R> Producer<R> createProducer(Bean<R> bean) {
return new DirectoryRepositoryProducer();
}
}

@Override
public void dispose(DirectoryRepository instance) {
log.info("Directory CDI extension has been disposed: " + instance);
}

@Override
public Set<InjectionPoint> getInjectionPoints() {
return Collections.emptySet();
}

@Override
public DirectoryRepository produce(CreationalContext<DirectoryRepository> cc) {
DirectoryRepository instance = new DirectoryRepository();

log.info("Directory CDI extension has been produced: " + instance);

return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.core.cdi.provider;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* A custom entity annotation for testing.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PersonEntity {

}
Loading