Skip to content

Commit b111a8c

Browse files
committed
Refactor Main and VeiculoView classes for better error handling and user experience
1 parent 37ce954 commit b111a8c

File tree

3 files changed

+269
-0
lines changed

3 files changed

+269
-0
lines changed

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package br.lazaro.views;
2+
3+
import br.lazaro.models.Veiculo;
4+
import br.lazaro.repositories.*;
5+
import br.lazaro.controllers.*;
6+
import org.junit.Test;
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.PrintStream;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.*;
14+
15+
public class VeiculoViewTest {
16+
17+
@Test
18+
public void test_instantiation() {
19+
VeiculoRepository repository = new VeiculoRepository();
20+
VeiculoController controller = new VeiculoController(repository);
21+
VeiculoView view = new VeiculoView(controller);
22+
23+
assertNotNull(view);
24+
}
25+
26+
@Test
27+
public void test_display_menu() {
28+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
29+
System.setOut(new PrintStream(outContent));
30+
31+
VeiculoRepository repository = new VeiculoRepository();
32+
VeiculoController controller = new VeiculoController(repository);
33+
VeiculoView view = new VeiculoView(controller);
34+
35+
view.showMenu();
36+
37+
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";
38+
assertEquals(expectedOutput, outContent.toString());
39+
}
40+
41+
@Test
42+
public void test_buy_vehicle() {
43+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
44+
System.setOut(new PrintStream(outContent));
45+
46+
ByteArrayInputStream inContent = new ByteArrayInputStream("Teste\nTeste\nTeste\n2022\n10000.0\n".getBytes());
47+
System.setIn(inContent);
48+
49+
VeiculoRepository repository = new VeiculoRepository();
50+
VeiculoController controller = new VeiculoController(repository);
51+
VeiculoView view = new VeiculoView(controller);
52+
53+
view.comprarVeiculo();
54+
55+
List<Veiculo> estoque = repository.estoque();
56+
assertEquals(1, estoque.size());
57+
}
58+
59+
@Test
60+
public void test_list_inventory() {
61+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
62+
System.setOut(new PrintStream(outContent));
63+
64+
VeiculoRepository repository = new VeiculoRepository();
65+
VeiculoController controller = new VeiculoController(repository);
66+
VeiculoView view = new VeiculoView(controller);
67+
68+
controller.venderVeiculo(0);
69+
view.listarEstoque();
70+
71+
String expectedOutput = "Nenhum veículo em estoque disponível.\r\n";
72+
assertEquals(expectedOutput, outContent.toString());
73+
}
74+
75+
@Test
76+
public void test_sell_vehicle() {
77+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
78+
System.setOut(new PrintStream(outContent));
79+
80+
ByteArrayInputStream inContent = new ByteArrayInputStream("0\n".getBytes());
81+
System.setIn(inContent);
82+
83+
VeiculoRepository repository = new VeiculoRepository();
84+
VeiculoController controller = new VeiculoController(repository);
85+
VeiculoView view = new VeiculoView(controller);
86+
87+
view.venderVeiculo();
88+
89+
List<Veiculo> vendidos = repository.vendidos();
90+
assertEquals(1, vendidos.size());
91+
}
92+
93+
@Test
94+
public void test_list_sold_vehicles() {
95+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
96+
System.setOut(new PrintStream(outContent));
97+
98+
VeiculoRepository repository = new VeiculoRepository();
99+
VeiculoController controller = new VeiculoController(repository);
100+
VeiculoView view = new VeiculoView(controller);
101+
102+
view.listarVendidos();
103+
104+
String expectedOutput = "Nenhum veículo vendido.\r\n";
105+
assertEquals(expectedOutput, outContent.toString());
106+
}
107+
108+
@Test
109+
public void test_list_inventory_empty() {
110+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
111+
System.setOut(new PrintStream(outContent));
112+
113+
VeiculoRepository repository = new VeiculoRepository();
114+
VeiculoController controller = new VeiculoController(repository);
115+
VeiculoView view = new VeiculoView(controller);
116+
117+
controller.venderVeiculo(0);
118+
view.listarEstoque();
119+
120+
String expectedOutput = "Nenhum veículo em estoque disponível.\r\n";
121+
assertEquals(expectedOutput, outContent.toString());
122+
}
123+
124+
@Test
125+
public void test_list_sold_vehicles_empty() {
126+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
127+
System.setOut(new PrintStream(outContent));
128+
129+
VeiculoRepository repository = new VeiculoRepository();
130+
VeiculoController controller = new VeiculoController(repository);
131+
VeiculoView view = new VeiculoView(controller);
132+
133+
view.listarVendidos();
134+
135+
String expectedOutput = "Nenhum veículo vendido.\r\n";
136+
assertEquals(expectedOutput, outContent.toString());
137+
}
138+
139+
}

0 commit comments

Comments
 (0)