### Question:
JndiRepositoryFactory implements ObjectFactory { public JcrRepository getObjectInstance( Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment ) throws IOException, SAXException, RepositoryException, NamingException { if (!(obj instanceof Reference)) return null; Reference ref = (Reference)obj; if (engine == null) { RefAddr configFile = ref.get(CONFIG_FILE); assert configFile != null; initializeEngine(configFile.getContent().toString()); if (nameCtx instanceof EventContext) { EventContext evtCtx = (EventContext)nameCtx; NamespaceChangeListener listener = new NamespaceChangeListener() { public void namingExceptionThrown( NamingExceptionEvent evt ) { evt.getException().printStackTrace(); } public void objectAdded( NamingEvent evt ) { } public void objectRemoved( NamingEvent evt ) { Object oldObject = evt.getOldBinding().getObject(); if (!(oldObject instanceof JcrEngine)) return; JcrEngine engine = (JcrEngine)oldObject; log.info(JcrI18n.engineStopping); long start = System.currentTimeMillis(); engine.shutdown(); try { engine.awaitTermination(30, TimeUnit.SECONDS); log.info(JcrI18n.engineStopped, (System.currentTimeMillis() - start)); } catch (InterruptedException ie) { } } public void objectRenamed( NamingEvent evt ) { } }; evtCtx.addNamingListener(name, EventContext.OBJECT_SCOPE, listener); } } assert engine != null; RefAddr repositoryName = ref.get(REPOSITORY_NAME); assert repositoryName != null; return engine.getRepository(repositoryName.getContent().toString()); }  JcrRepository getObjectInstance( Object obj,
                                            Name name,
                                            Context nameCtx,
                                            Hashtable<?, ?> environment );  }### Answer:
@Test public void shouldFindConfigFileOnClasspath() throws Exception { configFile = new StringRefAddr("configFile", "/tck/default/configRepository.xml"); reference.add(configFile); JcrRepository repo = factory.getObjectInstance(reference, null, null, null); assertThat(repo, is(notNullValue())); assertThat(repo.getRepositorySourceName(), is(REPOSITORY_SOURCE_NAME)); }
@Test public void shouldFindConfigFileInFileSystem() throws Exception { configFile = new StringRefAddr("configFile", "./src/test/resources/tck/default/configRepository.xml"); reference.add(configFile); JcrRepository repo = factory.getObjectInstance(reference, null, null, null); assertThat(repo, is(notNullValue())); assertThat(repo.getRepositorySourceName(), is(REPOSITORY_SOURCE_NAME)); }
@Test public void shouldReturnSameRepository() throws Exception { configFile = new StringRefAddr("configFile", "/tck/default/configRepository.xml"); reference.add(configFile); JcrRepository repo1 = factory.getObjectInstance(reference, null, null, null); JcrRepository repo2 = factory.getObjectInstance(reference, null, null, null); assertThat(repo1 == repo2, is(true)); }