Java Code Examples for org.junit.runners.model.TestClass#getName()

The following examples show how to use org.junit.runners.model.TestClass#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NamedParameterizedRunner.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Gets the parameterization
 * @return the parameterization collection
 * @throws Throwable if the annotation requirements are not met, or if there's an error in invoking
 * the class's "get parameterizations" method.
 */
private Collection<Parameterization> getParameterizations() throws Throwable
{
    TestClass cls = getTestClass();
    List<FrameworkMethod> methods = cls.getAnnotatedMethods(TestParameters.class);

    if (methods.size() != 1)
    {
        throw new Exception("class " + cls.getName() + " must have exactly 1 method annotated with "
            + TestParameters.class.getSimpleName() +"; found " + methods.size());
    }

    FrameworkMethod method = methods.get(0);
    checkParameterizationMethod(method);

    @SuppressWarnings("unchecked")
    Collection<Parameterization> ret = (Collection<Parameterization>) method.invokeExplosively(null);
    checkParameterizations(ret);
    return ret;
}
 
Example 2
Source File: Injected.java    From ion-java with Apache License 2.0 6 votes vote down vote up
private static PropertyDescriptor findDescriptor(TestClass testClass,
                                          PropertyDescriptor[] descriptors,
                                          FrameworkField field,
                                          String name)
throws Exception
{
    for (PropertyDescriptor d : descriptors)
    {
        if (d.getName().equals(name))
        {
            if (d.getWriteMethod() == null) break;  // To throw error
            return d;
        }
    }

    throw new Exception("@Inject value '" + name
                        + "' doesn't match a writeable property near "
                        + testClass.getName() + '.'
                        + field.getField().getName());
}
 
Example 3
Source File: SpringJUnit4ParameterizedClassRunner.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private FrameworkMethod getParametersMethod(TestClass testClass) throws Exception {
  List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameters.class);
  for (FrameworkMethod each : methods) {
    int modifiers = each.getMethod().getModifiers();
    if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
      return each;
  }

  throw new Exception("No public static parameters method on class " + testClass.getName());
}
 
Example 4
Source File: Injected.java    From ion-java with Apache License 2.0 5 votes vote down vote up
private static Dimension[] findDimensions(TestClass testClass)
throws Throwable
{
    List<FrameworkField> fields =
        testClass.getAnnotatedFields(Inject.class);
    if (fields.isEmpty())
    {
        throw new Exception("No fields of " + testClass.getName()
                            + " have the @Inject annotation");
    }

    BeanInfo beanInfo = Introspector.getBeanInfo(testClass.getJavaClass());
    PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();

    Dimension[] dimensions = new Dimension[fields.size()];

    int i = 0;
    for (FrameworkField field : fields)
    {
        int modifiers = field.getField().getModifiers();
        if (! Modifier.isPublic(modifiers) || ! Modifier.isStatic(modifiers))
        {
            throw new Exception("@Inject " + testClass.getName() + '.'
                                + field.getField().getName()
                                + " must be public static");
        }

        Dimension dim = new Dimension();
        dim.property = field.getField().getAnnotation(Inject.class).value();
        dim.descriptor = findDescriptor(testClass, descriptors, field, dim.property);
        dim.values = (Object[]) field.get(null);
        dimensions[i++] = dim;
    }

    return dimensions;
}
 
Example 5
Source File: ParameterizedMultiRunner.java    From cougar with Apache License 2.0 5 votes vote down vote up
private FrameworkMethod getParametersMethod(TestClass testClass)
        throws Exception {
    List<FrameworkMethod> methods= testClass
            .getAnnotatedMethods(Parameters.class);
    for (FrameworkMethod each : methods) {
        int modifiers= each.getMethod().getModifiers();
        if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
            return each;
    }

    throw new Exception("No public static parameters method on class "
            + testClass.getName());
}
 
