TemplateCache { public synchronized FileResult createFileResult(Path formatterDir, Path schemaDir, Path rootFormatterDir, String path, Map<String, Object> model) throws IOException { return new Request().createFileResult(formatterDir, schemaDir, rootFormatterDir, path, model); }  synchronized FileResult createFileResult(Path formatterDir, Path schemaDir, Path rootFormatterDir, String path,
                                                    Map<String, Object> model);  }
@Test public void testCreateFileResult() throws Exception { ConfigurableApplicationContext context = Mockito.mock(ConfigurableApplicationContext.class); ApplicationContextHolder.set(context); Mockito.when(context.getBean(SystemInfo.class)).thenReturn(SystemInfo.createForTesting(SystemInfo.STAGE_PRODUCTION)); final TemplateCache templateCache = new TemplateCache(); templateCache.xmlTemplateParser = AbstractTemplateParserTest.createTestParser(SystemInfo.STAGE_TESTING); final Path functionFile = IO.toPath(FormatterApiIntegrationTest.class.getResource("functions.xsl").toURI()); final FileResult fileResult = templateCache.createFileResult(functionFile.getParent(), functionFile.getParent(), functionFile.getParent(), functionFile.getFileName().toString(), Collections.<String, Object>emptyMap()); assertNotNull(fileResult); assertNotNull(templateCache.canonicalFileNameToText.get(functionFile.toRealPath())); }
@Test public void testFallback() throws Exception { String[] stagingProfiles = {SystemInfo.STAGE_TESTING, SystemInfo.STAGE_DEVELOPMENT, SystemInfo.STAGE_PRODUCTION}; final Path file0 = IO.toPath(FormatterApiIntegrationTest.class.getResource("template-cache-test/formatter/file0.txt").toURI()); final Path file1 = IO.toPath(FormatterApiIntegrationTest.class.getResource("template-cache-test/schema1/formatter/file1.txt").toURI()); final Path file2 = IO.toPath(FormatterApiIntegrationTest.class.getResource("template-cache-test/schema2/formatter/file2.txt").toURI()); final Path file3 = IO.toPath(FormatterApiIntegrationTest.class.getResource("template-cache-test/file3.txt").toURI()); for (String profile : stagingProfiles) { SchemaManager schemaManager = Mockito.mock(SchemaManager.class); Mockito.when(schemaManager.getSchemaDir("schema1")).thenReturn(file1.getParent().getParent()); Mockito.when(schemaManager.getSchemaDir("schema2")).thenReturn(file2.getParent().getParent()); ConfigurableApplicationContext context = Mockito.mock(ConfigurableApplicationContext.class); ApplicationContextHolder.set(context); Mockito.when(context.getBean(SystemInfo.class)).thenReturn(SystemInfo.createForTesting(SystemInfo.STAGE_PRODUCTION)); Mockito.when(context.getBean(SchemaManager.class)).thenReturn(schemaManager); final TemplateCache templateCache = new TemplateCache(); templateCache.xmlTemplateParser = AbstractTemplateParserTest.createTestParser(SystemInfo.STAGE_TESTING); final Path schemaAndRootDir = file0.getParent().getParent(); final Map<String, Object> emptyMap = Collections.emptyMap(); FileResult fileResult = templateCache.createFileResult(file0.getParent(), schemaAndRootDir, schemaAndRootDir, file0.getFileName().toString(), emptyMap); assertEquals(new String(Files.readAllBytes(file0), Constants.CHARSET).length(), fileResult.toString().length()); fileResult = templateCache.createFileResult(file0.getParent(), schemaAndRootDir, schemaAndRootDir, file1.getFileName().toString(), emptyMap); assertEquals(new String(Files.readAllBytes(file1), Constants.CHARSET).length(), fileResult.toString().length()); fileResult = templateCache.createFileResult(file0.getParent(), schemaAndRootDir, schemaAndRootDir, file2.getFileName().toString(), emptyMap); assertEquals(new String(Files.readAllBytes(file2), Constants.CHARSET).length(), fileResult.toString().length()); fileResult = templateCache.createFileResult(file0.getParent(), schemaAndRootDir, schemaAndRootDir, file3.getFileName().toString(), emptyMap); assertEquals(new String(Files.readAllBytes(file3), Constants.CHARSET).length(), fileResult.toString().length()); } }