Skip to content
Snippets Groups Projects
Commit 9e89b266 authored by ='s avatar =
Browse files

Ferdig lab3

parent 4c25b37e
No related branches found
No related tags found
No related merge requests found
package no.uib.inf101.terminal;
public class CmdEcho implements Command{
@Override
public String run(String[] args) {
String toReturn = "";
for (String s : args){
toReturn += s;
toReturn += " ";
}
return toReturn;
}
@Override
public String getName() {
return "echo";
}
}
package no.uib.inf101.terminal;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
public class CmdJavaProgram implements Command {
private final Class<?> mainClass;
private final String commandName;
/**
* Create a command from a Java program. Programs that read from
* standard input are not supported, but printing to standard
* output (i.e. using System.out to print) is ok.
*
* @param commandName The name of the command
* @param mainClass The class containing a main(String[]) method
*/
public CmdJavaProgram(String commandName, Class<?> mainClass) {
this.mainClass = mainClass;
this.commandName = commandName;
}
@Override
public String getName() {
return this.commandName;
}
@Override
public String run(String[] args) {
// Remember original System.out and System.err
final PrintStream orgOut = System.out;
final PrintStream orgErr = System.err;
try {
// Prepare for capturing the output of the program
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream newOut = new PrintStream(out, true, StandardCharsets.UTF_8);
System.setOut(newOut);
System.setErr(newOut);
// Invoke main method of the class for which this command was created
Method main = this.mainClass.getMethod("main", String[].class);
main.invoke(null, new Object[] {args});
// Convert the captured output to a string and return it
return out.toString(StandardCharsets.UTF_8);
} catch (NoSuchMethodException e) {
return "No main method found in class " + this.mainClass.getName();
} catch (Exception e) {
return e.toString();
} finally {
// Restore the original System.out and System.err
System.setOut(orgOut);
System.setErr(orgErr);
}
}
}
package no.uib.inf101.terminal;
public interface Command {
/**
* Called when a command is run
* @param args
* @return
*/
String run(String[] args);
/**
*
* @return name of the command
*/
String getName();
}
package no.uib.inf101.terminal;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
......@@ -7,10 +7,18 @@ public class Main {
public static void main(String[] args) {
// Create a new shell
CommandLineInterface shell = new DummyShell();
SimpleShell shell = new SimpleShell();
shell.installCommand(new CmdEcho());
// When you type hello into terminal, output will be Hello World!
shell.installCommand(new CmdJavaProgram("hello", HelloWorld.class));
// WHen you type new, a new terminal will open.
shell.installCommand(new CmdJavaProgram("new", Main.class));
// Run the shell in the terminal GUI
Terminal gui = new Terminal(shell);
gui.run();
}
}
\ No newline at end of file
......@@ -15,11 +15,12 @@ package no.uib.inf101.terminal;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
// TODO 1: Let SimpleShell implement CommandLineInterface
public class SimpleShell {
public class SimpleShell implements CommandLineInterface{
//////////////////////////////////////////////////////////////////////
/// Instance variables ///////////////////////////////////////////////
......@@ -34,6 +35,7 @@ public class SimpleShell {
/** The command currently being typed */
private String currentCommand = "";
// TODO 4: Create instance variable storing Command objects
private final HashMap<String, Command> allCommands = new HashMap<>();
//////////////////////////////////////////////////////////////////////
/// Public instance methods //////
......@@ -55,7 +57,7 @@ public class SimpleShell {
*
* @param key The key that was pressed
*/
public void aKeyIsPressed(char key) {
public void keyPressed(char key) {
if (key == '\n') {
this.processCurrentCommandLine();
} else if (key >= ' ' && key <= '~') {
......@@ -71,7 +73,7 @@ public class SimpleShell {
*
* @return the text
*/
public String whatTheScreenLooksLike() {
public String getScreenContent() {
String s = "";
for (String line : this.history) {
s += line;
......@@ -82,6 +84,10 @@ public class SimpleShell {
}
// TODO 5: Add method to install a command
public void installCommand(Command command) {
this.allCommands.put(command.getName(), command);
}
//////////////////////////////////////////////////////////////////////
/// Private methods ///////////////////
......@@ -122,7 +128,11 @@ public class SimpleShell {
private String executeCommand(String commandName, String[] args) {
// TODO 6: Call run on Command object for given commandName if present
// TODO 7-8-9: Remove if's for cd, ls, pwd once installed as commands
if (Objects.equals(commandName, "pwd")) {
Command command = this.allCommands.get(commandName);
if (command != null) {
return command.run(args);
}
else if (Objects.equals(commandName, "pwd")) {
return this.doPwd(args);
} else if (Objects.equals(commandName, "cd")) {
return this.doCd(args);
......@@ -133,6 +143,7 @@ public class SimpleShell {
}
}
// TODO 7: remove this method and replace it with Command -type object
private String doPwd(String[] args) {
return this.context.getCwd().getAbsolutePath();
......
......@@ -13,7 +13,7 @@ public class TextAnswers {
*
* What is the type of the variable cli?
*/
static final String q1 = "";
static final String q1 = "CommandLineInterface";
/**
* Consider the following code:
......@@ -21,7 +21,7 @@ public class TextAnswers {
*
* In which class is the object cli refers to?
*/
static final String q2 = "";
static final String q2 = "DummyShell";
/**
* Consider the following code:
......@@ -29,7 +29,7 @@ public class TextAnswers {
*
* true or false: CommandLineInterface is both a type and an interface.
*/
static final Boolean q3 = null;
static final Boolean q3 = true;
/**
* Consider the following code:
......@@ -37,6 +37,6 @@ public class TextAnswers {
*
* true or false: DummyShell is both a class and a type.
*/
static final Boolean q4 = null;
static final Boolean q4 = true;
}
......@@ -23,71 +23,71 @@ public class TestSimpleShellStarter {
@Test
public void testDoNothing() {
assertEquals("$ ", shell.whatTheScreenLooksLike());
assertEquals("$ ", shell.getScreenContent());
}
@Test
public void testWriteFoo() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
shell.keyPressed('f');
shell.keyPressed('o');
shell.keyPressed('o');
assertEquals("$ foo", shell.whatTheScreenLooksLike());
assertEquals("$ foo", shell.getScreenContent());
}
@Test
public void testIllegalCommand() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('\n');
shell.keyPressed('f');
shell.keyPressed('o');
shell.keyPressed('o');
shell.keyPressed('\n');
assertEquals("$ foo\nCommand not found: \"foo\"\n$ ", shell.whatTheScreenLooksLike());
assertEquals("$ foo\nCommand not found: \"foo\"\n$ ", shell.getScreenContent());
}
@Test
public void testPwd() throws IOException {
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
shell.keyPressed('p');
shell.keyPressed('w');
shell.keyPressed('d');
shell.keyPressed('\n');
String expected = "$ pwd" + "\n" + this.dir.getCanonicalPath() + "\n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
assertEquals(expected, shell.getScreenContent());
}
@Test
public void testLs() throws IOException {
shell.aKeyIsPressed('l');
shell.aKeyIsPressed('s');
shell.aKeyIsPressed('\n');
shell.keyPressed('l');
shell.keyPressed('s');
shell.keyPressed('\n');
String expected = "$ ls\n" + SUBDIR + " \n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
assertEquals(expected, shell.getScreenContent());
}
@Test
public void testCd() throws IOException {
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
shell.aKeyIsPressed('c');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed(' ');
shell.aKeyIsPressed('s');
shell.aKeyIsPressed('u');
shell.aKeyIsPressed('b');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('i');
shell.aKeyIsPressed('r');
shell.aKeyIsPressed('\n');
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
shell.keyPressed('p');
shell.keyPressed('w');
shell.keyPressed('d');
shell.keyPressed('\n');
shell.keyPressed('c');
shell.keyPressed('d');
shell.keyPressed(' ');
shell.keyPressed('s');
shell.keyPressed('u');
shell.keyPressed('b');
shell.keyPressed('d');
shell.keyPressed('i');
shell.keyPressed('r');
shell.keyPressed('\n');
shell.keyPressed('p');
shell.keyPressed('w');
shell.keyPressed('d');
shell.keyPressed('\n');
String expected = "$ pwd\n"
......@@ -97,7 +97,7 @@ public class TestSimpleShellStarter {
+ this.subdir.getCanonicalPath() + "\n"
+ "$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
assertEquals(expected, shell.getScreenContent());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment