Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amplified test doesn't execute for AmplProviderTest #537

Closed
vmassol opened this issue Sep 4, 2018 · 7 comments
Closed

Amplified test doesn't execute for AmplProviderTest #537

vmassol opened this issue Sep 4, 2018 · 7 comments

Comments

@vmassol
Copy link
Contributor

vmassol commented Sep 4, 2018

Dspot generated the following new test:

package org.xwiki.component;


import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.junit.Assert;
import org.junit.Test;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.ComponentRole;
import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.embed.EmbeddableComponentManagerTest;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;


public class AmplProviderTest {
    @ComponentRole
    public static interface TestComponentRole {}

    @Component
    @Singleton
    public static class TestComponentWithProviders implements AmplProviderTest.TestComponentRole {
        @Inject
        public Provider<String> provider1;

        @Inject
        @Named("another")
        public Provider<String> provider12;

        @Inject
        public Provider<Integer> provider2;

        @Inject
        public Provider<List<EmbeddableComponentManagerTest.Role>> providerList;

        @Inject
        public Provider<Map<String, EmbeddableComponentManagerTest.Role>> providerMap;
    }

    public static class TestProvider1 implements Provider<String> {
        @Override
        public String get() {
            return "value";
        }
    }

    @Named("another")
    public static class TestProvider12 implements Provider<String> {
        @Override
        public String get() {
            return "another value";
        }
    }

    public static class TestProvider2 implements Provider<Integer> {
        @Override
        public Integer get() {
            return 1;
        }
    }

    @Component
    @Named("exception")
    @Singleton
    public static class TestComponentWithProviderInException implements AmplProviderTest.TestComponentRole {
        @Inject
        @Named("exception")
        public Provider<String> providerWithExceptionInInitialize;
    }

    @Named("exception")
    public static class TestProviderWithExceptionInInitialize implements Provider<String> , Initializable {
        @Override
        public void initialize() throws InitializationException {
            throw new InitializationException("Some error in init");
        }

        @Override
        public String get() {
            throw new RuntimeException("should not be called!");
        }
    }

    @Test(timeout = 10000)
    public void loadAndInjectProviderWhenExceptionInInitialize() throws Exception {
        EmbeddableComponentManager cm = new EmbeddableComponentManager();
        Assert.assertNull(((EmbeddableComponentManager) (cm)).getParent());
        Assert.assertNull(((EmbeddableComponentManager) (cm)).getNamespace());
        Assert.assertNull(((EmbeddableComponentManager) (cm)).getComponentEventManager());
        cm.initialize(getClass().getClassLoader());
        try {
            cm.getInstance(AmplProviderTest.TestComponentRole.class, "exception");
        } catch (ComponentLookupException expected) {
            String String_0 = "Failed to lookup component " + ("[org.xwiki.component.ProviderTest$TestComponentWithProviderInException] identified by " + "type [interface org.xwiki.component.ProviderTest$TestComponentRole] and hint [exception]");
            Assert.assertEquals("Failed to lookup component [org.xwiki.component.ProviderTest$TestComponentWithProviderInException] identified by type [interface org.xwiki.component.ProviderTest$TestComponentRole] and hint [exception]", String_0);
            expected.getMessage();
            String String_1 = "Failed to lookup component " + ("[org.xwiki.component.ProviderTest$TestProviderWithExceptionInInitialize] identified by " + "type [javax.inject.Provider<java.lang.String>] and hint [exception]");
            Assert.assertEquals("Failed to lookup component [org.xwiki.component.ProviderTest$TestProviderWithExceptionInInitialize] identified by type [javax.inject.Provider<java.lang.String>] and hint [exception]", String_1);
            expected.getCause().getMessage();
            expected.getCause().getCause().getMessage();
        }
    }
}

But when adding it to my test suite and executing it I get:

