1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.Locale ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.impl.DefaultClassResolver; 21 import org.apache.hivemind.test.HiveMindTestCase; 22 import org.apache.tapestry.IEngine; 23 import org.apache.tapestry.engine.AbstractEngine; 24 import org.apache.tapestry.engine.BaseEngine; 25 import org.apache.tapestry.spec.IApplicationSpecification; 26 import org.easymock.MockControl; 27 28 34 public class TestEngineFactory extends HiveMindTestCase 35 { 36 public void testUseDefault() 37 { 38 MockControl specControl = newControl(IApplicationSpecification.class); 39 IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock(); 40 41 43 spec.getEngineClassName(); 44 specControl.setReturnValue(null); 45 46 EngineFactoryImpl f = new EngineFactoryImpl(); 47 48 f.setApplicationSpecification(spec); 49 f.setClassResolver(new DefaultClassResolver()); 50 f.setDefaultEngineClassName(BaseEngine.class.getName()); 51 52 replayControls(); 53 54 f.initializeService(); 55 56 IEngine result = f.constructNewEngineInstance(Locale.CANADA_FRENCH); 57 58 assertTrue(result instanceof BaseEngine); 59 assertEquals(Locale.CANADA_FRENCH, result.getLocale()); 60 61 verifyControls(); 62 } 63 64 public void testDefinedInSpec() 65 { 66 MockControl specControl = newControl(IApplicationSpecification.class); 67 IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock(); 68 69 71 spec.getEngineClassName(); 72 specControl.setReturnValue(EngineFixture.class.getName()); 73 74 EngineFactoryImpl f = new EngineFactoryImpl(); 75 76 f.setApplicationSpecification(spec); 77 f.setClassResolver(new DefaultClassResolver()); 78 79 replayControls(); 80 81 f.initializeService(); 82 83 IEngine result = f.constructNewEngineInstance(Locale.CHINESE); 84 85 assertTrue(result instanceof EngineFixture); 86 assertEquals(Locale.CHINESE, result.getLocale()); 87 88 verifyControls(); 89 } 90 91 public void testUnableToInstantiate() 92 { 93 MockControl specControl = newControl(IApplicationSpecification.class); 94 IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock(); 95 96 98 spec.getEngineClassName(); 99 specControl.setReturnValue(AbstractEngine.class.getName()); 100 101 EngineFactoryImpl f = new EngineFactoryImpl(); 102 103 f.setApplicationSpecification(spec); 104 f.setClassResolver(new DefaultClassResolver()); 105 106 replayControls(); 107 108 f.initializeService(); 109 110 try 111 { 112 f.constructNewEngineInstance(Locale.CHINESE); 113 unreachable(); 114 } 115 catch (ApplicationRuntimeException ex) 116 { 117 assertExceptionSubstring( 118 ex, 119 "Unable to instantiate engine as instance of class org.apache.tapestry.engine.AbstractEngine"); 120 } 121 122 verifyControls(); 123 } 124 125 public void testInvalidClass() 126 { 127 MockControl specControl = newControl(IApplicationSpecification.class); 128 IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock(); 129 130 132 spec.getEngineClassName(); 133 specControl.setReturnValue("foo.XyzzYx"); 134 135 EngineFactoryImpl f = new EngineFactoryImpl(); 136 137 f.setApplicationSpecification(spec); 138 f.setClassResolver(new DefaultClassResolver()); 139 140 replayControls(); 141 142 try 143 { 144 f.initializeService(); 145 unreachable(); 146 } 147 catch (ApplicationRuntimeException ex) 148 { 149 assertExceptionSubstring(ex, "Could not load class foo.XyzzYx"); 150 } 151 152 verifyControls(); 153 } 154 } | Popular Tags |