[jbosscache-commits] JBoss Cache SVN: r5309 - in amazon-s3/trunk: src/main/java/com/amazon/s3 and 2 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Feb 5 22:09:24 EST 2008


Author: genman
Date: 2008-02-05 22:09:24 -0500 (Tue, 05 Feb 2008)
New Revision: 5309

Removed:
   amazon-s3/trunk/src/test/java/com/amazon/s3/S3Driver.java
Modified:
   amazon-s3/trunk/pom.xml
   amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/GetResponse.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/ListResponse.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/LocationResponse.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/S3Object.java
   amazon-s3/trunk/src/main/java/com/amazon/s3/UrlEncoder.java
   amazon-s3/trunk/src/main/java/com/amazon/thirdparty/Base64.java
   amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java
Log:
Additional code clean-up

Modified: amazon-s3/trunk/pom.xml
===================================================================
--- amazon-s3/trunk/pom.xml	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/pom.xml	2008-02-06 03:09:24 UTC (rev 5309)
@@ -1,9 +1,39 @@
-<?xml version="1.0" encoding="UTF-8"?><project>
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.amazon</groupId>
   <artifactId>amazon-s3-rest</artifactId>
   <version>1.0.0.0-SNAPSHOT</version>
   <description>
     Cleaned-up version of the Amazon S3 REST access layer 
-  </description>
+  </description>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemProperties>
+            <property>
+              <name>accessKey</name>
+              <value>TODO</value>
+            </property>
+            <property>
+              <name>secretKey</name>
+              <value>TODO</value>
+            </property>
+          </systemProperties>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
 </project>
