How to use method of org.jmock.test.acceptance.MockeryFinalizationAcceptanceTests class

Best Jmock-library code snippet using org.jmock.test.acceptance.MockeryFinalizationAcceptanceTests.

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.jmock.test.acceptance;
2
3import static org.hamcrest.Matchers.isEmptyOrNullString;
4import static org.junit.Assert.assertThat;
5
6import java.io.ByteArrayOutputStream;
7import java.io.PrintStream;
8import java.lang.ref.WeakReference;
9
10import org.jmock.Mockery;
11import org.jmock.api.Imposteriser;
12import org.jmock.lib.concurrent.Synchroniser;
13import org.jmock.test.unit.lib.legacy.CodeGeneratingImposteriserParameterResolver;
14import org.jmock.test.unit.lib.legacy.ImposteriserParameterResolver;
15import org.junit.jupiter.api.AfterEach;
16import org.junit.jupiter.api.BeforeAll;
17import org.junit.jupiter.api.BeforeEach;
18import org.junit.jupiter.params.ParameterizedTest;
19import org.junit.jupiter.params.provider.ArgumentsSource;
20/**
21 * Nasty test to show GitHub #36 is fixed.
22 */
23public class MockeryFinalizationAcceptanceTests
24{
25    private static final int FINALIZE_COUNT = 10; // consistently shows a problem before GitHub #36 was fixed
26
27    private final Mockery mockery = new Mockery() {{
28        setThreadingPolicy(new Synchroniser());
29    }};
30    private final ErrorStream capturingErr = new ErrorStream();
31
32    @BeforeAll
33    public static void clearAnyOutstandingMessages() {
34        ErrorStream localErr = new ErrorStream();
35        localErr.install();
36        String error = null;
37        try {
38            finalizeUntilMessageOrCount(localErr, FINALIZE_COUNT);
39            error = localErr.output();
40        } finally {
41            localErr.uninstall();
42        }
43        if (error != null)
44            System.err.println("WARNING - a previous test left output in finalization [" + error + "]");
45    }
46
47    @BeforeEach
48    public void captureSysErr() {
49        capturingErr.install();
50    }
51
52    @AfterEach
53    public void replaceSysErr() {
54        capturingErr.uninstall();
55    }
56
57    @ParameterizedTest
58    @ArgumentsSource(ImposteriserParameterResolver.class)
59    public void mockedInterfaceDoesntWarnOnFinalize(Imposteriser imposterImpl) {
60        mockery.setImposteriser(imposterImpl);
61        
62        checkNoFinalizationMessage(mockery, CharSequence.class);
63    }
64
65    @ParameterizedTest
66    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
67    public void mockedClassDoesntWarnOnFinalize(Imposteriser imposterImpl) {
68        mockery.setImposteriser(imposterImpl);
69
70        checkNoFinalizationMessage(mockery, Object.class);
71    }
72
73    public interface TypeThatMakesFinalizePublic {
74        public void finalize();
75    }
76
77    @ParameterizedTest
78    @ArgumentsSource(ImposteriserParameterResolver.class)
79    public void mockedTypeThatMakesFinalizePublicDoesntWarnOnFinalize(Imposteriser imposterImpl) {
80        mockery.setImposteriser(imposterImpl);
81
82        checkNoFinalizationMessage(mockery, TypeThatMakesFinalizePublic.class);
83    }
84
85    private void checkNoFinalizationMessage(Mockery mockery, Class<?> typeToMock) {
86        WeakReference<Object> mockHolder = new WeakReference<Object>(mockery.mock(typeToMock));
87        while (mockHolder.get() != null) {
88            System.gc();
89            System.runFinalization();
90        }
91        finalizeUntilMessageOrCount(capturingErr, FINALIZE_COUNT);
92        assertThat(capturingErr.output(), isEmptyOrNullString());
93    }
94
95
96    private static void finalizeUntilMessageOrCount(ErrorStream capturingErr, int count) {
97        for (int i = 0; i < count && capturingErr.output().isEmpty(); i++) {
98            System.gc();
99            System.runFinalization();
100        }
101    }
102
103    private static class ErrorStream extends PrintStream {
104
105        private PrintStream oldSysErr;
106
107        public ErrorStream() {
108            super(new ByteArrayOutputStream());
109        }
110
111        public void install() {
112            oldSysErr = System.err;
113            System.setErr(this);
114        }
115
116        public void uninstall() {
117            System.setErr(oldSysErr);
118        }
119
120        public String output() {
121            return new String(((ByteArrayOutputStream) out).toByteArray());
122        }
123    }
124}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)