How to use method of org.jmock.lib.LastWordNamingScheme class

Best Jmock-library code snippet using org.jmock.lib.LastWordNamingScheme.

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.unit.lib;
2
3import java.lang.reflect.InvocationHandler;
4import java.net.URL;
5import java.util.Collection;
6import java.util.ListIterator;
7
8import junit.framework.TestCase;
9
10import org.jmock.api.MockObjectNamingScheme;
11import org.jmock.lib.LastWordNamingScheme;
12
13public class LastWordNamingSchemeTests extends TestCase {
14    MockObjectNamingScheme namingScheme = LastWordNamingScheme.INSTANCE;
15    
16    
17    public void testNamesMocksByLowercasingTheLastWordOfTheTypeName() {
18        assertEquals("iterator", namingScheme.defaultNameFor(ListIterator.class));
19        assertEquals("handler", namingScheme.defaultNameFor(InvocationHandler.class));
20    }
21    
22    
23    public void testLowercasesEntireNameIfItContainsOnlyOneWord() {
24        assertEquals("runnable", namingScheme.defaultNameFor(Runnable.class));
25        assertEquals("collection", namingScheme.defaultNameFor(Collection.class));
26    }
27
28    public interface X {}
29    public void testLowercasesNameContainingSingleUpperCaseLetter() {
30        assertEquals("x", namingScheme.defaultNameFor(X.class));
31    }
32    
33    public interface y {}
34    public void testLowercasesNameContainingSingleLowerCaseLetter() {
35        assertEquals("y", namingScheme.defaultNameFor(y.class));
36    }
37    
38    public void testLowercasesEntireNameIfItContainsOnlyCapitals() {
39        assertEquals("url", namingScheme.defaultNameFor(URL.class));
40    }
41    
42    public interface Party1999 {}
43    public interface NMEA0183 {} // standard for sending GPS data over serial lines
44    
45    public void testAllowsNamesToEndInNumbers() {
46        assertEquals("party1999", namingScheme.defaultNameFor(Party1999.class));
47        assertEquals("nmea0183", namingScheme.defaultNameFor(NMEA0183.class));
48    }
49    
50    public interface TelnetURL {}
51    
52    public void testReturnsTrailingAcronymInLowerCase() {
53        assertEquals("url", namingScheme.defaultNameFor(TelnetURL.class));
54    }
55}
56
Full Screen
copy
1package org.jmock.lib;
2
3import static java.lang.Character.isLowerCase;
4import static java.lang.Character.isUpperCase;
5import static java.lang.Math.max;
6
7import org.jmock.api.MockObjectNamingScheme;
8
9/**
10 * A naming scheme in which the implicit name for a mock object is
11 * the last word of the mocked type's name in lower case.
12 * E.g. A mock object of type HelloWorld would be named "world".
13 * If the type name is an acronym it is completely lowercased.  
14 * For example, the type URL would be named "url".  Digits and other
15 * non-letter characters are left on the end of the word.  For
16 * example, the type NMEA0183 would be named "nmea0183".
17 * 
18 * @author npryce
19 */
20public class LastWordNamingScheme implements MockObjectNamingScheme {
21    public static MockObjectNamingScheme INSTANCE = new LastWordNamingScheme();
22    
23    public String defaultNameFor(Class<?> typeToMock) {
24        String simpleName = typeToMock.getSimpleName();
25        int wordEnd = indexOfLastLetter(simpleName);
26        
27        int start;
28        if (isUpperCase(simpleName.charAt(wordEnd))) {
29            start = indexOfLastLowerCaseChar(simpleName, wordEnd)+1;
30        } else {
31            start = indexOfLastUpperCaseChar(simpleName, wordEnd);
32        }
33        
34        return simpleName.substring(max(0,start)).toLowerCase();
35    }
36    
37    private int indexOfLastLetter(String simpleName) {
38        int i = simpleName.length()-1;
39        while (i > 0 && !Character.isLetter(simpleName.charAt(i))) {
40            i--;
41        }
42        return i;
43    }
44    
45    private int indexOfLastUpperCaseChar(String simpleName, int wordEnd) {
46        int i = wordEnd;
47        while (i >= 0 && isLowerCase(simpleName.charAt(i))) {
48            i--;
49        }
50        return i;
51    }
52    
53    private int indexOfLastLowerCaseChar(String simpleName, int wordEnd) {
54        int i = wordEnd;
55        while (i >= 0 && isUpperCase(simpleName.charAt(i))) {
56            i--;
57        }
58        return i;
59    }
60    
61    private LastWordNamingScheme() {}
62}
63
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)