001: package org.drools.repository;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import junit.framework.TestCase;
008:
009: public class ArchiveItemTest extends TestCase {
010:
011: private RulesRepository getRepo() {
012: return RepositorySessionUtil.getRepository();
013: }
014:
015: private PackageItem getDefaultPackage() {
016: return getRepo().loadDefaultPackage();
017: }
018:
019: public void testFindArchivedAssets() throws Exception {
020: RulesRepository repo = RepositorySessionUtil.getRepository();
021:
022: repo.loadDefaultPackage().addAsset("testFindArchivedAssets1",
023: "X");
024: repo.loadDefaultPackage().addAsset("testFindArchivedAssets2",
025: "X");
026: repo.loadDefaultPackage().addAsset("testFindArchivedAssets3",
027: "X");
028: repo.loadDefaultPackage().addAsset("testFindArchivedAssets4",
029: "X");
030:
031: AssetItem item = RepositorySessionUtil.getRepository()
032: .loadDefaultPackage().loadAsset(
033: "testFindArchivedAssets1");
034: assertFalse(item.isArchived());
035: item.archiveItem(true);
036: item.checkin("archiving item 1");
037: item = RepositorySessionUtil.getRepository()
038: .loadDefaultPackage().loadAsset(
039: "testFindArchivedAssets1");
040: assertTrue(item.isArchived());
041:
042: item = RepositorySessionUtil.getRepository()
043: .loadDefaultPackage().loadAsset(
044: "testFindArchivedAssets2");
045: assertFalse(item.isArchived());
046: item.archiveItem(true);
047: item.checkin("archiving item 1");
048: item = RepositorySessionUtil.getRepository()
049: .loadDefaultPackage().loadAsset(
050: "testFindArchivedAssets2");
051: assertTrue(item.isArchived());
052:
053: item = RepositorySessionUtil.getRepository()
054: .loadDefaultPackage().loadAsset(
055: "testFindArchivedAssets3");
056: assertFalse(item.isArchived());
057: item.archiveItem(true);
058: item.checkin("archiving item 1");
059: item = RepositorySessionUtil.getRepository()
060: .loadDefaultPackage().loadAsset(
061: "testFindArchivedAssets3");
062: assertTrue(item.isArchived());
063:
064: AssetItemIterator it = repo.findArchivedAssets();
065:
066: List list = iteratorToList(it);
067: assertEquals(3, list.size());
068:
069: }
070:
071: public void testArchiveBooleanFlag() throws Exception {
072: AssetItem item = RepositorySessionUtil.getRepository()
073: .loadDefaultPackage().addAsset(
074: "testArchiveBooleanFlag", "yeah");
075:
076: assertFalse(item.isArchived());
077: item.archiveItem(true);
078: assertFalse(item.getNode().hasProperty(
079: AssetItem.CONTENT_PROPERTY_NAME));
080: assertTrue(item.getNode().hasProperty(
081: AssetItem.CONTENT_PROPERTY_ARCHIVE_FLAG));
082: assertTrue(item.isArchived());
083: item.checkin("lalalala");
084: }
085:
086: public void testLoadArchivedAsset() throws Exception {
087: AssetItem item = RepositorySessionUtil.getRepository()
088: .loadDefaultPackage().loadAsset(
089: "testArchiveBooleanFlag");
090: System.out.println(item);
091: assertTrue(item.isArchived());
092: }
093:
094: public void testFindArchivedAssetsByCategory() {
095: getRepo().loadCategory("/").addCategory(
096: "testFindRulesByCatArchive", "yeah");
097: AssetItem as1 = getDefaultPackage().addAsset(
098: "testFindRulesByCategoryArch", "ya",
099: "testFindRulesByCatArchive", "drl");
100: getDefaultPackage().addAsset("testFindRulesByCategoryArch2",
101: "ya", "testFindRulesByCatArchive",
102: AssetItem.DEFAULT_CONTENT_FORMAT).checkin("version0");
103:
104: as1.archiveItem(true);
105: as1.checkin("version0");
106:
107: assertEquals("drl", as1.getFormat());
108:
109: List rules = getRepo().findAssetsByCategory(
110: "testFindRulesByCatArchive");
111: assertEquals(1, rules.size());
112:
113: List rules1 = getRepo().findAssetsByCategory(
114: "testFindRulesByCatArchive", true);
115: assertEquals(2, rules1.size());
116: }
117:
118: List iteratorToList(Iterator it) {
119: List list = new ArrayList();
120: while (it.hasNext()) {
121: list.add(it.next());
122: }
123: return list;
124: }
125:
126: public void testLoadPackages() {
127: RulesRepository repo = getRepo();
128: PackageItem item = repo.createPackage("testListPackagesblah",
129: "lalalala");
130:
131: assertNotNull(item.getCreator());
132: assertFalse(item.getCreator().equals(""));
133:
134: List list = iteratorToList(repo.listPackages());
135: int prevSize = list.size();
136: repo.createPackage("testListPackages2blah", "abc");
137: repo.loadPackage("testListPackages2blah").archiveItem(true);
138: repo.createPackage("testListPackages2blah1", "abc");
139: repo.createPackage("testListPackages2blah3", "abc");
140: repo.createPackage("testListPackages2blah7", "abc");
141:
142: PackageIterator localIterator = (PackageIterator) repo
143: .listPackages();
144:
145: list = iteratorToList(localIterator);
146:
147: assertEquals(prevSize + 3, list.size());
148:
149: localIterator = (PackageIterator) repo.listPackages();
150: localIterator.setArchivedIterator(true);
151: list = iteratorToList(localIterator);
152: assertEquals(prevSize + 4, list.size());
153: }
154:
155: public void testFindRulesByNameArchived() throws Exception {
156: RulesRepository repo = RepositorySessionUtil.getRepository();
157:
158: repo.loadDefaultPackage().addAsset("findRulesByNameArchived1",
159: "X");
160: repo.loadDefaultPackage().addAsset("findRulesByNameArchived2",
161: "X");
162: repo.save();
163:
164: AssetItem item2 = RepositorySessionUtil.getRepository()
165: .loadDefaultPackage().loadAsset(
166: "findRulesByNameArchived1");
167: assertFalse(item2.isArchived());
168:
169: item2.archiveItem(true);
170:
171: // not archived until repository isn't save.
172: List list = iteratorToList(repo
173: .findAssetsByName("findRulesByNameArchived1"));
174: assertEquals(1, list.size());
175:
176: list = iteratorToList(repo.findAssetsByName(
177: "findRulesByNameArchived1", true));
178: assertEquals(1, list.size());
179:
180: list = iteratorToList(repo
181: .findAssetsByName("findRulesByNameArchived2"));
182: assertEquals(1, list.size());
183:
184: list = iteratorToList(repo
185: .findAssetsByName("findRulesByNameArchived%"));
186: assertEquals(2, list.size());
187:
188: repo.createPackageSnapshot(RulesRepository.DEFAULT_PACKAGE,
189: "testFindRulesByName2");
190: repo.save();
191:
192: list = iteratorToList(repo
193: .findAssetsByName("findRulesByNameArchived2"));
194: AssetItem item = (AssetItem) list.get(0);
195: assertEquals("findRulesByNameArchived2", item.getName());
196: assertEquals("X", item.getDescription());
197: assertEquals(1, list.size());
198:
199: list = iteratorToList(repo.findAssetsByName(
200: "findRulesByNameArchived1", true));
201: assertEquals(1, list.size());
202:
203: list = iteratorToList(repo
204: .findAssetsByName("findRulesByNameArchived1"));
205: assertEquals(0, list.size());
206:
207: // return just the un-archived item
208: list = iteratorToList(repo
209: .findAssetsByName("findRulesByNameArchived%"));
210: assertEquals(1, list.size());
211:
212: // return all itens including archived
213: list = iteratorToList(repo.findAssetsByName(
214: "findRulesByNameArchived%", true));
215: assertEquals(2, list.size());
216:
217: }
218:
219: public void testlistbyformatArchived() {
220:
221: }
222:
223: }
|