OldZargoFilePersister.java

1
/* $Id: OldZargoFilePersister.java 19907 2012-12-30 13:06:01Z closettop_nightlybuild $
2
 *****************************************************************************
3
 * Copyright (c) 2009-2012 Contributors - see below
4
 * All rights reserved. This program and the accompanying materials
5
 * are made available under the terms of the Eclipse Public License v1.0
6
 * which accompanies this distribution, and is available at
7
 * http://www.eclipse.org/legal/epl-v10.html
8
 *
9
 * Contributors:
10
 *    tfmorris
11
 *    Michiel van der Wulp
12
 *****************************************************************************
13
 *
14
 * Some portions of this file was previously release using the BSD License:
15
 */
16
17
// Copyright (c) 1996-2008 The Regents of the University of California. All
18
// Rights Reserved. Permission to use, copy, modify, and distribute this
19
// software and its documentation without fee, and without a written
20
// agreement is hereby granted, provided that the above copyright notice
21
// and this paragraph appear in all copies.  This software program and
22
// documentation are copyrighted by The Regents of the University of
23
// California. The software program and documentation are supplied "AS
24
// IS", without any accompanying services from The Regents. The Regents
25
// does not warrant that the operation of the program will be
26
// uninterrupted or error-free. The end-user understands that the program
27
// was developed for research purposes and is advised not to rely
28
// exclusively on the program for any reason.  IN NO EVENT SHALL THE
29
// UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
30
// SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
31
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
32
// THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
33
// SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
34
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
35
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
36
// PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
37
// CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
38
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
39
40
package org.argouml.persistence;
41
42
import java.io.BufferedWriter;
43
import java.io.File;
44
import java.io.FileNotFoundException;
45
import java.io.FileOutputStream;
46
import java.io.IOException;
47
import java.io.OutputStreamWriter;
48
import java.util.ArrayList;
49
import java.util.Collection;
50
import java.util.Hashtable;
51
import java.util.logging.Level;
52
import java.util.logging.Logger;
53
import java.util.zip.ZipEntry;
54
import java.util.zip.ZipOutputStream;
55
56
import org.argouml.application.helpers.ApplicationVersion;
57
import org.argouml.i18n.Translator;
58
import org.argouml.kernel.Project;
59
import org.argouml.kernel.ProjectMember;
60
import org.argouml.ocl.OCLExpander;
61
import org.argouml.util.FileConstants;
62
import org.tigris.gef.ocl.TemplateReader;
63
64
/**
65
 * To persist to and from zargo (zipped file) storage containing seperate
66
 * xmi, argo, plgml and todo zip entries.
67
 * @author Bob Tarling
68
 * TODO: Review use of this against ZargoFilePersister - Bob
69
 */
