Skip to content

Commit e4adc64

Browse files
committed
#77 Refactored page spec reader so it uses getResourceAsStream when search for specs
1 parent e7c908c commit e4adc64

7 files changed

Lines changed: 65 additions & 56 deletions

File tree

src/main/java/net/mindengine/galen/specs/reader/page/PageSpecLineProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private void importPageSpec(String filePath) throws IOException {
154154
else {
155155
path = filePath;
156156
}
157-
PageSpec spec = pageSpecReader.read(new File(path));
157+
PageSpec spec = pageSpecReader.read(path);
158158
if (spec != null) {
159159
pageSpec.merge(spec);
160160
}

src/main/java/net/mindengine/galen/specs/reader/page/PageSpecReader.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.mindengine.galen.parser.BashTemplateContext;
3030
import net.mindengine.galen.parser.BashTemplateJsFunctions;
3131
import net.mindengine.galen.parser.FileSyntaxException;
32+
import net.mindengine.galen.utils.GalenUtils;
3233

3334
public class PageSpecReader implements BashTemplateJsFunctions {
3435

@@ -39,47 +40,45 @@ public class PageSpecReader implements BashTemplateJsFunctions {
3940
* so they could be used within bash templates
4041
*/
4142
private PageSpec pageSpec;
42-
43+
4344
public PageSpecReader(Browser browser) {
4445
this.browser = browser;
4546
bashTemplateContext = new BashTemplateContext(this);
4647
}
47-
48-
49-
// Browser is used in order to fetch multi object
48+
49+
50+
// Browser is used in order to fetch multi object
5051
// at earlier state so that it is possible to use dynamic ranges
5152
private Browser browser;
52-
53-
53+
5454
// Used to store information about spec files that were already loaded
55-
private Set<String> processedFiles = new HashSet<String>();
56-
57-
public PageSpec read(File file) throws IOException {
58-
String absolutePath = file.getAbsolutePath();
59-
60-
if (processedFiles.contains(absolutePath)) {
61-
return null;
62-
}
63-
else {
64-
processedFiles.add(absolutePath);
65-
return read(new FileInputStream(file), absolutePath, file.getParent());
66-
}
55+
56+
private Set<String> processedFiles = new HashSet<String>();
57+
58+
public PageSpec read(String filePath) throws IOException {
59+
if (processedFiles.contains(filePath)) {
60+
return null;
61+
}
62+
else {
63+
processedFiles.add(filePath);
64+
return read(GalenUtils.findFileOrResourceAsStream(filePath), filePath, GalenUtils.getParentForFile(filePath));
65+
}
6766
}
6867

68+
6969
public PageSpec read(InputStream inputStream) throws IOException {
7070
return read(inputStream, "<unknown location>", ".");
7171
}
72-
73-
72+
7473
public PageSpec read(InputStream inputStream, String fileLocation, String contextPath) throws IOException {
75-
74+
7675
pageSpec = new PageSpec();
7776
PageSpecLineProcessor lineProcessor = new PageSpecLineProcessor(contextPath, this, pageSpec);
78-
77+
7978
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, System.getProperty("file.encoding")));
80-
79+
8180
String line = bufferedReader.readLine();
82-
81+
8382
int lineNumber = 1;
8483
try {
8584
while(line != null) {
@@ -102,21 +101,18 @@ public Browser getBrowser() {
102101
public void setBrowser(Browser browser) {
103102
this.browser = browser;
104103
}
105-
106-
@Override
107104
public int count(String regex) {
108105
String jRegex = regex.replace("*", ".*");
109106
Pattern pattern = Pattern.compile(jRegex);
110-
107+
111108
int count = 0;
112109
for (String name : pageSpec.getObjects().keySet()) {
113110
if (pattern.matcher(name).matches()) {
114111
count ++;
115112
}
116113
}
117-
return count;
114+
return count;
118115
}
119-
120-
116+
121117

122118
}

src/main/java/net/mindengine/galen/suite/actions/GalenPageActionCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public void execute(TestReport report, Browser browser, GalenPageTest pageTest,
7676

7777

7878
boolean isFailed = false;
79-
for (String specFile : specs) {
80-
PageSpec spec = pageSpecReader.read(GalenUtils.findFile(specFile));
79+
for (String specFilePath : specs) {
80+
PageSpec spec = pageSpecReader.read(specFilePath);
8181

8282
SectionFilter sectionFilter = new SectionFilter(includedTags, excludedTags);
8383
List<PageSection> pageSections = spec.findSections(sectionFilter);

src/main/java/net/mindengine/galen/utils/GalenUtils.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import java.awt.Dimension;
1919
import java.awt.Graphics2D;
2020
import java.awt.image.BufferedImage;
21-
import java.io.ByteArrayInputStream;
22-
import java.io.File;
23-
import java.io.IOException;
21+
import java.io.*;
2422
import java.net.URL;
2523
import java.util.Arrays;
2624
import java.util.List;
@@ -257,4 +255,22 @@ public static Object[] listToArray(List<?> list) {
257255
Object[] arr = new Object[list.size()];
258256
return list.toArray(arr);
259257
}
258+
259+
public static String getParentForFile(String filePath) {
260+
if (filePath != null) {
261+
return new File(filePath).getParent();
262+
}
263+
else return null;
264+
}
265+
266+
public static InputStream findFileOrResourceAsStream(String filePath) throws FileNotFoundException {
267+
File file = new File(filePath);
268+
269+
if (file.exists()) {
270+
return new FileInputStream(file);
271+
}
272+
else {
273+
return GalenUtils.class.getResourceAsStream(filePath);
274+
}
275+
}
260276
}

src/main/java/net/mindengine/galen/validation/specs/SpecValidationComponent.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,11 @@ public void check(PageValidation pageValidation, String objectName, SpecComponen
4747
Page objectContextPage = page.createObjectContextPage(mainObjectLocator);
4848

4949
ValidationListener validationListener = pageValidation.getValidationListener();
50-
51-
File file = new File(spec.getSpecPath());
52-
if (!file.exists()) {
53-
throw new SyntaxException("Component spec file not found: " + file.getAbsolutePath());
54-
}
55-
50+
5651
PageSpecReader pageSpecReader = new PageSpecReader(pageValidation.getBrowser());
5752
PageSpec componentPageSpec;
5853
try {
59-
componentPageSpec = pageSpecReader.read(file);
54+
componentPageSpec = pageSpecReader.read(spec.getSpecPath());
6055
} catch (IOException e) {
6156
throw new RuntimeException(e.getMessage(), e);
6257
}

src/test/java/net/mindengine/galen/tests/selenium/GalenSeleniumTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ public void givesErrors_whenValidating_incorrectWebSite() throws Exception {
403403
@Test
404404
public void performsValidations_ofComponentSpecs() throws IOException {
405405
openDriverForPage("page-for-component-specs.html");
406-
PageSpec pageSpec = new PageSpecReader(new SeleniumBrowser(driver)).read(new File(getClass().getResource("/specs/components/spec-for-component-test-main.spec").getFile()));
406+
PageSpec pageSpec = new PageSpecReader(new SeleniumBrowser(driver))
407+
.read(getClass().getResource("/specs/components/spec-for-component-test-main.spec").getFile());
407408

408409
driver.manage().window().setSize(new Dimension(1000, 800));
409410

@@ -483,7 +484,8 @@ public void performsValidations_ofComponentSpecs() throws IOException {
483484
@Test
484485
public void performsValidations_ofComponentSpecs_withFilteredChildSections() throws IOException {
485486
openDriverForPage("page-for-component-specs.html");
486-
PageSpec pageSpec = new PageSpecReader(new SeleniumBrowser(driver)).read(new File(getClass().getResource("/specs/components/spec-for-component-test-main.spec").getFile()));
487+
PageSpec pageSpec = new PageSpecReader(new SeleniumBrowser(driver))
488+
.read(getClass().getResource("/specs/components/spec-for-component-test-main.spec").getFile());
487489

488490
driver.manage().window().setSize(new Dimension(1000, 800));
489491

src/test/java/net/mindengine/galen/tests/specs/reader/PageSpecsReaderTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public void shouldBePossible_toReadSpec_fromInputStream() throws IOException {
7171

7272
@Test
7373
public void shouldBePossible_toReadSpec_fromFile() throws IOException {
74-
PageSpec pageSpec = new PageSpecReader(NO_BROWSER).read(new File(getClass().getResource("/specs.txt").getFile()));
74+
PageSpec pageSpec = new PageSpecReader(NO_BROWSER).read(getClass().getResource("/specs.txt").getFile());
7575
assertThat(pageSpec, is(notNullValue()));
7676
}
7777

7878
@Test
7979
public void shouldLoadSpecSuccessfully() throws IOException {
80-
pageSpec = pageSpecReader.read(new File(getClass().getResource("/specs.txt").getFile()));
80+
pageSpec = pageSpecReader.read(getClass().getResource("/specs.txt").getFile());
8181
assertThat(pageSpec, is(notNullValue()));
8282
}
8383

@@ -120,7 +120,7 @@ public void shouldRead_allSpecs_asSections_markedByTags() {
120120

121121
@Test
122122
public void shouldAlways_provideAsteriskTags_whenFiltering_byIncludedTags() throws IOException {
123-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/specs/spec-asterisk-tags.spec").getFile()));
123+
PageSpec pageSpec = pageSpecReader.read(getClass().getResource("/specs/spec-asterisk-tags.spec").getFile());
124124

125125
assertThat("Total amount of sections should be", pageSpec.getSections().size(), is(3));
126126

@@ -351,7 +351,7 @@ public void givesError_ifThereAre_tabsIndentations() throws Exception {
351351

352352
@Test
353353
public void shouldImport_otherSpecs_fromOtherFiles() throws Exception {
354-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/spec-import-test/main.spec").getFile()));
354+
PageSpec pageSpec = pageSpecReader.read("/spec-import-test/main.spec");
355355

356356
assertThat(pageSpec.getObjects(), hasKey("content"));
357357
assertThat(pageSpec.getObjects(), hasKey("header"));
@@ -378,7 +378,7 @@ public void shouldImport_otherSpecs_fromOtherFiles() throws Exception {
378378

379379
@Test
380380
public void shouldProcess_simpleMathOperations_inParameterizedSpecs() throws IOException {
381-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/spec-math.spec").getFile()));
381+
PageSpec pageSpec = pageSpecReader.read(getClass().getResource("/spec-math.spec").getFile());
382382

383383
List<Spec> specs = pageSpec.getSections().get(0).getObjects().get(0).getSpecs();
384384

@@ -390,7 +390,7 @@ public void shouldProcess_simpleMathOperations_inParameterizedSpecs() throws IOE
390390

391391
@Test
392392
public void shouldParse_conditionalSpecBlocks() throws Exception {
393-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/specs/spec-conditional-simple.spec").getFile()));
393+
PageSpec pageSpec = pageSpecReader.read(getClass().getResource("/specs/spec-conditional-simple.spec").getFile());
394394

395395
List<PageSection> sections = pageSpec.getSections();
396396
assertThat(sections.size(), is(1));
@@ -429,7 +429,7 @@ public void shouldParse_conditionalSpecBlocks() throws Exception {
429429

430430
@Test
431431
public void shouldParse_conditionalSpecBlocks_withOrStatement() throws Exception {
432-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/specs/spec-conditional-or.spec").getFile()));
432+
PageSpec pageSpec = pageSpecReader.read(getClass().getResource("/specs/spec-conditional-or.spec").getFile());
433433

434434
List<PageSection> sections = pageSpec.getSections();
435435
assertThat(sections.size(), is(1));
@@ -479,7 +479,7 @@ public void shouldParse_conditionalSpecBlocks_withOrStatement() throws Exception
479479

480480
@Test
481481
public void shouldParse_conditionalSpecBlocks_inverted() throws Exception {
482-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/specs/spec-conditional-inverted.spec").getFile()));
482+
PageSpec pageSpec = pageSpecReader.read(getClass().getResource("/specs/spec-conditional-inverted.spec").getFile());
483483

484484
List<PageSection> sections = pageSpec.getSections();
485485
assertThat(sections.size(), is(1));
@@ -521,7 +521,7 @@ public void shouldParse_conditionalSpecBlocks_inverted() throws Exception {
521521

522522
@Test
523523
public void shouldParse_conditionalSpecBlocks_withOtherwise() throws Exception {
524-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/specs/spec-conditional-otherwise.spec").getFile()));
524+
PageSpec pageSpec = pageSpecReader.read(getClass().getResource("/specs/spec-conditional-otherwise.spec").getFile());
525525

526526
List<PageSection> sections = pageSpec.getSections();
527527
assertThat(sections.size(), is(1));
@@ -558,7 +558,7 @@ public void shouldParse_conditionalSpecBlocks_withOtherwise() throws Exception {
558558

559559
@Test
560560
public void shouldParse_componentSpecs() throws Exception {
561-
PageSpec pageSpec = pageSpecReader.read(new File(getClass().getResource("/specs/components/spec-for-component-test-main.spec").getFile()));
561+
PageSpec pageSpec = pageSpecReader.read(getClass().getResource("/specs/components/spec-for-component-test-main.spec").getFile());
562562
List<PageSection> sections = pageSpec.getSections();
563563
assertThat(sections.size(), is(1));
564564

@@ -590,7 +590,7 @@ private void assertChildComponentSpec(List<Spec> specs) {
590590

591591
private FileSyntaxException expectExceptionFromReading(String file) throws IOException {
592592
try {
593-
pageSpecReader.read(new File(getClass().getResource(file).getFile()));
593+
pageSpecReader.read(getClass().getResource(file).getFile());
594594
}
595595
catch(FileSyntaxException exception) {
596596
return exception;

0 commit comments

Comments
 (0)