public void testCoverageContents() throws Exception { final Long processId = issueProcessAndWaitForTermination(); final String request = RESTLET_PATH + "/" + processId.longValue(); final MockHttpServletResponse response = getAsServletResponse(request); assertEquals(Status.SUCCESS_OK, Status.valueOf(response.getStatus())); assertEquals(MediaType.APPLICATION_ZIP, MediaType.valueOf(response.getContentType())); final ByteArrayInputStream responseStream = getBinaryInputStream(response); File dataDirectoryRoot = super.getTestData().getDataDirectoryRoot(); File file = new File(dataDirectoryRoot, "testCoverageContents.zip"); super.getTestData().copyTo(responseStream, file.getName()); ZipFile zipFile = new ZipFile(file); try { // TODO: change this expectation once we normalize the raster file name String rasterName = RASTER_LAYER.getPrefix() + ":" + RASTER_LAYER.getLocalPart() + ".tiff"; ZipEntry nextEntry = zipFile.getEntry(rasterName); assertNotNull(nextEntry); InputStream coverageInputStream = zipFile.getInputStream(nextEntry); // Use a file, geotiffreader might not work well reading out of a plain input stream File covFile = new File(file.getParentFile(), "coverage.tiff"); IOUtils.copy(coverageInputStream, covFile); GeoTiffReader geoTiffReader = new GeoTiffReader(covFile); GridCoverage2D coverage = geoTiffReader.read(null); CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem(); assertEquals(CRS.decode("EPSG:4326", true), crs); } finally { zipFile.close(); } }
public GeoServerTestApplicationContext(String[] configLocation, ServletContext servletContext) throws BeansException { super(configLocation, false); try { servletContext.setAttribute( "javax.servlet.context.tempdir", IOUtils.createRandomDirectory("./target", "mock", "tmp")); } catch (Exception e) { throw new RuntimeException(e); } this.servletContext = servletContext; }
/** * Check for the existence of the file, if the file exists do nothing. * * <p>If the file does not exist, check for a template file contained in the jar with the same * name, if found, use it. * * <p>If no template was found, use the default template * * @param fileName target location * @param namedRoot parent dir if fileName is relative * @param defaultResource the standard template * @throws IOException * @return the file to use */ protected Resource checkORCreateJDBCPropertyFile( String fileName, Resource namedRoot, String defaultResource) throws IOException { Resource resource; fileName = fileName != null ? fileName : defaultResource; File file = new File(fileName); if (file.isAbsolute()) { resource = Files.asResource(file); } else { resource = namedRoot.get(fileName); } if (Resources.exists(resource)) { return resource; // we are happy } // try to find a template with the same name InputStream is = this.getClass().getResourceAsStream(fileName); if (is != null) IOUtils.copy(is, resource.out()); else // use the default template FileUtils.copyURLToFile(getClass().getResource(defaultResource), file); return resource; }
@Override public void afterPropertiesSet() throws Exception { File properties = new File( config .getConfiguration(EmbeddedBrokerConfiguration.EMBEDDED_BROKER_PROPERTIES_KEY) .toString()); if (!properties.isAbsolute() && !properties.isFile()) { // try to resolve as absolute properties = new File(JMSConfiguration.getConfigPathDir(), properties.getPath()); if (!properties.isFile()) { // copy the defaults IOUtils.copy(defaults.getFile(), properties); } } final Resource res = new FileSystemResource(properties); super.setLocation(res); // make sure the activemq.base is set to a valuable default final Properties props = new Properties(); props.setProperty("activemq.base", (String) config.getConfiguration("CLUSTER_CONFIG_DIR")); props.setProperty("instanceName", (String) config.getConfiguration("instanceName")); setProperties(props); }
public MockCatalogBuilder coverage(QName qName, String fileName, String srs, Class scope) { scope = scope != null ? scope : getClass(); String cId = newId(); final CoverageStoreInfo cs = coverageStores.peekLast(); NamespaceInfo ns = namespaces.peekLast(); final String name = qName.getLocalPart(); File dir = new File(dataDirRoot, name); dir.mkdir(); try { IOUtils.copy(scope.getResourceAsStream(fileName), new File(dir, fileName)); } catch (IOException e) { throw new RuntimeException(e); } // initialize the mock by actually building a real one first CatalogBuilder cb = new CatalogBuilder(new CatalogImpl()); cb.setStore(cs); GridCoverage2DReader reader = cs.getFormat().getReader(cs.getURL()); if (reader == null) { throw new RuntimeException("No reader for " + cs.getURL()); } CoverageInfo real = null; try { real = cb.buildCoverage(reader, null); } catch (Exception e) { throw new RuntimeException(e); } final CoverageInfo c = createNiceMock(CoverageInfo.class); coverages.add(c); final List<CoverageInfo> coverageList = coverages; if (srs == null) { srs = real.getSRS(); } initResource( c, CoverageInfo.class, cId, name, cs, ns, srs, real.getProjectionPolicy(), real.getNativeBoundingBox(), real.getLatLonBoundingBox()); expect(c.getDefaultInterpolationMethod()) .andReturn(real.getDefaultInterpolationMethod()) .anyTimes(); expect(c.getDimensions()).andReturn(real.getDimensions()).anyTimes(); expect(c.getGrid()).andReturn(real.getGrid()).anyTimes(); expect(c.getInterpolationMethods()).andReturn(real.getInterpolationMethods()).anyTimes(); expect(c.getRequestSRS()).andReturn(real.getRequestSRS()).anyTimes(); expect(c.getResponseSRS()).andReturn(real.getResponseSRS()).anyTimes(); try { expect(c.getGridCoverageReader(null, null)).andReturn(reader).anyTimes(); } catch (IOException e) { } expect(catalog.getCoverageByName(or(eq(name), eq(ns.getPrefix() + ":" + name)))) .andReturn(c) .anyTimes(); expect( catalog.getCoverageByName( or(eq(new NameImpl(ns.getPrefix(), name)), eq(new NameImpl(ns.getURI(), name))))) .andReturn(c) .anyTimes(); expect(catalog.getCoverageByName(ns, name)).andReturn(c).anyTimes(); expect(catalog.getCoverageByName(ns.getPrefix(), name)).andReturn(c).anyTimes(); // expect(catalog.getFeatureTypeByName(or(eq(ns.getPrefix()), eq(ns.getURI())), name)) // .andReturn(ft).anyTimes(); // expect(catalog.getCoverageByStore(cs, name)).andReturn(c).anyTimes(); expect(catalog.getCoveragesByStore(cs)).andReturn(coverageList).anyTimes(); expect(catalog.getCoverageByCoverageStore(cs, name)).andReturn(c).anyTimes(); c.accept((CatalogVisitor) anyObject()); expectLastCall() .andAnswer( new VisitAnswer() { @Override protected void doVisit(CatalogVisitor visitor) { visitor.visit(c); } }) .anyTimes(); callback.onResource(name, c, cs, this); replay(c, createLayer(c, name, ns)); return this; }
public MockCatalogBuilder featureType( final String name, String srs, ProjectionPolicy projPolicy, ReferencedEnvelope envelope, ReferencedEnvelope latLonEnvelope) { String ftId = newId(); final DataStoreInfo ds = dataStores.peekLast(); NamespaceInfo ns = namespaces.peekLast(); final FeatureTypeInfo ft = createNiceMock(FeatureTypeInfo.class); featureTypes.add(ft); initResource( ft, FeatureTypeInfo.class, ftId, name, ds, ns, srs, projPolicy, envelope, latLonEnvelope); expect(ft.getNumDecimals()).andReturn(8); // setup the property file data File propDir = new File(dataDirRoot, ds.getName()); propDir.mkdirs(); String fileName = name + ".properties"; try { IOUtils.copy(getClass().getResourceAsStream(fileName), new File(propDir, fileName)); } catch (IOException e) { throw new RuntimeException(e); } try { expect(ft.getFeatureType()) .andAnswer( new IAnswer<FeatureType>() { @Override public FeatureType answer() throws Throwable { return ((DataStore) ds.getDataStore(null)).getSchema(name); } }) .anyTimes(); expect(ft.getFeatureSource(null, null)) .andAnswer( (IAnswer) new IAnswer<FeatureSource>() { @Override public FeatureSource answer() throws Throwable { return ((DataStore) ds.getDataStore(null)).getFeatureSource(name); } }) .anyTimes(); } catch (IOException e) { } expect(catalog.getFeatureTypeByName(or(eq(name), eq(ns.getPrefix() + ":" + name)))) .andReturn(ft) .anyTimes(); expect( catalog.getFeatureTypeByName( or(eq(new NameImpl(ns.getPrefix(), name)), eq(new NameImpl(ns.getURI(), name))))) .andReturn(ft) .anyTimes(); expect(catalog.getFeatureTypeByName(ns, name)).andReturn(ft).anyTimes(); expect(catalog.getFeatureTypeByName(ns.getPrefix(), name)).andReturn(ft).anyTimes(); // expect(catalog.getFeatureTypeByName(or(eq(ns.getPrefix()), eq(ns.getURI())), name)) // .andReturn(ft).anyTimes(); expect(catalog.getFeatureTypeByStore(ds, name)).andReturn(ft).anyTimes(); expect(catalog.getFeatureTypeByDataStore(ds, name)).andReturn(ft).anyTimes(); ft.accept((CatalogVisitor) anyObject()); expectLastCall() .andAnswer( new VisitAnswer() { @Override protected void doVisit(CatalogVisitor visitor) { visitor.visit(ft); } }) .anyTimes(); callback.onResource(name, ft, ds, this); replay(ft, createLayer(ft, name, ns)); return this; }
/** * Copies some content to a file under the base of the data directory. * * <p>The <code>location</code> is considred to be a path relative to the data directory root. * * <p>Note that the resulting file will be deleted when {@link #tearDown()} is called. * * @param input The content to copy. * @param location A relative path */ public void copyTo(InputStream input, String location) throws IOException { IOUtils.copy(input, new File(getDataDirectoryRoot(), location)); }
/** * Adds a raster layer to the setup. * * <p>This method configures a raster layer with the name <code>qName.getLocalPart()</code>. A * coverage store is created (if it doesn't already exist) with the same name. The workspace of * the resulting store and layer is determined by <code>qName.getPrefix()</code>. * * <p>The <tt>filename</tt> parameter defines the raster file to be loaded from the classpath and * copied into the data directory. The <tt>scope</tt> is used as the class from which to load the * file from. * * <p>In the case of adding a zipped archive that contains multiple file the <tt>filename</tt> * paramter should have a ".zip" extension and the <tt>extension</tt> parameter must define the * extension of the main raster file. The parameter is not necessary and may be null if the * <tt>filename</tt> does not refer to a zip file. * * <p>The <tt>props</tt> parameter is used to define custom properties for the layer. See the * {@link LayerProperty} class for supported properties. * * @param qName The name of the raster layer. * @param filename The name of the file containing the raster, to be loaded from the classpath. * @param extension The file extension (without a ".") of the main raster file. This parameter my * be <code>null</code> only if <tt>filename</tt> does not refer to a zip file. * @param props Custom properties to assign to the created raster layer. * @param scope The class from which to load the <tt>filename</tt> resource from. */ public void addRasterLayer( QName qName, String filename, String extension, Map<LayerProperty, Object> props, Class scope, Catalog catalog) throws IOException { String prefix = qName.getPrefix(); String name = qName.getLocalPart(); // setup the data File dir = new File(data, name); dir.mkdirs(); File file = new File(dir, filename); catalog.getResourceLoader().copyFromClassPath(filename, file, scope); String ext = FilenameUtils.getExtension(filename); if ("zip".equalsIgnoreCase(ext)) { // unpack the archive IOUtils.decompress(file, dir); // delete archive file.delete(); if (extension == null) { // zip with no extension, we just the directory as the file file = dir; } else { // files may have been top level, or one directory level deep file = new File(dir, FilenameUtils.getBaseName(filename) + "." + extension); if (!file.exists()) { File file2 = new File(new File(dir, dir.getName()), file.getName()); if (file2.exists()) { file = file2; } } } if (!file.exists()) { throw new FileNotFoundException(file.getPath()); } } // load the format/reader AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(file); if (format == null) { throw new RuntimeException("No format for " + file.getCanonicalPath()); } AbstractGridCoverage2DReader reader = null; try { reader = (AbstractGridCoverage2DReader) format.getReader(file); if (reader == null) { throw new RuntimeException( "No reader for " + file.getCanonicalPath() + " with format " + format.getName()); } // configure workspace if it doesn;t already exist if (catalog.getWorkspaceByName(prefix) == null) { addWorkspace(prefix, qName.getNamespaceURI(), catalog); } // create the store CoverageStoreInfo store = catalog.getCoverageStoreByName(prefix, name); if (store == null) { store = catalog.getFactory().createCoverageStore(); } store.setName(name); store.setWorkspace(catalog.getWorkspaceByName(prefix)); store.setEnabled(true); store.setURL(DataUtilities.fileToURL(file).toString()); store.setType(format.getName()); if (store.getId() == null) { catalog.add(store); } else { catalog.save(store); } // create the coverage CatalogBuilder builder = new CatalogBuilder(catalog); builder.setStore(store); CoverageInfo coverage = null; try { coverage = builder.buildCoverage(reader, null); // coverage read params if (format instanceof ImageMosaicFormat) { // make sure we work in immediate mode coverage .getParameters() .put(AbstractGridFormat.USE_JAI_IMAGEREAD.getName().getCode(), Boolean.FALSE); } } catch (Exception e) { throw new IOException(e); } coverage.setName(name); coverage.setTitle(name); coverage.setDescription(name); coverage.setEnabled(true); CoverageInfo cov = catalog.getCoverageByCoverageStore(store, name); if (cov == null) { catalog.add(coverage); } else { builder.updateCoverage(cov, coverage); catalog.save(cov); coverage = cov; } LayerInfo layer = catalog.getLayerByName(new NameImpl(qName)); if (layer == null) { layer = catalog.getFactory().createLayer(); } layer.setResource(coverage); layer.setDefaultStyle( catalog.getStyleByName(LayerProperty.STYLE.get(props, DEFAULT_RASTER_STYLE))); layer.setType(LayerInfo.Type.RASTER); layer.setEnabled(true); if (layer.getId() == null) { catalog.add(layer); } else { catalog.save(layer); } } finally { if (reader != null) { reader.dispose(); } } }
public void setUpSecurity() throws IOException { File secDir = new File(getDataDirectoryRoot(), "security"); IOUtils.decompress(SystemTestData.class.getResourceAsStream("security.zip"), secDir); }
public SystemTestData() throws IOException { // setup the root data = IOUtils.createRandomDirectory("./target", "default", "data"); data.delete(); data.mkdir(); }
public void tearDown() throws Exception { if (data != null) IOUtils.delete(data); }
/** * Deeps copy the dataDirSourceDirectory provided in the constructor into a temporary directory. * Subclasses may override it in order to add extra behavior (like setting up an external * database) */ public void setUp() throws Exception { data = IOUtils.createRandomDirectory("./target", "live", "data"); IOUtils.deepCopy(source, data); }