This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b88dbe9
commit 5099a7a
Showing
3 changed files
with
43 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,90 @@ | ||
package edu.stanford; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.xml.transform.*; | ||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
import java.io.*; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class LibraryPatronTest { | ||
public File xslt_file = new File(getClass().getClassLoader().getResource("library_patron.xsl").getFile()); | ||
private File xslt_file; | ||
|
||
@Before | ||
public void init() { | ||
xslt_file = new File(getClass().getClassLoader().getResource("library_patron.xsl").getFile()); | ||
} | ||
|
||
@Test | ||
public void hasActiveIDs() throws Exception { | ||
String symphony_user_rec = symphonyUserRecord(new File(this.getClass().getResource("/patron_one.xml").getFile())); | ||
File fixture_record = new File(this.getClass().getResource("/symph_user_one").getFile()); | ||
String filePath = fixture_record.getPath(); | ||
assertEquals(fileToString(filePath), symphony_user_rec); | ||
String symphony_user_record = symphUser("symph_user_one"); | ||
String flat_user_record = transformedUserRecord("patron_one.xml"); | ||
assertEquals(flat_user_record, symphony_user_record); | ||
} | ||
|
||
@Test | ||
public void hasBlankActiveIDs() throws Exception { | ||
String symphony_user_rec = symphonyUserRecord(new File(this.getClass().getResource("/patron_two.xml").getFile())); | ||
File fixture_record = new File(this.getClass().getResource("/symph_user_two").getFile()); | ||
String filePath = fixture_record.getPath(); | ||
assertEquals(fileToString(filePath), symphony_user_rec); | ||
String symphony_user_record = symphUser("symph_user_two"); | ||
String flat_user_record = transformedUserRecord("patron_two.xml"); | ||
assertEquals(flat_user_record, symphony_user_record); | ||
} | ||
|
||
@Test | ||
public void hasDisplayName() throws Exception { | ||
String symphony_user_rec = symphonyUserRecord(new File(this.getClass().getResource("/patron_three.xml").getFile())); | ||
File fixture_record = new File(this.getClass().getResource("/symph_user_three").getFile()); | ||
String filePath = fixture_record.getPath(); | ||
assertEquals(fileToString(filePath), symphony_user_rec); | ||
String symphony_user_record = symphUser("symph_user_three"); | ||
String flat_user_record = transformedUserRecord("patron_three.xml"); | ||
assertEquals(flat_user_record, symphony_user_record); | ||
} | ||
|
||
@Test | ||
public void usesRegisteredFirstName() throws Exception { | ||
String symphony_user_rec = symphonyUserRecord(new File(this.getClass().getResource("/patron_four.xml").getFile())); | ||
File fixture_record = new File(this.getClass().getResource("/symph_user_four").getFile()); | ||
String filePath = fixture_record.getPath(); | ||
assertEquals(fileToString(filePath), symphony_user_rec); | ||
String symphony_user_record = symphUser("symph_user_four"); | ||
String flat_user_record = transformedUserRecord("patron_four.xml"); | ||
assertEquals(flat_user_record, symphony_user_record); | ||
} | ||
|
||
@Test | ||
public void noNameNodes() throws Exception { | ||
String symphony_user_rec = symphonyUserRecord(new File(this.getClass().getResource("/patron_five.xml").getFile())); | ||
File fixture_record = new File(this.getClass().getResource("/symph_user_five").getFile()); | ||
String filePath = fixture_record.getPath(); | ||
assertEquals(fileToString(filePath), symphony_user_rec); | ||
String symphony_user_record = symphUser("symph_user_five"); | ||
String flat_user_record = transformedUserRecord("patron_five.xml"); | ||
assertEquals(flat_user_record, symphony_user_record); | ||
} | ||
|
||
@Test | ||
public void coterminalStudent() throws Exception { | ||
String symphony_user_rec = symphonyUserRecord(new File(this.getClass().getResource("/patron_six.xml").getFile())); | ||
File fixture_record = new File(this.getClass().getResource("/symph_user_six").getFile()); | ||
String filePath = fixture_record.getPath(); | ||
assertEquals(fileToString(filePath), symphony_user_rec); | ||
String symphony_user_record = symphUser("symph_user_six"); | ||
String flat_user_record = transformedUserRecord("patron_six.xml"); | ||
assertEquals(flat_user_record, symphony_user_record); | ||
} | ||
|
||
public Transformer libraryPatronTransformer(File xslt_file) { | ||
TransformerFactory factory = TransformerFactory.newInstance(); | ||
|
||
Source xslt = new StreamSource(xslt_file); | ||
Transformer transformer = null; | ||
try { | ||
transformer = factory.newTransformer(xslt); | ||
} catch (TransformerConfigurationException e) { | ||
e.printStackTrace(); | ||
} | ||
return transformer; | ||
private String symphUser(String resourceName) throws URISyntaxException, IOException { | ||
return new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(resourceName).toURI()))); | ||
} | ||
|
||
public String symphonyUserRecord(File personXmlFile) throws IOException, TransformerException { | ||
Source text = new StreamSource(personXmlFile); | ||
private String transformedUserRecord(String filename) throws IOException, TransformerException { | ||
Source text = new StreamSource(getClass().getClassLoader().getResourceAsStream(filename)); | ||
StringWriter outWriter = new StringWriter(); | ||
libraryPatronTransformer(xslt_file).transform(text, new StreamResult(outWriter)); | ||
StringBuffer sb = outWriter.getBuffer(); | ||
return sb.toString(); | ||
} | ||
|
||
public String fileToString(String filePath) throws IOException { | ||
StringBuilder contentBuilder = new StringBuilder(); | ||
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { | ||
String sCurrentLine; | ||
while ((sCurrentLine = br.readLine()) != null) { | ||
contentBuilder.append(sCurrentLine).append("\n"); | ||
} | ||
} | ||
catch (IOException e) { | ||
private Transformer libraryPatronTransformer(File xslt_file) { | ||
TransformerFactory factory = TransformerFactory.newInstance(); | ||
|
||
Source xslt = new StreamSource(xslt_file); | ||
Transformer transformer = null; | ||
try { | ||
transformer = factory.newTransformer(xslt); | ||
} catch (TransformerConfigurationException e) { | ||
e.printStackTrace(); | ||
} | ||
return contentBuilder.toString(); | ||
return transformer; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ FORM=LDUSER | |
.USER_XINFO_BEGIN. | ||
.ACTIVEID. |a | ||
.AFFIL1. |astaff | ||
.USER_XINFO_END. | ||
.USER_XINFO_END. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ FORM=LDUSER | |
.USER_XINFO_BEGIN. | ||
.ACTIVEID. |a | ||
.AFFIL1. |astaff | ||
.USER_XINFO_END. | ||
.USER_XINFO_END. |