You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultExtensionFactoryTest.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  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. package org.pf4j;
  17. import com.google.testing.compile.JavaFileObjects;
  18. import org.junit.jupiter.api.AfterEach;
  19. import org.junit.jupiter.api.BeforeEach;
  20. import org.junit.jupiter.api.Test;
  21. import org.pf4j.test.JavaFileObjectClassLoader;
  22. import org.pf4j.test.JavaSources;
  23. import org.pf4j.test.TestExtension;
  24. import javax.tools.JavaFileObject;
  25. import static org.junit.jupiter.api.Assertions.assertNotNull;
  26. import static org.junit.jupiter.api.Assertions.assertThrows;
  27. /**
  28. * @author Mario Franco
  29. */
  30. public class DefaultExtensionFactoryTest {
  31. public static final JavaFileObject FailTestExtension = JavaFileObjects.forSourceLines("FailTestExtension",
  32. "package test;",
  33. "import org.pf4j.test.TestExtensionPoint;",
  34. "import org.pf4j.Extension;",
  35. "",
  36. "@Extension",
  37. "public class FailTestExtension implements TestExtensionPoint {",
  38. " public FailTestExtension(String name) {}",
  39. "",
  40. " @Override",
  41. " public String saySomething() { return \"I am a fail test extension\";}",
  42. "}");
  43. private ExtensionFactory extensionFactory;
  44. @BeforeEach
  45. public void setUp() {
  46. extensionFactory = new DefaultExtensionFactory();
  47. }
  48. @AfterEach
  49. public void tearDown() {
  50. extensionFactory = null;
  51. }
  52. /**
  53. * Test of create method, of class DefaultExtensionFactory.
  54. */
  55. @Test
  56. public void testCreate() {
  57. assertNotNull(extensionFactory.create(TestExtension.class));
  58. }
  59. /**
  60. * Test of create method, of class DefaultExtensionFactory.
  61. */
  62. @Test
  63. public void testCreateFailConstructor() {
  64. JavaFileObject object = JavaSources.compile(FailTestExtension);
  65. JavaFileObjectClassLoader classLoader = new JavaFileObjectClassLoader();
  66. Class<?> extensionClass = (Class<?>) classLoader.load(object).values().toArray()[0];
  67. assertThrows(PluginRuntimeException.class, () -> extensionFactory.create(extensionClass));
  68. }
  69. }