[jboss-svn-commits] JBL Code SVN: r14464 - in labs/jbossrules/trunk/drools-repository/src: test/java/org/drools/repository and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Aug 23 01:55:00 EDT 2007
Author: michael.neale at jboss.com
Date: 2007-08-23 01:55:00 -0400 (Thu, 23 Aug 2007)
New Revision: 14464
Modified:
labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java
labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java
Log:
JBRULES-1113 fix for renaming whole packages
Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java 2007-08-23 05:20:52 UTC (rev 14463)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java 2007-08-23 05:55:00 UTC (rev 14464)
@@ -58,7 +58,7 @@
* version). The JCR specification supports a more complicated versioning
* system, and if there is sufficient demand, we can modify our versioning
* scheme to be better aligned with JCR's versioning abilities.
- *
+ *
* @author Ben Truitt
* @author Fernando Meyer
* @author Michael Neale
@@ -109,7 +109,7 @@
/**
* Will add a node named 'nodeName' of type 'type' to 'parent' if such a
* node does not already exist.
- *
+ *
* @param parent
* the parent node to add the new node to
* @param nodeName
@@ -143,7 +143,7 @@
}
/**
- * Explicitly logout of the underlying JCR repository.
+ * Explicitly logout of the underlying JCR repository.
*/
public void logout() {
this.session.logout();
@@ -331,7 +331,7 @@
/**
* Loads a RulePackage for the specified package name. Will throw an
* exception if the specified rule package does not exist.
- *
+ *
* @param name
* the name of the package to load
* @return a RulePackageItem object
@@ -364,7 +364,7 @@
}
}
-
+
public PackageItem loadPackageSnapshot(String packageName, String snapshotName) {
try {
Node n = this.getAreaNode( PACKAGE_SNAPSHOT_AREA ).getNode( packageName ).getNode( snapshotName );
@@ -400,7 +400,7 @@
String source = rulePackageNode.getPath();
this.session.getWorkspace().copy( source, newName );
-
+
} catch ( RepositoryException e ) {
log.error( "Unable to create snapshot", e );
throw new RulesRepositoryException( e );
@@ -434,7 +434,7 @@
/**
* Copies a snapshot to the new location/label.
- *
+ *
* @param packageName
* The name of the package.
* @param snapshotName
@@ -480,7 +480,7 @@
/**
* Similar to above. Loads a RulePackage for the specified uuid.
- *
+ *
* @param uuid
* the uuid of the package to load
* @return a RulePackageItem object
@@ -505,7 +505,7 @@
/**
* This will restore the historical version, save, and check it in as a new
* version with the given comment.
- *
+ *
* @param versionToRestore
* @param headVersion
* @param comment
@@ -544,7 +544,7 @@
/**
* Adds a package to the repository.
- *
+ *
* @param name
* what to name the node added
* @param description
@@ -588,7 +588,7 @@
/**
* Gets a StateItem for the specified state name. If a node for the
* specified state does not yet exist, one is first created.
- *
+ *
* @param name
* the name of the state to get
* @return a StateItem object encapsulating the retreived node
@@ -629,7 +629,7 @@
/**
* This will return a category for the given category path.
- *
+ *
* @param tagName
* the name of the tag to get. If the tag to get is within a
* heirarchy of tag nodes, specify the full path to the tag node
@@ -704,7 +704,7 @@
}
/**
- * TODO: comment
+ * TODO: comment
* @return
* @throws IOException
* @throws PathNotFoundException
@@ -734,7 +734,7 @@
}
/**
- *
+ *
* @param byteArray
*/
public void importRulesRepository(byte[] byteArray) {
@@ -751,7 +751,7 @@
}
/**
- *
+ *
* @param parentNode
* @return
* @throws RepositoryException
@@ -805,7 +805,7 @@
/**
* This moves a rule asset from one package to another, preserving history
* etc etc.
- *
+ *
* @param newPackage
* The destination package.
* @param uuid
@@ -835,7 +835,7 @@
}
}
-
+
/**
* This will rename an assset and apply the change immediately.
* @return the UUID of the new asset
@@ -848,8 +848,8 @@
String sourcePath = node.getPath();
String destPath = node.getParent().getPath() + "/" + newAssetName;
this.session.move( sourcePath, destPath );
-
-
+
+
itemOriginal.updateTitle( newAssetName );
itemOriginal.checkin( "Renamed asset " + itemOriginal.getName());
return itemOriginal.getUUID();
@@ -858,7 +858,7 @@
throw new RulesRepositoryException( e );
}
}
-
+
/**
* This will rename a package and apply the change immediately.
* @return the UUID of the package
@@ -871,16 +871,26 @@
String sourcePath = node.getPath();
String destPath = node.getParent().getPath() + "/" + newPackageName;
this.session.move( sourcePath, destPath );
-
-
+
+
itemOriginal.updateTitle( newPackageName );
itemOriginal.checkin( "Renamed package " + itemOriginal.getName());
+
+ PackageItem newPkg = loadPackage(newPackageName);
+
+ for ( Iterator iter = newPkg.getAssets(); iter.hasNext(); ) {
+ AssetItem as = (AssetItem) iter.next();
+ as.updateStringProperty( newPackageName, AssetItem.PACKAGE_NAME_PROPERTY );
+ }
+
+ save();
+
return itemOriginal.getUUID();
} catch (RepositoryException e) {
log.error( e );
throw new RulesRepositoryException( e );
}
- }
+ }
/**
* Return a list of the snapshots available for the given package name.
@@ -903,7 +913,7 @@
throw new RulesRepositoryException( e );
}
}
-
+
public AssetItemIterator findArchivedAssets() {
try {
@@ -923,8 +933,8 @@
throw new RulesRepositoryException( e );
}
}
-
+
/**
* This will search assets, looking for matches against the name.
*/
@@ -935,7 +945,7 @@
sql += " WHERE " + AssetItem.TITLE_PROPERTY_NAME + " LIKE '" + name + "'";
sql += " AND jcr:path LIKE '/" + RULES_REPOSITORY_NAME + "/" + RULE_PACKAGE_AREA + "/%'";
- if ( seekArchived == false ) {
+ if ( seekArchived == false ) {
sql += " AND " + AssetItem.CONTENT_PROPERTY_ARCHIVE_FLAG + " = 'false'";
}
@@ -981,7 +991,7 @@
public void copyPackage(String sourcePackageName, String destPackageName) {
PackageItem source = loadPackage( sourcePackageName );
String sourcePath;
-
+
try {
sourcePath = source.getNode().getPath();
@@ -999,7 +1009,7 @@
}
save();
-
+
} catch ( RepositoryException e ) {
log.error( e );
throw new RulesRepositoryException(e);
Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java 2007-08-23 05:20:52 UTC (rev 14463)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java 2007-08-23 05:55:00 UTC (rev 14464)
@@ -17,10 +17,10 @@
import junit.framework.TestCase;
public class RulesRepositoryTest extends TestCase {
-
+
public void testDefaultPackage() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
-
+
Iterator it = repo.listPackages();
boolean foundDefault = false;
while(it.hasNext()) {
@@ -30,184 +30,184 @@
}
}
assertTrue(foundDefault);
-
+
PackageItem def = repo.loadDefaultPackage();
assertNotNull(def);
assertEquals(RulesRepository.DEFAULT_PACKAGE, def.getName());
-
+
String userId = repo.getSession().getUserID();
assertNotNull(userId);
assertFalse(userId.equals( "" ));
-
+
}
-
+
public void testAddVersionARule() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
PackageItem pack = repo.createPackage( "testAddVersionARule", "description" );
repo.save();
-
+
AssetItem rule = pack.addAsset( "my rule", "foobar" );
assertEquals("my rule", rule.getName());
-
+
rule.updateContent( "foo foo" );
rule.checkin( "version0" );
-
+
pack.addAsset( "other rule", "description" );
-
+
rule.updateContent( "foo bar" );
rule.checkin( "version1" );
-
+
PackageItem pack2 = repo.loadPackage( "testAddVersionARule" );
-
+
Iterator it = pack2.getAssets();
-
+
it.next();
it.next();
-
+
assertFalse(it.hasNext());
-
+
AssetItem prev = (AssetItem) rule.getPrecedingVersion();
-
+
assertEquals("foo bar", rule.getContent());
assertEquals("foo foo", prev.getContent());
-
-
-
+
+
+
}
-
+
public void testFindRulesByName() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
-
-
+
+
repo.loadDefaultPackage().addAsset( "findRulesByNamex1", "X" );
repo.loadDefaultPackage().addAsset( "findRulesByNamex2", "X" );
repo.save();
-
-
- List list = iteratorToList(repo.findAssetsByName( "findRulesByNamex1" ));
+
+
+ List list = iteratorToList(repo.findAssetsByName( "findRulesByNamex1" ));
assertEquals(1, list.size());
- list = iteratorToList(repo.findAssetsByName( "findRulesByNamex2" ));
+ list = iteratorToList(repo.findAssetsByName( "findRulesByNamex2" ));
assertEquals(1, list.size());
-
+
list = iteratorToList( repo.findAssetsByName( "findRulesByNamex%") );
assertEquals(2, list.size());
-
-
+
+
repo.createPackageSnapshot( RulesRepository.DEFAULT_PACKAGE, "testFindRulesByName" );
repo.save();
- list = iteratorToList(repo.findAssetsByName( "findRulesByNamex2" ));
+ list = iteratorToList(repo.findAssetsByName( "findRulesByNamex2" ));
AssetItem item = (AssetItem)list.get( 0 );
assertEquals("findRulesByNamex2", item.getName());
assertEquals("X", item.getDescription());
assertEquals(1, list.size());
-
+
list = iteratorToList( repo.findAssetsByName( "findRulesByNamex%" ) );
assertEquals(2, list.size());
-
+
}
-
+
public void testLoadRuleByUUIDWithConcurrentSessions() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
-
+
PackageItem rulePackageItem = repo.loadDefaultPackage();
AssetItem rule = rulePackageItem.addAsset( "testLoadRuleByUUID", "this is a description");
-
+
repo.save();
-
+
String uuid = rule.getNode().getUUID();
AssetItem loaded = repo.loadAssetByUUID(uuid);
assertNotNull(loaded);
assertEquals("testLoadRuleByUUID", loaded.getName());
assertEquals( "this is a description", loaded.getDescription());
-
+
long oldVersionNumber = loaded.getVersionNumber();
-
+
loaded.updateContent( "xxx" );
loaded.checkin( "woo" );
-
-
-
-
+
+
+
+
AssetItem reload = repo.loadAssetByUUID( uuid );
assertEquals("testLoadRuleByUUID", reload.getName());
assertEquals("xxx", reload.getContent());
System.out.println(reload.getVersionNumber());
System.out.println(loaded.getVersionNumber());
assertFalse(reload.getVersionNumber() == oldVersionNumber );
-
- // try loading rule package that was not created
+
+ // try loading rule package that was not created
try {
repo.loadAssetByUUID("01010101-0101-0101-0101-010101010101");
fail("Exception not thrown loading rule package that was not created.");
} catch (RulesRepositoryException e) {
// that is OK!
assertNotNull(e.getMessage());
- }
+ }
//now test concurrent session access...
-
+
AssetItem asset1 = repo.loadDefaultPackage().addAsset( "testMultiSession", "description" );
asset1.updateContent( "yeah" );
asset1.checkin( "boo" );
uuid = asset1.getUUID();
asset1.updateState( "Draft" );
repo.save();
-
+
Session s2 = repo.getSession().getRepository().login(new SimpleCredentials("fdd", "password".toCharArray()));
-
+
RulesRepository repo2 = new RulesRepository(s2);
-
+
AssetItem asset2 = repo2.loadAssetByUUID( uuid );
asset2.updateContent( "yeah 42" );
asset2.checkin( "yeah" );
-
+
asset1 = repo.loadAssetByUUID( uuid );
assertEquals("yeah 42", asset1.getContent());
asset1.updateContent( "yeah 43" );
asset1.checkin( "la" );
-
+
asset2 = repo2.loadAssetByUUID( uuid );
assertEquals( "yeah 43", asset2.getContent() );
}
-
+
public void testAddRuleCalendarWithDates() {
RulesRepository rulesRepository = RepositorySessionUtil.getRepository();
-
+
Calendar effectiveDate = Calendar.getInstance();
Calendar expiredDate = Calendar.getInstance();
expiredDate.setTimeInMillis(effectiveDate.getTimeInMillis() + (1000 * 60 * 60 * 24));
AssetItem ruleItem1 = rulesRepository.loadDefaultPackage().addAsset("testAddRuleCalendarCalendar", "desc");
ruleItem1.updateDateEffective( effectiveDate );
ruleItem1.updateDateExpired( expiredDate );
-
+
assertNotNull(ruleItem1);
assertNotNull(ruleItem1.getNode());
assertEquals(effectiveDate, ruleItem1.getDateEffective());
assertEquals(expiredDate, ruleItem1.getDateExpired());
-
- ruleItem1.checkin( "ho " );
+
+ ruleItem1.checkin( "ho " );
}
public void testGetState() {
RulesRepository rulesRepository = RepositorySessionUtil.getRepository();
-
+
StateItem state0 = rulesRepository.createState( "testGetState" );
assertNotNull(state0);
assertEquals("testGetState", state0.getName());
StateItem stateItem1 = rulesRepository.getState("testGetState");
assertNotNull(stateItem1);
assertEquals("testGetState", stateItem1.getName());
-
+
StateItem stateItem2 = rulesRepository.getState("testGetState");
assertNotNull(stateItem2);
assertEquals("testGetState", stateItem2.getName());
@@ -216,37 +216,37 @@
public void testGetTag() {
RulesRepository rulesRepository = RepositorySessionUtil.getRepository();
-
+
CategoryItem root = rulesRepository.loadCategory( "/" );
CategoryItem tagItem1 = root.addCategory( "testGetTag", "ho");
assertNotNull(tagItem1);
assertEquals("testGetTag", tagItem1.getName());
assertEquals("testGetTag", tagItem1.getFullPath());
-
+
CategoryItem tagItem2 = rulesRepository.loadCategory("testGetTag");
assertNotNull(tagItem2);
assertEquals("testGetTag", tagItem2.getName());
assertEquals(tagItem1, tagItem2);
-
+
//now test getting a tag down in the tag hierarchy
CategoryItem tagItem3 = tagItem2.addCategory( "TestChildTag1", "ka");
assertNotNull(tagItem3);
assertEquals("TestChildTag1", tagItem3.getName());
- assertEquals("testGetTag/TestChildTag1", tagItem3.getFullPath());
+ assertEquals("testGetTag/TestChildTag1", tagItem3.getFullPath());
}
-
-
+
+
public void testListPackages() {
RulesRepository rulesRepository = RepositorySessionUtil.getRepository();
rulesRepository.createPackage("testListPackages", "desc");
-
+
assertTrue(rulesRepository.containsPackage( "testListPackages" ));
assertFalse(rulesRepository.containsPackage( "XXXXXXX" ));
-
+
Iterator it = rulesRepository.listPackages();
assertTrue(it.hasNext());
-
+
boolean found = false;
while ( it.hasNext() ) {
PackageItem element = (PackageItem) it.next();
@@ -258,31 +258,31 @@
System.out.println(element.getName());
}
assertTrue(found);
-
+
}
-
+
/**
* Here we are testing to make sure that category links don't pick up stuff in snapshots area.
*/
public void testCategoriesAndSnapshots() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
repo.loadCategory( "/" ).addCategory( "testCategoriesAndSnapshots", "X" );
-
+
PackageItem pkg = repo.createPackage( "testCategoriesAndSnapshots", "");
pkg.addAsset( "testCat1", "x", "/testCategoriesAndSnapshots", "drl");
pkg.addAsset( "testCat2", "x", "/testCategoriesAndSnapshots", "drl");
repo.save();
-
+
List items = repo.findAssetsByCategory( "/testCategoriesAndSnapshots" );
assertEquals(2, items.size());
-
+
repo.createPackageSnapshot( "testCategoriesAndSnapshots", "SNAP 1" );
items = repo.findAssetsByCategory( "testCategoriesAndSnapshots" );
- assertEquals(2, items.size());
-
-
+ assertEquals(2, items.size());
+
+
}
-
+
public void testMoveRulePackage() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
PackageItem pkg = repo.createPackage( "testMove", "description" );
@@ -290,45 +290,45 @@
r.checkin( "version0" );
String uuid = r.getUUID();
assertEquals("testMove", r.getPackageName());
-
+
repo.save();
-
+
assertEquals(1, iteratorToList( pkg.getAssets()).size());
-
+
repo.createPackage( "testMove2", "description" );
repo.moveRuleItemPackage( "testMove2", r.node.getUUID(), "explanation" );
-
+
pkg = repo.loadPackage( "testMove" );
assertEquals(0, iteratorToList( pkg.getAssets() ).size());
-
+
pkg = repo.loadPackage( "testMove2" );
assertEquals(1, iteratorToList( pkg.getAssets() ).size());
-
+
r = (AssetItem) pkg.getAssets().next();
assertEquals("testMove", r.getName());
assertEquals("testMove2", r.getPackageName());
assertEquals("explanation", r.getCheckinComment());
-
+
AssetItem p = (AssetItem) r.getPrecedingVersion();
assertEquals("testMove", p.getPackageName());
assertEquals("version0", p.getCheckinComment());
assertEquals(uuid, r.getUUID());
}
-
+
public void testCopyAsset() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
repo.createPackage( "testCopyAsset", "asset" );
AssetItem item = repo.loadDefaultPackage().addAsset( "testCopyAssetSource", "desc" );
item.updateContent( "la" );
item.checkin( "" );
-
+
String uuid = repo.copyAsset( item.getUUID(), "testCopyAsset", "testCopyAssetDestination" );
AssetItem dest = repo.loadAssetByUUID( uuid );
assertEquals("la", dest.getContent());
assertEquals("testCopyAsset", dest.getPackageName());
assertFalse(uuid.equals( item.getUUID() ));
}
-
+
public void testRenameAsset() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
repo.createPackage( "testRenameAsset", "asset" );
@@ -346,9 +346,9 @@
item = (AssetItem) assets.get( 0 );
assertEquals("testRename2", item.getName());
assertEquals("la", item.getContent());
-
+
}
-
+
public void testRenamePackage() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
PackageItem original = repo.createPackage( "testRenamePackage", "asset" );
@@ -361,20 +361,21 @@
PackageItem pkg = repo.loadPackageByUUID( uuid );
assertEquals("testRenamePackage2", pkg.getName());
-
+
List assets = iteratorToList( repo.loadPackage( "testRenamePackage2" ).getAssets() );
assertEquals(1, assets.size());
item = (AssetItem) assets.get( 0 );
assertEquals("testRenameAssetSource", item.getName());
assertEquals("la", item.getContent());
-
+ assertEquals("testRenamePackage2", item.getPackageName());
+
List packageFinal = iteratorToList( repo.listPackages() );
assertEquals(packagesOriginal.size(), packageFinal.size());
-
+
}
-
-
-
+
+
+
public void testCopyPackage() throws Exception {
RulesRepository repo = RepositorySessionUtil.getRepository();
PackageItem source = repo.createPackage( "testCopyPackage", "asset" );
@@ -382,60 +383,60 @@
item.updateContent( "la" );
item.checkin( "" );
repo.save();
-
+
repo.copyPackage( "testCopyPackage", "testCopyPackage2" );
PackageItem dest = repo.loadPackage( "testCopyPackage2" );
assertNotNull(dest);
assertFalse( source.getUUID().equals( dest.getUUID() ));
-
+
assertEquals(1, iteratorToList( dest.getAssets()).size());
AssetItem item2 = (AssetItem) dest.getAssets().next();
assertEquals("testCopyPackage", item.getPackageName());
assertEquals("testCopyPackage2", item2.getPackageName());
-
-
+
+
item2.updateContent( "goober choo" );
item2.checkin( "yeah" );
assertEquals("la", item.getContent());
-
-
+
+
try {
repo.copyPackage( "testCopyPackage", "testCopyPackage2" );
fail("should not be able to copy when existing.");
-
+
} catch (RulesRepositoryException e) {
assertNotNull(e.getMessage());
}
-
-
+
+
}
-
-
+
+
public void testListStates() {
RulesRepository repo = RepositorySessionUtil.getRepository();
StateItem[] items = repo.listStates();
assertTrue(items.length > 0);
-
+
repo.createState( "testListStates" );
-
+
StateItem[] items2 = repo.listStates();
assertEquals(items.length + 1, items2.length);
}
-
+
public void testImportExport() {
RulesRepository repo = RepositorySessionUtil.getRepository();
byte []repository_unitest;
byte []repository_backup;
-
+
try {
-
+
repository_backup = repo.dumpRepositoryXml();
assertNotNull( repository_backup );
-
+
repo.createPackage( "testImportExport", "nodescription" );
- repository_unitest = repo.dumpRepositoryXml();
+ repository_unitest = repo.dumpRepositoryXml();
repo.importRulesRepository( repository_backup );
assertFalse( repo.containsPackage( "testImportExport" ) );
repo.importRulesRepository( repository_unitest );
@@ -445,23 +446,23 @@
e.printStackTrace();
}
}
-
+
public void testExportZippedRepository () throws PathNotFoundException, IOException, RepositoryException {
-
+
RulesRepository repo = RepositorySessionUtil.getRepository();
byte []repository_unitest;
-
+
repository_unitest = repo.exportRulesRepository();
-
+
ByteArrayInputStream bin = new ByteArrayInputStream(repository_unitest);
ZipInputStream zis = new ZipInputStream (bin);
-
+
ZipEntry entry = zis.getNextEntry();
assertEquals( entry.getName() , "repository_export.xml" );
assertFalse( entry.isDirectory() );
}
-
-
+
+
List iteratorToList(Iterator it) {
List list = new ArrayList();
while(it.hasNext()) {
More information about the jboss-svn-commits
mailing list