@Test public void submitReturnsBadOutputWhenCodeIsBad() throws Exception { PowerMockito.when(ClientData.userDataExists()).thenReturn(true); paste.setParameter("path", "/hieno/path"); String result = paste.parseData(paste.call()).get(); assertTrue(result.contains(pasteUrl)); }
@Test(expected = ProtocolException.class) public void throwsErrorIfCourseCantBeRetrieved() throws Exception { PowerMockito.when(ClientData.userDataExists()).thenReturn(true); PowerMockito.when(ClientData.getCurrentCourse(Mockito.anyString())) .thenReturn(Optional.<Course>absent()); paste.data.put("path", "asdsad"); paste.checkData(); }
/** * *************************************************************** @Description - A method used to * execute input commands entered by the user. It also will display command input errors if * needed. * * @param The text command entered in by the user through the command line. @Returns - (N/A) * <p>************************************************************** */ @Override public boolean processCommand(String input) { // --- Variable Declarations ---------------------------// // determines if system should exit program boolean exit = false; /* The identifier received for the command to be executed. */ char commandID; /* Keeps track of document modifications for saving. */ boolean docChange = false; // --- Main Routine -------------------------------------// // Error check that the command is valid. if ((input.length() > 1 && input.charAt(1) == ' ') || input.length() == 1) { error = false; // Extract the first character from the command. commandID = input.charAt(0); commandID = Character.toLowerCase(commandID); // Go through and match a command to run. switch (commandID) { // Insert before current line. case 'b': saved = false; command = new BeforeLine(); command.executeCommand(input, CurrentLine); break; // Insert after current line. case 'i': saved = false; command = new AfterLine(); command.executeCommand(input, CurrentLine); break; // Move current line down a position. case 'm': command = new MoveIndicatorDown(); error = command.executeCommand(input, CurrentLine); break; // Move current line up 1 position. case 'u': command = new MoveIndicatorUp(); error = command.executeCommand(input, CurrentLine); break; // Remove the current line. case 'r': saved = false; command = new RemoveCurrentLine(); command.executeCommand(input, CurrentLine); break; // Display the project buffer. case 'd': clearConsole(); command = new DisplayAllLines(); displayOff = command.executeCommand(input, CurrentLine); break; // Clear and remove all lines in buffer. case 'c': if (!saved) { System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); System.out.println( " The document has yet to be saved." + "\n Would you like to save it before you exit?[y/n]"); System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); try { String save = inputReader.readLine(); save = save.toLowerCase(); if (save.equals("y")) { try { System.out.println("\nEnter save command: s <filename>:"); save = inputReader.readLine(); command = new SaveFile(); error = command.executeCommand(save, CurrentLine); } catch (IOException e) { System.out.println( "!!> Oops, There was an error " + "trying to run this command."); e.printStackTrace(); } } } catch (IOException e) { System.out.println("!!> Oops, There was an error " + "trying to run this command."); e.printStackTrace(); } } saved = false; command = new ClearLines(); command.executeCommand(input, CurrentLine); break; // Save file contents to a file directory. case 's': command = new SaveFile(); error = command.executeCommand(input, CurrentLine); saved = true; break; // Load a files contents from a directory. case 'l': if (!saved) { System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); System.out.println( " The document has yet to be saved." + "\n Would you like to save it before you exit?[y/n]"); System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); try { String save = inputReader.readLine(); save = save.toLowerCase(); if (save.equals("y")) { try { System.out.println("\nEnter save command: s <filename>:"); save = inputReader.readLine(); command = new SaveFile(); error = command.executeCommand(save, CurrentLine); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); error = true; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); error = true; } } command = new ClearLines(); command.executeCommand(input, CurrentLine); command = new LoadFile(); error = command.executeCommand(input, CurrentLine); break; // Display list of commands. case 'h': error = true; command = new Help(); command.executeCommand(input, CurrentLine); break; // Exit the editor. case 'x': if (input.equals("x")) { // Check and see if the document has yet to be saved. if (!saved) { System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); System.out.println( " The document has yet to be saved." + "\n Would you like to save it before you exit?[y/n]"); System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); try { String save = inputReader.readLine(); save = save.toLowerCase(); if (save.equals("y")) { try { System.out.println("\nEnter save command: s <filename>:"); save = inputReader.readLine(); command = new SaveFile(); error = command.executeCommand(save, CurrentLine); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Good bye!"); exit = true; } else { error = true; System.out.println( "Incorrect Input Format. " + "\nShould be of form: [command] " + "[data_entry]"); System.out.print("\n: "); } break; // Insert after last line. case 'e': command = new AfterLast(); command.executeCommand(input, CurrentLine); break; // Cut command. case 'o': Cut cutCommand = new Cut(); cutCommand.executeCommand(input, CurrentLine, clipBoardLine, documentLineCount); break; case 'p': Paste pasteCommand = new Paste(); pasteCommand.executeCommand(input, CurrentLine, CurrentLine); break; // Invalid command entered. default: error = true; System.out.println( "Incorrect Input Format. " + "\nShould be of form: [command] [data_entry]"); System.out.print("\n: "); break; } if (displayOff) { error = true; System.out.print(": "); } // Update document flag data. documentLineCount = textData.size(); // Update the edit flag. docChange = commandID == 's' ? true : docChange; } // Nothing was entered into the buffer. else { error = true; System.out.println("Command does not match an existing command, enter 'h' for help. "); System.out.print(": "); } return exit; }
@Test(expected = ProtocolException.class) public void throwsErrorIfNoCredentialsPresent() throws Exception { paste.data.put("path", "asdsad"); ClientData.clearUserData(); paste.checkData(); }
@Test(expected = ProtocolException.class) public void checkDataFailIfNoAuth() throws Exception { ClientData.clearUserData(); paste.checkData(); }
/** Check that if user didn't give correct data, data checking fails. */ @Test(expected = ProtocolException.class) public void testCheckDataFail() throws Exception { paste.checkData(); }
/** Check that data checking success. */ @Test public void testCheckDataSuccess() throws ProtocolException, IOException { PowerMockito.when(ClientData.userDataExists()).thenReturn(true); paste.setParameter("path", "/home/tmccli/uolevipuistossa"); paste.checkData(); }
@Override public void valueChanged(TreeSelectionEvent e) { // TODO Auto-generated method stub try { currentPath = tree.getSelectionPath(); currentnode = (FileTreeNode) e.getPath().getLastPathComponent(); currentfile = currentnode.GetFile(); tree.scrollPathToVisible(currentPath); // 确保路径中所有的路径组件均展开(最后一个路径组件除外)并滚动,以便显示curpath路径标识的节点 addressText.setText(currentnode.GetFile().getAbsolutePath()); // 将点击的节点的路径在地址栏中显示出来 isTable = false; if (!currentfile.getName().equals("Desktop")) { Vector files = treeview.GetAll(currentnode.GetFile()); // 获取点击节点下的所有文件 tablemodel.addAllFiles(files); // 将这些文件加到列表中 upbtn.setEnabled(true); FileNewFolder.setEnabled(true); NewFolder.setEnabled(true); FileNewFile.setEnabled(true); FileNature.setEnabled(true); FileRename.setEnabled(true); EditCut.setEnabled(true); EditCopy.setEnabled(true); EditPaste.setEnabled(true); NewFile.setEnabled(true); Nature.setEnabled(true); Rename.setEnabled(true); Cut.setEnabled(true); Copy.setEnabled(true); Paste.setEnabled(true); if (!(currentnode.getParent().toString()).equals(File.listRoots()) && (currentnode.toString() != "网络") && (currentnode.toString() != "库") && (currentnode.toString() != "家庭组")) // 不是根节点 { FileDelete.setEnabled(true); Delete.setEnabled(true); } else { FileDelete.setEnabled(false); Delete.setEnabled(true); } } else { downbtn.setEnabled(false); upbtn.setEnabled(false); FileNewFolder.setEnabled(false); // 菜单栏中的菜单选项 FileNewFile.setEnabled(false); FileNature.setEnabled(false); FileRename.setEnabled(false); FileDelete.setEnabled(false); EditCut.setEnabled(false); EditCopy.setEnabled(false); NewFolder.setEnabled(false); // 右键菜单中的菜单选项 NewFile.setEnabled(false); Nature.setEnabled(false); Rename.setEnabled(false); Delete.setEnabled(false); Cut.setEnabled(false); Copy.setEnabled(false); currentnode = (TreeNode) treemodel.getRoot(); tablemodel.addAllFiles(treeview.GetRoot()); } } catch (Exception ee) { JOptionPane.showMessageDialog(null, "读取文件错误!", "Error", JOptionPane.ERROR_MESSAGE); ee.printStackTrace(); } }
public static String post(String name, Paste paste, ReportFormat format, ExpireDate expireDate) { if (name == null) name = ""; String report_url = ""; try { URL urls = new URL(POST_URL); HttpURLConnection conn = (HttpURLConnection) urls.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.setRequestMethod("POST"); conn.addRequestProperty("Content-type", "application/x-www-form-urlencoded"); conn.setInstanceFollowRedirects(false); conn.setDoOutput(true); OutputStream out = conn.getOutputStream(); byte[] data = ("api_option=paste" + "&api_dev_key=" + URLEncoder.encode(API_KEY, "utf-8") + "&api_paste_code=" + URLEncoder.encode(paste.toString(), "utf-8") + "&api_paste_private=" + URLEncoder.encode("1", "utf-8") + // 1 = unlisted, 0 = public, 2 = private (need to be logged in for that) "&api_paste_name=" + URLEncoder.encode(name, "utf-8") + "&api_paste_expire_date=" + URLEncoder.encode(expireDate.toString(), "utf-8") + "&api_paste_format=" + URLEncoder.encode(format.toString(), "utf-8") + "&api_user_key=" + URLEncoder.encode("", "utf-8")) .getBytes(); out.write(data); out.flush(); out.close(); if (conn.getResponseCode() == 200) { InputStream receive = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(receive)); String line; StringBuffer response = new StringBuffer(); while ((line = reader.readLine()) != null) { response.append(line); response.append("\r\n"); } reader.close(); String result = response.toString().trim(); if (!result.contains("http://")) { report_url = "Failed to post! (returned result: " + result; } else { report_url = result.trim(); } } else { report_url = "Failed to post!"; } } catch (Exception e) { e.printStackTrace(); } return report_url; }