001: package org.vraptor.util;
002:
003: import java.util.Collection;
004: import java.util.List;
005: import java.util.Map;
006:
007: import org.vraptor.AbstractTest;
008: import org.vraptor.annotations.In;
009: import org.vraptor.component.BeanConstructor;
010: import org.vraptor.component.ComponentInstantiationException;
011: import org.vraptor.component.ComponentType;
012: import org.vraptor.component.FieldAnnotation;
013: import org.vraptor.component.InvalidComponentException;
014: import org.vraptor.component.LogicMethod;
015: import org.vraptor.component.LogicNotFoundException;
016: import org.vraptor.interceptor.InterceptorType;
017: import org.vraptor.introspector.BeanProvider;
018: import org.vraptor.introspector.ReadParameter;
019: import org.vraptor.plugin.VRaptorPlugin;
020: import org.vraptor.reflection.GettingException;
021: import org.vraptor.reflection.MethodInvocationException;
022: import org.vraptor.scope.ScopeType;
023: import org.vraptor.util.StatsQuery.ComponentTypeNameComparator;
024: import org.vraptor.webapp.DefaultWebApplication;
025: import org.vraptor.webapp.WebApplication;
026:
027: public class StatsQueryTest extends AbstractTest {
028:
029: public void testLogicName() throws InvalidComponentException,
030: ComponentInstantiationException, LogicNotFoundException {
031:
032: boolean found = false;
033: ComponentType statsQuery = registry.getComponentManager()
034: .getComponentType(StatsQuery.class);
035:
036: for (LogicMethod logic : statsQuery.getLogics()) {
037: if ("read".equals(logic.getName())) {
038: found = true;
039: }
040: }
041:
042: assertTrue("Logic name read not found", found);
043: }
044:
045: public void testChecksDifferentComponentsGetSortedByName() {
046: StatsQuery query = new StatsQuery(createApp());
047: ComponentTypeNameComparator comp = query.new ComponentTypeNameComparator();
048: assertTrue(comp.compare(new MockType("a"), new MockType("b")) < 0);
049: }
050:
051: private WebApplication createApp() {
052: return new DefaultWebApplication(createServletContext());
053: }
054:
055: public void testSortsComponentsWithSameName() {
056: StatsQuery query = new StatsQuery(createApp());
057: ComponentTypeNameComparator comp = query.new ComponentTypeNameComparator();
058: assertTrue(comp.compare(new MockType("a"), new MockType("a")) == 0);
059: }
060:
061: class MockType implements ComponentType {
062:
063: private String name;
064:
065: public MockType(String n) {
066: this .name = n;
067: }
068:
069: public Class getComponentClass() {
070: return null;
071: }
072:
073: public String getDestroyLogicName() {
074: return null;
075: }
076:
077: public List<FieldAnnotation<In>> getInAnnotations() {
078: return null;
079: }
080:
081: public List<InterceptorType> getInterceptors() {
082: return null;
083: }
084:
085: public String getKey() {
086: return null;
087: }
088:
089: public LogicMethod getLogic(String key)
090: throws LogicNotFoundException {
091: return null;
092: }
093:
094: public Collection<LogicMethod> getLogics() {
095: return null;
096: }
097:
098: public String getName() {
099: return name;
100: }
101:
102: public List<ReadParameter> getReadParameters() {
103: return null;
104: }
105:
106: public ScopeType getScope() {
107: return null;
108: }
109:
110: public Object newInstance(BeanProvider provider)
111: throws ComponentInstantiationException {
112: return null;
113: }
114:
115: public Map<String, Object> getOutjectedValues(Object comp,
116: ScopeType scope) throws GettingException,
117: MethodInvocationException {
118: return null;
119: }
120:
121: public BeanConstructor getConstructor() {
122: return null;
123: }
124:
125: }
126:
127: public void testOutjectsAllComponents()
128: throws InvalidComponentException, LogicNotFoundException {
129: WebApplication app = createApp();
130: ComponentType c = createComponentType(Component.class);
131: app.getComponentManager().register(c);
132: StatsQuery query = new StatsQuery(app);
133: query.read();
134: assertTrue(query.getComponents().contains(c));
135: }
136:
137: public void testOutjectsAllPlugins()
138: throws InvalidComponentException {
139: WebApplication app = createApp();
140: SimplePlugin plugin = new SimplePlugin();
141: app.getPluginManager().register(plugin);
142: StatsQuery query = new StatsQuery(app);
143: query.read();
144: assertTrue(query.getPlugins().contains(plugin));
145: }
146:
147: public static class SimplePlugin implements VRaptorPlugin {
148: public void init(WebApplication application) {
149: }
150: }
151:
152: public static class Component {
153: public void logic() {
154: }
155: }
156:
157: }
|