@Nonnull @Override public BackendFactory factory() { BackendFactory factory = mock(InMemoryBackendFactory.class); when(factory.supportsTransient()).thenCallRealMethod(); when(factory.createBackend(any(URI.class), any(ImmutableConfig.class))).thenAnswer((i) -> new BoundInMemoryBackend(id)); return factory; } }
/** * Retrieves the URI scheme for the specified {@code factory}. * * @param factory the factory * * @return the URI scheme * * @throws BindingException if no scheme is defined for this {@code type} */ @Nonnull public static String schemeOf(BackendFactory factory) { return UriFactory.createScheme(factory.name()); }
/** * Creates a new {@link StoreAdapter} on top of a newly created {@link Backend}, using the current {@link * #factory}. * * @param config the configuration of the store/back-end * * @return a new store */ @Nonnull private StoreAdapter createStore(ImmutableConfig config) { return createStore(factory.createBackend(uri, config), config); }
/** * Creates a new {@link StoreAdapter} on top of a newly created transient {@link Backend}, using the default {@link * InMemoryBackendFactory} implementation. * * @return a new store */ @Nonnull private StoreAdapter createTransientStore() { Backend backend; ImmutableConfig config; if (factory.supportsTransient()) { config = new InMemoryConfig(); backend = new InMemoryBackendFactory().createBackend(uri, config); } else { config = new BaseConfig<>(); backend = new InvalidBackend("This back-end does not provide a transient layer: you must save/load the associated resource before using it"); } return createStore(backend, config); }
@Test void createResourceRegistered() { PersistentResourceFactory resourceFactory = PersistentResourceFactory.getInstance(); URI uri = mock(URI.class); when(uri.scheme()).thenReturn("mock0"); Backend backend = mock(InMemoryBackend.class); BackendFactory backendFactory = mock(BackendFactory.class); when(backendFactory.createBackend(any(URI.class), any(ImmutableConfig.class))).thenReturn(backend); BackendFactoryRegistry.getInstance().register(uri.scheme(), backendFactory); PersistentResource resource = resourceFactory.createResource(uri); assertThat(resource).isNotNull(); assertThat(resource.getURI()).isSameAs(uri); assertThat(resource.eStore()).isNotNull(); BackendFactoryRegistry.getInstance().unregister(uri.scheme()); }
@Nonnull @Override public final PersistentResource createTransientResource(File file) throws IOException { return factory().supportsTransient() ? new ContextualResourceBuilder(this).file(file).createResource() : createPersistentResource(file); }
/** * Creates a new {@link DataMapper} according to the specified options. * * @return a new {@link DataMapper} * * @see Context#config() * @see #defaultConfig() */ @Nonnull public DataMapper createMapper() { Backend backend = context.factory().createBackend(uri, defaultConfig()); return StoreFactory.getInstance().createStore(backend, defaultConfig()); } }
/** * Retrieves the name for the specified {@code type}. * <p> * The {@code type} <b>must</b> be annotated with {@link FactoryBinding}, or implements {@link BackendFactory}. * * @param type the type * * @return the name * * @throws BindingException if no name is defined for this {@code type} */ @Nonnull public static String nameOf(Class<?> type) { return findFactory(type).name(); }
/** * Checks the creation of a {@link fr.inria.atlanmod.neoemf.data.Backend}. */ @ParameterizedTest @MethodSource("allMappings") public void testCreateBackend(ImmutableConfig config, Class<? extends Backend> expectedType) throws IOException { try (Backend backend = context().factory().createBackend(context().createUri(currentTempFile()), config)) { assertThat(backend).isInstanceOf(expectedType); } }
/** * Check the creation of chaining between the default {@link Store}s and a {@link Backend}. */ @Test public void testCreatePersistentStore() throws IOException { try (Backend backend = context().factory().createBackend(context().createUri(currentTempFile()), context().config())) { try (Store store = StoreFactory.getInstance().createStore(backend, context().config())) { assertThat(store).isInstanceOf(NoopStore.class); assertThat(store.backend()).isSameAs(backend); } } }
/** * Checks the copy of a {@link Backend} to another. */ @Test public void testCopyBackend() throws IOException { ImmutableConfig config = context().config(); File file = currentTempFile(); try (Backend transientBackend = new InMemoryBackendFactory().createBackend(new InMemoryUriFactory().createLocalUri(file), new InMemoryConfig())) { try (Backend persistentBackend = context().factory().createBackend(context().createUri(currentTempFile()), config)) { transientBackend.copyTo(persistentBackend); } } }