Skip to content

Commit 5ac6744

Browse files
committed
feat (exception notebook): update
1 parent 0d759d9 commit 5ac6744

File tree

1 file changed

+169
-64
lines changed

1 file changed

+169
-64
lines changed

notebooks/pt/c02oo-java/s14exception/excecoes-jogo.ipynb

Lines changed: 169 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"\n",
3939
"## Testes da Hierarquia de Exceções\n",
4040
"\n",
41-
"Monte um código que teste a sua hierarquia exceções (todas elas) conforme o exemplo a seguir."
41+
"Monte o código Java da sua hierarquia exceções (o máximo que conseguir até o momento) conforme o exemplo a seguir."
4242
]
4343
},
4444
{
@@ -49,7 +49,7 @@
4949
{
5050
"data": {
5151
"text/plain": [
52-
"com.twosigma.beaker.javash.bkrdb57d720.DivisaoInvalida"
52+
"com.twosigma.beaker.javash.bkrd09ab654.DivisaoInvalida"
5353
]
5454
},
5555
"execution_count": 1,
@@ -77,7 +77,7 @@
7777
{
7878
"data": {
7979
"text/plain": [
80-
"com.twosigma.beaker.javash.bkrdb57d720.DivisaoInutil"
80+
"com.twosigma.beaker.javash.bkrd09ab654.DivisaoInutil"
8181
]
8282
},
8383
"execution_count": 2,
@@ -105,7 +105,7 @@
105105
{
106106
"data": {
107107
"text/plain": [
108-
"com.twosigma.beaker.javash.bkrdb57d720.DivisaoNaoInteira"
108+
"com.twosigma.beaker.javash.bkrd09ab654.DivisaoNaoInteira"
109109
]
110110
},
111111
"execution_count": 3,
@@ -133,7 +133,7 @@
133133
{
134134
"data": {
135135
"text/plain": [
136-
"com.twosigma.beaker.javash.bkrdb57d720.Util"
136+
"com.twosigma.beaker.javash.bkrd09ab654.Util"
137137
]
138138
},
139139
"execution_count": 4,
@@ -147,32 +147,166 @@
147147
" int divisao;\n",
148148
" if (y == 1)\n",
149149
" throw new DivisaoInutil(\"Esta divisao eh inutil\");\n",
150-
" if (x%y > 0)\n",
150+
" if (y > 0 && x%y > 0)\n",
151151
" throw new DivisaoNaoInteira(\"Esta divisao nao eh inteira\");\n",
152152
" divisao = x / y;\n",
153153
" return divisao;\n",
154154
" }\n",
155155
"}"
156156
]
157157
},
158+
{
159+
"cell_type": "markdown",
160+
"metadata": {},
161+
"source": [
162+
"# Teste Sistemático\n",
163+
"\n",
164+
"Sistemas robustos têm rotinas que fazem teste sistemático do código na busca de falhas. Há frameworks como o [JUnit](https://junit.org/) que são especializados em testes. Nós faremos um teste sistemático altamente simplificado apenas das exceções. Para isso você pode adaptar ou estender a classe abaixo cuja função é testar o plano de exceções acima.\n",
165+
"\n",
166+
"A função de avaliação recebe os seguintes parâmetros:\n",
167+
"* `numerator` - numerador da divisão\n",
168+
"* `denominator` - denominador da divisão\n",
169+
"* `errorExpected` - se este teste devia produzir um erro ou não\n",
170+
"* `testSpecific` - se houver erro, se o teste deveria disparar uma exceção especializada\n",
171+
"\n",
172+
"Note que o teste é considerado correto se ele atender o que você informar (nos parâmetros) como esperado, ou seja, se você informar que um erro é esperado e o erro acontecer, ou se você informar que um erro não é esperado e o erro não acontecer. O mesmo vale para exceção especializada."
173+
]
174+
},
158175
{
159176
"cell_type": "code",
160177
"execution_count": 5,
161178
"metadata": {},
179+
"outputs": [
180+
{
181+
"data": {
182+
"text/plain": [
183+
"com.twosigma.beaker.javash.bkrd09ab654.Test"
184+
]
185+
},
186+
"execution_count": 5,
187+
"metadata": {},
188+
"output_type": "execute_result"
189+
}
190+
],
191+
"source": [
192+
"public class Test {\n",
193+
" private int round = 0,\n",
194+
" right = 0,\n",
195+
" wrong = 0;\n",
196+
"\n",
197+
" public void evaluate(int numerator, int denominator, boolean errorExpected, boolean testSpecific) {\n",
198+
" boolean error = true,\n",
199+
" errorCaptured = true,\n",
200+
" specific = false;\n",
201+
" round++;\n",
202+
" System.out.println(\"===== Test round \" + round + \" =====\");\n",
203+
" System.out.println(\"* error expected: \" + ((errorExpected) ? \"yes\" : \"no\"));\n",
204+
" System.out.println(\"* test specific exception: \" + ((testSpecific) ? \"yes\" : \"no\"));\n",
205+
" System.out.println(\"--- Testing...\");\n",
206+
" try {\n",
207+
" int division = Util.divide(numerator, denominator);\n",
208+
" System.out.println(\"Result of the division: \" + division);\n",
209+
" error = false;\n",
210+
" } catch (DivisaoInutil erro) {\n",
211+
" System.out.println(erro.getMessage());\n",
212+
" specific = true;\n",
213+
" } catch (DivisaoNaoInteira erro) {\n",
214+
" System.out.println(erro.getMessage());\n",
215+
" specific = true;\n",
216+
" } catch (DivisaoInvalida erro) {\n",
217+
" System.out.println(erro.getMessage());\n",
218+
" } catch (Exception erro) {\n",
219+
" System.out.println(\"Other error not captured: \" + erro.getMessage());\n",
220+
" errorCaptured = false;\n",
221+
" }\n",
222+
" \n",
223+
" System.out.println(\"--- Report\");\n",
224+
" System.out.println(\"* error found: \" + ((error) ? \"yes\" : \"no\"));\n",
225+
" System.out.println(\"* error captured: \" + ((errorCaptured) ? \"yes\" : \"no\"));\n",
226+
" System.out.println(\"* specific exception triggered: \" + ((specific) ? \"yes\" : \"no\"));\n",
227+
" boolean result = (((errorExpected && error && errorCaptured) || (!errorExpected && !error))\n",
228+
" && (testSpecific == specific));\n",
229+
" System.out.println(\"--- Final Result: \" + ((result) ? \"passed\" : \"not passed\"));\n",
230+
" if (result)\n",
231+
" right++;\n",
232+
" else\n",
233+
" wrong++;\n",
234+
" System.out.println();\n",
235+
" }\n",
236+
" \n",
237+
" public void summary() {\n",
238+
" System.out.println(\"===== Summary of Tests =====\");\n",
239+
" System.out.println(\"Tests passed: \" + right);\n",
240+
" System.out.println(\"Tests not passed: \" + wrong);\n",
241+
" }\n",
242+
"}"
243+
]
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"metadata": {},
248+
"source": [
249+
"# Executando o Teste Sistemático\n",
250+
"\n",
251+
"Elabore para a sua hierarquia de exceções uma sequência de testes como o ilustrado a seguir que teste todas as suas classes de exceção. No exemplo a seguir, deixei de propósito um teste que não passou (divisão por zero) como ilustração."
252+
]
253+
},
254+
{
255+
"cell_type": "code",
256+
"execution_count": 6,
257+
"metadata": {},
162258
"outputs": [
163259
{
164260
"name": "stdout",
165261
"output_type": "stream",
166262
"text": [
167-
"=== Primeiro teste\n",
168-
"Resultado da divisao: 4\n",
169-
"=== Segundo teste\n",
263+
"===== Test round 1 =====\n",
264+
"* error expected: no\n",
265+
"* test specific exception: no\n",
266+
"--- Testing...\n",
267+
"Result of the division: 4\n",
268+
"--- Report\n",
269+
"* error found: no\n",
270+
"* error captured: yes\n",
271+
"* specific exception triggered: no\n",
272+
"--- Final Result: passed\n",
273+
"\n",
274+
"===== Test round 2 =====\n",
275+
"* error expected: yes\n",
276+
"* test specific exception: yes\n",
277+
"--- Testing...\n",
170278
"Esta divisao eh inutil\n",
171-
"=== Terceiro teste\n",
279+
"--- Report\n",
280+
"* error found: yes\n",
281+
"* error captured: yes\n",
282+
"* specific exception triggered: yes\n",
283+
"--- Final Result: passed\n",
284+
"\n",
285+
"===== Test round 3 =====\n",
286+
"* error expected: yes\n",
287+
"* test specific exception: yes\n",
288+
"--- Testing...\n",
172289
"Esta divisao nao eh inteira\n",
173-
"=== Quarto teste\n",
174-
"Ocorreu um erro nao esperado na divisao\n",
175-
"--> Esta divisao nao eh inteira\n"
290+
"--- Report\n",
291+
"* error found: yes\n",
292+
"* error captured: yes\n",
293+
"* specific exception triggered: yes\n",
294+
"--- Final Result: passed\n",
295+
"\n",
296+
"===== Test round 4 =====\n",
297+
"* error expected: yes\n",
298+
"* test specific exception: no\n",
299+
"--- Testing...\n",
300+
"Other error not captured: / by zero\n",
301+
"--- Report\n",
302+
"* error found: yes\n",
303+
"* error captured: no\n",
304+
"* specific exception triggered: no\n",
305+
"--- Final Result: not passed\n",
306+
"\n",
307+
"===== Summary of Tests =====\n",
308+
"Tests passed: 3\n",
309+
"Tests not passed: 1\n"
176310
]
177311
},
178312
{
@@ -181,69 +315,27 @@
181315
"null"
182316
]
183317
},
184-
"execution_count": 5,
318+
"execution_count": 6,
185319
"metadata": {},
186320
"output_type": "execute_result"
187321
}
188322
],
189323
"source": [
190-
"// codigo testando a Excecao criada\n",
191-
"int numerador = 8;\n",
192-
"int denominador = 2;\n",
193-
"\n",
194-
"System.out.println(\"=== Primeiro teste\");\n",
324+
"Test testDivision = new Test();\n",
195325
"\n",
196326
"// testando uma divisao valida\n",
197-
"try {\n",
198-
" int divisao = Util.divide(numerador, denominador);\n",
199-
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
200-
"} catch (DivisaoInvalida erro) {\n",
201-
" System.out.println(\"Ocorreu um erro nao esperado na divisao\");\n",
202-
" System.out.println(erro.getMessage());\n",
203-
"} catch (Exception erro) {\n",
204-
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
205-
"}\n",
206-
"\n",
207-
"System.out.println(\"=== Segundo teste\");\n",
208-
"\n",
209-
"denominador = 1;\n",
327+
"testDivision.evaluate(8, 2, false, false);\n",
210328
"\n",
211329
"// testando a divisao inutil\n",
212-
"try {\n",
213-
" int divisao = Util.divide(numerador, denominador);\n",
214-
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
215-
"} catch (DivisaoInutil erro) {\n",
216-
" System.out.println(erro.getMessage());\n",
217-
"} catch (Exception erro) {\n",
218-
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
219-
"}\n",
220-
"\n",
221-
"System.out.println(\"=== Terceiro teste\");\n",
222-
"\n",
223-
"denominador = 3;\n",
330+
"testDivision.evaluate(8, 1, true, true);\n",
224331
"\n",
225332
"// testando a divisao nao inteira\n",
226-
"try {\n",
227-
" int divisao = Util.divide(numerador, denominador);\n",
228-
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
229-
"} catch (DivisaoNaoInteira erro) {\n",
230-
" System.out.println(erro.getMessage());\n",
231-
"} catch (Exception erro) {\n",
232-
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
233-
"}\n",
234-
"\n",
235-
"System.out.println(\"=== Quarto teste\");\n",
333+
"testDivision.evaluate(8, 3, true, true);\n",
236334
"\n",
237335
"// testando a super classe\n",
238-
"try {\n",
239-
" int divisao = Util.divide(numerador, denominador);\n",
240-
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
241-
"} catch (DivisaoInvalida erro) {\n",
242-
" System.out.println(\"Ocorreu um erro nao esperado na divisao\");\n",
243-
" System.out.println(\"--> \" + erro.getMessage());\n",
244-
"} catch (Exception erro) {\n",
245-
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
246-
"}"
336+
"testDivision.evaluate(8, 0, true, false);\n",
337+
"\n",
338+
"testDivision.summary();"
247339
]
248340
}
249341
],
@@ -259,7 +351,20 @@
259351
"mimetype": "",
260352
"name": "Java",
261353
"nbconverter_exporter": "",
262-
"version": "11.0.7"
354+
"version": "1.8.0_121"
355+
},
356+
"toc": {
357+
"base_numbering": 1,
358+
"nav_menu": {},
359+
"number_sections": false,
360+
"sideBar": false,
361+
"skip_h1_title": false,
362+
"title_cell": "Table of Contents",
363+
"title_sidebar": "Contents",
364+
"toc_cell": false,
365+
"toc_position": {},
366+
"toc_section_display": false,
367+
"toc_window_display": false
263368
}
264369
},
265370
"nbformat": 4,

0 commit comments

Comments
 (0)