This repository was archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Дмитрий Морозов, 396, JUnit #541
Open
annoing-morda
wants to merge
10
commits into
dkomanov:master
Choose a base branch
from
annoing-morda:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9c3ece5
JUnit
0dc5159
Checkstyle 1
7d718aa
Checkstyle 2
4924473
Checkstyle 3
db466ce
Checkstyle 3
c490fcd
Fixed
a2a1546
Fixed 1 and checkstyle
7fc4f4a
Fixed1
7f17c5b
Fixed stuff
0a5b849
Fixed stuff + checkstyle
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/ru/fizteh/fivt/students/dmitry_morozov/junit/BadDBFileException.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ru.fizteh.fivt.students.dmitry_morozov.junit; | ||
|
|
||
| public class BadDBFileException extends RuntimeException { | ||
|
|
||
| public BadDBFileException(String msg) { | ||
| super(msg); | ||
| } | ||
|
|
||
| } |
24 changes: 24 additions & 0 deletions
24
src/ru/fizteh/fivt/students/dmitry_morozov/junit/ChangingCommand.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package ru.fizteh.fivt.students.dmitry_morozov.junit; | ||
|
|
||
| enum CommandName { | ||
| PUT, REMOVE | ||
| }; | ||
|
|
||
| public class ChangingCommand { | ||
| public CommandName cName; | ||
| public String[] args; | ||
|
|
||
| public ChangingCommand(CommandName c, String arg1, String arg2) { | ||
| cName = c; | ||
| if (cName == CommandName.PUT) { | ||
| args = new String[2]; | ||
| args[0] = arg1; | ||
| args[1] = arg2; | ||
| } | ||
| if (cName == CommandName.REMOVE) { | ||
| args = new String[1]; | ||
| args[0] = arg1; | ||
| } | ||
| } | ||
|
|
||
| } | ||
89 changes: 89 additions & 0 deletions
89
src/ru/fizteh/fivt/students/dmitry_morozov/junit/DBCollection.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package ru.fizteh.fivt.students.dmitry_morozov.junit; | ||
|
|
||
| import java.util.List; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| import ru.fizteh.fivt.storage.strings.Table; | ||
| import ru.fizteh.fivt.storage.strings.TableProvider; | ||
|
|
||
| public class DBCollection implements TableProvider { | ||
| private String dirPath; | ||
| private FileMap maps; | ||
| private static final String INFO_FILE_NAME = "tables_info.dat"; | ||
|
|
||
| public DBCollection(String dirPath) throws IllegalArgumentException { | ||
| this.dirPath = dirPath; | ||
| maps = null; | ||
| if (dirPath == null) { | ||
| throw new IllegalArgumentException("path is null"); | ||
| } | ||
| if (dirPath.endsWith(File.separator) && !dirPath.equals(File.separator)) { | ||
| this.dirPath = dirPath.substring(0, dirPath.length() - 1); | ||
| } | ||
| try { | ||
| maps = new FileMap(dirPath + File.separator + INFO_FILE_NAME); | ||
| } catch (BadDBFileException | IOException e) { | ||
| throw new IllegalArgumentException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public List<String> showTables() { // Actually doesn't | ||
| // throw anything. | ||
| return maps.list(); | ||
| } | ||
|
|
||
| @Override | ||
| public Table getTable(String name) throws IllegalArgumentException { | ||
| String fullPath = maps.get(name); | ||
| if (fullPath == null) { | ||
| return null; | ||
| } | ||
| Table res; | ||
| try { | ||
| res = new TableWithTransactions(name); | ||
| } catch (BadDBFileException e) { | ||
| throw new IllegalArgumentException(e.getMessage()); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| @Override | ||
| public Table createTable(String name) { | ||
| // TODO Auto-generated method stub | ||
| if (maps.get(name) != null) { | ||
| return null; | ||
| } | ||
| maps.put(name, dirPath + File.separator + name); | ||
| File dir = new File(dirPath + File.separator + name); | ||
| if (!dir.mkdirs()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| TableWithTransactions res; | ||
| try { | ||
| res = new TableWithTransactions(dirPath + File.separator + name); | ||
| } catch (BadDBFileException e) { | ||
| maps.remove(name); | ||
| throw new IllegalStateException(); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| @Override | ||
| public void removeTable(String name) { | ||
| String tname = maps.get(name); | ||
| if (tname != null) { // Database found. | ||
| if (!Utils.removeDirectory(tname)) { | ||
| throw new IllegalStateException("Couldn't remove directory"); | ||
| } else { | ||
| maps.remove(name); | ||
| } | ||
| } else { // Database not found. | ||
| throw new IllegalStateException(); | ||
| } | ||
| } | ||
|
|
||
| public void close() throws IOException { | ||
| maps.close(); | ||
| } | ||
| } |
158 changes: 158 additions & 0 deletions
158
src/ru/fizteh/fivt/students/dmitry_morozov/junit/FileMap.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package ru.fizteh.fivt.students.dmitry_morozov.junit; | ||
|
|
||
| import java.io.DataInputStream; | ||
| import java.io.DataOutputStream; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.Set; | ||
|
|
||
| import ru.fizteh.fivt.storage.strings.Table; | ||
|
|
||
| public class FileMap implements Table { | ||
| private Map<String, String> table; | ||
| private File dbFile; | ||
|
|
||
| public FileMap(String path) throws BadDBFileException, IOException { | ||
| table = new HashMap<>(); | ||
| dbFile = new File(path); | ||
| if (!dbFile.exists()) { | ||
| if (!dbFile.createNewFile()) { | ||
| throw new BadDBFileException("Couldn't create db file"); | ||
| } | ||
| } else { | ||
| if (!dbFile.isFile()) { | ||
| throw new BadDBFileException("Is not a file"); | ||
| } | ||
| } | ||
| if (!(dbFile.setReadable(true)) && dbFile.setWritable(true)) { | ||
| throw new BadDBFileException("Couldn't set rw options"); | ||
| } | ||
| DataInputStream in = new DataInputStream(new FileInputStream(dbFile)); | ||
|
|
||
| while (true) { | ||
| String key = readString(in); | ||
| if (key == null) { | ||
| break; | ||
| } | ||
| String value = readString(in); | ||
| if (value == null) { | ||
| in.close(); | ||
| throw new BadDBFileException("Couldn't set rw options"); | ||
| } | ||
| table.put(key, value); | ||
| } | ||
| in.close(); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @return String read from file. If meets end of file, returns null. | ||
| * @throws BadDBFileException | ||
| * @throws IOException | ||
| * */ | ||
|
|
||
| private String readString(DataInputStream in) throws IOException, | ||
| BadDBFileException { | ||
| final int sizeOfInt = 4; | ||
| int len; | ||
| StringBuilder res = new StringBuilder(); | ||
| if (in.available() >= sizeOfInt) { | ||
| len = in.readInt(); | ||
| if (0 != len % 2 || in.available() < len) { | ||
| in.close(); | ||
| throw new BadDBFileException("File was damaged"); | ||
| } | ||
| len /= 2; | ||
| while (len > 0) { | ||
| char curChar = in.readChar(); | ||
| res.append(curChar); | ||
| len--; | ||
| } | ||
| } else { | ||
| return null; | ||
| } | ||
| return res.toString(); | ||
| } | ||
|
|
||
| public String put(String key, String value) throws IllegalArgumentException { | ||
| if (key == null) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| String res = table.get(key); | ||
| table.put(key, value); | ||
| return res; | ||
| } | ||
|
|
||
| public String get(String key) throws IllegalArgumentException { | ||
| if (key == null) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| String val = table.get(key); | ||
| return val; | ||
| } | ||
|
|
||
| public List<String> list() { | ||
| List<String> res = new ArrayList<>(); | ||
| Set<Entry<String, String>> tableSet = table.entrySet(); | ||
| for (Entry<String, String> i : tableSet) { | ||
| res.add(i.getKey()); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| public String remove(String key) throws IllegalArgumentException { | ||
| if (key == null) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| return table.remove(key); | ||
| } | ||
|
|
||
| public void close() throws IOException { | ||
| DataOutputStream out = new DataOutputStream( | ||
| new FileOutputStream(dbFile)); | ||
| Set<Entry<String, String>> tableSet = table.entrySet(); | ||
| for (Entry<String, String> it : tableSet) { | ||
| writeData(out, it.getKey()); | ||
| writeData(out, it.getValue()); | ||
| } | ||
| out.flush(); | ||
| out.close(); | ||
| } | ||
|
|
||
| private void writeData(DataOutputStream out, String toWrite) | ||
| throws IOException { | ||
| int len = toWrite.length(); | ||
| out.writeInt(len * 2); | ||
| out.writeChars(toWrite); | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return table.isEmpty(); | ||
| } | ||
|
|
||
| public int size() { | ||
| return table.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return dbFile.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public int commit() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. хотелось бы видеть здесь реализацию
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это реализация класса FileMap из второго задания, там вообще не должно быть механизма транзакций. Добавил commit() и rollback(), потому что унаследовал класс от Table. |
||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public int rollback() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и здесь |
||
| return 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Всё в этом файле - полный ад. Перепроектировать.
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.
Даже если будете оставлять, то прочитайте про возможности enum-ов в Джаве и сделайте честные enum-ы-объекты