ALMA Computing Group

ErrorComponentTest.java

Go to the documentation of this file.
00001 package alma.acsexmpl.clients; 00002 00003 import alma.ACSErrTypeCommon.ACSErrTypeCommonEx; 00004 import alma.ACSErrTypeCommon.GenericErrorEx; 00005 import alma.ACSErrTypeCommon.UnexpectedExceptionEx; 00006 import alma.ACSErrTypeCommon.wrappers.AcsJCORBAProblemEx; 00007 import alma.ACSErrTypeCommon.wrappers.AcsJGenericErrorEx; 00008 import alma.ACSErrTypeCommon.wrappers.AcsJUnexpectedExceptionEx; 00009 import alma.ACSErrTypeCommon.wrappers.AcsJUnknownEx; 00010 import alma.acs.component.client.ComponentClientTestCase; 00011 import alma.acs.exceptions.AcsJCompletion; 00012 import alma.acs.exceptions.AcsJException; 00013 import alma.acsexmplErrorComponent.ErrorComponent; 00014 import alma.acsexmplErrorComponent.ErrorComponentHelper; 00015 00016 00017 public class ErrorComponentTest extends ComponentClientTestCase { 00018 00019 private static final String errorCompName = "ERRORCOMP_JAVA"; 00020 private ErrorComponent errorComp; 00021 00022 public ErrorComponentTest() throws Exception { 00023 super("ErrorComponentTest"); 00024 } 00025 00026 protected void setUp() throws Exception { 00027 super.setUp(); 00028 errorComp = ErrorComponentHelper.narrow(getContainerServices().getComponent(errorCompName)); 00029 assertEquals(errorCompName, errorComp.name()); 00030 } 00031 00032 protected void tearDown() throws Exception { 00033 getContainerServices().releaseComponent(errorCompName); 00034 super.tearDown(); 00035 } 00036 00037 00038 public void testDisplayMessage() { 00039 errorComp.displayMessage(); 00040 } 00041 00042 00043 public void testBadMethod() { 00044 try { 00045 // call the component method 00046 errorComp.badMethod((short)0); 00047 } catch (GenericErrorEx e) { 00048 fail("No exception should be thrown for depth=0"); 00049 } catch (UnexpectedExceptionEx e){ 00050 AcsJUnexpectedExceptionEx jEx = AcsJUnexpectedExceptionEx.fromUnexpectedExceptionEx(e); 00051 jEx.log(m_logger); 00052 fail("No exception should be thrown for depth=0"); 00053 } 00054 00055 short[] depths = new short[] {1, 2, 5}; 00056 for (int i = 0; i < depths.length; i++) { 00057 try { 00058 // call the component method 00059 errorComp.badMethod((depths[i])); 00060 fail("Expected a GenericErrorEx in ErrorComponent#badMethod for depth=" + depths[i]); 00061 } catch (GenericErrorEx e) { 00062 m_logger.info("Caught GenericErrorEx as expected. Depth=" + depths[i]); 00063 AcsJGenericErrorEx jEx = AcsJGenericErrorEx.fromGenericErrorEx(e); 00064 verifyErrorTrace(depths[i], jEx); 00065 } catch (UnexpectedExceptionEx e){ 00066 AcsJUnexpectedExceptionEx jEx = AcsJUnexpectedExceptionEx.fromUnexpectedExceptionEx(e); 00067 jEx.log(m_logger); 00068 fail("Expected a GenericErrorEx in ErrorComponent#badMethod for depth=" + depths[i]); 00069 } 00070 } 00071 } 00072 00073 00074 public void testExceptionFromCompletion() { 00075 // depth == 0 00076 try { 00077 // call the component method 00078 errorComp.exceptionFromCompletion((short)0); 00079 } catch (GenericErrorEx e) { 00080 fail("No exception should be thrown for depth=0"); 00081 } 00082 // depth > 0 00083 short[] depths = new short[] {1, 2, 3, 5, 13}; 00084 for (int i = 0; i < depths.length; i++) { 00085 try { 00086 // call the component method 00087 errorComp.exceptionFromCompletion((depths[i])); 00088 fail("Expected a GenericErrorEx in ErrorComponent#badMethod for depth=" + depths[i]); 00089 } catch (GenericErrorEx e) { 00090 m_logger.info("Caught GenericErrorEx as expected. Depth=" + depths[i]); 00091 AcsJGenericErrorEx jEx = AcsJGenericErrorEx.fromGenericErrorEx(e); 00092 verifyErrorTrace(depths[i], jEx); 00093 } 00094 } 00095 } 00096 00097 public void testTypeException() { 00098 // depth == 0 00099 try { 00100 // call the component method 00101 errorComp.typeException((short)0); 00102 } catch (GenericErrorEx e) { 00103 fail("No exception should be thrown for depth=0"); 00104 } catch (ACSErrTypeCommonEx e) { 00105 fail("No exception should be thrown for depth=0"); 00106 } 00107 // depth > 0 00108 short[] depths = new short[] {1, 2, 3, 5, 13}; 00109 for (int i = 0; i < depths.length; i++) { 00110 try { 00111 // call the component method 00112 errorComp.typeException((depths[i])); 00113 fail("Expected a GenericErrorEx in ErrorComponent#badMethod for depth=" + depths[i]); 00114 } catch (GenericErrorEx e) { 00115 fail("The declared 'GenericErrorEx' should never be thrown by the Java implementation."); 00116 } catch (ACSErrTypeCommonEx e) { 00117 m_logger.info("Caught GenericErrorEx as expected. Depth=" + depths[i]); 00118 // TODO: add 'fromXYZ' method to generated type-exceptions. These would no longer be abstract, but have protected ctors. 00119 // AcsJACSErrTypeCommonEx jEx = AcsJACSErrTypeCommonEx.fromAcsJACSErrTypeCommonEx(e); 00120 // verifyErrorTrace(depths[i], jEx); 00121 // while we do not have the static fromXYZ method, we convert the exception by wrapping it, and take account of the depth increase 00122 verifyErrorTrace(depths[i] + 1, new AcsJGenericErrorEx(e)); 00123 } 00124 } 00125 } 00126 00127 public void testReceiveCorbaSystemException() { 00128 try { 00129 errorComp.corbaSystemException(); 00130 } 00131 /* 00132 * This shows how to map a CORBA System Exception into the 00133 * corresponding ACS Exception wrapper 00134 */ 00135 catch(org.omg.CORBA.SystemException ex) { 00136 m_logger.info("Caught CORBA.SystemException"); 00137 AcsJCORBAProblemEx corbaProblemEx = new AcsJCORBAProblemEx(ex); 00138 corbaProblemEx.setMinor(ex.minor); 00139 corbaProblemEx.setCompletionStatus(ex.completed.value()); 00140 corbaProblemEx.setInfo(ex.toString()); 00141 corbaProblemEx.log(m_logger); 00142 } catch(Throwable th) { 00143 m_logger.info("Caught an unexpected Exception"); 00144 AcsJGenericErrorEx badMethodEx = new AcsJGenericErrorEx(th); 00145 badMethodEx.setErrorDesc("corbaSystemException has thrown an UNEXPECTED exception"); 00146 badMethodEx.log(m_logger); 00147 fail("Expected a CORBA System Exception"); 00148 } 00149 00150 } 00151 00152 public void testCompletionFromException() { 00153 // depth == 0 00154 AcsJCompletion comp=null; 00155 try { 00156 // call the component method 00157 comp = AcsJCompletion.fromCorbaCompletion(errorComp.completionFromException((short)0)); 00158 } catch (Throwable th) { 00159 m_logger.info("Caught an unexpected Exception"); 00160 AcsJUnknownEx ex = new AcsJUnknownEx(th); 00161 ex.log(m_logger); 00162 fail("No exception should be thrown"); 00163 } 00164 verifyErrorTrace(1, new AcsJGenericErrorEx(comp.getAcsJException())); 00165 00166 // depth > 0 00167 short[] depths = new short[] {1, 2, 3, 5, 13}; 00168 for (int i = 0; i < depths.length; i++) { 00169 try { 00170 // call the component method 00171 comp = AcsJCompletion.fromCorbaCompletion(errorComp.completionFromException((depths[i]))); 00172 } catch (Throwable th) { 00173 m_logger.info("Caught an unexpected Exception at depth "+depths[i]); 00174 AcsJUnknownEx ex = new AcsJUnknownEx(th); 00175 ex.log(m_logger); 00176 fail("No exception should be thrown"); 00177 } 00178 verifyErrorTrace(depths[i]+1, new AcsJGenericErrorEx(comp.getAcsJException())); 00179 } 00180 00181 } 00182 00183 public void testCompletionFromCompletion() { 00184 // depth == 0 00185 AcsJCompletion comp=null; 00186 try { 00187 // call the component method 00188 comp = AcsJCompletion.fromCorbaCompletion(errorComp.completionFromCompletion((short)0)); 00189 } catch (Throwable th) { 00190 m_logger.info("Caught an unexpected Exception"); 00191 AcsJUnknownEx ex = new AcsJUnknownEx(th); 00192 ex.log(m_logger); 00193 fail("No exception should be thrown"); 00194 } 00195 verifyErrorTrace(1, new AcsJGenericErrorEx(comp.getAcsJException())); 00196 00197 // depth > 0 00198 short[] depths = new short[] {1, 2, 3, 5, 13}; 00199 for (int i = 0; i < depths.length; i++) { 00200 try { 00201 // call the component method 00202 comp = AcsJCompletion.fromCorbaCompletion(errorComp.completionFromCompletion((depths[i]))); 00203 } catch (Throwable th) { 00204 m_logger.info("Caught an unexpected Exception at depth "+depths[i]); 00205 AcsJUnknownEx ex = new AcsJUnknownEx(th); 00206 ex.log(m_logger); 00207 fail("No exception should be thrown"); 00208 } 00209 verifyErrorTrace(depths[i]+1, new AcsJGenericErrorEx(comp.getAcsJException())); 00210 } 00211 } 00212 00213 public void testCompletionOnStack() { 00214 //fail("Not yet implemented"); 00215 } 00216 00217 public void testOutCompletion() { 00218 alma.ACSErr.CompletionHolder comp= new alma.ACSErr.CompletionHolder(); 00219 try{ 00220 errorComp.outCompletion(comp); 00221 } catch(Throwable th) { 00222 m_logger.info("Caught an unexpected Exception"); 00223 AcsJUnknownEx ex = new AcsJUnknownEx(th); 00224 ex.log(m_logger); 00225 fail("No exception should be thrown"); 00226 } 00227 assertEquals(0 , comp.value.type ); 00228 assertEquals(0 , comp.value.code ); 00229 } 00230 00232 00233 private void verifyErrorTrace(int depth, AcsJException jEx) { 00234 Throwable cause = jEx; 00235 for (int i = 1; i < depth; i++) { 00236 assertNotNull("ErrorTrace too short for depth=" + depth+ ": exception cause #" + i + " missing.", cause.getCause()); 00237 cause = cause.getCause(); 00238 } 00239 assertNull("ErrorTrace too long for depth=" + depth + ": exception cause #" + depth + " has another cause. ", cause.getCause()); 00240 } 00241 00242 00243 }