Skip to content

Commit

Permalink
feat (exception notebook): update
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Jun 10, 2021
1 parent 0d759d9 commit 5ac6744
Showing 1 changed file with 169 additions and 64 deletions.
233 changes: 169 additions & 64 deletions notebooks/pt/c02oo-java/s14exception/excecoes-jogo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"\n",
"## Testes da Hierarquia de Exceções\n",
"\n",
"Monte um código que teste a sua hierarquia exceções (todas elas) conforme o exemplo a seguir."
"Monte o código Java da sua hierarquia exceções (o máximo que conseguir até o momento) conforme o exemplo a seguir."
]
},
{
Expand All @@ -49,7 +49,7 @@
{
"data": {
"text/plain": [
"com.twosigma.beaker.javash.bkrdb57d720.DivisaoInvalida"
"com.twosigma.beaker.javash.bkrd09ab654.DivisaoInvalida"
]
},
"execution_count": 1,
Expand Down Expand Up @@ -77,7 +77,7 @@
{
"data": {
"text/plain": [
"com.twosigma.beaker.javash.bkrdb57d720.DivisaoInutil"
"com.twosigma.beaker.javash.bkrd09ab654.DivisaoInutil"
]
},
"execution_count": 2,
Expand Down Expand Up @@ -105,7 +105,7 @@
{
"data": {
"text/plain": [
"com.twosigma.beaker.javash.bkrdb57d720.DivisaoNaoInteira"
"com.twosigma.beaker.javash.bkrd09ab654.DivisaoNaoInteira"
]
},
"execution_count": 3,
Expand Down Expand Up @@ -133,7 +133,7 @@
{
"data": {
"text/plain": [
"com.twosigma.beaker.javash.bkrdb57d720.Util"
"com.twosigma.beaker.javash.bkrd09ab654.Util"
]
},
"execution_count": 4,
Expand All @@ -147,32 +147,166 @@
" int divisao;\n",
" if (y == 1)\n",
" throw new DivisaoInutil(\"Esta divisao eh inutil\");\n",
" if (x%y > 0)\n",
" if (y > 0 && x%y > 0)\n",
" throw new DivisaoNaoInteira(\"Esta divisao nao eh inteira\");\n",
" divisao = x / y;\n",
" return divisao;\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Teste Sistemático\n",
"\n",
"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",
"\n",
"A função de avaliação recebe os seguintes parâmetros:\n",
"* `numerator` - numerador da divisão\n",
"* `denominator` - denominador da divisão\n",
"* `errorExpected` - se este teste devia produzir um erro ou não\n",
"* `testSpecific` - se houver erro, se o teste deveria disparar uma exceção especializada\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"com.twosigma.beaker.javash.bkrd09ab654.Test"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"public class Test {\n",
" private int round = 0,\n",
" right = 0,\n",
" wrong = 0;\n",
"\n",
" public void evaluate(int numerator, int denominator, boolean errorExpected, boolean testSpecific) {\n",
" boolean error = true,\n",
" errorCaptured = true,\n",
" specific = false;\n",
" round++;\n",
" System.out.println(\"===== Test round \" + round + \" =====\");\n",
" System.out.println(\"* error expected: \" + ((errorExpected) ? \"yes\" : \"no\"));\n",
" System.out.println(\"* test specific exception: \" + ((testSpecific) ? \"yes\" : \"no\"));\n",
" System.out.println(\"--- Testing...\");\n",
" try {\n",
" int division = Util.divide(numerator, denominator);\n",
" System.out.println(\"Result of the division: \" + division);\n",
" error = false;\n",
" } catch (DivisaoInutil erro) {\n",
" System.out.println(erro.getMessage());\n",
" specific = true;\n",
" } catch (DivisaoNaoInteira erro) {\n",
" System.out.println(erro.getMessage());\n",
" specific = true;\n",
" } catch (DivisaoInvalida erro) {\n",
" System.out.println(erro.getMessage());\n",
" } catch (Exception erro) {\n",
" System.out.println(\"Other error not captured: \" + erro.getMessage());\n",
" errorCaptured = false;\n",
" }\n",
" \n",
" System.out.println(\"--- Report\");\n",
" System.out.println(\"* error found: \" + ((error) ? \"yes\" : \"no\"));\n",
" System.out.println(\"* error captured: \" + ((errorCaptured) ? \"yes\" : \"no\"));\n",
" System.out.println(\"* specific exception triggered: \" + ((specific) ? \"yes\" : \"no\"));\n",
" boolean result = (((errorExpected && error && errorCaptured) || (!errorExpected && !error))\n",
" && (testSpecific == specific));\n",
" System.out.println(\"--- Final Result: \" + ((result) ? \"passed\" : \"not passed\"));\n",
" if (result)\n",
" right++;\n",
" else\n",
" wrong++;\n",
" System.out.println();\n",
" }\n",
" \n",
" public void summary() {\n",
" System.out.println(\"===== Summary of Tests =====\");\n",
" System.out.println(\"Tests passed: \" + right);\n",
" System.out.println(\"Tests not passed: \" + wrong);\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Executando o Teste Sistemático\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=== Primeiro teste\n",
"Resultado da divisao: 4\n",
"=== Segundo teste\n",
"===== Test round 1 =====\n",
"* error expected: no\n",
"* test specific exception: no\n",
"--- Testing...\n",
"Result of the division: 4\n",
"--- Report\n",
"* error found: no\n",
"* error captured: yes\n",
"* specific exception triggered: no\n",
"--- Final Result: passed\n",
"\n",
"===== Test round 2 =====\n",
"* error expected: yes\n",
"* test specific exception: yes\n",
"--- Testing...\n",
"Esta divisao eh inutil\n",
"=== Terceiro teste\n",
"--- Report\n",
"* error found: yes\n",
"* error captured: yes\n",
"* specific exception triggered: yes\n",
"--- Final Result: passed\n",
"\n",
"===== Test round 3 =====\n",
"* error expected: yes\n",
"* test specific exception: yes\n",
"--- Testing...\n",
"Esta divisao nao eh inteira\n",
"=== Quarto teste\n",
"Ocorreu um erro nao esperado na divisao\n",
"--> Esta divisao nao eh inteira\n"
"--- Report\n",
"* error found: yes\n",
"* error captured: yes\n",
"* specific exception triggered: yes\n",
"--- Final Result: passed\n",
"\n",
"===== Test round 4 =====\n",
"* error expected: yes\n",
"* test specific exception: no\n",
"--- Testing...\n",
"Other error not captured: / by zero\n",
"--- Report\n",
"* error found: yes\n",
"* error captured: no\n",
"* specific exception triggered: no\n",
"--- Final Result: not passed\n",
"\n",
"===== Summary of Tests =====\n",
"Tests passed: 3\n",
"Tests not passed: 1\n"
]
},
{
Expand All @@ -181,69 +315,27 @@
"null"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// codigo testando a Excecao criada\n",
"int numerador = 8;\n",
"int denominador = 2;\n",
"\n",
"System.out.println(\"=== Primeiro teste\");\n",
"Test testDivision = new Test();\n",
"\n",
"// testando uma divisao valida\n",
"try {\n",
" int divisao = Util.divide(numerador, denominador);\n",
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
"} catch (DivisaoInvalida erro) {\n",
" System.out.println(\"Ocorreu um erro nao esperado na divisao\");\n",
" System.out.println(erro.getMessage());\n",
"} catch (Exception erro) {\n",
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
"}\n",
"\n",
"System.out.println(\"=== Segundo teste\");\n",
"\n",
"denominador = 1;\n",
"testDivision.evaluate(8, 2, false, false);\n",
"\n",
"// testando a divisao inutil\n",
"try {\n",
" int divisao = Util.divide(numerador, denominador);\n",
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
"} catch (DivisaoInutil erro) {\n",
" System.out.println(erro.getMessage());\n",
"} catch (Exception erro) {\n",
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
"}\n",
"\n",
"System.out.println(\"=== Terceiro teste\");\n",
"\n",
"denominador = 3;\n",
"testDivision.evaluate(8, 1, true, true);\n",
"\n",
"// testando a divisao nao inteira\n",
"try {\n",
" int divisao = Util.divide(numerador, denominador);\n",
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
"} catch (DivisaoNaoInteira erro) {\n",
" System.out.println(erro.getMessage());\n",
"} catch (Exception erro) {\n",
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
"}\n",
"\n",
"System.out.println(\"=== Quarto teste\");\n",
"testDivision.evaluate(8, 3, true, true);\n",
"\n",
"// testando a super classe\n",
"try {\n",
" int divisao = Util.divide(numerador, denominador);\n",
" System.out.println(\"Resultado da divisao: \" + divisao);\n",
"} catch (DivisaoInvalida erro) {\n",
" System.out.println(\"Ocorreu um erro nao esperado na divisao\");\n",
" System.out.println(\"--> \" + erro.getMessage());\n",
"} catch (Exception erro) {\n",
" System.out.println(\"Outro erro: \" + erro.getMessage());\n",
"}"
"testDivision.evaluate(8, 0, true, false);\n",
"\n",
"testDivision.summary();"
]
}
],
Expand All @@ -259,7 +351,20 @@
"mimetype": "",
"name": "Java",
"nbconverter_exporter": "",
"version": "11.0.7"
"version": "1.8.0_121"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
Expand Down

0 comments on commit 5ac6744

Please sign in to comment.