You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ol...@apache.org on 2008/11/22 14:29:10 UTC

svn commit: r719847 [2/2] - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/message/ main/java/org/apache/james/mime4j/message/storage/ test/java/org/apache/james/mime4j/message/storage/

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/SimpleTempStorageTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/SimpleTempStorageTest.java?rev=719847&r1=719846&r2=719847&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/SimpleTempStorageTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/SimpleTempStorageTest.java Sat Nov 22 05:29:10 2008
@@ -1,132 +1,133 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one   *
- * or more contributor license agreements.  See the NOTICE file *
- * distributed with this work for additional information        *
- * regarding copyright ownership.  The ASF licenses this file   *
- * to you 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.james.mime4j.message.storage;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import org.apache.james.mime4j.message.storage.SimpleTempStorage;
-import org.apache.james.mime4j.message.storage.TempFile;
-import org.apache.james.mime4j.message.storage.TempPath;
-
-import junit.framework.TestCase;
-
-/**
- * 
- *
- * 
- * @version $Id: SimpleTempStorageTest.java,v 1.2 2004/10/02 12:41:12 ntherning Exp $
- */
-public class SimpleTempStorageTest extends TestCase {
-
-    public void testGetRootTempPath() {
-        SimpleTempStorage man = new SimpleTempStorage();
-        TempPath path = man.getRootTempPath();
-        File tmpdir = new File(System.getProperty("java.io.tmpdir"));
-        assertEquals(tmpdir.getAbsolutePath(), path.getAbsolutePath());
-    }
-
-    public void testCreateTempPath() throws IOException {
-        SimpleTempStorage man = new SimpleTempStorage();
-        TempPath path = man.getRootTempPath().createTempPath();
-        File tmpdir = new File(System.getProperty("java.io.tmpdir"));
-        assertTrue(path.getAbsolutePath().startsWith(tmpdir.getAbsolutePath()));
-        
-        String fileName = path.getAbsolutePath().substring(
-                path.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
-        assertTrue("Unexpected chars in file name " + fileName, 
-                    fileName.matches("^[0-9]+$"));
-        assertTrue("Temp dir doesn't exist", 
-                   new File(path.getAbsolutePath()).exists());
-    }
-
-    public void testCreateTempPathString() throws IOException {
-        SimpleTempStorage man = new SimpleTempStorage();
-        TempPath path = man.getRootTempPath().createTempPath("test_prefix");
-        File tmpdir = new File(System.getProperty("java.io.tmpdir"), 
-                               "test_prefix");
-        assertTrue(path.getAbsolutePath().startsWith(tmpdir.getAbsolutePath()));
-        
-        String fileName = path.getAbsolutePath().substring(
-                path.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
-        assertTrue("Unexpected chars in file name " + fileName,
-                    fileName.matches("^test_prefix[0-9]+$"));
-        assertTrue("Temp dir doesn't exist", 
-                   new File(path.getAbsolutePath()).exists());
-    }
-
-    public void testCreateTempFile() throws IOException {
-        SimpleTempStorage man = new SimpleTempStorage();
-        TempPath path = man.getRootTempPath().createTempPath();
-        TempFile file = path.createTempFile();
-        assertTrue(file.getAbsolutePath().startsWith(path.getAbsolutePath()));
-        
-        String fileName = file.getAbsolutePath().substring(
-                file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
-        assertTrue("Unexpected chars in file name " + fileName, 
-                    fileName.matches("^[0-9]+\\.tmp$"));
-        assertTrue("Temp file doesn't exist", 
-                   new File(file.getAbsolutePath()).exists());
-        
-        String s = "A short string";
-        OutputStream out = file.getOutputStream();
-        out.write(s.getBytes());
-        out.close();
-
-        byte[] buffer = new byte[s.length() * 2];
-        InputStream in = file.getInputStream();
-        int i = 0;
-        for (int data; (data = in.read()) != -1; i++) {
-            buffer[i] = (byte) data;
-        }
-        in.close();
-        assertTrue(s.length() == i);
-        assertEquals(s, new String(buffer, 0, i));
-    }
-    
-    public void testCreateTempFileStringString() throws IOException {
-        SimpleTempStorage man = new SimpleTempStorage();
-        TempPath path = man.getRootTempPath().createTempPath();
-        TempFile file = path.createTempFile("test_prefix", ".suffix");
-        assertTrue(file.getAbsolutePath().startsWith(path.getAbsolutePath()));
-        
-        String fileName = file.getAbsolutePath().substring(
-                file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
-        assertTrue("Unexpected chars in file name " + fileName, 
-                    fileName.matches("^test_prefix[0-9]+\\.suffix$"));
-        assertTrue("Temp file doesn't exist", 
-                   new File(file.getAbsolutePath()).exists());
-    }
-    
-    public void testDeleteTempFile() throws IOException {
-        SimpleTempStorage man = new SimpleTempStorage();
-        TempPath path = man.getRootTempPath().createTempPath();
-        TempFile tempFile = path.createTempFile("test_prefix", ".suffix");
-
-        File backingFile = new File(tempFile.getAbsolutePath());
-        assertTrue(backingFile.exists());
-
-        tempFile.delete();
-        assertFalse(backingFile.exists());
-    }
-
-}
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.mime4j.message.storage;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.james.mime4j.message.storage.SimpleTempStorage;
+import org.apache.james.mime4j.message.storage.TempFile;
+import org.apache.james.mime4j.message.storage.TempPath;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ *
+ * 
+ * @version $Id: SimpleTempStorageTest.java,v 1.2 2004/10/02 12:41:12 ntherning Exp $
+ */
+@SuppressWarnings("deprecation")
+public class SimpleTempStorageTest extends TestCase {
+
+    public void testGetRootTempPath() {
+        SimpleTempStorage man = new SimpleTempStorage();
+        TempPath path = man.getRootTempPath();
+        File tmpdir = new File(System.getProperty("java.io.tmpdir"));
+        assertEquals(tmpdir.getAbsolutePath(), path.getAbsolutePath());
+    }
+
+    public void testCreateTempPath() throws IOException {
+        SimpleTempStorage man = new SimpleTempStorage();
+        TempPath path = man.getRootTempPath().createTempPath();
+        File tmpdir = new File(System.getProperty("java.io.tmpdir"));
+        assertTrue(path.getAbsolutePath().startsWith(tmpdir.getAbsolutePath()));
+        
+        String fileName = path.getAbsolutePath().substring(
+                path.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
+        assertTrue("Unexpected chars in file name " + fileName, 
+                    fileName.matches("^[0-9]+$"));
+        assertTrue("Temp dir doesn't exist", 
+                   new File(path.getAbsolutePath()).exists());
+    }
+
+    public void testCreateTempPathString() throws IOException {
+        SimpleTempStorage man = new SimpleTempStorage();
+        TempPath path = man.getRootTempPath().createTempPath("test_prefix");
+        File tmpdir = new File(System.getProperty("java.io.tmpdir"), 
+                               "test_prefix");
+        assertTrue(path.getAbsolutePath().startsWith(tmpdir.getAbsolutePath()));
+        
+        String fileName = path.getAbsolutePath().substring(
+                path.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
+        assertTrue("Unexpected chars in file name " + fileName,
+                    fileName.matches("^test_prefix[0-9]+$"));
+        assertTrue("Temp dir doesn't exist", 
+                   new File(path.getAbsolutePath()).exists());
+    }
+
+    public void testCreateTempFile() throws IOException {
+        SimpleTempStorage man = new SimpleTempStorage();
+        TempPath path = man.getRootTempPath().createTempPath();
+        TempFile file = path.createTempFile();
+        assertTrue(file.getAbsolutePath().startsWith(path.getAbsolutePath()));
+        
+        String fileName = file.getAbsolutePath().substring(
+                file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
+        assertTrue("Unexpected chars in file name " + fileName, 
+                    fileName.matches("^[0-9]+\\.tmp$"));
+        assertTrue("Temp file doesn't exist", 
+                   new File(file.getAbsolutePath()).exists());
+        
+        String s = "A short string";
+        OutputStream out = file.getOutputStream();
+        out.write(s.getBytes());
+        out.close();
+
+        byte[] buffer = new byte[s.length() * 2];
+        InputStream in = file.getInputStream();
+        int i = 0;
+        for (int data; (data = in.read()) != -1; i++) {
+            buffer[i] = (byte) data;
+        }
+        in.close();
+        assertTrue(s.length() == i);
+        assertEquals(s, new String(buffer, 0, i));
+    }
+    
+    public void testCreateTempFileStringString() throws IOException {
+        SimpleTempStorage man = new SimpleTempStorage();
+        TempPath path = man.getRootTempPath().createTempPath();
+        TempFile file = path.createTempFile("test_prefix", ".suffix");
+        assertTrue(file.getAbsolutePath().startsWith(path.getAbsolutePath()));
+        
+        String fileName = file.getAbsolutePath().substring(
+                file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1);
+        assertTrue("Unexpected chars in file name " + fileName, 
+                    fileName.matches("^test_prefix[0-9]+\\.suffix$"));
+        assertTrue("Temp file doesn't exist", 
+                   new File(file.getAbsolutePath()).exists());
+    }
+    
+    public void testDeleteTempFile() throws IOException {
+        SimpleTempStorage man = new SimpleTempStorage();
+        TempPath path = man.getRootTempPath().createTempPath();
+        TempFile tempFile = path.createTempFile("test_prefix", ".suffix");
+
+        File backingFile = new File(tempFile.getAbsolutePath());
+        assertTrue(backingFile.exists());
+
+        tempFile.delete();
+        assertFalse(backingFile.exists());
+    }
+
+}

Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/StorageProviderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/StorageProviderTest.java?rev=719847&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/StorageProviderTest.java (added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/storage/StorageProviderTest.java Sat Nov 22 05:29:10 2008
@@ -0,0 +1,121 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.mime4j.message.storage;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.james.mime4j.decoder.CodecUtil;
+
+import junit.framework.TestCase;
+
+public class StorageProviderTest extends TestCase {
+
+    public void testMemoryStorageProvider() throws Exception {
+        StorageProvider provider = new MemoryStorageProvider();
+
+        testReadWrite(provider, 0);
+        testReadWrite(provider, 1);
+        testReadWrite(provider, 1024);
+
+        testDelete(provider);
+    }
+
+    public void testTempFileStorageProvider() throws Exception {
+        StorageProvider provider = new TempFileStorageProvider();
+
+        testReadWrite(provider, 0);
+        testReadWrite(provider, 1);
+        testReadWrite(provider, 1024);
+
+        testDelete(provider);
+    }
+
+    public void testThresholdStorageProvider() throws Exception {
+        final int threshold = 500;
+        StorageProvider backend = new TempFileStorageProvider();
+        StorageProvider provider = new ThresholdStorageProvider(backend,
+                threshold);
+
+        testReadWrite(provider, 0);
+        testReadWrite(provider, 1);
+        testReadWrite(provider, threshold - 1);
+        testReadWrite(provider, threshold);
+        testReadWrite(provider, threshold + 1);
+        testReadWrite(provider, 2 * threshold);
+
+        testDelete(provider);
+    }
+
+    public void testCipherStorageProvider() throws Exception {
+        StorageProvider backend = new TempFileStorageProvider();
+        StorageProvider provider = new CipherStorageProvider(backend);
+
+        testReadWrite(provider, 0);
+        testReadWrite(provider, 1);
+        testReadWrite(provider, 1024);
+
+        testDelete(provider);
+    }
+
+    private void testReadWrite(StorageProvider provider, int size)
+            throws IOException {
+        byte[] data = createData(size);
+        assertEquals(size, data.length);
+
+        Storage storage = provider.store(new ByteArrayInputStream(data));
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        CodecUtil.copy(storage.getInputStream(), out);
+
+        byte[] result = out.toByteArray();
+
+        assertEquals(size, result.length);
+        for (int i = 0; i < size; i++) {
+            assertEquals(data[i], result[i]);
+        }
+    }
+
+    private byte[] createData(int size) {
+        byte[] data = new byte[size];
+        for (int i = 0; i < size; i++) {
+            data[i] = (byte) i;
+        }
+        return data;
+    }
+
+    private void testDelete(StorageProvider provider) throws IOException {
+        Storage storage = provider.store(new ByteArrayInputStream(
+                createData(512)));
+
+        storage.delete();
+
+        // getInputStream has to throw an IllegalStateException
+        try {
+            storage.getInputStream();
+            fail();
+        } catch (IllegalStateException expected) {
+        }
+
+        // invoking delete a second time should not have any effect
+        storage.delete();
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org