読者です 読者をやめる 読者になる 読者になる

Webエンジニア susumuis の技術ブログ

このブログの内容は個人の見解であり、所属する組織の公式見解ではありません

静的ページのように見せかけるUrlCodingStrategy

Wicket java

前回作ったものを拡張して、パラメータなしの場合は/index.htmlを付与するようにしてみました。

public class FakeStaticHtmlBookmarkablePageRequestTargetUrlCodingStrategy extends BookmarkablePageRequestTargetUrlCodingStrategy {

	public FakeStaticHtmlBookmarkablePageRequestTargetUrlCodingStrategy(String mountPath,
			Class<? extends Page> bookmarkablePageClass, String pageMapName) {
		super(mountPath, bookmarkablePageClass, pageMapName);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void appendParameters(AppendingStringBuffer url, Map parameters) {
		super.appendParameters(url, parameters);
		if (parameters.size() > 0) {
			if (url.endsWith("/")) {
				url.delete(url.length() - 1, url.length());
				url.append(".html");
			}
		} else {
			if (!url.endsWith("/")) {
				url.append("/");
			}
			url.append("index.html");
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected ValueMap decodeParameters(String urlFragment, Map urlParameters) {
		if (urlFragment.endsWith("index.html")) {
			urlFragment = urlFragment.substring(0, urlFragment.length() - "index.html".length());
		} else if (urlFragment.endsWith(".html")) {
			urlFragment = urlFragment.substring(0, urlFragment.length() - ".html".length());
		}
		return super.decodeParameters(urlFragment, urlParameters);
	}
}

テスト

public class FakeStaticHtmlBookmarkablePageRequestTargetUrlCodingStrategyTest {
	
	private static class DummyPage extends Page {
	}

	static final IRequestTargetUrlCodingStrategy fake = new FakeStaticHtmlBookmarkablePageRequestTargetUrlCodingStrategy("/item", DummyPage.class, null);
	static final IRequestTargetUrlCodingStrategy base = new BookmarkablePageRequestTargetUrlCodingStrategy("/item", DummyPage.class, null);
	static final PageParameters params = new PageParameters("item_cd=123,item_sub_cd=1");
	
	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void testDecode() {
		RequestParameters req = new RequestParameters();
		req.setPath("item/item_cd/123/item_sub_cd/1.html");
		IRequestTarget target = fake.decode(req);
		if (target instanceof BookmarkablePageRequestTarget) {
			BookmarkablePageRequestTarget bTarget = (BookmarkablePageRequestTarget) target;
			assertEquals(DummyPage.class, bTarget.getPageClass());
			assertEquals("123", bTarget.getPageParameters().getString("item_cd"));
			assertEquals("1", bTarget.getPageParameters().getString("item_sub_cd"));
			assertEquals(2, bTarget.getPageParameters().size());
		} else {
			fail("not instance of BookmarkablePageRequestTarget");
		}
		
		req.setPath("item/index.html");
		target = fake.decode(req);
		if (target instanceof BookmarkablePageRequestTarget) {
			BookmarkablePageRequestTarget bTarget = (BookmarkablePageRequestTarget) target;
			assertEquals(DummyPage.class, bTarget.getPageClass());
			assertEquals(0, bTarget.getPageParameters().size());
		} else {
			fail("not instance of BookmarkablePageRequestTarget");
		}
		
		req.setPath("item");
		target = fake.decode(req);
		if (target instanceof BookmarkablePageRequestTarget) {
			BookmarkablePageRequestTarget bTarget = (BookmarkablePageRequestTarget) target;
			assertEquals(DummyPage.class, bTarget.getPageClass());
			assertEquals(0, bTarget.getPageParameters().size());
		} else {
			fail("not instance of BookmarkablePageRequestTarget");
		}
	}

	@Test
	public void testEncode() {
		assertEquals("item/item_cd/123/item_sub_cd/1/", base.encode(new BookmarkablePageRequestTarget(DummyPage.class, params)).toString());
		assertEquals("item/item_cd/123/item_sub_cd/1.html", fake.encode(new BookmarkablePageRequestTarget(DummyPage.class, params)).toString());
		assertEquals("item/index.html", fake.encode(new BookmarkablePageRequestTarget(DummyPage.class, new PageParameters())).toString());
	}

	@Test
	public void testMatches() {
		assertTrue(fake.matches("item/"));
		assertTrue(fake.matches("item/index.html"));
		assertTrue(fake.matches("item/a/b.html"));
		assertTrue(fake.matches("item/a/b/"));
		assertTrue(fake.matches("item/a/"));
		assertFalse(fake.matches("item.htm"));
	}
}