Revision 622

tmp/org.txm.jodconverter.core/src/test/resources/documents/test.csv (revision 622)
1
"Test spreadsheet"
tmp/org.txm.jodconverter.core/src/test/resources/documents/test.rtf (revision 622)
1
{\rtf1\ansi\deff0\adeflang1025
2
{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\froman\fprq2\fcharset0 Times New Roman;}{\f2\fswiss\fprq2\fcharset0 Arial;}{\f3\fnil\fprq2\fcharset0 DejaVu Sans;}}
3
{\colortbl;\red0\green0\blue0;\red128\green128\blue128;}
4
{\stylesheet{\s1\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang255\ltrch\dbch\af3\langfe255\hich\f0\fs24\lang2057\loch\f0\fs24\lang2057\snext1 Normal;}
5
{\s2\sb240\sa120\keepn\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\afs28\lang255\ltrch\dbch\langfe255\hich\f2\fs28\lang2057\loch\f2\fs28\lang2057\sbasedon1\snext3 Heading;}
6
{\s3\sa120\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang255\ltrch\dbch\af3\langfe255\hich\f0\fs24\lang2057\loch\f0\fs24\lang2057\sbasedon1\snext3 Body Text;}
7
{\s4\sa120\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang255\ltrch\dbch\af3\langfe255\hich\f0\fs24\lang2057\loch\f0\fs24\lang2057\sbasedon3\snext4 List;}
8
{\s5\sb120\sa120\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang255\ai\ltrch\dbch\af3\langfe255\hich\f0\fs24\lang2057\i\loch\f0\fs24\lang2057\i\sbasedon1\snext5 caption;}
9
{\s6\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang255\ltrch\dbch\af3\langfe255\hich\f0\fs24\lang2057\loch\f0\fs24\lang2057\sbasedon1\snext6 Index;}
10
}
11
{\info{\author Mirko Nasato}{\creatim\yr2008\mo7\dy12\hr15\min3}{\author Mirko Nasato}{\revtim\yr2008\mo7\dy12\hr15\min3}{\printim\yr0\mo0\dy0\hr0\min0}{\comment StarWriter}{\vern6800}}\deftab709
12
{\*\pgdsctbl
13
{\pgdsc0\pgdscuse195\pgwsxn11905\pghsxn16837\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\pgdscnxt0 Standard;}}
14
\paperh16837\paperw11905\margl1134\margr1134\margt1134\margb1134\sectd\sbknone\pgwsxn11905\pghsxn16837\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\ftnbj\ftnstart1\ftnrstcont\ftnnar\aenddoc\aftnrstcont\aftnstart1\aftnnrlc
15
\pard\plain \ltrpar\s1\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang255\ltrch\dbch\af3\langfe255\hich\f0\fs24\lang2057\loch\f0\fs24\lang2057 {\rtlch \ltrch\loch\f0\fs24\lang2057\i0\b0 Test document}
16
\par }
tmp/org.txm.jodconverter.core/src/test/resources/documents/test.txt (revision 622)
1
Test document
0 2

  
tmp/org.txm.jodconverter.core/src/test/resources/documents/test.html (revision 622)
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
<HTML>
3
<HEAD>
4
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
5
	<TITLE></TITLE>
6
	<META NAME="GENERATOR" CONTENT="OpenOffice.org 2.4  (Linux)">
7
	<META NAME="AUTHOR" CONTENT="Mirko Nasato">
8
	<META NAME="CREATED" CONTENT="20080712;15031700">
9
	<META NAME="CHANGEDBY" CONTENT="Mirko Nasato">
10
	<META NAME="CHANGED" CONTENT="20080712;15034700">
11
	<STYLE TYPE="text/css">
12
	<!--
13
		@page { margin: 2cm }
14
		P { margin-bottom: 0.21cm }
15
		P.western { so-language: en-GB }
16
	-->
17
	</STYLE>
18
</HEAD>
19
<BODY LANG="en-GB" DIR="LTR">
20
<P CLASS="western" STYLE="margin-bottom: 0cm">Test document</P>
21
</BODY>
22
</HTML>
0 23

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/ReflectionUtils.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter;
14

  
15
import java.lang.reflect.Field;
16

  
17
public class ReflectionUtils {
18

  
19
    private ReflectionUtils() {
20
        throw new AssertionError("utility class must not be instantiated");
21
    }
22

  
23
    public static Object getPrivateField(Object instance, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
24
        return getPrivateField(instance.getClass(), instance, fieldName);
25
    }
26

  
27
    public static Object getPrivateField(Class<?> type, Object instance, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
28
        Field field = type.getDeclaredField(fieldName);
29
        field.setAccessible(true);
30
        return field.get(instance);
31
    }
32

  
33
}
0 34

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/office/ExternalOfficeManagerTest.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter.office;
14

  
15
import static org.testng.Assert.assertTrue;
16

  
17
import java.io.File;
18

  
19
import org.artofsolving.jodconverter.ReflectionUtils;
20
import org.artofsolving.jodconverter.process.PureJavaProcessManager;
21
import org.testng.annotations.Test;
22

  
23
@Test(groups="integration")
24
public class ExternalOfficeManagerTest {
25

  
26
    public void executeTask() throws Exception {
27
        UnoUrl unoUrl = UnoUrl.socket(2002);
28
        OfficeProcess officeProcess = new OfficeProcess(OfficeUtils.getDefaultOfficeHome(), unoUrl,
29
            null, null, new File(System.getProperty("java.io.tmpdir")), new PureJavaProcessManager());
30
        officeProcess.start();
31
        Thread.sleep(2000);
32
        Integer exitCode = officeProcess.getExitCode();
33
        if (exitCode != null && exitCode.equals(Integer.valueOf(81))) {
34
            officeProcess.start(true);
35
            Thread.sleep(2000);
36
        }
37
        
38
        ExternalOfficeManager manager = new ExternalOfficeManager(unoUrl, true);
39
        manager.start();
40
        
41
        MockOfficeTask task = new MockOfficeTask();
42
        manager.execute(task);
43
        assertTrue(task.isCompleted());
44
        
45
        manager.stop();
46
        //TODO replace when OfficeProcess has a forciblyTerminate()
47
        Process process = (Process) ReflectionUtils.getPrivateField(officeProcess, "process");
48
        process.destroy();
49
    }
50

  
51
    //TODO test auto-reconnection
52

  
53
}
0 54

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/office/PooledOfficeManagerTest.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter.office;
14

  
15
import static org.testng.Assert.assertEquals;
16
import static org.testng.Assert.assertFalse;
17
import static org.testng.Assert.assertNotNull;
18
import static org.testng.Assert.assertTrue;
19
import static org.testng.Assert.fail;
20

  
21
import java.util.concurrent.CancellationException;
22
import java.util.concurrent.TimeoutException;
23

  
24

  
25
import org.artofsolving.jodconverter.ReflectionUtils;
26
import org.artofsolving.jodconverter.office.ManagedOfficeProcess;
27
import org.artofsolving.jodconverter.office.PooledOfficeManager;
28
import org.artofsolving.jodconverter.office.PooledOfficeManagerSettings;
29
import org.artofsolving.jodconverter.office.OfficeConnection;
30
import org.artofsolving.jodconverter.office.UnoUrl;
31
import org.artofsolving.jodconverter.office.OfficeException;
32
import org.artofsolving.jodconverter.office.OfficeProcess;
33
import org.testng.annotations.Test;
34

  
35
@Test(groups="integration")
36
public class PooledOfficeManagerTest {
37

  
38
    private static final UnoUrl CONNECTION_MODE = UnoUrl.socket(2002);
39
    private static final long RESTART_WAIT_TIME = 2 * 1000;
40

  
41
    public void executeTask() throws Exception {
42
        PooledOfficeManager officeManager = new PooledOfficeManager(CONNECTION_MODE);
43
        ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) ReflectionUtils.getPrivateField(officeManager, "managedOfficeProcess");
44
        OfficeProcess process = (OfficeProcess) ReflectionUtils.getPrivateField(managedOfficeProcess, "process");
45
        OfficeConnection connection = (OfficeConnection) ReflectionUtils.getPrivateField(managedOfficeProcess, "connection");
46
        
47
        officeManager.start();
48
        assertTrue(process.isRunning());
49
        assertTrue(connection.isConnected());
50
        
51
        MockOfficeTask task = new MockOfficeTask();
52
        officeManager.execute(task);
53
        assertTrue(task.isCompleted());
54
        
55
        officeManager.stop();
56
        assertFalse(connection.isConnected());
57
        assertFalse(process.isRunning());
58
        assertEquals(process.getExitCode(0, 0), 0);
59
    }
