|
| 1 | +package org.grails.io.support |
| 2 | + |
| 3 | +import grails.util.BuildSettings |
| 4 | +import org.grails.io.support.GrailsResourceUtils |
| 5 | +import org.grails.io.support.Resource |
| 6 | +import org.grails.io.support.UrlResource |
| 7 | +import org.springframework.mock.web.MockHttpServletRequest |
| 8 | +import org.springframework.mock.web.MockServletContext |
| 9 | +import spock.lang.Specification |
| 10 | + |
| 11 | +class GrailsResourceUtilsSpec extends Specification { |
| 12 | + |
| 13 | + private static final String TEST_URL = "file:///test/grails/app/grails-app/domain/Test.groovy" |
| 14 | + private static final String TEST_PACKAGE_URL = "file:///test/grails/app/grails-app/domain/mycompany/Test.groovy" |
| 15 | + private static final String TEST_CONTROLLER_URL = "file:///test/grails/app/grails-app/controllers/TestController.groovy" |
| 16 | + private static final String TEST_PLUGIN_CTRL = "file:///test/grails/app/plugins/myplugin/grails-app/controllers/TestController.groovy" |
| 17 | + |
| 18 | + private static final String WEBINF_CONTROLLER = "file:///test/grails/app/WEB-INF/grails-app/controllers/TestController.groovy" |
| 19 | + private static final String WEBINF_PLUGIN_CTRL = "file:///test/grails/app/WEB-INF/plugins/myplugin/grails-app/controllers/TestController.groovy" |
| 20 | + |
| 21 | + private static final String UNIT_TESTS_URL = "file:///test/grails/app/grails-tests/SomeTests.groovy" |
| 22 | + |
| 23 | + void testGetArtifactDirectory() { |
| 24 | + expect: |
| 25 | + "controllers" == GrailsResourceUtils.getArtefactDirectory(TEST_CONTROLLER_URL) |
| 26 | + "domain" == GrailsResourceUtils.getArtefactDirectory(TEST_PACKAGE_URL) |
| 27 | + } |
| 28 | + |
| 29 | + void testJavaAndGroovySources() { |
| 30 | + expect: |
| 31 | + "mycompany.Test" == GrailsResourceUtils.getClassName(TEST_PACKAGE_URL) |
| 32 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/domain/mycompany/Test.java").getPath()) |
| 33 | + "Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blahblah/Test.java").getPath()) |
| 34 | + "Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blah-blah/Test.java").getPath()) |
| 35 | + "Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blah--blah/Test.java").getPath()) |
| 36 | + "Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blah_blah/Test.java").getPath()) |
| 37 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blahblah/mycompany/Test.java").getPath()) |
| 38 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blah-blah/mycompany/Test.java").getPath()) |
| 39 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blah--blah/mycompany/Test.java").getPath()) |
| 40 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/grails-app/blah_blah/mycompany/Test.java").getPath()) |
| 41 | + |
| 42 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/main/groovy/mycompany/Test.java").getPath()) |
| 43 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/test/groovy/mycompany/Test.java").getPath()) |
| 44 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/main/java/mycompany/Test.java").getPath()) |
| 45 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/test/java/mycompany/Test.java").getPath()) |
| 46 | + |
| 47 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/main/groovy/mycompany/Test.java").getPath()) |
| 48 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/test/groovy/mycompany/Test.java").getPath()) |
| 49 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/main/java/mycompany/Test.java").getPath()) |
| 50 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/test/java/mycompany/Test.java").getPath()) |
| 51 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/main/groovy/mycompany/Test.groovy").getPath()) |
| 52 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/test/groovy/mycompany/Test.groovy").getPath()) |
| 53 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/main/java/mycompany/Test.groovy").getPath()) |
| 54 | + "mycompany.Test" == GrailsResourceUtils.getClassName(new File("/test/grails/app/src/test/java/mycompany/Test.groovy").getPath()) |
| 55 | + } |
| 56 | + |
| 57 | + void testIsDomainClass() { |
| 58 | + when: |
| 59 | + URL testUrl = new URL(TEST_URL) |
| 60 | + |
| 61 | + then: |
| 62 | + GrailsResourceUtils.isDomainClass(testUrl) |
| 63 | + } |
| 64 | + |
| 65 | + void testGetPathFromRoot() { |
| 66 | + expect: |
| 67 | + "mycompany/Test.groovy" == GrailsResourceUtils.getPathFromRoot(TEST_PACKAGE_URL) |
| 68 | + "Test.groovy" == GrailsResourceUtils.getPathFromRoot(TEST_URL) |
| 69 | + } |
| 70 | + |
| 71 | + void testGetClassNameResource() { |
| 72 | + when: |
| 73 | + Resource r = new UrlResource(new URL(TEST_URL)) |
| 74 | + |
| 75 | + then: |
| 76 | + "Test" == GrailsResourceUtils.getClassName(r) |
| 77 | + } |
| 78 | + |
| 79 | + void testGetClassNameString() { |
| 80 | + expect: |
| 81 | + "Test" == GrailsResourceUtils.getClassName(TEST_URL) |
| 82 | + } |
| 83 | + |
| 84 | + void testIsGrailsPath() { |
| 85 | + expect: |
| 86 | + GrailsResourceUtils.isGrailsPath(TEST_URL) |
| 87 | + } |
| 88 | + |
| 89 | + void testIsTestPath() { |
| 90 | + expect: |
| 91 | + GrailsResourceUtils.isGrailsPath(UNIT_TESTS_URL) |
| 92 | + } |
| 93 | + |
| 94 | + void testGetTestNameResource() { |
| 95 | + when: |
| 96 | + Resource r = new UrlResource(new URL(UNIT_TESTS_URL)) |
| 97 | + |
| 98 | + then: |
| 99 | + "SomeTests" == GrailsResourceUtils.getClassName(r) |
| 100 | + } |
| 101 | + |
| 102 | + void testGetTestNameString() { |
| 103 | + expect: |
| 104 | + "SomeTests" == GrailsResourceUtils.getClassName(UNIT_TESTS_URL) |
| 105 | + } |
| 106 | + |
| 107 | + void testGetViewsDirForURL() { |
| 108 | + when: |
| 109 | + Resource viewsDir = GrailsResourceUtils.getViewsDir(new UrlResource(TEST_CONTROLLER_URL)) |
| 110 | + |
| 111 | + then: |
| 112 | + toFileUrl("/test/grails/app/grails-app/views") == viewsDir.getURL().toString() |
| 113 | + |
| 114 | + viewsDir == GrailsResourceUtils.getViewsDir(new UrlResource(TEST_URL)) |
| 115 | + toFileUrl("/test/grails/app/grails-app/views") == viewsDir.getURL().toString() |
| 116 | + } |
| 117 | + |
| 118 | + void testGetAppDir() { |
| 119 | + when: |
| 120 | + Resource appDir = GrailsResourceUtils.getAppDir(new UrlResource(TEST_CONTROLLER_URL)) |
| 121 | + |
| 122 | + then: |
| 123 | + toFileUrl("/test/grails/app/grails-app") == appDir.getURL().toString() |
| 124 | + |
| 125 | + appDir == GrailsResourceUtils.getAppDir(new UrlResource(TEST_URL)) |
| 126 | + toFileUrl("/test/grails/app/grails-app") == appDir.getURL().toString() |
| 127 | + } |
| 128 | + |
| 129 | + void testGetDirWithinWebInf() { |
| 130 | + when: |
| 131 | + Resource viewsDir = GrailsResourceUtils.getViewsDir(new UrlResource(TEST_CONTROLLER_URL)) |
| 132 | + Resource pluginViews = GrailsResourceUtils.getViewsDir(new UrlResource(TEST_PLUGIN_CTRL)) |
| 133 | + |
| 134 | + Resource webInfViews = GrailsResourceUtils.getViewsDir(new UrlResource(WEBINF_CONTROLLER)) |
| 135 | + Resource webInfPluginViews = GrailsResourceUtils.getViewsDir(new UrlResource(WEBINF_PLUGIN_CTRL)) |
| 136 | + |
| 137 | + then: |
| 138 | + toFileUrl("/test/grails/app/grails-app/views") == viewsDir.getURL().toString() |
| 139 | + toFileUrl("/test/grails/app/plugins/myplugin/grails-app/views") == pluginViews.getURL().toString() |
| 140 | + toFileUrl("/test/grails/app/WEB-INF/grails-app/views") == webInfViews.getURL().toString() |
| 141 | + toFileUrl("/test/grails/app/WEB-INF/plugins/myplugin/grails-app/views") == webInfPluginViews.getURL().toString() |
| 142 | + |
| 143 | + "/WEB-INF/grails-app/views" == GrailsResourceUtils.getRelativeInsideWebInf(webInfViews) |
| 144 | + "/WEB-INF/plugins/myplugin/grails-app/views" == GrailsResourceUtils.getRelativeInsideWebInf(webInfPluginViews) |
| 145 | + |
| 146 | + "/WEB-INF/plugins/myplugin/grails-app/views" == GrailsResourceUtils.getRelativeInsideWebInf(pluginViews) |
| 147 | + "/WEB-INF/grails-app/views" == GrailsResourceUtils.getRelativeInsideWebInf(viewsDir) |
| 148 | + } |
| 149 | + |
| 150 | + void testGetPluginContextPath() { |
| 151 | + given: |
| 152 | + MockServletContext servletContext = new MockServletContext("/myapp") |
| 153 | + MockHttpServletRequest request = new MockHttpServletRequest(servletContext) |
| 154 | + request.setContextPath("/myapp") |
| 155 | + |
| 156 | + expect: |
| 157 | + "" == GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(TEST_CONTROLLER_URL), null) |
| 158 | + "plugins/myplugin" == GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(TEST_PLUGIN_CTRL), null) |
| 159 | + "" == GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_CONTROLLER), null) |
| 160 | + "plugins/myplugin" == GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_PLUGIN_CTRL), null) |
| 161 | + "/myapp" == GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_CONTROLLER), request.getContextPath()) |
| 162 | + "/myapp/plugins/myplugin" == GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_PLUGIN_CTRL), request.getContextPath()) |
| 163 | + } |
| 164 | + |
| 165 | + void testAppendPiecesForUri() { |
| 166 | + expect: |
| 167 | + "" == GrailsResourceUtils.appendPiecesForUri("") |
| 168 | + "/alpha/beta/gamma" == GrailsResourceUtils.appendPiecesForUri("/alpha", "/beta", "/gamma") |
| 169 | + "/alpha/beta/gamma" == GrailsResourceUtils.appendPiecesForUri("/alpha/", "/beta/", "/gamma") |
| 170 | + "/alpha/beta/gamma/" == GrailsResourceUtils.appendPiecesForUri("/alpha/", "/beta/", "/gamma/") |
| 171 | + "alpha/beta/gamma" == GrailsResourceUtils.appendPiecesForUri("alpha", "beta", "gamma") |
| 172 | + } |
| 173 | + |
| 174 | + void testGetPathFromBaseDir() { |
| 175 | + expect: |
| 176 | + "views/demo/index.gsp" == GrailsResourceUtils.getPathFromBaseDir("${BuildSettings.BASE_DIR.absolutePath}/grails-app/views/demo/index.gsp") |
| 177 | + "src/main/demo/index.gsp" == GrailsResourceUtils.getPathFromBaseDir("${BuildSettings.BASE_DIR.absolutePath}/src/main/demo/index.gsp") |
| 178 | + "/alpha/index.gsp" == GrailsResourceUtils.getPathFromBaseDir("/alpha/index.gsp") |
| 179 | + } |
| 180 | + |
| 181 | + private String toFileUrl(String path) { |
| 182 | + if (path == null) return path |
| 183 | + String url = null |
| 184 | + try { |
| 185 | + url = new File(path).toURI().toURL().toString() |
| 186 | + } catch (MalformedURLException e) { |
| 187 | + url = path |
| 188 | + } |
| 189 | + return url |
| 190 | + } |
| 191 | +} |
0 commit comments