KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > NodeProperty51907Test


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.nodes;
21
22 import java.util.Date JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import org.netbeans.junit.Log;
25 import org.netbeans.junit.NbTestCase;
26 import org.openide.ErrorManager;
27 import org.openide.util.Lookup;
28 import org.openide.util.lookup.AbstractLookup;
29 import org.openide.util.lookup.InstanceContent;
30
31 /**
32  * Tests for issue 51907. For more information see the
33  * <a HREF="http://openide.netbeans.org/issues/show_bug.cgi?id=51907">
34  * descrition in issuezilla</a>
35  *
36  * @author mkrauskopf
37  */

38 public class NodeProperty51907Test extends NbTestCase {
39     
40     public NodeProperty51907Test(String JavaDoc name) {
41         super(name);
42     }
43         
44     /**
45      * Note that for this test it doesn't matter what isDefaultValue() methods
46      * return.
47      */

48     public void testThatWarningIsLoggedForOldModulesProperty() {
49         CharSequence JavaDoc log = Log.enable("", Level.WARNING);
50         Node.Property property = new OldModulePropertyWithSDVReturningTrue();
51         // ErrorManager should log warning
52
property.isDefaultValue();
53         String JavaDoc className = property.getClass().getName();
54         assertTrue("The WARNING message should contain name of the property" +
55                 "class - " + className + " was log:\n" + log, log.toString().indexOf(className) >= 0);
56
57
58         int len = log.length();
59         
60         // ErrorManager shouldn't log warning more than once per property
61
property.isDefaultValue();
62         assertEquals("No other message logged", len, log.length());
63
64         Node.Property otherInstance = new OldModulePropertyWithSDVReturningTrue();
65         otherInstance.isDefaultValue();
66         assertEquals("No other message logged2", len, log.length());
67     }
68     
69     public void testThatWarningIsNotLoggedForPropertyWithBothMethodsOverrided() {
70         CharSequence JavaDoc log = Log.enable("", Level.WARNING);
71         
72         Node.Property property = new BothMethodsOverridedProperty();
73         // ErrorManager shouldn't log warning for correct implementations
74
property.isDefaultValue();
75         assertEquals("There shouldn't be any WARNING message logged by the ErrorManager", 0, log.length());
76     }
77     
78     public void testThatWarningIsNotLoggedForPropertyWithNoneMethodOverrided() {
79         CharSequence JavaDoc log = Log.enable("", Level.WARNING);
80         
81         Node.Property property = new DefaultTestProperty();
82         // ErrorManager shouldn't log warning for correct implementations
83
property.isDefaultValue();
84         assertEquals("There shouldn't be any WARNING message logged by the ErrorManager", 0, log.length());
85     }
86
87     
88     /**
89      * Simulates property for old modules which didn't know about
90      * isDefaultValue() method but could overrode restoreDefaultValue() to
91      * returns true. Warning has to be logged for such properties.
92      */

93     private static final class OldModulePropertyWithSDVReturningTrue
94             extends DefaultTestProperty {
95         public boolean supportsDefaultValue() {
96             return true;
97         }
98     }
99     
100     /**
101      * Simulates correctly implemented property which override both methods.
102      */

103     private static final class BothMethodsOverridedProperty
104             extends DefaultTestProperty {
105         public boolean supportsDefaultValue() {
106             return true;
107         }
108         public boolean isDefaultValue() {
109             return false;
110         }
111     }
112     
113     /**
114      * Simulates correctly implemented property which doesn't override any of
115      * the methods (supportsDefaultValue(), isDefaultValue()).
116      */

117     private static class DefaultTestProperty extends Node.Property {
118         /** We don't need any of these method (or constructor) for our testing. */
119         public DefaultTestProperty() { super(Object JavaDoc.class); }
120         public void setValue(Object JavaDoc val) {}
121         public Object JavaDoc getValue() { return null; }
122         public boolean canWrite() { return false; }
123         public boolean canRead() { return false; }
124     }
125     
126 }
127
Popular Tags