001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.cache.FxCacheException;
037: import com.flexive.shared.mbeans.FxCacheMBean;
038: import org.apache.commons.lang.StringUtils;
039: import org.testng.annotations.Test;
040:
041: import java.util.Set;
042:
043: /**
044: * Tree cache tests.
045: *
046: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
047: */
048: @Test(groups={"ejb","cache"})
049: public class CacheTest {
050: private static final String PATH_TEST = "/unitTests/"
051: + CacheTest.class;
052:
053: @Test
054: public void testGlobalPutGetRemove() throws FxCacheException {
055: FxCacheMBean cache = CacheAdmin.getInstance();
056: assert !cache.globalExists(PATH_TEST, 0) : PATH_TEST
057: + " not empty";
058: for (int i = 0; i < 100; i++) {
059: cache.globalPut(PATH_TEST, i, "Test value " + i);
060: assert cache.globalExists(PATH_TEST, i);
061: }
062: Set keys = cache.globalGetKeys(PATH_TEST);
063: assert keys.size() == 100;
064: for (int i = 0; i < 100; i++) {
065: assert keys.contains(i) : "Key " + i
066: + " not returned by globalGetKeys.";
067: assert ("Test value " + i).equals(cache.globalGet(
068: PATH_TEST, i)) : "Got unexpected cache value: "
069: + cache.globalGet(PATH_TEST, i);
070: cache.globalPut(PATH_TEST, i, "Test: " + i);
071: assert cache.globalExists(PATH_TEST, i);
072: assert ("Test: " + i).equals(cache.globalGet(PATH_TEST, i)) : "Got unexpected cache value: "
073: + cache.globalGet(PATH_TEST, i);
074: cache.globalRemove(PATH_TEST, i);
075: assert !cache.globalExists(PATH_TEST, i) : "Removed key still exists: "
076: + i;
077: }
078: cache.globalPut(PATH_TEST, 0, "Test value");
079: cache.globalPut(PATH_TEST, 1, "Test value");
080: cache.globalRemove(PATH_TEST);
081: assert !cache.globalExists(PATH_TEST, 0) : "Global cache remove did not clear key 0";
082: assert !cache.globalExists(PATH_TEST, 1) : "Global cache remove did not clear key 1";
083: }
084:
085: @Test
086: public void testPutGetRemove() throws FxCacheException {
087: FxCacheMBean cache = CacheAdmin.getInstance();
088: assert !cache.exists(PATH_TEST, 0) : PATH_TEST + " not empty";
089: for (int i = 0; i < 100; i++) {
090: cache.put(PATH_TEST, i, "Test value " + i);
091: assert cache.exists(PATH_TEST, i);
092: }
093: Set keys = cache.getKeys(PATH_TEST);
094: assert keys.size() == 100;
095: for (int i = 0; i < 100; i++) {
096: assert keys.contains(i) : "Key " + i
097: + " not returned by getKeys.";
098: assert ("Test value " + i).equals(cache.get(PATH_TEST, i)) : "Got unexpected cache value: "
099: + cache.get(PATH_TEST, i);
100: cache.put(PATH_TEST, i, "Test: " + i);
101: assert cache.exists(PATH_TEST, i);
102: assert ("Test: " + i).equals(cache.get(PATH_TEST, i)) : "Got unexpected cache value: "
103: + cache.get(PATH_TEST, i);
104: cache.remove(PATH_TEST, i);
105: assert !cache.exists(PATH_TEST, i) : "Removed key still exists: "
106: + i;
107: }
108: cache.put(PATH_TEST, 0, "Test value");
109: cache.put(PATH_TEST, 1, "Test value");
110: cache.remove(PATH_TEST);
111: assert !cache.exists(PATH_TEST, 0) : "Cache remove did not clear key 0";
112: assert !cache.exists(PATH_TEST, 1) : "Cache remove did not clear key 1";
113: }
114:
115: @Test
116: public void testAttributes() {
117: FxCacheMBean cache = CacheAdmin.getInstance();
118: assert StringUtils.isNotBlank(cache.getDeploymentId()) : "Deployment ID not set.";
119: assert cache.getSystemStartTime() < System.currentTimeMillis()
120: && cache.getSystemStartTime() > System
121: .currentTimeMillis() - 60 * 3600 * 1000 : "System start time in the future or more than one hour in the past: "
122: + cache.getSystemStartTime();
123: assert cache.getNodeStartTime() < System.currentTimeMillis()
124: && cache.getNodeStartTime() > System
125: .currentTimeMillis() - 60 * 3600 * 1000 : "Node start time in the future or more than one hour in the past: "
126: + cache.getNodeStartTime();
127: assert cache.getSystemStartTime() <= cache.getNodeStartTime() : "System start time later than node start time.";
128: }
129: }
|