samples/cloneproblem/test/org/apidesign/cloneproblem/ExploitTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
jtulach@368
     1
package org.apidesign.cloneproblem;
jtulach@368
     2
jtulach@368
     3
import java.util.Date;
jtulach@368
     4
import junit.framework.TestCase;
jtulach@368
     5
jtulach@368
     6
/** Test that would be written by the API hacker.
jtulach@368
     7
 *
jtulach@368
     8
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@368
     9
 */
jtulach@368
    10
public class ExploitTest extends TestCase {
jtulach@368
    11
    public ExploitTest(String name) {
jtulach@368
    12
        super(name);
jtulach@368
    13
    }
jtulach@368
    14
jtulach@368
    15
    public void testExploitOverridableClone() {
jtulach@368
    16
        try {
jtulach@368
    17
            HackedDate now = new HackedDate();
jtulach@368
    18
            HackedDate later = new HackedDate(now.getTime() + 1000);
jtulach@368
    19
            
jtulach@368
    20
            Interval interval = new Interval(now, later);
jtulach@368
    21
            assertEquals("1s", 1000, interval.getLength());
jtulach@368
    22
            
jtulach@368
    23
            fail("And I hoped NullPointerException will be thrown!");
jtulach@368
    24
        }  catch (NullPointerException ex) {
jtulach@368
    25
            // success, the quest is to generate NullPointerException!
jtulach@368
    26
        }
jtulach@368
    27
    }
jtulach@368
    28
jtulach@368
    29
    // BEGIN: interval.exploit
jtulach@368
    30
    private static class HackedDate extends Date {
jtulach@368
    31
        public HackedDate() {
jtulach@368
    32
        }
jtulach@368
    33
jtulach@368
    34
        public HackedDate(long date) {
jtulach@368
    35
            super(date);
jtulach@368
    36
        }
jtulach@368
    37
jtulach@368
    38
        @Override
jtulach@368
    39
        public Object clone() {
jtulach@368
    40
            return null;
jtulach@368
    41
        }
jtulach@368
    42
    }
jtulach@368
    43
    // END: interval.exploit
jtulach@368
    44
}