001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.create;
017:
018: import org.junit.Assert;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.directwebremoting.create.test.DummyDataManager;
024: import org.directwebremoting.extend.Creator;
025: import org.directwebremoting.spring.SpringCreator;
026: import org.junit.Before;
027: import org.junit.Ignore;
028: import org.junit.Test;
029: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
030: import org.springframework.context.support.StaticApplicationContext;
031:
032: /**
033: * @author Bram Smeets
034: * @author Joe Walker [joe at getahead dot ltd dot uk]
035: */
036: public class SpringCreatorTest {
037: private SpringCreator creator;
038:
039: @Before
040: public void setUp() throws Exception {
041: creator = new SpringCreator();
042:
043: Map<String, String> params = new HashMap<String, String>();
044: params.put("location",
045: "/uk/ltd/getahead/dwr/create/spring-beans.xml");
046: creator.setProperties(params);
047:
048: creator.setBeanName("dataManager");
049: }
050:
051: @Test
052: public void testBeanName() {
053: String beanName = "beanName";
054: creator.setBeanName(beanName);
055: Assert.assertEquals(beanName, creator.getBeanName());
056: }
057:
058: @Test
059: public void testGetScope() {
060: // make sure the default scope is the PAGE scope
061: Assert.assertEquals(Creator.PAGE, creator.getScope());
062: }
063:
064: @Ignore
065: @Test
066: public void testGetType() {
067: Assert.assertEquals(DummyDataManager.class, creator.getType());
068: }
069:
070: @Ignore
071: @Test
072: public void testGetInstance() throws Exception {
073: DummyDataManager mgr = (DummyDataManager) creator.getInstance();
074: Assert.assertEquals(new DummyDataManager(), mgr);
075: }
076:
077: @Ignore
078: @Test(expected=NoSuchBeanDefinitionException.class)
079: public void testNonExistingBean() throws Exception {
080: creator.setBeanName("nonExistingBean");
081: creator.getInstance();
082: }
083:
084: @Test
085: public void testOverrideBeanFactory() throws Exception {
086: StaticApplicationContext ctx = new StaticApplicationContext();
087: ctx.registerSingleton("dataManager", DummyDataManager.class);
088: SpringCreator.setOverrideBeanFactory(ctx);
089:
090: Assert.assertEquals(DummyDataManager.class, creator.getType());
091:
092: DummyDataManager mgr = (DummyDataManager) creator.getInstance();
093: Assert.assertEquals(new DummyDataManager(), mgr);
094:
095: SpringCreator.setOverrideBeanFactory(null);
096: }
097:
098: @Ignore
099: @Test(expected=IllegalStateException.class)
100: public void testWithoutBeanNameWithRequest() throws Exception {
101: SpringCreator creator2 = new SpringCreator();
102: creator2.setBeanName("dataManager");
103: creator2.getInstance();
104: }
105:
106: @Ignore
107: @Test(expected=IllegalStateException.class)
108: public void testWithoutBeanNameWithoutRequest() throws Exception {
109: SpringCreator creator2 = new SpringCreator();
110: creator2.setBeanName("dataManager");
111: creator2.getInstance();
112: }
113:
114: @Test(expected=IllegalArgumentException.class)
115: public void testSetClass() throws Exception {
116: SpringCreator creator2 = new SpringCreator();
117: creator2.setClass(getClass().getName());
118: creator2.setClass("NonExistingClass");
119: }
120: }
|