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 PackageItemTest extends TestCase {
010:
011: public void testListPackages() throws Exception {
012: RulesRepository repo = getRepo();
013: PackageItem item = repo.createPackage("testListPackages1",
014: "lalalala");
015:
016: assertNotNull(item.getCreator());
017:
018: item.updateStringProperty("goo", "whee");
019: assertEquals("goo", item.getStringProperty("whee"));
020: assertFalse(item.getCreator().equals(""));
021:
022: List list = iteratorToList(repo.listPackages());
023: int prevSize = list.size();
024: repo.createPackage("testListPackages2", "abc");
025:
026: list = iteratorToList(repo.listPackages());
027:
028: assertEquals(prevSize + 1, list.size());
029: }
030:
031: public void testRulePackageItem() throws Exception {
032: RulesRepository repo = getRepo();
033:
034: //calls constructor
035: PackageItem rulePackageItem1 = repo.createPackage(
036: "testRulePackage", "desc");
037: assertNotNull(rulePackageItem1);
038: assertEquals("testRulePackage", rulePackageItem1.getName());
039:
040: Iterator it = getRepo().listPackages();
041: assertTrue(it.hasNext());
042:
043: while (it.hasNext()) {
044: PackageItem pack = (PackageItem) it.next();
045: if (pack.getName().equals("testRulePackage")) {
046: return;
047: }
048: }
049: fail("should have picked up the testRulePackage but didnt.");
050:
051: }
052:
053: /**
054: * This is showing how to copy a package with standard JCR
055: */
056: public void testPackageCopy() throws Exception {
057: RulesRepository repo = getRepo();
058:
059: PackageItem pkg = repo.createPackage("testPackageCopy",
060: "this is something");
061:
062: AssetItem it1 = pkg.addAsset("testPackageCopy1", "la");
063: AssetItem it2 = pkg.addAsset("testPackageCopy2", "la");
064:
065: it1.updateContent("new content");
066: it2.updateContent("more content");
067: it1.checkin("c");
068: it2.checkin("c");
069:
070: it1 = pkg.loadAsset("testPackageCopy1");
071: List hist1 = iteratorToList(it1.getHistory());
072: System.out.println(hist1.size());
073:
074: repo.getSession().getWorkspace().copy(pkg.getNode().getPath(),
075: pkg.getNode().getPath() + "_");
076:
077: PackageItem pkg2 = repo.loadPackage("testPackageCopy_");
078: assertNotNull(pkg2);
079:
080: assertEquals(2, iteratorToList(pkg2.getAssets()).size());
081: AssetItem it1_ = pkg2.loadAsset("testPackageCopy1");
082:
083: it1.updateContent("new content2");
084: it1.checkin("la");
085: it1_ = pkg2.loadAsset("testPackageCopy1");
086: assertEquals("new content", it1_.getContent());
087:
088: }
089:
090: public void testPackageSnapshot() throws Exception {
091: RulesRepository repo = getRepo();
092:
093: PackageItem pkg = repo.createPackage("testPackageSnapshot",
094: "this is something");
095: assertFalse(pkg.isSnapshot());
096:
097: AssetItem it1 = pkg.addAsset("testPackageCopy1", "la");
098: AssetItem it2 = pkg.addAsset("testPackageCopy2", "la");
099:
100: it1.updateContent("new content");
101: it1.updateFormat("drl");
102: it2.updateContent("more content");
103: it2.updateFormat("drl");
104: it1.checkin("c");
105: it2.checkin("c");
106:
107: long ver1 = it1.getVersionNumber();
108: long ver2 = it2.getVersionNumber();
109: assertFalse(ver1 == 0);
110:
111: assertEquals(2, iteratorToList(
112: pkg.listAssetsByFormat(new String[] { "drl" })).size());
113: repo.createPackageSnapshot("testPackageSnapshot", "PROD 2.0");
114:
115: //just check we can load it all via UUID as well...
116: PackageItem pkgLoaded = repo.loadPackageSnapshot(
117: "testPackageSnapshot", "PROD 2.0");
118: assertTrue(pkgLoaded.isSnapshot());
119: assertEquals("PROD 2.0", pkgLoaded.getSnapshotName());
120: assertEquals("testPackageSnapshot", pkgLoaded.getName());
121:
122: PackageItem _pkgLoaded = repo.loadPackageByUUID(pkgLoaded
123: .getUUID());
124: assertNotNull(_pkgLoaded);
125: assertEquals(pkgLoaded.getCreatedDate(), _pkgLoaded
126: .getCreatedDate());
127: assertEquals(pkgLoaded.getName(), _pkgLoaded.getName());
128: //assertEquals("testPackageSnapshot", pkgLoaded.getName());
129: List loadedAssets = iteratorToList(pkgLoaded.getAssets());
130: List _loadedAssets = iteratorToList(_pkgLoaded.getAssets());
131: assertEquals(loadedAssets.size(), _loadedAssets.size());
132:
133: //now make some changes on the main line
134: it1.updateContent("XXX");
135: it1.checkin("X");
136: assertFalse(it1.getVersionNumber() == ver1);
137: AssetItem it3 = pkg.addAsset("testPackageCopy3", "x");
138: it3.updateFormat("drl");
139: it3.checkin("a");
140: assertEquals(3, iteratorToList(
141: pkg.listAssetsByFormat(new String[] { "drl" })).size());
142:
143: PackageItem pkg2 = repo.loadPackageSnapshot(
144: "testPackageSnapshot", "PROD 2.0");
145: assertNotNull(pkg2);
146: List snapAssets = iteratorToList(pkg2.getAssets());
147: assertEquals(2, snapAssets.size());
148: assertFalse(pkg2.getUUID().equals(pkg.getUUID()));
149: assertTrue(snapAssets.get(0) instanceof AssetItem);
150: assertTrue(snapAssets.get(1) instanceof AssetItem);
151:
152: AssetItem sn1 = (AssetItem) snapAssets.get(0);
153: AssetItem sn2 = (AssetItem) snapAssets.get(1);
154: assertEquals("la", sn1.getDescription());
155: assertEquals("la", sn2.getDescription());
156: assertEquals(ver1, sn1.getVersionNumber());
157: assertEquals(ver2, sn2.getVersionNumber());
158:
159: assertEquals(2, iteratorToList(
160: pkg2.listAssetsByFormat(new String[] { "drl" })).size());
161:
162: //now check we can list the snappies
163: String[] res = repo.listPackageSnapshots("testPackageSnapshot");
164:
165: assertEquals(1, res.length);
166: assertEquals("PROD 2.0", res[0]);
167:
168: res = repo.listPackageSnapshots("does not exist");
169: assertEquals(0, res.length);
170:
171: repo.removePackageSnapshot("testPackageSnapshot", "XX");
172: //does nothing... but should not barf...
173: try {
174: repo.removePackageSnapshot("NOTHING SENSIBLE", "XX");
175: fail("should not be able to remove this.");
176: } catch (RulesRepositoryException e) {
177: assertNotNull(e.getMessage());
178: }
179:
180: repo.removePackageSnapshot("testPackageSnapshot", "PROD 2.0");
181: repo.save();
182:
183: res = repo.listPackageSnapshots("testPackageSnapshot");
184: assertEquals(0, res.length);
185:
186: repo.createPackageSnapshot("testPackageSnapshot", "BOO");
187: res = repo.listPackageSnapshots("testPackageSnapshot");
188: assertEquals(1, res.length);
189: repo.copyPackageSnapshot("testPackageSnapshot", "BOO", "BOO2");
190: res = repo.listPackageSnapshots("testPackageSnapshot");
191: assertEquals(2, res.length);
192:
193: assertEquals("BOO", res[0]);
194: assertEquals("BOO2", res[1]);
195:
196: }
197:
198: private RulesRepository getRepo() {
199: return RepositorySessionUtil.getRepository();
200: }
201:
202: public void testLoadRulePackageItem() {
203:
204: PackageItem rulePackageItem = getRepo().createPackage(
205: "testLoadRuleRuleItem", "desc");
206:
207: rulePackageItem = getRepo().loadPackage("testLoadRuleRuleItem");
208: assertNotNull(rulePackageItem);
209: assertEquals("testLoadRuleRuleItem", rulePackageItem.getName());
210:
211: assertEquals("desc", rulePackageItem.getDescription());
212: assertEquals(PackageItem.PACKAGE_FORMAT, rulePackageItem
213: .getFormat());
214: // try loading rule package that was not created
215: try {
216: rulePackageItem = getRepo().loadPackage(
217: "anotherRuleRuleItem");
218: fail("Exception not thrown loading rule package that was not created.");
219: } catch (RulesRepositoryException e) {
220: // that is OK!
221: assertNotNull(e.getMessage());
222: }
223: }
224:
225: /**
226: * This will test getting rules of specific versions out of a package.
227: */
228: public void testPackageRuleVersionExtraction() throws Exception {
229: PackageItem pack = getRepo().createPackage("package extractor",
230: "foo");
231:
232: AssetItem rule1 = pack.addAsset("rule number 1", "yeah man");
233: rule1.checkin("version0");
234:
235: AssetItem rule2 = pack.addAsset("rule number 2", "no way");
236: rule2.checkin("version0");
237:
238: AssetItem rule3 = pack.addAsset("rule number 3", "yes way");
239: rule3.checkin("version0");
240:
241: getRepo().save();
242:
243: pack = getRepo().loadPackage("package extractor");
244: List rules = iteratorToList(pack.getAssets());
245: assertEquals(3, rules.size());
246:
247: getRepo().createState("foobar");
248:
249: StateItem state = getRepo().getState("foobar");
250:
251: rule1.updateState("foobar");
252: rule1.checkin("yeah");
253:
254: pack = getRepo().loadPackage("package extractor");
255:
256: rules = iteratorToList(pack.getAssetsWithStatus(state));
257:
258: assertEquals(1, rules.size());
259:
260: //now lets try an invalid state tag
261: getRepo().createState("whee");
262: rules = iteratorToList(pack.getAssetsWithStatus(getRepo()
263: .getState("whee")));
264: assertEquals(0, rules.size());
265:
266: //and Draft, as we start with Draft, should be able to get all three back
267: //although an older version of one of them
268: rules = iteratorToList(pack.getAssetsWithStatus(getRepo()
269: .getState(StateItem.DRAFT_STATE_NAME)));
270: assertEquals(3, rules.size());
271:
272: //now do an update, and pull it out via state
273: rule1.updateContent("new content");
274: getRepo().createState("extractorState");
275: rule1.updateState("extractorState");
276: rule1.checkin("latest");
277:
278: rules = iteratorToList(pack.getAssetsWithStatus(getRepo()
279: .getState("extractorState")));
280: assertEquals(1, rules.size());
281: AssetItem rule = (AssetItem) rules.get(0);
282: assertEquals("new content", rule.getContent());
283:
284: //get the previous one via state
285:
286: getRepo().createState("foobar");
287: rules = iteratorToList(pack.getAssetsWithStatus(getRepo()
288: .getState("foobar")));
289: assertEquals(1, rules.size());
290: AssetItem prior = (AssetItem) rules.get(0);
291:
292: assertFalse("new content".equals(prior.getContent()));
293:
294: }
295:
296: public void testIgnoreState() throws Exception {
297: PackageItem pack = getRepo().createPackage(
298: "package testIgnoreState", "foo");
299:
300: getRepo().createState("x");
301: AssetItem rule1 = pack.addAsset("rule number 1", "yeah man");
302: rule1.updateState("x");
303: rule1.checkin("version0");
304:
305: AssetItem rule2 = pack.addAsset("rule number 2", "no way");
306: rule2.updateState("x");
307: rule2.checkin("version0");
308:
309: AssetItem rule3 = pack.addAsset("rule number 3", "yes way");
310: getRepo().createState("disabled");
311:
312: rule3.updateState("disabled");
313: rule3.checkin("version0");
314:
315: getRepo().save();
316:
317: Iterator result = pack.getAssetsWithStatus(getRepo().getState(
318: "x"), getRepo().getState("disabled"));
319: List l = iteratorToList(result);
320: assertEquals(2, l.size());
321: }
322:
323: public void testDuplicatePackageName() throws Exception {
324: PackageItem pack = getRepo().createPackage("dupePackageTest",
325: "testing for dupe");
326: assertNotNull(pack.getName());
327:
328: try {
329: getRepo().createPackage("dupePackageTest",
330: "this should fail");
331: fail("Should not be able to add a package of the same name.");
332: } catch (RulesRepositoryException e) {
333: assertNotNull(e.getMessage());
334: }
335:
336: }
337:
338: public void testPackageInstanceWrongNodeType() throws Exception {
339: PackageItem pack = getRepo().loadDefaultPackage();
340: AssetItem rule = pack.addAsset("packageInstanceWrongNodeType",
341: "");
342:
343: try {
344: new PackageItem(this .getRepo(), rule.getNode());
345: fail("Can't create a package from a rule node.");
346: } catch (RulesRepositoryException e) {
347: assertNotNull(e.getMessage());
348: }
349:
350: }
351:
352: public void testLoadRulePackageItemByUUID() throws Exception {
353:
354: PackageItem rulePackageItem = getRepo().createPackage(
355: "testLoadRuleRuleItemByUUID", "desc");
356:
357: String uuid = null;
358: uuid = rulePackageItem.getNode().getUUID();
359:
360: rulePackageItem = getRepo().loadPackageByUUID(uuid);
361: assertNotNull(rulePackageItem);
362: assertEquals("testLoadRuleRuleItemByUUID", rulePackageItem
363: .getName());
364:
365: // try loading rule package that was not created
366: try {
367: rulePackageItem = getRepo().loadPackageByUUID(
368: "01010101-0101-0101-0101-010101010101");
369: fail("Exception not thrown loading rule package that was not created.");
370: } catch (RulesRepositoryException e) {
371: // that is OK!
372: assertNotNull(e.getMessage());
373: }
374: }
375:
376: public void testAddRuleRuleItem() {
377: PackageItem rulePackageItem1 = getRepo().createPackage(
378: "testAddRuleRuleItem", "desc");
379:
380: AssetItem ruleItem1 = rulePackageItem1.addAsset(
381: "testAddRuleRuleItem", "test description");
382: ruleItem1.updateContent("test content");
383: ruleItem1.checkin("updated the rule content");
384:
385: Iterator rulesIt = rulePackageItem1.getAssets();
386: assertNotNull(rulesIt);
387: AssetItem first = (AssetItem) rulesIt.next();
388: assertFalse(rulesIt.hasNext());
389: assertEquals("testAddRuleRuleItem", first.getName());
390:
391: //test that it is following the head revision
392: ruleItem1.updateContent("new lhs");
393: ruleItem1.checkin("updated again");
394: rulesIt = rulePackageItem1.getAssets();
395: assertNotNull(rulesIt);
396:
397: List rules = iteratorToList(rulesIt);
398: assertEquals(1, rules.size());
399: assertEquals("testAddRuleRuleItem", ((AssetItem) rules.get(0))
400: .getName());
401: assertEquals("new lhs", ((AssetItem) rules.get(0)).getContent());
402:
403: AssetItem ruleItem2 = rulePackageItem1.addAsset(
404: "testAddRuleRuleItem2", "test content");
405:
406: rules = iteratorToList(rulePackageItem1.getAssets());
407: assertNotNull(rules);
408: assertEquals(2, rules.size());
409:
410: }
411:
412: List iteratorToList(Iterator it) {
413: List list = new ArrayList();
414: while (it.hasNext()) {
415: list.add(it.next());
416: }
417: return list;
418: }
419:
420: public void testGetRules() {
421: PackageItem rulePackageItem1 = getRepo().createPackage(
422: "testGetRules", "desc");
423:
424: AssetItem ruleItem1 = rulePackageItem1.addAsset("testGetRules",
425: "desc");
426: ruleItem1.updateContent("test lhs content");
427:
428: assertTrue(rulePackageItem1.containsAsset("testGetRules"));
429: assertFalse(rulePackageItem1.containsAsset("XXXXYYYYZZZZ"));
430:
431: List rules = iteratorToList(rulePackageItem1.getAssets());
432: assertNotNull(rules);
433: assertEquals(1, rules.size());
434: assertEquals("testGetRules", ((AssetItem) rules.get(0))
435: .getName());
436:
437: AssetItem ruleItem2 = rulePackageItem1.addAsset(
438: "testGetRules2", "desc");
439: ruleItem2.updateContent("test lhs content");
440:
441: rules = iteratorToList(rulePackageItem1.getAssets());
442: assertNotNull(rules);
443: assertEquals(2, rules.size());
444:
445: //now lets test loading rule
446: AssetItem loaded = rulePackageItem1.loadAsset("testGetRules");
447: assertNotNull(loaded);
448: assertEquals("testGetRules", loaded.getName());
449: assertEquals("desc", loaded.getDescription());
450:
451: }
452:
453: public void testToString() {
454: PackageItem rulePackageItem1 = getRepo().createPackage(
455: "testToStringPackage", "desc");
456:
457: AssetItem ruleItem1 = rulePackageItem1.addAsset(
458: "testToStringPackage", "test lhs content");
459: ruleItem1.updateContent("test lhs content");
460:
461: assertNotNull(rulePackageItem1.toString());
462:
463: }
464:
465: public void testRemoveRule() {
466: PackageItem rulePackageItem1 = getRepo().createPackage(
467: "testRemoveRule", "desc");
468:
469: AssetItem ruleItem1 = rulePackageItem1.addAsset(
470: "testRemoveRule", "test lhs content");
471: ruleItem1.updateContent("test lhs content");
472:
473: Iterator rulesIt = rulePackageItem1.getAssets();
474: AssetItem next = (AssetItem) rulesIt.next();
475:
476: assertFalse(rulesIt.hasNext());
477: assertEquals("testRemoveRule", next.getName());
478:
479: ruleItem1.updateContent("new lhs");
480: List rules = iteratorToList(rulePackageItem1.getAssets());
481: assertNotNull(rules);
482: assertEquals(1, rules.size());
483: assertEquals("testRemoveRule", ((AssetItem) rules.get(0))
484: .getName());
485: assertEquals("new lhs", ((AssetItem) rules.get(0)).getContent());
486:
487: AssetItem ruleItem2 = rulePackageItem1.addAsset(
488: "testRemoveRule2", "test lhs content");
489:
490: //remove the rule, make sure the other rule in the pacakge stays around
491: rulePackageItem1.removeAsset(ruleItem1.getName());
492: rulePackageItem1.rulesRepository.save();
493: rules = iteratorToList(rulePackageItem1.getAssets());
494: assertEquals(1, rules.size());
495: assertEquals("testRemoveRule2", ((AssetItem) rules.get(0))
496: .getName());
497:
498: //remove the rule that is following the head revision, make sure the pacakge is now empty
499: rulePackageItem1.removeAsset(ruleItem2.getName());
500: rules = iteratorToList(rulePackageItem1.getAssets());
501: assertNotNull(rules);
502: assertEquals(0, rules.size());
503:
504: }
505:
506: public void testSearchByFormat() throws Exception {
507: PackageItem pkg = getRepo().createPackage("searchByFormat", "");
508: getRepo().save();
509:
510: AssetItem item = pkg.addAsset("searchByFormatAsset1", "");
511: item.updateFormat("xyz");
512: item.checkin("la");
513:
514: item = pkg.addAsset("searchByFormatAsset2", "wee");
515: item.updateFormat("xyz");
516: item.checkin("la");
517:
518: item = pkg.addAsset("searchByFormatAsset3", "wee");
519: item.updateFormat("ABC");
520: item.checkin("la");
521:
522: Thread.sleep(150);
523:
524: AssetItemIterator it = pkg.queryAssets("drools:format='xyz'");
525: List list = iteratorToList(it);
526: assertEquals(2, list.size());
527: assertTrue(list.get(0) instanceof AssetItem);
528: assertTrue(list.get(1) instanceof AssetItem);
529:
530: AssetItemIterator it2 = pkg
531: .listAssetsByFormat(new String[] { "xyz" });
532: List list2 = iteratorToList(it2);
533: assertEquals(2, list2.size());
534: assertTrue(list2.get(0) instanceof AssetItem);
535: assertTrue(list2.get(1) instanceof AssetItem);
536:
537: it2 = pkg.listAssetsByFormat(new String[] { "xyz", "ABC" });
538: list2 = iteratorToList(it2);
539: assertEquals(3, list2.size());
540: assertTrue(list2.get(0) instanceof AssetItem);
541: assertTrue(list2.get(1) instanceof AssetItem);
542: assertTrue(list2.get(2) instanceof AssetItem);
543:
544: }
545:
546: public void testListArchivedAssets() throws Exception {
547: PackageItem pkg = getRepo().createPackage(
548: "org.drools.archivedtest", "");
549: getRepo().save();
550:
551: AssetItem item = pkg.addAsset("archivedItem1", "");
552: item.archiveItem(true);
553: item.checkin("la");
554:
555: item = pkg.addAsset("archivedItem2", "wee");
556: item.archiveItem(true);
557: item.checkin("la");
558:
559: item = pkg.addAsset("archivedItem3", "wee");
560: item.archiveItem(true);
561: item.checkin("la");
562:
563: item = pkg.addAsset("NOTarchivedItem", "wee");
564: item.checkin("la");
565:
566: Thread.sleep(150);
567:
568: AssetItemIterator it = pkg.listArchivedAssets();
569:
570: List list = iteratorToList(it);
571: assertEquals(3, list.size());
572: assertTrue(list.get(0) instanceof AssetItem);
573: assertTrue(list.get(1) instanceof AssetItem);
574: assertTrue(list.get(2) instanceof AssetItem);
575:
576: it = pkg.queryAssets("", true);
577:
578: list = iteratorToList(it);
579: assertEquals(4, list.size());
580:
581: }
582:
583: public void testSortHistoryByVersionNumber() {
584: PackageItem item = new PackageItem();
585: List l = new ArrayList();
586:
587: AssetItem i1 = new MockAssetItem(42);
588: AssetItem i2 = new MockAssetItem(1);
589:
590: l.add(i2);
591: l.add(i1);
592:
593: assertEquals(i2, l.iterator().next());
594:
595: item.sortHistoryByVersionNumber(l);
596:
597: assertEquals(i1, l.iterator().next());
598: }
599:
600: public void testMiscProperties() {
601: PackageItem item = getRepo().createPackage("testHeader", "ya");
602:
603: item.updateHeader("new header");
604: item.updateExternalURI("boo");
605: getRepo().save();
606: assertEquals("new header", item.getHeader());
607: item = getRepo().loadPackage("testHeader");
608: assertEquals("new header", item.getHeader());
609: assertEquals("boo", item.getExternalURI());
610:
611: }
612:
613: public void testGetFormat() {
614: PackageItem rulePackageItem1 = getRepo().createPackage(
615: "testGetFormat", "woot");
616: assertNotNull(rulePackageItem1);
617: assertEquals(PackageItem.PACKAGE_FORMAT, rulePackageItem1
618: .getFormat());
619:
620: }
621:
622: public void testPackageCheckinConfig() {
623: PackageItem item = getRepo().createPackage(
624: "testPackageCheckinConfig", "description");
625:
626: AssetItem rule = item.addAsset("testPackageCheckinConfig", "w");
627: rule.checkin("goo");
628:
629: assertEquals(1, iteratorToList(item.getAssets()).size());
630: item.updateHeader("la");
631: item.checkin("woot");
632:
633: item.updateHeader("we");
634: item.checkin("gah");
635:
636: PackageItem pre = (PackageItem) item.getPrecedingVersion();
637: assertNotNull(pre);
638: assertEquals("la", pre.getHeader());
639:
640: AssetItem rule_ = getRepo().loadAssetByUUID(rule.getUUID());
641: assertEquals(rule.getVersionNumber(), rule_.getVersionNumber());
642:
643: item = getRepo().loadPackage("testPackageCheckinConfig");
644: long v = item.getVersionNumber();
645: item.updateCheckinComment("x");
646: getRepo().save();
647:
648: assertEquals(v, item.getVersionNumber());
649:
650: }
651:
652: static class MockAssetItem extends AssetItem {
653: private long version;
654:
655: MockAssetItem(long ver) {
656: this .version = ver;
657: }
658:
659: public long getVersionNumber() {
660: return this .version;
661: }
662:
663: public boolean equals(Object in) {
664: return in == this ;
665: }
666:
667: public String toString() {
668: return Long.toString(this.version);
669: }
670: }
671: }
|