001: package de.schlund.pfixcore.beans;
002:
003: import java.net.MalformedURLException;
004: import java.net.URL;
005: import java.util.HashSet;
006: import java.util.Set;
007:
008: import junit.framework.TestCase;
009:
010: import org.apache.log4j.ConsoleAppender;
011: import org.apache.log4j.Level;
012: import org.apache.log4j.Logger;
013: import org.apache.log4j.PatternLayout;
014:
015: import de.schlund.pfixcore.beans.BeanDescriptor;
016: import de.schlund.pfixcore.beans.BeanDescriptorFactory;
017: import de.schlund.pfixcore.beans.InitException;
018: import de.schlund.pfixcore.beans.metadata.Bean;
019: import de.schlund.pfixcore.beans.metadata.Beans;
020: import de.schlund.pfixcore.beans.metadata.DefaultLocator;
021: import de.schlund.pfixcore.beans.metadata.Locator;
022:
023: public class BeanDescriptorTest extends TestCase {
024:
025: public void testPropertyDetection() throws InitException {
026:
027: ConsoleAppender appender = new ConsoleAppender(
028: new PatternLayout("%p: %m\n"));
029: Logger logger = Logger.getRootLogger();
030: logger.setLevel((Level) Level.ERROR);
031: logger.removeAllAppenders();
032: logger.addAppender(appender);
033:
034: URL url = getClass().getResource("beanmetadata.xml");
035: if (url == null) {
036: try {
037: url = new URL(
038: "file:/tests/junit/de/schlund/pfixcore/webservice/beans/beanmetadata.xml");
039: } catch (MalformedURLException x) {
040: throw new RuntimeException(x);
041: }
042: }
043: Locator locator = new DefaultLocator(url);
044: BeanDescriptorFactory beanDescFactory = new BeanDescriptorFactory(
045: locator);
046:
047: //BeanA + BeanD
048:
049: Set<String> expProps = new HashSet<String>();
050: expProps.add("foo");
051: expProps.add("baz");
052: expProps.add("mytest");
053:
054: //BeanA annotations
055: BeanDescriptor beanDesc = new BeanDescriptor(BeanA.class);
056: assertEquals(expProps, beanDesc.getReadableProperties());
057:
058: //BeanA metadata
059: Beans beans = new Beans();
060: Bean bean = new Bean(BeanA.class.getName());
061: bean.excludeProperty("bar");
062: bean.setPropertyAlias("test", "mytest");
063: beans.setBean(bean);
064: beanDesc = new BeanDescriptor(BeanA.class, beans);
065: assertEquals(expProps, beanDesc.getReadableProperties());
066:
067: //BeanA xml metadata
068: beanDesc = beanDescFactory.getBeanDescriptor(BeanA.class);
069: assertEquals(expProps, beanDesc.getReadableProperties());
070:
071: //BeanD annotations
072: beanDesc = new BeanDescriptor(BeanD.class);
073: assertEquals(expProps, beanDesc.getReadableProperties());
074:
075: //BeanD metadata
076: beans = new Beans();
077: bean = new Bean(BeanD.class.getName());
078: bean.excludeByDefault();
079: bean.includeProperty("foo");
080: bean.includeProperty("baz");
081: bean.includeProperty("test");
082: bean.setPropertyAlias("test", "mytest");
083: beans.setBean(bean);
084: beanDesc = new BeanDescriptor(BeanD.class, beans);
085: assertEquals(expProps, beanDesc.getReadableProperties());
086:
087: //BeanD xml metadata
088: beanDesc = beanDescFactory.getBeanDescriptor(BeanD.class);
089: assertEquals(expProps, beanDesc.getReadableProperties());
090:
091: //BeanB + BeanE
092:
093: expProps = new HashSet<String>();
094: expProps.add("foo");
095: expProps.add("baz");
096: expProps.add("mytest");
097: expProps.add("my");
098:
099: //BeanB annotations
100: beanDesc = new BeanDescriptor(BeanB.class);
101: assertEquals(expProps, beanDesc.getReadableProperties());
102:
103: //BeanB metadata
104: beans = new Beans();
105: bean = new Bean(BeanB.class.getName());
106: bean.excludeByDefault();
107: bean.includeProperty("my");
108: beans.setBean(bean);
109: beanDesc = new BeanDescriptor(BeanB.class, beans);
110: assertEquals(expProps, beanDesc.getReadableProperties());
111:
112: //BeanB xml metadata
113: beanDesc = beanDescFactory.getBeanDescriptor(BeanB.class);
114: assertEquals(expProps, beanDesc.getReadableProperties());
115:
116: //BeanE annotations
117: beanDesc = new BeanDescriptor(BeanE.class);
118: assertEquals(expProps, beanDesc.getReadableProperties());
119:
120: //BeanE metadata
121: beans = new Beans();
122: bean = new Bean(BeanE.class.getName());
123: bean.excludeProperty("hey");
124: bean.excludeProperty("ho");
125: beans.setBean(bean);
126: beanDesc = new BeanDescriptor(BeanE.class, beans);
127: assertEquals(expProps, beanDesc.getReadableProperties());
128:
129: //BeanE xml metadata
130: beanDesc = beanDescFactory.getBeanDescriptor(BeanE.class);
131: assertEquals(expProps, beanDesc.getReadableProperties());
132:
133: //Test with beanmetadata from META-INF
134: locator = new DefaultLocator();
135: beanDescFactory = new BeanDescriptorFactory(locator);
136:
137: //BeanC + BeanF
138:
139: expProps = new HashSet<String>();
140: expProps.add("mybar");
141: expProps.add("baz");
142: expProps.add("mytest");
143: expProps.add("my");
144: expProps.add("hey");
145:
146: //BeanC annotations
147: beanDesc = new BeanDescriptor(BeanC.class);
148: assertEquals(expProps, beanDesc.getReadableProperties());
149:
150: //BeanC metadata
151: beans = new Beans();
152: bean = new Bean(BeanC.class.getName());
153: bean.excludeProperty("foo");
154: bean.setPropertyAlias("bar", "mybar");
155: beans.setBean(bean);
156: beanDesc = new BeanDescriptor(BeanC.class, beans);
157: assertEquals(expProps, beanDesc.getReadableProperties());
158:
159: //BeanC xml metadata
160: beanDesc = beanDescFactory.getBeanDescriptor(BeanC.class);
161: assertEquals(expProps, beanDesc.getReadableProperties());
162:
163: //BeanF annotations
164: beanDesc = new BeanDescriptor(BeanF.class);
165: assertEquals(expProps, beanDesc.getReadableProperties());
166:
167: //BeanF metadata
168: beans = new Beans();
169: bean = new Bean(BeanF.class.getName());
170: bean.excludeByDefault();
171: bean.includeProperty("hey");
172: bean.includeProperty("bar");
173: bean.setPropertyAlias("bar", "mybar");
174: beans.setBean(bean);
175: beanDesc = new BeanDescriptor(BeanF.class, beans);
176: assertEquals(expProps, beanDesc.getReadableProperties());
177:
178: //BeanF xml metadata
179: beanDesc = beanDescFactory.getBeanDescriptor(BeanF.class);
180: assertEquals(expProps, beanDesc.getReadableProperties());
181:
182: }
183:
184: public static void main(String[] args) throws Exception {
185: BeanDescriptorTest test = new BeanDescriptorTest();
186: test.testPropertyDetection();
187: }
188:
189: }
|