Skip to main content
summaryrefslogtreecommitdiffstats
blob: 48738545b147d1e1beacd15e33fa3c4e3fc5b1e9 (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
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * 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:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.context.symbol.tests;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import junit.framework.TestCase;

import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jst.jsf.context.symbol.IComponentSymbol;
import org.eclipse.jst.jsf.context.symbol.IMapTypeDescriptor;
import org.eclipse.jst.jsf.context.symbol.ISymbol;
import org.eclipse.jst.jsf.context.symbol.SymbolFactory;
import org.eclipse.jst.jsf.context.symbol.provider.IContentProposalProvider;
import org.eclipse.jst.jsf.context.symbol.provider.ProposalCreationFactoryAdapter;
import org.eclipse.jst.jsf.test.util.TestFileResource;
import org.eclipse.swt.graphics.Image;

/**
 * Tests for TestIPropertySymbolItemProvider
 * 
 * @author cbateman
 *
 */
public class TestIPropertySymbolItemProvider extends TestCase 
{   
    private Properties  props;
    
    protected void setUp() throws Exception 
    {
        super.setUp();
        TestFileResource propertiesFile = new TestFileResource();
        propertiesFile.load(ContextSymbolTestPlugin.getDefault().getBundle(), 
                                "/testdata/bundle.properties.data");
        props = new Properties();
        props.load(new ByteArrayInputStream(propertiesFile.toBytes()));
    }

    /**
     * Ensure test is in sync with test data and other sanity checks
     */
    public void testSanity()
    {
        assertEquals(props.size(), 3);
    }
    
    /**
     * Test completion proposal construction for a properties map
     */
    public void testProposalConstructionForMap()
    {
        IMapTypeDescriptor  mapDesc = 
            SymbolFactory.eINSTANCE.createIMapTypeDescriptor();
        mapDesc.setImmutable(true);
        mapDesc.setMapSource(props);

        final IComponentSymbol symbol = 
            SymbolFactory.eINSTANCE.createIComponentSymbol();
        symbol.setName("myTestSymbol");
        symbol.setTypeDescriptor(mapDesc);

        final ComposedAdapterFactory factory =
            new ComposedAdapterFactory(
                   ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

        final  MyCompletionFactory creationInfo = new MyCompletionFactory();

        for (final Iterator it = symbol.getTypeDescriptor().getProperties().iterator(); it.hasNext();)
        {
            final ISymbol propSymbol = (ISymbol) it.next();
            final Object  provider =  
              factory.adapt(propSymbol, IContentProposalProvider.class);

            assertTrue(provider instanceof IContentProposalProvider);

            final ICompletionProposal[] proposal  = 
                ((IContentProposalProvider) provider).
                    getProposals(propSymbol, creationInfo);

            assertNotNull(proposal);
        }
        
        List  list = creationInfo._replacementText;

        assertEquals(list.size(), 3);
        
        for (final Iterator it = list.iterator();it.hasNext();)
        {
            String replacementText = (String) it.next();
            
            if (replacementText.startsWith("['"))
            {
                replacementText = replacementText.substring(2);
                replacementText = replacementText.substring(0, replacementText.length()-2);
            }
            assertTrue(props.containsKey(replacementText));
        }
    }
    
    private class MyCompletionFactory extends ProposalCreationFactoryAdapter
    {
        final List<String>        _replacementText;
        
        /**
         * 
         */
        public MyCompletionFactory()
        {
            super(0, 0);
            _replacementText = new ArrayList<String>();
        }

        public ICompletionProposal createProposal(String replacementText, String displayText, String additionalText, Image displayImage, Object targetObject) 
        {
            _replacementText.add(replacementText);
            
            return super.createProposal(replacementText, displayText, additionalText,
                    displayImage, targetObject);
        }
    }
}

Back to the top