Created
July 25, 2017 23:43
-
-
Save fkenjikamei/283a4a826f5a95a8b33d911982866955 to your computer and use it in GitHub Desktop.
Usando Mocks e Conexao com banco de dados MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- phpMyAdmin SQL Dump | |
-- version 4.5.1 | |
-- http://www.phpmyadmin.net | |
-- | |
-- Host: 127.0.0.1 | |
-- Generation Time: 26-Jul-2017 às 01:42 | |
-- Versão do servidor: 10.1.13-MariaDB | |
-- PHP Version: 7.0.8 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8mb4 */; | |
-- | |
-- Database: `poo20171` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Estrutura da tabela `aluno` | |
-- | |
CREATE TABLE `aluno` ( | |
`nome` varchar(100) NOT NULL, | |
`email` varchar(40) NOT NULL, | |
`cpf` varchar(11) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
-- | |
-- Indexes for dumped tables | |
-- | |
-- | |
-- Indexes for table `aluno` | |
-- | |
ALTER TABLE `aluno` | |
ADD PRIMARY KEY (`email`); | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bo; | |
import dao.IAlunoDAO; | |
import exception.BOException; | |
import exception.DAOException; | |
public class AlunoBO { | |
IAlunoDAO alunoDAO; | |
public AlunoBO(IAlunoDAO alunoDAO) { | |
this.alunoDAO = alunoDAO; | |
} | |
void validarDadosCadastro(String nome, String cpf, String email) throws BOException, DAOException { | |
if(nome.equals("") || nome.equals(null) || nome.length() < 2) | |
throw new BOException("nome inválido"); | |
if(cpf.equals("") || cpf.equals(null) || cpf.length() < 11) | |
throw new BOException("cpf inválido"); | |
if(email.equals("") || email.equals(null) || email.length() < 2 || !email.contains("@") || !email.contains(".")) | |
throw new BOException("e-mail inválido"); | |
try { | |
this.alunoDAO.cadastrar(nome, cpf, email); | |
} catch(DAOException e) { | |
throw new DAOException(e.getMessage()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bo; | |
import org.junit.Test; | |
import dao.IAlunoDAO; | |
import junit.framework.TestCase; | |
public class AlunoBOTest extends TestCase { | |
IAlunoDAO alunoDAO; | |
@Override | |
protected void setUp() throws Exception { | |
this.alunoDAO = new AlunoDAOMock(); | |
} | |
@Test | |
public void testNaoDeveriaCadastrarAlunoSeNomeVazio() throws Exception { | |
AlunoBO alunoBO = new AlunoBO(this.alunoDAO); | |
String nome = ""; | |
String cpf = "12345678901"; | |
String email = "[email protected]"; | |
try { | |
alunoBO.validarDadosCadastro(nome, cpf, email); | |
fail("Falha - Não deveria ter passado na validação"); | |
} catch(Exception e) { | |
assertTrue(true); | |
} | |
} | |
public void testNaoDeveriaCairNaExcecaoSeDadosValidos() { | |
AlunoBO alunoBO = new AlunoBO(this.alunoDAO); | |
String nome = "oieeee"; | |
String cpf = "12345678901"; | |
String email = "[email protected]"; | |
try { | |
alunoBO.validarDadosCadastro(nome, cpf, email); | |
assertTrue(true); | |
} catch(Exception e) { | |
fail("Não deveria cair na exceção: "+e.getMessage()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bo; | |
import dao.IAlunoDAO; | |
import exception.DAOException; | |
public class AlunoDAOMock implements IAlunoDAO { | |
@Override | |
public void cadastrar(String nome, String cpf, String email) throws DAOException { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import org.junit.Test; | |
import exception.DAOException; | |
import junit.framework.TestCase; | |
public class AlunoDAOTest extends TestCase { | |
private Connection conexao; | |
public void testDeveriaCadastrarAluno() throws Exception { | |
ConexaoMySQL bancoMySQL = new ConexaoMySQL(); | |
try { | |
this.conexao = bancoMySQL.conectar(); | |
this.conexao.setAutoCommit(false); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
String nome = "Fulano"; | |
String email = "[email protected]"; | |
String cpf = "12345678912"; | |
try { | |
AlunoDAO alunoDAO = new AlunoDAO(this.conexao); | |
alunoDAO.cadastrar(nome, cpf, email); | |
Statement stmt = this.conexao.createStatement(); | |
ResultSet rs = stmt.executeQuery("SELECT * FROM aluno WHERE nome='"+nome+"' AND cpf='"+cpf+"' AND email='"+email+"'"); | |
while(rs.next()) { | |
assertEquals(nome, rs.getString("nome")); | |
} | |
} catch(DAOException e) { | |
fail("Inserção de usuário falhou: "+e.getMessage()); | |
} | |
} | |
public void testDeveriaExcluirAluno() throws Exception { | |
ConexaoMySQL bancoMySQL = new ConexaoMySQL(); | |
try { | |
this.conexao = bancoMySQL.conectar(); | |
this.conexao.setAutoCommit(false); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
String nome = "Fulano"; | |
String email = "[email protected]"; | |
String cpf = "12345678912"; | |
try { | |
Statement stmt = this.conexao.createStatement(); | |
stmt.executeUpdate("INSERT INTO aluno(nome, email, cpf) VALUES('"+nome+"','"+email+"','"+cpf+"')"); | |
this.conexao.commit(); | |
stmt.executeUpdate("DELETE FROM aluno WHERE email ='"+email+"'"); | |
this.conexao.commit(); | |
ResultSet rs = stmt.executeQuery("SELECT * FROM aluno WHERE nome='"+nome+"' AND cpf='"+cpf+"' AND email='"+email+"'"); | |
assertEquals(0, rs.getFetchSize()); | |
} catch(SQLException e) { | |
fail("Inserção de usuário falhou: "+e.getMessage()); | |
} | |
} | |
@Test | |
public void testDeveriaAtualizarAluno() throws Exception { | |
ConexaoMySQL bancoMySQL = new ConexaoMySQL(); | |
try { | |
this.conexao = bancoMySQL.conectar(); | |
this.conexao.setAutoCommit(false); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
String nome = "Fulano"; | |
String email = "[email protected]"; | |
String cpf = "12345678912"; | |
String novoEmail = "[email protected]"; | |
try { | |
Statement stmt = this.conexao.createStatement(); | |
stmt.executeUpdate("INSERT INTO aluno(nome, email, cpf) VALUES('"+nome+"','"+email+"','"+cpf+"')"); | |
this.conexao.commit(); | |
stmt.executeUpdate("UPDATE aluno SET email ='"+novoEmail+"' WHERE email='"+email+"'"); | |
this.conexao.commit(); | |
ResultSet rs = stmt.executeQuery("SELECT * FROM aluno WHERE email='"+novoEmail+"'"); | |
while(rs.next()) { | |
assertEquals(novoEmail, rs.getString("email")); | |
} | |
stmt.executeUpdate("DELETE FROM aluno WHERE email ='"+novoEmail+"'"); | |
this.conexao.commit(); | |
} catch(SQLException e) { | |
fail("Inserção de usuário falhou: "+e.getMessage()); | |
} | |
} | |
@Override | |
protected void tearDown() throws Exception { | |
super.tearDown(); | |
//this.conexao.rollback(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exception; | |
public class BOException extends Exception { | |
public BOException(String msg) { | |
super("Erro de negócio: "+msg); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import static org.junit.Assert.*; | |
import java.sql.Connection; | |
import java.sql.SQLDataException; | |
import java.sql.SQLException; | |
import org.junit.Test; | |
import junit.framework.TestCase; | |
public class ConexaoMySQLTest extends TestCase { | |
@Test | |
public void testDeveriaConectar() throws Exception { | |
ConexaoMySQL conexaoMySQL = new ConexaoMySQL(); | |
try { | |
Connection conexao = conexaoMySQL.conectar(); | |
} catch(SQLException e) { | |
fail("Falha ao conectar: "+e.getMessage()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exception; | |
public class DAOException extends Exception { | |
public DAOException(String msg) { | |
super("Erro de acesso aos dados: "+msg); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package view; | |
import java.util.Scanner; | |
import bo.AlunoBO; | |
import dao.AlunoDAO; | |
import dao.ConexaoMySQL; | |
import dao.IAlunoDAO; | |
import dao.IConexao; | |
public class SistemaAcademicoView { | |
public static void main(String[] args) { | |
System.out.println("Tela inicial"); | |
System.out.println("1- Cadastrar aluno"); | |
System.out.println(""); | |
System.out.println("Escolha a opção"); | |
Scanner entrada = new Scanner(System.in); | |
int opcao = entrada.nextInt(); | |
if(opcao == 1) { | |
SistemaAcademicoView.cadastrarAluno(); | |
} | |
} | |
private static void cadastrarAluno() { | |
System.out.println("Digite o nome do aluno: "); | |
Scanner entrada = new Scanner(System.in); | |
String nome = entrada.next(); | |
System.out.println("Digite o cpf do aluno: "); | |
String cpf = entrada.next(); | |
System.out.println("Digite o e-mail do aluno: "); | |
String email = entrada.next(); | |
IConexao conexao = new ConexaoMySQL(); | |
IAlunoDAO alunoDAO = new AlunoDAO(conexao); | |
AlunoBO alunoBO = new AlunoBO(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment