001 /*
002 * Created on Jan 4, 2005
003 *
004 * Copyright 2004 Chris Nelson
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
009 * Unless required by applicable law or agreed to in writing,
010 * software 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 limitations under the License.
013 */
014 package org.trails.component;
015
016 import java.util.Locale;
017
018 import org.apache.hivemind.Messages;
019 import org.apache.hivemind.impl.DefaultClassResolver;
020 import org.apache.hivemind.impl.MessageFinderImpl;
021 import org.apache.hivemind.impl.ModuleMessages;
022 import org.apache.hivemind.service.ThreadLocale;
023 import org.apache.hivemind.service.impl.ThreadLocaleImpl;
024 import org.apache.hivemind.util.ClasspathResource;
025 import org.apache.tapestry.test.Creator;
026 import org.jmock.Mock;
027 import org.jmock.MockObjectTestCase;
028 import org.trails.callback.CallbackStack;
029 import org.trails.descriptor.DescriptorService;
030 import org.trails.i18n.HiveMindMessageSource;
031 import org.trails.i18n.TrailsMessageSource;
032 import org.trails.page.EditPage;
033 import org.trails.persistence.PersistenceService;
034 import org.trails.test.TestUtils;
035 import org.trails.validation.TrailsValidationDelegate;
036
037 public class ComponentTest extends MockObjectTestCase
038 {
039 protected Creator creator = new Creator();
040 protected Mock descriptorServiceMock = new Mock(DescriptorService.class);
041 protected DescriptorService descriptorService;
042 protected CallbackStack callbackStack = new CallbackStack();
043 protected Mock persistenceMock = new Mock(PersistenceService.class);
044 protected TrailsValidationDelegate delegate = new TrailsValidationDelegate();
045 protected ThreadLocale threadLocale = new ThreadLocaleImpl(Locale.ENGLISH);
046
047 protected void setUp() throws Exception
048 {
049 descriptorService = (DescriptorService) descriptorServiceMock.proxy();
050 threadLocale.setLocale(Locale.ENGLISH);
051 }
052
053 public <T> T buildTrailsPage(Class<T> pageClass)
054 {
055 T page = (T) creator.newInstance(pageClass,
056 new Object[]{
057 "persistenceService", persistenceMock.proxy(),
058 "descriptorService", descriptorServiceMock.proxy(),
059 "callbackStack", callbackStack
060 });
061 return page;
062 }
063
064 protected EditPage buildEditPage()
065 {
066 DescriptorService descriptorService = (DescriptorService) descriptorServiceMock.proxy();
067 TrailsMessageSource messageSource = getNewTrailsMessageSource();
068
069 EditPage editPage = (EditPage) creator.newInstance(EditPage.class,
070 new Object[]{
071 "persistenceService", persistenceMock.proxy(),
072 "descriptorService", descriptorServiceMock.proxy(),
073 "callbackStack", callbackStack,
074 "delegate", delegate,
075 "resourceBundleMessageSource", messageSource
076 });
077 return editPage;
078 }
079
080 public TrailsMessageSource getNewTrailsMessageSource()
081 {
082 HiveMindMessageSource messageSource = new HiveMindMessageSource();
083 Messages hivemindMessages =
084 new ModuleMessages(
085 new MessageFinderImpl(new ClasspathResource(new DefaultClassResolver(), "messagestest.properties")),
086 threadLocale);
087 messageSource.setMessageSource(hivemindMessages);
088 return messageSource;
089 }
090
091 protected void tearDown() throws Exception
092 {
093 threadLocale.setLocale(Locale.ENGLISH);
094 TestUtils.cleanDescriptorInternationalizationAspect();
095 }
096 }