Author: bdelacretaz
Date: Thu Aug 13 15:39:31 2009
New Revision: 803933

URL: http://svn.apache.org/viewvc?rev=803933&view=rev
Log:
SLING-1078 - RegisteredResource tests

Added:
    
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InputStreamDigestTest.java
   (with props)
    
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/LocalFileRegisteredResource.java
   (with props)
    
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
   (with props)
Modified:
    
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResource.java

Modified: 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResource.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResource.java?rev=803933&r1=803932&r2=803933&view=diff
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResource.java
 (original)
+++ 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResource.java
 Thu Aug 13 15:39:31 2009
@@ -84,7 +84,7 @@
        }
        }
        
-       protected File getDataFile(BundleContext ctx) {
+       protected File getDataFile(BundleContext ctx) throws IOException {
                String filename = null;
                synchronized (getClass()) {
                        filename = getClass().getSimpleName() + "." + 
fileNumber++;
@@ -151,7 +151,6 @@
                        if(os != null) {
                                os.close();
                        }
-                       data.close();
                }
                return digestToString(d);
        }

Added: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InputStreamDigestTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InputStreamDigestTest.java?rev=803933&view=auto
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InputStreamDigestTest.java
 (added)
+++ 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InputStreamDigestTest.java
 Thu Aug 13 15:39:31 2009
@@ -0,0 +1,50 @@
+/*
+ * 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.sling.osgi.installer.impl;
+
+import java.io.ByteArrayInputStream;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.sling.osgi.installer.InstallableResource;
+
+public class InputStreamDigestTest {
+       
+       private InstallableResource getInstallableResource(String data) {
+               return new InstallableResource(data, new 
ByteArrayInputStream(data.getBytes()));
+       }
+       
+       @org.junit.Test public void testStreamDigest() throws Exception {
+               final String data1 = "This is some data";
+               final String data2 = "This is more data, which is different";
+       
+               final RegisteredResource r1 = new 
LocalFileRegisteredResource(getInstallableResource(data1));
+               final RegisteredResource r2 = new 
LocalFileRegisteredResource(getInstallableResource(data1));
+               final RegisteredResource r3 = new 
LocalFileRegisteredResource(getInstallableResource(data2));
+               
+               assertTrue("r1 has non-empty digest", r1.getDigest().length() > 
5);
+               assertTrue("r2 has non-empty digest", r2.getDigest().length() > 
5);
+               assertTrue("r3 has non-empty digest", r3.getDigest().length() > 
5);
+               
+               assertEquals("r1 and r2 have same digest", r1.getDigest(), 
r2.getDigest());
+               assertFalse("r1 and r3 have different digest", 
r1.getDigest().equals(r3.getDigest()));
+       }
+
+}

Propchange: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InputStreamDigestTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InputStreamDigestTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/LocalFileRegisteredResource.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/LocalFileRegisteredResource.java?rev=803933&view=auto
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/LocalFileRegisteredResource.java
 (added)
+++ 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/LocalFileRegisteredResource.java
 Thu Aug 13 15:39:31 2009
@@ -0,0 +1,43 @@
+/*
+ * 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.sling.osgi.installer.impl;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.sling.osgi.installer.InstallableResource;
+import org.osgi.framework.BundleContext;
+
+/** RegisteredResource that stores data to a local temporary file */
+class LocalFileRegisteredResource extends RegisteredResource {
+       private File storage;
+       
+       LocalFileRegisteredResource(InstallableResource r) throws IOException {
+               super(null, r);
+       }
+       
+       @Override
+       protected File getDataFile(BundleContext ctx) throws IOException {
+               if(storage == null) {
+                       storage = File.createTempFile(getClass().getName(), 
"test");
+                       storage.deleteOnExit();
+               }
+               return storage;
+       }
+}
\ No newline at end of file

Propchange: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/LocalFileRegisteredResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/LocalFileRegisteredResource.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java?rev=803933&view=auto
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
 (added)
+++ 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
 Thu Aug 13 15:39:31 2009
@@ -0,0 +1,66 @@
+/*
+ * 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.sling.osgi.installer.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.sling.osgi.installer.InstallableResource;
+
+public class RegisteredResourceTest {
+       
+       @org.junit.Test public void testStreamIsClosed() throws Exception {
+               final String data = "some data";
+               
+               class TestInputStream extends FilterInputStream {
+                       int closeCount;
+                       
+                       TestInputStream(InputStream i) {
+                               super(i);
+                       }
+
+                       @Override
+                       public void close() throws IOException {
+                               super.close();
+                               closeCount++;
+                       }
+                       
+               }
+               
+               final TestInputStream t = new TestInputStream(new 
ByteArrayInputStream(data.getBytes()));
+               final InstallableResource ir = new InstallableResource(data, t);
+               assertEquals("TestInputStream must not be closed before test", 
0, t.closeCount);
+               new LocalFileRegisteredResource(ir);
+               assertEquals("TestInputStream must be closed by 
RegisteredResource", 1, t.closeCount);
+       }
+       
+       @org.junit.Test public void testLocalFileCopy() throws Exception {
+               final String data = "This is some data";
+               final InputStream in = new 
ByteArrayInputStream(data.getBytes());
+               final LocalFileRegisteredResource r = new 
LocalFileRegisteredResource(new InstallableResource(data, in));
+               assertTrue("Local file exists", r.getDataFile(null).exists());
+               assertEquals("Local file length matches our data", 
data.getBytes().length, r.getDataFile(null).length());
+       }
+       
+}

Propchange: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL


Reply via email to