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

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