public void testQueryComputers() throws Exception { String[] classNames = { "ctf.jdo.tc9x.Laptop", "ctf.jdo.tc9x.Laptop", "ctf.jdo.tc9x.Server", "ctf.jdo.tc9x.Server" }; Database database = _category.getDatabase(); database.begin(); OQLQuery query = database.getOQLQuery( "select computer from " + Computer.class.getName() + " as computer order by computer.id"); QueryResults results = query.execute(); if (results.hasMore()) { int counter = 1; while (results.hasMore()) { Computer computer = (Computer) results.next(); assertNotNull(computer); assertEquals(counter, computer.getId()); assertEquals(classNames[counter - 1], computer.getClass().getName()); counter += 1; } } else { fail("Query does not return any Computer instances."); } database.commit(); database.close(); }
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST) public String update( @PathVariable Long id, @RequestParam(required = false) String name, @RequestParam(required = false) String introduced, @RequestParam(required = false) String discontinued, @RequestParam(required = false) String company, ModelMap model, RedirectAttributes redirectAttributes) { ComputerForm form = new ComputerForm(); form.setId(id); form.setName(name); form.setIntroduced(introduced); form.setDiscontinued(discontinued); form.setCompany(company); if (!form.isValid()) { model.addAttribute("form", form); model.addAttribute("companies", companyService.findAll()); return "/edit"; } else { Computer computer = form.toComputer(); computer = computerService.update(computer); redirectAttributes.addFlashAttribute( "success", "Computer with id " + computer.getId() + " has been updated"); return "redirect:/computers"; } }
public void testCreateAndLoadComputer() throws Exception { Database database = _category.getDatabase(); database.begin(); Order order = new Order(); order.setId(12); order.setName("order 12"); database.create(order); database.commit(); database.begin(); ProductDetail detail = new ProductDetail(); detail.setId(12); detail.setCategory("category 12"); detail.setLocation("location 12"); database.create(detail); database.commit(); database.begin(); Laptop laptop = new Laptop(); laptop.setId(12); laptop.setName("laptop 12"); laptop.setCpu("centrino"); laptop.setResolution("1600"); laptop.setWeight(2450); laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(12))); database.create(laptop); Collection orders = new LinkedList(); orders.add(database.load(Order.class, new Integer(12))); laptop.setOrders(orders); database.commit(); database.begin(); Computer computer = (Computer) database.load(Computer.class, new Integer(12)); database.commit(); assertNotNull(computer); assertEquals("ctf.jdo.tc9x.Laptop", computer.getClass().getName()); assertEquals(12, computer.getId()); assertEquals("laptop 12", computer.getName()); database.begin(); computer = (Computer) database.load(Computer.class, new Integer(12)); database.remove(computer); database.remove(database.load(ProductDetail.class, new Integer(12))); database.remove(database.load(Order.class, new Integer(12))); database.commit(); database.close(); }
public void testLoadComputer() throws Exception { Database database = _category.getDatabase(); database.begin(); Computer computer = (Computer) database.load(Computer.class, new Integer(2)); database.commit(); assertNotNull(computer); assertEquals("ctf.jdo.tc9x.Laptop", computer.getClass().getName()); assertEquals(2, computer.getId()); assertEquals("laptop 2", computer.getName()); database.close(); }