CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 2 One-to-One Association Recall: IdLastFirstContact_id 7003RogersTom ThompsonArt LaneJohn FlynnMabel856 Id _address TeacherContact_InfoStudentContact_Info Also:
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak public class ContactInfo { private long id; private String Address; public ContactInfo() {} public ContactInfo(String address) { this. Address = @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; public String get Address() { return Address; } public void set Address(String address) { this. Address = address; } }
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 4 Student.java public class Student {... private ContactInfo public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }... }
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 5 Student.java 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 session.save(new Student("Kim", "Smith", new session.save(new Student("John", "Doe", new session.save(new Student("Tim", "Novak", new session.save(new Student("Leslie", "Klein", new } tx.commit(); session.close(); System.out.println("Student table loaded."); }... }
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 6 Student.java public class Student {... private ContactInfo contactInfo;... public void print() { System.out.printf("%d: %s %s (%s)\n", id, firstName, lastName, contactInfo.get Address()); }... } SchoolDemo3
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 public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }
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 Address 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.
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 Address 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; }
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 public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }
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
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?
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 Data structures Java programming Compilers Software engineering Operating systems TeacherClass
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 14 public class Teacher {... private List targetEntity=Klass.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) public List getKlasses() { return klasses; } public void setKlasses(List klasses) { this.klasses = klasses; }... }
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak public class Klass {... private public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; }... }
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.
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
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 Data structures Java programming Compilers Software engineering Operating systems Student_idClass_code Student Class Student_Class Class Student Student-Class
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak 19 public class Student {... private List klasses = new 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 Student_Class
Computer Science Dept. Fall 2014: November 10 CS 160: Software Engineering © R. Mak public class Klass {... private List students = new public List getStudents() { return students; } public void setStudents(List students) { this.students = students; }... } Student_idClass_code Student_Class
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.
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?”
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
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
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)
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?
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).
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
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.
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.
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.
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
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
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.
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?
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?
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.
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.
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.