1 /*******************************************************************************
2 * Copyright (C) 2016 Obeo.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org
.eclipse
.egit
.ui
.view
.synchronize
;
13 import static org
.eclipse
.egit
.ui
.view
.synchronize
.MockLogicalModelProvider
.MOCK_LOGICAL_FILE_EXTENSION
;
15 import java
.io
.BufferedReader
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStream
;
18 import java
.io
.InputStreamReader
;
19 import java
.nio
.charset
.Charset
;
20 import java
.util
.ArrayList
;
21 import java
.util
.LinkedHashSet
;
22 import java
.util
.List
;
25 import org
.eclipse
.core
.resources
.IContainer
;
26 import org
.eclipse
.core
.resources
.IFile
;
27 import org
.eclipse
.core
.resources
.IProject
;
28 import org
.eclipse
.core
.resources
.IResource
;
29 import org
.eclipse
.core
.resources
.IStorage
;
30 import org
.eclipse
.core
.resources
.mapping
.RemoteResourceMappingContext
;
31 import org
.eclipse
.core
.resources
.mapping
.ResourceMapping
;
32 import org
.eclipse
.core
.resources
.mapping
.ResourceMappingContext
;
33 import org
.eclipse
.core
.resources
.mapping
.ResourceTraversal
;
34 import org
.eclipse
.core
.runtime
.CoreException
;
35 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
36 import org
.eclipse
.core
.runtime
.IStatus
;
37 import org
.eclipse
.core
.runtime
.Path
;
38 import org
.eclipse
.core
.runtime
.Status
;
39 import org
.eclipse
.core
.runtime
.SubMonitor
;
42 * This resource mapping requires a remote context to get the logical model. It
43 * will parse the content of its file which is supposed to be a list of files in
44 * the same folder and aggregate all these files in its logical model. It does
45 * that for every side, remote and base included.
47 public class MockLogicalResourceMapping
extends ResourceMapping
{
48 private final IFile file
;
50 private final String providerId
;
52 public MockLogicalResourceMapping(IFile file
, String providerId
) {
54 this.providerId
= providerId
;
58 public Object
getModelObject() {
63 public String
getModelProviderId() {
68 public ResourceTraversal
[] getTraversals(ResourceMappingContext context
,
69 IProgressMonitor monitor
) throws CoreException
{
70 SubMonitor sm
= SubMonitor
.convert(monitor
, 3);
71 Set
<IFile
> result
= new LinkedHashSet
<IFile
>();
74 List
<IFile
> dependencies
= getDependencies(file
, file
.getParent());
75 result
.addAll(dependencies
);
77 if (context
instanceof RemoteResourceMappingContext
) {
78 RemoteResourceMappingContext rmc
= (RemoteResourceMappingContext
) context
;
79 IStorage baseContents
= rmc
.fetchBaseContents(file
,
81 if (baseContents
!= null) {
83 getDependencies(baseContents
, file
.getParent()));
85 IStorage remoteContents
= rmc
.fetchRemoteContents(file
,
87 if (remoteContents
!= null) {
89 getDependencies(remoteContents
, file
.getParent()));
92 } catch (IOException e
) {
93 throw new CoreException(
94 new Status(IStatus
.ERROR
, "org.eclipse.egit.ui.test",
95 "Exception while computing logical model", e
));
97 final IResource
[] resourceArray
= result
98 .toArray(new IResource
[0]);
99 return new ResourceTraversal
[] { new ResourceTraversal(resourceArray
,
100 IResource
.DEPTH_ONE
, IResource
.NONE
), };
103 private List
<IFile
> getDependencies(IStorage storage
, IContainer c
)
104 throws CoreException
, IOException
{
105 List
<IFile
> result
= new ArrayList
<>();
106 try (InputStream contents
= storage
.getContents();) {
107 BufferedReader r
= new BufferedReader(
108 new InputStreamReader(contents
, Charset
.forName("UTF-8")));
111 String line
= r
.readLine();
112 IFile dep
= c
.getFile(new Path(line
));
115 } catch (Exception e
) {
122 protected void addLogicalModelFiles(IResource
[] resources
,
123 Set
<IFile
> sampleSiblings
) {
124 if (resources
== null) {
127 for (IResource res
: resources
) {
128 if (res
instanceof IFile
&& MOCK_LOGICAL_FILE_EXTENSION
129 .equals(res
.getFileExtension())) {
130 sampleSiblings
.add((IFile
) res
);
136 public IProject
[] getProjects() {
137 return new IProject
[] { file
.getProject(), };