Skip to content

Commit 4dab42b

Browse files
committed
Create JctFileManagers utility class
This provides static methods to create instances of the default JctFileManagerImpl and JctFileManagerFactoryImpl classes in third-party libraries outside this API.
1 parent 4b032e2 commit 4dab42b

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2022 - 2023, the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.ascopes.jct.filemanagers;
17+
18+
import io.github.ascopes.jct.compilers.JctCompiler;
19+
import io.github.ascopes.jct.filemanagers.impl.JctFileManagerFactoryImpl;
20+
import io.github.ascopes.jct.filemanagers.impl.JctFileManagerImpl;
21+
import io.github.ascopes.jct.utils.UtilityClass;
22+
import org.apiguardian.api.API;
23+
import org.apiguardian.api.API.Status;
24+
25+
/**
26+
* Helpers to create instances of default implementations for file managers.
27+
*
28+
* @author Ashley Scopes
29+
* @since 1.1.0
30+
*/
31+
@API(since = "1.1.0", status = Status.STABLE)
32+
public final class JctFileManagers extends UtilityClass {
33+
private JctFileManagers() {
34+
// Static-only class.
35+
}
36+
37+
/**
38+
* Create a new default implementation of a file manager.
39+
*
40+
* @param release the Java release to use for the file manager.
41+
* @return the file manager instance.
42+
*/
43+
public static JctFileManager newJctFileManager(String release) {
44+
return new JctFileManagerImpl(release);
45+
}
46+
47+
/**
48+
* Create a new default implementation of a file manager factory.
49+
*
50+
* @param compiler the JctCompiler to bind any file managers to.
51+
* @return the file manager factory instance.
52+
*/
53+
public static JctFileManagerFactory newJctFileManagerFactory(JctCompiler compiler) {
54+
return new JctFileManagerFactoryImpl(compiler);
55+
}
56+
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/filemanagers/impl/JctFileManagerFactoryImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ public JctFileManagerFactoryImpl(JctCompiler compiler) {
5656
this.compiler = compiler;
5757
}
5858

59+
/**
60+
* Get the compiler that was set on this file manager factory.
61+
*
62+
* @return the compiler
63+
* @since 1.1.0
64+
*/
65+
@API(since = "1.1.0", status = Status.INTERNAL)
66+
@VisibleForTestingOnly
67+
public JctCompiler getCompiler() {
68+
return compiler;
69+
}
70+
5971
@Override
6072
public JctFileManager createFileManager(Workspace workspace) {
6173
var release = compiler.getEffectiveRelease();

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/helpers/UtilityClassTestTemplate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ default void testClassExtendsUtilityClass() {
6969
.hasSuperclass(UtilityClass.class);
7070
}
7171

72+
@DisplayName("Class should not implement any interfaces")
73+
@Test
74+
default void classShouldNotImplementAnyInterfaces() {
75+
assertThat(getTypeBeingTested().getInterfaces())
76+
.withFailMessage("Expected utility class to not implement any interfaces")
77+
.isEmpty();
78+
}
79+
7280
@DisplayName("Class should be final")
7381
@Test
7482
default void testClassIsFinal() {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2022 - 2023, the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.ascopes.jct.tests.unit.filemanagers;
17+
18+
import static io.github.ascopes.jct.tests.helpers.Fixtures.someRelease;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.Mockito.mock;
21+
22+
import io.github.ascopes.jct.compilers.JctCompiler;
23+
import io.github.ascopes.jct.filemanagers.JctFileManagers;
24+
import io.github.ascopes.jct.filemanagers.impl.JctFileManagerFactoryImpl;
25+
import io.github.ascopes.jct.filemanagers.impl.JctFileManagerImpl;
26+
import io.github.ascopes.jct.tests.helpers.UtilityClassTestTemplate;
27+
import org.junit.jupiter.api.DisplayName;
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* {@link JctFileManagers} tests.
32+
*
33+
* @author Ashley Scopes
34+
*/
35+
@DisplayName("JctFileManagers tests")
36+
class JctFileManagersTest implements UtilityClassTestTemplate {
37+
38+
@Override
39+
public Class<?> getTypeBeingTested() {
40+
return JctFileManagers.class;
41+
}
42+
43+
@DisplayName(".newJctFileManager(String) returns a new JctFileManagerImpl instance")
44+
@Test
45+
void newJctFileManagerReturnsNewJctFileManagerImplInstance() {
46+
// Given
47+
var release = someRelease();
48+
49+
// When
50+
var fileManager = JctFileManagers.newJctFileManager(release);
51+
52+
// Then
53+
assertThat(fileManager).isInstanceOf(JctFileManagerImpl.class);
54+
assertThat(fileManager.getEffectiveRelease()).isEqualTo(release);
55+
}
56+
57+
@DisplayName(
58+
".newJctFileManagerFactory(JctCompiler) returns a new JctFileManagerFactoryImpl instance"
59+
)
60+
@Test
61+
void newJctFileManagerFactoryReturnsNewJctFileManagerFactoryImplInstance() {
62+
// Given
63+
var compiler = mock(JctCompiler.class);
64+
65+
// When
66+
var fileManagerFactory = JctFileManagers.newJctFileManagerFactory(compiler);
67+
68+
// Then
69+
assertThat(fileManagerFactory).isInstanceOf(JctFileManagerFactoryImpl.class);
70+
assertThat(((JctFileManagerFactoryImpl) fileManagerFactory).getCompiler()).isSameAs(compiler);
71+
}
72+
}

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/filemanagers/impl/JctFileManagerFactoryImplTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ class JctFileManagerFactoryImplTest {
7171
@Spy
7272
JctFileManagerFactoryImpl factory;
7373

74+
@DisplayName(".getCompiler() returns the compiler")
75+
@Test
76+
void getCompilerReturnsTheCompiler() {
77+
// Then
78+
assertThat(factory.getCompiler()).isSameAs(compiler);
79+
}
80+
7481
@DisplayName("Created file managers use the effective release")
7582
@Test
7683
void createdFileManagersUseTheEffectiveRelease() {

0 commit comments

Comments
 (0)