diff --git a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java index ff9e5257..499bddfd 100644 --- a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java +++ b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java @@ -3,6 +3,7 @@ import com.github.minecraft_ta.totalDebugCompanion.bytecode.RuntimeSnapshotBytecodeSource.Source; import com.github.minecraft_ta.totaldebug.storage.AtomicFiles; import com.github.minecraft_ta.totaldebug.storage.JsonFiles; +import com.github.minecraft_ta.totaldebug.storage.RuntimeInventory.ModuleKind; import com.github.minecraft_ta.totaldebug.storage.RuntimeInventory.RuntimeModule; import com.github.tth05.jindex.ClassIndex; import com.google.gson.JsonArray; @@ -119,6 +120,9 @@ static Manifest read(Path file) throws IOException { if (JsonFiles.integer(json, "format") != FORMAT) { throw new IOException("Unsupported runtime index format: " + file); } + if (!javaVersion().equals(JsonFiles.string(json, "javaVersion"))) { + throw new IOException("The index holds the classes of another Java runtime: " + JsonFiles.string(json, "javaVersion")); + } List sources = new ArrayList<>(); var ids = new HashSet(); for (var value : JsonFiles.array(json, "sources")) { @@ -154,12 +158,26 @@ static void requireSourcePaths(Manifest manifest) throws IOException { if (!Files.isRegularFile(path) && !Files.isDirectory(path)) { throw new IOException("Prepared runtime source is unavailable: " + path); } + if (source.module().kind() == ModuleKind.JAVA_RUNTIME && !path.equals(javaHome())) { + throw new IOException("The index holds the classes of another Java runtime: " + path); + } } } + /** The Java runtime whose classes an index holds: the one Companion runs on. */ + static Path javaHome() { + return Path.of(System.getProperty("java.home")).toAbsolutePath().normalize(); + } + + /** The version and build of that runtime, which an update in place changes. */ + static String javaVersion() { + return System.getProperty("java.runtime.version"); + } + private static JsonObject toJson(Manifest manifest) { JsonObject json = new JsonObject(); json.addProperty("format", FORMAT); + json.addProperty("javaVersion", javaVersion()); json.addProperty("sourceKind", manifest.identity().kind().name()); json.addProperty("sourceIdentity", manifest.identity().value()); if (!manifest.detail().isBlank()) json.addProperty("detail", manifest.detail()); diff --git a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java index 1f3d499f..07445534 100644 --- a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java +++ b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java @@ -353,7 +353,7 @@ private void buildOrLoad(Work work) { } checkpoint(work); update(work, new Status(Phase.BUILDING, "Building class index", null)); - return buildSnapshot(work, prepareInputs(inventory), IndexIdentity.runtime(inventory.id()), indexFile, inventory.javaHome()); + return buildSnapshot(work, prepareInputs(inventory), IndexIdentity.runtime(inventory.id()), indexFile); }); publishReady(work, ready); } catch (CancellationException ignored) { @@ -424,7 +424,7 @@ private void buildOrLoadLocal(Work work) throws IOException { update(work, new Status(Phase.BUILDING, work.detail + "Building local mod index", null)); var prepared = LocalModSources.prepare(scan, () -> checkpoint(work)); work.sourceDetail = prepared.detail(); - return buildSnapshot(work, prepared.inputs(), identity, indexFile, System.getProperty("java.home")); + return buildSnapshot(work, prepared.inputs(), identity, indexFile); }); publishReady(work, ready); } @@ -433,8 +433,7 @@ private ReadySnapshot buildSnapshot( Work work, List prepared, IndexIdentity identity, - Path indexFile, - String javaHome + Path indexFile ) throws IOException { AtomicFiles.cleanupAbandonedStaging(indexFile.getParent()); checkpoint(work); @@ -459,9 +458,10 @@ private ReadySnapshot buildSnapshot( try (var phase = RuntimePhase.start("index.jdk-inputs")) { addJdkClasses(jdkSourceId, indexSources); } + // The JDK read is Companion's own, which the mod starts on the game's Java; a cache records it (IndexCache). publishedSources.add(new RuntimeSnapshotBytecodeSource.Source( jdkSourceId, - Path.of(javaHome), + IndexCache.javaHome(), "jrt:/", new RuntimeInventory.RuntimeModule( "java-runtime", diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java index 821c375c..a1969814 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java @@ -25,7 +25,8 @@ class CompanionRenderingDefaultsTest { var builder = new ProcessBuilder(command).redirectErrorStream(true); for (String name : List.of("JAVA_TOOL_OPTIONS", "JDK_JAVA_OPTIONS", "_JAVA_OPTIONS")) builder.environment().remove(name); Process process = builder.start(); - boolean finished = process.waitFor(15, TimeUnit.SECONDS); + // A JVM on the whole test class path starts slowly on a loaded runner; the wait ends when it exits. + boolean finished = process.waitFor(60, TimeUnit.SECONDS); if (!finished) process.destroyForcibly().waitFor(); String output = new String(process.getInputStream().readAllBytes()); assertTrue(finished, output); diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java index cd62c1e8..6d84b58f 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java @@ -119,8 +119,8 @@ void aNavigationShowingAHiddenLivePageReadsItOnce() throws Exception { UiTestScope.showPages(panel); panel.session().refresh(); }); - Thread.sleep(500); // A read asks for the snippets twice: whether the side can run, then to run. + UiTestScope.await(() -> reads.get() >= asked + 2); assertEquals(asked + 2, reads.get(), "the navigation's read answers the read the live interval asked for"); } finally { UiTestScope.onEdt(panel::dispose); diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java index ca365ef4..9941f5eb 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java @@ -45,4 +45,48 @@ void roundTripsNativeIndexAndSourceIdentityAsOneFile() throws Exception { assertThrows(IOException.class, () -> IndexCache.requireSources(manifest)); } } + + @Test + void anIndexOfAnotherJavaRuntimeIsNotUsed() throws Exception { + var java = new RuntimeInventory.RuntimeModule("java-runtime", "Java Runtime", RuntimeInventory.ModuleKind.JAVA_RUNTIME); + IndexCache.requireSourcePaths(new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, IndexCache.javaHome(), "jrt:/", java)))); + var other = new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, this.home, "jrt:/", java))); + assertThrows(IOException.class, () -> IndexCache.requireSourcePaths(other)); + } + + @Test + void anIndexWrittenOnAnotherBuildOfTheJavaRuntimeIsNotUsed() throws Exception { + Path file = this.home.resolve("index.jindex"); + var java = new RuntimeInventory.RuntimeModule("java-runtime", "Java Runtime", RuntimeInventory.ModuleKind.JAVA_RUNTIME); + var manifest = new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, IndexCache.javaHome(), "jrt:/", java))); + try (var input = IndexCacheTest.class.getResourceAsStream("IndexCacheTest.class"); + ClassIndex index = ClassIndex.fromBytes(List.of(input.readAllBytes())); + ClassIndex ignored = IndexCache.write(file, index, manifest)) { + assertEquals(manifest, IndexCache.read(file)); + } + String version = System.getProperty("java.runtime.version"); + // The same path after an update in place. + System.setProperty("java.runtime.version", version + "-updated"); + try { + assertThrows(IOException.class, () -> IndexCache.read(file)); + } finally { + System.setProperty("java.runtime.version", version); + } + } + + @Test + void aJavaHomeWrittenWithRedundantPartsIsTheSameRuntime() throws Exception { + var java = new RuntimeInventory.RuntimeModule("java-runtime", "Java Runtime", RuntimeInventory.ModuleKind.JAVA_RUNTIME); + String property = System.getProperty("java.home"); + System.setProperty("java.home", Path.of(property, ".").toString()); + try { + IndexCache.requireSourcePaths(new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, IndexCache.javaHome(), "jrt:/", java)))); + } finally { + System.setProperty("java.home", property); + } + } } diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java index ccfc082f..5c2aa1a4 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java @@ -10,6 +10,7 @@ import com.github.minecraft_ta.totalDebugCompanion.navigation.WorldTab; import com.github.minecraft_ta.totalDebugCompanion.project.ProjectScope; import com.github.minecraft_ta.totalDebugCompanion.runtime.RuntimeIndexService; +import com.github.minecraft_ta.totalDebugCompanion.runtime.RuntimeTestSources; import com.github.minecraft_ta.totalDebugCompanion.session.CompanionLaunchConfiguration; import com.github.minecraft_ta.totalDebugCompanion.session.CompanionProfile; import com.github.minecraft_ta.totalDebugCompanion.storage.ChangeRecord; @@ -423,8 +424,7 @@ void theFirstAndTheLastChangeAddAndRemoveTheModpackRootAlone() throws Exception private static void assertReveals(FileTreeView tree, Callable> reveal, String name) throws Exception { CompletableFuture revealed = UiTestScope.onEdt(reveal); assertTrue(revealed.get(10, TimeUnit.SECONDS), name + " is revealed"); - settle(); - assertEquals(name, UiTestScope.onEdt(() -> selectedName(tree)), name + " is selected"); + UiTestScope.await(() -> name.equals(selectedName(tree))); } /** The catalog of a pack with one mod, {@code testmod}, ready. */ @@ -434,12 +434,21 @@ private void withCatalog(ProjectScope scope) throws Exception { settle(); } - /** A mod archive in the game's {@code mods} folder, which the Runtime root lists before a game runs. */ + /** + * A mod archive in the game's {@code mods} folder, which the Runtime root lists before a game runs, with its index + * already cached: the project loads it rather than indexing the JDK, which none of these tests is about. + */ private static void modJar(Path game) throws Exception { try (var zip = new ZipOutputStream(Files.newOutputStream(Files.createDirectories(game.resolve("mods")).resolve("sample.jar")))) { zip.putNextEntry(new ZipEntry("pack.mcmeta")); zip.write("{}".getBytes(StandardCharsets.UTF_8)); + String fixture = Before.class.getName().replace('.', '/') + ".class"; + zip.putNextEntry(new ZipEntry(fixture)); + try (var bytes = ProjectTreeTest.class.getClassLoader().getResourceAsStream(fixture)) { + bytes.transferTo(zip); + } } + RuntimeTestSources.writeLocalCache(game); } /** A script in a folder of the instance's Scripts. */ diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java index 81997940..b9aa2cb3 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java @@ -28,8 +28,9 @@ void firstConstructionDoesNotDeadlockAwtClassInitialization() throws Exception { "-cp", System.getProperty("java.class.path"), Probe.class.getName()) .redirectErrorStream(true).redirectOutput(output.toFile()).start(); try { - assertTrue(process.waitFor(5, TimeUnit.SECONDS), - () -> "Popup initialization did not finish within five seconds: " + output); + // A deadlock never ends; a minute leaves a slow start on a loaded runner its time. + assertTrue(process.waitFor(60, TimeUnit.SECONDS), + () -> "Popup initialization did not finish within a minute: " + output); assertEquals(0, process.exitValue(), Files.readString(output)); } finally { if (process.isAlive()) process.destroyForcibly().waitFor();