Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 2 One-to-One Association  Recall: IdLastFirstContact_id 7003RogersTom207 7008ThompsonArt190 7012LaneJohn458 7051FlynnMabel856 IdEmail_address 458jlane@sjsu.edu 856mflynn@sjsu.edu 207trogers@sjsu.edu 190athompson@sjsu.edu TeacherContact_InfoStudentContact_Info  Also:

3 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 3 ContactInfo.java @Entity @Table(name="Contact_Info") public class ContactInfo { private long id; private String emailAddress; public ContactInfo() {} public ContactInfo(String address) { this.emailAddress = address; } @Id @GeneratedValue @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; } @Column(name="email_address") public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String address) { this.emailAddress = address; } }

4 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 4 Student.java (One-to-One) @Entity public class Student {... private ContactInfo contactInfo; @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY) @JoinColumn(name="contact_id") public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }... }

5 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 5 Student.java (One-to-One) @Entity public class Student {... public static void load() { Session session = HibernateContext.getSession(); // Load the Student table in a transaction. Transaction tx = session.beginTransaction(); { session.save(new Student("Mary", "Jane", new ContactInfo("mjane@sjsu.edu"))); session.save(new Student("Kim", "Smith", new ContactInfo("ksmith@sjsu.edu"))); session.save(new Student("John", "Doe", new ContactInfo("jdoe@sjsu.edu"))); session.save(new Student("Tim", "Novak", new ContactInfo("tnovak@sjsu.edu"))); session.save(new Student("Leslie", "Klein", new ContactInfo("lklein@sjsu.edu"))); } tx.commit(); session.close(); System.out.println("Student table loaded."); }... }

6 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 6 Student.java (One-to-One) @Entity public class Student {... private ContactInfo contactInfo;... public void print() { System.out.printf("%d: %s %s (%s)\n", id, firstName, lastName, contactInfo.getEmailAddress()); }... } SchoolDemo3

7 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 7 Lazy Fetching  Each Student object has a one-to-one association with a ContactInfo object via its contactInfo field.  Whenever our Java program fetches a record from the Student table, Hibernate does not fetch the associated record from the Contact_Info table until our program references any field of the ContactInfo object. This is a Hibernate performance optimization. private ContactInfo contactInfo;... @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY) @JoinColumn(name="contact_id") public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }

8 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 8 Lazy Initialization Exception  Hibernate throws the dreaded LazyInitializationException whenever your program attempts to access the field of an object whose corresponding table record has not yet been fetched, often due to lazy fetching. In SchoolDemo3, we attempted to print the value of field emailAddress of a ContactInfo object. The corresponding record had not yet been fetched from the Contact_Info table. Hibernate can fetch table records only within the context of a session.

9 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 9 Lazy Initialization Exception  Recall that after we fetched a matching record from the Student table, we closed the session. Therefore, Hibernate couldn’t fetch the associated Contact_Info record, and we got the exception when we attempted to print the emailAddress field of the ContactInfo object. public static Student find(long id) { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Student where id = :idvar"); query.setLong("idvar", id); Student student = (Student) query.uniqueResult(); session.close(); return student; }

10 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 10 Lazy Initialization Exception  One solution: Use eager fetching. Whenever Hibernate fetches a table record, it immediately fetches records from associated tables. private ContactInfo contactInfo;... @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="contact_id") public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }

11 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 11 Lazy Initialization Exception  However, eager fetching is not always a good idea. Don’t defeat Hibernate’s performance optimization, especially if there are many associations and you’re fetching many records!  Better: Keep lazy fetching. Attach the Student object to another Hibernate session. Inside of this session, Hibernate can fetch the corresponding Contact_Info record to allow you to print ContactInfo fields. public void printInSession() { Session session = HibernateContext.getSession(); session.update(this); print(); session.close(); } Student.java

12 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 12 Two Common Errors 1. Attempting to access a Java object whose corresponding database record has not yet been fetched, likely due to lazy fetching. Attach the object to a new session to force Hibernate to fetch the record. 2. Taking a Java object already attached to a session and attempting to attach it to another session. Did you forget to close the first session?