70
class OldZargoFilePersister extends ZargoFilePersister {
71
    /**
72
     * Logger.
73
     */
74
    private static final Logger LOG =
75
        Logger.getLogger(OldZargoFilePersister.class.getName());
76
77
    /**
78
     * This is the old version of the ArgoUML tee file which does not contain
79
     * the detail of member elements.
80
     */
81
    private static final String ARGO_MINI_TEE =
82
        "/org/argouml/persistence/argo.tee";
83
84
    /**
85
     * The constructor.
86
     */
87
    public OldZargoFilePersister() {
88
    }
89
90
    /*
91
     * @see org.argouml.persistence.AbstractFilePersister#getDesc()
92
     */
93
    protected String getDesc() {
94 1 1. getDesc : mutated return of Object value for org/argouml/persistence/OldZargoFilePersister::getDesc to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return Translator.localize("combobox.filefilter.zargo");
95
    }
96
97
    /*
98
     * @see org.argouml.persistence.AbstractFilePersister#isSaveEnabled()
99
     */
100
    public boolean isSaveEnabled() {
101 1 1. isSaveEnabled : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return true;
102
    }
103
104
    /**
105
     * Save the project in ".zargo" format.
106
     *
107
     * @param file
108
     *            The file to write.
109
     * @param project
110
     *            the project to save
111
     * @throws SaveException
112
     *             when anything goes wrong
113
     * @throws InterruptedException     if the thread is interrupted
114
     *
115
     * @see org.argouml.persistence.ProjectFilePersister#save(
116
     *      org.argouml.kernel.Project, java.io.File)
117
     */
118
    public void doSave(Project project, File file) throws SaveException,
119
    InterruptedException {
120
121
        /* Retain the previous project file even when the save operation
122
         * crashes in the middle. Also create a backup file after saving. */
123
        boolean doSafeSaves = useSafeSaves();
124
125
        ProgressMgr progressMgr = new ProgressMgr();
126 1 1. doSave : removed call to org/argouml/persistence/AbstractFilePersister$ProgressMgr::setNumberOfPhases → NO_COVERAGE
        progressMgr.setNumberOfPhases(4);
127 1 1. doSave : removed call to org/argouml/persistence/AbstractFilePersister$ProgressMgr::nextPhase → NO_COVERAGE
        progressMgr.nextPhase();
128
129
        File lastArchiveFile = new File(file.getAbsolutePath() + "~");
130
        File tempFile = null;
131
132 1 1. doSave : negated conditional → NO_COVERAGE
        if (doSafeSaves) {
133
            try {
134
                tempFile = createTempFile(file);
135
            } catch (FileNotFoundException e) {
136
                throw new SaveException(Translator.localize(
137
                        "optionpane.save-project-exception-cause1"), e);
138
            } catch (IOException e) {
139
                throw new SaveException(Translator.localize(
140
                        "optionpane.save-project-exception-cause2"), e);
141
            }
142
        }
143
144
        BufferedWriter writer = null;
145
        try {
146
147 1 1. doSave : removed call to org/argouml/kernel/Project::setFile → NO_COVERAGE
            project.setFile(file);
148 1 1. doSave : removed call to org/argouml/kernel/Project::setVersion → NO_COVERAGE
            project.setVersion(ApplicationVersion.getVersion());
149 1 1. doSave : removed call to org/argouml/kernel/Project::setPersistenceVersion → NO_COVERAGE
            project.setPersistenceVersion(PERSISTENCE_VERSION);
150
151
            ZipOutputStream stream =
152
                new ZipOutputStream(new FileOutputStream(file));
153
            writer =
154
                new BufferedWriter(new OutputStreamWriter(stream, "UTF-8"));
155
156
            // Save the .argo entry
157
            // TODO: Cyclic dependency with PersistenceManager
158
            // move PersistenceManager..getProjectBaseName() someplace else
159
            ZipEntry zipEntry =
160
                new ZipEntry(PersistenceManager.getInstance()
161
                            .getProjectBaseName(project)
162
                        + FileConstants.UNCOMPRESSED_FILE_EXT);
163 1 1. doSave : removed call to java/util/zip/ZipOutputStream::putNextEntry → NO_COVERAGE
            stream.putNextEntry(zipEntry);
164
165
            Hashtable templates =
166
                TemplateReader.getInstance().read(ARGO_MINI_TEE);
167
            OCLExpander expander = new OCLExpander(templates);
168 1 1. doSave : removed call to org/argouml/ocl/OCLExpander::expand → NO_COVERAGE
            expander.expand(writer, project);
169
170 1 1. doSave : removed call to java/io/BufferedWriter::flush → NO_COVERAGE
            writer.flush();
171
172 1 1. doSave : removed call to java/util/zip/ZipOutputStream::closeEntry → NO_COVERAGE
            stream.closeEntry();
173
174
            int counter = 0;
175
            int size = project.getMembers().size();
176
            Collection<String> names = new ArrayList<String>();
177 3 1. doSave : changed conditional boundary → NO_COVERAGE
2. doSave : Changed increment from 1 to -1 → NO_COVERAGE
3. doSave : negated conditional → NO_COVERAGE
            for (int i = 0; i < size; i++) {
178
                ProjectMember projectMember = project.getMembers().get(i);
179 1 1. doSave : negated conditional → NO_COVERAGE
                if (!(projectMember.getType().equalsIgnoreCase("xmi"))) {
180
                    
181
                    LOG.log(Level.INFO,
182
                            "Saving member: {0}", projectMember.getZipName());
183
184
                    String name = projectMember.getZipName();
185
                    String originalName = name;
186 1 1. doSave : negated conditional → NO_COVERAGE
                    while (names.contains(name)) {
187
                        /* Issue 4806 explains why we need this! */
188 1 1. doSave : Changed increment from 1 to -1 → NO_COVERAGE
                        name = ++counter + originalName;
189
                    }
190
                    names.add(name);
191 1 1. doSave : removed call to java/util/zip/ZipOutputStream::putNextEntry → NO_COVERAGE
                    stream.putNextEntry(new ZipEntry(name));
192
                    MemberFilePersister persister =
193
                        getMemberFilePersister(projectMember);
194 1 1. doSave : removed call to org/argouml/persistence/MemberFilePersister::save → NO_COVERAGE
                    persister.save(projectMember, stream);
195 1 1. doSave : removed call to java/util/zip/ZipOutputStream::flush → NO_COVERAGE
                    stream.flush();
196 1 1. doSave : removed call to java/util/zip/ZipOutputStream::closeEntry → NO_COVERAGE
                    stream.closeEntry();
197
                }
198
            }
199
200 3 1. doSave : changed conditional boundary → NO_COVERAGE
2. doSave : Changed increment from 1 to -1 → NO_COVERAGE
3. doSave : negated conditional → NO_COVERAGE
            for (int i = 0; i < size; i++) {
201
                ProjectMember projectMember = project.getMembers().get(i);
202 1 1. doSave : negated conditional → NO_COVERAGE
                if (projectMember.getType().equalsIgnoreCase("xmi")) {
203
                    
204
                    LOG.log(Level.INFO,
205
                            "Saving member of type: {0}",
206
                            projectMember.getType());
207
208 1 1. doSave : removed call to java/util/zip/ZipOutputStream::putNextEntry → NO_COVERAGE
                    stream.putNextEntry(
209
                            new ZipEntry(projectMember.getZipName()));
210
                    OldModelMemberFilePersister persister =
211
                        new OldModelMemberFilePersister();
212 1 1. doSave : removed call to org/argouml/persistence/OldModelMemberFilePersister::save → NO_COVERAGE
                    persister.save(projectMember, stream);
213 1 1. doSave : removed call to java/util/zip/ZipOutputStream::flush → NO_COVERAGE
                    stream.flush();
214
                }
215
            }
216
217 1 1. doSave : negated conditional → NO_COVERAGE
            if (doSafeSaves) {
218
                // if save did not raise an exception
219
                // and name+"#" exists move name+"#" to name+"~"
220
                // this is the correct backup file
221 1 1. doSave : negated conditional → NO_COVERAGE
                if (lastArchiveFile.exists()) {
222
                    lastArchiveFile.delete();
223
                }
224 2 1. doSave : negated conditional → NO_COVERAGE
2. doSave : negated conditional → NO_COVERAGE
                if (tempFile.exists() && !lastArchiveFile.exists()) {
225
                    tempFile.renameTo(lastArchiveFile);
226
                }
227 1 1. doSave : negated conditional → NO_COVERAGE
                if (tempFile.exists()) {
228
                    tempFile.delete();
229
                }
230
            }
231
232 1 1. doSave : removed call to org/argouml/persistence/AbstractFilePersister$ProgressMgr::nextPhase → NO_COVERAGE
            progressMgr.nextPhase();
233
234
        } catch (Exception e) {
235
            LOG.log(Level.SEVERE, "Exception occured during save attempt", e);
236
            try {
237 1 1. doSave : removed call to java/io/BufferedWriter::close → NO_COVERAGE
                writer.close();
238
            } catch (Exception ex) {
239
                // Do nothing.
240
            }
241
242 1 1. doSave : negated conditional → NO_COVERAGE
            if (doSafeSaves) {
243
                // frank: in case of exception
244
                // delete name and mv name+"#" back to name if name+"#" exists
245
                // this is the "rollback" to old file
246
                file.delete();
247
                tempFile.renameTo(file);
248
            }
249
            // we have to give a message to user and set the system to unsaved!
250
            throw new SaveException(e);
251
        }
252
253
        try {
254 1 1. doSave : removed call to java/io/BufferedWriter::close → NO_COVERAGE
            writer.close();
255
        } catch (IOException ex) {
256
            LOG.log(Level.SEVERE, "Failed to close save output writer", ex);
257
        }
258
    }
259
260
    /**
261
     * The .zargo save format is able to save. We must override
262
     * UmlFilePersister which has turned this off (suggests a need for some
263
     * refactoring here)
264
     * @see org.argouml.persistence.AbstractFilePersister#isSaveEnabled()
265
     *
266
     * @return boolean
267
     */
268
    public boolean isLoadEnabled() {
269 1 1. isLoadEnabled : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return false;
270
    }
271
}

