home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (ObservableListTest.groovy)

This example Groovy source code file (ObservableListTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

bean, groovytestcase, javabean, observablelist, observablelist, observablelisttest, observablelisttest, propertychangeevent, propertychangeevent, propertychangelistener, samplelistpropertychangelistener, samplelistpropertychangelistener, string

The Groovy ObservableListTest.groovy source code

/*
 * Copyright 2003-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package groovy.util

import java.beans.PropertyChangeEvent
import java.beans.PropertyChangeListener

/**
 * @author <a href="mailto:aalmiray@users.sourceforge.net">Andres Almiray
 */
class ObservableListTest extends GroovyTestCase {
   void testFireEvent_add_withoutTest(){
      def list = new ObservableList()
      def contentListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.CONTENT_PROPERTY, contentListener )
      def sizeListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.SIZE_PROPERTY, sizeListener )

      def value1 = 'value1'
      def value2 = 'value2'

      list << value1
      assertNotNull( contentListener.event )
      assertEquals( list, contentListener.event.source )
      assertNull( contentListener.event.oldValue )
      assertEquals( value1, contentListener.event.newValue )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 0i, sizeListener.event.oldValue )
      assertEquals( 1i, sizeListener.event.newValue )

      list << value2
      assertNotNull( contentListener.event )
      assertEquals( list, contentListener.event.source )
      assertNull( contentListener.event.oldValue )
      assertEquals( value2, contentListener.event.newValue )

      contentListener.event = null
      list[1] = value1
      assertNotNull( contentListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( value2, contentListener.event.oldValue )
      assertEquals( value1, contentListener.event.newValue )
      assertEquals( 1, contentListener.event.index )

      contentListener.event = null
      list[0] = value1
      assertNull( contentListener.event )
   }

   void testFireEvent_remove(){
      def list = new ObservableList()
      def contentListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.CONTENT_PROPERTY, contentListener )
      def sizeListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.SIZE_PROPERTY, sizeListener )

      def value1 = 'value1'
      def value2 = 'value2'

      list << value1
      assertNotNull( contentListener.event )
      assertEquals( list, contentListener.event.source )
      assertNull( contentListener.event.oldValue )
      assertEquals( value1, contentListener.event.newValue )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 0i, sizeListener.event.oldValue )
      assertEquals( 1i, sizeListener.event.newValue )

      list << value2
      assertNotNull( contentListener.event )
      assertEquals( list, contentListener.event.source )
      assertNull( contentListener.event.oldValue )
      assertEquals( value2, contentListener.event.newValue )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 1i, sizeListener.event.oldValue )
      assertEquals( 2i, sizeListener.event.newValue )

      list.remove(value2)
      assertNotNull( contentListener.event )
      assertTrue( contentListener.event instanceof ObservableList.ElementRemovedEvent )
      assertEquals( list, contentListener.event.source )
      assertEquals( value2, contentListener.event.oldValue )
      assertEquals( 1, contentListener.event.index )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 2i, sizeListener.event.oldValue )
      assertEquals( 1i, sizeListener.event.newValue )
      
      list.remove(value1)
      assertNotNull( contentListener.event )
      assertTrue( contentListener.event instanceof ObservableList.ElementRemovedEvent )
      assertEquals( list, contentListener.event.source )
      assertEquals( value1, contentListener.event.oldValue )
      assertEquals( 0, contentListener.event.index )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 1i, sizeListener.event.oldValue )
      assertEquals( 0i, sizeListener.event.newValue )
   }

   void testFireEvent_clear(){
      def list = new ObservableList()
      def contentListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.CONTENT_PROPERTY, contentListener )
      def sizeListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.SIZE_PROPERTY, sizeListener )

      def value1 = 'value1'
      def value2 = 'value2'
      list << value1
      list << value2

      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 1i, sizeListener.event.oldValue )
      assertEquals( 2i, sizeListener.event.newValue )

      list.clear()

      assertNotNull( contentListener.event )
      assertTrue( contentListener.event instanceof ObservableList.ElementClearedEvent )
      assertEquals( list, contentListener.event.source )
      def values = contentListener.event.values
      assertNotNull( values )
      assertEquals( 2, values.size() )
      assertEquals( value1, values[0] )
      assertEquals( value2, values[1] )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 2i, sizeListener.event.oldValue )
      assertEquals( 0i, sizeListener.event.newValue )
   }

   void testFireEvent_addAll(){
      def list = new ObservableList()
      def contentListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.CONTENT_PROPERTY, contentListener )
      def sizeListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.SIZE_PROPERTY, sizeListener )

      def value1 = 'value1'
      def value2 = 'value2'
      list << value1

      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 0i, sizeListener.event.oldValue )
      assertEquals( 1i, sizeListener.event.newValue )

      list.addAll( [value1, value2] )

      assertNotNull( contentListener.event )
      assertTrue( contentListener.event instanceof ObservableList.MultiElementAddedEvent )
      assertEquals( list, contentListener.event.source )
      def values = contentListener.event.values
      assertNotNull( values )
      assertEquals( 2, values.size() )
      assertEquals( value1, values[0] )
      assertEquals( value2, values[1] )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 1i, sizeListener.event.oldValue )
      assertEquals( 3i, sizeListener.event.newValue )
   }
   
   void testFireEvent_removeAll(){
      def list = new ObservableList()
      def contentListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.CONTENT_PROPERTY, contentListener )
      def sizeListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.SIZE_PROPERTY, sizeListener )

      def value1 = 'value1'
      def value2 = 'value2'
      list << value1
      list << value2

      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 1i, sizeListener.event.oldValue )
      assertEquals( 2i, sizeListener.event.newValue )

      list.removeAll( [value2] )

      assertNotNull( contentListener.event )
      assertTrue( contentListener.event instanceof ObservableList.MultiElementRemovedEvent )
      assertEquals( list, contentListener.event.source )
      def values = contentListener.event.values
      assertNotNull( values )
      assertEquals( 1, values.size() )
      assertEquals( value2, values[0] )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 2i, sizeListener.event.oldValue )
      assertEquals( 1i, sizeListener.event.newValue )
   }
   
   void testFireEvent_retainAll(){
      def list = new ObservableList()
      def contentListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.CONTENT_PROPERTY, contentListener )
      def sizeListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.SIZE_PROPERTY, sizeListener )

      def value1 = 'value1'
      def value2 = 'value2'
      def value3 = 'value3'
      list << value1
      list << value2
      list << value3

      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 2i, sizeListener.event.oldValue )
      assertEquals( 3i, sizeListener.event.newValue )

      list.retainAll( [value2] )

      assertNotNull( contentListener.event )
      assertTrue( contentListener.event instanceof ObservableList.MultiElementRemovedEvent )
      assertEquals( list, contentListener.event.source )
      def values = contentListener.event.values
      assertNotNull( values )
      assertEquals( 2, values.size() )
      assertEquals( value1, values[0] )
      assertEquals( value3, values[1] )
      assertNotNull( sizeListener.event )
      assertEquals( list, contentListener.event.source )
      assertEquals( 3i, sizeListener.event.oldValue )
      assertEquals( 1i, sizeListener.event.newValue )
   }

   void testFireEvent_withTest(){
      def list = new ObservableList( { !(it instanceof String) } )
      def contentListener = new SampleListPropertyChangeListener()
      list.addPropertyChangeListener( ObservableList.CONTENT_PROPERTY, contentListener )

      def value1 = 1
      def value2 = 'value2'
      list << value1
      assertNotNull( contentListener.event )
      assertEquals( list, contentListener.event.source )
      assertNull( contentListener.event.oldValue )
      assertEquals( value1, contentListener.event.newValue )

      contentListener.event = null
      list << value2
      assertNull( contentListener.event )
   }
}

class SampleListPropertyChangeListener implements PropertyChangeListener {
   PropertyChangeEvent event

   public void propertyChange( PropertyChangeEvent evt ){
      event = evt
   }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ObservableListTest.groovy source code file:

new blog posts

 

Copyright 1998-2013 Alvin Alexander, alvinalexander.com
All Rights Reserved.