fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class TestFailedException extends Exception { }
  6.  
  7. class OtherTypeIDoNotOwn {
  8.  
  9. public void doD() { System.out.println("doD"); }
  10. public String doE() { System.out.println("doE"); return "doE"; }
  11. public OtherTypeIDoNotOwn doF(String string) {System.out.println("doF"); return new OtherTypeIDoNotOwn();}
  12.  
  13. }
  14.  
  15. class IDoNotOwnThisType {
  16.  
  17. public void doA(String string) { System.out.println("doA"); }
  18. public String doB() {System.out.println("doB"); return "doB";}
  19. public OtherTypeIDoNotOwn doC() {System.out.println("doC"); return new OtherTypeIDoNotOwn();}
  20.  
  21. }
  22.  
  23. interface OperationManipulator {
  24.  
  25. void beforeOperation(); //called before operation
  26. void handleSuccess(); //called after success
  27. void handleSoftFailure(Exception e); //called after every failure in every try
  28. void handleFailure(Exception e) throws TestFailedException; //called after reaching time limit
  29.  
  30. }
  31.  
  32. interface IWrapper<T extends IType> extends OperationManipulator {
  33.  
  34. public void doA(String string) throws TestFailedException;
  35. public String doB() throws TestFailedException;
  36. public T doC() throws TestFailedException;
  37.  
  38. }
  39.  
  40. interface IType<T extends IType> extends OperationManipulator {
  41.  
  42. public void doD() throws TestFailedException;
  43. public String doE() throws TestFailedException;
  44. public T doF(String string) throws TestFailedException;
  45.  
  46. }
  47.  
  48. abstract class AType<T extends IType> implements IType{
  49.  
  50. Object element; // I do not own type of this object, cant modify it.
  51. Class typeClass;
  52. long TIMEOUT = 5000;
  53. long WAIT_FOR_NEXT_TRY = 100;
  54.  
  55. public AType(Object element) {
  56. this.element = element;
  57. typeClass = this.getClass();
  58. }
  59.  
  60. @Override
  61. public void doD() throws TestFailedException {
  62. long startTime = System.currentTimeMillis();
  63. while (true) {
  64. try {
  65. beforeOperation();
  66. ((OtherTypeIDoNotOwn) element).doD();
  67. handleSuccess();
  68. break;
  69. } catch (Exception e) {
  70. handleSoftFailure(e);
  71. if (System.currentTimeMillis() - startTime > TIMEOUT) {
  72. handleFailure(e);
  73. break;
  74. } else {
  75. try {
  76. Thread.sleep(WAIT_FOR_NEXT_TRY);
  77. } catch (InterruptedException ex) {
  78. }
  79. }
  80. }
  81. }
  82. }
  83.  
  84. @Override
  85. public String doE() throws TestFailedException {
  86. String result = null;
  87. long startTime = System.currentTimeMillis();
  88. while (true) {
  89. try {
  90. beforeOperation();
  91. result = ((OtherTypeIDoNotOwn) element).doE();
  92. handleSuccess();
  93. break;
  94. } catch (Exception e) {
  95. handleSoftFailure(e);
  96. if (System.currentTimeMillis() - startTime > TIMEOUT) {
  97. handleFailure(e);
  98. break;
  99. } else {
  100. try {
  101. Thread.sleep(WAIT_FOR_NEXT_TRY);
  102. } catch (InterruptedException ex) {
  103. }
  104. }
  105. }
  106. }
  107. return result;
  108. }
  109.  
  110. @Override
  111. public T doF(String string) throws TestFailedException {
  112. T result = null;
  113. long startTime = System.currentTimeMillis();
  114. while (true) {
  115. try {
  116. beforeOperation();
  117. OtherTypeIDoNotOwn temp = ((OtherTypeIDoNotOwn) element).doF(string);
  118. result = (T) typeClass.getDeclaredConstructor(Object.class).newInstance(temp);
  119. handleSuccess();
  120. break;
  121. } catch (Exception e) {
  122. handleSoftFailure(e);
  123. if (System.currentTimeMillis() - startTime > TIMEOUT) {
  124. handleFailure(e);
  125. break;
  126. } else {
  127. try {
  128. Thread.sleep(WAIT_FOR_NEXT_TRY);
  129. } catch (InterruptedException ex) {
  130. }
  131. }
  132. }
  133. }
  134. return result;
  135. }
  136.  
  137. }
  138.  
  139. abstract class AWrapper<T extends IType> implements IWrapper{
  140.  
  141. Object element; // I do not own type of this object, cant modify it.
  142. Class typeClass;
  143. long TIMEOUT = 5000;
  144. long WAIT_FOR_NEXT_TRY = 100;
  145.  
  146. public AWrapper(Object element, Class typeClass) {
  147. this.element = element;
  148. this.typeClass = typeClass;
  149. }
  150.  
  151. @Override
  152. public void doA(String string) throws TestFailedException {
  153. long startTime = System.currentTimeMillis();
  154. while (true) {
  155. try {
  156. beforeOperation();
  157. ((IDoNotOwnThisType) element).doA(string);
  158. handleSuccess();
  159. break;
  160. } catch (Exception e) {
  161. handleSoftFailure(e);
  162. if (System.currentTimeMillis() - startTime > TIMEOUT) {
  163. handleFailure(e);
  164. break;
  165. } else {
  166. try {
  167. Thread.sleep(WAIT_FOR_NEXT_TRY);
  168. } catch (InterruptedException ex) {
  169. }
  170. }
  171. }
  172. }
  173. }
  174.  
  175. @Override
  176. public String doB() throws TestFailedException {
  177. String result = null;
  178. long startTime = System.currentTimeMillis();
  179. while (true) {
  180. try {
  181. beforeOperation();
  182. result = ((IDoNotOwnThisType) element).doB();
  183. handleSuccess();
  184. break;
  185. } catch (Exception e) {
  186. handleSoftFailure(e);
  187. if (System.currentTimeMillis() - startTime > TIMEOUT) {
  188. handleFailure(e);
  189. break;
  190. } else {
  191. try {
  192. Thread.sleep(WAIT_FOR_NEXT_TRY);
  193. } catch (InterruptedException ex) {
  194. }
  195. }
  196. }
  197. }
  198. return result;
  199. }
  200.  
  201. @Override
  202. public T doC() throws TestFailedException {
  203. T result = null;
  204. long startTime = System.currentTimeMillis();
  205. while (true) {
  206. try {
  207. beforeOperation();
  208. OtherTypeIDoNotOwn temp = ((IDoNotOwnThisType) element).doC();
  209. result = (T) typeClass.getDeclaredConstructor(Object.class).newInstance(temp);
  210. handleSuccess();
  211. break;
  212. } catch (Exception e) {
  213. handleSoftFailure(e);
  214. if (System.currentTimeMillis() - startTime > TIMEOUT) {
  215. handleFailure(e);
  216. break;
  217. } else {
  218. try {
  219. Thread.sleep(WAIT_FOR_NEXT_TRY);
  220. } catch (InterruptedException ex) {
  221. }
  222. }
  223. }
  224. }
  225. return result;
  226. }
  227.  
  228. }
  229.  
  230.  
  231.  
  232. class AssertingType extends AType<AssertingType> {
  233.  
  234. public AssertingType(Object element) {
  235. super(element);
  236. }
  237.  
  238. @Override
  239. public void beforeOperation() {
  240. System.out.println("Asserting type before operation!");
  241. }
  242.  
  243. @Override
  244. public void handleSuccess() {
  245. //TODO: add to log file and log to output
  246. System.out.println("Asserting type success!");
  247. }
  248.  
  249. @Override
  250. public void handleFailure(Exception e) throws TestFailedException {
  251. //TODO: add to log file, log to output and throw exception
  252. System.out.println("Asserting type failure!");
  253. e.printStackTrace();
  254. throw new TestFailedException();
  255. }
  256.  
  257. @Override
  258. public void handleSoftFailure(Exception e) {
  259. //TODO: add to log file, log to output
  260. System.out.println("Asserting type soft failure!");
  261. e.printStackTrace();
  262. }
  263.  
  264. }
  265.  
  266. class AssertingWrapper extends AWrapper<AssertingType> {
  267.  
  268. public AssertingWrapper (Object driver) {
  269. super(driver, AssertingType.class);
  270. }
  271.  
  272. @Override
  273. public void beforeOperation() {
  274. //TODO
  275. System.out.println("Asserting wrapper before operation!");
  276. }
  277.  
  278. @Override
  279. public void handleSuccess() {
  280. //TODO: add to log file and log to output
  281. System.out.println("Asserting wrapper success!");
  282. }
  283.  
  284. @Override
  285. public void handleFailure(Exception e) throws TestFailedException {
  286. //TODO: add to log file, log to output and throw exception
  287. System.out.println("Asserting wrapper failure!");
  288. throw new TestFailedException();
  289. }
  290.  
  291. @Override
  292. public void handleSoftFailure(Exception e) {
  293. //TODO: add to log file, log to output
  294. System.out.println("Asserting wrapper soft failure!");
  295. e.printStackTrace();
  296. }
  297. }
  298.  
  299.  
  300.  
  301. class Ideone {
  302. public static void main(String[] args) throws TestFailedException {
  303.  
  304. AssertingWrapper wrapper = new AssertingWrapper(new IDoNotOwnThisType());
  305.  
  306. AssertingType type = wrapper.doC();
  307.  
  308. AssertingType type2 = type.doF("arg");
  309. }
  310. }
Success #stdin #stdout 0.07s 2841600KB
stdin
Standard input is empty
stdout
Asserting wrapper before operation!
doC
Asserting wrapper success!
Asserting type before operation!
doF
Asserting type success!