diff --git a/.gitignore b/.gitignore index 84170b2..7636016 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +testDir +testFile + *.class # Mobile Tools for Java (J2ME) diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..f74504a --- /dev/null +++ b/build.gradle @@ -0,0 +1,14 @@ +group 'com.githun.nizshee' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..e582b84 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Oct 12 12:15:41 MSK 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..27309d9 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f6d5974 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..f805b28 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'hw12' + diff --git a/src/main/java/com/github/nizshee/client/Client.java b/src/main/java/com/github/nizshee/client/Client.java new file mode 100644 index 0000000..790d40e --- /dev/null +++ b/src/main/java/com/github/nizshee/client/Client.java @@ -0,0 +1,48 @@ +package com.github.nizshee.client; + + +import com.github.nizshee.shared.ListMethod; +import com.github.nizshee.shared.Method; +import com.github.nizshee.shared.RemoteFile; + +import java.io.*; +import java.net.Socket; +import java.util.List; + +@SuppressWarnings("all") +public class Client { + + private final String host; + private final int port; + + public Client(String host, int port) throws IOException { + this.host = host; + this.port = port; + } + + public List getList(String name) throws IOException { + return Method.getValue(new Socket(host, port), 1, new ListMethod(), name); + } + + public void getFile(String name) throws IOException { + try (Socket socket = new Socket(host, port)) { + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + dos.writeInt(2); + dos.writeUTF(name); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + long size = dis.readLong(); + byte[] buffer = new byte[1024]; + File file = new File(name); + try (FileOutputStream fos = new FileOutputStream(file)) { + long total = 0; + int partSize = 0; + while (partSize != -1 && total < size) { + fos.write(buffer, 0, (int) Math.min(size - total, partSize)); + total += partSize; + partSize = dis.read(buffer); + } + if (total != size) throw new IOException("Not enough bytes"); + } + } + } +} diff --git a/src/main/java/com/github/nizshee/client/ClientMain.java b/src/main/java/com/github/nizshee/client/ClientMain.java new file mode 100644 index 0000000..138d9cb --- /dev/null +++ b/src/main/java/com/github/nizshee/client/ClientMain.java @@ -0,0 +1,30 @@ +package com.github.nizshee.client; + + +import com.github.nizshee.shared.RemoteFile; + +import java.util.List; + +public class ClientMain { + + public static void main(String[] args) throws Exception { + Client client = new Client("localhost", 8080); + + if (args.length == 2 && args[0].equals("list")) { + List list = client.getList(args[1]); + for (RemoteFile file: list) { + System.out.print(file + " "); + if (file.isDirectory) { + System.out.println(""); + } else { + System.out.println(); + } + } + } else if (args.length == 2 && args[0].equals("get")) { + client.getFile(args[1]); + System.out.println("file downloaded"); + } else { + System.out.println("usage: list | get "); + } + } +} diff --git a/src/main/java/com/github/nizshee/server/CopyFileHandler.java b/src/main/java/com/github/nizshee/server/CopyFileHandler.java new file mode 100644 index 0000000..3a80eda --- /dev/null +++ b/src/main/java/com/github/nizshee/server/CopyFileHandler.java @@ -0,0 +1,27 @@ +package com.github.nizshee.server; + + +import java.io.*; + + +@SuppressWarnings("all") +public class CopyFileHandler implements Handler { + @Override + public void handle(DataInputStream dis, DataOutputStream dos) throws IOException { + String name = dis.readUTF(); + File file = new File(name); + try (FileInputStream fis = new FileInputStream(file)) { + if (!file.exists()) { + dos.writeLong(0); + return; + } + dos.writeLong(file.length()); + byte[] buffer = new byte[1024]; + int size = 0; + while (size != -1) { + dos.write(buffer, 0, size); + size = fis.read(buffer); + } + } + } +} diff --git a/src/main/java/com/github/nizshee/server/Handler.java b/src/main/java/com/github/nizshee/server/Handler.java new file mode 100644 index 0000000..b17a800 --- /dev/null +++ b/src/main/java/com/github/nizshee/server/Handler.java @@ -0,0 +1,12 @@ +package com.github.nizshee.server; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface Handler { + + void handle(DataInputStream dis, DataOutputStream dos) throws IOException; + +} diff --git a/src/main/java/com/github/nizshee/server/MethodWrapper.java b/src/main/java/com/github/nizshee/server/MethodWrapper.java new file mode 100644 index 0000000..1e5daeb --- /dev/null +++ b/src/main/java/com/github/nizshee/server/MethodWrapper.java @@ -0,0 +1,23 @@ +package com.github.nizshee.server; + + +import com.github.nizshee.shared.Method; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class MethodWrapper implements Handler { + private final Method method; + + public MethodWrapper(Method method) { + this.method = method; + } + + @Override + public void handle(DataInputStream dis, DataOutputStream dos) throws IOException { + Request request = method.readRequest(dis); + Response response = method.execute(request); + method.writeResponse(dos, response); + } +} diff --git a/src/main/java/com/github/nizshee/server/Server.java b/src/main/java/com/github/nizshee/server/Server.java new file mode 100644 index 0000000..d7189c0 --- /dev/null +++ b/src/main/java/com/github/nizshee/server/Server.java @@ -0,0 +1,82 @@ +package com.github.nizshee.server; + + +import com.github.nizshee.shared.ServerNotRunningException; +import com.github.nizshee.shared.ServerRunningException; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.*; +import java.util.Map; + +public class Server { + + private final InetAddress host; + private final Integer port; + private ServerSocket socket = null; + + public Server(InetAddress host, Integer port) { + this.host = host; + this.port = port; + } + + public synchronized void start(Map handlers) throws IOException, ServerRunningException { + if (socket != null) throw new ServerRunningException(); + socket = new ServerSocket(port, 50, host); + Thread thread = new Thread(new ServerRunnable(socket, handlers)); + thread.start(); + } + + @SuppressWarnings("all") + public void startSync(Map handlers) + throws IOException, ServerRunningException { + synchronized (this) { + if (socket != null) throw new ServerRunningException(); + socket = new ServerSocket(port, 50, host); + } + (new ServerRunnable(socket, handlers)).run(); + } + + public synchronized void stop() throws IOException, ServerNotRunningException { + if (socket == null) throw new ServerNotRunningException(); + socket.close(); + socket = null; + } + + private static class ServerRunnable implements Runnable { + + private final ServerSocket serverSocket; + private final Map handlers; + + ServerRunnable(ServerSocket serverSocket, Map handlers) { + this.serverSocket = serverSocket; + this.handlers = handlers; + } + + public void run() { + try { + System.out.println("Server started."); + //noinspection InfiniteLoopStatement + while (true) { + Socket socket = serverSocket.accept(); + try { + DataInputStream dis = new DataInputStream(socket.getInputStream()); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + Integer key = dis.readInt(); + if (handlers.containsKey(key)) { + Handler wrapper = handlers.get(key); + wrapper.handle(dis, dos); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + socket.close(); + } + } + } catch (IOException e) { + System.err.println(e.getMessage()); + } + } + } +} diff --git a/src/main/java/com/github/nizshee/server/ServerMain.java b/src/main/java/com/github/nizshee/server/ServerMain.java new file mode 100644 index 0000000..d55c772 --- /dev/null +++ b/src/main/java/com/github/nizshee/server/ServerMain.java @@ -0,0 +1,25 @@ +package com.github.nizshee.server; + + +import com.github.nizshee.shared.ListMethod; + +import java.net.InetAddress; +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("all") +public class ServerMain { + + private final static Map HANDLERS; + + static { + HANDLERS = new HashMap<>(); + HANDLERS.put(1, new MethodWrapper(new ListMethod())); + HANDLERS.put(2, new CopyFileHandler()); + } + + public static void main(String[] args) throws Exception { + Server server = new Server(InetAddress.getByName("localhost"), 8080); + server.startSync(HANDLERS); + } +} diff --git a/src/main/java/com/github/nizshee/shared/ListMethod.java b/src/main/java/com/github/nizshee/shared/ListMethod.java new file mode 100644 index 0000000..839e9c8 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/ListMethod.java @@ -0,0 +1,56 @@ +package com.github.nizshee.shared; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +public class ListMethod implements Method> { + + @Override + public List execute(String name) { + Path dir = Paths.get(name); + File[] files = dir.toFile().listFiles(); + if (files == null) return Collections.emptyList(); + List list = new LinkedList<>(); + for (File file: files) { + list.add(new RemoteFile(file.getName(), file.isDirectory())); + } + return list; + } + + @Override + public void writeRequest(DataOutputStream dos, String s) throws IOException { + dos.writeUTF(s); + } + + @Override + public String readRequest(DataInputStream dis) throws IOException { + return dis.readUTF(); + } + + @Override + public void writeResponse(DataOutputStream dos, List remoteFiles) throws IOException { + dos.writeInt(remoteFiles.size()); + for (RemoteFile file: remoteFiles) { + dos.writeUTF(file.name); + dos.writeBoolean(file.isDirectory); + } + } + + @Override + public List readResult(DataInputStream dis) throws IOException { + int size = dis.readInt(); + List list = new LinkedList<>(); + for (int i = 0; i < size; ++i) { + String name = dis.readUTF(); + boolean isDirectory = dis.readBoolean(); + list.add(new RemoteFile(name, isDirectory)); + } + return list; + } +} diff --git a/src/main/java/com/github/nizshee/shared/Method.java b/src/main/java/com/github/nizshee/shared/Method.java new file mode 100644 index 0000000..4a6fd2a --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/Method.java @@ -0,0 +1,30 @@ +package com.github.nizshee.shared; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; + +public interface Method { + Response execute(Request request); + + void writeRequest(DataOutputStream dos, Request request) throws IOException; + + Request readRequest(DataInputStream dis) throws IOException; + + void writeResponse(DataOutputStream dos, Response response) throws IOException; + + Response readResult(DataInputStream dis) throws IOException; + + static Response getValue(Socket socket, int code, Method method, Request request) + throws IOException { + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + dos.writeInt(code); + method.writeRequest(dos, request); + Response response = method.readResult(dis); + socket.close(); + return response; + } +} diff --git a/src/main/java/com/github/nizshee/shared/RemoteFile.java b/src/main/java/com/github/nizshee/shared/RemoteFile.java new file mode 100644 index 0000000..839ef60 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/RemoteFile.java @@ -0,0 +1,29 @@ +package com.github.nizshee.shared; + + +@SuppressWarnings("all") +public class RemoteFile { + + public final boolean isDirectory; + public final String name; + + public RemoteFile(String name, boolean isDirectory) { + this.isDirectory = isDirectory; + this.name = name; + } + + @Override + public int hashCode() { + return Boolean.hashCode(isDirectory) + name.hashCode() % 34; + } + + @Override + public boolean equals(Object o) { + return o instanceof RemoteFile && ((RemoteFile) o).name.equals(name) && ((RemoteFile) o).isDirectory == isDirectory; + } + + @Override + public String toString() { + return name; + } +} diff --git a/src/main/java/com/github/nizshee/shared/ServerNotRunningException.java b/src/main/java/com/github/nizshee/shared/ServerNotRunningException.java new file mode 100644 index 0000000..581c8a2 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/ServerNotRunningException.java @@ -0,0 +1,8 @@ +package com.github.nizshee.shared; + + +public class ServerNotRunningException extends Exception { + public ServerNotRunningException() { + super("Server not running."); + } +} diff --git a/src/main/java/com/github/nizshee/shared/ServerRunningException.java b/src/main/java/com/github/nizshee/shared/ServerRunningException.java new file mode 100644 index 0000000..0abd8e9 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/ServerRunningException.java @@ -0,0 +1,8 @@ +package com.github.nizshee.shared; + + +public class ServerRunningException extends Exception { + public ServerRunningException() { + super("Server already running."); + } +} diff --git a/src/test/java/com/github/nizshee/ClientServerTest.java b/src/test/java/com/github/nizshee/ClientServerTest.java new file mode 100644 index 0000000..4016896 --- /dev/null +++ b/src/test/java/com/github/nizshee/ClientServerTest.java @@ -0,0 +1,71 @@ +package com.github.nizshee; + +import com.github.nizshee.client.Client; +import com.github.nizshee.server.MethodWrapper; +import com.github.nizshee.server.Server; +import com.github.nizshee.server.Handler; +import com.github.nizshee.shared.Method; +import org.junit.Test; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.Assert.*; + +@SuppressWarnings("all") +public class ClientServerTest { + + private final static int code = 0; + private final static AtomicBoolean trigger = new AtomicBoolean(false); + public static final Method method = new Method() { + @Override + public String execute(String s) { + trigger.set(true); + return s + s; + } + + @Override + public void writeRequest(DataOutputStream dos, String s) throws IOException { + dos.writeUTF(s); + } + + @Override + public String readRequest(DataInputStream dis) throws IOException { + return dis.readUTF(); + } + + @Override + public void writeResponse(DataOutputStream dos, String s) throws IOException { + dos.writeUTF(s); + } + + @Override + public String readResult(DataInputStream dis) throws IOException { + return dis.readUTF(); + } + }; + private static final Map handlers; + static { + handlers = new HashMap<>(); + handlers.put(code, new MethodWrapper(method)); + } + + @Test + public void clientServerTest() throws Exception { + Server server = new Server(InetAddress.getByName("localhost"), 8080); + server.start(handlers); + Thread.sleep(500); + Client client = new Client("localhost", 8080); + Socket socket = new Socket("localhost", 8080); + String result = Method.getValue(socket, code, method, "abcd"); + assertEquals("abcdabcd", result); + socket.close(); + server.stop(); + } +} diff --git a/src/test/java/com/github/nizshee/shared/ListMethodTest.java b/src/test/java/com/github/nizshee/shared/ListMethodTest.java new file mode 100644 index 0000000..452866e --- /dev/null +++ b/src/test/java/com/github/nizshee/shared/ListMethodTest.java @@ -0,0 +1,64 @@ +package com.github.nizshee.shared; + +import org.junit.Test; + +import java.io.*; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import static org.junit.Assert.*; + +public class ListMethodTest { + + private final static ListMethod method = new ListMethod(); + + @Test + public void applyTest() throws Exception { + + @SuppressWarnings("ConstantConditions") + List result = method.execute(getClass().getClassLoader().getResource("test/").getPath()); + assertEquals(Arrays.asList( + new RemoteFile("a", false), + new RemoteFile("b", false), + new RemoteFile("c", true) + ), result); + + } + + @Test + public void valueTest() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + + method.writeRequest(dos, "testString1"); + method.writeRequest(dos, "testString2"); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + DataInputStream dis = new DataInputStream(bis); + + assertEquals("testString1", method.readRequest(dis)); + assertEquals("testString2", method.readRequest(dis)); + } + + @Test + public void resultTest() throws Exception { + List list1 = new LinkedList<>(); + List list2 = Arrays.asList( + new RemoteFile("1", true), + new RemoteFile("2", false), + new RemoteFile("3", true) + ); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + + method.writeResponse(dos, list1); + method.writeResponse(dos, list2); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + DataInputStream dis = new DataInputStream(bis); + + assertEquals(list1, method.readResult(dis)); + assertEquals(list2, method.readResult(dis)); + } +} diff --git a/src/test/resources/test/a b/src/test/resources/test/a new file mode 100644 index 0000000..d800886 --- /dev/null +++ b/src/test/resources/test/a @@ -0,0 +1 @@ +123 \ No newline at end of file diff --git a/src/test/resources/test/b b/src/test/resources/test/b new file mode 100644 index 0000000..bd41cba --- /dev/null +++ b/src/test/resources/test/b @@ -0,0 +1 @@ +12345 \ No newline at end of file diff --git a/src/test/resources/test/c/c b/src/test/resources/test/c/c new file mode 100644 index 0000000..e69de29