### Question:
TempResourceAction extends XWikiAction { protected File getTemporaryFile(String uri, XWikiContext context) { Matcher matcher = URI_PATTERN.matcher(uri); File result = null; if (matcher.find()) { List<String> pathSegments = new ArrayList<String>(); pathSegments.add("temp"); pathSegments.add(withMinimalURLEncoding(matcher.group(3))); pathSegments.add(encodeURLPathSegment(context.getWikiId())); pathSegments.add(withMinimalURLEncoding(matcher.group(1))); pathSegments.add(withMinimalURLEncoding(matcher.group(2))); String prefix = StringUtils.join(pathSegments, PATH_SEPARATOR); for (String filePathSegment : matcher.group(4).split(PATH_SEPARATOR)) { pathSegments.add(withMinimalURLEncoding(filePathSegment)); } String path = URI.create(StringUtils.join(pathSegments, PATH_SEPARATOR)).normalize().toString(); if (path.startsWith(prefix)) { result = new File(this.environment.getTemporaryDirectory(), path); result = result.exists() ? result : null; } } return result; }  @Override String render(XWikiContext context); static final Pattern URI_PATTERN; }### Answer:
@Test public void testGetTemporaryFileForBadURI() throws Exception { createEmptyFile("temp/secret.txt"); assertNull(action.getTemporaryFile("/xwiki/bin/temp/secret.txt", oldcore.getXWikiContext())); }
@Test public void testGetTemporaryFileForRelativeURI() throws Exception { createEmptyFile("temp/secret.txt"); assertNull(action.getTemporaryFile("/xwiki/bin/temp/../../module/secret.txt", oldcore.getXWikiContext())); }
@Test public void testGetTemporaryFileMissing() throws Exception { assertFalse(new File(this.oldcore.getTemporaryDirectory(), "temp/module/xwiki/Space/Page/file.txt").exists()); assertNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/module/file.txt", oldcore.getXWikiContext())); }
@Test public void testGetTemporaryFile() throws Exception { oldcore.getXWikiContext().setWikiId("wiki"); createEmptyFile("temp/module/wiki/Space/Page/file.txt"); assertNotNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/module/file.txt", oldcore.getXWikiContext())); }
@Test public void testGetTemporaryFileForOverEncodedURL() throws Exception { createEmptyFile("temp/officeviewer/xwiki/Sp*ace/Pa-ge/presentation.odp/presentation-slide0.jpg"); assertNotNull(action.getTemporaryFile( "/xwiki/bin/temp/Sp%2Aace/Pa%2Dge/officeviewer/presentation.odp/presentation-slide0.jpg", oldcore.getXWikiContext())); }
@Test public void testGetTemporaryFileForPartiallyDecodedURL() throws Exception { createEmptyFile("temp/officeviewer/xwiki/Space/Page/" + "attach%3Axwiki%3ASpace.Page%40pres%2Fentation.odp/13/presentation-slide0.jpg"); assertNotNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/officeviewer/" + "attach:xwiki:Space.Page@pres%2Fentation.odp/13/presentation-slide0.jpg", oldcore.getXWikiContext())); }