From b657044ef44f862147e11c624ff75aa16d307934 Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Sat, 9 Aug 2014 23:08:18 +0200 Subject: [PATCH 1/3] updated .gitignore and increased version number --- .gitignore | 9 +++++++++ gradle.properties | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b21aa94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +bin/ +build/ +log/ +.idea/ +.project +.settings/ +.gradle/ +.classpath +.directory diff --git a/gradle.properties b/gradle.properties index 16890e8..8c7aca9 100755 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ theGroup = com.team-grit -theName = GRIT -theVersion = v1.0- +theName = GRadeIT +theVersion = v1.1- theSourceCompatibility = 1.7 From c705137bb136803dec7f7d045474c70a8c2f60e1 Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Sun, 10 Aug 2014 16:18:14 +0200 Subject: [PATCH 2/3] removed temp files --- config/state.xml | 22 --- .../exercise-2/tests/TestIFraction.java | 136 ----------------- .../tests/fraction/TestIFraction.java | 138 ------------------ 3 files changed, 296 deletions(-) delete mode 100755 config/state.xml delete mode 100755 wdir/course-0/exercise-2/tests/TestIFraction.java delete mode 100755 wdir/course-0/exercise-3/tests/fraction/TestIFraction.java diff --git a/config/state.xml b/config/state.xml deleted file mode 100755 index f27e114..0000000 --- a/config/state.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/wdir/course-0/exercise-2/tests/TestIFraction.java b/wdir/course-0/exercise-2/tests/TestIFraction.java deleted file mode 100755 index b7a0563..0000000 --- a/wdir/course-0/exercise-2/tests/TestIFraction.java +++ /dev/null @@ -1,136 +0,0 @@ -package fraction; - -import static org.junit.Assert.*; - -import org.junit.Test; - -public class TestIFraction { - - /** EPSILON for rounding errors. */ - private final static double EPSILON = 0.0001; - - @Test - public final void testConstructors() { - IFraction f = new Fraction(); - assertTrue("Denominator must be greater 0", f.getDenominator() > 0); - assertEquals("Value must be 0", 0, f.getNumerator(), EPSILON); - f = new Fraction(5); - assertTrue("Denominator must be greater 0", f.getDenominator() > 0); - assertEquals("gcd must be 1", 5, f.getNumerator(), EPSILON); - assertEquals("gcd must be 1", 1, f.getDenominator(), EPSILON); - f = new Fraction(2, 3); - assertTrue("Denominator must be greater 0", f.getDenominator() > 0); - assertEquals("gcd must be 1", 2, f.getNumerator(), EPSILON); - assertEquals("gcd must be 1", 3, f.getDenominator(), EPSILON); - } - - @Test - public final void testGetNumerator() { - IFraction frac = new Fraction(5); - assertEquals(5, frac.getNumerator()); - } - - @Test - public final void testGetDenominator() { - IFraction frac = new Fraction(1, 5); - assertEquals(5, frac.getDenominator()); - } - - @Test - public final void testAdd() { - IFraction f1 = new Fraction(1, 2); - IFraction f2 = new Fraction(1, 3); - IFraction sum = f1.add(f2); - assertEquals("Numerator not correct", 5, sum.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 6, sum.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testSub() { - IFraction f1 = new Fraction(1, 2); - IFraction f2 = new Fraction(1, 3); - IFraction diff = f1.sub(f2); - assertEquals("Numerator not correct", 1, diff.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 6, diff.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testMult() { - IFraction f1 = new Fraction(2, 3); - IFraction f2 = new Fraction(1, 5); - IFraction prod = f1.mult(f2); - assertEquals("Numerator not correct", 2, prod.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 15, prod.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 5, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testDiv() { - IFraction f1 = new Fraction(2, 3); - IFraction f2 = new Fraction(1, 5); - IFraction div = f1.div(f2); - assertEquals("Numerator not correct", 10, div.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 3, div.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 5, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testNegated() { - IFraction f = new Fraction(1, 5); - IFraction fNeg = f.negated(); - IFraction fDoubleNeg = fNeg.negated(); - - - assertEquals("f + (-f) should be zero", 0, f.add(fNeg).getNumerator(), - EPSILON); - assertEquals("Double negation failed", f.getNumerator(), - fDoubleNeg.getNumerator(), EPSILON); - assertEquals("Double negation failed", f.getDenominator(), - fDoubleNeg.getDenominator(), EPSILON); - assertEquals("Fraction should not change", 1, f.getNumerator(), EPSILON); - assertEquals("Fraction should not change", 5, f.getDenominator(), - EPSILON); - } - - @Test - public final void testAsDouble() { - Fraction f = new Fraction(1, 2); - assertEquals("Wrong value as double", 0.5, f.asDouble(), EPSILON); - f = new Fraction(1, 3); - assertEquals("Wrong value as double", 0.33333, f.asDouble(), EPSILON); - } -} diff --git a/wdir/course-0/exercise-3/tests/fraction/TestIFraction.java b/wdir/course-0/exercise-3/tests/fraction/TestIFraction.java deleted file mode 100755 index 2cf5100..0000000 --- a/wdir/course-0/exercise-3/tests/fraction/TestIFraction.java +++ /dev/null @@ -1,138 +0,0 @@ -// package fraction; - -import static org.junit.Assert.*; - -import org.junit.Test; -import fraction.Fraction; -import fraction.IFraction; - -public class TestIFraction { - - /** EPSILON for rounding errors. */ - private final static double EPSILON = 0.0001; - - @Test - public final void testConstructors() { - IFraction f = new Fraction(); - assertTrue("Denominator must be greater 0", f.getDenominator() > 0); - assertEquals("Value must be 0", 0, f.getNumerator(), EPSILON); - f = new Fraction(5); - assertTrue("Denominator must be greater 0", f.getDenominator() > 0); - assertEquals("gcd must be 1", 5, f.getNumerator(), EPSILON); - assertEquals("gcd must be 1", 1, f.getDenominator(), EPSILON); - f = new Fraction(2, 3); - assertTrue("Denominator must be greater 0", f.getDenominator() > 0); - assertEquals("gcd must be 1", 2, f.getNumerator(), EPSILON); - assertEquals("gcd must be 1", 3, f.getDenominator(), EPSILON); - } - - @Test - public final void testGetNumerator() { - IFraction frac = new Fraction(5); - assertEquals(5, frac.getNumerator()); - } - - @Test - public final void testGetDenominator() { - IFraction frac = new Fraction(1, 5); - assertEquals(5, frac.getDenominator()); - } - - @Test - public final void testAdd() { - IFraction f1 = new Fraction(1, 2); - IFraction f2 = new Fraction(1, 3); - IFraction sum = f1.add(f2); - assertEquals("Numerator not correct", 5, sum.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 6, sum.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testSub() { - IFraction f1 = new Fraction(1, 2); - IFraction f2 = new Fraction(1, 3); - IFraction diff = f1.sub(f2); - assertEquals("Numerator not correct", 1, diff.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 6, diff.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testMult() { - IFraction f1 = new Fraction(2, 3); - IFraction f2 = new Fraction(1, 5); - IFraction prod = f1.mult(f2); - assertEquals("Numerator not correct", 2, prod.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 15, prod.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 5, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testDiv() { - IFraction f1 = new Fraction(2, 3); - IFraction f2 = new Fraction(1, 5); - IFraction div = f1.div(f2); - assertEquals("Numerator not correct", 10, div.getNumerator(), EPSILON); - assertEquals("Denominator not correct", 3, div.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 2, f1.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 3, f1.getDenominator(), - EPSILON); - assertEquals("Fraction should not change", 1, f2.getNumerator(), - EPSILON); - assertEquals("Fraction should not change", 5, f2.getDenominator(), - EPSILON); - } - - @Test - public final void testNegated() { - IFraction f = new Fraction(1, 5); - IFraction fNeg = f.negated(); - IFraction fDoubleNeg = fNeg.negated(); - - - assertEquals("f + (-f) should be zero", 0, f.add(fNeg).getNumerator(), - EPSILON); - assertEquals("Double negation failed", f.getNumerator(), - fDoubleNeg.getNumerator(), EPSILON); - assertEquals("Double negation failed", f.getDenominator(), - fDoubleNeg.getDenominator(), EPSILON); - assertEquals("Fraction should not change", 1, f.getNumerator(), EPSILON); - assertEquals("Fraction should not change", 5, f.getDenominator(), - EPSILON); - } - - @Test - public final void testAsDouble() { - Fraction f = new Fraction(1, 2); - assertEquals("Wrong value as double", 0.5, f.asDouble(), EPSILON); - f = new Fraction(1, 3); - assertEquals("Wrong value as double", 0.33333, f.asDouble(), EPSILON); - } -} From 06c4aad3f7a8acb3289e92a01ae5992c49539137 Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Sun, 10 Aug 2014 22:57:01 +0200 Subject: [PATCH 3/3] enhanced upload of java testfiles, so that they are placed into the correct subfolder coresponding to their qualified name. --- .gitignore | 1 + src/webserver/EntityHandler.java | 119 ++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index b21aa94..3eb2d8c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ bin/ build/ log/ +config/ .idea/ .project .settings/ diff --git a/src/webserver/EntityHandler.java b/src/webserver/EntityHandler.java index 9feef10..c9b0597 100755 --- a/src/webserver/EntityHandler.java +++ b/src/webserver/EntityHandler.java @@ -5,6 +5,7 @@ import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -16,6 +17,10 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.LineIterator; +import org.apache.commons.lang3.StringUtils; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; @@ -41,7 +46,7 @@ public abstract class EntityHandler extends AbstractHandler { * A GSON instance for JSON parsing. */ protected static final Gson GSON = new GsonBuilder() - .excludeFieldsWithoutExposeAnnotation().create(); + .excludeFieldsWithoutExposeAnnotation().create(); /** * The grit-wide logger. @@ -75,15 +80,15 @@ public EntityHandler() { /* * (non-Javadoc) - * + * * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, * org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ @Override - public void handle(String target, Request baseRequest, - HttpServletRequest request, HttpServletResponse response) - throws IOException { + public void handle( + String target, Request baseRequest, HttpServletRequest request, + HttpServletResponse response) throws IOException { /* * If this request contains a submitted file, use the multi-part @@ -109,20 +114,20 @@ public void handle(String target, Request baseRequest, */ try { response.getWriter().println(doAction(target, request)); - } catch (BadRequestException e) { - String message = e.getMessage(); + } catch (final BadRequestException e) { + final String message = e.getMessage(); LOGGER.severe(message); response.setContentType("text/plain;charset=utf-8"); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getWriter().println(message); - } catch (InternalActionErrorException e) { - String message = e.getMessage(); + } catch (final InternalActionErrorException e) { + final String message = e.getMessage(); LOGGER.severe(message); response.setContentType("text/plain;charset=utf-8"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().println(message); - } catch (Exception e) { - String message = + } catch (final Exception e) { + final String message = "An unexpected error occured:\n" + e.getClass().getSimpleName() + ":\n" + e.getMessage(); @@ -154,9 +159,9 @@ public void handle(String target, Request baseRequest, * @throws InternalActionErrorException * if something else goes wrong (e.g. could not write to file) */ - protected abstract String doAction(String target, - HttpServletRequest request) throws BadRequestException, - InternalActionErrorException; + protected abstract String doAction( + String target, HttpServletRequest request) + throws BadRequestException, InternalActionErrorException; // --------------------- PARAMETER HELPERS --------------------- @@ -178,7 +183,7 @@ protected String parseName(String parameter, String nameType) throw new BadRequestException("The passed " + nameType + " is null."); } - String name = parameter.trim(); + final String name = parameter.trim(); if (!name.matches(".+")) { throw new BadRequestException("The passed " + nameType + " is invalid."); @@ -207,7 +212,7 @@ protected LanguageType parseLanguageType(String parameter) } try { return LanguageType.valueOf(parameter); - } catch (NullPointerException e) { + } catch (final NullPointerException e) { throw new BadRequestException( "The passed language type does not exist."); } @@ -234,7 +239,7 @@ protected ConnectionType parseConnectionType(String parameter) } try { return ConnectionType.valueOf(parameter); - } catch (NullPointerException e) { + } catch (final NullPointerException e) { throw new BadRequestException( "The passed connection type does not exist."); } @@ -255,7 +260,7 @@ protected String parseLocation(String parameter) if (parameter == null) { throw new BadRequestException("The passed location is null."); } - String location = parameter.trim(); + final String location = parameter.trim(); if (!location.matches(".+")) { throw new BadRequestException("The passed location is invalid."); } @@ -279,7 +284,7 @@ protected Date parseDateTime(String parameter) throws BadRequestException { } try { return DATE_TIME_FORMAT.parse(parameter); - } catch (ParseException e) { + } catch (final ParseException e) { throw new BadRequestException(e.getMessage() + "\nA date has to be formatted like this: \"" + DATE_TIME_FORMAT.toPattern() + "\"."); @@ -307,8 +312,8 @@ protected long parseTimePeriod(String parameter) + "A time has to be formatted like this: \"" + TIME_FORMAT.toPattern() + "\"."); } - String[] time = parameter.split(":"); - long period = + final String[] time = parameter.split(":"); + final long period = ((Integer.parseInt(time[0]) * 60) + Integer.parseInt(time[1])) * 60 * 1000; return period; } @@ -399,15 +404,43 @@ protected int parseId(String parameter, String entityName) */ protected void writeSubmittedFile(Part part, Path outputDirectory) throws BadRequestException, InternalActionErrorException { - String submittedFileName = part.getSubmittedFileName(); + final String submittedFileName = part.getSubmittedFileName(); if ((submittedFileName == null) || ("".equals(submittedFileName))) { throw new BadRequestException("No file submitted!"); } try { - Path outFilePath = outputDirectory.resolve(submittedFileName); - InputStream testfileInputStream = part.getInputStream(); + // make sure the temp directory exists + final Path tempPath = Paths.get("wdir", "temp"); + Files.createDirectories(tempPath); + + // conpy the file to the temp directory + final InputStream testfileInputStream = part.getInputStream(); + final Path tempFile = tempPath.resolve(submittedFileName); + final String fileExtension = + FilenameUtils.getExtension(submittedFileName); + Files.copy(testfileInputStream, tempFile, + StandardCopyOption.REPLACE_EXISTING); + + final Path outFilePath; + + // java files have to be in the directory matching their + // qualified name + if ("[jJ][aA][vV][aA]".matches(fileExtension)) { + final String qualifiedName = + getQualifiedNameFromFile(tempFile); + String subDir = + StringUtils.replaceChars(qualifiedName, '.', '/'); + subDir = subDir + "." + fileExtension; + outFilePath = outputDirectory.resolve(subDir); + } else { + // not needed in other languages + outFilePath = outputDirectory.resolve(submittedFileName); + } + Files.createDirectories(outFilePath.getParent()); - Files.copy(testfileInputStream, outFilePath, + + // move the file to its final position + Files.move(tempFile, outFilePath, StandardCopyOption.REPLACE_EXISTING); } catch (InvalidPathException | IOException e) { throw new InternalActionErrorException( @@ -434,9 +467,43 @@ protected void optionalWriteSubmittedFile(Part part, Path outputDirectory) if (part == null) { return; } - String submittedFileName = part.getSubmittedFileName(); + final String submittedFileName = part.getSubmittedFileName(); if ((submittedFileName != null) && !("".equals(submittedFileName))) { writeSubmittedFile(part, outputDirectory); } } + + /** + * Iterates over a java source file, and identifies the fully qualified + * name of the class from the package declaration in the source. + * + * @param sourceFile + * a java source file + * @return the fully qualified name of the class + * @throws IOException + * if the source file can not be read from. + */ + private String getQualifiedNameFromFile(Path sourceFile) + throws IOException { + final String packageRegex = "package\\s[^,;]+;"; + LineIterator it; + String result = ""; + it = FileUtils.lineIterator(sourceFile.toFile(), "UTF-8"); + + // look for the line identifying the package + while (it.hasNext()) { + final String line = it.nextLine(); + if (line.matches(packageRegex)) { + result = line; + // strip not needed elements (the word package) + result = result.substring(8, result.length() - 1); + it.close(); + result = result + "."; + break; + } + } + it.close(); + // append the classname + return result + FilenameUtils.getBaseName(sourceFile.toString()); + } }