You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by sb...@apache.org on 2012/01/26 15:45:43 UTC
svn commit: r1236201 [5/5] - in /james/mangesieve: ./ api/ api/src/
api/src/main/ api/src/main/java/ api/src/main/java/org/
api/src/main/java/org/apache/ api/src/main/java/org/apache/james/
api/src/main/java/org/apache/james/managesieve/ api/src/main/j...
Added: james/mangesieve/mock/src/main/java/org/apache/james/managesieve/mock/MockSieveRepository.java
URL: http://svn.apache.org/viewvc/james/mangesieve/mock/src/main/java/org/apache/james/managesieve/mock/MockSieveRepository.java?rev=1236201&view=auto
==============================================================================
--- james/mangesieve/mock/src/main/java/org/apache/james/managesieve/mock/MockSieveRepository.java (added)
+++ james/mangesieve/mock/src/main/java/org/apache/james/managesieve/mock/MockSieveRepository.java Thu Jan 26 14:45:39 2012
@@ -0,0 +1,377 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.james.managesieve.mock;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import org.apache.james.managesieve.api.DuplicateException;
+import org.apache.james.managesieve.api.DuplicateUserException;
+import org.apache.james.managesieve.api.IsActiveException;
+import org.apache.james.managesieve.api.QuotaExceededException;
+import org.apache.james.managesieve.api.QuotaNotFoundException;
+import org.apache.james.managesieve.api.ScriptNotFoundException;
+import org.apache.james.managesieve.api.ScriptSummary;
+import org.apache.james.managesieve.api.SieveRepository;
+import org.apache.james.managesieve.api.StorageException;
+import org.apache.james.managesieve.api.UserNotFoundException;
+
+/**
+ * <code>MockSieveRepository</code>
+ */
+public class MockSieveRepository implements SieveRepository {
+
+ public class SieveScript
+ {
+ private String _name = null;
+ private String _content = null;
+ private boolean _isActive = false;
+
+ /**
+ * Creates a new instance of SieveScript.
+ *
+ */
+ private SieveScript() {
+ super();
+ }
+
+ /**
+ * Creates a new instance of SieveScript.
+ *
+ */
+ public SieveScript(String content, boolean isActive) {
+ this();
+ setContent(content);
+ setActive(isActive);
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return _name;
+ }
+
+ /**
+ * @param name the name to set
+ */
+ public void setName(String name) {
+ _name = name;
+ }
+
+ /**
+ * @return the content
+ */
+ public String getContent() {
+ return _content;
+ }
+
+ /**
+ * @param content the content to set
+ */
+ public void setContent(String content) {
+ _content = content;
+ }
+
+ /**
+ * @return the isActive
+ */
+ public boolean isActive() {
+ return _isActive;
+ }
+
+ /**
+ * @param isActive the isActive to set
+ */
+ public void setActive(boolean isActive) {
+ _isActive = isActive;
+ }
+ }
+
+ Map<String,Map<String, SieveScript>> _repository = null;
+
+ /**
+ * Creates a new instance of MockSieveRepository.
+ *
+ */
+ public MockSieveRepository() {
+ _repository = new HashMap<String,Map<String, SieveScript>>();
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#addUser(java.lang.String)
+ */
+ public void addUser(String user) throws DuplicateUserException, StorageException {
+ if (_repository.containsKey(user))
+ {
+ throw new DuplicateUserException(user);
+ }
+ _repository.put(user, new HashMap<String, SieveScript>());
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#deleteScript(java.lang.String, java.lang.String)
+ */
+ public void deleteScript(String user, String name) throws UserNotFoundException,
+ ScriptNotFoundException, IsActiveException, StorageException {
+ if (!_repository.containsKey(user))
+ {
+ throw new UserNotFoundException(user);
+ }
+ SieveScript script = _repository.get(user).get(name);
+ if (null == script)
+ {
+ throw new ScriptNotFoundException(name);
+ }
+ if (script.isActive())
+ {
+ throw new IsActiveException(name);
+ }
+ _repository.get(user).remove(name);
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#getActive(java.lang.String)
+ */
+ public String getActive(String user) throws UserNotFoundException, ScriptNotFoundException {
+ if (!_repository.containsKey(user))
+ {
+ throw new UserNotFoundException(user);
+ }
+ Set<Entry<String, SieveScript>> scripts = _repository.get(user).entrySet();
+ String content = null;
+ for (final Entry<String, SieveScript> entry : scripts)
+ {
+ if (entry.getValue().isActive())
+ {
+ content = entry.getValue().getContent();
+ break;
+ }
+ }
+ if (null == content)
+ {
+ throw new ScriptNotFoundException();
+ }
+ return content;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#getQuota()
+ */
+ public long getQuota() throws QuotaNotFoundException {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#getQuota(java.lang.String)
+ */
+ public long getQuota(String user) throws UserNotFoundException, QuotaNotFoundException {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#getScript(java.lang.String, java.lang.String)
+ */
+ public String getScript(String user, String name) throws UserNotFoundException,
+ ScriptNotFoundException {
+ if (!_repository.containsKey(user))
+ {
+ throw new UserNotFoundException(user);
+ }
+ SieveScript script = _repository.get(user).get(name);
+ if (null == script)
+ {
+ throw new ScriptNotFoundException(name);
+ }
+ return script.getContent();
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#hasQuota()
+ */
+ public boolean hasQuota() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#hasQuota(java.lang.String)
+ */
+ public boolean hasQuota(String user) throws UserNotFoundException {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#hasUser(java.lang.String)
+ */
+ public boolean hasUser(String user) {
+ return _repository.containsKey(user);
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#haveSpace(java.lang.String, java.lang.String, long)
+ */
+ public void haveSpace(String user, String name, long size) throws UserNotFoundException,
+ QuotaExceededException {
+ if (!_repository.containsKey(user))
+ {
+ throw new UserNotFoundException(user);
+ }
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#listScripts(java.lang.String)
+ */
+ public List<ScriptSummary> listScripts(String user) throws UserNotFoundException {
+ if (!_repository.containsKey(user))
+ {
+ throw new UserNotFoundException(user);
+ }
+ Set<Entry<String, SieveScript>> scripts = _repository.get(user).entrySet();
+ List<ScriptSummary> summaries = new ArrayList<ScriptSummary>(scripts.size());
+ for (final Entry<String, SieveScript> entry : scripts)
+ {
+ summaries.add(new ScriptSummary(){
+
+ public String getName() {
+ return entry.getKey();
+ }
+
+ public boolean isActive() {
+ return entry.getValue().isActive();
+ }});
+ }
+ return summaries;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#putScript(java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void putScript(String user, String name, String content) throws UserNotFoundException,
+ StorageException, QuotaExceededException {
+ if (!_repository.containsKey(user))
+ {
+ throw new UserNotFoundException(user);
+ }
+ Map<String,SieveScript> scripts = _repository.get(user);
+ scripts.put(name, new SieveScript(content, false));
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#removeQuota()
+ */
+ public void removeQuota() throws QuotaNotFoundException, StorageException {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#removeQuota(java.lang.String)
+ */
+ public void removeQuota(String user) throws UserNotFoundException, QuotaNotFoundException,
+ StorageException {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#removeUser(java.lang.String)
+ */
+ public void removeUser(String user) throws UserNotFoundException, StorageException {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#renameScript(java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void renameScript(String user, String oldName, String newName)
+ throws UserNotFoundException, ScriptNotFoundException,
+ DuplicateException, StorageException {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#setActive(java.lang.String, java.lang.String)
+ */
+ public void setActive(String user, String name) throws UserNotFoundException,
+ ScriptNotFoundException, StorageException {
+
+ // Turn off currently active script, if any
+ Entry<String, SieveScript> oldActive = null;
+ oldActive = getActiveEntry(user);
+ if (null != oldActive) {
+ oldActive.getValue().setActive(false);
+ }
+
+ // Turn on the new active script if not an empty name
+ if ((null != name) && (!name.trim().isEmpty())) {
+ if (_repository.get(user).containsKey(name)) {
+ _repository.get(user).get(name).setActive(true);
+ } else {
+ if (null != oldActive) {
+ oldActive.getValue().setActive(true);
+ }
+ throw new ScriptNotFoundException();
+ }
+ }
+ }
+
+ protected Entry<String, SieveScript> getActiveEntry(String user)
+ {
+ Set<Entry<String, SieveScript>> scripts = _repository.get(user).entrySet();
+ Entry<String, SieveScript> activeEntry = null;
+ for (final Entry<String, SieveScript> entry : scripts)
+ {
+ if (entry.getValue().isActive())
+ {
+ activeEntry = entry;
+ break;
+ }
+ }
+ return activeEntry;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#setQuota(long)
+ */
+ public void setQuota(long quota) throws StorageException {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#setQuota(java.lang.String, long)
+ */
+ public void setQuota(String user, long quota) throws UserNotFoundException, StorageException {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Propchange: james/mangesieve/mock/src/main/java/org/apache/james/managesieve/mock/MockSieveRepository.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: james/mangesieve/mock/src/main/java/org/apache/james/managesieve/mock/MockSieveRepository.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/mangesieve/pom.xml
URL: http://svn.apache.org/viewvc/james/mangesieve/pom.xml?rev=1236201&view=auto
==============================================================================
--- james/mangesieve/pom.xml (added)
+++ james/mangesieve/pom.xml Thu Jan 26 14:45:39 2012
@@ -0,0 +1,71 @@
+ <!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.james</groupId>
+ <artifactId>managesieve</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <packaging>pom</packaging>
+
+ <modules>
+ <module>api</module>
+ <module>core</module>
+ <module>jsieve</module>
+ <module>mailet</module>
+ <module>mock</module>
+ <module>server</module>
+ </modules>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <inceptionYear>2012</inceptionYear>
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <optimize>true</optimize>
+ <source>${target.jdk}</source>
+ <target>${target.jdk}</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+
+ <properties>
+ <target.jdk>1.6</target.jdk>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <jsieve.version>0.5-SNAPSHOT</jsieve.version>
+ <commons-io.version>2.1</commons-io.version>
+ <james-server.version>3.0-beta4-SNAPSHOT</james-server.version>
+ <mailet.version>2.4</mailet.version>
+ <mailet-base.version>1.0</mailet-base.version>
+ </properties>
+</project>
Propchange: james/mangesieve/pom.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: james/mangesieve/pom.xml
------------------------------------------------------------------------------
svn:mime-type = text/plain
Propchange: james/mangesieve/server/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Jan 26 14:45:39 2012
@@ -0,0 +1,7 @@
+target*
+
+.settings
+
+.classpath
+
+.project
Added: james/mangesieve/server/pom.xml
URL: http://svn.apache.org/viewvc/james/mangesieve/server/pom.xml?rev=1236201&view=auto
==============================================================================
--- james/mangesieve/server/pom.xml (added)
+++ james/mangesieve/server/pom.xml Thu Jan 26 14:45:39 2012
@@ -0,0 +1,52 @@
+ <!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.james</groupId>
+ <artifactId>managesieve</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>managesieve-server</artifactId>
+ <packaging>jar</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.james</groupId>
+ <artifactId>james-server-filesystem-api</artifactId>
+ <version>${james-server.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>${commons-io.version}</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>managesieve-core</artifactId>
+ <version>${project.version}</version>
+ <type>jar</type>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+</project>
Propchange: james/mangesieve/server/pom.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: james/mangesieve/server/pom.xml
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/mangesieve/server/src/main/java/org/apache/james/managesieve/file/SieveFileRepository.java
URL: http://svn.apache.org/viewvc/james/mangesieve/server/src/main/java/org/apache/james/managesieve/file/SieveFileRepository.java?rev=1236201&view=auto
==============================================================================
--- james/mangesieve/server/src/main/java/org/apache/james/managesieve/file/SieveFileRepository.java (added)
+++ james/mangesieve/server/src/main/java/org/apache/james/managesieve/file/SieveFileRepository.java Thu Jan 26 14:45:39 2012
@@ -0,0 +1,622 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.james.managesieve.file;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Scanner;
+
+import javax.annotation.Resource;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.managesieve.api.ConfigurationError;
+import org.apache.james.managesieve.api.DuplicateException;
+import org.apache.james.managesieve.api.DuplicateUserException;
+import org.apache.james.managesieve.api.IsActiveException;
+import org.apache.james.managesieve.api.ManageSieveError;
+import org.apache.james.managesieve.api.QuotaExceededException;
+import org.apache.james.managesieve.api.QuotaNotFoundException;
+import org.apache.james.managesieve.api.ScriptNotFoundException;
+import org.apache.james.managesieve.api.ScriptSummary;
+import org.apache.james.managesieve.api.SieveRepository;
+import org.apache.james.managesieve.api.StorageException;
+import org.apache.james.managesieve.api.UserNotFoundException;
+
+/**
+ * <code>SieveFileRepository</code> manages sieve scripts stored on the file system.
+ * <p>The sieve root directory is a sub-directory of the application base directory named "sieve".
+ * Scripts are stored in sub-directories of the sieve root directory, each with the name of the
+ * associated user.
+ */
+public class SieveFileRepository implements SieveRepository {
+
+ private static final String SIEVE_ROOT = FileSystem.FILE_PROTOCOL + "sieve/";
+ private static final String UTF_8 = "UTF-8";
+ private static final String FILE_NAME_QUOTA = ".quota";
+ private static final String FILE_NAME_ACTIVE = ".active";
+ private static final List<String> SYSTEM_FILES = Arrays.asList(FILE_NAME_QUOTA, FILE_NAME_ACTIVE);
+ private static final int MAX_BUFF_SIZE = 32768;
+
+ private FileSystem _fileSystem = null;
+
+ /**
+ * Read a file with the specified encoding into a String
+ *
+ * @param file
+ * @param encoding
+ * @return
+ * @throws FileNotFoundException
+ */
+ static protected String toString(File file, String encoding) throws FileNotFoundException {
+ String script = null;
+ Scanner scanner = null;
+ try {
+ scanner = new Scanner(file, encoding).useDelimiter("\\A");
+ script = scanner.next();
+ } finally {
+ if (null != scanner) {
+ scanner.close();
+ }
+ }
+ return script;
+ }
+
+ static protected void toFile(File file, String content) throws StorageException {
+ // Create a temporary file
+ int bufferSize = content.length() > MAX_BUFF_SIZE ? MAX_BUFF_SIZE : content.length();
+ File tmpFile = null;
+ Writer out = null;
+ try {
+ tmpFile = File.createTempFile(file.getName(), ".tmp", file.getParentFile());
+ try {
+ out = new OutputStreamWriter(new BufferedOutputStream(
+ new FileOutputStream(tmpFile), bufferSize), UTF_8);
+ out.write(content);
+ } catch (UnsupportedEncodingException ex1) {
+ // UTF-8 must always be supported
+ throw new ManageSieveError("Runtime must always support UTF-8", ex1);
+ } finally {
+ IOUtils.closeQuietly(out);
+ }
+ } catch (IOException ex) {
+ FileUtils.deleteQuietly(tmpFile);
+ throw new StorageException(ex);
+ }
+
+ // Does the file exist?
+ // If so, make a backup
+ File backupFile = new File(file.getParentFile(), file.getName() + ".bak");
+ if (file.exists()) {
+ try {
+ FileUtils.copyFile(file, backupFile);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+
+ // Copy the temporary file to its final name
+ try {
+ FileUtils.copyFile(tmpFile, file);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ // Tidy up
+ if (tmpFile.exists()) {
+ FileUtils.deleteQuietly(tmpFile);
+ }
+ if (backupFile.exists()) {
+ FileUtils.deleteQuietly(backupFile);
+ }
+ }
+
+ /**
+ * Creates a new instance of SieveFileRepository.
+ *
+ */
+ public SieveFileRepository() {
+ super();
+ }
+
+ /**
+ * Creates a new instance of SieveFileRepository.
+ *
+ * @param fileSystem
+ */
+ public SieveFileRepository(FileSystem fileSystem) {
+ this();
+ setFileSystem(fileSystem);
+ }
+
+ @Resource(name = "filesystem")
+ public void setFileSystem(FileSystem fileSystem)
+ {
+ _fileSystem = fileSystem;
+ }
+
+ /**
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#deleteScript(java.lang.String, java.lang.String)
+ */
+ public void deleteScript(final String user, final String name) throws UserNotFoundException,
+ ScriptNotFoundException, IsActiveException, StorageException {
+ synchronized (user) {
+ File file = getScriptFile(user, name);
+ if (isActiveFile(user, file)) {
+ throw new IsActiveException("User: " + user + "Script: " + name);
+ }
+ try {
+ FileUtils.forceDelete(file);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#getScript(java.lang.String, java.lang.String)
+ */
+ public String getScript(final String user, final String name) throws UserNotFoundException,
+ ScriptNotFoundException {
+ String script = null;
+ try {
+ script = toString(getScriptFile(user, name), UTF_8);
+ } catch (FileNotFoundException ex) {
+ throw new ScriptNotFoundException(ex);
+ }
+ return script;
+ }
+
+ /**
+ * The default quota, if any, is stored in file '.quota' in the sieve root directory. Quotas for
+ * specific users are stored in file '.quota' in the user's directory.
+ *
+ * <p>The '.quota' file contains a single positive integer value representing the quota in octets.
+ *
+ * @see org.apache.james.managesieve.api.SieveRepository#haveSpace(java.lang.String, java.lang.String, long)
+ */
+ public void haveSpace(final String user, final String name, final long size) throws UserNotFoundException,
+ QuotaExceededException {
+ long usedSpace = 0;
+ for (File file : getUserDirectory(user).listFiles()) {
+ if (!(file.getName().equals(name) || SYSTEM_FILES.contains(file.getName()))) {
+ usedSpace = usedSpace + file.length();
+ }
+ }
+
+ long quota = Long.MAX_VALUE;
+ File file = getQuotaFile(user);
+ if (!file.exists()) {
+ file = getQuotaFile();
+ }
+ if (file.exists()) {
+ Scanner scanner = null;
+ try {
+ scanner = new Scanner(file, UTF_8);
+ quota = scanner.nextLong();
+ } catch (FileNotFoundException ex) {
+ // no op
+ } catch (NoSuchElementException ex) {
+ // no op
+ }
+ finally
+ {
+ if (null != scanner)
+ {
+ scanner.close();
+ }
+ }
+ }
+ if ((usedSpace + size) > quota) {
+ throw new QuotaExceededException(" Quota: " + quota + " Used: " + usedSpace
+ + " Requested: " + size);
+ }
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#listScripts(java.lang.String)
+ */
+ public List<ScriptSummary> listScripts(final String user) throws UserNotFoundException {
+ File[] files = getUserDirectory(user).listFiles();
+ List<ScriptSummary> summaries = new ArrayList<ScriptSummary>(files.length);
+ File activeFile = null;
+ try {
+ activeFile = getActiveFile(user);
+ } catch (ScriptNotFoundException ex) {
+ // no op
+ }
+ final File activeFile1 = activeFile;
+ for (final File file : files) {
+ if (!SYSTEM_FILES.contains(file.getName())) {
+ summaries.add(new ScriptSummary() {
+
+ public String getName() {
+ return file.getName();
+ }
+
+ public boolean isActive() {
+ boolean isActive = false;
+ if (null != activeFile1)
+ {
+ isActive = 0 == activeFile1.compareTo(file);
+ }
+ return isActive;
+ }
+ });
+ }
+ }
+ return summaries;
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#putScript(java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void putScript(final String user, final String name, final String content)
+ throws UserNotFoundException, StorageException, QuotaExceededException {
+ synchronized (user) {
+ File file = new File(getUserDirectory(user), name);
+ haveSpace(user, name, content.length());
+ toFile(file, content);
+ }
+ }
+
+ /**
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#renameScript(java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void renameScript(final String user, final String oldName, final String newName)
+ throws UserNotFoundException, ScriptNotFoundException,
+ DuplicateException, StorageException {
+ synchronized (user) {
+ File oldFile = getScriptFile(user, oldName);
+ File newFile = new File(getUserDirectory(user), newName);
+ if (newFile.exists()) {
+ throw new DuplicateException("User: " + user + "Script: " + newName);
+ }
+ boolean isActive = isActiveFile(user, oldFile);
+ try {
+ FileUtils.copyFile(oldFile, newFile);
+ if (isActive) {
+ setActiveFile(newFile, true);
+ }
+ FileUtils.forceDelete(oldFile);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#getActive(java.lang.String)
+ */
+ public String getActive(final String user) throws UserNotFoundException,
+ ScriptNotFoundException {
+ String script = null;
+ try {
+ script = toString(getActiveFile(user), UTF_8);
+ } catch (FileNotFoundException ex) {
+ throw new ScriptNotFoundException(ex);
+ }
+ return script;
+ }
+
+ /**
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#setActive(java.lang.String, java.lang.String)
+ */
+ public void setActive(final String user, final String name) throws UserNotFoundException,
+ ScriptNotFoundException, StorageException {
+ synchronized (user) {
+ // Turn off currently active script, if any
+ File oldActive = null;
+ try {
+ oldActive = getActiveFile(user);
+ setActiveFile(oldActive, false);
+ } catch (ScriptNotFoundException ex) {
+ // This is permissible
+ }
+ // Turn on the new active script if not an empty name
+ if ((null != name) && (!name.trim().isEmpty())) {
+ try {
+ setActiveFile(getScriptFile(user, name), true);
+ } catch (ScriptNotFoundException ex) {
+ if (null != oldActive) {
+ setActiveFile(oldActive, true);
+ }
+ throw ex;
+ }
+ }
+ }
+ }
+
+ protected File getSieveRootDirectory()
+ {
+ try {
+ return _fileSystem.getFile(SIEVE_ROOT);
+ } catch (FileNotFoundException ex1) {
+ throw new ConfigurationError(ex1);
+ }
+ }
+
+ protected File getUserDirectory(String user) throws UserNotFoundException {
+ File file = getUserDirectoryFile(user);
+ if (!file.exists()) {
+ throw new UserNotFoundException("User: " + user);
+ }
+ return file;
+ }
+
+ protected File getUserDirectoryFile(String user) {
+ return new File(getSieveRootDirectory(), user + '/');
+ }
+
+ protected File getActiveFile(String user) throws UserNotFoundException,
+ ScriptNotFoundException {
+ File dir = getUserDirectory(user);
+ String content = null;
+ try {
+ content = toString(new File(dir, FILE_NAME_ACTIVE), UTF_8);
+ } catch (FileNotFoundException ex) {
+ throw new ScriptNotFoundException("There is no active script.");
+ }
+ return new File(dir,content);
+ }
+
+ protected boolean isActiveFile(String user, File file) throws UserNotFoundException
+ {
+ boolean isActive = false;
+ try {
+ isActive = 0 == getActiveFile(user).compareTo(file);
+ } catch (ScriptNotFoundException ex) {
+ // no op;
+ }
+ return isActive;
+ }
+
+ protected void setActiveFile(File activeFile, boolean isActive) throws StorageException {
+ File file = new File(activeFile.getParentFile(), FILE_NAME_ACTIVE);
+ if (isActive) {
+ String content = activeFile.getName();
+ toFile(file, content);
+ } else {
+ try {
+ FileUtils.forceDelete(file);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+ }
+
+ protected File getScriptFile(String user, String name) throws UserNotFoundException,
+ ScriptNotFoundException {
+ File file = new File(getUserDirectory(user), name);
+ if (!file.exists()) {
+ throw new ScriptNotFoundException("User: " + user + "Script: " + name);
+ }
+ return file;
+ }
+
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#hasUser()
+ */
+ public boolean hasUser(final String user) {
+ boolean userExists = true;
+ try {
+ getUserDirectory(user);
+ } catch (UserNotFoundException ex) {
+ userExists = false;
+ }
+ return userExists;
+ }
+
+ /**
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#addUser(java.lang.String)
+ */
+ public void addUser(final String user) throws DuplicateUserException, StorageException {
+ synchronized (user) {
+ boolean userExists = true;
+ try {
+ getUserDirectory(user);
+ } catch (UserNotFoundException ex) {
+ userExists = false;
+ }
+ if (userExists) {
+ throw new DuplicateUserException("User: " + user);
+ }
+ File dir = getUserDirectoryFile(user);
+ try {
+ FileUtils.forceMkdir(dir);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+ }
+
+ /**
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#removeUser(java.lang.String)
+ */
+ public void removeUser(final String user) throws UserNotFoundException, StorageException {
+ synchronized (user) {
+ File dir = getUserDirectory(user);
+ try {
+ FileUtils.forceDelete(dir);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+ }
+
+ protected File getQuotaFile()
+ {
+ return new File(getSieveRootDirectory(), FILE_NAME_QUOTA);
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#hasQuota()
+ */
+ public boolean hasQuota() {
+ return getQuotaFile().exists();
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#getQuota()
+ */
+ public long getQuota() throws QuotaNotFoundException {
+ Long quota = null;
+ File file = getQuotaFile();
+ if (file.exists()) {
+ Scanner scanner = null;
+ try {
+ scanner = new Scanner(file, UTF_8);
+ quota = scanner.nextLong();
+ } catch (FileNotFoundException ex) {
+ // no op
+ } catch (NoSuchElementException ex) {
+ // no op
+ }
+ finally {
+ if (null != scanner)
+ {
+ scanner.close();
+ }
+ }
+ }
+ if (null == quota)
+ {
+ throw new QuotaNotFoundException("No default quota");
+ }
+ return quota;
+ }
+
+ /**
+ * @throws QuotaNotFoundException
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#removeQuota()
+ */
+ public synchronized void removeQuota() throws QuotaNotFoundException, StorageException {
+ File file = getQuotaFile();
+ if (!file.exists()) {
+ throw new QuotaNotFoundException("No default quota");
+ }
+ try {
+ FileUtils.forceDelete(file);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#setQuota(long)
+ */
+ public synchronized void setQuota(final long quota) throws StorageException {
+ File file = getQuotaFile();
+ String content = Long.toString(quota);
+ toFile(file, content);
+ }
+
+ protected File getQuotaFile(String user) throws UserNotFoundException
+ {
+ return new File(getUserDirectory(user), FILE_NAME_QUOTA);
+ }
+
+ /**
+ * @see org.apache.james.managesieve.api.SieveRepository#hasQuota(java.lang.String)
+ */
+ public boolean hasQuota(final String user) throws UserNotFoundException {
+ return getQuotaFile(user).exists();
+ }
+
+ /**
+ * @throws QuotaNotFoundException
+ * @see org.apache.james.managesieve.api.SieveRepository#getQuota(java.lang.String)
+ */
+ public long getQuota(final String user) throws UserNotFoundException, QuotaNotFoundException {
+ Long quota = null;
+ File file = getQuotaFile(user);
+ if (file.exists()) {
+ Scanner scanner = null;
+ try {
+ scanner = new Scanner(file, UTF_8);
+ quota = scanner.nextLong();
+ } catch (FileNotFoundException ex) {
+ // no op
+ } catch (NoSuchElementException ex) {
+ // no op
+ } finally {
+ if (null != scanner) {
+ scanner.close();
+ }
+ }
+ }
+ if (null == quota) {
+ throw new QuotaNotFoundException("No quota for user: " + user);
+ }
+ return quota;
+ }
+
+ /**
+ * @throws QuotaNotFoundException
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#removeQuota(java.lang.String)
+ */
+ public void removeQuota(final String user) throws UserNotFoundException,
+ QuotaNotFoundException, StorageException {
+ synchronized (user) {
+ File file = getQuotaFile(user);
+ if (!file.exists()) {
+ throw new QuotaNotFoundException("No quota for user: " + user);
+ }
+ try {
+ FileUtils.forceDelete(file);
+ } catch (IOException ex) {
+ throw new StorageException(ex);
+ }
+ }
+ }
+
+ /**
+ * @throws StorageException
+ * @see org.apache.james.managesieve.api.SieveRepository#setQuota(java.lang.String, long)
+ */
+ public void setQuota(final String user, final long quota) throws UserNotFoundException,
+ StorageException {
+ synchronized (user) {
+ File file = getQuotaFile(user);
+ String content = Long.toString(quota);
+ toFile(file, content);
+ }
+ }
+
+}
Propchange: james/mangesieve/server/src/main/java/org/apache/james/managesieve/file/SieveFileRepository.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: james/mangesieve/server/src/main/java/org/apache/james/managesieve/file/SieveFileRepository.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/mangesieve/server/src/test/java/org/apache/james/managesieve/file/SieveFileRepositoryTestCase.java
URL: http://svn.apache.org/viewvc/james/mangesieve/server/src/test/java/org/apache/james/managesieve/file/SieveFileRepositoryTestCase.java?rev=1236201&view=auto
==============================================================================
--- james/mangesieve/server/src/test/java/org/apache/james/managesieve/file/SieveFileRepositoryTestCase.java (added)
+++ james/mangesieve/server/src/test/java/org/apache/james/managesieve/file/SieveFileRepositoryTestCase.java Thu Jan 26 14:45:39 2012
@@ -0,0 +1,649 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.james.managesieve.file;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.managesieve.api.DuplicateException;
+import org.apache.james.managesieve.api.DuplicateUserException;
+import org.apache.james.managesieve.api.IsActiveException;
+import org.apache.james.managesieve.api.QuotaExceededException;
+import org.apache.james.managesieve.api.QuotaNotFoundException;
+import org.apache.james.managesieve.api.ScriptNotFoundException;
+import org.apache.james.managesieve.api.ScriptSummary;
+import org.apache.james.managesieve.api.SieveRepository;
+import org.apache.james.managesieve.api.StorageException;
+import org.apache.james.managesieve.api.UserNotFoundException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * <code>SieveFileRepositoryTestCase</code>
+ */
+public class SieveFileRepositoryTestCase {
+ private static final String SIEVE_ROOT = FileSystem.FILE_PROTOCOL + "sieve";
+
+ private FileSystem fs = new FileSystem() {
+
+ public File getBasedir() throws FileNotFoundException {
+ return new File(System.getProperty("java.io.tmpdir"));
+ }
+
+ public InputStream getResource(String url) throws IOException {
+ return new FileInputStream(getFile(url));
+ }
+
+ public File getFile(String fileURL) throws FileNotFoundException {
+ return new File(getBasedir(), fileURL.substring(FileSystem.FILE_PROTOCOL.length()));
+ }
+ };
+
+ /**
+ * setUp.
+ *
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ File root = fs.getFile(SIEVE_ROOT);
+ // Remove files from the previous test, if any
+ if (root.exists())
+ {
+ FileUtils.forceDelete(root);
+ }
+ root.mkdir();
+ }
+
+ /**
+ * tearDown.
+ *
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ // Files from the current run are not removed to allow post run analysis
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#SieveFileRepository(org.apache.james.filesystem.api.FileSystem)}.
+ */
+ @Test
+ public final void testSieveFileRepository() {
+ SieveRepository repo = new SieveFileRepository(fs);
+ assertTrue(repo instanceof SieveRepository);
+ assertTrue(repo instanceof SieveFileRepository);
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#deleteScript(java.lang.String, java.lang.String)}.
+ * @throws StorageException
+ * @throws DuplicateUserException
+ * @throws QuotaExceededException
+ * @throws UserNotFoundException
+ * @throws IsActiveException
+ * @throws ScriptNotFoundException
+ * @throws FileNotFoundException
+ */
+ @Test
+ public final void testDeleteScript() throws DuplicateUserException, StorageException,
+ UserNotFoundException, QuotaExceededException, ScriptNotFoundException,
+ IsActiveException, FileNotFoundException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ String content = "01234567";
+
+ // Delete existent inactive script
+ repo.putScript(user, scriptName, content);
+ repo.deleteScript(user, scriptName);
+ assertTrue("Script deletion failed", !new File(fs.getFile(SIEVE_ROOT), user + '/'
+ + scriptName).exists());
+
+ // Delete existent active script
+ repo.putScript(user, scriptName, content);
+ repo.setActive(user, scriptName);
+ boolean isActiveExceptionThrown = false;
+ try {
+ repo.deleteScript(user, scriptName);
+ } catch (IsActiveException ex) {
+ isActiveExceptionThrown = true;
+ }
+ assertTrue(isActiveExceptionThrown);
+
+ // Delete non existent script
+ boolean scriptNotFoundExceptionThrown = false;
+ try {
+ repo.deleteScript(user, "nonExistent");
+ } catch (ScriptNotFoundException ex) {
+ scriptNotFoundExceptionThrown = true;
+ }
+ assertTrue(scriptNotFoundExceptionThrown);
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#getScript(java.lang.String, java.lang.String)}.
+ * @throws StorageException
+ * @throws DuplicateUserException
+ * @throws UserNotFoundException
+ * @throws QuotaExceededException
+ * @throws ScriptNotFoundException
+ */
+ @Test
+ public final void testGetScript() throws DuplicateUserException, StorageException, UserNotFoundException, QuotaExceededException, ScriptNotFoundException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ String content = "01234567";
+
+ // Non existent script
+ boolean scriptNotFoundExceptionThrown = false;
+ try {
+ repo.getScript(user, scriptName);
+ } catch (ScriptNotFoundException ex) {
+ scriptNotFoundExceptionThrown = true;
+ }
+ assertTrue(scriptNotFoundExceptionThrown);
+
+ // Existent script
+ repo.putScript(user, scriptName, content);
+ assertEquals("Script content did not match", content, repo.getScript(user, scriptName));
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#haveSpace(java.lang.String, java.lang.String, long)}.
+ * @throws DuplicateUserException
+ * @throws QuotaExceededException
+ * @throws UserNotFoundException
+ * @throws StorageException
+ * @throws ScriptNotFoundException
+ */
+ @Test
+ public final void testHaveSpace() throws DuplicateUserException, UserNotFoundException, QuotaExceededException, StorageException, ScriptNotFoundException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ long defaultQuota = Long.MAX_VALUE - 1;
+ long userQuota = Long.MAX_VALUE / 2;
+ boolean quotaExceededExceptionThrown = false;
+
+ // No quota
+ repo.haveSpace(user, scriptName, defaultQuota + 1);
+
+ // Default quota
+ repo.setQuota(defaultQuota);
+ // Default quota - not exceeded
+ repo.haveSpace(user, scriptName, defaultQuota);
+ // Default quota - exceeded
+ quotaExceededExceptionThrown = false;
+ try {
+ repo.haveSpace(user, scriptName, defaultQuota + 1);
+ } catch (QuotaExceededException ex) {
+ quotaExceededExceptionThrown = true;
+ }
+ assertTrue(quotaExceededExceptionThrown);
+
+ // User quota file
+ repo.setQuota(user, userQuota);
+ // User quota - not exceeded
+ repo.haveSpace(user, scriptName, userQuota);
+ // User quota - exceeded
+ quotaExceededExceptionThrown = false;
+ try {
+ repo.haveSpace(user, scriptName, userQuota + 1);
+ } catch (QuotaExceededException ex) {
+ quotaExceededExceptionThrown = true;
+ }
+ assertTrue(quotaExceededExceptionThrown);
+
+ // Script replacement
+ String content = "01234567";
+ repo.putScript(user, scriptName, content);
+ // Script replacement, quota not exceeded
+ repo.haveSpace(user, scriptName, userQuota);
+ // Script replacement, quota exceeded
+ quotaExceededExceptionThrown = false;
+ try {
+ repo.haveSpace(user, scriptName, userQuota + 1);
+ } catch (QuotaExceededException ex) {
+ quotaExceededExceptionThrown = true;
+ }
+ assertTrue(quotaExceededExceptionThrown);
+
+ // Active script
+ repo.setActive(user, scriptName);
+ // User quota - not exceeded
+ repo.haveSpace(user, scriptName, userQuota);
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#listScripts(java.lang.String)}.
+ * @throws StorageException
+ * @throws DuplicateUserException
+ * @throws UserNotFoundException
+ * @throws QuotaExceededException
+ * @throws ScriptNotFoundException
+ */
+ @Test
+ public final void testListScripts() throws DuplicateUserException, StorageException, UserNotFoundException, QuotaExceededException, ScriptNotFoundException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ String content = "01234567";
+ String scriptName1 = "script1";
+ String content1 = "abcdefgh";
+
+ // No scripts
+ assertTrue(repo.listScripts(user).isEmpty());
+
+ // Inactive script
+ repo.putScript(user, scriptName, content);
+ List<ScriptSummary> summaries = repo.listScripts(user);
+ assertEquals(1, summaries.size());
+ assertEquals(scriptName, summaries.get(0).getName());
+ assertTrue(!summaries.get(0).isActive());
+
+ // Active script
+ repo.setActive(user, scriptName);
+ summaries = repo.listScripts(user);
+ assertEquals(1, summaries.size());
+ assertEquals(scriptName, summaries.get(0).getName());
+ assertTrue(summaries.get(0).isActive());
+
+ // One of each
+ repo.putScript(user, scriptName1, content1);
+ summaries = repo.listScripts(user);
+ assertEquals(2, summaries.size());
+ assertEquals(scriptName, summaries.get(0).getName());
+ assertTrue(summaries.get(0).isActive());
+ assertEquals(scriptName1, summaries.get(1).getName());
+ assertTrue(!summaries.get(1).isActive());
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#putScript(java.lang.String, java.lang.String, java.lang.String)}.
+ * @throws DuplicateUserException
+ * @throws QuotaExceededException
+ * @throws StorageException
+ * @throws UserNotFoundException
+ * @throws FileNotFoundException
+ */
+ @Test
+ public final void testPutScript() throws DuplicateUserException, UserNotFoundException,
+ StorageException, QuotaExceededException, FileNotFoundException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ String content = "01234567";
+
+ // test new script
+ repo.putScript(user, scriptName, content);
+ assertTrue("Script creation failed", new File(fs.getFile(SIEVE_ROOT), user + '/'
+ + scriptName).exists());
+
+ // test script replacement
+ repo.putScript(user, scriptName, content);
+ assertTrue("Script replacement failed", new File(fs.getFile(SIEVE_ROOT), user + '/'
+ + scriptName).exists());
+
+ // test quota
+ repo.setQuota(content.length());
+ repo.putScript(user, scriptName, content);
+ repo.setQuota(content.length() - 1);
+ boolean quotaExceededExceptionThrown = false;
+ try {
+ repo.putScript(user, scriptName, content);
+ } catch (QuotaExceededException ex) {
+ quotaExceededExceptionThrown = true;
+ }
+ assertTrue(quotaExceededExceptionThrown);
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#renameScript(java.lang.String, java.lang.String, java.lang.String)}.
+ * @throws StorageException
+ * @throws DuplicateUserException
+ * @throws DuplicateException
+ * @throws IsActiveException
+ * @throws UserNotFoundException
+ * @throws ScriptNotFoundException
+ * @throws QuotaExceededException
+ */
+ @Test
+ public final void testRenameScript() throws DuplicateUserException, StorageException, UserNotFoundException, IsActiveException, DuplicateException, ScriptNotFoundException, QuotaExceededException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ String content = "01234567";
+ String scriptName1 = "script1";
+
+ // Non existent script
+ boolean scriptNotFoundExceptionThrown = false;
+ try {
+ repo.renameScript(user, scriptName, scriptName1);
+ } catch (ScriptNotFoundException ex) {
+ scriptNotFoundExceptionThrown = true;
+ }
+ assertTrue(scriptNotFoundExceptionThrown);
+
+ // Existent script
+ repo.putScript(user, scriptName, content);
+ repo.renameScript(user, scriptName, scriptName1);
+ assertEquals("Script content did not match", content, repo.getScript(user, scriptName1));
+
+ // Propagate active script
+ repo.setActive(user, scriptName1);
+ repo.renameScript(user, scriptName1, scriptName);
+ assertEquals("Script content did not match", content, repo.getActive(user));
+
+ // Duplicate script
+ repo.setActive(user, "");
+ boolean duplicateExceptionThrown = false;
+ try {
+ repo.renameScript(user, scriptName, scriptName);
+ } catch (DuplicateException ex) {
+ duplicateExceptionThrown = true;
+ }
+ assertTrue(duplicateExceptionThrown);
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#getActive(java.lang.String)}.
+ * @throws StorageException
+ * @throws DuplicateUserException
+ * @throws QuotaExceededException
+ * @throws UserNotFoundException
+ * @throws ScriptNotFoundException
+ */
+ @Test
+ public final void testGetActive() throws DuplicateUserException, StorageException, UserNotFoundException, QuotaExceededException, ScriptNotFoundException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ String content = "01234567";
+
+ // Non existent script
+ boolean scriptNotFoundExceptionThrown = false;
+ try {
+ repo.getActive(user);
+ } catch (ScriptNotFoundException ex) {
+ scriptNotFoundExceptionThrown = true;
+ }
+ assertTrue(scriptNotFoundExceptionThrown);
+
+ // Inactive script
+ repo.putScript(user, scriptName, content);
+ scriptNotFoundExceptionThrown = false;
+ try {
+ repo.getActive(user);
+ } catch (ScriptNotFoundException ex) {
+ scriptNotFoundExceptionThrown = true;
+ }
+ assertTrue(scriptNotFoundExceptionThrown);
+
+ // Active script
+ repo.setActive(user, scriptName);
+ assertEquals("Script content did not match", content, repo.getActive(user));
+ }
+
+ /**
+ * Test method for {@link org.apache.james.managesieve.file.SieveFileRepository#setActive(java.lang.String, java.lang.String)}.
+ * @throws StorageException
+ * @throws DuplicateUserException
+ * @throws UserNotFoundException
+ * @throws ScriptNotFoundException
+ * @throws QuotaExceededException
+ */
+ @Test
+ public final void testSetActive() throws DuplicateUserException, StorageException,
+ UserNotFoundException, ScriptNotFoundException, QuotaExceededException {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+ String scriptName = "script";
+ String content = "01234567";
+ String scriptName1 = "script1";
+ String content1 = "abcdefgh";
+
+ // Non existent script
+ boolean scriptNotFoundExceptionThrown = false;
+ try {
+ repo.setActive(user, scriptName);
+ } catch (ScriptNotFoundException ex) {
+ scriptNotFoundExceptionThrown = true;
+ }
+ assertTrue(scriptNotFoundExceptionThrown);
+
+ // Existent script
+ repo.putScript(user, scriptName, content);
+ repo.setActive(user, scriptName);
+ assertEquals("Script content did not match", content, repo.getActive(user));
+
+ // Switch active script
+ repo.putScript(user, scriptName1, content1);
+ scriptNotFoundExceptionThrown = false;
+ repo.setActive(user, scriptName1);
+ assertEquals("Script content did not match", content1, repo.getActive(user));
+
+ // Disable active script
+ repo.setActive(user, "");
+ scriptNotFoundExceptionThrown = false;
+ try {
+ repo.getActive(user);
+ } catch (ScriptNotFoundException ex) {
+ scriptNotFoundExceptionThrown = true;
+ }
+ assertTrue(scriptNotFoundExceptionThrown);
+ }
+
+ @Test
+ public final void testAddUser() throws DuplicateUserException, StorageException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+
+ repo.addUser(user);
+ assertTrue(repo.hasUser(user));
+ }
+
+ @Test
+ public final void testRemoveUser() throws StorageException, DuplicateUserException, UserNotFoundException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+
+ // Non existent user
+ boolean userNotFoundExceptionThrown = false;
+ try {
+ repo.removeUser(user);
+ } catch (UserNotFoundException ex) {
+ userNotFoundExceptionThrown = true;
+ }
+ assertTrue(userNotFoundExceptionThrown);
+
+ // Existent user
+ repo.addUser(user);
+ repo.removeUser(user);
+ assertTrue(!repo.hasUser(user));
+ }
+
+ @Test
+ public final void testHasUser() throws DuplicateUserException, StorageException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+
+ // Non existent user
+ assertTrue(!repo.hasUser(user));
+
+ // Existent user
+ repo.addUser(user);
+ assertTrue(repo.hasUser(user));
+ }
+
+ @Test
+ public final void testGetQuota() throws StorageException, QuotaNotFoundException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+
+ // Non existent quota
+ boolean quotaNotFoundExceptionThrown = false;
+ try {
+ repo.getQuota();
+ } catch (QuotaNotFoundException ex) {
+ quotaNotFoundExceptionThrown = true;
+ }
+ assertTrue(quotaNotFoundExceptionThrown);
+
+ // Existent Quota
+ repo.setQuota(Long.MAX_VALUE);
+ assertEquals(Long.MAX_VALUE, repo.getQuota());
+ }
+
+ @Test
+ public final void testHasQuota() throws StorageException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+
+ // Non existent quota
+ assertTrue(!repo.hasQuota());
+
+ // Existent quota
+ repo.setQuota(Long.MAX_VALUE);
+ assertTrue(repo.hasQuota());
+ }
+
+ @Test
+ public final void testRemoveQuota() throws StorageException, QuotaNotFoundException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+
+ // Non existent quota
+ boolean quotaNotFoundExceptionThrown = false;
+ try {
+ repo.removeQuota();
+ } catch (QuotaNotFoundException ex) {
+ quotaNotFoundExceptionThrown = true;
+ }
+ assertTrue(quotaNotFoundExceptionThrown);
+
+ // Existent quota
+ repo.setQuota(Long.MAX_VALUE);
+ repo.removeQuota();
+ assertTrue(!repo.hasQuota());
+ }
+
+ @Test
+ public final void testSetQuota() throws QuotaNotFoundException, StorageException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+
+ repo.setQuota(Long.MAX_VALUE);
+ assertEquals(Long.MAX_VALUE, repo.getQuota());
+ }
+
+ @Test
+ public final void testGetUserQuota() throws StorageException, QuotaNotFoundException, DuplicateUserException, UserNotFoundException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+
+ // Non existent quota
+ boolean quotaNotFoundExceptionThrown = false;
+ try {
+ repo.getQuota(user);
+ } catch (QuotaNotFoundException ex) {
+ quotaNotFoundExceptionThrown = true;
+ }
+ assertTrue(quotaNotFoundExceptionThrown);
+
+ // Existent Quota
+ repo.setQuota(user, Long.MAX_VALUE);
+ assertEquals(Long.MAX_VALUE, repo.getQuota(user));
+ }
+
+ @Test
+ public final void testHasUserQuota() throws StorageException, DuplicateUserException, UserNotFoundException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+
+ // Non existent quota
+ assertTrue(!repo.hasQuota(user));
+
+ // Existent quota
+ repo.setQuota(user, Long.MAX_VALUE);
+ assertTrue(repo.hasQuota(user));
+ }
+
+ @Test
+ public final void testRemoveUserQuota() throws StorageException, QuotaNotFoundException, DuplicateUserException, UserNotFoundException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+
+ // Non existent quota
+ boolean quotaNotFoundExceptionThrown = false;
+ try {
+ repo.removeQuota(user);
+ } catch (QuotaNotFoundException ex) {
+ quotaNotFoundExceptionThrown = true;
+ }
+ assertTrue(quotaNotFoundExceptionThrown);
+
+ // Existent quota
+ repo.setQuota(user, Long.MAX_VALUE);
+ repo.removeQuota(user);
+ assertTrue(!repo.hasQuota(user));
+ }
+
+ @Test
+ public final void testSetUserQuota() throws QuotaNotFoundException, StorageException, DuplicateUserException, UserNotFoundException
+ {
+ SieveRepository repo = new SieveFileRepository(fs);
+ String user = "test";
+ repo.addUser(user);
+
+ repo.setQuota(user, Long.MAX_VALUE);
+ assertEquals(Long.MAX_VALUE, repo.getQuota(user));
+ }
+
+}
Propchange: james/mangesieve/server/src/test/java/org/apache/james/managesieve/file/SieveFileRepositoryTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: james/mangesieve/server/src/test/java/org/apache/james/managesieve/file/SieveFileRepositoryTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org