Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Green2013-07-23 19:54:38 -0400
committerDavid Green2013-10-23 17:48:59 -0400
commit9c7ab4a6357cdacf9491a74f0d47772e2be63778 (patch)
treec845e2df044496573d8238ee3d63e3fe72073dc8
parentd0f959c177635edd9c5c4805c7b3d4ace3b400f7 (diff)
downloadorg.eclipse.mylyn.tasks-9c7ab4a6357cdacf9491a74f0d47772e2be63778.tar.gz
org.eclipse.mylyn.tasks-9c7ab4a6357cdacf9491a74f0d47772e2be63778.tar.xz
org.eclipse.mylyn.tasks-9c7ab4a6357cdacf9491a74f0d47772e2be63778.zip
413584: provide API for encoding/decoding task id
added TaskIdEncoder with tests Change-Id: I7e5628634c25df967770fc3f6515060713fc2e94 Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=413584
-rw-r--r--org.eclipse.mylyn.tasks.core.tests/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoderTest.java90
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoder.java51
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/package-info.java16
3 files changed, 157 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.tasks.core.tests/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoderTest.java b/org.eclipse.mylyn.tasks.core.tests/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoderTest.java
new file mode 100644
index 000000000..5ebbecf91
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core.tests/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoderTest.java
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tasks.core.spi;
+
+import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.UUID;
+
+import org.eclipse.core.runtime.AssertionFailedException;
+import org.eclipse.mylyn.internal.tasks.core.RepositoryTaskHandleUtil;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class TaskIdEncoderTest {
+ @Rule
+ public final ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void testEncodeNull() {
+ thrown.expect(AssertionFailedException.class);
+ TaskIdEncoder.encode(null);
+ }
+
+ @Test
+ public void testDecodeNull() {
+ thrown.expect(AssertionFailedException.class);
+ TaskIdEncoder.decode(null);
+ }
+
+ @Test
+ public void testEncodeSimple() {
+ assertEquals("1234 abc", TaskIdEncoder.encode("1234 abc"));
+ }
+
+ @Test
+ public void testEncodeComplex() {
+ assertEquals("1234%25%2Dabc @$", TaskIdEncoder.encode("1234%-abc @$"));
+ }
+
+ @Test
+ public void testDecodeSimple() {
+ assertEquals("1234 abc", TaskIdEncoder.decode("1234 abc"));
+ }
+
+ @Test
+ public void testDecodeComplex() {
+ assertEquals("1234%-abc @$", TaskIdEncoder.decode("1234%25%2Dabc @$"));
+ }
+
+ @Test
+ public void testUuidRoundTrip() {
+ assertRoundTrip(UUID.randomUUID().toString());
+ }
+
+ @Test
+ public void testHyphenatedRoundTrip() {
+ assertRoundTrip("TEST-1234");
+ }
+
+ @Test
+ public void testJsonRoundTrip() {
+ assertRoundTrip("{\"one\":true,\"two\":\"three\"}");
+ }
+
+ @Test
+ public void testJsonReadable() {
+ String json = "{\"one\":true,\"two\":\"three\"}";
+ assertEquals(json, TaskIdEncoder.encode(json));
+ }
+
+ private void assertRoundTrip(String original) {
+ String encoded = TaskIdEncoder.encode(original);
+
+ String handle = RepositoryTaskHandleUtil.getHandle("https://example.com", encoded);
+
+ assertTrue(handle.endsWith(encoded));
+ assertEquals(original, TaskIdEncoder.decode(encoded));
+ }
+}
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoder.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoder.java
new file mode 100644
index 000000000..14042a56f
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/TaskIdEncoder.java
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tasks.core.spi;
+
+import static org.eclipse.core.runtime.Assert.isNotNull;
+
+/**
+ * An encoder that can encode/decode task ids. Task ids must not contain specific characters when used in the Mylyn
+ * framework. This class can be used to safely encode a task id to the Mylyn-acceptable form, and decode it back to the
+ * repository-specific form.
+ *
+ * @since 3.10
+ */
+public class TaskIdEncoder {
+ /**
+ * Encodes the given {@code repositoryId} to a form that is acceptable to Mylyn.
+ *
+ * @param repositoryId
+ * the repository id to encode
+ * @return the encoded id
+ * @see #decode(String)
+ */
+ public static String encode(String repositoryId) {
+ isNotNull(repositoryId);
+ return repositoryId.replaceAll("%", "%25").replaceAll( //$NON-NLS-1$ //$NON-NLS-2$
+ org.eclipse.mylyn.internal.tasks.core.RepositoryTaskHandleUtil.HANDLE_DELIM, "%2D"); //$NON-NLS-1$
+ }
+
+ /**
+ * Decodes the given {@code encodedForm} to it's original format.
+ *
+ * @param encodedForm
+ * the decoded form of the string, previously obtained from {@link #encode(String)}
+ * @return the decoded string
+ */
+ public static String decode(String encodedForm) {
+ isNotNull(encodedForm);
+ return encodedForm.replaceAll(
+ "%2D", org.eclipse.mylyn.internal.tasks.core.RepositoryTaskHandleUtil.HANDLE_DELIM) //$NON-NLS-1$
+ .replaceAll("%25", "%"); //$NON-NLS-1$//$NON-NLS-2$
+ }
+}
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/package-info.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/package-info.java
new file mode 100644
index 000000000..a216aa8c5
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/spi/package-info.java
@@ -0,0 +1,16 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+/**
+ * Provides API for connector implementors.
+ */
+package org.eclipse.mylyn.tasks.core.spi;
+

Back to the top