-<?xml version="1.0" encoding="UTF-8" standalone="no"?><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/maven-v4_0_0.xsd">
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.argeo.commons</groupId>
<artifactId>org.argeo.util</artifactId>
<version>2.1.79-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.argeo.commons</groupId>
+ <artifactId>org.argeo.enterprise</artifactId>
+ <version>2.1.79-SNAPSHOT</version>
+ </dependency>
<dependency>
<groupId>org.argeo.commons</groupId>
<artifactId>org.argeo.jcr</artifactId>
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.spi.FileSystemProvider;
-import java.time.ZonedDateTime;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.argeo.jackrabbit.fs.DavexFsProvider;
import org.argeo.ssh.Sftp;
import org.argeo.sync.SyncException;
-import org.argeo.util.LangUtils;
+/** Synchronises two paths. */
public class PathSync implements Runnable {
- private final static Log log = LogFactory.getLog(PathSync.class);
-
private final URI sourceUri, targetUri;
+ private boolean delete = false;
public PathSync(URI sourceUri, URI targetUri) {
this.sourceUri = sourceUri;
try {
Path sourceBasePath = createPath(sourceUri);
Path targetBasePath = createPath(targetUri);
- SyncFileVisitor syncFileVisitor = new SyncFileVisitor(sourceBasePath, targetBasePath);
- ZonedDateTime begin = ZonedDateTime.now();
+ SyncFileVisitor syncFileVisitor = new SyncFileVisitor(sourceBasePath, targetBasePath, delete);
Files.walkFileTree(sourceBasePath, syncFileVisitor);
- if (log.isDebugEnabled())
- log.debug("Sync from " + sourceBasePath + " to " + targetBasePath + " took " + LangUtils.since(begin));
} catch (Exception e) {
e.printStackTrace();
}
throw new SyncException("URI scheme not supported for " + uri);
return path;
}
-
- static enum Arg {
- to, from;
- }
}
package org.argeo.sync.fs;
-import java.io.IOException;
-import java.nio.file.FileVisitResult;
-import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.SimpleFileVisitor;
-import java.nio.file.attribute.BasicFileAttributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.argeo.fs.BasicSyncFileVisitor;
/** Synchronises two directory structures. */
-public class SyncFileVisitor extends SimpleFileVisitor<Path> {
+public class SyncFileVisitor extends BasicSyncFileVisitor {
private final static Log log = LogFactory.getLog(SyncFileVisitor.class);
- private final Path sourceBasePath;
- private final Path targetBasePath;
-
- public SyncFileVisitor(Path sourceBasePath, Path targetBasePath) {
- this.sourceBasePath = sourceBasePath;
- this.targetBasePath = targetBasePath;
+ public SyncFileVisitor(Path sourceBasePath, Path targetBasePath, boolean delete) {
+ super(sourceBasePath, targetBasePath, delete);
}
@Override
- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
- Path targetPath = toTargetPath(dir);
- Files.createDirectories(targetPath);
- return FileVisitResult.CONTINUE;
+ protected void error(Object obj, Throwable e) {
+ log.error(obj, e);
}
@Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- Path targetPath = toTargetPath(file);
- try {
- Files.copy(file, targetPath);
- if (log.isDebugEnabled())
- log.debug("Copied " + targetPath);
- } catch (Exception e) {
- log.error("Cannot copy " + file + " to " + targetPath, e);
- }
- return FileVisitResult.CONTINUE;
+ protected boolean isDebugEnabled() {
+ return log.isDebugEnabled();
}
@Override
- public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
- log.error("Cannot sync " + file, exc);
- return FileVisitResult.CONTINUE;
- }
-
- private Path toTargetPath(Path sourcePath) {
- Path relativePath = sourceBasePath.relativize(sourcePath);
- Path targetPath = targetBasePath.resolve(relativePath.toString());
- return targetPath;
+ protected void debug(Object obj) {
+ log.debug(obj);
}
}
+Bundle-Activator: org.argeo.osgi.internal.EnterpriseActivator
+Bundle-ActivationPolicy: lazy
+
Import-Package: org.osgi.*;version=0.0.0,\
+!org.apache.commons.logging,\
*
source.. = src/,\
ext/test/
additional.bundles = org.junit,\
- org.slf4j.commons.logging,\
- org.slf4j.api,\
- org.slf4j.log4j12,\
- org.apache.log4j
+ org.argeo.util
--- /dev/null
+import org.argeo.fs.FsUtilsTest;
+import org.argeo.osgi.useradmin.LdifParserTest;
+import org.argeo.osgi.useradmin.UserAdminConfTest;
+import org.argeo.util.test.Tester;
+
+class AllEnterpriseTests {
+
+ public static void main(String[] args) throws Exception {
+ Tester tester = new Tester();
+
+ // FS
+ tester.execute(FsUtilsTest.class.getName());
+
+ // User admin
+ tester.execute(LdifParserTest.class.getName());
+ //tester.execute(LdifUserAdminTest.class.getName());
+ tester.execute(UserAdminConfTest.class.getName());
+ }
+
+}
+++ /dev/null
-log4j.rootLogger=WARN, console
-
-## Levels
-log4j.logger.org.argeo=TRACE
-
-log4j.logger.org.hibernate=WARN
-
-log4j.logger.org.springframework=WARN
-#log4j.logger.org.springframework.web=DEBUG
-#log4j.logger.org.springframework.jms=WARN
-#log4j.logger.org.springframework.security=WARN
-
-log4j.logger.org.apache.activemq=WARN
-log4j.logger.org.apache.activemq.transport=WARN
-log4j.logger.org.apache.activemq.ActiveMQMessageConsumer=INFO
-log4j.logger.org.apache.activemq.ActiveMQMessageProducer=INFO
-
-log4j.logger.org.apache.catalina=INFO
-log4j.logger.org.apache.coyote=INFO
-log4j.logger.org.apache.tomcat=INFO
-
-## Appenders
-# console is set to be a ConsoleAppender.
-log4j.appender.console=org.apache.log4j.ConsoleAppender
-
-# console uses PatternLayout.
-log4j.appender.console.layout=org.apache.log4j.PatternLayout
-log4j.appender.console.layout.ConversionPattern= %-5p %d{ISO8601} %m - %c%n
--- /dev/null
+package org.argeo.fs;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+/** {@link FsUtils} tests. */
+public class FsUtilsTest {
+ final static String FILE00 = "file00";
+ final static String FILE01 = "file01";
+ final static String SUB_DIR = "subDir";
+
+ public void testDelete() throws IOException {
+ Path dir = createDir00();
+ assert Files.exists(dir);
+ FsUtils.delete(dir);
+ assert !Files.exists(dir);
+ }
+
+ public void testSync() throws IOException {
+ Path source = createDir00();
+ Path target = Files.createTempDirectory(getClass().getName());
+ FsUtils.sync(source, target);
+ assert Files.exists(target.resolve(FILE00));
+ assert Files.exists(target.resolve(SUB_DIR));
+ assert Files.exists(target.resolve(SUB_DIR + File.separator + FILE01));
+ FsUtils.delete(source.resolve(SUB_DIR));
+ FsUtils.sync(source, target, true);
+ assert Files.exists(target.resolve(FILE00));
+ assert !Files.exists(target.resolve(SUB_DIR));
+ assert !Files.exists(target.resolve(SUB_DIR + File.separator + FILE01));
+
+ // clean up
+ FsUtils.delete(source);
+ FsUtils.delete(target);
+
+ }
+
+ Path createDir00() throws IOException {
+ Path base = Files.createTempDirectory(getClass().getName());
+ base.toFile().deleteOnExit();
+ Files.createFile(base.resolve(FILE00)).toFile().deleteOnExit();
+ Path subDir = Files.createDirectories(base.resolve(SUB_DIR));
+ subDir.toFile().deleteOnExit();
+ Files.createFile(subDir.resolve(FILE01)).toFile().deleteOnExit();
+ return base;
+ }
+}
import org.argeo.naming.LdapAttrs;
import org.argeo.naming.LdifParser;
-import junit.framework.TestCase;
-
-public class LdifParserTest extends TestCase implements BasicTestConstants {
+/** {@link LdifParser} tests. */
+public class LdifParserTest implements BasicTestConstants {
public void testBasicLdif() throws Exception {
LdifParser ldifParser = new LdifParser();
- SortedMap<LdapName, Attributes> res = ldifParser.read(getClass()
- .getResourceAsStream("basic.ldif"));
+ SortedMap<LdapName, Attributes> res = ldifParser.read(getClass().getResourceAsStream("basic.ldif"));
LdapName rootDn = new LdapName(ROOT_USER_DN);
Attributes rootAttributes = res.get(rootDn);
- assertNotNull(rootAttributes);
- assertEquals("Superuser",
- rootAttributes.get(LdapAttrs.description.name()).get());
- byte[] rawPwEntry = (byte[]) rootAttributes.get(
- LdapAttrs.userPassword.name()).get();
- assertEquals("{SHA}ieSV55Qc+eQOaYDRSha/AjzNTJE=",
- new String(rawPwEntry));
+ assert rootAttributes != null;
+ assert "Superuser".equals(rootAttributes.get(LdapAttrs.description.name()).get());
+ byte[] rawPwEntry = (byte[]) rootAttributes.get(LdapAttrs.userPassword.name()).get();
+ assert "{SHA}ieSV55Qc+eQOaYDRSha/AjzNTJE=".contentEquals(new String(rawPwEntry));
byte[] hashedPassword = DigestUtils.sha1("demo".getBytes());
- assertEquals("{SHA}" + Base64.getEncoder().encodeToString(hashedPassword),
- new String(rawPwEntry));
+ assert ("{SHA}" + Base64.getEncoder().encodeToString(hashedPassword)).equals(new String(rawPwEntry));
LdapName adminDn = new LdapName(ADMIN_GROUP_DN);
Attributes adminAttributes = res.get(adminDn);
- assertNotNull(adminAttributes);
+ assert adminAttributes != null;
Attribute memberAttribute = adminAttributes.get(LdapAttrs.member.name());
- assertNotNull(memberAttribute);
+ assert memberAttribute != null;
NamingEnumeration<?> members = memberAttribute.getAll();
List<String> users = new ArrayList<String>();
while (members.hasMore()) {
Object value = members.next();
users.add(value.toString());
}
- assertEquals(1, users.size());
- assertEquals(rootDn, new LdapName(users.get(0)));
+ assert 1 == users.size();
+ assert rootDn.equals(new LdapName(users.get(0)));
}
}
import junit.framework.TestCase;
+/** {@link LdifUserAdmin} tests. */
public class LdifUserAdminTest extends TestCase implements BasicTestConstants {
+ // We have to keep using JUnit because of
+ // https://issues.apache.org/jira/browse/SUREFIRE-1669
+
final static int TM_SIMPLE = 0;
final static int TM_BITRONIX = 1;
private AbstractUserDirectory userAdmin;
private Path tempDir;
- // public void testConcurrent() throws Exception {
- // }
+ public void setUp() {
+ System.out.println("Enter setUp()");
+ try {
+ tempDir = Files.createTempDirectory(getClass().getName());
+ tempDir.toFile().deleteOnExit();
+ String uriProp = System.getProperty("argeo.userdirectory.uri");
+ if (uriProp != null)
+ uri = new URI(uriProp);
+ else {
+ tempDir.toFile().deleteOnExit();
+ Path ldifPath = tempDir.resolve(BASE_DN + ".ldif");
+ try (InputStream in = getClass().getResource("basic.ldif").openStream()) {
+ Files.copy(in, ldifPath);
+ }
+ uri = ldifPath.toUri();
+ }
+
+ // Init transaction manager
+ if (TM_SIMPLE == tmType) {
+ tm = new SimpleTransactionManager();
+ }
+// else if (TM_BITRONIX == tmType) {
+// bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
+// tmConf.setServerId(UUID.randomUUID().toString());
+// tmConf.setLogPart1Filename(new File(tempDir.toFile(), "btm1.tlog").getAbsolutePath());
+// tmConf.setLogPart2Filename(new File(tempDir.toFile(), "btm2.tlog").getAbsolutePath());
+// tm = TransactionManagerServices.getTransactionManager();
+// }
+
+ userAdmin = initUserAdmin(uri, tm);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
public void testEdition() throws Exception {
User demoUser = (User) userAdmin.getRole(DEMO_USER_DN);
- assertNotNull(demoUser);
+ assert demoUser != null;
tm.begin();
String newName = "demo";
demoUser.getProperties().put("cn", newName);
- assertEquals(newName, demoUser.getProperties().get("cn"));
+ assert newName.equals(demoUser.getProperties().get("cn"));
tm.commit();
persistAndRestart();
- assertEquals(newName, demoUser.getProperties().get("cn"));
+ assert newName.equals(demoUser.getProperties().get("cn"));
tm.begin();
userAdmin.removeRole(DEMO_USER_DN);
// check data
Role[] search = userAdmin.getRoles("(objectclass=inetOrgPerson)");
- assertEquals(1, search.length);
+ assert 1 == search.length;
Group editorGroup = (Group) userAdmin.getRole(EDITORS_GROUP_DN);
- assertNotNull(editorGroup);
+ assert editorGroup != null;
Role[] members = editorGroup.getMembers();
- assertEquals(1, members.length);
+ assert 1 == members.length;
}
public void testRetrieve() throws Exception {
// users
User rootUser = (User) userAdmin.getRole(ROOT_USER_DN);
- assertNotNull(rootUser);
+ assert rootUser != null;
User demoUser = (User) userAdmin.getRole(DEMO_USER_DN);
- assertNotNull(demoUser);
+ assert demoUser != null;
// groups
Group adminGroup = (Group) userAdmin.getRole(ADMIN_GROUP_DN);
- assertNotNull(adminGroup);
+ assert adminGroup != null;
Role[] members = adminGroup.getMembers();
- assertEquals(1, members.length);
- assertEquals(rootUser, members[0]);
+ assert 1 == members.length;
+ assert rootUser.equals(members[0]);
Group editorGroup = (Group) userAdmin.getRole(EDITORS_GROUP_DN);
- assertNotNull(editorGroup);
+ assert editorGroup != null;
members = editorGroup.getMembers();
- assertEquals(2, members.length);
- assertEquals(adminGroup, members[0]);
- assertEquals(demoUser, members[1]);
+ assert 2 == members.length;
+ assert adminGroup.equals(members[0]);
+ assert demoUser.equals(members[1]);
Authorization rootAuth = userAdmin.getAuthorization(rootUser);
List<String> rootRoles = Arrays.asList(rootAuth.getRoles());
- assertEquals(3, rootRoles.size());
- assertTrue(rootRoles.contains(ROOT_USER_DN));
- assertTrue(rootRoles.contains(ADMIN_GROUP_DN));
- assertTrue(rootRoles.contains(EDITORS_GROUP_DN));
+ assert 3 == rootRoles.size();
+ assert rootRoles.contains(ROOT_USER_DN);
+ assert rootRoles.contains(ADMIN_GROUP_DN);
+ assert rootRoles.contains(EDITORS_GROUP_DN);
// properties
- assertEquals("root@localhost", rootUser.getProperties().get("mail"));
+ assert "root@localhost".equals(rootUser.getProperties().get("mail"));
// credentials
byte[] hashedPassword = ("{SHA}" + Base64.getEncoder().encodeToString(DigestUtils.sha1("demo".getBytes())))
.getBytes();
- assertTrue(rootUser.hasCredential(LdapAttrs.userPassword.name(), hashedPassword));
- assertTrue(demoUser.hasCredential(LdapAttrs.userPassword.name(), hashedPassword));
+ assert rootUser.hasCredential(LdapAttrs.userPassword.name(), hashedPassword);
+ assert demoUser.hasCredential(LdapAttrs.userPassword.name(), hashedPassword);
// search
Role[] search = userAdmin.getRoles(null);
- assertEquals(4, search.length);
+ assert 4 == search.length;
search = userAdmin.getRoles("(objectClass=groupOfNames)");
- assertEquals(2, search.length);
+ assert 2 == search.length;
search = userAdmin.getRoles("(objectclass=inetOrgPerson)");
- assertEquals(2, search.length);
+ assert 2 == search.length;
search = userAdmin.getRoles("(&(objectclass=inetOrgPerson)(uid=demo))");
- assertEquals(1, search.length);
+ assert 1 == search.length;
}
public void testReadWriteRead() throws Exception {
((LdifUserAdmin) userAdmin).load(in);
}
Role[] search = userAdmin.getRoles(null);
- assertEquals(4, search.length);
+ assert 4 == search.length;
} else {
// test not relevant for LDAP
}
}
- @Override
- protected void setUp() throws Exception {
- tempDir = Files.createTempDirectory(getClass().getName());
- tempDir.toFile().deleteOnExit();
- String uriProp = System.getProperty("argeo.userdirectory.uri");
- if (uriProp != null)
- uri = new URI(uriProp);
- else {
- tempDir.toFile().deleteOnExit();
- Path ldifPath = tempDir.resolve(BASE_DN + ".ldif");
- try (InputStream in = getClass().getResource("basic.ldif").openStream()) {
- Files.copy(in, ldifPath);
- }
- uri = ldifPath.toUri();
- }
-
- // Init transaction manager
- if (TM_SIMPLE == tmType) {
- tm = new SimpleTransactionManager();
- }
-// else if (TM_BITRONIX == tmType) {
-// bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
-// tmConf.setServerId(UUID.randomUUID().toString());
-// tmConf.setLogPart1Filename(new File(tempDir.toFile(), "btm1.tlog").getAbsolutePath());
-// tmConf.setLogPart2Filename(new File(tempDir.toFile(), "btm2.tlog").getAbsolutePath());
-// tm = TransactionManagerServices.getTransactionManager();
-// }
-
- userAdmin = initUserAdmin(uri, tm);
- }
-
private AbstractUserDirectory initUserAdmin(URI uri, TransactionManager tm) {
Dictionary<String, Object> props = new Hashtable<>();
props.put(UserAdminConf.uri.name(), uri.toString());
userAdmin = initUserAdmin(uri, tm);
}
- @Override
- protected void tearDown() throws Exception {
+ public void tearDown() throws Exception {
// if (TM_BITRONIX == tmType) {
// EhCacheXAResourceProducer.unregisterXAResource(UserDirectory.class.getName(), userAdmin.getXaResource());
// ((BitronixTransactionManager) tm).shutdown();
import java.net.URI;
import java.util.Dictionary;
-import junit.framework.TestCase;
-
-public class UserAdminConfTest extends TestCase {
+/** {@link UserAdminConf} tests. */
+public class UserAdminConfTest {
public void testUriFormat() throws Exception {
// LDAP
URI uriIn = new URI("ldap://" + "uid=admin,ou=system:secret@localhost:10389" + "/dc=example,dc=com"
+ "?readOnly=false&userObjectClass=person");
Dictionary<String, ?> props = uriAsProperties(uriIn.toString());
System.out.println(props);
- assertEquals("dc=example,dc=com", props.get(UserAdminConf.baseDn.name()));
- assertEquals("false", props.get(UserAdminConf.readOnly.name()));
- assertEquals("person", props.get(UserAdminConf.userObjectClass.name()));
+ assert "dc=example,dc=com".equals(props.get(UserAdminConf.baseDn.name()));
+ assert "false".equals(props.get(UserAdminConf.readOnly.name()));
+ assert "person".equals(props.get(UserAdminConf.userObjectClass.name()));
URI uriOut = propertiesAsUri(props);
System.out.println(uriOut);
- assertEquals("/dc=example,dc=com?userObjectClass=person&readOnly=false", uriOut.toString());
+ assert "/dc=example,dc=com?userObjectClass=person&readOnly=false".equals(uriOut.toString());
// File
uriIn = new URI("file://some/dir/dc=example,dc=com.ldif");
props = uriAsProperties(uriIn.toString());
System.out.println(props);
- assertEquals("dc=example,dc=com", props.get(UserAdminConf.baseDn.name()));
+ assert "dc=example,dc=com".equals(props.get(UserAdminConf.baseDn.name()));
// Base configuration
uriIn = new URI("/dc=example,dc=com.ldif?readOnly=true&userBase=ou=CoWorkers,ou=People&groupBase=ou=Roles");
props = uriAsProperties(uriIn.toString());
System.out.println(props);
- assertEquals("dc=example,dc=com", props.get(UserAdminConf.baseDn.name()));
- assertEquals("true", props.get(UserAdminConf.readOnly.name()));
- assertEquals("ou=CoWorkers,ou=People", props.get(UserAdminConf.userBase.name()));
- assertEquals("ou=Roles", props.get(UserAdminConf.groupBase.name()));
+ assert "dc=example,dc=com".equals(props.get(UserAdminConf.baseDn.name()));
+ assert "true".equals(props.get(UserAdminConf.readOnly.name()));
+ assert "ou=CoWorkers,ou=People".equals(props.get(UserAdminConf.userBase.name()));
+ assert "ou=Roles".equals(props.get(UserAdminConf.groupBase.name()));
uriOut = propertiesAsUri(props);
System.out.println(uriOut);
- assertEquals("/dc=example,dc=com?userBase=ou=CoWorkers,ou=People&groupBase=ou=Roles&readOnly=true", uriOut.toString());
+ assert "/dc=example,dc=com?userBase=ou=CoWorkers,ou=People&groupBase=ou=Roles&readOnly=true"
+ .equals(uriOut.toString());
// OS
uriIn = new URI("os:///dc=example,dc=com");
props = uriAsProperties(uriIn.toString());
System.out.println(props);
- assertEquals("dc=example,dc=com", props.get(UserAdminConf.baseDn.name()));
- assertEquals("true", props.get(UserAdminConf.readOnly.name()));
+ assert "dc=example,dc=com".equals(props.get(UserAdminConf.baseDn.name()));
+ assert "true".equals(props.get(UserAdminConf.readOnly.name()));
uriOut = propertiesAsUri(props);
System.out.println(uriOut);
- assertEquals("/dc=example,dc=com?readOnly=true", uriOut.toString());
+ assert "/dc=example,dc=com?readOnly=true".equals(uriOut.toString());
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<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/maven-v4_0_0.xsd">
+<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.argeo.commons</groupId>
</parent>
<artifactId>org.argeo.enterprise</artifactId>
<name>Commons Enterprise</name>
+ <dependencies>
+ <dependency>
+ <groupId>org.argeo.commons</groupId>
+ <artifactId>org.argeo.util</artifactId>
+ <version>2.1.79-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
\ No newline at end of file
--- /dev/null
+package org.argeo.fs;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.attribute.FileTime;
+
+/** Synchronises two directory structures. */
+public class BasicSyncFileVisitor extends SimpleFileVisitor<Path> {
+ // TODO make it configurable
+ private boolean debug = true;
+
+ private final Path sourceBasePath;
+ private final Path targetBasePath;
+ private final boolean delete;
+
+ public BasicSyncFileVisitor(Path sourceBasePath, Path targetBasePath, boolean delete) {
+ this.sourceBasePath = sourceBasePath;
+ this.targetBasePath = targetBasePath;
+ this.delete = delete;
+ }
+
+ @Override
+ public FileVisitResult preVisitDirectory(Path sourceDir, BasicFileAttributes attrs) throws IOException {
+ Path targetDir = toTargetPath(sourceDir);
+ Files.createDirectories(targetDir);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path sourceDir, IOException exc) throws IOException {
+ if (delete) {
+ Path targetDir = toTargetPath(sourceDir);
+ for (Path targetPath : Files.newDirectoryStream(targetDir)) {
+ Path sourcePath = sourceDir.resolve(targetPath.getFileName());
+ if (!Files.exists(sourcePath)) {
+ try {
+ FsUtils.delete(targetPath);
+ deleted(targetPath);
+ } catch (Exception e) {
+ deleteFailed(targetPath, exc);
+ }
+ }
+ }
+ }
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException {
+ Path targetFile = toTargetPath(sourceFile);
+ try {
+ if (!Files.exists(targetFile)) {
+ Files.copy(sourceFile, targetFile);
+ copied(sourceFile, targetFile);
+ } else {
+ if (shouldOverwrite(sourceFile, targetFile)) {
+ Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
+ }
+ }
+ } catch (Exception e) {
+ copyFailed(sourceFile, targetFile, e);
+ }
+ return FileVisitResult.CONTINUE;
+ }
+
+ protected boolean shouldOverwrite(Path sourceFile, Path targetFile) throws IOException {
+ long sourceSize = Files.size(sourceFile);
+ long targetSize = Files.size(targetFile);
+ if (sourceSize != targetSize) {
+ return true;
+ }
+ FileTime sourceLastModif = Files.getLastModifiedTime(sourceFile);
+ FileTime targetLastModif = Files.getLastModifiedTime(targetFile);
+ if (sourceLastModif.compareTo(targetLastModif) > 0)
+ return true;
+ return shouldOverwriteLaterSameSize(sourceFile, targetFile);
+ }
+
+ protected boolean shouldOverwriteLaterSameSize(Path sourceFile, Path targetFile) {
+ return false;
+ }
+
+// @Override
+// public FileVisitResult visitFileFailed(Path sourceFile, IOException exc) throws IOException {
+// error("Cannot sync " + sourceFile, exc);
+// return FileVisitResult.CONTINUE;
+// }
+
+ private Path toTargetPath(Path sourcePath) {
+ Path relativePath = sourceBasePath.relativize(sourcePath);
+ Path targetPath = targetBasePath.resolve(relativePath.toString());
+ return targetPath;
+ }
+
+ public Path getSourceBasePath() {
+ return sourceBasePath;
+ }
+
+ public Path getTargetBasePath() {
+ return targetBasePath;
+ }
+
+ protected void copied(Path sourcePath, Path targetPath) {
+ if (isDebugEnabled())
+ debug("Copied " + sourcePath + " to " + targetPath);
+ }
+
+ protected void copyFailed(Path sourcePath, Path targetPath, Exception e) {
+ error("Cannot copy " + sourcePath + " to " + targetPath, e);
+ }
+
+ protected void deleted(Path targetPath) {
+ if (isDebugEnabled())
+ debug("Deleted " + targetPath);
+ }
+
+ protected void deleteFailed(Path targetPath, Exception e) {
+ error("Cannot delete " + targetPath, e);
+ }
+
+ /** Log error. */
+ protected void error(Object obj, Throwable e) {
+ System.err.println(obj);
+ e.printStackTrace();
+ }
+
+ protected boolean isDebugEnabled() {
+ return debug;
+ }
+
+ protected void debug(Object obj) {
+ System.out.println(obj);
+ }
+}
--- /dev/null
+package org.argeo.fs;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+
+/** Utilities around the standard Java file abstractions. */
+public class FsUtils {
+ /** Sync a source path with a target path. */
+ public static void sync(Path sourceBasePath, Path targetBasePath) {
+ sync(sourceBasePath, targetBasePath, false);
+ }
+
+ /** Sync a source path with a target path. */
+ public static void sync(Path sourceBasePath, Path targetBasePath, boolean delete) {
+ sync(new BasicSyncFileVisitor(sourceBasePath, targetBasePath, delete));
+ }
+
+ public static void sync(BasicSyncFileVisitor syncFileVisitor) {
+ try {
+ Files.walkFileTree(syncFileVisitor.getSourceBasePath(), syncFileVisitor);
+ } catch (Exception e) {
+ throw new RuntimeException("Cannot sync " + syncFileVisitor.getSourceBasePath() + " with "
+ + syncFileVisitor.getTargetBasePath(), e);
+ }
+ }
+
+ /** Deletes this path, recursively if needed. */
+ public static void delete(Path path) {
+ try {
+ Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
+ if (e != null)
+ throw e;
+ Files.delete(directory);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Files.delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot delete " + path, e);
+ }
+ }
+
+ /** Singleton. */
+ private FsUtils() {
+ }
+
+}
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
public class DnsBrowser implements Closeable {
- private final static Log log = LogFactory.getLog(DnsBrowser.class);
-
private final DirContext initialCtx;
public DnsBrowser() throws NamingException {
try {
initialCtx.close();
} catch (NamingException e) {
- log.error("Cannot close context", e);
+ // silent
}
}
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.argeo.osgi.useradmin.UserDirectoryException;
/** Basic LDIF parser. */
public class LdifParser {
- private final static Log log = LogFactory.getLog(LdifParser.class);
private final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
protected Attributes addAttributes(SortedMap<LdapName, Attributes> res, int lineNumber, LdapName currentDn,
"Attribute " + nameAttr.getID() + "=" + nameAttr.get() + " not consistent with DN " + currentDn
+ " (shortly before line " + lineNumber + " in LDIF file)");
Attributes previous = res.put(currentDn, currentAttributes);
- if (log.isTraceEnabled())
- log.trace("Added " + currentDn);
return previous;
} catch (NamingException e) {
throw new UserDirectoryException("Cannot add " + currentDn, e);
try {
in.close();
} catch (IOException e) {
- if (log.isTraceEnabled())
- log.error("Cannot close stream", e);
+ // silent
}
}
}
//
Attributes previous = addAttributes(res, lineNumber, currentDn, currentAttributes);
if (previous != null) {
- log.warn("There was already an entry with DN " + currentDn
- + ", which has been discarded by a subsequent one.");
+// log.warn("There was already an entry with DN " + currentDn
+// + ", which has been discarded by a subsequent one.");
}
}
currentDn = new LdapName(attributeValue.toString());
currentAttributes = new BasicAttributes(true);
} catch (InvalidNameException e) {
- log.error(attributeValue + " not a valid DN, skipping the entry.");
+// log.error(attributeValue + " not a valid DN, skipping the entry.");
currentDn = null;
currentAttributes = null;
}
try {
reader.close();
} catch (IOException e) {
- if (log.isTraceEnabled())
- log.error("Cannot close stream", e);
+ // silent
}
}
return res;
--- /dev/null
+package org.argeo.osgi.internal;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+/**
+ * Called to gather information about the OSGi runtime. Should not activate
+ * anything else that canonical monitoring services (not creating implicit
+ * APIs), which is the responsibility of higher levels..
+ */
+public class EnterpriseActivator implements BundleActivator {
+
+ @Override
+ public void start(BundleContext context) throws Exception {
+ }
+
+ @Override
+ public void stop(BundleContext context) throws Exception {
+ }
+
+}
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.argeo.naming.LdapAttrs;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
static final String SHARED_STATE_USERNAME = "javax.security.auth.login.name";
static final String SHARED_STATE_PASSWORD = "javax.security.auth.login.password";
- private final static Log log = LogFactory.getLog(AbstractUserDirectory.class);
-
private final Hashtable<String, Object> properties;
private final LdapName baseDn, userBaseDn, groupBaseDn;
private final String userObjectClass, userBase, groupObjectClass, groupBase;
LdapName groupDn = new LdapName(value.toString());
DirectoryUser group = doGetRole(groupDn);
allRoles.add(group);
- if (log.isTraceEnabled())
- log.trace("Add memberOf " + groupDn);
}
} catch (Exception e) {
throw new UserDirectoryException("Cannot get memberOf groups for " + user, e);
// TODO check for loops
DirectoryUser group = doGetRole(groupDn);
allRoles.add(group);
- if (log.isTraceEnabled())
- log.trace("Add direct group " + groupDn);
collectRoles(group, allRoles);
}
}
doGetUser(key, value, collectedUsers);
} else {
throw new UserDirectoryException("Key cannot be null");
- // // try dn
- // DirectoryUser user = null;
- // try {
- // user = (DirectoryUser) getRole(value);
- // if (user != null)
- // collectedUsers.add(user);
- // } catch (Exception e) {
- // // silent
- // }
- // // try all indexes
- // for (String attr : getIndexedUserProperties())
- // doGetUser(attr, value, collectedUsers);
}
- if (collectedUsers.size() == 1)
+
+ if (collectedUsers.size() == 1) {
return collectedUsers.get(0);
- else if (collectedUsers.size() > 1)
- log.warn(collectedUsers.size() + " users for " + (key != null ? key + "=" : "") + value);
+ } else if (collectedUsers.size() > 1) {
+ // log.warn(collectedUsers.size() + " users for " + (key != null ? key + "=" :
+ // "") + value);
+ }
return null;
}
import javax.naming.ldap.LdapName;
import javax.transaction.TransactionManager;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.argeo.naming.LdapAttrs;
import org.osgi.framework.Filter;
import org.osgi.service.useradmin.Role;
* and an open transaction for write access.
*/
public class LdapUserAdmin extends AbstractUserDirectory {
- private final static Log log = LogFactory.getLog(LdapUserAdmin.class);
-
private InitialLdapContext initialLdapContext = null;
public LdapUserAdmin(Dictionary<String, ?> properties) {
// tls.close();
initialLdapContext.close();
} catch (NamingException e) {
- log.error("Cannot destroy LDAP user admin", e);
+ e.printStackTrace();
}
}
} catch (NameNotFoundException e) {
throw e;
} catch (NamingException e) {
- if (log.isTraceEnabled())
- log.error("Cannot get role: " + name, e);
return null;
}
}
|| objectClassAttr.contains(getUserObjectClass().toLowerCase()))
role = new LdifUser(this, dn, attrs);
else {
- log.warn("Unsupported LDAP type for " + searchResult.getName());
+// log.warn("Unsupported LDAP type for " + searchResult.getName());
continue results;
}
res.add(role);
import javax.naming.NamingException;
import javax.naming.ldap.LdapName;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.argeo.naming.DnsBrowser;
import org.argeo.naming.NamingUtils;
import org.osgi.framework.Constants;
realm(null);
public final static String FACTORY_PID = "org.argeo.osgi.useradmin.config";
- private final static Log log = LogFactory.getLog(UserAdminConf.class);
public final static String SCHEME_LDAP = "ldap";
public final static String SCHEME_FILE = "file";
}
URI convertedUri = new URI(
SCHEME_LDAP + "://" + ldapHostsStr + "/" + IpaUtils.domainToUserDirectoryConfigPath(kerberosRealm));
- if (log.isDebugEnabled())
- log.debug("Converted " + uri + " to " + convertedUri);
return convertedUri;
} catch (NamingException | IOException | URISyntaxException e) {
throw new UserDirectoryException("cannot convert IPA uri " + uri, e);
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
- log.warn("Using localhost as hostname", e);
hostname = "localhost.localdomain";
}
int dotIdx = hostname.indexOf('.');
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
/** {@link XAResource} for a user directory being edited. */
class WcXaResource implements XAResource {
- private final static Log log = LogFactory.getLog(WcXaResource.class);
-
private final AbstractUserDirectory userDirectory;
private Map<Xid, UserDirectoryWorkingCopy> workingCopies = new HashMap<Xid, UserDirectoryWorkingCopy>();
public synchronized void start(Xid xid, int flags) throws XAException {
if (editingXid != null)
throw new UserDirectoryException("Already editing " + editingXid);
- UserDirectoryWorkingCopy wc = workingCopies.put(xid,
- new UserDirectoryWorkingCopy());
+ UserDirectoryWorkingCopy wc = workingCopies.put(xid, new UserDirectoryWorkingCopy());
if (wc != null)
- throw new UserDirectoryException(
- "There is already a working copy for " + xid);
+ throw new UserDirectoryException("There is already a working copy for " + xid);
this.editingXid = xid;
}
return null;
UserDirectoryWorkingCopy wc = workingCopies.get(editingXid);
if (wc == null)
- throw new UserDirectoryException("No working copy found for "
- + editingXid);
+ throw new UserDirectoryException("No working copy found for " + editingXid);
return wc;
}
try {
userDirectory.prepare(wc);
} catch (Exception e) {
- log.error("Cannot prepare " + xid, e);
throw new XAException(XAException.XAER_RMERR);
}
return XA_OK;
userDirectory.prepare(wc);
userDirectory.commit(wc);
} catch (Exception e) {
- log.error("Cannot commit " + xid, e);
throw new XAException(XAException.XAER_RMERR);
} finally {
cleanUp(xid);
checkXid(xid);
userDirectory.rollback(wc(xid));
} catch (Exception e) {
- log.error("Cannot rollback " + xid, e);
throw new XAException(XAException.XAER_RMERR);
} finally {
cleanUp(xid);
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
+/** Simple implementation of an XA {@link Transaction}. */
class SimpleTransaction implements Transaction, Status {
- private final static Log log = LogFactory.getLog(SimpleTransaction.class);
-
private final Xid xid;
private int status = Status.STATUS_ACTIVE;
private final List<XAResource> xaResources = new ArrayList<XAResource>();
}
@Override
- public synchronized void commit() throws RollbackException,
- HeuristicMixedException, HeuristicRollbackException,
+ public synchronized void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
SecurityException, IllegalStateException, SystemException {
status = STATUS_PREPARING;
for (XAResource xaRes : xaResources) {
xaRes.prepare(xid);
} catch (XAException e) {
status = STATUS_MARKED_ROLLBACK;
- log.error("Cannot prepare " + xaRes + " for " + xid, e);
+ error("Cannot prepare " + xaRes + " for " + xid, e);
}
}
if (status == STATUS_MARKED_ROLLBACK) {
xaRes.commit(xid, false);
} catch (XAException e) {
status = STATUS_MARKED_ROLLBACK;
- log.error("Cannot prepare " + xaRes + " for " + xid, e);
+ error("Cannot prepare " + xaRes + " for " + xid, e);
}
}
if (status == STATUS_MARKED_ROLLBACK) {
// complete
status = STATUS_COMMITTED;
- if (log.isTraceEnabled())
- log.trace("COMMITTED " + xid);
clearResources(XAResource.TMSUCCESS);
transactionManager.unregister(xid);
}
@Override
- public synchronized void rollback() throws IllegalStateException,
- SystemException {
+ public synchronized void rollback() throws IllegalStateException, SystemException {
status = STATUS_ROLLING_BACK;
for (XAResource xaRes : xaResources) {
try {
xaRes.rollback(xid);
} catch (XAException e) {
- log.error("Cannot rollback " + xaRes + " for " + xid, e);
+ error("Cannot rollback " + xaRes + " for " + xid, e);
}
}
// complete
status = STATUS_ROLLEDBACK;
- if (log.isTraceEnabled())
- log.trace("ROLLEDBACK " + xid);
clearResources(XAResource.TMFAIL);
transactionManager.unregister(xid);
}
xaRes.start(getXid(), XAResource.TMNOFLAGS);
return true;
} catch (XAException e) {
- log.error("Cannot enlist " + xaRes, e);
+ error("Cannot enlist " + xaRes, e);
return false;
}
} else
try {
xaRes.end(getXid(), flag);
} catch (XAException e) {
- log.error("Cannot delist " + xaRes, e);
+ error("Cannot delist " + xaRes, e);
return false;
}
return true;
try {
xaRes.end(getXid(), flag);
} catch (XAException e) {
- log.error("Cannot end " + xaRes, e);
+ error("Cannot end " + xaRes, e);
}
xaResources.clear();
}
+ protected void error(Object obj, Exception e) {
+ System.err.println(obj);
+ e.printStackTrace();
+ }
+
@Override
public synchronized int getStatus() throws SystemException {
return status;
+++ /dev/null
-package org.argeo.transaction.simple;
-
-public class SimpleTransactionException extends RuntimeException {
- private static final long serialVersionUID = -7465792070797750212L;
-
- public SimpleTransactionException(String message) {
- super(message);
- }
-
- public SimpleTransactionException(String message, Throwable cause) {
- super(message, cause);
- }
-
-}
import javax.transaction.UserTransaction;
import javax.transaction.xa.Xid;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
+/**
+ * Simple implementation of an XA {@link TransactionManager} and
+ * {@link UserTransaction}.
+ */
public class SimpleTransactionManager implements TransactionManager, UserTransaction {
- private final static Log log = LogFactory.getLog(SimpleTransactionManager.class);
-
private ThreadLocal<SimpleTransaction> current = new ThreadLocal<SimpleTransaction>();
private Map<Xid, SimpleTransaction> knownTransactions = Collections
SimpleTransaction transaction = new SimpleTransaction(this);
knownTransactions.put(transaction.getXid(), transaction);
current.set(transaction);
- if (log.isTraceEnabled())
- log.trace("STARTED " + transaction.getXid());
}
@Override
return null;
return getCurrent().getXid();
} catch (SystemException e) {
- throw new SimpleTransactionException("Cannot get transaction key", e);
+ throw new IllegalStateException("Cannot get transaction key", e);
}
}
try {
return getStatus();
} catch (SystemException e) {
- throw new SimpleTransactionException("Cannot get status", e);
+ throw new IllegalStateException("Cannot get status", e);
}
}
try {
return getStatus() == Status.STATUS_MARKED_ROLLBACK;
} catch (SystemException e) {
- throw new SimpleTransactionException("Cannot get status", e);
+ throw new IllegalStateException("Cannot get status", e);
}
}
try {
getCurrent().setRollbackOnly();
} catch (Exception e) {
- throw new SimpleTransactionException("Cannot set rollback only", e);
+ throw new IllegalStateException("Cannot set rollback only", e);
}
}
source.. = src/,\
ext/test/
output.. = bin/
-additional.bundles = org.junit,\
- org.slf4j.commons.logging,\
- org.apache.log4j
--- /dev/null
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed 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.argeo.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.List;
+
+/** Tests that {@link CsvParser} can deal properly with encodings. */
+public class CsvParserEncodingTest {
+
+ private String iso = "ISO-8859-1";
+ private String utf8 = "UTF-8";
+
+ public void testParse() throws Exception {
+
+ String xml = new String("áéÃóúñ,éééé");
+ byte[] utfBytes = xml.getBytes(utf8);
+ byte[] isoBytes = xml.getBytes(iso);
+
+ InputStream inUtf = new ByteArrayInputStream(utfBytes);
+ InputStream inIso = new ByteArrayInputStream(isoBytes);
+
+ CsvParser csvParser = new CsvParser() {
+ protected void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
+ assert header.size() == tokens.size();
+ assert 2 == tokens.size();
+ assert "áéÃóúñ".equals(tokens.get(0));
+ assert "éééé".equals(tokens.get(1));
+ }
+ };
+
+ csvParser.parse(inUtf, utf8);
+ inUtf.close();
+ csvParser.parse(inIso, iso);
+ inIso.close();
+ }
+}
+++ /dev/null
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed 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.argeo.util;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-public class CsvParserEncodingTestCase extends TestCase {
-
- private String iso = "ISO-8859-1";
- private String utf8 = "UTF-8";
-
- public void testParse() throws Exception {
-
- String xml = new String("áéÃóúñ,éééé");
- byte[] utfBytes = xml.getBytes(utf8);
- byte[] isoBytes = xml.getBytes(iso);
-
- InputStream inUtf = new ByteArrayInputStream(utfBytes);
- InputStream inIso = new ByteArrayInputStream(isoBytes);
-
- CsvParser csvParser = new CsvParser() {
- protected void processLine(Integer lineNumber, List<String> header,
- List<String> tokens) {
- assertEquals(header.size(), tokens.size());
- assertEquals(2, tokens.size());
- assertEquals("áéÃóúñ", tokens.get(0));
- assertEquals("éééé", tokens.get(1));
- }
- };
-
- csvParser.parse(inUtf, utf8);
- inUtf.close();
- csvParser.parse(inIso, iso);
- inIso.close();
- }
-}
import java.util.HashMap;
import java.util.Map;
-import junit.framework.TestCase;
-
-public class CsvParserParseFileTest extends TestCase {
+/** Test that {@link CsvParser} can properly parse a CSV file. */
+public class CsvParserParseFileTest {
public void testParse() throws Exception {
final Map<Integer, Map<String, String>> lines = new HashMap<Integer, Map<String, String>>();
- InputStream in = getClass().getResourceAsStream(
- "/org/argeo/util/ReferenceFile.csv");
+ InputStream in = getClass().getResourceAsStream("/org/argeo/util/ReferenceFile.csv");
CsvParserWithLinesAsMap parser = new CsvParserWithLinesAsMap() {
- protected void processLine(Integer lineNumber,
- Map<String, String> line) {
+ protected void processLine(Integer lineNumber, Map<String, String> line) {
lines.put(lineNumber, line);
}
};
parser.parse(in);
in.close();
- assertEquals(5, lines.size());
+ assert 5 == lines.size();
}
}
--- /dev/null
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed 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.argeo.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.List;
+
+/** {@link CsvParser} tests. */
+public class CsvParserTest {
+ public void testParse() throws Exception {
+ String toParse = "Header1,\"Header\n2\",Header3,\"Header4\"\n" + "Col1,\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n"
+ + "Col1,\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n" + "Col1,\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n";
+
+ InputStream in = new ByteArrayInputStream(toParse.getBytes());
+
+ CsvParser csvParser = new CsvParser() {
+ protected void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
+ assert header.size() == tokens.size();
+ assert 4 == tokens.size();
+ assert "Col1".equals(tokens.get(0));
+ assert "Col\n2".equals(tokens.get(1));
+ assert "Col3".equals(tokens.get(2));
+ assert "\"Col4\"".equals(tokens.get(3));
+ }
+ };
+
+ csvParser.parse(in);
+ in.close();
+ }
+
+}
+++ /dev/null
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed 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.argeo.util;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-public class CsvParserTestCase extends TestCase {
- public void testParse() throws Exception {
- String toParse = "Header1,\"Header\n2\",Header3,\"Header4\"\n"
- + "Col1,\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n"
- + "Col1,\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n"
- + "Col1,\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n";
-
- InputStream in = new ByteArrayInputStream(toParse.getBytes());
-
- CsvParser csvParser = new CsvParser() {
- protected void processLine(Integer lineNumber, List<String> header,
- List<String> tokens) {
- assertEquals(header.size(), tokens.size());
- assertEquals(4, tokens.size());
- assertEquals("Col1", tokens.get(0));
- assertEquals("Col\n2", tokens.get(1));
- assertEquals("Col3", tokens.get(2));
- assertEquals("\"Col4\"", tokens.get(3));
- }
- };
-
- csvParser.parse(in);
- in.close();
- }
-
-}
import java.util.List;
import java.util.Map;
-import junit.framework.TestCase;
-
-public class CsvParserWithQuotedSeparatorTest extends TestCase {
+/** Test that {@link CsvParser} deals properly with "" quotes. */
+public class CsvParserWithQuotedSeparatorTest {
public void testSimpleParse() throws Exception {
String toParse = "Header1,\"Header2\",Header3,\"Header4\"\n"
+ "\"Col1, Col2\",\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n";
InputStream in = new ByteArrayInputStream(toParse.getBytes());
CsvParser csvParser = new CsvParser() {
- protected void processLine(Integer lineNumber, List<String> header,
- List<String> tokens) {
- assertEquals(header.size(), tokens.size());
- assertEquals(4, tokens.size());
- assertEquals("Col1, Col2", tokens.get(0));
+ protected void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
+ assert header.size() == tokens.size();
+ assert 4 == tokens.size();
+ assert "Col1, Col2".equals(tokens.get(0));
}
};
// System.out.println(toParse);
public void testParseFile() throws Exception {
final Map<Integer, Map<String, String>> lines = new HashMap<Integer, Map<String, String>>();
- InputStream in = getClass().getResourceAsStream(
- "/org/argeo/util/ReferenceFile.csv");
+ InputStream in = getClass().getResourceAsStream("/org/argeo/util/ReferenceFile.csv");
CsvParserWithLinesAsMap parser = new CsvParserWithLinesAsMap() {
- protected void processLine(Integer lineNumber,
- Map<String, String> line) {
+ protected void processLine(Integer lineNumber, Map<String, String> line) {
// System.out.println("processing line #" + lineNumber);
lines.put(lineNumber, line);
}
in.close();
Map<String, String> line = lines.get(2);
- assertEquals(",,,,", line.get("Coma testing"));
+ assert ",,,,".equals(line.get("Coma testing"));
line = lines.get(3);
- assertEquals(",, ,,", line.get("Coma testing"));
+ assert ",, ,,".equals(line.get("Coma testing"));
line = lines.get(4);
- assertEquals("module1, module2", line.get("Coma testing"));
+ assert "module1, module2".equals(line.get("Coma testing"));
line = lines.get(5);
- assertEquals("module1,module2", line.get("Coma testing"));
+ assert "module1,module2".equals(line.get("Coma testing"));
line = lines.get(6);
- assertEquals(",module1,module2, \nmodule3, module4",
- line.get("Coma testing"));
- assertEquals(5, lines.size());
+ assert ",module1,module2, \nmodule3, module4".equals(line.get("Coma testing"));
+ assert 5 == lines.size();
}
}
--- /dev/null
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed 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.argeo.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** {@link CsvWriter} tests. */
+public class CsvWriterTest {
+ public void testWrite() throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ final CsvWriter csvWriter = new CsvWriter(out);
+
+ String[] header = { "Header1", "Header 2", "Header,3", "Header\n4", "Header\"5\"" };
+ String[] line1 = { "Value1", "Value 2", "Value,3", "Value\n4", "Value\"5\"" };
+ csvWriter.writeLine(Arrays.asList(header));
+ csvWriter.writeLine(Arrays.asList(line1));
+
+ String reference = "Header1,Header 2,\"Header,3\",\"Header\n4\",\"Header\"\"5\"\"\"\n"
+ + "Value1,Value 2,\"Value,3\",\"Value\n4\",\"Value\"\"5\"\"\"\n";
+ String written = new String(out.toByteArray());
+ assert reference.equals(written);
+ out.close();
+ System.out.println(written);
+
+ final List<String> allTokens = new ArrayList<String>();
+ CsvParser csvParser = new CsvParser() {
+ protected void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
+ if (lineNumber == 2)
+ allTokens.addAll(header);
+ allTokens.addAll(tokens);
+ }
+ };
+ ByteArrayInputStream in = new ByteArrayInputStream(written.getBytes());
+ csvParser.parse(in);
+ in.close();
+ List<String> allTokensRef = new ArrayList<String>();
+ allTokensRef.addAll(Arrays.asList(header));
+ allTokensRef.addAll(Arrays.asList(line1));
+
+ assert allTokensRef.size() == allTokens.size();
+ for (int i = 0; i < allTokensRef.size(); i++)
+ assert allTokensRef.get(i).equals(allTokens.get(i));
+ }
+
+}
+++ /dev/null
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed 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.argeo.util;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-public class CsvWriterTestCase extends TestCase {
- public void testWrite() throws Exception {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- final CsvWriter csvWriter = new CsvWriter(out);
-
- String[] header = { "Header1", "Header 2", "Header,3", "Header\n4",
- "Header\"5\"" };
- String[] line1 = { "Value1", "Value 2", "Value,3", "Value\n4",
- "Value\"5\"" };
- csvWriter.writeLine(Arrays.asList(header));
- csvWriter.writeLine(Arrays.asList(line1));
-
- String reference = "Header1,Header 2,\"Header,3\",\"Header\n4\",\"Header\"\"5\"\"\"\n"
- + "Value1,Value 2,\"Value,3\",\"Value\n4\",\"Value\"\"5\"\"\"\n";
- String written = new String(out.toByteArray());
- assertEquals(reference, written);
- out.close();
- System.out.println(written);
-
- final List<String> allTokens = new ArrayList<String>();
- CsvParser csvParser = new CsvParser() {
- protected void processLine(Integer lineNumber, List<String> header,
- List<String> tokens) {
- if (lineNumber == 2)
- allTokens.addAll(header);
- allTokens.addAll(tokens);
- }
- };
- ByteArrayInputStream in = new ByteArrayInputStream(written.getBytes());
- csvParser.parse(in);
- in.close();
- List<String> allTokensRef = new ArrayList<String>();
- allTokensRef.addAll(Arrays.asList(header));
- allTokensRef.addAll(Arrays.asList(line1));
-
- assertEquals(allTokensRef.size(), allTokens.size());
- for (int i = 0; i < allTokensRef.size(); i++)
- assertEquals(allTokensRef.get(i), allTokens.get(i));
- }
-
-}
+++ /dev/null
-package org.argeo.util.test;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-
-/** A generic tester based on Java assertions and functional programming. */
-public class Test {
- private Map<String, TestStatus> results = Collections.synchronizedSortedMap(new TreeMap<>());
-
- protected void execute(String className) throws Throwable {
- ClassLoader classLoader = Test.class.getClassLoader();
- Class<?> clss = classLoader.loadClass(className);
- boolean assertionsEnabled = clss.desiredAssertionStatus();
- if (!assertionsEnabled)
- throw new IllegalStateException("Test runner " + getClass().getName()
- + " requires Java assertions to be enabled. Call the JVM with the -ea argument.");
- Object obj = clss.getDeclaredConstructor().newInstance();
- List<Method> methods = findMethods(clss);
- if (methods.size() == 0)
- throw new IllegalArgumentException("No test method found in " + clss);
- // TODO make order more predictable?
- for (Method method : methods) {
- String uid = method.getDeclaringClass().getName() + "#" + method.getName();
- TestStatus testStatus = new TestStatus(uid);
- try {
- method.invoke(obj);
- testStatus.setPassed();
- } catch (Exception e) {
- testStatus.setFailed(e);
- } finally {
- results.put(uid, testStatus);
- }
- }
- }
-
- protected List<Method> findMethods(Class<?> clss) {
- List<Method> methods = new ArrayList<Method>();
-// Method call = getMethod(clss, "call");
-// if (call != null)
-// methods.add(call);
-//
- for (Method method : clss.getMethods()) {
- if (method.getName().startsWith("test")) {
- methods.add(method);
- }
- }
- return methods;
- }
-
- protected Method getMethod(Class<?> clss, String name, Class<?>... parameterTypes) {
- try {
- return clss.getMethod(name, parameterTypes);
- } catch (NoSuchMethodException e) {
- return null;
- } catch (SecurityException e) {
- throw new IllegalStateException(e);
- }
- }
-
- public static void main(String[] args) {
- // deal with arguments
- String className;
- if (args.length < 1) {
- System.err.println(usage());
- System.exit(1);
- throw new IllegalArgumentException();
- } else {
- className = args[0];
- }
-
- Test test = new Test();
- try {
- test.execute(className);
- } catch (Throwable e) {
- e.printStackTrace();
- }
-
- Map<String, TestStatus> r = test.results;
- for (String uid : r.keySet()) {
- TestStatus testStatus = r.get(uid);
- System.out.println(testStatus);
- }
- }
-
- public static String usage() {
- return "java " + Test.class.getName() + " [test class name]";
-
- }
-}
+++ /dev/null
-package org.argeo.util.test;
-
-import java.io.Serializable;
-
-/** The status of a test. */
-public class TestStatus implements Serializable {
- private static final long serialVersionUID = 6272975746885487000L;
-
- private Boolean passed = null;
- private final String uid;
- private Throwable throwable = null;
-
- public TestStatus(String uid) {
- this.uid = uid;
- }
-
- /** For cloning. */
- public TestStatus(String uid, Boolean passed, Throwable throwable) {
- this(uid);
- this.passed = passed;
- this.throwable = throwable;
- }
-
- public synchronized Boolean isRunning() {
- return passed == null;
- }
-
- public synchronized Boolean isPassed() {
- assert passed != null;
- return passed;
- }
-
- public synchronized Boolean isFailed() {
- assert passed != null;
- return !passed;
- }
-
- public synchronized void setPassed() {
- setStatus(true);
- }
-
- public synchronized void setFailed() {
- setStatus(false);
- }
-
- public synchronized void setFailed(Throwable throwable) {
- setStatus(false);
- setThrowable(throwable);
- }
-
- protected void setStatus(Boolean passed) {
- if (this.passed != null)
- throw new IllegalStateException("Passed status of test " + uid + " is already set (to " + passed + ")");
- this.passed = passed;
- }
-
- protected void setThrowable(Throwable throwable) {
- if (this.throwable != null)
- throw new IllegalStateException("Throwable of test " + uid + " is already set (to " + passed + ")");
- this.throwable = throwable;
- }
-
- public String getUid() {
- return uid;
- }
-
- public Throwable getThrowable() {
- return throwable;
- }
-
- @Override
- protected Object clone() throws CloneNotSupportedException {
- // TODO Auto-generated method stub
- return super.clone();
- }
-
- @Override
- public boolean equals(Object o) {
- if (o instanceof TestStatus) {
- TestStatus other = (TestStatus) o;
- // we don't check consistency for performance purposes
- // this equals() is supposed to be used in collections or for transfer
- return other.uid.equals(uid);
- }
- return false;
- }
-
- @Override
- public int hashCode() {
- return uid.hashCode();
- }
-
- @Override
- public String toString() {
- return uid + "\t" + (passed ? "passed" : "failed");
- }
-
-}
--- /dev/null
+package org.argeo.util.test;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/** A generic tester based on Java assertions and functional programming. */
+public class Tester {
+ private Map<String, TesterStatus> results = Collections.synchronizedSortedMap(new TreeMap<>());
+
+ private ClassLoader classLoader;
+
+ /** Use {@link Thread#getContextClassLoader()} by default. */
+ public Tester() {
+ this(Thread.currentThread().getContextClassLoader());
+ }
+
+ public Tester(ClassLoader classLoader) {
+ this.classLoader = classLoader;
+ }
+
+ public void execute(String className) {
+ Class<?> clss;
+ try {
+ clss = classLoader.loadClass(className);
+ boolean assertionsEnabled = clss.desiredAssertionStatus();
+ if (!assertionsEnabled)
+ throw new IllegalStateException("Test runner " + getClass().getName()
+ + " requires Java assertions to be enabled. Call the JVM with the -ea argument.");
+ } catch (Exception e1) {
+ throw new IllegalArgumentException("Cannot initalise test for " + className, e1);
+
+ }
+ List<Method> methods = findMethods(clss);
+ if (methods.size() == 0)
+ throw new IllegalArgumentException("No test method found in " + clss);
+ // TODO make order more predictable?
+ for (Method method : methods) {
+ String uid = method.getDeclaringClass().getName() + "#" + method.getName();
+ TesterStatus testStatus = new TesterStatus(uid);
+ Object obj = null;
+ try {
+ beforeTest(uid, method);
+ obj = clss.getDeclaredConstructor().newInstance();
+ method.invoke(obj);
+ testStatus.setPassed();
+ afterTestPassed(uid, method, obj);
+ } catch (Exception e) {
+ testStatus.setFailed(e);
+ afterTestFailed(uid, method, obj, e);
+ } finally {
+ results.put(uid, testStatus);
+ }
+ }
+ }
+
+ protected void beforeTest(String uid, Method method) {
+ // System.out.println(uid + ": STARTING");
+ }
+
+ protected void afterTestPassed(String uid, Method method, Object obj) {
+ System.out.println(uid + ": PASSED");
+ }
+
+ protected void afterTestFailed(String uid, Method method, Object obj, Throwable e) {
+ System.out.println(uid + ": FAILED");
+ e.printStackTrace();
+ }
+
+ protected List<Method> findMethods(Class<?> clss) {
+ List<Method> methods = new ArrayList<Method>();
+// Method call = getMethod(clss, "call");
+// if (call != null)
+// methods.add(call);
+//
+ for (Method method : clss.getMethods()) {
+ if (method.getName().startsWith("test")) {
+ methods.add(method);
+ }
+ }
+ return methods;
+ }
+
+ protected Method getMethod(Class<?> clss, String name, Class<?>... parameterTypes) {
+ try {
+ return clss.getMethod(name, parameterTypes);
+ } catch (NoSuchMethodException e) {
+ return null;
+ } catch (SecurityException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ public static void main(String[] args) {
+ // deal with arguments
+ String className;
+ if (args.length < 1) {
+ System.err.println(usage());
+ System.exit(1);
+ throw new IllegalArgumentException();
+ } else {
+ className = args[0];
+ }
+
+ Tester test = new Tester();
+ try {
+ test.execute(className);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+
+ Map<String, TesterStatus> r = test.results;
+ for (String uid : r.keySet()) {
+ TesterStatus testStatus = r.get(uid);
+ System.out.println(testStatus);
+ }
+ }
+
+ public static String usage() {
+ return "java " + Tester.class.getName() + " [test class name]";
+
+ }
+}
--- /dev/null
+package org.argeo.util.test;
+
+import java.io.Serializable;
+
+/** The status of a test. */
+public class TesterStatus implements Serializable {
+ private static final long serialVersionUID = 6272975746885487000L;
+
+ private Boolean passed = null;
+ private final String uid;
+ private Throwable throwable = null;
+
+ public TesterStatus(String uid) {
+ this.uid = uid;
+ }
+
+ /** For cloning. */
+ public TesterStatus(String uid, Boolean passed, Throwable throwable) {
+ this(uid);
+ this.passed = passed;
+ this.throwable = throwable;
+ }
+
+ public synchronized Boolean isRunning() {
+ return passed == null;
+ }
+
+ public synchronized Boolean isPassed() {
+ assert passed != null;
+ return passed;
+ }
+
+ public synchronized Boolean isFailed() {
+ assert passed != null;
+ return !passed;
+ }
+
+ public synchronized void setPassed() {
+ setStatus(true);
+ }
+
+ public synchronized void setFailed() {
+ setStatus(false);
+ }
+
+ public synchronized void setFailed(Throwable throwable) {
+ setStatus(false);
+ setThrowable(throwable);
+ }
+
+ protected void setStatus(Boolean passed) {
+ if (this.passed != null)
+ throw new IllegalStateException("Passed status of test " + uid + " is already set (to " + passed + ")");
+ this.passed = passed;
+ }
+
+ protected void setThrowable(Throwable throwable) {
+ if (this.throwable != null)
+ throw new IllegalStateException("Throwable of test " + uid + " is already set (to " + passed + ")");
+ this.throwable = throwable;
+ }
+
+ public String getUid() {
+ return uid;
+ }
+
+ public Throwable getThrowable() {
+ return throwable;
+ }
+
+ @Override
+ protected Object clone() throws CloneNotSupportedException {
+ // TODO Auto-generated method stub
+ return super.clone();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o instanceof TesterStatus) {
+ TesterStatus other = (TesterStatus) o;
+ // we don't check consistency for performance purposes
+ // this equals() is supposed to be used in collections or for transfer
+ return other.uid.equals(uid);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return uid.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return uid + "\t" + (passed ? "passed" : "failed");
+ }
+
+}