How to use getName method of org.junit.runners.ParentRunner class

Best junit code snippet using org.junit.runners.ParentRunner.getName

Run junit automation tests on LambdaTest cloud grid

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

copy
1package gosu.db.plugin;
2
3import gw.lang.Gosu;
4import gw.lang.reflect.*;
5import gw.lang.reflect.java.IJavaType;
6import gw.lang.reflect.java.JavaTypes;
7import org.junit.BeforeClass;
8import org.junit.Ignore;
9import org.junit.Test;
10
11import java.io.IOException;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Map;
15import java.util.Set;
16import java.util.stream.Collectors;
17import java.util.stream.Stream;
18
19import static org.junit.Assert.*;
20
21public class SQLPluginTest {
22
23  @BeforeClass
24  public static void beforeClass() {
25    Gosu.init();
26    ITypeLoader sqlPlugin = new SQLPlugin(TypeSystem.getCurrentModule()); // global vs. current?
27    TypeSystem.pushTypeLoader(TypeSystem.getGlobalModule(), sqlPlugin);
28  }
29
30  @Test
31  public void getDdlType() {
32    IType result = TypeSystem.getByFullNameIfValid("gosu.db.foo.Users");
33    assertNotNull(result);
34    assertEquals("gosu.db.foo.Users", result.getName());
35  }
36
37  @Test
38  public void getSQLType() {
39    IType result = TypeSystem.getByFullNameIfValid("gosu.db.foo.MyQuery");
40    assertNotNull(result);
41    assertEquals("gosu.db.foo.MyQuery", result.getName());
42  }
43
44  @Test
45  public void getTablesFromSelect() {
46    IType result = TypeSystem.getByFullNameIfValid("gosu.db.foo.MyQuery.Contact");
47    assertNull(result);
48  }
49
50  @Test
51  public void getTypeExplicitly() {
52    IType result = TypeSystem.getByFullNameIfValid("gosu.db.foo.Users.Contact");
53    assertNotNull(result);
54    assertEquals("gosu.db.foo.Users.Contact", result.getName());
55  }
56
57  @Test
58  public void getNamesFromSQL() {
59    ISQLQueryType result = (ISQLQueryType) TypeSystem.getByFullNameIfValid("gosu.db.foo.MyQuery");
60    assertNotNull(result);
61    assertEquals("gosu.db.foo.MyQuery", result.getName());
62    assertEquals("gosu.db.foo", result.getNamespace());
63    assertEquals("MyQuery", result.getRelativeName());
64  }
65
66  @Test
67  public void getNonExistantType() {
68    IType result = TypeSystem.getByFullNameIfValid("gosu.db.foo.Unknown.DoesNotExist");
69    assertNull(result);
70  }
71
72  @Test
73  public void testSQLExecute() {
74    ISQLQueryType result = (ISQLQueryType) TypeSystem.getByFullNameIfValid("gosu.db.foo.MyQuery");
75    assertNotNull(result);
76    SQLBaseTypeInfo ti = (SQLBaseTypeInfo) result.getTypeInfo();
77    IMethodInfo execute = ti.getMethod("execute", JavaTypes.STRING());
78    assertNotNull(execute);
79  }
80
81  @Test
82  public void testSQLResult() {
83    TypeSystem.getByFullNameIfValid("gosu.db.foo.Users");
84    ISQLQueryType result = (ISQLQueryType) TypeSystem.getByFullNameIfValid("gosu.db.foo.MyQuery2");
85    assertNotNull(result);
86    SQLBaseTypeInfo ti = (SQLBaseTypeInfo) result.getTypeInfo();
87    IMethodInfo execute = ti.getMethod("execute");
88    assertNotNull(execute);
89    IType[] typeParameters = execute.getReturnType().getTypeParameters();
90    ITypeInfo typeInfo = typeParameters[0].getTypeInfo();
91    int a = 0;
92  }
93
94
95  @Test
96  public void oneSourceWithMultipleTypes() {
97    IType result = TypeSystem.getByFullNameIfValid("gosu.db.foo.Vehicles.Car");
98    assertNotNull(result);
99    assertEquals("gosu.db.foo.Vehicles.Car", result.getName());
100
101    result = TypeSystem.getByFullNameIfValid("gosu.db.foo.Vehicles.Motorcycle");
102    assertNotNull(result);
103    assertEquals("gosu.db.foo.Vehicles.Motorcycle", result.getName());
104  }
105
106  @Test
107  public void getColumnDefs() {
108    ISQLTableType result = (ISQLTableType) TypeSystem.getByFullNameIfValid("gosu.db.foo.Users.Contact");
109    assertNotNull(result);
110
111    List<ColumnDefinition> colDefs = result.getColumnDefinitions();
112    assertNotNull(colDefs);
113
114    Set<String> expectedColumnNames = Stream.of("UserId", "LastName", "FirstName", "Age").collect(Collectors.toSet());
115    Set<String> actualColumnNames = colDefs.stream().map(ColumnDefinition::getColumnName).collect(Collectors.toSet());
116
117    assertEquals(expectedColumnNames, actualColumnNames);
118
119    //TODO check columndef positions in the source files
120  }
121
122
123  public void getTypeInfo() {
124    ISQLTableType result = (ISQLTableType) TypeSystem.getByFullNameIfValid("gosu.db.foo.Users.Contact");
125    assertNotNull(result);
126    assertEquals("gosu.db.foo.Users.Contact", result.getName());
127    assertEquals("gosu.db.foo.Users", result.getNamespace());
128    assertEquals("Contact", result.getRelativeName());
129
130    SQLBaseTypeInfo ti = (SQLBaseTypeInfo) result.getTypeInfo();
131    assertEquals("Contact", ti.getName());
132
133    //make a set of expected Name/IJavaType pairs
134    Set<String> expectedPropertyNames = Stream.of("UserId", "LastName", "FirstName", "Age").collect(Collectors.toSet());
135    Map<String, IJavaType> expectedPropertyNameAndType = new HashMap<>(expectedPropertyNames.size());
136
137    expectedPropertyNameAndType.put("UserId", JavaTypes.pINT());
138    expectedPropertyNameAndType.put("LastName", JavaTypes.STRING());
139    expectedPropertyNameAndType.put("FirstName", JavaTypes.STRING());
140    expectedPropertyNameAndType.put("Age", JavaTypes.pINT());
141
142    //number of properties is what we expect
143    assertEquals(expectedPropertyNameAndType.size(), ti.getProperties().size()); //TODO domain logic breaks this test
144
145    //each property name has a match in the map, and the type is identical
146    for(IPropertyInfo actualProp : ti.getProperties()) {
147      IJavaType expectedType = expectedPropertyNameAndType.get(actualProp.getName());
148      assertNotNull("expectedType was null, meaning the actualProp's name was not found in the map", expectedType);
149      assertSame(expectedType, actualProp.getFeatureType());
150    }
151  }
152
153  @Test
154  public void getMethodInfo() {
155    ISQLTableType result = (ISQLTableType) TypeSystem.getByFullNameIfValid("gosu.db.foo.Users.Contact");
156    assertNotNull(result);
157    assertEquals("gosu.db.foo.Users.Contact", result.getName());
158    assertEquals("gosu.db.foo.Users", result.getNamespace());
159    assertEquals("Contact", result.getRelativeName());
160
161    SQLBaseTypeInfo ti = (SQLBaseTypeInfo) result.getTypeInfo();
162    assertEquals("Contact", ti.getName());
163
164    IMethodInfo findByAge = ti.getMethod("findByAge", JavaTypes.pINT());
165    assertNotNull(findByAge);
166    assertEquals("gosu.db.foo.Users.Contact", findByAge.getReturnType().getName()); //returns single Contact
167
168    IMethodInfo findAllByAge = ti.getMethod("findAllByAge", JavaTypes.pINT());
169    assertNotNull(findAllByAge);
170    assertEquals("java.lang.Iterable<gosu.db.foo.Users.Contact>", findAllByAge.getReturnType().getName()); //returns Iterable of Contacts //TODO KB
171
172    IMethodInfo findByAgeWithWrongSignature = ti.getMethod("findByAge", JavaTypes.STRING());
173    assertNull(findByAgeWithWrongSignature);
174
175    IMethodInfo unknownMethodWithNoArgs = ti.getMethod("findSomethingWhichDoesNotExist");
176    assertNull(unknownMethodWithNoArgs);
177  }
178
179  @Test
180  @Ignore
181  public void getRawSQL() {
182    ISQLDdlType result = (ISQLDdlType) TypeSystem.getByFullNameIfValid("gosu.db.foo.Users");
183    assertNotNull(result);
184    assertEquals("gosu.db.foo.Users", result.getName());
185
186    String expectedSource = "CREATE TABLE CONTACTS (\n" +
187        "    UserId int,\n" +
188        "    FirstName nchar(50),\n" +
189        "    LastName nchar(50),\n" +
190        "    Age int\n" +
191        "    -- TODO add Gender\n" +
192        ");";
193    String actualSource = null;
194
195    try {
196      actualSource = result.getSqlSource();
197      System.out.println(actualSource);
198    } catch(IOException e) {
199      e.printStackTrace();
200      fail();
201    }
202
203    assertEquals(expectedSource, actualSource);
204
205    SQLBaseTypeInfo ti = (SQLBaseTypeInfo) result.getTypeInfo();
206    assertEquals("Users", ti.getName());
207
208    IPropertyInfo readOnlySqlSourceProperty = ti.getProperty("SqlSource");
209    assertNotNull(readOnlySqlSourceProperty);
210    assertTrue(readOnlySqlSourceProperty.isReadable());
211    assertFalse(readOnlySqlSourceProperty.isWritable());
212    assertEquals(expectedSource, readOnlySqlSourceProperty.getAccessor().getValue(null));
213  }
214
215  @Test
216 // @Ignore("Fails individually on result != null, but succeeds in the whole test")
217  public void getResultInfo() throws InterruptedException{
218    TypeSystem.getByFullName("gosu.db.foo.Users");
219    ISQLQueryType query = (ISQLQueryType)TypeSystem.getByFullName("gosu.db.foo.MyQuery2");
220    query.getTypeInfo();
221    ISQLQueryResultType expectedResult = query.getResultType();
222    assertNotNull(expectedResult);
223    ISQLQueryResultType result = (ISQLQueryResultType) TypeSystem.getByFullName("gosu.db.foo.MyQuery2Result");
224    assertEquals(result.getTable().getTable().getTypeName(), "Contact");
225  }
226
227  @Test
228  public void getSynthesizedMethod() {
229    ISQLTableType result = (ISQLTableType) TypeSystem.getByFullNameIfValid("gosu.db.foo.Bars.Baz");
230    assertNotNull(result);
231    assertEquals("gosu.db.foo.Bars.Baz", result.getName());
232    assertEquals("gosu.db.foo.Bars", result.getNamespace());
233    assertEquals("Baz", result.getRelativeName());
234
235    SQLTableTypeInfo ti = (SQLTableTypeInfo) result.getTypeInfo();
236    assertEquals("Baz", ti.getName());
237
238    IMethodInfo domainLogicMethod = ti.getMethod("sayHi", JavaTypes.STRING());
239    assertNotNull(domainLogicMethod);
240    assertEquals("void", domainLogicMethod.getReturnType().getName());
241    //String returnValue = ReflectUtil.invokeMethod(domainLogicMethod.getName(), "hello world");
242  }
243
244  @Test
245  public void domainLogicMustExtendSQLRecord() {
246    ISQLTableType baseType = (ISQLTableType) TypeSystem.getByFullName("gosu.db.foo.BadExamples.Invalid");
247    assertEquals("gosu.db.foo.BadExamples.Invalid", baseType.getName());
248    assertEquals("gosu.db.foo.BadExamples", baseType.getNamespace());
249    assertEquals("Invalid", baseType.getRelativeName());
250
251    //assert the extension type exists ...
252    IType extensionType = TypeSystem.getByFullName("gosu.db.foo.BadExampleExt.InvalidExt");
253    assertEquals("gosu.db.foo.BadExampleExt.InvalidExt", extensionType.getName());
254    assertEquals("gosu.db.foo.BadExampleExt", extensionType.getNamespace());
255    assertEquals("InvalidExt", extensionType.getRelativeName());
256
257    final String extensionTypeMethodName = "sayHi";
258    IMethodInfo extensionTypeMethod = extensionType.getTypeInfo().getMethod(extensionTypeMethodName, JavaTypes.STRING());
259    assertNotNull(extensionTypeMethod);
260
261    // ... but that the base type is not decorated with the extension type's methods
262    SQLTableTypeInfo ti = (SQLTableTypeInfo) baseType.getTypeInfo();
263    assertEquals("Invalid", ti.getName());
264
265    IMethodInfo domainLogicMethod = ti.getMethod(extensionTypeMethodName, JavaTypes.STRING());
266    assertNull(domainLogicMethod);
267  }
268
269  @Test
270  public void getSynthesizedProperty() {
271    ISQLTableType result = (ISQLTableType) TypeSystem.getByFullNameIfValid("gosu.db.foo.Bars.Baz");
272    assertNotNull(result);
273    assertEquals("gosu.db.foo.Bars.Baz", result.getName());
274    assertEquals("gosu.db.foo.Bars", result.getNamespace());
275    assertEquals("Baz", result.getRelativeName());
276
277    SQLTableTypeInfo ti = (SQLTableTypeInfo) result.getTypeInfo();
278    assertEquals("Baz", ti.getName());
279
280    IPropertyInfo domainLogicProperty = ti.getProperty("MeaningOfLife");
281    assertNotNull(domainLogicProperty);
282    assertTrue(domainLogicProperty.isReadable());
283    assertFalse(domainLogicProperty.isWritable());
284    assertEquals(JavaTypes.pINT(), domainLogicProperty.getFeatureType());
285    //assertEquals(42, domainLogicProperty.getAccessor().getValue(result)); //TODO failing with seemingly legitimate error:
286    /**
287     * java.lang.IllegalArgumentException: object is not an instance of declaring class
288     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
289     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
290     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
291     at gw.internal.gosu.parser.GosuPropertyInfo$GosuPropertyAccessor.getValue(GosuPropertyInfo.java:290)
292     at gosu.db.plugin.SQLPluginTest.getInjectedProperty(SQLPluginTest.java:201)
293     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
294     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
295     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
296     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
297     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
298     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
299     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
300     at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
301     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
302     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
303     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
304     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
305     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
306     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
307     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
308     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
309     at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
310     at org.junit.runners.Suite.runChild(Suite.java:128)
311     at org.junit.runners.Suite.runChild(Suite.java:24)
312     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
313     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
314     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
315     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
316     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
317     at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
318     at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
319     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
320     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
321     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
322     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
323     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
324     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
325     */
326  }
327
328}
329
Full Screen
copy
1/*
2 * Copyright (C) 2016 The Android Open Source Project
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
17package vogar.target.junit;
18
19import java.util.List;
20import org.junit.rules.TestRule;
21import org.junit.runner.Description;
22import org.junit.runner.notification.RunNotifier;
23import org.junit.runners.ParentRunner;
24import org.junit.runners.model.InitializationError;
25import org.junit.runners.model.Statement;
26
27/**
28 * A {@link ParentRunner} whose children are {@link DescribableStatement} instances.
29 *
30 * <p>This is a generally useful form of {@link ParentRunner} that can easily be customized.
31 *
32 * <p>This is used for JUnit3 based classes which may have multiple constructors (a default and
33 * one that takes the test name) so it passes a {@link Dummy} class to the parent and overrides the
34 * {@link #getName()} method. That works because this does not use any information from the supplied
35 * class.
36 */
37public class ParentStatementRunner extends ParentRunner<DescribableStatement> {
38
39    private final String name;
40    private final List<DescribableStatement> statements;
41    private final TestRule testRule;
42
43    public ParentStatementRunner(Class<?> testClass, List<DescribableStatement> statements,
44                                 RunnerParams runnerParams)
45            throws InitializationError {
46        super(Dummy.class);
47        name = testClass.getName();
48        this.statements = statements;
49        testRule = runnerParams.getTestRule();
50    }
51
52    @Override
53    protected String getName() {
54        return name;
55    }
56
57    @Override
58    protected List<DescribableStatement> getChildren() {
59        return statements;
60    }
61
62    @Override
63    protected Description describeChild(DescribableStatement child) {
64        return child.getDescription();
65    }
66
67    @Override
68    protected void runChild(final DescribableStatement child, RunNotifier notifier) {
69        Description description = describeChild(child);
70        Statement statement = child;
71        statement = testRule.apply(statement, description);
72        ParentRunnerHelper.abortingRunLeaf(statement, description, notifier);
73    }
74
75    /**
76     * ParentRunner requires that the class be public.
77     */
78    public static class Dummy {
79    }
80}
81
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 getName code on LambdaTest Cloud Grid

Execute automation tests with getName 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)