empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r749063 - in /incubator/empire-db/trunk/empire-db/src: main/java/org/apache/empire/commons/ main/java/org/apache/empire/db/expr/column/ test/java/org/apache/empire/commons/
Date Sun, 01 Mar 2009 17:24:22 GMT
Author: francisdb
Date: Sun Mar  1 17:24:22 2009
New Revision: 749063

URL: http://svn.apache.org/viewvc?rev=749063&view=rev
Log:
unit testing and code/doc cleanup for Options

Added:
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/OptionsTest.java
Modified:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBConcatExpr.java
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java?rev=749063&r1=749062&r2=749063&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java
Sun Mar  1 17:24:22 2009
@@ -50,6 +50,7 @@
     public Options()
     {
         // Default constructor
+        // TODO clean up this class as it is quite messy (add vs set, object vs key, ...)
     }
     
     public Options(Options other)
@@ -60,7 +61,9 @@
     public Options(OptionEntry [] entries)
     {
         for (int i=0; i<entries.length; i++)
+        {
             this.add(entries[i]);
+        }
     }
 
     protected int getIndex(Object value)
@@ -98,11 +101,25 @@
         return (i >= 0 ? list.get(i).getText() : EMPTY_STRING);
     }
 
+    /**
+     * Gets the value of the entry at index i
+     * 
+     * @param i the index
+     * 
+     * @return the value or <code>null</code> if not found 
+     */
     public Object getValueAt(int i)
     {
         return (i>=0 && i<list.size() ? list.get(i).getValue() : null);
     }
 
+    /**
+     * Gets the text of the entry at index i
+     * 
+     * @param i the index
+     * 
+     * @return the text or an empty String if not found 
+     */
     public String getTextAt(int i)
     {
         return (i>=0 && i<list.size() ? list.get(i).getText() : EMPTY_STRING);
@@ -116,6 +133,13 @@
         return set;
     }
 
+    /**
+     * Sets or Adds an option at a certain position
+     * 
+     * @param value the value object
+     * @param text the text
+     * @param pos the position, see {@link InsertPos}
+     */
     public void set(Object value, String text, InsertPos pos)
     {
         if (text == null)
@@ -142,20 +166,35 @@
         }
     }
 
+    /**
+     * Sets or adds Adds an option at the bottom
+     * 
+     * @param value the value object
+     * @param text the text
+     */
     public void set(Object value, String text)
     {
         set(value, text, InsertPos.Bottom);
     }
 
+    /**
+     * Adds an object, the check for an existing can be skipped for
+     * performance issues (not recommended!)
+     * 
+     * @param value the value
+     * @param text the text
+     * @param noCheck set to true to skip testing for an existing key (handle with care!)
+     */
     public void add(Object value, String text, boolean noCheck)
     {
         if (noCheck)
-        { // fast add to list, without check for existing key
-            // handle with care!
+        { 
             list.add(createOptionEntry(value, text));
         } 
         else
+        {
             set(value, text);
+        }
     }
 
     @Override
@@ -186,12 +225,6 @@
         return (getIndex(object) >= 0);
     }
 
-    public boolean containsValue(Object object)
-    {
-        // Check if exits
-        return (getIndex(object) >= 0);
-    }
-
     @Override
     public boolean isEmpty()
     {
@@ -228,12 +261,16 @@
         return list.toArray();
     }
 
+    /**
+     * Adds all these options to the xml element
+     * 
+     * @param element the element to add the option tags to
+     * @param flags not used for now
+     */
     public void addXml(Element element, long flags)
