001/* 002 * Shredzone Commons 003 * 004 * Copyright (C) 2012 Richard "Shred" Körber 005 * http://commons.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Library General Public License as 009 * published by the Free Software Foundation, either version 3 of the 010 * License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU Library General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020 021package org.shredzone.commons.view.manager; 022 023import java.lang.reflect.Method; 024import java.util.Iterator; 025import java.util.Map; 026import java.util.SortedSet; 027import java.util.TreeSet; 028 029import org.junit.Assert; 030import org.junit.Test; 031import org.shredzone.commons.view.annotation.View; 032import org.shredzone.commons.view.annotation.ViewGroup; 033 034/** 035 * Unit tests for {@link ViewPattern}. 036 * 037 * @author Richard "Shred" Körber 038 */ 039public class ViewPatternTest { 040 041 @Test 042 @View(pattern = "test/${blafoo}/and/a/${path}.html") 043 public void functionalTest() throws NoSuchMethodException { 044 Method m = this.getClass().getMethod("functionalTest"); 045 046 ViewPattern pat = new ViewPattern(m.getAnnotation(View.class), null); 047 Assert.assertEquals(107, pat.getWeight()); 048 Assert.assertEquals("test/${blafoo}/and/a/${path}.html", pat.getPattern()); 049 Assert.assertEquals("\\Qtest/\\E([^/]*)\\Q/and/a/\\E([^/]*)\\Q.html\\E", pat.getRegEx().pattern()); 050 051 String[] expected = new String[] {"blafoo", "path"}; 052 Assert.assertArrayEquals(expected, pat.getParameters().toArray()); 053 try { 054 pat.getParameters().add("foo"); 055 Assert.fail("parameter list is modifiable"); 056 } catch (UnsupportedOperationException ex) { 057 // We expected this exception 058 } 059 060 Map<String, String> map; 061 062 map = pat.resolve("test/Something/and/a/1234.html"); 063 Assert.assertNotNull(map); 064 Assert.assertEquals(2, map.size()); 065 Assert.assertEquals("Something", map.get("blafoo")); 066 Assert.assertEquals("1234", map.get("path")); 067 068 map = pat.resolve("test//and/a/.html"); 069 Assert.assertNotNull(map); 070 Assert.assertEquals(2, map.size()); 071 Assert.assertEquals("", map.get("blafoo")); 072 Assert.assertEquals("", map.get("path")); 073 074 map = pat.resolve("test/Something/and/a/1234.htm"); 075 Assert.assertNull(map); 076 077 map = pat.resolve("something"); 078 Assert.assertNull(map); 079 080 } 081 082 @Test 083 @ViewGroup({ 084 @View(pattern = "test/${blafoo}.html"), 085 @View(pattern = "test.html"), 086 @View(pattern = "test/${blafoo}/and/a/${path}.html") 087 }) 088 public void orderTest() throws NoSuchMethodException { 089 Method m = this.getClass().getMethod("orderTest"); 090 SortedSet<ViewPattern> set = new TreeSet<ViewPattern>(); 091 092 ViewGroup groupAnno = m.getAnnotation(ViewGroup.class); 093 for (View view : groupAnno.value()) { 094 set.add(new ViewPattern(view, null)); 095 } 096 097 Iterator<ViewPattern> it = set.iterator(); 098 Assert.assertEquals("test/${blafoo}/and/a/${path}.html", it.next().getPattern()); 099 Assert.assertEquals("test/${blafoo}.html", it.next().getPattern()); 100 Assert.assertEquals("test.html", it.next().getPattern()); 101 } 102 103}