|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.api.plugin.testing.stubs; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.util.NoSuchElementException; |
| 23 | + |
| 24 | +import org.apache.maven.api.Service; |
| 25 | +import org.apache.maven.api.Session; |
| 26 | +import org.apache.maven.api.services.ArtifactFactory; |
| 27 | +import org.apache.maven.api.services.ArtifactManager; |
| 28 | +import org.apache.maven.api.services.OsService; |
| 29 | +import org.apache.maven.api.services.ProjectBuilder; |
| 30 | +import org.apache.maven.api.services.ProjectManager; |
| 31 | +import org.apache.maven.api.services.RepositoryFactory; |
| 32 | +import org.apache.maven.api.services.VersionParser; |
| 33 | +import org.apache.maven.api.services.xml.ModelXmlFactory; |
| 34 | +import org.apache.maven.impl.InternalSession; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for the getService method behavior in SessionMock and SessionStub. |
| 42 | + * This test verifies that both implementations correctly throw NoSuchElementException |
| 43 | + * when a service is not found, as specified in the Session interface contract. |
| 44 | + */ |
| 45 | +class SessionGetServiceTest { |
| 46 | + |
| 47 | + private static final String LOCAL_REPO = System.getProperty("java.io.tmpdir") + File.separator + "test-repo"; |
| 48 | + |
| 49 | + /** |
| 50 | + * Test that SessionStub.getService throws NoSuchElementException for unknown services. |
| 51 | + */ |
| 52 | + @Test |
| 53 | + void testSessionStubGetServiceThrowsNoSuchElementException() { |
| 54 | + Session session = new SessionStub(); |
| 55 | + |
| 56 | + // Test with a service interface that should not be available |
| 57 | + NoSuchElementException exception = assertThrows( |
| 58 | + NoSuchElementException.class, |
| 59 | + () -> session.getService(OsService.class), |
| 60 | + "SessionStub.getService should throw NoSuchElementException for unknown services"); |
| 61 | + |
| 62 | + // Verify the exception message contains the service class name |
| 63 | + assertNotNull(exception.getMessage()); |
| 64 | + assert exception.getMessage().contains(OsService.class.getName()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test that SessionStub.getService throws NoSuchElementException for any service. |
| 69 | + * Since SessionStub is a minimal stub, it should not provide any services. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + void testSessionStubGetServiceThrowsForAnyService() { |
| 73 | + Session session = new SessionStub(); |
| 74 | + |
| 75 | + // Test with various service types |
| 76 | + assertThrows(NoSuchElementException.class, () -> session.getService(ArtifactManager.class)); |
| 77 | + assertThrows(NoSuchElementException.class, () -> session.getService(ProjectBuilder.class)); |
| 78 | + assertThrows(NoSuchElementException.class, () -> session.getService(VersionParser.class)); |
| 79 | + assertThrows(NoSuchElementException.class, () -> session.getService(RepositoryFactory.class)); |
| 80 | + assertThrows(NoSuchElementException.class, () -> session.getService(ArtifactFactory.class)); |
| 81 | + assertThrows(NoSuchElementException.class, () -> session.getService(ProjectManager.class)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test that regular SessionMock.getService throws NoSuchElementException for unknown services. |
| 86 | + * This matches the behavior of the real Session implementation. |
| 87 | + */ |
| 88 | + @Test |
| 89 | + void testSessionMockGetServiceThrowsNoSuchElementExceptionForUnknownServices() { |
| 90 | + InternalSession session = SessionMock.getMockSession(LOCAL_REPO); |
| 91 | + |
| 92 | + // Test with a service that is not configured in SessionMock |
| 93 | + NoSuchElementException exception = assertThrows( |
| 94 | + NoSuchElementException.class, |
| 95 | + () -> session.getService(OsService.class), |
| 96 | + "SessionMock.getService should throw NoSuchElementException for unknown services"); |
| 97 | + |
| 98 | + // Verify the exception message contains the service class name |
| 99 | + assertNotNull(exception.getMessage()); |
| 100 | + assert exception.getMessage().contains(OsService.class.getName()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Test that enhanced SessionMock.getService throws NoSuchElementException for unknown services. |
| 105 | + * Note: This method is now equivalent to the regular getMockSession() method. |
| 106 | + */ |
| 107 | + @Test |
| 108 | + void testEnhancedSessionMockGetServiceThrowsNoSuchElementExceptionForUnknownServices() { |
| 109 | + InternalSession session = SessionMock.getMockSessionWithEnhancedServiceBehavior(LOCAL_REPO); |
| 110 | + |
| 111 | + // Test with a service that is not configured in SessionMock |
| 112 | + NoSuchElementException exception = assertThrows( |
| 113 | + NoSuchElementException.class, |
| 114 | + () -> session.getService(OsService.class), |
| 115 | + "Enhanced SessionMock.getService should throw NoSuchElementException for unknown services"); |
| 116 | + |
| 117 | + // Verify the exception message contains the service class name |
| 118 | + assertNotNull(exception.getMessage()); |
| 119 | + assert exception.getMessage().contains(OsService.class.getName()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test that SessionMock.getService returns configured services successfully. |
| 124 | + */ |
| 125 | + @Test |
| 126 | + void testSessionMockGetServiceReturnsConfiguredServices() { |
| 127 | + InternalSession session = SessionMock.getMockSession(LOCAL_REPO); |
| 128 | + |
| 129 | + // Test services that are configured in SessionMock |
| 130 | + assertNotNull(session.getService(RepositoryFactory.class)); |
| 131 | + assertNotNull(session.getService(VersionParser.class)); |
| 132 | + assertNotNull(session.getService(ArtifactManager.class)); |
| 133 | + assertNotNull(session.getService(ProjectManager.class)); |
| 134 | + assertNotNull(session.getService(ArtifactFactory.class)); |
| 135 | + assertNotNull(session.getService(ProjectBuilder.class)); |
| 136 | + assertNotNull(session.getService(ModelXmlFactory.class)); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Test that enhanced SessionMock.getService throws NoSuchElementException for custom service interfaces. |
| 141 | + * Note: This method is now equivalent to the regular getMockSession() method. |
| 142 | + */ |
| 143 | + @Test |
| 144 | + void testEnhancedSessionMockGetServiceThrowsForCustomServices() { |
| 145 | + InternalSession session = SessionMock.getMockSessionWithEnhancedServiceBehavior(LOCAL_REPO); |
| 146 | + |
| 147 | + // Test with a custom service interface |
| 148 | + assertThrows( |
| 149 | + NoSuchElementException.class, |
| 150 | + () -> session.getService(CustomTestService.class), |
| 151 | + "Enhanced SessionMock.getService should throw NoSuchElementException for custom services"); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Custom service interface for testing purposes. |
| 156 | + */ |
| 157 | + interface CustomTestService extends Service { |
| 158 | + void doSomething(); |
| 159 | + } |
| 160 | +} |
0 commit comments