13 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 13 One-to-Many / Many-to-One Associations  The Teacher table has a one-to-many association with the Class table.  The Class table has a many-to-one association with the Teacher table. _ IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel CodeTeacher_idSubject 9087008Data structures 9267003Java programming 9317051Compilers 9517012Software engineering 9747012Operating systems TeacherClass

14 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 14 Teacher.java @Entity public class Teacher {... private List klasses;... @OneToMany(mappedBy="teacher", targetEntity=Klass.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) public List getKlasses() { return klasses; } public void setKlasses(List klasses) { this.klasses = klasses; }... }

15 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 15 Klass.java @Entity @Table(name="Class") public class Klass {... private Teacher teacher;... @ManyToOne @JoinColumn(name="teacher_id") public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; }... }

16 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 16 Klass.java public static void load() { Session session = HibernateContext.getSession(); Teacher rogers = Teacher.find("Rogers"); Teacher thompson = Teacher.find("Thompson"); Teacher lane = Teacher.find("Lane"); Teacher flynn = Teacher.find("Flynn"); Klass java = new Klass("Java programming"); java.setTeacher(rogers); Klass ds = new Klass("Data structures"); ds.setTeacher(thompson); Klass se = new Klass("Software engineering"); se.setTeacher(lane); Klass os = new Klass("Operating systems"); os.setTeacher(lane); Klass compilers = new Klass("Compilers"); compilers.setTeacher(flynn);... } Find each teacher. Create classes and assign teachers to them.

17 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 17 Klass.java public static void load() {... Transaction tx = session.beginTransaction(); { session.save(java); session.save(ds); session.save(se); session.save(os); session.save(compilers); } tx.commit(); session.close(); System.out.println("Class table loaded."); } Persist the objects. SchoolDemo4

18 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 18 Many-to-Many Association IdLastFirst 1001DoeJohn 1005NovakTim 1009KleinLeslie 1014JaneMary 1021SmithKim CodeTeacher_idSubject 9087008Data structures 9267003Java programming 9317051Compilers 9517012Software engineering 9747012Operating systems Student_idClass_code 1001926 1001951 1001908 1005974 1005908 1014931 1021926 1021974 1021931 Student Class Student_Class Class Student Student-Class

19 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 19 Student.java @Entity public class Student {... private List klasses = new ArrayList ();... @ManyToMany @JoinTable(name="Student_Class", joinColumns={@JoinColumn(name="student_id")}, inverseJoinColumns={@JoinColumn(name="class_code")}) public List getKlasses() { return klasses; } public void setKlasses(List klasses) { this.klasses = klasses; }... } Hibernate will automatically create and populate the Student_Class join table. Student_idClass_code 1001926 1001951... Student_Class

20 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 20 Klass.java @Entity @Table(name="Class") public class Klass {... private List students = new ArrayList ();... @ManyToMany @JoinTable(name="Student_Class", joinColumns={@JoinColumn(name="class_code")}, inverseJoinColumns={@JoinColumn(name="student_id")}) public List getStudents() { return students; } public void setStudents(List students) { this.students = students; }... } Student_idClass_code 1001926 1001951... Student_Class

21 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 21 Klass.java public static void load() {... Student doe = Student.find("Doe"); Student jane = Student.find("Jane"); Student novak = Student.find("Novak"); Student smith = Student.find("Smith"); java.getStudents().add(smith); java.getStudents().add(doe); ds.getStudents().add(doe); ds.getStudents().add(novak); se.getStudents().add(doe); os.getStudents().add(novak); os.getStudents().add(smith); compilers.getStudents().add(smith); compilers.getStudents().add(jane);... } SchoolDemo5 Find each student. Assign students to classes.

22 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 22 Software Validation and Verification (V&V)  Validation Work with the client to make sure that the product requirements are complete and correct. Make sure that the proposed product meets all the requirements. “Are we building the right product?”  Verification Work with the developers to make sure that the product is being developed with high quality standards. “Are we building the product right?”

