diff options
11 files changed, 438 insertions, 47 deletions
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java index 653bbbde8..762215f15 100644 --- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java +++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java @@ -55,17 +55,17 @@ public class GlobalNonPlatformURIEditorOpener extends GlobalURIEditorOpener { public static URI getPlatformURI(URI uri) { if (uri.isPlatform()) return uri; - + // HOWTO: find absolute path location in workspace (as platform URI) IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IFile[] files = root.findFilesForLocationURI(java.net.URI.create(uri.toString())); - if (files.length!=0) { - String pluri = files[0].toString(); - // the pluri starts with L/ which we have to omit for URI.createPlatformResourceURI - uri = URI.createPlatformResourceURI(pluri.substring(2), true).appendFragment(uri.fragment()); - return uri; + for (IFile file : files) { // which file to choose ? + if (!file.isAccessible()) // avoid closed or other bad files + continue; + + return URI.createPlatformResourceURI(file.getFullPath().toString(), true).appendFragment(uri.fragment()); } - + return null; } diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java index c9c9010ed..930129a7a 100644 --- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java +++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java @@ -15,6 +15,7 @@ package org.eclipse.etrice.ui.common.base.editor; import java.io.IOException; import java.util.Collections; import java.util.List; +import java.util.Map; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -59,6 +60,17 @@ public class CustomPersistencyBehavior extends DefaultPersistencyBehavior { } @Override + protected Map<Resource, Map<?, ?>> createSaveOptions() { + Map<Resource, Map<?, ?>> resMap = super.createSaveOptions(); + + @SuppressWarnings("unchecked") + Map<Object, Object> diagSaveOptions = (Map<Object, Object>) resMap.get(diagramBehavior.getDiagramTypeProvider().getDiagram().eResource()); + RelativeFileURIHandler.addToOptions(diagSaveOptions); + + return resMap; + } + + @Override public void saveDiagram(IProgressMonitor monitor) { boolean valid = validateResourcesBeforeSave(monitor); diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomUpdateBehavior.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomUpdateBehavior.java index 1502fb98f..4a398aee4 100644 --- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomUpdateBehavior.java +++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomUpdateBehavior.java @@ -68,6 +68,8 @@ public class CustomUpdateBehavior extends DefaultUpdateBehavior { resourceSet = new XtextResourceSet(); } + RelativeFileURIHandler.addToOptions(resourceSet.getLoadOptions()); + final IWorkspaceCommandStack workspaceCommandStack = new GFWorkspaceCommandStackImpl(new DefaultOperationHistory()); final TransactionalEditingDomainImpl editingDomain = new TransactionalEditingDomainImpl(new ComposedAdapterFactory( diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java index d2199e72e..8b02d6f61 100644 --- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java +++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java @@ -15,14 +15,13 @@ package org.eclipse.etrice.ui.common.base.editor; import java.util.Collection; import java.util.Map; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature.Setting; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.etrice.core.common.ui.linking.GlobalNonPlatformURIEditorOpener; import org.eclipse.etrice.core.fsm.fSM.ModelComponent; import org.eclipse.etrice.core.fsm.ui.FSMUiModule; import org.eclipse.graphiti.mm.pictograms.Diagram; @@ -85,8 +84,10 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU ResourceSet resourceSet = getEditingDomain().getResourceSet(); Map<EObject, Collection<Setting>> result = EcoreUtil.UnresolvedProxyCrossReferencer.find(resourceSet); - if (!result.isEmpty()) + if (!result.isEmpty()){ System.err.println("ERROR in diagram viewer: could not resolve all proxies!"); + //System.out.println(result); + } mte.setTarget(getEditingDomain()); } @@ -135,6 +136,10 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU @Override public void createPartControl(Composite parent) { super.createPartControl(parent); + if(!getDiagramBehavior().isAlive()){ + // something is wrong, avoid further exceptions + return; + } superClassListener = new SuperClassListener(this, textEditorClass); PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener(superClassListener); @@ -162,11 +167,11 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU ModelComponent editorSc = editor.getModelComponent(); if (editorSc!=null) { - URI editorResURI = toCurrentPlatformURI(editorSc.eResource().getURI()); + URI editorResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(editorSc.eResource().getURI()); ModelComponent base = getModelComponent(); while((base = base.getBase()) != null){ - URI baseResURI = toCurrentPlatformURI(base.eResource().getURI()); + URI baseResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(base.eResource().getURI()); if(editorResURI.equals(baseResURI)) if(editorSc.getComponentName().equals(base.getComponentName())) return true; @@ -192,16 +197,16 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU @Override public Boolean exec(XtextResource resource) throws Exception { - URI editorResURI = toCurrentPlatformURI(resource.getURI()); - URI thisScResURI = toCurrentPlatformURI(getModelComponent().eResource().getURI()); + URI editorResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(resource.getURI()); + URI thisScResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(getModelComponent().eResource().getURI()); // ignore if in same file (handled by graphiti) - if(thisScResURI.equals(editorResURI)) + if(thisScResURI == null || thisScResURI.equals(editorResURI)) return false; ModelComponent base = getModelComponent(); while((base = base.getBase()) != null){ - URI baseResURI = toCurrentPlatformURI(base.eResource().getURI()); + URI baseResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(base.eResource().getURI()); if(editorResURI.equals(baseResURI)) return true; } @@ -211,19 +216,6 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU }); } - private URI toCurrentPlatformURI(URI uri) { - if(uri.isPlatform()) - return uri; - else if(uri.isFile()){ - final IPath rootPath = ResourcesPlugin.getWorkspace().getRoot().getLocation(); - String rootString = rootPath.toFile().toString(); - String fileString = uri.toFileString(); - if(fileString.startsWith(rootString)) - return URI.createPlatformResourceURI(fileString.replace(rootString, ""), false); - } - return null; - } - protected abstract void handleMissingDiagramBo(Diagram diagram); protected abstract void superClassChanged(); protected abstract EObject getModel(); diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/RelativeFileURIHandler.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/RelativeFileURIHandler.java new file mode 100644 index 000000000..5f3381e48 --- /dev/null +++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/RelativeFileURIHandler.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright (c) 2011 protos software gmbh (http://www.protos.de). + * 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: + * Juergen Haug (initial contribution) + * + *******************************************************************************/ + +package org.eclipse.etrice.ui.common.base.editor; + +import java.util.Map; + +import org.apache.log4j.Logger; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.xmi.XMIResource; +import org.eclipse.emf.ecore.xmi.XMLResource.URIHandler; +import org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl; + +/** + * {@linkplain URIHandler} which deresolves to relative file uri and resolves to absolute file uri. <br> + * Used to persist portable model references. <br> + * <br> + * TODO serialize room includes path <=> deresolve in StandardModelLocator + */ +public class RelativeFileURIHandler extends URIHandlerImpl { + + private final static Logger LOG = Logger.getLogger(RelativeFileURIHandler.class); + + public static Map<Object, Object> addToOptions(Map<Object, Object> options) { + options.put(XMIResource.OPTION_URI_HANDLER, new RelativeFileURIHandler((URIHandler) options.get(XMIResource.OPTION_URI_HANDLER))); + return options; + } + + final protected URIHandler fallback; + + public RelativeFileURIHandler(URIHandler fallback) { + this.fallback = fallback; + } + + /** + * @return absolute file uri + */ + @Override + public URI resolve(URI uri) { + if (resolve && baseURI != null && uri.isFile() && uri.hasRelativePath()) { + URI baseFileURI = toFileURI(baseURI); + URI fileURI = toFileURI(uri); + + URI resolvedFileURI = fileURI.resolve(baseFileURI, true); + // System.out.println("resolve: " + fileURI + " -> " + baseFileURI + " = " + resolvedFileURI); + return resolvedFileURI; + } + + return (fallback != null) ? fallback.resolve(uri) : uri; + } + + /** + * @return shorter relative file path + */ + @Override + public URI deresolve(URI uri) { + if (resolve && baseURI != null) { + URI baseFileURI = toFileURI(baseURI); + URI fileURI = toFileURI(uri); + + URI relativeFileURI = fileURI.deresolve(baseFileURI, true, true, true); + if(relativeFileURI.isFile() && relativeFileURI.hasRelativePath()) { + // System.out.println("deresolve: " + fileURI + " -> " + baseFileURI + " = " + relativeFileURI); + return relativeFileURI; + } + } + + return (fallback != null) ? fallback.deresolve(uri) : uri; + } + + private static URI toFileURI(URI uri) { + if (uri.isFile()) { + return uri; + } else if (uri.isPlatform()) { + IPath path = null; + if(uri.segmentCount() == 2){ + path = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.lastSegment()).getLocation(); + } else if(uri.segmentCount() > 2){ + path = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(false))).getLocation(); + } + if(path != null) { + return URI.createFileURI(path.toOSString()).appendQuery(uri.query()).appendFragment(uri.fragment()); + } + } + + LOG.error("unhandled uri " + uri); + return uri; + } +} diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java index 42ef42770..d8acb04f2 100644 --- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java +++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java @@ -33,10 +33,12 @@ import org.eclipse.emf.transaction.util.TransactionUtil; import org.eclipse.etrice.core.common.ui.linking.GlobalNonPlatformURIEditorOpener; import org.eclipse.etrice.ui.common.base.UIBaseActivator; import org.eclipse.etrice.ui.common.base.editor.DiagramEditorBase; +import org.eclipse.etrice.ui.common.base.editor.RelativeFileURIHandler; import org.eclipse.etrice.ui.common.base.preferences.UIBasePreferenceConstants; import org.eclipse.graphiti.mm.pictograms.Diagram; import org.eclipse.graphiti.services.Graphiti; import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; @@ -44,6 +46,7 @@ import org.eclipse.ui.part.FileEditorInput; import org.eclipse.xtext.resource.XtextResourceSet; import org.eclipse.xtext.ui.resource.IResourceSetProvider; +import com.google.common.collect.Maps; import com.google.inject.Inject; /** @@ -131,7 +134,7 @@ public abstract class DiagramAccessBase { populateDiagram(rootObject, diagram); try { - diagRes.save(Collections.EMPTY_MAP); + diagRes.save(RelativeFileURIHandler.addToOptions(Maps.newHashMap())); } catch (IOException e) { e.printStackTrace(); } @@ -206,34 +209,39 @@ public abstract class DiagramAccessBase { public DiagramEditorBase findDiagramEditor(EObject rootObject) { IFileEditorInput input = getEditorInput(rootObject); - return (DiagramEditorBase) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findEditor(input); + IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findEditor(input); + if(part instanceof DiagramEditorBase) { + return (DiagramEditorBase) part; + } + + return null; } public DiagramEditorBase openDiagramEditor(EObject rootObject) { IFileEditorInput input = getEditorInput(rootObject); - try { - return (DiagramEditorBase) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, getEditorId()); - } catch (PartInitException e) { - String error = "Error while opening diagram editor"; - System.err.println(error); + if(input != null) { + try { + IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, getEditorId()); + if(part instanceof DiagramEditorBase) { + return (DiagramEditorBase) part; + } + } catch (PartInitException e) { + String error = "Error while opening diagram editor"; + System.err.println(error); + } } + return null; } private IFileEditorInput getEditorInput(EObject rootObject) { Diagram diagram = getDiagram(rootObject); + if(diagram == null) return null; - URI uri = diagram.eResource().getURI(); - String platformString = null; - if (uri.isPlatform()) { - platformString = uri.toPlatformString(true); - } - else { - uri = GlobalNonPlatformURIEditorOpener.getPlatformURI(uri); - platformString = uri.toPlatformString(true); - } - IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString)); + URI uri = GlobalNonPlatformURIEditorOpener.getPlatformURI(diagram.eResource().getURI()); + if(uri == null) return null; + IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true))); IFileEditorInput input = new FileEditorInput(file); return input; } diff --git a/tests/org.eclipse.etrice.ui.tests.base/.classpath b/tests/org.eclipse.etrice.ui.tests.base/.classpath index ad32c83a7..c31ca403f 100644 --- a/tests/org.eclipse.etrice.ui.tests.base/.classpath +++ b/tests/org.eclipse.etrice.ui.tests.base/.classpath @@ -3,5 +3,6 @@ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="src" path="src"/> + <classpathentry kind="src" path="xtend-gen"/> <classpathentry kind="output" path="bin"/> </classpath> diff --git a/tests/org.eclipse.etrice.ui.tests.base/.project b/tests/org.eclipse.etrice.ui.tests.base/.project index ebe0323d9..a9cfde5ad 100644 --- a/tests/org.eclipse.etrice.ui.tests.base/.project +++ b/tests/org.eclipse.etrice.ui.tests.base/.project @@ -6,6 +6,11 @@ </projects> <buildSpec> <buildCommand> + <name>org.eclipse.xtext.ui.shared.xtextBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> @@ -24,5 +29,6 @@ <natures> <nature>org.eclipse.pde.PluginNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> + <nature>org.eclipse.xtext.ui.shared.xtextNature</nature> </natures> </projectDescription> diff --git a/tests/org.eclipse.etrice.ui.tests.base/META-INF/MANIFEST.MF b/tests/org.eclipse.etrice.ui.tests.base/META-INF/MANIFEST.MF index 523cd5351..3a1d4f91a 100644 --- a/tests/org.eclipse.etrice.ui.tests.base/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.etrice.ui.tests.base/META-INF/MANIFEST.MF @@ -8,7 +8,13 @@ Require-Bundle: org.junit;bundle-version="4.8.1", org.eclipse.etrice.core.room;bundle-version="1.1.1", org.eclipse.etrice.ui.structure;bundle-version="1.1.1", org.eclipse.xtext;bundle-version="2.6.0", - org.eclipse.graphiti;bundle-version="0.8.0" + org.eclipse.graphiti;bundle-version="0.8.0", + org.eclipse.etrice.ui.common.base, + com.google.guava, + org.eclipse.xtext.xbase.lib, + org.eclipse.xtend.lib, + org.eclipse.xtend.lib.macro Bundle-Vendor: Eclipse eTrice Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Export-Package: org.eclipse.etrice.tests.base + diff --git a/tests/org.eclipse.etrice.ui.tests.base/src/org/eclipse/etrice/tests/base/RelativeFileURITest.xtend b/tests/org.eclipse.etrice.ui.tests.base/src/org/eclipse/etrice/tests/base/RelativeFileURITest.xtend new file mode 100644 index 000000000..8c5ffd596 --- /dev/null +++ b/tests/org.eclipse.etrice.ui.tests.base/src/org/eclipse/etrice/tests/base/RelativeFileURITest.xtend @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (c) 2011 protos software gmbh (http://www.protos.de). + * 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: + * Juergen Haug (initial contribution) + * + *******************************************************************************/ + +package org.eclipse.etrice.tests.base + +import org.eclipse.emf.common.util.URI +import org.eclipse.etrice.ui.common.base.editor.RelativeFileURIHandler +import org.junit.Test + +import static org.junit.Assert.assertEquals + +class RelativeFileURITest { + // uris copied from + // https://github.com/eclipse/emf/blob/master/tests/org.eclipse.emf.test.core/src/org/eclipse/emf/test/core/common/util/URITest.java + + val static ABSOLUTE_URLS = #[ + "file:/", + "file:/bar", + "file:/bar/", + "file:/bar/baz", + "file:/bar/baz/", + "file:/c:", + "file:/c:/", + "file:/c:/bar", + "file:/c:/bar/", + "file:/c:/bar/baz", + "file:/c:/bar/baz/", + "file://foo", + "file://foo/", + "file://foo/bar", + "file://foo/bar/", + "file://foo/bar/baz", + "file://foo/bar/baz/", + "file://foo/c:", + "file://foo/c:/", + "file://foo/c:/bar", + "file://foo/c:/bar/", + "file://foo/c:/bar/baz", + "file://foo/c:/bar/baz/" + ] + + val static RESOLVED_PRESERVE_ABOVE_ROOT_URIS = #["file:/a/../g", "file:/a/../../g"] + + val static RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS = #["file:/a/g", "file:/a/g/e"] + + val static ENCODED_URIS = #[ + "http://www.eclipse.org/foo", + "http://server%231.eclipse.org/foo%20bar/baz#toc", + "myscheme:my%20name", + "file:/C:/My%20Documents/me/50%25+1.txt" + ] + + val static ENCODED_PLATFORM_PATH_URIS = #[ + "platform:/resource/project/myfile.txt", + "platform:/resource/My%20Project%20%231/My%20File.txt", + "platform:/resource/are%20you%20there%3F", + "platform:/resource/are%20you%20there%3F.txt", + "platform:/resource" + ] + + val static QUERIES = #["", "?q=huh"] + val static FRAGMENTS = #["", "#toc", "#/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p", "#1:2:3"] + + val static TEST_URI = URI.createURI("file:/a/b/c/d") + + def Iterable<URI> allURIs() { + (ABSOLUTE_URLS + RESOLVED_PRESERVE_ABOVE_ROOT_URIS + RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS + ENCODED_URIS + ENCODED_PLATFORM_PATH_URIS).map[ uri | + QUERIES.map[query | FRAGMENTS.map[fragment | uri + query + fragment]] + ].flatten.flatten.map[URI.createURI(it)] + } + + val handler = new RelativeFileURIHandler(null); + + @Test + def void resolveSelf() { + allURIs.forEach[ uri | + handler.baseURI = TEST_URI + assertEquals(uri, handler.resolve(uri)) + ] + } + + @Test + def void roundtripResolve() { + allURIs.forEach[ uri | + handler.baseURI = URI.createURI("file:/a/b/c/d") + assertEquals(uri, handler.resolve(handler.deresolve(uri))) + ] + } + + @Test + def void deresolveBaseFile() { + val testURI = TEST_URI.appendFragment('1:2:3') + + handler.baseURI = URI.createURI(RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS.get(0)) + assertEquals('b/c/d#1:2:3', handler.deresolve(testURI).toString) + + handler.baseURI = URI.createURI(RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS.get(1)) + assertEquals('../b/c/d#1:2:3', handler.deresolve(testURI).toString) + } + + @Test + def void interDocument() { + val testURI = TEST_URI.appendFragment('1:2:3') + + handler.baseURI = TEST_URI + assertEquals('#1:2:3', handler.deresolve(testURI).toString) + + handler.baseURI = TEST_URI.appendFragment('some') + assertEquals('#1:2:3', handler.deresolve(testURI).toString) + } + + // TODO add platform uri tests, how ? +} diff --git a/tests/org.eclipse.etrice.ui.tests.base/xtend-gen/org/eclipse/etrice/tests/base/RelativeFileURITest.java b/tests/org.eclipse.etrice.ui.tests.base/xtend-gen/org/eclipse/etrice/tests/base/RelativeFileURITest.java new file mode 100644 index 000000000..76f761404 --- /dev/null +++ b/tests/org.eclipse.etrice.ui.tests.base/xtend-gen/org/eclipse/etrice/tests/base/RelativeFileURITest.java @@ -0,0 +1,141 @@ +/** + * Copyright (c) 2011 protos software gmbh (http://www.protos.de). + * 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: + * Juergen Haug (initial contribution) + */ +package org.eclipse.etrice.tests.base; + +import com.google.common.collect.Iterables; +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; +import org.eclipse.emf.common.util.URI; +import org.eclipse.etrice.ui.common.base.editor.RelativeFileURIHandler; +import org.eclipse.xtext.xbase.lib.CollectionLiterals; +import org.eclipse.xtext.xbase.lib.Functions.Function1; +import org.eclipse.xtext.xbase.lib.IterableExtensions; +import org.eclipse.xtext.xbase.lib.ListExtensions; +import org.junit.Assert; +import org.junit.Test; + +@SuppressWarnings("all") +public class RelativeFileURITest { + private final static List<String> ABSOLUTE_URLS = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("file:/", "file:/bar", "file:/bar/", "file:/bar/baz", "file:/bar/baz/", "file:/c:", "file:/c:/", "file:/c:/bar", "file:/c:/bar/", "file:/c:/bar/baz", "file:/c:/bar/baz/", "file://foo", "file://foo/", "file://foo/bar", "file://foo/bar/", "file://foo/bar/baz", "file://foo/bar/baz/", "file://foo/c:", "file://foo/c:/", "file://foo/c:/bar", "file://foo/c:/bar/", "file://foo/c:/bar/baz", "file://foo/c:/bar/baz/")); + + private final static List<String> RESOLVED_PRESERVE_ABOVE_ROOT_URIS = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("file:/a/../g", "file:/a/../../g")); + + private final static List<String> RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("file:/a/g", "file:/a/g/e")); + + private final static List<String> ENCODED_URIS = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("http://www.eclipse.org/foo", "http://server%231.eclipse.org/foo%20bar/baz#toc", "myscheme:my%20name", "file:/C:/My%20Documents/me/50%25+1.txt")); + + private final static List<String> ENCODED_PLATFORM_PATH_URIS = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("platform:/resource/project/myfile.txt", "platform:/resource/My%20Project%20%231/My%20File.txt", "platform:/resource/are%20you%20there%3F", "platform:/resource/are%20you%20there%3F.txt", "platform:/resource")); + + private final static List<String> QUERIES = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("", "?q=huh")); + + private final static List<String> FRAGMENTS = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("", "#toc", "#/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p", "#1:2:3")); + + private final static URI TEST_URI = URI.createURI("file:/a/b/c/d"); + + public Iterable<URI> allURIs() { + Iterable<String> _plus = Iterables.<String>concat(RelativeFileURITest.ABSOLUTE_URLS, RelativeFileURITest.RESOLVED_PRESERVE_ABOVE_ROOT_URIS); + Iterable<String> _plus_1 = Iterables.<String>concat(_plus, RelativeFileURITest.RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS); + Iterable<String> _plus_2 = Iterables.<String>concat(_plus_1, RelativeFileURITest.ENCODED_URIS); + Iterable<String> _plus_3 = Iterables.<String>concat(_plus_2, RelativeFileURITest.ENCODED_PLATFORM_PATH_URIS); + final Function1<String, List<List<String>>> _function = new Function1<String, List<List<String>>>() { + @Override + public List<List<String>> apply(final String uri) { + final Function1<String, List<String>> _function = new Function1<String, List<String>>() { + @Override + public List<String> apply(final String query) { + final Function1<String, String> _function = new Function1<String, String>() { + @Override + public String apply(final String fragment) { + return ((uri + query) + fragment); + } + }; + return ListExtensions.<String, String>map(RelativeFileURITest.FRAGMENTS, _function); + } + }; + return ListExtensions.<String, List<String>>map(RelativeFileURITest.QUERIES, _function); + } + }; + Iterable<List<List<String>>> _map = IterableExtensions.<String, List<List<String>>>map(_plus_3, _function); + Iterable<List<String>> _flatten = Iterables.<List<String>>concat(_map); + Iterable<String> _flatten_1 = Iterables.<String>concat(_flatten); + final Function1<String, URI> _function_1 = new Function1<String, URI>() { + @Override + public URI apply(final String it) { + return URI.createURI(it); + } + }; + return IterableExtensions.<String, URI>map(_flatten_1, _function_1); + } + + private final RelativeFileURIHandler handler = new RelativeFileURIHandler(null); + + @Test + public void resolveSelf() { + Iterable<URI> _allURIs = this.allURIs(); + final Consumer<URI> _function = new Consumer<URI>() { + @Override + public void accept(final URI uri) { + RelativeFileURITest.this.handler.setBaseURI(RelativeFileURITest.TEST_URI); + URI _resolve = RelativeFileURITest.this.handler.resolve(uri); + Assert.assertEquals(uri, _resolve); + } + }; + _allURIs.forEach(_function); + } + + @Test + public void roundtripResolve() { + Iterable<URI> _allURIs = this.allURIs(); + final Consumer<URI> _function = new Consumer<URI>() { + @Override + public void accept(final URI uri) { + URI _createURI = URI.createURI("file:/a/b/c/d"); + RelativeFileURITest.this.handler.setBaseURI(_createURI); + URI _deresolve = RelativeFileURITest.this.handler.deresolve(uri); + URI _resolve = RelativeFileURITest.this.handler.resolve(_deresolve); + Assert.assertEquals(uri, _resolve); + } + }; + _allURIs.forEach(_function); + } + + @Test + public void deresolveBaseFile() { + final URI testURI = RelativeFileURITest.TEST_URI.appendFragment("1:2:3"); + String _get = RelativeFileURITest.RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS.get(0); + URI _createURI = URI.createURI(_get); + this.handler.setBaseURI(_createURI); + URI _deresolve = this.handler.deresolve(testURI); + String _string = _deresolve.toString(); + Assert.assertEquals("b/c/d#1:2:3", _string); + String _get_1 = RelativeFileURITest.RESOLVED_NO_PRESERVE_ABOVE_ROOT_URIS.get(1); + URI _createURI_1 = URI.createURI(_get_1); + this.handler.setBaseURI(_createURI_1); + URI _deresolve_1 = this.handler.deresolve(testURI); + String _string_1 = _deresolve_1.toString(); + Assert.assertEquals("../b/c/d#1:2:3", _string_1); + } + + @Test + public void interDocument() { + final URI testURI = RelativeFileURITest.TEST_URI.appendFragment("1:2:3"); + this.handler.setBaseURI(RelativeFileURITest.TEST_URI); + URI _deresolve = this.handler.deresolve(testURI); + String _string = _deresolve.toString(); + Assert.assertEquals("#1:2:3", _string); + URI _appendFragment = RelativeFileURITest.TEST_URI.appendFragment("some"); + this.handler.setBaseURI(_appendFragment); + URI _deresolve_1 = this.handler.deresolve(testURI); + String _string_1 = _deresolve_1.toString(); + Assert.assertEquals("#1:2:3", _string_1); + } +} |