|
| 1 | +/* |
| 2 | + * Copyright 2025 DiffPlug |
| 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 com.diffplug.spotless; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.ObjectInputStream; |
| 22 | +import java.io.ObjectOutputStream; |
| 23 | +import java.net.URLClassLoader; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import org.assertj.core.api.SoftAssertions; |
| 28 | +import org.junit.jupiter.api.AfterEach; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.io.TempDir; |
| 32 | + |
| 33 | +class JarStateTest { |
| 34 | + |
| 35 | + @TempDir |
| 36 | + java.nio.file.Path tempDir; |
| 37 | + |
| 38 | + File a; |
| 39 | + |
| 40 | + File b; |
| 41 | + |
| 42 | + Provisioner provisioner = (withTransitives, deps) -> deps.stream().map(name -> name.equals("a") ? a : b).collect(Collectors.toSet()); |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() throws IOException { |
| 46 | + a = Files.createTempFile(tempDir, "a", ".class").toFile(); |
| 47 | + Files.writeString(a.toPath(), "a"); |
| 48 | + b = Files.createTempFile(tempDir, "b", ".class").toFile(); |
| 49 | + Files.writeString(b.toPath(), "b"); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + void tearDown() { |
| 54 | + JarState.setForcedClassLoader(null); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void itCreatesClassloaderWhenForcedClassLoaderNotSet() throws IOException { |
| 59 | + JarState state1 = JarState.from(a.getName(), provisioner); |
| 60 | + JarState state2 = JarState.from(b.getName(), provisioner); |
| 61 | + |
| 62 | + SoftAssertions.assertSoftly(softly -> { |
| 63 | + softly.assertThat(state1.getClassLoader()).isNotNull(); |
| 64 | + softly.assertThat(state2.getClassLoader()).isNotNull(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void itReturnsForcedClassloaderIfSetNoMatterIfSetBeforeOrAfterCreation() throws IOException { |
| 70 | + JarState stateA = JarState.from(a.getName(), provisioner); |
| 71 | + ClassLoader forcedClassLoader = new URLClassLoader(new java.net.URL[0]); |
| 72 | + JarState.setForcedClassLoader(forcedClassLoader); |
| 73 | + JarState stateB = JarState.from(b.getName(), provisioner); |
| 74 | + |
| 75 | + SoftAssertions.assertSoftly(softly -> { |
| 76 | + softly.assertThat(stateA.getClassLoader()).isSameAs(forcedClassLoader); |
| 77 | + softly.assertThat(stateB.getClassLoader()).isSameAs(forcedClassLoader); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void itReturnsForcedClassloaderEvenWhenRountripSerialized() throws IOException, ClassNotFoundException { |
| 83 | + JarState stateA = JarState.from(a.getName(), provisioner); |
| 84 | + ClassLoader forcedClassLoader = new URLClassLoader(new java.net.URL[0]); |
| 85 | + JarState.setForcedClassLoader(forcedClassLoader); |
| 86 | + JarState stateB = JarState.from(b.getName(), provisioner); |
| 87 | + |
| 88 | + JarState stateARoundtripSerialized = roundtripSerialize(stateA); |
| 89 | + JarState stateBRoundtripSerialized = roundtripSerialize(stateB); |
| 90 | + |
| 91 | + SoftAssertions.assertSoftly(softly -> { |
| 92 | + softly.assertThat(stateARoundtripSerialized.getClassLoader()).isSameAs(forcedClassLoader); |
| 93 | + softly.assertThat(stateBRoundtripSerialized.getClassLoader()).isSameAs(forcedClassLoader); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + private JarState roundtripSerialize(JarState state) throws IOException, ClassNotFoundException { |
| 98 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 99 | + try (ObjectOutputStream oOut = new ObjectOutputStream(outputStream)) { |
| 100 | + oOut.writeObject(state); |
| 101 | + } |
| 102 | + try (ObjectInputStream oIn = new ObjectInputStream(new java.io.ByteArrayInputStream(outputStream.toByteArray()))) { |
| 103 | + return (JarState) oIn.readObject(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments