directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vte...@apache.org
Subject svn commit: r109586 - in incubator/directory/janus/trunk/jdbc/src: java/org/apache/janus/authentication/realm test/org/apache/janus/authentication/realm
Date Thu, 02 Dec 2004 23:20:06 GMT
Author: vtence
Date: Thu Dec  2 15:20:05 2004
New Revision: 109586

URL: http://svn.apache.org/viewcvs?view=rev&rev=109586
Log:
Extracted interfaces that should later move to core package. Refactored some more
Added:
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpression.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpressionBuilder.java
Removed:
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Expression.java
Modified:
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java
   incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java
   incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
   incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java
   incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java
Thu Dec  2 15:20:05 2004
@@ -17,45 +17,36 @@
 package org.apache.janus.authentication.realm;
 
 import java.util.Collection;
-import java.util.ArrayList;
-import java.util.Arrays;
 
-public class AndExpression implements Expression
+public class AndExpression implements SQLExpression
 {
-    private Expression m_left;
-    private Expression m_right;
+    private SQLExpression m_left;
+    private SQLExpression m_right;
 
-    public AndExpression( Expression left, Expression right )
+    public AndExpression( Object left, Object right )
     {
-        m_left = left;
-        m_right = right;
+        if (!(left instanceof SQLExpression)) throw new IllegalArgumentException( "Expecting
SQLExpression; unsupported: " + left.getClass().getName() );
+        if (!(right instanceof SQLExpression)) throw new IllegalArgumentException( "Expecting
SQLExpression; unsupported: " + right.getClass().getName() );
+        m_left = (SQLExpression) left;
+        m_right = (SQLExpression) right;
     }
 
-    public String getQueryString()
+    public void appendQueryString( StringBuffer query )
     {
-        StringBuffer fragment = new StringBuffer();
-        fragment.append( m_left.getQueryString() );
-        fragment.append( " " ).append( operand() ).append( " ");
-        fragment.append( m_right.getQueryString());
-
-        return fragment.toString();
-    }
-
-    public int getParameterCount()
-    {
-        return m_left.getParameterCount() + m_right.getParameterCount();
+        query.append( "(" );
+        m_left.appendQueryString( query );
+        query.append( " " ).append( operator() ).append( " " );
+        m_right.appendQueryString( query );
+        query.append( ")" );
     }
 
-    public String[] getParameterValues()
+    public void bindParameters( Collection parameters )
     {
-        Collection parameters = new ArrayList( );
-        parameters.addAll( Arrays.asList( m_left.getParameterValues() ) );
-        parameters.addAll( Arrays.asList( m_right.getParameterValues() ) );
-        
-        return ( String[] ) parameters.toArray( new String[parameters.size()] );
+        m_left.bindParameters( parameters );
+        m_right.bindParameters( parameters );
     }
 
-    protected String operand()
+    private String operator()
     {
         return "and";
     }

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java
Thu Dec  2 15:20:05 2004
@@ -16,7 +16,9 @@
  */
 package org.apache.janus.authentication.realm;
 
-public class EqExpression implements Expression
+import java.util.Collection;
+
+public class EqExpression implements SQLExpression
 {
     private final Column m_column;
     private final Object m_value;
@@ -27,27 +29,19 @@
         m_value = value;
     }
 
-    public String getQueryString()
-    {
-        StringBuffer statement = new StringBuffer();
-        statement.append( m_column.getName() );
-        statement.append( operand() );
-        statement.append( "?" );
-
-        return statement.toString();
-    }
-
-    public int getParameterCount()
+    public void appendQueryString( StringBuffer query )
     {
-        return 1;
+        query.append( m_column.getName() );
+        query.append( operator() );
+        query.append( "?" );
     }
 
-    public String[] getParameterValues()
+    public void bindParameters( Collection parameters )
     {
-        return new String[] { m_column.getValue( m_value ) };
+        parameters.add( m_column.getValue( m_value ) );
     }
 
-    protected String operand()
+    protected String operator()
     {
         return "=";
     }

Deleted: /incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Expression.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Expression.java?view=auto&rev=109585
==============================================================================

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java
Thu Dec  2 15:20:05 2004
@@ -16,22 +16,9 @@
  */
 package org.apache.janus.authentication.realm;
 
-public class ExpressionBuilder
+public interface ExpressionBuilder
 {
-    private final Table m_table;
+    public Object eq( Class type, Object value );
 
-    public ExpressionBuilder( Table table )
-    {
-        m_table = table;
-    }
-
-    public Expression eq( Class type, Object value )
-    {
-        return new EqExpression( m_table.getColumn( type ), value );
-    }
-
-    public Expression and( Expression left, Expression right )
-    {
-        return new AndExpression( left, right );
-    }
+    public Object and( Object left, Object right );
 }

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java
Thu Dec  2 15:20:05 2004
@@ -21,7 +21,6 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
 
@@ -36,8 +35,9 @@
         m_expressions = new ArrayList();
     }
 
-    public void addExpression( Expression expression )
+    public void addExpression( Object expression )
     {
+        if (!(expression instanceof SQLExpression)) throw new IllegalArgumentException( "Expecting
SQLExpression; unsupported: " + expression.getClass().getName() );
         m_expressions.add( expression );
     }
 
@@ -57,8 +57,8 @@
         Collection parameters = new ArrayList();
         for ( Iterator it = m_expressions.iterator(); it.hasNext(); )
         {
-            Expression expression = ( Expression ) it.next();
-            parameters.addAll( Arrays.asList( expression.getParameterValues( ) ) );
+            SQLExpression expression = ( SQLExpression ) it.next();
+            expression.bindParameters( parameters );
         }
 
         return ( String[] ) parameters.toArray( new String[parameters.size()] );
@@ -66,17 +66,17 @@
 
     private String buildQueryString()
     {
-        StringBuffer statement = new StringBuffer();
-        statement.append( "select * from " ).append( m_table.getName() ).append( " where
" );
+        StringBuffer query = new StringBuffer();
+        query.append( "select * from " ).append( m_table.getName() ).append( " where " );
 
         for ( Iterator it = m_expressions.iterator(); it.hasNext(); )
         {
-            Expression expression = ( Expression ) it.next();
-            statement.append( expression.getQueryString() );
-            if ( it.hasNext() ) statement.append( " and " );
+            SQLExpression expression = ( SQLExpression ) it.next();
+            expression.appendQueryString( query );
+            if ( it.hasNext() ) query.append( " and " );
         }
 
-        return statement.toString();
+        return query.toString();
     }
 
     private void bindParameterValues( PreparedStatement statement, String[] parameters )
throws SQLException

Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpression.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpression.java?view=auto&rev=109586
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpression.java
Thu Dec  2 15:20:05 2004
@@ -0,0 +1,26 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   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 org.apache.janus.authentication.realm;
+
+import java.util.Collection;
+
+public interface SQLExpression
+{
+    void appendQueryString( StringBuffer query );
+
+    void bindParameters( Collection parameters );
+}

Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpressionBuilder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpressionBuilder.java?view=auto&rev=109586
==============================================================================
--- (empty file)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SQLExpressionBuilder.java
Thu Dec  2 15:20:05 2004
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   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 org.apache.janus.authentication.realm;
+
+public class SQLExpressionBuilder implements ExpressionBuilder
+{
+    private final Table m_table;
+
+    public SQLExpressionBuilder( Table table )
+    {
+        m_table = table;
+    }
+
+    public Object eq( Class type, Object value )
+    {
+        return new EqExpression( m_table.getColumn( type ), value );
+    }
+
+    public Object and( Object left, Object right )
+    {
+        return new AndExpression( left, right );
+    }
+}

Modified: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java
Thu Dec  2 15:20:05 2004
@@ -18,5 +18,5 @@
 
 public interface Selector
 {
-    void addExpression( Expression expression );
+    void addExpression( Object expression );
 }

Modified: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java
Thu Dec  2 15:20:05 2004
@@ -47,7 +47,7 @@
     {
         JDBCSelector selector = new JDBCSelector( Tables.users() );
         SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.joe() );
-        builder.build( new ExpressionBuilder( Tables.users() ), selector );
+        builder.build( new SQLExpressionBuilder( Tables.users() ), selector );
 
         assertTrue( m_realm.find( selector ) );
     }
@@ -57,7 +57,7 @@
         JDBCSelector selector = new JDBCSelector( undefinedTable() );
 
         SameCredentialsBuilder builder = new SameCredentialsBuilder( Creds.joe() );
-        builder.build( new ExpressionBuilder(Tables.users()), selector );
+        builder.build( new SQLExpressionBuilder(Tables.users()), selector );
 
         try
         {

Modified: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java
Thu Dec  2 15:20:05 2004
@@ -69,4 +69,17 @@
                 new EqExpression( Columns.password(), "foo" ) ) );
         assertTrue( m_selector.select( m_connection ) );
     }
+
+    public void testRejectsNonSQLExpressions()
+    {
+        try
+        {
+            m_selector.addExpression( new Object() );
+            fail( "Unsupported expression type accepted" );
+        }
+        catch ( IllegalArgumentException expected )
+        {
+            assertTrue( true );
+        }
+    }    
 }

Modified: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java?view=diff&rev=109586&p1=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java&r1=109585&p2=incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java&r2=109586
==============================================================================
--- incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java
(original)
+++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java
Thu Dec  2 15:20:05 2004
@@ -44,7 +44,7 @@
 
         mockSelector.expects( once() ).method( "addExpression" ).with( eq( new EqExpression(
Columns.username(), "joe" ) ) );
         mockSelector.expects( once() ).method( "addExpression" ).with( eq( new EqExpression(
Columns.password(), "foo" ) ) );
-        m_builder.build( new ExpressionBuilder( Tables.users() ), ( Selector ) mockSelector.proxy()
);
+        m_builder.build( new SQLExpressionBuilder( Tables.users() ), ( Selector ) mockSelector.proxy()
);
         mockSelector.verify();
     }
 }

Mime
View raw message