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.

SingletonExtensionFactoryTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 org.junit.jupiter.api.AfterEach;
  18. import org.junit.jupiter.api.BeforeEach;
  19. import org.junit.jupiter.api.Test;
  20. import org.pf4j.test.TestExtension;
  21. import java.io.File;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.net.URLClassLoader;
  25. import static org.junit.jupiter.api.Assertions.assertNotSame;
  26. import static org.junit.jupiter.api.Assertions.assertSame;
  27. /**
  28. * @author Decebal Suiu
  29. * @author Ajith Kumar
  30. */
  31. public class SingletonExtensionFactoryTest {
  32. private PluginManager pluginManager;
  33. @BeforeEach
  34. public void setUp() {
  35. pluginManager = new DefaultPluginManager();
  36. }
  37. @AfterEach
  38. public void tearDown() {
  39. pluginManager = null;
  40. }
  41. @Test
  42. public void create() {
  43. ExtensionFactory extensionFactory = new SingletonExtensionFactory(pluginManager);
  44. Object extensionOne = extensionFactory.create(TestExtension.class);
  45. Object extensionTwo = extensionFactory.create(TestExtension.class);
  46. assertSame(extensionOne, extensionTwo);
  47. }
  48. @Test
  49. public void createNewEachTime() {
  50. ExtensionFactory extensionFactory = new SingletonExtensionFactory(pluginManager, "FailTestExtension.class");
  51. Object extensionOne = extensionFactory.create(TestExtension.class);
  52. Object extensionTwo = extensionFactory.create(TestExtension.class);
  53. assertNotSame(extensionOne, extensionTwo);
  54. }
  55. @Test
  56. @SuppressWarnings("unchecked")
  57. public void createNewEachTimeFromDifferentClassLoaders() throws Exception {
  58. ExtensionFactory extensionFactory = new SingletonExtensionFactory(pluginManager);
  59. // Get classpath locations
  60. URL[] classpathReferences = getClasspathReferences();
  61. // Create different classloaders for the classpath references and load classes respectively
  62. ClassLoader klassLoaderOne = new URLClassLoader(classpathReferences, null);
  63. Class klassOne = klassLoaderOne.loadClass(TestExtension.class.getName());
  64. ClassLoader klassLoaderTwo = new URLClassLoader(classpathReferences, null);
  65. Class klassTwo = klassLoaderTwo.loadClass(TestExtension.class.getName());
  66. // create instances
  67. Object instanceOne = extensionFactory.create(klassOne);
  68. Object instanceTwo = extensionFactory.create(klassTwo);
  69. // assert that instances not same
  70. assertNotSame(instanceOne, instanceTwo);
  71. }
  72. private URL[] getClasspathReferences() throws MalformedURLException {
  73. String classpathProperty = System.getProperty("java.class.path");
  74. String[] classpaths = classpathProperty.split(File.pathSeparator);
  75. URL[] uris = new URL[classpaths.length];
  76. for (int index = 0; index < classpaths.length; index++) {
  77. uris[index] = new File(classpaths[index]).toURI().toURL();
  78. }
  79. return uris;
  80. }
  81. }