001: package org.vraptor.url;
002:
003: import org.jmock.Expectations;
004: import org.jmock.Mockery;
005: import org.vraptor.AbstractTest;
006: import org.vraptor.component.ComponentManager;
007: import org.vraptor.component.ComponentNotFoundException;
008: import org.vraptor.component.ComponentType;
009: import org.vraptor.component.InvalidComponentException;
010: import org.vraptor.component.LogicMethod;
011: import org.vraptor.component.LogicNotFoundException;
012:
013: import javax.servlet.http.HttpServletRequest;
014: import java.util.Set;
015:
016: /**
017: * Tests the default url dealer.
018: *
019: * @author Guilherme Silveira
020: */
021: public class DefaultLogicLocatorTest extends AbstractTest {
022:
023: private Mockery mockery;
024: private HttpServletRequest request;
025: private ComponentManager manager;
026: private DefaultLogicLocator locator;
027:
028: @Override
029: protected void setUp() throws Exception {
030: super .setUp();
031: mockery = new Mockery();
032: request = mockery.mock(HttpServletRequest.class);
033: locator = new DefaultLogicLocator(new FakeManager());
034: }
035:
036: private void useURL(final String contextPath, final String uri) {
037: mockery.checking(new Expectations() {
038: {
039: one(request).getContextPath();
040: will(returnValue(contextPath));
041: one(request).getRequestURI();
042: will(returnValue(uri));
043:
044: atLeast(1).of(request).getAttribute(
045: "javax.servlet.include.request_uri");
046: will(returnValue(null));
047: }
048: });
049: }
050:
051: public void testChecksSimpleUrl() throws Exception {
052: useURL("/context", "/context/component.action.logic");
053: LogicMethod logicMethod = locator.locate(request);
054:
055: assertEquals("action", logicMethod.getName());
056: assertEquals("component", logicMethod.getComponentType()
057: .getName());
058: mockery.assertIsSatisfied();
059: }
060:
061: public void testRootContextPath() throws LogicNotFoundException,
062: ComponentNotFoundException, InvalidURLException {
063: useURL("", "/component.action.logic");
064: LogicMethod logicMethod = locator.locate(request);
065:
066: assertEquals("action", logicMethod.getName());
067: assertEquals("component", logicMethod.getComponentType()
068: .getName());
069: mockery.assertIsSatisfied();
070: }
071:
072: public void testRootContextPathWithNamespaces()
073: throws LogicNotFoundException, ComponentNotFoundException,
074: InvalidURLException {
075: useURL("", "/module/component.action.logic");
076: LogicMethod logicMethod = locator.locate(request);
077:
078: assertEquals("action", logicMethod.getName());
079: assertEquals("module/component", logicMethod.getComponentType()
080: .getName());
081: mockery.assertIsSatisfied();
082: }
083:
084: public void testChecksUrlWithComponentModule()
085: throws InvalidURLException, LogicNotFoundException,
086: ComponentNotFoundException {
087: useURL("/context", "/context/module/component.action.logic");
088: LogicMethod logicMethod = locator.locate(request);
089:
090: assertEquals("action", logicMethod.getName());
091: assertEquals("module/component", logicMethod.getComponentType()
092: .getName());
093: mockery.assertIsSatisfied();
094: }
095:
096: public void testChecksUrlWithComplexComponentNamespace()
097: throws InvalidURLException, LogicNotFoundException,
098: ComponentNotFoundException {
099: useURL("/context",
100: "/context/super/complex/module/component.action.logic");
101: LogicMethod logicMethod = locator.locate(request);
102:
103: assertEquals("action", logicMethod.getName());
104: assertEquals("super/complex/module/component", logicMethod
105: .getComponentType().getName());
106: mockery.assertIsSatisfied();
107: }
108:
109: public void testChecksDifferentExtensionUrl()
110: throws InvalidURLException, LogicNotFoundException,
111: ComponentNotFoundException {
112: useURL("/context", "/context/component.action.weird");
113: LogicMethod logicMethod = locator.locate(request);
114:
115: assertEquals("action", logicMethod.getName());
116: assertEquals("component", logicMethod.getComponentType()
117: .getName());
118: mockery.assertIsSatisfied();
119: }
120:
121: public void testChecksUrlMissingExtension()
122: throws LogicNotFoundException, ComponentNotFoundException {
123: try {
124: useURL("/context", "/context/component.action");
125: locator.locate(request);
126: fail();
127: } catch (InvalidURLException e) {
128: // ok
129: }
130: }
131:
132: public void testChecksUrlMissingLogicName()
133: throws LogicNotFoundException, ComponentNotFoundException {
134: try {
135: useURL("/context", "/context/component.logic");
136: locator.locate(request);
137: fail();
138: } catch (InvalidURLException e) {
139: // ok
140: }
141: }
142:
143: public void testChecksUrlMissingLogicNameAndExtension()
144: throws LogicNotFoundException, ComponentNotFoundException {
145: try {
146: useURL("/context", "/context/component");
147: locator.locate(request);
148: fail();
149: } catch (InvalidURLException e) {
150: // ok
151: }
152: }
153:
154: public void testEncapsulatedURIForServerSideRequests()
155: throws InvalidURLException, LogicNotFoundException,
156: ComponentNotFoundException {
157: mockery.checking(new Expectations() {
158: {
159: one(request).getContextPath();
160: will(returnValue("/context"));
161: never(request).getRequestURI();
162:
163: atLeast(1).of(request).getAttribute(
164: "javax.servlet.include.request_uri");
165: will(returnValue("xpto.com/context/module/component.method.logic"));
166: }
167: });
168: LogicMethod logicMethod = locator.locate(request);
169: assertEquals("method", logicMethod.getName());
170: assertEquals("module/component", logicMethod.getComponentType()
171: .getName());
172: mockery.assertIsSatisfied();
173: }
174:
175: private class FakeManager implements ComponentManager {
176:
177: public ComponentType getComponent(final String name,
178: final String logicName)
179: throws ComponentNotFoundException,
180: LogicNotFoundException {
181: final ComponentType componentType = mockery
182: .mock(ComponentType.class);
183: final LogicMethod logicMethod = mockery
184: .mock(LogicMethod.class);
185: mockery.checking(new Expectations() {
186: {
187: allowing(componentType).getName();
188: will(returnValue(name));
189: allowing(logicMethod).getName();
190: will(returnValue(logicName));
191: allowing(componentType).getLogic(logicName);
192: will(returnValue(logicMethod));
193: allowing(logicMethod).getComponentType();
194: will(returnValue(componentType));
195: }
196: });
197: return componentType;
198: }
199:
200: public boolean register(String type)
201: throws InvalidComponentException {
202: return false; //To change body of implemented methods use File | Settings | File Templates.
203: }
204:
205: public Set<ComponentType> getComponents() {
206: return null; //To change body of implemented methods use File | Settings | File Templates.
207: }
208:
209: public void register(ComponentType type)
210: throws LogicNotFoundException {
211: }
212:
213: public ComponentType getComponentType(Class<?> type)
214: throws InvalidComponentException {
215: return null; //To change body of implemented methods use File | Settings | File Templates.
216: }
217: }
218:
219: }
|