import org.opensim.modeling.*;
import java.io.IOException;
import static java.nio.file.StandardOpenOption.*;
import java.nio.file.*;

class TestEditMarkers {
  public static void main(String[] args) {
    try {
        // GUI model loading
        Model model = new Model("gait10dof18musc_subject01.osim");
        OpenSimContext context=null;
        context = new OpenSimContext(model.initSystem(), model);
        MarkerSet markerset = model.getMarkerSet();
        // The following code exercises the Marker->Add New  functionality
        Vec3 offset = new Vec3(0.11, 0.22, 0.33);
        Body body = model.getBodySet().get(0);
        String newMarkerName = "newMarker";
        Marker newMarker = new Marker();
        newMarker.setName(newMarkerName);
        newMarker.set_location(offset);
        newMarker.setParentFrame(body);
        context.cacheModelAndState();
        markerset.adoptAndAppend(newMarker);
        try {
            context.restoreStateFromCachedModel();
        } catch (IOException ex) {
            System.exit(1);
        }
        // This exercises Marker -> delete
        context.cacheModelAndState();
        // we use getComponent to make sure Marker is accessible thru model traversal
        Marker findMarker = Marker.safeDownCast(model.getComponent("newMarker"));
        assert (findMarker != null);
        markerset.remove(findMarker);
        try {
            context.restoreStateFromCachedModel();
        } catch (IOException ex) {
            System.exit(1);
        }
		// Exercise saveToFile
        markerset.print("savedMarkers.xml");
        // Now create a new MarkerSet from the file
        MarkerSet newMarkerSet = new MarkerSet(model, "savedMarkers.xml");
        Marker newMarkerRenamed = newMarkerSet.get(0);
        String addedMarkerName = newMarkerRenamed.getName()+"_renamed";
        newMarkerRenamed.setName(addedMarkerName);
        context.cacheModelAndState();
        markerset.adoptAndAppend(newMarkerRenamed);
        try {
            context.restoreStateFromCachedModel();
        } catch (IOException ex) {
            System.exit(1);
        }
        Marker findMarkerAdded = Marker.safeDownCast(model.getComponent(addedMarkerName));
        assert(findMarkerAdded != null);
        System.exit(0);
    }
    catch(IOException ex){
        System.out.println("Failed to load model or in initSystem");
        System.exit(-1);
    }
    // TODO to cause test to fail: System.exit(-1);
  }

}

@@ -829,13 +829,27 @@
 rame
+Name
 (body
+.getName()
 );%0A 
@@ -894,39 +894,30 @@
        m
-arkerset.adoptAndAppend
+odel.addMarker
 (newMark
@@ -1258,48 +1258,22 @@
 r = 
-M
+m
 arker
-.safeDownCast(model.getComponent(%22
+set.get(
 newM
@@ -1277,18 +1277,20 @@
 ewMarker
-%22)
+Name
 );%0A     
@@ -2137,45 +2137,20 @@
 d = 
-M
+m
 arker
-.safeDownCast(model.getComponen
+set.ge
 t(ad
@@ -2163,17 +2163,16 @@
 kerName)
-)
 ;%0A      

Remove call to access Marker using getComponent per feedback on PR. Also update API calls to latest version on master.