Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 Andreas Höhmann. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Andreas Höhmann - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.mylyn.ide.tests; |
12 |
|
13 |
import java.util.Iterator; |
14 |
|
15 |
import junit.framework.TestCase; |
16 |
|
17 |
import org.apache.commons.lang.builder.ToStringBuilder; |
18 |
import org.apache.commons.lang.exception.ExceptionUtils; |
19 |
import org.eclipse.jdt.internal.corext.template.java.JavaContextType; |
20 |
import org.eclipse.jdt.internal.corext.template.java.JavaDocContextType; |
21 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
22 |
import org.eclipse.jface.text.BadLocationException; |
23 |
import org.eclipse.jface.text.Document; |
24 |
import org.eclipse.jface.text.Position; |
25 |
import org.eclipse.jface.text.templates.ContextTypeRegistry; |
26 |
import org.eclipse.jface.text.templates.DocumentTemplateContext; |
27 |
import org.eclipse.jface.text.templates.Template; |
28 |
import org.eclipse.jface.text.templates.TemplateBuffer; |
29 |
import org.eclipse.jface.text.templates.TemplateContextType; |
30 |
import org.eclipse.jface.text.templates.TemplateException; |
31 |
import org.eclipse.jface.text.templates.TemplateTranslator; |
32 |
import org.eclipse.jface.text.templates.TemplateVariable; |
33 |
import org.eclipse.jface.text.templates.TemplateVariableResolver; |
34 |
import org.eclipse.mylyn.internal.tasks.core.LocalTask; |
35 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
36 |
import org.eclipse.mylyn.tasks.core.ITask; |
37 |
import org.eclipse.mylyn.tasks.tests.connector.MockTask; |
38 |
|
39 |
/** |
40 |
* @author andreas hoehmann |
41 |
* @since 3.4 |
42 |
*/ |
43 |
public class TaskTemplateResolverTest extends TestCase { |
44 |
|
45 |
/** |
46 |
* Test with no active task. The resolver should not be able to resolve the mylyn template-variable. |
47 |
*/ |
48 |
public void testNoTaskActive() { |
49 |
canHandleTemplateResolver("${activeTaskKey}", "activeTaskKey"); |
50 |
canHandleTemplateResolver("${activeTaskPrefix}", "activeTaskPrefix"); |
51 |
} |
52 |
|
53 |
/** |
54 |
* Test with active task. The resolver must resolve the mylyn template-variable to the expected values. |
55 |
*/ |
56 |
public void testActiveLocalTask() { |
57 |
ITask task = new LocalTask("12345", "Test Task"); |
58 |
TasksUiPlugin.getTaskActivityManager().activateTask(task); |
59 |
canHandleTemplateResolver("${activeTaskKey}", "12345"); |
60 |
canHandleTemplateResolver("${activeTaskPrefix}", "task"); |
61 |
canHandleTemplateResolver("${activeTaskPrefix} ${activeTaskKey}", "task 12345"); |
62 |
canHandleTemplateResolver("${activeTaskPrefix} #${activeTaskKey}", "task #12345"); |
63 |
} |
64 |
|
65 |
/** |
66 |
* Test with active task. Special check if task-key contains a "-". |
67 |
*/ |
68 |
public void testActiveTaskJira() { |
69 |
ITask task = new MockTask("http://foo.bar", "12345"); |
70 |
task.setTaskKey("DEMO-2"); |
71 |
TasksUiPlugin.getTaskActivityManager().activateTask(task); |
72 |
canHandleTemplateResolver("${activeTaskKey}", "DEMO-2"); |
73 |
canHandleTemplateResolver("${activeTaskPrefix} ${activeTaskKey}", "task DEMO-2"); |
74 |
} |
75 |
|
76 |
/** |
77 |
* Test with active task. Check if fallback from task-key to task-id is working. |
78 |
*/ |
79 |
public void testActiveTaskIdOrKey() { |
80 |
ITask task = new MockTask("http://foo.bar", "12345"); |
81 |
TasksUiPlugin.getTaskActivityManager().activateTask(task); |
82 |
canHandleTemplateResolver("${activeTaskKey}", "12345"); |
83 |
canHandleTemplateResolver("${activeTaskPrefix}", "task"); |
84 |
// from now the task have a key ... the resolve will prefer this key |
85 |
task.setTaskKey("foobar"); |
86 |
canHandleTemplateResolver("${activeTaskKey}", "foobar"); |
87 |
} |
88 |
|
89 |
private void canHandleTemplateResolver(String templateContent, String expectedResolvedTemplate) { |
90 |
// check java template context |
91 |
canHandleTemplateResolver(JavaContextType.ID_ALL, templateContent, expectedResolvedTemplate); |
92 |
// check javadoc template context |
93 |
canHandleTemplateResolver(JavaDocContextType.ID, templateContent, expectedResolvedTemplate); |
94 |
} |
95 |
|
96 |
private void canHandleTemplateResolver(final String contextType, final String templateContent, |
97 |
final String expectedResolvedTemplate) { |
98 |
final ContextTypeRegistry registry = JavaPlugin.getDefault().getTemplateContextRegistry(); |
99 |
final TemplateContextType context = registry.getContextType(contextType); |
100 |
final Template template = new Template("name", "description", contextType, templateContent, false); |
101 |
final TemplateTranslator translator = new TemplateTranslator(); |
102 |
TemplateBuffer buffer = null; |
103 |
try { |
104 |
buffer = translator.translate(template); |
105 |
} catch (TemplateException e) { |
106 |
fail(String.format("Can't translate template '%s' : %s", ToStringBuilder.reflectionToString(template), |
107 |
ExceptionUtils.getMessage(e))); |
108 |
} |
109 |
final TemplateVariable[] variables = buffer.getVariables(); |
110 |
for (final TemplateVariable variable : variables) { |
111 |
assertTrue(String.format("No resolver found for variable '%s' in template '%s'", |
112 |
ToStringBuilder.reflectionToString(variable), ToStringBuilder.reflectionToString(template)), |
113 |
canHandleVariable(context, variable)); |
114 |
} |
115 |
assertEquals(expectedResolvedTemplate, getResolveTemplate(context, template)); |
116 |
} |
117 |
|
118 |
private boolean canHandleVariable(final TemplateContextType context, final TemplateVariable variable) { |
119 |
for (final Iterator iterator = context.resolvers(); iterator.hasNext();) { |
120 |
final TemplateVariableResolver resolver = (TemplateVariableResolver) iterator.next(); |
121 |
if (variable.getType().equals(resolver.getType())) { |
122 |
return true; |
123 |
} |
124 |
} |
125 |
return false; |
126 |
} |
127 |
|
128 |
private String getResolveTemplate(final TemplateContextType context, final Template template) { |
129 |
final DocumentTemplateContext templateContext = new DocumentTemplateContext(context, new Document(), |
130 |
new Position(0)); |
131 |
TemplateBuffer templateBuffer = null; |
132 |
try { |
133 |
templateBuffer = templateContext.evaluate(template); |
134 |
} catch (BadLocationException e) { |
135 |
fail(String.format("Can't evaluate template '%s' : %s", ToStringBuilder.reflectionToString(template), |
136 |
ExceptionUtils.getMessage(e))); |
137 |
} catch (TemplateException e) { |
138 |
fail(String.format("Can't evaluate template '%s' : %s", ToStringBuilder.reflectionToString(template), |
139 |
ExceptionUtils.getMessage(e))); |
140 |
} |
141 |
return templateBuffer.getString(); |
142 |
} |
143 |
} |