### Question:
ContextAndActionURLNormalizer extends org.xwiki.url.internal.container.ContextAndActionURLNormalizer implements Initializable { @Override public ExtendedURL normalize(ExtendedURL partialURL) { String contextPath = StringUtils.strip(getContextPath(), URL_SEGMENT_DELIMITER); if (contextPath == null) { throw new RuntimeException(String.format("Failed to normalize the URL [%s] since the " + "application's Servlet context couldn't be computed.", partialURL)); } List<String> segments = new ArrayList<>(); if (StringUtils.isNotEmpty(contextPath)) { segments.add(contextPath); } List<String> servletPath = getActionAndWikiServletMapping(); for (String segment : servletPath) { if (StringUtils.isNotEmpty(segment)) { segments.add(segment); } } segments.addAll(partialURL.getSegments()); return new ExtendedURL(segments, partialURL.getParameters()); }  @Override void initialize(); @Override ExtendedURL normalize(ExtendedURL partialURL);  }### Answer:
@Test public void normalizeWithNoKnownContextPathThrowsException() throws Exception { this.mocker.registerMockComponent(Environment.class); try { this.mocker.getComponentUnderTest().normalize(this.testURL); fail("Should have thrown an exception"); } catch (RuntimeException expected) { assertEquals("Failed to normalize the URL [/one/two] since the application's Servlet context couldn't be " + "computed.", expected.getMessage()); } }
@Test public void normalizeRemovesLeadingAndTrailingSlashFromConfiguration() throws Exception { when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("/xwiki/"); assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); }
@Test public void normalizeFromVirtualWikiRequestPreservesWikiPath() throws Exception { when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("xwiki"); HttpServletRequest req = createMockRequest(); when(req.getServletPath()).thenReturn("/wiki"); when(this.modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("dev")); assertEquals("/xwiki/wiki/dev/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); }
@Test public void normalizePreservesParameters() throws Exception { when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("xwiki"); Map<String, List<String>> params = new HashMap<>(); params.put("age", Arrays.asList("32")); params.put("colors", Arrays.asList("red", "blue")); ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"), params); assertSame(params, this.mocker.getComponentUnderTest().normalize(extendedURL).getParameters()); }