001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis.type.basic;
019:
020: import javax.xml.namespace.QName;
021:
022: import org.apache.cxf.aegis.AbstractAegisTest;
023: import org.apache.cxf.aegis.Context;
024: import org.apache.cxf.aegis.type.DefaultTypeMappingRegistry;
025: import org.apache.cxf.aegis.type.TypeMapping;
026: import org.apache.cxf.aegis.type.TypeMappingRegistry;
027: import org.apache.cxf.aegis.xml.stax.ElementReader;
028: import org.junit.Test;
029:
030: public class DynamicProxyTest extends AbstractAegisTest {
031: TypeMapping mapping;
032:
033: public void setUp() throws Exception {
034: super .setUp();
035:
036: TypeMappingRegistry reg = new DefaultTypeMappingRegistry(true);
037: mapping = reg.createTypeMapping(true);
038: }
039:
040: @Test
041: public void testDynamicProxy() throws Exception {
042: BeanType type = new BeanType();
043: type.setTypeClass(IMyInterface.class);
044: type.setTypeMapping(mapping);
045: type.setSchemaType(new QName("urn:MyInterface", "data"));
046:
047: ElementReader reader = new ElementReader(
048: getResourceAsStream("MyInterface.xml"));
049: IMyInterface data = (IMyInterface) type.readObject(reader,
050: new Context());
051: assertEquals("junk", data.getName());
052: assertEquals(true, data.isUseless());
053: data.setName("bigjunk");
054: data.setUseless(false);
055: assertEquals("bigjunk", data.getName());
056: assertEquals(false, data.isUseless());
057:
058: assertTrue(data.hashCode() != 0);
059: assertTrue(data.equals(data));
060: // checkstyle isn't smart enough to know we're testing equals....
061: // assertFalse(data.equals(null));
062: // assertFalse("bigjunk".equals(data));
063: assertNotNull(data.toString());
064:
065: assertEquals("foo", data.getFOO());
066:
067: assertEquals(0, data.getNonSpecifiedInt());
068: }
069:
070: @Test
071: public void testDynamicProxyNonStandardGetter() throws Exception {
072: BeanType type = new BeanType();
073: type.setTypeClass(IMyInterface.class);
074: type.setTypeMapping(mapping);
075: type.setSchemaType(new QName("urn:MyInterface", "data"));
076:
077: ElementReader reader = new ElementReader(
078: getResourceAsStream("MyInterface.xml"));
079: IMyInterface data = (IMyInterface) type.readObject(reader,
080: new Context());
081:
082: try {
083: data.getNameById(0);
084: fail(IllegalAccessError.class + " should be thrown.");
085: } catch (IllegalAccessError e) {
086: // do nothing
087: }
088:
089: try {
090: data.get();
091: fail(IllegalAccessError.class + " should be thrown.");
092: } catch (IllegalAccessError e) {
093: // do nothing
094: }
095: }
096:
097: @Test
098: public void testDynamicProxyNonStandardSetter() throws Exception {
099: BeanType type = new BeanType();
100: type.setTypeClass(IMyInterface.class);
101: type.setTypeMapping(mapping);
102: type.setSchemaType(new QName("urn:MyInterface", "data"));
103:
104: ElementReader reader = new ElementReader(
105: getResourceAsStream("MyInterface.xml"));
106: IMyInterface data = (IMyInterface) type.readObject(reader,
107: new Context());
108:
109: try {
110: data.setNameNoParams();
111: fail(IllegalAccessError.class + " should be thrown.");
112: } catch (IllegalAccessError e) {
113: // do nothing
114: }
115:
116: try {
117: data.set();
118: fail(IllegalAccessError.class + " should be thrown.");
119: } catch (IllegalAccessError e) {
120: // do nothing
121: }
122: }
123:
124: @Test
125: public void testDynamicProxyNonGetterSetter() throws Exception {
126: BeanType type = new BeanType();
127: type.setTypeClass(IMyInterface.class);
128: type.setTypeMapping(mapping);
129: type.setSchemaType(new QName("urn:MyInterface", "data"));
130:
131: ElementReader reader = new ElementReader(
132: getResourceAsStream("MyInterface.xml"));
133: IMyInterface data = (IMyInterface) type.readObject(reader,
134: new Context());
135:
136: try {
137: data.doSomething();
138: fail(IllegalAccessError.class + " should be thrown.");
139: } catch (IllegalAccessError e) {
140: // do nothing
141: }
142: }
143:
144: @Test
145: public void testDynamicProxyMissingAttribute() throws Exception {
146: BeanType type = new BeanType();
147: type.setTypeClass(IMyInterface.class);
148: type.setTypeMapping(mapping);
149: type.setSchemaType(new QName("urn:MyInterface", "data"));
150:
151: ElementReader reader = new ElementReader(
152: getResourceAsStream("MyInterface.xml"));
153: IMyInterface data = (IMyInterface) type.readObject(reader,
154: new Context());
155:
156: assertEquals("junk", data.getName());
157: assertNull(data.getType());
158: }
159:
160: @Test
161: public void testDynamicProxyNested() throws Exception {
162: BeanType type = new BeanType();
163: type.setTypeClass(IMyInterface.class);
164: type.setSchemaType(new QName("urn:MyInterface", "myInterface"));
165: type.setTypeMapping(mapping);
166: BeanType type2 = new BeanType();
167: type2.setTypeClass(IMyInterface2.class);
168: type2.setSchemaType(new QName("urn:MyInterface2",
169: "myInterface2"));
170: type2.setTypeMapping(mapping);
171: type2.getTypeInfo().mapType(
172: new QName("urn:MyInterface", "myInterface"), type);
173:
174: ElementReader reader = new ElementReader(
175: getResourceAsStream("MyInterface2.xml"));
176: IMyInterface2 data = (IMyInterface2) type2.readObject(reader,
177: new Context());
178:
179: assertNotNull(data.getMyInterface());
180: assertEquals("junk", data.getMyInterface().getName());
181: assertEquals(true, data.getMyInterface().isUseless());
182: }
183:
184: public interface IMyInterface {
185: String getName();
186:
187: void setName(String name);
188:
189: boolean isUseless();
190:
191: void setUseless(boolean useless);
192:
193: String getNameById(int id);
194:
195: void setNameNoParams();
196:
197: void doSomething();
198:
199: String get();
200:
201: Integer set();
202:
203: String getType();
204:
205: String getFOO();
206:
207: int getNonSpecifiedInt();
208: }
209:
210: public interface IMyInterface2 {
211: IMyInterface getMyInterface();
212: }
213: }
|