Search in sources :

Example 1 with Runner

use of org.jvnet.hudson.test.recipes.Recipe.Runner in project hudson-2.x by hudson.

the class HudsonTestCase method recipe.

//
// recipe methods. Control the test environments.
//
/**
     * Called during the {@link #setUp()} to give a test case an opportunity to
     * control the test environment in which Hudson is run.
     *
     * <p>
     * One could override this method and call a series of {@code withXXX} methods,
     * or you can use the annotations with {@link Recipe} meta-annotation.
     */
protected void recipe() throws Exception {
    recipeLoadCurrentPlugin();
    // look for recipe meta-annotation
    try {
        Method runMethod = getClass().getMethod(getName());
        for (final Annotation a : runMethod.getAnnotations()) {
            Recipe r = a.annotationType().getAnnotation(Recipe.class);
            if (r == null)
                continue;
            final Runner runner = r.value().newInstance();
            recipes.add(runner);
            tearDowns.add(new LenientRunnable() {

                public void run() throws Exception {
                    runner.tearDown(HudsonTestCase.this, a);
                }
            });
            runner.setup(this, a);
        }
    } catch (NoSuchMethodException e) {
    // not a plain JUnit test.
    }
}
Also used : Runner(org.jvnet.hudson.test.recipes.Recipe.Runner) Recipe(org.jvnet.hudson.test.recipes.Recipe) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) BadCredentialsException(org.acegisecurity.BadCredentialsException) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CSSParseException(org.w3c.css.sac.CSSParseException) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) AuthenticationException(org.acegisecurity.AuthenticationException) SAXException(org.xml.sax.SAXException) DataAccessException(org.springframework.dao.DataAccessException) CSSException(org.w3c.css.sac.CSSException) MalformedURLException(java.net.MalformedURLException)

Example 2 with Runner

use of org.jvnet.hudson.test.recipes.Recipe.Runner in project hudson-2.x by hudson.

the class HudsonTestCase method recipeLoadCurrentPlugin.

/**
     * If this test harness is launched for a Hudson plugin, locate the <tt>target/test-classes/the.hpl</tt>
     * and add a recipe to install that to the new Hudson.
     *
     * <p>
     * This file is created by <tt>maven-hpi-plugin</tt> at the testCompile phase when the current
     * packaging is <tt>hpi</tt>.
     */
protected void recipeLoadCurrentPlugin() throws Exception {
    final Enumeration<URL> e = getClass().getClassLoader().getResources("the.hpl");
    // nope
    if (!e.hasMoreElements())
        return;
    final URL hpl = e.nextElement();
    recipes.add(new Runner() {

        @Override
        public void decorateHome(HudsonTestCase testCase, File home) throws Exception {
            while (e.hasMoreElements()) {
                final URL hpl = e.nextElement();
                // make the plugin itself available
                Manifest m = new Manifest(hpl.openStream());
                String shortName = m.getMainAttributes().getValue("Short-Name");
                if (shortName == null)
                    throw new Error(hpl + " doesn't have the Short-Name attribute");
                FileUtils.copyURLToFile(hpl, new File(home, "plugins/" + shortName + ".hpl"));
                // make dependency plugins available
                // TODO: probably better to read POM, but where to read from?
                // TODO: this doesn't handle transitive dependencies
                // Tom: plugins are now searched on the classpath first. They should be available on
                // the compile or test classpath. As a backup, we do a best-effort lookup in the Maven repository
                // For transitive dependencies, we could evaluate Plugin-Dependencies transitively.
                String dependencies = m.getMainAttributes().getValue("Plugin-Dependencies");
                if (dependencies != null) {
                    MavenEmbedder embedder = new MavenEmbedder(getClass().getClassLoader(), null);
                    for (String dep : dependencies.split(",")) {
                        String[] tokens = dep.split(":");
                        String artifactId = tokens[0];
                        String version = tokens[1];
                        File dependencyJar = null;
                        // need to search multiple group IDs
                        // TODO: extend manifest to include groupID:artifactID:version
                        Exception resolutionError = null;
                        for (String groupId : new String[] { "org.jvnet.hudson.plugins", "org.jvnet.hudson.main" }) {
                            // first try to find it on the classpath.
                            // this takes advantage of Maven POM located in POM
                            URL dependencyPomResource = getClass().getResource("/META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml");
                            if (dependencyPomResource != null) {
                                // found it
                                dependencyJar = Which.jarFile(dependencyPomResource);
                                break;
                            } else {
                                Artifact a;
                                a = embedder.createArtifact(groupId, artifactId, version, "compile", /*doesn't matter*/
                                "hpi");
                                try {
                                    embedder.resolve(a, Arrays.asList(embedder.createRepository("http://maven.glassfish.org/content/groups/public/", "repo")), embedder.getLocalRepository());
                                    dependencyJar = a.getFile();
                                } catch (AbstractArtifactResolutionException x) {
                                    // could be a wrong groupId
                                    resolutionError = x;
                                }
                            }
                        }
                        if (dependencyJar == null)
                            throw new Exception("Failed to resolve plugin: " + dep, resolutionError);
                        File dst = new File(home, "plugins/" + artifactId + ".hpi");
                        if (!dst.exists() || dst.lastModified() != dependencyJar.lastModified()) {
                            FileUtils.copyFile(dependencyJar, dst);
                        }
                    }
                }
            }
        }
    });
}
Also used : Runner(org.jvnet.hudson.test.recipes.Recipe.Runner) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) Manifest(java.util.jar.Manifest) URL(java.net.URL) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) BadCredentialsException(org.acegisecurity.BadCredentialsException) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CSSParseException(org.w3c.css.sac.CSSParseException) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) AuthenticationException(org.acegisecurity.AuthenticationException) SAXException(org.xml.sax.SAXException) DataAccessException(org.springframework.dao.DataAccessException) CSSException(org.w3c.css.sac.CSSException) MalformedURLException(java.net.MalformedURLException) Artifact(org.apache.maven.artifact.Artifact) MavenEmbedder(hudson.maven.MavenEmbedder) File(java.io.File)

Aggregations

FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 AuthenticationException (org.acegisecurity.AuthenticationException)2 BadCredentialsException (org.acegisecurity.BadCredentialsException)2 UsernameNotFoundException (org.acegisecurity.userdetails.UsernameNotFoundException)2 AbstractArtifactResolutionException (org.apache.maven.artifact.resolver.AbstractArtifactResolutionException)2 Runner (org.jvnet.hudson.test.recipes.Recipe.Runner)2 DataAccessException (org.springframework.dao.DataAccessException)2 CSSException (org.w3c.css.sac.CSSException)2 CSSParseException (org.w3c.css.sac.CSSParseException)2 SAXException (org.xml.sax.SAXException)2 MavenEmbedder (hudson.maven.MavenEmbedder)1 File (java.io.File)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 Manifest (java.util.jar.Manifest)1 Artifact (org.apache.maven.artifact.Artifact)1