/** * Synchronizes a design document to the Database. * * <p>This method will first try to find a document in the database with the same id as the given * document, if it is not found then the given document will be saved to the database. * * <p>If the document was found in the database, it will be compared with the given document using * {@code equals()}. If both documents are not equal, then the given document will be saved to the * database and updates the existing document. * * @param document The design document to synchronize * @return {@link Response} as a result of a document save or update, or returns {@code null} if * no action was taken and the document in the database is up-to-date with the given document. */ public Response synchronizeWithDb(DesignDocument document) { assertNotEmpty(document, "Document"); DesignDocument documentFromDb = null; try { documentFromDb = getFromDb(document.getId()); } catch (NoDocumentException e) { return dbc.save(document); } if (!document.equals(documentFromDb)) { document.setRevision(documentFromDb.getRevision()); return dbc.update(document); } return null; }
/** * Test of getDescriptorRegistry method, of class * org.netbeans.modules.vmd.api.model.DesignDocument. */ public void testGetDescriptorRegistry() { System.out.println("getDescriptorRegistry"); // NOI18N final DescriptorRegistry result = document.getDescriptorRegistry(); assertNotNull(result); }
/** * Test of getSelectedComponents method, of class * org.netbeans.modules.vmd.api.model.DesignDocument. */ public void testGetSetSelectedComponents() { System.out.println( "getSetSelectedComponents, setSelectedComponents, getSelectionSourceID"); // NOI18N document .getTransactionManager() .writeAccess( new Runnable() { public void run() { String sourceID = "TestSorceId"; // NOI18N DesignComponent comp1 = document.createComponent(FirstCD.TYPEID_CLASS); DesignComponent comp2 = document.createComponent(SecondCD.TYPEID_CLASS); Collection<DesignComponent> selectedComponents = new HashSet<DesignComponent>(); selectedComponents.add(comp1); selectedComponents.add(comp2); document.setSelectedComponents(sourceID, selectedComponents); // NOI18N for (DesignComponent comp : selectedComponents) { boolean result = document.getSelectedComponents().contains(comp); assertTrue(result); } String result = document.getSelectionSourceID(); String expResult = sourceID; assertEquals(expResult, result); } }); }
/** * Gets a design document from desk. * * @param id The document id to get. * @return {@link DesignDocument} */ public DesignDocument getFromDesk(String id) { assertNotEmpty(id, "id"); DesignDocument dd = new DesignDocument(); String rootPath = format("%s/%s/", DESIGN_DOCS_DIR, id); List<String> elements = listResources(rootPath); if (elements == null) { throw new IllegalArgumentException("Design docs directory cannot be empty."); } if (elements.contains(VALIDATE_DOC)) { // validate_doc_update String validateDocPath = format("%s%s/", rootPath, VALIDATE_DOC); List<String> dirList = listResources(validateDocPath); for (String file : dirList) { String contents = readFile(format("/%s%s", validateDocPath, file)); dd.setValidateDocUpdate(contents); break; // only one validate_doc_update file } } // /validate_doc_update Map<String, MapReduce> views = null; if (elements.contains(VIEWS)) { // views String viewsPath = format("%s%s/", rootPath, VIEWS); views = new HashMap<String, MapReduce>(); for (String viewDirName : listResources(viewsPath)) { // views sub-dirs MapReduce mr = new MapReduce(); String viewPath = format("%s%s/", viewsPath, viewDirName); List<String> dirList = listResources(viewPath); for (String fileName : dirList) { // view files String def = readFile(format("/%s%s", viewPath, fileName)); if (MAP_JS.equals(fileName)) mr.setMap(def); else if (REDUCE_JS.equals(fileName)) mr.setReduce(def); } // /foreach view files views.put(viewDirName, mr); } // /foreach views sub-dirs } // /views dd.setId(DESIGN_PREFIX + id); dd.setLanguage(JAVASCRIPT); dd.setViews(views); dd.setFilters(populateMap(rootPath, elements, FILTERS)); dd.setShows(populateMap(rootPath, elements, SHOWS)); dd.setLists(populateMap(rootPath, elements, LISTS)); dd.setUpdates(populateMap(rootPath, elements, UPDATES)); return dd; }
/** * Complex test (addDocument, addComponent, getRootComponent, setRootComponent, createComponent, * of class org.netbeans.modules.vmd.api.model.DesignDocument. */ public void testComplex() { System.out.println( "addDocument, addComponent, getRootComponent, setRootComponent," + // NOI18N " createComponent,getComponentByID" + "getComponentByID"); // NOI18N final DesignDocument instance = ModelTestUtil.createTestDesignDocument(ModelTestUtil.PROJECT_ID); instance .getTransactionManager() .writeAccess( new Runnable() { public void run() { DesignComponent comp1 = instance.createComponent(FirstCD.TYPEID_CLASS); DesignComponent comp2 = instance.createComponent(SecondCD.TYPEID_CLASS); // setRooComponent, addComponent instance.setRootComponent(comp1); comp1.addComponent(comp2); // getComponentByID DesignComponent resultComp2ByID = instance.getComponentByUID(comp2.getComponentID()); DesignComponent expComp2 = comp2; assertEquals(expComp2, resultComp2ByID); // setRootComponent getRootComponent DesignComponent expGetComp = comp1; DesignComponent resultGetComp = instance.getRootComponent(); assertEquals(expGetComp, resultGetComp); // writeProperty to Component comp1.writeProperty( FirstCD.PROPERTY_REFERENCE, PropertyValue.createComponentReference(comp2)); } }); }
/** * Test of getListenerManager method, of class org.netbeans.modules.vmd.api.model.DesignDocument. */ public void testGetListenerManager() { System.out.println("getListenerManager"); // NOI18N ListenerManager listenerManager = document.getListenerManager(); assertNotNull(listenerManager); DesignListener listener = new DesignListener() { public void designChanged(DesignEvent event) { // TODO Fill it with code } }; assertNotNull(listenerManager); // Additional test // TODO Expand this part of some real situatuon with listeneres, documents and components listenerManager.addDesignListener(listener, new DesignEventFilter()); listenerManager.removeDesignListener(listener); }
/** * Test of getTransactionManager method, of class * org.netbeans.modules.vmd.api.model.DesignDocument. */ public void testGetTransactionManager() { System.out.println("getTransactionManager"); // NOI18N1 final long compID = 0; TransactionManager result = document.getTransactionManager(); assertNotNull(result); result.writeAccess( new Runnable() { public void run() { document.createComponent(FirstCD.TYPEID_CLASS); } }); result.readAccess( new Runnable() { public void run() { document.getComponentByUID(compID); } }); }
/** * Test of getDeleteComponent method, of class org.netbeans.modules.vmd.api.model.DesignDocument. */ @SuppressWarnings("deprecation") // NOI18N public void testDeleteComponent() { // TODO Right now there is no way to tested if document is deleted or not. This test only check // if there is any exception rise when component is deleted System.out.println("deleteComponent"); // NOI18N document .getTransactionManager() .writeAccess( new Runnable() { public void run() { // createComponent DesignComponent comp = document.createComponent(FirstCD.TYPEID_CLASS); DesignComponent comp2 = document.createComponent(SecondCD.TYPEID_CLASS); document.setRootComponent(comp); comp.addComponent(comp2); document.deleteComponent(comp2); } }); }
/** * Gets a design document from desk. * * @param id The document id to get. * @return {@link DesignDocument} */ public DesignDocument getFromDesk(String id) { assertNotEmpty(id, "id"); final DesignDocument dd = new DesignDocument(); final String rootPath = format("%s/%s/", DESIGN_DOCS_DIR, id); final List<String> elements = listResources(rootPath); if (elements == null) { throw new IllegalArgumentException("Design docs directory cannot be empty."); } // Views Map<String, MapReduce> views = null; if (elements.contains(VIEWS)) { views = new HashMap<String, MapReduce>(); final String viewsPath = format("%s%s/", rootPath, VIEWS); for (String viewDirName : listResources(viewsPath)) { // views sub-dirs final MapReduce mr = new MapReduce(); final String viewPath = format("%s%s/", viewsPath, viewDirName); final List<String> dirList = listResources(viewPath); for (String fileName : dirList) { // view files final String def = readFile(format("/%s%s", viewPath, fileName)); if (MAP_JS.equals(fileName)) mr.setMap(def); else if (REDUCE_JS.equals(fileName)) mr.setReduce(def); } // /foreach view files views.put(viewDirName, mr); } // /foreach views sub-dirs } // /views dd.setId(DESIGN_PREFIX + id); dd.setLanguage(JAVASCRIPT); dd.setViews(views); dd.setFilters(populateMap(rootPath, elements, FILTERS)); dd.setShows(populateMap(rootPath, elements, SHOWS)); dd.setLists(populateMap(rootPath, elements, LISTS)); dd.setUpdates(populateMap(rootPath, elements, UPDATES)); dd.setValidateDocUpdate(readContent(elements, rootPath, VALIDATE_DOC)); dd.setRewrites( dbc.getGson().fromJson(readContent(elements, rootPath, REWRITES), JsonArray.class)); dd.setFulltext( dbc.getGson().fromJson(readContent(elements, rootPath, FULLTEXT), JsonObject.class)); dd.setIndexes( dbc.getGson().fromJson(readContent(elements, rootPath, INDEXES), JsonObject.class)); return dd; }