[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ xwiki-commons-component-default ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.xwiki.component.embed.AmplEmbeddableComponentManagerTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.602 s - in org.xwiki.component.embed.AmplEmbeddableComponentManagerTest
[INFO] Running org.xwiki.component.embed.EmbeddableComponentManagerTest
[INFO] Tests run: 24, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in org.xwiki.component.embed.EmbeddableComponentManagerTest
[INFO] Running org.xwiki.component.guice.GuiceCompatibilityTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.168 s - in org.xwiki.component.guice.GuiceCompatibilityTest
[INFO] Running org.xwiki.component.internal.StackingComponentEventManagerTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.038 s - in org.xwiki.component.internal.StackingComponentEventManagerTest
[INFO] Running org.xwiki.component.ProviderTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.012 s - in org.xwiki.component.ProviderTest
[INFO] Running org.xwiki.component.annotation.ComponentDescriptorFactoryTest
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in org.xwiki.component.annotation.ComponentDescriptorFactoryTest
[INFO] Running org.xwiki.component.annotation.ComponentAnnotationLoaderTest
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 s - in org.xwiki.component.annotation.ComponentAnnotationLoaderTest
[INFO] Running org.xwiki.component.AmplProviderTest
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.01 s <<< FAILURE! - in org.xwiki.component.AmplProviderTest
[ERROR] loadAndInjectProviderWhenExceptionInInitialize  Time elapsed: 0.01 s  <<< ERROR!
java.lang.NullPointerException
	at org.xwiki.component.AmplProviderTest.loadAndInjectProviderWhenExceptionInInitialize(AmplProviderTest.java:104)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.lang.Thread.run(Thread.java:748)
@vmassol
Copy link
Contributor Author

vmassol commented Sep 4, 2018

FTR here's the original test:

    @Test
    public void loadAndInjectProviderWhenExceptionInInitialize() throws Exception
    {
        EmbeddableComponentManager cm = new EmbeddableComponentManager();
        cm.initialize(getClass().getClassLoader());

        try {
            cm.getInstance(TestComponentRole.class, "exception");
            Assert.fail("Should have thrown an exception");
        } catch (ComponentLookupException expected) {
            Assert.assertEquals("Failed to lookup component "
                + "[org.xwiki.component.ProviderTest$TestComponentWithProviderInException] identified by "
                + "type [interface org.xwiki.component.ProviderTest$TestComponentRole] and hint [exception]",
                expected.getMessage());
            Assert.assertEquals("Failed to lookup component "
                + "[org.xwiki.component.ProviderTest$TestProviderWithExceptionInInitialize] identified by "
                + "type [javax.inject.Provider<java.lang.String>] and hint [exception]",
                expected.getCause().getMessage());
            Assert.assertEquals("Some error in init", expected.getCause().getCause().getMessage());
        }
    }

@vmassol vmassol changed the title Amplified test doesn't execute Amplified test doesn't execute for AmplProviderTest Sep 4, 2018
@danglotb
Copy link
Member

danglotb commented Sep 5, 2018

Hi @vmassol

It seems that you have a registration process of your test components.

I added to src/test/resources/META-INF/components.txt the following lines:

org.xwiki.component.AmplProviderTest$TestComponentWithProviders
org.xwiki.component.AmplProviderTest$TestProvider1
org.xwiki.component.AmplProviderTest$TestProvider12
org.xwiki.component.AmplProviderTest$TestProvider2
org.xwiki.component.AmplProviderTest$TestComponentWithProviderInException
org.xwiki.component.AmplProviderTest$TestProviderWithExceptionInInitialize

and the test passes.

@vmassol
Copy link
Contributor Author

vmassol commented Sep 5, 2018

Indeed, so one idea would be to generate an amplified test with the same name. We need to try if it would work for the xwiki test component registration.

@danglotb
Copy link
Member

danglotb commented Sep 5, 2018

Indeed, so one idea would be to generate an amplified test with the same name.

In fact, if you do not use --generate-new-test-class flag, it should work.

I close this issue for now.

@danglotb danglotb closed this as completed Sep 5, 2018
@vmassol
Copy link
Contributor Author

vmassol commented Sep 5, 2018

In fact, if you do not use --generate-new-test-class flag, it should work.

But I can't do that since I only want new tests! I don't understand why there's a different file name behavior whether I use --generate-new-test-class or not.

For me this issue is not closed yet. Or maybe you could close it and instead open another one about using the same file name as the original test in all cases? Thx

@danglotb
Copy link
Member

danglotb commented Sep 5, 2018

I don't understand why there's a different file name behavior whether I use --generate-new-test-class or not.

This allows to copy/paste the amplified test class into the test suite without troubles:

  1. without the flag --generate-new-test-class, the original test methods are kept and the test class can be erased with the amplified version (same test class name), without loose compared to the original test class.
  2. with the flag, the amplified version has a different name and has only amplified test methods, without redundancy with the original test class.

I do understand that you would like to have a configuration that keeps the same test class name (as the original) but without original test methods because you will have two separate test suites, right?

For me this issue is not closed yet. Or maybe you could close it and instead open another one about using the same file name as the original test in all cases? Thx

I will introduce a new command line option that does the job, even if it seems very specific to your usage.

@danglotb danglotb reopened this Sep 5, 2018
@vmassol
Copy link
Contributor Author

vmassol commented Sep 5, 2018

Actually one interesting point is that if you change the class name you're risking breaking the test.

Imagine a test that does reflection with a Class.forName() on the test class name (ok it's bit conceived but you get the gist). Another good example is the one from this issue that breaks the test component registration in XWiki. I'm sure there are plenty of use cases that get broken when the test class name is changed.

So I don't think it's specific to XWiki :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants