How to use method of org.easymock.tests2.EasyMockPropertiesTest class

Best Easymock code snippet using org.easymock.tests2.EasyMockPropertiesTest.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2001-2021 the original author or authors.
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 */
16package org.easymock.tests2;
17
18import org.easymock.EasyMock;
19import org.easymock.internal.EasyMockProperties;
20import org.junit.AfterClass;
21import org.junit.Before;
22import org.junit.BeforeClass;
23import org.junit.Test;
24
25import java.io.*;
26import java.lang.reflect.Field;
27
28import static org.easymock.EasyMock.*;
29import static org.junit.Assert.*;
30
31/**
32 * @author Henri Tremblay
33 */
34public class EasyMockPropertiesTest {
35
36    private static final String NORMAL_PATH = "target" + File.separatorChar + "test-classes";
37    private static final String CLOVER_PATH = "target" + File.separatorChar + "clover" + File.separatorChar + "test-classes";
38    private static final File PROPERTIES_FILE = new File(isCloverBuild() ? CLOVER_PATH : NORMAL_PATH, "easymock.properties");
39
40    /**
41     * Check that the test execution isn't ran by Clover. Because it it's the case, we need to generate the easymock.properties
42     * file in a different directory
43     */
44    private static boolean isCloverBuild() {
45        String surefireClasspath = System.getProperty("surefire.test.class.path");
46        if(surefireClasspath != null) {
47            return surefireClasspath.contains(CLOVER_PATH);
48        }
49        return false;
50    }
51
52    @BeforeClass
53    public static void beforeClass() throws Exception {
54        // Create an EasyMock property file for the test
55        BufferedWriter out = new BufferedWriter(new FileWriter(PROPERTIES_FILE));
56        out.write(EasyMock.ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT + "=" + true);
57        out.newLine();
58        out.write("easymock.a=1");
59        out.newLine();
60        out.write("easymock.b=2");
61        out.newLine();
62        out.write(EasyMock.NOT_THREAD_SAFE_BY_DEFAULT + "=" + true);
63        out.newLine();
64        out.close();
65    }
66
67    @AfterClass
68    public static void afterClass() throws Exception {
69        // Cleanup the mess
70        PROPERTIES_FILE.delete();
71
72        // Make sure we reset all our mess at the end
73        resetInstance();
74    }
75
76    @Before
77    public void setup() throws Exception {
78        // Make sure to reset to prevent getting an already initialized
79        // EasyMockProperties
80        resetInstance();
81
82        // Set manually a new one
83        setEasyMockProperty("easymock.e", "7");
84
85        // Set manually an old one
86        setEasyMockProperty("easymock.c", "8");
87
88        // Overload after (will be ignored)
89        System.setProperty("easymock.h", "4");
90    }
91
92    @Test
93    public void testGetInstance() {
94        assertExpectedValue("1", "easymock.a");
95        assertExpectedValue("8", "easymock.c");
96        assertExpectedValue("7", "easymock.e");
97        assertExpectedValue(null, "easymock.g");
98        assertExpectedValue(null, "easymock.h");
99        assertExpectedValue(null, "xxx.yyy");
100
101        assertExpectedValue(Boolean.TRUE.toString(), EasyMock.NOT_THREAD_SAFE_BY_DEFAULT);
102        assertExpectedValue(null, EasyMock.DISABLE_CLASS_MOCKING);
103    }
104
105    @Test
106    public void testGetProperty() {
107        EasyMockProperties instance = EasyMockProperties.getInstance();
108
109        // use the default
110        assertEquals("1", instance.getProperty("easymock.a", "10"));
111        // don't use the default
112        assertEquals("10", instance.getProperty("easymock.z", "10"));
113        // null default
114        assertNull(instance.getProperty("easymock.z", null));
115    }
116
117    @Test(expected = IllegalArgumentException.class)
118    public void testSetProperty() {
119        EasyMockProperties instance = EasyMockProperties.getInstance();
120
121        instance.setProperty("tralala.a", null);
122    }
123
124    @Test
125    public void testNoThreadContextClassLoader() throws Exception {
126        ClassLoader old = Thread.currentThread().getContextClassLoader();
127        try {
128            resetInstance();
129
130            // Remove the context class loader
131            Thread.currentThread().setContextClassLoader(null);
132
133            // This instance will load easymock.properties from the
134            // EasyMockProperties class loader
135            EasyMockProperties.getInstance();
136
137            // And so "easymock.a" should be there
138            assertExpectedValue("1", "easymock.a");
139
140        } finally {
141            // Whatever happens, set the initial class loader back or it'll get
142            // messy
143            Thread.currentThread().setContextClassLoader(old);
144        }
145    }
146
147    @Test
148    public void testBadPropertiesFile() throws Exception {
149
150        final boolean[] close = new boolean[1];
151
152        // A ClassLoader that returns no easymock.properties
153        ClassLoader cl = new ClassLoader(getClass().getClassLoader()) {
154
155            @Override
156            public InputStream getResourceAsStream(String name) {
157                if ("easymock.properties".equals(name)) {
158                    return new InputStream() {
159                        @Override
160                        public void close() {
161                            close[0] = true;
162                        }
163
164                        @Override
165                        public int read(byte[] b, int off, int len) throws IOException {
166                            throw new IOException("Failed!");
167                        }
168
169                        @Override
170                        public int read(byte[] b) throws IOException {
171                            throw new IOException("Failed!");
172                        }
173
174                        @Override
175                        public int read() throws IOException {
176                            throw new IOException("Failed!");
177                        }
178                    };
179                }
180                return super.getResourceAsStream(name);
181            }
182
183        };
184        ClassLoader old = Thread.currentThread().getContextClassLoader();
185        try {
186            resetInstance();
187
188            // Remove the context class loader
189            Thread.currentThread().setContextClassLoader(cl);
190
191            try {
192                EasyMockProperties.getInstance();
193                fail("Should have an issue loading the easymock.properties file");
194            } catch (RuntimeException e) {
195                assertEquals("Failed to read easymock.properties file", e.getMessage());
196                // Make sure the thread was closed
197                assertTrue(close[0]);
198            }
199
200        } finally {
201            // Whatever happens, set the initial class loader back or it'll get
202            // messy
203            Thread.currentThread().setContextClassLoader(old);
204        }
205    }
206
207    @Test
208    public void testNoEasymockPropertiesFile() throws Exception {
209        // A ClassLoader that returns no easymock.properties
210        ClassLoader cl = new ClassLoader(getClass().getClassLoader()) {
211
212            @Override
213            public InputStream getResourceAsStream(String name) {
214                if ("easymock.properties".equals(name)) {
215                    return null;
216                }
217                return super.getResourceAsStream(name);
218            }
219
220        };
221        ClassLoader old = Thread.currentThread().getContextClassLoader();
222        try {
223            resetInstance();
224
225            // Set our class loader
226            Thread.currentThread().setContextClassLoader(cl);
227
228            // This instance will try to load easymock.properties with our
229            // custom class loader and so won't find it
230            EasyMockProperties.getInstance();
231
232            // And so it shouldn't find "easymock.a"
233            assertExpectedValue(null, "easymock.a");
234
235        } finally {
236            // Whatever happens, set the initial class loader back or it'll get
237            // messy
238            Thread.currentThread().setContextClassLoader(old);
239        }
240    }
241
242    private static void resetInstance() throws NoSuchFieldException, IllegalAccessException {
243        // Cheat and make the singleton uninitialized
244        Field field = EasyMockProperties.class.getDeclaredField("instance");
245        field.setAccessible(true);
246        field.set(null, null);
247    }
248
249    private static void assertExpectedValue(String expected, String key) {
250        assertEquals(expected, getEasyMockProperty(key));
251    }
252}
253
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)