Mutations

94

1.1
Location : getDesc
Killed by : none
mutated return of Object value for org/argouml/persistence/OldZargoFilePersister::getDesc to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

101

1.1
Location : isSaveEnabled
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

126

1.1
Location : doSave
Killed by : none
removed call to org/argouml/persistence/AbstractFilePersister$ProgressMgr::setNumberOfPhases → NO_COVERAGE

127

1.1
Location : doSave
Killed by : none
removed call to org/argouml/persistence/AbstractFilePersister$ProgressMgr::nextPhase → NO_COVERAGE

132

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

147

1.1
Location : doSave
Killed by : none
removed call to org/argouml/kernel/Project::setFile → NO_COVERAGE

148

1.1
Location : doSave
Killed by : none
removed call to org/argouml/kernel/Project::setVersion → NO_COVERAGE

149

1.1
Location : doSave
Killed by : none
removed call to org/argouml/kernel/Project::setPersistenceVersion → NO_COVERAGE

163

1.1
Location : doSave
Killed by : none
removed call to java/util/zip/ZipOutputStream::putNextEntry → NO_COVERAGE

168

1.1
Location : doSave
Killed by : none
removed call to org/argouml/ocl/OCLExpander::expand → NO_COVERAGE

170

1.1
Location : doSave
Killed by : none
removed call to java/io/BufferedWriter::flush → NO_COVERAGE

