001: package org.drools.scm.jcr;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.drools.repository.AssetItem;
008: import org.drools.repository.PackageItem;
009: import org.drools.repository.RepositorySessionUtil;
010: import org.drools.repository.RulesRepository;
011: import org.drools.scm.CompositeScmAction;
012: import org.drools.scm.ScmAction;
013: import org.drools.scm.ScmActionFactory;
014: import org.drools.scm.ScmEntry;
015: import org.drools.scm.jcr.JcrActionFactory.AddFile;
016: import org.drools.scm.jcr.JcrActionFactory.AddDirectory;
017:
018: import junit.framework.TestCase;
019:
020: public class JcrActionFactoryTest extends TestCase {
021:
022: public void testMapPathNameToPackage() {
023: JcrActionFactory fact = new JcrActionFactory(null);
024: assertEquals("org.foo.bar", fact.toPackageName("org/foo/bar"));
025: assertEquals("foo", fact.toPackageName("foo"));
026: assertEquals("FooBar", fact.toPackageName("FooBar"));
027:
028: assertEquals("org/foo/bar", fact.toDirectoryName("org.foo.bar"));
029: assertEquals("foo", fact.toDirectoryName("foo"));
030: }
031:
032: public void testAddDirectories() throws Exception {
033: ScmActionFactory svn = new JcrActionFactory(
034: RepositorySessionUtil.getRepository());
035:
036: CompositeScmAction actions = new CompositeScmAction();
037:
038: // Correctly add a new Directory at root
039: actions = new CompositeScmAction();
040: ScmAction addDirectory = new AddDirectory("", "folder1");
041: actions.addScmAction(addDirectory);
042:
043: svn.execute(actions, "test message");
044:
045: // Now check various flat and deep Directory creations
046: actions = new CompositeScmAction();
047:
048: addDirectory = new AddDirectory("folder1", "folder1_1");
049: actions.addScmAction(addDirectory);
050:
051: addDirectory = new AddDirectory("folder1/folder1_1",
052: "folder1_1_1");
053: actions.addScmAction(addDirectory);
054:
055: addDirectory = new AddDirectory("folder1", "folder1_2");
056: actions.addScmAction(addDirectory);
057:
058: addDirectory = new AddDirectory("", "folder2/folder2_1");
059: actions.addScmAction(addDirectory);
060:
061: addDirectory = new AddDirectory("",
062: "folder3/folder3_1/folder3_1_1/folder3_1_1_1");
063: actions.addScmAction(addDirectory);
064:
065: svn.execute(actions, "test message");
066: //------
067: // Now test results
068: //-------
069: List list = convertToStringList(svn.listEntries(""));
070:
071: assertTrue(list.contains("folder1"));
072: assertTrue(list.contains("folder1/folder1_1"));
073: assertTrue(list.contains("folder1/folder1_2"));
074: assertTrue(list.contains("folder2/folder2_1"));
075: assertTrue(list
076: .contains("folder3/folder3_1/folder3_1_1/folder3_1_1_1"));
077: }
078:
079: public void testAddFiles() throws Exception {
080: RulesRepository repo = RepositorySessionUtil.getRepository();
081:
082: repo.createPackage("testAddFiles.package", "just for testing");
083:
084: JcrActionFactory fact = new JcrActionFactory(repo);
085:
086: byte[] data = "this is content".getBytes();
087: ScmAction action = fact.addFile("testAddFiles/package",
088: "someFile.drl", data);
089:
090: fact.execute(action, "some message");
091:
092: PackageItem pk = repo.loadPackage("testAddFiles.package");
093: AssetItem asset = pk.loadAsset("someFile");
094:
095: assertEquals("drl", asset.getFormat());
096: assertEquals("this is content", asset.getContent());
097: assertEquals("some message", asset.getDescription());
098: assertEquals("Draft", asset.getStateDescription());
099: }
100:
101: public void testUpdateFiles() throws Exception {
102: RulesRepository repo = RepositorySessionUtil.getRepository();
103: PackageItem pkg = repo.loadDefaultPackage();
104: AssetItem asset = pkg.addAsset("testUpdateFilesSVN",
105: "something");
106:
107: asset.updateContent("lala");
108: asset.checkin("yeah");
109: long oldVersion = asset.getVersionNumber();
110:
111: JcrActionFactory fact = new JcrActionFactory(repo);
112: ScmAction action = fact.updateFile(
113: RulesRepository.DEFAULT_PACKAGE,
114: "testUpdateFilesSVN.drl", "lala".getBytes(), "lala2"
115: .getBytes());
116:
117: fact.execute(action, "goo");
118:
119: AssetItem asset2 = pkg.loadAsset("testUpdateFilesSVN");
120: assertFalse(oldVersion == asset2.getVersionNumber());
121: assertEquals("lala2", asset2.getContent());
122: assertEquals("goo", asset2.getCheckinComment());
123: }
124:
125: public static List convertToStringList(List list) {
126: List files = new ArrayList(list.size());
127:
128: for (Iterator it = list.iterator(); it.hasNext();) {
129: ScmEntry entry = (ScmEntry) it.next();
130: files.add(entry.getPath().equals("") ? entry.getName()
131: : entry.getPath() + "/" + entry.getName());
132: }
133: return files;
134: }
135:
136: }
|