Skip to content

Commit

Permalink
Use temporary folder rule in tests rather than temporary files.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Sep 22, 2024
1 parent b6d7c21 commit fff9e87
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ class ForFolder implements ClassFileLocator {
*/
public ForFolder(File folder) throws IOException {
this.folder = folder;
int current = ClassFileVersion.ofThisVm().getMajorVersion();
int current = ClassFileVersion.ofThisVm().getJavaVersion();
if (current < 9) {
multiRelease = new int[0];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import net.bytebuddy.dynamic.scaffold.inline.MethodNameTransformer;
import net.bytebuddy.implementation.LoadedTypeInitializer;
import net.bytebuddy.matcher.ElementMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.ArgumentMatchers;
Expand Down Expand Up @@ -48,6 +50,9 @@ public static Collection<Object[]> data() {
});
}

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private final Plugin.Engine.Dispatcher.Factory dispatcherFactory;

private final boolean eager;
Expand Down Expand Up @@ -317,7 +322,7 @@ public void testManifest() throws Exception {

@Test
public void testImplicitFileInput() throws Exception {
File file = File.createTempFile("foo", "bar");
File file = temporaryFolder.newFile();
JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(file));
try {
outputStream.putNextEntry(new JarEntry("dummy"));
Expand All @@ -342,9 +347,7 @@ public Plugin.Engine.Summary answer(InvocationOnMock invocationOnMock) {

@Test
public void testImplicitFolderInput() throws Exception {
File file = File.createTempFile("foo", "bar");
assertThat(file.delete(), is(true));
assertThat(file.mkdir(), is(true));
File file = temporaryFolder.newFolder();
Plugin.Engine engine = spy(new Plugin.Engine.Default());
doAnswer(new Answer<Plugin.Engine.Summary>() {
public Plugin.Engine.Summary answer(InvocationOnMock invocationOnMock) {
Expand Down Expand Up @@ -376,8 +379,7 @@ public void testOfEntryPoint() {

@Test
public void testMain() throws Exception {
File source = File.createTempFile("foo", "bar"), target = File.createTempFile("qux", "baz");
assertThat(target.delete(), is(true));
File source = temporaryFolder.newFile(), target = temporaryFolder.newFile();
JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(source));
try {
outputStream.putNextEntry(new JarEntry("dummy"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.bytebuddy.build;

import net.bytebuddy.utility.StreamDrainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -19,18 +20,14 @@

public class PluginEngineSourceForFolderTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File folder;

@Before
public void setUp() throws Exception {
folder = File.createTempFile("foo", "bar");
assertThat(folder.delete(), is(true));
assertThat(folder.mkdir(), is(true));
}

@After
public void tearDown() throws Exception {
assertThat(folder.delete(), is(true));
folder = temporaryFolder.newFolder();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import net.bytebuddy.utility.StreamDrainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -16,16 +18,14 @@

public class PluginEngineSourceForJarFileTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File file;

@Before
public void setUp() throws Exception {
file = File.createTempFile("foo", "bar");
}

@After
public void tearDown() throws Exception {
assertThat(file.delete(), is(true));
file = temporaryFolder.newFile();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.utility.StreamDrainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.rules.TemporaryFolder;

import java.io.*;
import java.util.Collections;
Expand All @@ -26,18 +26,14 @@ public class PluginEngineTargetForFolderTest {
@Rule
public MethodRule javaVersionRule = new JavaVersionRule();

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File folder;

@Before
public void setUp() throws Exception {
folder = File.createTempFile("foo", "bar");
assertThat(folder.delete(), is(true));
assertThat(folder.mkdir(), is(true));
}

@After
public void tearDown() throws Exception {
assertThat(folder.delete(), is(true));
folder = temporaryFolder.newFolder();
}

@Test
Expand Down Expand Up @@ -92,7 +88,7 @@ public void testWriteResourceFromFile() throws Exception {
Plugin.Engine.Source.Element element = mock(Plugin.Engine.Source.Element.class);
when(element.getName()).thenReturn(FOO + "/" + BAR);
when(element.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[]{1, 2, 3}));
File original = File.createTempFile("qux", "baz");
File original = temporaryFolder.newFile();
try {
FileOutputStream outputStream = new FileOutputStream(original);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.utility.StreamDrainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -25,16 +26,14 @@ public class PluginEngineTargetForJarFileTest {

private static final String FOO = "foo", BAR = "bar";

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File file;

@Before
public void setUp() throws Exception {
file = File.createTempFile("foo", "bar");
assertThat(file.delete(), is(true));
}

@After
public void tearDown() throws Exception {
file = temporaryFolder.newFile();
assertThat(file.delete(), is(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -24,19 +25,14 @@ public class ClassFileLocatorForFolderTest {
@Rule
public JavaVersionRule javaVersionRule = new JavaVersionRule();

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File folder;

@Before
public void setUp() throws Exception {
File file = File.createTempFile(FOO, BAR);
assertThat(file.delete(), is(true));
folder = new File(file.getParentFile(), FOO + new Random().nextInt());
assertThat(folder.mkdir(), is(true));
}

@After
public void tearDown() throws Exception {
assertThat(folder.delete(), is(true));
folder = temporaryFolder.newFolder();
}

@Test
Expand All @@ -56,8 +52,6 @@ public void testSuccessfulLocation() throws Exception {
ClassFileLocator.Resolution resolution = classFileLocator.locate(FOO + "." + BAR);
assertThat(resolution.isResolved(), is(true));
assertThat(resolution.resolve(), is(new byte[]{VALUE, VALUE * 2}));
assertThat(file.delete(), is(true));
assertThat(packageFolder.delete(), is(true));
}

@Test
Expand All @@ -77,22 +71,17 @@ public void testClose() throws Exception {
public void testSuccessfulVersionLocation() throws Exception {
File metaInf = new File(folder, "META-INF");
assertThat(metaInf.mkdir(), is(true));
File manifestMf = new File(metaInf, "MANIFEST.MF");
Manifest manifest = new Manifest();
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
manifest.getMainAttributes().putValue("Multi-Release", "true");
OutputStream outputStream = new FileOutputStream(manifestMf);
OutputStream outputStream = new FileOutputStream(new File(metaInf, "MANIFEST.MF"));
try {
manifest.write(outputStream);
} finally {
outputStream.close();
}
File versions = new File(metaInf, "versions");
assertThat(versions.mkdir(), is(true));
File version = new File(versions, "9");
assertThat(version.mkdir(), is(true));
File packageFolder = new File(version, FOO);
assertThat(packageFolder.mkdir(), is(true));
File packageFolder = new File(metaInf, "versions/9/" + FOO);
assertThat(packageFolder.mkdirs(), is(true));
File file = new File(packageFolder, BAR + ".class");
assertThat(file.createNewFile(), is(true));
outputStream = new FileOutputStream(file);
Expand All @@ -106,11 +95,5 @@ public void testSuccessfulVersionLocation() throws Exception {
ClassFileLocator.Resolution resolution = classFileLocator.locate(FOO + "." + BAR);
assertThat(resolution.isResolved(), is(true));
assertThat(resolution.resolve(), is(new byte[]{VALUE, VALUE * 2}));
assertThat(file.delete(), is(true));
assertThat(packageFolder.delete(), is(true));
assertThat(version.delete(), is(true));
assertThat(versions.delete(), is(true));
assertThat(manifestMf.delete(), is(true));
assertThat(metaInf.delete(), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.rules.TemporaryFolder;
import org.objectweb.asm.ClassVisitor;

import java.io.File;
Expand All @@ -28,16 +29,14 @@ public class ClassFileLocatorForJarFileTest {
@Rule
public MethodRule javaVersionRule = new JavaVersionRule();

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File file;

@Before
public void setUp() throws Exception {
file = File.createTempFile(FOO, BAR);
}

@After
public void tearDown() throws Exception {
assertThat(file.delete(), is(true));
file = temporaryFolder.newFile();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package net.bytebuddy.dynamic;

import net.bytebuddy.test.utility.JavaVersionRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.rules.TemporaryFolder;

import java.io.Closeable;
import java.io.File;
Expand All @@ -27,16 +27,14 @@ public class ClassFileLocatorForModuleFileTest {
@Rule
public MethodRule javaVersionRule = new JavaVersionRule();

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File file;

@Before
public void setUp() throws Exception {
file = File.createTempFile(FOO, BAR);
}

@After
public void tearDown() throws Exception {
assertThat(file.delete(), is(true));
file = temporaryFolder.newFile();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package net.bytebuddy.dynamic;

import net.bytebuddy.test.utility.JavaVersionRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.rules.TemporaryFolder;

import java.io.Closeable;
import java.io.File;
Expand All @@ -28,16 +28,14 @@ public class ClassFileLocatorForUrlTest {
@Rule
public MethodRule javaVersionRule = new JavaVersionRule();

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File file;

@Before
public void setUp() throws Exception {
file = File.createTempFile(FOO, BAR);
}

@After
public void tearDown() throws Exception {
assertThat(file.delete(), is(true));
file = temporaryFolder.newFile();
}

@Test
Expand Down
Loading

0 comments on commit fff9e87

Please sign in to comment.