23 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 23 Software Reliability  Reliable software has a low probability of failure while operating for a specified period of time under specified operating conditions.  The specified time period and the specified operating conditions are part of the nonfunctional requirements.  For some applications, reliability may be more important than the other requirements. mission-critical applications medical applications

24 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 24 Software Reliability, cont’d  Reliable software is the result of good software quality assurance (SQA) throughout an application’s life cycle.  Design Good requirements elicitation Good object-oriented design and analysis Good architecture

25 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 25 Software Reliability, cont’d  Development Good management (e.g., source control, reasonable schedules) Good coding practices (e.g., design patterns) Good testing practices  Deployment Preventive maintenance (e.g., training) Performance monitoring Failure analysis (when failures do occur)

26 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 26 Software Testing  What is testing?  What is a successful test?  Who does testing?  When does testing occur?  What are the different types of testing?  What testing tools are available?  How do you know your tests covered everything?  When can you stop testing?

27 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 27 What is testing?  Testing is a systematic procedure to discover faults in software in order to prevent failure.  Failure: A deviation of the software’s behavior from its specified behavior, according to its requirements. Can be minor to major (such as a crash).

28 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 28 What is testing?  Erroneous state: A state that the operating software is in that will lead to a failure. Example: low on memory  Fault: What caused the software to enter an erroneous state. AKA: defect, bug Example: a memory leak fault  erroneous state  failure

29 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 29 What is a successful test?  A successful test is one that finds bugs.  Testing is the opposite of coding. Coding: Create software and try to get it to work. Testing: Break the software and demonstrate that it doesn’t work.

30 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 30 What is a successful test?  Testing and coding require different mind sets.  It can be very difficult for developers to test their own code.  If you wrote the code, you psychologically want it to work and not see it break.  Since you know how the code should be used, you may not think to try using it in ways other than as you intended.

31 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 31 Who does testing?  Developers As difficult as it may be, you must test your own code.  unit testing Test each other’s code  peer testing  Testers Members of the quality assurance (QA) department. Software engineers who did not write the code. Manual writers and trainers who create examples and demos.

32 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 32 Who does testing? cont’d  Users As beta testers As customers

33 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 33 When does testing occur?  Recall the Old Waterfall Model: Requirements Design Implementation Testing  In the new Agile Methodology, testing is part of each and every iteration. Testing occurs throughout development, not just at the end. XXX

34 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 34 What are the different types of testing?  Functional testing Does the code do what it’s supposed to do? Tests derived from use cases.  Unit testing Developers test an individual “unit”. Unit: A small set of related components.  Regression testing Rerun previous tests to ensure that the latest code changes didn’t break something. Often run automatically from scripts.

35 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 35 Different types of testing, cont’d  Integration testing Developers test how well their units work together with other units.  Usability testing Is the user interface easy to use?

36 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 36 Different types of testing, cont’d  System testing Test how an entire system works.  Performance testing How quickly does the system respond?  Stress testing How much can the system tolerate before breaking?

37 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 37 Alpha Testing vs. Beta Testing  Alpha testing Usability and system testing of a nearly complete application in the development environment.  Beta testing Usability and system testing of a complete or nearly complete application in the user’s environment. It is not uncommon for software companies to release an application to the public for beta testing. New releases of web-based applications are put “into beta” by software companies.

38 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 38 Black Box Testing vs. White Box Testing  Black box testing Deals only with the input/output behavior of the unit.  Calling parameters  Return values The internals of the unit are not considered. Commonly known as functional testing.  White box testing Tests the internal behavior of the unit.  execution paths  state transitions Part of each developer’s unit testing.

39 Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 39 Unit Testing  Each unit test focuses on components created by a developer. Done by the developer before committing the code to the source repository. Easier to find and fix bugs when there are fewer components. Bottom-up testing.  Developers create test cases to do unit tests.


Download ppt "CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google