").append(m.parseHTML(a)).find(d):a)}).complete(c&&function(a,b){g.each(c,e||[a.responseText,b,a])}),this},m.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(a,b){m.fn[b]=function(a){return this.on(b,a)}}),m.expr.filters.animated=function(a){return m.grep(m.timers,function(b){return a===b.elem}).length};var cd=a.document.documentElement;function dd(a){return m.isWindow(a)?a:9===a.nodeType?a.defaultView||a.parentWindow:!1}m.offset={setOffset:function(a,b,c){var d,e,f,g,h,i,j,k=m.css(a,"position"),l=m(a),n={};"static"===k&&(a.style.position="relative"),h=l.offset(),f=m.css(a,"top"),i=m.css(a,"left"),j=("absolute"===k||"fixed"===k)&&m.inArray("auto",[f,i])>-1,j?(d=l.position(),g=d.top,e=d.left):(g=parseFloat(f)||0,e=parseFloat(i)||0),m.isFunction(b)&&(b=b.call(a,c,h)),null!=b.top&&(n.top=b.top-h.top+g),null!=b.left&&(n.left=b.left-h.left+e),"using"in b?b.using.call(a,n):l.css(n)}},m.fn.extend({offset:function(a){if(arguments.length)return void 0===a?this:this.each(function(b){m.offset.setOffset(this,a,b)});var b,c,d={top:0,left:0},e=this[0],f=e&&e.ownerDocument;if(f)return b=f.documentElement,m.contains(b,e)?(typeof e.getBoundingClientRect!==K&&(d=e.getBoundingClientRect()),c=dd(f),{top:d.top+(c.pageYOffset||b.scrollTop)-(b.clientTop||0),left:d.left+(c.pageXOffset||b.scrollLeft)-(b.clientLeft||0)}):d},position:function(){if(this[0]){var a,b,c={top:0,left:0},d=this[0];return"fixed"===m.css(d,"position")?b=d.getBoundingClientRect():(a=this.offsetParent(),b=this.offset(),m.nodeName(a[0],"html")||(c=a.offset()),c.top+=m.css(a[0],"borderTopWidth",!0),c.left+=m.css(a[0],"borderLeftWidth",!0)),{top:b.top-c.top-m.css(d,"marginTop",!0),left:b.left-c.left-m.css(d,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var a=this.offsetParent||cd;while(a&&!m.nodeName(a,"html")&&"static"===m.css(a,"position"))a=a.offsetParent;return a||cd})}}),m.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(a,b){var c=/Y/.test(b);m.fn[a]=function(d){return V(this,function(a,d,e){var f=dd(a);return void 0===e?f?b in f?f[b]:f.document.documentElement[d]:a[d]:void(f?f.scrollTo(c?m(f).scrollLeft():e,c?e:m(f).scrollTop()):a[d]=e)},a,d,arguments.length,null)}}),m.each(["top","left"],function(a,b){m.cssHooks[b]=Lb(k.pixelPosition,function(a,c){return c?(c=Jb(a,b),Hb.test(c)?m(a).position()[b]+"px":c):void 0})}),m.each({Height:"height",Width:"width"},function(a,b){m.each({padding:"inner"+a,content:b,"":"outer"+a},function(c,d){m.fn[d]=function(d,e){var f=arguments.length&&(c||"boolean"!=typeof d),g=c||(d===!0||e===!0?"margin":"border");return V(this,function(b,c,d){var e;return m.isWindow(b)?b.document.documentElement["client"+a]:9===b.nodeType?(e=b.documentElement,Math.max(b.body["scroll"+a],e["scroll"+a],b.body["offset"+a],e["offset"+a],e["client"+a])):void 0===d?m.css(b,c,g):m.style(b,c,d,g)},b,f?d:void 0,f,null)}})}),m.fn.size=function(){return this.length},m.fn.andSelf=m.fn.addBack,"function"==typeof define&&define.amd&&define("jquery",[],function(){return m});var ed=a.jQuery,fd=a.$;return m.noConflict=function(b){return a.$===m&&(a.$=fd),b&&a.jQuery===m&&(a.jQuery=ed),m},typeof b===K&&(a.jQuery=a.$=m),m});
5 |
--------------------------------------------------------------------------------
/clinic-pet-web/src/main/webapp/views/user/CreateUser.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" pageEncoding="UTF-8" session="true"%>
2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 |
4 |
5 |
6 |
7 |
8 |
9 |
28 |
29 |
--------------------------------------------------------------------------------
/clinic-pet-web/src/main/webapp/views/user/EditUser.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" pageEncoding="UTF-8" session="true"%>
2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 |
4 |
5 |
6 |
7 |
8 |
9 |
29 |
30 |
--------------------------------------------------------------------------------
/clinic-pet-web/src/main/webapp/views/user/UserView.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" pageEncoding="UTF-8" session="true"%>
2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 |
4 |
5 |
6 |
7 |
8 |
9 |
Добавить пользователя
10 |
11 |
12 | Логин Email |
13 | Действия |
14 |
15 |
16 |
17 | ${user.login} ${user.email} |
18 |
19 | Редактировать
20 | Удалить
21 | |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/clinic-pet-web/src/test/java/ru/parsentev/servlets/UserCRUDServletTest.java:
--------------------------------------------------------------------------------
1 | package ru.parsentev.servlets;
2 |
3 | import org.junit.Test;
4 | import org.mockito.Mockito;
5 | import ru.parsentev.store.UserCache;
6 |
7 | import javax.servlet.ServletException;
8 | import javax.servlet.http.HttpServletRequest;
9 | import javax.servlet.http.HttpServletResponse;
10 |
11 | import java.io.IOException;
12 |
13 | import static org.junit.Assert.*;
14 |
15 | /**
16 | * TODO: comment
17 | * @author parsentev
18 | * @since 23.04.2015
19 | */
20 | public class UserCRUDServletTest extends Mockito {
21 |
22 | final UserCache cache = UserCache.getInstance();
23 |
24 | @Test
25 | public void createUser() throws ServletException, IOException {
26 |
27 | HttpServletRequest request = mock(HttpServletRequest.class);
28 | HttpServletResponse response = mock(HttpServletResponse.class);
29 |
30 | when(request.getParameter("login")).thenReturn("test");
31 | when(request.getParameter("email")).thenReturn("test");
32 |
33 | assertTrue(cache.values().isEmpty());
34 |
35 | new UserCreateServlet().doPost(request, response);
36 |
37 | verify(request, atLeast(1)).getParameter("login");
38 | verify(request, atLeast(1)).getParameter("email");
39 | assertFalse(cache.values().isEmpty());
40 |
41 | cache.delete(cache.findByLogin("test").getId());
42 | }
43 | }
--------------------------------------------------------------------------------
/clinic-pet-web/src/test/java/ru/parsentev/store/HibernateStorageTest.java:
--------------------------------------------------------------------------------
1 | package ru.parsentev.store;
2 |
3 | import org.junit.Test;
4 | import ru.parsentev.models.User;
5 |
6 | import static org.junit.Assert.*;
7 |
8 | /**
9 | * TODO: comment
10 | * @author parsentev
11 | * @since 01.05.2015
12 | */
13 | public class HibernateStorageTest {
14 | @Test
15 | public void testCreate() throws Exception {
16 | final HibernateStorage storage = new HibernateStorage();
17 | final int id = storage.add(new User(-1, "hibenate", null));
18 | final User user = storage.get(id);
19 | assertEquals(id, user.getId());
20 | assertEquals(id, storage.findByLogin("hibenate").getId());
21 | storage.delete(id);
22 | assertNull(storage.get(id));
23 | storage.close();
24 | }
25 | }
--------------------------------------------------------------------------------
/clinic-pet-web/src/test/java/ru/parsentev/store/JdbcStorageTest.java:
--------------------------------------------------------------------------------
1 | package ru.parsentev.store;
2 |
3 | import org.junit.Test;
4 | import ru.parsentev.models.User;
5 |
6 | import static org.junit.Assert.*;
7 |
8 | /**
9 | * TODO: comment
10 | * @author parsentev
11 | * @since 29.04.2015
12 | */
13 | public class JdbcStorageTest {
14 |
15 | @Test
16 | public void testCreate() throws Exception {
17 | final JdbcStorage storage = new JdbcStorage();
18 | final int id = storage.add(new User(-1, "test", null));
19 | final User user = storage.get(id);
20 | assertEquals(id, user.getId());
21 | storage.close();
22 | }
23 | }
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | 4.0.0
5 |
6 | ru.parsentev
7 | jlessons
8 | 1.0-SNAPSHOT
9 |
10 | pom
11 |
12 | Java Lesson
13 | http://maven.apache.org
14 |
15 |
16 | se
17 | clinic-pet-web
18 |
19 |
20 |
21 |
22 | org.apache.maven.plugins
23 | maven-compiler-plugin
24 | 3.3
25 |
26 | 1.7
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/se/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | ru.parsentev
6 | se
7 | 1.0-SNAPSHOT
8 | jar
9 |
10 | SE
11 | http://maven.apache.org
12 |
13 |
14 |
15 | commons-lang
16 | commons-lang
17 | 2.1
18 |
19 |
20 | org.codehaus.plexus
21 | plexus-utils
22 | 1.1
23 |
24 |
25 | junit
26 | junit
27 | 4.8.2
28 | test
29 |
30 |
31 |
32 |
33 |
34 |
35 | org.apache.maven.plugins
36 | maven-jar-plugin
37 |
38 |
39 |
40 | ru.lessons.lession_3.Calculate
41 | true
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lession_3/Calculate.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lession_3;
2 |
3 | /**
4 | * Класс для вычисления суммы двух чисел. Числа беруться из входщего массива.
5 | */
6 | public class Calculate {
7 | public static void main(String[] arg) {
8 | System.out.println("Calculate...");
9 | int first = Integer.valueOf(arg[0]);
10 | int second = Integer.valueOf(arg[1]);
11 | int sum = first + second;
12 | System.out.println("Sum : " + sum);
13 | }
14 | }
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_10/User.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_10;
2 |
3 |
4 | /**
5 | * TODO: comment
6 | * @author parsentev
7 | * @since 06.04.2015
8 | */
9 | public class User {
10 | private String id;
11 | private String name;
12 |
13 | public User(String id, String name) {
14 | this.id = id;
15 | this.name = name;
16 | }
17 |
18 | public String getId() {
19 | return id;
20 | }
21 |
22 | public String getName() {
23 | return name;
24 | }
25 |
26 | @Override
27 | public boolean equals(Object o) {
28 | if (this == o) {
29 | return true;
30 | }
31 | if (o == null || getClass() != o.getClass()) {
32 | return false;
33 | }
34 |
35 | User user = (User) o;
36 |
37 | if (id != null ? !id.equals(user.id) : user.id != null) {
38 | return false;
39 | }
40 | if (name != null ? !name.equals(user.name) : user.name != null) {
41 | return false;
42 | }
43 |
44 | return true;
45 | }
46 |
47 | @Override
48 | public int hashCode() {
49 | int result = id != null ? id.hashCode() : 0;
50 | result = 31 * result + (name != null ? name.hashCode() : 0);
51 | return result;
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return "User{" +
57 | "id='" + id + '\'' +
58 | ", name='" + name + '\'' +
59 | '}';
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_10/UserRunner.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_10;
2 |
3 |
4 | import java.util.HashMap;
5 | import java.util.Map;
6 |
7 | /**
8 | * TODO: comment
9 | * @author parsentev
10 | * @since 06.04.2015
11 | */
12 | public class UserRunner {
13 | public static void main(String[] args) {
14 | Map
users = new HashMap();
15 | users.put("1", new User("1", "first"));
16 | users.put("2", new User("2", "two"));
17 | users.put("1", new User("1", "first"));
18 |
19 | for (Map.Entry user : users.entrySet()) {
20 | System.out.println(user);
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_12/BlockQueue.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_12;
2 |
3 | import java.util.LinkedList;
4 | import java.util.Queue;
5 |
6 | /**
7 | * TODO: comment
8 | * @author parsentev
9 | * @since 08.04.2015
10 | */
11 | public class BlockQueue {
12 | public final LinkedList queue = new LinkedList();
13 |
14 | public void push(final T t) {
15 | synchronized (this.queue) {
16 | this.queue.add(t);
17 | this.queue.notifyAll();
18 | }
19 | }
20 |
21 | public T poll() {
22 | synchronized (this.queue) {
23 | while (this.queue.isEmpty()) {
24 | try {
25 | this.queue.wait();
26 | } catch (InterruptedException e) {
27 | e.printStackTrace();
28 | }
29 | }
30 | return this.queue.remove();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_12/Counter.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_12;
2 |
3 |
4 | import java.util.concurrent.atomic.AtomicInteger;
5 |
6 | /**
7 | * TODO: comment
8 | * @author parsentev
9 | * @since 09.04.2015
10 | */
11 | public class Counter {
12 | private AtomicInteger amount = new AtomicInteger(0);
13 |
14 | public int increase() {
15 | return amount.incrementAndGet();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/BaseAction.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 08.04.2015
7 | */
8 | public class BaseAction implements UserAction {
9 | private final GeneratorBoard generator;
10 | private final Board board;
11 | private final SaperLogic logic;
12 |
13 | public BaseAction(final SaperLogic logic, final Board board, final GeneratorBoard generator) {
14 | this.generator = generator;
15 | this.board = board;
16 | this.logic = logic;
17 | }
18 |
19 | public void initGame() {
20 | final Cell[][] cells = generator.generate();
21 | this.board.drawBoard(cells);
22 | this.logic.loadBoard(cells);
23 | }
24 |
25 | public void select(int x, int y, boolean bomb) {
26 | this.logic.suggest(x, y, bomb);
27 | board.drawCell(x, y);
28 | if (this.logic.shouldBang(x, y)) {
29 | this.board.drawBang();
30 | }
31 | if (this.logic.finish()) {
32 | board.drawCongratulate();
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/Board.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13;
2 |
3 | /**
4 | * Описывает поведения доски.
5 | * @author parsentev
6 | * @since 08.04.2015
7 | */
8 | public interface Board {
9 |
10 | /**
11 | * Рисует доску исходя из входящего массива ячеек.
12 | * @param cells Массив ячеек.
13 | */
14 | void drawBoard(Cell[][] cells);
15 |
16 | /**
17 | * Рисует ячеейку.
18 | * @param x позиция по горизонтали.
19 | * @param y позиция по вертикали.
20 | */
21 | void drawCell(int x, int y);
22 |
23 | /**
24 | * Рисует взрыв всех бомб.
25 | */
26 | void drawBang();
27 |
28 | /**
29 | * Рисует подзравления когда игра выграна.
30 | */
31 | void drawCongratulate();
32 | }
33 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/Cell.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 08.04.2015
7 | */
8 | public interface Cell {
9 |
10 | boolean isBomb();
11 |
12 | boolean isSuggestBomb();
13 |
14 | boolean isSuggestEmpty();
15 |
16 | void suggectEmpty();
17 |
18 | void suggectBomb();
19 |
20 | void draw(T paint, boolean real);
21 | }
22 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/GeneratorBoard.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 08.04.2015
7 | */
8 | public interface GeneratorBoard {
9 | Cell[][] generate();
10 | }
11 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/SaperLogic.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13;
2 |
3 | public interface SaperLogic {
4 |
5 | void loadBoard(Cell[][] cells);
6 |
7 | boolean shouldBang(int x, int y);
8 |
9 | boolean finish();
10 |
11 | void suggest(int x, int y, boolean bomb);
12 | }
13 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/UserAction.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13;
2 |
3 | public interface UserAction {
4 |
5 | void initGame();
6 |
7 | void select(int x, int y, boolean bomb);
8 | }
9 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/console/ConsoleBoard.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13.console;
2 |
3 | import ru.lessons.lesson_13.Board;
4 | import ru.lessons.lesson_13.Cell;
5 |
6 | /**
7 | * TODO: comment
8 | * @author parsentev
9 | * @since 08.04.2015
10 | */
11 | public class ConsoleBoard implements Board {
12 | private Cell[][] cells;
13 |
14 | public void drawBoard(Cell[][] cells) {
15 | this.cells = cells;
16 | this.redraw(false);
17 | }
18 |
19 | public void drawCell(int x, int y) {
20 | System.out.println("***** SELECT ******");
21 | this.redraw(false);
22 | }
23 |
24 | public void drawBang() {
25 | System.out.println("***** BANG ******");
26 | this.redraw(true);
27 | }
28 |
29 | public void drawCongratulate() {
30 | System.out.println("***** CONGRATULATE ******");
31 | }
32 |
33 | private void redraw(boolean real) {
34 | for (Cell[] row : cells) {
35 | for (Cell cell : row) {
36 | cell.draw(System.out, real);
37 | }
38 | System.out.println();
39 | }
40 | System.out.println();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/console/ConsoleCell.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13.console;
2 |
3 | import ru.lessons.lesson_13.Cell;
4 |
5 | import java.io.PrintStream;
6 |
7 | /**
8 | * TODO: comment
9 | * @author parsentev
10 | * @since 08.04.2015
11 | */
12 | public class ConsoleCell implements Cell {
13 | private boolean bomb;
14 | private boolean suggestBomb = false;
15 | private boolean suggestEmpty = false;
16 |
17 | public ConsoleCell(boolean bomb) {
18 | this.bomb = bomb;
19 | }
20 |
21 | @Override
22 | public boolean isBomb() {
23 | return this.bomb;
24 | }
25 |
26 | @Override
27 | public boolean isSuggestBomb() {
28 | return this.suggestBomb;
29 | }
30 |
31 | @Override
32 | public boolean isSuggestEmpty() {
33 | return this.suggestEmpty;
34 | }
35 |
36 | @Override
37 | public void suggectEmpty() {
38 | this.suggestEmpty = true;
39 | }
40 |
41 | @Override
42 | public void suggectBomb() {
43 | this.suggestBomb = true;
44 | }
45 |
46 | @Override
47 | public void draw(PrintStream paint, boolean real) {
48 | if (real) {
49 | if (this.isBomb()) {
50 | paint.print("[*] ");
51 | } else {
52 | paint.print("[ ] ");
53 | }
54 | } else {
55 | if (this.suggestBomb) {
56 | paint.print("[?] ");
57 | } else if (this.suggestEmpty) {
58 | paint.print("[ ] ");
59 | } else {
60 | paint.print("[Х] ");
61 | }
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/gui/GUIAction.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13.gui;
2 |
3 | import ru.lessons.lesson_13.BaseAction;
4 | import ru.lessons.lesson_13.Board;
5 | import ru.lessons.lesson_13.GeneratorBoard;
6 | import ru.lessons.lesson_13.SaperLogic;
7 |
8 | import java.awt.event.ActionEvent;
9 | import java.awt.event.ActionListener;
10 | import java.awt.event.MouseEvent;
11 | import java.awt.event.MouseListener;
12 |
13 | /**
14 | * TODO: comment
15 | * @author parsentev
16 | * @since 08.04.2015
17 | */
18 | public class GUIAction extends BaseAction implements ActionListener, MouseListener {
19 | private GUIBoard board;
20 |
21 | public GUIAction(SaperLogic logic, GUIBoard board, GeneratorBoard generator) {
22 | super(logic, board, generator);
23 | this.board = board;
24 | this.board.addMouseListener(this);
25 | }
26 |
27 | public void actionPerformed(ActionEvent e) {
28 | this.initGame();
29 | }
30 |
31 | public void mouseClicked(MouseEvent e) {
32 | board.repaint();
33 | }
34 |
35 | public void mousePressed(MouseEvent e) {
36 | }
37 |
38 | public void mouseReleased(MouseEvent e) {
39 | }
40 |
41 | public void mouseEntered(MouseEvent e) {
42 | }
43 |
44 | public void mouseExited(MouseEvent e) {
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/gui/GUIBoard.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13.gui;
2 |
3 | import ru.lessons.lesson_13.Board;
4 | import ru.lessons.lesson_13.Cell;
5 |
6 | import javax.swing.*;
7 | import java.awt.*;
8 | import java.awt.event.MouseEvent;
9 | import java.awt.event.MouseListener;
10 | import java.util.Random;
11 |
12 | /**
13 | * TODO: comment
14 | * @author parsentev
15 | * @since 08.04.2015
16 | */
17 | public class GUIBoard extends JPanel implements Board {
18 |
19 | public static final int PADDING = 50;
20 |
21 | public Cell[][] cells;
22 |
23 | @Override
24 | protected void paintComponent(Graphics graphics) {
25 | super.paintComponent(graphics);
26 | if (this.cells != null) {
27 | for (int x=0;x!=cells.length;x++) {
28 | for (int y = 0; y!=cells[0].length; y++) {
29 | graphics.setColor(Color.black);
30 | cells[x][y].draw(graphics, false);
31 | graphics.drawRect(x * PADDING, y * PADDING, PADDING, PADDING);
32 | }
33 | }
34 | }
35 | }
36 |
37 | public void drawBoard(Cell[][] cells) {
38 | this.cells = cells;
39 | this.repaint();
40 | }
41 |
42 | public void drawCell(int x, int y) {
43 | this.repaint();
44 | }
45 |
46 | public void drawBang() {
47 | this.repaint();
48 | }
49 |
50 | public void drawCongratulate() {
51 | }
52 | }
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/gui/GUICell.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13.gui;
2 |
3 | import ru.lessons.lesson_13.Cell;
4 |
5 | import java.awt.*;
6 |
7 | /**
8 | * TODO: comment
9 | * @author parsentev
10 | * @since 08.04.2015
11 | */
12 | public class GUICell implements Cell {
13 |
14 | public boolean isBomb() {
15 | return false;
16 | }
17 |
18 | public boolean isSuggestBomb() {
19 | return false;
20 | }
21 |
22 | public boolean isSuggestEmpty() {
23 | return false;
24 | }
25 |
26 | public void suggectEmpty() {
27 | }
28 |
29 | public void suggectBomb() {
30 | }
31 |
32 | public void draw(Graphics paint, boolean real) {
33 |
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/gui/Main.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13.gui;
2 |
3 |
4 | import ru.lessons.lesson_13.Cell;
5 | import ru.lessons.lesson_13.GeneratorBoard;
6 | import ru.lessons.lesson_13.logics.Easy;
7 |
8 | import javax.swing.*;
9 | import javax.swing.border.EmptyBorder;
10 | import java.awt.*;
11 | import java.awt.event.ActionEvent;
12 | import java.awt.event.ActionListener;
13 | import java.awt.event.WindowAdapter;
14 | import java.awt.event.WindowEvent;
15 |
16 | /**
17 | * TODO: comment
18 | * @author parsentev
19 | * @since 08.04.2015
20 | */
21 | public class Main {
22 | private static final JPanel controlPanel = new JPanel();
23 | private static final GUIBoard board = new GUIBoard();
24 |
25 | public static void main(String[] arg) {
26 | SwingUtilities.invokeLater(new Runnable() {
27 | public void run() {
28 | final JFrame frame = new JFrame();
29 | frame.setTitle("Сапер");
30 | frame.setLayout(new BorderLayout());
31 | frame.setSize(500, 500);
32 | frame.add(board, BorderLayout.CENTER);
33 | board.setBorder(new EmptyBorder(10, 10, 10, 10));
34 | frame.add(controlPanel, BorderLayout.PAGE_END);
35 | controlPanel.setLayout(new FlowLayout());
36 | final JButton generate = new JButton("Начать");
37 | generate.addActionListener(
38 | new GUIAction(
39 | new Easy(), board,
40 | new GeneratorBoard() {
41 | public Cell[][] generate() {
42 | return new Cell[][] {{ new GUICell(), new GUICell()}, {new GUICell(), new GUICell()}};
43 | }
44 | }
45 | ));
46 | controlPanel.add(generate);
47 | centre(frame);
48 | frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
49 | frame.addWindowListener(new WindowAdapter() {
50 | @Override
51 | public void windowClosing(WindowEvent e) {
52 | closePerform(frame);
53 | }
54 | });
55 | frame.setVisible(true);
56 | }
57 | });
58 | }
59 |
60 | public static void centre(Window w) {
61 | Dimension us = w.getSize();
62 | Dimension them = Toolkit.getDefaultToolkit().getScreenSize();
63 | int newX = (them.width - us.width) / 2;
64 | int newY = (them.height - us.height) / 2;
65 | w.setLocation(newX, newY);
66 |
67 | }
68 |
69 | public static void closePerform(JFrame frame) {
70 | frame.setVisible(false);
71 | frame.dispose();
72 | System.exit(0);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_13/logics/Easy.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13.logics;
2 |
3 | import ru.lessons.lesson_13.Cell;
4 | import ru.lessons.lesson_13.SaperLogic;
5 |
6 | /**
7 | * TODO: comment
8 | * @author parsentev
9 | * @since 08.04.2015
10 | */
11 | public class Easy implements SaperLogic {
12 | private Cell[][] cells;
13 |
14 | @Override
15 | public void loadBoard(Cell[][] cells) {
16 | this.cells = cells;
17 | }
18 |
19 | @Override
20 | public boolean shouldBang(int x, int y) {
21 | final Cell selected = this.cells[x][y];
22 | return selected.isBomb() && !selected.isSuggestBomb();
23 | }
24 |
25 | @Override
26 | public boolean finish() {
27 | boolean finish = false;
28 | for (Cell[] row : cells) {
29 | for (Cell cell : row) {
30 | finish = ((cell.isSuggestBomb() && cell.isBomb()) ||
31 | (cell.isSuggestEmpty() && !cell.isBomb()));
32 | }
33 | }
34 | return finish;
35 | }
36 |
37 | @Override
38 | public void suggest(int x, int y, boolean bomb) {
39 | if (bomb) {
40 | this.cells[x][y].suggectBomb();
41 | } else {
42 | this.cells[x][y].suggectEmpty();
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_28/Action.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_28;
2 |
3 |
4 | /**
5 | * TODO: comment
6 | * @author parsentev
7 | * @since 03.10.2015
8 | */
9 | public interface Action {
10 |
11 | String operation();
12 |
13 | int arithmetical(int first, int second);
14 | }
15 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_28/Input.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_28;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 01.10.2015
7 | */
8 | public interface Input {
9 | String next();
10 |
11 | String ask(String question);
12 |
13 | void close();
14 | }
15 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_28/InteractCalculator.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_28;
2 |
3 | import ru.lessons.lesson_4.Calculator;
4 | import ru.lessons.lesson_8.UserException;
5 |
6 | /**
7 | * TODO: comment
8 | * @author parsentev
9 | * @since 01.10.2015
10 | */
11 | public class InteractCalculator {
12 | private final Input input;
13 | private final Output output;
14 | private final Calculator calculator;
15 |
16 | public InteractCalculator(Input input, Output output, Calculator calculator) {
17 | this.input = input;
18 | this.output = output;
19 | this.calculator = calculator;
20 | }
21 |
22 | public void action() {
23 | do {
24 | String first = input.ask("Enter first arg : ");
25 | String operation = input.ask("Enter operation : ");
26 | String second = input.ask("Enter second arg : ");
27 | this.calculator.calculate(Integer.valueOf(first), Integer.valueOf(second), operation);
28 | output.println(String.format("Result : %s", calculator.getResult()));
29 | if ("yes".equals(input.ask("Do you want to clean the result?"))) {
30 | calculator.cleanResult();
31 | }
32 | } while ("no".equals(input.ask("Exit : yes/no ")));
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_28/Output.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_28;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 01.10.2015
7 | */
8 | public interface Output {
9 | void println(String line);
10 | }
11 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_28/package-info.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/peterarsentev/java-courses/8ab163fa5b5c26234fa71402486d3c8db3911723/se/src/main/java/ru/lessons/lesson_28/package-info.java
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_4/ArgRunner.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_4;
2 |
3 | /**
4 | * Класс для запуска калькулятор с параметрами.
5 | */
6 | public class ArgRunner {
7 |
8 | public static void main(String[] arg) {
9 | System.out.println("Calculate...");
10 | if (arg.length == 2) {
11 | int first = Integer.valueOf(arg[0]);
12 | int second = Integer.valueOf(arg[1]);
13 | Calculator calc = new Calculator();
14 | calc.add(first, second);
15 | System.out.println("Sum : " + calc.getResult());
16 | } else {
17 | System.out.println("Enter two paramenters.");
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_4/Calculator.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_4;
2 |
3 | import ru.lessons.lesson_28.Action;
4 | import ru.lessons.lesson_8.UserException;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | /**
10 | * Класс реализует калькулятор.
11 | */
12 | public class Calculator {
13 | /**
14 | Результат вычисления.
15 | */
16 | private int result;
17 |
18 | private final Map operation = new HashMap();
19 |
20 | public Calculator() {
21 | this.load(new Add());
22 | this.load(new Div());
23 | }
24 |
25 | public void load(Action action) {
26 | this.operation.put(action.operation(), action);
27 | }
28 |
29 | public void calculate(int first, int second, String operation) {
30 | this.result = this.operation.get(operation).arithmetical(first, second);
31 | }
32 |
33 |
34 | public static final class Add implements Action {
35 |
36 | public String operation() {
37 | return "+";
38 | }
39 |
40 | public int arithmetical(int first, int second) {
41 | return first + second;
42 | }
43 | }
44 |
45 | public static final class Div implements Action {
46 |
47 | public String operation() {
48 | return "/";
49 | }
50 |
51 | public int arithmetical(int first, int second) {
52 | if (second != 0) {
53 | return first / second;
54 | } else {
55 | throw new ArithmeticException();
56 | }
57 | }
58 | }
59 |
60 | /**
61 | * Суммируем аргументы.
62 | * @param params Аргументы суммирования.
63 | */
64 | public void add(int ... params) {
65 | for (int param : params) {
66 | this.result += param;
67 | }
68 | }
69 |
70 | /**
71 | * Вычиляем деление.
72 | * @param args входящие агрументы.
73 | * @throws ru.lessons.lesson_8.UserException Если агрументов нет, выкидывает исключение.
74 | */
75 | public void div(int ... args) throws UserException {
76 | if (args.length > 0) {
77 | this.result = args[0];
78 | for (int params : args) {
79 | if (params == 0) {
80 | throw new IllegalArgumentException("You try to div by 0. Please change arg!");
81 | }
82 | this.result /= params;
83 | }
84 | } else {
85 | throw new UserException("You should enter args!");
86 | }
87 | }
88 |
89 | /**
90 | * Получить результат.
91 | * @return результат вычисления.
92 | */
93 | public int getResult() {
94 | return this.result;
95 | }
96 |
97 | /**
98 | * Очистить результат вычисления.
99 | */
100 | public void cleanResult() {
101 | this.result = 0;
102 | }
103 | }
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_4/InteractRunner.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_4;
2 |
3 | import ru.lessons.lesson_8.UserException;
4 |
5 | import java.util.Scanner;
6 |
7 | /**
8 | * Класс для запуска калькулятора. Поддерживает ввод пользователя.
9 | */
10 | public class InteractRunner {
11 |
12 | public static void main(String[] arg) {
13 | Scanner reader = new Scanner(System.in);
14 | try {
15 | Calculator calc = new Calculator();
16 | String exit = "no";
17 | while (!exit.equals("yes")) {
18 | System.out.println("Enter first arg : ");
19 | String first = reader.next();
20 | System.out.println("Enter second arg : ");
21 | String second = reader.next();
22 | try {
23 | calc.div(Integer.valueOf(first), Integer.valueOf(second));
24 | } catch (UserException e) {
25 | System.out.println(e.getMessage());
26 | System.out.println("Please enter two args.");
27 | }
28 |
29 | System.out.println("Result : " + calc.getResult());
30 | calc.cleanResult();
31 | System.out.println("Exit : yes/no ");
32 | exit = reader.next();
33 | }
34 | } finally {
35 | reader.close();
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_6/Animal.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_6;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 07.04.2015
7 | */
8 | public class Animal implements Pet {
9 | private final String name;
10 |
11 | public Animal(final String name) {
12 | this.name = name;
13 | }
14 |
15 | public String getName() {
16 | return this.name;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_6/Dog.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_6;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 16.04.2015
7 | */
8 | public class Dog implements Pet {
9 | private final Animal animal;
10 |
11 | public Dog(Animal animal) {
12 | this.animal = animal;
13 | }
14 |
15 | public String getName() {
16 | return this.animal.getName();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_6/Pet.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_6;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 16.04.2015
7 | */
8 | public interface Pet {
9 | String getName();
10 | }
11 |
--------------------------------------------------------------------------------
/se/src/main/java/ru/lessons/lesson_8/UserException.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_8;
2 |
3 |
4 | /**
5 | * TODO: comment
6 | * @author parsentev
7 | * @since 31.03.2015
8 | */
9 | public class UserException extends Exception {
10 | public UserException(final String message) {
11 | super(message);
12 | }
13 | }
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_10/CollectionTest.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_10;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.ArrayList;
6 | import java.util.HashMap;
7 | import java.util.HashSet;
8 | import java.util.List;
9 | import java.util.Map;
10 | import java.util.Set;
11 |
12 | import static org.junit.Assert.*;
13 |
14 | public class CollectionTest {
15 | @Test
16 | public void arrayContains() {
17 | List users = new ArrayList();
18 | users.add(new User("1", "first"));
19 | users.add(new User("2", "second"));
20 |
21 | assertTrue(users.contains(new User("1", "first")));
22 | }
23 |
24 | /**
25 | * Зависимость {@link java.util.Set#add} от методов hashCode equals
26 | */
27 | @Test
28 | public void setAdd() {
29 | Set users = new HashSet();
30 | users.add(new User("1", "first"));
31 | users.add(new User("2", "second"));
32 |
33 | assertEquals(2, users.size());
34 | }
35 |
36 | /**
37 | * Зависимость {@link java.util.Map#put} от методов hashCode equals
38 | */
39 | @Test
40 | public void mapPut() {
41 | Map users = new HashMap();
42 | users.put(new User("1", "first"), new User("1", "first"));
43 | users.put(new User("2", "second"), new User("2", "second"));
44 | users.put(new User("1", "first"), new User("3", "first"));
45 |
46 | assertEquals(2, users.size());
47 | }
48 | }
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_10/UserTest.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_10;
2 |
3 | import org.junit.Test;
4 | import ru.lessons.lesson_10.User;
5 |
6 | import static org.junit.Assert.*;
7 |
8 | /**
9 | * Зависимость equals = true то hashCode = true
10 | * Обратная зависимость несправедлива. hashCode = true то equals ?
11 | */
12 | public class UserTest {
13 | /**
14 | * x.equals(x)
15 | */
16 | @Test
17 | public void reflexive() {
18 | User pit = new User("1", "1");
19 | assertEquals(pit, pit);
20 | }
21 |
22 | /**
23 | * x.equals(y) y.equals(x)
24 | */
25 | @Test
26 | public void symmetric() {
27 | User pit = new User("1", "1");
28 | User dim = new User("1", "1");
29 | assertEquals(pit, dim);
30 | assertEquals(dim, pit);
31 | }
32 |
33 | /**
34 | * x.equals(y) y.equals(z) x.equals(z)
35 | */
36 | @Test
37 | public void transitive() {
38 | User pit = new User("1", "1");
39 | User dim = new User("1", "1");
40 | User max = new User("1", "1");
41 | assertEquals(pit, dim);
42 | assertEquals(dim, max);
43 | assertEquals(pit, max);
44 | }
45 |
46 | /**
47 | * multi y.equals(x) consistently
48 | */
49 | @Test
50 | public void consistent() {
51 | User pit = new User("1", "1");
52 | User dim = new User("1", "1");
53 | for (int i=0;i!=10;++i) {
54 | assertEquals(pit, dim);
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_12/BlockQueueTest.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_12;
2 |
3 | import org.junit.Test;
4 | import ru.lessons.lesson_10.User;
5 |
6 | import java.util.Arrays;
7 | import java.util.List;
8 | import java.util.concurrent.atomic.AtomicInteger;
9 |
10 | import static org.junit.Assert.*;
11 | import static org.junit.Assert.assertEquals;
12 |
13 | public class BlockQueueTest {
14 |
15 | @Test
16 | public void queue() throws InterruptedException {
17 | final BlockQueue queue = new BlockQueue();
18 | final List customers = Arrays.asList(new Customer(queue), new Customer(queue));
19 | for (Customer customer : customers) {
20 | customer.start();
21 | }
22 | Producer producer = new Producer(
23 | queue,
24 | Arrays.asList(
25 | new User("1", "1"), new User("2", "2"),
26 | new User("3", "3"), new User("4", "4")
27 | )
28 | );
29 | producer.start();
30 | producer.join();
31 | Thread.sleep(10l);
32 | int count = 0;
33 | for (Customer customer : customers) {
34 | count += customer.size();
35 | }
36 | assertEquals(count, producer.size());
37 | }
38 |
39 |
40 | private static final class Producer extends Thread {
41 | private final BlockQueue queue;
42 | private final List store;
43 |
44 | public Producer(final BlockQueue queue, final List store) {
45 | super();
46 | this.queue = queue;
47 | this.store = store;
48 | }
49 |
50 | @Override
51 | public void run() {
52 | for (User user : this.store) {
53 | this.queue.push(user);
54 | }
55 | }
56 |
57 | public int size() {
58 | return this.store.size();
59 | }
60 | }
61 |
62 | private static final class Customer extends Thread {
63 | private final BlockQueue queue;
64 | private final AtomicInteger counter = new AtomicInteger(0);
65 |
66 | public Customer(final BlockQueue queue) {
67 | super();
68 | this.queue = queue;
69 | }
70 |
71 | @Override
72 | public void run() {
73 | while (true) {
74 | System.out.println(
75 | String.format("%s : %s", Thread.currentThread().getId(), this.queue.poll())
76 | );
77 | counter.incrementAndGet();
78 | }
79 | }
80 |
81 | public int size() {
82 | return this.counter.get();
83 | }
84 | }
85 | }
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_12/CounterTest.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_12;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.Arrays;
6 | import java.util.List;
7 |
8 | import static org.junit.Assert.*;
9 |
10 | public class CounterTest {
11 | @Test
12 | public void increase() throws InterruptedException {
13 | final Counter counter = new Counter();
14 | final List readers = Arrays.asList(
15 | new Reader(counter), new Reader(counter), new Reader(counter), new Reader(counter), new Reader(counter), new Reader(counter),
16 | new Reader(counter), new Reader(counter), new Reader(counter), new Reader(counter), new Reader(counter), new Reader(counter));
17 | for (final Reader reader : readers) {
18 | reader.start();
19 | }
20 | Thread.sleep(1000l);
21 | assertEquals(1201, counter.increase());
22 | }
23 |
24 | private static final class Reader extends Thread {
25 | private final Counter counter;
26 |
27 | public Reader(Counter counter) {
28 | this.counter = counter;
29 | }
30 |
31 | @Override
32 | public void run() {
33 | for (int i = 0; i != 100; ++i) {
34 | System.out.println(String.format("%s : %s", Thread.currentThread().getId(), this.counter.increase()));
35 | }
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_13/BaseActionTest.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_13;
2 |
3 | import org.junit.Test;
4 | import ru.lessons.lesson_13.console.ConsoleBoard;
5 | import ru.lessons.lesson_13.console.ConsoleCell;
6 | import ru.lessons.lesson_13.logics.Easy;
7 |
8 | import static org.junit.Assert.*;
9 |
10 | public class BaseActionTest {
11 |
12 | final BaseAction action = new BaseAction(
13 | new Easy(), new ConsoleBoard(),
14 | new GeneratorBoard() {
15 | @Override
16 | public Cell[][] generate() {
17 | return new Cell[][] {
18 | {new ConsoleCell(true), new ConsoleCell(false)},
19 | {new ConsoleCell(true), new ConsoleCell(false)}};
20 | }
21 | }
22 | );
23 |
24 | @Test
25 | public void successGame() {
26 | action.initGame();
27 | action.select(0, 0, true);
28 | action.select(1, 0, true);
29 | action.select(0, 1, false);
30 | action.select(1, 1, false);
31 | }
32 |
33 | @Test
34 | public void failureGame() {
35 | action.initGame();
36 | action.select(0, 0, true);
37 | action.select(1, 0, false);
38 | }
39 | }
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_28/InputStub.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_28;
2 |
3 | import java.util.Iterator;
4 | import java.util.List;
5 |
6 | /**
7 | * TODO: comment
8 | * @author parsentev
9 | * @since 01.10.2015
10 | */
11 | public final class InputStub implements Input {
12 | private final Iterator answers;
13 | private final Output output;
14 |
15 | public InputStub(Iterator answers, Output output) {
16 | this.answers = answers;
17 | this.output = output;
18 | }
19 |
20 | public String next() {
21 | return this.answers.next();
22 | }
23 |
24 | public String ask(String question) {
25 | this.output.println(question);
26 | return this.answers.next();
27 | }
28 |
29 | public void close() {
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_28/InteractCalculatorTest.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_28;
2 |
3 | import org.junit.Test;
4 | import ru.lessons.lesson_4.Calculator;
5 |
6 | import java.util.Arrays;
7 |
8 | import static org.hamcrest.core.Is.is;
9 | import static org.junit.Assert.*;
10 |
11 | /**
12 | * TODO: comment
13 | * @author parsentev
14 | * @since 01.10.2015
15 | */
16 | public class InteractCalculatorTest {
17 |
18 | @Test
19 | public void testAction() throws Exception {
20 | //assign
21 | final Output out = new OutputStub();
22 | final Input input = new InputStub(Arrays.asList("1", "+", "1", "no", "yes").iterator(), out);
23 | final Calculator calc = new Calculator();
24 | final InteractCalculator interact = new InteractCalculator(input, out, calc);
25 | //action
26 | interact.action();
27 | input.close();
28 | //assert
29 | assertThat(calc.getResult(), is(2));
30 | }
31 | }
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_28/OutputStub.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_28;
2 |
3 | /**
4 | * TODO: comment
5 | * @author parsentev
6 | * @since 01.10.2015
7 | */
8 | public final class OutputStub implements Output {
9 | public void println(String line) {
10 | System.out.println(line);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/se/src/test/java/ru/lessons/lesson_4/CalculatorTest.java:
--------------------------------------------------------------------------------
1 | package ru.lessons.lesson_4;
2 |
3 | import org.junit.Test;
4 | import ru.lessons.lesson_4.Calculator;
5 | import ru.lessons.lesson_8.UserException;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class CalculatorTest {
10 |
11 | @Test
12 | public void testAdd() throws Exception {
13 | Calculator calculator = new Calculator();
14 | calculator.add(1, 1);
15 | assertEquals(2, calculator.getResult());
16 | }
17 |
18 | @Test(expected = UserException.class)
19 | public void divException() throws UserException {
20 | Calculator calculator = new Calculator();
21 | calculator.div();
22 | }
23 |
24 | @Test()
25 | public void div() throws UserException {
26 | Calculator calculator = new Calculator();
27 | calculator.div(1, 1);
28 | assertEquals(1, calculator.getResult());
29 | }
30 |
31 | @Test(expected = IllegalArgumentException.class)
32 | public void divRuntimeException() throws UserException {
33 | Calculator calculator = new Calculator();
34 | calculator.div(1, 0);
35 | }
36 | }
--------------------------------------------------------------------------------