Author: ivaynberg
Date: Mon Apr 26 17:50:00 2010
New Revision: 938151

URL: http://svn.apache.org/viewvc?rev=938151&view=rev
Log:
ajax not working due to bugs in resource handling
Issue: WICKET-2839

Added:
    wicket/trunk/wicket/src/test/java/org/apache/wicket/request/resource/
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/request/resource/WriteCallbackTest.java
   (with props)
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/resource/UrlResourceStreamTest.java
   (with props)
Modified:
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java?rev=938151&r1=938150&r2=938151&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java
 Mon Apr 26 17:50:00 2010
@@ -463,7 +463,7 @@ public abstract class AbstractResource i
                                @Override
                                public void write(byte[] b, int off, int len) 
throws IOException
                                {
-                                       if (off == 0 || len == b.length)
+                                       if (off == 0 && len == b.length)
                                        {
                                                write(b);
                                        }

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java?rev=938151&r1=938150&r2=938151&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
 Mon Apr 26 17:50:00 2010
@@ -193,26 +193,26 @@ public class UrlResourceStream extends A
        @Override
        public Time lastModifiedTime()
        {
-               if (file != null)
+               try
                {
-                       // in case the file has been removed by now
-                       if (file.exists() == false)
+                       if (file != null)
                        {
-                               return null;
-                       }
+                               // in case the file has been removed by now
+                               if (file.exists() == false)
+                               {
+                                       return null;
+                               }
 
-                       long lastModified = file.lastModified();
+                               long lastModified = file.lastModified();
 
-                       // if last modified changed update content length and 
last modified date
-                       if (lastModified != this.lastModified)
-                       {
-                               this.lastModified = lastModified;
-                               contentLength = (int)file.length();
+                               // if last modified changed update content 
length and last modified date
+                               if (lastModified != this.lastModified)
+                               {
+                                       this.lastModified = lastModified;
+                                       setContentLength();
+                               }
                        }
-               }
-               else
-               {
-                       try
+                       else
                        {
                                long lastModified = 
Connections.getLastModified(url);
 
@@ -221,31 +221,35 @@ public class UrlResourceStream extends A
                                {
                                        this.lastModified = lastModified;
 
-                                       URLConnection connection = 
url.openConnection();
-                                       contentLength = 
connection.getContentLength();
-                                       Connections.close(connection);
+                                       setContentLength();
                                }
                        }
-                       catch (IOException e)
+                       return Time.milliseconds(lastModified);
+               }
+               catch (IOException e)
+               {
+                       if (url.toString().indexOf(".jar!") >= 0)
                        {
-                               if (url.toString().indexOf(".jar!") >= 0)
+                               if (log.isDebugEnabled())
                                {
-                                       if (log.isDebugEnabled())
-                                       {
-                                               log.debug("getLastModified for 
" + url + " failed: " + e.getMessage());
-                                       }
+                                       log.debug("getLastModified for " + url 
+ " failed: " + e.getMessage());
                                }
-                               else
-                               {
-                                       log.warn("getLastModified for " + url + 
" failed: " + e.getMessage());
-                               }
-
-                               // allow modification watcher to detect the 
problem
-                               return null;
+                       }
+                       else
+                       {
+                               log.warn("getLastModified for " + url + " 
failed: " + e.getMessage());
                        }
 
+                       // allow modification watcher to detect the problem
+                       return null;
                }
-               return Time.milliseconds(lastModified);
+       }
+
+       private void setContentLength() throws IOException
+       {
+               URLConnection connection = url.openConnection();
+               contentLength = connection.getContentLength();
+               Connections.close(connection);
        }
 
        /**

Added: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/request/resource/WriteCallbackTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/request/resource/WriteCallbackTest.java?rev=938151&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/request/resource/WriteCallbackTest.java
 (added)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/request/resource/WriteCallbackTest.java
 Mon Apr 26 17:50:00 2010
@@ -0,0 +1,63 @@
+/*
+ * 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.wicket.request.resource;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+import org.apache.wicket.mock.MockWebRequest;
+import org.apache.wicket.request.Url;
+import org.apache.wicket.request.resource.AbstractResource.WriteCallback;
+import org.apache.wicket.request.resource.IResource.Attributes;
+import org.apache.wicket.response.ByteArrayResponse;
+
+
+/**
+ * @author Kent Tong
+ */
+public class WriteCallbackTest extends TestCase
+{
+
+       /**
+        */
+       public void testWriteStream()
+       {
+               WriteCallback callback = new WriteCallback()
+               {
+
+                       @Override
+                       public void writeData(Attributes attributes)
+                       {
+
+                       }
+               };
+               ByteArrayResponse response = new ByteArrayResponse();
+               Attributes attributes = new Attributes(new MockWebRequest(new 
Url()), response);
+               byte[] srcData = new byte[5000];
+               for (int i = 0; i < srcData.length; i++)
+               {
+                       srcData[i] = (byte)i;
+               }
+               InputStream in = new ByteArrayInputStream(srcData);
+               callback.writeStream(attributes, in);
+               assertTrue("Content not equal", 
Arrays.equals(response.getBytes(), srcData));
+       }
+
+}

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/request/resource/WriteCallbackTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/resource/UrlResourceStreamTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/util/resource/UrlResourceStreamTest.java?rev=938151&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/resource/UrlResourceStreamTest.java
 (added)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/resource/UrlResourceStreamTest.java
 Mon Apr 26 17:50:00 2010
@@ -0,0 +1,46 @@
+/*
+ * 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.wicket.util.resource;
+
+import java.io.IOException;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ * @author Kent Tong
+ */
+public class UrlResourceStreamTest extends TestCase
+{
+       /**
+        * lastModified() shouldn't change the content length if the file isn't 
really changed.
+        * 
+        * @throws IOException
+        */
+       public void testLastModifiedForResourceInJar() throws IOException
+       {
+               String anyClassInJarFile = "/java/lang/String.class";
+               URL url = getClass().getResource(anyClassInJarFile);
+               UrlResourceStream stream = new UrlResourceStream(url);
+               long length = stream.length();
+               stream.lastModifiedTime();
+               assertEquals(stream.length(), length);
+               stream.close();
+       }
+
+}

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/resource/UrlResourceStreamTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to