繁体   English   中英

DropWizard JAR签名错误

[英]DropWizard JAR Signing Bug

我正在尝试对我的DropWizard 0.7.0应用程序进行单元测试,就像文档建议的那样

@Test
public void runRegisertsLocationResource() {
    given:
    Environment environment = Mockito.mock(Environment)
    JerseyEnvironment jersey = Mockito.mock(JerseyEnvironment) 
    MyApplication fixture = new MyApplication()
    MyConfiguration config = new MyConfiguration()

    Mockito.when(environment.jersey()).thenReturn(jersey)

    when:
    fixture.run(config, environment)

    then:
    Mockito.verify(jersey).register(Mockito.any(LocationResource))
}

当我运行它时,我得到:

java.lang.SecurityException: class "javax.servlet.DispatcherType"'s signer information does not match signer information of other classes in the same package
    at java.lang.ClassLoader.checkCerts(ClassLoader.java:952)
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:666)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:794)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at io.dropwizard.Configuration.<init>(Configuration.java:62)
    at io.auditcloud.micros.backend.service.config.BackendConfiguration.<init>(BackendConfiguration.groovy)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:186)
    at io.auditcloud.micros.backend.service.BackendApplicationTest.runRegisertsAllProviders(BackendApplicationTest.groovy:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这是我在build.gradle定义的依赖build.gradle

dependencies {
    compile (
        'com.codahale.metrics:metrics-core:3.0.2',
        'com.codahale.metrics:metrics-json:3.0.1',
        'com.wordnik:swagger-jaxrs_2.10:1.3.4',
        'io.dropwizard:dropwizard-core:0.7.0',
        'io.dropwizard:dropwizard-auth:0.7.0',
        'org.apache.commons:commons-lang3:3.3.2',
        'io.dropwizard:dropwizard-client:0.7.0',
        'org.eclipse.jetty:jetty-servlets:8.1.14.v20131031',
        'com.google.inject:guice:3.0'
    )

    testCompile(
        'org.mockito:mockito-all:1.9.0'
    )
}

这里发生了什么?


更新:如何复制(逐步)

  1. 创建一个新的DropWizard项目,并使用上面提到的确切依赖项(我使用Gradle,但是任何工作都可以)。
  2. src/test/java下创建一个新的JUnit测试类,使其与上面的JUnit测试完全一样, 除了MyApplication替换为DropWizard应用程序主类的名称之外,并将MyConfiguration替换为相应的配置隐含。 注意:我的应用程序是用Groovy编写的,因此使用了Groovy语法。 您可能需要“ Java验证”上面的代码片段才能进行编译。
  3. 运行测试; 您应该得到与我上面发布的相同的例外。

编辑:请参阅下面的更新

这不是答案。 请不要对此投票。 在OP确认后将其删除

因此,我想知道您是否已经亲自测试了上述步骤。 我遵循了您所说的步骤,当然创建了我自己的类(由于没有指定任何东西,所以非常简单,几乎没有实现。而且我也没有得到想要的结果。我也在使用Maven和Java。我不确定问题是否出在groovy上,因为我不是groovy用户。

在下面,您将逐步找到我的最小,完整和可验证的示例(MCVE)实施。 我建议您创建类似的东西来重现该问题。 如您所述(从下面的示例作为起点),从头开始创建一个项目,然后开始添加功能,直到重现问题。 这也是您自己发现问题根源的好方法。

我将从一个简单的Groovy项目开始,如果一个简单的项目(如下面的项目)引起了问题,则可能是一个Groovy问题,您应该使用MCVE编辑您的文章,以便熟悉Groovy的其他人可以尝试找到该项目。问题。

如果不是Groovy问题,并且您是通过在我的MCVE之上构建而发现问题的,并且您无法找出原因和解决方法,那么也请发布MCVE。

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.stackoverflow.spring</groupId>
    <artifactId>dropwizard-stackoverflow</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <dropwizard.version>0.7.0</dropwizard.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-core</artifactId>
            <version>${dropwizard.version}</version>
        </dependency>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-auth</artifactId>
            <version>${dropwizard.version}</version>
        </dependency>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-client</artifactId>
            <version>${dropwizard.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-json</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

我的配置

import io.dropwizard.Configuration;

public class MyConfiguration extends Configuration {}

我的应用程序

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class MyApplication extends Application<MyConfiguration> {
    @Override
    public void initialize(Bootstrap<MyConfiguration> btstrp) {}

    @Override
    public void run(MyConfiguration t, Environment e) throws Exception {
        e.jersey().register(new LocationResource());
    }   
}

位置资源

import javax.ws.rs.Path;

@Path("/location")
public class LocationResource {}

测试

import dropwizard.stackoverflow.resource.LocationResource;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.setup.Environment;
import org.junit.Test;
import org.mockito.Mockito;

public class SimpleDropWizardTest {
    @Test
    public void runRegisertsLocationResource() throws Exception {

        Environment environment = Mockito.mock(Environment.class);
        JerseyEnvironment jersey = Mockito.mock(JerseyEnvironment.class);
        MyApplication fixture = new MyApplication();
        MyConfiguration config = new MyConfiguration();

        Mockito.when(environment.jersey()).thenReturn(jersey);

        fixture.run(config, environment);

        Mockito.verify(jersey).register(Mockito.any(LocationResource.class));
    }
}

测试结果

在此处输入图片说明


UPDATE

因此,我似乎错误地制作了我的MCVE。 由于某种原因我错过了

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlets</artifactId>
    <version>8.1.14.v20131031</version>
</dependency>

碰巧的是,这似乎是吸烟枪。 当我将此依赖项添加到上述项目中时,我会得到一个类似的异常,该异常并不完全相同,但是在类加载期间详细说明了相同的问题。

如果摆脱了这种依赖关系,它将与上面的示例一起使用。 似乎是版本冲突。 Dropwizard已经依赖于Jetty 9.0.7.v20131107。 您可以看到Maven依赖树

[INFO] +- io.dropwizard:dropwizard-core:jar:0.7.0:compile
...
[INFO] |  +- io.dropwizard:dropwizard-jetty:jar:0.7.0:compile
[INFO] |  |  +- com.codahale.metrics:metrics-jetty9:jar:3.0.1:compile
[INFO] |  |  +- org.eclipse.jetty:jetty-server:jar:9.0.7.v20131107:compile
[INFO] |  |  |  \- org.eclipse.jetty:jetty-io:jar:9.0.7.v20131107:compile
[INFO] |  |  +- org.eclipse.jetty:jetty-servlet:jar:9.0.7.v20131107:compile
[INFO] |  |  |  \- org.eclipse.jetty:jetty-security:jar:9.0.7.v20131107:compile
[INFO] |  |  +- org.eclipse.jetty:jetty-servlets:jar:9.0.7.v20131107:compile
[INFO] |  |  |  \- org.eclipse.jetty:jetty-continuation:jar:9.0.7.v20131107:compile
[INFO] |  |  \- org.eclipse.jetty:jetty-http:jar:9.0.7.v20131107:compile

基本上,当您声明顶级依赖项时(与Jetty一样),它将取代任何传递性依赖项。 因此,您有一个Jetty 9.0.7工件试图与您声明的8.1.14 servlet工件进行交互。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM