J2JS
Home > Projects > j2js-Runtime > test-src > j2js > testing > cases > CookieTestCase.java
package j2js.testing.cases;

import java.util.Random;

import j2js.client.Cookie;
import junit.framework.TestCase;

/* Test  JavaScript code for cookies. 
var parts = window.location.pathname.split("/");

var path = "";
for (var i=0; i<parts.length-1; i++) {
   path = path + parts[i] + "/";
   document.cookie = "foobar=" + path + ";path=" + path + ";";
}

alert(document.cookie)
*/
public class CookieTestCase extends TestCase {

    public void testDelete() {
        Cookie cookie = new Cookie("foo", "bar");
        cookie.save();
        assertTrue(Cookie.loadCookie("foo") != null);
        
        cookie.delete();
        assertTrue(Cookie.loadCookie("foo") == null);
    }
    
    public void testStore() {
        Random random = new Random();
        String value = "bar" + random.nextInt();
        Cookie cookie = new Cookie("testStore", value);
        cookie.save();
        
        cookie = Cookie.loadCookie("testStore");
        assertTrue(cookie != null);
        assertEquals(value, cookie.getValue());
    }
    
    public void testPersistentStore() {
        String value = "bar";
        Cookie cookie = Cookie.loadCookie("testPersistentStore");
        try {
            assertTrue("Cookie not found (may succeed the second time)", cookie != null);
            assertEquals(value, cookie.getValue());
        } finally {
            // Save a cookie for the next test run!
            cookie = new Cookie("testPersistentStore", value);
            cookie.save();
        }
    }
    
}

Extracted from /projects/j2js-Runtime/test-src/j2js/testing/cases/CookieTestCase.java
Last build on Sat Jan 03 11:07:06 CET 2009