ComponentProxyObjectFactory { protected ApplicationContext getContext() { ApplicationContext appContext; String configFilePath = getPropertyFileURL() + getConfigFileName(); if (LOG.isDebugEnabled()) { LOG.debug("Trying to get lock for ApplicationContext " + configFilePath); } synchronized (CACHE_LOCK) { LocalApplicationContextInfo appContextInfo = getAppContextInfo(getConfigFileName()); if (appContextInfo == null) { LOG.debug("ApplicationContext for: " + getConfigFileName() + " was null - creating."); appContextInfo = new LocalApplicationContextInfo(); appContextInfo.setApplicationContext(createApplicationContext(configFilePath)); appContextInfo.setConfigLastModified(getLastModified(configFilePath)); contextMap.put(getConfigFileName(), appContextInfo); appContext = appContextInfo.getApplicationContext(); } else { LOG.debug("ApplicationContext for: " + getConfigFileName() + " was not null - checking to see if it is stale."); long lastModified = getLastModified(configFilePath); if (appContextInfo.getConfigLastModified() != lastModified) { LOG.debug("Refreshing the Spring application context for: " + getConfigFileName()); refreshConfigurationContext(appContextInfo.getApplicationContext()); appContextInfo.setConfigLastModified(lastModified); } appContext = appContextInfo.getApplicationContext(); } } if (LOG.isDebugEnabled()) { LOG.debug("Releasing lock for ApplicationContext" + configFilePath); } return appContext; }    }
@Test public void testGetContextHappy() { try { final ApplicationContext mockContext = context.mock(ApplicationContext.class); ComponentProxyObjectFactory sut = new ComponentProxyObjectFactory() { @Override protected long getLastModified(String filePath) { return 5L; } @Override protected String getPropertyFileURL() { return "/test/"; } @Override protected String getConfigFileName() { return "TestFile.xml"; } @Override protected ApplicationContext createApplicationContext(String configFilePath) { return mockContext; } }; assertNotNull("ApplicationContext from getContext", sut.getContext()); } catch (Throwable t) { System.out.println("Error running testGetContextHappy test: " + t.getMessage()); t.printStackTrace(); fail("Error running testGetContextHappy test: " + t.getMessage()); } }
@Test public void testGetContextRefresh() { try { final ApplicationContext mockContext = context.mock(ApplicationContext.class); ComponentProxyObjectFactory sut = new ComponentProxyObjectFactory() { @Override protected void refreshConfigurationContext(ApplicationContext appContext) { } @Override protected long getLastModified(String filePath) { return 6L; } @Override protected String getPropertyFileURL() { return "/test/"; } @Override protected String getConfigFileName() { return "TestFile.xml"; } @Override protected ApplicationContext createApplicationContext(String configFilePath) { return mockContext; } @Override protected LocalApplicationContextInfo getAppContextInfo(String sKey) { LocalApplicationContextInfo oInfo = new LocalApplicationContextInfo(); oInfo.setApplicationContext(mockContext); oInfo.setConfigLastModified(5L); return oInfo; } }; assertNotNull("ApplicationContext from getContext", sut.getContext()); } catch (Throwable t) { System.out.println("Error running testGetContextRefresh test: " + t.getMessage()); t.printStackTrace(); fail("Error running testGetContextRefresh test: " + t.getMessage()); } }