|
| 1 | +/* |
| 2 | +* Copyright 2026 Basis Technology Corp. |
| 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 | + |
| 17 | +package com.basistech.rosette.annotations; |
| 18 | + |
| 19 | +import com.google.testing.compile.Compilation; |
| 20 | +import com.google.testing.compile.Compiler; |
| 21 | +import com.google.testing.compile.JavaFileObjects; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import javax.tools.Diagnostic; |
| 25 | +import javax.tools.JavaFileObject; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static com.google.testing.compile.CompilationSubject.assertThat; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +class JacksonMixinProcessorTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + void testJacksonMixinWithoutBuilderProducesError() { |
| 36 | + JavaFileObject classWithoutBuilder = JavaFileObjects.forSourceString( |
| 37 | + "test.ModelWithoutBuilder", |
| 38 | + """ |
| 39 | + package test; |
| 40 | + |
| 41 | + import com.basistech.rosette.annotations.JacksonMixin; |
| 42 | + import lombok.Value; |
| 43 | + |
| 44 | + @Value |
| 45 | + @JacksonMixin |
| 46 | + public class ModelWithoutBuilder { |
| 47 | + String name; |
| 48 | + } |
| 49 | + """ |
| 50 | + ); |
| 51 | + |
| 52 | + Compilation compilation = Compiler.javac() |
| 53 | + .withProcessors(new JacksonMixinProcessor()) |
| 54 | + .compile(classWithoutBuilder); |
| 55 | + |
| 56 | + assertThat(compilation).failed(); |
| 57 | + |
| 58 | + List<Diagnostic<? extends JavaFileObject>> errors = compilation.errors(); |
| 59 | + String errorMessage = errors.get(0).getMessage(null); |
| 60 | + assertTrue(errorMessage.contains("@JacksonMixin requires @Builder annotation"), |
| 61 | + "Error should state that Builder annotation is required. Actual: " + errorMessage); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testClassWithoutJacksonMixinHasNoWarnings() { |
| 66 | + JavaFileObject normalClass = JavaFileObjects.forSourceString( |
| 67 | + "test.NormalClass", |
| 68 | + """ |
| 69 | + package test; |
| 70 | + |
| 71 | + public class NormalClass { |
| 72 | + String name; |
| 73 | + } |
| 74 | + """ |
| 75 | + ); |
| 76 | + |
| 77 | + Compilation compilation = Compiler.javac() |
| 78 | + .withProcessors(new JacksonMixinProcessor()) |
| 79 | + .compile(normalClass); |
| 80 | + |
| 81 | + assertThat(compilation).succeeded(); |
| 82 | + |
| 83 | + // Should have no warnings |
| 84 | + List<Diagnostic<? extends JavaFileObject>> warnings = compilation.warnings(); |
| 85 | + assertEquals(0, warnings.size(), |
| 86 | + "Should have no warnings for classes without JacksonMixin"); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + @Test |
| 91 | + void testJacksonMixinOnInterfaceProducesError() { |
| 92 | + JavaFileObject interfaceWithAnnotation = JavaFileObjects.forSourceString( |
| 93 | + "test.MyInterface", |
| 94 | + """ |
| 95 | + package test; |
| 96 | + |
| 97 | + import com.basistech.rosette.annotations.JacksonMixin; |
| 98 | + |
| 99 | + @JacksonMixin |
| 100 | + public interface MyInterface { |
| 101 | + String getName(); |
| 102 | + } |
| 103 | + """ |
| 104 | + ); |
| 105 | + |
| 106 | + Compilation compilation = Compiler.javac() |
| 107 | + .withProcessors(new JacksonMixinProcessor()) |
| 108 | + .compile(interfaceWithAnnotation); |
| 109 | + |
| 110 | + // Should fail with an error |
| 111 | + assertThat(compilation).failed(); |
| 112 | + |
| 113 | + // Verify it's a meaningful error message (not a stack trace) |
| 114 | + List<Diagnostic<? extends JavaFileObject>> errors = compilation.errors(); |
| 115 | + String errorMessage = errors.get(0).getMessage(null); |
| 116 | + assertTrue(errorMessage.contains("Annotation only available to Class"), |
| 117 | + "Error should state annotation is only for classes. Actual: " + errorMessage); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void testJacksonMixinOnEnumProducesError() { |
| 122 | + JavaFileObject enumWithAnnotation = JavaFileObjects.forSourceString( |
| 123 | + "test.MyEnum", |
| 124 | + """ |
| 125 | + package test; |
| 126 | + |
| 127 | + import com.basistech.rosette.annotations.JacksonMixin; |
| 128 | + |
| 129 | + @JacksonMixin |
| 130 | + public enum MyEnum { |
| 131 | + VALUE1, VALUE2 |
| 132 | + } |
| 133 | + """ |
| 134 | + ); |
| 135 | + |
| 136 | + Compilation compilation = Compiler.javac() |
| 137 | + .withProcessors(new JacksonMixinProcessor()) |
| 138 | + .compile(enumWithAnnotation); |
| 139 | + |
| 140 | + // Should fail with an error |
| 141 | + assertThat(compilation).failed(); |
| 142 | + |
| 143 | + // Verify it's a meaningful error message (not a stack trace) |
| 144 | + List<Diagnostic<? extends JavaFileObject>> errors = compilation.errors(); |
| 145 | + String errorMessage = errors.get(0).getMessage(null); |
| 146 | + assertTrue(errorMessage.contains("Annotation only available to Class"), |
| 147 | + "Error should state annotation is only for classes. Actual: " + errorMessage); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testGetSupportedAnnotationTypes() { |
| 152 | + JacksonMixinProcessor processor = new JacksonMixinProcessor(); |
| 153 | + java.util.Set<String> supportedTypes = processor.getSupportedAnnotationTypes(); |
| 154 | + |
| 155 | + // Should have exactly one supported annotation type |
| 156 | + assertEquals(1, supportedTypes.size(), |
| 157 | + "Should support exactly one annotation type"); |
| 158 | + |
| 159 | + // Should be the JacksonMixin annotation |
| 160 | + String expectedType = JacksonMixin.class.getCanonicalName(); |
| 161 | + assertTrue(supportedTypes.contains(expectedType), |
| 162 | + "Should support " + expectedType + " annotation. Actual: " + supportedTypes); |
| 163 | + } |
| 164 | +} |
0 commit comments