172

1.1
Location : doSave
Killed by : none
removed call to java/util/zip/ZipOutputStream::closeEntry → NO_COVERAGE

177

1.1
Location : doSave
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : doSave
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

179

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

186

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

188

1.1
Location : doSave
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

191

1.1
Location : doSave
Killed by : none
removed call to java/util/zip/ZipOutputStream::putNextEntry → NO_COVERAGE

194

1.1
Location : doSave
Killed by : none
removed call to org/argouml/persistence/MemberFilePersister::save → NO_COVERAGE

195

1.1
Location : doSave
Killed by : none
removed call to java/util/zip/ZipOutputStream::flush → NO_COVERAGE

196

1.1
Location : doSave
Killed by : none
removed call to java/util/zip/ZipOutputStream::closeEntry → NO_COVERAGE

200

1.1
Location : doSave
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : doSave
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

202

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

208

1.1
Location : doSave
Killed by : none
removed call to java/util/zip/ZipOutputStream::putNextEntry → NO_COVERAGE

212

1.1
Location : doSave
Killed by : none
removed call to org/argouml/persistence/OldModelMemberFilePersister::save → NO_COVERAGE

213

1.1
Location : doSave
Killed by : none
removed call to java/util/zip/ZipOutputStream::flush → NO_COVERAGE

217

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

221

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

224

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

227

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

232

1.1
Location : doSave
Killed by : none
removed call to org/argouml/persistence/AbstractFilePersister$ProgressMgr::nextPhase → NO_COVERAGE

237

1.1
Location : doSave
Killed by : none
removed call to java/io/BufferedWriter::close → NO_COVERAGE

242

1.1
Location : doSave
Killed by : none
negated conditional → NO_COVERAGE

254

1.1
Location : doSave
Killed by : none
removed call to java/io/BufferedWriter::close → NO_COVERAGE

269

1.1
Location : isLoadEnabled
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.32