001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import javassist.ClassPool;
018: import javassist.CtClass;
019: import javassist.Loader;
020: import javassist.LoaderClassPath;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.tapestry.annotations.ApplicationState;
024: import org.apache.tapestry.internal.InternalComponentResources;
025: import org.apache.tapestry.internal.test.InternalBaseTestCase;
026: import org.apache.tapestry.ioc.internal.services.PropertyAccessImpl;
027: import org.apache.tapestry.ioc.services.PropertyAccess;
028: import org.apache.tapestry.model.MutableComponentModel;
029: import org.apache.tapestry.services.ApplicationStateManager;
030: import org.apache.tapestry.services.ClassTransformation;
031: import org.apache.tapestry.services.ComponentClassTransformWorker;
032: import org.testng.annotations.AfterClass;
033: import org.testng.annotations.Test;
034:
035: public class ApplicationStateWorkerTest extends InternalBaseTestCase {
036: private final ClassLoader _contextClassLoader = Thread
037: .currentThread().getContextClassLoader();
038:
039: private PropertyAccess _access = new PropertyAccessImpl();
040:
041: @AfterClass
042: public void cleanup() {
043: _access = null;
044: }
045:
046: @Test
047: public void no_fields_with_annotation() {
048: ApplicationStateManager manager = newApplicationStateManager();
049: ClassTransformation ct = mockClassTransformation();
050: MutableComponentModel model = mockMutableComponentModel();
051:
052: train_findFieldsWithAnnotation(ct, ApplicationState.class);
053:
054: replay();
055:
056: ComponentClassTransformWorker worker = new ApplicationStateWorker(
057: manager);
058:
059: worker.transform(ct, model);
060:
061: verify();
062: }
063:
064: @SuppressWarnings("unchecked")
065: @Test
066: public void field_read_and_write() throws Exception {
067: ApplicationStateManager manager = newApplicationStateManager();
068: Log log = mockLog();
069: MutableComponentModel model = mockMutableComponentModel();
070: InternalComponentResources resources = mockInternalComponentResources();
071:
072: String componentClassName = StateHolder.class.getName();
073: Class asoClass = ReadOnlyBean.class;
074:
075: ClassPool pool = new ClassPool();
076: ClassLoader contextLoader = Thread.currentThread()
077: .getContextClassLoader();
078: pool.appendClassPath(new LoaderClassPath(contextLoader));
079:
080: Loader loader = new Loader(contextLoader, pool);
081:
082: loader.delegateLoadingOf("org.apache.tapestry.");
083:
084: CtClass ctClass = pool.get(componentClassName);
085: InternalClassTransformation transformation = new InternalClassTransformationImpl(
086: ctClass, _contextClassLoader, log, null);
087:
088: replay();
089:
090: new ApplicationStateWorker(manager).transform(transformation,
091: model);
092:
093: verify();
094:
095: transformation.finish();
096:
097: Class transformedClass = pool.toClass(ctClass, loader);
098:
099: Instantiator instantiator = transformation
100: .createInstantiator(transformedClass);
101:
102: Object component = instantiator.newInstance(resources);
103:
104: // Test the companion flag field
105:
106: expect(manager.exists(asoClass)).andReturn(true);
107:
108: replay();
109:
110: assertEquals(_access.get(component, "beanExists"), true);
111:
112: verify();
113:
114: // Test read property (get from ASM)
115:
116: Object aso = new ReadOnlyBean();
117:
118: train_get(manager, asoClass, aso);
119:
120: replay();
121:
122: assertSame(_access.get(component, "bean"), aso);
123:
124: verify();
125:
126: // Test write property (set ASM)
127:
128: Object aso2 = new ReadOnlyBean();
129:
130: manager.set(asoClass, aso2);
131:
132: replay();
133:
134: _access.set(component, "bean", aso2);
135:
136: verify();
137: }
138:
139: protected final ApplicationStateManager newApplicationStateManager() {
140: return newMock(ApplicationStateManager.class);
141: }
142:
143: protected final <T> void train_get(ApplicationStateManager manager,
144: Class<T> asoClass, T aso) {
145: expect(manager.get(asoClass)).andReturn(aso);
146: }
147: }
|