ALMA Computing Group

DynamicProxyFactoryTest.java

Go to the documentation of this file.
00001 /* 00002 * ALMA - Atacama Large Millimiter Array 00003 * (c) European Southern Observatory, 2002 00004 * Copyright by ESO (in the framework of the ALMA collaboration), 00005 * All rights reserved 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00020 * MA 02111-1307 USA 00021 */ 00022 package alma.acs.component.dynwrapper; 00023 00024 import java.lang.reflect.UndeclaredThrowableException; 00025 import java.util.ArrayList; 00026 import java.util.Arrays; 00027 import java.util.Random; 00028 import java.util.logging.Logger; 00029 00030 import org.exolab.castor.xml.MarshalException; 00031 00032 import junit.framework.TestCase; 00033 00034 import alma.ACS.ComponentStates; 00035 import alma.JContExmplErrTypeTest.XmlComponentErrorEx; 00036 import alma.acs.logging.ClientLogManager; 00037 import alma.demo.ObsProjectTree; 00038 import alma.demo.ObsProjectTreeJ; 00039 import alma.demo.SchedBlockHolder; 00040 import alma.demo.XmlComponentJ; 00041 import alma.demo.XmlComponentOperations; 00042 import alma.xmlentity.XmlEntityStruct; 00043 import alma.xmlentity.XmlEntityStructHolder; 00044 import alma.xmljbind.test.obsproposal.ObsProposal; 00045 import alma.xmljbind.test.obsproposal.ObsProposalEntityT; 00046 import alma.xmljbind.test.schedblock.SchedBlock; 00047 import alma.xmljbind.test.schedblock.SchedBlockControlT; 00048 import alma.xmljbind.test.schedblock.SchedBlockEntityT; 00049 00058 public class DynamicProxyFactoryTest extends TestCase 00059 { 00060 private Class corbaIF = alma.demo.XmlComponentOperations.class; 00061 private XmlComponentJ compImpl; 00062 private Class compIF = XmlComponentJ.class; 00063 private Logger m_logger = ClientLogManager.getAcsLogManager().getLoggerForApplication("DynamicProxyFactoryTest", false); 00064 00069 public DynamicProxyFactoryTest(String name) 00070 { 00071 super(name); 00072 } 00073 00077 protected void setUp() throws Exception 00078 { 00079 compImpl = new DynamicProxyFactoryTest.XmlTestComponent(); 00080 } 00081 00082 00083 public void testCreateServerProxy() throws DynWrapperException 00084 { 00085 assertNotNull(corbaIF); 00086 assertNotNull(compImpl); 00087 assertNotNull(compIF); 00088 00089 XmlComponentOperations serverProxy = createServerProxy(); 00090 00091 assertNotNull(serverProxy); 00092 } 00093 00094 private XmlComponentOperations createServerProxy() throws DynWrapperException 00095 { 00096 return (XmlComponentOperations) 00097 DynamicProxyFactory.getDynamicProxyFactory(m_logger). 00098 createServerProxy(corbaIF, compImpl, compIF); 00099 } 00100 00101 00102 public void testCallCreateObsProposal() throws DynWrapperException 00103 { 00104 XmlComponentOperations serverProxy = createServerProxy(); 00105 assertNotNull(serverProxy); 00106 00107 XmlEntityStruct entStruct = serverProxy.createObsProposal(); 00108 assertNotNull(entStruct); 00109 assertNotNull(entStruct.xmlString); 00110 00111 System.out.println("received ObsProposal as XML: " + entStruct.xmlString); 00112 } 00113 00114 public void testCallXmlInOutMethod() throws DynWrapperException 00115 { 00116 XmlComponentOperations serverProxy = createServerProxy(); 00117 assertNotNull(serverProxy); 00118 00119 XmlEntityStruct entStructObsProp = serverProxy.createObsProposal(); 00120 XmlEntityStructHolder xesh = new XmlEntityStructHolder(); 00121 00122 // send invalid xml 00123 entStructObsProp.xmlString = 00124 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + 00125 "<ns1:ObsProposal xmlns:ns1=\"AlmaTest/ObsProposal\">" + 00126 " <ns1:ObsProposalEntity entityIdEncrypted=\"ljasd;ljfa;lsfd\"" + 00127 " entityId=\"uid://X0000000000000000/X00000001\" entityTypeName=\"ObsProposal\"/>" + 00128 " <ns1:ContactPerson>Otis P. Driftwood.</ns1:ContactPerson>" + 00129 "</ns1:ObsProposal>"; 00130 serverProxy.xmlInOutMethod(entStructObsProp, xesh); 00131 assertNotNull(xesh.value); 00132 String xml = xesh.value.xmlString; 00133 assertNotNull(xml); 00134 00135 System.out.println("received out-param SchedBlock as XML: " + xml); 00136 } 00137 00138 public void testCallGetAllSchedBlocks() throws DynWrapperException 00139 { 00140 XmlComponentOperations serverProxy = createServerProxy(); 00141 assertNotNull(serverProxy); 00142 00143 XmlEntityStruct[] entStructSchedBlocks = serverProxy.getAllSchedBlocks(); 00144 00145 assertNotNull(entStructSchedBlocks); 00146 System.out.println("received SchedBlocks as XML: "); 00147 for (int i = 0; i < entStructSchedBlocks.length; i++) 00148 { 00149 XmlEntityStruct structSB = entStructSchedBlocks[i]; 00150 assertNotNull(structSB); 00151 assertNotNull(structSB.xmlString); 00152 System.out.println(structSB.xmlString); 00153 } 00154 } 00155 00156 public void testCallGetEntireTreeInAStruct() throws DynWrapperException 00157 { 00158 XmlComponentOperations serverProxy = createServerProxy(); 00159 assertNotNull(serverProxy); 00160 00161 ObsProjectTree struct = serverProxy.getEntireTreeInAStruct(); 00162 00163 assertNotNull("returned ObsProjectTree object not null", struct); 00164 00165 assertNotNull("ObsProposal not null", struct.prop); 00166 System.out.println("received ObsProposal as XML: " + struct.prop.xmlString); 00167 00168 assertNotNull("SchedBlockArray not null", struct.schedBlocks); 00169 assertTrue("SchedBlockArray not empty", struct.schedBlocks.length > 0); 00170 00171 // System.out.println(); 00172 } 00173 00174 00180 public void testEmptyXml() throws DynWrapperException 00181 { 00182 XmlComponentOperations serverProxy = createServerProxy(); 00183 assertNotNull(serverProxy); 00184 00185 // just to have a valid struct 00186 XmlEntityStruct entStructObsProp = serverProxy.createObsProposal(); 00187 XmlEntityStructHolder xesh = new XmlEntityStructHolder(); 00188 00189 // send empty xml 00190 entStructObsProp.xmlString = " "; 00191 serverProxy.xmlInOutMethod(entStructObsProp, xesh); 00192 // SchedBlock struct must be null 00193 assertNull(xesh.value); 00194 } 00195 00196 00197 public void testInvalidXml() throws DynWrapperException 00198 { 00199 XmlComponentOperations serverProxy = createServerProxy(); 00200 assertNotNull(serverProxy); 00201 00202 XmlEntityStruct entStructObsProp = serverProxy.createObsProposal(); 00203 XmlEntityStructHolder xesh = new XmlEntityStructHolder(); 00204 00205 // send invalid xml 00206 entStructObsProp.xmlString = 00207 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + 00208 // "<ns1:ObsProposal xmlns:ns1=\"AlmaTest/ObsProposal\">" + 00209 " <ns1:ObsProposalEntity entityIdEncrypted=\"ljasd;ljfa;lsfd\"" + 00210 " entityId=\"uid://X0000000000000000/X00000001\" entityTypeName=\"ObsProposal\"/>" + 00211 " <ns1:PerformanceGoals>peak performance enduring a 24-7-365 schedule.</ns1:PerformanceGoals>" + 00212 "</ns1:ObsProposal>"; 00213 00214 try 00215 { 00216 serverProxy.xmlInOutMethod(entStructObsProp, xesh); 00217 fail("invalid XML should have caused an exception"); 00218 } 00219 catch (UndeclaredThrowableException e) 00220 { 00221 Throwable cause = e.getCause(); 00222 assertTrue(cause instanceof DynWrapperException); 00223 00224 Throwable cause2 = cause.getCause(); 00225 assertTrue(cause2 instanceof MarshalException); 00226 assertTrue(((MarshalException)cause2).getCause() instanceof org.xml.sax.SAXException); 00227 } 00228 } 00229 00230 00231 00232 00237 private static class XmlTestComponent implements XmlComponentJ 00238 { 00239 private ArrayList<SchedBlock> m_schedBlockList = new ArrayList<SchedBlock>(); 00240 00241 XmlTestComponent() 00242 { 00243 populateListWithStupidIncompleteSBs(); 00244 } 00245 00252 public ComponentStates componentState() 00253 { 00254 return ComponentStates.COMPSTATE_OPERATIONAL; 00255 } 00256 00261 public String name() 00262 { 00263 return "not a component, just for testing"; 00264 } 00265 00269 public void addNewSchedBlocks(SchedBlock[] newSchedBlocks) 00270 { 00271 System.out.println("received call to addNewSchedBlocks in component implementation."); 00272 m_schedBlockList.addAll(Arrays.asList(newSchedBlocks)); 00273 } 00274 00278 public ObsProposal createObsProposal() 00279 { 00280 System.out.println("received call to createObsProposal in component implementation."); 00281 ObsProposal obsProp = new ObsProposal(); 00282 00283 ObsProposalEntityT entity = new ObsProposalEntityT(); 00284 entity.setEntityId("uid://X0000000000000000/X00000001"); 00285 entity.setEntityIdEncrypted("ljasd;ljfa;lsfd"); 00286 obsProp.setObsProposalEntity(entity); 00287 00288 obsProp.setScientificJustification("peak performance enduring a 24-7-365 schedule."); 00289 00290 return obsProp; 00291 } 00292 00296 public int dumbMethod(String somevalue) 00297 { 00298 System.out.println("received call to 'dumbMethod' in component implementation. " + 00299 "parameter somevalue = " + somevalue); 00300 return 1234567; 00301 } 00302 00306 public SchedBlock[] getAllSchedBlocks() 00307 { 00308 System.out.println("received call to 'getAllSchedBlocks' in component implementation."); 00309 return m_schedBlockList.toArray(new SchedBlock[0]); 00310 } 00311 00315 public SchedBlock getBestSchedBlock() 00316 { 00317 System.out.println("received call to 'getBestSchedBlock' in component implementation."); 00318 Random random = new Random(System.currentTimeMillis()); 00319 int sbIndex = random.nextInt(m_schedBlockList.size()); 00320 00321 SchedBlock sb = m_schedBlockList.get(sbIndex); 00322 return sb; 00323 } 00324 00328 public String sayHello() 00329 { 00330 System.out.println("received call to 'sayHello' in component implementation."); 00331 return "hello"; 00332 } 00333 00340 public void xmlInOutMethod(ObsProposal obsPropIn, SchedBlockHolder schedBlockOut) 00341 { 00342 System.out.println("received call to 'xmlInOutMethod' in component implementation."); 00343 00344 SchedBlock sb = null; 00345 00346 if (obsPropIn != null) 00347 { 00348 sb = getBestSchedBlock(); 00349 assertNotNull(sb); 00350 } 00351 schedBlockOut.value = sb; 00352 } 00353 00354 00358 public ObsProjectTreeJ getEntireTreeInAStruct() 00359 { 00360 ObsProjectTreeJ struct = new ObsProjectTreeJ(); 00361 00362 struct.prop = createObsProposal(); 00363 struct.schedBlocks = getAllSchedBlocks(); 00364 00365 return struct; 00366 } 00367 00368 00369 private void populateListWithStupidIncompleteSBs() 00370 { 00371 for (int sbCount = 0; sbCount < 10; sbCount++) 00372 { 00373 SchedBlock sb = new SchedBlock(); 00374 00375 SchedBlockEntityT entity = new SchedBlockEntityT(); 00376 entity.setEntityId("uid://X0000000000000000/X0000000" + Integer.toHexString(sbCount)); 00377 entity.setEntityIdEncrypted("not yet encrypted"); 00378 sb.setSchedBlockEntity(entity); 00379 00380 SchedBlockControlT sbCtrl = new SchedBlockControlT(); 00381 sb.setSchedBlockControl(sbCtrl); 00382 sbCtrl.setRepeatCount(sbCount%3); 00383 00384 m_schedBlockList.add(sb); 00385 } 00386 } 00387 00388 public void exceptionMethod() throws XmlComponentErrorEx 00389 { 00390 throw new XmlComponentErrorEx(); 00391 } 00392 00393 00394 } 00395 00396 } 00397