Example 6
Source File: BytecoderUnitTestRunner.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private void testJSBackendFrameworkMethod(final FrameworkMethod aFrameworkMethod, final RunNotifier aRunNotifier, final TestOption aTestOption) {
    if ("".equals(System.getProperty("BYTECODER_DISABLE_JSTESTS", ""))) {
        final TestClass testClass = getTestClass();
        final Description theDescription = Description.createTestDescription(testClass.getJavaClass(), aFrameworkMethod.getName() + " " + aTestOption.toDescription());
        aRunNotifier.fireTestStarted(theDescription);

        try {
            final CompileTarget theCompileTarget = new CompileTarget(testClass.getJavaClass().getClassLoader(), CompileTarget.BackendType.js);

            final BytecodeMethodSignature theSignature = theCompileTarget.toMethodSignature(aFrameworkMethod.getMethod());

            final BytecodeObjectTypeRef theTestClass = new BytecodeObjectTypeRef(testClass.getName());
            final BytecodeMethodSignature theTestClassConstructorSignature = new BytecodeMethodSignature(BytecodePrimitiveTypeRef.VOID, new BytecodeTypeRef[0]);

            final StringWriter theStrWriter = new StringWriter();
            final PrintWriter theCodeWriter = new PrintWriter(theStrWriter);

            final CompileOptions theOptions = new CompileOptions(LOGGER, true, KnownOptimizer.ALL, aTestOption.isExceptionsEnabled(), "bytecoder", 512, 512, aTestOption.isMinify(), aTestOption.isPreferStackifier(), Allocator.linear, additionalClassesToLink, additionalResources, null, aTestOption.isEscapeAnalysisEnabled());
            final JSCompileResult result = (JSCompileResult) theCompileTarget.compile(theOptions, testClass.getJavaClass(), aFrameworkMethod.getName(), theSignature);
            final CompileResult.StringContent content = (CompileResult.StringContent) result.getContent()[0];

            theCodeWriter.println(content.asString());

            final String theFilename = result.getMinifier().toClassName(theTestClass) + "." + result.getMinifier().toMethodName(aFrameworkMethod.getName(), theSignature) + "_" + aTestOption.toFilePrefix() + ".html";

            theCodeWriter.println();

            theCodeWriter.println("console.log(\"Starting test\");");
            theCodeWriter.println("bytecoder.bootstrap();");
            theCodeWriter.println("var theTestInstance = " + result.getMinifier().toClassName(theTestClass) + "." +  result.getMinifier().toSymbol("__runtimeclass") + "." + result.getMinifier().toMethodName("$newInstance", theTestClassConstructorSignature) + "();");
            theCodeWriter.println("try {");
            theCodeWriter.println("     theTestInstance." + result.getMinifier().toMethodName(aFrameworkMethod.getName(), theSignature) + "();");
            theCodeWriter.println("     console.log(\"Test finished OK\");");
            theCodeWriter.println("} catch (e) {");
            theCodeWriter.println("     if (e.exception) {");
            theCodeWriter.println("         console.log(\"Test finished with exception. Message = \" + bytecoder.toJSString(e.exception.message));");
            theCodeWriter.println("     } else {");
            theCodeWriter.println("         console.log(\"Test finished with exception.\");");
            theCodeWriter.println("     }");
            theCodeWriter.println("     console.log(e.stack);");
            theCodeWriter.println("}");

            theCodeWriter.flush();

            final File theWorkingDirectory = new File(".");

            initializeTestWebServer();

            final BrowserWebDriverContainer theContainer = initializeSeleniumContainer();

            final File theMavenTargetDir = new File(theWorkingDirectory, "target");
            final File theGeneratedFilesDir = new File(theMavenTargetDir, "bytecoderjs");
            theGeneratedFilesDir.mkdirs();

            // Copy additional resources
            for (final CompileResult.Content c : result.getContent()) {
                if (c instanceof CompileResult.URLContent) {
                    try (final FileOutputStream fos = new FileOutputStream(new File(theGeneratedFilesDir, c.getFileName()))) {
                        c.writeTo(fos);
                    }
                }
            }

            final File theGeneratedFile = new File(theGeneratedFilesDir, theFilename);
            final PrintWriter theWriter = new PrintWriter(theGeneratedFile);
            theWriter.println("<html><body><script>");
            theWriter.println(theStrWriter.toString());
            theWriter.println("</script></body></html>");
            theWriter.flush();
            theWriter.close();

            initializeWebRoot(theGeneratedFile.getParentFile());

            final URL theTestURL = getTestFileUrl(theGeneratedFile);
            final WebDriver theDriver = theContainer.getWebDriver();
            theDriver.get(theTestURL.toString());

            final List<LogEntry> theAll = theDriver.manage().logs().get(LogType.BROWSER).getAll();
            if (1 > theAll.size()) {
                aRunNotifier.fireTestFailure(new Failure(theDescription, new RuntimeException("No console output from browser")));
            }
            for (final LogEntry theEntry : theAll) {
                LOGGER.info(theEntry.getMessage());
            }
            final LogEntry theLast = theAll.get(theAll.size() - 1);

            if (!theLast.getMessage().contains("Test finished OK")) {
                aRunNotifier.fireTestFailure(new Failure(theDescription, new RuntimeException("Test did not succeed! Got : " + theLast.getMessage())));
            }
        } catch (final Exception e) {
            aRunNotifier.fireTestFailure(new Failure(theDescription, e));
        } finally {
            aRunNotifier.fireTestFinished(theDescription);
        }
    }
}