Skip to content

Commit

Permalink
Refactor Main and VeiculoView classes for better error handling and u…
Browse files Browse the repository at this point in the history
…ser experience
  • Loading branch information
lazaromx committed Nov 24, 2023
1 parent 37ce954 commit b111a8c
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 0 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions src/test/java/br/lazaro/views/VeiculoViewTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package br.lazaro.views;

import br.lazaro.models.Veiculo;
import br.lazaro.repositories.*;
import br.lazaro.controllers.*;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;

import static org.junit.Assert.*;

public class VeiculoViewTest {

@Test
public void test_instantiation() {
VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

assertNotNull(view);
}

@Test
public void test_display_menu() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

view.showMenu();

String expectedOutput = "1 - Comprar veículo\r\n2 - Estoque de veículo\r\n3 - Vender veículo\r\n4 - Relatório de vendas\r\n";
assertEquals(expectedOutput, outContent.toString());
}

@Test
public void test_buy_vehicle() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

ByteArrayInputStream inContent = new ByteArrayInputStream("Teste\nTeste\nTeste\n2022\n10000.0\n".getBytes());
System.setIn(inContent);

VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

view.comprarVeiculo();

List<Veiculo> estoque = repository.estoque();
assertEquals(1, estoque.size());
}

@Test
public void test_list_inventory() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

controller.venderVeiculo(0);
view.listarEstoque();

String expectedOutput = "Nenhum veículo em estoque disponível.\r\n";
assertEquals(expectedOutput, outContent.toString());
}

@Test
public void test_sell_vehicle() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

ByteArrayInputStream inContent = new ByteArrayInputStream("0\n".getBytes());
System.setIn(inContent);

VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

view.venderVeiculo();

List<Veiculo> vendidos = repository.vendidos();
assertEquals(1, vendidos.size());
}

@Test
public void test_list_sold_vehicles() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

view.listarVendidos();

String expectedOutput = "Nenhum veículo vendido.\r\n";
assertEquals(expectedOutput, outContent.toString());
}

@Test
public void test_list_inventory_empty() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

controller.venderVeiculo(0);
view.listarEstoque();

String expectedOutput = "Nenhum veículo em estoque disponível.\r\n";
assertEquals(expectedOutput, outContent.toString());
}

@Test
public void test_list_sold_vehicles_empty() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

VeiculoRepository repository = new VeiculoRepository();
VeiculoController controller = new VeiculoController(repository);
VeiculoView view = new VeiculoView(controller);

view.listarVendidos();

String expectedOutput = "Nenhum veículo vendido.\r\n";
assertEquals(expectedOutput, outContent.toString());
}

}

0 comments on commit b111a8c

Please sign in to comment.