|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * A plugin for managing SciJava-based projects. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2025 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.maven.plugin.enforcer; |
| 31 | + |
| 32 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | +import static org.hamcrest.Matchers.containsString; |
| 34 | +import static org.junit.Assert.assertTrue; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | + |
| 39 | +import org.apache.maven.enforcer.rule.api.EnforcerLogger; |
| 40 | +import org.apache.maven.enforcer.rule.api.EnforcerRuleException; |
| 41 | +import org.apache.maven.model.Build; |
| 42 | +import org.apache.maven.project.MavenProject; |
| 43 | +import org.junit.Assert; |
| 44 | +import org.junit.Before; |
| 45 | +import org.junit.Rule; |
| 46 | +import org.junit.Test; |
| 47 | +import org.junit.rules.ExpectedException; |
| 48 | +import org.junit.rules.TemporaryFolder; |
| 49 | + |
| 50 | +/** |
| 51 | + * Tests for {@link MainClassExistsRule}. |
| 52 | + * |
| 53 | + * @author Gabriel Selzer |
| 54 | + */ |
| 55 | +public class MainClassExistsRuleTest { |
| 56 | + |
| 57 | + /** |
| 58 | + * Test subclass that provides a mock logger. |
| 59 | + */ |
| 60 | + private class MainClassExistsRuleMock extends MainClassExistsRule { |
| 61 | + public MainClassExistsRuleMock(MavenProject project) { |
| 62 | + super(project); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public EnforcerLogger getLog() { |
| 67 | + return logMock; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Rule |
| 72 | + public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 73 | + |
| 74 | + private MavenProject project; |
| 75 | + private File outputDirectory; |
| 76 | + private EnforcerLoggerMock logMock; |
| 77 | + |
| 78 | + @Before |
| 79 | + public void setUp() throws Exception { |
| 80 | + // Create a temporary output directory |
| 81 | + outputDirectory = temporaryFolder.newFolder("target", "classes"); |
| 82 | + |
| 83 | + // Create a mock MavenProject |
| 84 | + project = new MavenProject(); |
| 85 | + Build build = new Build(); |
| 86 | + build.setOutputDirectory(outputDirectory.getAbsolutePath()); |
| 87 | + project.setBuild(build); |
| 88 | + |
| 89 | + // Create a mock logger |
| 90 | + logMock = new EnforcerLoggerMock(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void execute_NoMainClassProperty_Passes() throws Exception { |
| 95 | + // No "main-class" property set |
| 96 | + MainClassExistsRule rule = new MainClassExistsRuleMock(project); |
| 97 | + // Execute the rule and make sure it passes |
| 98 | + rule.execute(); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void execute_MainClassExists_Passes() throws Exception { |
| 103 | + // Set main-class property |
| 104 | + project.getProperties().setProperty("main-class", "com.example.Main"); |
| 105 | + |
| 106 | + // Create the corresponding .class file |
| 107 | + createClassFile("com.example.Main"); |
| 108 | + |
| 109 | + // Execute the rule and make sure it passes |
| 110 | + MainClassExistsRule rule = new MainClassExistsRuleMock(project); |
| 111 | + rule.execute(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void execute_MainClassDoesNotExist_ThrowsException() throws Exception { |
| 116 | + // Set main-class property... |
| 117 | + project.getProperties().setProperty("main-class", "com.example.NonExistent"); |
| 118 | + |
| 119 | + // ...but don't create the file |
| 120 | + MainClassExistsRule rule = new MainClassExistsRuleMock(project); |
| 121 | + |
| 122 | + // ...and assert an Exception is thrown. |
| 123 | + Assert.assertThrows(EnforcerRuleException.class, rule::execute); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Helper method to create a .class file at the appropriate location |
| 128 | + * based on the fully qualified class name. |
| 129 | + */ |
| 130 | + private void createClassFile(String fullyQualifiedClassName) throws IOException { |
| 131 | + String classFilePath = fullyQualifiedClassName.replace('.', File.separatorChar) + ".class"; |
| 132 | + File classFile = new File(outputDirectory, classFilePath); |
| 133 | + |
| 134 | + // Create parent directories if needed |
| 135 | + classFile.getParentFile().mkdirs(); |
| 136 | + |
| 137 | + // Create the .class file |
| 138 | + assertTrue("Failed to create class file: " + classFile.getAbsolutePath(), |
| 139 | + classFile.createNewFile()); |
| 140 | + } |
| 141 | +} |
0 commit comments