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.5KB

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