60

  
61
    public void restartAfterCrash() throws Exception {
62
        final PooledOfficeManager officeManager = new PooledOfficeManager(CONNECTION_MODE);
63
        ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) ReflectionUtils.getPrivateField(officeManager, "managedOfficeProcess");
64
        OfficeProcess process = (OfficeProcess) ReflectionUtils.getPrivateField(managedOfficeProcess, "process");
65
        OfficeConnection connection = (OfficeConnection) ReflectionUtils.getPrivateField(managedOfficeProcess, "connection");
66
        assertNotNull(connection);
67
        
68
        officeManager.start();
69
        assertTrue(process.isRunning());
70
        assertTrue(connection.isConnected());
71
        
72
        new Thread() {
73
            public void run() {
74
                MockOfficeTask badTask = new MockOfficeTask(10 * 1000);
75
                try {
76
                    officeManager.execute(badTask);
77
                    fail("task should be cancelled");
78
                    //FIXME being in a separate thread the test won't actually fail
79
                } catch (OfficeException officeException) {
80
                    assertTrue(officeException.getCause() instanceof CancellationException);
81
                }
82
            }
83
        }.start();
84
        Thread.sleep(500);
85
        Process underlyingProcess = (Process) ReflectionUtils.getPrivateField(process, "process");
86
        assertNotNull(underlyingProcess);
87
        underlyingProcess.destroy();  // simulate crash
88

  
89
        Thread.sleep(RESTART_WAIT_TIME);
90
        assertTrue(process.isRunning());
91
        assertTrue(connection.isConnected());
92

  
93
        MockOfficeTask goodTask = new MockOfficeTask();
94
        officeManager.execute(goodTask);
95
        assertTrue(goodTask.isCompleted());
96

  
97
        officeManager.stop();
98
        assertFalse(connection.isConnected());
99
        assertFalse(process.isRunning());
100
        assertEquals(process.getExitCode(0, 0), 0);
101
    }
102

  
103
    public void restartAfterTaskTimeout() throws Exception {
104
        PooledOfficeManagerSettings configuration = new PooledOfficeManagerSettings(CONNECTION_MODE);
105
        configuration.setTaskExecutionTimeout(1500L);
106
        final PooledOfficeManager officeManager = new PooledOfficeManager(configuration);
107
        
108
        ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) ReflectionUtils.getPrivateField(officeManager, "managedOfficeProcess");
109
        OfficeProcess process = (OfficeProcess) ReflectionUtils.getPrivateField(managedOfficeProcess, "process");
110
        OfficeConnection connection = (OfficeConnection) ReflectionUtils.getPrivateField(managedOfficeProcess, "connection");
111
        assertNotNull(connection);
112
        
113
        officeManager.start();
114
        assertTrue(process.isRunning());
115
        assertTrue(connection.isConnected());
116
        
117
        MockOfficeTask longTask = new MockOfficeTask(2000);
118
        try {
119
            officeManager.execute(longTask);
120
            fail("task should be timed out");
121
        } catch (OfficeException officeException) {
122
            assertTrue(officeException.getCause() instanceof TimeoutException);
123
        }
124

  
125
        Thread.sleep(RESTART_WAIT_TIME);
126
        assertTrue(process.isRunning());
127
        assertTrue(connection.isConnected());
128

  
129
        MockOfficeTask goodTask = new MockOfficeTask();
130
        officeManager.execute(goodTask);
131
        assertTrue(goodTask.isCompleted());
132

  
133
        officeManager.stop();
134
        assertFalse(connection.isConnected());
135
        assertFalse(process.isRunning());
136
        assertEquals(process.getExitCode(0, 0), 0);
137
    }
138

  
139
    public void restartWhenMaxTasksPerProcessReached() throws Exception {
140
        PooledOfficeManagerSettings configuration = new PooledOfficeManagerSettings(CONNECTION_MODE);
141
        configuration.setMaxTasksPerProcess(3);
142
        final PooledOfficeManager officeManager = new PooledOfficeManager(configuration);
143
        
144
        ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) ReflectionUtils.getPrivateField(officeManager, "managedOfficeProcess");
145
        OfficeProcess process = (OfficeProcess) ReflectionUtils.getPrivateField(managedOfficeProcess, "process");
146
        OfficeConnection connection = (OfficeConnection) ReflectionUtils.getPrivateField(managedOfficeProcess, "connection");
147
        assertNotNull(connection);
148
        
149
        officeManager.start();
150
        assertTrue(process.isRunning());
151
        assertTrue(connection.isConnected());
152
        
153
        for (int i = 0; i < 3; i++) {
154
            MockOfficeTask task = new MockOfficeTask();
155
            officeManager.execute(task);
156
            assertTrue(task.isCompleted());
157
            int taskCount = (Integer) ReflectionUtils.getPrivateField(officeManager, "taskCount");
158
            assertEquals(taskCount, i + 1);
159
        }
160

  
161
        MockOfficeTask task = new MockOfficeTask();
162
        officeManager.execute(task);
163
        assertTrue(task.isCompleted());
164
        int taskCount = (Integer) ReflectionUtils.getPrivateField(officeManager, "taskCount");
165
        assertEquals(taskCount, 0);  //FIXME should be 1 to be precise
166

  
167
        officeManager.stop();
168
        assertFalse(connection.isConnected());
169
        assertFalse(process.isRunning());
170
        assertEquals(process.getExitCode(0, 0), 0);
171
    }
172

  
173
}
0 174

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/office/OfficeUtilsTest.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter.office;
14

  
15
import static org.artofsolving.jodconverter.office.OfficeUtils.toUrl;
16
import static org.testng.Assert.assertEquals;
17

  
18
import java.io.File;
19

  
20
public class OfficeUtilsTest {
21

  
22
    public void testToUrl() {
23
        //TODO create separate tests for Windows
24
        assertEquals(toUrl(new File("/tmp/document.odt")), "file:///tmp/document.odt");
25
        assertEquals(toUrl(new File("/tmp/document with spaces.odt")), "file:///tmp/document%20with%20spaces.odt");
26
    }
27

  
28
}
0 29

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/office/MockOfficeTask.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter.office;
14

  
15
import static org.artofsolving.jodconverter.office.OfficeUtils.*;
16

  
17
import org.artofsolving.jodconverter.office.OfficeContext;
18
import org.artofsolving.jodconverter.office.OfficeException;
19
import org.artofsolving.jodconverter.office.OfficeTask;
20

  
21
import com.sun.star.beans.PropertyValue;
22
import com.sun.star.frame.XComponentLoader;
23
import com.sun.star.lang.XComponent;
24
import com.sun.star.util.XCloseable;
25

  
26
public class MockOfficeTask implements OfficeTask {
27

  
28
    private long delayTime = 0L;
29

  
30
    private boolean completed = false;
31

  
32
    public MockOfficeTask() {
33
        // default
34
    }
35

  
36
    public MockOfficeTask(long delayTime) {
37
        this.delayTime = delayTime;
38
    }
39

  
40
    public void execute(OfficeContext context) throws OfficeException {
41
        XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
42
        assert loader != null : "desktop object is null";
43
        try {
44
            PropertyValue[] arguments = new PropertyValue[] { property("Hidden", true) };
45
            XComponent document = loader.loadComponentFromURL("private:factory/swriter", "_blank", 0, arguments);
46
            if (delayTime > 0) {
47
                Thread.sleep(delayTime);
48
            }
49
            cast(XCloseable.class, document).close(true);
50
            completed = true;
51
        } catch (Exception exception) {
52
            throw new OfficeException("failed to create document", exception);
53
        }
54
    }
55

  
56
    public boolean isCompleted() {
57
        return completed;
58
    }
59

  
60
}
0 61

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/document/DumpJsonDefaultDocumentFormatRegistry.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter.document;
14

  
15
import java.lang.reflect.Field;
16
import java.util.LinkedHashMap;
17
import java.util.List;
18
import java.util.Map;
19

  
20

  
21
import org.artofsolving.jodconverter.ReflectionUtils;
22
import org.json.JSONArray;
23
import org.json.JSONException;
24
import org.json.JSONObject;
25

  
26
/**
27
 * Exectable class that dumps a JSON version of the {@link DefaultDocumentFormatRegistry}
28
 */
29
class DumpJsonDefaultDocumentFormatRegistry {
30

  
31
    private static class SortedJsonObject extends JSONObject {
32
         public SortedJsonObject() {
33
             try {
34
                 Field field = JSONObject.class.getDeclaredField("myHashMap");
35
                 field.setAccessible(true);
36
                 field.set(this, new LinkedHashMap<String,Object>());
37
             } catch (Exception exception) {
38
                 // pass; will not be sorted
39
             }
40
        }
41
    }
42

  
43
    private static JSONObject toJson(DocumentFormat format) throws JSONException {
44
        JSONObject jsonFormat = new SortedJsonObject();
45
        jsonFormat.put("name", format.getName());
46
        jsonFormat.put("extension", format.getExtension());
47
        jsonFormat.put("mediaType", format.getMediaType());
48
        if (format.getInputFamily() != null) {
49
            jsonFormat.put("inputFamily", format.getInputFamily().name());
50
        }
51
        if (format.getLoadProperties() != null) {
52
            jsonFormat.put("loadProperties", toJson(format.getLoadProperties()));
53
        }
54
        if (format.getStorePropertiesByFamily() != null) {
55
            JSONObject jsonStorePropertiesByFamily = new SortedJsonObject();
56
            for (Map.Entry<DocumentFamily,Map<String,?>> entry : format.getStorePropertiesByFamily().entrySet()) {
57
                jsonStorePropertiesByFamily.put(entry.getKey().name(), toJson(entry.getValue()));
58
            }
59
            jsonFormat.put("storePropertiesByFamily", jsonStorePropertiesByFamily);
60
        }
61
        return jsonFormat;
62
    }
63

  
64
    @SuppressWarnings("unchecked")
65
    private static JSONObject toJson(Map<String,?> properties) throws JSONException {
66
        JSONObject jsonProperties = new SortedJsonObject();
67
        for (Map.Entry<String,?> entry : properties.entrySet()) {
68
            if (entry.getValue() instanceof Map) {
69
                Map<String,?> jsonValue = (Map<String,?>) entry.getValue();
70
                jsonProperties.put(entry.getKey(), toJson(jsonValue));
71
            } else {
72
                jsonProperties.put(entry.getKey(), entry.getValue());
73
            }
74
        }
75
        return jsonProperties;
76
    }
77

  
78
    public static void main(String[] args) throws Exception {
79
        DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
80
        @SuppressWarnings("unchecked")
81
        List<DocumentFormat> formats = (List<DocumentFormat>) ReflectionUtils.getPrivateField(SimpleDocumentFormatRegistry.class, registry, "documentFormats");
82
        JSONArray array = new JSONArray();
83
        for (DocumentFormat format : formats) {
84
            array.put(toJson(format));
85
        }
86
        System.out.println(array.toString(2));
87
    }
88

  
89
}
0 90

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/document/JsonDocumentFormatRegistryTest.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter.document;
14

  
15
import static org.testng.Assert.*;
16

  
17
import java.io.IOException;
18
import java.io.InputStream;
19

  
20
import org.apache.commons.io.IOUtils;
21
import org.artofsolving.jodconverter.document.DocumentFamily;
22
import org.artofsolving.jodconverter.document.DocumentFormat;
23
import org.artofsolving.jodconverter.document.DocumentFormatRegistry;
24
import org.artofsolving.jodconverter.document.JsonDocumentFormatRegistry;
25

  
26
import org.json.JSONException;
27
import org.testng.annotations.Test;
28

  
29
@Test
30
public class JsonDocumentFormatRegistryTest {
31

  
32
    public void readJsonRegistry() throws JSONException, IOException {
33
        InputStream input = getClass().getResourceAsStream("/document-formats.js");
34
        DocumentFormatRegistry registry = null;
35
        try {
36
            registry = new JsonDocumentFormatRegistry(input);
37
        } finally {
38
            IOUtils.closeQuietly(input);
39
        }
40
        DocumentFormat odt = registry.getFormatByExtension("odt");
41
        assertNotNull(odt);
42
        assertNotNull(odt.getStoreProperties(DocumentFamily.TEXT));
43
    }
44

  
45
}
0 46

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/process/ProcessManagerTest.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter.process;
14

  
15
import static org.testng.Assert.assertEquals;
16
import static org.testng.Assert.assertFalse;
17

  
18
import org.artofsolving.jodconverter.ReflectionUtils;
19
import org.artofsolving.jodconverter.util.PlatformUtils;
20
import org.testng.SkipException;
21
import org.testng.annotations.Test;
22

  
23
@Test
24
public class ProcessManagerTest {
25

  
26
    public void linuxProcessManager() throws Exception {
27
        if (!PlatformUtils.isLinux()) {
28
            throw new SkipException("LinuxProcessManager can only be tested on Linux");
29
        }
30

  
31
        ProcessManager processManager = new LinuxProcessManager();
32
        Process process = new ProcessBuilder("sleep", "5s").start();
33
        ProcessQuery query = new ProcessQuery("sleep", "5s");
34
        
35
        long pid = processManager.findPid(query);
36
        assertFalse(pid == ProcessManager.PID_NOT_FOUND);
37
        Integer javaPid = (Integer) ReflectionUtils.getPrivateField(process, "pid");
38
        assertEquals(pid, javaPid.longValue());
39
        
40
        processManager.kill(process, pid);
41
        assertEquals(processManager.findPid(query), ProcessManager.PID_NOT_FOUND);
42
    }
43

  
44
    public void sigarProcessManager() throws Exception {
45
        ProcessManager processManager = new SigarProcessManager();
46
        Process process = new ProcessBuilder("sleep", "5s").start();
47
        ProcessQuery query = new ProcessQuery("sleep", "5s");
48
        
49
        long pid = processManager.findPid(query);
50
        assertFalse(pid == ProcessManager.PID_NOT_FOUND);
51
        if (PlatformUtils.isLinux()) {
52
            Integer javaPid = (Integer) ReflectionUtils.getPrivateField(process, "pid");
53
            assertEquals(pid, javaPid.longValue());
54
        }
55

  
56
        processManager.kill(process, pid);
57
        assertEquals(processManager.findPid(query), ProcessManager.PID_NOT_FOUND);
58
    }
59

  
60
}
0 61

  
tmp/org.txm.jodconverter.core/src/test/java/org/artofsolving/jodconverter/OfficeDocumentConverterFunctionalTest.java (revision 622)
1
//
2
// JODConverter - Java OpenDocument Converter
3
// Copyright 2004-2012 Mirko Nasato and contributors
4
//
5
// JODConverter is Open Source software, you can redistribute it and/or
6
// modify it under either (at your option) of the following licenses
7
//
8
// 1. The GNU Lesser General Public License v3 (or later)
9
//    -> http://www.gnu.org/licenses/lgpl-3.0.txt
10
// 2. The Apache License, Version 2.0
11
//    -> http://www.apache.org/licenses/LICENSE-2.0.txt
12
//
13
package org.artofsolving.jodconverter;
14

  
15
import static org.testng.Assert.assertNotNull;
16
import static org.testng.Assert.assertTrue;
17

  
18
import java.io.File;
19
import java.io.FilenameFilter;
20
import java.io.IOException;
21
import java.util.Set;
22

  
23
import org.apache.commons.io.FilenameUtils;
24
import org.artofsolving.jodconverter.document.DocumentFormat;
25
import org.artofsolving.jodconverter.document.DocumentFormatRegistry;
26
import org.artofsolving.jodconverter.office.OfficeManager;
27
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
28
import org.testng.annotations.Test;
29

  
30
@Test(groups="functional")
31
public class OfficeDocumentConverterFunctionalTest {
32

  
33
    public void runAllPossibleConversions() throws IOException {
34
        OfficeManager officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager();
35
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
36
        DocumentFormatRegistry formatRegistry = converter.getFormatRegistry();
37
        
38
        officeManager.start();
39
        try {
40
            File dir = new File("src/test/resources/documents");
41
            File[] files = dir.listFiles(new FilenameFilter() {
42
            	public boolean accept(File dir, String name) {
43
            		return !name.startsWith(".");
44
            	}
45
            });
46
			for (File inputFile : files) {
47
                String inputExtension = FilenameUtils.getExtension(inputFile.getName());
48
                DocumentFormat inputFormat = formatRegistry.getFormatByExtension(inputExtension);
49
                assertNotNull(inputFormat, "unknown input format: " + inputExtension);
50
                Set<DocumentFormat> outputFormats = formatRegistry.getOutputFormats(inputFormat.getInputFamily());
51
                for (DocumentFormat outputFormat : outputFormats) {
52
                    File outputFile = File.createTempFile("test", "." + outputFormat.getExtension());
53
                    outputFile.deleteOnExit();
54
                    System.out.printf("-- converting %s to %s... ", inputFormat.getExtension(), outputFormat.getExtension());
55
                    converter.convert(inputFile, outputFile, outputFormat);
56
                    System.out.printf("done.\n");
57
                    assertTrue(outputFile.isFile() && outputFile.length() > 0);
58
                    //TODO use file detection to make sure outputFile is in the expected format
59
                }
60
            }
61
        } finally {
62
            officeManager.stop();
63
        }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff