@Test public void departmentTest() throws Exception { Department dpNew = new Department(); em.getTransaction().begin(); dpNew.setName("Bob's Department"); dpNew.setLocation("Baltimore"); em.persist(dpNew); em.getTransaction().commit(); int newId = dpNew.getId(); // test earlier persist Department dpLoad1 = em.find(Department.class, newId); assertEquals(dpLoad1.getId(), newId); assertEquals("Insert didn't work", dpLoad1.getName(), "Bob's Department"); assertEquals("Insert didn't work", dpLoad1.getLocation(), "Baltimore"); // test update em.getTransaction().begin(); dpLoad1.setName("Phil's Department"); dpLoad1.setLocation("Philadelphia"); em.getTransaction().commit(); Department dpLoad2 = em.find(Department.class, newId); assertEquals(dpLoad2.getId(), newId); assertEquals("Update didn't work", dpLoad2.getName(), "Phil's Department"); assertEquals("Update didn't work", dpLoad2.getLocation(), "Philadelphia"); em.getTransaction().begin(); em.remove(dpLoad1); em.getTransaction().commit(); Department shouldBeNull = em.find(Department.class, newId); assertNull("Delete didn't work", shouldBeNull); }
public static void main(String args[]) { Employee hrManager = new Employee( "1", "SpongeBob", "SquarePants", "HR Manager", new Date(System.currentTimeMillis())); Department hr = new Department(); hr.setManager(hrManager); hr.setName("Humane Resources"); try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("hr.ser"))) { out.writeObject(hr); System.out.println("Wrote \"" + hr.getName() + "\" to file"); } catch (IOException e) { System.err.println("Error writing object: " + e.getMessage()); } }
@Override protected Void doInBackground(Void... arg0) { try { XmlResourceParser parser = getResources().getXml(R.xml.departments); int eventType = parser.getEventType(); while (eventType != XmlResourceParser.END_DOCUMENT) { if (eventType == XmlResourceParser.START_TAG && parser.getName().equalsIgnoreCase("department")) { Department d = new Department(); d.setName(parser.getAttributeValue(null, "name")); d.setUrl(parser.getAttributeValue(null, "url")); List<HoursForDayOfWeek> hoursOfOperation = new ArrayList<HoursForDayOfWeek>(); eventType = parser.next(); if (parser.getName().equalsIgnoreCase("hoursofoperation")) { eventType = parser.next(); for (int i = 1; i <= 7; i++) { HoursForDayOfWeek day = new HoursForDayOfWeek(); day.setDayOfWeek(parser.getAttributeIntValue(null, "dayOfWeek", 1)); day.setOpeningTime(parser.getAttributeIntValue(null, "openingTime", 0)); day.setClosingTime(parser.getAttributeIntValue(null, "closingTime", 0)); hoursOfOperation.add(day); eventType = parser.next(); eventType = parser.next(); } d.setHoursOfOperation(hoursOfOperation); } departments.add(d); } else { eventType = parser.next(); } } } catch (MalformedURLException e) { Log.e("GGC-CONNECT", "MalformedURL", e); } catch (XmlPullParserException e) { Log.e("GGC-CONNECT", "XmlPULLParser", e); } catch (IOException e) { Log.e("GGC-CONNECT", "IO", e); } return null; }