abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From calav...@apache.org
Subject svn commit: r682320 - in /incubator/abdera/java/trunk/core/src: main/java/org/apache/abdera/model/AtomDate.java test/java/org/apache/abdera/model/ test/java/org/apache/abdera/model/AtomDateTest.java
Date Mon, 04 Aug 2008 10:55:55 GMT
Author: calavera
Date: Mon Aug  4 03:55:55 2008
New Revision: 682320

URL: http://svn.apache.org/viewvc?rev=682320&view=rev
Log:
[ABDERA-193]: AtomDate hardening - clone java.util.Date, make non-null a requirement. Thanks
to Nathan Beyer.

Added:
    incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/model/
    incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/model/AtomDateTest.java
Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/AtomDate.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/AtomDate.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/AtomDate.java?rev=682320&r1=682319&r2=682320&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/AtomDate.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/AtomDate.java Mon
Aug  4 03:55:55 2008
@@ -69,7 +69,7 @@
 
   private static final long serialVersionUID = -7062139688635877771L;
 
-  private Date value = null;
+  private Date value;
   
   /**
    * Create an AtomDate using the current date and time
@@ -89,14 +89,16 @@
   /**
    * Create an AtomDate using a java.util.Date
    * @param value The java.util.Date value
+   * @throws NullPointerException if {@code date} is {@code null}
    */
   public AtomDate(Date value) {
-    this.value = value;
+    this.value = (Date) value.clone();
   }
   
   /**
    * Create an AtomDate using a java.util.Calendar.
    * @param value The java.util.Calendar value
+   * @throws NullPointerException if {@code value} is {@code null}
    */
   public AtomDate(Calendar value) {
     this(value.getTime());
@@ -130,9 +132,10 @@
   /**
    * Sets the value of the Atom date using java.util.Date
    * @param date A java.util.Date
+   * @throws NullPointerException if {@code date} is {@code null}
    */
   public AtomDate setValue(Date date) {
-    this.value = date;
+    this.value = (Date) date.clone();
     return this;
   }
 
@@ -159,7 +162,7 @@
    * @return A java.util.Date representing this Atom Date
    */
   public Date getDate() {
-    return value;
+    return (Date) value.clone();
   }
   
   /**
@@ -185,10 +188,11 @@
     return getValue();
   }
   
-  @Override public int hashCode() {
+  @Override 
+  public int hashCode() {
     final int prime = 31;
     int result = 1;
-    result = prime * result + ((value == null) ? 0 : value.hashCode());
+    result = prime * result + value.hashCode();
     return result;
   }
 
@@ -216,7 +220,7 @@
     try {
       return super.clone();
     } catch (CloneNotSupportedException e) {
-      throw new RuntimeException(e);
+      throw new AssertionError(e);
     }
   }
   
@@ -231,7 +235,7 @@
   public static Date parse(String date) {
     Matcher m = PATTERN.matcher(date);
     if (m.find()) {
-      Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+      Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
       int hoff = 0, moff = 0, doff = -1;
       if (m.group(9) != null) {
         doff = m.group(9).equals("-") ? 1 : -1;
@@ -258,7 +262,7 @@
    */
   public static String format(Date date) {
     StringBuilder sb = new StringBuilder();
-    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
     c.setTime(date);
     sb.append(c.get(Calendar.YEAR));
     sb.append('-');

Added: incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/model/AtomDateTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/model/AtomDateTest.java?rev=682320&view=auto
==============================================================================
--- incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/model/AtomDateTest.java
(added)
+++ incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/model/AtomDateTest.java
Mon Aug  4 03:55:55 2008
@@ -0,0 +1,134 @@
+package org.apache.abdera.model;
+
+import static org.junit.Assert.*;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+public class AtomDateTest {
+
+  @Test
+  public void testHashCode() {
+    long time = System.currentTimeMillis();
+    
+    AtomDate ad1 = new AtomDate(time);
+    assertTrue(ad1.hashCode() == ad1.hashCode());
+    
+    AtomDate ad2 = new AtomDate(time + 10);
+    assertFalse(ad1.hashCode() == ad2.hashCode());
+    
+  }
+
+  @Test
+  public void testAtomDateDate() {
+    Date now = new Date();
+    AtomDate adNow = new AtomDate(now);
+    assertEquals(now, adNow.getDate());
+    
+    // mutate 'now', to assert AtomDate cloned value
+    now.setTime(now.getTime() + 10);
+    assertFalse(now.getTime() == adNow.getTime());
+  }
+
+  @Test
+  public void testGetDate() {
+    Date now = new Date();
+    AtomDate adNow = new AtomDate(now);
+    assertEquals(now, adNow.getDate());
+    
+    // getDate, then mutate to assert it was cloned
+    Date now2 = adNow.getDate();
+    now2.setTime(now2.getTime() + 10);
+    assertFalse(now2.equals(adNow.getDate()));
+  }
+
+  @Test
+  public void testSetValueDate() {
+    Date now = new Date();
+    AtomDate adNow = new AtomDate(now);
+    assertEquals(now, adNow.getDate());
+    
+    // set time, then mutate 'now' to assert cloning
+    now.setTime(now.getTime() + 10);
+    adNow.setValue(now);
+    now.setTime(now.getTime() + 10);
+    assertFalse(now.equals(adNow.getDate()));
+  }
+
+  @Test
+  public void testClone() {
+    AtomDate ad = new AtomDate();
+    AtomDate adClone = (AtomDate) ad.clone();
+    assertEquals(ad, adClone);
+    assertNotSame(ad, adClone);
+  }
+
+}
+package org.apache.abdera.model;
+
+import static org.junit.Assert.*;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+public class AtomDateTest {
+
+  @Test
+  public void testHashCode() {
+    long time = System.currentTimeMillis();
+    
+    AtomDate ad1 = new AtomDate(time);
+    assertTrue(ad1.hashCode() == ad1.hashCode());
+    
+    AtomDate ad2 = new AtomDate(time + 10);
+    assertFalse(ad1.hashCode() == ad2.hashCode());
+    
+  }
+
+  @Test
+  public void testAtomDateDate() {
+    Date now = new Date();
+    AtomDate adNow = new AtomDate(now);
+    assertEquals(now, adNow.getDate());
+    
+    // mutate 'now', to assert AtomDate cloned value
+    now.setTime(now.getTime() + 10);
+    assertFalse(now.getTime() == adNow.getTime());
+  }
+
+  @Test
+  public void testGetDate() {
+    Date now = new Date();
+    AtomDate adNow = new AtomDate(now);
+    assertEquals(now, adNow.getDate());
+    
+    // getDate, then mutate to assert it was cloned
+    Date now2 = adNow.getDate();
+    now2.setTime(now2.getTime() + 10);
+    assertFalse(now2.equals(adNow.getDate()));
+  }
+
+  @Test
+  public void testSetValueDate() {
+    Date now = new Date();
+    AtomDate adNow = new AtomDate(now);
+    assertEquals(now, adNow.getDate());
+    
+    // set time, then mutate 'now' to assert cloning
+    now.setTime(now.getTime() + 10);
+    adNow.setValue(now);
+    now.setTime(now.getTime() + 10);
+    assertFalse(now.equals(adNow.getDate()));
+  }
+
+  @Test
+  public void testClone() {
+    AtomDate ad = new AtomDate();
+    AtomDate adClone = (AtomDate) ad.clone();
+    assertEquals(ad, adClone);
+    assertNotSame(ad, adClone);
+  }
+
+}



Mime
View raw message