-    { // add All Options
-        Iterator<OptionEntry> i = iterator();
-        while (i.hasNext())
-        {
-            OptionEntry e = i.next();
+    { 
+        // add All Options
+        for(OptionEntry e:list){
             String value = String.valueOf(e.getValue());
             // Create Option Element
             Element opt = XMLUtil.addElement(element, "option", e.getText());

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBConcatExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBConcatExpr.java?rev=749063&r1=749062&r2=749063&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBConcatExpr.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBConcatExpr.java
Sun Mar  1 17:24:22 2009
@@ -56,11 +56,6 @@
         this.right = right;
     }
 
-    /**
-     * Returns the current DBDatabase object.
-     * 
-     * @return the current DBDatabase object
-     */
     @Override
     public DBDatabase getDatabase()
     {
@@ -68,9 +63,9 @@
     }
 
     /**
-     * Returns the data type: DT_TEXT.
+     * Returns the data type: {@link DataType#TEXT}
      * 
-     * @return the data type: DT_TEXT
+     * @return {@link DataType#TEXT}
      */
     @Override
     public DataType getDataType()
@@ -78,11 +73,6 @@
         return DataType.TEXT;
     }
 
-    /**
-     * This helper function returns expression name.
-     * 
-     * @return the expression name
-     */
     @Override
     public String getName()
     { // Get the expression name
@@ -95,7 +85,6 @@
         return name;
     }
 
-    /** This helper function calls the DBColumnExpr.addXML(Element, long) method. */
     @Override
     public Element addXml(Element parent, long flags)
     {
@@ -112,11 +101,6 @@
         return elem;
     }
 
-    /**
-     * returns null
-     * 
-     * @return null
-     */
     @Override
     public DBColumn getUpdateColumn()
     {

Added: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/OptionsTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/OptionsTest.java?rev=749063&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/OptionsTest.java
(added)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/OptionsTest.java
Sun Mar  1 17:24:22 2009
@@ -0,0 +1,341 @@
+/**
+ * 
+ */
+package org.apache.empire.commons;
+
+import static org.junit.Assert.*;
+
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.empire.commons.Options.InsertPos;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * @author francisdb
+ *
+ */
+public class OptionsTest
+{
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#size()}.
+     */
+    @Test
+    public void testSize()
+    {
+        Options options = new Options();
+        assertEquals(0, options.size());
+        options.add(new Object(), "text", true);
+        assertEquals(1, options.size());
+        options.add(new Object(), "text", true);
+        assertEquals(2, options.size());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#isEmpty()}.
+     */
+    @Test
+    public void testIsEmpty()
+    {
+        Options options = new Options();
+        assertTrue(options.isEmpty());
+        options.add(new Object(), "text", true);
+        assertFalse(options.isEmpty());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#clear()}.
+     */
+    @Test
+    public void testClear()
+    {
+        Options options = new Options();
+        options.add(new Object(), "text", true);
+        options.clear();
+        assertTrue(options.isEmpty());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#Options()}.
+     */
+    @Test
+    public void testOptions()
+    {
+        Options options = new Options();
+        assertTrue(options.isEmpty());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#Options(org.apache.empire.commons.Options)}.
+     */
+    @Test
+    public void testOptionsOptions()
+    {
+        Options options = new Options();
+        options.add(new Object(), "text", true);
+        Options options2 = new Options(options);
+        assertEquals(1, options2.size());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#Options(org.apache.empire.commons.OptionEntry[])}.
+     */
+    @Test
+    public void testOptionsOptionEntryArray()
+    {
+        Options options = new Options(new OptionEntry[]{});
+        assertTrue(options.isEmpty());
+        Options options2 = new Options(new OptionEntry[]{new OptionEntry(new Object(),"text")});
+        assertEquals(1, options2.size());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#getIndex(java.lang.Object)}.
+     */
+    @Test
+    public void testGetIndex()
+    {
+        Options options = new Options();
+        assertEquals(-1, options.getIndex(new Object()));
+        options.add(Integer.valueOf(123), "text", true);
+        options.add(Integer.valueOf(456), "text", true);
+        assertEquals(0, options.getIndex(Integer.valueOf(123)));
+        assertEquals(1, options.getIndex(Integer.valueOf(456)));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#createOptionEntry(java.lang.Object,
java.lang.String)}.
+     */
+    @Test
+    public void testCreateOptionEntry()
+    {
+        Options options = new Options();
+        OptionEntry entry = options.createOptionEntry(Integer.valueOf(123), "test");
+        assertEquals("test", entry.getText());
+        assertEquals(Integer.valueOf(123), entry.getValue());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#getEntry(java.lang.Object)}.
+     */
+    @Test
+    public void testGetEntry()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf("123"), "text", true);
+        OptionEntry entry = options.getEntry(Integer.valueOf("123"));
+        assertNotNull(entry);
+        assertEquals("text", entry.getText());
+        assertEquals(Integer.valueOf(123), entry.getValue());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#get(java.lang.Object)}.
+     */
+    @Test
+    public void testGet()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf("123"), "text", true);
+        String text = options.get(Integer.valueOf("123"));
+        assertNotNull(text);
+        assertEquals("text", text);
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#getValueAt(int)}.
+     */
+    @Test
+    public void testGetValueAt()
+    {
+        Options options = new Options();
+        assertEquals(null, options.getValueAt(10));
+        options.add(Integer.valueOf(123), "text", true);
+        options.add(Integer.valueOf(456), "text2", true);
+        assertEquals(Integer.valueOf(456), options.getValueAt(1));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#getTextAt(int)}.
+     */
+    @Test
+    public void testGetTextAt()
+    {
+        Options options = new Options();
+        assertEquals("", options.getTextAt(10));
+        options.add(Integer.valueOf(123), "text", true);
+        options.add(Integer.valueOf(456), "text2", true);
+        assertEquals("text2", options.getTextAt(1));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#getValues()}.
+     */
+    @Test
+    public void testGetValues()
+    {
+        Options options = new Options();
+        assertEquals(0, options.getValues().size());
+        options.add(Integer.valueOf(123), "text", true);
+        options.add(Integer.valueOf(456), "text2", true);
+        Set<Object> values = options.getValues();
+        assertTrue(values.contains(Integer.valueOf(123)));
+        assertTrue(values.contains(Integer.valueOf(456)));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#set(java.lang.Object, java.lang.String,
org.apache.empire.commons.Options.InsertPos)}.
+     */
+    @Test
+    public void testSetObjectStringInsertPos()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(123), "text", true);
+        options.set(Integer.valueOf(456), "text2", InsertPos.Top);
+        options.set(Integer.valueOf(789), "text3", InsertPos.Bottom);
+        assertEquals(Integer.valueOf(456), options.getValueAt(0));
+        assertEquals(Integer.valueOf(789), options.getValueAt(2));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#set(java.lang.Object, java.lang.String)}.
+     */
+    @Test
+    public void testSetObjectString()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(1), "text", true);
+        options.set(Integer.valueOf(2), "text2");
+        assertEquals(Integer.valueOf(2), options.getValueAt(1));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#add(java.lang.Object, java.lang.String,
boolean)}.
+     */
+    @Test
+    public void testAddObjectStringBoolean()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(1), "text", true);
+        assertEquals(Integer.valueOf(1), options.getValueAt(0));
+        options.add(Integer.valueOf(1), "text", true);
+        assertEquals(Integer.valueOf(1), options.getValueAt(1));
+        
+        options = new Options();
+        options.add(Integer.valueOf(1), "text", false);
+        assertEquals(Integer.valueOf(1), options.getValueAt(0));
+        options.add(Integer.valueOf(1), "text2", false);
+        assertEquals(null, options.getValueAt(1));
+        assertEquals("text2", options.getTextAt(0));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#add(org.apache.empire.commons.OptionEntry)}.
+     */
+    @Test
+    public void testAddOptionEntry()
+    {
+        Options options = new Options();
+        options.add(new OptionEntry(Integer.valueOf(1),"test1"));
+        options.add(new OptionEntry(Integer.valueOf(1),"test1-2"));
+        options.add(new OptionEntry(Integer.valueOf(3),"test3"));
+        assertEquals("test1-2", options.getTextAt(0));
+        assertEquals("test3", options.getTextAt(1));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#contains(java.lang.Object)}.
+     */
+    @Test
+    public void testContainsObject()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(1), "txt", false);
+        options.add(Integer.valueOf(2), "txt", false);
+        assertTrue(options.contains(Integer.valueOf(1)));
+        assertTrue(options.contains(Integer.valueOf(2)));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#iterator()}.
+     */
+    @Test
+    public void testIterator()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(1), "txt", false);
+        options.add(Integer.valueOf(2), "txt2", false);
+        Iterator<OptionEntry> it = options.iterator();
+        it.next();
+        assertEquals("txt2", it.next().getText());
+        it.remove();
+        assertEquals(1, options.size());
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#remove(java.lang.Object)}.
+     */
+    @Test
+    public void testRemoveObject()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(1), "txt", false);
+        options.add(Integer.valueOf(2), "txt2", false);
+        options.remove(Integer.valueOf(1));
+        assertFalse(options.contains(Integer.valueOf(1)));
+        assertTrue(options.contains(Integer.valueOf(2)));
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#toArray()}.
+     */
+    @Test
+    public void testToArray()
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(1), "txt", false);
+        options.add(Integer.valueOf(2), "txt2", false);
+        assertEquals(2, options.toArray().length);
+    }
+
+    /**
+     * Test method for {@link org.apache.empire.commons.Options#addXml(org.w3c.dom.Element,
long)}.
+     * @throws ParserConfigurationException 
+     */
+    @Test
+    public void testAddXml() throws ParserConfigurationException
+    {
+        Options options = new Options();
+        options.add(Integer.valueOf(1), "txt", false);
+        options.add(Integer.valueOf(2), "txt2", false);
+
+        
+        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        Document doc = builder.newDocument();
+        Element root = doc.createElement("root");
+        doc.appendChild(root);
+
+        // TODO get rid of flags param??
+        options.addXml(root, 0);
+        Node node = root.getFirstChild();
+        assertNotNull("no child was added", node);
+        assertEquals("option", node.getNodeName());
+        assertEquals("1", node.getAttributes().getNamedItem("value").getNodeValue());
+        assertEquals("txt", node.getTextContent());
+        
+        node = node.getNextSibling();
+        assertNotNull("no child was added", node);
+        assertEquals("option", node.getNodeName());
+        assertEquals("2", node.getAttributes().getNamedItem("value").getNodeValue());
+        assertEquals("txt2", node.getTextContent());
+    }
+
+}

Modified: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java?rev=749063&r1=749062&r2=749063&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java
Sun Mar  1 17:24:22 2009
@@ -40,7 +40,7 @@
 	@Test
 	public void testToStringObject()
 	{
-		assertEquals(null,StringUtils.toString(null));
+		assertEquals(null,StringUtils.toString((Object)null));
 		assertEquals("test",StringUtils.toString("test"));
 		assertEquals(Boolean.FALSE.toString(),StringUtils.toString(Boolean.FALSE));
 	}



Mime
View raw message