-
Notifications
You must be signed in to change notification settings - Fork 69
FileMap with JUnit (#Задание4) #598
base: master
Are you sure you want to change the base?
Changes from 6 commits
da7a17c
5e0ca7b
895e6fe
1cbccab
7cbea81
10bf88b
1e0693a
e162d7d
b615130
f412af5
512eb42
228acff
ec5d7cd
93aca57
44da6c6
b5fae6b
e74a8c7
7ad5cb3
45290a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Класс @class FactoryImplements отвечает за фабрику по созданию таблиц. | ||
| * Имплиментируя интерфейс TableProviderFactory, переопределяет метод по | ||
| * созданию провайдера базы данных. | ||
| * | ||
| * @author Timokhin Andrew | ||
| */ | ||
|
|
||
| public class FactoryImplements implements TableProviderFactory { | ||
|
|
||
| @Override | ||
| public TableProvider create(String dir) { | ||
| if (dir == null) { | ||
| throw new IllegalArgumentException("Error in create-meth."); | ||
| } | ||
| try { | ||
| return new TableProviderImplements(dir); | ||
| } catch (IOException xcpt) { | ||
| System.out.println(xcpt.toString()); | ||
|
||
| } | ||
| return null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class JUnit { | ||
| public static void main(String[] args) throws IOException, | ||
| IllegalArgumentException, KeyNullAndNotFound { | ||
| FactoryImplements tb = new FactoryImplements(); | ||
| TableProviderImplements tp = (TableProviderImplements) tb | ||
| .create("C:\\DataBase"); | ||
| Table tableA1 = tp.createTable("A1"); | ||
| tableA1 = tp.getTable("A1"); | ||
|
|
||
| /* | ||
| * Uncomment to see, that we have, when we run this programm twice. A1 | ||
| * must be : (1,1); (2,2); (3,3); A2 must be : all changes | ||
| */ | ||
| /* | ||
| * System.out.println("<===twice===>\n"); for (String tm : | ||
| * tableA1.list()) { System.out.println("key -> " + tm + " value -> " + | ||
| * tableA1.get(tm) ); } System.out.println("\n<===twice===>\n"); | ||
| */ | ||
|
|
||
| tableA1.put("1", "1"); | ||
| tableA1.put("2", "2"); | ||
| tableA1.put("3", "3"); | ||
| tableA1.commit(); | ||
| tableA1.put("4", "4"); | ||
| tableA1.put("5", "5"); | ||
| tableA1.put("6", "6"); | ||
| tableA1.put("7", "7"); | ||
| tableA1.commit(); | ||
| tableA1.rollback(); | ||
|
|
||
| for (String tm : tableA1.list()) { | ||
| System.out.println("key -> " + tm + " value -> " + tableA1.get(tm)); | ||
| } | ||
|
|
||
| Table tableA2 = tp.createTable("A2"); | ||
| tableA2 = tp.getTable("A2"); | ||
|
|
||
| /* | ||
| * System.out.println("<===twice===> \n"); for (String time: | ||
| * tableA2.list()){ System.out.println("key -> " + time + " value -> " + | ||
| * tableA2.get(time) ); } System.out.println("\n<===twice===> \n"); | ||
| */ | ||
|
|
||
| tableA2.put("1", "green"); | ||
| tableA2.put("2", "blue"); | ||
|
|
||
| System.out.println("<========NEXT========>\n"); | ||
| tableA2.remove("1"); | ||
|
|
||
| for (String time : tableA2.list()) { | ||
| System.out.println("key -> " + time + " value -> " | ||
| + tableA2.get(time)); | ||
| } | ||
|
|
||
| tableA2.commit(); | ||
| tableA2.put("3", "green"); | ||
| tableA2.put("4", "black"); | ||
| tableA2.put("5", "red"); | ||
| tableA2.commit(); | ||
| try { | ||
| tableA2.get(null); | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println("\n" + e + "\n" + e.getCause() + "\n"); | ||
| } | ||
| System.out.println(tableA2.get("4")); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
|
||
| @SuppressWarnings("serial") | ||
| public class KeyNullAndNotFound extends Exception { | ||
| public KeyNullAndNotFound(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
|
||
| import java.io.DataInputStream; | ||
| import java.io.EOFException; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.util.Iterator; | ||
| import java.util.Vector; | ||
|
||
|
|
||
| /** | ||
| * Класс @class Reader отвечает за физическое чтение данных с жесткого диска или | ||
|
||
| * другого физического носителя информации. | ||
| * | ||
| * | ||
| * @author Timokhin Andrew | ||
| */ | ||
|
|
||
| public class Reader { | ||
| public void read(TableProviderImplements tp) throws IOException { | ||
|
||
| int i; | ||
|
||
| StringBuilder keyBuilder = new StringBuilder(); | ||
| StringBuilder valueBuilder = new StringBuilder(); | ||
| int length; | ||
| String path = tp.dir; | ||
| Vector<TableImplement> agregat = new Vector<TableImplement>(); | ||
|
||
| int validator = 0; | ||
|
||
| File testDir = new File(path); | ||
| if (testDir.list() != null) { | ||
| for (String time : testDir.list()) { | ||
| File checkDir = new File(path + "\\" + time); | ||
|
||
|
|
||
| if (checkDir.isDirectory()) { | ||
| validator++; | ||
| TableImplement dataBase = new TableImplement(time, tp.dir); | ||
| agregat.add(dataBase); | ||
| for (i = 0; i < 16; i++) { | ||
|
||
| Integer numberDir = new Integer(i); | ||
| File locDB = new File(path + "\\" + time + "\\" | ||
| + numberDir.toString() + ".dir"); | ||
|
||
| if (locDB.exists()) { | ||
| for (String file : locDB.list()) { | ||
|
|
||
| try (DataInputStream rd = new DataInputStream( | ||
| new FileInputStream( | ||
| locDB.getAbsolutePath() + "\\" | ||
|
||
| + file))) { | ||
|
|
||
| while (true) { | ||
|
|
||
| try { | ||
| length = rd.readInt(); | ||
| for (int k = 0; k < length; k++) { | ||
| keyBuilder | ||
| .append(rd.readChar()); | ||
|
|
||
| } | ||
| length = rd.readInt(); | ||
|
||
| for (int k = 0; k < length; k++) { | ||
| valueBuilder.append(rd | ||
| .readChar()); | ||
|
|
||
| } | ||
|
|
||
| dataBase.getMap().put( | ||
| keyBuilder.toString(), | ||
| valueBuilder.toString()); | ||
| dataBase.getBackup().put( | ||
| keyBuilder.toString(), | ||
| valueBuilder.toString()); | ||
| keyBuilder.replace(0, | ||
|
||
| keyBuilder.length(), ""); | ||
| valueBuilder.replace(0, | ||
| valueBuilder.length(), ""); | ||
| } catch (EOFException e) { | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } catch (FileNotFoundException fnfe) { | ||
| System.out.println(fnfe.toString()); | ||
|
||
|
|
||
| } | ||
|
|
||
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
| } | ||
| if (validator == 0) { | ||
| return; | ||
| } | ||
| TableImplement[] copy = new TableImplement[agregat.size()]; | ||
| i = 0; | ||
| Iterator<TableImplement> it = agregat.iterator(); | ||
|
|
||
| while (it.hasNext()) { | ||
|
||
|
|
||
| copy[i] = it.next(); | ||
| i++; | ||
| } | ||
| tp.collection = copy; | ||
|
||
| return; | ||
|
|
||
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface Table { | ||
|
|
||
| /** | ||
| * ���������� �������� ���� ������. | ||
| */ | ||
| String getName(); | ||
|
|
||
| /** | ||
| * �������� �������� �� ���������� �����. | ||
| * | ||
| * @param key | ||
| * ����. | ||
| * @return ��������. ���� �� �������, ���������� null. | ||
| * @throws KeyNullAndNotFound | ||
| * | ||
| * @throws IllegalArgumentException | ||
| * ���� �������� ��������� key �������� null. | ||
| */ | ||
| String get(String key) throws IllegalArgumentException, KeyNullAndNotFound; | ||
|
|
||
| /** | ||
| * ������������� �������� �� ���������� �����. | ||
| * | ||
| * @param key | ||
| * ����. | ||
| * @param value | ||
| * ��������. | ||
| * @return ��������, ������� ���� �������� �� ����� ����� �����. ���� ����� | ||
| * �������� �� ���� ��������, ���������� null. | ||
| * | ||
| * @throws IllegalArgumentException | ||
| * ���� �������� ���������� key ��� value �������� null. | ||
| */ | ||
| String put(String key, String value); | ||
|
|
||
| /** | ||
| * ������� �������� �� ���������� �����. | ||
| * | ||
| * @param key | ||
| * ����. | ||
| * @return ��������. ���� �� �������, ���������� null. | ||
| * | ||
| * @throws IllegalArgumentException | ||
| * ���� �������� ��������� key �������� null. | ||
| */ | ||
| String remove(String key); | ||
|
|
||
| /** | ||
| * ���������� ���������� ������ � �������. | ||
| * | ||
| * @return ���������� ������ � �������. | ||
| */ | ||
| int size(); | ||
|
|
||
| /** | ||
| * ��������� �������� ���������. | ||
| * | ||
| * @return ���������� ����������� ������. | ||
| */ | ||
| int commit(); | ||
|
|
||
| /** | ||
| * ��������� ����� ��������� � ������� ��������� ��������. | ||
| * | ||
| * @return ���������� ���������� ������. | ||
| */ | ||
| int rollback(); | ||
|
|
||
| /** | ||
| * ������� ������ ������ ������� | ||
| * | ||
| * @return ������ ������. | ||
| */ | ||
| List<String> list(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Точка в текстах исключений не ставится: см. стандартную библиотеку за примерами.
meth - необычное сокращение
Сообщение об ошибке не описывает причину проблемы и не помогает отладить приложение. Написать нормальное.