Skip to main content
summaryrefslogtreecommitdiffstats
blob: d13e95ffce6865fef0b1dba8d37c7e583ef768dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package org.eclipse.jst.jsf.test.util.mock;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jst.jsf.test.util.JSFTestUtil;
import org.eclipse.jst.jsf.test.util.junit4.TestDataBaseLocation;
import org.eclipse.jst.jsf.test.util.junit4.WorkspaceContext;
import org.eclipse.jst.jsf.test.util.junit4.WorkspaceRunner;
import org.eclipse.jst.jsf.test.util.mock.IWorkspaceContext.ZipFileLoader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(WorkspaceRunner.class)
public class TestMockWorkspaceContext
{
    @WorkspaceContext
    private IWorkspaceContext _wsContext;
    @TestDataBaseLocation
    private File _baseLoc;
    private ZipFileLoader _testZipFileLoader;

    @Before
    public void setUp() throws Exception
    {
        System.out.println(_baseLoc);
        IPath path = new Path(_baseLoc.getAbsolutePath());
        path = path.append("/data/TestProject.zip");
        final File file = path.toFile();
        assertTrue(file.exists());
        _testZipFileLoader = new FileSystemZipLoader(file, "TestProject/");
    }

//    @Test
//    public void testFireWorkspaceEvent()
//    {
//        // TODO:
//        _wsContext.fireWorkspaceEvent(null);
//    }

    @Test
    public void testDispose() throws Exception
    {
        _wsContext.dispose();
    }

    @Test
    public void testGetWorkspace()
    {
        final IWorkspace workspace = _wsContext.getWorkspace();
        assertNotNull(workspace);
        assertSame(workspace, _wsContext.getWorkspace());
    }

    @Test
    public void testGetProject() throws Exception
    {
        final IPath path = new Path("TestProject");
        IProject project = _wsContext.getProject(path);
        assertNull(project);
        // now load the resource
        project = _wsContext.loadProject(path, _testZipFileLoader);
        assertNotNull(project);
    }

    @Test
    public void testGetResource() throws Exception
    {
        final IPath projPath = new Path("TestProject");
        // if we haven't done anything special, then this res shouldn't exist.
        final IPath path = new Path("/WebContent/test.xhtml");
        final IPath fullPath = projPath.append(path);
        IResource resource = _wsContext.getResource(path);
        assertNull(resource);
        // now load the resource; still won't have the resource until it is
        // requested.
        final IProject project = _wsContext.loadProject(projPath,
                _testZipFileLoader);
        assertNotNull(project);

        // TODO: only true for non-plugin
        //        resource = _wsContext.getResource(fullPath);
//        assertNull(resource);
        // now request the resource through the project interface. This should
        // cause it to be known by the context.
        final IFile file = project.getFile(path);
        assertNotNull(file);
        assertEquals(file, _wsContext.getResource(fullPath));
    }

    @Test
    public void testGetFile() throws Exception
    {
        final IPath projPath = new Path("TestProject");
        // if we haven't done anything special, then this res shouldn't exist.
        final IPath path = new Path("/WebContent/test.xhtml");
        final IPath fullPath = projPath.append(path);
        IFile file = _wsContext.getFile(path);
        assertNull(file);
        // now load the resource; still won't have the resource until it is
        // requested.
        final IProject project = _wsContext.loadProject(projPath,
                _testZipFileLoader);
        assertNotNull(project);
        // TODO:
//        file = _wsContext.getFile(fullPath);
//        assertNull(file);
        // now request the resource through the project interface. This should
        // cause it to be known by the context.
        final IFile fileFromProject = project.getFile(path);
        assertNotNull(fileFromProject);
        assertEquals(fileFromProject, _wsContext.getFile(fullPath));
    }

    @Test
    public void testCreateProjectString()
    {
        final IProject project = _wsContext.createProject("Foo");
        assertNotNull(project);
        final IProject project2 = _wsContext.createProject("Foo");
        assertNotNull(project2);
        // two projects created with the same baseId should have unique
        // paths as taken care off by the context.
        assertFalse(project.equals(project2));
    }

    @Test
    public void testCreateProjectIPath()
    {
        final IProject project = _wsContext.createProject(new Path("Foo"));
        assertNotNull(project);
        assertEquals("Foo", project.getName());
    }

    @Test
    public void testLoadProjectIPathZipFile() throws Exception
    {
        final IProject project = _wsContext.loadProject(
                new Path("TestProject1"), _testZipFileLoader);
        assertNotNull(project);
        assertTrue(project.exists());
        final IFile file = project.getFile(new Path("WebContent/test.xhtml"));
        assertNotNull(file);
        assertTrue(file.exists());
        final InputStream contents = file.getContents();
        final ByteArrayOutputStream data = JSFTestUtil
                .loadFromInputStream(contents);
        assertEquals(367, data.toByteArray().length);
    }
}

Back to the top