TS 74 JavaCodeCompletion

Java Code Completinon

Author:Jiri Prox
Version:7.4
Last update:2013/07/21
Introduction:

Contents

Test suite: Normal Code Completion

  1. Basic Case
    1. Open class completion.Normal from sample project
    2. Inside method test try type following and invoke CC (if not popup automatically):
      1. this.
      2. list.
      3. list.
      4. Thread.
      5. threads[0].
      6. String.
    3. Try several cases by your own.
    4. Open class completion.NormalContextAware from sample project
    5. add import to the proper place and open CC
    6. add extends to the proper place and open CC
    7. add implements to the proper place and open CC
    8. add throws to the constructor and call CC
    9. add
      try { TBD
    10. type throw new int method body call CC
    11. Repeat steps 2-7 but before invoking completion type several letters
    12. Create new class
    13. In constructor write following code:TBD
    14. Invoke CC after the dot
    • EXPECTED RESULT:Code completion contains method run() , method() and filed field , others item are from class Object
                   
  1. Method Finishing
    1. Open class completion.NormalMethods from sample project
    2. Inside testMethod type method
    3. Call CC and select one of the provided items
    • EXPECTED RESULT:The CC provides 4 possible completion, after confirming one of them the text is substituted, the parameter names are copied to code (no parameters matching takes part in this situation) and the parameter tooltip is opened.
                   
  1. Constructor Finishing
    1. Open class completion.NormalMethods from sample project
    2. Inside testMethod type new NormalMethods
    3. Call CC and select one of the provided constructor
    • EXPECTED RESULT:The CC window providing 5 possible ending is opened, 4 are the for the available constructors and the last one finishes the class name (will have no effect in this situation since the whole name is already entered and there is not other class with such prefix). After selecting item corresponding to constructor it behaves as common method call finishing as shown in previous test case
                   
  1. Creating Anonymous Class Body
    1. Create new class
    2. It it's constructor type new Comparable
    3. Call code completion and select first item
    • EXPECTED RESULT:The whole class body is added. Check if compareTo method has one parameter of type Object.
                   
  1. Negative Test Cases
    1. Call completion inside string literal
    2. Call completion inside double literal (e.g. 3.3)
    • EXPECTED RESULT:No suggestion is returned
  2. Switch Controled By Enum
    1. Open class completion.NormalSwitch from sample project
    2. in the switch statement type case
    3. call code completion
    • EXPECTED RESULT:CC provides only constants from enum Type
  3. Common part
    1. In editor type
      LinkedList linkedList = new LinkedList();
      linkedList.addLast(null);
    2. Put cursor before 'L' in addLast
    3. call code completion, by arrows select addFirst
    4. confirm by CTRL-ENTER
    • EXPECTED RESULT:The method is completed, the Last text is removed, and the parameters stay unchanged
  4. Static Elements
    1. In editor type createTem
    2. call code completion
    3. select File.createTempFile() and press enter
    • EXPECTED RESULT:A import for java.io.File is added, the method is completed to File.createTempFile(). Cursor is places inside the braces.

Test suite: All Symbols Code Completion

Purpose: All Symbols Code Completion returns all classes with required prefix which are available (in JDK, in current project or additional libraries), even when they are not imported. After selecting item from list an import statement is added if necessary.
Setup:

  1. All Classes
    1. Open any class
    2. Add several places call All Symbols CC (Ctrl+Alt+Space) or (Ctrl+Space twice)
    • EXPECTED RESULT: Check if CC provides all available classes matching entered prefix
  2. Switching From Normal to All-Symbols
    1. Create new class
    2. Inside class type JTab and call normal code completion (Ctrl+Space)
    • EXPECTED RESULT:The normal CC should not find any matching item so it should be automatically switched to all symbols CC which provide 3 items. Visual difference between normal and all-symbols CC is that the normal CC has footer in the popup window displaying information about others types of completions.
    

Test suite: Code Completion Settings

Purpose: This suite focus on customizing code completion behavior.
Setup:

  1. Defaut Values
    1. Open Tools | Options | Editor | General
    2. Check default values in Code Completion section
    • EXPECTED RESULT: Image:CCsettings_TS_74_JavaCodeCompletion.png
  2. Auto Popup
    1. Uncheck Auto Popup Completion Window
    2. In any class type this.
    3. Restore the default value and repeat previous step
    4. Uncheck Insert Single Proposals Automatically
    5. In editor type StringBuffe and call normal code completion
    6. In editor type Thread.curren and call normal code completion
    7. Restore the default value and repeat previous two steps
    • EXPECTED RESULT:The CC list provide one item but does not insert it if the options if switched off. Automatic substitution for class names is always turned off.
  3. Case Sensitive
    1. Check Case Sensitive Code Completion
    2. In editor type string and call normal code completion
    3. type String and call CC
    4. Revert changes in options and repeat step 2
    • EXPECTED RESULT: In step 2 the CC should return no suggestions since there is no such class. The step 3 and step 2 after returning changes should provide String* items
  4. Show Deprecated Item
    1. Uncheck Show Deprecated Members in Code Completion
    2. In editor type new Thread().
    3. Return changes and repeat previous step
    • EXPECTED RESULT: If turned off no deprecated members (as susspend() or resume() ) are shown
  5. Closing Brackets
    1. Uncheck Insert Closing Brackets Automatically
    2. In constructor type
       this( 
    3. In class body type
       int[ 
    4. In class body type
       void method( 
    5. Revert changes and repeat steps 2-4
    • EXPECTED RESULT:Closing bracket in not inserted if option is turned off. The closing bracket of correct type is inserted otherwise, the cursor is located between them.
  6. Documentation popup
    1. Uncheck Auto Popup Documentation Window
    2. Invokde code completion
    • EXPECTED RESULT: The window with javadoc is not poped up any more.
  7. Documentation popup next to Completion
    1. Make sure Auto Popup Documentation Window is on
    2. Check Display Documentation Popup Next to Compleiton
    3. Invokde code completion
    • EXPECTED RESULT: Javadoc window is displayed next (left or right) to the completion window
  8. Guess filled arguments
    1. Switch to java specific settings
    2. Uncheck Guess Filled Method Arguments
    3. Try completing some method call
    4. Check the option again
    5. Try completing some method call
    • EXPECTED RESULT: The the checkbox is checked the code completion will try to match the expected arguments type with variables avaiable in the actual position. E.g. it will replace the name of parameter with actual variable name if the types match. Otherwise it will complete method call using the names of parameters as are written in method declaration.
  9. Autopopup char
    1. Switch to java specific settings
    2. Change char for automatic poping up code completion window
    3. Use several chars - e.g. , and :
    • EXPECTED RESULT: The CC window is not poped up after typing '.' but after typing specified chars
  10. Auto popup on typing
    1. Switch to java specific settings
    2. Check Auto Popup on Typing Any Java Identifier Part
    3. Switch to editor and type any java identifier
    • EXPECTED RESULT: Code completion window is opened every time char is typed
  11. Confirm char
    1. Switch to java specific settings
    2. change Completion Selectors for Java
    3. Open CC window and use all of completion selector
    • EXPECTED RESULT: CC is confirmed (e.g. selected item is inserted into the code) when the selector char is typed
  12. Subword completion
    1. Switch to java specific settings
    2. select Subword completion
    3. Type some text in editor and call code completion
    • EXPECTED RESULT:Item containing type text (as a substring) are displayed. E.g. when typing ring a SecurityManager, String, StringBuffer, StringBuilder, SupressWarnings are provided
  13. Javadoc popup char
    1. Switch to java specific settings
    2. Writte following text into javadoc comment
      1. @see org.
      2. @
      3. {@
      4. {@link Boolean#
    3. change Auto Popup Triggers for Javadoc
    4. try typing in javadoc
    • EXPECTED RESULT: In all case in step 2 javadoc code completion is opened. In step 4 the changed setting are respected

Test suite: Keyword Code Completion

Setup:

  1. Within Class
    1. Create new class
    2. Delete all its content except package statement
    3. Try to enter whole class with several method, field and inner classes using CC to produce all keywords (only typing identifiers and symbols like brackets and operators by hand)
    • EXPECTED RESULT: The whole class can be written using only CC to insert keywords
  2. Within Annotation
    1. Repeat previous test case, but this time create annotation type
    • EXPECTED RESULT: Annotation type can be created w/o any limitations

Test suite: Smart Code Completion

Purpose: Smart code completion return only items with has (or return) the type suitable in current position. It's invoked by shortcut Ctrl-Space and takes the upper part of code completion popup.
Setup:

  1. Assign Statement
    1. Open class completion.SmartAssign from sample project
    2. Type List l = at the end of test method and call smart CC
    3. Replace last entered line with List<String> l = and call smart CC
    4. Replace last entered line with List<String> l = new and call smart CC
    5. open class completion.SmartReturn from sample project
    6. Replace last line of method getName with return and call smart CC
    7. Replace last line of method getName with return "". and call smart CC
    8. Replace last line of method getName with return new and call smart CC
    • EXPECTED RESULT: In step 2 all elements of type (or returning type) String are listed together with all static methods from class java.lang.String returning String
      In step 3 all methods of java.lang.String returning String are listed
      In step 4 identifier String is inserted, since it was the only possible completion in this situation (String cannot have subclasses it's final)
  2. Method Parameter
    1. open class completion.SmartParams from sample project
    2. On line 39 call smart CC for each parameter
    • EXPECTED RESULT: Only right items are suggested by CC.
  3. Catch statement
    1. Open class completion.SmartCatch from sample project
    2. Delete Exception from the catch statement and call smart CC on its place
    3. In method of throwingMethod type throw new and call smart CC
    • EXPECTED RESULT: In both cases the exceptions (and their subtypes) which can by thrown by throwingMethod are listed.
  4. Loop
    1. Open class completion.SmartLoop from sample project
    2. Inside method test type following code: for(String s :
    3. Call smart CC
    • EXPECTED RESULT: The smart CC should provide only Collection whose element type is String
  5. Smart Switch
    1. Open class completion.SmartSwitch from sample project
    2. To the switch statement type: case and call smart CC
    3. Change the control variable of switch to i
    • EXPECTED RESULT: In first case the CC returns only enum constants. In the second case it returns all int fields in actual class and static methods from java.lang.Integer returning int.
  6. Chained method
    1. Create class with following body:
      String field;
      public void test(String param) {
      String variable;
      if() {}
      }
    2. Put cursor between empty braces (where is expected boolean)
    3. Press Ctrl-Space twice
    • EXPECTED RESULT:All symbols code completion is opened. Above the line, there are methods calls which returns boolean. As param.isEmpty(), field.isEmpty(), variable.isEmpty(), getClass.isEmpty() and toString.isEmpty()

Test suite: Generate - constructor

Purpose:

Setup:

  1. Generate constructor
    1. Open java file
    2. Add several fields with various modifiers
    3. In editor press Alt-insert
    4. From popup menu select "Constructor ..."
    5. Select some fields from dialog and confirm
    • EXPECTED RESULT: A new constructor is generated with required parameters. Its body consists only from assign statements. In step 5 check that all fields are listed
  2. Generate default constructor
    1. In editor call Generate code from context menu
    2. Select "Constructor..."
    3. Leave selection empty and confirm
    • EXPECTED RESULT: New constructor is added w/o any parameters
  3. Generate constructor - context
    1. Open java file and add inner class and one top-level class
    2. Put caret in inner class (resp. top-level class)
    3. Generate constructor using "Generate Code | Constructor ..."
    • EXPECTED RESULT: Constructor is generated in right class
  4. Generate constructor - context
    1. Open java file extending Exception
    2. Put caret inside class body
    3. Call "Generate Code | Constructor ..."
    4. Select Exception(Strting message, Throwable cause) as super constructor and confirm
    • EXPECTED RESULT:Constructor is generated
          public Main(String message, Throwable cause) {
      
       super(message, cause);
   }

Test suite: Delegate method

Purpose:

Setup:

  1. Delegate Method
    1. Open class with non-primitive members
    2. Call "Generate Code|Delegate Method"
    3. In left panel select field whose method you want to delegate
    4. From right panel select required method
    • EXPECTED RESULT: In step 3 verify that all fields are listed in left panel. After selecting any item the right panel is updated, it show the hierarchical view of super-classes of selected type. When method is selected from right panel and whole dialog is confirmed the method is created in the code. In the body, there is call of selected method.
    

Test suite: Generate Getters & Setters

Purpose:

Setup:

  1. Generate setter
    1. Open java class
    2. Add several fields
    3. From context menu select "Generate Code| Setter"
    4. Select some of the listed fields and confirm
    • EXPECTED RESULT: The setters for selected fields are added
  2. Generate getter
    1. Use class from previous test case
    2. Generate getter for some fields using "Generate Code" menu
    • EXPECTED RESULT: Getters are properly added
  3. Generate getter&setter
    1. Open java files with several fields
    2. Generate Getters and Setters for some fields using "Generate Code | Getter and Setter"
    • EXPECTED RESULT: Both getter and setter is added for each selected item
  4. Existing getter or setter
    1. Open any class from previous testcases
    2. Try to generate getter or setter which already exists
    • EXPECTED RESULT: It is impossible to generate duplicate method. The field is not listed if the corresponding method already exists. For creating getter and setter at once, the field is not listed if one of the method exists.
      In case the list of available fields is empty (all fields have requested method or there are no fields) the whole menu item is missing.
  5. Getters & Setters for superclass
    1. Open java class which is extending another class
    2. Open context menu and select Generate Code | Getter (resp. Setter, resp. both)
    • EXPECTED RESULT: The list contains whole class hierarchy, it is possible to select field from super class
  6. Getters & Setters context aware
    1. Try generate Getter or Setter for inner class
    2. Try generate Getter or Setter in anonymous class
    3. Try generate Getter or Setter in non-public top-level class
    • EXPECTED RESULT: The list of available fields corresponds to the place where the action was invoked from. The methods are added to the right place

Test suite: Generate equals and hashCode

Purpose:

Setup:

  1. Equals & HashCode methods
    1. Open java class with several fields
    2. Open Generate Code menu (Alt-Insert) and select Equals and hashCode
    3. Select several fields to be included in equals() method
    4. Select several fields to be included in hashCode() method and confirm
    • EXPECTED RESULT: The equals() function and hashCode() function are generated properly.
  2. Equals
    1. Create new java class with fields String a; int b; Object c;
    2. Open Generate Code menu (Alt-Insert) and select all field for generating Equals
    • EXPECTED RESULT: The equals() function is generated, check the way various types of fields are compared
          @Override
      
   public boolean equals(Object obj) {
       if (obj == null) {
           return false;
       }
       if (getClass() != obj.getClass()) {
           return false;
       }
       final Main other = (Main) obj;
       if ((this.a == null) ? (other.a != null) : !this.a.equals(other.a)) {
           return false;
       }
       if (this.b != other.b) {
           return false;
       }
       if (this.c != other.c && (this.c == null || !this.c.equals(other.c))) {
           return false;
       }
       return true;
   }

Test suite: Override method

Purpose:

Setup:

  1. Override method
    1. Open class extending other class
    2. Open popup by pressing Alt-Insert and select Override Methods
    3. Select several methods and confirm
    • EXPECTED RESULT: Check that in step 3 all super classes are listed. After closing dialog selected methods are created in the code. In the body of each method there is super call.

Test suite: Matching Method Call Parameters

Purpose:

Setup:

  1. Matching Name
    1. Open matching.Matching from the sample project
    2. At the last line of test method type method
    3. Finish the method call from CC
    4. Open matching.Matching from the sample project
    5. At the last line of test method type setSerial
    6. Finish the method call from CC
    • EXPECTED RESULT: The CC inserts setSerial(serial); while serial (field from supertype) is of type int and is close to the name of expected parameter (int SeRiAl)

Test suite: Parameter ToolTip

Purpose: Parameter tooltip shows expected parameter (types and names) of method call. It highlights actual parameter when moving cursor within method call.
Setup:

  1. Method Call
    1. Open class tooltip.ParamToolTip from sample project
    2. In method test type method(|) , the '|' stands for cursor position
    3. Press Ctrl-P
    4. Type 1,1.2
    5. Complete method call by providing all required arguments
    6. Open class tooltip.ParamToolTip from sample project
    7. In method test type new ParamToolTip(|) , the '|' stands for cursor position
    8. Open parameter tool tip and enter parameters
    • EXPECTED RESULT: The parameter tooltip works as in previous test case
  2. Moving Among Parameters
    1. Open class tooltip.ParamToolTip from sample project
    2. In method test type method(|1,2.2,"sss", "sss", this);) , the '|' stands for cursor position
    3. Invoke parameter tooltip and browse parameters by arrow-keys
    • EXPECTED RESULT: The tooltip highlights correctly parameter under cursor.

Test suite: Generated method body customization

Purpose: This test suite describes the customization of generated method body

  1. Generated method
    1. Open Tools -> Templates
    2. In the tree select Java -> Code Snippets -> Generated Method Body
    3. Click on Open in Editor
    4. Customize the snippet that all variables are used - for example create block comment containing all the variable
    5. In code type int x = newMethod(1,"");
    6. Use hint to create new method
    • EXPECTED RESULT:New method is created respecting the code snippet, are variables are substituted by correct values
  2. Overridden method - no return type
    1. Open Tools -> Templates
    2. In the tree select Java -> Code Snippets -> Overridden Method Body
    3. Click on Open in Editor
    4. Customize the snippet that all variables are used - for example create block comment containing all the variable
    5. In the editor create new class and add extends DefaultTreeModel
    6. Use Inset Code to override nodeChanged(TreeNode) method
    • EXPECTED RESULT:New method is created respecting the code snippet, are variables are substituted by correct values
  3. Overridden method
    1. Use class from previous test case
    2. Use Insert Code to override isLeaf(Object)
    • EXPECTED RESULT:New method is created respecting the code snippet, are variables are substituted by correct values
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo