jeudi 7 novembre 2019

Java IO, inputstream and Outputstream testing

I try to create a simple chat program , therefor I wrote at first this cod to save an inputed message from user into Text file ,depending on the given Command ,but I don't have any Idea about the testing strategy. wether I have just to write my Test for runUi Method, because this method already contains switch statement and inside it are the other methods Invoked , or I have to test all the methods in addation to runUi .

public class CmdLineUI implements ChatUI, Chat {
public static final String WRITE = "write";
public static final String READ = "read";
public static final String EXIT = "exit";
public static final String ERRORMESSAGE = "unknown command";





private final PrintStream consoleOutput;
private final BufferedReader userInput;

public CmdLineUI(PrintStream os, InputStream is) {
    this.consoleOutput = os;
    this.userInput = new BufferedReader(new InputStreamReader(is));
}

public static void main(String[] args) throws Exception {
    PrintStream os = System.out;
    os.println("***Welcome to The ChatSystem:  input: write for chatting ,"
            + "\n    for Reading the messages input: read ,for closing Chat input: exit ***");

    CmdLineUI userCmd = new CmdLineUI(os, System.in);

    userCmd.runUi(System.in, os);
}



private String[] readCommands() throws Exception {
    String[] result = new String[1];
    InputStreamReader isr = new InputStreamReader(System.in);

    BufferedReader br = new BufferedReader(isr);

    String commandLinString = null;

    String command = null;

    try {

        System.out.println("> ");
        commandLinString = br.readLine();

        StringTokenizer st = new StringTokenizer(commandLinString);
        command = st.nextToken().trim();
        // msg = st.nextToken();

        System.out.println("command :" + command);

    } catch (IOException e) {

        System.out.println("Exception when reading from comman line" + e.getLocalizedMessage());

    }

    result[0] = command;
    // result[1] = msg;

    return result;

}

@Override
public void writeMessage(String message) throws Exception {
    System.out.println("Start with Writing mood:");
    try {
        File file = new File("/home/sami/Desktop/file.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(System.in); // A stream for reading from the
                                                                                // console
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader); // Connect InputStreamReader to a
                                                                                // BufferedReader

        FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
        BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the
                                                                        // BufferedWriter

        String line;
        boolean continuee = true;
        while (!(line = bufferedReader.readLine()).equals("stop")) {
            continuee = false;
            bufferedWriter.write(line);
        }

        // Close the stream
        bufferedWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
public void readMessage() throws Exception {
    System.out.println("start with reading mood:");
    File file = new File("/home/sami/Desktop/file.txt");
    FileInputStream fileinputstream = new FileInputStream(file);

    try {
        int oneByte;
        while ((oneByte = fileinputstream.read()) != -1) {
            System.out.write(oneByte);
        }

        System.out.flush();
    }

    catch (Exception ex) {
        ex.getMessage();
    }

    fileinputstream.close();



}

@Override
public void runUi(InputStream inp, OutputStream outp) throws Exception {
    File file = new File("/home/sami/Desktop/file.txt");
    FileReader filereader = new FileReader(file);
    BufferedReader br = new BufferedReader(filereader);

    // CmdLineUI obj = new CmdLineUI(consoleOutput);

    PrintStream ps = new PrintStream(outp);

    boolean again = true;
    while (again) {

        String[] commandMsg = readCommands();

        String command = commandMsg[0];

        switch (command) {

        case WRITE:

            writeMessage(br.readLine());

            again = true;
            break;

        case READ:

            readMessage();

            again = true;
            break;
        case EXIT:

            again = false;
            break;
        default:
            ps.println(ERRORMESSAGE);
        }
    }

}

}

and I have created these two interfaces here is the first one

public interface ChatUI {

public void runUi (InputStream inp, OutputStream outp) throws Exception;

}

the second Interface

public interface Chat {
 //First Method 
public void writeMessage(String message)throws Exception;

// Second Method
public void readMessage()throws Exception;

}

Aucun commentaire:

Enregistrer un commentaire