\ No newline at end of file

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -11,7 +11,6 @@
 
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.Iterator;
 import java.util.Map;
 
 /**
@@ -21,9 +20,9 @@
  */
 public abstract class CallingFormat {
 
-	protected static CallingFormat pathCallingFormat = new PathCallingFormat();
-	protected static CallingFormat subdomainCallingFormat = new SubdomainCallingFormat();
-	protected static CallingFormat vanityCallingFormat = new VanityCallingFormat();
+	private final static CallingFormat pathCallingFormat = new PathCallingFormat();
+	private final static CallingFormat subdomainCallingFormat = new SubdomainCallingFormat();
+	private final static CallingFormat vanityCallingFormat = new VanityCallingFormat();
 
 	public abstract boolean supportsLocatedBuckets();
 
@@ -45,8 +44,8 @@
 	    String argumentValue;
 	    boolean firstRun = true;
 	    if (pathArgs != null) {
-	        for (Iterator<String> argumentIterator = pathArgs.keySet().iterator(); argumentIterator.hasNext(); ) {
-	            String argument = argumentIterator.next();
+	        for (Map.Entry<String, String> me : pathArgs.entrySet()) {
+	            String argument = me.getKey();
 	            if (firstRun) {
 	                firstRun = false; 
 	                pathArgsString.append("?");
@@ -54,7 +53,7 @@
 	                pathArgsString.append("&");
 	            } 
 	            
-	            argumentValue = pathArgs.get(argument);
+	            argumentValue = me.getValue();
 	            pathArgsString.append(argument);
 	            if (argumentValue != null) {
 	                pathArgsString.append("=");

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -3,7 +3,6 @@
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.NoSuchAlgorithmException;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -50,8 +49,8 @@
 	public static String make(Method method, Bucket bucket, String key, Map<String, String> pathArgs, 
 	                                         Headers headers, String expires)
 	{
-	    StringBuilder buf = new StringBuilder();
-	    buf.append(method + "\n");
+	    StringBuilder buf = new StringBuilder(128);
+	    buf.append(method.name()).append("\n");
 	
 	    // Add all interesting headers to a list, then sort them.  "Interesting"
 	    // is defined as Content-MD5, Content-Type, Date, and x-amz-
@@ -92,12 +91,12 @@
 	    }
 	
 	    // Finally, add all the interesting headers (i.e.: all that startwith x-amz- ;-))
-	    for (Iterator<String> i = interestingHeaders.keySet().iterator(); i.hasNext(); ) {
-	        String headerKey = i.next();
+	    for (Map.Entry<String, String> me : interestingHeaders.entrySet()) {
+	        String headerKey = me.getKey();
 	        if (headerKey.startsWith(AMAZON_HEADER_PREFIX)) {
-	            buf.append(headerKey).append(':').append(interestingHeaders.get(headerKey));
+	            buf.append(headerKey).append(':').append(me.getValue());
 	        } else {
-	            buf.append(interestingHeaders.get(headerKey));
+	            buf.append(me.getValue());
 	        }
 	        buf.append("\n");
 	    }

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -254,8 +254,11 @@
         return new Response(request);
     }
     
-    public Response put(Bucket bucket, String key, S3Object object) {
-    	return put(bucket, key, object);
+    /**
+     * Writes an object to S3.
+     */
+    public Response put(Bucket bucket, String key, S3Object object) throws IOException {
+    	return put(bucket, key, object, null);
     }
 
     /**
@@ -271,9 +274,12 @@
         return new GetResponse(makeRequest(Method.GET, bucket, key, null, headers));
     }
     
-    public GetResponse get(Bucket bucket, String key)
+    /**
+     * Reads an object from S3.
+     */
+    public GetResponse get(Bucket bucket, String key) throws IOException
     {
-    	return get(bucket, key);
+    	return get(bucket, key, null);
     }
 
     /**
@@ -292,9 +298,9 @@
     /**
      * Deletes an object from S3.
      */
-    public Response delete(Bucket bucket, String key)
+    public Response delete(Bucket bucket, String key) throws IOException
     {
-    	return delete(bucket, key);
+    	return delete(bucket, key, null);
     }
 
     /**

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/GetResponse.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/GetResponse.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/GetResponse.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -63,4 +63,12 @@
 	public S3Object getObject() {
 		return object;
 	}
+	
+	/**
+	 * Returns a debug string.
+	 */
+	@Override
+	public String toString() {
+		return super.toString() + " object=" + object;
+	}
 }

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -66,6 +66,13 @@
 		return headers.get(header);
 	}
 	
+	public String getValue(String header) {
+		List<String> values = getValues(header);
+		if (values == null || values.isEmpty())
+			return null;
+		return values.get(0);
+	}
+
 	/**
 	 * Returns the number of headers.
 	 */

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -95,4 +95,10 @@
 
 	}
 
+    
+    @Override
+    public String toString() {
+    	return super.toString() + 
+    		" entries=" + entries;
+    }
 }

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/ListResponse.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/ListResponse.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/ListResponse.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -80,7 +80,10 @@
 		return isTruncated;
 	}
 
-	public class CommonPrefixEntry {
+	/**
+	 * Prefix entry.
+	 */
+	public static class CommonPrefixEntry {
 		
         /**
          * The prefix common to the delimited keys it represents
@@ -131,11 +134,12 @@
             } else if (name.equals("CommonPrefixes")){
                 this.commonPrefixEntry = new CommonPrefixEntry();
             }
+            this.currText = new StringBuilder();
         }
 
         public void endElement(String uri, String name, String qName) {
             if (name.equals("Name")) {
-                name = this.currText.toString();
+                ListResponse.this.name = this.currText.toString();
             } 
             // this prefix is the one we echo back from the request
             else if (name.equals("Prefix") && this.isEchoedPrefix) {
@@ -178,8 +182,6 @@
             else if (name.equals("Prefix")) {
                 this.commonPrefixEntry.prefix = this.currText.toString();
             }
-            if(this.currText.length() != 0)
-                this.currText = new StringBuilder();
         }
 
         public void characters(char ch[], int start, int length) {
@@ -232,5 +234,18 @@
     public String getNextMarker() {
         return this.nextMarker;
     }
+    
+    @Override
+    public String toString() {
+    	return super.toString() + 
+    		" name=" + name +
+    		" prefix=" + prefix +
+    		" marker=" + marker +
+    		" maxKeys=" + maxKeys +
+    		" isTruncated=" + isTruncated +
+    		" nextMarker=" + nextMarker +
+    		" entries=" + entries +
+    		" prefix=" + commonPrefixEntries;
+    }
 
 }

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/LocationResponse.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/LocationResponse.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/LocationResponse.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -71,4 +71,10 @@
                 this.currText.append(ch, start, length);
         }
     }
+    
+    @Override
+    public String toString() {
+    	return super.toString() + " location=" + location;
+    }
+    
 }

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -126,6 +126,9 @@
         if (object != null) {
             metadata = object.getMetadata();
         }
+        if (headers == null) {
+        	headers = new Headers();
+        }
 
         return generateURL(Method.PUT, bucket, UrlEncoder.encode(key), headers.mergeMetadata(metadata));
     }

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/S3Object.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/S3Object.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/S3Object.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -9,8 +9,6 @@
 
 package com.amazon.s3;
 
-
-
 /**
  * A representation of a single object stored in S3.
  */

Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/UrlEncoder.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/UrlEncoder.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/UrlEncoder.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -12,10 +12,10 @@
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 
-
-
-public class UrlEncoder {
+class UrlEncoder {
 	
+	private UrlEncoder() {}
+	
     static String encode(String unencoded) {
 	    try {
 	        return URLEncoder.encode(unencoded, "UTF-8");

Modified: amazon-s3/trunk/src/main/java/com/amazon/thirdparty/Base64.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/thirdparty/Base64.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/main/java/com/amazon/thirdparty/Base64.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -773,226 +773,6 @@
 
     
 
-    /**
-     * Attempts to decode Base64 data and deserialize a Java
-     * Object within. Returns <tt>null</tt> if there was an error.
-     *
-     * @param encodedObject The Base64 data to decode
-     * @return The decoded and deserialized object
-     * @since 1.5
-     */
-    public static Object decodeToObject( String encodedObject )
-    {
-        // Decode and gunzip if necessary
-        byte[] objBytes = decode( encodedObject );
-        
-        java.io.ByteArrayInputStream  bais = null;
-        java.io.ObjectInputStream     ois  = null;
-        Object obj = null;
-        
-        try
-        {
-            bais = new java.io.ByteArrayInputStream( objBytes );
-            ois  = new java.io.ObjectInputStream( bais );
-        
-            obj = ois.readObject();
-        }   // end try
-        catch( java.io.IOException e )
-        {
-            e.printStackTrace();
-            obj = null;
-        }   // end catch
-        catch( java.lang.ClassNotFoundException e )
-        {
-            e.printStackTrace();
-            obj = null;
-        }   // end catch
-        finally
-        {
-            try{ bais.close(); } catch( Exception e ){}
-            try{ ois.close();  } catch( Exception e ){}
-        }   // end finally
-        
-        return obj;
-    }   // end decodeObject
-    
-    
-    
-    /**
-     * Convenience method for encoding data to a file.
-     *
-     * @param dataToEncode byte array of data to encode in base64 form
-     * @param filename Filename for saving encoded data
-     * @return <tt>true</tt> if successful, <tt>false</tt> otherwise
-     *
-     * @since 2.1
-     */
-    public static boolean encodeToFile( byte[] dataToEncode, String filename )
-    {
-        boolean success = false;
-        Base64.OutputStream bos = null;
-        try
-        {
-            bos = new Base64.OutputStream( 
-                      new java.io.FileOutputStream( filename ), Base64.ENCODE );
-            bos.write( dataToEncode );
-            success = true;
-        }   // end try
-        catch( java.io.IOException e )
-        {
-            
-            success = false;
-        }   // end catch: IOException
-        finally
-        {
-            try{ bos.close(); } catch( Exception e ){}
-        }   // end finally
-        
-        return success;
-    }   // end encodeToFile
-    
-    
-    /**
-     * Convenience method for decoding data to a file.
-     *
-     * @param dataToDecode Base64-encoded data as a string
-     * @param filename Filename for saving decoded data
-     * @return <tt>true</tt> if successful, <tt>false</tt> otherwise
-     *
-     * @since 2.1
-     */
-    public static boolean decodeToFile( String dataToDecode, String filename )
-    {
-        boolean success = false;
-        Base64.OutputStream bos = null;
-        try
-        {
-                bos = new Base64.OutputStream( 
-                          new java.io.FileOutputStream( filename ), Base64.DECODE );
-                bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) );
-                success = true;
-        }   // end try
-        catch( java.io.IOException e )
-        {
-            success = false;
-        }   // end catch: IOException
-        finally
-        {
-                try{ bos.close(); } catch( Exception e ){}
-        }   // end finally
-        
-        return success;
-    }   // end decodeToFile
-    
-    
-    
-    
-    /**
-     * Convenience method for reading a base64-encoded
-     * file and decoding it.
-     *
-     * @param filename Filename for reading encoded data
-     * @return decoded byte array or null if unsuccessful
-     *
-     * @since 2.1
-     */
-    public static byte[] decodeFromFile( String filename )
-    {
-        byte[] decodedData = null;
-        Base64.InputStream bis = null;
-        try
-        {
-            // Set up some useful variables
-            java.io.File file = new java.io.File( filename );
-            byte[] buffer = null;
-            int length   = 0;
-            int numBytes = 0;
-            
-            // Check for size of file
-            if( file.length() > Integer.MAX_VALUE )
-            {
-                System.err.println( "File is too big for this convenience method (" + file.length() + " bytes)." );
-                return null;
-            }   // end if: file too big for int index
-            buffer = new byte[ (int)file.length() ];
-            
-            // Open a stream
-            bis = new Base64.InputStream( 
-                      new java.io.BufferedInputStream( 
-                      new java.io.FileInputStream( file ) ), Base64.DECODE );
-            
-            // Read until done
-            while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
-                length += numBytes;
-            
-            // Save in a variable to return
-            decodedData = new byte[ length ];
-            System.arraycopy( buffer, 0, decodedData, 0, length );
-            
-        }   // end try
-        catch( java.io.IOException e )
-        {
-            System.err.println( "Error decoding from file " + filename );
-        }   // end catch: IOException
-        finally
-        {
-            try{ bis.close(); } catch( Exception e) {}
-        }   // end finally
-        
-        return decodedData;
-    }   // end decodeFromFile
-    
-    
-    
-    /**
-     * Convenience method for reading a binary file
-     * and base64-encoding it.
-     *
-     * @param filename Filename for reading binary data
-     * @return base64-encoded string or null if unsuccessful
-     *
-     * @since 2.1
-     */
-    public static String encodeFromFile( String filename )
-    {
-        String encodedData = null;
-        Base64.InputStream bis = null;
-        try
-        {
-            // Set up some useful variables
-            java.io.File file = new java.io.File( filename );
-            byte[] buffer = new byte[ (int)(file.length() * 1.4) ];
-            int length   = 0;
-            int numBytes = 0;
-            
-            // Open a stream
-            bis = new Base64.InputStream( 
-                      new java.io.BufferedInputStream( 
-                      new java.io.FileInputStream( file ) ), Base64.ENCODE );
-            
-            // Read until done
-            while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
-                length += numBytes;
-            
-            // Save in a variable to return
-            encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
-                
-        }   // end try
-        catch( java.io.IOException e )
-        {
-            System.err.println( "Error encoding from file " + filename );
-        }   // end catch: IOException
-        finally
-        {
-            try{ bis.close(); } catch( Exception e) {}
-        }   // end finally
-        
-        return encodedData;
-        }   // end encodeFromFile
-    
-    
-    
-    
     /* ********  I N N E R   C L A S S   I N P U T S T R E A M  ******** */
     
     

Deleted: amazon-s3/trunk/src/test/java/com/amazon/s3/S3Driver.java
===================================================================
--- amazon-s3/trunk/src/test/java/com/amazon/s3/S3Driver.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/test/java/com/amazon/s3/S3Driver.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -1,139 +0,0 @@
-package com.amazon.s3;
-//  This software code is made available "AS IS" without warranties of any
-//  kind.  You may copy, display, modify and redistribute the software
-//  code either by itself or as incorporated into your code; provided that
-//  you do not remove any proprietary notices.  Your use of this software
-//  code is at your own risk and you waive any claim against Amazon
-//  Digital Services, Inc. or its affiliates with respect to your use of
-//  this software code. (c) 2006-2007 Amazon Digital Services, Inc. or its
-//  affiliates.
-
-
-public class S3Driver {
-
-    static final String awsAccessKeyId = "1CK9DVFVC6GRDSP6Z9G2"; //<INSERT YOUR AWS ACCESS KEY ID HERE>";
-    static final String awsSecretAccessKey = "<INSERT YOUR AWS SECRET ACCESS KEY HERE>";
-    
-    
-    // convert the bucket to lowercase for vanity domains
-    // the bucket name must be lowercase since DNS is case-insensitive
-    static final Bucket bucket = new Bucket(awsAccessKeyId.toLowerCase() + "-test-bucket");
-    static final String keyName = "test-key";
-
-    public static void main(String args[]) throws Exception {
-        if (awsAccessKeyId.startsWith("<INSERT")) {
-            System.err.println("Please examine S3Driver.java and update it with your credentials");
-            System.exit(-1);
-        }
-
-        Connection conn =
-            new Connection(awsAccessKeyId, awsSecretAccessKey);
-        QueryGenerator generator =
-            new QueryGenerator(awsAccessKeyId, awsSecretAccessKey);
-
-        // Check if the bucket exists.  The high availability engineering of 
-        // Amazon S3 is focused on get, put, list, and delete operations. 
-        // Because bucket operations work against a centralized, global
-        // resource space, it is not appropriate to make bucket create or
-        // delete calls on the high availability code path of your application.
-        // It is better to create or delete buckets in a separate initialization
-        // or setup routine that you run less often.
-        if (!conn.exists(bucket))
-        {
-            System.out.println("----- creating bucket -----");
-            System.out.println(conn.create(bucket, Connection.LOCATION_DEFAULT, null).getResponseMessage());
-            // sample creating an EU located bucket.
-            // (note path-style urls will not work with location-constrained buckets)
-            //System.out.println(conn.createBucket(bucketName, AWSAuthConnection.LOCATION_EU, null).connection.getResponseMessage());
-        }
-
-        System.out.println("----- listing bucket -----");
-        System.out.println(conn.list(bucket).getEntries());
-
-        System.out.println("----- bucket location -----");
-        System.out.println(conn.getLocation(bucket).getLocation());
-
-        System.out.println("----- putting object -----");
-        S3Object object = new S3Object("this is a test".getBytes(), null);
-        Headers headers = new Headers();
-        headers.put("Content-Type", "text/plain");
-        System.out.println(
-                conn.put(bucket, keyName, object, headers).getResponseMessage()
-            );
-
-        System.out.println("----- listing bucket -----");
-        System.out.println(conn.list(bucket, null, null, null, null).getEntries());
-
-        System.out.println("----- getting object -----");
-        System.out.println(
-                new String(conn.get(bucket, keyName, null).getObject().getData())
-            );
-
-        System.out.println("----- query string auth example -----");
-        generator.setExpiresIn(60 * 1000);
-
-        System.out.println("Try this url in your web browser (it will only work for 60 seconds)\n");
-        System.out.println(generator.get(bucket, keyName, null));
-        System.out.print("\npress enter> ");
-        System.in.read();
-
-        System.out.println("\nNow try just the url without the query string arguments.  It should fail.\n");
-        System.out.println(generator.makeBareURL(bucket, keyName));
-        System.out.print("\npress enter> ");
-        System.in.read();
-
-        System.out.println("----- putting object with metadata and public read acl -----");
-
-        Headers metadata = new Headers();
-        metadata.put("blah", "foo");
-        object = new S3Object("this is a publicly readable test".getBytes(), new Headers(metadata));
-
-        headers = new Headers();
-        headers.put("x-amz-acl", "public-read");
-        headers.put("Content-Type", "text/plain");
-
-        System.out.println(
-                conn.put(bucket, keyName + "-public", object, headers).getResponseMessage()
-            );
-
-        System.out.println("----- anonymous read test -----");
-        System.out.println("\nYou should be able to try this in your browser\n");
-        System.out.println(generator.makeBareURL(bucket, keyName + "-public"));
-        System.out.print("\npress enter> ");
-        System.in.read();
-        
-        System.out.println("----- path style url example -----");
-        System.out.println("\nNon-location-constrained buckets can also be specified as part of the url path.  (This was the original url style supported by S3.)");
-        System.out.println("\nTry this url out in your browser (it will only be valid for 60 seconds)\n");
-        generator.setCallingFormat(CallingFormat.getPathCallingFormat());
-        // could also have been done like this:
-        //  generator = new QueryStringAuthGenerator(awsAccessKeyId, awsSecretAccessKey, true, Utils.DEFAULT_HOST, CallingFormat.getPathCallingFormat());
-        generator.setExpiresIn(60 * 1000);
-        System.out.println(generator.get(bucket, keyName, null));
-        System.out.print("\npress enter> ");
-        System.in.read();
-
-        System.out.println("----- getting object's acl -----");
-        System.out.println(new String(conn.getACL(bucket, keyName, null).getObject().getData()));
-
-        System.out.println("----- deleting objects -----");
-        System.out.println(
-                conn.delete(bucket, keyName, null).getResponseMessage()
-            );
-        System.out.println(
-                conn.delete(bucket, keyName + "-public", null).getResponseMessage()
-            );
-
-        System.out.println("----- listing bucket -----");
-        System.out.println(conn.list(bucket, null, null, null, null).getEntries());
-
-        System.out.println("----- listing all my buckets -----");
-        System.out.println(conn.listAllBuckets().getEntries());
-
-        System.out.println("----- deleting bucket -----");
-        System.out.println(
-                conn.delete(bucket).getResponseMessage()
-            );
-    }
-
-}

Modified: amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java
===================================================================
--- amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java	2008-02-06 01:49:23 UTC (rev 5308)
+++ amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java	2008-02-06 03:09:24 UTC (rev 5309)
@@ -14,29 +14,34 @@
 import java.net.URL;
 import java.util.Arrays;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import org.junit.Before;
+import org.junit.Test;
+
 public class S3Test {
-    // static final String awsAccessKeyId = "<INSERT YOUR AWS ACCESS KEY ID HERE>";
-    // static final String awsSecretAccessKey = "<INSERT YOUR AWS SECRET ACCESS KEY HERE>";
-    static final String awsAccessKeyId = "1CK9DVFVC6GRDSP6Z9G2"; //<INSERT YOUR AWS ACCESS KEY ID HERE>";
-    static final String awsSecretAccessKey = "";
-    
-    // for subdomains (bucket.s3.amazonaws.com), 
-    // the bucket name must be lowercase since DNS is case-insensitive
-    static final Bucket bucket = new Bucket(awsAccessKeyId.toLowerCase() + "-test-bucket");
-    static int assertionCount = 0;
+	
+    String awsAccessKeyId = System.getProperty("accessKey");
+    String awsSecretAccessKey = System.getProperty("secretKey");
+    Bucket bucket;
 
     static final int UnspecifiedMaxKeys = -1;
 
-    public static void main(String args[]) throws Exception {
-        if (awsAccessKeyId.startsWith("<INSERT")) {
-            System.err.println("Please examine S3Test.java and update it with your credentials");
-            System.exit(-1);
-        }
-        
+    @Before
+    public void setUp() {
+    	if (awsAccessKeyId == null)
+    		throw new IllegalStateException("accessKey system propery null");
+    	if (awsSecretAccessKey == null)
+    		throw new IllegalStateException("secretKey system propery null");
+    	
+        // for subdomains (bucket.s3.amazonaws.com), 
+        // the bucket name must be lowercase since DNS is case-insensitive
+        bucket = new Bucket(awsAccessKeyId.toLowerCase() + "-test-bucket");
+    }
+    
+    @Test
+    public void testMe() throws Exception {
         // test all operation for both regular and vanity domains
         // regular: http://s3.amazonaws.com/key
         // subdomain: http://bucket.s3.amazonaws.com/key
@@ -47,9 +52,8 @@
         test(CallingFormat.getSubdomainCallingFormat(), Connection.LOCATION_EU, true, Connection.DEFAULT_HOST);
     }
 
-    private static void test(CallingFormat format, String location, boolean secure, String server) throws Exception
+    private void test(CallingFormat format, String location, boolean secure, String server) throws Exception
     {
-        assertionCount = 0;
         System.out.println((secure ? "http" : "https") + " / " + server + " / " +
                 ((location == null) ? "<no-location>" : location) + " / " +
                 format.getClass().getName());
@@ -70,6 +74,7 @@
                 HttpURLConnection.HTTP_OK,
                 listBucketResponse.getResponseCode());
         assertEquals("list wasn't empty", 0, listBucketResponse.getEntries().size());
+        System.out.println(listBucketResponse);
         verifyBucketResponseParameters(listBucketResponse, bucket, "", "", UnspecifiedMaxKeys, null, false, null);
 
         // start delimiter tests
@@ -118,7 +123,7 @@
         verifyBucketResponseParameters(listBucketResponse, bucket, "", "", UnspecifiedMaxKeys, "/", false, null);
 
         // root "directory" with a max-keys of "1"
-        listBucketResponse = conn.list(bucket, null, null, new Integer( 1 ), "/", null);
+        listBucketResponse = conn.list(bucket, null, null, 1, "/", null);
         assertEquals(
                 "couldn't get list",
                 HttpURLConnection.HTTP_OK,
@@ -128,7 +133,7 @@
         verifyBucketResponseParameters(listBucketResponse, bucket, "", "", 1, "/", true, "example.txt");
 
         // root "directory" with a max-keys of "2"
-        listBucketResponse = conn.list(bucket, null, null, new Integer( 2 ), "/", null);
+        listBucketResponse = conn.list(bucket, null, null, 2, "/", null);
         assertEquals(
                 "couldn't get list",
                 HttpURLConnection.HTTP_OK,
@@ -179,7 +184,7 @@
                 HttpURLConnection.HTTP_OK,
                 response.getResponseCode());
 
-        Map metadata = new HashMap();
+        Map<String, List<String>> metadata = new HashMap<String, List<String>>();
         metadata.put("title", Arrays.asList(new String[] { "title" }));
         response = conn.put(bucket, key, new S3Object(text.getBytes(), new Headers(metadata)), null);
         assertEquals(
@@ -196,12 +201,8 @@
         assertEquals("didn't get the right metadata back", 1, getResponse.getObject().getMetadata().size());
         assertEquals(
                 "didn't get the right metadata back",
-                1,
-                ((List)getResponse.getObject().getMetadata().getValues("title")).size());
-        assertEquals(
-                "didn't get the right metadata back",
                 "title",
-                ((List)getResponse.getObject().getMetadata().getValues("title")).get(0));
+                getResponse.getObject().getMetadata().getValue("title"));
         assertEquals(
                 "didn't get the right content-length",
                 ""+text.length(),
@@ -225,12 +226,8 @@
         assertEquals("didn't get the right metadata back", getResponse.getObject().getMetadata().size(), 1);
         assertEquals(
                 "didn't get the right metadata back",
-                1,
-                ((List)getResponse.getObject().getMetadata().getValues("title")).size());
-        assertEquals(
-                "didn't get the right metadata back",
                 titleWithSpaces.trim(),
-                ((List)getResponse.getObject().getMetadata().getValues("title")).get(0));
+                getResponse.getObject().getMetadata().getValue("title"));
 
         String weirdKey = "&=//%# ++++";
         response = conn.put(bucket, weirdKey, new S3Object(text.getBytes(), null), null);
@@ -299,7 +296,7 @@
                 "couldn't list bucket",
                 HttpURLConnection.HTTP_OK,
                 listBucketResponse.getResponseCode());
-        List entries = listBucketResponse.getEntries();
+        List<Entry> entries = listBucketResponse.getEntries();
         assertEquals("didn't get back the right number of entries", 2, entries.size());
         // depends on weirdKey < $key
         assertEquals("first key isn't right", weirdKey, ((Entry)entries.get(0)).getKey());
@@ -317,8 +314,7 @@
                 listBucketResponse.getEntries().size());
         verifyBucketResponseParameters(listBucketResponse, bucket, "", "", 1, null, true, null);
 
-        for (Iterator it = entries.iterator(); it.hasNext(); ) {
-            Entry entry = (Entry)it.next();
+        for (Entry entry : entries) {
             response = conn.delete(bucket, entry.getKey(), null);
             assertEquals(
                     "couldn't delete entry",
@@ -331,7 +327,7 @@
                 "couldn't list all my buckets",
                 HttpURLConnection.HTTP_OK,
                 listAllMyBucketsResponse.getResponseCode());
-        List buckets = listAllMyBucketsResponse.getEntries();
+        List<Bucket> buckets = listAllMyBucketsResponse.getEntries();
 
         response = conn.delete(bucket);
         assertEquals(
@@ -418,8 +414,6 @@
                 "DELETE",
                 HttpURLConnection.HTTP_NO_CONTENT,
                 "delete bucket");
-
-        System.out.println("OK (" + assertionCount + " tests passed)");
     }
 
     private static void verifyBucketResponseParameters( ListResponse listBucketResponse,
@@ -439,14 +433,12 @@
 
 
     private static void assertEquals(String message, int expected, int actual) {
-        assertionCount++;
         if (expected != actual) {
             throw new RuntimeException(message + ": expected " + expected + " but got " + actual);
         }
     }
     
     private static void assertEquals(String message, byte[] expected, byte[] actual) {
-        assertionCount++;
         if (! Arrays.equals(expected, actual)) {
             throw new RuntimeException(
                     message +
@@ -458,14 +450,12 @@
     }
 
     private static void assertEquals(String message, Object expected, Object actual) {
-        assertionCount++;
         if (expected != actual && (actual == null || ! actual.equals(expected))) {
             throw new RuntimeException(message + ": expected " + expected + " but got " + actual);
         }
     }
 
     private static void assertEquals(String message, boolean expected, boolean actual) {
-        assertionCount++;
         if (expected != actual) {
             throw new RuntimeException(message + ": expected " + expected + " but got " + actual);
         }
@@ -495,4 +485,123 @@
 
         assertEquals(message, code, connection.getResponseCode());
     }
+    
+    private void readline() throws IOException {
+    	// TODO
+    	// System.in.read();
+    }
+    
+    @Test
+    public void testDriver() throws Exception {
+        Bucket bucket = new Bucket(awsAccessKeyId.toLowerCase() + "-test-bucket");
+        String keyName = "KEY";
+
+        Connection conn = new Connection(awsAccessKeyId, awsSecretAccessKey);
+        QueryGenerator generator = new QueryGenerator(awsAccessKeyId, awsSecretAccessKey);
+
+        // Check if the bucket exists.  The high availability engineering of 
+        // Amazon S3 is focused on get, put, list, and delete operations. 
+        // Because bucket operations work against a centralized, global
+        // resource space, it is not appropriate to make bucket create or
+        // delete calls on the high availability code path of your application.
+        // It is better to create or delete buckets in a separate initialization
+        // or setup routine that you run less often.
+        if (!conn.exists(bucket))
+        {
+            System.out.println("----- creating bucket -----");
+            System.out.println(conn.create(bucket, Connection.LOCATION_DEFAULT, null).getResponseMessage());
+            // sample creating an EU located bucket.
+            // (note path-style urls will not work with location-constrained buckets)
+            //System.out.println(conn.createBucket(bucketName, AWSAuthConnection.LOCATION_EU, null).connection.getResponseMessage());
+        }
+
+        System.out.println("----- listing bucket -----");
+        System.out.println(conn.list(bucket).getEntries());
+
+        System.out.println("----- bucket location -----");
+        System.out.println(conn.getLocation(bucket).getLocation());
+
+        System.out.println("----- putting object -----");
+        S3Object object = new S3Object("this is a test".getBytes(), null);
+        Headers headers = new Headers();
+        headers.put("Content-Type", "text/plain");
+		System.out.println(
+                conn.put(bucket, keyName, object, headers).getResponseMessage()
+            );
+
+        System.out.println("----- listing bucket -----");
+        System.out.println(conn.list(bucket, null, null, null, null).getEntries());
+
+        System.out.println("----- getting object -----");
+        System.out.println(
+                new String(conn.get(bucket, keyName, null).getObject().getData())
+            );
+
+        System.out.println("----- query string auth example -----");
+        generator.setExpiresIn(60 * 1000);
+
+        System.out.println("Try this url in your web browser (it will only work for 60 seconds)\n");
+        System.out.println(generator.get(bucket, keyName, null));
+        System.out.print("\npress enter> ");
+        readline();
+
+        System.out.println("\nNow try just the url without the query string arguments.  It should fail.\n");
+        System.out.println(generator.makeBareURL(bucket, keyName));
+        System.out.print("\npress enter> ");
+        readline();
+
+        System.out.println("----- putting object with metadata and public read acl -----");
+
+        Headers metadata = new Headers();
+        metadata.put("blah", "foo");
+        object = new S3Object("this is a publicly readable test".getBytes(), new Headers(metadata));
+
+        headers = new Headers();
+        headers.put("x-amz-acl", "public-read");
+        headers.put("Content-Type", "text/plain");
+
+        System.out.println(
+                conn.put(bucket, keyName + "-public", object, headers).getResponseMessage()
+            );
+
+        System.out.println("----- anonymous read test -----");
+        System.out.println("\nYou should be able to try this in your browser\n");
+        System.out.println(generator.makeBareURL(bucket, keyName + "-public"));
+        System.out.print("\npress enter> ");
+        readline();
+        
+        System.out.println("----- path style url example -----");
+        System.out.println("\nNon-location-constrained buckets can also be specified as part of the url path.  (This was the original url style supported by S3.)");
+        System.out.println("\nTry this url out in your browser (it will only be valid for 60 seconds)\n");
+        generator.setCallingFormat(CallingFormat.getPathCallingFormat());
+        // could also have been done like this:
+        //  generator = new QueryStringAuthGenerator(awsAccessKeyId, awsSecretAccessKey, true, Utils.DEFAULT_HOST, CallingFormat.getPathCallingFormat());
+        generator.setExpiresIn(60 * 1000);
+        System.out.println(generator.get(bucket, keyName, null));
+        System.out.print("\npress enter> ");
+        readline();
+
+        System.out.println("----- getting object's acl -----");
+        System.out.println(new String(conn.getACL(bucket, keyName, null).getObject().getData()));
+
+        System.out.println("----- deleting objects -----");
+        System.out.println(
+                conn.delete(bucket, keyName, null).getResponseMessage()
+            );
+        System.out.println(
+                conn.delete(bucket, keyName + "-public", null).getResponseMessage()
+            );
+
+        System.out.println("----- listing bucket -----");
+        System.out.println(conn.list(bucket, null, null, null, null).getEntries());
+
+        System.out.println("----- listing all my buckets -----");
+        System.out.println(conn.listAllBuckets().getEntries());
+
+        System.out.println("----- deleting bucket -----");
+        System.out.println(
+                conn.delete(bucket).getResponseMessage()
+            );
+    }
+
 }




More information about the jbosscache-commits mailing list