Skip to content
This repository was archived by the owner on Sep 27, 2022. It is now read-only.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ru.fizteh.fivt.students.LevkovMiron.ProxyTest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import ru.fizteh.fivt.students.LevkovMiron.Proxy.CTable;
import ru.fizteh.fivt.students.LevkovMiron.Proxy.CTableProvider;
import ru.fizteh.fivt.students.LevkovMiron.Proxy.CTableProviderFactory;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;

/**
* Created by Мирон on 01.12.2014 ru.fizteh.fivt.students.LevkovMiron.ProxyTest.
*/
@RunWith(Parameterized.class)
public class TableCloseabilityTest {

private Method currentMethod;
private CTableProvider provider;
private CTable table;
private CTableProviderFactory factory = new CTableProviderFactory();

@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();

@Before
public void before() throws IOException {
factory = new CTableProviderFactory();
provider = (CTableProvider) factory.create(tmpFolder.newFolder().getAbsolutePath());
table = (CTable) provider.createTable("t1", Arrays.asList(Integer.class, String.class));
}

@Parameterized.Parameters
public static Collection<Method[]> init() {
Method[] methods = CTable.class.getDeclaredMethods();
Method[][] data = new Method[methods.length][1];
for (int i = 0; i < methods.length; i++) {
data[i][0] = methods[i];
}
return Arrays.asList(data);
}

public TableCloseabilityTest(Method method) {
currentMethod = method;
}

@Test (expected = IllegalStateException.class)
public void testCurrentMethod() throws Throwable {
table.close();
if (currentMethod.equals(CTable.class.getMethod("close")) || currentMethod.getModifiers() != 1) {
throw new IllegalStateException();
}
Object[] args = new Object[currentMethod.getParameterCount()];
if (currentMethod.getName().equals("getColumnType")) {
args[0] = 0;
}
try {
currentMethod.invoke(table, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;


public interface Commands<State> {

String getCommandName();

int getArgumentQuantity();

void implement(String[] args, State state) throws SomethingIsWrongException;
}
45 changes: 45 additions & 0 deletions src/ru/fizteh/fivt/students/NikolaiKrivchanskii/Shell/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Parser {


public static ArrayList<String> parseCommandArgs(String params) {
ArrayList<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
Matcher regexMatcher = regex.matcher(params);
while (regexMatcher.find()) {
String param = regexMatcher.group().replaceAll("\"?([~\"]*)\"?", "$1");
matchList.add(param);
}
return matchList;
}

public static String getName(String command) {
int spiltIndex = command.indexOf(' ');
if (spiltIndex == -1) {
return command;
} else {
return command.substring(0, spiltIndex);
}
}

public static String[] parseFullCommand(String commands) {
String[] commandArray = commands.split(";");
for (int i = 0; i < commandArray.length; ++i) {
commandArray[i] = commandArray[i].trim();
}
return commandArray;
}
public static String getParameters(String command) {
int spiltIndex = command.indexOf(' ');
if (spiltIndex == -1) {
return "";
} else {
return command.substring(spiltIndex + 1);
}
}
}
138 changes: 138 additions & 0 deletions src/ru/fizteh/fivt/students/NikolaiKrivchanskii/Shell/Shell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import ru.fizteh.fivt.students.NikolaiKrivchanskii.multifilemap.MultiFileMapShellState;


public class Shell<State> {
private final Map<String, Commands> availableCommands;
private static final String GREETING = "$ ";
State state;


public Shell(Set<Commands> commands) {
Map<String, Commands> tempCommands = new HashMap<String, Commands>();
for (Commands<State> temp : commands) {
tempCommands.put(temp.getCommandName(), temp);
}
availableCommands = Collections.unmodifiableMap(tempCommands);
}

public void setShellState(State state) {
this.state = state;
}

private void runCommand(String[] data, State state) throws SomethingIsWrongException {
if (data[0].length() == 0) {
throw new SomethingIsWrongException("Empty string.");
}
Commands<State> usedOne = availableCommands.get(data[0]);
if (usedOne == null) {
throw new SomethingIsWrongException("Unknown command.");
} else if (data.length - 1 != usedOne.getArgumentQuantity()) {
if (!(usedOne.getCommandName().equals("rm") && data.length - 1 == usedOne.getArgumentQuantity() + 1)
&& !(usedOne.getCommandName().equals("cp") && data.length - 1
== usedOne.getArgumentQuantity() + 1)) {
throw new SomethingIsWrongException("Wrong number of arguments. Correct argument quantity = "
+ (data.length - 1) + "\nTo correctly run this command use "
+ usedOne.getArgumentQuantity() + " arguments.");
}
}
String[] commandArguments = Arrays.copyOfRange(data, 1, data.length);
usedOne.implement(commandArguments, state);
}

private String[] splitLine(String str) {
str = str.trim();
String[] toReturn = str.split("\\s*;\\s*", -2);
return toReturn;
}
private void runLine(String line, State state) throws SomethingIsWrongException {
String[] splitted = splitLine(line);
int count = splitted.length - 1;
for (String temp : splitted) {
--count;
if (count >= 0) {
runCommand(temp.split("\\s+"), state); //This thing is needed to check if after last ";"
} else if (count < 0) { //situated a command or an empty string. So it
if (temp.length() != 0) { //won't throw a "Wrong command" exception when it
runCommand(temp.split("\\s+"), state); //looks like: "dir; cd ..; dir;" neither it will
} //loose a "dir" in: "dir; cd ..; dir; dir".
} //So it does nothing if it is the end, but performs
} //if there is a command after last ";".
}

public void consoleWay(State state) {
Scanner forInput = new Scanner(System.in);
while (!Thread.currentThread().isInterrupted()) {
System.out.print(GREETING);
try {
runLine(forInput.nextLine(), state);
} catch (SomethingIsWrongException exc) {
if (exc.getMessage().equals("EXIT")) {
forInput.close();
System.exit(0);
} else {
System.err.println(exc.getMessage());
}
}
}
forInput.close();
}

public void run(String[] args, Shell<MultiFileMapShellState> shell) {
if (args.length != 0) {
String arg = UtilMethods.uniteItems(Arrays.asList(args), " ");
String[] commands = Parser.parseFullCommand(arg);
try {
runCommand(commands, state);
} catch (SomethingIsWrongException exc) {
if (exc.getMessage().equals("EXIT")) {
System.exit(0);
} else {
System.err.println(exc.getMessage());
System.exit(-1);
}
}
} else {
consoleWay(state);
}
System.exit(0);
}

/* public static void notmain(String[] args) {
ShellState state = new ShellState(System.getProperty("user.dir"));
Set<Commands> commands = new HashSet<Commands>() {{
/*add(new WhereAreWeCommand()); add(new CopyCommand()); add(new DirectoryInfoCommand());
add(new ExitCommand()); add(new MakeDirectoryCommand());
add(new MoveCommand()); add(new RemoveCommand()); }};*/
/* Shell<ShellState> shell = new Shell<ShellState>(commands);

if (args.length != 0) {
String arg = UtilMethods.uniteItems(Arrays.asList(args), " ");
try {
shell.runLine(arg, state);
} catch (SomethingIsWrongException exc) {
if (exc.getMessage().equals("EXIT")) {
System.exit(0);
} else {
System.err.println(exc.getMessage());
System.exit(-1);
}
}
} else {
shell.consoleWay(state);
}
System.exit(0);
}*/



}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

public class ShellState {
private String currentDirectory;
public ShellState(String currentDirectory) {
this.currentDirectory = currentDirectory;
}
public String getCurDir() {
return currentDirectory;
}
void changeCurDir(String newCurDir) {
currentDirectory = newCurDir;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

public abstract class SomeCommand<State> implements Commands<State> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;


public final class SomethingIsWrongException extends Exception {
public SomethingIsWrongException() {
super();
}

public SomethingIsWrongException(String message) {
super(message);
}

public SomethingIsWrongException(String message, Throwable cause) {
super(message, cause);
}

public SomethingIsWrongException(Throwable cause) {
super(cause);
}
}
Loading