001    /* ====================================================================
002     * The Apache Software License, Version 1.1
003     *
004     * Copyright (c) 2003 The Apache Software Foundation.  All rights
005     * reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     *
011     * 1. Redistributions of source code must retain the above copyright
012     *    notice, this list of conditions and the following disclaimer.
013     *
014     * 2. Redistributions in binary form must reproduce the above copyright
015     *    notice, this list of conditions and the following disclaimer in
016     *    the documentation and/or other materials provided with the
017     *    distribution.
018     *
019     * 3. The end-user documentation included with the redistribution,
020     *    if any, must include the following acknowledgment:
021     *       "This product includes software developed by the
022     *        Apache Software Foundation (http://www.apache.org/)."
023     *    Alternately, this acknowledgment may appear in the software itself,
024     *    if and wherever such third-party acknowledgments normally appear.
025     *
026     * 4. The names "The Jakarta Project", "Commons", and "Apache Software
027     *    Foundation" must not be used to endorse or promote products derived
028     *    from this software without prior written permission. For written
029     *    permission, please contact apache@apache.org.
030     *
031     * 5. Products derived from this software may not be called "Apache",
032     *    nor may "Apache" appear in their name, without prior written
033     *    permission of the Apache Software Foundation.
034     *
035     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038     * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046     * SUCH DAMAGE.
047     * ====================================================================
048     *
049     * This software consists of voluntary contributions made by many
050     * individuals on behalf of the Apache Software Foundation.  For more
051     * information on the Apache Software Foundation, please see
052     * <http://www.apache.org/>.
053     *
054     */
055    package org.jpu.patterns.proxy.test;
056    
057    import java.io.ObjectInputStream;
058    import java.io.ObjectOutputStream;
059    import java.lang.reflect.Proxy;
060    import java.util.HashMap;
061    import java.util.Map;
062    
063    import org.apache.commons.beanutils.PropertyUtils;
064    import org.jpu.patterns.common.JPUBeanUtils;
065    import org.jpu.patterns.proxy.IProxyBean;
066    import org.jpu.patterns.proxy.ProxyBean;
067    
068    public class ProxyBeanTest {
069        public static void main(String[] argv) throws Throwable {
070            int n = Integer.parseInt( argv[0] );
071            String which = argv[1];
072            boolean trackChanges = which.equals("pojoTrackingChanges");
073            System.out.println( "trackChanges=" + trackChanges );
074            System.out.println( "which=" + which );
075            System.out.println( "n=" + n );
076    
077            Account[] accts = new Account[100];
078            for( int i = 0; i < accts.length; i++ ) {
079                Account acct = null;
080                if ( which.startsWith("pojo") ) {
081                    acct = (Account)new AccountImpl(trackChanges).newProxy( new Class[] { Account.class, IProxyBean.class } );
082                }
083                else if ( which.equals("proxy") ) {
084                    ProxyAccountImpl ih = new ProxyAccountImpl();
085                    acct = (Account)Proxy.newProxyInstance(
086                        Account.class.getClassLoader(), 
087                        new Class[] { Account.class },
088                        ih ); 
089                }
090                else if ( which.equals("jpuproxy") ) {
091                    JPUProxyAccountImpl ih = new JPUProxyAccountImpl();
092                    acct = (Account)ih.newProxy( new Class[] { Account.class } ); 
093                }
094                else {
095                    acct = new HardCodedAccountImpl();
096                }
097                accts[i] = acct;
098                
099            }
100    
101            long start = System.currentTimeMillis();
102            if ( which.equals("pojoTrackingChanges") ) {
103                for( int i = 0; i < n; i++ ) {
104                    Account acct = accts[i%accts.length];
105                    IProxyBean pojo = (IProxyBean)acct;
106                    pojo.clearChangedFlags();
107                    acct.setId( new Integer(i) );
108                    Integer storedId = acct.getId();
109                    if ( storedId.intValue() != i ) {
110                        System.err.println( "Failure!" );
111                    }
112                    System.out.println( "Changed attributes: " + pojo.getChangedAttributes() );
113                }   
114            }
115            else if ( which.equals("pojo") || which.equals("proxy") || which.equals("jpuproxy") ) {
116                for( int i = 0; i < n; i++ ) {
117                    Account acct = accts[i%accts.length];
118                    acct.setId( new Integer(i) );
119                    Integer storedId = acct.getId();
120                    if ( storedId.intValue() != i ) {
121                        System.err.println( "Failure!" );
122                    }
123                }   
124            }
125            else if ( which.equals("bean") ) {
126                for( int i = 0; i < n; i++ ) {
127                    Account acct = accts[i%accts.length];
128                    PropertyUtils.setSimpleProperty( acct, "id", new Integer(i) );
129    
130                    Integer storedId = (Integer)PropertyUtils.getSimpleProperty( acct, "id" );
131                    if ( storedId.intValue() != i ) {
132                        System.err.println( "Failure!" );
133                    }
134                }   
135            }
136            else if ( which.equals("map") ) {
137                Map map = new HashMap();
138                for( int i = 0; i < n; i++ ) {
139                    map.put( "id", new Integer(i) );
140                    Integer storedId = (Integer)map.get("id");
141                    if ( storedId.intValue() != i ) {
142                        System.err.println( "Failure!" );
143                    }
144                }   
145            }
146            else if ( which.equals("hardcoded") ) {
147                for( int i = 0; i < n; i++ ) {
148                    Account acct = accts[i%accts.length];
149                    acct.setId( new Integer(i) );
150                    Integer storedId = acct.getId();
151                    if ( storedId.intValue() != i ) {
152                        System.err.println( "Failure!" );
153                    }
154                }   
155            }
156            else {
157                throw new Exception( "Unknown test '" + which + "'" );
158            }
159            long end = System.currentTimeMillis();
160            long elapsed = end - start;
161            double rate = (double)n / (double)elapsed;
162            rate *= 1000;
163            System.out.println( "ProxyBean.hits = " + ProxyBean.getCacheHits() );
164            System.out.println( "ProxyBean.misses = " + ProxyBean.getCacheMisses() );
165            System.out.println( "JPUBeanUtils.hits = " + JPUBeanUtils.getCacheHits() );
166            System.out.println( "JPUBeanUtils.misses = " + JPUBeanUtils.getCacheMisses() );
167            System.out.println( "# accts = " + accts.length );
168            System.out.println( "rate = " + rate + " cycles/sec" );
169    
170            {
171                Account acct1 = (Account)new AccountImpl(false).newProxy( new Class[] { Account.class, IProxyBean.class } );
172                acct1.setId( new Integer(1) );
173                assertTrue( acct1.getId().equals( new Integer(1) ) );
174                Account acct2 = (Account)new AccountImpl(false).newProxy( new Class[] { Account.class, IProxyBean.class } );
175                acct2.setId( new Integer(2) );
176                assertTrue( acct2.getId().equals( new Integer(2) ) );
177    
178                ((IProxyBean)acct1).copyTo(acct2);
179                assertTrue( acct2.getId().equals( new Integer(1) ) );
180    
181                java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
182                ObjectOutputStream oos = new ObjectOutputStream(buf);
183    
184                System.out.println( "Serializing account '" + acct1.getId() + "'" );
185                oos.writeObject(acct1);
186                oos.close();
187                buf.close();
188    
189                java.io.ByteArrayInputStream inbuf = new java.io.ByteArrayInputStream( buf.toByteArray() );
190                ObjectInputStream ois = new ObjectInputStream(inbuf);
191    
192                System.out.println( "Deserializing account." );
193                Account newAcct = (Account)ois.readObject();
194    
195                System.out.println( "Read account '" + newAcct.getId() + "' successfully." );
196            }
197        }
198    
199        private static void assertTrue(boolean b) {
200            if ( ! b ) {
201                System.out.println( "Assertion failure at test #" + testnum );
202            }
203            else {
204                System.out.println( "Assertion #" + testnum + " succeeded." );
205            }
206            testnum++;
207        }
208        private static int testnum = 0;
209    }
210