This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Commit 822cc3b1 authored by Michael Breu's avatar Michael Breu
Browse files

Redefining ExerciseIds

parent 47d313ec
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src">
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
......
eclipse.preferences.version=1
encoding//src/main=UTF-8
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
encoding/src=UTF-8
......@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.codeability.sharing</groupId>
<artifactId>SharingPluginPlatformAPI</artifactId>
<version>0.2.1</version>
<version>0.3.0</version>
<name>SharingPluginPlatformAPI</name>
<description>CodeAbility Sharing Plugin Platform API</description>
<packaging>jar</packaging>
......@@ -35,11 +35,21 @@
<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-jaxb-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.10.4</version>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<repository>
......@@ -51,7 +61,7 @@
</distributionManagement>
<build>
<defaultGoal>package</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
......
......@@ -8,4 +8,5 @@ module sharingPluginPlatform {
exports org.codeability.sharing.plugins.api.search;
requires com.fasterxml.jackson.annotation;
requires com.fasterxml.jackson.databind;
requires org.apache.commons.lang3;
}
\ No newline at end of file
......@@ -5,6 +5,8 @@ import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* represents an exercise id.
* Also provides some utility methods to parse and unparse exercise Ids
......@@ -15,16 +17,23 @@ public class ExerciseId {
private String projectId; // should be numeric
private String path; // a path with slashes (root slash omitted)
private String gitUrl; // optional: a url to a git repository
public ExerciseId() {
}
public ExerciseId(String projectId, String path) {
this(projectId, path, null);
}
public ExerciseId(String projectId, String path, String gitUrl) {
super();
this.projectId = projectId;
this.path = path;
this.gitUrl = gitUrl;
}
/**
* @return the projectId
......@@ -40,6 +49,10 @@ public class ExerciseId {
return path;
}
public String getGitUrl() {
return gitUrl;
}
/**
* adds an extra path to existing path
* @param path
......@@ -56,14 +69,21 @@ public class ExerciseId {
return path + "/" + extraPath;
}
protected final static Pattern ExerciseIdPattern = Pattern.compile("(\\d+)(:(.*))?");
// protected final static Pattern ExerciseIdPattern = Pattern.compile("^(http[^[]+)?(\\[(\\d*)\\])?(.+)?$");
protected final static Pattern ExerciseIdPattern = Pattern.compile("^(http[^\\[]+)?(\\[(\\d*)\\])?(.+)?$");
protected final static Pattern ExerciseIdPatternOld = Pattern.compile("(\\d+)(:(.*))?");
public String toString() {
if(path==null)
return projectId;
if (path.equals(""))
return projectId;
return projectId + ":" + path;
if(!StringUtils.isEmpty(gitUrl)) {
if(StringUtils.isEmpty(path))
return gitUrl;
else
return gitUrl + "[]" + path;
}
if(StringUtils.isEmpty(path))
return "["+projectId + "]";
return "["+projectId + "]" + path;
}
/**
......@@ -74,6 +94,35 @@ public class ExerciseId {
*/
public static ExerciseId fromString(String externalRepresentation) throws ParseException {
final Matcher ma = ExerciseIdPattern.matcher(externalRepresentation);
if(!ma.matches()) {
return fromStringOld(externalRepresentation);
}
final MatchResult matchResult = ma.toMatchResult();
String gitUrl = matchResult.group(1);
String projectId = matchResult.group(3);
if(projectId == null) {
return fromStringOld(externalRepresentation);
}
if("".equals(projectId)) {
projectId = null;
}
String path = matchResult.group(4);
while (path!=null && path.startsWith("/")) {
path = path.substring(1);
}
return new ExerciseId(projectId, path, gitUrl);
}
/**
* parses the external representation of an ExerciseId (old format without brackets).
* @param externalRepresentation
* @return
* @throws ParseException
*/
public static ExerciseId fromStringOld(String externalRepresentation) throws ParseException {
final Matcher ma = ExerciseIdPatternOld.matcher(externalRepresentation);
if(!ma.matches()) throw new ParseException("Cannot parse " + externalRepresentation, 0);
final MatchResult matchResult = ma.toMatchResult();
......@@ -85,8 +134,6 @@ public class ExerciseId {
}
return new ExerciseId(projectId, path);
}
}
package org.codeability.sharing.plugins.api.search.util;
import java.text.ParseException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class ExerciseIdTests {
@Test
void testVariousStringRepresentations() throws ParseException {
ExerciseId id1 = ExerciseId.fromString("123");
Assertions.assertNull(id1.getGitUrl());
Assertions.assertEquals("123", id1.getProjectId());
Assertions.assertNull(id1.getPath());
Assertions.assertEquals("[123]", id1.toString());
ExerciseId id2 = ExerciseId.fromString("[124]");
Assertions.assertNull(id2.getGitUrl());
Assertions.assertEquals("124", id2.getProjectId());
Assertions.assertNull(id2.getPath());
Assertions.assertEquals("[124]", id2.toString());
ExerciseId id3 = ExerciseId.fromString("[124]subDir/meta.yml");
Assertions.assertNull(id3.getGitUrl());
Assertions.assertEquals("124", id3.getProjectId());
Assertions.assertEquals("subDir/meta.yml", id3.getPath());
Assertions.assertEquals("[124]subDir/meta.yml", id3.toString());
ExerciseId id4 = ExerciseId.fromString("[124]/subDir2/meta.yml");
Assertions.assertNull(id4.getGitUrl());
Assertions.assertEquals("124", id4.getProjectId());
Assertions.assertEquals("subDir2/meta.yml", id4.getPath());
Assertions.assertEquals("[124]subDir2/meta.yml", id4.toString());
ExerciseId id5 = ExerciseId.fromString("https://sharing.codeability-austria.uibk.ac.at/sharing/health-check-tests/collection_tests/collectionstests1[126]/subDir2/meta.yml");
Assertions.assertEquals("https://sharing.codeability-austria.uibk.ac.at/sharing/health-check-tests/collection_tests/collectionstests1", id5.getGitUrl());
Assertions.assertEquals("126", id5.getProjectId());
Assertions.assertEquals("subDir2/meta.yml", id5.getPath());
ExerciseId id6 = ExerciseId.fromString("https://sharing.codeability-austria.uibk.ac.at/sharing/health-check-tests/collection_tests/collectionstests1[]/subDir2/meta.yml");
Assertions.assertEquals("https://sharing.codeability-austria.uibk.ac.at/sharing/health-check-tests/collection_tests/collectionstests1", id6.getGitUrl());
Assertions.assertNull(id6.getProjectId());
Assertions.assertEquals("subDir2/meta.yml", id6.getPath());
Assertions.assertEquals("https://sharing.codeability-austria.uibk.ac.at/sharing/health-check-tests/collection_tests/collectionstests1[]subDir2/meta.yml", id6.toString());
}
@ParameterizedTest
@ValueSource(strings = { "[3]", "[3]path", "[3]path1/path2" })
public void parseVariousIds(String externalRepresentation) throws ParseException {
Assertions.assertEquals(externalRepresentation, ExerciseId.fromString(externalRepresentation).toString());
}
@Test
public void parseIdsWithTrailingSlashes() throws ParseException {
Assertions.assertEquals("[30]abc", ExerciseId.fromString("30:/abc").toString());
Assertions.assertEquals("[30]abc", ExerciseId.fromString("30://abc").toString());
Assertions.assertEquals("[30]", ExerciseId.fromString("30://").toString());
Assertions.assertEquals("[30]abc/def", ExerciseId.fromString("30:/abc/def").toString());
}
@ParameterizedTest
@ValueSource(strings = { "=", ":path", "3x2", "3x2:/asd", "asdf=asd:30" })
public void parseCorruptIds(String corruptIds) throws ParseException {
Assertions.assertThrows(ParseException.class, () -> ExerciseId.fromString(corruptIds));
}
}
......@@ -6,3 +6,5 @@
/maven-javadoc-plugin-stale-data.txt
/classes/
/SharingPluginPlatformAPI-0.0.2-javadoc.jar
/test-classes/
/surefire-reports/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment