Skip to content

Commit 313c795

Browse files
author
Gabriel Roldan
committed
A couple additional utility methods on TestData
Signed-off-by: Gabriel Roldan <[email protected]>
1 parent 7c0d2f1 commit 313c795

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

src/core/src/test/java/org/locationtech/geogig/test/TestData.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Iterator;
1818
import java.util.List;
1919
import java.util.Map;
20+
import java.util.UUID;
2021

2122
import org.geotools.data.DataUtilities;
2223
import org.geotools.data.memory.MemoryDataStore;
@@ -33,8 +34,11 @@
3334
import org.locationtech.geogig.model.impl.RevFeatureBuilder;
3435
import org.locationtech.geogig.model.impl.RevFeatureTypeBuilder;
3536
import org.locationtech.geogig.plumbing.LsTreeOp;
37+
import org.locationtech.geogig.plumbing.LsTreeOp.Strategy;
3638
import org.locationtech.geogig.plumbing.RefParse;
3739
import org.locationtech.geogig.plumbing.RevParse;
40+
import org.locationtech.geogig.plumbing.TransactionBegin;
41+
import org.locationtech.geogig.plumbing.TransactionResolve;
3842
import org.locationtech.geogig.porcelain.AddOp;
3943
import org.locationtech.geogig.porcelain.BranchCreateOp;
4044
import org.locationtech.geogig.porcelain.CheckoutOp;
@@ -65,6 +69,7 @@
6569
import com.google.common.base.Optional;
6670
import com.google.common.collect.Iterators;
6771
import com.google.common.collect.Lists;
72+
import com.google.common.collect.Maps;
6873

6974
/**
7075
* A helper class to set repositories to a desired state to aid in integration testing.
@@ -183,7 +188,46 @@ public void setTransaction(GeogigTransaction transaction) {
183188
this.transaction = transaction;
184189
}
185190

186-
private Context getContext() {
191+
/**
192+
* Opposite of {@link #resumeTransaction}, shorthand for {@link #setTransaction
193+
* setTransaction(null)}
194+
*/
195+
public TestData exitFromTransaction() {
196+
setTransaction(null);
197+
return this;
198+
}
199+
200+
public TestData resumeTransaction(UUID transactionId) {
201+
checkState(transaction == null, "There's a transaction already running");
202+
Optional<GeogigTransaction> tx = getContext().command(TransactionResolve.class)
203+
.setId(transactionId).call();
204+
checkState(tx.isPresent(), "Transaction %s does not exist", transactionId);
205+
setTransaction(tx.get());
206+
return this;
207+
}
208+
209+
public TestData startTransaction() {
210+
checkState(transaction == null, "There's a transaction already running");
211+
GeogigTransaction tx = getContext().command(TransactionBegin.class).call();
212+
setTransaction(tx);
213+
return this;
214+
}
215+
216+
public TestData commitTransaction() {
217+
checkState(transaction != null, "There's no transaction active");
218+
transaction.commit();
219+
setTransaction(null);
220+
return this;
221+
}
222+
223+
public TestData abortTransaction() {
224+
checkState(transaction != null, "There's no transaction active");
225+
transaction.abort();
226+
setTransaction(null);
227+
return this;
228+
}
229+
230+
public Context getContext() {
187231
if (transaction != null) {
188232
return transaction;
189233
}
@@ -311,15 +355,16 @@ public TestData addAndCommit(String commitMessage, SimpleFeature... features) {
311355
}
312356

313357
public TestData insert(SimpleFeature... features) {
314-
WorkingTree workingTree = getContext().workingTree();
358+
Context context = getContext();
359+
WorkingTree workingTree = context.workingTree();
315360
Map<FeatureType, RevFeatureType> types = new HashMap<>();
316361
for (SimpleFeature sf : features) {
317362
SimpleFeatureType ft = sf.getType();
318363
RevFeatureType rft = types.get(ft);
319364
if (null == rft) {
320365
rft = RevFeatureTypeBuilder.build(ft);
321366
types.put(ft, rft);
322-
getContext().objectDatabase().put(rft);
367+
context.objectDatabase().put(rft);
323368
}
324369
String parentTreePath = ft.getName().getLocalPart();
325370
String path = NodeRef.appendChild(parentTreePath, sf.getID());
@@ -338,6 +383,12 @@ public TestData remove(SimpleFeature... features) {
338383
return this;
339384
}
340385

386+
public Map<String, NodeRef> getFeatureNodes(String treeIsh) {
387+
Iterator<NodeRef> refs = getContext().command(LsTreeOp.class).setReference(treeIsh)
388+
.setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).call();
389+
return Maps.uniqueIndex(refs, n -> n.path());
390+
}
391+
341392
public TestData remove(String... featureIds) {
342393
WorkingTree workingTree = getContext().workingTree();
343394
workingTree.delete(Arrays.asList(featureIds).iterator(), new DefaultProgressListener());
@@ -384,4 +435,9 @@ public TestData resetHard(ObjectId id) {
384435
return this;
385436
}
386437

438+
public static SimpleFeature clone(SimpleFeature f) {
439+
SimpleFeatureBuilder cloner = new SimpleFeatureBuilder(f.getFeatureType());
440+
cloner.init(f);
441+
return cloner.buildFeature(f.getID());
442+
}
387443
}

0 commit comments

Comments
 (0)