From 3419d5b06747494144e592a26f072162fcacea70 Mon Sep 17 00:00:00 2001 From: Brian Remmington Date: Tue, 16 Nov 2010 08:00:52 +0000 Subject: [PATCH] Merged /BRANCHES/V3.4 to HEAD: 23464: Reinstate transfer test of greek paths. - Manifest was being written by FileWriter() which would use default character set. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@23662 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../repo/transfer/TransferServiceImpl2.java | 11 +- .../transfer/TransferServiceImplTest.java | 200 ++++++++++++++++++ .../manifest/ManifestIntegrationTest.java | 11 +- .../manifest/TransferManifestTest.java | 12 +- 4 files changed, 222 insertions(+), 12 deletions(-) diff --git a/source/java/org/alfresco/repo/transfer/TransferServiceImpl2.java b/source/java/org/alfresco/repo/transfer/TransferServiceImpl2.java index 903ad07e2b..c36979c49b 100644 --- a/source/java/org/alfresco/repo/transfer/TransferServiceImpl2.java +++ b/source/java/org/alfresco/repo/transfer/TransferServiceImpl2.java @@ -19,13 +19,16 @@ package org.alfresco.repo.transfer; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.Serializable; +import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collection; @@ -915,7 +918,7 @@ public class TransferServiceImpl2 implements TransferService2 // where to put snapshot ? File tempDir = TempFileProvider.getLongLifeTempDir(FILE_DIRECTORY); File snapshotFile = TempFileProvider.createTempFile("TRX-SNAP", FILE_SUFFIX, tempDir); - FileWriter snapshotWriter = new FileWriter(snapshotFile); + Writer snapshotWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(snapshotFile), "UTF-8")); // Write the manifest file TransferManifestWriter formatter = new XMLTransferManifestWriter(); @@ -1289,7 +1292,7 @@ public class TransferServiceImpl2 implements TransferService2 */ private static void outputFile(File file) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(file)); + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); String s = reader.readLine(); while(s != null) { diff --git a/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java b/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java index b1e79c4e1d..941856857d 100644 --- a/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java +++ b/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java @@ -7632,6 +7632,206 @@ public class TransferServiceImplTest extends BaseAlfrescoSpringTest } // test replace node + /** + * Test the transfer method with regard to obscure paths. + * + * This is a unit test so it does some shenanigans to send to he same instance of alfresco. + */ + public void testHorriblePaths() throws Exception + { + setDefaultRollback(false); + + final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper(); + + final String CONTENT_TITLE = "ContentTitle"; + final String CONTENT_TITLE_UPDATED = "ContentTitleUpdated"; + final String CONTENT_NAME = "Demo Node 1"; + final Locale CONTENT_LOCALE = Locale.GERMAN; + final String CONTENT_STRING = "The quick brown fox"; + final Setnodes = new HashSet(); + + final String targetName = "testManyNodes"; + + class TestContext + { + TransferTarget transferMe; + NodeRef nodeA = null; + NodeRef childNode = null; + }; + + /** + * Unit test kludge to transfer from guest home to company home + */ + final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome(); + + TransferTarget transferMe; + + final QName[] difficult = { QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"testNodeB"), + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"with.dot"), + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"8332"), + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"&#~@"), + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"_-+ )"), + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"with space"), + // A, e with accent + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "\u0041\u00E9"), + // Greek Alpha, Omega + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "\u0391\u03A9") + }; + + RetryingTransactionCallback setupCB = new RetryingTransactionCallback() + { + @Override + public TestContext execute() throws Throwable + { + TestContext ctx = new TestContext(); + + /** + * Get guest home + */ + String guestHomeQuery = "/app:company_home/app:guest_home"; + ResultSet guestHomeResult = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_XPATH, guestHomeQuery); + assertEquals("", 1, guestHomeResult.length()); + NodeRef guestHome = guestHomeResult.getNodeRef(0); + + /** + * Create a test node that we will read and write + */ + String guid = GUID.generate(); + + /** + * Create a tree with "difficult" characters in the path + * ManyNodesRoot + * A (Folder) + * ... childNode + */ + ChildAssociationRef child; + + child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(guid), ContentModel.TYPE_FOLDER); + NodeRef testRootNode = child.getChildRef(); + nodeService.setProperty(testRootNode , ContentModel.PROP_TITLE, guid); + nodeService.setProperty(testRootNode , ContentModel.PROP_NAME, guid); + nodes.add(testRootNode); + + child = nodeService.createNode(testRootNode, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"testNodeA"), ContentModel.TYPE_FOLDER); + ctx.nodeA = child.getChildRef(); + nodeService.setProperty(ctx.nodeA , ContentModel.PROP_TITLE, "TestNodeA"); + nodeService.setProperty(ctx.nodeA , ContentModel.PROP_NAME, "TestNodeA"); + nodes.add(ctx.nodeA); + + NodeRef current = ctx.nodeA; + + for(QName name : difficult) + { + child = nodeService.createNode(current, ContentModel.ASSOC_CONTAINS, name, ContentModel.TYPE_FOLDER); + current = child.getChildRef(); + nodeService.setProperty(current , ContentModel.PROP_TITLE, name); + nodeService.setProperty(current , ContentModel.PROP_NAME, "testName"); + nodes.add(current); + } + + child = nodeService.createNode(current, ContentModel.ASSOC_CONTAINS, QName.createQName("testNodeAC"), ContentModel.TYPE_CONTENT); + ctx.childNode = child.getChildRef(); + nodeService.setProperty( ctx.childNode , ContentModel.PROP_TITLE, CONTENT_TITLE + "AC"); + nodeService.setProperty( ctx.childNode , ContentModel.PROP_NAME, "DemoNodeAC"); + + { + ContentWriter writer = contentService.getWriter( ctx.childNode , ContentModel.PROP_CONTENT, true); + writer.setLocale(CONTENT_LOCALE); + writer.putContent(CONTENT_STRING); + nodes.add( ctx.childNode); + } + + + /** + * Now go ahead and create our first transfer target + */ + if(!transferService.targetExists(targetName)) + { + ctx.transferMe = createTransferTarget(targetName); + } + else + { + ctx.transferMe = transferService.getTransferTarget(targetName); + } + + return ctx; + } + }; + + final TestContext testContext = tran.doInTransaction(setupCB); + + RetryingTransactionCallback transferCB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + TransferDefinition definition = new TransferDefinition(); + definition.setNodes(nodes); + transferService.transfer(targetName, definition); + + return null; + } + }; + + tran.doInTransaction(transferCB); + + RetryingTransactionCallback check1CB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + NodeRef destChildNode = testNodeFactory.getMappedNodeRef(testContext.childNode); + assertTrue("dest node does not exist", nodeService.exists(destChildNode)); + + /** + * Step through source and dest trees on nodes comparing the path as we go. + */ + Path srcPath = nodeService.getPath(testContext.childNode); + Path destPath = nodeService.getPath(destChildNode); + + int srcSize = srcPath.size(); + int destSize = destPath.size(); + + Path dest = destPath.subPath(2, destSize-1); + Path src = srcPath.subPath(3, srcSize-1); + +// System.out.println("src=" + src); +// System.out.println("dest=" + dest); + assertEquals("paths are different", src.toString(), dest.toString()); + + return null; + } + }; + + tran.doInTransaction(check1CB); + + RetryingTransactionCallback updateCB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + + return null; + } + }; + + tran.doInTransaction(updateCB); + + tran.doInTransaction(transferCB); + + RetryingTransactionCallback check2CB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + assertTrue("dest node does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testContext.childNode))); + + return null; + } + }; + tran.doInTransaction(check2CB); + + } // horrible paths private void createUser(String userName, String password) { diff --git a/source/java/org/alfresco/repo/transfer/manifest/ManifestIntegrationTest.java b/source/java/org/alfresco/repo/transfer/manifest/ManifestIntegrationTest.java index aeaa7a7ce3..82604d0fe9 100644 --- a/source/java/org/alfresco/repo/transfer/manifest/ManifestIntegrationTest.java +++ b/source/java/org/alfresco/repo/transfer/manifest/ManifestIntegrationTest.java @@ -20,8 +20,11 @@ package org.alfresco.repo.transfer.manifest; import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -119,7 +122,7 @@ public class ManifestIntegrationTest extends BaseAlfrescoSpringTest nodeService.setProperty(childNodeRef, ContentModel.PROP_NAME, CONTENT_NAME); snapshotFile = TempFileProvider.createTempFile("xxx", ".xml"); - FileWriter snapshotWriter = new FileWriter(snapshotFile); + Writer snapshotWriter = new OutputStreamWriter(new FileOutputStream(snapshotFile), "UTF-8"); Set nodes = new HashSet(); @@ -252,7 +255,7 @@ public class ManifestIntegrationTest extends BaseAlfrescoSpringTest */ private static void outputFile(File file) throws Exception { - BufferedReader reader = new BufferedReader(new FileReader(file)); + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); String s = reader.readLine(); while(s != null) { diff --git a/source/java/org/alfresco/repo/transfer/manifest/TransferManifestTest.java b/source/java/org/alfresco/repo/transfer/manifest/TransferManifestTest.java index c6bafab2dd..d8ed525f02 100644 --- a/source/java/org/alfresco/repo/transfer/manifest/TransferManifestTest.java +++ b/source/java/org/alfresco/repo/transfer/manifest/TransferManifestTest.java @@ -19,10 +19,14 @@ package org.alfresco.repo.transfer.manifest; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.Serializable; +import java.io.Writer; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -68,7 +72,7 @@ public class TransferManifestTest extends TestCase // where to put snapshot ? File snapshotFile = TempFileProvider.createTempFile(prefix, suffix); - FileWriter snapshotWriter = new FileWriter(snapshotFile); + Writer snapshotWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(snapshotFile), "UTF-8")); // Write the manifest file TransferManifestWriter formatter = new XMLTransferManifestWriter(); @@ -245,7 +249,7 @@ public class TransferManifestTest extends TestCase snapshotWriter.close(); // - BufferedReader reader = new BufferedReader(new FileReader(snapshotFile)); + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(snapshotFile), "UTF-8")); String s = reader.readLine(); while(s != null) {