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.

ServiceProviderExtensionStorageTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.processor;
  17. import org.junit.jupiter.api.Test;
  18. import javax.annotation.processing.Filer;
  19. import javax.tools.FileObject;
  20. import javax.tools.StandardLocation;
  21. import java.io.IOException;
  22. import java.io.StringReader;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.HashSet;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import static org.hamcrest.MatcherAssert.assertThat;
  29. import static org.hamcrest.core.Is.is;
  30. import static org.mockito.ArgumentMatchers.any;
  31. import static org.mockito.BDDMockito.given;
  32. import static org.mockito.Mockito.mock;
  33. /**
  34. * @author Josiah Haswell
  35. */
  36. public class ServiceProviderExtensionStorageTest {
  37. @Test
  38. public void ensureServiceProviderExtensionStorageReadWorks() throws IOException {
  39. final StringReader file = new StringReader("#hello\n World");
  40. final Set<String> entries = new HashSet<>();
  41. ServiceProviderExtensionStorage.read(file, entries);
  42. assertThat(entries.size(), is(1));
  43. assertThat(entries.contains("World"), is(true));
  44. }
  45. @Test
  46. public void ensureReadingExtensionsProducesCorrectListOfExtensions() {
  47. final StringReader file = new StringReader("#hello\n World");
  48. final ExtensionAnnotationProcessor processor = mock(ExtensionAnnotationProcessor.class);
  49. final Map<String, Set<String>> extensions = new HashMap<>();
  50. extensions.put("hello", Collections.singleton("world"));
  51. given(processor.getExtensions()).willReturn(extensions);
  52. ServiceProviderExtensionStorage extensionStorage = new ServiceProviderExtensionStorage(processor) {
  53. @Override
  54. protected Filer getFiler() {
  55. try {
  56. Filer filer = mock(Filer.class);
  57. FileObject fileObject = mock(FileObject.class);
  58. given(fileObject.openReader(true)).willReturn(file);
  59. given(filer.getResource(
  60. any(StandardLocation.class),
  61. any(String.class),
  62. any(String.class)
  63. )).willReturn(fileObject);
  64. return filer;
  65. } catch(IOException ex) {
  66. throw new IllegalStateException("Shouldn't have gotten here");
  67. }
  68. }
  69. };
  70. Map<String, Set<String>> read = extensionStorage.read();
  71. assertThat(read.containsKey("hello"), is(true));
  72. assertThat(read.get("hello"), is(Collections.singleton("World")));
  73. }
  74. }