|
| 1 | +/* |
| 2 | + * Tai-e: A Static Analysis Framework for Java |
| 3 | + * |
| 4 | + * Copyright (C) 2022 Tian Tan <[email protected]> |
| 5 | + * Copyright (C) 2022 Yue Li <[email protected]> |
| 6 | + * |
| 7 | + * This file is part of Tai-e. |
| 8 | + * |
| 9 | + * Tai-e is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License |
| 11 | + * as published by the Free Software Foundation, either version 3 |
| 12 | + * of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * Tai-e is distributed in the hope that it will be useful,but WITHOUT |
| 15 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
| 17 | + * Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with Tai-e. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +package pascal.taie.integration; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.BeforeAll; |
| 26 | +import org.junit.jupiter.api.DisplayName; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.io.TempDir; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.nio.file.Files; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 36 | + |
| 37 | +class FatJarTests { |
| 38 | + |
| 39 | + @TempDir |
| 40 | + private static File tempDir; |
| 41 | + |
| 42 | + private static FatJarRunner jarRunner; |
| 43 | + |
| 44 | + private static File rootProjectDir; |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + static void setupClass() { |
| 48 | + String jarPath = System.getProperty("tai-e.jar.path"); |
| 49 | + assertNotNull(jarPath, "System property 'tai-e.jar.path'" |
| 50 | + + " must be set to the path of the fat JAR"); |
| 51 | + rootProjectDir = new File(System.getProperty("tai-e.project.path")); |
| 52 | + assertTrue(rootProjectDir.exists() && rootProjectDir.isDirectory(), |
| 53 | + "System property 'tai-e.project.path' must point to the root of the Tai-e project"); |
| 54 | + String javaPath = System.getProperty("java.executable.path"); |
| 55 | + assertNotNull(javaPath, "System property 'java.executable.path'" |
| 56 | + + " must be set to the path of the Java executable"); |
| 57 | + jarRunner = new FatJarRunner(javaPath, jarPath, tempDir); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @DisplayName("Should display usage with no arguments") |
| 62 | + void testNoArguments() throws Exception { |
| 63 | + ProcessResult result = jarRunner.run(); |
| 64 | + assertTrue(result.isSuccess()); |
| 65 | + assertTrue(result.stdout().contains("Usage: Options")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @DisplayName("Should display usage with -h") |
| 70 | + void testHelp() throws Exception { |
| 71 | + ProcessResult result = jarRunner.run("-h"); |
| 72 | + assertTrue(result.isSuccess()); |
| 73 | + assertTrue(result.stdout().contains("Usage: Options")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @DisplayName("Should handle invalid command") |
| 78 | + void testInvalidCommand() throws Exception { |
| 79 | + ProcessResult result = jarRunner.run("invalid"); |
| 80 | + assertFalse(result.isSuccess()); |
| 81 | + // TODO: should print usage message |
| 82 | +// assertTrue(result.stdout().contains("Usage: Options")); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DisplayName("Should write tai-e.log and options.yml files") |
| 87 | + void testLogFile() throws Exception { |
| 88 | + ProcessResult result = jarRunner.run("-pp"); |
| 89 | + // Check if the process ran successfully |
| 90 | + assertTrue(result.isSuccess()); |
| 91 | + // Check if the log file is created |
| 92 | + File logFile = new File(tempDir, "output/tai-e.log"); |
| 93 | + assertTrue(logFile.exists(), "Log file should be created"); |
| 94 | + // Check if the log file is not empty |
| 95 | + String logContent = new String(Files.readAllBytes(logFile.toPath())); |
| 96 | + assertFalse(logContent.isEmpty(), "Log file should not be empty"); |
| 97 | + // Check if the log file contains expected content |
| 98 | + assertTrue(logContent.contains("Writing log to")); |
| 99 | + assertTrue(result.stdout().contains("Writing log to")); |
| 100 | + // Check if the options.yml file is created |
| 101 | + File optionsFile = new File(tempDir, "output/options.yml"); |
| 102 | + assertTrue(optionsFile.exists(), "Options file should be created"); |
| 103 | + // Check if the options.yml file is not empty |
| 104 | + String optionsContent = new String(Files.readAllBytes(optionsFile.toPath())); |
| 105 | + assertFalse(optionsContent.isEmpty(), "Options file should not be empty"); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments