Skip to main content
summaryrefslogtreecommitdiffstats
blob: 652145bda00edd53dc65349e5bd46b4887e50bb5 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*******************************************************************************
 * 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.core.tests.util;

import junit.framework.TestCase;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jst.jsf.common.util.JDTBeanProperty;
import org.eclipse.jst.jsf.common.util.JDTBeanPropertyWorkingCopy;
import org.eclipse.jst.jsf.core.tests.TestsPlugin;
import org.eclipse.jst.jsf.test.util.JDTTestEnvironment;
import org.eclipse.jst.jsf.test.util.JSFTestUtil;
import org.eclipse.jst.jsf.test.util.TestFileResource;
import org.eclipse.jst.jsf.test.util.WebProjectTestEnvironment;

/**
 * Tests basic operations on the jdt bean property
 * 
 * @author cbateman
 * 
 */
public class TestJDTBeanPropertyWorkingCopy extends TestCase
{
    private JDTTestEnvironment  _jdtTestEnvironment;
    private IType               _testBean1Type;

    private final static String srcFolderName = "src";
    private final static String packageName1  = "com.test";
    private final static String testBeanName1 = "TestBean1";

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();

        JSFTestUtil.setValidationEnabled(false);
        JSFTestUtil.setInternetProxyPreferences(true,
                "www-proxy.us.oracle.com", "80");

        final WebProjectTestEnvironment projectTestEnvironment =
            new WebProjectTestEnvironment("TestJDTBeanPropertyWorkingCopy"
                    + getClass().getName() + "_" + getName());
        projectTestEnvironment.createProject(true);

        _jdtTestEnvironment = new JDTTestEnvironment(projectTestEnvironment);

        // load TestBean1
        final TestFileResource codeRes = new TestFileResource();
        codeRes.load(TestsPlugin.getDefault().getBundle(),
        "/testfiles/TestBean1.java.data");
        final String code = codeRes.toString();
        _jdtTestEnvironment.addSourceFile(srcFolderName, packageName1,
                testBeanName1, code);

        _testBean1Type =
            _jdtTestEnvironment.getJavaProject().findType(
                    packageName1 + "." + testBeanName1);
        assertNotNull(_testBean1Type);
    }

    @Override
    protected void tearDown() throws Exception
    {
        super.tearDown();
        final IProject project = _jdtTestEnvironment.getJavaProject().getProject();

        try
        {
            project.close(null);
            project.delete(true, null);
        }
        catch (final CoreException ce)
        {
            ce.printStackTrace(System.err);
        }
    }

    /**
     * Test simple dt bean introspection
     */
    public void testSimpleBeanProperty()
    {
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleGetter =
            _testBean1Type.getMethod("getStringProp1", new String[0]);
        final IMethod simpleSetter =
            _testBean1Type.getMethod("setStringProp1", new String[]
                                                                  { "QString;" });
        assertNotNull(simpleGetter);
        assertNotNull(simpleSetter);

        workingCopy.addSetter(simpleSetter);
        workingCopy.setGetter(simpleGetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // we provided both a get and set, so should be readable and writable
        assertTrue(beanProperty.isReadable());
        assertTrue(beanProperty.isWritable());

        // the getter and setter methods should match the ones we gave
        assertTrue(beanProperty.getGetter() == simpleGetter);
        assertTrue(beanProperty.getSetter() == simpleSetter);

        // the type should resolve to a String
        assertEquals("Ljava.lang.String;",beanProperty.getTypeSignature());

        // should be able to resolve the IType
        assertNotNull(beanProperty.getType());
    }

    /**
     * Test a simple dt bean based on a boolean "is" getter
     */
    public void testSimpleIsBeanProperty()
    {
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleGetter =
            _testBean1Type.getMethod("isBooleanIsProp1", new String[0]);
        final IMethod simpleSetter =
            _testBean1Type.getMethod("setBooleanIsProp1", new String[]
                                                                     { Signature.SIG_BOOLEAN });
        assertNotNull(simpleGetter);
        assertNotNull(simpleSetter);

        workingCopy.addSetter(simpleSetter);
        workingCopy.setGetter(simpleGetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // we provided both a get and set, so should be readable and writable
        assertTrue(beanProperty.isReadable());
        assertTrue(beanProperty.isWritable());

        // the getter and setter methods should match the ones we gave
        assertTrue(beanProperty.getGetter() == simpleGetter);
        assertTrue(beanProperty.getSetter() == simpleSetter);

        // the type should resolve to a String
        assertEquals(Signature.SIG_BOOLEAN
                , beanProperty.getTypeSignature());

        // should be a boolean so no IType
        assertNull(beanProperty.getType());
    }

    /**
     * Assert that where there is both and "is" and "get" accessor for a boolean
     * property, the "is" accessor is always the one that is used
     */
    public void testIsAccessorTakesPrecedence()
    {
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleIsGetter =
            _testBean1Type.getMethod("isBooleanIsProp2", new String[0]);
        final IMethod simpleGetter =
            _testBean1Type.getMethod("getBooleanIsProp2", new String[0]);
        final IMethod simpleSetter =
            _testBean1Type.getMethod("setBooleanIsProp2", new String[]
                                                                     { Signature.SIG_BOOLEAN });
        assertNotNull(simpleGetter);
        assertNotNull(simpleSetter);
        assertNotNull(simpleIsGetter);

        workingCopy.setIsGetter(simpleIsGetter);
        workingCopy.addSetter(simpleSetter);
        workingCopy.setGetter(simpleGetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // we provided both a get and set, so should be readable and writable
        assertTrue(beanProperty.isReadable());
        assertTrue(beanProperty.isWritable());

        // the getter and setter methods should match the ones we gave
        // MOST IMPORTANT is that the is getter is selected
        assertTrue(beanProperty.getGetter() == simpleIsGetter);
        assertTrue(beanProperty.getSetter() == simpleSetter);

        // the type should resolve to a String
        assertEquals(Signature.SIG_BOOLEAN
                , beanProperty.getTypeSignature());

        // should be a boolean so no IType
        assertNull(beanProperty.getType());
    }

    // TODO: this test belongs in the bean introspector tests
    // /**
    // * The is accessor should not be used if it is not a boolean return type
    // */
    // public void testIsAccessorNotUsedIfNotBoolean()
    // {
    // JDTBeanPropertyWorkingCopy workingCopy =
    // new JDTBeanPropertyWorkingCopy(_testBean1Type);
    //
    // IMethod simpleIsGetter = _testBean1Type.getMethod("isNotBooleanIsProp1",
    // new String[0]);
    // IMethod simpleGetter = _testBean1Type.getMethod("getNotBooleanIsProp1",
    // new String[0]);
    // IMethod simpleSetter = _testBean1Type.getMethod("setNotBooleanIsProp1",
    // new String[] {Signature.SIG_BOOLEAN});
    //
    // workingCopy.setIsGetter(simpleIsGetter);
    // workingCopy.addSetter(simpleSetter);
    // workingCopy.setGetter(simpleGetter);
    //
    // JDTBeanProperty beanProperty = workingCopy.toValueObjects();
    // assertNotNull(beanProperty);
    //
    // // we provided both a get and set, so should be readable and writable
    // assertTrue(beanProperty.isReadable());
    // assertTrue(beanProperty.isWritable());
    //
    // // the getter and setter methods should match the ones we gave
    // // MOST IMPORTANT is that the is getter is NOT selected
    // assertTrue(beanProperty.getGetter() == simpleGetter);
    // assertTrue(beanProperty.getSetter() == simpleSetter);
    //
    // // the type should resolve to a String
    // assertTrue(Signature.SIG_BOOLEAN.equals(beanProperty.getTypeSignature()));
    //
    // // should be a boolean so no IType
    // assertNull(beanProperty.getType());
    //
    // }

    /**
     * A setter should be ignored if it doesn't match the getter's return type
     */
    public void testDoNotUseSetterIfDoesNotMatchGetterType()
    {
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleGetter =
            _testBean1Type.getMethod("getStringProperty2", new String[0]);
        final IMethod simpleSetter =
            _testBean1Type.getMethod("setStringProperty2", new String[]
                                                                      { Signature.SIG_INT });
        assertNotNull(simpleGetter);
        assertNotNull(simpleSetter);

        workingCopy.addSetter(simpleSetter);
        workingCopy.setGetter(simpleGetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // valid getter so readable
        assertTrue(beanProperty.isReadable());
        // setter should be ignored so not writable
        assertFalse(beanProperty.isWritable());

        // getter should match
        assertTrue(beanProperty.getGetter() == simpleGetter);

        // setter should be null since the one given is ignored
        assertNull(beanProperty.getSetter());

        // the type should resolve to a String
        assertEquals("Ljava.lang.String;", beanProperty.getTypeSignature());

        // should be an IType for a String
        assertNotNull(beanProperty.getType());
    }

    /**
     * test read-only bean properties
     */
    public void testReadonlyBeanProperty()
    {
        // readonly get
        JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleGetter =
            _testBean1Type.getMethod("getReadonlyStringProperty",
                    new String[0]);
        assertNotNull(simpleGetter);

        workingCopy.addSetter(null);
        workingCopy.setGetter(simpleGetter);

        JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // valid getter so readable
        assertTrue(beanProperty.isReadable());
        // setter is null so should no be writable
        assertTrue(!beanProperty.isWritable());

        // getter should match
        assertTrue(beanProperty.getGetter() == simpleGetter);

        // setter should be null
        assertNull(beanProperty.getSetter());

        // the type should resolve to a String
        assertEquals("Ljava.lang.String;", beanProperty.getTypeSignature());

        // should be an IType for a String
        assertNotNull(beanProperty.getType());

        workingCopy = new JDTBeanPropertyWorkingCopy(_testBean1Type);

        // readonly is getter
        final IMethod isGetter =
            _testBean1Type.getMethod("isReadonlyBooleanProperty",
                    new String[0]);
        assertNotNull(isGetter);

        workingCopy.addSetter(null);
        workingCopy.setGetter(isGetter);

        beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // valid getter so readable
        assertTrue(beanProperty.isReadable());
        // setter null so not writable
        assertFalse(beanProperty.isWritable());

        // getter should match the isGetter
        assertTrue(beanProperty.getGetter() == isGetter);

        // setter should be null
        assertNull(beanProperty.getSetter());

        // the type should resolve to a String
        assertEquals(Signature.SIG_BOOLEAN
                , beanProperty.getTypeSignature());

        // should not be an IType for a boolean
        assertNull(beanProperty.getType());
    }

    /**
     * Test a simple write-only bean
     */
    public void testWriteonlyBeanProperty()
    {
        // readonly get
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleSetter =
            _testBean1Type.getMethod("setWriteonlyStringProperty",
                    new String[]
                               { "QString;" });
        assertNotNull(simpleSetter);

        workingCopy.addSetter(simpleSetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // not readable
        assertFalse(beanProperty.isReadable());
        // writable
        assertTrue(beanProperty.isWritable());

        // setter should match
        assertTrue(beanProperty.getSetter() == simpleSetter);

        // getter should be null
        assertNull(beanProperty.getGetter());

        // the type should resolve to a String
        assertEquals("Ljava.lang.String;", beanProperty.getTypeSignature());

        // should be an IType for a String
        assertNotNull(beanProperty.getType());
    }

    /**
     * 
     */
    public void testStringArrayProperty()
    {
        // readonly get
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleGetter =
            _testBean1Type.getMethod("getStringArrayProperty",
                    new String[0]);
        final IMethod simpleSetter =
            _testBean1Type.getMethod("setStringArrayProperty", new String[]
                                                                          { "[QString;" });
        assertNotNull(simpleGetter);
        assertNotNull(simpleSetter);

        workingCopy.setGetter(simpleGetter);
        workingCopy.addSetter(simpleSetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // readable
        assertTrue(beanProperty.isReadable());
        // writable
        assertTrue(beanProperty.isWritable());

        // getter/setter should match
        assertTrue(beanProperty.getGetter() == simpleGetter);
        assertTrue(beanProperty.getSetter() == simpleSetter);

        // the type should resolve to a String[]
        assertEquals("[Ljava.lang.String;",
                beanProperty.getTypeSignature());

        // Should resolve to base type (String)
        assertNotNull(beanProperty.getType());
    }

    /**
     * 
     */
    public void testCollectionProperty()
    {
        // readonly get
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleGetter =
            _testBean1Type
            .getMethod("getCollectionProperty", new String[0]);
        final IMethod simpleSetter =
            _testBean1Type.getMethod("setCollectionProperty", new String[]
                                                                         { "QCollection;" });
        assertNotNull(simpleGetter);
        assertNotNull(simpleSetter);

        workingCopy.setGetter(simpleGetter);
        workingCopy.addSetter(simpleSetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // readable
        assertTrue(beanProperty.isReadable());
        // writable
        assertTrue(beanProperty.isWritable());

        // getter/setter should match
        assertTrue(beanProperty.getGetter() == simpleGetter);
        assertTrue(beanProperty.getSetter() == simpleSetter);

        // the type should resolve to java.util.Collection
        assertEquals("Ljava.util.Collection;", beanProperty
                .getTypeSignature());

        // should resolve a type
        assertNotNull(beanProperty.getType());
    }

    /**
     * 
     */
    public void testMapProperty()
    {
        // readonly get
        final JDTBeanPropertyWorkingCopy workingCopy =
            new JDTBeanPropertyWorkingCopy(_testBean1Type);

        final IMethod simpleGetter =
            _testBean1Type.getMethod("getMapProperty", new String[0]);
        final IMethod simpleSetter =
            _testBean1Type.getMethod("setMapProperty", new String[]
                                                                  { "QMap;" });
        assertNotNull(simpleGetter);
        assertNotNull(simpleSetter);

        workingCopy.setGetter(simpleGetter);
        workingCopy.addSetter(simpleSetter);

        final JDTBeanProperty beanProperty = workingCopy.toValueObject();
        assertNotNull(beanProperty);

        // readable
        assertTrue(beanProperty.isReadable());
        // writable
        assertTrue(beanProperty.isWritable());

        // getter/setter should match
        assertTrue(beanProperty.getGetter() == simpleGetter);
        assertTrue(beanProperty.getSetter() == simpleSetter);

        // the type should resolve to java.util.Map
        assertEquals("Ljava.util.Map;", beanProperty.getTypeSignature());

        // should resolve a type
        assertNotNull(beanProperty.getType());
    }

}

Back to the top