001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.persistence;
022:
023: import com.liferay.portal.NoSuchResourceException;
024: import com.liferay.portal.kernel.bean.BeanLocatorUtil;
025: import com.liferay.portal.model.Resource;
026: import com.liferay.portal.service.persistence.BasePersistenceTestCase;
027:
028: /**
029: * <a href="ResourcePersistenceTest.java.html"><b><i>View Source</i></b></a>
030: *
031: * @author Brian Wing Shun Chan
032: *
033: */
034: public class ResourcePersistenceTest extends BasePersistenceTestCase {
035: protected void setUp() throws Exception {
036: super .setUp();
037:
038: _persistence = (ResourcePersistence) BeanLocatorUtil
039: .locate(_TX_IMPL);
040: }
041:
042: public void testCreate() throws Exception {
043: long pk = nextLong();
044:
045: Resource resource = _persistence.create(pk);
046:
047: assertNotNull(resource);
048:
049: assertEquals(resource.getPrimaryKey(), pk);
050: }
051:
052: public void testRemove() throws Exception {
053: Resource newResource = addResource();
054:
055: _persistence.remove(newResource);
056:
057: Resource existingResource = _persistence
058: .fetchByPrimaryKey(newResource.getPrimaryKey());
059:
060: assertNull(existingResource);
061: }
062:
063: public void testUpdateNew() throws Exception {
064: addResource();
065: }
066:
067: public void testUpdateExisting() throws Exception {
068: long pk = nextLong();
069:
070: Resource newResource = _persistence.create(pk);
071:
072: newResource.setCodeId(nextLong());
073: newResource.setPrimKey(randomString());
074:
075: _persistence.update(newResource);
076:
077: Resource existingResource = _persistence
078: .findByPrimaryKey(newResource.getPrimaryKey());
079:
080: assertEquals(existingResource.getResourceId(), newResource
081: .getResourceId());
082: assertEquals(existingResource.getCodeId(), newResource
083: .getCodeId());
084: assertEquals(existingResource.getPrimKey(), newResource
085: .getPrimKey());
086: }
087:
088: public void testFindByPrimaryKeyExisting() throws Exception {
089: Resource newResource = addResource();
090:
091: Resource existingResource = _persistence
092: .findByPrimaryKey(newResource.getPrimaryKey());
093:
094: assertEquals(existingResource, newResource);
095: }
096:
097: public void testFindByPrimaryKeyMissing() throws Exception {
098: long pk = nextLong();
099:
100: try {
101: _persistence.findByPrimaryKey(pk);
102:
103: fail("Missing entity did not throw NoSuchResourceException");
104: } catch (NoSuchResourceException nsee) {
105: }
106: }
107:
108: public void testFetchByPrimaryKeyExisting() throws Exception {
109: Resource newResource = addResource();
110:
111: Resource existingResource = _persistence
112: .fetchByPrimaryKey(newResource.getPrimaryKey());
113:
114: assertEquals(existingResource, newResource);
115: }
116:
117: public void testFetchByPrimaryKeyMissing() throws Exception {
118: long pk = nextLong();
119:
120: Resource missingResource = _persistence.fetchByPrimaryKey(pk);
121:
122: assertNull(missingResource);
123: }
124:
125: protected Resource addResource() throws Exception {
126: long pk = nextLong();
127:
128: Resource resource = _persistence.create(pk);
129:
130: resource.setCodeId(nextLong());
131: resource.setPrimKey(randomString());
132:
133: _persistence.update(resource);
134:
135: return resource;
136: }
137:
138: private static final String _TX_IMPL = ResourcePersistence.class
139: .getName()
140: + ".transaction";
141: private ResourcePersistence _persistence;
142: }
|