-
Notifications
You must be signed in to change notification settings - Fork 77
Convert com.vogella.tasks.services.tests to plug-in test with OSGi service testing #55
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ bin | |
| target | ||
| .searchable | ||
| screenshots/ | ||
| dataFile | ||
|
|
||
| # You can use wildcards as well | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>com.vogella.tycho</groupId> | ||
| <artifactId>releng</artifactId> | ||
| <version>1.0.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>com.vogella.tasks.services.tests</artifactId> | ||
| <version>1.0.0-SNAPSHOT</version> | ||
| <packaging>eclipse-test-plugin</packaging> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,79 @@ | ||
| package com.vogella.tasks.services.tests; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.osgi.framework.BundleContext; | ||
| import org.osgi.framework.FrameworkUtil; | ||
| import org.osgi.framework.ServiceReference; | ||
|
|
||
| import com.vogella.tasks.model.Task; | ||
| import com.vogella.tasks.services.internal.TransientTaskServiceImpl; | ||
| import com.vogella.tasks.model.TaskService; | ||
|
|
||
| /** | ||
| * Plug-in test for TaskService OSGi service. | ||
| * This test verifies that the TaskService is properly available as an OSGi service | ||
| * and functions correctly. | ||
| */ | ||
| class TransientTaskServiceImplTests { | ||
|
|
||
| @Test | ||
| @DisplayName("Ensures that the test service always returns 9 elements") | ||
| void testThatTestServiceReturnsNineTasks() { | ||
| TransientTaskServiceImpl taskService = new TransientTaskServiceImpl(); | ||
| List<Task> tasks = taskService.getAll(); | ||
| assertEquals(9, tasks.size()); | ||
| } | ||
| private TaskService taskService; | ||
| private BundleContext bundleContext; | ||
| private ServiceReference<TaskService> serviceReference; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); | ||
| assertNotNull(bundleContext, "Bundle context should be available"); | ||
|
|
||
| serviceReference = bundleContext.getServiceReference(TaskService.class); | ||
| assertNotNull(serviceReference, "TaskService should be registered as an OSGi service"); | ||
|
|
||
| taskService = bundleContext.getService(serviceReference); | ||
| assertNotNull(taskService, "TaskService should be available"); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| if (serviceReference != null) { | ||
| bundleContext.ungetService(serviceReference); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Ensures that the test service returns tasks") | ||
| void testThatTestServiceReturnsTasks() { | ||
| List<Task> tasks = taskService.getAll(); | ||
| // The service returns at least 9 tasks initially, but may have more due to other tests | ||
| assertTrue(tasks.size() >= 9, "Should have at least 9 tasks, got " + tasks.size()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test getting a task by ID") | ||
| void testGetTaskById() { | ||
| List<Task> tasks = taskService.getAll(); | ||
| assertTrue(tasks.size() > 0, "Should have tasks"); | ||
|
|
||
| long firstTaskId = tasks.get(0).getId(); | ||
| var task = taskService.get(firstTaskId); | ||
|
|
||
| assertTrue(task.isPresent(), "Should find task by ID"); | ||
| assertEquals(firstTaskId, task.get().getId()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test consuming tasks") | ||
| void testConsumeTask() { | ||
| taskService.consume(tasks -> { | ||
| assertNotNull(tasks, "Tasks list should not be null"); | ||
| assertTrue(tasks.size() >= 9, "Should have at least 9 tasks"); | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="com.vogella.tasks.services.internal.TaskServiceComponent"> | ||
| <service> | ||
| <provide interface="com.vogella.tasks.model.TaskService"/> | ||
| </service> | ||
| <implementation class="com.vogella.tasks.services.internal.TaskServiceComponent"/> | ||
| </scr:component> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package com.vogella.tasks.services.internal; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.osgi.service.component.annotations.Component; | ||
|
|
||
| import com.vogella.tasks.model.Task; | ||
| import com.vogella.tasks.model.TaskService; | ||
|
|
||
| /** | ||
| * OSGi Declarative Services component that provides TaskService. | ||
| * This is a simplified version of TransientTaskServiceImpl without Eclipse DI dependencies, | ||
| * suitable for OSGi testing. | ||
| */ | ||
| @Component(service = TaskService.class) | ||
| public class TaskServiceComponent implements TaskService { | ||
|
|
||
| private static AtomicInteger current = new AtomicInteger(1); | ||
| private List<Task> tasks; | ||
|
|
||
| public TaskServiceComponent() { | ||
| tasks = createTestData(); | ||
| } | ||
|
|
||
| @Override | ||
| public void consume(Consumer<List<Task>> taskConsumer) { | ||
| taskConsumer.accept(tasks.stream().map(Task::copy).collect(Collectors.toList())); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized boolean update(Task newTask) { | ||
| Optional<Task> taskOptional = findById(newTask.getId()); | ||
| Task task = taskOptional.orElse(new Task(current.getAndIncrement())); | ||
| task.setSummary(newTask.getSummary()); | ||
| task.setDescription(newTask.getDescription()); | ||
| task.setDone(newTask.isDone()); | ||
| task.setDueDate(newTask.getDueDate()); | ||
|
|
||
| if (!taskOptional.isPresent()) { | ||
| tasks.add(task); | ||
| } | ||
|
|
||
| JSONUtil.saveAsGson(tasks); | ||
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Task> get(long id) { | ||
| return findById(id).map(Task::copy); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean delete(long id) { | ||
| Optional<Task> deletedTask = findById(id); | ||
| deletedTask.ifPresent(tasks::remove); | ||
| return deletedTask.isPresent(); | ||
| } | ||
|
|
||
| private List<Task> createTestData() { | ||
| List<Task> list = JSONUtil.retrieveSavedData(); | ||
|
||
| if (list.isEmpty()) { | ||
| list = List.of( | ||
| create("Application model", "Flexible and extensible"), | ||
| create("DI", "@Inject as programming mode"), | ||
| create("OSGi", "Services"), | ||
| create("SWT", "Widgets"), | ||
| create("JFace", "Especially Viewers!"), | ||
| create("CSS Styling", "Style your application"), | ||
| create("Eclipse services", "Selection, model, Part"), | ||
| create("Renderer", "Different UI toolkit"), | ||
| create("Compatibility Layer", "Run Eclipse 3.x") | ||
| ); | ||
| } | ||
| return new ArrayList<>(list); | ||
| } | ||
|
|
||
| private Task create(String summary, String description) { | ||
| return new Task(current.getAndIncrement(), summary, description, false, LocalDate.now()); | ||
| } | ||
|
|
||
| private Optional<Task> findById(long id) { | ||
| return tasks.stream().filter(t -> t.getId() == id).findAny(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Task> getAll() { | ||
| return tasks.stream().map(Task::copy).collect(Collectors.toList()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Use explicit type declaration instead of 'var' for better code readability and consistency with the rest of the codebase.